PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/pm.php

https://bitbucket.org/webop/webop-forum
PHP | 1502 lines | 741 code | 223 blank | 538 comment | 196 complexity | 4ac81028146986eed951df93957bd5b6 MD5 | raw file
Possible License(s): LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  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. // These language strings are set dynamically, so the language
  20. // tool won't recognize them automatically. Therefore they are
  21. // mentioned here.
  22. // $PHORUM["DATA"]["LANG"]["PMFolderCreateSuccess"]
  23. // $PHORUM["DATA"]["LANG"]["PMFolderRenameSuccess"]
  24. // $PHORUM["DATA"]["LANG"]["PMFolderDeleteSuccess"]
  25. // $PHORUM["DATA"]["LANG"]["PMSent"]
  26. // PMTODO If reading from a mail notify, lookup the folder_id,
  27. // so the close button will work. Now the folder_id is empty.
  28. // PMTODO implement pm_reply_flag functionality
  29. define('phorum_page','pm');
  30. include_once("./common.php");
  31. phorum_require_login();
  32. // CSRF protection: we do not accept posting to this script,
  33. // when the browser does not include a Phorum signed token
  34. // in the request.
  35. phorum_check_posting_token();
  36. // set all our common URL's
  37. phorum_build_common_urls();
  38. include_once("./include/email_functions.php");
  39. include_once("./include/format_functions.php");
  40. // a user has to be logged in to use the private messages system
  41. if (!$PHORUM["DATA"]["LOGGEDIN"]) {
  42. phorum_redirect_by_url(phorum_get_url(PHORUM_LIST_URL));
  43. exit();
  44. }
  45. // if the user is not fully logged in, send him to the login page
  46. if (!$PHORUM["DATA"]["FULLY_LOGGEDIN"]) {
  47. // Construct the URL to redirect to after logging in.
  48. $args = array(PHORUM_PM_URL);
  49. foreach ($PHORUM["args"] as $k => $v) {
  50. if (in_array("$k=$v", $PHORUM["DATA"]["GET_VARS"])) continue;
  51. if(is_numeric($k)) $args[] = $v; else $args[] = "$k=$v";
  52. }
  53. $redir = urlencode(call_user_func_array('phorum_get_url', $args));
  54. phorum_redirect_by_url(phorum_get_url(PHORUM_LOGIN_URL, "redir=$redir"));
  55. exit();
  56. }
  57. // If private messages are disabled, just show a simple error message.
  58. if (! $PHORUM["enable_pm"]) {
  59. $PHORUM["DATA"]["BLOCK_CONTENT"] = $PHORUM["DATA"]["LANG"]["PMDisabled"];
  60. phorum_output("stdblock");
  61. return;
  62. }
  63. // fill the breadcrumbs-info
  64. $PHORUM['DATA']['BREADCRUMBS'][]=array(
  65. 'URL'=>$PHORUM['DATA']['URL']['PM'],
  66. 'TEXT'=>$PHORUM['DATA']['LANG']['PrivateMessages'],
  67. 'TYPE'=>'pm'
  68. );
  69. // ------------------------------------------------------------------------
  70. // Parameter handling
  71. // ------------------------------------------------------------------------
  72. // Retrieve a parameter from either the args-list or $_POST.
  73. // Do typecasting if requested.
  74. function phorum_getparam($name, $type = NULL)
  75. {
  76. $PHORUM = $GLOBALS["PHORUM"];
  77. $ret = NULL;
  78. if (isset($PHORUM["args"][$name])) {
  79. $ret = trim($PHORUM["args"][$name]);
  80. }elseif (isset($_POST[$name])) {
  81. $ret = trim($_POST[$name]);
  82. }
  83. // Apply typecasting if requested.
  84. if ($ret != NULL && $type != NULL) {
  85. switch ($type) {
  86. case 'integer':
  87. $ret = (int) $ret;
  88. break;
  89. case 'boolean':
  90. $ret = $ret ? 1 : 0;
  91. break;
  92. case 'folder_id':
  93. if ($ret != PHORUM_PM_INBOX && $ret != PHORUM_PM_OUTBOX) {
  94. $ret = (int)$ret;
  95. }
  96. break;
  97. default:
  98. trigger_error(
  99. "Internal error in phorum_getparam: " .
  100. "illegal type for typecasting: ".htmlspecialchars($type),
  101. E_USER_ERROR
  102. );
  103. }
  104. }
  105. return $ret;
  106. }
  107. // Get basic parameters.
  108. $action = phorum_getparam('action');
  109. $page = phorum_getparam('page');
  110. $folder_id = phorum_getparam('folder_id', 'folder_id');
  111. $pm_id = phorum_getparam('pm_id', 'integer');
  112. $forum_id = (int)$PHORUM["forum_id"];
  113. $user_id = (int)$PHORUM["user"]["user_id"];
  114. $hide_userselect = phorum_getparam('hide_userselect', 'boolean');
  115. // Cleanup array with checked PM items.
  116. if (isset($_POST["checked"])) {
  117. $checked = array();
  118. foreach ($_POST["checked"] as $pm_id) {
  119. $checked[] = (int)$pm_id;
  120. }
  121. $_POST["checked"] = $checked;
  122. }
  123. // Get recipients from the form and create a valid list of recipients.
  124. $recipients = array();
  125. if (isset($_POST["recipients"]) && is_array($_POST["recipients"])) {
  126. foreach ($_POST["recipients"] as $id => $dummy) {
  127. $user = phorum_api_user_get($id);
  128. if ($user && $user["active"] == 1) {
  129. $recipients[$id] = $user;
  130. }
  131. }
  132. }
  133. // init error var
  134. $error_msg = "";
  135. // ------------------------------------------------------------------------
  136. // Banlist checking
  137. // ------------------------------------------------------------------------
  138. // Start editor Post message Post reply
  139. if ($page == 'send' || $action == 'post' || ($action == 'list' && isset($pm_id)))
  140. {
  141. include_once("./include/profile_functions.php");
  142. $error = phorum_check_bans(array(
  143. array($PHORUM["user"]["username"], PHORUM_BAD_NAMES),
  144. array($PHORUM["user"]["email"], PHORUM_BAD_EMAILS),
  145. array($user_id, PHORUM_BAD_USERID),
  146. array(NULL, PHORUM_BAD_IPS),
  147. ));
  148. // Show an error in case we encountered a ban.
  149. if (! empty($error)) {
  150. $PHORUM["DATA"]["ERROR"] = $error;
  151. phorum_output("message");
  152. return;
  153. }
  154. }
  155. // ------------------------------------------------------------------------
  156. // Perform actions
  157. // ------------------------------------------------------------------------
  158. // Initialize error and ok message.
  159. $error = '';
  160. $okmsg = '';
  161. // init folder list
  162. $pm_folders = phorum_db_pm_getfolders(NULL, true);
  163. // Translate button clicks from the read page to appropriate actions.
  164. if (isset($_POST['close_message'])) {
  165. $page = 'list';
  166. } elseif (isset($_POST['delete_message'])) {
  167. $page = 'list';
  168. $_POST['delete'] = 1;
  169. $_POST['checked'] = array($pm_id);
  170. $action = 'list';
  171. } elseif (isset($_POST['move_message'])) {
  172. $page = 'list';
  173. $_POST['move'] = 1;
  174. $_POST['checked'] = array($pm_id);
  175. $action = 'list';
  176. } elseif (isset($_POST['reply']) || isset($_POST['reply_to_all'])) {
  177. $page = 'send';
  178. $action = '';
  179. }
  180. if (!empty($action)) {
  181. // Utility function to check if a foldername already exists.
  182. // No extreme checking with locking here. Technically
  183. // speaking duplicate foldernames will work. It's just
  184. // confusing for the user.
  185. function phorum_pm_folder_exists($foldername)
  186. {
  187. global $pm_folders;
  188. foreach ($pm_folders as $id => $data) {
  189. if (strcasecmp($foldername, $data["name"]) == 0) {
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. // Redirect will be set to a true value if after performing
  196. // the action we want to use a redirect to get to the
  197. // result page. This is done for two reasons:
  198. // 1) Let the result page use refreshed PM data;
  199. // 2) Prevent reloading of the action page (which could for
  200. // example result in duplicate message sending).
  201. // The variable $redirect_message can be set to a language
  202. // key string to have a message displayed after redirection.
  203. $redirect = false;
  204. $redirect_message = '';
  205. switch($action) {
  206. // Actions which are triggered from the folder management interface.
  207. case "folders":
  208. $redirect = false;
  209. $page = "folders";
  210. // Create folder.
  211. if (!empty($_POST['create_folder']))
  212. {
  213. $foldername = trim($_POST["create_folder_name"]);
  214. if ($foldername != '')
  215. {
  216. if (phorum_pm_folder_exists($foldername)) {
  217. $error = $PHORUM["DATA"]["LANG"]["PMFolderExistsError"];
  218. } else {
  219. phorum_db_pm_create_folder($foldername);
  220. $redirect_message = "PMFolderCreateSuccess";
  221. $redirect = true;
  222. }
  223. }
  224. }
  225. // Rename a folder.
  226. elseif (!empty($_POST['rename_folder']))
  227. {
  228. $from = $_POST['rename_folder_from'];
  229. $to = trim($_POST['rename_folder_to']);
  230. if (!empty($from) && $to != '') {
  231. if (phorum_pm_folder_exists($to)) {
  232. $error = $PHORUM["DATA"]["LANG"]["PMFolderExistsError"];
  233. } else {
  234. phorum_db_pm_rename_folder($from, $to);
  235. $redirect_message = "PMFolderRenameSuccess";
  236. $redirect = true;
  237. }
  238. }
  239. }
  240. // Delete a folder.
  241. elseif (!empty($_POST['delete_folder']))
  242. {
  243. $folder_id = $_POST["delete_folder_target"];
  244. if (!empty($folder_id)) {
  245. phorum_db_pm_delete_folder($folder_id);
  246. /**
  247. * [hook]
  248. * pm_delete_folder
  249. *
  250. * [availability]
  251. * Phorum 5 >= 5.2.13
  252. *
  253. * [description]
  254. * This hook can be used for working on deletion of a
  255. * private message folder. E.g. for deleting messages
  256. * in the folder before.
  257. *
  258. * [category]
  259. * Private message system
  260. *
  261. * [when]
  262. * Right before Phorum deletes the private message folder.
  263. *
  264. * [input]
  265. * The id of the private message folder going to be deleted.
  266. *
  267. * [output]
  268. * Same as input.
  269. *
  270. * [example]
  271. * <hookcode>
  272. * function phorum_mod_foo_pm_delete_folder($folder_id)
  273. * {
  274. * // do something with the folder going to be deleted
  275. *
  276. * return $folder_id;
  277. * }
  278. * </hookcode>
  279. */
  280. if (isset($PHORUM['hooks']['pm_delete_folder'])) {
  281. phorum_hook('pm_delete_folder', $folder_id);
  282. }
  283. $redirect_message = "PMFolderDeleteSuccess";
  284. $redirect = true;
  285. // Invalidate user cache, to update message counts.
  286. phorum_cache_remove('user',$user_id);
  287. }
  288. }
  289. break;
  290. // Actions which are triggered from the list interface.
  291. case "list":
  292. // Delete all checked messages.
  293. if (isset($_POST["delete"]) && isset($_POST["checked"])) {
  294. foreach($_POST["checked"] as $pm_id) {
  295. if (phorum_db_pm_get($pm_id, $folder_id)) {
  296. phorum_db_pm_delete($pm_id, $folder_id);
  297. /**
  298. * [hook]
  299. * pm_delete
  300. *
  301. * [availability]
  302. * Phorum 5 >= 5.2.13
  303. *
  304. * [description]
  305. * This hook can be used for working deletion of a
  306. * private message.
  307. *
  308. * [category]
  309. * Private message system
  310. *
  311. * [when]
  312. * Right before Phorum deletes the private message.
  313. *
  314. * [input]
  315. * The id of the private message going to be deleted.
  316. *
  317. * [output]
  318. * Same as input.
  319. *
  320. * [example]
  321. * <hookcode>
  322. * function phorum_mod_foo_pm_delete($pm_id)
  323. * {
  324. * // do something with the message going to be deleted
  325. *
  326. * return $pm_id;
  327. * }
  328. * </hookcode>
  329. */
  330. if (isset($PHORUM['hooks']['pm_delete'])) {
  331. phorum_hook('pm_delete', $pm_id);
  332. }
  333. }
  334. }
  335. // Invalidate user cache, to update message counts.
  336. phorum_cache_remove('user',$user_id);
  337. }
  338. // Move checked messages to another folder.
  339. elseif (isset($_POST["move"]) && isset($_POST["checked"])) {
  340. $to = $_POST['target_folder'];
  341. if (! empty($to)) {
  342. foreach($_POST["checked"] as $pm_id) {
  343. if (phorum_db_pm_get($pm_id, $folder_id)) {
  344. phorum_db_pm_move($pm_id, $folder_id, $to);
  345. }
  346. }
  347. }
  348. }
  349. $page = "list";
  350. $redirect = true;
  351. break;
  352. // Actions which are triggered from the post form.
  353. case "post":
  354. // Parse clicks on the image buttons that we use for
  355. // deleting recipients from the list of recipients.
  356. // These are not sent as name=value, but instead
  357. // name_x=xclickoffset and name_y=yclickoffset are sent.
  358. // Also accept normal button clicks with name="del_rcpt::<id>",
  359. // so template builders can use that.
  360. $del_rcpt = NULL;
  361. foreach ($_POST as $key => $val) {
  362. if (preg_match('/^del_rcpt::(\d+)(_x)?$/', $key, $m)) {
  363. $del_rcpt = $m[1];
  364. break;
  365. }
  366. }
  367. // Determine what action to perform.
  368. $action = "post";
  369. if (isset($_POST["preview"])) $action = "preview";
  370. if (isset($_POST["rcpt_add"])) $action = "rcpt_add";
  371. if (!is_null($del_rcpt)) $action = "del_rcpt";
  372. // Adding a recipient.
  373. if ($action == "rcpt_add" || $action == "preview" || $action == "post") {
  374. // Convert adding a recipient by name to adding by user id.
  375. if (isset($_POST["to_name"])) {
  376. $to_name = trim($_POST["to_name"]);
  377. if ($to_name != '') {
  378. $to_user_ids = phorum_api_user_search(
  379. 'display_name', $to_name, '=', TRUE
  380. );
  381. if (empty($to_user_ids) || count($to_user_ids) > 1) {
  382. $error = $PHORUM["DATA"]["LANG"]["UserNotFound"];
  383. } else {
  384. $_POST["to_id"] = array_shift($to_user_ids);
  385. unset($_POST["to_name"]);
  386. }
  387. }
  388. }
  389. // Add a recipient by id.
  390. if (isset($_POST["to_id"]) && is_numeric($_POST["to_id"])) {
  391. $user = phorum_api_user_get($_POST["to_id"]);
  392. if ($user && $user["active"] == PHORUM_USER_ACTIVE) {
  393. $recipients[$user["user_id"]] = $user;
  394. } else {
  395. $error = $PHORUM["DATA"]["LANG"]["UserNotFound"];
  396. }
  397. }
  398. $page = "send";
  399. // Deleting a recipient.
  400. } elseif ($action == "del_rcpt") {
  401. unset($recipients[$del_rcpt]);
  402. $page = "send";
  403. // When deleting a recipient, we always have to
  404. // show the user selection. Put it back in, for
  405. // situations where we had the user selection
  406. // hidden intentionally.
  407. $hide_userselect = 0;
  408. }
  409. // For previewing the message, no action has to be taken.
  410. if ($action == "preview") {
  411. $page = "send";
  412. }
  413. // Posting the message.
  414. elseif ($action == "post") {
  415. // Only send the message if we have at least one recipient.
  416. if (count($recipients)) {
  417. $_POST["subject"] = trim($_POST["subject"]);
  418. $_POST["message"] = trim($_POST["message"]);
  419. // Only send the message if all required message data is filled in.
  420. if ($_POST["subject"] == '' || $_POST["message"] == '') {
  421. $error = $PHORUM["DATA"]["LANG"]["PMRequiredFields"];
  422. // Message data is okay. Post the message.
  423. } else {
  424. if (empty($_POST["keep"])) $_POST["keep"] = 0;
  425. // Check if sender and recipients have not yet reached the
  426. // maximum number of messages that may be stored on the server.
  427. // Administrators may always send PM.
  428. if (!$PHORUM['user']['admin'] && isset($PHORUM['max_pm_messagecount']) && $PHORUM['max_pm_messagecount'])
  429. {
  430. // Build a list of users to check.
  431. $checkusers = $recipients;
  432. if ($_POST['keep']) $checkusers[] = $PHORUM['user'];
  433. // Check all users.
  434. foreach ($checkusers as $user)
  435. {
  436. if ($user['admin']) continue; // No limits for admins
  437. $current_count = phorum_db_pm_messagecount(PHORUM_PM_ALLFOLDERS, $user["user_id"]);
  438. if ($current_count['total'] >= $PHORUM['max_pm_messagecount']) {
  439. if ($user['user_id'] == $PHORUM["user"]["user_id"]) {
  440. $error = $PHORUM["DATA"]["LANG"]["PMFromMailboxFull"];
  441. } else {
  442. $error = $PHORUM["DATA"]["LANG"]["PMToMailboxFull"];
  443. $recipient =
  444. (empty($PHORUM["custom_display_name"])
  445. ? htmlspecialchars($user["display_name"], ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"])
  446. : $user["display_name"]);
  447. $error = str_replace('%recipient%', $recipient, $error);
  448. }
  449. }
  450. }
  451. }
  452. /**
  453. * [hook]
  454. * pm_before_send
  455. *
  456. * [availability]
  457. * Phorum 5 >= 5.2.15
  458. *
  459. * [description]
  460. * This hook can be used for doing modifications to
  461. * PM message data that is stored in the database.
  462. * This hook can also be used to apply checks to
  463. * the data that is to be posted and to return an
  464. * error in case the data should not be posted.
  465. *
  466. * [category]
  467. * Private message system
  468. *
  469. * [when]
  470. * Just before the private message is stored in
  471. * the database.
  472. *
  473. * [input]
  474. * An array containing private message data. The
  475. * fields in this data are "subject", "message",
  476. * "recipients" and "keep".
  477. *
  478. * [output]
  479. * The message data, possibly modified. A hook can
  480. * set the field "error" in the data. In that case,
  481. * sending the PM will be halted and the error
  482. * message is shown to the user.
  483. *
  484. * [example]
  485. * <hookcode>
  486. * function phorum_mod_foo_pm_send_init($message, $action)
  487. * {
  488. * if ($message['error'] !== NULL) return $message;
  489. *
  490. * // Enable "keep copy" option by default.
  491. * if ($action === NULL) {
  492. * $message['keep'] = 1;
  493. * }
  494. *
  495. * return $message;
  496. * }
  497. * </hookcode>
  498. */
  499. $pm_message = array(
  500. 'subject' => $_POST['subject'],
  501. 'message' => $_POST['message'],
  502. 'recipients' => $recipients,
  503. 'keep' => $_POST['keep'],
  504. 'error' => NULL
  505. );
  506. if (isset($PHORUM['hooks']['pm_before_send'])) {
  507. $pm_message = phorum_hook('pm_before_send', $pm_message);
  508. if ($pm_message['error']) {
  509. $error = $pm_message['error'];
  510. }
  511. }
  512. // Send the private message if no errors occurred.
  513. if (empty($error)) {
  514. $pm_message_id = phorum_db_pm_send($pm_message["subject"], $pm_message["message"], array_keys($pm_message['recipients']), NULL, $pm_message["keep"]);
  515. $pm_message['pm_message_id'] = $pm_message_id;
  516. $pm_message['from_username'] = $PHORUM['user']['display_name'];
  517. $pm_message['user_id'] = $user_id;
  518. // Show an error in case of problems.
  519. if (! $pm_message_id) {
  520. $error = $PHORUM["DATA"]["LANG"]["PMNotSent"];
  521. // Do e-mail notifications on successful sending.
  522. } elseif (!empty($PHORUM['allow_pm_email_notify'])) {
  523. include_once("./include/email_functions.php");
  524. // Sort all recipients that want a notify by language.
  525. $langrcpts = array();
  526. foreach ($pm_message['recipients'] as $rcpt_id => $rcpt) {
  527. if ($rcpt["pm_email_notify"]) {
  528. if (!isset($langrcpts[$rcpt["user_language"]])) {
  529. $langrcpts[$rcpt["user_language"]] = array($rcpt);
  530. } else {
  531. $langrcpts[$rcpt["user_language"]][] = $rcpt;
  532. }
  533. }
  534. }
  535. phorum_email_pm_notice($pm_message, $langrcpts);
  536. }
  537. if (isset($PHORUM["hooks"]["pm_sent"])) {
  538. phorum_hook("pm_sent", $pm_message, array_keys($pm_message['recipients']));
  539. }
  540. }
  541. // Invalidate user cache, to update message counts.
  542. phorum_cache_remove('user', $user_id);
  543. foreach ($recipients as $rcpt) {
  544. phorum_cache_remove('user', $rcpt["user_id"]);
  545. }
  546. $redirect_message = "PMSent";
  547. $page = "list";
  548. $folder_id = "inbox";
  549. }
  550. } else {
  551. $error = $PHORUM["DATA"]["LANG"]["PMNoRecipients"];
  552. }
  553. // Stay on the post page in case of errors. Redirect on success.
  554. if ($error) {
  555. $page = "send";
  556. } else {
  557. $redirect = true;
  558. }
  559. }
  560. break;
  561. // Actions that are triggered from the buddy list.
  562. case "buddies":
  563. // Delete all checked buddies.
  564. if (isset($_POST["delete"]) && isset($_POST["checked"])) {
  565. foreach($_POST["checked"] as $buddy_user_id) {
  566. phorum_db_pm_buddy_delete($buddy_user_id);
  567. if (isset($PHORUM["hooks"]["buddy_delete"]))
  568. phorum_hook("buddy_delete", $buddy_user_id);
  569. }
  570. }
  571. // Send a PM to the checked buddies.
  572. if (isset($_POST["send_pm"]) && isset($_POST["checked"])) {
  573. $pm_rcpts = $_POST["checked"];
  574. if (count($pm_rcpts)) {
  575. $redirect = true;
  576. $page = "send";
  577. } else {
  578. unset($pm_rcpts);
  579. }
  580. }
  581. break;
  582. // Add a user to this user's buddy list.
  583. case "addbuddy":
  584. $buddy_user_id = $PHORUM["args"]["addbuddy_id"];
  585. if (!empty($buddy_user_id)) {
  586. if (phorum_db_pm_buddy_add($buddy_user_id)) {
  587. $okmsg = $PHORUM["DATA"]["LANG"]["BuddyAddSuccess"];
  588. if (isset($PHORUM["hooks"]["buddy_add"]))
  589. phorum_hook("buddy_add", $buddy_user_id);
  590. } else {
  591. $error = $PHORUM["DATA"]["LANG"]["BuddyAddFail"];
  592. }
  593. }
  594. break;
  595. default:
  596. trigger_error(
  597. "Unhandled action for pm.php: " . htmlspecialchars($action),
  598. E_USER_ERROR
  599. );
  600. }
  601. // The action has been completed successfully.
  602. // Redirect the user to the result page.
  603. if ($redirect)
  604. {
  605. $args = array(
  606. PHORUM_PM_URL,
  607. "page=" . $page,
  608. "folder_id=" . $folder_id,
  609. );
  610. if (isset($pm_rcpts)) $args[] = "to_id=" . implode(':', $pm_rcpts);
  611. if (!empty($pm_id)) $args[] = "pm_id=" . $pm_id;
  612. if (!empty($redirect_message)) $args[] = "okmsg=" . $redirect_message;
  613. $redir_url = call_user_func_array('phorum_get_url', $args);
  614. phorum_redirect_by_url($redir_url);
  615. exit();
  616. }
  617. }
  618. // ------------------------------------------------------------------------
  619. // Display a PM page
  620. // ------------------------------------------------------------------------
  621. if(empty($PHORUM["DATA"]["HEADING"])){
  622. $PHORUM["DATA"]["HEADING"] = $PHORUM["DATA"]["LANG"]["PrivateMessages"];
  623. }
  624. // unset default description
  625. $PHORUM['DATA']['DESCRIPTION'] = '';
  626. $PHORUM['DATA']['HTML_DESCRIPTION'] = '';
  627. // Use the message list as the default page.
  628. if (!$page){
  629. $page = "list";
  630. $folder_id = PHORUM_PM_INBOX;
  631. }
  632. // Show an OK message for a redirected page?
  633. $okmsg_id = phorum_getparam('okmsg');
  634. if ($okmsg_id && isset($PHORUM["DATA"]["LANG"][$okmsg_id])) {
  635. $okmsg = $PHORUM["DATA"]["LANG"][$okmsg_id];
  636. }
  637. // Make error and OK messages available in the template.
  638. $PHORUM["DATA"]["ERROR"] = (empty($error)) ? "" : $error;
  639. $PHORUM["DATA"]["OKMSG"] = (empty($okmsg)) ? "" : $okmsg;
  640. $template = "";
  641. switch ($page) {
  642. // Manage the PM folders.
  643. case "folders":
  644. $PHORUM["DATA"]["CREATE_FOLDER_NAME"] = isset($_POST["create_folder_name"]) ? htmlspecialchars($_POST["create_folder_name"], ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]) : '';
  645. $PHORUM["DATA"]["RENAME_FOLDER_NAME"] = isset($_POST["rename_folder_name"]) ? htmlspecialchars($_POST["rename_folder_name"], ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]) : '';
  646. $template = "pm_folders";
  647. break;
  648. // Manage the buddies.
  649. case "buddies":
  650. // Retrieve a list of users that are buddies for the current user.
  651. $buddy_list = phorum_db_pm_buddy_list(NULL, true);
  652. if (count($buddy_list)) {
  653. $buddy_users = phorum_api_user_get(array_keys($buddy_list));
  654. if (isset($PHORUM["hooks"]["read_user_info"]))
  655. $buddy_users = phorum_hook("read_user_info", $buddy_users);
  656. } else {
  657. $buddy_users = array();
  658. }
  659. // Sort the buddies by name.
  660. function phorum_sort_buddy_list($a,$b) {
  661. return strcasecmp($a["display_name"], $b["display_name"]);
  662. }
  663. uasort($buddy_users, 'phorum_sort_buddy_list');
  664. $buddies = array();
  665. foreach ($buddy_users as $id => $buddy_user) {
  666. $buddy = array(
  667. 'user_id' => $id,
  668. 'display_name' =>
  669. (empty($PHORUM["custom_display_name"])
  670. ? htmlspecialchars($buddy_user["display_name"], ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"])
  671. : $buddy_user["display_name"]),
  672. 'mutual' => $buddy_list[$id]["mutual"],
  673. );
  674. $buddy["URL"]["PROFILE"] =
  675. phorum_get_url(PHORUM_PROFILE_URL, $buddy_user["user_id"]);
  676. if (!$buddy_user['hide_activity']) {
  677. $buddy["raw_date_last_active"] = $buddy_user["date_last_active"];
  678. $buddy["date_last_active"] = phorum_date($PHORUM["short_date_time"], $buddy_user["date_last_active"]);
  679. } else {
  680. $buddy["date_last_active"] = "-";
  681. }
  682. $buddies[$id] = $buddy;
  683. }
  684. /**
  685. * [hook]
  686. * buddy_list
  687. *
  688. * [availability]
  689. * Phorum 5 >= 5.2.7
  690. *
  691. * [description]
  692. * This hook can be used for reformatting a list of buddies.
  693. * Reformatting could mean things like changing the sort
  694. * order or modifying the fields in the buddy arrays.
  695. *
  696. * [category]
  697. * Buddies system
  698. *
  699. * [when]
  700. * Right after Phorum has formatted the buddy list. This is
  701. * primarily done when the list of buddies is shown in the
  702. * private message system.
  703. *
  704. * [input]
  705. * An array of buddy info arrays. Each info array contains a
  706. * couple of fields that describe the budy: user_id,
  707. * display_name, mutual (0 = not mutual, 1 = mutual),
  708. * URL->PROFILE, date_last_active (formatted date) and
  709. * raw_date_last_active (Epoch timestamp).
  710. *
  711. * [output]
  712. * The same array as was used for the hook call argument,
  713. * possibly with some updated fields in it.
  714. *
  715. * [example]
  716. * <hookcode>
  717. * function phorum_mod_foo_buddy_list($buddies)
  718. * {
  719. * // Add a CSS class around the display names for
  720. * // the mutual buddies (of course this could also
  721. * // easily be implemented as a pure template change,
  722. * // but remember that this is just an example).
  723. * foreach ($buddies as $id => $buddy)
  724. * {
  725. * if ($buddy['mutual'])
  726. * {
  727. * $buddies[$id]['display_name'] =
  728. * '<span class="mutual_buddy">' .
  729. * $buddy['display_name'] .
  730. * '</span>';
  731. * }
  732. * }
  733. *
  734. * return $buddies;
  735. * }
  736. * </hookcode>
  737. */
  738. if (isset($PHORUM['hooks']['buddy_list'])) {
  739. $buddies = phorum_hook('buddy_list', $buddies);
  740. }
  741. $PHORUM["DATA"]["USERTRACK"] = $PHORUM["track_user_activity"];
  742. $PHORUM["DATA"]["BUDDIES"] = $buddies;
  743. $PHORUM["DATA"]["BUDDYCOUNT"] = count($buddies);
  744. $PHORUM["DATA"]["PMLOCATION"] = $PHORUM["DATA"]["LANG"]["Buddies"];
  745. $template = "pm_buddies";
  746. break;
  747. // Show a listing of messages in a folder.
  748. case "list":
  749. // Check if the folder exists for the user.
  750. if (! isset($pm_folders[$folder_id])) {
  751. $PHORUM["DATA"]["BLOCK_CONTENT"] = $PHORUM["DATA"]["LANG"]["PMFolderNotAvailable"];
  752. $template = "stdblock";
  753. } else {
  754. $list = null;
  755. /**
  756. * [hook]
  757. * before_pm_list
  758. *
  759. * [availability]
  760. * Phorum 5 >= 5.2.17
  761. *
  762. * [description]
  763. * This hook can be used for retreiveing a list of messages
  764. * via an alernate method other than the built in.
  765. *
  766. * [category]
  767. * Private message system
  768. *
  769. * [when]
  770. * Before the private message list is retreived from the database
  771. *
  772. * [input]
  773. * A PM folder id
  774. *
  775. * [output]
  776. * A list of private messages compatible with
  777. * phourm_db_pm_list()
  778. *
  779. * [example]
  780. * <hookcode>
  781. * function phorum_mod_foo_before_pm_list($folder_id)
  782. * {
  783. * // Query the db directly and apply custom code
  784. *
  785. * return $messages;
  786. * }
  787. * </hookcode>
  788. */
  789. if (isset($PHORUM['hooks']['before_pm_list'])) {
  790. $list = phorum_hook('before_pm_list', $folder_id);
  791. }
  792. if(is_null($list)){
  793. $list = phorum_db_pm_list($folder_id);
  794. }
  795. // Prepare data for the templates (formatting and XSS prevention).
  796. $list = phorum_pm_format($list);
  797. /**
  798. * [hook]
  799. * pm_list
  800. *
  801. * [availability]
  802. * Phorum 5 >= 5.2.7
  803. *
  804. * [description]
  805. * This hook can be used for reformatting a list of
  806. * private messages.
  807. *
  808. * [category]
  809. * Private message system
  810. *
  811. * [when]
  812. * Right after Phorum has formatted the private message list.
  813. * This is primarily done when a list of private messages is
  814. * shown in the private message system.
  815. *
  816. * [input]
  817. * An array of private message info arrays.
  818. *
  819. * [output]
  820. * The same array as was used for the hook call argument,
  821. * possibly with some updated fields in it.
  822. *
  823. * [example]
  824. * <hookcode>
  825. * function phorum_mod_foo_pm_list($messages)
  826. * {
  827. * // Filter out private messages that are sent by
  828. * // evil user X with user_id 666.
  829. * foreach ($messages as $id => $message) {
  830. * if ($message['user_id'] == 666) {
  831. * unset($messages[$id]);
  832. * }
  833. * }
  834. * return $messages;
  835. * }
  836. * </hookcode>
  837. */
  838. if (isset($PHORUM['hooks']['pm_list'])) {
  839. $list = phorum_hook('pm_list', $list);
  840. }
  841. // Setup template variables.
  842. $PHORUM["DATA"]["MESSAGECOUNT"] = count($list);
  843. $PHORUM["DATA"]["MESSAGES"] = $list;
  844. $PHORUM["DATA"]["PMLOCATION"] = $pm_folders[$folder_id]["name"];
  845. $template = "pm_list";
  846. }
  847. break;
  848. // Read a single private message.
  849. case "read":
  850. if (($message=phorum_db_pm_get($pm_id, $folder_id))) {
  851. // Mark the message read.
  852. if (! $message['read_flag']) {
  853. phorum_db_pm_setflag($message["pm_message_id"], PHORUM_PM_READ_FLAG, true);
  854. // Invalidate user cache, to update message counts.
  855. phorum_cache_remove('user',$user_id);
  856. }
  857. // Run the message through the default message formatting.
  858. list($message) = phorum_pm_format(array($message));
  859. // We do not want to show a recipient list if there are
  860. // a lot of recipients.
  861. $message["show_recipient_list"] = ($message["recipient_count"]<10);
  862. /**
  863. * [hook]
  864. * pm_read
  865. *
  866. * [availability]
  867. * Phorum 5 >= 5.2.7
  868. *
  869. * [description]
  870. * This hook can be used for reformatting a single private
  871. * message for reading.
  872. *
  873. * [category]
  874. * Private message system
  875. *
  876. * [when]
  877. * Right after Phorum has formatted the private message.
  878. * This is primarily done when a private message read page is
  879. * shown in the private message system.
  880. *
  881. * [input]
  882. * An array, describing a single private message.
  883. *
  884. * [output]
  885. * The same array as was used for the hook call argument,
  886. * possibly with some updated fields in it.
  887. *
  888. * [example}
  889. * <hookcode>
  890. * function phorum_mod_foo_pm_read($message)
  891. * {
  892. * // Add a notice to messages that were sent by
  893. * // evil user X with user_id 666.
  894. * if ($message['user_id'] == 666) {
  895. * $message['subject'] .= ' [EVIL!]';
  896. * }
  897. *
  898. * return $message;
  899. * }
  900. * </hookcode>
  901. */
  902. if (isset($PHORUM['hooks']['pm_read'])) {
  903. $message = phorum_hook('pm_read', $message);
  904. }
  905. $PHORUM["DATA"]["MESSAGE"] = $message;
  906. $PHORUM["DATA"]["PMLOCATION"] = $PHORUM["DATA"]["LANG"]["PMRead"];
  907. // re-init folder list to account for change in read flags
  908. $pm_folders = phorum_db_pm_getfolders(NULL, true);
  909. // Set folder id to the right folder for this message.
  910. $folder_id = $message["pm_folder_id"];
  911. if ($folder_id == 0) {
  912. $folder_id = $message["special_folder"];
  913. }
  914. $template = "pm_read";
  915. } else {
  916. // The message was not found. Show an error.
  917. $PHORUM["DATA"]["BLOCK_CONTENT"] = $PHORUM["DATA"]["LANG"]["PMNotAvailable"];
  918. $template = "stdblock";
  919. }
  920. break;
  921. // Post a new private message.
  922. case "send":
  923. // Setup the default array with the message data.
  924. $msg = array(
  925. "user_id" => $PHORUM["user"]["user_id"],
  926. "author" => $PHORUM["user"]["display_name"],
  927. "keep" => isset($_POST["keep"]) && $_POST["keep"] ? 1 : 0,
  928. "subject" => isset($_POST["subject"]) ? $_POST["subject"] : '',
  929. "message" => isset($_POST["message"]) ? $_POST["message"] : '',
  930. "preview" => isset($_POST["preview"]) ? 1 : 0,
  931. "recipients" => $recipients,
  932. );
  933. // Data initialization for posting messages on first request.
  934. if ($action === NULL || $action != "post")
  935. {
  936. // Setup data for sending a private message to specified recipients.
  937. // Recipients are passed on as a standard phorum argument "to_id"
  938. // containing a colon separated list of users.
  939. if (isset($PHORUM["args"]["to_id"])) {
  940. foreach (explode(":", $PHORUM["args"]["to_id"]) as $rcpt_id) {
  941. settype($rcpt_id, "int");
  942. $user = phorum_api_user_get($rcpt_id);
  943. if ($user) {
  944. $msg["recipients"][$rcpt_id] = array(
  945. "display_name" => $user["display_name"],
  946. "user_id" => $user["user_id"]
  947. );
  948. }
  949. }
  950. $hide_userselect = 1;
  951. // Setup data for replying to a private message.
  952. } elseif (isset($pm_id)) {
  953. $message = phorum_db_pm_get($pm_id);
  954. $msg["subject"] = $message["subject"];
  955. $msg["message"] = $message["message"];
  956. $msg["recipients"][$message["user_id"]] = array(
  957. "display_name" => $message["author"],
  958. "user_id" => $message["user_id"]
  959. );
  960. $msg = phorum_pm_quoteformat($message["author"], $message["user_id"], $msg);
  961. // Include the other recipient, excecpt the active
  962. // user himself, when replying to all.
  963. if (isset($_POST["reply_to_all"])) {
  964. foreach($message["recipients"] as $rcpt) {
  965. if ($user_id == $rcpt["user_id"]) continue;
  966. $msg["recipients"][$rcpt["user_id"]] = array(
  967. "display_name" => $rcpt["display_name"],
  968. "user_id" => $rcpt["user_id"],
  969. );
  970. }
  971. }
  972. $hide_userselect = 1;
  973. // Setup data for replying privately to a forum post.
  974. } elseif (isset($PHORUM["args"]["message_id"])) {
  975. $message = phorum_db_get_message($PHORUM["args"]["message_id"], "message_id", true);
  976. if (phorum_api_user_check_access(PHORUM_USER_ALLOW_READ) && ($PHORUM["forum_id"]==$message["forum_id"] || $message["forum_id"] == 0)) {
  977. // get url to the message board thread
  978. $origurl = phorum_get_url(PHORUM_READ_URL, $message["thread"], $message["message_id"]);
  979. // Get the data for the user that we reply to.
  980. $user = phorum_api_user_get($message["user_id"]);
  981. $msg["subject"] = $message["subject"];
  982. $msg["message"] = $message["body"];
  983. $msg["recipients"][$message["user_id"]] = array(
  984. 'display_name' => $user["display_name"],
  985. 'user_id' => $user["user_id"]
  986. );
  987. $msg = phorum_pm_quoteformat($user["display_name"], $user["user_id"], $msg, $origurl);
  988. }
  989. $hide_userselect = 1;
  990. }
  991. }
  992. /**
  993. * [hook]
  994. * pm_send_init
  995. *
  996. * [availability]
  997. * Phorum 5 >= 5.2.15
  998. *
  999. * [description]
  1000. * This hook can be used for doing modifications to the
  1001. * PM message data that is used for sending a PM at an
  1002. * early stage in the request.
  1003. *
  1004. * [category]
  1005. * Private message system
  1006. *
  1007. * [when]
  1008. * At the start of "send" page handling, after the code that sets
  1009. * up the message values on the first request.
  1010. *
  1011. * [input]
  1012. * Two arguments: the private message data array and the action that
  1013. * is being handled (one of NULL (initial request), rpct_add,
  1014. * preview, posting).
  1015. *
  1016. * [output]
  1017. * The private message data, possibly modified.
  1018. *
  1019. * [example]
  1020. * <hookcode>
  1021. * function phorum_mod_foo_pm_send_init($message, $action)
  1022. * {
  1023. * // Enable "keep copy" option by default.
  1024. * if ($action === NULL) {
  1025. * $message['keep'] = 1;
  1026. * }
  1027. *
  1028. * return $message;
  1029. * }
  1030. * </hookcode>
  1031. */
  1032. if (isset($PHORUM['hooks']['pm_send_init'])) {
  1033. $msg = phorum_hook('pm_send_init', $msg, $action);
  1034. }
  1035. // Setup data for previewing a message.
  1036. if ($msg["preview"]) {
  1037. list($preview) = phorum_pm_format(array($msg));
  1038. $PHORUM["DATA"]["PREVIEW"] = $preview;
  1039. }
  1040. // XSS prevention.
  1041. foreach ($msg as $key => $val) {
  1042. switch ($key) {
  1043. case "recipients": {
  1044. foreach ($val as $id => $data) {
  1045. $msg[$key][$id]["display_name"] =
  1046. (empty($PHORUM["custom_display_name"])
  1047. ? htmlspecialchars($data["display_name"], ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"])
  1048. : $data["display_name"]);
  1049. }
  1050. break;
  1051. }
  1052. case "author": {
  1053. $msg[$key] =
  1054. (empty($PHORUM["custom_display_name"])
  1055. ? htmlspecialchars($val, ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]) : $val);
  1056. break;
  1057. }
  1058. default: {
  1059. $msg[$key] = htmlspecialchars($val, ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]);
  1060. break;
  1061. }
  1062. }
  1063. }
  1064. $PHORUM["DATA"]["MESSAGE"] = $msg;
  1065. $PHORUM["DATA"]["RECIPIENT_COUNT"] = count($msg["recipients"]);
  1066. $PHORUM["DATA"]["SHOW_USERSELECTION"] = true;
  1067. // Determine what input element gets the focus.
  1068. $focus_id = 'userselection';
  1069. if ($PHORUM["DATA"]["RECIPIENT_COUNT"]) $focus_id = 'subject';
  1070. if (!empty($msg["subject"])) $focus_id = 'body';
  1071. $PHORUM["DATA"]["FOCUS_TO_ID"] = $focus_id;
  1072. // Create data for a user dropdown list, if configured.
  1073. if ($PHORUM["DATA"]["SHOW_USERSELECTION"] && $PHORUM["enable_dropdown_userlist"])
  1074. {
  1075. $allusers = array();
  1076. $userlist = phorum_api_user_list(PHORUM_GET_ACTIVE);
  1077. foreach ($userlist as $user_id => $userinfo){
  1078. if (isset($msg["recipients"][$user_id])) continue;
  1079. $userinfo["display_name"] = htmlspecialchars($userinfo["display_name"], ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]);
  1080. $userinfo["user_id"] = $user_id;
  1081. $allusers[] = $userinfo;
  1082. }
  1083. $PHORUM["DATA"]["USERS"] = $allusers;
  1084. if (count($allusers) == 0) $PHORUM["DATA"]["SHOW_USERSELECTION"] = false;
  1085. }
  1086. /**
  1087. * [hook]
  1088. * pm_before_editor
  1089. *
  1090. * [availability]
  1091. * Phorum 5 >= 5.2.15
  1092. *
  1093. * [description]
  1094. * This hook can be used for tweaking the template data that
  1095. * is used for setting up the private message editor.
  1096. *
  1097. * [category]
  1098. * Private message system
  1099. *
  1100. * [when]
  1101. * Right after Phorum has formatted the template data for the
  1102. * editor and just before the editor template is loaded.
  1103. *
  1104. * [input]
  1105. * Two arguments: the private message data array and the action that
  1106. * is being handled (one of NULL (initial request), rpct_add,
  1107. * preview, posting).
  1108. *
  1109. * [output]
  1110. * The private message data, possibly modified.
  1111. *
  1112. * [example]
  1113. * <hookcode>
  1114. * function phorum_mod_foo_pm_before_editor($message)
  1115. * {
  1116. * return $message;
  1117. * }
  1118. * </hookcode>
  1119. */
  1120. if (isset($PHORUM['hooks']['pm_before_editor'])) {
  1121. $msg = phorum_hook('pm_befo…

Large files files are truncated, but you can click here to view the full file