3 /* Copyright (C) 2006-2007 Reini Urban
4 * Copyright (C) 2009 Marc-Etienne Vargenau, Alcatel-Lucent
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with PhpWiki; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Handle the pagelist pref[notifyPages] logic for users
25 * and notify => hash ( page => (userid => userhash) ) for pages.
26 * Generate notification emails.
28 * We add WikiDB handlers and register ourself there:
29 * onChangePage, onDeletePage, onRenamePage
30 * Administrative actions:
31 * [Watch] WatchPage - add a page, or delete watch handlers into the users
32 * pref[notifyPages] slot.
33 * My WatchList - view or edit list/regex of pref[notifyPages].
34 * EMailConfirm methods: send and verify
39 * ? handle emailed confirmation links (EmailSignup, ModeratedPage)
45 if (!defined("MAILER_LOG"))
47 define("MAILER_LOG", 'c:/wikimail.log');
49 define("MAILER_LOG", '/var/log/wikimail.log');
53 function MailNotify($pagename) {
54 $this->pagename = $pagename; /* which page */
55 $this->emails = array(); /* to which addresses */
56 $this->userids = array(); /* corresponding array of displayed names,
57 don't display the email addresses */
58 /* From: from whom the mail appears to be */
59 $this->from = $this->fromId();
64 if (defined('GFORGE') and GFORGE) {
65 return $request->_user->getId();
67 return $request->_user->getId() . '@' . $request->get('REMOTE_HOST');
71 function userEmail($userid, $doverify = true) {
74 // Disable verification of emails for corporate env.
75 if (defined('GFORGE') and GFORGE) {
79 $u = $request->getUser();
80 if ($u->UserName() == $userid) { // lucky: current user
81 $prefs = $u->getPreferences();
82 $email = $prefs->get('email');
83 // do a dynamic emailVerified check update
84 if ($doverify and !$request->_prefs->get('emailVerified'))
86 } else { // not current user
87 if (ENABLE_USER_NEW) {
88 $u = WikiUser($userid);
92 $u = new WikiUser($request, $userid);
93 $prefs = $u->getPreferences();
95 $email = $prefs->get('email');
96 if ($doverify and !$prefs->get('emailVerified')) {
104 * getPageChangeEmails($notify)
105 * @param $notify: hash ( page => (userid => userhash) )
107 * unique array of ($emails, $userids)
109 function getPageChangeEmails($notify) {
111 $emails = array(); $userids = array();
112 foreach ($notify as $page => $users) {
113 if (glob_match($page, $this->pagename)) {
116 $curuser = $request->getUser();
117 $curusername = $curuser->UserName();
118 $curuserprefs = $curuser->getPreferences();
119 $curuserprefsemail = $curuserprefs->get('email');
120 $ownModifications = $curuserprefs->get('ownModifications');
121 $majorModificationsOnly = $curuserprefs->get('majorModificationsOnly');
123 foreach ($users as $userid => $user) {
125 $usermail = $user['email'];
127 if (($usermail == $curuserprefsemail)
128 and ($ownModifications)) {
129 // It's my own modification
130 // and I do not want to receive it
134 if ($majorModificationsOnly) {
135 $backend = &$request->_dbi->_backend;
136 $version = $backend->get_latest_version($this->pagename);
137 $versiondata = $backend->get_versiondata($this->pagename, $version, true);
138 if ($versiondata['is_minor_edit']) {
139 // It's a minor modification
140 // and I do not want to receive it
145 if (!$user) { // handle the case for ModeratePage:
146 // no prefs, just userid's.
147 $emails[] = $this->userEmail($userid, false);
148 $userids[] = $userid;
150 if (!empty($user['verified']) and !empty($user['email'])) {
151 $emails[] = $user['email'];
152 $userids[] = $userid;
153 } elseif (!empty($user['email'])) {
154 // do a dynamic emailVerified check update
155 $email = $this->userEmail($userid, true);
157 $notify[$page][$userid]['verified'] = 1;
158 $request->_dbi->set('notify', $notify);
160 $userids[] = $userid;
163 // ignore verification
166 if (!in_array($user['email'], $emails))
167 $emails[] = $user['email'];
174 $this->emails = array_unique($emails);
175 $this->userids = array_unique($userids);
176 return array($this->emails, $this->userids);
179 function sendMail($subject, $content,
184 if (!DEBUG and $silent === 0)
186 $emails = $this->emails;
188 // Do not send if modification is from Gforge admin
189 if (defined('GFORGE') and GFORGE) {
190 if ($from == ADMIN_USER) {
194 if (!$notice) $notice = _("PageChange Notification of %s");
195 $ok = mail(($to = array_shift($emails)),
196 "[".WIKI_NAME."] ".$subject,
197 $subject."\n".$content,
198 "From: $from\r\nBcc: ".join(',', $emails)
200 if (MAILER_LOG and is_writable(MAILER_LOG)) {
201 $f = fopen(MAILER_LOG, "a");
202 fwrite($f, "\n\nX-MailSentOK: " . $ok ? 'OK' : 'FAILED');
204 global $ErrorManager;
205 // get last error message
206 $last_err = $ErrorManager->_postponed_errors[count($ErrorHandler->_postponed_errors)-1];
207 fwrite($f, "\nX-MailFailure: " . $last_err);
209 fwrite($f, "\nDate: " . CTime());
210 fwrite($f, "\nSubject: $subject");
211 fwrite($f, "\nFrom: $from");
212 fwrite($f, "\nTo: $to");
213 fwrite($f, "\nBcc: ".join(',', $emails));
214 fwrite($f, "\n\n". $content);
219 trigger_error(sprintf($notice, $this->pagename)
221 . sprintf(_("sent to %s"), join(',',$this->userids)),
225 trigger_error(sprintf($notice, $this->pagename)
227 . sprintf(_("Error: Couldn't send %s to %s"),
228 $subject."\n".$content, join(',',$this->userids)),
235 * Send udiff for a changed page to multiple users.
236 * See rename and remove methods also
238 function sendPageChangeNotification(&$wikitext, $version, &$meta) {
242 if (@is_array($request->_deferredPageChangeNotification)) {
243 // collapse multiple changes (loaddir) into one email
244 $request->_deferredPageChangeNotification[] =
245 array($this->pagename, $this->emails, $this->userids);
248 $backend = &$request->_dbi->_backend;
249 $subject = _("Page change").' '.urlencode($this->pagename);
250 $previous = $backend->get_previous_version($this->pagename, $version);
251 if (!isset($meta['mtime'])) $meta['mtime'] = time();
253 $difflink = WikiURL($this->pagename, array('action'=>'diff'), true);
254 $cache = &$request->_dbi->_cache;
255 $this_content = explode("\n", $wikitext);
256 $prevdata = $cache->get_versiondata($this->pagename, $previous, true);
257 if (empty($prevdata['%content']))
258 $prevdata = $backend->get_versiondata($this->pagename, $previous, true);
259 $other_content = explode("\n", $prevdata['%content']);
261 include_once("lib/difflib.php");
262 $diff2 = new Diff($other_content, $this_content);
263 //$context_lines = max(4, count($other_content) + 1,
264 // count($this_content) + 1);
265 $fmt = new UnifiedDiffFormatter(/*$context_lines*/);
266 $content = $this->pagename . " " . $previous . " " .
267 Iso8601DateTime($prevdata['mtime']) . "\n";
268 $content .= $this->pagename . " " . $version . " " .
269 Iso8601DateTime($meta['mtime']) . "\n";
270 $content .= $fmt->format($diff2);
273 $difflink = WikiURL($this->pagename,array(),true);
274 $content = $this->pagename . " " . $version . " " .
275 Iso8601DateTime($meta['mtime']) . "\n";
276 $content .= _("New page");
278 $content .= $wikitext;
280 $editedby = sprintf(_("Edited by: %s"), $this->from);
281 //$editedby = sprintf(_("Edited by: %s"), $meta['author']);
282 $this->sendMail($subject,
283 $editedby."\n".$difflink."\n\n".$content);
287 * Support mass rename / remove (not yet tested)
289 function sendPageRenameNotification ($to, &$meta) {
292 if (@is_array($request->_deferredPageRenameNotification)) {
293 $request->_deferredPageRenameNotification[] =
294 array($this->pagename, $to, $meta, $this->emails, $this->userids);
296 $pagename = $this->pagename;
297 //$editedby = sprintf(_("Edited by: %s"), $meta['author']) . ' ' . $meta['author_id'];
298 $editedby = sprintf(_("Edited by: %s"), $this->from);
299 $subject = sprintf(_("Page rename %s to %s"), urlencode($pagename), urlencode($to));
300 $link = WikiURL($to, true);
301 $this->sendMail($subject,
302 $editedby."\n".$link."\n\n"."Renamed $pagename to $to");
309 function onChangePage (&$wikidb, &$wikitext, $version, &$meta) {
311 if (!isa($GLOBALS['request'],'MockRequest')) {
312 $notify = $wikidb->get('notify');
313 /* Generate notification emails? */
314 if (!empty($notify) and is_array($notify)) {
315 if (empty($this->pagename))
316 $this->pagename = $meta['pagename'];
317 // TODO: Should be used for ModeratePage and RSS2 Cloud xml-rpc also.
318 $this->getPageChangeEmails($notify);
319 if (!empty($this->emails)) {
320 $result = $this->sendPageChangeNotification($wikitext, $version, $meta);
327 function onDeletePage (&$wikidb, $pagename) {
329 /* Generate notification emails? */
330 if (! $wikidb->isWikiPage($pagename) and !isa($GLOBALS['request'],'MockRequest')) {
331 $notify = $wikidb->get('notify');
332 if (!empty($notify) and is_array($notify)) {
333 //TODO: deferr it (quite a massive load if you remove some pages).
334 $this->getPageChangeEmails($notify);
335 if (!empty($this->emails)) {
336 $editedby = sprintf(_("Removed by: %s"), $this->from); // Todo: host_id
337 //$emails = join(',', $this->emails);
338 $subject = sprintf(_("Page removed %s"), urlencode($pagename));
339 $page = $wikidb->getPage($pagename);
340 $rev = $page->getCurrentRevision(true);
341 $content = $rev->getPackedContent();
342 $result = $this->sendMail($subject,
343 $editedby."\n"."Deleted $pagename"."\n\n".$content);
347 //How to create a RecentChanges entry with explaining summary? Dynamically
349 $page = $this->getPage($pagename);
350 $current = $page->getCurrentRevision();
351 $meta = $current->_data;
352 $version = $current->getVersion();
353 $meta['summary'] = _("removed");
354 $page->save($current->getPackedContent(), $version + 1, $meta);
359 function onRenamePage (&$wikidb, $oldpage, $new_pagename) {
361 if (!isa($GLOBALS['request'], 'MockRequest')) {
362 $notify = $wikidb->get('notify');
363 if (!empty($notify) and is_array($notify)) {
364 $this->getPageChangeEmails($notify);
365 if (!empty($this->emails)) {
366 $newpage = $wikidb->getPage($new_pagename);
367 $current = $newpage->getCurrentRevision();
368 $meta = $current->_data;
369 $this->pagename = $oldpage;
370 $result = $this->sendPageRenameNotification($new_pagename, $meta);
377 * Send mail to user and store the cookie in the db
378 * wikiurl?action=ConfirmEmail&id=bla
380 function sendEmailConfirmation ($email, $userid) {
381 $id = rand_ascii_readable(16);
382 $wikidb = $GLOBALS['request']->getDbh();
383 $data = $wikidb->get('ConfirmEmail');
384 while(!empty($data[$id])) { // id collision
385 $id = rand_ascii_readable(16);
387 $subject = WIKI_NAME . " " . _("e-mail address confirmation");
388 $ip = $request->get('REMOTE_HOST');
389 $expire_date = time() + 7*86400;
390 $content = fmt("Someone, probably you from IP address %s, has registered an
391 account \"%s\" with this e-mail address on %s.
393 To confirm that this account really does belong to you and activate
394 e-mail features on %s, open this link in your browser:
398 If this is *not* you, don't follow the link. This confirmation code
400 $ip, $userid, WIKI_NAME, WIKI_NAME,
401 WikiURL(HOME_PAGE, array('action' => 'ConfirmEmail',
404 CTime($expire_date));
405 $this->sendMail($subject, $content, "", true);
406 $data[$id] = array('email' => $email,
408 'expire' => $expire_date);
409 $wikidb->set('ConfirmEmail', $data);
413 function checkEmailConfirmation () {
415 $wikidb = $request->getDbh();
416 $data = $wikidb->get('ConfirmEmail');
417 $id = $request->getArg('id');
418 if (empty($data[$id])) { // id not found
419 return HTML(HTML::h1("Confirm E-mail address"),
420 HTML::h1("Sorry! Wrong URL"));
423 $userid = $data['userid'];
424 $email = $data['email'];
425 $u = $request->getUser();
426 if ($u->UserName() == $userid) { // lucky: current user (session)
427 $prefs = $u->getPreferences();
428 $request->_user->_level = WIKIAUTH_USER;
429 $request->_prefs->set('emailVerified', true);
430 } else { // not current user
431 if (ENABLE_USER_NEW) {
432 $u = WikiUser($userid);
433 $u->getPreferences();
434 $prefs = &$u->_prefs;
436 $u = new WikiUser($request, $userid);
437 $prefs = $u->getPreferences();
439 $u->_level = WIKIAUTH_USER;
440 $request->setUser($u);
441 $request->_prefs->set('emailVerified', true);
444 $wikidb->set('ConfirmEmail', $data);
445 return HTML(HTML::h1("Confirm E-mail address"),
446 HTML::p("Your e-mail address has now been confirmed."));
454 // c-hanging-comment-ender-p: nil
455 // indent-tabs-mode: nil