Guest User

includes/functions_posting.php

a guest
Feb 18th, 2015
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 84.86 KB | None | 0 0
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13.  
  14. /**
  15. * @ignore
  16. */
  17. if (!defined('IN_PHPBB'))
  18. {
  19. exit;
  20. }
  21.  
  22. /**
  23. * Fill smiley templates (or just the variables) with smilies, either in a window or inline
  24. */
  25. function generate_smilies($mode, $forum_id)
  26. {
  27. global $db, $user, $config, $template, $phpbb_dispatcher;
  28. global $phpEx, $phpbb_root_path, $phpbb_container, $phpbb_path_helper;
  29.  
  30. $base_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&amp;f=' . $forum_id);
  31. $pagination = $phpbb_container->get('pagination');
  32. $start = request_var('start', 0);
  33.  
  34. if ($mode == 'window')
  35. {
  36. if ($forum_id)
  37. {
  38. $sql = 'SELECT forum_style
  39. FROM ' . FORUMS_TABLE . "
  40. WHERE forum_id = $forum_id";
  41. $result = $db->sql_query_limit($sql, 1);
  42. $row = $db->sql_fetchrow($result);
  43. $db->sql_freeresult($result);
  44.  
  45. $user->setup('posting', (int) $row['forum_style']);
  46. }
  47. else
  48. {
  49. $user->setup('posting');
  50. }
  51.  
  52. page_header($user->lang['SMILIES']);
  53.  
  54. $sql = 'SELECT COUNT(smiley_id) AS item_count
  55. FROM ' . SMILIES_TABLE . '
  56. GROUP BY smiley_url';
  57. $result = $db->sql_query($sql, 3600);
  58.  
  59. $smiley_count = 0;
  60. while ($row = $db->sql_fetchrow($result))
  61. {
  62. ++$smiley_count;
  63. }
  64. $db->sql_freeresult($result);
  65.  
  66. $template->set_filenames(array(
  67. 'body' => 'posting_smilies.html')
  68. );
  69.  
  70. $start = $pagination->validate_start($start, $config['smilies_per_page'], $smiley_count);
  71. $pagination->generate_template_pagination($base_url, 'pagination', 'start', $smiley_count, $config['smilies_per_page'], $start);
  72. }
  73.  
  74. $display_link = false;
  75. if ($mode == 'inline')
  76. {
  77. $sql = 'SELECT smiley_id
  78. FROM ' . SMILIES_TABLE . '
  79. WHERE display_on_posting = 0';
  80. $result = $db->sql_query_limit($sql, 1, 0, 3600);
  81.  
  82. if ($row = $db->sql_fetchrow($result))
  83. {
  84. $display_link = true;
  85. }
  86. $db->sql_freeresult($result);
  87. }
  88.  
  89. if ($mode == 'window')
  90. {
  91. $sql = 'SELECT smiley_url, MIN(emotion) as emotion, MIN(code) AS code, smiley_width, smiley_height, MIN(smiley_order) AS min_smiley_order
  92. FROM ' . SMILIES_TABLE . '
  93. GROUP BY smiley_url, smiley_width, smiley_height
  94. ORDER BY min_smiley_order';
  95. $result = $db->sql_query_limit($sql, $config['smilies_per_page'], $start, 3600);
  96. }
  97. else
  98. {
  99. $sql = 'SELECT *
  100. FROM ' . SMILIES_TABLE . '
  101. WHERE display_on_posting = 1
  102. ORDER BY smiley_order';
  103. $result = $db->sql_query($sql, 3600);
  104. }
  105.  
  106. $smilies = array();
  107. while ($row = $db->sql_fetchrow($result))
  108. {
  109. if (empty($smilies[$row['smiley_url']]))
  110. {
  111. $smilies[$row['smiley_url']] = $row;
  112. }
  113. }
  114. $db->sql_freeresult($result);
  115.  
  116. if (sizeof($smilies))
  117. {
  118. $root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_path_helper->get_web_root_path();
  119.  
  120. foreach ($smilies as $row)
  121. {
  122. $template->assign_block_vars('smiley', array(
  123. 'SMILEY_CODE' => $row['code'],
  124. 'A_SMILEY_CODE' => addslashes($row['code']),
  125. 'SMILEY_IMG' => $root_path . $config['smilies_path'] . '/' . $row['smiley_url'],
  126. 'SMILEY_WIDTH' => $row['smiley_width'],
  127. 'SMILEY_HEIGHT' => $row['smiley_height'],
  128. 'SMILEY_DESC' => $row['emotion'])
  129. );
  130. }
  131. }
  132.  
  133. /**
  134. * This event is called after the smilies are populated
  135. *
  136. * @event core.generate_smilies_after
  137. * @var string mode Mode of the smilies: window|inline
  138. * @var int forum_id The forum ID we are currently in
  139. * @var bool display_link Shall we display the "more smilies" link?
  140. * @since 3.1.0-a1
  141. */
  142. $vars = array('mode', 'forum_id', 'display_link');
  143. extract($phpbb_dispatcher->trigger_event('core.generate_smilies_after', compact($vars)));
  144.  
  145. if ($mode == 'inline' && $display_link)
  146. {
  147. $template->assign_vars(array(
  148. 'S_SHOW_SMILEY_LINK' => true,
  149. 'U_MORE_SMILIES' => $base_url,
  150. ));
  151. }
  152.  
  153. if ($mode == 'window')
  154. {
  155. page_footer();
  156. }
  157. }
  158.  
  159. /**
  160. * Update last post information
  161. * Should be used instead of sync() if only the last post information are out of sync... faster
  162. *
  163. * @param string $type Can be forum|topic
  164. * @param mixed $ids topic/forum ids
  165. * @param bool $return_update_sql true: SQL query shall be returned, false: execute SQL
  166. */
  167. function update_post_information($type, $ids, $return_update_sql = false)
  168. {
  169. global $db;
  170.  
  171. if (empty($ids))
  172. {
  173. return;
  174. }
  175. if (!is_array($ids))
  176. {
  177. $ids = array($ids);
  178. }
  179.  
  180. $update_sql = $empty_forums = $not_empty_forums = array();
  181.  
  182. if ($type != 'topic')
  183. {
  184. $topic_join = ', ' . TOPICS_TABLE . ' t';
  185. $topic_condition = 'AND t.topic_id = p.topic_id AND t.topic_visibility = ' . ITEM_APPROVED;
  186. }
  187. else
  188. {
  189. $topic_join = '';
  190. $topic_condition = '';
  191. }
  192.  
  193. if (sizeof($ids) == 1)
  194. {
  195. $sql = 'SELECT MAX(p.post_id) as last_post_id
  196. FROM ' . POSTS_TABLE . " p $topic_join
  197. WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
  198. $topic_condition
  199. AND p.post_visibility = " . ITEM_APPROVED;
  200. }
  201. else
  202. {
  203. $sql = 'SELECT p.' . $type . '_id, MAX(p.post_id) as last_post_id
  204. FROM ' . POSTS_TABLE . " p $topic_join
  205. WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
  206. $topic_condition
  207. AND p.post_visibility = " . ITEM_APPROVED . "
  208. GROUP BY p.{$type}_id";
  209. }
  210. $result = $db->sql_query($sql);
  211.  
  212. $last_post_ids = array();
  213. while ($row = $db->sql_fetchrow($result))
  214. {
  215. if (sizeof($ids) == 1)
  216. {
  217. $row[$type . '_id'] = $ids[0];
  218. }
  219.  
  220. if ($type == 'forum')
  221. {
  222. $not_empty_forums[] = $row['forum_id'];
  223.  
  224. if (empty($row['last_post_id']))
  225. {
  226. $empty_forums[] = $row['forum_id'];
  227. }
  228. }
  229.  
  230. $last_post_ids[] = $row['last_post_id'];
  231. }
  232. $db->sql_freeresult($result);
  233.  
  234. if ($type == 'forum')
  235. {
  236. $empty_forums = array_merge($empty_forums, array_diff($ids, $not_empty_forums));
  237.  
  238. foreach ($empty_forums as $void => $forum_id)
  239. {
  240. $update_sql[$forum_id][] = 'forum_last_post_id = 0';
  241. $update_sql[$forum_id][] = "forum_last_post_subject = ''";
  242. $update_sql[$forum_id][] = 'forum_last_post_time = 0';
  243. $update_sql[$forum_id][] = 'forum_last_poster_id = 0';
  244. $update_sql[$forum_id][] = "forum_last_poster_name = ''";
  245. $update_sql[$forum_id][] = "forum_last_poster_colour = ''";
  246. }
  247. }
  248.  
  249. if (sizeof($last_post_ids))
  250. {
  251. $sql = 'SELECT p.' . $type . '_id, p.post_id, p.post_subject, p.post_time, p.poster_id, p.post_username, u.user_id, u.username, u.user_colour
  252. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  253. WHERE p.poster_id = u.user_id
  254. AND ' . $db->sql_in_set('p.post_id', $last_post_ids);
  255. $result = $db->sql_query($sql);
  256.  
  257. while ($row = $db->sql_fetchrow($result))
  258. {
  259. $update_sql[$row["{$type}_id"]][] = $type . '_last_post_id = ' . (int) $row['post_id'];
  260. $update_sql[$row["{$type}_id"]][] = "{$type}_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
  261. $update_sql[$row["{$type}_id"]][] = $type . '_last_post_time = ' . (int) $row['post_time'];
  262. $update_sql[$row["{$type}_id"]][] = $type . '_last_poster_id = ' . (int) $row['poster_id'];
  263. $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
  264. $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "'";
  265. }
  266. $db->sql_freeresult($result);
  267. }
  268. unset($empty_forums, $ids, $last_post_ids);
  269.  
  270. if ($return_update_sql || !sizeof($update_sql))
  271. {
  272. return $update_sql;
  273. }
  274.  
  275. $table = ($type == 'forum') ? FORUMS_TABLE : TOPICS_TABLE;
  276.  
  277. foreach ($update_sql as $update_id => $update_sql_ary)
  278. {
  279. $sql = "UPDATE $table
  280. SET " . implode(', ', $update_sql_ary) . "
  281. WHERE {$type}_id = $update_id";
  282. $db->sql_query($sql);
  283. }
  284.  
  285. return;
  286. }
  287.  
  288. /**
  289. * Generate Topic Icons for display
  290. */
  291. function posting_gen_topic_icons($mode, $icon_id)
  292. {
  293. global $phpbb_root_path, $config, $template, $cache;
  294.  
  295. // Grab icons
  296. $icons = $cache->obtain_icons();
  297.  
  298. if (!$icon_id)
  299. {
  300. $template->assign_var('S_NO_ICON_CHECKED', ' checked="checked"');
  301. }
  302.  
  303. if (sizeof($icons))
  304. {
  305. $root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_root_path;
  306.  
  307. foreach ($icons as $id => $data)
  308. {
  309. if ($data['display'])
  310. {
  311. $template->assign_block_vars('topic_icon', array(
  312. 'ICON_ID' => $id,
  313. 'ICON_IMG' => $root_path . $config['icons_path'] . '/' . $data['img'],
  314. 'ICON_WIDTH' => $data['width'],
  315. 'ICON_HEIGHT' => $data['height'],
  316.  
  317. 'S_CHECKED' => ($id == $icon_id) ? true : false,
  318. 'S_ICON_CHECKED' => ($id == $icon_id) ? ' checked="checked"' : '')
  319. );
  320. }
  321. }
  322.  
  323. return true;
  324. }
  325.  
  326. return false;
  327. }
  328.  
  329. /**
  330. * Build topic types able to be selected
  331. */
  332. function posting_gen_topic_types($forum_id, $cur_topic_type = POST_NORMAL)
  333. {
  334. global $auth, $user, $template, $topic_type;
  335.  
  336. $toggle = false;
  337.  
  338. $topic_types = array(
  339. 'sticky' => array('const' => POST_STICKY, 'lang' => 'POST_STICKY'),
  340. 'announce' => array('const' => POST_ANNOUNCE, 'lang' => 'POST_ANNOUNCEMENT'),
  341. 'global' => array('const' => POST_GLOBAL, 'lang' => 'POST_GLOBAL')
  342. );
  343.  
  344. $topic_type_array = array();
  345.  
  346. foreach ($topic_types as $auth_key => $topic_value)
  347. {
  348. // We do not have a special post global announcement permission
  349. $auth_key = ($auth_key == 'global') ? 'announce' : $auth_key;
  350.  
  351. if ($auth->acl_get('f_' . $auth_key, $forum_id))
  352. {
  353. $toggle = true;
  354.  
  355. $topic_type_array[] = array(
  356. 'VALUE' => $topic_value['const'],
  357. 'S_CHECKED' => ($cur_topic_type == $topic_value['const']) ? ' checked="checked"' : '',
  358. 'L_TOPIC_TYPE' => $user->lang[$topic_value['lang']]
  359. );
  360. }
  361. }
  362.  
  363. if ($toggle)
  364. {
  365. $topic_type_array = array_merge(array(0 => array(
  366. 'VALUE' => POST_NORMAL,
  367. 'S_CHECKED' => ($cur_topic_type == POST_NORMAL) ? ' checked="checked"' : '',
  368. 'L_TOPIC_TYPE' => $user->lang['POST_NORMAL'])),
  369.  
  370. $topic_type_array
  371. );
  372.  
  373. foreach ($topic_type_array as $array)
  374. {
  375. $template->assign_block_vars('topic_type', $array);
  376. }
  377.  
  378. $template->assign_vars(array(
  379. 'S_TOPIC_TYPE_STICKY' => ($auth->acl_get('f_sticky', $forum_id)),
  380. 'S_TOPIC_TYPE_ANNOUNCE' => ($auth->acl_get('f_announce', $forum_id)))
  381. );
  382. }
  383.  
  384. return $toggle;
  385. }
  386.  
  387. //
  388. // Attachment related functions
  389. //
  390.  
  391. /**
  392. * Upload Attachment - filedata is generated here
  393. * Uses upload class
  394. *
  395. * @param string $form_name The form name of the file upload input
  396. * @param int $forum_id The id of the forum
  397. * @param bool $local Whether the file is local or not
  398. * @param string $local_storage The path to the local file
  399. * @param bool $is_message Whether it is a PM or not
  400. * @param \filespec $local_filedata A filespec object created for the local file
  401. * @param \phpbb\mimetype\guesser $mimetype_guesser The mimetype guesser object if used
  402. * @param \phpbb\plupload\plupload $plupload The plupload object if one is being used
  403. *
  404. * @return object filespec
  405. */
  406. function upload_attachment($form_name, $forum_id, $local = false, $local_storage = '', $is_message = false, $local_filedata = false, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null)
  407. {
  408. global $auth, $user, $config, $db, $cache;
  409. global $phpbb_root_path, $phpEx, $phpbb_dispatcher;
  410.  
  411. $filedata = array(
  412. 'error' => array()
  413. );
  414.  
  415. include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
  416. $upload = new fileupload();
  417.  
  418. if ($config['check_attachment_content'] && isset($config['mime_triggers']))
  419. {
  420. $upload->set_disallowed_content(explode('|', $config['mime_triggers']));
  421. }
  422. else if (!$config['check_attachment_content'])
  423. {
  424. $upload->set_disallowed_content(array());
  425. }
  426.  
  427. $filedata['post_attach'] = $local || $upload->is_valid($form_name);
  428.  
  429. if (!$filedata['post_attach'])
  430. {
  431. $filedata['error'][] = $user->lang['NO_UPLOAD_FORM_FOUND'];
  432. return $filedata;
  433. }
  434.  
  435. $extensions = $cache->obtain_attach_extensions((($is_message) ? false : (int) $forum_id));
  436. $upload->set_allowed_extensions(array_keys($extensions['_allowed_']));
  437.  
  438. $file = ($local) ? $upload->local_upload($local_storage, $local_filedata, $mimetype_guesser) : $upload->form_upload($form_name, $mimetype_guesser, $plupload);
  439.  
  440. if ($file->init_error)
  441. {
  442. $filedata['post_attach'] = false;
  443. return $filedata;
  444. }
  445.  
  446. // Whether the uploaded file is in the image category
  447. $is_image = (isset($extensions[$file->get('extension')]['display_cat'])) ? $extensions[$file->get('extension')]['display_cat'] == ATTACHMENT_CATEGORY_IMAGE : false;
  448.  
  449. if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id))
  450. {
  451. // Check Image Size, if it is an image
  452. if ($is_image)
  453. {
  454. $file->upload->set_allowed_dimensions(0, 0, $config['img_max_width'], $config['img_max_height']);
  455. }
  456.  
  457. // Admins and mods are allowed to exceed the allowed filesize
  458. if (!empty($extensions[$file->get('extension')]['max_filesize']))
  459. {
  460. $allowed_filesize = $extensions[$file->get('extension')]['max_filesize'];
  461. }
  462. else
  463. {
  464. $allowed_filesize = ($is_message) ? $config['max_filesize_pm'] : $config['max_filesize'];
  465. }
  466.  
  467. $file->upload->set_max_filesize($allowed_filesize);
  468. }
  469.  
  470. $file->clean_filename('unique', $user->data['user_id'] . '_');
  471.  
  472. // Are we uploading an image *and* this image being within the image category?
  473. // Only then perform additional image checks.
  474. $file->move_file($config['upload_path'], false, !$is_image);
  475.  
  476. // Do we have to create a thumbnail?
  477. $filedata['thumbnail'] = ($is_image && $config['img_create_thumbnail']) ? 1 : 0;
  478.  
  479. if (sizeof($file->error))
  480. {
  481. $file->remove();
  482. $filedata['error'] = array_merge($filedata['error'], $file->error);
  483. $filedata['post_attach'] = false;
  484.  
  485. return $filedata;
  486. }
  487.  
  488. // Make sure the image category only holds valid images...
  489. if ($is_image && !$file->is_image())
  490. {
  491. $file->remove();
  492.  
  493. if ($plupload && $plupload->is_active())
  494. {
  495. $plupload->emit_error(104, 'ATTACHED_IMAGE_NOT_IMAGE');
  496. }
  497.  
  498. // If this error occurs a user tried to exploit an IE Bug by renaming extensions
  499. // Since the image category is displaying content inline we need to catch this.
  500. trigger_error($user->lang['ATTACHED_IMAGE_NOT_IMAGE']);
  501. }
  502.  
  503. $filedata['filesize'] = $file->get('filesize');
  504. $filedata['mimetype'] = $file->get('mimetype');
  505. $filedata['extension'] = $file->get('extension');
  506. $filedata['physical_filename'] = $file->get('realname');
  507. $filedata['real_filename'] = $file->get('uploadname');
  508. $filedata['filetime'] = time();
  509.  
  510. /**
  511. * Event to modify uploaded file before submit to the post
  512. *
  513. * @event core.modify_uploaded_file
  514. * @var array filedata Array containing uploaded file data
  515. * @var bool is_image Flag indicating if the file is an image
  516. * @since 3.1.0-RC3
  517. */
  518. $vars = array(
  519. 'filedata',
  520. 'is_image',
  521. );
  522. extract($phpbb_dispatcher->trigger_event('core.modify_uploaded_file', compact($vars)));
  523.  
  524. // Check our complete quota
  525. if ($config['attachment_quota'])
  526. {
  527. if ($config['upload_dir_size'] + $file->get('filesize') > $config['attachment_quota'])
  528. {
  529. $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
  530. $filedata['post_attach'] = false;
  531.  
  532. $file->remove();
  533.  
  534. return $filedata;
  535. }
  536. }
  537.  
  538. // Check free disk space
  539. if ($free_space = @disk_free_space($phpbb_root_path . $config['upload_path']))
  540. {
  541. if ($free_space <= $file->get('filesize'))
  542. {
  543. if ($auth->acl_get('a_'))
  544. {
  545. $filedata['error'][] = $user->lang['ATTACH_DISK_FULL'];
  546. }
  547. else
  548. {
  549. $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
  550. }
  551. $filedata['post_attach'] = false;
  552.  
  553. $file->remove();
  554.  
  555. return $filedata;
  556. }
  557. }
  558.  
  559. // Create Thumbnail
  560. if ($filedata['thumbnail'])
  561. {
  562. $source = $file->get('destination_file');
  563. $destination = $file->get('destination_path') . '/thumb_' . $file->get('realname');
  564.  
  565. if (!create_thumbnail($source, $destination, $file->get('mimetype')))
  566. {
  567. $filedata['thumbnail'] = 0;
  568. }
  569. }
  570.  
  571. return $filedata;
  572. }
  573.  
  574. /**
  575. * Calculate the needed size for Thumbnail
  576. */
  577. function get_img_size_format($width, $height)
  578. {
  579. global $config;
  580.  
  581. // Maximum Width the Image can take
  582. $max_width = ($config['img_max_thumb_width']) ? $config['img_max_thumb_width'] : 400;
  583.  
  584. if ($width > $height)
  585. {
  586. return array(
  587. round($width * ($max_width / $width)),
  588. round($height * ($max_width / $width))
  589. );
  590. }
  591. else
  592. {
  593. return array(
  594. round($width * ($max_width / $height)),
  595. round($height * ($max_width / $height))
  596. );
  597. }
  598. }
  599.  
  600. /**
  601. * Return supported image types
  602. */
  603. function get_supported_image_types($type = false)
  604. {
  605. if (@extension_loaded('gd'))
  606. {
  607. $format = imagetypes();
  608. $new_type = 0;
  609.  
  610. if ($type !== false)
  611. {
  612. // Type is one of the IMAGETYPE constants - it is fetched from getimagesize()
  613. switch ($type)
  614. {
  615. // GIF
  616. case IMAGETYPE_GIF:
  617. $new_type = ($format & IMG_GIF) ? IMG_GIF : false;
  618. break;
  619.  
  620. // JPG, JPC, JP2
  621. case IMAGETYPE_JPEG:
  622. case IMAGETYPE_JPC:
  623. case IMAGETYPE_JPEG2000:
  624. case IMAGETYPE_JP2:
  625. case IMAGETYPE_JPX:
  626. case IMAGETYPE_JB2:
  627. $new_type = ($format & IMG_JPG) ? IMG_JPG : false;
  628. break;
  629.  
  630. // PNG
  631. case IMAGETYPE_PNG:
  632. $new_type = ($format & IMG_PNG) ? IMG_PNG : false;
  633. break;
  634.  
  635. // WBMP
  636. case IMAGETYPE_WBMP:
  637. $new_type = ($format & IMG_WBMP) ? IMG_WBMP : false;
  638. break;
  639. }
  640. }
  641. else
  642. {
  643. $new_type = array();
  644. $go_through_types = array(IMG_GIF, IMG_JPG, IMG_PNG, IMG_WBMP);
  645.  
  646. foreach ($go_through_types as $check_type)
  647. {
  648. if ($format & $check_type)
  649. {
  650. $new_type[] = $check_type;
  651. }
  652. }
  653. }
  654.  
  655. return array(
  656. 'gd' => ($new_type) ? true : false,
  657. 'format' => $new_type,
  658. 'version' => (function_exists('imagecreatetruecolor')) ? 2 : 1
  659. );
  660. }
  661.  
  662. return array('gd' => false);
  663. }
  664.  
  665. /**
  666. * Create Thumbnail
  667. */
  668. function create_thumbnail($source, $destination, $mimetype)
  669. {
  670. global $config;
  671.  
  672. $min_filesize = (int) $config['img_min_thumb_filesize'];
  673. $img_filesize = (file_exists($source)) ? @filesize($source) : false;
  674.  
  675. if (!$img_filesize || $img_filesize <= $min_filesize)
  676. {
  677. return false;
  678. }
  679.  
  680. $dimension = @getimagesize($source);
  681.  
  682. if ($dimension === false)
  683. {
  684. return false;
  685. }
  686.  
  687. list($width, $height, $type, ) = $dimension;
  688.  
  689. if (empty($width) || empty($height))
  690. {
  691. return false;
  692. }
  693.  
  694. list($new_width, $new_height) = get_img_size_format($width, $height);
  695.  
  696. // Do not create a thumbnail if the resulting width/height is bigger than the original one
  697. if ($new_width >= $width && $new_height >= $height)
  698. {
  699. return false;
  700. }
  701.  
  702. $used_imagick = false;
  703.  
  704. // Only use imagemagick if defined and the passthru function not disabled
  705. if ($config['img_imagick'] && function_exists('passthru'))
  706. {
  707. if (substr($config['img_imagick'], -1) !== '/')
  708. {
  709. $config['img_imagick'] .= '/';
  710. }
  711.  
  712. @passthru(escapeshellcmd($config['img_imagick']) . 'convert' . ((defined('PHP_OS') && preg_match('#^win#i', PHP_OS)) ? '.exe' : '') . ' -quality 85 -geometry ' . $new_width . 'x' . $new_height . ' "' . str_replace('\\', '/', $source) . '" "' . str_replace('\\', '/', $destination) . '"');
  713.  
  714. if (file_exists($destination))
  715. {
  716. $used_imagick = true;
  717. }
  718. }
  719.  
  720. if (!$used_imagick)
  721. {
  722. $type = get_supported_image_types($type);
  723.  
  724. if ($type['gd'])
  725. {
  726. // If the type is not supported, we are not able to create a thumbnail
  727. if ($type['format'] === false)
  728. {
  729. return false;
  730. }
  731.  
  732. switch ($type['format'])
  733. {
  734. case IMG_GIF:
  735. $image = @imagecreatefromgif($source);
  736. break;
  737.  
  738. case IMG_JPG:
  739. @ini_set('gd.jpeg_ignore_warning', 1);
  740. $image = @imagecreatefromjpeg($source);
  741. break;
  742.  
  743. case IMG_PNG:
  744. $image = @imagecreatefrompng($source);
  745. break;
  746.  
  747. case IMG_WBMP:
  748. $image = @imagecreatefromwbmp($source);
  749. break;
  750. }
  751.  
  752. if (empty($image))
  753. {
  754. return false;
  755. }
  756.  
  757. if ($type['version'] == 1)
  758. {
  759. $new_image = imagecreate($new_width, $new_height);
  760.  
  761. if ($new_image === false)
  762. {
  763. return false;
  764. }
  765.  
  766. imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  767. }
  768. else
  769. {
  770. $new_image = imagecreatetruecolor($new_width, $new_height);
  771.  
  772. if ($new_image === false)
  773. {
  774. return false;
  775. }
  776.  
  777. // Preserve alpha transparency (png for example)
  778. @imagealphablending($new_image, false);
  779. @imagesavealpha($new_image, true);
  780.  
  781. imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  782. }
  783.  
  784. // If we are in safe mode create the destination file prior to using the gd functions to circumvent a PHP bug
  785. if (@ini_get('safe_mode') || @strtolower(ini_get('safe_mode')) == 'on')
  786. {
  787. @touch($destination);
  788. }
  789.  
  790. switch ($type['format'])
  791. {
  792. case IMG_GIF:
  793. imagegif($new_image, $destination);
  794. break;
  795.  
  796. case IMG_JPG:
  797. imagejpeg($new_image, $destination, 90);
  798. break;
  799.  
  800. case IMG_PNG:
  801. imagepng($new_image, $destination);
  802. break;
  803.  
  804. case IMG_WBMP:
  805. imagewbmp($new_image, $destination);
  806. break;
  807. }
  808.  
  809. imagedestroy($new_image);
  810. }
  811. else
  812. {
  813. return false;
  814. }
  815. }
  816.  
  817. if (!file_exists($destination))
  818. {
  819. return false;
  820. }
  821.  
  822. phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);
  823.  
  824. return true;
  825. }
  826.  
  827. /**
  828. * Assign Inline attachments (build option fields)
  829. */
  830. function posting_gen_inline_attachments(&$attachment_data)
  831. {
  832. global $template;
  833.  
  834. if (sizeof($attachment_data))
  835. {
  836. $s_inline_attachment_options = '';
  837.  
  838. foreach ($attachment_data as $i => $attachment)
  839. {
  840. $s_inline_attachment_options .= '<option value="' . $i . '">' . utf8_basename($attachment['real_filename']) . '</option>';
  841. }
  842.  
  843. $template->assign_var('S_INLINE_ATTACHMENT_OPTIONS', $s_inline_attachment_options);
  844.  
  845. return true;
  846. }
  847.  
  848. return false;
  849. }
  850.  
  851. /**
  852. * Generate inline attachment entry
  853. */
  854. function posting_gen_attachment_entry($attachment_data, &$filename_data, $show_attach_box = true)
  855. {
  856. global $template, $config, $phpbb_root_path, $phpEx, $user;
  857.  
  858. // Some default template variables
  859. $template->assign_vars(array(
  860. 'S_SHOW_ATTACH_BOX' => $show_attach_box,
  861. 'S_HAS_ATTACHMENTS' => sizeof($attachment_data),
  862. 'FILESIZE' => $config['max_filesize'],
  863. 'FILE_COMMENT' => (isset($filename_data['filecomment'])) ? $filename_data['filecomment'] : '',
  864. ));
  865.  
  866. if (sizeof($attachment_data))
  867. {
  868. // We display the posted attachments within the desired order.
  869. ($config['display_order']) ? krsort($attachment_data) : ksort($attachment_data);
  870.  
  871. foreach ($attachment_data as $count => $attach_row)
  872. {
  873. $hidden = '';
  874. $attach_row['real_filename'] = utf8_basename($attach_row['real_filename']);
  875.  
  876. foreach ($attach_row as $key => $value)
  877. {
  878. $hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />';
  879. }
  880.  
  881. $download_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'mode=view&amp;id=' . (int) $attach_row['attach_id'], true, ($attach_row['is_orphan']) ? $user->session_id : false);
  882.  
  883. $template->assign_block_vars('attach_row', array(
  884. 'FILENAME' => utf8_basename($attach_row['real_filename']),
  885. 'A_FILENAME' => addslashes(utf8_basename($attach_row['real_filename'])),
  886. 'FILE_COMMENT' => $attach_row['attach_comment'],
  887. 'ATTACH_ID' => $attach_row['attach_id'],
  888. 'S_IS_ORPHAN' => $attach_row['is_orphan'],
  889. 'ASSOC_INDEX' => $count,
  890. 'FILESIZE' => get_formatted_filesize($attach_row['filesize']),
  891.  
  892. 'U_VIEW_ATTACHMENT' => $download_link,
  893. 'S_HIDDEN' => $hidden)
  894. );
  895. }
  896. }
  897.  
  898. return sizeof($attachment_data);
  899. }
  900.  
  901. //
  902. // General Post functions
  903. //
  904.  
  905. /**
  906. * Load Drafts
  907. */
  908. function load_drafts($topic_id = 0, $forum_id = 0, $id = 0, $pm_action = '', $msg_id = 0)
  909. {
  910. global $user, $db, $template, $auth;
  911. global $phpbb_root_path, $phpbb_dispatcher, $phpEx;
  912.  
  913. $topic_ids = $forum_ids = $draft_rows = array();
  914.  
  915. // Load those drafts not connected to forums/topics
  916. // If forum_id == 0 AND topic_id == 0 then this is a PM draft
  917. if (!$topic_id && !$forum_id)
  918. {
  919. $sql_and = ' AND d.forum_id = 0 AND d.topic_id = 0';
  920. }
  921. else
  922. {
  923. $sql_and = '';
  924. $sql_and .= ($forum_id) ? ' AND d.forum_id = ' . (int) $forum_id : '';
  925. $sql_and .= ($topic_id) ? ' AND d.topic_id = ' . (int) $topic_id : '';
  926. }
  927.  
  928. $sql = 'SELECT d.*, f.forum_id, f.forum_name
  929. FROM ' . DRAFTS_TABLE . ' d
  930. LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = d.forum_id)
  931. WHERE d.user_id = ' . $user->data['user_id'] . "
  932. $sql_and
  933. ORDER BY d.save_time DESC";
  934. $result = $db->sql_query($sql);
  935.  
  936. while ($row = $db->sql_fetchrow($result))
  937. {
  938. if ($row['topic_id'])
  939. {
  940. $topic_ids[] = (int) $row['topic_id'];
  941. }
  942. $draft_rows[] = $row;
  943. }
  944. $db->sql_freeresult($result);
  945.  
  946. if (!sizeof($draft_rows))
  947. {
  948. return;
  949. }
  950.  
  951. $topic_rows = array();
  952. if (sizeof($topic_ids))
  953. {
  954. $sql = 'SELECT topic_id, forum_id, topic_title, topic_poster
  955. FROM ' . TOPICS_TABLE . '
  956. WHERE ' . $db->sql_in_set('topic_id', array_unique($topic_ids));
  957. $result = $db->sql_query($sql);
  958.  
  959. while ($row = $db->sql_fetchrow($result))
  960. {
  961. $topic_rows[$row['topic_id']] = $row;
  962. }
  963. $db->sql_freeresult($result);
  964. }
  965.  
  966. /**
  967. * Drafts found and their topics
  968. * Edit $draft_rows in order to add or remove drafts loaded
  969. *
  970. * @event core.load_drafts_draft_list_result
  971. * @var array draft_rows The drafts query result. Includes its forum id and everything about the draft
  972. * @var array topic_ids The list of topics got from the topics table
  973. * @var array topic_rows The topics that draft_rows references
  974. * @since 3.1.0-RC3
  975. */
  976. $vars = array('draft_rows', 'topic_ids', 'topic_rows');
  977. extract($phpbb_dispatcher->trigger_event('core.load_drafts_draft_list_result', compact($vars)));
  978.  
  979. unset($topic_ids);
  980.  
  981. $template->assign_var('S_SHOW_DRAFTS', true);
  982.  
  983. foreach ($draft_rows as $draft)
  984. {
  985. $link_topic = $link_forum = $link_pm = false;
  986. $insert_url = $view_url = $title = '';
  987.  
  988. if (isset($topic_rows[$draft['topic_id']])
  989. && (
  990. ($topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_get('f_read', $topic_rows[$draft['topic_id']]['forum_id']))
  991. ||
  992. (!$topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_getf_global('f_read'))
  993. ))
  994. {
  995. $topic_forum_id = ($topic_rows[$draft['topic_id']]['forum_id']) ? $topic_rows[$draft['topic_id']]['forum_id'] : $forum_id;
  996.  
  997. $link_topic = true;
  998. $view_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topic_forum_id . '&amp;t=' . $draft['topic_id']);
  999. $title = $topic_rows[$draft['topic_id']]['topic_title'];
  1000.  
  1001. $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $topic_forum_id . '&amp;t=' . $draft['topic_id'] . '&amp;mode=reply&amp;d=' . $draft['draft_id']);
  1002. }
  1003. else if ($draft['forum_id'] && $auth->acl_get('f_read', $draft['forum_id']))
  1004. {
  1005. $link_forum = true;
  1006. $view_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $draft['forum_id']);
  1007. $title = $draft['forum_name'];
  1008.  
  1009. $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $draft['forum_id'] . '&amp;mode=post&amp;d=' . $draft['draft_id']);
  1010. }
  1011. else
  1012. {
  1013. // Either display as PM draft if forum_id and topic_id are empty or if access to the forums has been denied afterwards...
  1014. $link_pm = true;
  1015. $insert_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&amp;mode=compose&amp;d={$draft['draft_id']}" . (($pm_action) ? "&amp;action=$pm_action" : '') . (($msg_id) ? "&amp;p=$msg_id" : ''));
  1016. }
  1017.  
  1018. $template->assign_block_vars('draftrow', array(
  1019. 'DRAFT_ID' => $draft['draft_id'],
  1020. 'DATE' => $user->format_date($draft['save_time']),
  1021. 'DRAFT_SUBJECT' => $draft['draft_subject'],
  1022.  
  1023. 'TITLE' => $title,
  1024. 'U_VIEW' => $view_url,
  1025. 'U_INSERT' => $insert_url,
  1026.  
  1027. 'S_LINK_PM' => $link_pm,
  1028. 'S_LINK_TOPIC' => $link_topic,
  1029. 'S_LINK_FORUM' => $link_forum)
  1030. );
  1031. }
  1032. }
  1033.  
  1034. /**
  1035. * Topic Review
  1036. */
  1037. function topic_review($topic_id, $forum_id, $mode = 'topic_review', $cur_post_id = 0, $show_quote_button = true)
  1038. {
  1039. global $user, $auth, $db, $template, $bbcode, $cache;
  1040. global $config, $phpbb_root_path, $phpEx, $phpbb_container;
  1041.  
  1042. $phpbb_content_visibility = $phpbb_container->get('content.visibility');
  1043. $sql_sort = ($mode == 'post_review') ? 'ASC' : 'DESC';
  1044.  
  1045. // Go ahead and pull all data for this topic
  1046. $sql = 'SELECT p.post_id
  1047. FROM ' . POSTS_TABLE . ' p' . "
  1048. WHERE p.topic_id = $topic_id
  1049. AND " . $phpbb_content_visibility->get_visibility_sql('post', $forum_id, 'p.') . '
  1050. ' . (($mode == 'post_review') ? " AND p.post_id > $cur_post_id" : '') . '
  1051. ' . (($mode == 'post_review_edit') ? " AND p.post_id = $cur_post_id" : '') . '
  1052. ORDER BY p.post_time ' . $sql_sort . ', p.post_id ' . $sql_sort;
  1053. $result = $db->sql_query_limit($sql, $config['posts_per_page']);
  1054.  
  1055. $post_list = array();
  1056.  
  1057. while ($row = $db->sql_fetchrow($result))
  1058. {
  1059. $post_list[] = $row['post_id'];
  1060. }
  1061.  
  1062. $db->sql_freeresult($result);
  1063.  
  1064. if (!sizeof($post_list))
  1065. {
  1066. return false;
  1067. }
  1068.  
  1069. // Handle 'post_review_edit' like 'post_review' from now on
  1070. if ($mode == 'post_review_edit')
  1071. {
  1072. $mode = 'post_review';
  1073. }
  1074.  
  1075. $sql_ary = array(
  1076. 'SELECT' => 'u.username, u.user_id, u.user_colour, p.*, z.friend, z.foe',
  1077.  
  1078. 'FROM' => array(
  1079. USERS_TABLE => 'u',
  1080. POSTS_TABLE => 'p',
  1081. ),
  1082.  
  1083. 'LEFT_JOIN' => array(
  1084. array(
  1085. 'FROM' => array(ZEBRA_TABLE => 'z'),
  1086. 'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id',
  1087. ),
  1088. ),
  1089.  
  1090. 'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
  1091. AND u.user_id = p.poster_id',
  1092. );
  1093.  
  1094. $sql = $db->sql_build_query('SELECT', $sql_ary);
  1095. $result = $db->sql_query($sql);
  1096.  
  1097. $bbcode_bitfield = '';
  1098. $rowset = array();
  1099. $has_attachments = false;
  1100. while ($row = $db->sql_fetchrow($result))
  1101. {
  1102. $rowset[$row['post_id']] = $row;
  1103. $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
  1104.  
  1105. if ($row['post_attachment'])
  1106. {
  1107. $has_attachments = true;
  1108. }
  1109. }
  1110. $db->sql_freeresult($result);
  1111.  
  1112. // Instantiate BBCode class
  1113. if (!isset($bbcode) && $bbcode_bitfield !== '')
  1114. {
  1115. include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
  1116. $bbcode = new bbcode(base64_encode($bbcode_bitfield));
  1117. }
  1118.  
  1119. // Grab extensions
  1120. $extensions = $attachments = array();
  1121. if ($has_attachments && $auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
  1122. {
  1123. $extensions = $cache->obtain_attach_extensions($forum_id);
  1124.  
  1125. // Get attachments...
  1126. $sql = 'SELECT *
  1127. FROM ' . ATTACHMENTS_TABLE . '
  1128. WHERE ' . $db->sql_in_set('post_msg_id', $post_list) . '
  1129. AND in_message = 0
  1130. ORDER BY filetime DESC, post_msg_id ASC';
  1131. $result = $db->sql_query($sql);
  1132.  
  1133. while ($row = $db->sql_fetchrow($result))
  1134. {
  1135. $attachments[$row['post_msg_id']][] = $row;
  1136. }
  1137. $db->sql_freeresult($result);
  1138. }
  1139.  
  1140. for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
  1141. {
  1142. // A non-existing rowset only happens if there was no user present for the entered poster_id
  1143. // This could be a broken posts table.
  1144. if (!isset($rowset[$post_list[$i]]))
  1145. {
  1146. continue;
  1147. }
  1148.  
  1149. $row = $rowset[$post_list[$i]];
  1150.  
  1151. $poster_id = $row['user_id'];
  1152. $post_subject = $row['post_subject'];
  1153.  
  1154. $decoded_message = false;
  1155.  
  1156. if ($show_quote_button && $auth->acl_get('f_reply', $forum_id))
  1157. {
  1158. $decoded_message = censor_text($row['post_text']);
  1159. decode_message($decoded_message, $row['bbcode_uid']);
  1160.  
  1161. $decoded_message = bbcode_nl2br($decoded_message);
  1162. }
  1163.  
  1164. $parse_flags = ($row['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0);
  1165. $parse_flags |= ($row['enable_smilies'] ? OPTION_FLAG_SMILIES : 0);
  1166. $message = generate_text_for_display($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $parse_flags, true);
  1167.  
  1168. if (!empty($attachments[$row['post_id']]))
  1169. {
  1170. $update_count = array();
  1171. parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
  1172. }
  1173.  
  1174. $post_subject = censor_text($post_subject);
  1175.  
  1176. $post_anchor = ($mode == 'post_review') ? 'ppr' . $row['post_id'] : 'pr' . $row['post_id'];
  1177. $u_show_post = append_sid($phpbb_root_path . 'viewtopic.' . $phpEx, "f=$forum_id&amp;t=$topic_id&amp;p={$row['post_id']}&amp;view=show#p{$row['post_id']}");
  1178.  
  1179. $template->assign_block_vars($mode . '_row', array(
  1180. 'POST_AUTHOR_FULL' => get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  1181. 'POST_AUTHOR_COLOUR' => get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  1182. 'POST_AUTHOR' => get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  1183. 'U_POST_AUTHOR' => get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  1184.  
  1185. 'S_HAS_ATTACHMENTS' => (!empty($attachments[$row['post_id']])) ? true : false,
  1186. 'S_FRIEND' => ($row['friend']) ? true : false,
  1187. 'S_IGNORE_POST' => ($row['foe']) ? true : false,
  1188. 'L_IGNORE_POST' => ($row['foe']) ? sprintf($user->lang['POST_BY_FOE'], get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']), "<a href=\"{$u_show_post}\" onclick=\"phpbb.toggleDisplay('{$post_anchor}', 1); return false;\">", '</a>') : '',
  1189.  
  1190. 'POST_SUBJECT' => $post_subject,
  1191. 'MINI_POST_IMG' => $user->img('icon_post_target', $user->lang['POST']),
  1192. 'POST_DATE' => $user->format_date($row['post_time']),
  1193. 'MESSAGE' => $message,
  1194. 'DECODED_MESSAGE' => $decoded_message,
  1195. 'POST_ID' => $row['post_id'],
  1196. 'U_MINI_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . '#p' . $row['post_id'],
  1197. 'U_MCP_DETAILS' => ($auth->acl_get('m_info', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=post_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
  1198. 'POSTER_QUOTE' => ($show_quote_button && $auth->acl_get('f_reply', $forum_id)) ? addslashes(get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username'])) : '')
  1199. );
  1200.  
  1201. // Display not already displayed Attachments for this post, we already parsed them. ;)
  1202. if (!empty($attachments[$row['post_id']]))
  1203. {
  1204. foreach ($attachments[$row['post_id']] as $attachment)
  1205. {
  1206. $template->assign_block_vars($mode . '_row.attachment', array(
  1207. 'DISPLAY_ATTACHMENT' => $attachment)
  1208. );
  1209. }
  1210. }
  1211.  
  1212. unset($rowset[$post_list[$i]]);
  1213. }
  1214.  
  1215. if ($mode == 'topic_review')
  1216. {
  1217. $template->assign_var('QUOTE_IMG', $user->img('icon_post_quote', $user->lang['REPLY_WITH_QUOTE']));
  1218. }
  1219.  
  1220. return true;
  1221. }
  1222.  
  1223. //
  1224. // Post handling functions
  1225. //
  1226.  
  1227. /**
  1228. * Delete Post
  1229. */
  1230. function delete_post($forum_id, $topic_id, $post_id, &$data, $is_soft = false, $softdelete_reason = '')
  1231. {
  1232. global $db, $user, $auth, $phpbb_container;
  1233. global $config, $phpEx, $phpbb_root_path;
  1234.  
  1235. // Specify our post mode
  1236. $post_mode = 'delete';
  1237. if (($data['topic_first_post_id'] === $data['topic_last_post_id']) && ($data['topic_posts_approved'] + $data['topic_posts_unapproved'] + $data['topic_posts_softdeleted'] == 1))
  1238. {
  1239. $post_mode = 'delete_topic';
  1240. }
  1241. else if ($data['topic_first_post_id'] == $post_id)
  1242. {
  1243. $post_mode = 'delete_first_post';
  1244. }
  1245. else if ($data['topic_last_post_id'] == $post_id)
  1246. {
  1247. $post_mode = 'delete_last_post';
  1248. }
  1249. $sql_data = array();
  1250. $next_post_id = false;
  1251.  
  1252. include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  1253.  
  1254. $db->sql_transaction('begin');
  1255.  
  1256. // we must make sure to update forums that contain the shadow'd topic
  1257. if ($post_mode == 'delete_topic')
  1258. {
  1259. $shadow_forum_ids = array();
  1260.  
  1261. $sql = 'SELECT forum_id
  1262. FROM ' . TOPICS_TABLE . '
  1263. WHERE ' . $db->sql_in_set('topic_moved_id', $topic_id);
  1264. $result = $db->sql_query($sql);
  1265. while ($row = $db->sql_fetchrow($result))
  1266. {
  1267. if (!isset($shadow_forum_ids[(int) $row['forum_id']]))
  1268. {
  1269. $shadow_forum_ids[(int) $row['forum_id']] = 1;
  1270. }
  1271. else
  1272. {
  1273. $shadow_forum_ids[(int) $row['forum_id']]++;
  1274. }
  1275. }
  1276. $db->sql_freeresult($result);
  1277. }
  1278.  
  1279. $phpbb_content_visibility = $phpbb_container->get('content.visibility');
  1280.  
  1281. // (Soft) delete the post
  1282. if ($is_soft && ($post_mode != 'delete_topic'))
  1283. {
  1284. $phpbb_content_visibility->set_post_visibility(ITEM_DELETED, $post_id, $topic_id, $forum_id, $user->data['user_id'], time(), $softdelete_reason, ($data['topic_first_post_id'] == $post_id), ($data['topic_last_post_id'] == $post_id));
  1285. }
  1286. else if (!$is_soft)
  1287. {
  1288. if (!delete_posts('post_id', array($post_id), false, false, false))
  1289. {
  1290. // Try to delete topic, we may had an previous error causing inconsistency
  1291. if ($post_mode == 'delete_topic')
  1292. {
  1293. delete_topics('topic_id', array($topic_id), false);
  1294. }
  1295. trigger_error('ALREADY_DELETED');
  1296. }
  1297. }
  1298.  
  1299. $db->sql_transaction('commit');
  1300.  
  1301. // Collect the necessary information for updating the tables
  1302. $sql_data[FORUMS_TABLE] = $sql_data[TOPICS_TABLE] = '';
  1303. switch ($post_mode)
  1304. {
  1305. case 'delete_topic':
  1306.  
  1307. foreach ($shadow_forum_ids as $updated_forum => $topic_count)
  1308. {
  1309. // counting is fun! we only have to do sizeof($forum_ids) number of queries,
  1310. // even if the topic is moved back to where its shadow lives (we count how many times it is in a forum)
  1311. $sql = 'UPDATE ' . FORUMS_TABLE . '
  1312. SET forum_topics_approved = forum_topics_approved - ' . $topic_count . '
  1313. WHERE forum_id = ' . $updated_forum;
  1314. $db->sql_query($sql);
  1315. update_post_information('forum', $updated_forum);
  1316. }
  1317.  
  1318. if ($is_soft)
  1319. {
  1320. $topic_row = array();
  1321. $phpbb_content_visibility->set_topic_visibility(ITEM_DELETED, $topic_id, $forum_id, $user->data['user_id'], time(), $softdelete_reason);
  1322. }
  1323. else
  1324. {
  1325. delete_topics('topic_id', array($topic_id), false);
  1326.  
  1327. $phpbb_content_visibility->remove_topic_from_statistic($data, $sql_data);
  1328.  
  1329. $update_sql = update_post_information('forum', $forum_id, true);
  1330. if (sizeof($update_sql))
  1331. {
  1332. $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
  1333. $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
  1334. }
  1335. }
  1336.  
  1337. break;
  1338.  
  1339. case 'delete_first_post':
  1340. $sql = 'SELECT p.post_id, p.poster_id, p.post_time, p.post_username, u.username, u.user_colour
  1341. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
  1342. WHERE p.topic_id = $topic_id
  1343. AND p.poster_id = u.user_id
  1344. AND p.post_visibility = " . ITEM_APPROVED . '
  1345. ORDER BY p.post_time ASC, p.post_id ASC';
  1346. $result = $db->sql_query_limit($sql, 1);
  1347. $row = $db->sql_fetchrow($result);
  1348. $db->sql_freeresult($result);
  1349.  
  1350. if (!$row)
  1351. {
  1352. // No approved post, so the first is a not-approved post (unapproved or soft deleted)
  1353. $sql = 'SELECT p.post_id, p.poster_id, p.post_time, p.post_username, u.username, u.user_colour
  1354. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
  1355. WHERE p.topic_id = $topic_id
  1356. AND p.poster_id = u.user_id
  1357. ORDER BY p.post_time ASC, p.post_id ASC";
  1358. $result = $db->sql_query_limit($sql, 1);
  1359. $row = $db->sql_fetchrow($result);
  1360. $db->sql_freeresult($result);
  1361. }
  1362.  
  1363. $next_post_id = (int) $row['post_id'];
  1364.  
  1365. $sql_data[TOPICS_TABLE] = $db->sql_build_array('UPDATE', array(
  1366. 'topic_poster' => (int) $row['poster_id'],
  1367. 'topic_first_post_id' => (int) $row['post_id'],
  1368. 'topic_first_poster_colour' => $row['user_colour'],
  1369. 'topic_first_poster_name' => ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'],
  1370. 'topic_time' => (int) $row['post_time'],
  1371. ));
  1372. break;
  1373.  
  1374. case 'delete_last_post':
  1375. if (!$is_soft)
  1376. {
  1377. // Update last post information when hard deleting. Soft delete already did that by itself.
  1378. $update_sql = update_post_information('forum', $forum_id, true);
  1379. if (sizeof($update_sql))
  1380. {
  1381. $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . implode(', ', $update_sql[$forum_id]);
  1382. }
  1383.  
  1384. $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_bumped = 0, topic_bumper = 0';
  1385.  
  1386. $update_sql = update_post_information('topic', $topic_id, true);
  1387. if (!empty($update_sql))
  1388. {
  1389. $sql_data[TOPICS_TABLE] .= ', ' . implode(', ', $update_sql[$topic_id]);
  1390. $next_post_id = (int) str_replace('topic_last_post_id = ', '', $update_sql[$topic_id][0]);
  1391. }
  1392. }
  1393.  
  1394. if (!$next_post_id)
  1395. {
  1396. $sql = 'SELECT MAX(post_id) as last_post_id
  1397. FROM ' . POSTS_TABLE . "
  1398. WHERE topic_id = $topic_id
  1399. AND " . $phpbb_content_visibility->get_visibility_sql('post', $forum_id);
  1400. $result = $db->sql_query($sql);
  1401. $next_post_id = (int) $db->sql_fetchfield('last_post_id');
  1402. $db->sql_freeresult($result);
  1403. }
  1404. break;
  1405.  
  1406. case 'delete':
  1407. $sql = 'SELECT post_id
  1408. FROM ' . POSTS_TABLE . "
  1409. WHERE topic_id = $topic_id
  1410. AND " . $phpbb_content_visibility->get_visibility_sql('post', $forum_id) . '
  1411. AND post_time > ' . $data['post_time'] . '
  1412. ORDER BY post_time ASC, post_id ASC';
  1413. $result = $db->sql_query_limit($sql, 1);
  1414. $next_post_id = (int) $db->sql_fetchfield('post_id');
  1415. $db->sql_freeresult($result);
  1416. break;
  1417. }
  1418.  
  1419. if (($post_mode == 'delete') || ($post_mode == 'delete_last_post') || ($post_mode == 'delete_first_post'))
  1420. {
  1421. if (!$is_soft)
  1422. {
  1423. $phpbb_content_visibility->remove_post_from_statistic($data, $sql_data);
  1424. }
  1425.  
  1426. $sql = 'SELECT 1 AS has_attachments
  1427. FROM ' . ATTACHMENTS_TABLE . '
  1428. WHERE topic_id = ' . $topic_id;
  1429. $result = $db->sql_query_limit($sql, 1);
  1430. $has_attachments = (int) $db->sql_fetchfield('has_attachments');
  1431. $db->sql_freeresult($result);
  1432.  
  1433. if (!$has_attachments)
  1434. {
  1435. $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_attachment = 0';
  1436. }
  1437. }
  1438.  
  1439. $db->sql_transaction('begin');
  1440.  
  1441. $where_sql = array(
  1442. FORUMS_TABLE => "forum_id = $forum_id",
  1443. TOPICS_TABLE => "topic_id = $topic_id",
  1444. USERS_TABLE => 'user_id = ' . $data['poster_id'],
  1445. );
  1446.  
  1447. foreach ($sql_data as $table => $update_sql)
  1448. {
  1449. if ($update_sql)
  1450. {
  1451. $db->sql_query("UPDATE $table SET $update_sql WHERE " . $where_sql[$table]);
  1452. }
  1453. }
  1454.  
  1455. // Adjust posted info for this user by looking for a post by him/her within this topic...
  1456. if ($post_mode != 'delete_topic' && $config['load_db_track'] && $data['poster_id'] != ANONYMOUS)
  1457. {
  1458. $sql = 'SELECT poster_id
  1459. FROM ' . POSTS_TABLE . '
  1460. WHERE topic_id = ' . $topic_id . '
  1461. AND poster_id = ' . $data['poster_id'];
  1462. $result = $db->sql_query_limit($sql, 1);
  1463. $poster_id = (int) $db->sql_fetchfield('poster_id');
  1464. $db->sql_freeresult($result);
  1465.  
  1466. // The user is not having any more posts within this topic
  1467. if (!$poster_id)
  1468. {
  1469. $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
  1470. WHERE topic_id = ' . $topic_id . '
  1471. AND user_id = ' . $data['poster_id'];
  1472. $db->sql_query($sql);
  1473. }
  1474. }
  1475.  
  1476. $db->sql_transaction('commit');
  1477.  
  1478. if ($data['post_reported'] && ($post_mode != 'delete_topic'))
  1479. {
  1480. sync('topic_reported', 'topic_id', array($topic_id));
  1481. }
  1482.  
  1483. return $next_post_id;
  1484. }
  1485.  
  1486. /**
  1487. * Submit Post
  1488. * @todo Split up and create lightweight, simple API for this.
  1489. */
  1490. function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $update_message = true, $update_search_index = true)
  1491. {
  1492. global $db, $auth, $user, $config, $phpEx, $template, $phpbb_root_path, $phpbb_container, $phpbb_dispatcher;
  1493.  
  1494. /**
  1495. * Modify the data for post submitting
  1496. *
  1497. * @event core.modify_submit_post_data
  1498. * @var string mode Variable containing posting mode value
  1499. * @var string subject Variable containing post subject value
  1500. * @var string username Variable containing post author name
  1501. * @var int topic_type Variable containing topic type value
  1502. * @var array poll Array with the poll data for the post
  1503. * @var array data Array with the data for the post
  1504. * @var bool update_message Flag indicating if the post will be updated
  1505. * @var bool update_search_index Flag indicating if the search index will be updated
  1506. * @since 3.1.0-a4
  1507. */
  1508. $vars = array(
  1509. 'mode',
  1510. 'subject',
  1511. 'username',
  1512. 'topic_type',
  1513. 'poll',
  1514. 'data',
  1515. 'update_message',
  1516. 'update_search_index',
  1517. );
  1518. extract($phpbb_dispatcher->trigger_event('core.modify_submit_post_data', compact($vars)));
  1519.  
  1520. // We do not handle erasing posts here
  1521. if ($mode == 'delete')
  1522. {
  1523. return false;
  1524. }
  1525.  
  1526. $current_time = time();
  1527.  
  1528. if ($mode == 'post')
  1529. {
  1530. $post_mode = 'post';
  1531. $update_message = true;
  1532. }
  1533. else if ($mode != 'edit')
  1534. {
  1535. $post_mode = 'reply';
  1536. $update_message = true;
  1537. }
  1538. else if ($mode == 'edit')
  1539. {
  1540. $post_mode = ($data['topic_posts_approved'] + $data['topic_posts_unapproved'] + $data['topic_posts_softdeleted'] == 1) ? 'edit_topic' : (($data['topic_first_post_id'] == $data['post_id']) ? 'edit_first_post' : (($data['topic_last_post_id'] == $data['post_id']) ? 'edit_last_post' : 'edit'));
  1541. }
  1542.  
  1543. // First of all make sure the subject and topic title are having the correct length.
  1544. // To achieve this without cutting off between special chars we convert to an array and then count the elements.
  1545. $subject = truncate_string($subject, 120);
  1546. $data['topic_title'] = truncate_string($data['topic_title'], 120);
  1547.  
  1548. // Collect some basic information about which tables and which rows to update/insert
  1549. $sql_data = $topic_row = array();
  1550. $poster_id = ($mode == 'edit') ? $data['poster_id'] : (int) $user->data['user_id'];
  1551.  
  1552. // Retrieve some additional information if not present
  1553. if ($mode == 'edit' && (!isset($data['post_visibility']) || !isset($data['topic_visibility']) || $data['post_visibility'] === false || $data['topic_visibility'] === false))
  1554. {
  1555. $sql = 'SELECT p.post_visibility, t.topic_type, t.topic_posts_approved, t.topic_posts_unapproved, t.topic_posts_softdeleted, t.topic_visibility
  1556. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
  1557. WHERE t.topic_id = p.topic_id
  1558. AND p.post_id = ' . $data['post_id'];
  1559. $result = $db->sql_query($sql);
  1560. $topic_row = $db->sql_fetchrow($result);
  1561. $db->sql_freeresult($result);
  1562.  
  1563. $data['topic_visibility'] = $topic_row['topic_visibility'];
  1564. $data['post_visibility'] = $topic_row['post_visibility'];
  1565. }
  1566.  
  1567. // This variable indicates if the user is able to post or put into the queue
  1568. $post_visibility = ITEM_APPROVED;
  1569.  
  1570. // Check the permissions for post approval.
  1571. // Moderators must go through post approval like ordinary users.
  1572. if (!$auth->acl_get('f_noapprove', $data['forum_id']))
  1573. {
  1574. // Post not approved, but in queue
  1575. $post_visibility = ITEM_UNAPPROVED;
  1576. switch ($post_mode)
  1577. {
  1578. case 'edit_first_post':
  1579. case 'edit':
  1580. case 'edit_last_post':
  1581. case 'edit_topic':
  1582. $post_visibility = ITEM_REAPPROVE;
  1583. break;
  1584. }
  1585. }
  1586.  
  1587. // MODs/Extensions are able to force any visibility on posts
  1588. if (isset($data['force_approved_state']))
  1589. {
  1590. $post_visibility = (in_array((int) $data['force_approved_state'], array(ITEM_APPROVED, ITEM_UNAPPROVED, ITEM_DELETED, ITEM_REAPPROVE))) ? (int) $data['force_approved_state'] : $post_visibility;
  1591. }
  1592. if (isset($data['force_visibility']))
  1593. {
  1594. $post_visibility = (in_array((int) $data['force_visibility'], array(ITEM_APPROVED, ITEM_UNAPPROVED, ITEM_DELETED, ITEM_REAPPROVE))) ? (int) $data['force_visibility'] : $post_visibility;
  1595. }
  1596.  
  1597. // Start the transaction here
  1598. $db->sql_transaction('begin');
  1599.  
  1600. // Collect Information
  1601. switch ($post_mode)
  1602. {
  1603. case 'post':
  1604. case 'reply':
  1605. $sql_data[POSTS_TABLE]['sql'] = array(
  1606. 'forum_id' => $data['forum_id'],
  1607. 'poster_id' => (int) $user->data['user_id'],
  1608. 'icon_id' => $data['icon_id'],
  1609. 'poster_ip' => $user->ip,
  1610. 'post_time' => $current_time,
  1611. 'post_visibility' => $post_visibility,
  1612. 'enable_bbcode' => $data['enable_bbcode'],
  1613. 'enable_smilies' => $data['enable_smilies'],
  1614. 'enable_magic_url' => $data['enable_urls'],
  1615. 'enable_sig' => $data['enable_sig'],
  1616. 'post_username' => (!$user->data['is_registered']) ? $username : '',
  1617. 'post_subject' => $subject,
  1618. 'post_text' => $data['message'],
  1619. 'post_checksum' => $data['message_md5'],
  1620. 'post_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
  1621. 'bbcode_bitfield' => $data['bbcode_bitfield'],
  1622. 'bbcode_uid' => $data['bbcode_uid'],
  1623. 'post_postcount' => ($auth->acl_get('f_postcount', $data['forum_id'])) ? 1 : 0,
  1624. 'post_edit_locked' => $data['post_edit_locked']
  1625. );
  1626. break;
  1627.  
  1628. case 'edit_first_post':
  1629. case 'edit':
  1630.  
  1631. case 'edit_last_post':
  1632. case 'edit_topic':
  1633.  
  1634. // If edit reason is given always display edit info
  1635.  
  1636. // If editing last post then display no edit info
  1637. // If m_edit permission then display no edit info
  1638. // If normal edit display edit info
  1639.  
  1640. // Display edit info if edit reason given or user is editing his post, which is not the last within the topic.
  1641. if ($data['post_edit_reason'] || (!$auth->acl_get('m_edit', $data['forum_id']) && ($post_mode == 'edit' || $post_mode == 'edit_first_post')))
  1642. {
  1643. $data['post_edit_reason'] = truncate_string($data['post_edit_reason'], 255, 255, false);
  1644.  
  1645. $sql_data[POSTS_TABLE]['sql'] = array(
  1646. 'post_edit_time' => $current_time,
  1647. 'post_edit_reason' => $data['post_edit_reason'],
  1648. 'post_edit_user' => (int) $data['post_edit_user'],
  1649. );
  1650.  
  1651. $sql_data[POSTS_TABLE]['stat'][] = 'post_edit_count = post_edit_count + 1';
  1652. }
  1653. else if (!$data['post_edit_reason'] && $mode == 'edit' && $auth->acl_get('m_edit', $data['forum_id']))
  1654. {
  1655. $sql_data[POSTS_TABLE]['sql'] = array(
  1656. 'post_edit_reason' => '',
  1657. );
  1658. }
  1659.  
  1660. // If the person editing this post is different to the one having posted then we will add a log entry stating the edit
  1661. // Could be simplified by only adding to the log if the edit is not tracked - but this may confuse admins/mods
  1662. if ($user->data['user_id'] != $poster_id)
  1663. {
  1664. $log_subject = ($subject) ? $subject : $data['topic_title'];
  1665. add_log('mod', $data['forum_id'], $data['topic_id'], 'LOG_POST_EDITED', $log_subject, (!empty($username)) ? $username : $user->lang['GUEST'], $data['post_edit_reason']);
  1666. }
  1667.  
  1668. if (!isset($sql_data[POSTS_TABLE]['sql']))
  1669. {
  1670. $sql_data[POSTS_TABLE]['sql'] = array();
  1671. }
  1672.  
  1673. $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
  1674. 'forum_id' => $data['forum_id'],
  1675. 'poster_id' => $data['poster_id'],
  1676. 'icon_id' => $data['icon_id'],
  1677. // We will change the visibility later
  1678. //'post_visibility' => $post_visibility,
  1679. 'enable_bbcode' => $data['enable_bbcode'],
  1680. 'enable_smilies' => $data['enable_smilies'],
  1681. 'enable_magic_url' => $data['enable_urls'],
  1682. 'enable_sig' => $data['enable_sig'],
  1683. 'post_username' => ($username && $data['poster_id'] == ANONYMOUS) ? $username : '',
  1684. 'post_subject' => $subject,
  1685. 'post_checksum' => $data['message_md5'],
  1686. 'post_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
  1687. 'bbcode_bitfield' => $data['bbcode_bitfield'],
  1688. 'bbcode_uid' => $data['bbcode_uid'],
  1689. 'post_edit_locked' => $data['post_edit_locked'])
  1690. );
  1691.  
  1692. if ($update_message)
  1693. {
  1694. $sql_data[POSTS_TABLE]['sql']['post_text'] = $data['message'];
  1695. }
  1696.  
  1697. break;
  1698. }
  1699. $topic_row = array();
  1700.  
  1701. // And the topic ladies and gentlemen
  1702. switch ($post_mode)
  1703. {
  1704. case 'post':
  1705. $sql_data[TOPICS_TABLE]['sql'] = array(
  1706. 'topic_poster' => (int) $user->data['user_id'],
  1707. 'topic_time' => $current_time,
  1708. 'topic_last_view_time' => $current_time,
  1709. 'forum_id' => $data['forum_id'],
  1710. 'icon_id' => $data['icon_id'],
  1711. 'topic_posts_approved' => ($post_visibility == ITEM_APPROVED) ? 1 : 0,
  1712. 'topic_posts_softdeleted' => ($post_visibility == ITEM_DELETED) ? 1 : 0,
  1713. 'topic_posts_unapproved' => ($post_visibility == ITEM_UNAPPROVED) ? 1 : 0,
  1714. 'topic_visibility' => $post_visibility,
  1715. 'topic_delete_user' => ($post_visibility != ITEM_APPROVED) ? (int) $user->data['user_id'] : 0,
  1716. 'topic_title' => $subject,
  1717. 'topic_first_poster_name' => (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
  1718. 'topic_first_poster_colour' => $user->data['user_colour'],
  1719. 'topic_type' => $topic_type,
  1720. 'topic_time_limit' => ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
  1721. 'topic_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
  1722. );
  1723.  
  1724. if (isset($poll['poll_options']) && !empty($poll['poll_options']))
  1725. {
  1726. $poll_start = ($poll['poll_start']) ? $poll['poll_start'] : $current_time;
  1727. $poll_length = $poll['poll_length'] * 86400;
  1728. if ($poll_length < 0)
  1729. {
  1730. $poll_start = $poll_start + $poll_length;
  1731. if ($poll_start < 0)
  1732. {
  1733. $poll_start = 0;
  1734. }
  1735. $poll_length = 1;
  1736. }
  1737.  
  1738. $sql_data[TOPICS_TABLE]['sql'] = array_merge($sql_data[TOPICS_TABLE]['sql'], array(
  1739. 'poll_title' => $poll['poll_title'],
  1740. 'poll_start' => $poll_start,
  1741. 'poll_max_options' => $poll['poll_max_options'],
  1742. 'poll_length' => $poll_length,
  1743. 'poll_vote_change' => $poll['poll_vote_change'])
  1744. );
  1745. }
  1746.  
  1747. $sql_data[USERS_TABLE]['stat'][] = "user_lastpost_time = $current_time" . (($auth->acl_get('f_postcount', $data['forum_id']) && $post_visibility == ITEM_APPROVED) ? ', user_posts = user_posts + 1' : '');
  1748.  
  1749. if ($post_visibility == ITEM_APPROVED)
  1750. {
  1751. $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_approved = forum_topics_approved + 1';
  1752. $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts_approved = forum_posts_approved + 1';
  1753. }
  1754. else if ($post_visibility == ITEM_UNAPPROVED)
  1755. {
  1756. $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_unapproved = forum_topics_unapproved + 1';
  1757. $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts_unapproved = forum_posts_unapproved + 1';
  1758. }
  1759. else if ($post_visibility == ITEM_DELETED)
  1760. {
  1761. $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_softdeleted = forum_topics_softdeleted + 1';
  1762. $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts_softdeleted = forum_posts_softdeleted + 1';
  1763. }
  1764. break;
  1765.  
  1766. case 'reply':
  1767. $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_view_time = ' . $current_time . ',
  1768. topic_bumped = 0,
  1769. topic_bumper = 0' .
  1770. (($post_visibility == ITEM_APPROVED) ? ', topic_posts_approved = topic_posts_approved + 1' : '') .
  1771. (($post_visibility == ITEM_UNAPPROVED) ? ', topic_posts_unapproved = topic_posts_unapproved + 1' : '') .
  1772. (($post_visibility == ITEM_DELETED) ? ', topic_posts_softdeleted = topic_posts_softdeleted + 1' : '') .
  1773. ((!empty($data['attachment_data']) || (isset($data['topic_attachment']) && $data['topic_attachment'])) ? ', topic_attachment = 1' : '');
  1774.  
  1775. $sql_data[USERS_TABLE]['stat'][] = "user_lastpost_time = $current_time" . (($auth->acl_get('f_postcount', $data['forum_id']) && $post_visibility == ITEM_APPROVED) ? ', user_posts = user_posts + 1' : '');
  1776.  
  1777. if ($post_visibility == ITEM_APPROVED)
  1778. {
  1779. $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts_approved = forum_posts_approved + 1';
  1780. }
  1781. else if ($post_visibility == ITEM_UNAPPROVED)
  1782. {
  1783. $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts_unapproved = forum_posts_unapproved + 1';
  1784. }
  1785. else if ($post_visibility == ITEM_DELETED)
  1786. {
  1787. $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts_softdeleted = forum_posts_softdeleted + 1';
  1788. }
  1789. break;
  1790.  
  1791. case 'edit_topic':
  1792. case 'edit_first_post':
  1793. if (isset($poll['poll_options']))
  1794. {
  1795. $poll_start = ($poll['poll_start'] || empty($poll['poll_options'])) ? $poll['poll_start'] : $current_time;
  1796. $poll_length = $poll['poll_length'] * 86400;
  1797. if ($poll_length < 0)
  1798. {
  1799. $poll_start = $poll_start + $poll_length;
  1800. if ($poll_start < 0)
  1801. {
  1802. $poll_start = 0;
  1803. }
  1804. $poll_length = 1;
  1805. }
  1806. }
  1807.  
  1808. $sql_data[TOPICS_TABLE]['sql'] = array(
  1809. 'forum_id' => $data['forum_id'],
  1810. 'icon_id' => $data['icon_id'],
  1811. 'topic_title' => $subject,
  1812. 'topic_first_poster_name' => $username,
  1813. 'topic_type' => $topic_type,
  1814. 'topic_time_limit' => ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
  1815. 'poll_title' => (isset($poll['poll_options'])) ? $poll['poll_title'] : '',
  1816. 'poll_start' => (isset($poll['poll_options'])) ? $poll_start : 0,
  1817. 'poll_max_options' => (isset($poll['poll_options'])) ? $poll['poll_max_options'] : 1,
  1818. 'poll_length' => (isset($poll['poll_options'])) ? $poll_length : 0,
  1819. 'poll_vote_change' => (isset($poll['poll_vote_change'])) ? $poll['poll_vote_change'] : 0,
  1820. 'topic_last_view_time' => $current_time,
  1821.  
  1822. 'topic_attachment' => (!empty($data['attachment_data'])) ? 1 : (isset($data['topic_attachment']) ? $data['topic_attachment'] : 0),
  1823. );
  1824.  
  1825. break;
  1826. }
  1827.  
  1828. // Submit new topic
  1829. if ($post_mode == 'post')
  1830. {
  1831. $sql = 'INSERT INTO ' . TOPICS_TABLE . ' ' .
  1832. $db->sql_build_array('INSERT', $sql_data[TOPICS_TABLE]['sql']);
  1833. $db->sql_query($sql);
  1834.  
  1835. $data['topic_id'] = $db->sql_nextid();
  1836.  
  1837. $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
  1838. 'topic_id' => $data['topic_id'])
  1839. );
  1840. unset($sql_data[TOPICS_TABLE]['sql']);
  1841. }
  1842.  
  1843. // Submit new post
  1844. if ($post_mode == 'post' || $post_mode == 'reply')
  1845. {
  1846. if ($post_mode == 'reply')
  1847. {
  1848. $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
  1849. 'topic_id' => $data['topic_id'],
  1850. ));
  1851. }
  1852.  
  1853. $sql = 'INSERT INTO ' . POSTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_data[POSTS_TABLE]['sql']);
  1854. $db->sql_query($sql);
  1855. $data['post_id'] = $db->sql_nextid();
  1856.  
  1857. if ($post_mode == 'post' || $post_visibility == ITEM_APPROVED)
  1858. {
  1859. $sql_data[TOPICS_TABLE]['sql'] = array(
  1860. 'topic_last_post_id' => $data['post_id'],
  1861. 'topic_last_post_time' => $current_time,
  1862. 'topic_last_poster_id' => $sql_data[POSTS_TABLE]['sql']['poster_id'],
  1863. 'topic_last_poster_name' => ($user->data['user_id'] == ANONYMOUS) ? $sql_data[POSTS_TABLE]['sql']['post_username'] : $user->data['username'],
  1864. 'topic_last_poster_colour' => $user->data['user_colour'],
  1865. 'topic_last_post_subject' => (string) $subject,
  1866. );
  1867. }
  1868.  
  1869. if ($post_mode == 'post')
  1870. {
  1871. $sql_data[TOPICS_TABLE]['sql']['topic_first_post_id'] = $data['post_id'];
  1872. }
  1873.  
  1874. // Update total post count and forum information
  1875. if ($post_visibility == ITEM_APPROVED)
  1876. {
  1877. if ($post_mode == 'post')
  1878. {
  1879. set_config_count('num_topics', 1, true);
  1880. }
  1881. set_config_count('num_posts', 1, true);
  1882.  
  1883. $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . $data['post_id'];
  1884. $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($subject) . "'";
  1885. $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . $current_time;
  1886. $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $user->data['user_id'];
  1887. $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape((!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : '')) . "'";
  1888. $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($user->data['user_colour']) . "'";
  1889. }
  1890.  
  1891. unset($sql_data[POSTS_TABLE]['sql']);
  1892. }
  1893.  
  1894. // Update the topics table
  1895. if (isset($sql_data[TOPICS_TABLE]['sql']))
  1896. {
  1897. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1898. SET ' . $db->sql_build_array('UPDATE', $sql_data[TOPICS_TABLE]['sql']) . '
  1899. WHERE topic_id = ' . $data['topic_id'];
  1900. $db->sql_query($sql);
  1901.  
  1902. unset($sql_data[TOPICS_TABLE]['sql']);
  1903. }
  1904.  
  1905. // Update the posts table
  1906. if (isset($sql_data[POSTS_TABLE]['sql']))
  1907. {
  1908. $sql = 'UPDATE ' . POSTS_TABLE . '
  1909. SET ' . $db->sql_build_array('UPDATE', $sql_data[POSTS_TABLE]['sql']) . '
  1910. WHERE post_id = ' . $data['post_id'];
  1911. $db->sql_query($sql);
  1912.  
  1913. unset($sql_data[POSTS_TABLE]['sql']);
  1914. }
  1915.  
  1916. // Update Poll Tables
  1917. if (isset($poll['poll_options']))
  1918. {
  1919. $cur_poll_options = array();
  1920.  
  1921. if ($mode == 'edit')
  1922. {
  1923. $sql = 'SELECT *
  1924. FROM ' . POLL_OPTIONS_TABLE . '
  1925. WHERE topic_id = ' . $data['topic_id'] . '
  1926. ORDER BY poll_option_id';
  1927. $result = $db->sql_query($sql);
  1928.  
  1929. $cur_poll_options = array();
  1930. while ($row = $db->sql_fetchrow($result))
  1931. {
  1932. $cur_poll_options[] = $row;
  1933. }
  1934. $db->sql_freeresult($result);
  1935. }
  1936.  
  1937. $sql_insert_ary = array();
  1938.  
  1939. for ($i = 0, $size = sizeof($poll['poll_options']); $i < $size; $i++)
  1940. {
  1941. if (strlen(trim($poll['poll_options'][$i])))
  1942. {
  1943. if (empty($cur_poll_options[$i]))
  1944. {
  1945. // If we add options we need to put them to the end to be able to preserve votes...
  1946. $sql_insert_ary[] = array(
  1947. 'poll_option_id' => (int) sizeof($cur_poll_options) + 1 + sizeof($sql_insert_ary),
  1948. 'topic_id' => (int) $data['topic_id'],
  1949. 'poll_option_text' => (string) $poll['poll_options'][$i]
  1950. );
  1951. }
  1952. else if ($poll['poll_options'][$i] != $cur_poll_options[$i])
  1953. {
  1954. $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . "
  1955. SET poll_option_text = '" . $db->sql_escape($poll['poll_options'][$i]) . "'
  1956. WHERE poll_option_id = " . $cur_poll_options[$i]['poll_option_id'] . '
  1957. AND topic_id = ' . $data['topic_id'];
  1958. $db->sql_query($sql);
  1959. }
  1960. }
  1961. }
  1962.  
  1963. $db->sql_multi_insert(POLL_OPTIONS_TABLE, $sql_insert_ary);
  1964.  
  1965. if (sizeof($poll['poll_options']) < sizeof($cur_poll_options))
  1966. {
  1967. $sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . '
  1968. WHERE poll_option_id > ' . sizeof($poll['poll_options']) . '
  1969. AND topic_id = ' . $data['topic_id'];
  1970. $db->sql_query($sql);
  1971. }
  1972.  
  1973. // If edited, we would need to reset votes (since options can be re-ordered above, you can't be sure if the change is for changing the text or adding an option
  1974. if ($mode == 'edit' && sizeof($poll['poll_options']) != sizeof($cur_poll_options))
  1975. {
  1976. $db->sql_query('DELETE FROM ' . POLL_VOTES_TABLE . ' WHERE topic_id = ' . $data['topic_id']);
  1977. $db->sql_query('UPDATE ' . POLL_OPTIONS_TABLE . ' SET poll_option_total = 0 WHERE topic_id = ' . $data['topic_id']);
  1978. }
  1979. }
  1980.  
  1981. // Submit Attachments
  1982. if (!empty($data['attachment_data']) && $data['post_id'] && in_array($mode, array('post', 'reply', 'quote', 'edit')))
  1983. {
  1984. $space_taken = $files_added = 0;
  1985. $orphan_rows = array();
  1986.  
  1987. foreach ($data['attachment_data'] as $pos => $attach_row)
  1988. {
  1989. $orphan_rows[(int) $attach_row['attach_id']] = array();
  1990. }
  1991.  
  1992. if (sizeof($orphan_rows))
  1993. {
  1994. $sql = 'SELECT attach_id, filesize, physical_filename
  1995. FROM ' . ATTACHMENTS_TABLE . '
  1996. WHERE ' . $db->sql_in_set('attach_id', array_keys($orphan_rows)) . '
  1997. AND is_orphan = 1
  1998. AND poster_id = ' . $user->data['user_id'];
  1999. $result = $db->sql_query($sql);
  2000.  
  2001. $orphan_rows = array();
  2002. while ($row = $db->sql_fetchrow($result))
  2003. {
  2004. $orphan_rows[$row['attach_id']] = $row;
  2005. }
  2006. $db->sql_freeresult($result);
  2007. }
  2008.  
  2009. foreach ($data['attachment_data'] as $pos => $attach_row)
  2010. {
  2011. if ($attach_row['is_orphan'] && !isset($orphan_rows[$attach_row['attach_id']]))
  2012. {
  2013. continue;
  2014. }
  2015.  
  2016. if (!$attach_row['is_orphan'])
  2017. {
  2018. // update entry in db if attachment already stored in db and filespace
  2019. $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
  2020. SET attach_comment = '" . $db->sql_escape($attach_row['attach_comment']) . "'
  2021. WHERE attach_id = " . (int) $attach_row['attach_id'] . '
  2022. AND is_orphan = 0';
  2023. $db->sql_query($sql);
  2024. }
  2025. else
  2026. {
  2027. // insert attachment into db
  2028. if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . utf8_basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
  2029. {
  2030. continue;
  2031. }
  2032.  
  2033. $space_taken += $orphan_rows[$attach_row['attach_id']]['filesize'];
  2034. $files_added++;
  2035.  
  2036. $attach_sql = array(
  2037. 'post_msg_id' => $data['post_id'],
  2038. 'topic_id' => $data['topic_id'],
  2039. 'is_orphan' => 0,
  2040. 'poster_id' => $poster_id,
  2041. 'attach_comment' => $attach_row['attach_comment'],
  2042. );
  2043.  
  2044. $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $attach_sql) . '
  2045. WHERE attach_id = ' . $attach_row['attach_id'] . '
  2046. AND is_orphan = 1
  2047. AND poster_id = ' . $user->data['user_id'];
  2048. $db->sql_query($sql);
  2049. }
  2050. }
  2051.  
  2052. if ($space_taken && $files_added)
  2053. {
  2054. set_config_count('upload_dir_size', $space_taken, true);
  2055. set_config_count('num_files', $files_added, true);
  2056. }
  2057. }
  2058.  
  2059. $first_post_has_topic_info = ($post_mode == 'edit_first_post' &&
  2060. (($post_visibility == ITEM_DELETED && $data['topic_posts_softdeleted'] == 1) ||
  2061. ($post_visibility == ITEM_UNAPPROVED && $data['topic_posts_unapproved'] == 1) ||
  2062. ($post_visibility == ITEM_REAPPROVE && $data['topic_posts_unapproved'] == 1) ||
  2063. ($post_visibility == ITEM_APPROVED && $data['topic_posts_approved'] == 1)));
  2064. // Fix the post's and topic's visibility and first/last post information, when the post is edited
  2065. if (($post_mode != 'post' && $post_mode != 'reply') && $data['post_visibility'] != $post_visibility)
  2066. {
  2067. // If the post was not approved, it could also be the starter,
  2068. // so we sync the starter after approving/restoring, to ensure that the stats are correct
  2069. // Same applies for the last post
  2070. $is_starter = ($post_mode == 'edit_first_post' || $post_mode == 'edit_topic' || $data['post_visibility'] != ITEM_APPROVED);
  2071. $is_latest = ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || $data['post_visibility'] != ITEM_APPROVED);
  2072.  
  2073. $phpbb_content_visibility = $phpbb_container->get('content.visibility');
  2074. $phpbb_content_visibility->set_post_visibility($post_visibility, $data['post_id'], $data['topic_id'], $data['forum_id'], $user->data['user_id'], time(), '', $is_starter, $is_latest);
  2075. }
  2076. else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || $first_post_has_topic_info)
  2077. {
  2078. if ($post_visibility == ITEM_APPROVED || $data['topic_visibility'] == $post_visibility)
  2079. {
  2080. // only the subject can be changed from edit
  2081. $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
  2082.  
  2083. // Maybe not only the subject, but also changing anonymous usernames. ;)
  2084. if ($data['poster_id'] == ANONYMOUS)
  2085. {
  2086. $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape($username) . "'";
  2087. }
  2088.  
  2089. if ($post_visibility == ITEM_APPROVED)
  2090. {
  2091. // this does not _necessarily_ mean that we must update the info again,
  2092. // it just means that we might have to
  2093. $sql = 'SELECT forum_last_post_id, forum_last_post_subject
  2094. FROM ' . FORUMS_TABLE . '
  2095. WHERE forum_id = ' . (int) $data['forum_id'];
  2096. $result = $db->sql_query($sql);
  2097. $row = $db->sql_fetchrow($result);
  2098. $db->sql_freeresult($result);
  2099.  
  2100. // this post is the latest post in the forum, better update
  2101. if ($row['forum_last_post_id'] == $data['post_id'] && ($row['forum_last_post_subject'] !== $subject || $data['poster_id'] == ANONYMOUS))
  2102. {
  2103. // the post's subject changed
  2104. if ($row['forum_last_post_subject'] !== $subject)
  2105. {
  2106. $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($subject) . "'";
  2107. }
  2108.  
  2109. // Update the user name if poster is anonymous... just in case a moderator changed it
  2110. if ($data['poster_id'] == ANONYMOUS)
  2111. {
  2112. $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape($username) . "'";
  2113. }
  2114. }
  2115. }
  2116. }
  2117. }
  2118.  
  2119. // Update forum stats
  2120. $where_sql = array(
  2121. POSTS_TABLE => 'post_id = ' . $data['post_id'],
  2122. TOPICS_TABLE => 'topic_id = ' . $data['topic_id'],
  2123. FORUMS_TABLE => 'forum_id = ' . $data['forum_id'],
  2124. USERS_TABLE => 'user_id = ' . $poster_id
  2125. );
  2126.  
  2127. foreach ($sql_data as $table => $update_ary)
  2128. {
  2129. if (isset($update_ary['stat']) && implode('', $update_ary['stat']))
  2130. {
  2131. $sql = "UPDATE $table SET " . implode(', ', $update_ary['stat']) . ' WHERE ' . $where_sql[$table];
  2132. $db->sql_query($sql);
  2133. }
  2134. }
  2135.  
  2136. // Delete topic shadows (if any exist). We do not need a shadow topic for an global announcement
  2137. if ($topic_type == POST_GLOBAL)
  2138. {
  2139. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  2140. WHERE topic_moved_id = ' . $data['topic_id'];
  2141. $db->sql_query($sql);
  2142. }
  2143.  
  2144. // Committing the transaction before updating search index
  2145. $db->sql_transaction('commit');
  2146.  
  2147. // Delete draft if post was loaded...
  2148. $draft_id = request_var('draft_loaded', 0);
  2149. if ($draft_id)
  2150. {
  2151. $sql = 'DELETE FROM ' . DRAFTS_TABLE . "
  2152. WHERE draft_id = $draft_id
  2153. AND user_id = {$user->data['user_id']}";
  2154. $db->sql_query($sql);
  2155. }
  2156.  
  2157. // Index message contents
  2158. if ($update_search_index && $data['enable_indexing'])
  2159. {
  2160. // Select the search method and do some additional checks to ensure it can actually be utilised
  2161. $search_type = $config['search_type'];
  2162.  
  2163. if (!class_exists($search_type))
  2164. {
  2165. trigger_error('NO_SUCH_SEARCH_MODULE');
  2166. }
  2167.  
  2168. $error = false;
  2169. $search = new $search_type($error, $phpbb_root_path, $phpEx, $auth, $config, $db, $user);
  2170.  
  2171. if ($error)
  2172. {
  2173. trigger_error($error);
  2174. }
  2175.  
  2176. $search->index($mode, $data['post_id'], $data['message'], $subject, $poster_id, $data['forum_id']);
  2177. }
  2178.  
  2179. // Topic Notification, do not change if moderator is changing other users posts...
  2180. if ($user->data['user_id'] == $poster_id)
  2181. {
  2182. if (!$data['notify_set'] && $data['notify'])
  2183. {
  2184. $sql = 'INSERT INTO ' . TOPICS_WATCH_TABLE . ' (user_id, topic_id)
  2185. VALUES (' . $user->data['user_id'] . ', ' . $data['topic_id'] . ')';
  2186. $db->sql_query($sql);
  2187. }
  2188. else if (($config['email_enable'] || $config['jab_enable']) && $data['notify_set'] && !$data['notify'])
  2189. {
  2190. $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . '
  2191. WHERE user_id = ' . $user->data['user_id'] . '
  2192. AND topic_id = ' . $data['topic_id'];
  2193. $db->sql_query($sql);
  2194. }
  2195. }
  2196.  
  2197. if ($mode == 'post' || $mode == 'reply' || $mode == 'quote')
  2198. {
  2199. // Mark this topic as posted to
  2200. markread('post', $data['forum_id'], $data['topic_id']);
  2201. }
  2202.  
  2203. // Mark this topic as read
  2204. // We do not use post_time here, this is intended (post_time can have a date in the past if editing a message)
  2205. markread('topic', $data['forum_id'], $data['topic_id'], time());
  2206.  
  2207. //
  2208. if ($config['load_db_lastread'] && $user->data['is_registered'])
  2209. {
  2210. $sql = 'SELECT mark_time
  2211. FROM ' . FORUMS_TRACK_TABLE . '
  2212. WHERE user_id = ' . $user->data['user_id'] . '
  2213. AND forum_id = ' . $data['forum_id'];
  2214. $result = $db->sql_query($sql);
  2215. $f_mark_time = (int) $db->sql_fetchfield('mark_time');
  2216. $db->sql_freeresult($result);
  2217. }
  2218. else if ($config['load_anon_lastread'] || $user->data['is_registered'])
  2219. {
  2220. $f_mark_time = false;
  2221. }
  2222.  
  2223. if (($config['load_db_lastread'] && $user->data['is_registered']) || $config['load_anon_lastread'] || $user->data['is_registered'])
  2224. {
  2225. // Update forum info
  2226. $sql = 'SELECT forum_last_post_time
  2227. FROM ' . FORUMS_TABLE . '
  2228. WHERE forum_id = ' . $data['forum_id'];
  2229. $result = $db->sql_query($sql);
  2230. $forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
  2231. $db->sql_freeresult($result);
  2232.  
  2233. update_forum_tracking_info($data['forum_id'], $forum_last_post_time, $f_mark_time, false);
  2234. }
  2235.  
  2236. // If a username was supplied or the poster is a guest, we will use the supplied username.
  2237. // Doing it this way we can use "...post by guest-username..." in notifications when
  2238. // "guest-username" is supplied or ommit the username if it is not.
  2239. $username = ($username !== '' || !$user->data['is_registered']) ? $username : $user->data['username'];
  2240.  
  2241. // Send Notifications
  2242. $notification_data = array_merge($data, array(
  2243. 'topic_title' => (isset($data['topic_title'])) ? $data['topic_title'] : $subject,
  2244. 'post_username' => $username,
  2245. 'poster_id' => $poster_id,
  2246. 'post_text' => $data['message'],
  2247. 'post_time' => $current_time,
  2248. 'post_subject' => $subject,
  2249. ));
  2250.  
  2251. $phpbb_notifications = $phpbb_container->get('notification_manager');
  2252.  
  2253. if ($post_visibility == ITEM_APPROVED)
  2254. {
  2255. switch ($mode)
  2256. {
  2257. case 'post':
  2258. $phpbb_notifications->add_notifications(array(
  2259. 'notification.type.quote',
  2260. 'notification.type.topic',
  2261. ), $notification_data);
  2262. break;
  2263.  
  2264. case 'reply':
  2265. case 'quote':
  2266. $phpbb_notifications->add_notifications(array(
  2267. 'notification.type.quote',
  2268. 'notification.type.bookmark',
  2269. 'notification.type.post',
  2270. ), $notification_data);
  2271. break;
  2272.  
  2273. case 'edit_topic':
  2274. case 'edit_first_post':
  2275. case 'edit':
  2276. case 'edit_last_post':
  2277. $phpbb_notifications->update_notifications(array(
  2278. 'notification.type.quote',
  2279. 'notification.type.bookmark',
  2280. 'notification.type.topic',
  2281. 'notification.type.post',
  2282. ), $notification_data);
  2283. break;
  2284. }
  2285. }
  2286. else if ($post_visibility == ITEM_UNAPPROVED)
  2287. {
  2288. switch ($mode)
  2289. {
  2290. case 'post':
  2291. $phpbb_notifications->add_notifications('notification.type.topic_in_queue', $notification_data);
  2292. break;
  2293.  
  2294. case 'reply':
  2295. case 'quote':
  2296. $phpbb_notifications->add_notifications('notification.type.post_in_queue', $notification_data);
  2297. break;
  2298.  
  2299. case 'edit_topic':
  2300. case 'edit_first_post':
  2301. case 'edit':
  2302. case 'edit_last_post':
  2303. // Nothing to do here
  2304. break;
  2305. }
  2306. }
  2307. else if ($post_visibility == ITEM_REAPPROVE)
  2308. {
  2309. switch ($mode)
  2310. {
  2311. case 'edit_topic':
  2312. case 'edit_first_post':
  2313. $phpbb_notifications->add_notifications('notification.type.topic_in_queue', $notification_data);
  2314.  
  2315. // Delete the approve_post notification so we can notify the user again,
  2316. // when his post got reapproved
  2317. $phpbb_notifications->delete_notifications('notification.type.approve_post', $notification_data['post_id']);
  2318. break;
  2319.  
  2320. case 'edit':
  2321. case 'edit_last_post':
  2322. $phpbb_notifications->add_notifications('notification.type.post_in_queue', $notification_data);
  2323.  
  2324. // Delete the approve_post notification so we can notify the user again,
  2325. // when his post got reapproved
  2326. $phpbb_notifications->delete_notifications('notification.type.approve_post', $notification_data['post_id']);
  2327. break;
  2328.  
  2329. case 'post':
  2330. case 'reply':
  2331. case 'quote':
  2332. // Nothing to do here
  2333. break;
  2334. }
  2335. }
  2336. else if ($post_visibility == ITEM_DELETED)
  2337. {
  2338. switch ($mode)
  2339. {
  2340. case 'post':
  2341. case 'reply':
  2342. case 'quote':
  2343. case 'edit_topic':
  2344. case 'edit_first_post':
  2345. case 'edit':
  2346. case 'edit_last_post':
  2347. // Nothing to do here
  2348. break;
  2349. }
  2350. }
  2351.  
  2352. $params = $add_anchor = '';
  2353.  
  2354. if ($post_visibility == ITEM_APPROVED)
  2355. {
  2356. $params .= '&amp;t=' . $data['topic_id'];
  2357.  
  2358. if ($mode != 'post')
  2359. {
  2360. $params .= '&amp;p=' . $data['post_id'];
  2361. $add_anchor = '#p' . $data['post_id'];
  2362. }
  2363. }
  2364. else if ($mode != 'post' && $post_mode != 'edit_first_post' && $post_mode != 'edit_topic')
  2365. {
  2366. $params .= '&amp;t=' . $data['topic_id'];
  2367. }
  2368.  
  2369. $url = (!$params) ? "{$phpbb_root_path}viewforum.$phpEx" : "{$phpbb_root_path}viewtopic.$phpEx";
  2370. $url = append_sid($url, 'f=' . $data['forum_id'] . $params) . $add_anchor;
  2371.  
  2372. /**
  2373. * This event is used for performing actions directly after a post or topic
  2374. * has been submitted. When a new topic is posted, the topic ID is
  2375. * available in the $data array.
  2376. *
  2377. * The only action that can be done by altering data made available to this
  2378. * event is to modify the return URL ($url).
  2379. *
  2380. * @event core.submit_post_end
  2381. * @var string mode Variable containing posting mode value
  2382. * @var string subject Variable containing post subject value
  2383. * @var string username Variable containing post author name
  2384. * @var int topic_type Variable containing topic type value
  2385. * @var array poll Array with the poll data for the post
  2386. * @var array data Array with the data for the post
  2387. * @var int post_visibility Variable containing up to date post visibility
  2388. * @var bool update_message Flag indicating if the post will be updated
  2389. * @var bool update_search_index Flag indicating if the search index will be updated
  2390. * @var string url The "Return to topic" URL
  2391. *
  2392. * @since 3.1.0-a3
  2393. * @change 3.1.0-RC3 Added vars mode, subject, username, topic_type,
  2394. * poll, update_message, update_search_index
  2395. */
  2396. $vars = array(
  2397. 'mode',
  2398. 'subject',
  2399. 'username',
  2400. 'topic_type',
  2401. 'poll',
  2402. 'data',
  2403. 'post_visibility',
  2404. 'update_message',
  2405. 'update_search_index',
  2406. 'url',
  2407. );
  2408. extract($phpbb_dispatcher->trigger_event('core.submit_post_end', compact($vars)));
  2409.  
  2410. return $url;
  2411. }
  2412.  
  2413. /**
  2414. * Handle topic bumping
  2415. * @param int $forum_id The ID of the forum the topic is being bumped belongs to
  2416. * @param int $topic_id The ID of the topic is being bumping
  2417. * @param array $post_data Passes some topic parameters:
  2418. * - 'topic_title'
  2419. * - 'topic_last_post_id'
  2420. * - 'topic_last_poster_id'
  2421. * - 'topic_last_post_subject'
  2422. * - 'topic_last_poster_name'
  2423. * - 'topic_last_poster_colour'
  2424. * @param int $bump_time The time at which topic was bumped, usually it is a current time as obtained via time().
  2425. * @return string An URL to the bumped topic, example: ./viewtopic.php?forum_id=1&amptopic_id=2&ampp=3#p3
  2426. */
  2427. function phpbb_bump_topic($forum_id, $topic_id, $post_data, $bump_time = false)
  2428. {
  2429. global $config, $db, $user, $phpEx, $phpbb_root_path;
  2430.  
  2431. if ($bump_time === false)
  2432. {
  2433. $bump_time = time();
  2434. }
  2435.  
  2436. // Begin bumping
  2437. $db->sql_transaction('begin');
  2438.  
  2439. // Update the topic's last post post_time
  2440. $sql = 'UPDATE ' . POSTS_TABLE . "
  2441. SET post_time = $bump_time
  2442. WHERE post_id = {$post_data['topic_last_post_id']}
  2443. AND topic_id = $topic_id";
  2444. $db->sql_query($sql);
  2445.  
  2446. // Sync the topic's last post time, the rest of the topic's last post data isn't changed
  2447. $sql = 'UPDATE ' . TOPICS_TABLE . "
  2448. SET topic_last_post_time = $bump_time,
  2449. topic_bumped = 1,
  2450. topic_bumper = " . $user->data['user_id'] . "
  2451. WHERE topic_id = $topic_id";
  2452. $db->sql_query($sql);
  2453.  
  2454. // Update the forum's last post info
  2455. $sql = 'UPDATE ' . FORUMS_TABLE . "
  2456. SET forum_last_post_id = " . $post_data['topic_last_post_id'] . ",
  2457. forum_last_poster_id = " . $post_data['topic_last_poster_id'] . ",
  2458. forum_last_post_subject = '" . $db->sql_escape($post_data['topic_last_post_subject']) . "',
  2459. forum_last_post_time = $bump_time,
  2460. forum_last_poster_name = '" . $db->sql_escape($post_data['topic_last_poster_name']) . "',
  2461. forum_last_poster_colour = '" . $db->sql_escape($post_data['topic_last_poster_colour']) . "'
  2462. WHERE forum_id = $forum_id";
  2463. $db->sql_query($sql);
  2464.  
  2465. // Update bumper's time of the last posting to prevent flood
  2466. $sql = 'UPDATE ' . USERS_TABLE . "
  2467. SET user_lastpost_time = $bump_time
  2468. WHERE user_id = " . $user->data['user_id'];
  2469. $db->sql_query($sql);
  2470.  
  2471. $db->sql_transaction('commit');
  2472.  
  2473. // Mark this topic as posted to
  2474. markread('post', $forum_id, $topic_id, $bump_time);
  2475.  
  2476. // Mark this topic as read
  2477. markread('topic', $forum_id, $topic_id, $bump_time);
  2478.  
  2479. // Update forum tracking info
  2480. if ($config['load_db_lastread'] && $user->data['is_registered'])
  2481. {
  2482. $sql = 'SELECT mark_time
  2483. FROM ' . FORUMS_TRACK_TABLE . '
  2484. WHERE user_id = ' . $user->data['user_id'] . '
  2485. AND forum_id = ' . $forum_id;
  2486. $result = $db->sql_query($sql);
  2487. $f_mark_time = (int) $db->sql_fetchfield('mark_time');
  2488. $db->sql_freeresult($result);
  2489. }
  2490. else if ($config['load_anon_lastread'] || $user->data['is_registered'])
  2491. {
  2492. $f_mark_time = false;
  2493. }
  2494.  
  2495. if (($config['load_db_lastread'] && $user->data['is_registered']) || $config['load_anon_lastread'] || $user->data['is_registered'])
  2496. {
  2497. // Update forum info
  2498. $sql = 'SELECT forum_last_post_time
  2499. FROM ' . FORUMS_TABLE . '
  2500. WHERE forum_id = ' . $forum_id;
  2501. $result = $db->sql_query($sql);
  2502. $forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
  2503. $db->sql_freeresult($result);
  2504.  
  2505. update_forum_tracking_info($forum_id, $forum_last_post_time, $f_mark_time, false);
  2506. }
  2507.  
  2508. add_log('mod', $forum_id, $topic_id, 'LOG_BUMP_TOPIC', $post_data['topic_title']);
  2509.  
  2510. $url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;p={$post_data['topic_last_post_id']}") . "#p{$post_data['topic_last_post_id']}";
  2511.  
  2512. return $url;
  2513. }
  2514.  
  2515. /**
  2516. * Show upload popup (progress bar)
  2517. */
  2518. function phpbb_upload_popup($forum_style = 0)
  2519. {
  2520. global $template, $user;
  2521.  
  2522. ($forum_style) ? $user->setup('posting', $forum_style) : $user->setup('posting');
  2523.  
  2524. page_header($user->lang['PROGRESS_BAR']);
  2525.  
  2526. $template->set_filenames(array(
  2527. 'popup' => 'posting_progress_bar.html')
  2528. );
  2529.  
  2530. $template->assign_vars(array(
  2531. 'PROGRESS_BAR' => $user->img('upload_bar', $user->lang['UPLOAD_IN_PROGRESS']))
  2532. );
  2533.  
  2534. $template->display('popup');
  2535.  
  2536. garbage_collection();
  2537. exit_handler();
  2538. }
  2539.  
  2540. /**
  2541. * Do the various checks required for removing posts as well as removing it
  2542. */
  2543. function phpbb_handle_post_delete($forum_id, $topic_id, $post_id, &$post_data, $is_soft = false, $delete_reason = '')
  2544. {
  2545. global $user, $auth, $config, $request;
  2546. global $phpbb_root_path, $phpEx;
  2547.  
  2548. $perm_check = ($is_soft) ? 'softdelete' : 'delete';
  2549.  
  2550. // If moderator removing post or user itself removing post, present a confirmation screen
  2551. if ($auth->acl_get("m_$perm_check", $forum_id) || ($post_data['poster_id'] == $user->data['user_id'] && $user->data['is_registered'] && $auth->acl_get("f_$perm_check", $forum_id) && $post_id == $post_data['topic_last_post_id'] && !$post_data['post_edit_locked'] && ($post_data['post_time'] > time() - ($config['delete_time'] * 60) || !$config['delete_time'])))
  2552. {
  2553. $s_hidden_fields = array(
  2554. 'p' => $post_id,
  2555. 'f' => $forum_id,
  2556. 'mode' => ($is_soft) ? 'soft_delete' : 'delete',
  2557. );
  2558.  
  2559. if (confirm_box(true))
  2560. {
  2561. $data = array(
  2562. 'topic_first_post_id' => $post_data['topic_first_post_id'],
  2563. 'topic_last_post_id' => $post_data['topic_last_post_id'],
  2564. 'topic_posts_approved' => $post_data['topic_posts_approved'],
  2565. 'topic_posts_unapproved' => $post_data['topic_posts_unapproved'],
  2566. 'topic_posts_softdeleted' => $post_data['topic_posts_softdeleted'],
  2567. 'topic_visibility' => $post_data['topic_visibility'],
  2568. 'topic_type' => $post_data['topic_type'],
  2569. 'post_visibility' => $post_data['post_visibility'],
  2570. 'post_reported' => $post_data['post_reported'],
  2571. 'post_time' => $post_data['post_time'],
  2572. 'poster_id' => $post_data['poster_id'],
  2573. 'post_postcount' => $post_data['post_postcount'],
  2574. );
  2575.  
  2576. $next_post_id = delete_post($forum_id, $topic_id, $post_id, $data, $is_soft, $delete_reason);
  2577. $post_username = ($post_data['poster_id'] == ANONYMOUS && !empty($post_data['post_username'])) ? $post_data['post_username'] : $post_data['username'];
  2578.  
  2579. if ($next_post_id === false)
  2580. {
  2581. add_log('mod', $forum_id, $topic_id, (($is_soft) ? 'LOG_SOFTDELETE_TOPIC' : 'LOG_DELETE_TOPIC'), $post_data['topic_title'], $post_username, $delete_reason);
  2582.  
  2583. $meta_info = append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id");
  2584. $message = $user->lang['POST_DELETED'];
  2585. }
  2586. else
  2587. {
  2588. add_log('mod', $forum_id, $topic_id, (($is_soft) ? 'LOG_SOFTDELETE_POST' : 'LOG_DELETE_POST'), $post_data['post_subject'], $post_username, $delete_reason);
  2589.  
  2590. $meta_info = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;p=$next_post_id") . "#p$next_post_id";
  2591. $message = $user->lang['POST_DELETED'];
  2592.  
  2593. if (!$request->is_ajax())
  2594. {
  2595. $message .= '<br /><br />' . $user->lang('RETURN_TOPIC', '<a href="' . $meta_info . '">', '</a>');
  2596. }
  2597. }
  2598.  
  2599. meta_refresh(3, $meta_info);
  2600. if (!$request->is_ajax())
  2601. {
  2602. $message .= '<br /><br />' . $user->lang('RETURN_FORUM', '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id) . '">', '</a>');
  2603. }
  2604. trigger_error($message);
  2605. }
  2606. else
  2607. {
  2608. global $user, $template, $request;
  2609.  
  2610. $can_delete = $auth->acl_get('m_delete', $forum_id) || ($post_data['poster_id'] == $user->data['user_id'] && $user->data['is_registered'] && $auth->acl_get('f_delete', $forum_id));
  2611. $can_softdelete = $auth->acl_get('m_softdelete', $forum_id) || ($post_data['poster_id'] == $user->data['user_id'] && $user->data['is_registered'] && $auth->acl_get('f_softdelete', $forum_id));
  2612.  
  2613. $template->assign_vars(array(
  2614. 'S_SOFTDELETED' => $post_data['post_visibility'] == ITEM_DELETED,
  2615. 'S_CHECKED_PERMANENT' => $request->is_set_post('delete_permanent') ? ' checked="checked"' : '',
  2616. 'S_ALLOWED_DELETE' => $can_delete,
  2617. 'S_ALLOWED_SOFTDELETE' => $can_softdelete,
  2618. ));
  2619.  
  2620. $l_confirm = 'DELETE_POST';
  2621. if ($post_data['post_visibility'] == ITEM_DELETED)
  2622. {
  2623. $l_confirm .= '_PERMANENTLY';
  2624. $s_hidden_fields['delete_permanent'] = '1';
  2625. }
  2626. else if (!$can_softdelete)
  2627. {
  2628. $s_hidden_fields['delete_permanent'] = '1';
  2629. }
  2630.  
  2631. confirm_box(false, $l_confirm, build_hidden_fields($s_hidden_fields), 'confirm_delete_body.html');
  2632. }
  2633. }
  2634.  
  2635. // If we are here the user is not able to delete - present the correct error message
  2636. if ($post_data['poster_id'] != $user->data['user_id'] && $auth->acl_get('f_delete', $forum_id))
  2637. {
  2638. trigger_error('DELETE_OWN_POSTS');
  2639. }
  2640.  
  2641. if ($post_data['poster_id'] == $user->data['user_id'] && $auth->acl_get('f_delete', $forum_id) && $post_id != $post_data['topic_last_post_id'])
  2642. {
  2643. trigger_error('CANNOT_DELETE_REPLIED');
  2644. }
  2645.  
  2646. trigger_error('USER_CANNOT_DELETE');
  2647. }
Add Comment
Please, Sign In to add comment