PageRenderTime 95ms CodeModel.GetById 28ms RepoModel.GetById 8ms app.codeStats 1ms

/forum/includes/functions_posting.php

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