1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/functions_posting.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,2716 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package phpBB3
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +/**
1.23 +* Fill smiley templates (or just the variables) with smilies, either in a window or inline
1.24 +*/
1.25 +function generate_smilies($mode, $forum_id)
1.26 +{
1.27 + global $auth, $db, $user, $config, $template;
1.28 + global $phpEx, $phpbb_root_path;
1.29 +
1.30 + $start = request_var('start', 0);
1.31 +
1.32 + if ($mode == 'window')
1.33 + {
1.34 + if ($forum_id)
1.35 + {
1.36 + $sql = 'SELECT forum_style
1.37 + FROM ' . FORUMS_TABLE . "
1.38 + WHERE forum_id = $forum_id";
1.39 + $result = $db->sql_query_limit($sql, 1);
1.40 + $row = $db->sql_fetchrow($result);
1.41 + $db->sql_freeresult($result);
1.42 +
1.43 + $user->setup('posting', (int) $row['forum_style']);
1.44 + }
1.45 + else
1.46 + {
1.47 + $user->setup('posting');
1.48 + }
1.49 +
1.50 + page_header($user->lang['SMILIES']);
1.51 +
1.52 + $sql = 'SELECT COUNT(smiley_id) AS item_count
1.53 + FROM ' . SMILIES_TABLE . '
1.54 + GROUP BY smiley_url';
1.55 + $result = $db->sql_query($sql, 3600);
1.56 +
1.57 + $smiley_count = 0;
1.58 + while ($row = $db->sql_fetchrow($result))
1.59 + {
1.60 + ++$smiley_count;
1.61 + }
1.62 + $db->sql_freeresult($result);
1.63 +
1.64 + $template->set_filenames(array(
1.65 + 'body' => 'posting_smilies.html')
1.66 + );
1.67 +
1.68 + $template->assign_var('PAGINATION',
1.69 + generate_pagination(append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&f=' . $forum_id),
1.70 + $smiley_count, $config['smilies_per_page'], $start, true)
1.71 + );
1.72 + }
1.73 +
1.74 + $display_link = false;
1.75 + if ($mode == 'inline')
1.76 + {
1.77 + $sql = 'SELECT smiley_id
1.78 + FROM ' . SMILIES_TABLE . '
1.79 + WHERE display_on_posting = 0';
1.80 + $result = $db->sql_query_limit($sql, 1, 0, 3600);
1.81 +
1.82 + if ($row = $db->sql_fetchrow($result))
1.83 + {
1.84 + $display_link = true;
1.85 + }
1.86 + $db->sql_freeresult($result);
1.87 + }
1.88 +
1.89 + if ($mode == 'window')
1.90 + {
1.91 + $sql = 'SELECT smiley_url, MIN(emotion) as emotion, MIN(code) AS code, smiley_width, smiley_height, MIN(smiley_order) AS min_smiley_order
1.92 + FROM ' . SMILIES_TABLE . '
1.93 + GROUP BY smiley_url, smiley_width, smiley_height
1.94 + ORDER BY min_smiley_order';
1.95 + $result = $db->sql_query_limit($sql, $config['smilies_per_page'], $start, 3600);
1.96 + }
1.97 + else
1.98 + {
1.99 + $sql = 'SELECT *
1.100 + FROM ' . SMILIES_TABLE . '
1.101 + WHERE display_on_posting = 1
1.102 + ORDER BY smiley_order';
1.103 + $result = $db->sql_query($sql, 3600);
1.104 + }
1.105 +
1.106 + $smilies = array();
1.107 + while ($row = $db->sql_fetchrow($result))
1.108 + {
1.109 + if (empty($smilies[$row['smiley_url']]))
1.110 + {
1.111 + $smilies[$row['smiley_url']] = $row;
1.112 + }
1.113 + }
1.114 + $db->sql_freeresult($result);
1.115 +
1.116 + if (sizeof($smilies))
1.117 + {
1.118 + $root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_root_path;
1.119 +
1.120 + foreach ($smilies as $row)
1.121 + {
1.122 + $template->assign_block_vars('smiley', array(
1.123 + 'SMILEY_CODE' => $row['code'],
1.124 + 'A_SMILEY_CODE' => addslashes($row['code']),
1.125 + 'SMILEY_IMG' => $root_path . $config['smilies_path'] . '/' . $row['smiley_url'],
1.126 + 'SMILEY_WIDTH' => $row['smiley_width'],
1.127 + 'SMILEY_HEIGHT' => $row['smiley_height'],
1.128 + 'SMILEY_DESC' => $row['emotion'])
1.129 + );
1.130 + }
1.131 + }
1.132 +
1.133 + if ($mode == 'inline' && $display_link)
1.134 + {
1.135 + $template->assign_vars(array(
1.136 + 'S_SHOW_SMILEY_LINK' => true,
1.137 + 'U_MORE_SMILIES' => append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&f=' . $forum_id))
1.138 + );
1.139 + }
1.140 +
1.141 + if ($mode == 'window')
1.142 + {
1.143 + page_footer();
1.144 + }
1.145 +}
1.146 +
1.147 +/**
1.148 +* Update last post information
1.149 +* Should be used instead of sync() if only the last post information are out of sync... faster
1.150 +*
1.151 +* @param string $type Can be forum|topic
1.152 +* @param mixed $ids topic/forum ids
1.153 +* @param bool $return_update_sql true: SQL query shall be returned, false: execute SQL
1.154 +*/
1.155 +function update_post_information($type, $ids, $return_update_sql = false)
1.156 +{
1.157 + global $db;
1.158 +
1.159 + if (empty($ids))
1.160 + {
1.161 + return;
1.162 + }
1.163 + if (!is_array($ids))
1.164 + {
1.165 + $ids = array($ids);
1.166 + }
1.167 +
1.168 +
1.169 + $update_sql = $empty_forums = $not_empty_forums = array();
1.170 +
1.171 + if ($type != 'topic')
1.172 + {
1.173 + $topic_join = ', ' . TOPICS_TABLE . ' t';
1.174 + $topic_condition = 'AND t.topic_id = p.topic_id AND t.topic_approved = 1';
1.175 + }
1.176 + else
1.177 + {
1.178 + $topic_join = '';
1.179 + $topic_condition = '';
1.180 + }
1.181 +
1.182 + if (sizeof($ids) == 1)
1.183 + {
1.184 + $sql = 'SELECT MAX(p.post_id) as last_post_id
1.185 + FROM ' . POSTS_TABLE . " p $topic_join
1.186 + WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
1.187 + $topic_condition
1.188 + AND p.post_approved = 1";
1.189 + }
1.190 + else
1.191 + {
1.192 + $sql = 'SELECT p.' . $type . '_id, MAX(p.post_id) as last_post_id
1.193 + FROM ' . POSTS_TABLE . " p $topic_join
1.194 + WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
1.195 + $topic_condition
1.196 + AND p.post_approved = 1
1.197 + GROUP BY p.{$type}_id";
1.198 + }
1.199 + $result = $db->sql_query($sql);
1.200 +
1.201 + $last_post_ids = array();
1.202 + while ($row = $db->sql_fetchrow($result))
1.203 + {
1.204 + if (sizeof($ids) == 1)
1.205 + {
1.206 + $row[$type . '_id'] = $ids[0];
1.207 + }
1.208 +
1.209 + if ($type == 'forum')
1.210 + {
1.211 + $not_empty_forums[] = $row['forum_id'];
1.212 +
1.213 + if (empty($row['last_post_id']))
1.214 + {
1.215 + $empty_forums[] = $row['forum_id'];
1.216 + }
1.217 + }
1.218 +
1.219 + $last_post_ids[] = $row['last_post_id'];
1.220 + }
1.221 + $db->sql_freeresult($result);
1.222 +
1.223 + if ($type == 'forum')
1.224 + {
1.225 + $empty_forums = array_merge($empty_forums, array_diff($ids, $not_empty_forums));
1.226 +
1.227 + foreach ($empty_forums as $void => $forum_id)
1.228 + {
1.229 + $update_sql[$forum_id][] = 'forum_last_post_id = 0';
1.230 + $update_sql[$forum_id][] = "forum_last_post_subject = ''";
1.231 + $update_sql[$forum_id][] = 'forum_last_post_time = 0';
1.232 + $update_sql[$forum_id][] = 'forum_last_poster_id = 0';
1.233 + $update_sql[$forum_id][] = "forum_last_poster_name = ''";
1.234 + $update_sql[$forum_id][] = "forum_last_poster_colour = ''";
1.235 + }
1.236 + }
1.237 +
1.238 + if (sizeof($last_post_ids))
1.239 + {
1.240 + $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
1.241 + FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1.242 + WHERE p.poster_id = u.user_id
1.243 + AND ' . $db->sql_in_set('p.post_id', $last_post_ids);
1.244 + $result = $db->sql_query($sql);
1.245 +
1.246 + while ($row = $db->sql_fetchrow($result))
1.247 + {
1.248 + $update_sql[$row["{$type}_id"]][] = $type . '_last_post_id = ' . (int) $row['post_id'];
1.249 + $update_sql[$row["{$type}_id"]][] = "{$type}_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
1.250 + $update_sql[$row["{$type}_id"]][] = $type . '_last_post_time = ' . (int) $row['post_time'];
1.251 + $update_sql[$row["{$type}_id"]][] = $type . '_last_poster_id = ' . (int) $row['poster_id'];
1.252 + $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
1.253 + $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "'";
1.254 + }
1.255 + $db->sql_freeresult($result);
1.256 + }
1.257 + unset($empty_forums, $ids, $last_post_ids);
1.258 +
1.259 + if ($return_update_sql || !sizeof($update_sql))
1.260 + {
1.261 + return $update_sql;
1.262 + }
1.263 +
1.264 + $table = ($type == 'forum') ? FORUMS_TABLE : TOPICS_TABLE;
1.265 +
1.266 + foreach ($update_sql as $update_id => $update_sql_ary)
1.267 + {
1.268 + $sql = "UPDATE $table
1.269 + SET " . implode(', ', $update_sql_ary) . "
1.270 + WHERE {$type}_id = $update_id";
1.271 + $db->sql_query($sql);
1.272 + }
1.273 +
1.274 + return;
1.275 +}
1.276 +
1.277 +/**
1.278 +* Generate Topic Icons for display
1.279 +*/
1.280 +function posting_gen_topic_icons($mode, $icon_id)
1.281 +{
1.282 + global $phpbb_root_path, $config, $template, $cache;
1.283 +
1.284 + // Grab icons
1.285 + $icons = $cache->obtain_icons();
1.286 +
1.287 + if (!$icon_id)
1.288 + {
1.289 + $template->assign_var('S_NO_ICON_CHECKED', ' checked="checked"');
1.290 + }
1.291 +
1.292 + if (sizeof($icons))
1.293 + {
1.294 + foreach ($icons as $id => $data)
1.295 + {
1.296 + if ($data['display'])
1.297 + {
1.298 + $template->assign_block_vars('topic_icon', array(
1.299 + 'ICON_ID' => $id,
1.300 + 'ICON_IMG' => $phpbb_root_path . $config['icons_path'] . '/' . $data['img'],
1.301 + 'ICON_WIDTH' => $data['width'],
1.302 + 'ICON_HEIGHT' => $data['height'],
1.303 +
1.304 + 'S_CHECKED' => ($id == $icon_id) ? true : false,
1.305 + 'S_ICON_CHECKED' => ($id == $icon_id) ? ' checked="checked"' : '')
1.306 + );
1.307 + }
1.308 + }
1.309 +
1.310 + return true;
1.311 + }
1.312 +
1.313 + return false;
1.314 +}
1.315 +
1.316 +/**
1.317 +* Build topic types able to be selected
1.318 +*/
1.319 +function posting_gen_topic_types($forum_id, $cur_topic_type = POST_NORMAL)
1.320 +{
1.321 + global $auth, $user, $template, $topic_type;
1.322 +
1.323 + $toggle = false;
1.324 +
1.325 + $topic_types = array(
1.326 + 'sticky' => array('const' => POST_STICKY, 'lang' => 'POST_STICKY'),
1.327 + 'announce' => array('const' => POST_ANNOUNCE, 'lang' => 'POST_ANNOUNCEMENT'),
1.328 + 'global' => array('const' => POST_GLOBAL, 'lang' => 'POST_GLOBAL')
1.329 + );
1.330 +
1.331 + $topic_type_array = array();
1.332 +
1.333 + foreach ($topic_types as $auth_key => $topic_value)
1.334 + {
1.335 + // We do not have a special post global announcement permission
1.336 + $auth_key = ($auth_key == 'global') ? 'announce' : $auth_key;
1.337 +
1.338 + if ($auth->acl_get('f_' . $auth_key, $forum_id))
1.339 + {
1.340 + $toggle = true;
1.341 +
1.342 + $topic_type_array[] = array(
1.343 + 'VALUE' => $topic_value['const'],
1.344 + 'S_CHECKED' => ($cur_topic_type == $topic_value['const'] || ($forum_id == 0 && $topic_value['const'] == POST_GLOBAL)) ? ' checked="checked"' : '',
1.345 + 'L_TOPIC_TYPE' => $user->lang[$topic_value['lang']]
1.346 + );
1.347 + }
1.348 + }
1.349 +
1.350 + if ($toggle)
1.351 + {
1.352 + $topic_type_array = array_merge(array(0 => array(
1.353 + 'VALUE' => POST_NORMAL,
1.354 + 'S_CHECKED' => ($cur_topic_type == POST_NORMAL) ? ' checked="checked"' : '',
1.355 + 'L_TOPIC_TYPE' => $user->lang['POST_NORMAL'])),
1.356 +
1.357 + $topic_type_array
1.358 + );
1.359 +
1.360 + foreach ($topic_type_array as $array)
1.361 + {
1.362 + $template->assign_block_vars('topic_type', $array);
1.363 + }
1.364 +
1.365 + $template->assign_vars(array(
1.366 + 'S_TOPIC_TYPE_STICKY' => ($auth->acl_get('f_sticky', $forum_id)),
1.367 + 'S_TOPIC_TYPE_ANNOUNCE' => ($auth->acl_get('f_announce', $forum_id)))
1.368 + );
1.369 + }
1.370 +
1.371 + return $toggle;
1.372 +}
1.373 +
1.374 +//
1.375 +// Attachment related functions
1.376 +//
1.377 +
1.378 +/**
1.379 +* Upload Attachment - filedata is generated here
1.380 +* Uses upload class
1.381 +*/
1.382 +function upload_attachment($form_name, $forum_id, $local = false, $local_storage = '', $is_message = false, $local_filedata = false)
1.383 +{
1.384 + global $auth, $user, $config, $db, $cache;
1.385 + global $phpbb_root_path, $phpEx;
1.386 +
1.387 + $filedata = array(
1.388 + 'error' => array()
1.389 + );
1.390 +
1.391 + include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
1.392 + $upload = new fileupload();
1.393 +
1.394 + if ($config['check_attachment_content'] && isset($config['mime_triggers']))
1.395 + {
1.396 + $upload->set_disallowed_content(explode('|', $config['mime_triggers']));
1.397 + }
1.398 +
1.399 + if (!$local)
1.400 + {
1.401 + $filedata['post_attach'] = ($upload->is_valid($form_name)) ? true : false;
1.402 + }
1.403 + else
1.404 + {
1.405 + $filedata['post_attach'] = true;
1.406 + }
1.407 +
1.408 + if (!$filedata['post_attach'])
1.409 + {
1.410 + $filedata['error'][] = $user->lang['NO_UPLOAD_FORM_FOUND'];
1.411 + return $filedata;
1.412 + }
1.413 +
1.414 + $extensions = $cache->obtain_attach_extensions((($is_message) ? false : (int) $forum_id));
1.415 + $upload->set_allowed_extensions(array_keys($extensions['_allowed_']));
1.416 +
1.417 + $file = ($local) ? $upload->local_upload($local_storage, $local_filedata) : $upload->form_upload($form_name);
1.418 +
1.419 + if ($file->init_error)
1.420 + {
1.421 + $filedata['post_attach'] = false;
1.422 + return $filedata;
1.423 + }
1.424 +
1.425 + $cat_id = (isset($extensions[$file->get('extension')]['display_cat'])) ? $extensions[$file->get('extension')]['display_cat'] : ATTACHMENT_CATEGORY_NONE;
1.426 +
1.427 + // Make sure the image category only holds valid images...
1.428 + if ($cat_id == ATTACHMENT_CATEGORY_IMAGE && !$file->is_image())
1.429 + {
1.430 + $file->remove();
1.431 +
1.432 + // If this error occurs a user tried to exploit an IE Bug by renaming extensions
1.433 + // Since the image category is displaying content inline we need to catch this.
1.434 + trigger_error($user->lang['ATTACHED_IMAGE_NOT_IMAGE']);
1.435 + }
1.436 +
1.437 + // Do we have to create a thumbnail?
1.438 + $filedata['thumbnail'] = ($cat_id == ATTACHMENT_CATEGORY_IMAGE && $config['img_create_thumbnail']) ? 1 : 0;
1.439 +
1.440 + // Check Image Size, if it is an image
1.441 + if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id) && $cat_id == ATTACHMENT_CATEGORY_IMAGE)
1.442 + {
1.443 + $file->upload->set_allowed_dimensions(0, 0, $config['img_max_width'], $config['img_max_height']);
1.444 + }
1.445 +
1.446 + // Admins and mods are allowed to exceed the allowed filesize
1.447 + if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id))
1.448 + {
1.449 + if (!empty($extensions[$file->get('extension')]['max_filesize']))
1.450 + {
1.451 + $allowed_filesize = $extensions[$file->get('extension')]['max_filesize'];
1.452 + }
1.453 + else
1.454 + {
1.455 + $allowed_filesize = ($is_message) ? $config['max_filesize_pm'] : $config['max_filesize'];
1.456 + }
1.457 +
1.458 + $file->upload->set_max_filesize($allowed_filesize);
1.459 + }
1.460 +
1.461 + $file->clean_filename('unique', $user->data['user_id'] . '_');
1.462 +
1.463 + // Are we uploading an image *and* this image being within the image category? Only then perform additional image checks.
1.464 + $no_image = ($cat_id == ATTACHMENT_CATEGORY_IMAGE) ? false : true;
1.465 +
1.466 + $file->move_file($config['upload_path'], false, $no_image);
1.467 +
1.468 + if (sizeof($file->error))
1.469 + {
1.470 + $file->remove();
1.471 + $filedata['error'] = array_merge($filedata['error'], $file->error);
1.472 + $filedata['post_attach'] = false;
1.473 +
1.474 + return $filedata;
1.475 + }
1.476 +
1.477 + $filedata['filesize'] = $file->get('filesize');
1.478 + $filedata['mimetype'] = $file->get('mimetype');
1.479 + $filedata['extension'] = $file->get('extension');
1.480 + $filedata['physical_filename'] = $file->get('realname');
1.481 + $filedata['real_filename'] = $file->get('uploadname');
1.482 + $filedata['filetime'] = time();
1.483 +
1.484 + // Check our complete quota
1.485 + if ($config['attachment_quota'])
1.486 + {
1.487 + if ($config['upload_dir_size'] + $file->get('filesize') > $config['attachment_quota'])
1.488 + {
1.489 + $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
1.490 + $filedata['post_attach'] = false;
1.491 +
1.492 + $file->remove();
1.493 +
1.494 + return $filedata;
1.495 + }
1.496 + }
1.497 +
1.498 + // Check free disk space
1.499 + if ($free_space = @disk_free_space($phpbb_root_path . $config['upload_path']))
1.500 + {
1.501 + if ($free_space <= $file->get('filesize'))
1.502 + {
1.503 + $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
1.504 + $filedata['post_attach'] = false;
1.505 +
1.506 + $file->remove();
1.507 +
1.508 + return $filedata;
1.509 + }
1.510 + }
1.511 +
1.512 + // Create Thumbnail
1.513 + if ($filedata['thumbnail'])
1.514 + {
1.515 + $source = $file->get('destination_file');
1.516 + $destination = $file->get('destination_path') . '/thumb_' . $file->get('realname');
1.517 +
1.518 + if (!create_thumbnail($source, $destination, $file->get('mimetype')))
1.519 + {
1.520 + $filedata['thumbnail'] = 0;
1.521 + }
1.522 + }
1.523 +
1.524 + return $filedata;
1.525 +}
1.526 +
1.527 +/**
1.528 +* Calculate the needed size for Thumbnail
1.529 +*/
1.530 +function get_img_size_format($width, $height)
1.531 +{
1.532 + global $config;
1.533 +
1.534 + // Maximum Width the Image can take
1.535 + $max_width = ($config['img_max_thumb_width']) ? $config['img_max_thumb_width'] : 400;
1.536 +
1.537 + if ($width > $height)
1.538 + {
1.539 + return array(
1.540 + round($width * ($max_width / $width)),
1.541 + round($height * ($max_width / $width))
1.542 + );
1.543 + }
1.544 + else
1.545 + {
1.546 + return array(
1.547 + round($width * ($max_width / $height)),
1.548 + round($height * ($max_width / $height))
1.549 + );
1.550 + }
1.551 +}
1.552 +
1.553 +/**
1.554 +* Return supported image types
1.555 +*/
1.556 +function get_supported_image_types($type = false)
1.557 +{
1.558 + if (@extension_loaded('gd'))
1.559 + {
1.560 + $format = imagetypes();
1.561 + $new_type = 0;
1.562 +
1.563 + if ($type !== false)
1.564 + {
1.565 + // Type is one of the IMAGETYPE constants - it is fetched from getimagesize()
1.566 + // We do not use the constants here, because some were not available in PHP 4.3.x
1.567 + switch ($type)
1.568 + {
1.569 + // GIF
1.570 + case 1:
1.571 + $new_type = ($format & IMG_GIF) ? IMG_GIF : false;
1.572 + break;
1.573 +
1.574 + // JPG, JPC, JP2
1.575 + case 2:
1.576 + case 9:
1.577 + case 10:
1.578 + case 11:
1.579 + case 12:
1.580 + $new_type = ($format & IMG_JPG) ? IMG_JPG : false;
1.581 + break;
1.582 +
1.583 + // PNG
1.584 + case 3:
1.585 + $new_type = ($format & IMG_PNG) ? IMG_PNG : false;
1.586 + break;
1.587 +
1.588 + // WBMP
1.589 + case 15:
1.590 + $new_type = ($format & IMG_WBMP) ? IMG_WBMP : false;
1.591 + break;
1.592 + }
1.593 + }
1.594 + else
1.595 + {
1.596 + $new_type = array();
1.597 + $go_through_types = array(IMG_GIF, IMG_JPG, IMG_PNG, IMG_WBMP);
1.598 +
1.599 + foreach ($go_through_types as $check_type)
1.600 + {
1.601 + if ($format & $check_type)
1.602 + {
1.603 + $new_type[] = $check_type;
1.604 + }
1.605 + }
1.606 + }
1.607 +
1.608 + return array(
1.609 + 'gd' => ($new_type) ? true : false,
1.610 + 'format' => $new_type,
1.611 + 'version' => (function_exists('imagecreatetruecolor')) ? 2 : 1
1.612 + );
1.613 + }
1.614 +
1.615 + return array('gd' => false);
1.616 +}
1.617 +
1.618 +/**
1.619 +* Create Thumbnail
1.620 +*/
1.621 +function create_thumbnail($source, $destination, $mimetype)
1.622 +{
1.623 + global $config;
1.624 +
1.625 + $min_filesize = (int) $config['img_min_thumb_filesize'];
1.626 + $img_filesize = (file_exists($source)) ? @filesize($source) : false;
1.627 +
1.628 + if (!$img_filesize || $img_filesize <= $min_filesize)
1.629 + {
1.630 + return false;
1.631 + }
1.632 +
1.633 + $dimension = @getimagesize($source);
1.634 +
1.635 + if ($dimension === false)
1.636 + {
1.637 + return false;
1.638 + }
1.639 +
1.640 + list($width, $height, $type, ) = $dimension;
1.641 +
1.642 + if (empty($width) || empty($height))
1.643 + {
1.644 + return false;
1.645 + }
1.646 +
1.647 + list($new_width, $new_height) = get_img_size_format($width, $height);
1.648 +
1.649 + // Do not create a thumbnail if the resulting width/height is bigger than the original one
1.650 + if ($new_width >= $width && $new_height >= $height)
1.651 + {
1.652 + return false;
1.653 + }
1.654 +
1.655 + $used_imagick = false;
1.656 +
1.657 + // Only use imagemagick if defined and the passthru function not disabled
1.658 + if ($config['img_imagick'] && function_exists('passthru'))
1.659 + {
1.660 + if (substr($config['img_imagick'], -1) !== '/')
1.661 + {
1.662 + $config['img_imagick'] .= '/';
1.663 + }
1.664 +
1.665 + @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) . '"');
1.666 +
1.667 + if (file_exists($destination))
1.668 + {
1.669 + $used_imagick = true;
1.670 + }
1.671 + }
1.672 +
1.673 + if (!$used_imagick)
1.674 + {
1.675 + $type = get_supported_image_types($type);
1.676 +
1.677 + if ($type['gd'])
1.678 + {
1.679 + // If the type is not supported, we are not able to create a thumbnail
1.680 + if ($type['format'] === false)
1.681 + {
1.682 + return false;
1.683 + }
1.684 +
1.685 + switch ($type['format'])
1.686 + {
1.687 + case IMG_GIF:
1.688 + $image = @imagecreatefromgif($source);
1.689 + break;
1.690 +
1.691 + case IMG_JPG:
1.692 + @ini_set('gd.jpeg_ignore_warning', 1);
1.693 + $image = @imagecreatefromjpeg($source);
1.694 + break;
1.695 +
1.696 + case IMG_PNG:
1.697 + $image = @imagecreatefrompng($source);
1.698 + break;
1.699 +
1.700 + case IMG_WBMP:
1.701 + $image = @imagecreatefromwbmp($source);
1.702 + break;
1.703 + }
1.704 +
1.705 + if (empty($image))
1.706 + {
1.707 + return false;
1.708 + }
1.709 +
1.710 + if ($type['version'] == 1)
1.711 + {
1.712 + $new_image = imagecreate($new_width, $new_height);
1.713 +
1.714 + if ($new_image === false)
1.715 + {
1.716 + return false;
1.717 + }
1.718 +
1.719 + imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
1.720 + }
1.721 + else
1.722 + {
1.723 + $new_image = imagecreatetruecolor($new_width, $new_height);
1.724 +
1.725 + if ($new_image === false)
1.726 + {
1.727 + return false;
1.728 + }
1.729 +
1.730 + // Preserve alpha transparency (png for example)
1.731 + @imagealphablending($new_image, false);
1.732 + @imagesavealpha($new_image, true);
1.733 +
1.734 + imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
1.735 + }
1.736 +
1.737 + // If we are in safe mode create the destination file prior to using the gd functions to circumvent a PHP bug
1.738 + if (@ini_get('safe_mode') || @strtolower(ini_get('safe_mode')) == 'on')
1.739 + {
1.740 + @touch($destination);
1.741 + }
1.742 +
1.743 + switch ($type['format'])
1.744 + {
1.745 + case IMG_GIF:
1.746 + imagegif($new_image, $destination);
1.747 + break;
1.748 +
1.749 + case IMG_JPG:
1.750 + imagejpeg($new_image, $destination, 90);
1.751 + break;
1.752 +
1.753 + case IMG_PNG:
1.754 + imagepng($new_image, $destination);
1.755 + break;
1.756 +
1.757 + case IMG_WBMP:
1.758 + imagewbmp($new_image, $destination);
1.759 + break;
1.760 + }
1.761 +
1.762 + imagedestroy($new_image);
1.763 + }
1.764 + else
1.765 + {
1.766 + return false;
1.767 + }
1.768 + }
1.769 +
1.770 + if (!file_exists($destination))
1.771 + {
1.772 + return false;
1.773 + }
1.774 +
1.775 + phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);
1.776 +
1.777 + return true;
1.778 +}
1.779 +
1.780 +/**
1.781 +* Assign Inline attachments (build option fields)
1.782 +*/
1.783 +function posting_gen_inline_attachments(&$attachment_data)
1.784 +{
1.785 + global $template;
1.786 +
1.787 + if (sizeof($attachment_data))
1.788 + {
1.789 + $s_inline_attachment_options = '';
1.790 +
1.791 + foreach ($attachment_data as $i => $attachment)
1.792 + {
1.793 + $s_inline_attachment_options .= '<option value="' . $i . '">' . utf8_basename($attachment['real_filename']) . '</option>';
1.794 + }
1.795 +
1.796 + $template->assign_var('S_INLINE_ATTACHMENT_OPTIONS', $s_inline_attachment_options);
1.797 +
1.798 + return true;
1.799 + }
1.800 +
1.801 + return false;
1.802 +}
1.803 +
1.804 +/**
1.805 +* Generate inline attachment entry
1.806 +*/
1.807 +function posting_gen_attachment_entry($attachment_data, &$filename_data, $show_attach_box = true)
1.808 +{
1.809 + global $template, $config, $phpbb_root_path, $phpEx, $user, $auth;
1.810 +
1.811 + // Some default template variables
1.812 + $template->assign_vars(array(
1.813 + 'S_SHOW_ATTACH_BOX' => $show_attach_box,
1.814 + 'S_HAS_ATTACHMENTS' => sizeof($attachment_data),
1.815 + 'FILESIZE' => $config['max_filesize'],
1.816 + 'FILE_COMMENT' => (isset($filename_data['filecomment'])) ? $filename_data['filecomment'] : '',
1.817 + ));
1.818 +
1.819 + if (sizeof($attachment_data))
1.820 + {
1.821 + // We display the posted attachments within the desired order.
1.822 + ($config['display_order']) ? krsort($attachment_data) : ksort($attachment_data);
1.823 +
1.824 + foreach ($attachment_data as $count => $attach_row)
1.825 + {
1.826 + $hidden = '';
1.827 + $attach_row['real_filename'] = utf8_basename($attach_row['real_filename']);
1.828 +
1.829 + foreach ($attach_row as $key => $value)
1.830 + {
1.831 + $hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />';
1.832 + }
1.833 +
1.834 + $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);
1.835 +
1.836 + $template->assign_block_vars('attach_row', array(
1.837 + 'FILENAME' => utf8_basename($attach_row['real_filename']),
1.838 + 'A_FILENAME' => addslashes(utf8_basename($attach_row['real_filename'])),
1.839 + 'FILE_COMMENT' => $attach_row['attach_comment'],
1.840 + 'ATTACH_ID' => $attach_row['attach_id'],
1.841 + 'S_IS_ORPHAN' => $attach_row['is_orphan'],
1.842 + 'ASSOC_INDEX' => $count,
1.843 +
1.844 + 'U_VIEW_ATTACHMENT' => $download_link,
1.845 + 'S_HIDDEN' => $hidden)
1.846 + );
1.847 + }
1.848 + }
1.849 +
1.850 + return sizeof($attachment_data);
1.851 +}
1.852 +
1.853 +//
1.854 +// General Post functions
1.855 +//
1.856 +
1.857 +/**
1.858 +* Load Drafts
1.859 +*/
1.860 +function load_drafts($topic_id = 0, $forum_id = 0, $id = 0, $pm_action = '', $msg_id = 0)
1.861 +{
1.862 + global $user, $db, $template, $auth;
1.863 + global $phpbb_root_path, $phpEx;
1.864 +
1.865 + $topic_ids = $forum_ids = $draft_rows = array();
1.866 +
1.867 + // Load those drafts not connected to forums/topics
1.868 + // If forum_id == 0 AND topic_id == 0 then this is a PM draft
1.869 + if (!$topic_id && !$forum_id)
1.870 + {
1.871 + $sql_and = ' AND d.forum_id = 0 AND d.topic_id = 0';
1.872 + }
1.873 + else
1.874 + {
1.875 + $sql_and = '';
1.876 + $sql_and .= ($forum_id) ? ' AND d.forum_id = ' . (int) $forum_id : '';
1.877 + $sql_and .= ($topic_id) ? ' AND d.topic_id = ' . (int) $topic_id : '';
1.878 + }
1.879 +
1.880 + $sql = 'SELECT d.*, f.forum_id, f.forum_name
1.881 + FROM ' . DRAFTS_TABLE . ' d
1.882 + LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = d.forum_id)
1.883 + WHERE d.user_id = ' . $user->data['user_id'] . "
1.884 + $sql_and
1.885 + ORDER BY d.save_time DESC";
1.886 + $result = $db->sql_query($sql);
1.887 +
1.888 + while ($row = $db->sql_fetchrow($result))
1.889 + {
1.890 + if ($row['topic_id'])
1.891 + {
1.892 + $topic_ids[] = (int) $row['topic_id'];
1.893 + }
1.894 + $draft_rows[] = $row;
1.895 + }
1.896 + $db->sql_freeresult($result);
1.897 +
1.898 + if (!sizeof($draft_rows))
1.899 + {
1.900 + return;
1.901 + }
1.902 +
1.903 + $topic_rows = array();
1.904 + if (sizeof($topic_ids))
1.905 + {
1.906 + $sql = 'SELECT topic_id, forum_id, topic_title
1.907 + FROM ' . TOPICS_TABLE . '
1.908 + WHERE ' . $db->sql_in_set('topic_id', array_unique($topic_ids));
1.909 + $result = $db->sql_query($sql);
1.910 +
1.911 + while ($row = $db->sql_fetchrow($result))
1.912 + {
1.913 + $topic_rows[$row['topic_id']] = $row;
1.914 + }
1.915 + $db->sql_freeresult($result);
1.916 + }
1.917 + unset($topic_ids);
1.918 +
1.919 + $template->assign_var('S_SHOW_DRAFTS', true);
1.920 +
1.921 + foreach ($draft_rows as $draft)
1.922 + {
1.923 + $link_topic = $link_forum = $link_pm = false;
1.924 + $insert_url = $view_url = $title = '';
1.925 +
1.926 + if (isset($topic_rows[$draft['topic_id']])
1.927 + && (
1.928 + ($topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_get('f_read', $topic_rows[$draft['topic_id']]['forum_id']))
1.929 + ||
1.930 + (!$topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_getf_global('f_read'))
1.931 + ))
1.932 + {
1.933 + $topic_forum_id = ($topic_rows[$draft['topic_id']]['forum_id']) ? $topic_rows[$draft['topic_id']]['forum_id'] : $forum_id;
1.934 +
1.935 + $link_topic = true;
1.936 + $view_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topic_forum_id . '&t=' . $draft['topic_id']);
1.937 + $title = $topic_rows[$draft['topic_id']]['topic_title'];
1.938 +
1.939 + $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $topic_forum_id . '&t=' . $draft['topic_id'] . '&mode=reply&d=' . $draft['draft_id']);
1.940 + }
1.941 + else if ($draft['forum_id'] && $auth->acl_get('f_read', $draft['forum_id']))
1.942 + {
1.943 + $link_forum = true;
1.944 + $view_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $draft['forum_id']);
1.945 + $title = $draft['forum_name'];
1.946 +
1.947 + $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $draft['forum_id'] . '&mode=post&d=' . $draft['draft_id']);
1.948 + }
1.949 + else
1.950 + {
1.951 + // Either display as PM draft if forum_id and topic_id are empty or if access to the forums has been denied afterwards...
1.952 + $link_pm = true;
1.953 + $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" : ''));
1.954 + }
1.955 +
1.956 + $template->assign_block_vars('draftrow', array(
1.957 + 'DRAFT_ID' => $draft['draft_id'],
1.958 + 'DATE' => $user->format_date($draft['save_time']),
1.959 + 'DRAFT_SUBJECT' => $draft['draft_subject'],
1.960 +
1.961 + 'TITLE' => $title,
1.962 + 'U_VIEW' => $view_url,
1.963 + 'U_INSERT' => $insert_url,
1.964 +
1.965 + 'S_LINK_PM' => $link_pm,
1.966 + 'S_LINK_TOPIC' => $link_topic,
1.967 + 'S_LINK_FORUM' => $link_forum)
1.968 + );
1.969 + }
1.970 +}
1.971 +
1.972 +/**
1.973 +* Topic Review
1.974 +*/
1.975 +function topic_review($topic_id, $forum_id, $mode = 'topic_review', $cur_post_id = 0, $show_quote_button = true)
1.976 +{
1.977 + global $user, $auth, $db, $template, $bbcode, $cache;
1.978 + global $config, $phpbb_root_path, $phpEx;
1.979 +
1.980 + // Go ahead and pull all data for this topic
1.981 + $sql = 'SELECT p.post_id
1.982 + FROM ' . POSTS_TABLE . ' p' . "
1.983 + WHERE p.topic_id = $topic_id
1.984 + " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . '
1.985 + ' . (($mode == 'post_review') ? " AND p.post_id > $cur_post_id" : '') . '
1.986 + ' . (($mode == 'post_review_edit') ? " AND p.post_id = $cur_post_id" : '') . '
1.987 + ORDER BY p.post_time ';
1.988 + $sql .= ($mode == 'post_review') ? 'ASC' : 'DESC';
1.989 + $result = $db->sql_query_limit($sql, $config['posts_per_page']);
1.990 +
1.991 + $post_list = array();
1.992 +
1.993 + while ($row = $db->sql_fetchrow($result))
1.994 + {
1.995 + $post_list[] = $row['post_id'];
1.996 + }
1.997 +
1.998 + $db->sql_freeresult($result);
1.999 +
1.1000 + if (!sizeof($post_list))
1.1001 + {
1.1002 + return false;
1.1003 + }
1.1004 +
1.1005 + // Handle 'post_review_edit' like 'post_review' from now on
1.1006 + if ($mode == 'post_review_edit')
1.1007 + {
1.1008 + $mode = 'post_review';
1.1009 + }
1.1010 +
1.1011 + $sql = $db->sql_build_query('SELECT', array(
1.1012 + 'SELECT' => 'u.username, u.user_id, u.user_colour, p.*, z.friend, z.foe',
1.1013 +
1.1014 + 'FROM' => array(
1.1015 + USERS_TABLE => 'u',
1.1016 + POSTS_TABLE => 'p',
1.1017 + ),
1.1018 +
1.1019 + 'LEFT_JOIN' => array(
1.1020 + array(
1.1021 + 'FROM' => array(ZEBRA_TABLE => 'z'),
1.1022 + 'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
1.1023 + )
1.1024 + ),
1.1025 +
1.1026 + 'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
1.1027 + AND u.user_id = p.poster_id'
1.1028 + ));
1.1029 +
1.1030 + $result = $db->sql_query($sql);
1.1031 +
1.1032 + $bbcode_bitfield = '';
1.1033 + $rowset = array();
1.1034 + $has_attachments = false;
1.1035 + while ($row = $db->sql_fetchrow($result))
1.1036 + {
1.1037 + $rowset[$row['post_id']] = $row;
1.1038 + $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
1.1039 +
1.1040 + if ($row['post_attachment'])
1.1041 + {
1.1042 + $has_attachments = true;
1.1043 + }
1.1044 + }
1.1045 + $db->sql_freeresult($result);
1.1046 +
1.1047 + // Instantiate BBCode class
1.1048 + if (!isset($bbcode) && $bbcode_bitfield !== '')
1.1049 + {
1.1050 + include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
1.1051 + $bbcode = new bbcode(base64_encode($bbcode_bitfield));
1.1052 + }
1.1053 +
1.1054 + // Grab extensions
1.1055 + $extensions = $attachments = array();
1.1056 + if ($has_attachments && $auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
1.1057 + {
1.1058 + $extensions = $cache->obtain_attach_extensions($forum_id);
1.1059 +
1.1060 + // Get attachments...
1.1061 + $sql = 'SELECT *
1.1062 + FROM ' . ATTACHMENTS_TABLE . '
1.1063 + WHERE ' . $db->sql_in_set('post_msg_id', $post_list) . '
1.1064 + AND in_message = 0
1.1065 + ORDER BY filetime DESC, post_msg_id ASC';
1.1066 + $result = $db->sql_query($sql);
1.1067 +
1.1068 + while ($row = $db->sql_fetchrow($result))
1.1069 + {
1.1070 + $attachments[$row['post_msg_id']][] = $row;
1.1071 + }
1.1072 + $db->sql_freeresult($result);
1.1073 + }
1.1074 +
1.1075 + for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
1.1076 + {
1.1077 + // A non-existing rowset only happens if there was no user present for the entered poster_id
1.1078 + // This could be a broken posts table.
1.1079 + if (!isset($rowset[$post_list[$i]]))
1.1080 + {
1.1081 + continue;
1.1082 + }
1.1083 +
1.1084 + $row =& $rowset[$post_list[$i]];
1.1085 +
1.1086 + $poster_id = $row['user_id'];
1.1087 + $post_subject = $row['post_subject'];
1.1088 + $message = censor_text($row['post_text']);
1.1089 +
1.1090 + $decoded_message = false;
1.1091 +
1.1092 + if ($show_quote_button && $auth->acl_get('f_reply', $forum_id))
1.1093 + {
1.1094 + $decoded_message = $message;
1.1095 + decode_message($decoded_message, $row['bbcode_uid']);
1.1096 +
1.1097 + $decoded_message = bbcode_nl2br($decoded_message);
1.1098 + }
1.1099 +
1.1100 + if ($row['bbcode_bitfield'])
1.1101 + {
1.1102 + $bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
1.1103 + }
1.1104 +
1.1105 + $message = bbcode_nl2br($message);
1.1106 + $message = smiley_text($message, !$row['enable_smilies']);
1.1107 +
1.1108 + if (!empty($attachments[$row['post_id']]))
1.1109 + {
1.1110 + $update_count = array();
1.1111 + parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
1.1112 + }
1.1113 +
1.1114 + $post_subject = censor_text($post_subject);
1.1115 +
1.1116 + $post_anchor = ($mode == 'post_review') ? 'ppr' . $row['post_id'] : 'pr' . $row['post_id'];
1.1117 + $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']}");
1.1118 +
1.1119 + $template->assign_block_vars($mode . '_row', array(
1.1120 + 'POST_AUTHOR_FULL' => get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1.1121 + 'POST_AUTHOR_COLOUR' => get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1.1122 + 'POST_AUTHOR' => get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1.1123 + 'U_POST_AUTHOR' => get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1.1124 +
1.1125 + 'S_HAS_ATTACHMENTS' => (!empty($attachments[$row['post_id']])) ? true : false,
1.1126 + 'S_FRIEND' => ($row['friend']) ? true : false,
1.1127 + 'S_IGNORE_POST' => ($row['foe']) ? true : false,
1.1128 + '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>') : '',
1.1129 +
1.1130 + 'POST_SUBJECT' => $post_subject,
1.1131 + 'MINI_POST_IMG' => $user->img('icon_post_target', $user->lang['POST']),
1.1132 + 'POST_DATE' => $user->format_date($row['post_time']),
1.1133 + 'MESSAGE' => $message,
1.1134 + 'DECODED_MESSAGE' => $decoded_message,
1.1135 + 'POST_ID' => $row['post_id'],
1.1136 + 'U_MINI_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . '#p' . $row['post_id'],
1.1137 + '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) : '',
1.1138 + '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'])) : '')
1.1139 + );
1.1140 +
1.1141 + // Display not already displayed Attachments for this post, we already parsed them. ;)
1.1142 + if (!empty($attachments[$row['post_id']]))
1.1143 + {
1.1144 + foreach ($attachments[$row['post_id']] as $attachment)
1.1145 + {
1.1146 + $template->assign_block_vars($mode . '_row.attachment', array(
1.1147 + 'DISPLAY_ATTACHMENT' => $attachment)
1.1148 + );
1.1149 + }
1.1150 + }
1.1151 +
1.1152 + unset($rowset[$post_list[$i]]);
1.1153 + }
1.1154 +
1.1155 + if ($mode == 'topic_review')
1.1156 + {
1.1157 + $template->assign_var('QUOTE_IMG', $user->img('icon_post_quote', $user->lang['REPLY_WITH_QUOTE']));
1.1158 + }
1.1159 +
1.1160 + return true;
1.1161 +}
1.1162 +
1.1163 +/**
1.1164 +* User Notification
1.1165 +*/
1.1166 +function user_notification($mode, $subject, $topic_title, $forum_name, $forum_id, $topic_id, $post_id)
1.1167 +{
1.1168 + global $db, $user, $config, $phpbb_root_path, $phpEx, $auth;
1.1169 +
1.1170 + $topic_notification = ($mode == 'reply' || $mode == 'quote') ? true : false;
1.1171 + $forum_notification = ($mode == 'post') ? true : false;
1.1172 +
1.1173 + if (!$topic_notification && !$forum_notification)
1.1174 + {
1.1175 + trigger_error('NO_MODE');
1.1176 + }
1.1177 +
1.1178 + if (($topic_notification && !$config['allow_topic_notify']) || ($forum_notification && !$config['allow_forum_notify']))
1.1179 + {
1.1180 + return;
1.1181 + }
1.1182 +
1.1183 + $topic_title = ($topic_notification) ? $topic_title : $subject;
1.1184 + $topic_title = censor_text($topic_title);
1.1185 +
1.1186 + // Get banned User ID's
1.1187 + $sql = 'SELECT ban_userid
1.1188 + FROM ' . BANLIST_TABLE . '
1.1189 + WHERE ban_userid <> 0
1.1190 + AND ban_exclude <> 1';
1.1191 + $result = $db->sql_query($sql);
1.1192 +
1.1193 + $sql_ignore_users = ANONYMOUS . ', ' . $user->data['user_id'];
1.1194 + while ($row = $db->sql_fetchrow($result))
1.1195 + {
1.1196 + $sql_ignore_users .= ', ' . (int) $row['ban_userid'];
1.1197 + }
1.1198 + $db->sql_freeresult($result);
1.1199 +
1.1200 + $notify_rows = array();
1.1201 +
1.1202 + // -- get forum_userids || topic_userids
1.1203 + $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
1.1204 + FROM ' . (($topic_notification) ? TOPICS_WATCH_TABLE : FORUMS_WATCH_TABLE) . ' w, ' . USERS_TABLE . ' u
1.1205 + WHERE w.' . (($topic_notification) ? 'topic_id' : 'forum_id') . ' = ' . (($topic_notification) ? $topic_id : $forum_id) . "
1.1206 + AND w.user_id NOT IN ($sql_ignore_users)
1.1207 + AND w.notify_status = " . NOTIFY_YES . '
1.1208 + AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')
1.1209 + AND u.user_id = w.user_id';
1.1210 + $result = $db->sql_query($sql);
1.1211 +
1.1212 + while ($row = $db->sql_fetchrow($result))
1.1213 + {
1.1214 + $notify_rows[$row['user_id']] = array(
1.1215 + 'user_id' => $row['user_id'],
1.1216 + 'username' => $row['username'],
1.1217 + 'user_email' => $row['user_email'],
1.1218 + 'user_jabber' => $row['user_jabber'],
1.1219 + 'user_lang' => $row['user_lang'],
1.1220 + 'notify_type' => ($topic_notification) ? 'topic' : 'forum',
1.1221 + 'template' => ($topic_notification) ? 'topic_notify' : 'newtopic_notify',
1.1222 + 'method' => $row['user_notify_type'],
1.1223 + 'allowed' => false
1.1224 + );
1.1225 + }
1.1226 + $db->sql_freeresult($result);
1.1227 +
1.1228 + // forum notification is sent to those not already receiving topic notifications
1.1229 + if ($topic_notification)
1.1230 + {
1.1231 + if (sizeof($notify_rows))
1.1232 + {
1.1233 + $sql_ignore_users .= ', ' . implode(', ', array_keys($notify_rows));
1.1234 + }
1.1235 +
1.1236 + $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
1.1237 + FROM ' . FORUMS_WATCH_TABLE . ' fw, ' . USERS_TABLE . " u
1.1238 + WHERE fw.forum_id = $forum_id
1.1239 + AND fw.user_id NOT IN ($sql_ignore_users)
1.1240 + AND fw.notify_status = " . NOTIFY_YES . '
1.1241 + AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')
1.1242 + AND u.user_id = fw.user_id';
1.1243 + $result = $db->sql_query($sql);
1.1244 +
1.1245 + while ($row = $db->sql_fetchrow($result))
1.1246 + {
1.1247 + $notify_rows[$row['user_id']] = array(
1.1248 + 'user_id' => $row['user_id'],
1.1249 + 'username' => $row['username'],
1.1250 + 'user_email' => $row['user_email'],
1.1251 + 'user_jabber' => $row['user_jabber'],
1.1252 + 'user_lang' => $row['user_lang'],
1.1253 + 'notify_type' => 'forum',
1.1254 + 'template' => 'forum_notify',
1.1255 + 'method' => $row['user_notify_type'],
1.1256 + 'allowed' => false
1.1257 + );
1.1258 + }
1.1259 + $db->sql_freeresult($result);
1.1260 + }
1.1261 +
1.1262 + if (!sizeof($notify_rows))
1.1263 + {
1.1264 + return;
1.1265 + }
1.1266 +
1.1267 + // Make sure users are allowed to read the forum
1.1268 + foreach ($auth->acl_get_list(array_keys($notify_rows), 'f_read', $forum_id) as $forum_id => $forum_ary)
1.1269 + {
1.1270 + foreach ($forum_ary as $auth_option => $user_ary)
1.1271 + {
1.1272 + foreach ($user_ary as $user_id)
1.1273 + {
1.1274 + $notify_rows[$user_id]['allowed'] = true;
1.1275 + }
1.1276 + }
1.1277 + }
1.1278 +
1.1279 +
1.1280 + // Now, we have to do a little step before really sending, we need to distinguish our users a little bit. ;)
1.1281 + $msg_users = $delete_ids = $update_notification = array();
1.1282 + foreach ($notify_rows as $user_id => $row)
1.1283 + {
1.1284 + if (!$row['allowed'] || !trim($row['user_email']))
1.1285 + {
1.1286 + $delete_ids[$row['notify_type']][] = $row['user_id'];
1.1287 + }
1.1288 + else
1.1289 + {
1.1290 + $msg_users[] = $row;
1.1291 + $update_notification[$row['notify_type']][] = $row['user_id'];
1.1292 + }
1.1293 + }
1.1294 + unset($notify_rows);
1.1295 +
1.1296 + // Now, we are able to really send out notifications
1.1297 + if (sizeof($msg_users))
1.1298 + {
1.1299 + include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
1.1300 + $messenger = new messenger();
1.1301 +
1.1302 + $msg_list_ary = array();
1.1303 + foreach ($msg_users as $row)
1.1304 + {
1.1305 + $pos = (!isset($msg_list_ary[$row['template']])) ? 0 : sizeof($msg_list_ary[$row['template']]);
1.1306 +
1.1307 + $msg_list_ary[$row['template']][$pos]['method'] = $row['method'];
1.1308 + $msg_list_ary[$row['template']][$pos]['email'] = $row['user_email'];
1.1309 + $msg_list_ary[$row['template']][$pos]['jabber'] = $row['user_jabber'];
1.1310 + $msg_list_ary[$row['template']][$pos]['name'] = $row['username'];
1.1311 + $msg_list_ary[$row['template']][$pos]['lang'] = $row['user_lang'];
1.1312 + $msg_list_ary[$row['template']][$pos]['user_id']= $row['user_id'];
1.1313 + }
1.1314 + unset($msg_users);
1.1315 +
1.1316 + foreach ($msg_list_ary as $email_template => $email_list)
1.1317 + {
1.1318 + foreach ($email_list as $addr)
1.1319 + {
1.1320 + $messenger->template($email_template, $addr['lang']);
1.1321 +
1.1322 + $messenger->to($addr['email'], $addr['name']);
1.1323 + $messenger->im($addr['jabber'], $addr['name']);
1.1324 +
1.1325 + $messenger->assign_vars(array(
1.1326 + 'USERNAME' => htmlspecialchars_decode($addr['name']),
1.1327 + 'TOPIC_TITLE' => htmlspecialchars_decode($topic_title),
1.1328 + 'FORUM_NAME' => htmlspecialchars_decode($forum_name),
1.1329 +
1.1330 + 'U_FORUM' => generate_board_url() . "/viewforum.$phpEx?f=$forum_id",
1.1331 + 'U_TOPIC' => generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id",
1.1332 + 'U_NEWEST_POST' => generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id&p=$post_id&e=$post_id",
1.1333 + 'U_STOP_WATCHING_TOPIC' => generate_board_url() . "/viewtopic.$phpEx?uid={$addr['user_id']}&f=$forum_id&t=$topic_id&unwatch=topic",
1.1334 + 'U_STOP_WATCHING_FORUM' => generate_board_url() . "/viewforum.$phpEx?uid={$addr['user_id']}&f=$forum_id&unwatch=forum",
1.1335 + ));
1.1336 +
1.1337 + $messenger->send($addr['method']);
1.1338 + }
1.1339 + }
1.1340 + unset($msg_list_ary);
1.1341 +
1.1342 + $messenger->save_queue();
1.1343 + }
1.1344 +
1.1345 + // Handle the DB updates
1.1346 + $db->sql_transaction('begin');
1.1347 +
1.1348 + if (!empty($update_notification['topic']))
1.1349 + {
1.1350 + $sql = 'UPDATE ' . TOPICS_WATCH_TABLE . '
1.1351 + SET notify_status = ' . NOTIFY_NO . "
1.1352 + WHERE topic_id = $topic_id
1.1353 + AND " . $db->sql_in_set('user_id', $update_notification['topic']);
1.1354 + $db->sql_query($sql);
1.1355 + }
1.1356 +
1.1357 + if (!empty($update_notification['forum']))
1.1358 + {
1.1359 + $sql = 'UPDATE ' . FORUMS_WATCH_TABLE . '
1.1360 + SET notify_status = ' . NOTIFY_NO . "
1.1361 + WHERE forum_id = $forum_id
1.1362 + AND " . $db->sql_in_set('user_id', $update_notification['forum']);
1.1363 + $db->sql_query($sql);
1.1364 + }
1.1365 +
1.1366 + // Now delete the user_ids not authorised to receive notifications on this topic/forum
1.1367 + if (!empty($delete_ids['topic']))
1.1368 + {
1.1369 + $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . "
1.1370 + WHERE topic_id = $topic_id
1.1371 + AND " . $db->sql_in_set('user_id', $delete_ids['topic']);
1.1372 + $db->sql_query($sql);
1.1373 + }
1.1374 +
1.1375 + if (!empty($delete_ids['forum']))
1.1376 + {
1.1377 + $sql = 'DELETE FROM ' . FORUMS_WATCH_TABLE . "
1.1378 + WHERE forum_id = $forum_id
1.1379 + AND " . $db->sql_in_set('user_id', $delete_ids['forum']);
1.1380 + $db->sql_query($sql);
1.1381 + }
1.1382 +
1.1383 + $db->sql_transaction('commit');
1.1384 +}
1.1385 +
1.1386 +//
1.1387 +// Post handling functions
1.1388 +//
1.1389 +
1.1390 +/**
1.1391 +* Delete Post
1.1392 +*/
1.1393 +function delete_post($forum_id, $topic_id, $post_id, &$data)
1.1394 +{
1.1395 + global $db, $user, $auth;
1.1396 + global $config, $phpEx, $phpbb_root_path;
1.1397 +
1.1398 + // Specify our post mode
1.1399 + $post_mode = 'delete';
1.1400 + if (($data['topic_first_post_id'] === $data['topic_last_post_id']) && $data['topic_replies_real'] == 0)
1.1401 + {
1.1402 + $post_mode = 'delete_topic';
1.1403 + }
1.1404 + else if ($data['topic_first_post_id'] == $post_id)
1.1405 + {
1.1406 + $post_mode = 'delete_first_post';
1.1407 + }
1.1408 + else if ($data['topic_last_post_id'] == $post_id)
1.1409 + {
1.1410 + $post_mode = 'delete_last_post';
1.1411 + }
1.1412 + $sql_data = array();
1.1413 + $next_post_id = false;
1.1414 +
1.1415 + include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
1.1416 +
1.1417 + $db->sql_transaction('begin');
1.1418 +
1.1419 + // we must make sure to update forums that contain the shadow'd topic
1.1420 + if ($post_mode == 'delete_topic')
1.1421 + {
1.1422 + $shadow_forum_ids = array();
1.1423 +
1.1424 + $sql = 'SELECT forum_id
1.1425 + FROM ' . TOPICS_TABLE . '
1.1426 + WHERE ' . $db->sql_in_set('topic_moved_id', $topic_id);
1.1427 + $result = $db->sql_query($sql);
1.1428 + while ($row = $db->sql_fetchrow($result))
1.1429 + {
1.1430 + if (!isset($shadow_forum_ids[(int) $row['forum_id']]))
1.1431 + {
1.1432 + $shadow_forum_ids[(int) $row['forum_id']] = 1;
1.1433 + }
1.1434 + else
1.1435 + {
1.1436 + $shadow_forum_ids[(int) $row['forum_id']]++;
1.1437 + }
1.1438 + }
1.1439 + $db->sql_freeresult($result);
1.1440 + }
1.1441 +
1.1442 + if (!delete_posts('post_id', array($post_id), false, false))
1.1443 + {
1.1444 + // Try to delete topic, we may had an previous error causing inconsistency
1.1445 + if ($post_mode == 'delete_topic')
1.1446 + {
1.1447 + delete_topics('topic_id', array($topic_id), false);
1.1448 + }
1.1449 + trigger_error('ALREADY_DELETED');
1.1450 + }
1.1451 +
1.1452 + $db->sql_transaction('commit');
1.1453 +
1.1454 + // Collect the necessary information for updating the tables
1.1455 + $sql_data[FORUMS_TABLE] = '';
1.1456 + switch ($post_mode)
1.1457 + {
1.1458 + case 'delete_topic':
1.1459 +
1.1460 + foreach ($shadow_forum_ids as $updated_forum => $topic_count)
1.1461 + {
1.1462 + // counting is fun! we only have to do sizeof($forum_ids) number of queries,
1.1463 + // even if the topic is moved back to where its shadow lives (we count how many times it is in a forum)
1.1464 + $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);
1.1465 + update_post_information('forum', $updated_forum);
1.1466 + }
1.1467 +
1.1468 + delete_topics('topic_id', array($topic_id), false);
1.1469 +
1.1470 + if ($data['topic_type'] != POST_GLOBAL)
1.1471 + {
1.1472 + $sql_data[FORUMS_TABLE] .= 'forum_topics_real = forum_topics_real - 1';
1.1473 + $sql_data[FORUMS_TABLE] .= ($data['topic_approved']) ? ', forum_posts = forum_posts - 1, forum_topics = forum_topics - 1' : '';
1.1474 + }
1.1475 +
1.1476 + $update_sql = update_post_information('forum', $forum_id, true);
1.1477 + if (sizeof($update_sql))
1.1478 + {
1.1479 + $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
1.1480 + $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
1.1481 + }
1.1482 + break;
1.1483 +
1.1484 + case 'delete_first_post':
1.1485 + $sql = 'SELECT p.post_id, p.poster_id, p.post_time, p.post_username, u.username, u.user_colour
1.1486 + FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
1.1487 + WHERE p.topic_id = $topic_id
1.1488 + AND p.poster_id = u.user_id
1.1489 + ORDER BY p.post_time ASC";
1.1490 + $result = $db->sql_query_limit($sql, 1);
1.1491 + $row = $db->sql_fetchrow($result);
1.1492 + $db->sql_freeresult($result);
1.1493 +
1.1494 + if ($data['topic_type'] != POST_GLOBAL)
1.1495 + {
1.1496 + $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
1.1497 + }
1.1498 +
1.1499 + $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'];
1.1500 +
1.1501 + // 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"
1.1502 + $sql_data[TOPICS_TABLE] .= ', topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
1.1503 +
1.1504 + $next_post_id = (int) $row['post_id'];
1.1505 + break;
1.1506 +
1.1507 + case 'delete_last_post':
1.1508 + if ($data['topic_type'] != POST_GLOBAL)
1.1509 + {
1.1510 + $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
1.1511 + }
1.1512 +
1.1513 + $update_sql = update_post_information('forum', $forum_id, true);
1.1514 + if (sizeof($update_sql))
1.1515 + {
1.1516 + $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
1.1517 + $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
1.1518 + }
1.1519 +
1.1520 + $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' : '');
1.1521 +
1.1522 + $update_sql = update_post_information('topic', $topic_id, true);
1.1523 + if (sizeof($update_sql))
1.1524 + {
1.1525 + $sql_data[TOPICS_TABLE] .= ', ' . implode(', ', $update_sql[$topic_id]);
1.1526 + $next_post_id = (int) str_replace('topic_last_post_id = ', '', $update_sql[$topic_id][0]);
1.1527 + }
1.1528 + else
1.1529 + {
1.1530 + $sql = 'SELECT MAX(post_id) as last_post_id
1.1531 + FROM ' . POSTS_TABLE . "
1.1532 + WHERE topic_id = $topic_id " .
1.1533 + ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '');
1.1534 + $result = $db->sql_query($sql);
1.1535 + $row = $db->sql_fetchrow($result);
1.1536 + $db->sql_freeresult($result);
1.1537 +
1.1538 + $next_post_id = (int) $row['last_post_id'];
1.1539 + }
1.1540 + break;
1.1541 +
1.1542 + case 'delete':
1.1543 + $sql = 'SELECT post_id
1.1544 + FROM ' . POSTS_TABLE . "
1.1545 + WHERE topic_id = $topic_id " .
1.1546 + ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '') . '
1.1547 + AND post_time > ' . $data['post_time'] . '
1.1548 + ORDER BY post_time ASC';
1.1549 + $result = $db->sql_query_limit($sql, 1);
1.1550 + $row = $db->sql_fetchrow($result);
1.1551 + $db->sql_freeresult($result);
1.1552 +
1.1553 + if ($data['topic_type'] != POST_GLOBAL)
1.1554 + {
1.1555 + $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
1.1556 + }
1.1557 +
1.1558 + $sql_data[TOPICS_TABLE] = 'topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
1.1559 + $next_post_id = (int) $row['post_id'];
1.1560 + break;
1.1561 + }
1.1562 +
1.1563 + if (($post_mode == 'delete') || ($post_mode == 'delete_last_post') || ($post_mode == 'delete_first_post'))
1.1564 + {
1.1565 + $sql = 'SELECT 1 AS has_attachments
1.1566 + FROM ' . ATTACHMENTS_TABLE . '
1.1567 + WHERE topic_id = ' . $topic_id;
1.1568 + $result = $db->sql_query_limit($sql, 1);
1.1569 + $has_attachments = (int) $db->sql_fetchfield('has_attachments');
1.1570 + $db->sql_freeresult($result);
1.1571 +
1.1572 + if (!$has_attachments)
1.1573 + {
1.1574 + $sql_data[TOPICS_TABLE] .= ', topic_attachment = 0';
1.1575 + }
1.1576 + }
1.1577 +
1.1578 +// $sql_data[USERS_TABLE] = ($data['post_postcount']) ? 'user_posts = user_posts - 1' : '';
1.1579 +
1.1580 + $db->sql_transaction('begin');
1.1581 +
1.1582 + $where_sql = array(
1.1583 + FORUMS_TABLE => "forum_id = $forum_id",
1.1584 + TOPICS_TABLE => "topic_id = $topic_id",
1.1585 + USERS_TABLE => 'user_id = ' . $data['poster_id']
1.1586 + );
1.1587 +
1.1588 + foreach ($sql_data as $table => $update_sql)
1.1589 + {
1.1590 + if ($update_sql)
1.1591 + {
1.1592 + $db->sql_query("UPDATE $table SET $update_sql WHERE " . $where_sql[$table]);
1.1593 + }
1.1594 + }
1.1595 +
1.1596 + // Adjust posted info for this user by looking for a post by him/her within this topic...
1.1597 + if ($post_mode != 'delete_topic' && $config['load_db_track'] && $data['poster_id'] != ANONYMOUS)
1.1598 + {
1.1599 + $sql = 'SELECT poster_id
1.1600 + FROM ' . POSTS_TABLE . '
1.1601 + WHERE topic_id = ' . $topic_id . '
1.1602 + AND poster_id = ' . $data['poster_id'];
1.1603 + $result = $db->sql_query_limit($sql, 1);
1.1604 + $poster_id = (int) $db->sql_fetchfield('poster_id');
1.1605 + $db->sql_freeresult($result);
1.1606 +
1.1607 + // The user is not having any more posts within this topic
1.1608 + if (!$poster_id)
1.1609 + {
1.1610 + $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
1.1611 + WHERE topic_id = ' . $topic_id . '
1.1612 + AND user_id = ' . $data['poster_id'];
1.1613 + $db->sql_query($sql);
1.1614 + }
1.1615 + }
1.1616 +
1.1617 + $db->sql_transaction('commit');
1.1618 +
1.1619 + if ($data['post_reported'] && ($post_mode != 'delete_topic'))
1.1620 + {
1.1621 + sync('topic_reported', 'topic_id', array($topic_id));
1.1622 + }
1.1623 +
1.1624 + return $next_post_id;
1.1625 +}
1.1626 +
1.1627 +/**
1.1628 +* Submit Post
1.1629 +* @todo Split up and create lightweight, simple API for this.
1.1630 +*/
1.1631 +function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $update_message = true, $update_search_index = true)
1.1632 +{
1.1633 + global $db, $auth, $user, $config, $phpEx, $template, $phpbb_root_path;
1.1634 +
1.1635 + // We do not handle erasing posts here
1.1636 + if ($mode == 'delete')
1.1637 + {
1.1638 + return false;
1.1639 + }
1.1640 +
1.1641 + $current_time = time();
1.1642 +
1.1643 + if ($mode == 'post')
1.1644 + {
1.1645 + $post_mode = 'post';
1.1646 + $update_message = true;
1.1647 + }
1.1648 + else if ($mode != 'edit')
1.1649 + {
1.1650 + $post_mode = 'reply';
1.1651 + $update_message = true;
1.1652 + }
1.1653 + else if ($mode == 'edit')
1.1654 + {
1.1655 + $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'));
1.1656 + }
1.1657 +
1.1658 + // First of all make sure the subject and topic title are having the correct length.
1.1659 + // To achieve this without cutting off between special chars we convert to an array and then count the elements.
1.1660 + $subject = truncate_string($subject);
1.1661 + $data['topic_title'] = truncate_string($data['topic_title']);
1.1662 +
1.1663 + // Collect some basic information about which tables and which rows to update/insert
1.1664 + $sql_data = $topic_row = array();
1.1665 + $poster_id = ($mode == 'edit') ? $data['poster_id'] : (int) $user->data['user_id'];
1.1666 +
1.1667 + // Retrieve some additional information if not present
1.1668 + if ($mode == 'edit' && (!isset($data['post_approved']) || !isset($data['topic_approved']) || $data['post_approved'] === false || $data['topic_approved'] === false))
1.1669 + {
1.1670 + $sql = 'SELECT p.post_approved, t.topic_type, t.topic_replies, t.topic_replies_real, t.topic_approved
1.1671 + FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
1.1672 + WHERE t.topic_id = p.topic_id
1.1673 + AND p.post_id = ' . $data['post_id'];
1.1674 + $result = $db->sql_query($sql);
1.1675 + $topic_row = $db->sql_fetchrow($result);
1.1676 + $db->sql_freeresult($result);
1.1677 +
1.1678 + $data['topic_approved'] = $topic_row['topic_approved'];
1.1679 + $data['post_approved'] = $topic_row['post_approved'];
1.1680 + }
1.1681 +
1.1682 + // 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
1.1683 + // The variable name should be $post_approved, because it indicates if the post is approved or not
1.1684 + $post_approval = 1;
1.1685 +
1.1686 + // Check the permissions for post approval. Moderators are not affected.
1.1687 + if (!$auth->acl_get('f_noapprove', $data['forum_id']) && !$auth->acl_get('m_approve', $data['forum_id']))
1.1688 + {
1.1689 + // Post not approved, but in queue
1.1690 + $post_approval = 0;
1.1691 + }
1.1692 +
1.1693 + // Mods are able to force approved/unapproved posts. True means the post is approved, false the post is unapproved
1.1694 + if (isset($data['force_approved_state']))
1.1695 + {
1.1696 + $post_approval = ($data['force_approved_state']) ? 1 : 0;
1.1697 + }
1.1698 +
1.1699 + // Start the transaction here
1.1700 + $db->sql_transaction('begin');
1.1701 +
1.1702 + // Collect Information
1.1703 + switch ($post_mode)
1.1704 + {
1.1705 + case 'post':
1.1706 + case 'reply':
1.1707 + $sql_data[POSTS_TABLE]['sql'] = array(
1.1708 + 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1.1709 + 'poster_id' => (int) $user->data['user_id'],
1.1710 + 'icon_id' => $data['icon_id'],
1.1711 + 'poster_ip' => $user->ip,
1.1712 + 'post_time' => $current_time,
1.1713 + 'post_approved' => $post_approval,
1.1714 + 'enable_bbcode' => $data['enable_bbcode'],
1.1715 + 'enable_smilies' => $data['enable_smilies'],
1.1716 + 'enable_magic_url' => $data['enable_urls'],
1.1717 + 'enable_sig' => $data['enable_sig'],
1.1718 + 'post_username' => (!$user->data['is_registered']) ? $username : '',
1.1719 + 'post_subject' => $subject,
1.1720 + 'post_text' => $data['message'],
1.1721 + 'post_checksum' => $data['message_md5'],
1.1722 + 'post_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
1.1723 + 'bbcode_bitfield' => $data['bbcode_bitfield'],
1.1724 + 'bbcode_uid' => $data['bbcode_uid'],
1.1725 + 'post_postcount' => ($auth->acl_get('f_postcount', $data['forum_id'])) ? 1 : 0,
1.1726 + 'post_edit_locked' => $data['post_edit_locked']
1.1727 + );
1.1728 + break;
1.1729 +
1.1730 + case 'edit_first_post':
1.1731 + case 'edit':
1.1732 +
1.1733 + case 'edit_last_post':
1.1734 + case 'edit_topic':
1.1735 +
1.1736 + // If edit reason is given always display edit info
1.1737 +
1.1738 + // If editing last post then display no edit info
1.1739 + // If m_edit permission then display no edit info
1.1740 + // If normal edit display edit info
1.1741 +
1.1742 + // Display edit info if edit reason given or user is editing his post, which is not the last within the topic.
1.1743 + if ($data['post_edit_reason'] || (!$auth->acl_get('m_edit', $data['forum_id']) && ($post_mode == 'edit' || $post_mode == 'edit_first_post')))
1.1744 + {
1.1745 + $data['post_edit_reason'] = truncate_string($data['post_edit_reason'], 255, 255, false);
1.1746 +
1.1747 + $sql_data[POSTS_TABLE]['sql'] = array(
1.1748 + 'post_edit_time' => $current_time,
1.1749 + 'post_edit_reason' => $data['post_edit_reason'],
1.1750 + 'post_edit_user' => (int) $data['post_edit_user'],
1.1751 + );
1.1752 +
1.1753 + $sql_data[POSTS_TABLE]['stat'][] = 'post_edit_count = post_edit_count + 1';
1.1754 + }
1.1755 + else if (!$data['post_edit_reason'] && $mode == 'edit' && $auth->acl_get('m_edit', $data['forum_id']))
1.1756 + {
1.1757 + $sql_data[POSTS_TABLE]['sql'] = array(
1.1758 + 'post_edit_reason' => '',
1.1759 + );
1.1760 + }
1.1761 +
1.1762 + // If the person editing this post is different to the one having posted then we will add a log entry stating the edit
1.1763 + // Could be simplified by only adding to the log if the edit is not tracked - but this may confuse admins/mods
1.1764 + if ($user->data['user_id'] != $poster_id)
1.1765 + {
1.1766 + $log_subject = ($subject) ? $subject : $data['topic_title'];
1.1767 + add_log('mod', $data['forum_id'], $data['topic_id'], 'LOG_POST_EDITED', $log_subject, (!empty($username)) ? $username : $user->lang['GUEST']);
1.1768 + }
1.1769 +
1.1770 + if (!isset($sql_data[POSTS_TABLE]['sql']))
1.1771 + {
1.1772 + $sql_data[POSTS_TABLE]['sql'] = array();
1.1773 + }
1.1774 +
1.1775 + $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1.1776 + 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1.1777 + 'poster_id' => $data['poster_id'],
1.1778 + 'icon_id' => $data['icon_id'],
1.1779 + 'post_approved' => (!$post_approval) ? 0 : $data['post_approved'],
1.1780 + 'enable_bbcode' => $data['enable_bbcode'],
1.1781 + 'enable_smilies' => $data['enable_smilies'],
1.1782 + 'enable_magic_url' => $data['enable_urls'],
1.1783 + 'enable_sig' => $data['enable_sig'],
1.1784 + 'post_username' => ($username && $data['poster_id'] == ANONYMOUS) ? $username : '',
1.1785 + 'post_subject' => $subject,
1.1786 + 'post_checksum' => $data['message_md5'],
1.1787 + 'post_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
1.1788 + 'bbcode_bitfield' => $data['bbcode_bitfield'],
1.1789 + 'bbcode_uid' => $data['bbcode_uid'],
1.1790 + 'post_edit_locked' => $data['post_edit_locked'])
1.1791 + );
1.1792 +
1.1793 + if ($update_message)
1.1794 + {
1.1795 + $sql_data[POSTS_TABLE]['sql']['post_text'] = $data['message'];
1.1796 + }
1.1797 +
1.1798 + break;
1.1799 + }
1.1800 +
1.1801 + $post_approved = $sql_data[POSTS_TABLE]['sql']['post_approved'];
1.1802 + $topic_row = array();
1.1803 +
1.1804 + // And the topic ladies and gentlemen
1.1805 + switch ($post_mode)
1.1806 + {
1.1807 + case 'post':
1.1808 + $sql_data[TOPICS_TABLE]['sql'] = array(
1.1809 + 'topic_poster' => (int) $user->data['user_id'],
1.1810 + 'topic_time' => $current_time,
1.1811 + 'topic_last_view_time' => $current_time,
1.1812 + 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1.1813 + 'icon_id' => $data['icon_id'],
1.1814 + 'topic_approved' => $post_approval,
1.1815 + 'topic_title' => $subject,
1.1816 + 'topic_first_poster_name' => (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
1.1817 + 'topic_first_poster_colour' => $user->data['user_colour'],
1.1818 + 'topic_type' => $topic_type,
1.1819 + 'topic_time_limit' => ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
1.1820 + 'topic_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
1.1821 + );
1.1822 +
1.1823 + if (isset($poll['poll_options']) && !empty($poll['poll_options']))
1.1824 + {
1.1825 + $poll_start = ($poll['poll_start']) ? $poll['poll_start'] : $current_time;
1.1826 + $poll_length = $poll['poll_length'] * 86400;
1.1827 + if ($poll_length < 0)
1.1828 + {
1.1829 + $poll_start = $poll_start + $poll_length;
1.1830 + if ($poll_start < 0)
1.1831 + {
1.1832 + $poll_start = 0;
1.1833 + }
1.1834 + $poll_length = 1;
1.1835 + }
1.1836 +
1.1837 + $sql_data[TOPICS_TABLE]['sql'] = array_merge($sql_data[TOPICS_TABLE]['sql'], array(
1.1838 + 'poll_title' => $poll['poll_title'],
1.1839 + 'poll_start' => $poll_start,
1.1840 + 'poll_max_options' => $poll['poll_max_options'],
1.1841 + 'poll_length' => $poll_length,
1.1842 + 'poll_vote_change' => $poll['poll_vote_change'])
1.1843 + );
1.1844 + }
1.1845 +
1.1846 + $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' : '');
1.1847 +
1.1848 + if ($topic_type != POST_GLOBAL)
1.1849 + {
1.1850 + if ($post_approval)
1.1851 + {
1.1852 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
1.1853 + }
1.1854 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($post_approval) ? ', forum_topics = forum_topics + 1' : '');
1.1855 + }
1.1856 + break;
1.1857 +
1.1858 + case 'reply':
1.1859 + $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_view_time = ' . $current_time . ',
1.1860 + topic_replies_real = topic_replies_real + 1,
1.1861 + topic_bumped = 0,
1.1862 + topic_bumper = 0' .
1.1863 + (($post_approval) ? ', topic_replies = topic_replies + 1' : '') .
1.1864 + ((!empty($data['attachment_data']) || (isset($data['topic_attachment']) && $data['topic_attachment'])) ? ', topic_attachment = 1' : '');
1.1865 +
1.1866 + $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' : '');
1.1867 +
1.1868 + if ($post_approval && $topic_type != POST_GLOBAL)
1.1869 + {
1.1870 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
1.1871 + }
1.1872 + break;
1.1873 +
1.1874 + case 'edit_topic':
1.1875 + case 'edit_first_post':
1.1876 + if (isset($poll['poll_options']))
1.1877 + {
1.1878 + $poll_start = ($poll['poll_start'] || empty($poll['poll_options'])) ? $poll['poll_start'] : $current_time;
1.1879 + $poll_length = $poll['poll_length'] * 86400;
1.1880 + if ($poll_length < 0)
1.1881 + {
1.1882 + $poll_start = $poll_start + $poll_length;
1.1883 + if ($poll_start < 0)
1.1884 + {
1.1885 + $poll_start = 0;
1.1886 + }
1.1887 + $poll_length = 1;
1.1888 + }
1.1889 + }
1.1890 +
1.1891 + $sql_data[TOPICS_TABLE]['sql'] = array(
1.1892 + 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1.1893 + 'icon_id' => $data['icon_id'],
1.1894 + 'topic_approved' => (!$post_approval) ? 0 : $data['topic_approved'],
1.1895 + 'topic_title' => $subject,
1.1896 + 'topic_first_poster_name' => $username,
1.1897 + 'topic_type' => $topic_type,
1.1898 + 'topic_time_limit' => ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
1.1899 + 'poll_title' => (isset($poll['poll_options'])) ? $poll['poll_title'] : '',
1.1900 + 'poll_start' => (isset($poll['poll_options'])) ? $poll_start : 0,
1.1901 + 'poll_max_options' => (isset($poll['poll_options'])) ? $poll['poll_max_options'] : 1,
1.1902 + 'poll_length' => (isset($poll['poll_options'])) ? $poll_length : 0,
1.1903 + 'poll_vote_change' => (isset($poll['poll_vote_change'])) ? $poll['poll_vote_change'] : 0,
1.1904 + 'topic_last_view_time' => $current_time,
1.1905 +
1.1906 + 'topic_attachment' => (!empty($data['attachment_data'])) ? 1 : (isset($data['topic_attachment']) ? $data['topic_attachment'] : 0),
1.1907 + );
1.1908 +
1.1909 + // Correctly set back the topic replies and forum posts... only if the topic was approved before and now gets disapproved
1.1910 + if (!$post_approval && $data['topic_approved'])
1.1911 + {
1.1912 + // Do we need to grab some topic informations?
1.1913 + if (!sizeof($topic_row))
1.1914 + {
1.1915 + $sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_approved
1.1916 + FROM ' . TOPICS_TABLE . '
1.1917 + WHERE topic_id = ' . $data['topic_id'];
1.1918 + $result = $db->sql_query($sql);
1.1919 + $topic_row = $db->sql_fetchrow($result);
1.1920 + $db->sql_freeresult($result);
1.1921 + }
1.1922 +
1.1923 + // If this is the only post remaining we do not need to decrement topic_replies.
1.1924 + // Also do not decrement if first post - then the topic_replies will not be adjusted if approving the topic again.
1.1925 +
1.1926 + // If this is an edited topic or the first post the topic gets completely disapproved later on...
1.1927 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics = forum_topics - 1';
1.1928 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - ' . ($topic_row['topic_replies'] + 1);
1.1929 +
1.1930 + set_config_count('num_topics', -1, true);
1.1931 + set_config_count('num_posts', ($topic_row['topic_replies'] + 1) * (-1), true);
1.1932 +
1.1933 + // Only decrement this post, since this is the one non-approved now
1.1934 + if ($auth->acl_get('f_postcount', $data['forum_id']))
1.1935 + {
1.1936 + $sql_data[USERS_TABLE]['stat'][] = 'user_posts = user_posts - 1';
1.1937 + }
1.1938 + }
1.1939 +
1.1940 + break;
1.1941 +
1.1942 + case 'edit':
1.1943 + case 'edit_last_post':
1.1944 +
1.1945 + // Correctly set back the topic replies and forum posts... but only if the post was approved before.
1.1946 + if (!$post_approval && $data['post_approved'])
1.1947 + {
1.1948 + $sql_data[TOPICS_TABLE]['stat'][] = 'topic_replies = topic_replies - 1, topic_last_view_time = ' . $current_time;
1.1949 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - 1';
1.1950 +
1.1951 + set_config_count('num_posts', -1, true);
1.1952 +
1.1953 + if ($auth->acl_get('f_postcount', $data['forum_id']))
1.1954 + {
1.1955 + $sql_data[USERS_TABLE]['stat'][] = 'user_posts = user_posts - 1';
1.1956 + }
1.1957 + }
1.1958 +
1.1959 + break;
1.1960 + }
1.1961 +
1.1962 + // Submit new topic
1.1963 + if ($post_mode == 'post')
1.1964 + {
1.1965 + $sql = 'INSERT INTO ' . TOPICS_TABLE . ' ' .
1.1966 + $db->sql_build_array('INSERT', $sql_data[TOPICS_TABLE]['sql']);
1.1967 + $db->sql_query($sql);
1.1968 +
1.1969 + $data['topic_id'] = $db->sql_nextid();
1.1970 +
1.1971 + $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1.1972 + 'topic_id' => $data['topic_id'])
1.1973 + );
1.1974 + unset($sql_data[TOPICS_TABLE]['sql']);
1.1975 + }
1.1976 +
1.1977 + // Submit new post
1.1978 + if ($post_mode == 'post' || $post_mode == 'reply')
1.1979 + {
1.1980 + if ($post_mode == 'reply')
1.1981 + {
1.1982 + $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1.1983 + 'topic_id' => $data['topic_id'])
1.1984 + );
1.1985 + }
1.1986 +
1.1987 + $sql = 'INSERT INTO ' . POSTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_data[POSTS_TABLE]['sql']);
1.1988 + $db->sql_query($sql);
1.1989 + $data['post_id'] = $db->sql_nextid();
1.1990 +
1.1991 + if ($post_mode == 'post')
1.1992 + {
1.1993 + $sql_data[TOPICS_TABLE]['sql'] = array(
1.1994 + 'topic_first_post_id' => $data['post_id'],
1.1995 + 'topic_last_post_id' => $data['post_id'],
1.1996 + 'topic_last_post_time' => $current_time,
1.1997 + 'topic_last_poster_id' => (int) $user->data['user_id'],
1.1998 + 'topic_last_poster_name' => (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
1.1999 + 'topic_last_poster_colour' => $user->data['user_colour'],
1.2000 + 'topic_last_post_subject' => (string) $subject,
1.2001 + );
1.2002 + }
1.2003 +
1.2004 + unset($sql_data[POSTS_TABLE]['sql']);
1.2005 + }
1.2006 +
1.2007 + $make_global = false;
1.2008 +
1.2009 + // Are we globalising or unglobalising?
1.2010 + if ($post_mode == 'edit_first_post' || $post_mode == 'edit_topic')
1.2011 + {
1.2012 + if (!sizeof($topic_row))
1.2013 + {
1.2014 + $sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_approved, topic_last_post_id
1.2015 + FROM ' . TOPICS_TABLE . '
1.2016 + WHERE topic_id = ' . $data['topic_id'];
1.2017 + $result = $db->sql_query($sql);
1.2018 + $topic_row = $db->sql_fetchrow($result);
1.2019 + $db->sql_freeresult($result);
1.2020 + }
1.2021 +
1.2022 + // globalise/unglobalise?
1.2023 + if (($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL) || ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL))
1.2024 + {
1.2025 + if (!empty($sql_data[FORUMS_TABLE]['stat']) && implode('', $sql_data[FORUMS_TABLE]['stat']))
1.2026 + {
1.2027 + $db->sql_query('UPDATE ' . FORUMS_TABLE . ' SET ' . implode(', ', $sql_data[FORUMS_TABLE]['stat']) . ' WHERE forum_id = ' . $data['forum_id']);
1.2028 + }
1.2029 +
1.2030 + $make_global = true;
1.2031 + $sql_data[FORUMS_TABLE]['stat'] = array();
1.2032 + }
1.2033 +
1.2034 + // globalise
1.2035 + if ($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL)
1.2036 + {
1.2037 + // Decrement topic/post count
1.2038 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - ' . ($topic_row['topic_replies_real'] + 1);
1.2039 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real - 1' . (($topic_row['topic_approved']) ? ', forum_topics = forum_topics - 1' : '');
1.2040 +
1.2041 + // Update forum_ids for all posts
1.2042 + $sql = 'UPDATE ' . POSTS_TABLE . '
1.2043 + SET forum_id = 0
1.2044 + WHERE topic_id = ' . $data['topic_id'];
1.2045 + $db->sql_query($sql);
1.2046 + }
1.2047 + // unglobalise
1.2048 + else if ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL)
1.2049 + {
1.2050 + // Increment topic/post count
1.2051 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + ' . ($topic_row['topic_replies_real'] + 1);
1.2052 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($topic_row['topic_approved']) ? ', forum_topics = forum_topics + 1' : '');
1.2053 +
1.2054 + // Update forum_ids for all posts
1.2055 + $sql = 'UPDATE ' . POSTS_TABLE . '
1.2056 + SET forum_id = ' . $data['forum_id'] . '
1.2057 + WHERE topic_id = ' . $data['topic_id'];
1.2058 + $db->sql_query($sql);
1.2059 + }
1.2060 + }
1.2061 +
1.2062 + // Update the topics table
1.2063 + if (isset($sql_data[TOPICS_TABLE]['sql']))
1.2064 + {
1.2065 + $sql = 'UPDATE ' . TOPICS_TABLE . '
1.2066 + SET ' . $db->sql_build_array('UPDATE', $sql_data[TOPICS_TABLE]['sql']) . '
1.2067 + WHERE topic_id = ' . $data['topic_id'];
1.2068 + $db->sql_query($sql);
1.2069 + }
1.2070 +
1.2071 + // Update the posts table
1.2072 + if (isset($sql_data[POSTS_TABLE]['sql']))
1.2073 + {
1.2074 + $sql = 'UPDATE ' . POSTS_TABLE . '
1.2075 + SET ' . $db->sql_build_array('UPDATE', $sql_data[POSTS_TABLE]['sql']) . '
1.2076 + WHERE post_id = ' . $data['post_id'];
1.2077 + $db->sql_query($sql);
1.2078 + }
1.2079 +
1.2080 + // Update Poll Tables
1.2081 + if (isset($poll['poll_options']))
1.2082 + {
1.2083 + $cur_poll_options = array();
1.2084 +
1.2085 + if ($mode == 'edit')
1.2086 + {
1.2087 + $sql = 'SELECT *
1.2088 + FROM ' . POLL_OPTIONS_TABLE . '
1.2089 + WHERE topic_id = ' . $data['topic_id'] . '
1.2090 + ORDER BY poll_option_id';
1.2091 + $result = $db->sql_query($sql);
1.2092 +
1.2093 + $cur_poll_options = array();
1.2094 + while ($row = $db->sql_fetchrow($result))
1.2095 + {
1.2096 + $cur_poll_options[] = $row;
1.2097 + }
1.2098 + $db->sql_freeresult($result);
1.2099 + }
1.2100 +
1.2101 + $sql_insert_ary = array();
1.2102 +
1.2103 + for ($i = 0, $size = sizeof($poll['poll_options']); $i < $size; $i++)
1.2104 + {
1.2105 + if (strlen(trim($poll['poll_options'][$i])))
1.2106 + {
1.2107 + if (empty($cur_poll_options[$i]))
1.2108 + {
1.2109 + // If we add options we need to put them to the end to be able to preserve votes...
1.2110 + $sql_insert_ary[] = array(
1.2111 + 'poll_option_id' => (int) sizeof($cur_poll_options) + 1 + sizeof($sql_insert_ary),
1.2112 + 'topic_id' => (int) $data['topic_id'],
1.2113 + 'poll_option_text' => (string) $poll['poll_options'][$i]
1.2114 + );
1.2115 + }
1.2116 + else if ($poll['poll_options'][$i] != $cur_poll_options[$i])
1.2117 + {
1.2118 + $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . "
1.2119 + SET poll_option_text = '" . $db->sql_escape($poll['poll_options'][$i]) . "'
1.2120 + WHERE poll_option_id = " . $cur_poll_options[$i]['poll_option_id'] . '
1.2121 + AND topic_id = ' . $data['topic_id'];
1.2122 + $db->sql_query($sql);
1.2123 + }
1.2124 + }
1.2125 + }
1.2126 +
1.2127 + $db->sql_multi_insert(POLL_OPTIONS_TABLE, $sql_insert_ary);
1.2128 +
1.2129 + if (sizeof($poll['poll_options']) < sizeof($cur_poll_options))
1.2130 + {
1.2131 + $sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . '
1.2132 + WHERE poll_option_id > ' . sizeof($poll['poll_options']) . '
1.2133 + AND topic_id = ' . $data['topic_id'];
1.2134 + $db->sql_query($sql);
1.2135 + }
1.2136 +
1.2137 + // 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
1.2138 + if ($mode == 'edit' && sizeof($poll['poll_options']) != sizeof($cur_poll_options))
1.2139 + {
1.2140 + $db->sql_query('DELETE FROM ' . POLL_VOTES_TABLE . ' WHERE topic_id = ' . $data['topic_id']);
1.2141 + $db->sql_query('UPDATE ' . POLL_OPTIONS_TABLE . ' SET poll_option_total = 0 WHERE topic_id = ' . $data['topic_id']);
1.2142 + }
1.2143 + }
1.2144 +
1.2145 + // Submit Attachments
1.2146 + if (!empty($data['attachment_data']) && $data['post_id'] && in_array($mode, array('post', 'reply', 'quote', 'edit')))
1.2147 + {
1.2148 + $space_taken = $files_added = 0;
1.2149 + $orphan_rows = array();
1.2150 +
1.2151 + foreach ($data['attachment_data'] as $pos => $attach_row)
1.2152 + {
1.2153 + $orphan_rows[(int) $attach_row['attach_id']] = array();
1.2154 + }
1.2155 +
1.2156 + if (sizeof($orphan_rows))
1.2157 + {
1.2158 + $sql = 'SELECT attach_id, filesize, physical_filename
1.2159 + FROM ' . ATTACHMENTS_TABLE . '
1.2160 + WHERE ' . $db->sql_in_set('attach_id', array_keys($orphan_rows)) . '
1.2161 + AND is_orphan = 1
1.2162 + AND poster_id = ' . $user->data['user_id'];
1.2163 + $result = $db->sql_query($sql);
1.2164 +
1.2165 + $orphan_rows = array();
1.2166 + while ($row = $db->sql_fetchrow($result))
1.2167 + {
1.2168 + $orphan_rows[$row['attach_id']] = $row;
1.2169 + }
1.2170 + $db->sql_freeresult($result);
1.2171 + }
1.2172 +
1.2173 + foreach ($data['attachment_data'] as $pos => $attach_row)
1.2174 + {
1.2175 + if ($attach_row['is_orphan'] && !isset($orphan_rows[$attach_row['attach_id']]))
1.2176 + {
1.2177 + continue;
1.2178 + }
1.2179 +
1.2180 + if (!$attach_row['is_orphan'])
1.2181 + {
1.2182 + // update entry in db if attachment already stored in db and filespace
1.2183 + $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
1.2184 + SET attach_comment = '" . $db->sql_escape($attach_row['attach_comment']) . "'
1.2185 + WHERE attach_id = " . (int) $attach_row['attach_id'] . '
1.2186 + AND is_orphan = 0';
1.2187 + $db->sql_query($sql);
1.2188 + }
1.2189 + else
1.2190 + {
1.2191 + // insert attachment into db
1.2192 + if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . utf8_basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
1.2193 + {
1.2194 + continue;
1.2195 + }
1.2196 +
1.2197 + $space_taken += $orphan_rows[$attach_row['attach_id']]['filesize'];
1.2198 + $files_added++;
1.2199 +
1.2200 + $attach_sql = array(
1.2201 + 'post_msg_id' => $data['post_id'],
1.2202 + 'topic_id' => $data['topic_id'],
1.2203 + 'is_orphan' => 0,
1.2204 + 'poster_id' => $poster_id,
1.2205 + 'attach_comment' => $attach_row['attach_comment'],
1.2206 + );
1.2207 +
1.2208 + $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $attach_sql) . '
1.2209 + WHERE attach_id = ' . $attach_row['attach_id'] . '
1.2210 + AND is_orphan = 1
1.2211 + AND poster_id = ' . $user->data['user_id'];
1.2212 + $db->sql_query($sql);
1.2213 + }
1.2214 + }
1.2215 +
1.2216 + if ($space_taken && $files_added)
1.2217 + {
1.2218 + set_config_count('upload_dir_size', $space_taken, true);
1.2219 + set_config_count('num_files', $files_added, true);
1.2220 + }
1.2221 + }
1.2222 +
1.2223 + // we need to update the last forum information
1.2224 + // only applicable if the topic is not global and it is approved
1.2225 + // we also check to make sure we are not dealing with globaling the latest topic (pretty rare but still needs to be checked)
1.2226 + if ($topic_type != POST_GLOBAL && !$make_global && ($post_approved || !$data['post_approved']))
1.2227 + {
1.2228 + // the last post makes us update the forum table. This can happen if...
1.2229 + // We make a new topic
1.2230 + // We reply to a topic
1.2231 + // We edit the last post in a topic and this post is the latest in the forum (maybe)
1.2232 + // We edit the only post in the topic
1.2233 + // We edit the first post in the topic and all the other posts are not approved
1.2234 + if (($post_mode == 'post' || $post_mode == 'reply') && $post_approved)
1.2235 + {
1.2236 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . $data['post_id'];
1.2237 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($subject) . "'";
1.2238 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . $current_time;
1.2239 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $user->data['user_id'];
1.2240 + $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'] : '')) . "'";
1.2241 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($user->data['user_colour']) . "'";
1.2242 + }
1.2243 + else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies']))
1.2244 + {
1.2245 + // this does not _necessarily_ mean that we must update the info again,
1.2246 + // it just means that we might have to
1.2247 + $sql = 'SELECT forum_last_post_id, forum_last_post_subject
1.2248 + FROM ' . FORUMS_TABLE . '
1.2249 + WHERE forum_id = ' . (int) $data['forum_id'];
1.2250 + $result = $db->sql_query($sql);
1.2251 + $row = $db->sql_fetchrow($result);
1.2252 + $db->sql_freeresult($result);
1.2253 +
1.2254 + // this post is the latest post in the forum, better update
1.2255 + if ($row['forum_last_post_id'] == $data['post_id'])
1.2256 + {
1.2257 + // If post approved and subject changed, or poster is anonymous, we need to update the forum_last* rows
1.2258 + if ($post_approved && ($row['forum_last_post_subject'] !== $subject || $data['poster_id'] == ANONYMOUS))
1.2259 + {
1.2260 + // the post's subject changed
1.2261 + if ($row['forum_last_post_subject'] !== $subject)
1.2262 + {
1.2263 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_subject = \'' . $db->sql_escape($subject) . '\'';
1.2264 + }
1.2265 +
1.2266 + // Update the user name if poster is anonymous... just in case an admin changed it
1.2267 + if ($data['poster_id'] == ANONYMOUS)
1.2268 + {
1.2269 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape($username) . "'";
1.2270 + }
1.2271 + }
1.2272 + else if ($data['post_approved'] !== $post_approved)
1.2273 + {
1.2274 + // we need a fresh change of socks, everything has become invalidated
1.2275 + $sql = 'SELECT MAX(topic_last_post_id) as last_post_id
1.2276 + FROM ' . TOPICS_TABLE . '
1.2277 + WHERE forum_id = ' . (int) $data['forum_id'] . '
1.2278 + AND topic_approved = 1';
1.2279 + $result = $db->sql_query($sql);
1.2280 + $row = $db->sql_fetchrow($result);
1.2281 + $db->sql_freeresult($result);
1.2282 +
1.2283 + // any posts left in this forum?
1.2284 + if (!empty($row['last_post_id']))
1.2285 + {
1.2286 + $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
1.2287 + FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1.2288 + WHERE p.poster_id = u.user_id
1.2289 + AND p.post_id = ' . (int) $row['last_post_id'];
1.2290 + $result = $db->sql_query($sql);
1.2291 + $row = $db->sql_fetchrow($result);
1.2292 + $db->sql_freeresult($result);
1.2293 +
1.2294 + // salvation, a post is found! jam it into the forums table
1.2295 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
1.2296 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
1.2297 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
1.2298 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
1.2299 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
1.2300 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
1.2301 + }
1.2302 + else
1.2303 + {
1.2304 + // just our luck, the last topic in the forum has just been turned unapproved...
1.2305 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = 0';
1.2306 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = ''";
1.2307 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = 0';
1.2308 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = 0';
1.2309 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = ''";
1.2310 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = ''";
1.2311 + }
1.2312 + }
1.2313 + }
1.2314 + }
1.2315 + }
1.2316 + else if ($make_global)
1.2317 + {
1.2318 + // somebody decided to be a party pooper, we must recalculate the whole shebang (maybe)
1.2319 + $sql = 'SELECT forum_last_post_id
1.2320 + FROM ' . FORUMS_TABLE . '
1.2321 + WHERE forum_id = ' . (int) $data['forum_id'];
1.2322 + $result = $db->sql_query($sql);
1.2323 + $forum_row = $db->sql_fetchrow($result);
1.2324 + $db->sql_freeresult($result);
1.2325 +
1.2326 + // we made a topic global, go get new data
1.2327 + if ($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL && $forum_row['forum_last_post_id'] == $topic_row['topic_last_post_id'])
1.2328 + {
1.2329 + // we need a fresh change of socks, everything has become invalidated
1.2330 + $sql = 'SELECT MAX(topic_last_post_id) as last_post_id
1.2331 + FROM ' . TOPICS_TABLE . '
1.2332 + WHERE forum_id = ' . (int) $data['forum_id'] . '
1.2333 + AND topic_approved = 1';
1.2334 + $result = $db->sql_query($sql);
1.2335 + $row = $db->sql_fetchrow($result);
1.2336 + $db->sql_freeresult($result);
1.2337 +
1.2338 + // any posts left in this forum?
1.2339 + if (!empty($row['last_post_id']))
1.2340 + {
1.2341 + $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
1.2342 + FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1.2343 + WHERE p.poster_id = u.user_id
1.2344 + AND p.post_id = ' . (int) $row['last_post_id'];
1.2345 + $result = $db->sql_query($sql);
1.2346 + $row = $db->sql_fetchrow($result);
1.2347 + $db->sql_freeresult($result);
1.2348 +
1.2349 + // salvation, a post is found! jam it into the forums table
1.2350 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
1.2351 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
1.2352 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
1.2353 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
1.2354 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
1.2355 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
1.2356 + }
1.2357 + else
1.2358 + {
1.2359 + // just our luck, the last topic in the forum has just been globalized...
1.2360 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = 0';
1.2361 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = ''";
1.2362 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = 0';
1.2363 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = 0';
1.2364 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = ''";
1.2365 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = ''";
1.2366 + }
1.2367 + }
1.2368 + else if ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL && $forum_row['forum_last_post_id'] < $topic_row['topic_last_post_id'])
1.2369 + {
1.2370 + // this post has a higher id, it is newer
1.2371 + $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
1.2372 + FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1.2373 + WHERE p.poster_id = u.user_id
1.2374 + AND p.post_id = ' . (int) $topic_row['topic_last_post_id'];
1.2375 + $result = $db->sql_query($sql);
1.2376 + $row = $db->sql_fetchrow($result);
1.2377 + $db->sql_freeresult($result);
1.2378 +
1.2379 + // salvation, a post is found! jam it into the forums table
1.2380 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
1.2381 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
1.2382 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
1.2383 + $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
1.2384 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
1.2385 + $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
1.2386 + }
1.2387 + }
1.2388 +
1.2389 + // topic sync time!
1.2390 + // simply, we update if it is a reply or the last post is edited
1.2391 + if ($post_approved)
1.2392 + {
1.2393 + // reply requires the whole thing
1.2394 + if ($post_mode == 'reply')
1.2395 + {
1.2396 + $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_id = ' . (int) $data['post_id'];
1.2397 + $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_poster_id = ' . (int) $user->data['user_id'];
1.2398 + $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'] : '')) . "'";
1.2399 + $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_colour = '" . (($user->data['user_id'] != ANONYMOUS) ? $db->sql_escape($user->data['user_colour']) : '') . "'";
1.2400 + $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
1.2401 + $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_time = ' . (int) $current_time;
1.2402 + }
1.2403 + else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies']))
1.2404 + {
1.2405 + // only the subject can be changed from edit
1.2406 + $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
1.2407 +
1.2408 + // Maybe not only the subject, but also changing anonymous usernames. ;)
1.2409 + if ($data['poster_id'] == ANONYMOUS)
1.2410 + {
1.2411 + $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape($username) . "'";
1.2412 + }
1.2413 + }
1.2414 + }
1.2415 + else if (!$data['post_approved'] && ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies'])))
1.2416 + {
1.2417 + // like having the rug pulled from under us
1.2418 + $sql = 'SELECT MAX(post_id) as last_post_id
1.2419 + FROM ' . POSTS_TABLE . '
1.2420 + WHERE topic_id = ' . (int) $data['topic_id'] . '
1.2421 + AND post_approved = 1';
1.2422 + $result = $db->sql_query($sql);
1.2423 + $row = $db->sql_fetchrow($result);
1.2424 + $db->sql_freeresult($result);
1.2425 +
1.2426 + // any posts left in this forum?
1.2427 + if (!empty($row['last_post_id']))
1.2428 + {
1.2429 + $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
1.2430 + FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1.2431 + WHERE p.poster_id = u.user_id
1.2432 + AND p.post_id = ' . (int) $row['last_post_id'];
1.2433 + $result = $db->sql_query($sql);
1.2434 + $row = $db->sql_fetchrow($result);
1.2435 + $db->sql_freeresult($result);
1.2436 +
1.2437 + // salvation, a post is found! jam it into the topics table
1.2438 + $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_id = ' . (int) $row['post_id'];
1.2439 + $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
1.2440 + $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_time = ' . (int) $row['post_time'];
1.2441 + $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_poster_id = ' . (int) $row['poster_id'];
1.2442 + $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
1.2443 + $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
1.2444 + }
1.2445 + }
1.2446 +
1.2447 + // Update total post count, do not consider moderated posts/topics
1.2448 + if ($post_approval)
1.2449 + {
1.2450 + if ($post_mode == 'post')
1.2451 + {
1.2452 + set_config_count('num_topics', 1, true);
1.2453 + set_config_count('num_posts', 1, true);
1.2454 + }
1.2455 +
1.2456 + if ($post_mode == 'reply')
1.2457 + {
1.2458 + set_config_count('num_posts', 1, true);
1.2459 + }
1.2460 + }
1.2461 +
1.2462 + // Update forum stats
1.2463 + $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);
1.2464 +
1.2465 + foreach ($sql_data as $table => $update_ary)
1.2466 + {
1.2467 + if (isset($update_ary['stat']) && implode('', $update_ary['stat']))
1.2468 + {
1.2469 + $sql = "UPDATE $table SET " . implode(', ', $update_ary['stat']) . ' WHERE ' . $where_sql[$table];
1.2470 + $db->sql_query($sql);
1.2471 + }
1.2472 + }
1.2473 +
1.2474 + // Delete topic shadows (if any exist). We do not need a shadow topic for an global announcement
1.2475 + if ($make_global)
1.2476 + {
1.2477 + $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1.2478 + WHERE topic_moved_id = ' . $data['topic_id'];
1.2479 + $db->sql_query($sql);
1.2480 + }
1.2481 +
1.2482 + // Committing the transaction before updating search index
1.2483 + $db->sql_transaction('commit');
1.2484 +
1.2485 + // Delete draft if post was loaded...
1.2486 + $draft_id = request_var('draft_loaded', 0);
1.2487 + if ($draft_id)
1.2488 + {
1.2489 + $sql = 'DELETE FROM ' . DRAFTS_TABLE . "
1.2490 + WHERE draft_id = $draft_id
1.2491 + AND user_id = {$user->data['user_id']}";
1.2492 + $db->sql_query($sql);
1.2493 + }
1.2494 +
1.2495 + // Index message contents
1.2496 + if ($update_search_index && $data['enable_indexing'])
1.2497 + {
1.2498 + // Select the search method and do some additional checks to ensure it can actually be utilised
1.2499 + $search_type = basename($config['search_type']);
1.2500 +
1.2501 + if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
1.2502 + {
1.2503 + trigger_error('NO_SUCH_SEARCH_MODULE');
1.2504 + }
1.2505 +
1.2506 + if (!class_exists($search_type))
1.2507 + {
1.2508 + include("{$phpbb_root_path}includes/search/$search_type.$phpEx");
1.2509 + }
1.2510 +
1.2511 + $error = false;
1.2512 + $search = new $search_type($error);
1.2513 +
1.2514 + if ($error)
1.2515 + {
1.2516 + trigger_error($error);
1.2517 + }
1.2518 +
1.2519 + $search->index($mode, $data['post_id'], $data['message'], $subject, $poster_id, ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']);
1.2520 + }
1.2521 +
1.2522 + // Topic Notification, do not change if moderator is changing other users posts...
1.2523 + if ($user->data['user_id'] == $poster_id)
1.2524 + {
1.2525 + if (!$data['notify_set'] && $data['notify'])
1.2526 + {
1.2527 + $sql = 'INSERT INTO ' . TOPICS_WATCH_TABLE . ' (user_id, topic_id)
1.2528 + VALUES (' . $user->data['user_id'] . ', ' . $data['topic_id'] . ')';
1.2529 + $db->sql_query($sql);
1.2530 + }
1.2531 + else if (($config['email_enable'] || $config['jab_enable']) && $data['notify_set'] && !$data['notify'])
1.2532 + {
1.2533 + $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . '
1.2534 + WHERE user_id = ' . $user->data['user_id'] . '
1.2535 + AND topic_id = ' . $data['topic_id'];
1.2536 + $db->sql_query($sql);
1.2537 + }
1.2538 + }
1.2539 +
1.2540 + if ($mode == 'post' || $mode == 'reply' || $mode == 'quote')
1.2541 + {
1.2542 + // Mark this topic as posted to
1.2543 + markread('post', $data['forum_id'], $data['topic_id']);
1.2544 + }
1.2545 +
1.2546 + // Mark this topic as read
1.2547 + // We do not use post_time here, this is intended (post_time can have a date in the past if editing a message)
1.2548 + markread('topic', (($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']), $data['topic_id'], time());
1.2549 +
1.2550 + //
1.2551 + if ($config['load_db_lastread'] && $user->data['is_registered'])
1.2552 + {
1.2553 + $sql = 'SELECT mark_time
1.2554 + FROM ' . FORUMS_TRACK_TABLE . '
1.2555 + WHERE user_id = ' . $user->data['user_id'] . '
1.2556 + AND forum_id = ' . (($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']);
1.2557 + $result = $db->sql_query($sql);
1.2558 + $f_mark_time = (int) $db->sql_fetchfield('mark_time');
1.2559 + $db->sql_freeresult($result);
1.2560 + }
1.2561 + else if ($config['load_anon_lastread'] || $user->data['is_registered'])
1.2562 + {
1.2563 + $f_mark_time = false;
1.2564 + }
1.2565 +
1.2566 + if (($config['load_db_lastread'] && $user->data['is_registered']) || $config['load_anon_lastread'] || $user->data['is_registered'])
1.2567 + {
1.2568 + // Update forum info
1.2569 + if ($topic_type == POST_GLOBAL)
1.2570 + {
1.2571 + $sql = 'SELECT MAX(topic_last_post_time) as forum_last_post_time
1.2572 + FROM ' . TOPICS_TABLE . '
1.2573 + WHERE forum_id = 0';
1.2574 + }
1.2575 + else
1.2576 + {
1.2577 + $sql = 'SELECT forum_last_post_time
1.2578 + FROM ' . FORUMS_TABLE . '
1.2579 + WHERE forum_id = ' . $data['forum_id'];
1.2580 + }
1.2581 + $result = $db->sql_query($sql);
1.2582 + $forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
1.2583 + $db->sql_freeresult($result);
1.2584 +
1.2585 + update_forum_tracking_info((($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']), $forum_last_post_time, $f_mark_time, false);
1.2586 + }
1.2587 +
1.2588 + // Send Notifications
1.2589 + if (($mode == 'reply' || $mode == 'quote' || $mode == 'post') && $post_approval)
1.2590 + {
1.2591 + user_notification($mode, $subject, $data['topic_title'], $data['forum_name'], $data['forum_id'], $data['topic_id'], $data['post_id']);
1.2592 + }
1.2593 +
1.2594 + $params = $add_anchor = '';
1.2595 +
1.2596 + if ($post_approval)
1.2597 + {
1.2598 + $params .= '&t=' . $data['topic_id'];
1.2599 +
1.2600 + if ($mode != 'post')
1.2601 + {
1.2602 + $params .= '&p=' . $data['post_id'];
1.2603 + $add_anchor = '#p' . $data['post_id'];
1.2604 + }
1.2605 + }
1.2606 + else if ($mode != 'post' && $post_mode != 'edit_first_post' && $post_mode != 'edit_topic')
1.2607 + {
1.2608 + $params .= '&t=' . $data['topic_id'];
1.2609 + }
1.2610 +
1.2611 + $url = (!$params) ? "{$phpbb_root_path}viewforum.$phpEx" : "{$phpbb_root_path}viewtopic.$phpEx";
1.2612 + $url = append_sid($url, 'f=' . $data['forum_id'] . $params) . $add_anchor;
1.2613 +
1.2614 + return $url;
1.2615 +}
1.2616 +
1.2617 +/**
1.2618 +* Handle topic bumping
1.2619 +* @param int $forum_id The ID of the forum the topic is being bumped belongs to
1.2620 +* @param int $topic_id The ID of the topic is being bumping
1.2621 +* @param array $post_data Passes some topic parameters:
1.2622 +* - 'topic_title'
1.2623 +* - 'topic_last_post_id'
1.2624 +* - 'topic_last_poster_id'
1.2625 +* - 'topic_last_post_subject'
1.2626 +* - 'topic_last_poster_name'
1.2627 +* - 'topic_last_poster_colour'
1.2628 +* @param int $bump_time The time at which topic was bumped, usually it is a current time as obtained via time().
1.2629 +* @return string An URL to the bumped topic, example: ./viewtopic.php?forum_id=1&topic_id=2&p=3#p3
1.2630 +*/
1.2631 +function phpbb_bump_topic($forum_id, $topic_id, $post_data, $bump_time = false)
1.2632 +{
1.2633 + global $config, $db, $user, $phpEx, $phpbb_root_path;
1.2634 +
1.2635 + if ($bump_time === false)
1.2636 + {
1.2637 + $bump_time = time();
1.2638 + }
1.2639 +
1.2640 + // Begin bumping
1.2641 + $db->sql_transaction('begin');
1.2642 +
1.2643 + // Update the topic's last post post_time
1.2644 + $sql = 'UPDATE ' . POSTS_TABLE . "
1.2645 + SET post_time = $bump_time
1.2646 + WHERE post_id = {$post_data['topic_last_post_id']}
1.2647 + AND topic_id = $topic_id";
1.2648 + $db->sql_query($sql);
1.2649 +
1.2650 + // Sync the topic's last post time, the rest of the topic's last post data isn't changed
1.2651 + $sql = 'UPDATE ' . TOPICS_TABLE . "
1.2652 + SET topic_last_post_time = $bump_time,
1.2653 + topic_bumped = 1,
1.2654 + topic_bumper = " . $user->data['user_id'] . "
1.2655 + WHERE topic_id = $topic_id";
1.2656 + $db->sql_query($sql);
1.2657 +
1.2658 + // Update the forum's last post info
1.2659 + $sql = 'UPDATE ' . FORUMS_TABLE . "
1.2660 + SET forum_last_post_id = " . $post_data['topic_last_post_id'] . ",
1.2661 + forum_last_poster_id = " . $post_data['topic_last_poster_id'] . ",
1.2662 + forum_last_post_subject = '" . $db->sql_escape($post_data['topic_last_post_subject']) . "',
1.2663 + forum_last_post_time = $bump_time,
1.2664 + forum_last_poster_name = '" . $db->sql_escape($post_data['topic_last_poster_name']) . "',
1.2665 + forum_last_poster_colour = '" . $db->sql_escape($post_data['topic_last_poster_colour']) . "'
1.2666 + WHERE forum_id = $forum_id";
1.2667 + $db->sql_query($sql);
1.2668 +
1.2669 + // Update bumper's time of the last posting to prevent flood
1.2670 + $sql = 'UPDATE ' . USERS_TABLE . "
1.2671 + SET user_lastpost_time = $bump_time
1.2672 + WHERE user_id = " . $user->data['user_id'];
1.2673 + $db->sql_query($sql);
1.2674 +
1.2675 + $db->sql_transaction('commit');
1.2676 +
1.2677 + // Mark this topic as posted to
1.2678 + markread('post', $forum_id, $topic_id, $bump_time);
1.2679 +
1.2680 + // Mark this topic as read
1.2681 + markread('topic', $forum_id, $topic_id, $bump_time);
1.2682 +
1.2683 + // Update forum tracking info
1.2684 + if ($config['load_db_lastread'] && $user->data['is_registered'])
1.2685 + {
1.2686 + $sql = 'SELECT mark_time
1.2687 + FROM ' . FORUMS_TRACK_TABLE . '
1.2688 + WHERE user_id = ' . $user->data['user_id'] . '
1.2689 + AND forum_id = ' . $forum_id;
1.2690 + $result = $db->sql_query($sql);
1.2691 + $f_mark_time = (int) $db->sql_fetchfield('mark_time');
1.2692 + $db->sql_freeresult($result);
1.2693 + }
1.2694 + else if ($config['load_anon_lastread'] || $user->data['is_registered'])
1.2695 + {
1.2696 + $f_mark_time = false;
1.2697 + }
1.2698 +
1.2699 + if (($config['load_db_lastread'] && $user->data['is_registered']) || $config['load_anon_lastread'] || $user->data['is_registered'])
1.2700 + {
1.2701 + // Update forum info
1.2702 + $sql = 'SELECT forum_last_post_time
1.2703 + FROM ' . FORUMS_TABLE . '
1.2704 + WHERE forum_id = ' . $forum_id;
1.2705 + $result = $db->sql_query($sql);
1.2706 + $forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
1.2707 + $db->sql_freeresult($result);
1.2708 +
1.2709 + update_forum_tracking_info($forum_id, $forum_last_post_time, $f_mark_time, false);
1.2710 + }
1.2711 +
1.2712 + add_log('mod', $forum_id, $topic_id, 'LOG_BUMP_TOPIC', $post_data['topic_title']);
1.2713 +
1.2714 + $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']}";
1.2715 +
1.2716 + return $url;
1.2717 +}
1.2718 +
1.2719 +?>
1.2720 \ No newline at end of file