PageRenderTime 75ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/moderation.php

https://bitbucket.org/webop/webop-forum
PHP | 1073 lines | 534 code | 126 blank | 413 comment | 84 complexity | 9c8c05c499811ee8e035c92576476ed5 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // //
  4. // Copyright (C) 2010 Phorum Development Team //
  5. // http://www.phorum.org //
  6. // //
  7. // This program is free software. You can redistribute it and/or modify //
  8. // it under the terms of either the current Phorum License (viewable at //
  9. // phorum.org) or the Phorum License that was distributed with this file //
  10. // //
  11. // This program is distributed in the hope that it will be useful, //
  12. // but WITHOUT ANY WARRANTY, without even the implied warranty of //
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
  14. // //
  15. // You should have received a copy of the Phorum License //
  16. // along with this program. //
  17. // //
  18. ////////////////////////////////////////////////////////////////////////////////
  19. define('phorum_page','moderation');
  20. include_once("./common.php");
  21. include_once("./include/moderation_functions.php");
  22. include_once("./include/thread_info.php");
  23. include_once("./include/email_functions.php");
  24. if(!phorum_check_read_common()) {
  25. return;
  26. }
  27. // CSRF protection: we do not accept posting to this script,
  28. // when the browser does not include a Phorum signed token
  29. // in the request.
  30. phorum_check_posting_token();
  31. $PHORUM["DATA"]["MODERATOR"] =
  32. phorum_api_user_check_access(PHORUM_USER_ALLOW_MODERATE_MESSAGES);
  33. if(isset($_POST["thread"])) {
  34. $msgthd_id = (int)$_POST["thread"];
  35. } elseif(isset($PHORUM['args'][2])) {
  36. $msgthd_id = (int)$PHORUM['args'][2];
  37. } else {
  38. $msgthd_id = 0;
  39. }
  40. if(isset($_POST["mod_step"])) {
  41. $mod_step = (int)$_POST["mod_step"];
  42. } elseif(isset($PHORUM['args'][1])) {
  43. $mod_step = (int)$PHORUM['args'][1];
  44. } else {
  45. $mod_step = 0;
  46. }
  47. if(empty($msgthd_id) || !$PHORUM["DATA"]["MODERATOR"]) {
  48. phorum_return_to_list();
  49. }
  50. // If the user is not fully logged in, send him to the login page.
  51. // because moderation action can vary so much, the only safe bet is to send them
  52. // to the referrer if they are not fully logged in
  53. if(!$PHORUM["DATA"]["FULLY_LOGGEDIN"]){
  54. phorum_redirect_by_url(phorum_get_url(PHORUM_LOGIN_URL, "redir=".$_SERVER["HTTP_REFERER"]));
  55. exit();
  56. }
  57. // if we gave the user a confirmation form and they clicked No, send them back to the message
  58. if(isset($_POST["confirmation"]) && $_POST["confirmation"]==$PHORUM["DATA"]["LANG"]["No"]){
  59. if(isset($_POST["prepost"])) {
  60. // add some additional args
  61. $addcode = "";
  62. if(isset($_POST['moddays']) && is_numeric($_POST['moddays'])) {
  63. $addcode.="moddays=".$_POST['moddays'];
  64. }
  65. if(isset($_POST['onlyunapproved']) && is_numeric($_POST['onlyunapproved'])) {
  66. if(!empty($addcode))
  67. $addcode.=",";
  68. $addcode.="onlyunapproved=".$_POST['onlyunapproved'];
  69. }
  70. $url = phorum_get_url(PHORUM_CONTROLCENTER_URL,"panel=".PHORUM_CC_UNAPPROVED,$addcode);
  71. } else {
  72. $message = phorum_db_get_message($msgthd_id);
  73. $url = phorum_get_url(PHORUM_READ_URL, $message["thread"], $message["message_id"]);
  74. }
  75. phorum_redirect_by_url($url);
  76. exit();
  77. }
  78. $template="message";
  79. // set all our URL's
  80. phorum_build_common_urls();
  81. // make it possible to override this var in a hook
  82. $is_admin_user=$PHORUM["user"]["admin"];
  83. /*
  84. * [hook]
  85. * moderation
  86. *
  87. * [description]
  88. * This hook can be used for logging moderator actions. You can
  89. * use the <literal>$PHORUM</literal> array to retrieve additional info
  90. * like the moderating user's id and similar.<sbr/>
  91. * <sbr/>
  92. * The moderation step id is the variable <literal>$mod_step</literal>
  93. * that is used in <filename>moderation.php</filename>. Please read that
  94. * script to see what moderation steps are available and for what moderation
  95. * actions they stand.<sbr/>
  96. * <sbr/>
  97. * When checking the moderation step id for a certain step, always use
  98. * the contstants that are defined for this in
  99. * <filename>include/constants.php</filename>. The numerical value of this
  100. * id can change between Phorum releases.
  101. *
  102. * [category]
  103. * Moderation
  104. *
  105. * [when]
  106. * At the start of <filename>moderation.php</filename>
  107. *
  108. * [input]
  109. * The id of the moderation step which is run (read-only).
  110. *
  111. * [output]
  112. * Same as input.
  113. *
  114. * [example]
  115. * <hookcode>
  116. * function phorum_mod_foo_moderation ($mod_step)
  117. * {
  118. * global $PHORUM;
  119. *
  120. * // Update the last timestamp for the moderation step
  121. * $PHORUM["mod_foo"]["moderation_step_timestamps"][$mod_step] = time();
  122. * phorum_db_update_settings(array("mod_foo" => $PHORUM["mod_foo"]));
  123. *
  124. * return $mod_step;
  125. * }
  126. * </hookcode>
  127. */
  128. if (isset($PHORUM["hooks"]["moderation"]))
  129. phorum_hook("moderation", $mod_step);
  130. $invalidate_message_cache = array();
  131. switch ($mod_step) {
  132. case PHORUM_DELETE_MESSAGE: // this is a message delete
  133. if(count($_GET) && empty($_POST["thread"])){
  134. $args = array(
  135. "mod_step" => PHORUM_DELETE_MESSAGE,
  136. "thread" => $msgthd_id
  137. );
  138. foreach($PHORUM["args"] as $k=>$v){
  139. if(!is_numeric($k)){
  140. $args[$k] = $v;
  141. }
  142. }
  143. return phorum_show_confirmation_form(
  144. $PHORUM["DATA"]["LANG"]["ConfirmDeleteMessage"],
  145. phorum_get_url(PHORUM_MODERATION_ACTION_URL),
  146. $args
  147. );
  148. }
  149. $message = phorum_db_get_message($msgthd_id);
  150. /*
  151. * [hook]
  152. * before_delete
  153. *
  154. * [description]
  155. * This hook allows modules to implement extra or different delete
  156. * functionality.<sbr/>
  157. * <sbr/>
  158. * The primary use of this hook would be for moving the messages
  159. * to some archive-area instead of really deleting them.
  160. *
  161. * [category]
  162. * Moderation
  163. *
  164. * [when]
  165. * In <filename>moderation.php</filename>, just before deleting
  166. * the message(s)
  167. *
  168. * [input]
  169. * An array containing the following 5 parameters:
  170. * <ul>
  171. * <li><literal>$delete_handled</literal>:
  172. * default = <literal>false</literal>, set it to true to avoid
  173. * the real delete afterwards</li>
  174. * <li><literal>$msg_ids</literal>:
  175. * an array containing all deleted message ids</li>
  176. * <li><literal>$msgthd_id</literal>:
  177. * the msg-id or thread-id to be deleted</li>
  178. * <li><literal>$message</literal>:
  179. * an array of the data for the message retrieved with
  180. * <literal>$msgthd_id</literal></li>
  181. * <li><literal>$delete_mode</literal>:
  182. * mode of deletion, either
  183. * <literal>PHORUM_DELETE_MESSAGE</literal> or
  184. * <literal>PHORUM_DELETE_TREE</literal></li>
  185. * </ul>
  186. *
  187. * [output]
  188. * Same as input.<sbr/>
  189. * <literal>$delete_handled</literal> and
  190. * <literal>$msg_ids</literal> are used as return data for the hook.
  191. *
  192. * [example]
  193. * <hookcode>
  194. * function phorum_mod_foo_before_delete($data)
  195. * {
  196. * global $PHORUM;
  197. *
  198. * // Store the message data in the module's settings for
  199. * // future use.
  200. * $PHORUM["mod_foo"]["deleted_messages"][$msgthd_id] = $message;
  201. * phorum_db_update_settings(array("mod_foo" => $PHORUM["mod_foo"]));
  202. *
  203. * return $data;
  204. * }
  205. * </hookcode>
  206. */
  207. $delete_handled = 0;
  208. if (isset($PHORUM["hooks"]["before_delete"]))
  209. list($delete_handled,$msg_ids,$msgthd_id,$message,$delete_mode) = phorum_hook("before_delete", array(0,0,$msgthd_id,$message,PHORUM_DELETE_MESSAGE));
  210. // Handle the delete action, unless a module already handled it.
  211. if (!$delete_handled) {
  212. // Delete the message from the database.
  213. phorum_db_delete_message($msgthd_id, PHORUM_DELETE_MESSAGE);
  214. // Delete the message attachments from the database.
  215. require_once('./include/api/file_storage.php');
  216. $files=phorum_db_get_message_file_list($msgthd_id);
  217. foreach($files as $file_id=>$data) {
  218. phorum_api_file_delete($file_id);
  219. }
  220. }
  221. /*
  222. * [hook]
  223. * delete
  224. *
  225. * [description]
  226. * This hook can be used for cleaning up anything you may have
  227. * created with the post_post hook or any other hook that stored
  228. * data tied to messages.
  229. *
  230. * [category]
  231. * Moderation
  232. *
  233. * [when]
  234. * In <filename>moderation.php</filename>, right after deleting a
  235. * message from the database.
  236. *
  237. * [input]
  238. * An array of ids for messages that have been deleted (read-only).
  239. *
  240. * [output]
  241. * Same as input.
  242. *
  243. * [example]
  244. * <hookcode>
  245. * function phorum_mod_foo_delete($msgthd_ids)
  246. * {
  247. * global $PHORUM;
  248. *
  249. * // Log the deleted message ids
  250. * foreach ($msgthd_ids as $msgthd_id) {
  251. * $PHORUM["mod_foo"]["deleted_messages"][] = $msgthd_id;
  252. * }
  253. * phorum_db_update_settings(array("mod_foo" => $PHORUM["mod_foo"]));
  254. *
  255. * return $msgthd_ids;
  256. * }
  257. * </hookcode>
  258. */
  259. if (isset($PHORUM["hooks"]["delete"]))
  260. phorum_hook("delete", array($msgthd_id));
  261. $PHORUM['DATA']['OKMSG']="1 ".$PHORUM["DATA"]['LANG']['MsgDeletedOk'];
  262. if(isset($PHORUM['args']['old_forum']) && !empty($PHORUM['args']['old_forum'])) {
  263. $PHORUM['forum_id']=(int)$PHORUM['args']['old_forum'];
  264. }
  265. if(isset($PHORUM['args']["prepost"])) {
  266. $PHORUM['DATA']["URL"]["REDIRECT"]=phorum_get_url(PHORUM_CONTROLCENTER_URL,"panel=".PHORUM_CC_UNAPPROVED);
  267. } else {
  268. $PHORUM['DATA']["URL"]["REDIRECT"]=$PHORUM["DATA"]["URL"]["LIST"];
  269. }
  270. break;
  271. case PHORUM_DELETE_TREE: // this is a message delete
  272. if(count($_GET) && empty($_POST["thread"])){
  273. $args = array(
  274. "mod_step" => PHORUM_DELETE_TREE,
  275. "thread" => $msgthd_id
  276. );
  277. foreach($PHORUM["args"] as $k=>$v){
  278. if(!is_numeric($k)){
  279. $args[$k] = $v;
  280. }
  281. }
  282. return phorum_show_confirmation_form(
  283. $PHORUM["DATA"]["LANG"]["ConfirmDeleteThread"],
  284. phorum_get_url(PHORUM_MODERATION_ACTION_URL),
  285. $args
  286. );
  287. }
  288. $message = phorum_db_get_message($msgthd_id);
  289. $nummsgs = 0;
  290. // A hook to allow modules to implement extra or different
  291. // delete functionality.
  292. $delete_handled = 0;
  293. if (isset($PHORUM["hooks"]["before_delete"]))
  294. list($delete_handled,$msg_ids,$msgthd_id,$message,$delete_mode) = phorum_hook("before_delete", array(0,array(),$msgthd_id,$message,PHORUM_DELETE_TREE));
  295. if(!$delete_handled) {
  296. // Delete the message and all its replies.
  297. $msg_ids = phorum_db_delete_message($msgthd_id, PHORUM_DELETE_TREE);
  298. // Cleanup the attachments for all deleted messages.
  299. require_once('./include/api/file_storage.php');
  300. foreach($msg_ids as $id){
  301. $files=phorum_db_get_message_file_list($id);
  302. foreach($files as $file_id=>$data){
  303. phorum_api_file_delete($file_id);
  304. }
  305. }
  306. // Check if we have moved threads to delete.
  307. // We unset the forum id, so phorum_db_get_messages()
  308. // will return messages with the same thread id in
  309. // other forums as well (those are the move notifications).
  310. $forum_id = $PHORUM["forum_id"];
  311. $PHORUM["forum_id"] = 0;
  312. $moved = phorum_db_get_messages($msgthd_id);
  313. $PHORUM["forum_id"] = $forum_id;
  314. foreach ($moved as $id => $data) {
  315. if (!empty($data["moved"])) {
  316. phorum_db_delete_message($id, PHORUM_DELETE_MESSAGE);
  317. }
  318. }
  319. }
  320. $nummsgs=count($msg_ids);
  321. // Run a hook for performing custom actions after cleanup.
  322. if (isset($PHORUM["hooks"]["delete"]))
  323. phorum_hook("delete", $msg_ids);
  324. $PHORUM['DATA']['OKMSG']=$nummsgs." ".$PHORUM["DATA"]["LANG"]['MsgDeletedOk'];
  325. if(isset($PHORUM['args']['old_forum']) && !empty($PHORUM['args']['old_forum'])) {
  326. $PHORUM['forum_id']=(int)$PHORUM['args']['old_forum'];
  327. }
  328. if(isset($PHORUM['args']["prepost"])) {
  329. // add some additional args
  330. $addcode = "";
  331. if(isset($PHORUM['args']['moddays']) && is_numeric($PHORUM['args']['moddays'])) {
  332. $addcode.="moddays=".$PHORUM['args']['moddays'];
  333. }
  334. if(isset($PHORUM['args']['onlyunapproved']) && is_numeric($PHORUM['args']['onlyunapproved'])) {
  335. if(!empty($addcode))
  336. $addcode.=",";
  337. $addcode.="onlyunapproved=".$PHORUM['args']['onlyunapproved'];
  338. }
  339. $PHORUM['DATA']["URL"]["REDIRECT"]=phorum_get_url(PHORUM_CONTROLCENTER_URL,"panel=".PHORUM_CC_UNAPPROVED,$addcode);
  340. } else {
  341. $PHORUM['DATA']["URL"]["REDIRECT"]=$PHORUM["DATA"]["URL"]["LIST"];
  342. }
  343. break;
  344. case PHORUM_MOVE_THREAD: // this is the first step of a message move
  345. include_once './include/forum_functions.php';
  346. $message = phorum_db_get_message($msgthd_id);
  347. $PHORUM['DATA']['URL']["ACTION"]=phorum_get_url(PHORUM_MODERATION_ACTION_URL);
  348. $PHORUM['DATA']["FORM"]["forum_id"]=$PHORUM["forum_id"];
  349. $PHORUM['DATA']["FORM"]["thread_id"]=$msgthd_id;
  350. $PHORUM['DATA']["FORM"]["mod_step"]=PHORUM_DO_THREAD_MOVE;
  351. $PHORUM['DATA']["FORM"]["subject"] =htmlspecialchars($message["subject"], ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]);
  352. // get all the forums the moderator may move to
  353. $PHORUM['DATA']["MoveForumsOption"]="";
  354. // TODO: this does not match the check at the start of the read
  355. // TODO: and list scripts, where we check if this user has perms
  356. // TODO: for moderation of two or more forums, before we
  357. // TODO: enable the move feature. We should either check
  358. // TODO: for 2 or more moderated forums and check that moving
  359. // TODO: is only done between moderated forums or check for
  360. // TODO: 1 or more moderated forums and allow moving between
  361. // TODO: any two forums. Now we have a mix of those two.
  362. // add && phorum_api_user_check_access(PHORUM_USER_ALLOW_MODERATE_MESSAGES, $id) if the
  363. // mod should only be able to move to forums he also moderates
  364. // get the forumlist
  365. $forums=phorum_build_forum_list();
  366. // ignore the current forum
  367. unset($forums[$PHORUM["forum_id"]]);
  368. $PHORUM['DATA']['FORUMS']=$forums;
  369. $PHORUM['DATA']['FRM']=1;
  370. $output=true;
  371. $template="move_form";
  372. break;
  373. case PHORUM_DO_THREAD_MOVE: // this is the last step of a message move
  374. $movetoid=(int)$_POST['moveto'];
  375. // only do something if a forum was selected
  376. if(empty($movetoid)) {
  377. $PHORUM['DATA']['MESSAGE']=$PHORUM["DATA"]['LANG']['MsgMoveSelectForum'];
  378. } else {
  379. $PHORUM['DATA']['OKMSG']=$PHORUM["DATA"]['LANG']['MsgMoveOk'];
  380. $PHORUM['DATA']["URL"]["REDIRECT"]=$PHORUM["DATA"]["URL"]["LIST"];
  381. $message = phorum_db_get_message($msgthd_id);
  382. // find out if we have a notification-message already in this
  383. // target-forum for this thread ... it doesn't make sense to keep this
  384. // message any longer as the thread has reappeared on its original location
  385. $temp_forum_id=$PHORUM['forum_id'];
  386. $PHORUM['forum_id']=$movetoid;
  387. $check_messages=phorum_db_get_messages($msgthd_id);
  388. unset($check_messages['users']);
  389. // ok, we found exactly one message of this thread in the target forum
  390. if(is_array($check_messages) && count($check_messages) == 1) {
  391. // ... going to delete it
  392. $tmp_message=array_shift($check_messages);
  393. $retval=phorum_db_delete_message($tmp_message['message_id']);
  394. }
  395. $PHORUM['forum_id']=$temp_forum_id;
  396. // Move the thread to another forum.
  397. phorum_db_move_thread($msgthd_id, $movetoid);
  398. // Create a new message in place of the old one to notify
  399. // visitors that the thread was moved.
  400. if(isset($_POST['create_notification']) && $_POST['create_notification']) {
  401. $newmessage = $message;
  402. $newmessage['body']=" -- moved topic -- ";
  403. $newmessage['moved']=1;
  404. $newmessage['sort']=PHORUM_SORT_DEFAULT;
  405. unset($newmessage['message_id']);
  406. phorum_db_post_message($newmessage);
  407. }
  408. /*
  409. * [hook]
  410. * move_thread
  411. *
  412. * [description]
  413. * This hook can be used for performing actions like sending
  414. * notifications or for making log entries after moving a
  415. * thread.
  416. *
  417. * [category]
  418. * Moderation
  419. *
  420. * [when]
  421. * In <filename>moderation.php</filename>, right after a thread
  422. * has been moved by a moderator.
  423. *
  424. * [input]
  425. * The id of the thread that has been moved (read-only).
  426. *
  427. * [output]
  428. * Same as input.
  429. *
  430. * [example]
  431. * <hookcode>
  432. * function phorum_mod_foo_move_thread($msgthd_id)
  433. * {
  434. * global $PHORUM;
  435. *
  436. * // Log the moved thread id
  437. * $PHORUM["mod_foo"]["moved_threads"][] = $msgthd_id;
  438. * phorum_db_update_settings(array("mod_foo" => $PHORUM["mod_foo"]));
  439. *
  440. * return $msgthd_ids;
  441. * }
  442. * </hookcode>
  443. */
  444. if (isset($PHORUM["hooks"]["move_thread"]))
  445. phorum_hook("move_thread", $msgthd_id);
  446. foreach ($message['meta']['message_ids'] as $message_id) {
  447. $invalidate_message_cache[] = array(
  448. "message_id" => $message_id,
  449. "forum_id" => $message['forum_id']
  450. );
  451. }
  452. }
  453. break;
  454. case PHORUM_CLOSE_THREAD: // we have to close a thread
  455. $PHORUM['DATA']['OKMSG']=$PHORUM["DATA"]['LANG']['ThreadClosedOk'];
  456. $PHORUM['DATA']["URL"]["REDIRECT"]=$PHORUM["DATA"]["URL"]["LIST"];
  457. phorum_db_close_thread($msgthd_id);
  458. /*
  459. * [hook]
  460. * close_thread
  461. *
  462. * [description]
  463. * This hook can be used for performing actions like sending
  464. * notifications or making log entries after closing threads.
  465. *
  466. * [category]
  467. * Moderation
  468. *
  469. * [when]
  470. * In <filename>moderation.php</filename>, right after a thread has
  471. * been closed by a moderator.
  472. *
  473. * [input]
  474. * The id of the thread that has been closed (read-only).
  475. *
  476. * [output]
  477. * Same as input.
  478. *
  479. * [example]
  480. * <hookcode>
  481. * function phorum_mod_foo_close_thread($msgthd_id)
  482. * {
  483. * global $PHORUM;
  484. *
  485. * // Log the closed thread id
  486. * $PHORUM["mod_foo"]["closed_threads"][] = $msgthd_id;
  487. * phorum_db_update_settings(array("mod_foo" => $PHORUM["mod_foo"]));
  488. *
  489. * return $msgthd_ids;
  490. * }
  491. * </hookcode>
  492. */
  493. if (isset($PHORUM["hooks"]["close_thread"]))
  494. phorum_hook("close_thread", $msgthd_id);
  495. $invalidate_message_cache[] = array(
  496. "message_id" => $msgthd_id,
  497. "forum_id" => $PHORUM["forum_id"]
  498. );
  499. break;
  500. case PHORUM_REOPEN_THREAD: // we have to reopen a thread
  501. $PHORUM['DATA']['OKMSG']=$PHORUM["DATA"]['LANG']['ThreadReopenedOk'];
  502. $PHORUM['DATA']["URL"]["REDIRECT"]=$PHORUM["DATA"]["URL"]["LIST"];
  503. phorum_db_reopen_thread($msgthd_id);
  504. /*
  505. * [hook]
  506. * reopen_thread
  507. *
  508. * [description]
  509. * This hook can be used for performing actions like sending
  510. * notifications or making log entries after reopening threads.
  511. *
  512. * [category]
  513. * Moderation
  514. *
  515. * [when]
  516. * In <filename>moderation.php</filename>, right after a thread has
  517. * been reopened by a moderator.
  518. *
  519. * [input]
  520. * The id of the thread that has been reopened (read-only).
  521. *
  522. * [output]
  523. * Same as input.
  524. *
  525. * [example]
  526. * <hookcode>
  527. * function phorum_mod_foo_reopen_thread($msgthd_id)
  528. * {
  529. * global $PHORUM;
  530. *
  531. * // Log the reopened thread id
  532. * $PHORUM["mod_foo"]["reopened_threads"][] = $msgthd_id;
  533. * phorum_db_update_settings(array("mod_foo" => $PHORUM["mod_foo"]));
  534. *
  535. * return $msgthd_id;
  536. * }
  537. * </hookcode>
  538. */
  539. if (isset($PHORUM["hooks"]["reopen_thread"]))
  540. phorum_hook("reopen_thread", $msgthd_id);
  541. $invalidate_message_cache[] = array(
  542. "message_id" => $msgthd_id,
  543. "forum_id" => $PHORUM["forum_id"]
  544. );
  545. break;
  546. case PHORUM_APPROVE_MESSAGE: // approving a message
  547. $PHORUM['DATA']['OKMSG']="1 ".$PHORUM["DATA"]['LANG']['MsgApprovedOk'];
  548. $old_message = phorum_db_get_message($msgthd_id);
  549. $newpost=array("status"=>PHORUM_STATUS_APPROVED);
  550. // setting the new status
  551. phorum_db_update_message($msgthd_id, $newpost);
  552. // updating the thread-info
  553. phorum_update_thread_info($old_message['thread']);
  554. // updating the forum-stats
  555. phorum_db_update_forum_stats(false, 1, $old_message["datestamp"]);
  556. /*
  557. * [hook]
  558. * after_approve
  559. *
  560. * [description]
  561. * This hook can be used for performing extra actions after a
  562. * message has been approved.
  563. *
  564. * [category]
  565. * Moderation
  566. *
  567. * [when]
  568. * In <filename>moderation.php</filename>, right approving a message
  569. * and possibly its replies.
  570. *
  571. * [input]
  572. * An array containing two elements:
  573. * <ul>
  574. * <li>The message data</li>
  575. * <li>The type of approval (either
  576. * <literal>PHORUM_APPROVE_MESSAGE</literal> or
  577. * <literal>PHORUM_APPROVE_MESSAGE_TREE</literal>)</li>
  578. * </ul>
  579. *
  580. * [output]
  581. * Same as input.
  582. *
  583. * [example]
  584. * <hookcode>
  585. * function phorum_mod_foo_after_approve($data)
  586. * {
  587. * global $PHORUM;
  588. *
  589. * // alert the message author that their message has been
  590. * // approved
  591. * $pm_message = preg_replace(
  592. * "%message_subject%",
  593. * $data[0]["subject"],
  594. * $PHORUM["DATA"]["LANG"]["mod_foo"]["MessageApprovedBody"]
  595. * );
  596. * phorum_db_pm_send(
  597. * $PHORUM["DATA"]["LANG"]["mod_foo"]["MessageApprovedSubject"],
  598. * $pm_message,
  599. * $data[0]["user_id"]
  600. * );
  601. *
  602. * return $data;
  603. *
  604. * }
  605. * </hookcode>
  606. */
  607. if (isset($PHORUM["hooks"]["after_approve"]))
  608. phorum_hook("after_approve", array($old_message, PHORUM_APPROVE_MESSAGE));
  609. if($old_message['status'] != PHORUM_STATUS_HIDDEN ) {
  610. phorum_email_notice($old_message);
  611. }
  612. if(isset($PHORUM['args']['old_forum']) && is_numeric($PHORUM['args']['old_forum'])) {
  613. $PHORUM['forum_id']=(int)$PHORUM['args']['old_forum'];
  614. }
  615. if(isset($PHORUM['args']["prepost"])) {
  616. // add some additional args
  617. $addcode = "";
  618. if(isset($PHORUM['args']['moddays']) && is_numeric($PHORUM['args']['moddays'])) {
  619. $addcode.="moddays=".$PHORUM['args']['moddays'];
  620. }
  621. if(isset($PHORUM['args']['onlyunapproved']) && is_numeric($PHORUM['args']['onlyunapproved'])) {
  622. if(!empty($addcode))
  623. $addcode.=",";
  624. $addcode.="onlyunapproved=".$PHORUM['args']['onlyunapproved'];
  625. }
  626. $PHORUM['DATA']["URL"]["REDIRECT"]=phorum_get_url(PHORUM_CONTROLCENTER_URL,"panel=".PHORUM_CC_UNAPPROVED,$addcode);
  627. } else {
  628. $PHORUM['DATA']["URL"]["REDIRECT"]=$PHORUM["DATA"]["URL"]["LIST"];
  629. }
  630. $invalidate_message_cache[] = array(
  631. "message_id" => $msgthd_id,
  632. "forum_id" => $PHORUM["forum_id"]
  633. );
  634. break;
  635. case PHORUM_APPROVE_MESSAGE_TREE: // approve a message and all answers to it
  636. $old_message = phorum_db_get_message($msgthd_id);
  637. $newpost=array("status"=>PHORUM_STATUS_APPROVED);
  638. $mids = phorum_db_get_messagetree($msgthd_id, $old_message["forum_id"]);
  639. // make an array from the string
  640. $mids_arr=explode(",",$mids);
  641. // count the entries for later use
  642. $num_approved=count($mids_arr);
  643. foreach($mids_arr as $key => $mid) {
  644. // setting the new status
  645. phorum_db_update_message($mid, $newpost);
  646. $invalidate_message_cache[] = array(
  647. "message_id" => $mid,
  648. "forum_id" => $PHORUM["forum_id"]
  649. );
  650. }
  651. // updating the thread-info
  652. phorum_update_thread_info($old_message['thread']);
  653. // updating the forum-stats
  654. phorum_db_update_forum_stats(false, "+$num_approved", $old_message["datestamp"]);
  655. if (isset($PHORUM["hooks"]["after_approve"]))
  656. phorum_hook("after_approve", array($old_message, PHORUM_APPROVE_MESSAGE_TREE));
  657. $PHORUM['DATA']['OKMSG']="$num_approved ".$PHORUM['DATA']['LANG']['MsgApprovedOk'];
  658. if(isset($PHORUM['args']["prepost"])) {
  659. // add some additional args
  660. $addcode = "";
  661. if(isset($PHORUM['args']['moddays']) && is_numeric($PHORUM['args']['moddays'])) {
  662. $addcode.="moddays=".$PHORUM['args']['moddays'];
  663. }
  664. if(isset($PHORUM['args']['onlyunapproved']) && is_numeric($PHORUM['args']['onlyunapproved'])) {
  665. if(!empty($addcode))
  666. $addcode.=",";
  667. $addcode.="onlyunapproved=".$PHORUM['args']['onlyunapproved'];
  668. }
  669. $PHORUM['DATA']["URL"]["REDIRECT"]=phorum_get_url(PHORUM_CONTROLCENTER_URL,"panel=".PHORUM_CC_UNAPPROVED,$addcode);
  670. } else {
  671. $PHORUM['DATA']["URL"]["REDIRECT"]=$PHORUM["DATA"]["URL"]["LIST"];
  672. }
  673. break;
  674. case PHORUM_HIDE_POST: // hiding a message (and its replies)
  675. $old_message = phorum_db_get_message($msgthd_id);
  676. $newpost=array("status"=>PHORUM_STATUS_HIDDEN);
  677. $mids = phorum_db_get_messagetree($msgthd_id, $old_message["forum_id"]);
  678. // make an array from the string
  679. $mids_arr=explode(",",$mids);
  680. // count the entries for later use
  681. $num_hidden=count($mids_arr);
  682. foreach($mids_arr as $key => $mid) {
  683. // setting the new status
  684. phorum_db_update_message($mid, $newpost);
  685. }
  686. /*
  687. * [hook]
  688. * hide_thread
  689. *
  690. * [description]
  691. * This hook can be used for performing actions like sending
  692. * notifications or making log entries after hiding a message.
  693. *
  694. * [category]
  695. * Moderation
  696. *
  697. * [when]
  698. * In <filename>moderation.php</filename>, right after a message has
  699. * been hidden by a moderator.
  700. *
  701. * [input]
  702. * The id of the thread that has been hidden (read-only).
  703. *
  704. * [output]
  705. * Same as input.
  706. *
  707. * [example]
  708. * <hookcode>
  709. * function phorum_mod_foo_hide_thread($msgthd_id)
  710. * {
  711. * global $PHORUM;
  712. *
  713. * // Log the hidden thread id
  714. * $PHORUM["mod_foo"]["hidden_threads"][] = $msgthd_id;
  715. * phorum_db_update_settings(array("mod_foo" => $PHORUM["mod_foo"]));
  716. *
  717. * return $msgthd_id;
  718. * }
  719. * </hookcode>
  720. */
  721. if (isset($PHORUM["hooks"]["hide_thread"]))
  722. phorum_hook("hide_thread", $msgthd_id);
  723. // updating the thread-info
  724. phorum_update_thread_info($old_message['thread']);
  725. // updating the forum-stats
  726. phorum_db_update_forum_stats(false, "-$num_hidden");
  727. $PHORUM['DATA']['OKMSG']="$num_hidden ".$PHORUM['DATA']['LANG']['MsgHiddenOk'];
  728. if(isset($PHORUM['args']["prepost"])) {
  729. $PHORUM['DATA']["URL"]["REDIRECT"]=phorum_get_url(PHORUM_CONTROLCENTER_URL,"panel=".PHORUM_CC_UNAPPROVED);
  730. } else {
  731. $PHORUM['DATA']["URL"]["REDIRECT"]=$PHORUM["DATA"]["URL"]["LIST"];
  732. }
  733. break;
  734. case PHORUM_MERGE_THREAD: // this is the first step of a thread merge
  735. $template="merge_form";
  736. $PHORUM['DATA']['URL']["ACTION"] = phorum_get_url(PHORUM_MODERATION_ACTION_URL);
  737. $PHORUM['DATA']["FORM"]["forum_id"] = $PHORUM["forum_id"];
  738. $PHORUM['DATA']["FORM"]["thread_id"] = $msgthd_id;
  739. $PHORUM['DATA']["FORM"]["mod_step"] = PHORUM_DO_THREAD_MERGE;
  740. // the moderator selects the target thread to merge to
  741. $merge_t1 = phorum_moderator_data_get('merge_t1');
  742. if( !$merge_t1 || $merge_t1==$msgthd_id ) {
  743. phorum_moderator_data_put('merge_t1', $msgthd_id);
  744. $PHORUM['DATA']["FORM"]["merge_none"] =true;
  745. $message = phorum_db_get_message($msgthd_id, "message_id", true);
  746. $PHORUM['DATA']["FORM"]["merge_subject1"] =htmlspecialchars($message["subject"], ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]);
  747. }
  748. // the moderator selects the source thread to merge from
  749. else {
  750. $PHORUM['DATA']["FORM"]["merge_t1"] =$merge_t1;
  751. $message = phorum_db_get_message($merge_t1, "message_id", true);
  752. $PHORUM['DATA']["FORM"]["merge_subject1"] =htmlspecialchars($message["subject"], ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]);
  753. $message = phorum_db_get_message($msgthd_id);
  754. $PHORUM['DATA']["FORM"]["thread_subject"] =htmlspecialchars($message["subject"], ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]);
  755. }
  756. break;
  757. case PHORUM_DO_THREAD_MERGE: // this is the last step of a thread merge
  758. if( isset($_POST['thread1']) && $_POST['thread1']) {
  759. // Commit Thread Merge
  760. settype($_POST['thread1'], "int");
  761. settype($_POST['thread'], "int"); // Thread 2
  762. $PHORUM['DATA']['OKMSG'] = $PHORUM["DATA"]['LANG']['MsgMergeOk'];
  763. $PHORUM['DATA']["URL"]["REDIRECT"] = $PHORUM["DATA"]["URL"]["LIST"];
  764. $PHORUM["reverse_threading"] = 0;
  765. // Get the target thread.
  766. $target =phorum_db_get_message($_POST['thread1'], "message_id", true);
  767. if (!$target) trigger_error(
  768. "Can't retrieve target thread " . $_POST['thread1'],
  769. E_USER_ERROR
  770. );
  771. // Get all messages from the thread that we have to merge.
  772. $merge_messages=phorum_db_get_messages($_POST['thread']);
  773. unset($merge_messages['users']);
  774. // Create new messages in the target thread for
  775. // all messages that have to be merged.
  776. $msgid_translation=array();
  777. foreach($merge_messages as $msg)
  778. {
  779. $oldid=$msg['message_id'];
  780. $msg['thread'] = $target['thread']; // the thread we merge with
  781. $msg['forum_id'] = $target['forum_id']; // the forum_id of the new thread
  782. $msg['sort'] = $target['sort']; // the sort type of the new thread
  783. if($msg['message_id'] == $msg['thread']) {
  784. $msg['parent_id']=$target['thread'];
  785. } elseif(isset($msgid_translation[$msg['parent_id']])) {
  786. $msg['parent_id']=$msgid_translation[$msg['parent_id']];
  787. } else {
  788. $msg['parent_id']=$msg['thread'];
  789. }
  790. unset($msg['message_id']);
  791. unset($msg['modifystamp']);
  792. phorum_db_post_message($msg,true);
  793. // Link attached files to the new message id.
  794. $linked_files = phorum_db_get_message_file_list($oldid);
  795. foreach ($linked_files as $linked_file) {
  796. phorum_db_file_link($linked_file["file_id"], $msg["message_id"], PHORUM_LINK_MESSAGE);
  797. }
  798. // save the new message-id for later use
  799. $msgid_translation[$oldid]=$msg['message_id'];
  800. }
  801. // deleting messages which are now doubled
  802. phorum_db_delete_message($_POST['thread'], PHORUM_DELETE_TREE);
  803. // update message count / stats
  804. phorum_db_update_forum_stats(true);
  805. // change forum_id for the following calls to update the right forum
  806. $PHORUM["forum_id"] =$target['forum_id'];
  807. // update message count / stats
  808. phorum_update_thread_info($target['thread']);
  809. phorum_db_update_forum_stats(true);
  810. /*
  811. * [hook]
  812. * after_merge
  813. *
  814. * [description]
  815. * This hook can be used for performing actions on
  816. * merging threads
  817. *
  818. * [category]
  819. * Moderation
  820. *
  821. * [when]
  822. * In <filename>moderation.php</filename>, right after two threads have
  823. * been merged by a moderator.
  824. *
  825. * [input]
  826. * An array with the translated message-ids; old-message_id -> new-message_id
  827. *
  828. * [output]
  829. * Same as input.
  830. */
  831. phorum_hook('after_merge', $msgid_translation);
  832. } else {
  833. // Cancel Thread Merge
  834. $PHORUM['DATA']['OKMSG']=$PHORUM["DATA"]['LANG']['MsgMergeCancel'];
  835. $PHORUM['DATA']["URL"]["REDIRECT"]=$PHORUM["DATA"]["URL"]["LIST"];
  836. }
  837. // unset temporary moderator_data
  838. phorum_moderator_data_remove('merge_t1');
  839. break;
  840. case PHORUM_SPLIT_THREAD: // this is the first step of a thread split
  841. $PHORUM['DATA']['URL']["ACTION"]=phorum_get_url(PHORUM_MODERATION_ACTION_URL);
  842. $PHORUM['DATA']["FORM"]["forum_id"]=$PHORUM["forum_id"];
  843. $message =phorum_db_get_message($msgthd_id);
  844. $PHORUM['DATA']["FORM"]["thread_id"]=$message["thread"];
  845. $PHORUM['DATA']["FORM"]["message_id"]=$msgthd_id;
  846. $PHORUM['DATA']["FORM"]["message_subject"]=htmlspecialchars($message["subject"], ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]);
  847. $PHORUM['DATA']["FORM"]["mod_step"]=PHORUM_DO_THREAD_SPLIT;
  848. $template="split_form";
  849. break;
  850. case PHORUM_DO_THREAD_SPLIT: // this is the last step of a thread split
  851. $PHORUM['DATA']['OKMSG']=$PHORUM["DATA"]['LANG']['MsgSplitOk'];
  852. $PHORUM['DATA']["URL"]["REDIRECT"]=$PHORUM["DATA"]["URL"]["LIST"];
  853. settype($_POST['forum_id'], "int");
  854. settype($_POST['message'], "int");
  855. settype($_POST['thread'], "int");
  856. phorum_db_split_thread($_POST['message'],$_POST['forum_id']);
  857. if ($PHORUM['cache_messages']) {
  858. $message = phorum_db_get_message($_POST['thread']);
  859. foreach ($message['meta']['message_ids'] as $message_id) {
  860. phorum_cache_remove('message', $message_id);
  861. }
  862. }
  863. // update message count / stats
  864. phorum_update_thread_info($_POST['thread']);
  865. phorum_update_thread_info($_POST['message']);
  866. phorum_db_update_forum_stats(true);
  867. /*
  868. * [hook]
  869. * after_split
  870. *
  871. * [description]
  872. * This hook can be used for performing actions on
  873. * splitting threads
  874. *
  875. * [category]
  876. * Moderation
  877. *
  878. * [when]
  879. * In <filename>moderation.php</filename>, right after a thread has
  880. * been split by a moderator.
  881. *
  882. * [input]
  883. * The id of the newly created thread
  884. *
  885. * [output]
  886. * Same as input.
  887. */
  888. phorum_hook('after_split', $_POST['message']);
  889. break;
  890. default:
  891. if(!isset($PHORUM['DATA']['OKMSG'])) $PHORUM['DATA']['OKMSG']="";
  892. $PHORUM['DATA']["URL"]["REDIRECT"]=$PHORUM["DATA"]["URL"]["LIST"];
  893. }
  894. // remove the affected messages from the cache if caching is enabled.
  895. if ($PHORUM['cache_messages']) {
  896. $invalidate_forums = array();
  897. foreach($invalidate_message_cache as $message) {
  898. phorum_cache_remove('message', $message["message_id"]);
  899. $invalidate_forums[$message['forum_id']]=$message['forum_id'];
  900. }
  901. if(is_array($invalidate_forums) && count($invalidate_forums)) {
  902. // retrieve the data for all involved forums to get the correct cache version
  903. $forums_data = phorum_api_forums_get($invalidate_forums);
  904. // increment the cache version for all involved forums once
  905. foreach($invalidate_forums as $forum_id) {
  906. phorum_db_update_forum(array('forum_id'=>$forum_id,'cache_version'=>($forums_data[$forum_id]['cache_version']+1)));
  907. }
  908. }
  909. }
  910. if(!isset($PHORUM['DATA']['BACKMSG'])) {
  911. $PHORUM['DATA']["BACKMSG"]=$PHORUM['DATA']["LANG"]["BackToList"];
  912. }
  913. phorum_output($template);
  914. ?>