$config to phpbb::$config
[phpbb.git] / phpBB / includes / functions_posting.php
blob29d6e3ea7c5600ec42130bd1eb18a0265def9b8e
1 <?php
2 /**
4 * @package phpBB3
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
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, $template;
26 if ($mode == 'window')
28 if ($forum_id)
30 $sql = 'SELECT forum_style
31 FROM ' . FORUMS_TABLE . "
32 WHERE forum_id = $forum_id";
33 $result = $db->sql_query_limit($sql, 1);
34 $row = $db->sql_fetchrow($result);
35 $db->sql_freeresult($result);
37 $user->setup('posting', (int) $row['forum_style']);
39 else
41 $user->setup('posting');
44 page_header($user->lang['SMILIES']);
46 $template->set_filenames(array(
47 'body' => 'posting_smilies.html')
51 $display_link = false;
52 if ($mode == 'inline')
54 $sql = 'SELECT smiley_id
55 FROM ' . SMILIES_TABLE . '
56 WHERE display_on_posting = 0';
57 $result = $db->sql_query_limit($sql, 1, 0, 3600);
59 if ($row = $db->sql_fetchrow($result))
61 $display_link = true;
63 $db->sql_freeresult($result);
66 $last_url = '';
68 $sql = 'SELECT *
69 FROM ' . SMILIES_TABLE .
70 (($mode == 'inline') ? ' WHERE display_on_posting = 1 ' : '') . '
71 ORDER BY smiley_order';
72 $result = $db->sql_query($sql, 3600);
74 $smilies = array();
75 while ($row = $db->sql_fetchrow($result))
77 if (empty($smilies[$row['smiley_url']]))
79 $smilies[$row['smiley_url']] = $row;
82 $db->sql_freeresult($result);
84 if (sizeof($smilies))
86 foreach ($smilies as $row)
88 $template->assign_block_vars('smiley', array(
89 'SMILEY_CODE' => $row['code'],
90 'A_SMILEY_CODE' => addslashes($row['code']),
91 'SMILEY_IMG' => PHPBB_ROOT_PATH . phpbb::$config['smilies_path'] . '/' . $row['smiley_url'],
92 'SMILEY_WIDTH' => $row['smiley_width'],
93 'SMILEY_HEIGHT' => $row['smiley_height'],
94 'SMILEY_DESC' => $row['emotion'])
99 if ($mode == 'inline' && $display_link)
101 $template->assign_vars(array(
102 'S_SHOW_SMILEY_LINK' => true,
103 'U_MORE_SMILIES' => append_sid('posting', 'mode=smilies&amp;f=' . $forum_id))
107 if ($mode == 'window')
109 page_footer();
114 * Update last post information
115 * Should be used instead of sync() if only the last post information are out of sync... faster
117 * @param string $type Can be forum|topic
118 * @param mixed $ids topic/forum ids
119 * @param bool $return_update_sql true: SQL query shall be returned, false: execute SQL
121 function update_post_information($type, $ids, $return_update_sql = false)
123 global $db;
125 if (empty($ids))
127 return;
129 if (!is_array($ids))
131 $ids = array($ids);
135 $update_sql = $empty_forums = $not_empty_forums = array();
137 if ($type != 'topic')
139 $topic_join = ', ' . TOPICS_TABLE . ' t';
140 $topic_condition = 'AND t.topic_id = p.topic_id AND t.topic_approved = 1';
142 else
144 $topic_join = '';
145 $topic_condition = '';
148 if (sizeof($ids) == 1)
150 $sql = 'SELECT MAX(p.post_id) as last_post_id
151 FROM ' . POSTS_TABLE . " p $topic_join
152 WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
153 $topic_condition
154 AND p.post_approved = 1";
156 else
158 $sql = 'SELECT p.' . $type . '_id, MAX(p.post_id) as last_post_id
159 FROM ' . POSTS_TABLE . " p $topic_join
160 WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
161 $topic_condition
162 AND p.post_approved = 1
163 GROUP BY p.{$type}_id";
165 $result = $db->sql_query($sql);
167 $last_post_ids = array();
168 while ($row = $db->sql_fetchrow($result))
170 if (sizeof($ids) == 1)
172 $row[$type . '_id'] = $ids[0];
175 if ($type == 'forum')
177 $not_empty_forums[] = $row['forum_id'];
179 if (empty($row['last_post_id']))
181 $empty_forums[] = $row['forum_id'];
185 $last_post_ids[] = $row['last_post_id'];
187 $db->sql_freeresult($result);
189 if ($type == 'forum')
191 $empty_forums = array_merge($empty_forums, array_diff($ids, $not_empty_forums));
193 foreach ($empty_forums as $void => $forum_id)
195 $update_sql[$forum_id][] = 'forum_last_post_id = 0';
196 $update_sql[$forum_id][] = "forum_last_post_subject = ''";
197 $update_sql[$forum_id][] = 'forum_last_post_time = 0';
198 $update_sql[$forum_id][] = 'forum_last_poster_id = 0';
199 $update_sql[$forum_id][] = "forum_last_poster_name = ''";
200 $update_sql[$forum_id][] = "forum_last_poster_colour = ''";
204 if (sizeof($last_post_ids))
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);
212 while ($row = $db->sql_fetchrow($result))
214 $update_sql[$row["{$type}_id"]][] = $type . '_last_post_id = ' . (int) $row['post_id'];
215 $update_sql[$row["{$type}_id"]][] = "{$type}_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
216 $update_sql[$row["{$type}_id"]][] = $type . '_last_post_time = ' . (int) $row['post_time'];
217 $update_sql[$row["{$type}_id"]][] = $type . '_last_poster_id = ' . (int) $row['poster_id'];
218 $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
219 $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "'";
221 $db->sql_freeresult($result);
223 unset($empty_forums, $ids, $last_post_ids);
225 if ($return_update_sql || !sizeof($update_sql))
227 return $update_sql;
230 $table = ($type == 'forum') ? FORUMS_TABLE : TOPICS_TABLE;
232 foreach ($update_sql as $update_id => $update_sql_ary)
234 $sql = "UPDATE $table
235 SET " . implode(', ', $update_sql_ary) . "
236 WHERE {$type}_id = $update_id";
237 $db->sql_query($sql);
240 return;
244 * Generate Topic Icons for display
246 function posting_gen_topic_icons($mode, $icon_id)
248 global $template;
250 // Grab icons
251 $icons = phpbb_cache::obtain_icons();
253 if (!$icon_id)
255 $template->assign_var('S_NO_ICON_CHECKED', ' checked="checked"');
258 if (sizeof($icons))
260 foreach ($icons as $id => $data)
262 if ($data['display'])
264 $template->assign_block_vars('topic_icon', array(
265 'ICON_ID' => $id,
266 'ICON_IMG' => PHPBB_ROOT_PATH . phpbb::$config['icons_path'] . '/' . $data['img'],
267 'ICON_WIDTH' => $data['width'],
268 'ICON_HEIGHT' => $data['height'],
270 'S_CHECKED' => ($id == $icon_id) ? true : false,
271 'S_ICON_CHECKED' => ($id == $icon_id) ? ' checked="checked"' : '')
276 return true;
279 return false;
283 * Build topic types able to be selected
285 function posting_gen_topic_types($forum_id, $cur_topic_type = POST_NORMAL)
287 global $auth, $user, $template, $topic_type;
289 $toggle = false;
291 $topic_types = array(
292 'sticky' => array('const' => POST_STICKY, 'lang' => 'POST_STICKY'),
293 'announce' => array('const' => POST_ANNOUNCE, 'lang' => 'POST_ANNOUNCEMENT'),
294 'global' => array('const' => POST_GLOBAL, 'lang' => 'POST_GLOBAL')
297 $topic_type_array = array();
299 foreach ($topic_types as $auth_key => $topic_value)
301 // We do not have a special post global announcement permission
302 $auth_key = ($auth_key == 'global') ? 'announce' : $auth_key;
304 if ($auth->acl_get('f_' . $auth_key, $forum_id))
306 $toggle = true;
308 $topic_type_array[] = array(
309 'VALUE' => $topic_value['const'],
310 'S_CHECKED' => ($cur_topic_type == $topic_value['const'] || ($forum_id == 0 && $topic_value['const'] == POST_GLOBAL)) ? ' checked="checked"' : '',
311 'L_TOPIC_TYPE' => $user->lang[$topic_value['lang']]
316 if ($toggle)
318 $topic_type_array = array_merge(array(0 => array(
319 'VALUE' => POST_NORMAL,
320 'S_CHECKED' => ($topic_type == POST_NORMAL) ? ' checked="checked"' : '',
321 'L_TOPIC_TYPE' => $user->lang['POST_NORMAL'])),
323 $topic_type_array
326 foreach ($topic_type_array as $array)
328 $template->assign_block_vars('topic_type', $array);
331 $template->assign_vars(array(
332 'S_TOPIC_TYPE_STICKY' => ($auth->acl_get('f_sticky', $forum_id)),
333 'S_TOPIC_TYPE_ANNOUNCE' => ($auth->acl_get('f_announce', $forum_id)))
337 return $toggle;
341 // Attachment related functions
345 * Upload Attachment - filedata is generated here
346 * Uses upload class
348 function upload_attachment($form_name, $forum_id, $local = false, $local_storage = '', $is_message = false, $local_filedata = false)
350 global $auth, $user, $db;
352 $filedata = array(
353 'error' => array()
356 include_once(PHPBB_ROOT_PATH . 'includes/functions_upload.' . PHP_EXT);
357 $upload = new fileupload();
359 if (phpbb::$config['check_attachment_content'])
361 $upload->set_disallowed_content(explode('|', phpbb::$config['mime_triggers']));
364 if (!$local)
366 $filedata['post_attach'] = ($upload->is_valid($form_name)) ? true : false;
368 else
370 $filedata['post_attach'] = true;
373 if (!$filedata['post_attach'])
375 $filedata['error'][] = $user->lang['NO_UPLOAD_FORM_FOUND'];
376 return $filedata;
379 $extensions = ($is_message) ? phpbb_cache::obtain_extensions_pm() : phpbb_cache::obtain_extensions_forum($forum_id);
380 $upload->set_allowed_extensions(array_keys($extensions['_allowed_']));
382 $file = ($local) ? $upload->local_upload($local_storage, $local_filedata) : $upload->form_upload($form_name);
384 if ($file->init_error)
386 $filedata['post_attach'] = false;
387 return $filedata;
390 $cat_id = (isset($extensions[$file->get('extension')]['display_cat'])) ? $extensions[$file->get('extension')]['display_cat'] : ATTACHMENT_CATEGORY_NONE;
392 // Make sure the image category only holds valid images...
393 if ($cat_id == ATTACHMENT_CATEGORY_IMAGE && !$file->is_image())
395 $file->remove();
397 // If this error occurs a user tried to exploit an IE Bug by renaming extensions
398 // Since the image category is displaying content inline we need to catch this.
399 trigger_error($user->lang['ATTACHED_IMAGE_NOT_IMAGE']);
402 // Do we have to create a thumbnail?
403 $filedata['thumbnail'] = ($cat_id == ATTACHMENT_CATEGORY_IMAGE && phpbb::$config['img_create_thumbnail']) ? 1 : 0;
405 // Check Image Size, if it is an image
406 if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id) && $cat_id == ATTACHMENT_CATEGORY_IMAGE)
408 $file->upload->set_allowed_dimensions(0, 0, phpbb::$config['img_max_width'], phpbb::$config['img_max_height']);
411 // Admins and mods are allowed to exceed the allowed filesize
412 if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id))
414 if (!empty($extensions[$file->get('extension')]['max_filesize']))
416 $allowed_filesize = $extensions[$file->get('extension')]['max_filesize'];
418 else
420 $allowed_filesize = ($is_message) ? phpbb::$config['max_filesize_pm'] : phpbb::$config['max_filesize'];
423 $file->upload->set_max_filesize($allowed_filesize);
426 $file->clean_filename('unique', $user->data['user_id'] . '_');
428 // Are we uploading an image *and* this image being within the image category? Only then perform additional image checks.
429 $no_image = ($cat_id == ATTACHMENT_CATEGORY_IMAGE) ? false : true;
431 $file->move_file(phpbb::$config['upload_path'], false, $no_image);
433 if (sizeof($file->error))
435 $file->remove();
436 $filedata['error'] = array_merge($filedata['error'], $file->error);
437 $filedata['post_attach'] = false;
439 return $filedata;
442 $filedata['filesize'] = $file->get('filesize');
443 $filedata['mimetype'] = $file->get('mimetype');
444 $filedata['extension'] = $file->get('extension');
445 $filedata['physical_filename'] = $file->get('realname');
446 $filedata['real_filename'] = $file->get('uploadname');
447 $filedata['filetime'] = time();
449 // Check our complete quota
450 if (phpbb::$config['attachment_quota'])
452 if (phpbb::$config['upload_dir_size'] + $file->get('filesize') > phpbb::$config['attachment_quota'])
454 $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
455 $filedata['post_attach'] = false;
457 $file->remove();
459 return $filedata;
463 // Check free disk space
464 if ($free_space = @disk_free_space(PHPBB_ROOT_PATH . phpbb::$config['upload_path']))
466 if ($free_space <= $file->get('filesize'))
468 $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
469 $filedata['post_attach'] = false;
471 $file->remove();
473 return $filedata;
477 // Create Thumbnail
478 if ($filedata['thumbnail'])
480 $source = $file->get('destination_file');
481 $destination = $file->get('destination_path') . '/thumb_' . $file->get('realname');
483 if (!create_thumbnail($source, $destination, $file->get('mimetype')))
485 $filedata['thumbnail'] = 0;
489 return $filedata;
493 * Calculate the needed size for Thumbnail
495 function get_img_size_format($width, $height)
497 // Maximum Width the Image can take
498 $max_width = (phpbb::$config['img_max_thumb_width']) ? phpbb::$config['img_max_thumb_width'] : 400;
500 if ($width > $height)
502 return array(
503 round($width * ($max_width / $width)),
504 round($height * ($max_width / $width))
507 else
509 return array(
510 round($width * ($max_width / $height)),
511 round($height * ($max_width / $height))
517 * Return supported image types
519 function get_supported_image_types($type = false)
521 if (@extension_loaded('gd'))
523 $format = imagetypes();
524 $new_type = 0;
526 if ($type !== false)
528 // Type is one of the IMAGETYPE constants - it is fetched from getimagesize()
529 // We do not use the constants here, because some were not available in PHP 4.3.x
530 switch ($type)
532 // GIF
533 case 1:
534 $new_type = ($format & IMG_GIF) ? IMG_GIF : false;
535 break;
537 // JPG, JPC, JP2
538 case 2:
539 case 9:
540 case 10:
541 case 11:
542 case 12:
543 $new_type = ($format & IMG_JPG) ? IMG_JPG : false;
544 break;
546 // PNG
547 case 3:
548 $new_type = ($format & IMG_PNG) ? IMG_PNG : false;
549 break;
551 // WBMP
552 case 15:
553 $new_type = ($format & IMG_WBMP) ? IMG_WBMP : false;
554 break;
557 else
559 $new_type = array();
560 $go_through_types = array(IMG_GIF, IMG_JPG, IMG_PNG, IMG_WBMP);
562 foreach ($go_through_types as $check_type)
564 if ($format & $check_type)
566 $new_type[] = $check_type;
571 return array(
572 'gd' => ($new_type) ? true : false,
573 'format' => $new_type,
574 'version' => (function_exists('imagecreatetruecolor')) ? 2 : 1
578 return array('gd' => false);
582 * Create Thumbnail
584 function create_thumbnail($source, $destination, $mimetype)
586 $min_filesize = (int) phpbb::$config['img_min_thumb_filesize'];
587 $img_filesize = (file_exists($source)) ? @filesize($source) : false;
589 if (!$img_filesize || $img_filesize <= $min_filesize)
591 return false;
594 $dimension = @getimagesize($source);
596 if ($dimension === false)
598 return false;
601 list($width, $height, $type, ) = $dimension;
603 if (empty($width) || empty($height))
605 return false;
608 list($new_width, $new_height) = get_img_size_format($width, $height);
610 // Do not create a thumbnail if the resulting width/height is bigger than the original one
611 if ($new_width > $width && $new_height > $height)
613 return false;
616 $used_imagick = false;
618 // Only use imagemagick if defined and the passthru function not disabled
619 if (phpbb::$config['img_imagick'] && function_exists('passthru'))
621 if (substr(phpbb::$config['img_imagick'], -1) !== '/')
623 phpbb::$config['img_imagick'] .= '/';
626 @passthru(escapeshellcmd(phpbb::$config['img_imagick']) . 'convert' . ((defined('PHP_OS') && preg_match('#^win#i', PHP_OS)) ? '.exe' : '') . ' -quality 85 -antialias -sample ' . $new_width . 'x' . $new_height . ' "' . str_replace('\\', '/', $source) . '" +profile "*" "' . str_replace('\\', '/', $destination) . '"');
628 if (file_exists($destination))
630 $used_imagick = true;
634 if (!$used_imagick)
636 $type = get_supported_image_types($type);
638 if ($type['gd'])
640 // If the type is not supported, we are not able to create a thumbnail
641 if ($type['format'] === false)
643 return false;
646 switch ($type['format'])
648 case IMG_GIF:
649 $image = @imagecreatefromgif($source);
650 break;
652 case IMG_JPG:
653 $image = @imagecreatefromjpeg($source);
654 break;
656 case IMG_PNG:
657 $image = @imagecreatefrompng($source);
658 break;
660 case IMG_WBMP:
661 $image = @imagecreatefromwbmp($source);
662 break;
665 if ($type['version'] == 1)
667 $new_image = imagecreate($new_width, $new_height);
669 if ($new_image === false)
671 return false;
674 imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
676 else
678 $new_image = imagecreatetruecolor($new_width, $new_height);
680 if ($new_image === false)
682 return false;
685 // Preserve alpha transparency (png for example)
686 @imagealphablending($new_image, false);
687 @imagesavealpha($new_image, true);
689 imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
692 // If we are in safe mode create the destination file prior to using the gd functions to circumvent a PHP bug
693 if (@ini_get('safe_mode') || @strtolower(ini_get('safe_mode')) == 'on')
695 @touch($destination);
698 switch ($type['format'])
700 case IMG_GIF:
701 imagegif($new_image, $destination);
702 break;
704 case IMG_JPG:
705 imagejpeg($new_image, $destination, 90);
706 break;
708 case IMG_PNG:
709 imagepng($new_image, $destination);
710 break;
712 case IMG_WBMP:
713 imagewbmp($new_image, $destination);
714 break;
717 imagedestroy($new_image);
719 else
721 return false;
725 if (!file_exists($destination))
727 return false;
730 phpbb_chmod($destination, phpbb::CHMOD_READ | phpbb::CHMOD_WRITE);
732 return true;
736 * Assign Inline attachments (build option fields)
738 function posting_gen_inline_attachments(&$attachment_data)
740 global $template;
742 if (sizeof($attachment_data))
744 $s_inline_attachment_options = '';
746 foreach ($attachment_data as $i => $attachment)
748 $s_inline_attachment_options .= '<option value="' . $i . '">' . basename($attachment['real_filename']) . '</option>';
751 $template->assign_var('S_INLINE_ATTACHMENT_OPTIONS', $s_inline_attachment_options);
753 return true;
756 return false;
760 * Generate inline attachment entry
762 function posting_gen_attachment_entry($attachment_data, &$filename_data, $show_attach_box = true)
764 global $template, $user, $auth;
766 // Some default template variables
767 $template->assign_vars(array(
768 'S_SHOW_ATTACH_BOX' => $show_attach_box,
769 'S_HAS_ATTACHMENTS' => sizeof($attachment_data),
770 'FILESIZE' => phpbb::$config['max_filesize'],
771 'FILE_COMMENT' => (isset($filename_data['filecomment'])) ? $filename_data['filecomment'] : '',
774 if (sizeof($attachment_data))
776 // We display the posted attachments within the desired order.
777 (phpbb::$config['display_order']) ? krsort($attachment_data) : ksort($attachment_data);
779 foreach ($attachment_data as $count => $attach_row)
781 $hidden = '';
782 $attach_row['real_filename'] = basename($attach_row['real_filename']);
784 foreach ($attach_row as $key => $value)
786 $hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />';
789 $download_link = append_sid('download/file', 'mode=view&amp;id=' . (int) $attach_row['attach_id'], true, ($attach_row['is_orphan']) ? $user->session_id : false);
791 $template->assign_block_vars('attach_row', array(
792 'FILENAME' => basename($attach_row['real_filename']),
793 'A_FILENAME' => addslashes(basename($attach_row['real_filename'])),
794 'FILE_COMMENT' => $attach_row['attach_comment'],
795 'ATTACH_ID' => $attach_row['attach_id'],
796 'S_IS_ORPHAN' => $attach_row['is_orphan'],
797 'ASSOC_INDEX' => $count,
799 'U_VIEW_ATTACHMENT' => $download_link,
800 'S_HIDDEN' => $hidden)
805 return sizeof($attachment_data);
809 // General Post functions
813 * Load Drafts
815 function load_drafts($topic_id = 0, $forum_id = 0, $id = 0)
817 global $user, $db, $template, $auth;
819 $topic_ids = $forum_ids = $draft_rows = array();
821 // Load those drafts not connected to forums/topics
822 // If forum_id == 0 AND topic_id == 0 then this is a PM draft
823 if (!$topic_id && !$forum_id)
825 $sql_and = ' AND d.forum_id = 0 AND d.topic_id = 0';
827 else
829 $sql_and = '';
830 $sql_and .= ($forum_id) ? ' AND d.forum_id = ' . (int) $forum_id : '';
831 $sql_and .= ($topic_id) ? ' AND d.topic_id = ' . (int) $topic_id : '';
834 $sql = 'SELECT d.*, f.forum_id, f.forum_name
835 FROM ' . DRAFTS_TABLE . ' d
836 LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = d.forum_id)
837 WHERE d.user_id = ' . $user->data['user_id'] . "
838 $sql_and
839 ORDER BY d.save_time DESC";
840 $result = $db->sql_query($sql);
842 while ($row = $db->sql_fetchrow($result))
844 if ($row['topic_id'])
846 $topic_ids[] = (int) $row['topic_id'];
848 $draft_rows[] = $row;
850 $db->sql_freeresult($result);
852 if (!sizeof($draft_rows))
854 return;
857 $topic_rows = array();
858 if (sizeof($topic_ids))
860 $sql = 'SELECT topic_id, forum_id, topic_title
861 FROM ' . TOPICS_TABLE . '
862 WHERE ' . $db->sql_in_set('topic_id', array_unique($topic_ids));
863 $result = $db->sql_query($sql);
865 while ($row = $db->sql_fetchrow($result))
867 $topic_rows[$row['topic_id']] = $row;
869 $db->sql_freeresult($result);
871 unset($topic_ids);
873 $template->assign_var('S_SHOW_DRAFTS', true);
875 foreach ($draft_rows as $draft)
877 $link_topic = $link_forum = $link_pm = false;
878 $insert_url = $view_url = $title = '';
880 if (isset($topic_rows[$draft['topic_id']])
881 && (
882 ($topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_get('f_read', $topic_rows[$draft['topic_id']]['forum_id']))
884 (!$topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_getf_global('f_read'))
887 $topic_forum_id = ($topic_rows[$draft['topic_id']]['forum_id']) ? $topic_rows[$draft['topic_id']]['forum_id'] : $forum_id;
889 $link_topic = true;
890 $view_url = append_sid('viewtopic', 'f=' . $topic_forum_id . '&amp;t=' . $draft['topic_id']);
891 $title = $topic_rows[$draft['topic_id']]['topic_title'];
893 $insert_url = append_sid('posting', 'f=' . $topic_forum_id . '&amp;t=' . $draft['topic_id'] . '&amp;mode=reply&amp;d=' . $draft['draft_id']);
895 else if ($draft['forum_id'] && $auth->acl_get('f_read', $draft['forum_id']))
897 $link_forum = true;
898 $view_url = append_sid('viewforum', 'f=' . $draft['forum_id']);
899 $title = $draft['forum_name'];
901 $insert_url = append_sid('posting', 'f=' . $draft['forum_id'] . '&amp;mode=post&amp;d=' . $draft['draft_id']);
903 else
905 // Either display as PM draft if forum_id and topic_id are empty or if access to the forums has been denied afterwards...
906 $link_pm = true;
907 $insert_url = append_sid('ucp', "i=$id&amp;mode=compose&amp;d={$draft['draft_id']}");
910 $template->assign_block_vars('draftrow', array(
911 'DRAFT_ID' => $draft['draft_id'],
912 'DATE' => $user->format_date($draft['save_time']),
913 'DRAFT_SUBJECT' => $draft['draft_subject'],
915 'TITLE' => $title,
916 'U_VIEW' => $view_url,
917 'U_INSERT' => $insert_url,
919 'S_LINK_PM' => $link_pm,
920 'S_LINK_TOPIC' => $link_topic,
921 'S_LINK_FORUM' => $link_forum)
927 * Topic Review
929 function topic_review($topic_id, $forum_id, $mode = 'topic_review', $cur_post_id = 0, $show_quote_button = true)
931 global $user, $auth, $db, $template, $bbcode;
933 // Go ahead and pull all data for this topic
934 $sql = 'SELECT p.post_id
935 FROM ' . POSTS_TABLE . ' p' . "
936 WHERE p.topic_id = $topic_id
937 " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . '
938 ' . (($mode == 'post_review') ? " AND p.post_id > $cur_post_id" : '') . '
939 ORDER BY p.post_time ';
940 $sql .= ($mode == 'post_review') ? 'ASC' : 'DESC';
941 $result = $db->sql_query_limit($sql, phpbb::$config['posts_per_page']);
943 $post_list = array();
945 while ($row = $db->sql_fetchrow($result))
947 $post_list[] = $row['post_id'];
950 $db->sql_freeresult($result);
952 if (!sizeof($post_list))
954 return false;
957 $sql = $db->sql_build_query('SELECT', array(
958 'SELECT' => 'u.username, u.user_id, u.user_colour, p.*',
960 'FROM' => array(
961 USERS_TABLE => 'u',
962 POSTS_TABLE => 'p',
965 'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
966 AND u.user_id = p.poster_id'
969 $result = $db->sql_query($sql);
971 $bbcode_bitfield = '';
972 $rowset = array();
973 $has_attachments = false;
974 while ($row = $db->sql_fetchrow($result))
976 $rowset[$row['post_id']] = $row;
977 $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
979 if ($row['post_attachment'])
981 $has_attachments = true;
984 $db->sql_freeresult($result);
986 // Instantiate BBCode class
987 if (!isset($bbcode) && $bbcode_bitfield !== '')
989 include_once(PHPBB_ROOT_PATH . 'includes/bbcode.' . PHP_EXT);
990 $bbcode = new bbcode(base64_encode($bbcode_bitfield));
993 // Grab extensions
994 $extensions = $attachments = array();
995 if ($has_attachments && $auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
997 $extensions = phpbb_cache::obtain_extensions_forum($forum_id);
999 // Get attachments...
1000 $sql = 'SELECT *
1001 FROM ' . ATTACHMENTS_TABLE . '
1002 WHERE ' . $db->sql_in_set('post_msg_id', $post_list) . '
1003 AND in_message = 0
1004 ORDER BY filetime DESC, post_msg_id ASC';
1005 $result = $db->sql_query($sql);
1007 while ($row = $db->sql_fetchrow($result))
1009 $attachments[$row['post_msg_id']][] = $row;
1011 $db->sql_freeresult($result);
1014 for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
1016 // A non-existing rowset only happens if there was no user present for the entered poster_id
1017 // This could be a broken posts table.
1018 if (!isset($rowset[$post_list[$i]]))
1020 continue;
1023 $row =& $rowset[$post_list[$i]];
1025 $poster_id = $row['user_id'];
1026 $post_subject = $row['post_subject'];
1027 $message = censor_text($row['post_text']);
1029 $decoded_message = false;
1031 if ($show_quote_button && $auth->acl_get('f_reply', $forum_id))
1033 $decoded_message = $message;
1034 decode_message($decoded_message, $row['bbcode_uid']);
1036 $decoded_message = bbcode_nl2br($decoded_message);
1039 if ($row['bbcode_bitfield'])
1041 $bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
1044 $message = bbcode_nl2br($message);
1045 $message = smiley_text($message, !$row['enable_smilies']);
1047 if (!empty($attachments[$row['post_id']]))
1049 $update_count = array();
1050 parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
1053 $post_subject = censor_text($post_subject);
1055 $template->assign_block_vars($mode . '_row', array(
1056 'POST_AUTHOR_FULL' => get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1057 'POST_AUTHOR_COLOUR' => get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1058 'POST_AUTHOR' => get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1059 'U_POST_AUTHOR' => get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1061 'S_HAS_ATTACHMENTS' => (!empty($attachments[$row['post_id']])) ? true : false,
1063 'POST_SUBJECT' => $post_subject,
1064 'MINI_POST_IMG' => $user->img('icon_post_target', $user->lang['POST']),
1065 'POST_DATE' => $user->format_date($row['post_time']),
1066 'MESSAGE' => $message,
1067 'DECODED_MESSAGE' => $decoded_message,
1068 'POST_ID' => $row['post_id'],
1069 'U_MINI_POST' => append_sid('viewtopic', 'p=' . $row['post_id']) . '#p' . $row['post_id'],
1070 'U_MCP_DETAILS' => ($auth->acl_get('m_info', $forum_id)) ? append_sid('mcp', 'i=main&amp;mode=post_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
1071 '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'])) : '')
1074 // Display not already displayed Attachments for this post, we already parsed them. ;)
1075 if (!empty($attachments[$row['post_id']]))
1077 foreach ($attachments[$row['post_id']] as $attachment)
1079 $template->assign_block_vars($mode . '_row.attachment', array(
1080 'DISPLAY_ATTACHMENT' => $attachment)
1085 unset($rowset[$i]);
1088 if ($mode == 'topic_review')
1090 $template->assign_var('QUOTE_IMG', $user->img('icon_post_quote', $user->lang['REPLY_WITH_QUOTE']));
1093 return true;
1097 * User Notification
1099 function user_notification($mode, $subject, $topic_title, $forum_name, $forum_id, $topic_id, $post_id)
1101 global $db, $user, $auth;
1103 $topic_notification = ($mode == 'reply' || $mode == 'quote') ? true : false;
1104 $forum_notification = ($mode == 'post') ? true : false;
1106 if (!$topic_notification && !$forum_notification)
1108 trigger_error('WRONG_NOTIFICATION_MODE');
1111 if (($topic_notification && !phpbb::$config['allow_topic_notify']) || ($forum_notification && !phpbb::$config['allow_forum_notify']))
1113 return;
1116 $topic_title = ($topic_notification) ? $topic_title : $subject;
1117 $topic_title = censor_text($topic_title);
1119 // Get banned User ID's
1120 $sql = 'SELECT ban_userid
1121 FROM ' . BANLIST_TABLE . '
1122 WHERE ban_userid <> 0
1123 AND ban_exclude <> 1';
1124 $result = $db->sql_query($sql);
1126 $sql_ignore_users = ANONYMOUS . ', ' . $user->data['user_id'];
1127 while ($row = $db->sql_fetchrow($result))
1129 $sql_ignore_users .= ', ' . (int) $row['ban_userid'];
1131 $db->sql_freeresult($result);
1133 $notify_rows = array();
1135 // -- get forum_userids || topic_userids
1136 $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
1137 FROM ' . (($topic_notification) ? TOPICS_WATCH_TABLE : FORUMS_WATCH_TABLE) . ' w, ' . USERS_TABLE . ' u
1138 WHERE w.' . (($topic_notification) ? 'topic_id' : 'forum_id') . ' = ' . (($topic_notification) ? $topic_id : $forum_id) . "
1139 AND w.user_id NOT IN ($sql_ignore_users)
1140 AND w.notify_status = 0
1141 AND u.user_type IN (" . phpbb::USER_NORMAL . ', ' . phpbb::USER_FOUNDER . ')
1142 AND u.user_id = w.user_id';
1143 $result = $db->sql_query($sql);
1145 while ($row = $db->sql_fetchrow($result))
1147 $notify_rows[$row['user_id']] = array(
1148 'user_id' => $row['user_id'],
1149 'username' => $row['username'],
1150 'user_email' => $row['user_email'],
1151 'user_jabber' => $row['user_jabber'],
1152 'user_lang' => $row['user_lang'],
1153 'notify_type' => ($topic_notification) ? 'topic' : 'forum',
1154 'template' => ($topic_notification) ? 'topic_notify' : 'newtopic_notify',
1155 'method' => $row['user_notify_type'],
1156 'allowed' => false
1159 $db->sql_freeresult($result);
1161 // forum notification is sent to those not already receiving topic notifications
1162 if ($topic_notification)
1164 if (sizeof($notify_rows))
1166 $sql_ignore_users .= ', ' . implode(', ', array_keys($notify_rows));
1169 $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
1170 FROM ' . FORUMS_WATCH_TABLE . ' fw, ' . USERS_TABLE . " u
1171 WHERE fw.forum_id = $forum_id
1172 AND fw.user_id NOT IN ($sql_ignore_users)
1173 AND fw.notify_status = 0
1174 AND u.user_type IN (" . phpbb::USER_NORMAL . ', ' . phpbb::USER_FOUNDER . ')
1175 AND u.user_id = fw.user_id';
1176 $result = $db->sql_query($sql);
1178 while ($row = $db->sql_fetchrow($result))
1180 $notify_rows[$row['user_id']] = array(
1181 'user_id' => $row['user_id'],
1182 'username' => $row['username'],
1183 'user_email' => $row['user_email'],
1184 'user_jabber' => $row['user_jabber'],
1185 'user_lang' => $row['user_lang'],
1186 'notify_type' => 'forum',
1187 'template' => 'forum_notify',
1188 'method' => $row['user_notify_type'],
1189 'allowed' => false
1192 $db->sql_freeresult($result);
1195 if (!sizeof($notify_rows))
1197 return;
1200 // Make sure users are allowed to read the forum
1201 foreach ($auth->acl_get_list(array_keys($notify_rows), 'f_read', $forum_id) as $forum_id => $forum_ary)
1203 foreach ($forum_ary as $auth_option => $user_ary)
1205 foreach ($user_ary as $user_id)
1207 $notify_rows[$user_id]['allowed'] = true;
1213 // Now, we have to do a little step before really sending, we need to distinguish our users a little bit. ;)
1214 $msg_users = $delete_ids = $update_notification = array();
1215 foreach ($notify_rows as $user_id => $row)
1217 if (!$row['allowed'] || !trim($row['user_email']))
1219 $delete_ids[$row['notify_type']][] = $row['user_id'];
1221 else
1223 $msg_users[] = $row;
1224 $update_notification[$row['notify_type']][] = $row['user_id'];
1227 unset($notify_rows);
1229 // Now, we are able to really send out notifications
1230 if (sizeof($msg_users))
1232 include_once(PHPBB_ROOT_PATH . 'includes/functions_messenger.' . PHP_EXT);
1233 $messenger = new messenger();
1235 $msg_list_ary = array();
1236 foreach ($msg_users as $row)
1238 $pos = (!isset($msg_list_ary[$row['template']])) ? 0 : sizeof($msg_list_ary[$row['template']]);
1240 $msg_list_ary[$row['template']][$pos]['method'] = $row['method'];
1241 $msg_list_ary[$row['template']][$pos]['email'] = $row['user_email'];
1242 $msg_list_ary[$row['template']][$pos]['jabber'] = $row['user_jabber'];
1243 $msg_list_ary[$row['template']][$pos]['name'] = $row['username'];
1244 $msg_list_ary[$row['template']][$pos]['lang'] = $row['user_lang'];
1245 $msg_list_ary[$row['template']][$pos]['user_id']= $row['user_id'];
1247 unset($msg_users);
1249 foreach ($msg_list_ary as $email_template => $email_list)
1251 foreach ($email_list as $addr)
1253 $messenger->template($email_template, $addr['lang']);
1255 $messenger->to($addr['email'], $addr['name']);
1256 $messenger->im($addr['jabber'], $addr['name']);
1258 $messenger->assign_vars(array(
1259 'USERNAME' => htmlspecialchars_decode($addr['name']),
1260 'TOPIC_TITLE' => htmlspecialchars_decode($topic_title),
1261 'FORUM_NAME' => htmlspecialchars_decode($forum_name),
1263 'U_FORUM' => generate_board_url() . '/viewforum.' . PHP_EXT . "?f=$forum_id",
1264 'U_TOPIC' => generate_board_url() . '/viewtopic.' . PHP_EXT . "?f=$forum_id&t=$topic_id",
1265 'U_NEWEST_POST' => generate_board_url() . '/viewtopic.' . PHP_EXT . "?f=$forum_id&t=$topic_id&p=$post_id&e=$post_id",
1266 'U_STOP_WATCHING_TOPIC' => generate_board_url() . '/viewtopic.' . PHP_EXT . "?uid={$addr['user_id']}&f=$forum_id&t=$topic_id&unwatch=topic",
1267 'U_STOP_WATCHING_FORUM' => generate_board_url() . '/viewforum.' . PHP_EXT . "?uid={$addr['user_id']}&f=$forum_id&unwatch=forum",
1270 $messenger->send($addr['method']);
1273 unset($msg_list_ary);
1275 $messenger->save_queue();
1278 // Handle the DB updates
1279 $db->sql_transaction('begin');
1281 if (!empty($update_notification['topic']))
1283 $sql = 'UPDATE ' . TOPICS_WATCH_TABLE . "
1284 SET notify_status = 1
1285 WHERE topic_id = $topic_id
1286 AND " . $db->sql_in_set('user_id', $update_notification['topic']);
1287 $db->sql_query($sql);
1290 if (!empty($update_notification['forum']))
1292 $sql = 'UPDATE ' . FORUMS_WATCH_TABLE . "
1293 SET notify_status = 1
1294 WHERE forum_id = $forum_id
1295 AND " . $db->sql_in_set('user_id', $update_notification['forum']);
1296 $db->sql_query($sql);
1299 // Now delete the user_ids not authorised to receive notifications on this topic/forum
1300 if (!empty($delete_ids['topic']))
1302 $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . "
1303 WHERE topic_id = $topic_id
1304 AND " . $db->sql_in_set('user_id', $delete_ids['topic']);
1305 $db->sql_query($sql);
1308 if (!empty($delete_ids['forum']))
1310 $sql = 'DELETE FROM ' . FORUMS_WATCH_TABLE . "
1311 WHERE forum_id = $forum_id
1312 AND " . $db->sql_in_set('user_id', $delete_ids['forum']);
1313 $db->sql_query($sql);
1316 $db->sql_transaction('commit');
1320 // Post handling functions
1324 * Delete Post
1326 function delete_post($forum_id, $topic_id, $post_id, &$data)
1328 global $db, $user, $auth;
1330 // Specify our post mode
1331 $post_mode = 'delete';
1332 if (($data['topic_first_post_id'] === $data['topic_last_post_id']) && $data['topic_replies_real'] == 0)
1334 $post_mode = 'delete_topic';
1336 else if ($data['topic_first_post_id'] == $post_id)
1338 $post_mode = 'delete_first_post';
1340 else if ($data['topic_last_post_id'] == $post_id)
1342 $post_mode = 'delete_last_post';
1344 $sql_data = array();
1345 $next_post_id = false;
1347 include_once(PHPBB_ROOT_PATH . 'includes/functions_admin.' . PHP_EXT);
1349 $db->sql_transaction('begin');
1351 // we must make sure to update forums that contain the shadow'd topic
1352 if ($post_mode == 'delete_topic')
1354 $shadow_forum_ids = array();
1356 $sql = 'SELECT forum_id
1357 FROM ' . TOPICS_TABLE . '
1358 WHERE ' . $db->sql_in_set('topic_moved_id', $topic_id);
1359 $result = $db->sql_query($sql);
1360 while ($row = $db->sql_fetchrow($result))
1362 if (!isset($shadow_forum_ids[(int) $row['forum_id']]))
1364 $shadow_forum_ids[(int) $row['forum_id']] = 1;
1366 else
1368 $shadow_forum_ids[(int) $row['forum_id']]++;
1371 $db->sql_freeresult($result);
1374 if (!delete_posts('post_id', array($post_id), false, false))
1376 // Try to delete topic, we may had an previous error causing inconsistency
1377 if ($post_mode == 'delete_topic')
1379 delete_topics('topic_id', array($topic_id), false);
1381 trigger_error('ALREADY_DELETED');
1384 $db->sql_transaction('commit');
1386 // Collect the necessary information for updating the tables
1387 $sql_data[FORUMS_TABLE] = '';
1388 switch ($post_mode)
1390 case 'delete_topic':
1392 foreach ($shadow_forum_ids as $updated_forum => $topic_count)
1394 // counting is fun! we only have to do sizeof($forum_ids) number of queries,
1395 // even if the topic is moved back to where its shadow lives (we count how many times it is in a forum)
1396 $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);
1397 update_post_information('forum', $updated_forum);
1400 delete_topics('topic_id', array($topic_id), false);
1402 if ($data['topic_type'] != POST_GLOBAL)
1404 $sql_data[FORUMS_TABLE] .= 'forum_topics_real = forum_topics_real - 1';
1405 $sql_data[FORUMS_TABLE] .= ($data['topic_approved']) ? ', forum_posts = forum_posts - 1, forum_topics = forum_topics - 1' : '';
1408 $update_sql = update_post_information('forum', $forum_id, true);
1409 if (sizeof($update_sql))
1411 $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
1412 $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
1414 break;
1416 case 'delete_first_post':
1417 $sql = 'SELECT p.post_id, p.poster_id, p.post_username, u.username, u.user_colour
1418 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
1419 WHERE p.topic_id = $topic_id
1420 AND p.poster_id = u.user_id
1421 ORDER BY p.post_time ASC";
1422 $result = $db->sql_query_limit($sql, 1);
1423 $row = $db->sql_fetchrow($result);
1424 $db->sql_freeresult($result);
1426 if ($data['topic_type'] != POST_GLOBAL)
1428 $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
1431 $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'])) . "'";
1433 // 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"
1434 $sql_data[TOPICS_TABLE] .= ', topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
1436 $next_post_id = (int) $row['post_id'];
1437 break;
1439 case 'delete_last_post':
1440 if ($data['topic_type'] != POST_GLOBAL)
1442 $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
1445 $update_sql = update_post_information('forum', $forum_id, true);
1446 if (sizeof($update_sql))
1448 $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
1449 $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
1452 $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' : '');
1454 $update_sql = update_post_information('topic', $topic_id, true);
1455 if (sizeof($update_sql))
1457 $sql_data[TOPICS_TABLE] .= ', ' . implode(', ', $update_sql[$topic_id]);
1458 $next_post_id = (int) str_replace('topic_last_post_id = ', '', $update_sql[$topic_id][0]);
1460 else
1462 $sql = 'SELECT MAX(post_id) as last_post_id
1463 FROM ' . POSTS_TABLE . "
1464 WHERE topic_id = $topic_id " .
1465 ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '');
1466 $result = $db->sql_query($sql);
1467 $row = $db->sql_fetchrow($result);
1468 $db->sql_freeresult($result);
1470 $next_post_id = (int) $row['last_post_id'];
1472 break;
1474 case 'delete':
1475 $sql = 'SELECT post_id
1476 FROM ' . POSTS_TABLE . "
1477 WHERE topic_id = $topic_id " .
1478 ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '') . '
1479 AND post_time > ' . $data['post_time'] . '
1480 ORDER BY post_time ASC';
1481 $result = $db->sql_query_limit($sql, 1);
1482 $row = $db->sql_fetchrow($result);
1483 $db->sql_freeresult($result);
1485 if ($data['topic_type'] != POST_GLOBAL)
1487 $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
1490 $sql_data[TOPICS_TABLE] = 'topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
1491 $next_post_id = (int) $row['post_id'];
1492 break;
1495 // $sql_data[USERS_TABLE] = ($data['post_postcount']) ? 'user_posts = user_posts - 1' : '';
1497 $db->sql_transaction('begin');
1499 $where_sql = array(
1500 FORUMS_TABLE => "forum_id = $forum_id",
1501 TOPICS_TABLE => "topic_id = $topic_id",
1502 USERS_TABLE => 'user_id = ' . $data['poster_id']
1505 foreach ($sql_data as $table => $update_sql)
1507 if ($update_sql)
1509 $db->sql_query("UPDATE $table SET $update_sql WHERE " . $where_sql[$table]);
1513 // Adjust posted info for this user by looking for a post by him/her within this topic...
1514 if ($post_mode != 'delete_topic' && phpbb::$config['load_db_track'] && $data['poster_id'] != ANONYMOUS)
1516 $sql = 'SELECT poster_id
1517 FROM ' . POSTS_TABLE . '
1518 WHERE topic_id = ' . $topic_id . '
1519 AND poster_id = ' . $data['poster_id'];
1520 $result = $db->sql_query_limit($sql, 1);
1521 $poster_id = (int) $db->sql_fetchfield('poster_id');
1522 $db->sql_freeresult($result);
1524 // The user is not having any more posts within this topic
1525 if (!$poster_id)
1527 $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
1528 WHERE topic_id = ' . $topic_id . '
1529 AND user_id = ' . $data['poster_id'];
1530 $db->sql_query($sql);
1534 $db->sql_transaction('commit');
1536 if ($data['post_reported'] && ($post_mode != 'delete_topic'))
1538 sync('topic_reported', 'topic_id', array($topic_id));
1541 return $next_post_id;
1545 * Submit Post
1547 function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $update_message = true)
1549 global $db, $auth, $user, $template;
1551 // We do not handle erasing posts here
1552 if ($mode == 'delete')
1554 return false;
1557 $current_time = time();
1559 if ($mode == 'post')
1561 $post_mode = 'post';
1562 $update_message = true;
1564 else if ($mode != 'edit')
1566 $post_mode = 'reply';
1567 $update_message = true;
1569 else if ($mode == 'edit')
1571 $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'));
1574 // First of all make sure the subject and topic title are having the correct length.
1575 // To achieve this without cutting off between special chars we convert to an array and then count the elements.
1576 $subject = truncate_string($subject);
1577 $data['topic_title'] = truncate_string($data['topic_title']);
1579 // Collect some basic information about which tables and which rows to update/insert
1580 $sql_data = $topic_row = array();
1581 $poster_id = ($mode == 'edit') ? $data['poster_id'] : (int) $user->data['user_id'];
1583 // Retrieve some additional information if not present
1584 if ($mode == 'edit' && (!isset($data['post_approved']) || !isset($data['topic_approved']) || $data['post_approved'] === false || $data['topic_approved'] === false))
1586 $sql = 'SELECT p.post_approved, t.topic_type, t.topic_replies, t.topic_replies_real, t.topic_approved
1587 FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
1588 WHERE t.topic_id = p.topic_id
1589 AND p.post_id = ' . $data['post_id'];
1590 $result = $db->sql_query($sql);
1591 $topic_row = $db->sql_fetchrow($result);
1592 $db->sql_freeresult($result);
1594 $data['topic_approved'] = $topic_row['topic_approved'];
1595 $data['post_approved'] = $topic_row['post_approved'];
1598 // 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
1599 $post_approval = 1;
1601 // Check the permissions for post approval, as well as the queue trigger where users are put on approval with a post count lower than specified. Moderators are not affected.
1602 if (((phpbb::$config['enable_queue_trigger'] && $user->data['user_posts'] < phpbb::$config['queue_trigger_posts']) || !$auth->acl_get('f_noapprove', $data['forum_id'])) && !$auth->acl_get('m_approve', $data['forum_id']))
1604 $post_approval = 0;
1607 // Start the transaction here
1608 $db->sql_transaction('begin');
1610 // Collect Information
1611 switch ($post_mode)
1613 case 'post':
1614 case 'reply':
1615 $sql_data[POSTS_TABLE]['sql'] = array(
1616 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1617 'poster_id' => (int) $user->data['user_id'],
1618 'icon_id' => $data['icon_id'],
1619 'poster_ip' => $user->ip,
1620 'post_time' => $current_time,
1621 'post_approved' => $post_approval,
1622 'enable_bbcode' => $data['enable_bbcode'],
1623 'enable_smilies' => $data['enable_smilies'],
1624 'enable_magic_url' => $data['enable_urls'],
1625 'enable_sig' => $data['enable_sig'],
1626 'post_username' => (!$user->data['is_registered']) ? $username : '',
1627 'post_subject' => $subject,
1628 'post_text' => $data['message'],
1629 'post_checksum' => $data['message_md5'],
1630 'post_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
1631 'bbcode_bitfield' => $data['bbcode_bitfield'],
1632 'bbcode_uid' => $data['bbcode_uid'],
1633 'post_postcount' => ($auth->acl_get('f_postcount', $data['forum_id'])) ? 1 : 0,
1634 'post_edit_locked' => $data['post_edit_locked']
1636 break;
1638 case 'edit_first_post':
1639 case 'edit':
1641 case 'edit_last_post':
1642 case 'edit_topic':
1644 // If edit reason is given always display edit info
1646 // If editing last post then display no edit info
1647 // If m_edit permission then display no edit info
1648 // If normal edit display edit info
1650 // Display edit info if edit reason given or user is editing his post, which is not the last within the topic.
1651 if ($data['post_edit_reason'] || (!$auth->acl_get('m_edit', $data['forum_id']) && ($post_mode == 'edit' || $post_mode == 'edit_first_post')))
1653 $data['post_edit_reason'] = truncate_string($data['post_edit_reason'], 255, 255, false);
1655 $sql_data[POSTS_TABLE]['sql'] = array(
1656 'post_edit_time' => $current_time,
1657 'post_edit_reason' => $data['post_edit_reason'],
1658 'post_edit_user' => (int) $data['post_edit_user'],
1661 $sql_data[POSTS_TABLE]['stat'][] = 'post_edit_count = post_edit_count + 1';
1663 else if (!$data['post_edit_reason'] && $mode == 'edit' && $auth->acl_get('m_edit', $data['forum_id']))
1665 $sql_data[POSTS_TABLE]['sql'] = array(
1666 'post_edit_reason' => '',
1670 // If the person editing this post is different to the one having posted then we will add a log entry stating the edit
1671 // Could be simplified by only adding to the log if the edit is not tracked - but this may confuse admins/mods
1672 if ($user->data['user_id'] != $poster_id)
1674 $log_subject = ($subject) ? $subject : $data['topic_title'];
1675 add_log('mod', $data['forum_id'], $data['topic_id'], 'LOG_POST_EDITED', $log_subject, (!empty($username)) ? $username : $user->lang['GUEST']);
1678 if (!isset($sql_data[POSTS_TABLE]['sql']))
1680 $sql_data[POSTS_TABLE]['sql'] = array();
1683 $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1684 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1685 'poster_id' => $data['poster_id'],
1686 'icon_id' => $data['icon_id'],
1687 'post_approved' => (!$post_approval) ? 0 : $data['post_approved'],
1688 'enable_bbcode' => $data['enable_bbcode'],
1689 'enable_smilies' => $data['enable_smilies'],
1690 'enable_magic_url' => $data['enable_urls'],
1691 'enable_sig' => $data['enable_sig'],
1692 'post_username' => ($username && $data['poster_id'] == ANONYMOUS) ? $username : '',
1693 'post_subject' => $subject,
1694 'post_checksum' => $data['message_md5'],
1695 'post_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
1696 'bbcode_bitfield' => $data['bbcode_bitfield'],
1697 'bbcode_uid' => $data['bbcode_uid'],
1698 'post_edit_locked' => $data['post_edit_locked'])
1701 if ($update_message)
1703 $sql_data[POSTS_TABLE]['sql']['post_text'] = $data['message'];
1706 break;
1709 $post_approved = $sql_data[POSTS_TABLE]['sql']['post_approved'];
1710 $topic_row = array();
1712 // And the topic ladies and gentlemen
1713 switch ($post_mode)
1715 case 'post':
1716 $sql_data[TOPICS_TABLE]['sql'] = array(
1717 'topic_poster' => (int) $user->data['user_id'],
1718 'topic_time' => $current_time,
1719 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1720 'icon_id' => $data['icon_id'],
1721 'topic_approved' => $post_approval,
1722 'topic_title' => $subject,
1723 'topic_first_poster_name' => (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
1724 'topic_first_poster_colour' => $user->data['user_colour'],
1725 'topic_type' => $topic_type,
1726 'topic_time_limit' => ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
1727 'topic_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
1730 if (isset($poll['poll_options']) && !empty($poll['poll_options']))
1732 $sql_data[TOPICS_TABLE]['sql'] = array_merge($sql_data[TOPICS_TABLE]['sql'], array(
1733 'poll_title' => $poll['poll_title'],
1734 'poll_start' => ($poll['poll_start']) ? $poll['poll_start'] : $current_time,
1735 'poll_max_options' => $poll['poll_max_options'],
1736 'poll_length' => ($poll['poll_length'] * 86400),
1737 'poll_vote_change' => $poll['poll_vote_change'])
1741 $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' : '');
1743 if ($topic_type != POST_GLOBAL)
1745 if ($post_approval)
1747 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
1749 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($post_approval) ? ', forum_topics = forum_topics + 1' : '');
1751 break;
1753 case 'reply':
1754 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_replies_real = topic_replies_real + 1, topic_bumped = 0, topic_bumper = 0' . (($post_approval) ? ', topic_replies = topic_replies + 1' : '') . ((!empty($data['attachment_data']) || (isset($data['topic_attachment']) && $data['topic_attachment'])) ? ', topic_attachment = 1' : '');
1755 $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' : '');
1757 if ($post_approval && $topic_type != POST_GLOBAL)
1759 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
1761 break;
1763 case 'edit_topic':
1764 case 'edit_first_post':
1766 $sql_data[TOPICS_TABLE]['sql'] = array(
1767 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1768 'icon_id' => $data['icon_id'],
1769 'topic_approved' => (!$post_approval) ? 0 : $data['topic_approved'],
1770 'topic_title' => $subject,
1771 'topic_first_poster_name' => $username,
1772 'topic_type' => $topic_type,
1773 'topic_time_limit' => ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
1774 'poll_title' => (isset($poll['poll_options'])) ? $poll['poll_title'] : '',
1775 'poll_start' => (isset($poll['poll_options'])) ? (($poll['poll_start']) ? $poll['poll_start'] : $current_time) : 0,
1776 'poll_max_options' => (isset($poll['poll_options'])) ? $poll['poll_max_options'] : 1,
1777 'poll_length' => (isset($poll['poll_options'])) ? ($poll['poll_length'] * 86400) : 0,
1778 'poll_vote_change' => (isset($poll['poll_vote_change'])) ? $poll['poll_vote_change'] : 0,
1780 'topic_attachment' => (!empty($data['attachment_data'])) ? 1 : (isset($data['topic_attachment']) ? $data['topic_attachment'] : 0),
1783 // Correctly set back the topic replies and forum posts... only if the topic was approved before and now gets disapproved
1784 if (!$post_approval && $data['topic_approved'])
1786 // Do we need to grab some topic informations?
1787 if (!sizeof($topic_row))
1789 $sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_approved
1790 FROM ' . TOPICS_TABLE . '
1791 WHERE topic_id = ' . $data['topic_id'];
1792 $result = $db->sql_query($sql);
1793 $topic_row = $db->sql_fetchrow($result);
1794 $db->sql_freeresult($result);
1797 // If this is the only post remaining we do not need to decrement topic_replies.
1798 // Also do not decrement if first post - then the topic_replies will not be adjusted if approving the topic again.
1800 // If this is an edited topic or the first post the topic gets completely disapproved later on...
1801 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics = forum_topics - 1';
1802 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - ' . ($topic_row['topic_replies'] + 1);
1804 set_config('num_topics', phpbb::$config['num_topics'] - 1, true);
1805 set_config('num_posts', phpbb::$config['num_posts'] - ($topic_row['topic_replies'] + 1), true);
1807 // Only decrement this post, since this is the one non-approved now
1808 if ($auth->acl_get('f_postcount', $data['forum_id']))
1810 $sql_data[USERS_TABLE]['stat'][] = 'user_posts = user_posts - 1';
1814 break;
1816 case 'edit':
1817 case 'edit_last_post':
1819 // Correctly set back the topic replies and forum posts... but only if the post was approved before.
1820 if (!$post_approval && $data['post_approved'])
1822 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_replies = topic_replies - 1';
1823 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - 1';
1825 set_config('num_posts', phpbb::$config['num_posts'] - 1, true);
1827 if ($auth->acl_get('f_postcount', $data['forum_id']))
1829 $sql_data[USERS_TABLE]['stat'][] = 'user_posts = user_posts - 1';
1833 break;
1836 // Submit new topic
1837 if ($post_mode == 'post')
1839 $sql = 'INSERT INTO ' . TOPICS_TABLE . ' ' .
1840 $db->sql_build_array('INSERT', $sql_data[TOPICS_TABLE]['sql']);
1841 $db->sql_query($sql);
1843 $data['topic_id'] = $db->sql_nextid();
1845 $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1846 'topic_id' => $data['topic_id'])
1848 unset($sql_data[TOPICS_TABLE]['sql']);
1851 // Submit new post
1852 if ($post_mode == 'post' || $post_mode == 'reply')
1854 if ($post_mode == 'reply')
1856 $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1857 'topic_id' => $data['topic_id'])
1861 $sql = 'INSERT INTO ' . POSTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_data[POSTS_TABLE]['sql']);
1862 $db->sql_query($sql);
1863 $data['post_id'] = $db->sql_nextid();
1865 if ($post_mode == 'post')
1867 $sql_data[TOPICS_TABLE]['sql'] = array(
1868 'topic_first_post_id' => $data['post_id'],
1869 'topic_last_post_id' => $data['post_id'],
1870 'topic_last_post_time' => $current_time,
1871 'topic_last_poster_id' => (int) $user->data['user_id'],
1872 'topic_last_poster_name' => (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
1873 'topic_last_poster_colour' => $user->data['user_colour'],
1874 'topic_last_post_subject' => (string) $subject,
1878 unset($sql_data[POSTS_TABLE]['sql']);
1881 $make_global = false;
1883 // Are we globalising or unglobalising?
1884 if ($post_mode == 'edit_first_post' || $post_mode == 'edit_topic')
1886 if (!sizeof($topic_row))
1888 $sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_approved, topic_last_post_id
1889 FROM ' . TOPICS_TABLE . '
1890 WHERE topic_id = ' . $data['topic_id'];
1891 $result = $db->sql_query($sql);
1892 $topic_row = $db->sql_fetchrow($result);
1893 $db->sql_freeresult($result);
1896 // globalise/unglobalise?
1897 if (($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL) || ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL))
1899 if (!empty($sql_data[FORUMS_TABLE]['stat']) && implode('', $sql_data[FORUMS_TABLE]['stat']))
1901 $db->sql_query('UPDATE ' . FORUMS_TABLE . ' SET ' . implode(', ', $sql_data[FORUMS_TABLE]['stat']) . ' WHERE forum_id = ' . $data['forum_id']);
1904 $make_global = true;
1905 $sql_data[FORUMS_TABLE]['stat'] = array();
1908 // globalise
1909 if ($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL)
1911 // Decrement topic/post count
1912 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - ' . ($topic_row['topic_replies_real'] + 1);
1913 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real - 1' . (($topic_row['topic_approved']) ? ', forum_topics = forum_topics - 1' : '');
1915 // Update forum_ids for all posts
1916 $sql = 'UPDATE ' . POSTS_TABLE . '
1917 SET forum_id = 0
1918 WHERE topic_id = ' . $data['topic_id'];
1919 $db->sql_query($sql);
1921 // unglobalise
1922 else if ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL)
1924 // Increment topic/post count
1925 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + ' . ($topic_row['topic_replies_real'] + 1);
1926 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($topic_row['topic_approved']) ? ', forum_topics = forum_topics + 1' : '');
1928 // Update forum_ids for all posts
1929 $sql = 'UPDATE ' . POSTS_TABLE . '
1930 SET forum_id = ' . $data['forum_id'] . '
1931 WHERE topic_id = ' . $data['topic_id'];
1932 $db->sql_query($sql);
1936 // Update the topics table
1937 if (isset($sql_data[TOPICS_TABLE]['sql']))
1939 $sql = 'UPDATE ' . TOPICS_TABLE . '
1940 SET ' . $db->sql_build_array('UPDATE', $sql_data[TOPICS_TABLE]['sql']) . '
1941 WHERE topic_id = ' . $data['topic_id'];
1942 $db->sql_query($sql);
1945 // Update the posts table
1946 if (isset($sql_data[POSTS_TABLE]['sql']))
1948 $sql = 'UPDATE ' . POSTS_TABLE . '
1949 SET ' . $db->sql_build_array('UPDATE', $sql_data[POSTS_TABLE]['sql']) . '
1950 WHERE post_id = ' . $data['post_id'];
1951 $db->sql_query($sql);
1954 // Update Poll Tables
1955 if (isset($poll['poll_options']) && !empty($poll['poll_options']))
1957 $cur_poll_options = array();
1959 if ($poll['poll_start'] && $mode == 'edit')
1961 $sql = 'SELECT *
1962 FROM ' . POLL_OPTIONS_TABLE . '
1963 WHERE topic_id = ' . $data['topic_id'] . '
1964 ORDER BY poll_option_id';
1965 $result = $db->sql_query($sql);
1967 $cur_poll_options = array();
1968 while ($row = $db->sql_fetchrow($result))
1970 $cur_poll_options[] = $row;
1972 $db->sql_freeresult($result);
1975 $sql_insert_ary = array();
1977 for ($i = 0, $size = sizeof($poll['poll_options']); $i < $size; $i++)
1979 if (strlen(trim($poll['poll_options'][$i])))
1981 if (empty($cur_poll_options[$i]))
1983 // If we add options we need to put them to the end to be able to preserve votes...
1984 $sql_insert_ary[] = array(
1985 'poll_option_id' => (int) sizeof($cur_poll_options) + 1 + sizeof($sql_insert_ary),
1986 'topic_id' => (int) $data['topic_id'],
1987 'poll_option_text' => (string) $poll['poll_options'][$i]
1990 else if ($poll['poll_options'][$i] != $cur_poll_options[$i])
1992 $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . "
1993 SET poll_option_text = '" . $db->sql_escape($poll['poll_options'][$i]) . "'
1994 WHERE poll_option_id = " . $cur_poll_options[$i]['poll_option_id'] . '
1995 AND topic_id = ' . $data['topic_id'];
1996 $db->sql_query($sql);
2001 $db->sql_multi_insert(POLL_OPTIONS_TABLE, $sql_insert_ary);
2003 if (sizeof($poll['poll_options']) < sizeof($cur_poll_options))
2005 $sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . '
2006 WHERE poll_option_id > ' . sizeof($poll['poll_options']) . '
2007 AND topic_id = ' . $data['topic_id'];
2008 $db->sql_query($sql);
2011 // 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
2012 if ($mode == 'edit' && sizeof($poll['poll_options']) != sizeof($cur_poll_options))
2014 $db->sql_query('DELETE FROM ' . POLL_VOTES_TABLE . ' WHERE topic_id = ' . $data['topic_id']);
2015 $db->sql_query('UPDATE ' . POLL_OPTIONS_TABLE . ' SET poll_option_total = 0 WHERE topic_id = ' . $data['topic_id']);
2019 // Submit Attachments
2020 if (!empty($data['attachment_data']) && $data['post_id'] && in_array($mode, array('post', 'reply', 'quote', 'edit')))
2022 $space_taken = $files_added = 0;
2023 $orphan_rows = array();
2025 foreach ($data['attachment_data'] as $pos => $attach_row)
2027 $orphan_rows[(int) $attach_row['attach_id']] = array();
2030 if (sizeof($orphan_rows))
2032 $sql = 'SELECT attach_id, filesize, physical_filename
2033 FROM ' . ATTACHMENTS_TABLE . '
2034 WHERE ' . $db->sql_in_set('attach_id', array_keys($orphan_rows)) . '
2035 AND is_orphan = 1
2036 AND poster_id = ' . $user->data['user_id'];
2037 $result = $db->sql_query($sql);
2039 $orphan_rows = array();
2040 while ($row = $db->sql_fetchrow($result))
2042 $orphan_rows[$row['attach_id']] = $row;
2044 $db->sql_freeresult($result);
2047 foreach ($data['attachment_data'] as $pos => $attach_row)
2049 if ($attach_row['is_orphan'] && !isset($orphan_rows[$attach_row['attach_id']]))
2051 continue;
2054 if (!$attach_row['is_orphan'])
2056 // update entry in db if attachment already stored in db and filespace
2057 $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
2058 SET attach_comment = '" . $db->sql_escape($attach_row['attach_comment']) . "'
2059 WHERE attach_id = " . (int) $attach_row['attach_id'] . '
2060 AND is_orphan = 0';
2061 $db->sql_query($sql);
2063 else
2065 // insert attachment into db
2066 if (!@file_exists(PHPBB_ROOT_PATH . phpbb::$config['upload_path'] . '/' . basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
2068 continue;
2071 $space_taken += $orphan_rows[$attach_row['attach_id']]['filesize'];
2072 $files_added++;
2074 $attach_sql = array(
2075 'post_msg_id' => $data['post_id'],
2076 'topic_id' => $data['topic_id'],
2077 'is_orphan' => 0,
2078 'poster_id' => $poster_id,
2079 'attach_comment' => $attach_row['attach_comment'],
2082 $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $attach_sql) . '
2083 WHERE attach_id = ' . $attach_row['attach_id'] . '
2084 AND is_orphan = 1
2085 AND poster_id = ' . $user->data['user_id'];
2086 $db->sql_query($sql);
2090 if ($space_taken && $files_added)
2092 set_config('upload_dir_size', phpbb::$config['upload_dir_size'] + $space_taken, true);
2093 set_config('num_files', phpbb::$config['num_files'] + $files_added, true);
2097 // we need to update the last forum information
2098 // only applicable if the topic is not global and it is approved
2099 // we also check to make sure we are not dealing with globaling the latest topic (pretty rare but still needs to be checked)
2100 if ($topic_type != POST_GLOBAL && !$make_global && ($post_approved != $data['post_approved']))
2102 // the last post makes us update the forum table. This can happen if...
2103 // We make a new topic
2104 // We reply to a topic
2105 // We edit the last post in a topic and this post is the latest in the forum (maybe)
2106 // We edit the only post in the topic
2107 // We edit the first post in the topic and all the other posts are not approved
2108 if (($post_mode == 'post' || $post_mode == 'reply') && $post_approved)
2110 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . $data['post_id'];
2111 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($subject) . "'";
2112 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . $current_time;
2113 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $user->data['user_id'];
2114 $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'] : '')) . "'";
2115 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($user->data['user_colour']) . "'";
2117 else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies']))
2119 // this does not _necessarily_ mean that we must update the info again,
2120 // it just means that we might have to
2121 $sql = 'SELECT forum_last_post_id, forum_last_post_subject
2122 FROM ' . FORUMS_TABLE . '
2123 WHERE forum_id = ' . (int) $data['forum_id'];
2124 $result = $db->sql_query($sql);
2125 $row = $db->sql_fetchrow($result);
2126 $db->sql_freeresult($result);
2128 // this post is the latest post in the forum, better update
2129 if ($row['forum_last_post_id'] == $data['post_id'])
2131 // If post approved and subject changed, or poster is anonymous, we need to update the forum_last* rows
2132 if ($post_approved && ($row['forum_last_post_subject'] !== $subject || $data['poster_id'] == ANONYMOUS))
2134 // the post's subject changed
2135 if ($row['forum_last_post_subject'] !== $subject)
2137 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_subject = \'' . $db->sql_escape($subject) . '\'';
2140 // Update the user name if poster is anonymous... just in case an admin changed it
2141 if ($data['poster_id'] == ANONYMOUS)
2143 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape($username) . "'";
2146 else if ($data['post_approved'] !== $post_approved)
2148 // we need a fresh change of socks, everything has become invalidated
2149 $sql = 'SELECT MAX(topic_last_post_id) as last_post_id
2150 FROM ' . TOPICS_TABLE . '
2151 WHERE forum_id = ' . (int) $data['forum_id'] . '
2152 AND topic_approved = 1';
2153 $result = $db->sql_query($sql);
2154 $row = $db->sql_fetchrow($result);
2155 $db->sql_freeresult($result);
2157 // any posts left in this forum?
2158 if (!empty($row['last_post_id']))
2160 $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
2161 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2162 WHERE p.poster_id = u.user_id
2163 AND p.post_id = ' . (int) $row['last_post_id'];
2164 $result = $db->sql_query($sql);
2165 $row = $db->sql_fetchrow($result);
2166 $db->sql_freeresult($result);
2168 // salvation, a post is found! jam it into the forums table
2169 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
2170 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
2171 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
2172 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
2173 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
2174 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
2176 else
2178 // just our luck, the last topic in the forum has just been turned unapproved...
2179 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = 0';
2180 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = ''";
2181 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = 0';
2182 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = 0';
2183 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = ''";
2184 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = ''";
2190 else if ($make_global)
2192 // somebody decided to be a party pooper, we must recalculate the whole shebang (maybe)
2193 $sql = 'SELECT forum_last_post_id
2194 FROM ' . FORUMS_TABLE . '
2195 WHERE forum_id = ' . (int) $data['forum_id'];
2196 $result = $db->sql_query($sql);
2197 $forum_row = $db->sql_fetchrow($result);
2198 $db->sql_freeresult($result);
2200 // we made a topic global, go get new data
2201 if ($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL && $forum_row['forum_last_post_id'] == $topic_row['topic_last_post_id'])
2203 // we need a fresh change of socks, everything has become invalidated
2204 $sql = 'SELECT MAX(topic_last_post_id) as last_post_id
2205 FROM ' . TOPICS_TABLE . '
2206 WHERE forum_id = ' . (int) $data['forum_id'] . '
2207 AND topic_approved = 1';
2208 $result = $db->sql_query($sql);
2209 $row = $db->sql_fetchrow($result);
2210 $db->sql_freeresult($result);
2212 // any posts left in this forum?
2213 if (!empty($row['last_post_id']))
2215 $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
2216 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2217 WHERE p.poster_id = u.user_id
2218 AND p.post_id = ' . (int) $row['last_post_id'];
2219 $result = $db->sql_query($sql);
2220 $row = $db->sql_fetchrow($result);
2221 $db->sql_freeresult($result);
2223 // salvation, a post is found! jam it into the forums table
2224 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
2225 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
2226 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
2227 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
2228 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
2229 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
2231 else
2233 // just our luck, the last topic in the forum has just been globalized...
2234 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = 0';
2235 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = ''";
2236 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = 0';
2237 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = 0';
2238 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = ''";
2239 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = ''";
2242 else if ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL && $forum_row['forum_last_post_id'] < $topic_row['topic_last_post_id'])
2244 // this post has a higher id, it is newer
2245 $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
2246 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2247 WHERE p.poster_id = u.user_id
2248 AND p.post_id = ' . (int) $topic_row['topic_last_post_id'];
2249 $result = $db->sql_query($sql);
2250 $row = $db->sql_fetchrow($result);
2251 $db->sql_freeresult($result);
2253 // salvation, a post is found! jam it into the forums table
2254 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
2255 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
2256 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
2257 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
2258 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
2259 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
2263 // topic sync time!
2264 // simply, we update if it is a reply or the last post is edited
2265 if ($post_approved)
2267 // reply requires the whole thing
2268 if ($post_mode == 'reply')
2270 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_id = ' . (int) $data['post_id'];
2271 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_poster_id = ' . (int) $user->data['user_id'];
2272 $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'] : '')) . "'";
2273 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_colour = '" . (($user->data['user_id'] != ANONYMOUS) ? $db->sql_escape($user->data['user_colour']) : '') . "'";
2274 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
2275 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_time = ' . (int) $current_time;
2277 else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies']))
2279 // only the subject can be changed from edit
2280 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
2282 // Maybe not only the subject, but also changing anonymous usernames. ;)
2283 if ($data['poster_id'] == ANONYMOUS)
2285 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape($username) . "'";
2289 else if (!$data['post_approved'] && ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies'])))
2291 // like having the rug pulled from under us
2292 $sql = 'SELECT MAX(post_id) as last_post_id
2293 FROM ' . POSTS_TABLE . '
2294 WHERE topic_id = ' . (int) $data['topic_id'] . '
2295 AND post_approved = 1';
2296 $result = $db->sql_query($sql);
2297 $row = $db->sql_fetchrow($result);
2298 $db->sql_freeresult($result);
2300 // any posts left in this forum?
2301 if (!empty($row['last_post_id']))
2303 $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
2304 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2305 WHERE p.poster_id = u.user_id
2306 AND p.post_id = ' . (int) $row['last_post_id'];
2307 $result = $db->sql_query($sql);
2308 $row = $db->sql_fetchrow($result);
2309 $db->sql_freeresult($result);
2311 // salvation, a post is found! jam it into the topics table
2312 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_id = ' . (int) $row['post_id'];
2313 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
2314 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_time = ' . (int) $row['post_time'];
2315 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_poster_id = ' . (int) $row['poster_id'];
2316 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
2317 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
2321 // Update total post count, do not consider moderated posts/topics
2322 if ($post_approval)
2324 if ($post_mode == 'post')
2326 set_config('num_topics', phpbb::$config['num_topics'] + 1, true);
2327 set_config('num_posts', phpbb::$config['num_posts'] + 1, true);
2330 if ($post_mode == 'reply')
2332 set_config('num_posts', phpbb::$config['num_posts'] + 1, true);
2336 // Update forum stats
2337 $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);
2339 foreach ($sql_data as $table => $update_ary)
2341 if (isset($update_ary['stat']) && implode('', $update_ary['stat']))
2343 $sql = "UPDATE $table SET " . implode(', ', $update_ary['stat']) . ' WHERE ' . $where_sql[$table];
2344 $db->sql_query($sql);
2348 // Delete topic shadows (if any exist). We do not need a shadow topic for an global announcement
2349 if ($make_global)
2351 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
2352 WHERE topic_moved_id = ' . $data['topic_id'];
2353 $db->sql_query($sql);
2356 // Committing the transaction before updating search index
2357 $db->sql_transaction('commit');
2359 // Delete draft if post was loaded...
2360 $draft_id = request_var('draft_loaded', 0);
2361 if ($draft_id)
2363 $sql = 'DELETE FROM ' . DRAFTS_TABLE . "
2364 WHERE draft_id = $draft_id
2365 AND user_id = {$user->data['user_id']}";
2366 $db->sql_query($sql);
2369 // Index message contents
2370 if ($update_message && $data['enable_indexing'])
2372 // Select the search method and do some additional checks to ensure it can actually be utilised
2373 $search_type = basename(phpbb::$config['search_type']);
2375 if (!file_exists(PHPBB_ROOT_PATH . 'includes/search/' . $search_type . '.' . PHP_EXT))
2377 trigger_error('NO_SUCH_SEARCH_MODULE');
2380 if (!class_exists($search_type))
2382 include(PHPBB_ROOT_PATH . "includes/search/$search_type." . PHP_EXT);
2385 $error = false;
2386 $search = new $search_type($error);
2388 if ($error)
2390 trigger_error($error);
2393 $search->index($mode, $data['post_id'], $data['message'], $subject, $poster_id, ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']);
2396 // Topic Notification, do not change if moderator is changing other users posts...
2397 if ($user->data['user_id'] == $poster_id)
2399 if (!$data['notify_set'] && $data['notify'])
2401 $sql = 'INSERT INTO ' . TOPICS_WATCH_TABLE . ' (user_id, topic_id)
2402 VALUES (' . $user->data['user_id'] . ', ' . $data['topic_id'] . ')';
2403 $db->sql_query($sql);
2405 else if ($data['notify_set'] && !$data['notify'])
2407 $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . '
2408 WHERE user_id = ' . $user->data['user_id'] . '
2409 AND topic_id = ' . $data['topic_id'];
2410 $db->sql_query($sql);
2414 if ($mode == 'post' || $mode == 'reply' || $mode == 'quote')
2416 // Mark this topic as posted to
2417 markread('post', $data['forum_id'], $data['topic_id'], $data['post_time']);
2420 // Mark this topic as read
2421 // We do not use post_time here, this is intended (post_time can have a date in the past if editing a message)
2422 markread('topic', $data['forum_id'], $data['topic_id'], time());
2425 if (phpbb::$config['load_db_lastread'] && $user->data['is_registered'])
2427 $sql = 'SELECT mark_time
2428 FROM ' . FORUMS_TRACK_TABLE . '
2429 WHERE user_id = ' . $user->data['user_id'] . '
2430 AND forum_id = ' . $data['forum_id'];
2431 $result = $db->sql_query($sql);
2432 $f_mark_time = (int) $db->sql_fetchfield('mark_time');
2433 $db->sql_freeresult($result);
2435 else if (phpbb::$config['load_anon_lastread'] || $user->data['is_registered'])
2437 $f_mark_time = false;
2440 if ((phpbb::$config['load_db_lastread'] && $user->data['is_registered']) || phpbb::$config['load_anon_lastread'] || $user->data['is_registered'])
2442 // Update forum info
2443 $sql = 'SELECT forum_last_post_time
2444 FROM ' . FORUMS_TABLE . '
2445 WHERE forum_id = ' . $data['forum_id'];
2446 $result = $db->sql_query($sql);
2447 $forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
2448 $db->sql_freeresult($result);
2450 update_forum_tracking_info($data['forum_id'], $forum_last_post_time, $f_mark_time, false);
2453 // Send Notifications
2454 if ($mode != 'edit' && $mode != 'delete' && $post_approval)
2456 user_notification($mode, $subject, $data['topic_title'], $data['forum_name'], $data['forum_id'], $data['topic_id'], $data['post_id']);
2459 $params = $add_anchor = '';
2461 if ($post_approval)
2463 $params .= '&amp;t=' . $data['topic_id'];
2465 if ($mode != 'post')
2467 $params .= '&amp;p=' . $data['post_id'];
2468 $add_anchor = '#p' . $data['post_id'];
2471 else if ($mode != 'post' && $post_mode != 'edit_first_post' && $post_mode != 'edit_topic')
2473 $params .= '&amp;t=' . $data['topic_id'];
2476 $url = (!$params) ? 'viewforum' : 'viewtopic';
2477 $url = append_sid($url, 'f=' . $data['forum_id'] . $params) . $add_anchor;
2479 return $url;