4 * Copyright 2004,2005 $ThePhpWikiProgrammingTeam
5 * Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
9 * PhpWiki is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * PhpWiki is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * This plugin requires an action page (default: ModeratedPage)
26 * and provides delayed execution of restricted actions,
27 * after a special moderators request. Usually by email.
28 * http://mywiki/SomeModeratedPage?action=ModeratedPage&id=kdclcr78431zr43uhrn&pass=approve
30 * Not yet ready! part 3/3 is missing: The moderator approve/reject methods.
32 * See http://phpwiki.org/PageModeration
36 require_once 'lib/WikiPlugin.php';
38 class WikiPlugin_ModeratedPage
42 return _("ModeratedPage");
44 function getDescription () {
45 return _("Support moderated pages");
47 function getDefaultArguments() {
48 return array('page' => '[pagename]',
49 'moderators' => false,
50 'require_level' => false, // 1=bogo
51 'require_access' => 'edit,remove,change',
57 function run($dbi, $argstr, &$request, $basepage) {
58 $args = $this->getArgs($argstr, $request);
60 // Handle moderation request from urls sent by email
61 if (!empty($args['id']) and !empty($args['pass'])) {
63 return $this->error(sprintf(_("A required argument '%s' is missing."), 'page'));
65 $page = $dbi->getPage($args['page']);
66 if ($moderated = $page->get("moderated")) {
67 if (array_key_exists($args['id'], $moderated['data'])) {
68 $moderation = $moderated['data'][$args['id']];
71 if ($request->isPost()) {
72 $button = $request->getArg('ModeratedPage');
73 if (isset($button['reject']))
74 return $this->reject($request, $args, $moderation);
75 elseif (isset($button['approve']))
76 return $this->approve($request, $args, $moderation);
78 return $this->error("Wrong button pressed");
80 if ($args['pass'] == 'approve')
81 return $this->approve($request, $args, $moderation);
82 elseif ($args['pass'] == 'reject')
83 return $this->reject($request, $args, $moderation);
85 return $this->error("Wrong pass ".$args['pass']);
87 return $this->error("Wrong id ".htmlentities($args['id']));
95 * resolve moderators and require_access (not yet) from actionpage plugin argstr
97 function resolve_argstr(&$request, $argstr) {
98 $args = $this->getArgs($argstr);
99 $group = $request->getGroup();
100 if (empty($args['moderators'])) {
101 $admins = $group->getSpecialMembersOf(GROUP_ADMIN);
102 // email or usernames?
103 $args['moderators'] = array_merge($admins, array(ADMIN_USER));
105 // resolve possible group names
106 $moderators = explode(',', $args['moderators']);
107 for ($i=0; $i < count($moderators); $i++) {
108 $members = $group->getMembersOf($moderators[$i]);
109 if (!empty($members)) {
110 array_splice($moderators, $i, 1, $members);
113 if (!$moderators) $moderators = array(ADMIN_USER);
114 $args['moderators'] = $moderators;
116 //resolve email for $args['moderators']
117 $page = $request->getPage();
119 foreach ($args['moderators'] as $userid) {
122 require_once 'lib/MailNotify.php';
123 $mail = new MailNotify($page->getName());
125 list($args['emails'], $args['moderators']) =
126 $mail->getPageChangeEmails(array($page->getName() => $users));
128 if (!empty($args['require_access'])) {
129 $args['require_access'] = preg_split("/\s*,\s*/", $args['require_access']);
130 if (empty($args['require_access']))
131 unset($args['require_access']);
133 if ($args['require_level'] !== false) {
134 $args['require_level'] = (integer) $args['require_level'];
137 unset($args['page']);
138 unset($args['pass']);
143 * Handle client-side moderation change request.
144 * Hook called on the lock action, if moderation metadata already exists.
146 function lock_check(&$request, &$page, $moderated) {
147 $action_page = $request->getPage(_("ModeratedPage"));
148 $status = $this->getSiteStatus($request, $action_page);
149 if (is_array($status)) {
150 if (empty($status['emails'])) {
151 trigger_error(_("ModeratedPage: No emails for the moderators defined"),
155 $page->set('moderation', array('status' => $status));
156 return $this->notice(
157 fmt("ModeratedPage status update:\n Moderators: '%s'\n require_access: '%s'",
158 join(',', $status['moderators']), $status['require_access']));
160 $page->set('moderation', false);
161 return $this->notice(HTML($status,
162 fmt("'%s' is no ModeratedPage anymore.", $page->getName())));
167 * Handle client-side moderation change request by the user.
168 * Hook called on the lock action, if moderation metadata should be added.
169 * Need to store the the plugin args (who, when) in the page meta-data
171 function lock_add(&$request, &$page, &$action_page) {
172 $status = $this->getSiteStatus($request, $action_page);
173 if (is_array($status)) {
174 if (empty($status['emails'])) {
175 // We really should present such warnings prominently.
176 trigger_error(_("ModeratedPage: No emails for the moderators defined"),
180 $page->set('moderation', array('status' => $status));
181 return $this->notice(
182 fmt("ModeratedPage status update: '%s' is now a ModeratedPage.\n Moderators: '%s'\n require_access: '%s'",
183 $page->getName(), join(',', $status['moderators']), $status['require_access']));
190 function notice($msg) {
191 return HTML::div(array('class' => 'wiki-edithelp'), $msg);
194 function generateId() {
197 for ($i = 1; $i <= 25; $i++) {
198 $r = function_exists('mt_rand') ? mt_rand(55, 90) : rand(55, 90);
199 $s .= chr(($r < 65) ? ($r-17) : $r);
201 $len = $r = function_exists('mt_rand') ? mt_rand(15, 25) : rand(15,25);
202 return substr(base64_encode($s),3,$len);
206 * Handle client-side POST moderation request on any moderated page.
207 * if ($page->get('moderation')) WikiPlugin_ModeratedPage::handler(...);
208 * return false if not handled (pass through), true if handled and displayed.
210 function handler(&$request, &$page) {
211 $action = $request->getArg('action');
212 $moderated = $page->get('moderated');
213 // cached version, need re-lock of each page to update moderators
214 if (!empty($moderated['status']))
215 $status = $moderated['status'];
217 $action_page = $request->getPage(_("ModeratedPage"));
218 $status = $this->getSiteStatus($request, $action_page);
219 $moderated['status'] = $status;
221 if (empty($status['emails'])) {
222 trigger_error(_("ModeratedPage: No emails for the moderators defined"),
227 if (!empty($status['require_access'])
228 and !in_array(action2access($action), $status['require_access']))
229 return false; // allow and fall through, not moderated
230 if (!empty($status['require_level'])
231 and $request->_user->_level >= $status['require_level'])
232 return false; // allow and fall through, not moderated
233 // else all post actions are moderated by default
234 if (1) /* or in_array($action, array('edit','remove','rename')*/ {
235 $id = $this->generateId();
236 while (!empty($moderated['data'][$id])) $id = $this->generateId(); // avoid duplicates
237 $moderated['id'] = $id; // overwrite current id
238 $tempuser = $request->_user;
239 if (isset($tempuser->_HomePagehandle))
240 unset($tempuser->_HomePagehandle);
241 $moderated['data'][$id] = array( // add current request
242 'timestamp' => time(),
243 'userid' => $request->_user->getId(),
244 'args' => $request->getArgs(),
245 'user' => serialize( $tempuser ),
247 $this->_tokens['CONTENT'] =
248 HTML::div(array('class' => 'wikitext'),
249 fmt("%s: action forwarded to moderator %s",
251 join(", ", $status['moderators'])
254 require_once 'lib/MailNotify.php';
255 $pagename = $page->getName();
256 $mailer = new MailNotify($pagename);
257 $subject = "[".WIKI_NAME.'] '.$action._(": ")._("ModeratedPage").' '.$pagename;
258 $content = "You are approved as Moderator of the ".WIKI_NAME. " wiki.\n".
259 "Someone wanted to edit a moderated page, which you have to approve or reject.\n\n".
260 $action._(": ")._("ModeratedPage").' '.$pagename."\n"
261 //. serialize($moderated['data'][$id])
262 ."\n<".WikiURL($pagename, array('action' => _("ModeratedPage"),
263 'id' => $id, 'pass' => 'approve'), 1).">"
264 ."\n<".WikiURL($pagename, array('action' => _("ModeratedPage"),
265 'id' => $id, 'pass' => 'reject'), 1).">\n";
266 $mailer->emails = $mailer->userids = $status['emails'];
267 $mailer->from = $request->_user->_userid;
268 if ($mailer->sendMail($subject, $content, "Moderation notice")) {
269 $page->set('moderated', $moderated);
270 return false; // pass thru
273 $page->set('moderated', $moderated);
274 //FIXME: This msg gets lost on the edit redirect
275 trigger_error(_("ModeratedPage Notification Error: Couldn't send email"),
284 * Handle admin-side moderation resolve.
285 * We might have to convert the GET to a POST request to continue
286 * with the left-over stored request.
287 * Better we display a post form for verification.
289 function approve(&$request, $args, &$moderation) {
290 if ($request->isPost()) {
291 // this is unsafe because we dont know if it will succeed. but we tried.
292 $this->cleanup_and_notify($request, $args, $moderation);
293 // start from scratch, dispatch the action as in lib/main to the action handler
294 $request->discardOutput();
295 $oldargs = $request->args;
296 $olduser = $request->_user;
297 $request->args = $moderation['args'];
298 $request->_user->_userid = $moderation['userid']; // keep current perms but fake the id.
299 // TODO: fake author ip also
300 extract($request->args);
301 $method = "action_$action";
302 if (method_exists($request, $method)) {
303 $request->{$method}();
305 elseif ($page = $this->findActionPage($action)) {
306 $this->actionpage($page);
309 $this->finish(fmt("%s: Bad action", $action));
311 // now we are gone and nobody brings us back here.
313 //$moderated['data'][$id]->args->action+edit(array)+...
314 // timestamp,user(obj)+userid
315 // handle $moderated['data'][$id]['args']['action']
317 return $this->_approval_form($request, $args, $moderation, 'approve');
322 * Handle admin-side moderation resolve.
324 function reject(&$request, $args, &$moderation) {
325 // check id, delete action
326 if ($request->isPost()) {
327 // clean up and notify the requestor. Mabye: store and revert to have a diff later on?
328 $this->cleanup_and_notify($request, $args, $moderation);
330 return $this->_approval_form($request, $args, $moderation, 'reject');
334 function cleanup_and_notify (&$request, $args, &$moderation) {
335 $pagename = $moderation['args']['pagename'];
336 $page = $request->_dbi->getPage($pagename);
337 $pass = $args['pass']; // accept or reject
338 $reason = $args['reason']; // summary why
339 $user = $moderation['args']['user'];
340 $action = $moderation['args']['action'];
342 unset($moderation['data'][$id]);
343 unset($moderation['id']);
344 $page->set('moderation', $moderation);
346 // TODO: Notify the user, only if the user has an email:
347 if ($email = $user->getPref('email')) {
348 $action_page = $request->getPage(_("ModeratedPage"));
349 $status = $this->getSiteStatus($request, $action_page);
350 require_once 'lib/MailNotify.php';
351 $mailer = new MailNotify($pagename);
352 $subject = "[".WIKI_NAME."] $pass $action "._("ModeratedPage")._(": ").$pagename;
353 $mailer->from = $request->_user->UserFrom();
354 $content = sprintf(_("%s approved your wiki action from %s"),
355 $mailer->from,CTime($moderation['timestamp']))
359 ."\n<".WikiURL($pagename).">\n";
360 $mailer->emails = $mailer->userids = $email;
361 $mailer->sendMail($subject, $content, "Approval notice");
365 function _approval_form(&$request, $args, $moderation, $pass='approve') {
366 $header = HTML::h3(_("Please approve or reject this request:"));
368 $loader = new WikiPluginLoader();
369 $BackendInfo = $loader->getPlugin("_BackendInfo");
370 $table = HTML::table(array('border' => 1,
372 'cellspacing' => 0));
375 if ($moderation['args']['action'] == 'edit') {
376 $pagename = $moderation['args']['pagename'];
377 $p = $request->_dbi->getPage($pagename);
378 $rev = $p->getCurrentRevision(true);
379 $curr_content = $rev->getPackedContent();
380 $new_content = $moderation['args']['edit']['content'];
381 include_once 'lib/difflib.php';
382 $diff2 = new Diff($curr_content, $new_content);
383 $fmt = new UnifiedDiffFormatter(/*$context_lines*/);
384 $diff = $pagename . " Current Version " .
385 Iso8601DateTime($p->get('mtime')) . "\n";
386 $diff .= $pagename . " Edited Version " .
387 Iso8601DateTime($moderation['timestamp']) . "\n";
388 $diff .= $fmt->format($diff2);
390 $content->pushContent($BackendInfo->_showhash("Request",
391 array('User' => $moderation['userid'],
392 'When' => CTime($moderation['timestamp']),
393 'Pagename' => $pagename,
394 'Action' => $moderation['args']['action'],
395 'Diff' => HTML::pre($diff))));
396 $content_dbg = $table;
398 $BackendInfo->_fixupData($myargs);
399 $content_dbg->pushContent($BackendInfo->_showhash("raw request args", $myargs));
400 $BackendInfo->_fixupData($moderation);
401 $content_dbg->pushContent($BackendInfo->_showhash("raw moderation data", $moderation));
402 $reason = HTML::div(_("Reason: "), HTML::textarea(array('name' => 'reason')));
403 $approve = Button('submit:ModeratedPage[approve]', _("Approve"),
404 $pass == 'approve' ? 'wikiadmin' : 'button');
405 $reject = Button('submit:ModeratedPage[reject]', _("Reject"),
406 $pass == 'reject' ? 'wikiadmin' : 'button');
407 $args['action'] = _("ModeratedPage");
408 return HTML::form(array('action' => $request->getPostURL(),
411 $content, HTML::p(""), $content_dbg,
415 : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
417 $pass == 'approve' ? HTML::p($approve, $reject)
418 : HTML::p($reject, $approve));
422 * Get the side-wide ModeratedPage status, reading the action-page args.
423 * Who are the moderators? What actions should be moderated?
425 function getSiteStatus(&$request, &$action_page) {
426 $loader = new WikiPluginLoader();
427 $rev = $action_page->getCurrentRevision();
428 $content = $rev->getPackedContent();
429 list($pi) = explode("\n", $content, 2); // plugin ModeratedPage must be first line!
430 if ($parsed = $loader->parsePI($pi)) {
431 $plugin =& $parsed[1];
432 if ($plugin->getName() != _("ModeratedPage"))
433 return $this->error(sprintf(_("<<ModeratedPage ... >> not found in first line of %s"),
434 $action_page->getName()));
435 if (!$action_page->get('locked'))
436 return $this->error(sprintf(_("%s is not locked!"),
437 $action_page->getName()));
438 return $plugin->resolve_argstr($request, $parsed[2]);
440 return $this->error(sprintf(_("<<ModeratedPage ... >> not found in first line of %s"),
441 $action_page->getName()));
451 // c-hanging-comment-ender-p: nil
452 // indent-tabs-mode: nil