PageRenderTime 46ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/functions_privmsgs.php

https://bitbucket.org/jablonski/yebood
PHP | 2180 lines | 1641 code | 335 blank | 204 comment | 273 complexity | ab726daf8c051014c9112249d901df2c MD5 | raw file
Possible License(s): AGPL-1.0

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

  1. <?php
  2. /**
  3. *
  4. * @package phpBB3
  5. * @version $Id$
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. */
  12. if (!defined('IN_PHPBB'))
  13. {
  14. exit;
  15. }
  16. /*
  17. Ability to simply add own rules by doing three things:
  18. 1) Add an appropriate constant
  19. 2) Add a new check array to the global_privmsgs_rules variable and the condition array (if one is required)
  20. 3) Add a new language variable to ucp.php
  21. The user is then able to select the new rule. It will be checked against and handled as specified.
  22. To add new actions (yes, checks can be added here too) to the rule management, the core code has to be modified.
  23. */
  24. define('RULE_IS_LIKE', 1); // Is Like
  25. define('RULE_IS_NOT_LIKE', 2); // Is Not Like
  26. define('RULE_IS', 3); // Is
  27. define('RULE_IS_NOT', 4); // Is Not
  28. define('RULE_BEGINS_WITH', 5); // Begins with
  29. define('RULE_ENDS_WITH', 6); // Ends with
  30. define('RULE_IS_FRIEND', 7); // Is Friend
  31. define('RULE_IS_FOE', 8); // Is Foe
  32. define('RULE_IS_USER', 9); // Is User
  33. define('RULE_IS_GROUP', 10); // Is In Usergroup
  34. define('RULE_ANSWERED', 11); // Answered
  35. define('RULE_FORWARDED', 12); // Forwarded
  36. define('RULE_TO_GROUP', 14); // Usergroup
  37. define('RULE_TO_ME', 15); // Me
  38. define('ACTION_PLACE_INTO_FOLDER', 1);
  39. define('ACTION_MARK_AS_READ', 2);
  40. define('ACTION_MARK_AS_IMPORTANT', 3);
  41. define('ACTION_DELETE_MESSAGE', 4);
  42. define('CHECK_SUBJECT', 1);
  43. define('CHECK_SENDER', 2);
  44. define('CHECK_MESSAGE', 3);
  45. define('CHECK_STATUS', 4);
  46. define('CHECK_TO', 5);
  47. /**
  48. * Global private message rules
  49. * These rules define what to do if a rule is hit
  50. */
  51. $global_privmsgs_rules = array(
  52. CHECK_SUBJECT => array(
  53. RULE_IS_LIKE => array('check0' => 'message_subject', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
  54. RULE_IS_NOT_LIKE => array('check0' => 'message_subject', 'function' => '!(preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0}))'),
  55. RULE_IS => array('check0' => 'message_subject', 'function' => '{CHECK0} == {STRING}'),
  56. RULE_IS_NOT => array('check0' => 'message_subject', 'function' => '{CHECK0} != {STRING}'),
  57. RULE_BEGINS_WITH => array('check0' => 'message_subject', 'function' => 'preg_match("/^" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
  58. RULE_ENDS_WITH => array('check0' => 'message_subject', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "$/i", {CHECK0})'),
  59. ),
  60. CHECK_SENDER => array(
  61. RULE_IS_LIKE => array('check0' => 'username', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
  62. RULE_IS_NOT_LIKE => array('check0' => 'username', 'function' => '!(preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0}))'),
  63. RULE_IS => array('check0' => 'username', 'function' => '{CHECK0} == {STRING}'),
  64. RULE_IS_NOT => array('check0' => 'username', 'function' => '{CHECK0} != {STRING}'),
  65. RULE_BEGINS_WITH => array('check0' => 'username', 'function' => 'preg_match("/^" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
  66. RULE_ENDS_WITH => array('check0' => 'username', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "$/i", {CHECK0})'),
  67. RULE_IS_FRIEND => array('check0' => 'friend', 'function' => '{CHECK0} == 1'),
  68. RULE_IS_FOE => array('check0' => 'foe', 'function' => '{CHECK0} == 1'),
  69. RULE_IS_USER => array('check0' => 'author_id', 'function' => '{CHECK0} == {USER_ID}'),
  70. RULE_IS_GROUP => array('check0' => 'author_in_group', 'function' => 'in_array({GROUP_ID}, {CHECK0})'),
  71. ),
  72. CHECK_MESSAGE => array(
  73. RULE_IS_LIKE => array('check0' => 'message_text', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
  74. RULE_IS_NOT_LIKE => array('check0' => 'message_text', 'function' => '!(preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0}))'),
  75. RULE_IS => array('check0' => 'message_text', 'function' => '{CHECK0} == {STRING}'),
  76. RULE_IS_NOT => array('check0' => 'message_text', 'function' => '{CHECK0} != {STRING}'),
  77. ),
  78. CHECK_STATUS => array(
  79. RULE_ANSWERED => array('check0' => 'pm_replied', 'function' => '{CHECK0} == 1'),
  80. RULE_FORWARDED => array('check0' => 'pm_forwarded', 'function' => '{CHECK0} == 1'),
  81. ),
  82. CHECK_TO => array(
  83. RULE_TO_GROUP => array('check0' => 'to', 'check1' => 'bcc', 'check2' => 'user_in_group', 'function' => 'in_array("g_" . {CHECK2}, {CHECK0}) || in_array("g_" . {CHECK2}, {CHECK1})'),
  84. RULE_TO_ME => array('check0' => 'to', 'check1' => 'bcc', 'function' => 'in_array("u_" . $user_id, {CHECK0}) || in_array("u_" . $user_id, {CHECK1})'),
  85. )
  86. );
  87. /**
  88. * This is for defining which condition fields to show for which Rule
  89. */
  90. $global_rule_conditions = array(
  91. RULE_IS_LIKE => 'text',
  92. RULE_IS_NOT_LIKE => 'text',
  93. RULE_IS => 'text',
  94. RULE_IS_NOT => 'text',
  95. RULE_BEGINS_WITH => 'text',
  96. RULE_ENDS_WITH => 'text',
  97. RULE_IS_USER => 'user',
  98. RULE_IS_GROUP => 'group'
  99. );
  100. /**
  101. * Get all folder
  102. */
  103. function get_folder($user_id, $folder_id = false)
  104. {
  105. global $db, $user, $template;
  106. global $phpbb_root_path, $phpEx;
  107. $folder = array();
  108. // Get folder information
  109. $sql = 'SELECT folder_id, COUNT(msg_id) as num_messages, SUM(pm_unread) as num_unread
  110. FROM ' . PRIVMSGS_TO_TABLE . "
  111. WHERE user_id = $user_id
  112. AND folder_id <> " . PRIVMSGS_NO_BOX . '
  113. GROUP BY folder_id';
  114. $result = $db->sql_query($sql);
  115. $num_messages = $num_unread = array();
  116. while ($row = $db->sql_fetchrow($result))
  117. {
  118. $num_messages[(int) $row['folder_id']] = $row['num_messages'];
  119. $num_unread[(int) $row['folder_id']] = $row['num_unread'];
  120. }
  121. $db->sql_freeresult($result);
  122. // Make sure the default boxes are defined
  123. $available_folder = array(PRIVMSGS_INBOX, PRIVMSGS_OUTBOX, PRIVMSGS_SENTBOX);
  124. foreach ($available_folder as $default_folder)
  125. {
  126. if (!isset($num_messages[$default_folder]))
  127. {
  128. $num_messages[$default_folder] = 0;
  129. }
  130. if (!isset($num_unread[$default_folder]))
  131. {
  132. $num_unread[$default_folder] = 0;
  133. }
  134. }
  135. // Adjust unread status for outbox
  136. $num_unread[PRIVMSGS_OUTBOX] = $num_messages[PRIVMSGS_OUTBOX];
  137. $folder[PRIVMSGS_INBOX] = array(
  138. 'folder_name' => $user->lang['PM_INBOX'],
  139. 'num_messages' => $num_messages[PRIVMSGS_INBOX],
  140. 'unread_messages' => $num_unread[PRIVMSGS_INBOX]
  141. );
  142. // Custom Folder
  143. $sql = 'SELECT folder_id, folder_name, pm_count
  144. FROM ' . PRIVMSGS_FOLDER_TABLE . "
  145. WHERE user_id = $user_id";
  146. $result = $db->sql_query($sql);
  147. while ($row = $db->sql_fetchrow($result))
  148. {
  149. $folder[$row['folder_id']] = array(
  150. 'folder_name' => $row['folder_name'],
  151. 'num_messages' => $row['pm_count'],
  152. 'unread_messages' => ((isset($num_unread[$row['folder_id']])) ? $num_unread[$row['folder_id']] : 0)
  153. );
  154. }
  155. $db->sql_freeresult($result);
  156. $folder[PRIVMSGS_OUTBOX] = array(
  157. 'folder_name' => $user->lang['PM_OUTBOX'],
  158. 'num_messages' => $num_messages[PRIVMSGS_OUTBOX],
  159. 'unread_messages' => $num_unread[PRIVMSGS_OUTBOX]
  160. );
  161. $folder[PRIVMSGS_SENTBOX] = array(
  162. 'folder_name' => $user->lang['PM_SENTBOX'],
  163. 'num_messages' => $num_messages[PRIVMSGS_SENTBOX],
  164. 'unread_messages' => $num_unread[PRIVMSGS_SENTBOX]
  165. );
  166. // Define Folder Array for template designers (and for making custom folders usable by the template too)
  167. foreach ($folder as $f_id => $folder_ary)
  168. {
  169. $folder_id_name = ($f_id == PRIVMSGS_INBOX) ? 'inbox' : (($f_id == PRIVMSGS_OUTBOX) ? 'outbox' : 'sentbox');
  170. $template->assign_block_vars('folder', array(
  171. 'FOLDER_ID' => $f_id,
  172. 'FOLDER_NAME' => $folder_ary['folder_name'],
  173. 'NUM_MESSAGES' => $folder_ary['num_messages'],
  174. 'UNREAD_MESSAGES' => $folder_ary['unread_messages'],
  175. 'U_FOLDER' => ($f_id > 0) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=' . $f_id) : append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=' . $folder_id_name),
  176. 'S_CUR_FOLDER' => ($f_id === $folder_id) ? true : false,
  177. 'S_UNREAD_MESSAGES' => ($folder_ary['unread_messages']) ? true : false,
  178. 'S_CUSTOM_FOLDER' => ($f_id > 0) ? true : false)
  179. );
  180. }
  181. if ($folder_id !== false && !isset($folder[$folder_id]))
  182. {
  183. trigger_error('UNKNOWN_FOLDER');
  184. }
  185. return $folder;
  186. }
  187. /**
  188. * Delete Messages From Sentbox
  189. * we are doing this here because this saves us a bunch of checks and queries
  190. */
  191. function clean_sentbox($num_sentbox_messages)
  192. {
  193. global $db, $user, $config;
  194. // Check Message Limit
  195. if ($user->data['message_limit'] && $num_sentbox_messages > $user->data['message_limit'])
  196. {
  197. // Delete old messages
  198. $sql = 'SELECT t.msg_id
  199. FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p
  200. WHERE t.msg_id = p.msg_id
  201. AND t.user_id = ' . $user->data['user_id'] . '
  202. AND t.folder_id = ' . PRIVMSGS_SENTBOX . '
  203. ORDER BY p.message_time ASC';
  204. $result = $db->sql_query_limit($sql, ($num_sentbox_messages - $user->data['message_limit']));
  205. $delete_ids = array();
  206. while ($row = $db->sql_fetchrow($result))
  207. {
  208. $delete_ids[] = $row['msg_id'];
  209. }
  210. $db->sql_freeresult($result);
  211. delete_pm($user->data['user_id'], $delete_ids, PRIVMSGS_SENTBOX);
  212. }
  213. }
  214. /**
  215. * Check Rule against Message Information
  216. */
  217. function check_rule(&$rules, &$rule_row, &$message_row, $user_id)
  218. {
  219. global $user, $config;
  220. if (!isset($rules[$rule_row['rule_check']][$rule_row['rule_connection']]))
  221. {
  222. return false;
  223. }
  224. $check_ary = $rules[$rule_row['rule_check']][$rule_row['rule_connection']];
  225. // Replace Check Literals
  226. $evaluate = $check_ary['function'];
  227. $evaluate = preg_replace('/{(CHECK[0-9])}/', '$message_row[$check_ary[strtolower("\1")]]', $evaluate);
  228. // Replace Rule Literals
  229. $evaluate = preg_replace('/{(STRING|USER_ID|GROUP_ID)}/', '$rule_row["rule_" . strtolower("\1")]', $evaluate);
  230. // Evil Statement
  231. $result = false;
  232. eval('$result = (' . $evaluate . ') ? true : false;');
  233. if (!$result)
  234. {
  235. return false;
  236. }
  237. switch ($rule_row['rule_action'])
  238. {
  239. case ACTION_PLACE_INTO_FOLDER:
  240. return array('action' => $rule_row['rule_action'], 'folder_id' => $rule_row['rule_folder_id']);
  241. break;
  242. case ACTION_MARK_AS_READ:
  243. case ACTION_MARK_AS_IMPORTANT:
  244. return array('action' => $rule_row['rule_action'], 'pm_unread' => $message_row['pm_unread'], 'pm_marked' => $message_row['pm_marked']);
  245. break;
  246. case ACTION_DELETE_MESSAGE:
  247. global $db, $auth;
  248. // Check for admins/mods - users are not allowed to remove those messages...
  249. // We do the check here to make sure the data we use is consistent
  250. $sql = 'SELECT user_id, user_type, user_permissions
  251. FROM ' . USERS_TABLE . '
  252. WHERE user_id = ' . (int) $message_row['author_id'];
  253. $result = $db->sql_query($sql);
  254. $userdata = $db->sql_fetchrow($result);
  255. $db->sql_freeresult($result);
  256. $auth2 = new auth();
  257. $auth2->acl($userdata);
  258. if (!$auth2->acl_get('a_') && !$auth2->acl_get('m_') && !$auth2->acl_getf_global('m_'))
  259. {
  260. return array('action' => $rule_row['rule_action'], 'pm_unread' => $message_row['pm_unread'], 'pm_marked' => $message_row['pm_marked']);
  261. }
  262. return false;
  263. break;
  264. default:
  265. return false;
  266. }
  267. return false;
  268. }
  269. /**
  270. * Update user PM count
  271. */
  272. function update_pm_counts()
  273. {
  274. global $user, $db;
  275. // Update unread count
  276. $sql = 'SELECT COUNT(msg_id) as num_messages
  277. FROM ' . PRIVMSGS_TO_TABLE . '
  278. WHERE pm_unread = 1
  279. AND folder_id <> ' . PRIVMSGS_OUTBOX . '
  280. AND user_id = ' . $user->data['user_id'];
  281. $result = $db->sql_query($sql);
  282. $user->data['user_unread_privmsg'] = (int) $db->sql_fetchfield('num_messages');
  283. $db->sql_freeresult($result);
  284. // Update new pm count
  285. $sql = 'SELECT COUNT(msg_id) as num_messages
  286. FROM ' . PRIVMSGS_TO_TABLE . '
  287. WHERE pm_new = 1
  288. AND folder_id IN (' . PRIVMSGS_NO_BOX . ', ' . PRIVMSGS_HOLD_BOX . ')
  289. AND user_id = ' . $user->data['user_id'];
  290. $result = $db->sql_query($sql);
  291. $user->data['user_new_privmsg'] = (int) $db->sql_fetchfield('num_messages');
  292. $db->sql_freeresult($result);
  293. $db->sql_query('UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
  294. 'user_unread_privmsg' => (int) $user->data['user_unread_privmsg'],
  295. 'user_new_privmsg' => (int) $user->data['user_new_privmsg'],
  296. )) . ' WHERE user_id = ' . $user->data['user_id']);
  297. // Ok, here we need to repair something, other boxes than privmsgs_no_box and privmsgs_hold_box should not carry the pm_new flag.
  298. if (!$user->data['user_new_privmsg'])
  299. {
  300. $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
  301. SET pm_new = 0
  302. WHERE pm_new = 1
  303. AND folder_id NOT IN (' . PRIVMSGS_NO_BOX . ', ' . PRIVMSGS_HOLD_BOX . ')
  304. AND user_id = ' . $user->data['user_id'];
  305. $db->sql_query($sql);
  306. }
  307. }
  308. /**
  309. * Place new messages into appropriate folder
  310. */
  311. function place_pm_into_folder(&$global_privmsgs_rules, $release = false)
  312. {
  313. global $db, $user, $config;
  314. if (!$user->data['user_new_privmsg'])
  315. {
  316. return array('not_moved' => 0, 'removed' => 0);
  317. }
  318. $user_message_rules = (int) $user->data['user_message_rules'];
  319. $user_id = (int) $user->data['user_id'];
  320. $action_ary = $move_into_folder = array();
  321. $num_removed = 0;
  322. // Newly processing on-hold messages
  323. if ($release)
  324. {
  325. $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
  326. SET folder_id = ' . PRIVMSGS_NO_BOX . '
  327. WHERE folder_id = ' . PRIVMSGS_HOLD_BOX . "
  328. AND user_id = $user_id";
  329. $db->sql_query($sql);
  330. }
  331. // Get those messages not yet placed into any box
  332. $retrieve_sql = 'SELECT t.*, p.*, u.username, u.user_id, u.group_id
  333. FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p, ' . USERS_TABLE . " u
  334. WHERE t.user_id = $user_id
  335. AND p.author_id = u.user_id
  336. AND t.folder_id = " . PRIVMSGS_NO_BOX . '
  337. AND t.msg_id = p.msg_id';
  338. // Just place into the appropriate arrays if no rules need to be checked
  339. if (!$user_message_rules)
  340. {
  341. $result = $db->sql_query($retrieve_sql);
  342. while ($row = $db->sql_fetchrow($result))
  343. {
  344. $action_ary[$row['msg_id']][] = array('action' => false);
  345. }
  346. $db->sql_freeresult($result);
  347. }
  348. else
  349. {
  350. $user_rules = $zebra = $check_rows = array();
  351. $user_ids = $memberships = array();
  352. // First of all, grab all rules and retrieve friends/foes
  353. $sql = 'SELECT *
  354. FROM ' . PRIVMSGS_RULES_TABLE . "
  355. WHERE user_id = $user_id";
  356. $result = $db->sql_query($sql);
  357. $user_rules = $db->sql_fetchrowset($result);
  358. $db->sql_freeresult($result);
  359. if (sizeof($user_rules))
  360. {
  361. $sql = 'SELECT zebra_id, friend, foe
  362. FROM ' . ZEBRA_TABLE . "
  363. WHERE user_id = $user_id";
  364. $result = $db->sql_query($sql);
  365. while ($row = $db->sql_fetchrow($result))
  366. {
  367. $zebra[$row['zebra_id']] = $row;
  368. }
  369. $db->sql_freeresult($result);
  370. }
  371. // Now build a bare-bone check_row array
  372. $result = $db->sql_query($retrieve_sql);
  373. while ($row = $db->sql_fetchrow($result))
  374. {
  375. $check_rows[] = array_merge($row, array(
  376. 'to' => explode(':', $row['to_address']),
  377. 'bcc' => explode(':', $row['bcc_address']),
  378. 'friend' => (isset($zebra[$row['author_id']])) ? $zebra[$row['author_id']]['friend'] : 0,
  379. 'foe' => (isset($zebra[$row['author_id']])) ? $zebra[$row['author_id']]['foe'] : 0,
  380. 'user_in_group' => array($user->data['group_id']),
  381. 'author_in_group' => array())
  382. );
  383. $user_ids[] = $row['user_id'];
  384. }
  385. $db->sql_freeresult($result);
  386. // Retrieve user memberships
  387. if (sizeof($user_ids))
  388. {
  389. $sql = 'SELECT *
  390. FROM ' . USER_GROUP_TABLE . '
  391. WHERE ' . $db->sql_in_set('user_id', $user_ids) . '
  392. AND user_pending = 0';
  393. $result = $db->sql_query($sql);
  394. while ($row = $db->sql_fetchrow($result))
  395. {
  396. $memberships[$row['user_id']][] = $row['group_id'];
  397. }
  398. $db->sql_freeresult($result);
  399. }
  400. // Now place into the appropriate folder
  401. foreach ($check_rows as $row)
  402. {
  403. // Add membership if set
  404. if (isset($memberships[$row['author_id']]))
  405. {
  406. $row['author_in_group'] = $memberships[$row['user_id']];
  407. }
  408. // Check Rule - this should be very quick since we have all information we need
  409. $is_match = false;
  410. foreach ($user_rules as $rule_row)
  411. {
  412. if (($action = check_rule($global_privmsgs_rules, $rule_row, $row, $user_id)) !== false)
  413. {
  414. $is_match = true;
  415. $action_ary[$row['msg_id']][] = $action;
  416. }
  417. }
  418. if (!$is_match)
  419. {
  420. $action_ary[$row['msg_id']][] = array('action' => false);
  421. }
  422. }
  423. unset($user_rules, $zebra, $check_rows, $user_ids, $memberships);
  424. }
  425. // We place actions into arrays, to save queries.
  426. $sql = $unread_ids = $delete_ids = $important_ids = array();
  427. foreach ($action_ary as $msg_id => $msg_ary)
  428. {
  429. // It is allowed to execute actions more than once, except placing messages into folder
  430. $folder_action = $message_removed = false;
  431. foreach ($msg_ary as $pos => $rule_ary)
  432. {
  433. if ($folder_action && $rule_ary['action'] == ACTION_PLACE_INTO_FOLDER)
  434. {
  435. continue;
  436. }
  437. switch ($rule_ary['action'])
  438. {
  439. case ACTION_PLACE_INTO_FOLDER:
  440. // Folder actions have precedence, so we will remove any other ones
  441. $folder_action = true;
  442. $move_into_folder[(int) $rule_ary['folder_id']][] = $msg_id;
  443. break;
  444. case ACTION_MARK_AS_READ:
  445. if ($rule_ary['pm_unread'])
  446. {
  447. $unread_ids[] = $msg_id;
  448. }
  449. break;
  450. case ACTION_DELETE_MESSAGE:
  451. $delete_ids[] = $msg_id;
  452. $message_removed = true;
  453. break;
  454. case ACTION_MARK_AS_IMPORTANT:
  455. if (!$rule_ary['pm_marked'])
  456. {
  457. $important_ids[] = $msg_id;
  458. }
  459. break;
  460. }
  461. }
  462. // We place this here because it could happen that the messages are doubled if a rule marks a message and then moves it into a specific
  463. // folder. Here we simply move the message into the INBOX if it gets not removed and also not put into a custom folder.
  464. if (!$folder_action && !$message_removed)
  465. {
  466. $move_into_folder[PRIVMSGS_INBOX][] = $msg_id;
  467. }
  468. }
  469. // Do not change the order of processing
  470. // The number of queries needed to be executed here highly depends on the defined rules and are
  471. // only gone through if new messages arrive.
  472. // Delete messages
  473. if (sizeof($delete_ids))
  474. {
  475. $num_removed += sizeof($delete_ids);
  476. delete_pm($user_id, $delete_ids, PRIVMSGS_NO_BOX);
  477. }
  478. // Set messages to Unread
  479. if (sizeof($unread_ids))
  480. {
  481. $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
  482. SET pm_unread = 0
  483. WHERE ' . $db->sql_in_set('msg_id', $unread_ids) . "
  484. AND user_id = $user_id
  485. AND folder_id = " . PRIVMSGS_NO_BOX;
  486. $db->sql_query($sql);
  487. }
  488. // mark messages as important
  489. if (sizeof($important_ids))
  490. {
  491. $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
  492. SET pm_marked = 1 - pm_marked
  493. WHERE folder_id = ' . PRIVMSGS_NO_BOX . "
  494. AND user_id = $user_id
  495. AND " . $db->sql_in_set('msg_id', $important_ids);
  496. $db->sql_query($sql);
  497. }
  498. // Move into folder
  499. $folder = array();
  500. if (sizeof($move_into_folder))
  501. {
  502. // Determine Full Folder Action - we need the move to folder id later eventually
  503. $full_folder_action = ($user->data['user_full_folder'] == FULL_FOLDER_NONE) ? ($config['full_folder_action'] - (FULL_FOLDER_NONE*(-1))) : $user->data['user_full_folder'];
  504. $sql_folder = array_keys($move_into_folder);
  505. if ($full_folder_action >= 0)
  506. {
  507. $sql_folder[] = $full_folder_action;
  508. }
  509. $sql = 'SELECT folder_id, pm_count
  510. FROM ' . PRIVMSGS_FOLDER_TABLE . '
  511. WHERE ' . $db->sql_in_set('folder_id', $sql_folder) . "
  512. AND user_id = $user_id";
  513. $result = $db->sql_query($sql);
  514. while ($row = $db->sql_fetchrow($result))
  515. {
  516. $folder[(int) $row['folder_id']] = (int) $row['pm_count'];
  517. }
  518. $db->sql_freeresult($result);
  519. unset($sql_folder);
  520. if (isset($move_into_folder[PRIVMSGS_INBOX]))
  521. {
  522. $sql = 'SELECT COUNT(msg_id) as num_messages
  523. FROM ' . PRIVMSGS_TO_TABLE . "
  524. WHERE user_id = $user_id
  525. AND folder_id = " . PRIVMSGS_INBOX;
  526. $result = $db->sql_query($sql);
  527. $folder[PRIVMSGS_INBOX] = (int) $db->sql_fetchfield('num_messages');
  528. $db->sql_freeresult($result);
  529. }
  530. }
  531. // Here we have ideally only one folder to move into
  532. foreach ($move_into_folder as $folder_id => $msg_ary)
  533. {
  534. $dest_folder = $folder_id;
  535. $full_folder_action = FULL_FOLDER_NONE;
  536. // Check Message Limit - we calculate with the complete array, most of the time it is one message
  537. // But we are making sure that the other way around works too (more messages in queue than allowed to be stored)
  538. if ($user->data['message_limit'] && $folder[$folder_id] && ($folder[$folder_id] + sizeof($msg_ary)) > $user->data['message_limit'])
  539. {
  540. $full_folder_action = ($user->data['user_full_folder'] == FULL_FOLDER_NONE) ? ($config['full_folder_action'] - (FULL_FOLDER_NONE*(-1))) : $user->data['user_full_folder'];
  541. // If destination folder itself is full...
  542. if ($full_folder_action >= 0 && ($folder[$full_folder_action] + sizeof($msg_ary)) > $user->data['message_limit'])
  543. {
  544. $full_folder_action = $config['full_folder_action'] - (FULL_FOLDER_NONE*(-1));
  545. }
  546. // If Full Folder Action is to move to another folder, we simply adjust the destination folder
  547. if ($full_folder_action >= 0)
  548. {
  549. $dest_folder = $full_folder_action;
  550. }
  551. else if ($full_folder_action == FULL_FOLDER_DELETE)
  552. {
  553. // Delete some messages. NOTE: Ordered by msg_id here instead of message_time!
  554. $sql = 'SELECT msg_id
  555. FROM ' . PRIVMSGS_TO_TABLE . "
  556. WHERE user_id = $user_id
  557. AND folder_id = $dest_folder
  558. ORDER BY msg_id ASC";
  559. $result = $db->sql_query_limit($sql, (($folder[$dest_folder] + sizeof($msg_ary)) - $user->data['message_limit']));
  560. $delete_ids = array();
  561. while ($row = $db->sql_fetchrow($result))
  562. {
  563. $delete_ids[] = $row['msg_id'];
  564. }
  565. $db->sql_freeresult($result);
  566. $num_removed += sizeof($delete_ids);
  567. delete_pm($user_id, $delete_ids, $dest_folder);
  568. }
  569. }
  570. //
  571. if ($full_folder_action == FULL_FOLDER_HOLD)
  572. {
  573. $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
  574. SET folder_id = ' . PRIVMSGS_HOLD_BOX . '
  575. WHERE folder_id = ' . PRIVMSGS_NO_BOX . "
  576. AND user_id = $user_id
  577. AND " . $db->sql_in_set('msg_id', $msg_ary);
  578. $db->sql_query($sql);
  579. }
  580. else
  581. {
  582. $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
  583. SET folder_id = $dest_folder, pm_new = 0
  584. WHERE folder_id = " . PRIVMSGS_NO_BOX . "
  585. AND user_id = $user_id
  586. AND pm_new = 1
  587. AND " . $db->sql_in_set('msg_id', $msg_ary);
  588. $db->sql_query($sql);
  589. if ($dest_folder != PRIVMSGS_INBOX)
  590. {
  591. $sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . '
  592. SET pm_count = pm_count + ' . (int) $db->sql_affectedrows() . "
  593. WHERE folder_id = $dest_folder
  594. AND user_id = $user_id";
  595. $db->sql_query($sql);
  596. }
  597. }
  598. }
  599. if (sizeof($action_ary))
  600. {
  601. // Move from OUTBOX to SENTBOX
  602. // We are not checking any full folder status here... SENTBOX is a special treatment (old messages get deleted)
  603. $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
  604. SET folder_id = ' . PRIVMSGS_SENTBOX . '
  605. WHERE folder_id = ' . PRIVMSGS_OUTBOX . '
  606. AND ' . $db->sql_in_set('msg_id', array_keys($action_ary));
  607. $db->sql_query($sql);
  608. }
  609. // Update new/unread count
  610. update_pm_counts();
  611. // Now check how many messages got not moved...
  612. $sql = 'SELECT COUNT(msg_id) as num_messages
  613. FROM ' . PRIVMSGS_TO_TABLE . "
  614. WHERE user_id = $user_id
  615. AND folder_id = " . PRIVMSGS_HOLD_BOX;
  616. $result = $db->sql_query($sql);
  617. $num_not_moved = (int) $db->sql_fetchfield('num_messages');
  618. $db->sql_freeresult($result);
  619. return array('not_moved' => $num_not_moved, 'removed' => $num_removed);
  620. }
  621. /**
  622. * Move PM from one to another folder
  623. */
  624. function move_pm($user_id, $message_limit, $move_msg_ids, $dest_folder, $cur_folder_id)
  625. {
  626. global $db, $user;
  627. global $phpbb_root_path, $phpEx;
  628. $num_moved = 0;
  629. if (!is_array($move_msg_ids))
  630. {
  631. $move_msg_ids = array($move_msg_ids);
  632. }
  633. if (sizeof($move_msg_ids) && !in_array($dest_folder, array(PRIVMSGS_NO_BOX, PRIVMSGS_OUTBOX, PRIVMSGS_SENTBOX)) &&
  634. !in_array($cur_folder_id, array(PRIVMSGS_NO_BOX, PRIVMSGS_OUTBOX)) && $cur_folder_id != $dest_folder)
  635. {
  636. // We have to check the destination folder ;)
  637. if ($dest_folder != PRIVMSGS_INBOX)
  638. {
  639. $sql = 'SELECT folder_id, folder_name, pm_count
  640. FROM ' . PRIVMSGS_FOLDER_TABLE . "
  641. WHERE folder_id = $dest_folder
  642. AND user_id = $user_id";
  643. $result = $db->sql_query($sql);
  644. $row = $db->sql_fetchrow($result);
  645. $db->sql_freeresult($result);
  646. if (!$row)
  647. {
  648. trigger_error('NOT_AUTHORISED');
  649. }
  650. if ($message_limit && $row['pm_count'] + sizeof($move_msg_ids) > $message_limit)
  651. {
  652. $message = sprintf($user->lang['NOT_ENOUGH_SPACE_FOLDER'], $row['folder_name']) . '<br /><br />';
  653. $message .= sprintf($user->lang['CLICK_RETURN_FOLDER'], '<a href="' . append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=' . $row['folder_id']) . '">', '</a>', $row['folder_name']);
  654. trigger_error($message);
  655. }
  656. }
  657. else
  658. {
  659. $sql = 'SELECT COUNT(msg_id) as num_messages
  660. FROM ' . PRIVMSGS_TO_TABLE . '
  661. WHERE folder_id = ' . PRIVMSGS_INBOX . "
  662. AND user_id = $user_id";
  663. $result = $db->sql_query($sql);
  664. $num_messages = (int) $db->sql_fetchfield('num_messages');
  665. $db->sql_freeresult($result);
  666. if ($message_limit && $num_messages + sizeof($move_msg_ids) > $message_limit)
  667. {
  668. $message = sprintf($user->lang['NOT_ENOUGH_SPACE_FOLDER'], $user->lang['PM_INBOX']) . '<br /><br />';
  669. $message .= sprintf($user->lang['CLICK_RETURN_FOLDER'], '<a href="' . append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=inbox') . '">', '</a>', $user->lang['PM_INBOX']);
  670. trigger_error($message);
  671. }
  672. }
  673. $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
  674. SET folder_id = $dest_folder
  675. WHERE folder_id = $cur_folder_id
  676. AND user_id = $user_id
  677. AND " . $db->sql_in_set('msg_id', $move_msg_ids);
  678. $db->sql_query($sql);
  679. $num_moved = $db->sql_affectedrows();
  680. // Update pm counts
  681. if ($num_moved)
  682. {
  683. if (!in_array($cur_folder_id, array(PRIVMSGS_INBOX, PRIVMSGS_OUTBOX, PRIVMSGS_SENTBOX)))
  684. {
  685. $sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . "
  686. SET pm_count = pm_count - $num_moved
  687. WHERE folder_id = $cur_folder_id
  688. AND user_id = $user_id";
  689. $db->sql_query($sql);
  690. }
  691. if ($dest_folder != PRIVMSGS_INBOX)
  692. {
  693. $sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . "
  694. SET pm_count = pm_count + $num_moved
  695. WHERE folder_id = $dest_folder
  696. AND user_id = $user_id";
  697. $db->sql_query($sql);
  698. }
  699. }
  700. }
  701. else if (in_array($cur_folder_id, array(PRIVMSGS_NO_BOX, PRIVMSGS_OUTBOX)))
  702. {
  703. trigger_error('CANNOT_MOVE_SPECIAL');
  704. }
  705. return $num_moved;
  706. }
  707. /**
  708. * Update unread message status
  709. */
  710. function update_unread_status($unread, $msg_id, $user_id, $folder_id)
  711. {
  712. if (!$unread)
  713. {
  714. return;
  715. }
  716. global $db, $user;
  717. $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
  718. SET pm_unread = 0
  719. WHERE msg_id = $msg_id
  720. AND user_id = $user_id
  721. AND folder_id = $folder_id";
  722. $db->sql_query($sql);
  723. $sql = 'UPDATE ' . USERS_TABLE . "
  724. SET user_unread_privmsg = user_unread_privmsg - 1
  725. WHERE user_id = $user_id";
  726. $db->sql_query($sql);
  727. if ($user->data['user_id'] == $user_id)
  728. {
  729. $user->data['user_unread_privmsg']--;
  730. // Try to cope with previous wrong conversions...
  731. if ($user->data['user_unread_privmsg'] < 0)
  732. {
  733. $sql = 'UPDATE ' . USERS_TABLE . "
  734. SET user_unread_privmsg = 0
  735. WHERE user_id = $user_id";
  736. $db->sql_query($sql);
  737. $user->data['user_unread_privmsg'] = 0;
  738. }
  739. }
  740. }
  741. /**
  742. * Handle all actions possible with marked messages
  743. */
  744. function handle_mark_actions($user_id, $mark_action)
  745. {
  746. global $db, $user, $phpbb_root_path, $phpEx;
  747. $msg_ids = request_var('marked_msg_id', array(0));
  748. $cur_folder_id = request_var('cur_folder_id', PRIVMSGS_NO_BOX);
  749. $confirm = (isset($_POST['confirm'])) ? true : false;
  750. if (!sizeof($msg_ids))
  751. {
  752. return false;
  753. }
  754. switch ($mark_action)
  755. {
  756. case 'mark_important':
  757. $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
  758. SET pm_marked = 1 - pm_marked
  759. WHERE folder_id = $cur_folder_id
  760. AND user_id = $user_id
  761. AND " . $db->sql_in_set('msg_id', $msg_ids);
  762. $db->sql_query($sql);
  763. break;
  764. case 'delete_marked':
  765. global $auth;
  766. if (!$auth->acl_get('u_pm_delete'))
  767. {
  768. trigger_error('NO_AUTH_DELETE_MESSAGE');
  769. }
  770. if (confirm_box(true))
  771. {
  772. delete_pm($user_id, $msg_ids, $cur_folder_id);
  773. $success_msg = (sizeof($msg_ids) == 1) ? 'MESSAGE_DELETED' : 'MESSAGES_DELETED';
  774. $redirect = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=' . $cur_folder_id);
  775. meta_refresh(3, $redirect);
  776. trigger_error($user->lang[$success_msg] . '<br /><br />' . sprintf($user->lang['RETURN_FOLDER'], '<a href="' . $redirect . '">', '</a>'));
  777. }
  778. else
  779. {
  780. $s_hidden_fields = array(
  781. 'cur_folder_id' => $cur_folder_id,
  782. 'mark_option' => 'delete_marked',
  783. 'submit_mark' => true,
  784. 'marked_msg_id' => $msg_ids
  785. );
  786. confirm_box(false, 'DELETE_MARKED_PM', build_hidden_fields($s_hidden_fields));
  787. }
  788. break;
  789. default:
  790. return false;
  791. }
  792. return true;
  793. }
  794. /**
  795. * Delete PM(s)
  796. */
  797. function delete_pm($user_id, $msg_ids, $folder_id)
  798. {
  799. global $db, $user, $phpbb_root_path, $phpEx;
  800. $user_id = (int) $user_id;
  801. $folder_id = (int) $folder_id;
  802. if (!$user_id)
  803. {
  804. return false;
  805. }
  806. if (!is_array($msg_ids))
  807. {
  808. if (!$msg_ids)
  809. {
  810. return false;
  811. }
  812. $msg_ids = array($msg_ids);
  813. }
  814. if (!sizeof($msg_ids))
  815. {
  816. return false;
  817. }
  818. // Get PM Information for later deleting
  819. $sql = 'SELECT msg_id, pm_unread, pm_new
  820. FROM ' . PRIVMSGS_TO_TABLE . '
  821. WHERE ' . $db->sql_in_set('msg_id', array_map('intval', $msg_ids)) . "
  822. AND folder_id = $folder_id
  823. AND user_id = $user_id";
  824. $result = $db->sql_query($sql);
  825. $delete_rows = array();
  826. $num_unread = $num_new = $num_deleted = 0;
  827. while ($row = $db->sql_fetchrow($result))
  828. {
  829. $num_unread += (int) $row['pm_unread'];
  830. $num_new += (int) $row['pm_new'];
  831. $delete_rows[$row['msg_id']] = 1;
  832. }
  833. $db->sql_freeresult($result);
  834. unset($msg_ids);
  835. if (!sizeof($delete_rows))
  836. {
  837. return false;
  838. }
  839. $db->sql_transaction('begin');
  840. // if no one has read the message yet (meaning it is in users outbox)
  841. // then mark the message as deleted...
  842. if ($folder_id == PRIVMSGS_OUTBOX)
  843. {
  844. // Remove PM from Outbox
  845. $sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . "
  846. WHERE user_id = $user_id AND folder_id = " . PRIVMSGS_OUTBOX . '
  847. AND ' . $db->sql_in_set('msg_id', array_keys($delete_rows));
  848. $db->sql_query($sql);
  849. // Update PM Information for safety
  850. $sql = 'UPDATE ' . PRIVMSGS_TABLE . " SET message_text = ''
  851. WHERE " . $db->sql_in_set('msg_id', array_keys($delete_rows));
  852. $db->sql_query($sql);
  853. // Set delete flag for those intended to receive the PM
  854. // We do not remove the message actually, to retain some basic information (sent time for example)
  855. $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
  856. SET pm_deleted = 1
  857. WHERE ' . $db->sql_in_set('msg_id', array_keys($delete_rows));
  858. $db->sql_query($sql);
  859. $num_deleted = $db->sql_affectedrows();
  860. }
  861. else
  862. {
  863. // Delete private message data
  864. $sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . "
  865. WHERE user_id = $user_id
  866. AND folder_id = $folder_id
  867. AND " . $db->sql_in_set('msg_id', array_keys($delete_rows));
  868. $db->sql_query($sql);
  869. $num_deleted = $db->sql_affectedrows();
  870. }
  871. // if folder id is user defined folder then decrease pm_count
  872. if (!in_array($folder_id, array(PRIVMSGS_INBOX, PRIVMSGS_OUTBOX, PRIVMSGS_SENTBOX, PRIVMSGS_NO_BOX)))
  873. {
  874. $sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . "
  875. SET pm_count = pm_count - $num_deleted
  876. WHERE folder_id = $folder_id";
  877. $db->sql_query($sql);
  878. }
  879. // Update unread and new status field
  880. if ($num_unread || $num_new)
  881. {
  882. $set_sql = ($num_unread) ? 'user_unread_privmsg = user_unread_privmsg - ' . $num_unread : '';
  883. if ($num_new)
  884. {
  885. $set_sql .= ($set_sql != '') ? ', ' : '';
  886. $set_sql .= 'user_new_privmsg = user_new_privmsg - ' . $num_new;
  887. }
  888. $db->sql_query('UPDATE ' . USERS_TABLE . " SET $set_sql WHERE user_id = $user_id");
  889. $user->data['user_new_privmsg'] -= $num_new;
  890. $user->data['user_unread_privmsg'] -= $num_unread;
  891. }
  892. // Now we have to check which messages we can delete completely
  893. $sql = 'SELECT msg_id
  894. FROM ' . PRIVMSGS_TO_TABLE . '
  895. WHERE ' . $db->sql_in_set('msg_id', array_keys($delete_rows));
  896. $result = $db->sql_query($sql);
  897. while ($row = $db->sql_fetchrow($result))
  898. {
  899. unset($delete_rows[$row['msg_id']]);
  900. }
  901. $db->sql_freeresult($result);
  902. $delete_ids = array_keys($delete_rows);
  903. if (sizeof($delete_ids))
  904. {
  905. // Check if there are any attachments we need to remove
  906. if (!function_exists('delete_attachments'))
  907. {
  908. include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  909. }
  910. delete_attachments('message', $delete_ids, false);
  911. $sql = 'DELETE FROM ' . PRIVMSGS_TABLE . '
  912. WHERE ' . $db->sql_in_set('msg_id', $delete_ids);
  913. $db->sql_query($sql);
  914. }
  915. $db->sql_transaction('commit');
  916. return true;
  917. }
  918. /**
  919. * Delete all PM(s) for a given user and delete the ones without references
  920. *
  921. * @param int $user_id ID of the user whose private messages we want to delete
  922. *
  923. * @return boolean False if there were no pms found, true otherwise.
  924. */
  925. function phpbb_delete_user_pms($user_id)
  926. {
  927. global $db, $user, $phpbb_root_path, $phpEx;
  928. $user_id = (int) $user_id;
  929. if (!$user_id)
  930. {
  931. return false;
  932. }
  933. // Get PM Information for later deleting
  934. // The two queries where split, so we can use our indexes
  935. $undelivered_msg = $delete_ids = array();
  936. // Part 1: get PMs the user received
  937. $sql = 'SELECT msg_id
  938. FROM ' . PRIVMSGS_TO_TABLE . '
  939. WHERE user_id = ' . $user_id;
  940. $result = $db->sql_query($sql);
  941. while ($row = $db->sql_fetchrow($result))
  942. {
  943. $msg_id = (int) $row['msg_id'];
  944. $delete_ids[$msg_id] = $msg_id;
  945. }
  946. $db->sql_freeresult($result);
  947. // Part 2: get PMs the user sent, but have yet to be received
  948. // We cannot simply delete them. First we have to check,
  949. // whether another user already received and read the message.
  950. $sql = 'SELECT msg_id
  951. FROM ' . PRIVMSGS_TO_TABLE . '
  952. WHERE author_id = ' . $user_id . '
  953. AND folder_id = ' . PRIVMSGS_NO_BOX;
  954. $result = $db->sql_query($sql);
  955. while ($row = $db->sql_fetchrow($result))
  956. {
  957. $msg_id = (int) $row['msg_id'];
  958. $undelivered_msg[$msg_id] = $msg_id;
  959. }
  960. $db->sql_freeresult($result);
  961. if (empty($delete_ids) && empty($undelivered_msg))
  962. {
  963. return false;
  964. }
  965. $db->sql_transaction('begin');
  966. if (!empty($undelivered_msg))
  967. {
  968. // A pm is delivered, if for any recipient the message was moved
  969. // from their NO_BOX to another folder. We do not delete such
  970. // messages, but only delete them for users, who have not yet
  971. // received them.
  972. $sql = 'SELECT msg_id
  973. FROM ' . PRIVMSGS_TO_TABLE . '
  974. WHERE author_id = ' . $user_id . '
  975. AND folder_id <> ' . PRIVMSGS_NO_BOX . '
  976. AND folder_id <> ' . PRIVMSGS_OUTBOX . '
  977. AND folder_id <> ' . PRIVMSGS_SENTBOX;
  978. $result = $db->sql_query($sql);
  979. $delivered_msg = array();
  980. while ($row = $db->sql_fetchrow($result))
  981. {
  982. $msg_id = (int) $row['msg_id'];
  983. $delivered_msg[$msg_id] = $msg_id;
  984. unset($undelivered_msg[$msg_id]);
  985. }
  986. $db->sql_freeresult($result);
  987. $undelivered_user = array();
  988. // Count the messages we delete, so we can correct the user pm data
  989. $sql = 'SELECT user_id, COUNT(msg_id) as num_undelivered_privmsgs
  990. FROM ' . PRIVMSGS_TO_TABLE . '
  991. WHERE author_id = ' . $user_id . '
  992. AND folder_id = ' . PRIVMSGS_NO_BOX . '
  993. AND ' . $db->sql_in_set('msg_id', array_merge($undelivered_msg, $delivered_msg)) . '
  994. GROUP BY user_id';
  995. $result = $db->sql_query($sql);
  996. while ($row = $db->sql_fetchrow($result))
  997. {
  998. $num_pms = (int) $row['num_undelivered_privmsgs'];
  999. $undelivered_user[$num_pms][] = (int) $row['user_id'];
  1000. if (sizeof($undelivered_user[$num_pms]) > 50)
  1001. {
  1002. // If there are too many users affected the query might get
  1003. // too long, so we update the value for the first bunch here.
  1004. $sql = 'UPDATE ' . USERS_TABLE . '
  1005. SET user_new_privmsg = user_new_privmsg - ' . $num_pms . ',
  1006. user_unread_privmsg = user_unread_privmsg - ' . $num_pms . '
  1007. WHERE ' . $db->sql_in_set('user_id', $undelivered_user[$num_pms]);
  1008. $db->sql_query($sql);
  1009. unset($undelivered_user[$num_pms]);
  1010. }
  1011. }
  1012. $db->sql_freeresult($result);
  1013. foreach ($undelivered_user as $num_pms => $undelivered_user_set)
  1014. {
  1015. $sql = 'UPDATE ' . USERS_TABLE . '
  1016. SET user_new_privmsg = user_new_privmsg - ' . $num_pms . ',
  1017. user_unread_privmsg = user_unread_privmsg - ' . $num_pms . '
  1018. WHERE ' . $db->sql_in_set('user_id', $undelivered_user_set);
  1019. $db->sql_query($sql);
  1020. }
  1021. if (!empty($delivered_msg))
  1022. {
  1023. $sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . '
  1024. WHERE folder_id = ' . PRIVMSGS_NO_BOX . '
  1025. AND ' . $db->sql_in_set('msg_id', $delivered_msg);
  1026. $db->sql_query($sql);
  1027. }
  1028. if (!empty($undelivered_msg))
  1029. {
  1030. $sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . '
  1031. WHERE ' . $db->sql_in_set('msg_id', $undelivered_msg);
  1032. $db->sql_query($sql);
  1033. $sql = 'DELETE FROM ' . PRIVMSGS_TABLE . '
  1034. WHERE ' . $db->sql_in_set('msg_id', $undelivered_msg);
  1035. $db->sql_query($sql);
  1036. }
  1037. }
  1038. // Reset the user's pm count to 0
  1039. $sql = 'UPDATE ' . USERS_TABLE . '
  1040. SET user_new_privmsg = 0,
  1041. user_unread_privmsg = 0
  1042. WHERE user_id = ' . $user_id;
  1043. $db->sql_query($sql);
  1044. // Delete private message data of the user
  1045. $sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . '
  1046. WHERE user_id = ' . (int) $user_id;
  1047. $db->sql_query($sql);
  1048. if (!empty($delete_ids))
  1049. {
  1050. // Now we have to check which messages we can delete completely
  1051. $sql = 'SELECT msg_id
  1052. FROM ' . PRIVMSGS_TO_TABLE . '
  1053. WHERE ' . $db->sql_in_set('msg_id', $delete_ids);
  1054. $result = $db->sql_query($sql);
  1055. while ($row = $db->sql_fetchrow($result))
  1056. {
  1057. unset($delete_ids[$row['msg_id']]);
  1058. }
  1059. $db->sql_freeresult($result);
  1060. if (!empty($delete_ids))
  1061. {
  1062. // Check if there are any attachments we need to remove
  1063. if (!function_exists('delete_attachments'))
  1064. {
  1065. include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  1066. }
  1067. delete_attachments('message', $delete_ids, false);
  1068. $sql = 'DELETE FROM ' . PRIVMSGS_TABLE . '
  1069. WHERE ' . $db->sql_in_set('msg_id', $delete_ids);
  1070. $db->sql_query($sql);
  1071. }
  1072. }
  1073. // Set the remaining author id to anonymous
  1074. // This way users are still able to read messages from users being removed
  1075. $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
  1076. SET author_id = ' . ANONYMOUS . '
  1077. WHERE author_id = ' . $user_id;
  1078. $db->sql_query($sql);
  1079. $sql = 'UPDATE ' . PRIVMSGS_TABLE . '
  1080. SET author_id = ' . ANONYMOUS . '
  1081. WHERE author_id = ' . $user_id;
  1082. $db->sql_query($sql);
  1083. $db->sql_transaction('commit');
  1084. return true;
  1085. }
  1086. /**
  1087. * Rebuild message header
  1088. */
  1089. function rebuild_header($check_ary)
  1090. {
  1091. global $db;
  1092. $address = array();
  1093. foreach ($check_ary as $check_type => $address_field)
  1094. {
  1095. // Split Addresses into users and groups
  1096. preg_match_all('/:?(u|g)_([0-9]+):?/', $address_field, $match);
  1097. $u = $g = array();
  1098. foreach ($match[1] as $id => $type)
  1099. {
  1100. ${$type}[] = (int) $match[2][$id];
  1101. }
  1102. $_types = array('u', 'g');
  1103. foreach ($_types as $type)
  1104. {
  1105. if (sizeof($$type))
  1106. {
  1107. foreach ($$type as $id)
  1108. {
  1109. $address[$type][$id] = $check_type;
  1110. }
  1111. }
  1112. }
  1113. }
  1114. return $address;
  1115. }
  1116. /**
  1117. * Print out/assign recipient information
  1118. */
  1119. function write_pm_addresses($check_ary, $author_id, $plaintext = false)
  1120. {
  1121. global $db, $user, $template, $phpbb_root_path, $phpEx;
  1122. $addresses = array();
  1123. foreach ($check_ary as $check_type => $address_field)
  1124. {
  1125. if (!is_array($address_field))
  1126. {
  1127. // Split Addresses into users and groups
  1128. preg_match_all('/:?(u|g)_([0-9]+):?/', $address_field, $match);
  1129. $u = $g = array();
  1130. foreach ($match[1] as $id => $type)
  1131. {
  1132. ${$type}[] = (int) $match[2][$id];
  1133. }
  1134. }
  1135. else
  1136. {
  1137. $u = $address_field['u'];
  1138. $g = $address_field['g'];
  1139. }
  1140. $address = array();
  1141. if (sizeof($u))
  1142. {
  1143. $sql = 'SELECT user_id, username, user_colour
  1144. FROM ' . USERS_TABLE . '
  1145. WHERE ' . $db->sql_in_set('user_id', $u);
  1146. $result = $db->sql_query($sql);
  1147. while ($row = $db->sql_fetchrow($result))
  1148. {
  1149. if ($check_type == 'to' || $author_id == $user->data['user_id'] || $row['user_id'] == $user->data['user_id'])
  1150. {
  1151. if ($plaintext)
  1152. {
  1153. $address[] = $row['username'];
  1154. }
  1155. else
  1156. {
  1157. $address['user'][$row['user_id']] = array('name' => $row['username'], 'colour' => $row['user_colour']);
  1158. }
  1159. }
  1160. }
  1161. $db->sql_freeresult($result);
  1162. }
  1163. if (sizeof($g))
  1164. {
  1165. if ($plaintext)
  1166. {
  1167. $sql = 'SELECT group_name, group_type
  1168. FROM ' . GROUPS_TABLE . '
  1169. WHERE ' . $db->sql_in_set('group_id', $g);
  1170. $result = $db->sql_query($sql);
  1171. while ($row = $db->sql_fetchrow($result))
  1172. {
  1173. if ($check_type == 'to' || $author_id == $user->data['user_id'] || $row['user_id'] == $user->data['user_id'])
  1174. {
  1175. $address[] = ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'];
  1176. }
  1177. }
  1178. $db->sql_freeresult($result);
  1179. }
  1180. else
  1181. {
  1182. $sql = 'SELECT g.group_id, g.group_name, g.group_colour, g.group_type, ug.user_id
  1183. FROM ' . GROUPS_TABLE . ' g, ' . USER_GROUP_TABLE . ' ug
  1184. WHERE ' . $db->sql_in_set('g.group_id', $g) . '
  1185. AND g.group_id = ug.group_id
  1186. AND ug.user_pending = 0';
  1187. $result = $db->sql_query($sql);
  1188. while ($row = $db->sql_fetchrow($result))
  1189. {
  1190. if (!isset($address['group'][$row['group_id']]))
  1191. {
  1192. if ($check_type == 'to' || $author_id == $user->data['user_id'] || $row['user_id'] == $user->data['user_id'])
  1193. {
  1194. $row['group_name'] = ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'];
  1195. $address['group'][$row['group_id']] = array('name' => $row['group_name'], 'colour' => $row['group_colour']);
  1196. }
  1197. }
  1198. if (isset($address['user'][$row['user_id']]))
  1199. {
  1200. $address['user'][$row['user_id']]['in_group'] = $row['group_id'];
  1201. }
  1202. }
  1203. $db->sql_freeresult($result);
  1204. }
  1205. }
  1206. if (sizeof($address) && !$plaintext)
  1207. {
  1208. $template->assign_var('S_' . strtoupper($check_type) . '_RECIPIENT', true);
  1209. foreach ($address as $type => $adr_ary)
  1210. {
  1211. foreach ($adr_ary as $id => $row)
  1212. {
  1213. $tpl_ary = array(
  1214. 'IS_GROUP' => ($type == 'group') ? true : false,
  1215. 'IS_USER' => ($type == 'user') ? true : false,
  1216. 'UG_ID' => $id,
  1217. 'NAME' => $row['name'],
  1218. 'COLOUR' => ($row['colour']) ? '#' . $row['colour'] : '',
  1219. 'TYPE' => $type,
  1220. );
  1221. if ($type == 'user')
  1222. {
  1223. $tpl_ary = array_merge($tpl_ary, array(
  1224. 'U_VIEW' => get_username_string('profile', $id, $row['name'], $row['colour']),
  1225. 'NAME_FULL' => get_username_string('full', $id, $row['name'], $row['colour']),
  1226. ));
  1227. }
  1228. else
  1229. {
  1230. $tpl_ary = array_merge($tpl_ary, array(
  1231. 'U_VIEW' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=group&amp;g=' . $id),
  1232. ));
  1233. }
  1234. $template->assign_block_vars($check_type . '_recipient', $tpl_ary);
  1235. }
  1236. }
  1237. }
  1238. $addresses[$check_type] = $address;
  1239. }
  1240. return $addresses;
  1241. }
  1242. /**
  1243. * Get folder status
  1244. */
  1245. function get_folder_status($folder_id, $folder)
  1246. {
  1247. global $db, $user, $config;
  1248. if (isset($folder[$folder_id]))
  1249. {
  1250. $folder = $folder[$folder_id];
  1251. }
  1252. else
  1253. {
  1254. return false;
  1255. }
  1256. $return = array(
  1257. 'folder_name' => $folder['folder_name'],
  1258. 'cur' => $folder['num_messages'],
  1259. 'remaining' => ($user->data['message_limit']) ? $user->data['message_limit'] - $folder['num_messages'] : 0,
  1260. 'max' => $user->data['message_limit'],
  1261. 'percent' => ($user->data['message_limit']) ? (($user->data['message_limit'] > 0) ? round(($folder['num_messages'] / $user->data['message_limit']) * 100) : 100) : 0,
  1262. );
  1263. $return['message'] = sprintf($user->lang['FOLDER_STATUS_MSG'], $return['percent'], $return['cur'], $return['max']);
  1264. return $return;
  1265. }
  1266. //
  1267. // COMPOSE MESSAGES
  1268. //
  1269. /**
  1270. * Submit PM
  1271. */
  1272. function submit_pm($mode, $subject, &$data, $put_in_outbox = true)
  1273. {
  1274. global $db, $auth, $config, $phpEx, $template, $user, $phpbb_root_path;
  1275. // We do not handle erasing pms here
  1276. if ($mode == 'delete')
  1277. {
  1278. return false;
  1279. }
  1280. $current_time = time();
  1281. // Collect some basic information about which tables and which rows to update/insert
  1282. $sql_data = array();
  1283. $root_level = 0;
  1284. // Recipient Information
  1285. $recipients = $to = $bcc = array();
  1286. if ($mode != 'edit')
  1287. {
  1288. // Build Recipient List
  1289. // u|g => array($user_id => 'to'|'bcc')
  1290. $_types = array('u', 'g');
  1291. foreach ($_types as $ug_type)
  1292. {
  1293. if (isset($data['address_list'][$ug_type]) && sizeof($data['address_list'][$ug_type]))
  1294. {
  1295. foreach ($data['address_list'][$ug_type] as $id => $field)
  1296. {
  1297. $id = (int) $id;
  1298. // Do not rely on the address list being "valid"
  1299. if (!$id || ($ug_type == 'u' && $id == ANONYMOUS))
  1300. {
  1301. continue;
  1302. }
  1303. $field = ($field == 'to') ? 'to' : 'bcc';
  1304. if ($ug_type == 'u')
  1305. {
  1306. $recipients[$id] = $field;
  1307. }
  1308. ${$field}[] = $ug_type . '_' . $id;
  1309. }
  1310. }
  1311. }
  1312. if (isset($data['address_list']['g']) && sizeof($data['address_list']['g']))
  1313. {
  1314. // We need to check the PM status of group members (do they want to receive PM's?)
  1315. // Only check if not a moderator or admin, since they are allowed to override this user setting
  1316. $sql_allow_pm = (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_')) ? ' AND u.user_allow_pm = 1' : '';
  1317. $sql = 'SELECT u.user_type, ug.group_id, ug.user_id
  1318. FROM ' . USERS_TABLE . ' u, ' . USER_GROUP_TABLE . ' ug
  1319. WHERE ' . $db->sql_in_set('ug.group_id', array_keys($data['address_list']['g'])) . '
  1320. AND ug.user_pending = 0
  1321. AND u.user_id = ug.user_id
  1322. AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')' .
  1323. $sql_allow_pm;
  1324. $result = $db->sql_query($sql);
  1325. while ($row = $db->sql_fetchrow($result))
  1326. {
  1327. $field = ($data['address_list']['g'][$row['group_id']] == 'to') ? 'to' : 'bcc';
  1328. $recipients[$row['user_id']] = $field;
  1329. }
  1330. $db->sql_freeresult($result);
  1331. }
  1332. if (!sizeof($recipients))
  1333. {
  1334. trigger_error('NO_RECIPIENT');
  1335. }
  1336. }
  1337. // First of all make sure the subject are having the correct length.
  1338. $subject = truncate_string($subject);
  1339. $db->sql_transaction('begin');
  1340. $sql = '';
  1341. switch ($mode)
  1342. {
  1343. case 'reply':
  1344. case 'quote':
  1345. $root_level = ($data['reply_from_root_level']) ? $data['reply_from_root_level'] : $data['reply_from_msg_id'];
  1346. // Set message_replied switch for this user
  1347. $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
  1348. SET pm_replied = 1
  1349. WHERE user_id = ' . $data['from_user_id'] . '
  1350. AND msg_id = ' . $data['reply_from_msg_id'];
  1351. // no break
  1352. case 'forward':
  1353. case 'post':
  1354. case 'quotepost':
  1355. $sql_data = array(
  1356. 'root_level' => $root_level,
  1357. 'author_id' => $data['from_user_id'],
  1358. 'icon_id' => $data['icon_id'],
  1359. 'author_ip' => $data['from_user_ip'],
  1360. 'message_time' => $current_time,
  1361. 'enable_bbcode' => $data['enable_bbcode'],
  1362. 'enable_smilies' => $data['enable_smilies'],
  1363. 'enable_magic_url' => $data['enable_urls'],
  1364. 'enable_sig' => $data['enable_sig'],
  1365. 'message_subject' => $subject,
  1366. 'message_text' => $data['message'],
  1367. 'message_attachment'=> (!empty($data['attachment_data'])) ? 1 : 0,
  1368. 'bbcode_bitfield' => $data['bbcode_bitfield'],
  1369. 'bbcode_uid' => $data['bbcode_uid'],
  1370. 'to_address' => implode(':', $to),
  1371. 'bcc_address' => implode(':', $bcc),
  1372. 'message_reported' => 0,
  1373. );
  1374. break;
  1375. case 'edit':
  1376. $sql_data = array(
  1377. 'icon_id' => $data['icon_id'],
  1378. 'message_edit_time' => $current_time,
  1379. 'enable_bbcode' => $data['enable_bbcode'],
  1380. 'enable_smilies' => $data['enable_smilies'],
  1381. 'enable_magic_url' => $data['enable_urls'],
  1382. 'enable_sig' => $data['enable_sig'],
  1383. 'message_subject' => $subject,
  1384. 'message_text' => $data['message'],
  1385. 'message_attachment'=> (!empty($data['attachment_data'])) ? 1 : 0,
  1386. 'bbcode_bitfield' => $data['bbcode_bitfield'],
  1387. 'bbcode_uid' => $data['bbcode_uid']
  1388. );
  1389. break;
  1390. }
  1391. if (sizeof($sql_data))
  1392. {
  1393. $query = '';
  1394. if ($mode == 'post' || $mode == 'reply' || $mode == 'quote' || $mode == 'quotepost' || $mode == 'forward')
  1395. {
  1396. $db->sql_query('INSERT INTO ' . PRIVMSGS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_data));
  1397. $data['msg_id'] = $db->sql_nextid();
  1398. }
  1399. else if ($mode == 'edit')
  1400. {
  1401. $sql = 'UPDATE ' . PRIVMSGS_TABLE . '
  1402. SET message_edit_count = message_edit_count + 1, ' . $db->sql_build_array('UPDATE', $sql_data) . '
  1403. WHERE msg_id = ' . $data['msg_id'];
  1404. $db->sql_query($sql);
  1405. }
  1406. }
  1407. if ($mode != 'edit')
  1408. {
  1409. if ($sql)
  1410. {
  1411. $db->sql_query($sql);
  1412. }
  1413. unset($sql);
  1414. $sql_ary = array();
  1415. foreach ($recipients as $user_id => $type)
  1416. {
  1417. $sql_ary[] = array(
  1418. 'msg_id' => (int) $data['msg_id'],
  1419. 'user_id' => (int) $user_id,
  1420. 'author_id' => (int) $data['from_user_id'],
  1421. 'folder_id' => PRIVMSGS_NO_BOX,
  1422. 'pm_new' => 1,
  1423. 'pm_unread' => 1,
  1424. 'pm_forwarded' => ($mode == 'forward') ? 1 : 0
  1425. );
  1426. }
  1427. $db->sql_multi_insert(PRIVMSGS_TO_TABLE, $sql_ary);
  1428. $sql = 'UPDATE ' . USERS_TABLE . '
  1429. SET user_new_privmsg = user_new_privmsg + 1, user_unread_privmsg = user_unread_privmsg + 1, user_last_privmsg = ' . time() . '
  1430. WHERE ' . $db->sql_in_set('user_id', array_keys($recipients));
  1431. $db->sql_query($sql);
  1432. // Put PM into outbox
  1433. if ($put_in_outbox)
  1434. {
  1435. $db->sql_query('INSERT INTO ' . PRIVMSGS_TO_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  1436. 'msg_id' => (int) $data['msg_id'],
  1437. 'user_id' => (int) $data['from_user_id'],
  1438. 'author_id' => (int) $data['from_user_id'],
  1439. 'folder_id' => PRIVMSGS_OUTBOX,
  1440. 'pm_new' => 0,
  1441. 'pm_unread' => 0,
  1442. 'pm_forwarded' => ($mode == 'forward') ? 1 : 0))
  1443. );
  1444. }
  1445. }
  1446. // Set user last post time
  1447. if ($mode == 'reply' || $mode == 'quote' || $mode == 'quotepost' || $mode == 'forward' || $mode == 'post')
  1448. {
  1449. $sql = 'UPDATE ' . USERS_TABLE . "
  1450. SET user_lastpost_time = $current_time
  1451. WHERE user_id = " . $data['from_user_id'];
  1452. $db->sql_query($sql);
  1453. }
  1454. // Submit Attachments
  1455. if (!empty($data['attachment_data']) && $data['msg_id'] && in_array($mode, array('post', 'reply', 'quote', 'quotepost', 'edit', 'forward')))
  1456. {
  1457. $space_taken = $files_added = 0;
  1458. $orphan_rows = array();
  1459. foreach ($data['attachment_data'] as $pos => $attach_row)
  1460. {
  1461. $orphan_rows[(int) $atta

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