6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * Fill smiley templates (or just the variables) with smilies, either in a window or inline
22 function generate_smilies($mode, $forum_id)
24 global $auth, $db, $user, $config, $template;
25 global $phpEx, $phpbb_root_path;
27 $start = request_var('start', 0);
29 if ($mode == 'window')
33 $sql = 'SELECT forum_style
34 FROM ' . FORUMS_TABLE . "
35 WHERE forum_id = $forum_id";
36 $result = $db->sql_query_limit($sql, 1);
37 $row = $db->sql_fetchrow($result);
38 $db->sql_freeresult($result);
40 $user->setup('posting', (int) $row['forum_style']);
44 $user->setup('posting');
47 page_header($user->lang['SMILIES']);
49 $sql = 'SELECT COUNT(smiley_id) AS item_count
50 FROM ' . SMILIES_TABLE . '
52 $result = $db->sql_query($sql, 3600);
55 while ($row = $db->sql_fetchrow($result))
59 $db->sql_freeresult($result);
61 $template->set_filenames(array(
62 'body' => 'posting_smilies.html')
65 $template->assign_var('PAGINATION',
66 generate_pagination(append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&f=' . $forum_id),
67 $smiley_count, $config['smilies_per_page'], $start, true)
71 $display_link = false;
72 if ($mode == 'inline')
74 $sql = 'SELECT smiley_id
75 FROM ' . SMILIES_TABLE . '
76 WHERE display_on_posting = 0';
77 $result = $db->sql_query_limit($sql, 1, 0, 3600);
79 if ($row = $db->sql_fetchrow($result))
83 $db->sql_freeresult($result);
86 if ($mode == 'window')
88 $sql = 'SELECT smiley_url, MIN(emotion) as emotion, MIN(code) AS code, smiley_width, smiley_height, MIN(smiley_order) AS min_smiley_order
89 FROM ' . SMILIES_TABLE . '
90 GROUP BY smiley_url, smiley_width, smiley_height
91 ORDER BY min_smiley_order';
92 $result = $db->sql_query_limit($sql, $config['smilies_per_page'], $start, 3600);
97 FROM ' . SMILIES_TABLE . '
98 WHERE display_on_posting = 1
99 ORDER BY smiley_order';
100 $result = $db->sql_query($sql, 3600);
104 while ($row = $db->sql_fetchrow($result))
106 if (empty($smilies[$row['smiley_url']]))
108 $smilies[$row['smiley_url']] = $row;
111 $db->sql_freeresult($result);
113 if (sizeof($smilies))
115 $root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_root_path;
117 foreach ($smilies as $row)
119 $template->assign_block_vars('smiley', array(
120 'SMILEY_CODE' => $row['code'],
121 'A_SMILEY_CODE' => addslashes($row['code']),
122 'SMILEY_IMG' => $root_path . $config['smilies_path'] . '/' . $row['smiley_url'],
123 'SMILEY_WIDTH' => $row['smiley_width'],
124 'SMILEY_HEIGHT' => $row['smiley_height'],
125 'SMILEY_DESC' => $row['emotion'])
130 if ($mode == 'inline' && $display_link)
132 $template->assign_vars(array(
133 'S_SHOW_SMILEY_LINK' => true,
134 'U_MORE_SMILIES' => append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&f=' . $forum_id))
138 if ($mode == 'window')
145 * Update last post information
146 * Should be used instead of sync() if only the last post information are out of sync... faster
148 * @param string $type Can be forum|topic
149 * @param mixed $ids topic/forum ids
150 * @param bool $return_update_sql true: SQL query shall be returned, false: execute SQL
152 function update_post_information($type, $ids, $return_update_sql = false)
166 $update_sql = $empty_forums = $not_empty_forums = array();
168 if ($type != 'topic')
170 $topic_join = ', ' . TOPICS_TABLE . ' t';
171 $topic_condition = 'AND t.topic_id = p.topic_id AND t.topic_approved = 1';
176 $topic_condition = '';
179 if (sizeof($ids) == 1)
181 $sql = 'SELECT MAX(p.post_id) as last_post_id
182 FROM ' . POSTS_TABLE . " p $topic_join
183 WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
185 AND p.post_approved = 1";
189 $sql = 'SELECT p.' . $type . '_id, MAX(p.post_id) as last_post_id
190 FROM ' . POSTS_TABLE . " p $topic_join
191 WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
193 AND p.post_approved = 1
194 GROUP BY p.{$type}_id";
196 $result = $db->sql_query($sql);
198 $last_post_ids = array();
199 while ($row = $db->sql_fetchrow($result))
201 if (sizeof($ids) == 1)
203 $row[$type . '_id'] = $ids[0];
206 if ($type == 'forum')
208 $not_empty_forums[] = $row['forum_id'];
210 if (empty($row['last_post_id']))
212 $empty_forums[] = $row['forum_id'];
216 $last_post_ids[] = $row['last_post_id'];
218 $db->sql_freeresult($result);
220 if ($type == 'forum')
222 $empty_forums = array_merge($empty_forums, array_diff($ids, $not_empty_forums));
224 foreach ($empty_forums as $void => $forum_id)
226 $update_sql[$forum_id][] = 'forum_last_post_id = 0';
227 $update_sql[$forum_id][] = "forum_last_post_subject = ''";
228 $update_sql[$forum_id][] = 'forum_last_post_time = 0';
229 $update_sql[$forum_id][] = 'forum_last_poster_id = 0';
230 $update_sql[$forum_id][] = "forum_last_poster_name = ''";
231 $update_sql[$forum_id][] = "forum_last_poster_colour = ''";
235 if (sizeof($last_post_ids))
237 $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
238 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
239 WHERE p.poster_id = u.user_id
240 AND ' . $db->sql_in_set('p.post_id', $last_post_ids);
241 $result = $db->sql_query($sql);
243 while ($row = $db->sql_fetchrow($result))
245 $update_sql[$row["{$type}_id"]][] = $type . '_last_post_id = ' . (int) $row['post_id'];
246 $update_sql[$row["{$type}_id"]][] = "{$type}_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
247 $update_sql[$row["{$type}_id"]][] = $type . '_last_post_time = ' . (int) $row['post_time'];
248 $update_sql[$row["{$type}_id"]][] = $type . '_last_poster_id = ' . (int) $row['poster_id'];
249 $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
250 $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "'";
252 $db->sql_freeresult($result);
254 unset($empty_forums, $ids, $last_post_ids);
256 if ($return_update_sql || !sizeof($update_sql))
261 $table = ($type == 'forum') ? FORUMS_TABLE : TOPICS_TABLE;
263 foreach ($update_sql as $update_id => $update_sql_ary)
265 $sql = "UPDATE $table
266 SET " . implode(', ', $update_sql_ary) . "
267 WHERE {$type}_id = $update_id";
268 $db->sql_query($sql);
275 * Generate Topic Icons for display
277 function posting_gen_topic_icons($mode, $icon_id)
279 global $phpbb_root_path, $config, $template, $cache;
282 $icons = $cache->obtain_icons();
286 $template->assign_var('S_NO_ICON_CHECKED', ' checked="checked"');
291 foreach ($icons as $id => $data)
293 if ($data['display'])
295 $template->assign_block_vars('topic_icon', array(
297 'ICON_IMG' => $phpbb_root_path . $config['icons_path'] . '/' . $data['img'],
298 'ICON_WIDTH' => $data['width'],
299 'ICON_HEIGHT' => $data['height'],
301 'S_CHECKED' => ($id == $icon_id) ? true : false,
302 'S_ICON_CHECKED' => ($id == $icon_id) ? ' checked="checked"' : '')
314 * Build topic types able to be selected
316 function posting_gen_topic_types($forum_id, $cur_topic_type = POST_NORMAL)
318 global $auth, $user, $template, $topic_type;
322 $topic_types = array(
323 'sticky' => array('const' => POST_STICKY, 'lang' => 'POST_STICKY'),
324 'announce' => array('const' => POST_ANNOUNCE, 'lang' => 'POST_ANNOUNCEMENT'),
325 'global' => array('const' => POST_GLOBAL, 'lang' => 'POST_GLOBAL')
328 $topic_type_array = array();
330 foreach ($topic_types as $auth_key => $topic_value)
332 // We do not have a special post global announcement permission
333 $auth_key = ($auth_key == 'global') ? 'announce' : $auth_key;
335 if ($auth->acl_get('f_' . $auth_key, $forum_id))
339 $topic_type_array[] = array(
340 'VALUE' => $topic_value['const'],
341 'S_CHECKED' => ($cur_topic_type == $topic_value['const'] || ($forum_id == 0 && $topic_value['const'] == POST_GLOBAL)) ? ' checked="checked"' : '',
342 'L_TOPIC_TYPE' => $user->lang[$topic_value['lang']]
349 $topic_type_array = array_merge(array(0 => array(
350 'VALUE' => POST_NORMAL,
351 'S_CHECKED' => ($cur_topic_type == POST_NORMAL) ? ' checked="checked"' : '',
352 'L_TOPIC_TYPE' => $user->lang['POST_NORMAL'])),
357 foreach ($topic_type_array as $array)
359 $template->assign_block_vars('topic_type', $array);
362 $template->assign_vars(array(
363 'S_TOPIC_TYPE_STICKY' => ($auth->acl_get('f_sticky', $forum_id)),
364 'S_TOPIC_TYPE_ANNOUNCE' => ($auth->acl_get('f_announce', $forum_id)))
372 // Attachment related functions
376 * Upload Attachment - filedata is generated here
379 function upload_attachment($form_name, $forum_id, $local = false, $local_storage = '', $is_message = false, $local_filedata = false)
381 global $auth, $user, $config, $db, $cache;
382 global $phpbb_root_path, $phpEx;
388 include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
389 $upload = new fileupload();
391 if ($config['check_attachment_content'] && isset($config['mime_triggers']))
393 $upload->set_disallowed_content(explode('|', $config['mime_triggers']));
398 $filedata['post_attach'] = ($upload->is_valid($form_name)) ? true : false;
402 $filedata['post_attach'] = true;
405 if (!$filedata['post_attach'])
407 $filedata['error'][] = $user->lang['NO_UPLOAD_FORM_FOUND'];
411 $extensions = $cache->obtain_attach_extensions((($is_message) ? false : (int) $forum_id));
412 $upload->set_allowed_extensions(array_keys($extensions['_allowed_']));
414 $file = ($local) ? $upload->local_upload($local_storage, $local_filedata) : $upload->form_upload($form_name);
416 if ($file->init_error)
418 $filedata['post_attach'] = false;
422 $cat_id = (isset($extensions[$file->get('extension')]['display_cat'])) ? $extensions[$file->get('extension')]['display_cat'] : ATTACHMENT_CATEGORY_NONE;
424 // Make sure the image category only holds valid images...
425 if ($cat_id == ATTACHMENT_CATEGORY_IMAGE && !$file->is_image())
429 // If this error occurs a user tried to exploit an IE Bug by renaming extensions
430 // Since the image category is displaying content inline we need to catch this.
431 trigger_error($user->lang['ATTACHED_IMAGE_NOT_IMAGE']);
434 // Do we have to create a thumbnail?
435 $filedata['thumbnail'] = ($cat_id == ATTACHMENT_CATEGORY_IMAGE && $config['img_create_thumbnail']) ? 1 : 0;
437 // Check Image Size, if it is an image
438 if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id) && $cat_id == ATTACHMENT_CATEGORY_IMAGE)
440 $file->upload->set_allowed_dimensions(0, 0, $config['img_max_width'], $config['img_max_height']);
443 // Admins and mods are allowed to exceed the allowed filesize
444 if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id))
446 if (!empty($extensions[$file->get('extension')]['max_filesize']))
448 $allowed_filesize = $extensions[$file->get('extension')]['max_filesize'];
452 $allowed_filesize = ($is_message) ? $config['max_filesize_pm'] : $config['max_filesize'];
455 $file->upload->set_max_filesize($allowed_filesize);
458 $file->clean_filename('unique', $user->data['user_id'] . '_');
460 // Are we uploading an image *and* this image being within the image category? Only then perform additional image checks.
461 $no_image = ($cat_id == ATTACHMENT_CATEGORY_IMAGE) ? false : true;
463 $file->move_file($config['upload_path'], false, $no_image);
465 if (sizeof($file->error))
468 $filedata['error'] = array_merge($filedata['error'], $file->error);
469 $filedata['post_attach'] = false;
474 $filedata['filesize'] = $file->get('filesize');
475 $filedata['mimetype'] = $file->get('mimetype');
476 $filedata['extension'] = $file->get('extension');
477 $filedata['physical_filename'] = $file->get('realname');
478 $filedata['real_filename'] = $file->get('uploadname');
479 $filedata['filetime'] = time();
481 // Check our complete quota
482 if ($config['attachment_quota'])
484 if ($config['upload_dir_size'] + $file->get('filesize') > $config['attachment_quota'])
486 $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
487 $filedata['post_attach'] = false;
495 // Check free disk space
496 if ($free_space = @disk_free_space($phpbb_root_path . $config['upload_path']))
498 if ($free_space <= $file->get('filesize'))
500 $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
501 $filedata['post_attach'] = false;
510 if ($filedata['thumbnail'])
512 $source = $file->get('destination_file');
513 $destination = $file->get('destination_path') . '/thumb_' . $file->get('realname');
515 if (!create_thumbnail($source, $destination, $file->get('mimetype')))
517 $filedata['thumbnail'] = 0;
525 * Calculate the needed size for Thumbnail
527 function get_img_size_format($width, $height)
531 // Maximum Width the Image can take
532 $max_width = ($config['img_max_thumb_width']) ? $config['img_max_thumb_width'] : 400;
534 if ($width > $height)
537 round($width * ($max_width / $width)),
538 round($height * ($max_width / $width))
544 round($width * ($max_width / $height)),
545 round($height * ($max_width / $height))
551 * Return supported image types
553 function get_supported_image_types($type = false)
555 if (@extension_loaded('gd'))
557 $format = imagetypes();
562 // Type is one of the IMAGETYPE constants - it is fetched from getimagesize()
563 // We do not use the constants here, because some were not available in PHP 4.3.x
568 $new_type = ($format & IMG_GIF) ? IMG_GIF : false;
577 $new_type = ($format & IMG_JPG) ? IMG_JPG : false;
582 $new_type = ($format & IMG_PNG) ? IMG_PNG : false;
587 $new_type = ($format & IMG_WBMP) ? IMG_WBMP : false;
594 $go_through_types = array(IMG_GIF, IMG_JPG, IMG_PNG, IMG_WBMP);
596 foreach ($go_through_types as $check_type)
598 if ($format & $check_type)
600 $new_type[] = $check_type;
606 'gd' => ($new_type) ? true : false,
607 'format' => $new_type,
608 'version' => (function_exists('imagecreatetruecolor')) ? 2 : 1
612 return array('gd' => false);
618 function create_thumbnail($source, $destination, $mimetype)
622 $min_filesize = (int) $config['img_min_thumb_filesize'];
623 $img_filesize = (file_exists($source)) ? @filesize($source) : false;
625 if (!$img_filesize || $img_filesize <= $min_filesize)
630 $dimension = @getimagesize($source);
632 if ($dimension === false)
637 list($width, $height, $type, ) = $dimension;
639 if (empty($width) || empty($height))
644 list($new_width, $new_height) = get_img_size_format($width, $height);
646 // Do not create a thumbnail if the resulting width/height is bigger than the original one
647 if ($new_width >= $width && $new_height >= $height)
652 $used_imagick = false;
654 // Only use imagemagick if defined and the passthru function not disabled
655 if ($config['img_imagick'] && function_exists('passthru'))
657 if (substr($config['img_imagick'], -1) !== '/')
659 $config['img_imagick'] .= '/';
662 @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) . '"');
664 if (file_exists($destination))
666 $used_imagick = true;
672 $type = get_supported_image_types($type);
676 // If the type is not supported, we are not able to create a thumbnail
677 if ($type['format'] === false)
682 switch ($type['format'])
685 $image = @imagecreatefromgif($source);
689 @ini_set('gd.jpeg_ignore_warning', 1);
690 $image = @imagecreatefromjpeg($source);
694 $image = @imagecreatefrompng($source);
698 $image = @imagecreatefromwbmp($source);
707 if ($type['version'] == 1)
709 $new_image = imagecreate($new_width, $new_height);
711 if ($new_image === false)
716 imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
720 $new_image = imagecreatetruecolor($new_width, $new_height);
722 if ($new_image === false)
727 // Preserve alpha transparency (png for example)
728 @imagealphablending($new_image, false);
729 @imagesavealpha($new_image, true);
731 imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
734 // If we are in safe mode create the destination file prior to using the gd functions to circumvent a PHP bug
735 if (@ini_get('safe_mode') || @strtolower(ini_get('safe_mode')) == 'on')
737 @touch($destination);
740 switch ($type['format'])
743 imagegif($new_image, $destination);
747 imagejpeg($new_image, $destination, 90);
751 imagepng($new_image, $destination);
755 imagewbmp($new_image, $destination);
759 imagedestroy($new_image);
767 if (!file_exists($destination))
772 phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);
778 * Assign Inline attachments (build option fields)
780 function posting_gen_inline_attachments(&$attachment_data)
784 if (sizeof($attachment_data))
786 $s_inline_attachment_options = '';
788 foreach ($attachment_data as $i => $attachment)
790 $s_inline_attachment_options .= '<option value="' . $i . '">' . utf8_basename($attachment['real_filename']) . '</option>';
793 $template->assign_var('S_INLINE_ATTACHMENT_OPTIONS', $s_inline_attachment_options);
802 * Generate inline attachment entry
804 function posting_gen_attachment_entry($attachment_data, &$filename_data, $show_attach_box = true)
806 global $template, $config, $phpbb_root_path, $phpEx, $user, $auth;
808 // Some default template variables
809 $template->assign_vars(array(
810 'S_SHOW_ATTACH_BOX' => $show_attach_box,
811 'S_HAS_ATTACHMENTS' => sizeof($attachment_data),
812 'FILESIZE' => $config['max_filesize'],
813 'FILE_COMMENT' => (isset($filename_data['filecomment'])) ? $filename_data['filecomment'] : '',
816 if (sizeof($attachment_data))
818 // We display the posted attachments within the desired order.
819 ($config['display_order']) ? krsort($attachment_data) : ksort($attachment_data);
821 foreach ($attachment_data as $count => $attach_row)
824 $attach_row['real_filename'] = utf8_basename($attach_row['real_filename']);
826 foreach ($attach_row as $key => $value)
828 $hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />';
831 $download_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'mode=view&id=' . (int) $attach_row['attach_id'], true, ($attach_row['is_orphan']) ? $user->session_id : false);
833 $template->assign_block_vars('attach_row', array(
834 'FILENAME' => utf8_basename($attach_row['real_filename']),
835 'A_FILENAME' => addslashes(utf8_basename($attach_row['real_filename'])),
836 'FILE_COMMENT' => $attach_row['attach_comment'],
837 'ATTACH_ID' => $attach_row['attach_id'],
838 'S_IS_ORPHAN' => $attach_row['is_orphan'],
839 'ASSOC_INDEX' => $count,
841 'U_VIEW_ATTACHMENT' => $download_link,
842 'S_HIDDEN' => $hidden)
847 return sizeof($attachment_data);
851 // General Post functions
857 function load_drafts($topic_id = 0, $forum_id = 0, $id = 0, $pm_action = '', $msg_id = 0)
859 global $user, $db, $template, $auth;
860 global $phpbb_root_path, $phpEx;
862 $topic_ids = $forum_ids = $draft_rows = array();
864 // Load those drafts not connected to forums/topics
865 // If forum_id == 0 AND topic_id == 0 then this is a PM draft
866 if (!$topic_id && !$forum_id)
868 $sql_and = ' AND d.forum_id = 0 AND d.topic_id = 0';
873 $sql_and .= ($forum_id) ? ' AND d.forum_id = ' . (int) $forum_id : '';
874 $sql_and .= ($topic_id) ? ' AND d.topic_id = ' . (int) $topic_id : '';
877 $sql = 'SELECT d.*, f.forum_id, f.forum_name
878 FROM ' . DRAFTS_TABLE . ' d
879 LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = d.forum_id)
880 WHERE d.user_id = ' . $user->data['user_id'] . "
882 ORDER BY d.save_time DESC";
883 $result = $db->sql_query($sql);
885 while ($row = $db->sql_fetchrow($result))
887 if ($row['topic_id'])
889 $topic_ids[] = (int) $row['topic_id'];
891 $draft_rows[] = $row;
893 $db->sql_freeresult($result);
895 if (!sizeof($draft_rows))
900 $topic_rows = array();
901 if (sizeof($topic_ids))
903 $sql = 'SELECT topic_id, forum_id, topic_title
904 FROM ' . TOPICS_TABLE . '
905 WHERE ' . $db->sql_in_set('topic_id', array_unique($topic_ids));
906 $result = $db->sql_query($sql);
908 while ($row = $db->sql_fetchrow($result))
910 $topic_rows[$row['topic_id']] = $row;
912 $db->sql_freeresult($result);
916 $template->assign_var('S_SHOW_DRAFTS', true);
918 foreach ($draft_rows as $draft)
920 $link_topic = $link_forum = $link_pm = false;
921 $insert_url = $view_url = $title = '';
923 if (isset($topic_rows[$draft['topic_id']])
925 ($topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_get('f_read', $topic_rows[$draft['topic_id']]['forum_id']))
927 (!$topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_getf_global('f_read'))
930 $topic_forum_id = ($topic_rows[$draft['topic_id']]['forum_id']) ? $topic_rows[$draft['topic_id']]['forum_id'] : $forum_id;
933 $view_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topic_forum_id . '&t=' . $draft['topic_id']);
934 $title = $topic_rows[$draft['topic_id']]['topic_title'];
936 $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $topic_forum_id . '&t=' . $draft['topic_id'] . '&mode=reply&d=' . $draft['draft_id']);
938 else if ($draft['forum_id'] && $auth->acl_get('f_read', $draft['forum_id']))
941 $view_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $draft['forum_id']);
942 $title = $draft['forum_name'];
944 $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $draft['forum_id'] . '&mode=post&d=' . $draft['draft_id']);
948 // Either display as PM draft if forum_id and topic_id are empty or if access to the forums has been denied afterwards...
950 $insert_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&mode=compose&d={$draft['draft_id']}" . (($pm_action) ? "&action=$pm_action" : '') . (($msg_id) ? "&p=$msg_id" : ''));
953 $template->assign_block_vars('draftrow', array(
954 'DRAFT_ID' => $draft['draft_id'],
955 'DATE' => $user->format_date($draft['save_time']),
956 'DRAFT_SUBJECT' => $draft['draft_subject'],
959 'U_VIEW' => $view_url,
960 'U_INSERT' => $insert_url,
962 'S_LINK_PM' => $link_pm,
963 'S_LINK_TOPIC' => $link_topic,
964 'S_LINK_FORUM' => $link_forum)
972 function topic_review($topic_id, $forum_id, $mode = 'topic_review', $cur_post_id = 0, $show_quote_button = true)
974 global $user, $auth, $db, $template, $bbcode, $cache;
975 global $config, $phpbb_root_path, $phpEx;
977 // Go ahead and pull all data for this topic
978 $sql = 'SELECT p.post_id
979 FROM ' . POSTS_TABLE . ' p' . "
980 WHERE p.topic_id = $topic_id
981 " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . '
982 ' . (($mode == 'post_review') ? " AND p.post_id > $cur_post_id" : '') . '
983 ' . (($mode == 'post_review_edit') ? " AND p.post_id = $cur_post_id" : '') . '
984 ORDER BY p.post_time ';
985 $sql .= ($mode == 'post_review') ? 'ASC' : 'DESC';
986 $result = $db->sql_query_limit($sql, $config['posts_per_page']);
988 $post_list = array();
990 while ($row = $db->sql_fetchrow($result))
992 $post_list[] = $row['post_id'];
995 $db->sql_freeresult($result);
997 if (!sizeof($post_list))
1002 // Handle 'post_review_edit' like 'post_review' from now on
1003 if ($mode == 'post_review_edit')
1005 $mode = 'post_review';
1008 $sql = $db->sql_build_query('SELECT', array(
1009 'SELECT' => 'u.username, u.user_id, u.user_colour, p.*, z.friend, z.foe',
1016 'LEFT_JOIN' => array(
1018 'FROM' => array(ZEBRA_TABLE => 'z'),
1019 'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
1023 'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
1024 AND u.user_id = p.poster_id'
1027 $result = $db->sql_query($sql);
1029 $bbcode_bitfield = '';
1031 $has_attachments = false;
1032 while ($row = $db->sql_fetchrow($result))
1034 $rowset[$row['post_id']] = $row;
1035 $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
1037 if ($row['post_attachment'])
1039 $has_attachments = true;
1042 $db->sql_freeresult($result);
1044 // Instantiate BBCode class
1045 if (!isset($bbcode) && $bbcode_bitfield !== '')
1047 include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
1048 $bbcode = new bbcode(base64_encode($bbcode_bitfield));
1052 $extensions = $attachments = array();
1053 if ($has_attachments && $auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
1055 $extensions = $cache->obtain_attach_extensions($forum_id);
1057 // Get attachments...
1059 FROM ' . ATTACHMENTS_TABLE . '
1060 WHERE ' . $db->sql_in_set('post_msg_id', $post_list) . '
1062 ORDER BY filetime DESC, post_msg_id ASC';
1063 $result = $db->sql_query($sql);
1065 while ($row = $db->sql_fetchrow($result))
1067 $attachments[$row['post_msg_id']][] = $row;
1069 $db->sql_freeresult($result);
1072 for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
1074 // A non-existing rowset only happens if there was no user present for the entered poster_id
1075 // This could be a broken posts table.
1076 if (!isset($rowset[$post_list[$i]]))
1081 $row =& $rowset[$post_list[$i]];
1083 $poster_id = $row['user_id'];
1084 $post_subject = $row['post_subject'];
1085 $message = censor_text($row['post_text']);
1087 $decoded_message = false;
1089 if ($show_quote_button && $auth->acl_get('f_reply', $forum_id))
1091 $decoded_message = $message;
1092 decode_message($decoded_message, $row['bbcode_uid']);
1094 $decoded_message = bbcode_nl2br($decoded_message);
1097 if ($row['bbcode_bitfield'])
1099 $bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
1102 $message = bbcode_nl2br($message);
1103 $message = smiley_text($message, !$row['enable_smilies']);
1105 if (!empty($attachments[$row['post_id']]))
1107 $update_count = array();
1108 parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
1111 $post_subject = censor_text($post_subject);
1113 $post_anchor = ($mode == 'post_review') ? 'ppr' . $row['post_id'] : 'pr' . $row['post_id'];
1114 $u_show_post = append_sid($phpbb_root_path . 'viewtopic.' . $phpEx, "f=$forum_id&t=$topic_id&p={$row['post_id']}&view=show#p{$row['post_id']}");
1116 $template->assign_block_vars($mode . '_row', array(
1117 'POST_AUTHOR_FULL' => get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1118 'POST_AUTHOR_COLOUR' => get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1119 'POST_AUTHOR' => get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1120 'U_POST_AUTHOR' => get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1122 'S_HAS_ATTACHMENTS' => (!empty($attachments[$row['post_id']])) ? true : false,
1123 'S_FRIEND' => ($row['friend']) ? true : false,
1124 'S_IGNORE_POST' => ($row['foe']) ? true : false,
1125 '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>') : '',
1127 'POST_SUBJECT' => $post_subject,
1128 'MINI_POST_IMG' => $user->img('icon_post_target', $user->lang['POST']),
1129 'POST_DATE' => $user->format_date($row['post_time']),
1130 'MESSAGE' => $message,
1131 'DECODED_MESSAGE' => $decoded_message,
1132 'POST_ID' => $row['post_id'],
1133 'U_MINI_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . '#p' . $row['post_id'],
1134 'U_MCP_DETAILS' => ($auth->acl_get('m_info', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&mode=post_details&f=' . $forum_id . '&p=' . $row['post_id'], true, $user->session_id) : '',
1135 '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'])) : '')
1138 // Display not already displayed Attachments for this post, we already parsed them. ;)
1139 if (!empty($attachments[$row['post_id']]))
1141 foreach ($attachments[$row['post_id']] as $attachment)
1143 $template->assign_block_vars($mode . '_row.attachment', array(
1144 'DISPLAY_ATTACHMENT' => $attachment)
1149 unset($rowset[$post_list[$i]]);
1152 if ($mode == 'topic_review')
1154 $template->assign_var('QUOTE_IMG', $user->img('icon_post_quote', $user->lang['REPLY_WITH_QUOTE']));
1163 function user_notification($mode, $subject, $topic_title, $forum_name, $forum_id, $topic_id, $post_id)
1165 global $db, $user, $config, $phpbb_root_path, $phpEx, $auth;
1167 $topic_notification = ($mode == 'reply' || $mode == 'quote') ? true : false;
1168 $forum_notification = ($mode == 'post') ? true : false;
1170 if (!$topic_notification && !$forum_notification)
1172 trigger_error('NO_MODE');
1175 if (($topic_notification && !$config['allow_topic_notify']) || ($forum_notification && !$config['allow_forum_notify']))
1180 $topic_title = ($topic_notification) ? $topic_title : $subject;
1181 $topic_title = censor_text($topic_title);
1183 // Get banned User ID's
1184 $sql = 'SELECT ban_userid
1185 FROM ' . BANLIST_TABLE . '
1186 WHERE ban_userid <> 0
1187 AND ban_exclude <> 1';
1188 $result = $db->sql_query($sql);
1190 $sql_ignore_users = ANONYMOUS . ', ' . $user->data['user_id'];
1191 while ($row = $db->sql_fetchrow($result))
1193 $sql_ignore_users .= ', ' . (int) $row['ban_userid'];
1195 $db->sql_freeresult($result);
1197 $notify_rows = array();
1199 // -- get forum_userids || topic_userids
1200 $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
1201 FROM ' . (($topic_notification) ? TOPICS_WATCH_TABLE : FORUMS_WATCH_TABLE) . ' w, ' . USERS_TABLE . ' u
1202 WHERE w.' . (($topic_notification) ? 'topic_id' : 'forum_id') . ' = ' . (($topic_notification) ? $topic_id : $forum_id) . "
1203 AND w.user_id NOT IN ($sql_ignore_users)
1204 AND w.notify_status = " . NOTIFY_YES . '
1205 AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')
1206 AND u.user_id = w.user_id';
1207 $result = $db->sql_query($sql);
1209 while ($row = $db->sql_fetchrow($result))
1211 $notify_rows[$row['user_id']] = array(
1212 'user_id' => $row['user_id'],
1213 'username' => $row['username'],
1214 'user_email' => $row['user_email'],
1215 'user_jabber' => $row['user_jabber'],
1216 'user_lang' => $row['user_lang'],
1217 'notify_type' => ($topic_notification) ? 'topic' : 'forum',
1218 'template' => ($topic_notification) ? 'topic_notify' : 'newtopic_notify',
1219 'method' => $row['user_notify_type'],
1223 $db->sql_freeresult($result);
1225 // forum notification is sent to those not already receiving topic notifications
1226 if ($topic_notification)
1228 if (sizeof($notify_rows))
1230 $sql_ignore_users .= ', ' . implode(', ', array_keys($notify_rows));
1233 $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
1234 FROM ' . FORUMS_WATCH_TABLE . ' fw, ' . USERS_TABLE . " u
1235 WHERE fw.forum_id = $forum_id
1236 AND fw.user_id NOT IN ($sql_ignore_users)
1237 AND fw.notify_status = " . NOTIFY_YES . '
1238 AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')
1239 AND u.user_id = fw.user_id';
1240 $result = $db->sql_query($sql);
1242 while ($row = $db->sql_fetchrow($result))
1244 $notify_rows[$row['user_id']] = array(
1245 'user_id' => $row['user_id'],
1246 'username' => $row['username'],
1247 'user_email' => $row['user_email'],
1248 'user_jabber' => $row['user_jabber'],
1249 'user_lang' => $row['user_lang'],
1250 'notify_type' => 'forum',
1251 'template' => 'forum_notify',
1252 'method' => $row['user_notify_type'],
1256 $db->sql_freeresult($result);
1259 if (!sizeof($notify_rows))
1264 // Make sure users are allowed to read the forum
1265 foreach ($auth->acl_get_list(array_keys($notify_rows), 'f_read', $forum_id) as $forum_id => $forum_ary)
1267 foreach ($forum_ary as $auth_option => $user_ary)
1269 foreach ($user_ary as $user_id)
1271 $notify_rows[$user_id]['allowed'] = true;
1277 // Now, we have to do a little step before really sending, we need to distinguish our users a little bit. ;)
1278 $msg_users = $delete_ids = $update_notification = array();
1279 foreach ($notify_rows as $user_id => $row)
1281 if (!$row['allowed'] || !trim($row['user_email']))
1283 $delete_ids[$row['notify_type']][] = $row['user_id'];
1287 $msg_users[] = $row;
1288 $update_notification[$row['notify_type']][] = $row['user_id'];
1291 unset($notify_rows);
1293 // Now, we are able to really send out notifications
1294 if (sizeof($msg_users))
1296 include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
1297 $messenger = new messenger();
1299 $msg_list_ary = array();
1300 foreach ($msg_users as $row)
1302 $pos = (!isset($msg_list_ary[$row['template']])) ? 0 : sizeof($msg_list_ary[$row['template']]);
1304 $msg_list_ary[$row['template']][$pos]['method'] = $row['method'];
1305 $msg_list_ary[$row['template']][$pos]['email'] = $row['user_email'];
1306 $msg_list_ary[$row['template']][$pos]['jabber'] = $row['user_jabber'];
1307 $msg_list_ary[$row['template']][$pos]['name'] = $row['username'];
1308 $msg_list_ary[$row['template']][$pos]['lang'] = $row['user_lang'];
1309 $msg_list_ary[$row['template']][$pos]['user_id']= $row['user_id'];
1313 foreach ($msg_list_ary as $email_template => $email_list)
1315 foreach ($email_list as $addr)
1317 $messenger->template($email_template, $addr['lang']);
1319 $messenger->to($addr['email'], $addr['name']);
1320 $messenger->im($addr['jabber'], $addr['name']);
1322 $messenger->assign_vars(array(
1323 'USERNAME' => htmlspecialchars_decode($addr['name']),
1324 'TOPIC_TITLE' => htmlspecialchars_decode($topic_title),
1325 'FORUM_NAME' => htmlspecialchars_decode($forum_name),
1327 'U_FORUM' => generate_board_url() . "/viewforum.$phpEx?f=$forum_id",
1328 'U_TOPIC' => generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id",
1329 'U_NEWEST_POST' => generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id&p=$post_id&e=$post_id",
1330 'U_STOP_WATCHING_TOPIC' => generate_board_url() . "/viewtopic.$phpEx?uid={$addr['user_id']}&f=$forum_id&t=$topic_id&unwatch=topic",
1331 'U_STOP_WATCHING_FORUM' => generate_board_url() . "/viewforum.$phpEx?uid={$addr['user_id']}&f=$forum_id&unwatch=forum",
1334 $messenger->send($addr['method']);
1337 unset($msg_list_ary);
1339 $messenger->save_queue();
1342 // Handle the DB updates
1343 $db->sql_transaction('begin');
1345 if (!empty($update_notification['topic']))
1347 $sql = 'UPDATE ' . TOPICS_WATCH_TABLE . '
1348 SET notify_status = ' . NOTIFY_NO . "
1349 WHERE topic_id = $topic_id
1350 AND " . $db->sql_in_set('user_id', $update_notification['topic']);
1351 $db->sql_query($sql);
1354 if (!empty($update_notification['forum']))
1356 $sql = 'UPDATE ' . FORUMS_WATCH_TABLE . '
1357 SET notify_status = ' . NOTIFY_NO . "
1358 WHERE forum_id = $forum_id
1359 AND " . $db->sql_in_set('user_id', $update_notification['forum']);
1360 $db->sql_query($sql);
1363 // Now delete the user_ids not authorised to receive notifications on this topic/forum
1364 if (!empty($delete_ids['topic']))
1366 $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . "
1367 WHERE topic_id = $topic_id
1368 AND " . $db->sql_in_set('user_id', $delete_ids['topic']);
1369 $db->sql_query($sql);
1372 if (!empty($delete_ids['forum']))
1374 $sql = 'DELETE FROM ' . FORUMS_WATCH_TABLE . "
1375 WHERE forum_id = $forum_id
1376 AND " . $db->sql_in_set('user_id', $delete_ids['forum']);
1377 $db->sql_query($sql);
1380 $db->sql_transaction('commit');
1384 // Post handling functions
1390 function delete_post($forum_id, $topic_id, $post_id, &$data)
1392 global $db, $user, $auth;
1393 global $config, $phpEx, $phpbb_root_path;
1395 // Specify our post mode
1396 $post_mode = 'delete';
1397 if (($data['topic_first_post_id'] === $data['topic_last_post_id']) && $data['topic_replies_real'] == 0)
1399 $post_mode = 'delete_topic';
1401 else if ($data['topic_first_post_id'] == $post_id)
1403 $post_mode = 'delete_first_post';
1405 else if ($data['topic_last_post_id'] == $post_id)
1407 $post_mode = 'delete_last_post';
1409 $sql_data = array();
1410 $next_post_id = false;
1412 include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
1414 $db->sql_transaction('begin');
1416 // we must make sure to update forums that contain the shadow'd topic
1417 if ($post_mode == 'delete_topic')
1419 $shadow_forum_ids = array();
1421 $sql = 'SELECT forum_id
1422 FROM ' . TOPICS_TABLE . '
1423 WHERE ' . $db->sql_in_set('topic_moved_id', $topic_id);
1424 $result = $db->sql_query($sql);
1425 while ($row = $db->sql_fetchrow($result))
1427 if (!isset($shadow_forum_ids[(int) $row['forum_id']]))
1429 $shadow_forum_ids[(int) $row['forum_id']] = 1;
1433 $shadow_forum_ids[(int) $row['forum_id']]++;
1436 $db->sql_freeresult($result);
1439 if (!delete_posts('post_id', array($post_id), false, false))
1441 // Try to delete topic, we may had an previous error causing inconsistency
1442 if ($post_mode == 'delete_topic')
1444 delete_topics('topic_id', array($topic_id), false);
1446 trigger_error('ALREADY_DELETED');
1449 $db->sql_transaction('commit');
1451 // Collect the necessary information for updating the tables
1452 $sql_data[FORUMS_TABLE] = '';
1455 case 'delete_topic':
1457 foreach ($shadow_forum_ids as $updated_forum => $topic_count)
1459 // counting is fun! we only have to do sizeof($forum_ids) number of queries,
1460 // even if the topic is moved back to where its shadow lives (we count how many times it is in a forum)
1461 $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);
1462 update_post_information('forum', $updated_forum);
1465 delete_topics('topic_id', array($topic_id), false);
1467 if ($data['topic_type'] != POST_GLOBAL)
1469 $sql_data[FORUMS_TABLE] .= 'forum_topics_real = forum_topics_real - 1';
1470 $sql_data[FORUMS_TABLE] .= ($data['topic_approved']) ? ', forum_posts = forum_posts - 1, forum_topics = forum_topics - 1' : '';
1473 $update_sql = update_post_information('forum', $forum_id, true);
1474 if (sizeof($update_sql))
1476 $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
1477 $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
1481 case 'delete_first_post':
1482 $sql = 'SELECT p.post_id, p.poster_id, p.post_time, p.post_username, u.username, u.user_colour
1483 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
1484 WHERE p.topic_id = $topic_id
1485 AND p.poster_id = u.user_id
1486 ORDER BY p.post_time ASC";
1487 $result = $db->sql_query_limit($sql, 1);
1488 $row = $db->sql_fetchrow($result);
1489 $db->sql_freeresult($result);
1491 if ($data['topic_type'] != POST_GLOBAL)
1493 $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
1496 $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'];
1498 // 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"
1499 $sql_data[TOPICS_TABLE] .= ', topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
1501 $next_post_id = (int) $row['post_id'];
1504 case 'delete_last_post':
1505 if ($data['topic_type'] != POST_GLOBAL)
1507 $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
1510 $update_sql = update_post_information('forum', $forum_id, true);
1511 if (sizeof($update_sql))
1513 $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
1514 $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
1517 $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' : '');
1519 $update_sql = update_post_information('topic', $topic_id, true);
1520 if (sizeof($update_sql))
1522 $sql_data[TOPICS_TABLE] .= ', ' . implode(', ', $update_sql[$topic_id]);
1523 $next_post_id = (int) str_replace('topic_last_post_id = ', '', $update_sql[$topic_id][0]);
1527 $sql = 'SELECT MAX(post_id) as last_post_id
1528 FROM ' . POSTS_TABLE . "
1529 WHERE topic_id = $topic_id " .
1530 ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '');
1531 $result = $db->sql_query($sql);
1532 $row = $db->sql_fetchrow($result);
1533 $db->sql_freeresult($result);
1535 $next_post_id = (int) $row['last_post_id'];
1540 $sql = 'SELECT post_id
1541 FROM ' . POSTS_TABLE . "
1542 WHERE topic_id = $topic_id " .
1543 ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '') . '
1544 AND post_time > ' . $data['post_time'] . '
1545 ORDER BY post_time ASC';
1546 $result = $db->sql_query_limit($sql, 1);
1547 $row = $db->sql_fetchrow($result);
1548 $db->sql_freeresult($result);
1550 if ($data['topic_type'] != POST_GLOBAL)
1552 $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
1555 $sql_data[TOPICS_TABLE] = 'topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
1556 $next_post_id = (int) $row['post_id'];
1560 if (($post_mode == 'delete') || ($post_mode == 'delete_last_post') || ($post_mode == 'delete_first_post'))
1562 $sql = 'SELECT 1 AS has_attachments
1563 FROM ' . ATTACHMENTS_TABLE . '
1564 WHERE topic_id = ' . $topic_id;
1565 $result = $db->sql_query_limit($sql, 1);
1566 $has_attachments = (int) $db->sql_fetchfield('has_attachments');
1567 $db->sql_freeresult($result);
1569 if (!$has_attachments)
1571 $sql_data[TOPICS_TABLE] .= ', topic_attachment = 0';
1575 // $sql_data[USERS_TABLE] = ($data['post_postcount']) ? 'user_posts = user_posts - 1' : '';
1577 $db->sql_transaction('begin');
1580 FORUMS_TABLE => "forum_id = $forum_id",
1581 TOPICS_TABLE => "topic_id = $topic_id",
1582 USERS_TABLE => 'user_id = ' . $data['poster_id']
1585 foreach ($sql_data as $table => $update_sql)
1589 $db->sql_query("UPDATE $table SET $update_sql WHERE " . $where_sql[$table]);
1593 // Adjust posted info for this user by looking for a post by him/her within this topic...
1594 if ($post_mode != 'delete_topic' && $config['load_db_track'] && $data['poster_id'] != ANONYMOUS)
1596 $sql = 'SELECT poster_id
1597 FROM ' . POSTS_TABLE . '
1598 WHERE topic_id = ' . $topic_id . '
1599 AND poster_id = ' . $data['poster_id'];
1600 $result = $db->sql_query_limit($sql, 1);
1601 $poster_id = (int) $db->sql_fetchfield('poster_id');
1602 $db->sql_freeresult($result);
1604 // The user is not having any more posts within this topic
1607 $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
1608 WHERE topic_id = ' . $topic_id . '
1609 AND user_id = ' . $data['poster_id'];
1610 $db->sql_query($sql);
1614 $db->sql_transaction('commit');
1616 if ($data['post_reported'] && ($post_mode != 'delete_topic'))
1618 sync('topic_reported', 'topic_id', array($topic_id));
1621 return $next_post_id;
1626 * @todo Split up and create lightweight, simple API for this.
1628 function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $update_message = true, $update_search_index = true)
1630 global $db, $auth, $user, $config, $phpEx, $template, $phpbb_root_path;
1632 // We do not handle erasing posts here
1633 if ($mode == 'delete')
1638 $current_time = time();
1640 if ($mode == 'post')
1642 $post_mode = 'post';
1643 $update_message = true;
1645 else if ($mode != 'edit')
1647 $post_mode = 'reply';
1648 $update_message = true;
1650 else if ($mode == 'edit')
1652 $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'));
1655 // First of all make sure the subject and topic title are having the correct length.
1656 // To achieve this without cutting off between special chars we convert to an array and then count the elements.
1657 $subject = truncate_string($subject);
1658 $data['topic_title'] = truncate_string($data['topic_title']);
1660 // Collect some basic information about which tables and which rows to update/insert
1661 $sql_data = $topic_row = array();
1662 $poster_id = ($mode == 'edit') ? $data['poster_id'] : (int) $user->data['user_id'];
1664 // Retrieve some additional information if not present
1665 if ($mode == 'edit' && (!isset($data['post_approved']) || !isset($data['topic_approved']) || $data['post_approved'] === false || $data['topic_approved'] === false))
1667 $sql = 'SELECT p.post_approved, t.topic_type, t.topic_replies, t.topic_replies_real, t.topic_approved
1668 FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
1669 WHERE t.topic_id = p.topic_id
1670 AND p.post_id = ' . $data['post_id'];
1671 $result = $db->sql_query($sql);
1672 $topic_row = $db->sql_fetchrow($result);
1673 $db->sql_freeresult($result);
1675 $data['topic_approved'] = $topic_row['topic_approved'];
1676 $data['post_approved'] = $topic_row['post_approved'];
1679 // 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
1680 // The variable name should be $post_approved, because it indicates if the post is approved or not
1683 // Check the permissions for post approval. Moderators are not affected.
1684 if (!$auth->acl_get('f_noapprove', $data['forum_id']) && !$auth->acl_get('m_approve', $data['forum_id']))
1686 // Post not approved, but in queue
1690 // Mods are able to force approved/unapproved posts. True means the post is approved, false the post is unapproved
1691 if (isset($data['force_approved_state']))
1693 $post_approval = ($data['force_approved_state']) ? 1 : 0;
1696 // Start the transaction here
1697 $db->sql_transaction('begin');
1699 // Collect Information
1704 $sql_data[POSTS_TABLE]['sql'] = array(
1705 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1706 'poster_id' => (int) $user->data['user_id'],
1707 'icon_id' => $data['icon_id'],
1708 'poster_ip' => $user->ip,
1709 'post_time' => $current_time,
1710 'post_approved' => $post_approval,
1711 'enable_bbcode' => $data['enable_bbcode'],
1712 'enable_smilies' => $data['enable_smilies'],
1713 'enable_magic_url' => $data['enable_urls'],
1714 'enable_sig' => $data['enable_sig'],
1715 'post_username' => (!$user->data['is_registered']) ? $username : '',
1716 'post_subject' => $subject,
1717 'post_text' => $data['message'],
1718 'post_checksum' => $data['message_md5'],
1719 'post_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
1720 'bbcode_bitfield' => $data['bbcode_bitfield'],
1721 'bbcode_uid' => $data['bbcode_uid'],
1722 'post_postcount' => ($auth->acl_get('f_postcount', $data['forum_id'])) ? 1 : 0,
1723 'post_edit_locked' => $data['post_edit_locked']
1727 case 'edit_first_post':
1730 case 'edit_last_post':
1733 // If edit reason is given always display edit info
1735 // If editing last post then display no edit info
1736 // If m_edit permission then display no edit info
1737 // If normal edit display edit info
1739 // Display edit info if edit reason given or user is editing his post, which is not the last within the topic.
1740 if ($data['post_edit_reason'] || (!$auth->acl_get('m_edit', $data['forum_id']) && ($post_mode == 'edit' || $post_mode == 'edit_first_post')))
1742 $data['post_edit_reason'] = truncate_string($data['post_edit_reason'], 255, 255, false);
1744 $sql_data[POSTS_TABLE]['sql'] = array(
1745 'post_edit_time' => $current_time,
1746 'post_edit_reason' => $data['post_edit_reason'],
1747 'post_edit_user' => (int) $data['post_edit_user'],
1750 $sql_data[POSTS_TABLE]['stat'][] = 'post_edit_count = post_edit_count + 1';
1752 else if (!$data['post_edit_reason'] && $mode == 'edit' && $auth->acl_get('m_edit', $data['forum_id']))
1754 $sql_data[POSTS_TABLE]['sql'] = array(
1755 'post_edit_reason' => '',
1759 // If the person editing this post is different to the one having posted then we will add a log entry stating the edit
1760 // Could be simplified by only adding to the log if the edit is not tracked - but this may confuse admins/mods
1761 if ($user->data['user_id'] != $poster_id)
1763 $log_subject = ($subject) ? $subject : $data['topic_title'];
1764 add_log('mod', $data['forum_id'], $data['topic_id'], 'LOG_POST_EDITED', $log_subject, (!empty($username)) ? $username : $user->lang['GUEST']);
1767 if (!isset($sql_data[POSTS_TABLE]['sql']))
1769 $sql_data[POSTS_TABLE]['sql'] = array();
1772 $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1773 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1774 'poster_id' => $data['poster_id'],
1775 'icon_id' => $data['icon_id'],
1776 'post_approved' => (!$post_approval) ? 0 : $data['post_approved'],
1777 'enable_bbcode' => $data['enable_bbcode'],
1778 'enable_smilies' => $data['enable_smilies'],
1779 'enable_magic_url' => $data['enable_urls'],
1780 'enable_sig' => $data['enable_sig'],
1781 'post_username' => ($username && $data['poster_id'] == ANONYMOUS) ? $username : '',
1782 'post_subject' => $subject,
1783 'post_checksum' => $data['message_md5'],
1784 'post_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
1785 'bbcode_bitfield' => $data['bbcode_bitfield'],
1786 'bbcode_uid' => $data['bbcode_uid'],
1787 'post_edit_locked' => $data['post_edit_locked'])
1790 if ($update_message)
1792 $sql_data[POSTS_TABLE]['sql']['post_text'] = $data['message'];
1798 $post_approved = $sql_data[POSTS_TABLE]['sql']['post_approved'];
1799 $topic_row = array();
1801 // And the topic ladies and gentlemen
1805 $sql_data[TOPICS_TABLE]['sql'] = array(
1806 'topic_poster' => (int) $user->data['user_id'],
1807 'topic_time' => $current_time,
1808 'topic_last_view_time' => $current_time,
1809 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1810 'icon_id' => $data['icon_id'],
1811 'topic_approved' => $post_approval,
1812 'topic_title' => $subject,
1813 'topic_first_poster_name' => (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
1814 'topic_first_poster_colour' => $user->data['user_colour'],
1815 'topic_type' => $topic_type,
1816 'topic_time_limit' => ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
1817 'topic_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
1820 if (isset($poll['poll_options']) && !empty($poll['poll_options']))
1822 $poll_start = ($poll['poll_start']) ? $poll['poll_start'] : $current_time;
1823 $poll_length = $poll['poll_length'] * 86400;
1824 if ($poll_length < 0)
1826 $poll_start = $poll_start + $poll_length;
1827 if ($poll_start < 0)
1834 $sql_data[TOPICS_TABLE]['sql'] = array_merge($sql_data[TOPICS_TABLE]['sql'], array(
1835 'poll_title' => $poll['poll_title'],
1836 'poll_start' => $poll_start,
1837 'poll_max_options' => $poll['poll_max_options'],
1838 'poll_length' => $poll_length,
1839 'poll_vote_change' => $poll['poll_vote_change'])
1843 $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' : '');
1845 if ($topic_type != POST_GLOBAL)
1849 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
1851 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($post_approval) ? ', forum_topics = forum_topics + 1' : '');
1856 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_view_time = ' . $current_time . ',
1857 topic_replies_real = topic_replies_real + 1,
1860 (($post_approval) ? ', topic_replies = topic_replies + 1' : '') .
1861 ((!empty($data['attachment_data']) || (isset($data['topic_attachment']) && $data['topic_attachment'])) ? ', topic_attachment = 1' : '');
1863 $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' : '');
1865 if ($post_approval && $topic_type != POST_GLOBAL)
1867 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
1872 case 'edit_first_post':
1873 if (isset($poll['poll_options']))
1875 $poll_start = ($poll['poll_start'] || empty($poll['poll_options'])) ? $poll['poll_start'] : $current_time;
1876 $poll_length = $poll['poll_length'] * 86400;
1877 if ($poll_length < 0)
1879 $poll_start = $poll_start + $poll_length;
1880 if ($poll_start < 0)
1888 $sql_data[TOPICS_TABLE]['sql'] = array(
1889 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1890 'icon_id' => $data['icon_id'],
1891 'topic_approved' => (!$post_approval) ? 0 : $data['topic_approved'],
1892 'topic_title' => $subject,
1893 'topic_first_poster_name' => $username,
1894 'topic_type' => $topic_type,
1895 'topic_time_limit' => ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
1896 'poll_title' => (isset($poll['poll_options'])) ? $poll['poll_title'] : '',
1897 'poll_start' => (isset($poll['poll_options'])) ? $poll_start : 0,
1898 'poll_max_options' => (isset($poll['poll_options'])) ? $poll['poll_max_options'] : 1,
1899 'poll_length' => (isset($poll['poll_options'])) ? $poll_length : 0,
1900 'poll_vote_change' => (isset($poll['poll_vote_change'])) ? $poll['poll_vote_change'] : 0,
1901 'topic_last_view_time' => $current_time,
1903 'topic_attachment' => (!empty($data['attachment_data'])) ? 1 : (isset($data['topic_attachment']) ? $data['topic_attachment'] : 0),
1906 // Correctly set back the topic replies and forum posts... only if the topic was approved before and now gets disapproved
1907 if (!$post_approval && $data['topic_approved'])
1909 // Do we need to grab some topic informations?
1910 if (!sizeof($topic_row))
1912 $sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_approved
1913 FROM ' . TOPICS_TABLE . '
1914 WHERE topic_id = ' . $data['topic_id'];
1915 $result = $db->sql_query($sql);
1916 $topic_row = $db->sql_fetchrow($result);
1917 $db->sql_freeresult($result);
1920 // If this is the only post remaining we do not need to decrement topic_replies.
1921 // Also do not decrement if first post - then the topic_replies will not be adjusted if approving the topic again.
1923 // If this is an edited topic or the first post the topic gets completely disapproved later on...
1924 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics = forum_topics - 1';
1925 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - ' . ($topic_row['topic_replies'] + 1);
1927 set_config_count('num_topics', -1, true);
1928 set_config_count('num_posts', ($topic_row['topic_replies'] + 1) * (-1), true);
1930 // Only decrement this post, since this is the one non-approved now
1931 if ($auth->acl_get('f_postcount', $data['forum_id']))
1933 $sql_data[USERS_TABLE]['stat'][] = 'user_posts = user_posts - 1';
1940 case 'edit_last_post':
1942 // Correctly set back the topic replies and forum posts... but only if the post was approved before.
1943 if (!$post_approval && $data['post_approved'])
1945 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_replies = topic_replies - 1, topic_last_view_time = ' . $current_time;
1946 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - 1';
1948 set_config_count('num_posts', -1, true);
1950 if ($auth->acl_get('f_postcount', $data['forum_id']))
1952 $sql_data[USERS_TABLE]['stat'][] = 'user_posts = user_posts - 1';
1960 if ($post_mode == 'post')
1962 $sql = 'INSERT INTO ' . TOPICS_TABLE . ' ' .
1963 $db->sql_build_array('INSERT', $sql_data[TOPICS_TABLE]['sql']);
1964 $db->sql_query($sql);
1966 $data['topic_id'] = $db->sql_nextid();
1968 $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1969 'topic_id' => $data['topic_id'])
1971 unset($sql_data[TOPICS_TABLE]['sql']);
1975 if ($post_mode == 'post' || $post_mode == 'reply')
1977 if ($post_mode == 'reply')
1979 $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1980 'topic_id' => $data['topic_id'])
1984 $sql = 'INSERT INTO ' . POSTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_data[POSTS_TABLE]['sql']);
1985 $db->sql_query($sql);
1986 $data['post_id'] = $db->sql_nextid();
1988 if ($post_mode == 'post')
1990 $sql_data[TOPICS_TABLE]['sql'] = array(
1991 'topic_first_post_id' => $data['post_id'],
1992 'topic_last_post_id' => $data['post_id'],
1993 'topic_last_post_time' => $current_time,
1994 'topic_last_poster_id' => (int) $user->data['user_id'],
1995 'topic_last_poster_name' => (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
1996 'topic_last_poster_colour' => $user->data['user_colour'],
1997 'topic_last_post_subject' => (string) $subject,
2001 unset($sql_data[POSTS_TABLE]['sql']);
2004 $make_global = false;
2006 // Are we globalising or unglobalising?
2007 if ($post_mode == 'edit_first_post' || $post_mode == 'edit_topic')
2009 if (!sizeof($topic_row))
2011 $sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_approved, topic_last_post_id
2012 FROM ' . TOPICS_TABLE . '
2013 WHERE topic_id = ' . $data['topic_id'];
2014 $result = $db->sql_query($sql);
2015 $topic_row = $db->sql_fetchrow($result);
2016 $db->sql_freeresult($result);
2019 // globalise/unglobalise?
2020 if (($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL) || ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL))
2022 if (!empty($sql_data[FORUMS_TABLE]['stat']) && implode('', $sql_data[FORUMS_TABLE]['stat']))
2024 $db->sql_query('UPDATE ' . FORUMS_TABLE . ' SET ' . implode(', ', $sql_data[FORUMS_TABLE]['stat']) . ' WHERE forum_id = ' . $data['forum_id']);
2027 $make_global = true;
2028 $sql_data[FORUMS_TABLE]['stat'] = array();
2032 if ($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL)
2034 // Decrement topic/post count
2035 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - ' . ($topic_row['topic_replies_real'] + 1);
2036 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real - 1' . (($topic_row['topic_approved']) ? ', forum_topics = forum_topics - 1' : '');
2038 // Update forum_ids for all posts
2039 $sql = 'UPDATE ' . POSTS_TABLE . '
2041 WHERE topic_id = ' . $data['topic_id'];
2042 $db->sql_query($sql);
2045 else if ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL)
2047 // Increment topic/post count
2048 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + ' . ($topic_row['topic_replies_real'] + 1);
2049 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($topic_row['topic_approved']) ? ', forum_topics = forum_topics + 1' : '');
2051 // Update forum_ids for all posts
2052 $sql = 'UPDATE ' . POSTS_TABLE . '
2053 SET forum_id = ' . $data['forum_id'] . '
2054 WHERE topic_id = ' . $data['topic_id'];
2055 $db->sql_query($sql);
2059 // Update the topics table
2060 if (isset($sql_data[TOPICS_TABLE]['sql']))
2062 $sql = 'UPDATE ' . TOPICS_TABLE . '
2063 SET ' . $db->sql_build_array('UPDATE', $sql_data[TOPICS_TABLE]['sql']) . '
2064 WHERE topic_id = ' . $data['topic_id'];
2065 $db->sql_query($sql);
2068 // Update the posts table
2069 if (isset($sql_data[POSTS_TABLE]['sql']))
2071 $sql = 'UPDATE ' . POSTS_TABLE . '
2072 SET ' . $db->sql_build_array('UPDATE', $sql_data[POSTS_TABLE]['sql']) . '
2073 WHERE post_id = ' . $data['post_id'];
2074 $db->sql_query($sql);
2077 // Update Poll Tables
2078 if (isset($poll['poll_options']))
2080 $cur_poll_options = array();
2082 if ($mode == 'edit')
2085 FROM ' . POLL_OPTIONS_TABLE . '
2086 WHERE topic_id = ' . $data['topic_id'] . '
2087 ORDER BY poll_option_id';
2088 $result = $db->sql_query($sql);
2090 $cur_poll_options = array();
2091 while ($row = $db->sql_fetchrow($result))
2093 $cur_poll_options[] = $row;
2095 $db->sql_freeresult($result);
2098 $sql_insert_ary = array();
2100 for ($i = 0, $size = sizeof($poll['poll_options']); $i < $size; $i++)
2102 if (strlen(trim($poll['poll_options'][$i])))
2104 if (empty($cur_poll_options[$i]))
2106 // If we add options we need to put them to the end to be able to preserve votes...
2107 $sql_insert_ary[] = array(
2108 'poll_option_id' => (int) sizeof($cur_poll_options) + 1 + sizeof($sql_insert_ary),
2109 'topic_id' => (int) $data['topic_id'],
2110 'poll_option_text' => (string) $poll['poll_options'][$i]
2113 else if ($poll['poll_options'][$i] != $cur_poll_options[$i])
2115 $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . "
2116 SET poll_option_text = '" . $db->sql_escape($poll['poll_options'][$i]) . "'
2117 WHERE poll_option_id = " . $cur_poll_options[$i]['poll_option_id'] . '
2118 AND topic_id = ' . $data['topic_id'];
2119 $db->sql_query($sql);
2124 $db->sql_multi_insert(POLL_OPTIONS_TABLE, $sql_insert_ary);
2126 if (sizeof($poll['poll_options']) < sizeof($cur_poll_options))
2128 $sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . '
2129 WHERE poll_option_id > ' . sizeof($poll['poll_options']) . '
2130 AND topic_id = ' . $data['topic_id'];
2131 $db->sql_query($sql);
2134 // 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
2135 if ($mode == 'edit' && sizeof($poll['poll_options']) != sizeof($cur_poll_options))
2137 $db->sql_query('DELETE FROM ' . POLL_VOTES_TABLE . ' WHERE topic_id = ' . $data['topic_id']);
2138 $db->sql_query('UPDATE ' . POLL_OPTIONS_TABLE . ' SET poll_option_total = 0 WHERE topic_id = ' . $data['topic_id']);
2142 // Submit Attachments
2143 if (!empty($data['attachment_data']) && $data['post_id'] && in_array($mode, array('post', 'reply', 'quote', 'edit')))
2145 $space_taken = $files_added = 0;
2146 $orphan_rows = array();
2148 foreach ($data['attachment_data'] as $pos => $attach_row)
2150 $orphan_rows[(int) $attach_row['attach_id']] = array();
2153 if (sizeof($orphan_rows))
2155 $sql = 'SELECT attach_id, filesize, physical_filename
2156 FROM ' . ATTACHMENTS_TABLE . '
2157 WHERE ' . $db->sql_in_set('attach_id', array_keys($orphan_rows)) . '
2159 AND poster_id = ' . $user->data['user_id'];
2160 $result = $db->sql_query($sql);
2162 $orphan_rows = array();
2163 while ($row = $db->sql_fetchrow($result))
2165 $orphan_rows[$row['attach_id']] = $row;
2167 $db->sql_freeresult($result);
2170 foreach ($data['attachment_data'] as $pos => $attach_row)
2172 if ($attach_row['is_orphan'] && !isset($orphan_rows[$attach_row['attach_id']]))
2177 if (!$attach_row['is_orphan'])
2179 // update entry in db if attachment already stored in db and filespace
2180 $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
2181 SET attach_comment = '" . $db->sql_escape($attach_row['attach_comment']) . "'
2182 WHERE attach_id = " . (int) $attach_row['attach_id'] . '
2184 $db->sql_query($sql);
2188 // insert attachment into db
2189 if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . utf8_basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
2194 $space_taken += $orphan_rows[$attach_row['attach_id']]['filesize'];
2197 $attach_sql = array(
2198 'post_msg_id' => $data['post_id'],
2199 'topic_id' => $data['topic_id'],
2201 'poster_id' => $poster_id,
2202 'attach_comment' => $attach_row['attach_comment'],
2205 $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $attach_sql) . '
2206 WHERE attach_id = ' . $attach_row['attach_id'] . '
2208 AND poster_id = ' . $user->data['user_id'];
2209 $db->sql_query($sql);
2213 if ($space_taken && $files_added)
2215 set_config_count('upload_dir_size', $space_taken, true);
2216 set_config_count('num_files', $files_added, true);
2220 // we need to update the last forum information
2221 // only applicable if the topic is not global and it is approved
2222 // we also check to make sure we are not dealing with globaling the latest topic (pretty rare but still needs to be checked)
2223 if ($topic_type != POST_GLOBAL && !$make_global && ($post_approved || !$data['post_approved']))
2225 // the last post makes us update the forum table. This can happen if...
2226 // We make a new topic
2227 // We reply to a topic
2228 // We edit the last post in a topic and this post is the latest in the forum (maybe)
2229 // We edit the only post in the topic
2230 // We edit the first post in the topic and all the other posts are not approved
2231 if (($post_mode == 'post' || $post_mode == 'reply') && $post_approved)
2233 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . $data['post_id'];
2234 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($subject) . "'";
2235 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . $current_time;
2236 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $user->data['user_id'];
2237 $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'] : '')) . "'";
2238 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($user->data['user_colour']) . "'";
2240 else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies']))
2242 // this does not _necessarily_ mean that we must update the info again,
2243 // it just means that we might have to
2244 $sql = 'SELECT forum_last_post_id, forum_last_post_subject
2245 FROM ' . FORUMS_TABLE . '
2246 WHERE forum_id = ' . (int) $data['forum_id'];
2247 $result = $db->sql_query($sql);
2248 $row = $db->sql_fetchrow($result);
2249 $db->sql_freeresult($result);
2251 // this post is the latest post in the forum, better update
2252 if ($row['forum_last_post_id'] == $data['post_id'])
2254 // If post approved and subject changed, or poster is anonymous, we need to update the forum_last* rows
2255 if ($post_approved && ($row['forum_last_post_subject'] !== $subject || $data['poster_id'] == ANONYMOUS))
2257 // the post's subject changed
2258 if ($row['forum_last_post_subject'] !== $subject)
2260 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_subject = \'' . $db->sql_escape($subject) . '\'';
2263 // Update the user name if poster is anonymous... just in case an admin changed it
2264 if ($data['poster_id'] == ANONYMOUS)
2266 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape($username) . "'";
2269 else if ($data['post_approved'] !== $post_approved)
2271 // we need a fresh change of socks, everything has become invalidated
2272 $sql = 'SELECT MAX(topic_last_post_id) as last_post_id
2273 FROM ' . TOPICS_TABLE . '
2274 WHERE forum_id = ' . (int) $data['forum_id'] . '
2275 AND topic_approved = 1';
2276 $result = $db->sql_query($sql);
2277 $row = $db->sql_fetchrow($result);
2278 $db->sql_freeresult($result);
2280 // any posts left in this forum?
2281 if (!empty($row['last_post_id']))
2283 $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
2284 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2285 WHERE p.poster_id = u.user_id
2286 AND p.post_id = ' . (int) $row['last_post_id'];
2287 $result = $db->sql_query($sql);
2288 $row = $db->sql_fetchrow($result);
2289 $db->sql_freeresult($result);
2291 // salvation, a post is found! jam it into the forums table
2292 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
2293 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
2294 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
2295 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
2296 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
2297 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
2301 // just our luck, the last topic in the forum has just been turned unapproved...
2302 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = 0';
2303 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = ''";
2304 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = 0';
2305 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = 0';
2306 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = ''";
2307 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = ''";
2313 else if ($make_global)
2315 // somebody decided to be a party pooper, we must recalculate the whole shebang (maybe)
2316 $sql = 'SELECT forum_last_post_id
2317 FROM ' . FORUMS_TABLE . '
2318 WHERE forum_id = ' . (int) $data['forum_id'];
2319 $result = $db->sql_query($sql);
2320 $forum_row = $db->sql_fetchrow($result);
2321 $db->sql_freeresult($result);
2323 // we made a topic global, go get new data
2324 if ($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL && $forum_row['forum_last_post_id'] == $topic_row['topic_last_post_id'])
2326 // we need a fresh change of socks, everything has become invalidated
2327 $sql = 'SELECT MAX(topic_last_post_id) as last_post_id
2328 FROM ' . TOPICS_TABLE . '
2329 WHERE forum_id = ' . (int) $data['forum_id'] . '
2330 AND topic_approved = 1';
2331 $result = $db->sql_query($sql);
2332 $row = $db->sql_fetchrow($result);
2333 $db->sql_freeresult($result);
2335 // any posts left in this forum?
2336 if (!empty($row['last_post_id']))
2338 $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
2339 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2340 WHERE p.poster_id = u.user_id
2341 AND p.post_id = ' . (int) $row['last_post_id'];
2342 $result = $db->sql_query($sql);
2343 $row = $db->sql_fetchrow($result);
2344 $db->sql_freeresult($result);
2346 // salvation, a post is found! jam it into the forums table
2347 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
2348 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
2349 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
2350 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
2351 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
2352 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
2356 // just our luck, the last topic in the forum has just been globalized...
2357 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = 0';
2358 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = ''";
2359 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = 0';
2360 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = 0';
2361 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = ''";
2362 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = ''";
2365 else if ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL && $forum_row['forum_last_post_id'] < $topic_row['topic_last_post_id'])
2367 // this post has a higher id, it is newer
2368 $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
2369 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2370 WHERE p.poster_id = u.user_id
2371 AND p.post_id = ' . (int) $topic_row['topic_last_post_id'];
2372 $result = $db->sql_query($sql);
2373 $row = $db->sql_fetchrow($result);
2374 $db->sql_freeresult($result);
2376 // salvation, a post is found! jam it into the forums table
2377 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
2378 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
2379 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
2380 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
2381 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
2382 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
2387 // simply, we update if it is a reply or the last post is edited
2390 // reply requires the whole thing
2391 if ($post_mode == 'reply')
2393 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_id = ' . (int) $data['post_id'];
2394 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_poster_id = ' . (int) $user->data['user_id'];
2395 $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'] : '')) . "'";
2396 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_colour = '" . (($user->data['user_id'] != ANONYMOUS) ? $db->sql_escape($user->data['user_colour']) : '') . "'";
2397 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
2398 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_time = ' . (int) $current_time;
2400 else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies']))
2402 // only the subject can be changed from edit
2403 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
2405 // Maybe not only the subject, but also changing anonymous usernames. ;)
2406 if ($data['poster_id'] == ANONYMOUS)
2408 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape($username) . "'";
2412 else if (!$data['post_approved'] && ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies'])))
2414 // like having the rug pulled from under us
2415 $sql = 'SELECT MAX(post_id) as last_post_id
2416 FROM ' . POSTS_TABLE . '
2417 WHERE topic_id = ' . (int) $data['topic_id'] . '
2418 AND post_approved = 1';
2419 $result = $db->sql_query($sql);
2420 $row = $db->sql_fetchrow($result);
2421 $db->sql_freeresult($result);
2423 // any posts left in this forum?
2424 if (!empty($row['last_post_id']))
2426 $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
2427 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2428 WHERE p.poster_id = u.user_id
2429 AND p.post_id = ' . (int) $row['last_post_id'];
2430 $result = $db->sql_query($sql);
2431 $row = $db->sql_fetchrow($result);
2432 $db->sql_freeresult($result);
2434 // salvation, a post is found! jam it into the topics table
2435 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_id = ' . (int) $row['post_id'];
2436 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
2437 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_time = ' . (int) $row['post_time'];
2438 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_poster_id = ' . (int) $row['poster_id'];
2439 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
2440 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
2444 // Update total post count, do not consider moderated posts/topics
2447 if ($post_mode == 'post')
2449 set_config_count('num_topics', 1, true);
2450 set_config_count('num_posts', 1, true);
2453 if ($post_mode == 'reply')
2455 set_config_count('num_posts', 1, true);
2459 // Update forum stats
2460 $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);
2462 foreach ($sql_data as $table => $update_ary)
2464 if (isset($update_ary['stat']) && implode('', $update_ary['stat']))
2466 $sql = "UPDATE $table SET " . implode(', ', $update_ary['stat']) . ' WHERE ' . $where_sql[$table];
2467 $db->sql_query($sql);
2471 // Delete topic shadows (if any exist). We do not need a shadow topic for an global announcement
2474 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
2475 WHERE topic_moved_id = ' . $data['topic_id'];
2476 $db->sql_query($sql);
2479 // Committing the transaction before updating search index
2480 $db->sql_transaction('commit');
2482 // Delete draft if post was loaded...
2483 $draft_id = request_var('draft_loaded', 0);
2486 $sql = 'DELETE FROM ' . DRAFTS_TABLE . "
2487 WHERE draft_id = $draft_id
2488 AND user_id = {$user->data['user_id']}";
2489 $db->sql_query($sql);
2492 // Index message contents
2493 if ($update_search_index && $data['enable_indexing'])
2495 // Select the search method and do some additional checks to ensure it can actually be utilised
2496 $search_type = basename($config['search_type']);
2498 if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
2500 trigger_error('NO_SUCH_SEARCH_MODULE');
2503 if (!class_exists($search_type))
2505 include("{$phpbb_root_path}includes/search/$search_type.$phpEx");
2509 $search = new $search_type($error);
2513 trigger_error($error);
2516 $search->index($mode, $data['post_id'], $data['message'], $subject, $poster_id, ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']);
2519 // Topic Notification, do not change if moderator is changing other users posts...
2520 if ($user->data['user_id'] == $poster_id)
2522 if (!$data['notify_set'] && $data['notify'])
2524 $sql = 'INSERT INTO ' . TOPICS_WATCH_TABLE . ' (user_id, topic_id)
2525 VALUES (' . $user->data['user_id'] . ', ' . $data['topic_id'] . ')';
2526 $db->sql_query($sql);
2528 else if (($config['email_enable'] || $config['jab_enable']) && $data['notify_set'] && !$data['notify'])
2530 $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . '
2531 WHERE user_id = ' . $user->data['user_id'] . '
2532 AND topic_id = ' . $data['topic_id'];
2533 $db->sql_query($sql);
2537 if ($mode == 'post' || $mode == 'reply' || $mode == 'quote')
2539 // Mark this topic as posted to
2540 markread('post', $data['forum_id'], $data['topic_id']);
2543 // Mark this topic as read
2544 // We do not use post_time here, this is intended (post_time can have a date in the past if editing a message)
2545 markread('topic', (($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']), $data['topic_id'], time());
2548 if ($config['load_db_lastread'] && $user->data['is_registered'])
2550 $sql = 'SELECT mark_time
2551 FROM ' . FORUMS_TRACK_TABLE . '
2552 WHERE user_id = ' . $user->data['user_id'] . '
2553 AND forum_id = ' . (($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']);
2554 $result = $db->sql_query($sql);
2555 $f_mark_time = (int) $db->sql_fetchfield('mark_time');
2556 $db->sql_freeresult($result);
2558 else if ($config['load_anon_lastread'] || $user->data['is_registered'])
2560 $f_mark_time = false;
2563 if (($config['load_db_lastread'] && $user->data['is_registered']) || $config['load_anon_lastread'] || $user->data['is_registered'])
2565 // Update forum info
2566 if ($topic_type == POST_GLOBAL)
2568 $sql = 'SELECT MAX(topic_last_post_time) as forum_last_post_time
2569 FROM ' . TOPICS_TABLE . '
2570 WHERE forum_id = 0';
2574 $sql = 'SELECT forum_last_post_time
2575 FROM ' . FORUMS_TABLE . '
2576 WHERE forum_id = ' . $data['forum_id'];
2578 $result = $db->sql_query($sql);
2579 $forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
2580 $db->sql_freeresult($result);
2582 update_forum_tracking_info((($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']), $forum_last_post_time, $f_mark_time, false);
2585 // Send Notifications
2586 if (($mode == 'reply' || $mode == 'quote' || $mode == 'post') && $post_approval)
2588 user_notification($mode, $subject, $data['topic_title'], $data['forum_name'], $data['forum_id'], $data['topic_id'], $data['post_id']);
2591 $params = $add_anchor = '';
2595 $params .= '&t=' . $data['topic_id'];
2597 if ($mode != 'post')
2599 $params .= '&p=' . $data['post_id'];
2600 $add_anchor = '#p' . $data['post_id'];
2603 else if ($mode != 'post' && $post_mode != 'edit_first_post' && $post_mode != 'edit_topic')
2605 $params .= '&t=' . $data['topic_id'];
2608 $url = (!$params) ? "{$phpbb_root_path}viewforum.$phpEx" : "{$phpbb_root_path}viewtopic.$phpEx";
2609 $url = append_sid($url, 'f=' . $data['forum_id'] . $params) . $add_anchor;
2615 * Handle topic bumping
2616 * @param int $forum_id The ID of the forum the topic is being bumped belongs to
2617 * @param int $topic_id The ID of the topic is being bumping
2618 * @param array $post_data Passes some topic parameters:
2620 * - 'topic_last_post_id'
2621 * - 'topic_last_poster_id'
2622 * - 'topic_last_post_subject'
2623 * - 'topic_last_poster_name'
2624 * - 'topic_last_poster_colour'
2625 * @param int $bump_time The time at which topic was bumped, usually it is a current time as obtained via time().
2626 * @return string An URL to the bumped topic, example: ./viewtopic.php?forum_id=1&topic_id=2&p=3#p3
2628 function phpbb_bump_topic($forum_id, $topic_id, $post_data, $bump_time = false)
2630 global $config, $db, $user, $phpEx, $phpbb_root_path;
2632 if ($bump_time === false)
2634 $bump_time = time();
2638 $db->sql_transaction('begin');
2640 // Update the topic's last post post_time
2641 $sql = 'UPDATE ' . POSTS_TABLE . "
2642 SET post_time = $bump_time
2643 WHERE post_id = {$post_data['topic_last_post_id']}
2644 AND topic_id = $topic_id";
2645 $db->sql_query($sql);
2647 // Sync the topic's last post time, the rest of the topic's last post data isn't changed
2648 $sql = 'UPDATE ' . TOPICS_TABLE . "
2649 SET topic_last_post_time = $bump_time,
2651 topic_bumper = " . $user->data['user_id'] . "
2652 WHERE topic_id = $topic_id";
2653 $db->sql_query($sql);
2655 // Update the forum's last post info
2656 $sql = 'UPDATE ' . FORUMS_TABLE . "
2657 SET forum_last_post_id = " . $post_data['topic_last_post_id'] . ",
2658 forum_last_poster_id = " . $post_data['topic_last_poster_id'] . ",
2659 forum_last_post_subject = '" . $db->sql_escape($post_data['topic_last_post_subject']) . "',
2660 forum_last_post_time = $bump_time,
2661 forum_last_poster_name = '" . $db->sql_escape($post_data['topic_last_poster_name']) . "',
2662 forum_last_poster_colour = '" . $db->sql_escape($post_data['topic_last_poster_colour']) . "'
2663 WHERE forum_id = $forum_id";
2664 $db->sql_query($sql);
2666 // Update bumper's time of the last posting to prevent flood
2667 $sql = 'UPDATE ' . USERS_TABLE . "
2668 SET user_lastpost_time = $bump_time
2669 WHERE user_id = " . $user->data['user_id'];
2670 $db->sql_query($sql);
2672 $db->sql_transaction('commit');
2674 // Mark this topic as posted to
2675 markread('post', $forum_id, $topic_id, $bump_time);
2677 // Mark this topic as read
2678 markread('topic', $forum_id, $topic_id, $bump_time);
2680 // Update forum tracking info
2681 if ($config['load_db_lastread'] && $user->data['is_registered'])
2683 $sql = 'SELECT mark_time
2684 FROM ' . FORUMS_TRACK_TABLE . '
2685 WHERE user_id = ' . $user->data['user_id'] . '
2686 AND forum_id = ' . $forum_id;
2687 $result = $db->sql_query($sql);
2688 $f_mark_time = (int) $db->sql_fetchfield('mark_time');
2689 $db->sql_freeresult($result);
2691 else if ($config['load_anon_lastread'] || $user->data['is_registered'])
2693 $f_mark_time = false;
2696 if (($config['load_db_lastread'] && $user->data['is_registered']) || $config['load_anon_lastread'] || $user->data['is_registered'])
2698 // Update forum info
2699 $sql = 'SELECT forum_last_post_time
2700 FROM ' . FORUMS_TABLE . '
2701 WHERE forum_id = ' . $forum_id;
2702 $result = $db->sql_query($sql);
2703 $forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
2704 $db->sql_freeresult($result);
2706 update_forum_tracking_info($forum_id, $forum_last_post_time, $f_mark_time, false);
2709 add_log('mod', $forum_id, $topic_id, 'LOG_BUMP_TOPIC', $post_data['topic_title']);
2711 $url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&t=$topic_id&p={$post_data['topic_last_post_id']}") . "#p{$post_data['topic_last_post_id']}";