PageRenderTime 37ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/functions_posting.php

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