PageRenderTime 70ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/functions_posting.php

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