MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / wiki / ewiki / plugins / notify.php
blob7992c3886e0ff46b1af198d3956fe5f7c2fd9329
1 <?php
3 # The otherwise invisible markup [notify:you@there.net] will trigger a
4 # mail, whenever a page is changed. The TLD decides in which language
5 # the message will be delivered. One can also append the lang code after
6 # a comma or semicolon behind the mail address to set it explicitely:
7 # [notify:me@here.org,de] or [notify:you@there.net;eo]
9 # Nevertheless English will be used as the default automagically, if
10 # nothing else was specified, no need to worry about this.
12 # additional features:
13 # * diff inclusion
14 # * [notify:icq:123456789] - suddenly ICQ.com took the pager service down
16 # To include a diff, just set the following constant. Also use it to
17 # define the minimum number of changed bytes that are necessary to
18 # result in a notification mail. Only use it with Linux/UNIX.
20 define("EWIKI_NOTIFY_WITH_DIFF", 0); #-- set it to 100 or so
21 define("EWIKI_NOTIFY_SENDER",'ewiki');
24 #-- glue
25 $ewiki_plugins["edit_hook"][] = "ewiki_notify_edit_hook";
26 $ewiki_plugins["format_source"][] = "ewiki_format_remove_notify";
27 $ewiki_config["interwiki"]["notify"] = "mailto:";
30 #-- email message text ---------------------------------------------------
31 $ewiki_t["en"]["NOTIFY_SUBJECT"] = '"$id" was changed [notify:...]';
32 $ewiki_t["en"]["NOTIFY_BODY"] = <<<_END_OF_STRING
33 Hi,
35 A WikiPage has changed and you requested to be notified when this
36 happens. The changed page was '\$id' and can be found
37 at the following URL:
38 \$link
40 To stop messages like this please strip the [notify:...] with your address
41 from the page edit box at \$edit_link
43 (\$wiki_title on http://\$server/)
44 \$server_admin
45 _END_OF_STRING;
48 #-- translation.de
49 $ewiki_t["de"]["NOTIFY_SUBJECT"] = '"$id" wurde geändert [notify:...]';
50 $ewiki_t["de"]["NOTIFY_BODY"] = <<<_END_OF_STRING
51 Hi,
53 Eine WikiSeite hat sich geändert, und du wolltest ja unbedingt wissen,
54 wenn das passiert. Die geänderte Seite war '\$id' und
55 ist leicht zu finden unter folgender URL:
56 \$link
58 Wenn du diese Benachrichtigungen nicht mehr bekommen willst, solltest du
59 deine [notify:...]-Adresse aus der entsprechenden Edit-Box herauslöschen:
60 \$edit_link
62 (\$wiki_title auf http://\$server/)
63 \$server_admin
64 _END_OF_STRING;
67 #----------------------------------------------------------------------------
71 #-- implementatition
72 function ewiki_notify_edit_hook($id, $data, &$hidden_postdata) {
74 global $ewiki_t, $ewiki_plugins;
75 $ret_err = 0;
77 if (!isset($_REQUEST["save"])) {
78 return(false);
81 $mailto = ewiki_notify_links($data["content"], 0);
83 if (!count($mailto)) {
84 return(false);
87 #-- generate diff
88 $diff = "";
89 if (EWIKI_NOTIFY_WITH_DIFF && (DIRECTORY_SEPARATOR=="/")) {
91 #-- save page versions temporarily as files
92 $fn1 = EWIKI_TMP."/ewiki.tmp.notify.diff.".md5($data["content"]);
93 $fn2 = EWIKI_TMP."/ewiki.tmp.notify.diff.".md5($_REQUEST["content"]);
94 $f = fopen($fn1, "w");
95 fwrite($f, $data["content"]);
96 fclose($f);
97 $f = fopen($fn2, "w");
98 fwrite($f, $_REQUEST["content"]);
99 fclose($f);
100 #-- set mtime of the old one (GNU diff will report it)
101 touch($fn1, $data["lastmodified"]);
103 #-- get diff output, rm temp files
104 $diff_exe = "diff";
105 if ($f = popen("$diff_exe --normal --ignore-case --ignore-space-change $fn1 $fn2 2>&1 ", "r")) {
107 $diff .= fread($f, 16<<10);
108 pclose($f);
110 $diff_failed = !strlen($diff)
111 || (strpos($diff, "Files ") === 0);
113 #-- do not [notify:] if changes were minimal
114 if ((!$diff_failed) && (strlen($diff) < EWIKI_NOTIFY_WITH_DIFF)) {
115 #echo("WikiNotice: no notify, because too few changes (" .strlen($diff)." byte)\n");
116 $ret_err = 1;
119 $diff = "\n\n-----------------------------------------------------------------------------\n\n"
120 . $diff;
122 else {
123 $diff = "";
124 #echo("WikiWarning: diff failed in notify module\n");
127 unlink($fn1);
128 unlink($fn2);
130 if ($ret_err) {
131 return(false);
135 #-- separate addresses into (TLD) groups
136 $mailto_lang = array(
138 foreach ($mailto as $m) {
140 $lang = "";
142 #-- remove lang selection trailer
143 $m = strtok($m, ",");
144 if ($uu = strtok(",")) {
145 $lang = $uu;
147 $m = strtok($m, ";");
148 if ($uu = strtok(";")) {
149 $lang = $uu;
152 #-- else use TLD as language code
153 if (empty($lang)) {
154 $r = strrpos($m, ".");
155 $lang = substr($m, $r+1);
157 $lang = trim($lang);
159 #-- address mangling
160 $m = trim($m);
161 if (substr($m, 0, 4) == "icq:") {
162 $m = substr($m, 4) . "@pager.icq.com";
165 $mailto_lang[$lang][] = $m;
168 #-- go thru email address groups
169 foreach ($mailto_lang as $lang=>$a_mailto) {
171 $pref_langs = array_merge(array(
172 "$lang", "en"
173 ), $ewiki_t["languages"]);
175 ($server = $_SERVER["HTTP_HOST"]) or
176 ($server = $_SERVER["SERVER_NAME"]);
177 $s_4 = "http".($_SERVER['HTTPS'] == "on" ? 's':'')."://" . $server . $_SERVER["REQUEST_URI"];
178 $link = str_replace("edit/$id", "$id", $s_4);
180 $m_text = ewiki_t("NOTIFY_BODY", array(
181 "id" => $id,
182 "link" => $link,
183 "edit_link" => $s_4,
184 "server_admin" => $_SERVER["SERVER_ADMIN"],
185 "server" => $server,
186 "wiki_title" => EWIKI_PAGE_INDEX,
187 ), $pref_langs);
188 $m_text .= $diff;
190 $m_from = EWIKI_NOTIFY_SENDER."@$server";
191 $m_subject = ewiki_t("NOTIFY_SUBJECT", array(
192 "id" => $id,
193 ), $pref_langs);
195 $m_to = implode(", ", $a_mailto);
197 mail($m_to, $m_subject, $m_text, "From: \"$s_2\" <$m_from>\nX-Mailer: ErfurtWiki/".EWIKI_VERSION);
204 function ewiki_notify_links(&$source, $strip=1) {
205 $links = array();
206 $l = 0;
207 if (strlen($source) > 10)
208 while (($l = @strpos($source, "[notify:", $l)) !== false) {
209 $r = strpos($source, "]", $l);
210 $str = substr($source, $l, $r + 1 - $l);
211 if (!strpos("\n", $str)) {
212 $links[] = trim(substr($str, 8, -1));
213 if ($strip) {
214 $source = substr($source, 0, $l) . substr($source, $r + 1);
217 $l++;
219 return($links);
224 function ewiki_format_remove_notify(&$source) {
225 ewiki_notify_links($source, 1);