Blame Extras/phpBB/3.0.4/includes/functions_posting.php

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package phpBB3
4c79b5
* @version $Id: functions_posting.php 9166 2008-12-03 16:40:53Z acydburn $
4c79b5
* @copyright (c) 2005 phpBB Group
4c79b5
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
4c79b5
*
4c79b5
*/
4c79b5
4c79b5
/**
4c79b5
* @ignore
4c79b5
*/
4c79b5
if (!defined('IN_PHPBB'))
4c79b5
{
4c79b5
	exit;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Fill smiley templates (or just the variables) with smilies, either in a window or inline
4c79b5
*/
4c79b5
function generate_smilies($mode, $forum_id)
4c79b5
{
4c79b5
	global $auth, $db, $user, $config, $template;
4c79b5
	global $phpEx, $phpbb_root_path;
4c79b5
4c79b5
	if ($mode == 'window')
4c79b5
	{
4c79b5
		if ($forum_id)
4c79b5
		{
4c79b5
			$sql = 'SELECT forum_style
4c79b5
				FROM ' . FORUMS_TABLE . "
4c79b5
				WHERE forum_id = $forum_id";
4c79b5
			$result = $db->sql_query_limit($sql, 1);
4c79b5
			$row = $db->sql_fetchrow($result);
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			$user->setup('posting', (int) $row['forum_style']);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$user->setup('posting');
4c79b5
		}
4c79b5
4c79b5
		page_header($user->lang['SMILIES']);
4c79b5
4c79b5
		$template->set_filenames(array(
4c79b5
			'body' => 'posting_smilies.html')
4c79b5
		);
4c79b5
	}
4c79b5
4c79b5
	$display_link = false;
4c79b5
	if ($mode == 'inline')
4c79b5
	{
4c79b5
		$sql = 'SELECT smiley_id
4c79b5
			FROM ' . SMILIES_TABLE . '
4c79b5
			WHERE display_on_posting = 0';
4c79b5
		$result = $db->sql_query_limit($sql, 1, 0, 3600);
4c79b5
4c79b5
		if ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$display_link = true;
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
	}
4c79b5
4c79b5
	$last_url = '';
4c79b5
4c79b5
	$sql = 'SELECT *
4c79b5
		FROM ' . SMILIES_TABLE .
4c79b5
		(($mode == 'inline') ? ' WHERE display_on_posting = 1 ' : '') . '
4c79b5
		ORDER BY smiley_order';
4c79b5
	$result = $db->sql_query($sql, 3600);
4c79b5
4c79b5
	$smilies = array();
4c79b5
	while ($row = $db->sql_fetchrow($result))
4c79b5
	{
4c79b5
		if (empty($smilies[$row['smiley_url']]))
4c79b5
		{
4c79b5
			$smilies[$row['smiley_url']] = $row;
4c79b5
		}
4c79b5
	}
4c79b5
	$db->sql_freeresult($result);
4c79b5
4c79b5
	if (sizeof($smilies))
4c79b5
	{
4c79b5
		foreach ($smilies as $row)
4c79b5
		{
4c79b5
			$template->assign_block_vars('smiley', array(
4c79b5
				'SMILEY_CODE'	=> $row['code'],
4c79b5
				'A_SMILEY_CODE'	=> addslashes($row['code']),
4c79b5
				'SMILEY_IMG'	=> $phpbb_root_path . $config['smilies_path'] . '/' . $row['smiley_url'],
4c79b5
				'SMILEY_WIDTH'	=> $row['smiley_width'],
4c79b5
				'SMILEY_HEIGHT'	=> $row['smiley_height'],
4c79b5
				'SMILEY_DESC'	=> $row['emotion'])
4c79b5
			);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	if ($mode == 'inline' && $display_link)
4c79b5
	{
4c79b5
		$template->assign_vars(array(
4c79b5
			'S_SHOW_SMILEY_LINK' 	=> true,
4c79b5
			'U_MORE_SMILIES' 		=> append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&f=' . $forum_id))
4c79b5
		);
4c79b5
	}
4c79b5
4c79b5
	if ($mode == 'window')
4c79b5
	{
4c79b5
		page_footer();
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Update last post information
4c79b5
* Should be used instead of sync() if only the last post information are out of sync... faster
4c79b5
*
4c79b5
* @param	string	$type				Can be forum|topic
4c79b5
* @param	mixed	$ids				topic/forum ids
4c79b5
* @param	bool	$return_update_sql	true: SQL query shall be returned, false: execute SQL
4c79b5
*/
4c79b5
function update_post_information($type, $ids, $return_update_sql = false)
4c79b5
{
4c79b5
	global $db;
4c79b5
4c79b5
	if (empty($ids))
4c79b5
	{
4c79b5
		return;
4c79b5
	}
4c79b5
	if (!is_array($ids))
4c79b5
	{
4c79b5
		$ids = array($ids);
4c79b5
	}
4c79b5
4c79b5
4c79b5
	$update_sql = $empty_forums = $not_empty_forums = array();
4c79b5
4c79b5
	if ($type != 'topic')
4c79b5
	{
4c79b5
		$topic_join = ', ' . TOPICS_TABLE . ' t';
4c79b5
		$topic_condition = 'AND t.topic_id = p.topic_id AND t.topic_approved = 1';
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		$topic_join = '';
4c79b5
		$topic_condition = '';
4c79b5
	}
4c79b5
4c79b5
	if (sizeof($ids) == 1)
4c79b5
	{
4c79b5
		$sql = 'SELECT MAX(p.post_id) as last_post_id
4c79b5
			FROM ' . POSTS_TABLE . " p $topic_join
4c79b5
			WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
4c79b5
				$topic_condition
4c79b5
				AND p.post_approved = 1";
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		$sql = 'SELECT p.' . $type . '_id, MAX(p.post_id) as last_post_id
4c79b5
			FROM ' . POSTS_TABLE . " p $topic_join
4c79b5
			WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
4c79b5
				$topic_condition
4c79b5
				AND p.post_approved = 1
4c79b5
			GROUP BY p.{$type}_id";
4c79b5
	}
4c79b5
	$result = $db->sql_query($sql);
4c79b5
4c79b5
	$last_post_ids = array();
4c79b5
	while ($row = $db->sql_fetchrow($result))
4c79b5
	{
4c79b5
		if (sizeof($ids) == 1)
4c79b5
		{
4c79b5
			$row[$type . '_id'] = $ids[0];
4c79b5
		}
4c79b5
4c79b5
		if ($type == 'forum')
4c79b5
		{
4c79b5
			$not_empty_forums[] = $row['forum_id'];
4c79b5
4c79b5
			if (empty($row['last_post_id']))
4c79b5
			{
4c79b5
				$empty_forums[] = $row['forum_id'];
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		$last_post_ids[] = $row['last_post_id'];
4c79b5
	}
4c79b5
	$db->sql_freeresult($result);
4c79b5
4c79b5
	if ($type == 'forum')
4c79b5
	{
4c79b5
		$empty_forums = array_merge($empty_forums, array_diff($ids, $not_empty_forums));
4c79b5
4c79b5
		foreach ($empty_forums as $void => $forum_id)
4c79b5
		{
4c79b5
			$update_sql[$forum_id][] = 'forum_last_post_id = 0';
4c79b5
			$update_sql[$forum_id][] = "forum_last_post_subject = ''";
4c79b5
			$update_sql[$forum_id][] = 'forum_last_post_time = 0';
4c79b5
			$update_sql[$forum_id][] = 'forum_last_poster_id = 0';
4c79b5
			$update_sql[$forum_id][] = "forum_last_poster_name = ''";
4c79b5
			$update_sql[$forum_id][] = "forum_last_poster_colour = ''";
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	if (sizeof($last_post_ids))
4c79b5
	{
4c79b5
		$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
4c79b5
			FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
4c79b5
			WHERE p.poster_id = u.user_id
4c79b5
				AND ' . $db->sql_in_set('p.post_id', $last_post_ids);
4c79b5
		$result = $db->sql_query($sql);
4c79b5
4c79b5
		while ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$update_sql[$row["{$type}_id"]][] = $type . '_last_post_id = ' . (int) $row['post_id'];
4c79b5
			$update_sql[$row["{$type}_id"]][] = "{$type}_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
4c79b5
			$update_sql[$row["{$type}_id"]][] = $type . '_last_post_time = ' . (int) $row['post_time'];
4c79b5
			$update_sql[$row["{$type}_id"]][] = $type . '_last_poster_id = ' . (int) $row['poster_id'];
4c79b5
			$update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
4c79b5
			$update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "'";
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
	}
4c79b5
	unset($empty_forums, $ids, $last_post_ids);
4c79b5
4c79b5
	if ($return_update_sql || !sizeof($update_sql))
4c79b5
	{
4c79b5
		return $update_sql;
4c79b5
	}
4c79b5
4c79b5
	$table = ($type == 'forum') ? FORUMS_TABLE : TOPICS_TABLE;
4c79b5
4c79b5
	foreach ($update_sql as $update_id => $update_sql_ary)
4c79b5
	{
4c79b5
		$sql = "UPDATE $table
4c79b5
			SET " . implode(', ', $update_sql_ary) . "
4c79b5
			WHERE {$type}_id = $update_id";
4c79b5
		$db->sql_query($sql);
4c79b5
	}
4c79b5
4c79b5
	return;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Generate Topic Icons for display
4c79b5
*/
4c79b5
function posting_gen_topic_icons($mode, $icon_id)
4c79b5
{
4c79b5
	global $phpbb_root_path, $config, $template, $cache;
4c79b5
4c79b5
	// Grab icons
4c79b5
	$icons = $cache->obtain_icons();
4c79b5
4c79b5
	if (!$icon_id)
4c79b5
	{
4c79b5
		$template->assign_var('S_NO_ICON_CHECKED', ' checked="checked"');
4c79b5
	}
4c79b5
4c79b5
	if (sizeof($icons))
4c79b5
	{
4c79b5
		foreach ($icons as $id => $data)
4c79b5
		{
4c79b5
			if ($data['display'])
4c79b5
			{
4c79b5
				$template->assign_block_vars('topic_icon', array(
4c79b5
					'ICON_ID'		=> $id,
4c79b5
					'ICON_IMG'		=> $phpbb_root_path . $config['icons_path'] . '/' . $data['img'],
4c79b5
					'ICON_WIDTH'	=> $data['width'],
4c79b5
					'ICON_HEIGHT'	=> $data['height'],
4c79b5
4c79b5
					'S_CHECKED'			=> ($id == $icon_id) ? true : false,
4c79b5
					'S_ICON_CHECKED'	=> ($id == $icon_id) ? ' checked="checked"' : '')
4c79b5
				);
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	return false;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Build topic types able to be selected
4c79b5
*/
4c79b5
function posting_gen_topic_types($forum_id, $cur_topic_type = POST_NORMAL)
4c79b5
{
4c79b5
	global $auth, $user, $template, $topic_type;
4c79b5
4c79b5
	$toggle = false;
4c79b5
4c79b5
	$topic_types = array(
4c79b5
		'sticky'	=> array('const' => POST_STICKY, 'lang' => 'POST_STICKY'),
4c79b5
		'announce'	=> array('const' => POST_ANNOUNCE, 'lang' => 'POST_ANNOUNCEMENT'),
4c79b5
		'global'	=> array('const' => POST_GLOBAL, 'lang' => 'POST_GLOBAL')
4c79b5
	);
4c79b5
4c79b5
	$topic_type_array = array();
4c79b5
4c79b5
	foreach ($topic_types as $auth_key => $topic_value)
4c79b5
	{
4c79b5
		// We do not have a special post global announcement permission
4c79b5
		$auth_key = ($auth_key == 'global') ? 'announce' : $auth_key;
4c79b5
4c79b5
		if ($auth->acl_get('f_' . $auth_key, $forum_id))
4c79b5
		{
4c79b5
			$toggle = true;
4c79b5
4c79b5
			$topic_type_array[] = array(
4c79b5
				'VALUE'			=> $topic_value['const'],
4c79b5
				'S_CHECKED'		=> ($cur_topic_type == $topic_value['const'] || ($forum_id == 0 && $topic_value['const'] == POST_GLOBAL)) ? ' checked="checked"' : '',
4c79b5
				'L_TOPIC_TYPE'	=> $user->lang[$topic_value['lang']]
4c79b5
			);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	if ($toggle)
4c79b5
	{
4c79b5
		$topic_type_array = array_merge(array(0 => array(
4c79b5
			'VALUE'			=> POST_NORMAL,
4c79b5
			'S_CHECKED'		=> ($topic_type == POST_NORMAL) ? ' checked="checked"' : '',
4c79b5
			'L_TOPIC_TYPE'	=> $user->lang['POST_NORMAL'])),
4c79b5
4c79b5
			$topic_type_array
4c79b5
		);
4c79b5
4c79b5
		foreach ($topic_type_array as $array)
4c79b5
		{
4c79b5
			$template->assign_block_vars('topic_type', $array);
4c79b5
		}
4c79b5
4c79b5
		$template->assign_vars(array(
4c79b5
			'S_TOPIC_TYPE_STICKY'	=> ($auth->acl_get('f_sticky', $forum_id)),
4c79b5
			'S_TOPIC_TYPE_ANNOUNCE'	=> ($auth->acl_get('f_announce', $forum_id)))
4c79b5
		);
4c79b5
	}
4c79b5
4c79b5
	return $toggle;
4c79b5
}
4c79b5
4c79b5
//
4c79b5
// Attachment related functions
4c79b5
//
4c79b5
4c79b5
/**
4c79b5
* Upload Attachment - filedata is generated here
4c79b5
* Uses upload class
4c79b5
*/
4c79b5
function upload_attachment($form_name, $forum_id, $local = false, $local_storage = '', $is_message = false, $local_filedata = false)
4c79b5
{
4c79b5
	global $auth, $user, $config, $db, $cache;
4c79b5
	global $phpbb_root_path, $phpEx;
4c79b5
4c79b5
	$filedata = array(
4c79b5
		'error'	=> array()
4c79b5
	);
4c79b5
4c79b5
	include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
4c79b5
	$upload = new fileupload();
4c79b5
4c79b5
	if ($config['check_attachment_content'])
4c79b5
	{
4c79b5
		$upload->set_disallowed_content(explode('|', $config['mime_triggers']));
4c79b5
	}
4c79b5
4c79b5
	if (!$local)
4c79b5
	{
4c79b5
		$filedata['post_attach'] = ($upload->is_valid($form_name)) ? true : false;
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		$filedata['post_attach'] = true;
4c79b5
	}
4c79b5
4c79b5
	if (!$filedata['post_attach'])
4c79b5
	{
4c79b5
		$filedata['error'][] = $user->lang['NO_UPLOAD_FORM_FOUND'];
4c79b5
		return $filedata;
4c79b5
	}
4c79b5
4c79b5
	$extensions = $cache->obtain_attach_extensions((($is_message) ? false : (int) $forum_id));
4c79b5
	$upload->set_allowed_extensions(array_keys($extensions['_allowed_']));
4c79b5
4c79b5
	$file = ($local) ? $upload->local_upload($local_storage, $local_filedata) : $upload->form_upload($form_name);
4c79b5
4c79b5
	if ($file->init_error)
4c79b5
	{
4c79b5
		$filedata['post_attach'] = false;
4c79b5
		return $filedata;
4c79b5
	}
4c79b5
4c79b5
	$cat_id = (isset($extensions[$file->get('extension')]['display_cat'])) ? $extensions[$file->get('extension')]['display_cat'] : ATTACHMENT_CATEGORY_NONE;
4c79b5
4c79b5
	// Make sure the image category only holds valid images...
4c79b5
	if ($cat_id == ATTACHMENT_CATEGORY_IMAGE && !$file->is_image())
4c79b5
	{
4c79b5
		$file->remove();
4c79b5
4c79b5
		// If this error occurs a user tried to exploit an IE Bug by renaming extensions
4c79b5
		// Since the image category is displaying content inline we need to catch this.
4c79b5
		trigger_error($user->lang['ATTACHED_IMAGE_NOT_IMAGE']);
4c79b5
	}
4c79b5
4c79b5
	// Do we have to create a thumbnail?
4c79b5
	$filedata['thumbnail'] = ($cat_id == ATTACHMENT_CATEGORY_IMAGE && $config['img_create_thumbnail']) ? 1 : 0;
4c79b5
4c79b5
	// Check Image Size, if it is an image
4c79b5
	if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id) && $cat_id == ATTACHMENT_CATEGORY_IMAGE)
4c79b5
	{
4c79b5
		$file->upload->set_allowed_dimensions(0, 0, $config['img_max_width'], $config['img_max_height']);
4c79b5
	}
4c79b5
4c79b5
	// Admins and mods are allowed to exceed the allowed filesize
4c79b5
	if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id))
4c79b5
	{
4c79b5
		if (!empty($extensions[$file->get('extension')]['max_filesize']))
4c79b5
		{
4c79b5
			$allowed_filesize = $extensions[$file->get('extension')]['max_filesize'];
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$allowed_filesize = ($is_message) ? $config['max_filesize_pm'] : $config['max_filesize'];
4c79b5
		}
4c79b5
4c79b5
		$file->upload->set_max_filesize($allowed_filesize);
4c79b5
	}
4c79b5
4c79b5
	$file->clean_filename('unique', $user->data['user_id'] . '_');
4c79b5
4c79b5
	// Are we uploading an image *and* this image being within the image category? Only then perform additional image checks.
4c79b5
	$no_image = ($cat_id == ATTACHMENT_CATEGORY_IMAGE) ? false : true;
4c79b5
4c79b5
	$file->move_file($config['upload_path'], false, $no_image);
4c79b5
4c79b5
	if (sizeof($file->error))
4c79b5
	{
4c79b5
		$file->remove();
4c79b5
		$filedata['error'] = array_merge($filedata['error'], $file->error);
4c79b5
		$filedata['post_attach'] = false;
4c79b5
4c79b5
		return $filedata;
4c79b5
	}
4c79b5
4c79b5
	$filedata['filesize'] = $file->get('filesize');
4c79b5
	$filedata['mimetype'] = $file->get('mimetype');
4c79b5
	$filedata['extension'] = $file->get('extension');
4c79b5
	$filedata['physical_filename'] = $file->get('realname');
4c79b5
	$filedata['real_filename'] = $file->get('uploadname');
4c79b5
	$filedata['filetime'] = time();
4c79b5
4c79b5
	// Check our complete quota
4c79b5
	if ($config['attachment_quota'])
4c79b5
	{
4c79b5
		if ($config['upload_dir_size'] + $file->get('filesize') > $config['attachment_quota'])
4c79b5
		{
4c79b5
			$filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
4c79b5
			$filedata['post_attach'] = false;
4c79b5
4c79b5
			$file->remove();
4c79b5
4c79b5
			return $filedata;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Check free disk space
4c79b5
	if ($free_space = @disk_free_space($phpbb_root_path . $config['upload_path']))
4c79b5
	{
4c79b5
		if ($free_space <= $file->get('filesize'))
4c79b5
		{
4c79b5
			$filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
4c79b5
			$filedata['post_attach'] = false;
4c79b5
4c79b5
			$file->remove();
4c79b5
4c79b5
			return $filedata;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Create Thumbnail
4c79b5
	if ($filedata['thumbnail'])
4c79b5
	{
4c79b5
		$source = $file->get('destination_file');
4c79b5
		$destination = $file->get('destination_path') . '/thumb_' . $file->get('realname');
4c79b5
4c79b5
		if (!create_thumbnail($source, $destination, $file->get('mimetype')))
4c79b5
		{
4c79b5
			$filedata['thumbnail'] = 0;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	return $filedata;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Calculate the needed size for Thumbnail
4c79b5
*/
4c79b5
function get_img_size_format($width, $height)
4c79b5
{
4c79b5
	global $config;
4c79b5
4c79b5
	// Maximum Width the Image can take
4c79b5
	$max_width = ($config['img_max_thumb_width']) ? $config['img_max_thumb_width'] : 400;
4c79b5
4c79b5
	if ($width > $height)
4c79b5
	{
4c79b5
		return array(
4c79b5
			round($width * ($max_width / $width)),
4c79b5
			round($height * ($max_width / $width))
4c79b5
		);
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		return array(
4c79b5
			round($width * ($max_width / $height)),
4c79b5
			round($height * ($max_width / $height))
4c79b5
		);
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Return supported image types
4c79b5
*/
4c79b5
function get_supported_image_types($type = false)
4c79b5
{
4c79b5
	if (@extension_loaded('gd'))
4c79b5
	{
4c79b5
		$format = imagetypes();
4c79b5
		$new_type = 0;
4c79b5
4c79b5
		if ($type !== false)
4c79b5
		{
4c79b5
			// Type is one of the IMAGETYPE constants - it is fetched from getimagesize()
4c79b5
			// We do not use the constants here, because some were not available in PHP 4.3.x
4c79b5
			switch ($type)
4c79b5
			{
4c79b5
				// GIF
4c79b5
				case 1:
4c79b5
					$new_type = ($format & IMG_GIF) ? IMG_GIF : false;
4c79b5
				break;
4c79b5
4c79b5
				// JPG, JPC, JP2
4c79b5
				case 2:
4c79b5
				case 9:
4c79b5
				case 10:
4c79b5
				case 11:
4c79b5
				case 12:
4c79b5
					$new_type = ($format & IMG_JPG) ? IMG_JPG : false;
4c79b5
				break;
4c79b5
4c79b5
				// PNG
4c79b5
				case 3:
4c79b5
					$new_type = ($format & IMG_PNG) ? IMG_PNG : false;
4c79b5
				break;
4c79b5
4c79b5
				// WBMP
4c79b5
				case 15:
4c79b5
					$new_type = ($format & IMG_WBMP) ? IMG_WBMP : false;
4c79b5
				break;
4c79b5
			}
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$new_type = array();
4c79b5
			$go_through_types = array(IMG_GIF, IMG_JPG, IMG_PNG, IMG_WBMP);
4c79b5
4c79b5
			foreach ($go_through_types as $check_type)
4c79b5
			{
4c79b5
				if ($format & $check_type)
4c79b5
				{
4c79b5
					$new_type[] = $check_type;
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		return array(
4c79b5
			'gd'		=> ($new_type) ? true : false,
4c79b5
			'format'	=> $new_type,
4c79b5
			'version'	=> (function_exists('imagecreatetruecolor')) ? 2 : 1
4c79b5
		);
4c79b5
	}
4c79b5
4c79b5
	return array('gd' => false);
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Create Thumbnail
4c79b5
*/
4c79b5
function create_thumbnail($source, $destination, $mimetype)
4c79b5
{
4c79b5
	global $config;
4c79b5
4c79b5
	$min_filesize = (int) $config['img_min_thumb_filesize'];
4c79b5
	$img_filesize = (file_exists($source)) ? @filesize($source) : false;
4c79b5
4c79b5
	if (!$img_filesize || $img_filesize <= $min_filesize)
4c79b5
	{
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	$dimension = @getimagesize($source);
4c79b5
4c79b5
	if ($dimension === false)
4c79b5
	{
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	list($width, $height, $type, ) = $dimension;
4c79b5
4c79b5
	if (empty($width) || empty($height))
4c79b5
	{
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	list($new_width, $new_height) = get_img_size_format($width, $height);
4c79b5
4c79b5
	// Do not create a thumbnail if the resulting width/height is bigger than the original one
4c79b5
	if ($new_width > $width && $new_height > $height)
4c79b5
	{
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	$used_imagick = false;
4c79b5
4c79b5
	// Only use imagemagick if defined and the passthru function not disabled
4c79b5
	if ($config['img_imagick'] && function_exists('passthru'))
4c79b5
	{
4c79b5
		if (substr($config['img_imagick'], -1) !== '/')
4c79b5
		{
4c79b5
			$config['img_imagick'] .= '/';
4c79b5
		}
4c79b5
4c79b5
		@passthru(escapeshellcmd($config['img_imagick']) . 'convert' . ((defined('PHP_OS') && preg_match('#^win#i', PHP_OS)) ? '.exe' : '') . ' -quality 85 -antialias -sample ' . $new_width . 'x' . $new_height . ' "' . str_replace('\\', '/', $source) . '" +profile "*" "' . str_replace('\\', '/', $destination) . '"');
4c79b5
4c79b5
		if (file_exists($destination))
4c79b5
		{
4c79b5
			$used_imagick = true;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	if (!$used_imagick)
4c79b5
	{
4c79b5
		$type = get_supported_image_types($type);
4c79b5
4c79b5
		if ($type['gd'])
4c79b5
		{
4c79b5
			// If the type is not supported, we are not able to create a thumbnail
4c79b5
			if ($type['format'] === false)
4c79b5
			{
4c79b5
				return false;
4c79b5
			}
4c79b5
4c79b5
			switch ($type['format'])
4c79b5
			{
4c79b5
				case IMG_GIF:
4c79b5
					$image = @imagecreatefromgif($source);
4c79b5
				break;
4c79b5
4c79b5
				case IMG_JPG:
4c79b5
					$image = @imagecreatefromjpeg($source);
4c79b5
				break;
4c79b5
4c79b5
				case IMG_PNG:
4c79b5
					$image = @imagecreatefrompng($source);
4c79b5
				break;
4c79b5
4c79b5
				case IMG_WBMP:
4c79b5
					$image = @imagecreatefromwbmp($source);
4c79b5
				break;
4c79b5
			}
4c79b5
4c79b5
			if ($type['version'] == 1)
4c79b5
			{
4c79b5
				$new_image = imagecreate($new_width, $new_height);
4c79b5
4c79b5
				if ($new_image === false)
4c79b5
				{
4c79b5
					return false;
4c79b5
				}
4c79b5
4c79b5
				imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$new_image = imagecreatetruecolor($new_width, $new_height);
4c79b5
4c79b5
				if ($new_image === false)
4c79b5
				{
4c79b5
					return false;
4c79b5
				}
4c79b5
4c79b5
				// Preserve alpha transparency (png for example)
4c79b5
				@imagealphablending($new_image, false);
4c79b5
				@imagesavealpha($new_image, true);
4c79b5
4c79b5
				imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
4c79b5
			}
4c79b5
4c79b5
			// If we are in safe mode create the destination file prior to using the gd functions to circumvent a PHP bug
4c79b5
			if (@ini_get('safe_mode') || @strtolower(ini_get('safe_mode')) == 'on')
4c79b5
			{
4c79b5
				@touch($destination);
4c79b5
			}
4c79b5
4c79b5
			switch ($type['format'])
4c79b5
			{
4c79b5
				case IMG_GIF:
4c79b5
					imagegif($new_image, $destination);
4c79b5
				break;
4c79b5
4c79b5
				case IMG_JPG:
4c79b5
					imagejpeg($new_image, $destination, 90);
4c79b5
				break;
4c79b5
4c79b5
				case IMG_PNG:
4c79b5
					imagepng($new_image, $destination);
4c79b5
				break;
4c79b5
4c79b5
				case IMG_WBMP:
4c79b5
					imagewbmp($new_image, $destination);
4c79b5
				break;
4c79b5
			}
4c79b5
4c79b5
			imagedestroy($new_image);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			return false;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	if (!file_exists($destination))
4c79b5
	{
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);
4c79b5
4c79b5
	return true;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Assign Inline attachments (build option fields)
4c79b5
*/
4c79b5
function posting_gen_inline_attachments(&$attachment_data)
4c79b5
{
4c79b5
	global $template;
4c79b5
4c79b5
	if (sizeof($attachment_data))
4c79b5
	{
4c79b5
		$s_inline_attachment_options = '';
4c79b5
4c79b5
		foreach ($attachment_data as $i => $attachment)
4c79b5
		{
4c79b5
			$s_inline_attachment_options .= '<option value="' . $i . '">' . basename($attachment['real_filename']) . '</option>';
4c79b5
		}
4c79b5
4c79b5
		$template->assign_var('S_INLINE_ATTACHMENT_OPTIONS', $s_inline_attachment_options);
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	return false;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Generate inline attachment entry
4c79b5
*/
4c79b5
function posting_gen_attachment_entry($attachment_data, &$filename_data, $show_attach_box = true)
4c79b5
{
4c79b5
	global $template, $config, $phpbb_root_path, $phpEx, $user, $auth;
4c79b5
4c79b5
	// Some default template variables
4c79b5
	$template->assign_vars(array(
4c79b5
		'S_SHOW_ATTACH_BOX'	=> $show_attach_box,
4c79b5
		'S_HAS_ATTACHMENTS'	=> sizeof($attachment_data),
4c79b5
		'FILESIZE'			=> $config['max_filesize'],
4c79b5
		'FILE_COMMENT'		=> (isset($filename_data['filecomment'])) ? $filename_data['filecomment'] : '',
4c79b5
	));
4c79b5
4c79b5
	if (sizeof($attachment_data))
4c79b5
	{
4c79b5
		// We display the posted attachments within the desired order.
4c79b5
		($config['display_order']) ? krsort($attachment_data) : ksort($attachment_data);
4c79b5
4c79b5
		foreach ($attachment_data as $count => $attach_row)
4c79b5
		{
4c79b5
			$hidden = '';
4c79b5
			$attach_row['real_filename'] = basename($attach_row['real_filename']);
4c79b5
4c79b5
			foreach ($attach_row as $key => $value)
4c79b5
			{
4c79b5
				$hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />';
4c79b5
			}
4c79b5
4c79b5
			$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);
4c79b5
4c79b5
			$template->assign_block_vars('attach_row', array(
4c79b5
				'FILENAME'			=> basename($attach_row['real_filename']),
4c79b5
				'A_FILENAME'		=> addslashes(basename($attach_row['real_filename'])),
4c79b5
				'FILE_COMMENT'		=> $attach_row['attach_comment'],
4c79b5
				'ATTACH_ID'			=> $attach_row['attach_id'],
4c79b5
				'S_IS_ORPHAN'		=> $attach_row['is_orphan'],
4c79b5
				'ASSOC_INDEX'		=> $count,
4c79b5
4c79b5
				'U_VIEW_ATTACHMENT'	=> $download_link,
4c79b5
				'S_HIDDEN'			=> $hidden)
4c79b5
			);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	return sizeof($attachment_data);
4c79b5
}
4c79b5
4c79b5
//
4c79b5
// General Post functions
4c79b5
//
4c79b5
4c79b5
/**
4c79b5
* Load Drafts
4c79b5
*/
4c79b5
function load_drafts($topic_id = 0, $forum_id = 0, $id = 0)
4c79b5
{
4c79b5
	global $user, $db, $template, $auth;
4c79b5
	global $phpbb_root_path, $phpEx;
4c79b5
4c79b5
	$topic_ids = $forum_ids = $draft_rows = array();
4c79b5
4c79b5
	// Load those drafts not connected to forums/topics
4c79b5
	// If forum_id == 0 AND topic_id == 0 then this is a PM draft
4c79b5
	if (!$topic_id && !$forum_id)
4c79b5
	{
4c79b5
		$sql_and = ' AND d.forum_id = 0 AND d.topic_id = 0';
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		$sql_and = '';
4c79b5
		$sql_and .= ($forum_id) ? ' AND d.forum_id = ' . (int) $forum_id : '';
4c79b5
		$sql_and .= ($topic_id) ? ' AND d.topic_id = ' . (int) $topic_id : '';
4c79b5
	}
4c79b5
4c79b5
	$sql = 'SELECT d.*, f.forum_id, f.forum_name
4c79b5
		FROM ' . DRAFTS_TABLE . ' d
4c79b5
		LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = d.forum_id)
4c79b5
			WHERE d.user_id = ' . $user->data['user_id'] . "
4c79b5
			$sql_and
4c79b5
		ORDER BY d.save_time DESC";
4c79b5
	$result = $db->sql_query($sql);
4c79b5
4c79b5
	while ($row = $db->sql_fetchrow($result))
4c79b5
	{
4c79b5
		if ($row['topic_id'])
4c79b5
		{
4c79b5
			$topic_ids[] = (int) $row['topic_id'];
4c79b5
		}
4c79b5
		$draft_rows[] = $row;
4c79b5
	}
4c79b5
	$db->sql_freeresult($result);
4c79b5
4c79b5
	if (!sizeof($draft_rows))
4c79b5
	{
4c79b5
		return;
4c79b5
	}
4c79b5
4c79b5
	$topic_rows = array();
4c79b5
	if (sizeof($topic_ids))
4c79b5
	{
4c79b5
		$sql = 'SELECT topic_id, forum_id, topic_title
4c79b5
			FROM ' . TOPICS_TABLE . '
4c79b5
			WHERE ' . $db->sql_in_set('topic_id', array_unique($topic_ids));
4c79b5
		$result = $db->sql_query($sql);
4c79b5
4c79b5
		while ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$topic_rows[$row['topic_id']] = $row;
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
	}
4c79b5
	unset($topic_ids);
4c79b5
4c79b5
	$template->assign_var('S_SHOW_DRAFTS', true);
4c79b5
4c79b5
	foreach ($draft_rows as $draft)
4c79b5
	{
4c79b5
		$link_topic = $link_forum = $link_pm = false;
4c79b5
		$insert_url = $view_url = $title = '';
4c79b5
4c79b5
		if (isset($topic_rows[$draft['topic_id']])
4c79b5
			&& (
4c79b5
				($topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_get('f_read', $topic_rows[$draft['topic_id']]['forum_id']))
4c79b5
				||
4c79b5
				(!$topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_getf_global('f_read'))
4c79b5
			))
4c79b5
		{
4c79b5
			$topic_forum_id = ($topic_rows[$draft['topic_id']]['forum_id']) ? $topic_rows[$draft['topic_id']]['forum_id'] : $forum_id;
4c79b5
4c79b5
			$link_topic = true;
4c79b5
			$view_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topic_forum_id . '&t=' . $draft['topic_id']);
4c79b5
			$title = $topic_rows[$draft['topic_id']]['topic_title'];
4c79b5
4c79b5
			$insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $topic_forum_id . '&t=' . $draft['topic_id'] . '&mode=reply&d=' . $draft['draft_id']);
4c79b5
		}
4c79b5
		else if ($draft['forum_id'] && $auth->acl_get('f_read', $draft['forum_id']))
4c79b5
		{
4c79b5
			$link_forum = true;
4c79b5
			$view_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $draft['forum_id']);
4c79b5
			$title = $draft['forum_name'];
4c79b5
4c79b5
			$insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $draft['forum_id'] . '&mode=post&d=' . $draft['draft_id']);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			// Either display as PM draft if forum_id and topic_id are empty or if access to the forums has been denied afterwards...
4c79b5
			$link_pm = true;
4c79b5
			$insert_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&mode=compose&d={$draft['draft_id']}");
4c79b5
		}
4c79b5
4c79b5
		$template->assign_block_vars('draftrow', array(
4c79b5
			'DRAFT_ID'		=> $draft['draft_id'],
4c79b5
			'DATE'			=> $user->format_date($draft['save_time']),
4c79b5
			'DRAFT_SUBJECT'	=> $draft['draft_subject'],
4c79b5
4c79b5
			'TITLE'			=> $title,
4c79b5
			'U_VIEW'		=> $view_url,
4c79b5
			'U_INSERT'		=> $insert_url,
4c79b5
4c79b5
			'S_LINK_PM'		=> $link_pm,
4c79b5
			'S_LINK_TOPIC'	=> $link_topic,
4c79b5
			'S_LINK_FORUM'	=> $link_forum)
4c79b5
		);
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Topic Review
4c79b5
*/
4c79b5
function topic_review($topic_id, $forum_id, $mode = 'topic_review', $cur_post_id = 0, $show_quote_button = true)
4c79b5
{
4c79b5
	global $user, $auth, $db, $template, $bbcode, $cache;
4c79b5
	global $config, $phpbb_root_path, $phpEx;
4c79b5
4c79b5
	// Go ahead and pull all data for this topic
4c79b5
	$sql = 'SELECT p.post_id
4c79b5
		FROM ' . POSTS_TABLE . ' p' . "
4c79b5
		WHERE p.topic_id = $topic_id
4c79b5
			" . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . '
4c79b5
			' . (($mode == 'post_review') ? " AND p.post_id > $cur_post_id" : '') . '
4c79b5
		ORDER BY p.post_time ';
4c79b5
	$sql .= ($mode == 'post_review') ? 'ASC' : 'DESC';
4c79b5
	$result = $db->sql_query_limit($sql, $config['posts_per_page']);
4c79b5
4c79b5
	$post_list = array();
4c79b5
4c79b5
	while ($row = $db->sql_fetchrow($result))
4c79b5
	{
4c79b5
		$post_list[] = $row['post_id'];
4c79b5
	}
4c79b5
4c79b5
	$db->sql_freeresult($result);
4c79b5
4c79b5
	if (!sizeof($post_list))
4c79b5
	{
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	$sql = $db->sql_build_query('SELECT', array(
4c79b5
		'SELECT'	=> 'u.username, u.user_id, u.user_colour, p.*',
4c79b5
4c79b5
		'FROM'		=> array(
4c79b5
			USERS_TABLE		=> 'u',
4c79b5
			POSTS_TABLE		=> 'p',
4c79b5
		),
4c79b5
4c79b5
		'WHERE'		=> $db->sql_in_set('p.post_id', $post_list) . '
4c79b5
			AND u.user_id = p.poster_id'
4c79b5
	));
4c79b5
4c79b5
	$result = $db->sql_query($sql);
4c79b5
4c79b5
	$bbcode_bitfield = '';
4c79b5
	$rowset = array();
4c79b5
	$has_attachments = false;
4c79b5
	while ($row = $db->sql_fetchrow($result))
4c79b5
	{
4c79b5
		$rowset[$row['post_id']] = $row;
4c79b5
		$bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
4c79b5
4c79b5
		if ($row['post_attachment'])
4c79b5
		{
4c79b5
			$has_attachments = true;
4c79b5
		}
4c79b5
	}
4c79b5
	$db->sql_freeresult($result);
4c79b5
4c79b5
	// Instantiate BBCode class
4c79b5
	if (!isset($bbcode) && $bbcode_bitfield !== '')
4c79b5
	{
4c79b5
		include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
4c79b5
		$bbcode = new bbcode(base64_encode($bbcode_bitfield));
4c79b5
	}
4c79b5
4c79b5
	// Grab extensions
4c79b5
	$extensions = $attachments = array();
4c79b5
	if ($has_attachments && $auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
4c79b5
	{
4c79b5
		$extensions = $cache->obtain_attach_extensions($forum_id);
4c79b5
4c79b5
		// Get attachments...
4c79b5
		$sql = 'SELECT *
4c79b5
			FROM ' . ATTACHMENTS_TABLE . '
4c79b5
			WHERE ' . $db->sql_in_set('post_msg_id', $post_list) . '
4c79b5
				AND in_message = 0
4c79b5
			ORDER BY filetime DESC, post_msg_id ASC';
4c79b5
		$result = $db->sql_query($sql);
4c79b5
4c79b5
		while ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$attachments[$row['post_msg_id']][] = $row;
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
	}
4c79b5
4c79b5
	for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
4c79b5
	{
4c79b5
		// A non-existing rowset only happens if there was no user present for the entered poster_id
4c79b5
		// This could be a broken posts table.
4c79b5
		if (!isset($rowset[$post_list[$i]]))
4c79b5
		{
4c79b5
			continue;
4c79b5
		}
4c79b5
4c79b5
		$row =& $rowset[$post_list[$i]];
4c79b5
4c79b5
		$poster_id		= $row['user_id'];
4c79b5
		$post_subject	= $row['post_subject'];
4c79b5
		$message		= censor_text($row['post_text']);
4c79b5
4c79b5
		$decoded_message = false;
4c79b5
4c79b5
		if ($show_quote_button && $auth->acl_get('f_reply', $forum_id))
4c79b5
		{
4c79b5
			$decoded_message = $message;
4c79b5
			decode_message($decoded_message, $row['bbcode_uid']);
4c79b5
4c79b5
			$decoded_message = bbcode_nl2br($decoded_message);
4c79b5
		}
4c79b5
4c79b5
		if ($row['bbcode_bitfield'])
4c79b5
		{
4c79b5
			$bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
4c79b5
		}
4c79b5
4c79b5
		$message = bbcode_nl2br($message);
4c79b5
		$message = smiley_text($message, !$row['enable_smilies']);
4c79b5
4c79b5
		if (!empty($attachments[$row['post_id']]))
4c79b5
		{
4c79b5
			$update_count = array();
4c79b5
			parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
4c79b5
		}
4c79b5
4c79b5
		$post_subject = censor_text($post_subject);
4c79b5
4c79b5
		$template->assign_block_vars($mode . '_row', array(
4c79b5
			'POST_AUTHOR_FULL'		=> get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
4c79b5
			'POST_AUTHOR_COLOUR'	=> get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
4c79b5
			'POST_AUTHOR'			=> get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
4c79b5
			'U_POST_AUTHOR'			=> get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
4c79b5
4c79b5
			'S_HAS_ATTACHMENTS'	=> (!empty($attachments[$row['post_id']])) ? true : false,
4c79b5
4c79b5
			'POST_SUBJECT'		=> $post_subject,
4c79b5
			'MINI_POST_IMG'		=> $user->img('icon_post_target', $user->lang['POST']),
4c79b5
			'POST_DATE'			=> $user->format_date($row['post_time']),
4c79b5
			'MESSAGE'			=> $message,
4c79b5
			'DECODED_MESSAGE'	=> $decoded_message,
4c79b5
			'POST_ID'			=> $row['post_id'],
4c79b5
			'U_MINI_POST'		=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . '#p' . $row['post_id'],
4c79b5
			'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) : '',
4c79b5
			'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'])) : '')
4c79b5
		);
4c79b5
4c79b5
		// Display not already displayed Attachments for this post, we already parsed them. ;)
4c79b5
		if (!empty($attachments[$row['post_id']]))
4c79b5
		{
4c79b5
			foreach ($attachments[$row['post_id']] as $attachment)
4c79b5
			{
4c79b5
				$template->assign_block_vars($mode . '_row.attachment', array(
4c79b5
					'DISPLAY_ATTACHMENT'	=> $attachment)
4c79b5
				);
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		unset($rowset[$i]);
4c79b5
	}
4c79b5
4c79b5
	if ($mode == 'topic_review')
4c79b5
	{
4c79b5
		$template->assign_var('QUOTE_IMG', $user->img('icon_post_quote', $user->lang['REPLY_WITH_QUOTE']));
4c79b5
	}
4c79b5
4c79b5
	return true;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* User Notification
4c79b5
*/
4c79b5
function user_notification($mode, $subject, $topic_title, $forum_name, $forum_id, $topic_id, $post_id)
4c79b5
{
4c79b5
	global $db, $user, $config, $phpbb_root_path, $phpEx, $auth;
4c79b5
4c79b5
	$topic_notification = ($mode == 'reply' || $mode == 'quote') ? true : false;
4c79b5
	$forum_notification = ($mode == 'post') ? true : false;
4c79b5
4c79b5
	if (!$topic_notification && !$forum_notification)
4c79b5
	{
4c79b5
		trigger_error('WRONG_NOTIFICATION_MODE');
4c79b5
	}
4c79b5
4c79b5
	if (($topic_notification && !$config['allow_topic_notify']) || ($forum_notification && !$config['allow_forum_notify']))
4c79b5
	{
4c79b5
		return;
4c79b5
	}
4c79b5
4c79b5
	$topic_title = ($topic_notification) ? $topic_title : $subject;
4c79b5
	$topic_title = censor_text($topic_title);
4c79b5
4c79b5
	// Get banned User ID's
4c79b5
	$sql = 'SELECT ban_userid
4c79b5
		FROM ' . BANLIST_TABLE . '
4c79b5
		WHERE ban_userid <> 0
4c79b5
			AND ban_exclude <> 1';
4c79b5
	$result = $db->sql_query($sql);
4c79b5
4c79b5
	$sql_ignore_users = ANONYMOUS . ', ' . $user->data['user_id'];
4c79b5
	while ($row = $db->sql_fetchrow($result))
4c79b5
	{
4c79b5
		$sql_ignore_users .= ', ' . (int) $row['ban_userid'];
4c79b5
	}
4c79b5
	$db->sql_freeresult($result);
4c79b5
4c79b5
	$notify_rows = array();
4c79b5
4c79b5
	// -- get forum_userids	|| topic_userids
4c79b5
	$sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
4c79b5
		FROM ' . (($topic_notification) ? TOPICS_WATCH_TABLE : FORUMS_WATCH_TABLE) . ' w, ' . USERS_TABLE . ' u
4c79b5
		WHERE w.' . (($topic_notification) ? 'topic_id' : 'forum_id') . ' = ' . (($topic_notification) ? $topic_id : $forum_id) . "
4c79b5
			AND w.user_id NOT IN ($sql_ignore_users)
4c79b5
			AND w.notify_status = 0
4c79b5
			AND u.user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')
4c79b5
			AND u.user_id = w.user_id';
4c79b5
	$result = $db->sql_query($sql);
4c79b5
4c79b5
	while ($row = $db->sql_fetchrow($result))
4c79b5
	{
4c79b5
		$notify_rows[$row['user_id']] = array(
4c79b5
			'user_id'		=> $row['user_id'],
4c79b5
			'username'		=> $row['username'],
4c79b5
			'user_email'	=> $row['user_email'],
4c79b5
			'user_jabber'	=> $row['user_jabber'],
4c79b5
			'user_lang'		=> $row['user_lang'],
4c79b5
			'notify_type'	=> ($topic_notification) ? 'topic' : 'forum',
4c79b5
			'template'		=> ($topic_notification) ? 'topic_notify' : 'newtopic_notify',
4c79b5
			'method'		=> $row['user_notify_type'],
4c79b5
			'allowed'		=> false
4c79b5
		);
4c79b5
	}
4c79b5
	$db->sql_freeresult($result);
4c79b5
4c79b5
	// forum notification is sent to those not already receiving topic notifications
4c79b5
	if ($topic_notification)
4c79b5
	{
4c79b5
		if (sizeof($notify_rows))
4c79b5
		{
4c79b5
			$sql_ignore_users .= ', ' . implode(', ', array_keys($notify_rows));
4c79b5
		}
4c79b5
4c79b5
		$sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
4c79b5
			FROM ' . FORUMS_WATCH_TABLE . ' fw, ' . USERS_TABLE . " u
4c79b5
			WHERE fw.forum_id = $forum_id
4c79b5
				AND fw.user_id NOT IN ($sql_ignore_users)
4c79b5
				AND fw.notify_status = 0
4c79b5
				AND u.user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')
4c79b5
				AND u.user_id = fw.user_id';
4c79b5
		$result = $db->sql_query($sql);
4c79b5
4c79b5
		while ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$notify_rows[$row['user_id']] = array(
4c79b5
				'user_id'		=> $row['user_id'],
4c79b5
				'username'		=> $row['username'],
4c79b5
				'user_email'	=> $row['user_email'],
4c79b5
				'user_jabber'	=> $row['user_jabber'],
4c79b5
				'user_lang'		=> $row['user_lang'],
4c79b5
				'notify_type'	=> 'forum',
4c79b5
				'template'		=> 'forum_notify',
4c79b5
				'method'		=> $row['user_notify_type'],
4c79b5
				'allowed'		=> false
4c79b5
			);
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
	}
4c79b5
4c79b5
	if (!sizeof($notify_rows))
4c79b5
	{
4c79b5
		return;
4c79b5
	}
4c79b5
4c79b5
	// Make sure users are allowed to read the forum
4c79b5
	foreach ($auth->acl_get_list(array_keys($notify_rows), 'f_read', $forum_id) as $forum_id => $forum_ary)
4c79b5
	{
4c79b5
		foreach ($forum_ary as $auth_option => $user_ary)
4c79b5
		{
4c79b5
			foreach ($user_ary as $user_id)
4c79b5
			{
4c79b5
				$notify_rows[$user_id]['allowed'] = true;
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
4c79b5
	// Now, we have to do a little step before really sending, we need to distinguish our users a little bit. ;)
4c79b5
	$msg_users = $delete_ids = $update_notification = array();
4c79b5
	foreach ($notify_rows as $user_id => $row)
4c79b5
	{
4c79b5
		if (!$row['allowed'] || !trim($row['user_email']))
4c79b5
		{
4c79b5
			$delete_ids[$row['notify_type']][] = $row['user_id'];
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$msg_users[] = $row;
4c79b5
			$update_notification[$row['notify_type']][] = $row['user_id'];
4c79b5
		}
4c79b5
	}
4c79b5
	unset($notify_rows);
4c79b5
4c79b5
	// Now, we are able to really send out notifications
4c79b5
	if (sizeof($msg_users))
4c79b5
	{
4c79b5
		include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
4c79b5
		$messenger = new messenger();
4c79b5
4c79b5
		$msg_list_ary = array();
4c79b5
		foreach ($msg_users as $row)
4c79b5
		{
4c79b5
			$pos = (!isset($msg_list_ary[$row['template']])) ? 0 : sizeof($msg_list_ary[$row['template']]);
4c79b5
4c79b5
			$msg_list_ary[$row['template']][$pos]['method']	= $row['method'];
4c79b5
			$msg_list_ary[$row['template']][$pos]['email']	= $row['user_email'];
4c79b5
			$msg_list_ary[$row['template']][$pos]['jabber']	= $row['user_jabber'];
4c79b5
			$msg_list_ary[$row['template']][$pos]['name']	= $row['username'];
4c79b5
			$msg_list_ary[$row['template']][$pos]['lang']	= $row['user_lang'];
4c79b5
			$msg_list_ary[$row['template']][$pos]['user_id']= $row['user_id'];
4c79b5
		}
4c79b5
		unset($msg_users);
4c79b5
4c79b5
		foreach ($msg_list_ary as $email_template => $email_list)
4c79b5
		{
4c79b5
			foreach ($email_list as $addr)
4c79b5
			{
4c79b5
				$messenger->template($email_template, $addr['lang']);
4c79b5
4c79b5
				$messenger->to($addr['email'], $addr['name']);
4c79b5
				$messenger->im($addr['jabber'], $addr['name']);
4c79b5
4c79b5
				$messenger->assign_vars(array(
4c79b5
					'USERNAME'		=> htmlspecialchars_decode($addr['name']),
4c79b5
					'TOPIC_TITLE'	=> htmlspecialchars_decode($topic_title),
4c79b5
					'FORUM_NAME'	=> htmlspecialchars_decode($forum_name),
4c79b5
4c79b5
					'U_FORUM'				=> generate_board_url() . "/viewforum.$phpEx?f=$forum_id",
4c79b5
					'U_TOPIC'				=> generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id",
4c79b5
					'U_NEWEST_POST'			=> generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id&p=$post_id&e=$post_id",
4c79b5
					'U_STOP_WATCHING_TOPIC'	=> generate_board_url() . "/viewtopic.$phpEx?uid={$addr['user_id']}&f=$forum_id&t=$topic_id&unwatch=topic",
4c79b5
					'U_STOP_WATCHING_FORUM'	=> generate_board_url() . "/viewforum.$phpEx?uid={$addr['user_id']}&f=$forum_id&unwatch=forum",
4c79b5
				));
4c79b5
4c79b5
				$messenger->send($addr['method']);
4c79b5
			}
4c79b5
		}
4c79b5
		unset($msg_list_ary);
4c79b5
4c79b5
		$messenger->save_queue();
4c79b5
	}
4c79b5
4c79b5
	// Handle the DB updates
4c79b5
	$db->sql_transaction('begin');
4c79b5
4c79b5
	if (!empty($update_notification['topic']))
4c79b5
	{
4c79b5
		$sql = 'UPDATE ' . TOPICS_WATCH_TABLE . "
4c79b5
			SET notify_status = 1
4c79b5
			WHERE topic_id = $topic_id
4c79b5
				AND " . $db->sql_in_set('user_id', $update_notification['topic']);
4c79b5
		$db->sql_query($sql);
4c79b5
	}
4c79b5
4c79b5
	if (!empty($update_notification['forum']))
4c79b5
	{
4c79b5
		$sql = 'UPDATE ' . FORUMS_WATCH_TABLE . "
4c79b5
			SET notify_status = 1
4c79b5
			WHERE forum_id = $forum_id
4c79b5
				AND " . $db->sql_in_set('user_id', $update_notification['forum']);
4c79b5
		$db->sql_query($sql);
4c79b5
	}
4c79b5
4c79b5
	// Now delete the user_ids not authorised to receive notifications on this topic/forum
4c79b5
	if (!empty($delete_ids['topic']))
4c79b5
	{
4c79b5
		$sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . "
4c79b5
			WHERE topic_id = $topic_id
4c79b5
				AND " . $db->sql_in_set('user_id', $delete_ids['topic']);
4c79b5
		$db->sql_query($sql);
4c79b5
	}
4c79b5
4c79b5
	if (!empty($delete_ids['forum']))
4c79b5
	{
4c79b5
		$sql = 'DELETE FROM ' . FORUMS_WATCH_TABLE . "
4c79b5
			WHERE forum_id = $forum_id
4c79b5
				AND " . $db->sql_in_set('user_id', $delete_ids['forum']);
4c79b5
		$db->sql_query($sql);
4c79b5
	}
4c79b5
4c79b5
	$db->sql_transaction('commit');
4c79b5
}
4c79b5
4c79b5
//
4c79b5
// Post handling functions
4c79b5
//
4c79b5
4c79b5
/**
4c79b5
* Delete Post
4c79b5
*/
4c79b5
function delete_post($forum_id, $topic_id, $post_id, &$data)
4c79b5
{
4c79b5
	global $db, $user, $auth;
4c79b5
	global $config, $phpEx, $phpbb_root_path;
4c79b5
4c79b5
	// Specify our post mode
4c79b5
	$post_mode = 'delete';
4c79b5
	if (($data['topic_first_post_id'] === $data['topic_last_post_id']) && $data['topic_replies_real'] == 0)
4c79b5
	{
4c79b5
		$post_mode = 'delete_topic';
4c79b5
	}
4c79b5
	else if ($data['topic_first_post_id'] == $post_id)
4c79b5
	{
4c79b5
		$post_mode = 'delete_first_post';
4c79b5
	}
4c79b5
	else if ($data['topic_last_post_id'] == $post_id)
4c79b5
	{
4c79b5
		$post_mode = 'delete_last_post';
4c79b5
	}
4c79b5
	$sql_data = array();
4c79b5
	$next_post_id = false;
4c79b5
4c79b5
	include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
4c79b5
4c79b5
	$db->sql_transaction('begin');
4c79b5
4c79b5
	// we must make sure to update forums that contain the shadow'd topic
4c79b5
	if ($post_mode == 'delete_topic')
4c79b5
	{
4c79b5
		$shadow_forum_ids = array();
4c79b5
4c79b5
		$sql = 'SELECT forum_id
4c79b5
			FROM ' . TOPICS_TABLE . '
4c79b5
			WHERE ' . $db->sql_in_set('topic_moved_id', $topic_id);
4c79b5
		$result = $db->sql_query($sql);
4c79b5
		while ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			if (!isset($shadow_forum_ids[(int) $row['forum_id']]))
4c79b5
			{
4c79b5
				$shadow_forum_ids[(int) $row['forum_id']] = 1;
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$shadow_forum_ids[(int) $row['forum_id']]++;
4c79b5
			}
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
	}
4c79b5
4c79b5
	if (!delete_posts('post_id', array($post_id), false, false))
4c79b5
	{
4c79b5
		// Try to delete topic, we may had an previous error causing inconsistency
4c79b5
		if ($post_mode == 'delete_topic')
4c79b5
		{
4c79b5
			delete_topics('topic_id', array($topic_id), false);
4c79b5
		}
4c79b5
		trigger_error('ALREADY_DELETED');
4c79b5
	}
4c79b5
4c79b5
	$db->sql_transaction('commit');
4c79b5
4c79b5
	// Collect the necessary information for updating the tables
4c79b5
	$sql_data[FORUMS_TABLE] = '';
4c79b5
	switch ($post_mode)
4c79b5
	{
4c79b5
		case 'delete_topic':
4c79b5
4c79b5
			foreach ($shadow_forum_ids as $updated_forum => $topic_count)
4c79b5
			{
4c79b5
				// counting is fun! we only have to do sizeof($forum_ids) number of queries,
4c79b5
				// even if the topic is moved back to where its shadow lives (we count how many times it is in a forum)
4c79b5
				$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);
4c79b5
				update_post_information('forum', $updated_forum);
4c79b5
			}
4c79b5
4c79b5
			delete_topics('topic_id', array($topic_id), false);
4c79b5
4c79b5
			if ($data['topic_type'] != POST_GLOBAL)
4c79b5
			{
4c79b5
				$sql_data[FORUMS_TABLE] .= 'forum_topics_real = forum_topics_real - 1';
4c79b5
				$sql_data[FORUMS_TABLE] .= ($data['topic_approved']) ? ', forum_posts = forum_posts - 1, forum_topics = forum_topics - 1' : '';
4c79b5
			}
4c79b5
4c79b5
			$update_sql = update_post_information('forum', $forum_id, true);
4c79b5
			if (sizeof($update_sql))
4c79b5
			{
4c79b5
				$sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
4c79b5
				$sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
4c79b5
			}
4c79b5
		break;
4c79b5
4c79b5
		case 'delete_first_post':
4c79b5
			$sql = 'SELECT p.post_id, p.poster_id, p.post_username, u.username, u.user_colour
4c79b5
				FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
4c79b5
				WHERE p.topic_id = $topic_id
4c79b5
					AND p.poster_id = u.user_id
4c79b5
				ORDER BY p.post_time ASC";
4c79b5
			$result = $db->sql_query_limit($sql, 1);
4c79b5
			$row = $db->sql_fetchrow($result);
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			if ($data['topic_type'] != POST_GLOBAL)
4c79b5
			{
4c79b5
				$sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
4c79b5
			}
4c79b5
4c79b5
			$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'])) . "'";
4c79b5
4c79b5
			// 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"
4c79b5
			$sql_data[TOPICS_TABLE] .= ', topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
4c79b5
4c79b5
			$next_post_id = (int) $row['post_id'];
4c79b5
		break;
4c79b5
4c79b5
		case 'delete_last_post':
4c79b5
			if ($data['topic_type'] != POST_GLOBAL)
4c79b5
			{
4c79b5
				$sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
4c79b5
			}
4c79b5
4c79b5
			$update_sql = update_post_information('forum', $forum_id, true);
4c79b5
			if (sizeof($update_sql))
4c79b5
			{
4c79b5
				$sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
4c79b5
				$sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
4c79b5
			}
4c79b5
4c79b5
			$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' : '');
4c79b5
4c79b5
			$update_sql = update_post_information('topic', $topic_id, true);
4c79b5
			if (sizeof($update_sql))
4c79b5
			{
4c79b5
				$sql_data[TOPICS_TABLE] .= ', ' . implode(', ', $update_sql[$topic_id]);
4c79b5
				$next_post_id = (int) str_replace('topic_last_post_id = ', '', $update_sql[$topic_id][0]);
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$sql = 'SELECT MAX(post_id) as last_post_id
4c79b5
					FROM ' . POSTS_TABLE . "
4c79b5
					WHERE topic_id = $topic_id " .
4c79b5
						((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '');
4c79b5
				$result = $db->sql_query($sql);
4c79b5
				$row = $db->sql_fetchrow($result);
4c79b5
				$db->sql_freeresult($result);
4c79b5
4c79b5
				$next_post_id = (int) $row['last_post_id'];
4c79b5
			}
4c79b5
		break;
4c79b5
4c79b5
		case 'delete':
4c79b5
			$sql = 'SELECT post_id
4c79b5
				FROM ' . POSTS_TABLE . "
4c79b5
				WHERE topic_id = $topic_id " .
4c79b5
					((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '') . '
4c79b5
					AND post_time > ' . $data['post_time'] . '
4c79b5
				ORDER BY post_time ASC';
4c79b5
			$result = $db->sql_query_limit($sql, 1);
4c79b5
			$row = $db->sql_fetchrow($result);
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			if ($data['topic_type'] != POST_GLOBAL)
4c79b5
			{
4c79b5
				$sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
4c79b5
			}
4c79b5
4c79b5
			$sql_data[TOPICS_TABLE] = 'topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
4c79b5
			$next_post_id = (int) $row['post_id'];
4c79b5
		break;
4c79b5
	}
4c79b5
4c79b5
//	$sql_data[USERS_TABLE] = ($data['post_postcount']) ? 'user_posts = user_posts - 1' : '';
4c79b5
4c79b5
	$db->sql_transaction('begin');
4c79b5
4c79b5
	$where_sql = array(
4c79b5
		FORUMS_TABLE	=> "forum_id = $forum_id",
4c79b5
		TOPICS_TABLE	=> "topic_id = $topic_id",
4c79b5
		USERS_TABLE		=> 'user_id = ' . $data['poster_id']
4c79b5
	);
4c79b5
4c79b5
	foreach ($sql_data as $table => $update_sql)
4c79b5
	{
4c79b5
		if ($update_sql)
4c79b5
		{
4c79b5
			$db->sql_query("UPDATE $table SET $update_sql WHERE " . $where_sql[$table]);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Adjust posted info for this user by looking for a post by him/her within this topic...
4c79b5
	if ($post_mode != 'delete_topic' && $config['load_db_track'] && $data['poster_id'] != ANONYMOUS)
4c79b5
	{
4c79b5
		$sql = 'SELECT poster_id
4c79b5
			FROM ' . POSTS_TABLE . '
4c79b5
			WHERE topic_id = ' . $topic_id . '
4c79b5
				AND poster_id = ' . $data['poster_id'];
4c79b5
		$result = $db->sql_query_limit($sql, 1);
4c79b5
		$poster_id = (int) $db->sql_fetchfield('poster_id');
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		// The user is not having any more posts within this topic
4c79b5
		if (!$poster_id)
4c79b5
		{
4c79b5
			$sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
4c79b5
				WHERE topic_id = ' . $topic_id . '
4c79b5
					AND user_id = ' . $data['poster_id'];
4c79b5
			$db->sql_query($sql);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	$db->sql_transaction('commit');
4c79b5
4c79b5
	if ($data['post_reported'] && ($post_mode != 'delete_topic'))
4c79b5
	{
4c79b5
		sync('topic_reported', 'topic_id', array($topic_id));
4c79b5
	}
4c79b5
4c79b5
	return $next_post_id;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Submit Post
4c79b5
*/
4c79b5
function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $update_message = true)
4c79b5
{
4c79b5
	global $db, $auth, $user, $config, $phpEx, $template, $phpbb_root_path;
4c79b5
4c79b5
	// We do not handle erasing posts here
4c79b5
	if ($mode == 'delete')
4c79b5
	{
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	$current_time = time();
4c79b5
4c79b5
	if ($mode == 'post')
4c79b5
	{
4c79b5
		$post_mode = 'post';
4c79b5
		$update_message = true;
4c79b5
	}
4c79b5
	else if ($mode != 'edit')
4c79b5
	{
4c79b5
		$post_mode = 'reply';
4c79b5
		$update_message = true;
4c79b5
	}
4c79b5
	else if ($mode == 'edit')
4c79b5
	{
4c79b5
		$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'));
4c79b5
	}
4c79b5
4c79b5
	// First of all make sure the subject and topic title are having the correct length.
4c79b5
	// To achieve this without cutting off between special chars we convert to an array and then count the elements.
4c79b5
	$subject = truncate_string($subject);
4c79b5
	$data['topic_title'] = truncate_string($data['topic_title']);
4c79b5
4c79b5
	// Collect some basic information about which tables and which rows to update/insert
4c79b5
	$sql_data = $topic_row = array();
4c79b5
	$poster_id = ($mode == 'edit') ? $data['poster_id'] : (int) $user->data['user_id'];
4c79b5
4c79b5
	// Retrieve some additional information if not present
4c79b5
	if ($mode == 'edit' && (!isset($data['post_approved']) || !isset($data['topic_approved']) || $data['post_approved'] === false || $data['topic_approved'] === false))
4c79b5
	{
4c79b5
		$sql = 'SELECT p.post_approved, t.topic_type, t.topic_replies, t.topic_replies_real, t.topic_approved
4c79b5
			FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
4c79b5
			WHERE t.topic_id = p.topic_id
4c79b5
				AND p.post_id = ' . $data['post_id'];
4c79b5
		$result = $db->sql_query($sql);
4c79b5
		$topic_row = $db->sql_fetchrow($result);
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		$data['topic_approved'] = $topic_row['topic_approved'];
4c79b5
		$data['post_approved'] = $topic_row['post_approved'];
4c79b5
	}
4c79b5
4c79b5
	// 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
4c79b5
	$post_approval = 1;
4c79b5
4c79b5
	// Check the permissions for post approval, as well as the queue trigger where users are put on approval with a post count lower than specified. Moderators are not affected.
4c79b5
	if ((($config['enable_queue_trigger'] && $user->data['user_posts'] < $config['queue_trigger_posts']) || !$auth->acl_get('f_noapprove', $data['forum_id'])) && !$auth->acl_get('m_approve', $data['forum_id']))
4c79b5
	{
4c79b5
		$post_approval = 0;
4c79b5
	}
4c79b5
4c79b5
	// Start the transaction here
4c79b5
	$db->sql_transaction('begin');
4c79b5
4c79b5
	// Collect Information
4c79b5
	switch ($post_mode)
4c79b5
	{
4c79b5
		case 'post':
4c79b5
		case 'reply':
4c79b5
			$sql_data[POSTS_TABLE]['sql'] = array(
4c79b5
				'forum_id'			=> ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
4c79b5
				'poster_id'			=> (int) $user->data['user_id'],
4c79b5
				'icon_id'			=> $data['icon_id'],
4c79b5
				'poster_ip'			=> $user->ip,
4c79b5
				'post_time'			=> $current_time,
4c79b5
				'post_approved'		=> $post_approval,
4c79b5
				'enable_bbcode'		=> $data['enable_bbcode'],
4c79b5
				'enable_smilies'	=> $data['enable_smilies'],
4c79b5
				'enable_magic_url'	=> $data['enable_urls'],
4c79b5
				'enable_sig'		=> $data['enable_sig'],
4c79b5
				'post_username'		=> (!$user->data['is_registered']) ? $username : '',
4c79b5
				'post_subject'		=> $subject,
4c79b5
				'post_text'			=> $data['message'],
4c79b5
				'post_checksum'		=> $data['message_md5'],
4c79b5
				'post_attachment'	=> (!empty($data['attachment_data'])) ? 1 : 0,
4c79b5
				'bbcode_bitfield'	=> $data['bbcode_bitfield'],
4c79b5
				'bbcode_uid'		=> $data['bbcode_uid'],
4c79b5
				'post_postcount'	=> ($auth->acl_get('f_postcount', $data['forum_id'])) ? 1 : 0,
4c79b5
				'post_edit_locked'	=> $data['post_edit_locked']
4c79b5
			);
4c79b5
		break;
4c79b5
4c79b5
		case 'edit_first_post':
4c79b5
		case 'edit':
4c79b5
4c79b5
		case 'edit_last_post':
4c79b5
		case 'edit_topic':
4c79b5
4c79b5
			// If edit reason is given always display edit info
4c79b5
4c79b5
			// If editing last post then display no edit info
4c79b5
			// If m_edit permission then display no edit info
4c79b5
			// If normal edit display edit info
4c79b5
4c79b5
			// Display edit info if edit reason given or user is editing his post, which is not the last within the topic.
4c79b5
			if ($data['post_edit_reason'] || (!$auth->acl_get('m_edit', $data['forum_id']) && ($post_mode == 'edit' || $post_mode == 'edit_first_post')))
4c79b5
			{
4c79b5
				$data['post_edit_reason']		= truncate_string($data['post_edit_reason'], 255, 255, false);
4c79b5
4c79b5
				$sql_data[POSTS_TABLE]['sql']	= array(
4c79b5
					'post_edit_time'	=> $current_time,
4c79b5
					'post_edit_reason'	=> $data['post_edit_reason'],
4c79b5
					'post_edit_user'	=> (int) $data['post_edit_user'],
4c79b5
				);
4c79b5
4c79b5
				$sql_data[POSTS_TABLE]['stat'][] = 'post_edit_count = post_edit_count + 1';
4c79b5
			}
4c79b5
			else if (!$data['post_edit_reason'] && $mode == 'edit' && $auth->acl_get('m_edit', $data['forum_id']))
4c79b5
			{
4c79b5
				$sql_data[POSTS_TABLE]['sql'] = array(
4c79b5
					'post_edit_reason'	=> '',
4c79b5
				);
4c79b5
			}
4c79b5
4c79b5
			// If the person editing this post is different to the one having posted then we will add a log entry stating the edit
4c79b5
			// Could be simplified by only adding to the log if the edit is not tracked - but this may confuse admins/mods
4c79b5
			if ($user->data['user_id'] != $poster_id)
4c79b5
			{
4c79b5
				$log_subject = ($subject) ? $subject : $data['topic_title'];
4c79b5
				add_log('mod', $data['forum_id'], $data['topic_id'], 'LOG_POST_EDITED', $log_subject, (!empty($username)) ? $username : $user->lang['GUEST']);
4c79b5
			}
4c79b5
4c79b5
			if (!isset($sql_data[POSTS_TABLE]['sql']))
4c79b5
			{
4c79b5
				$sql_data[POSTS_TABLE]['sql'] = array();
4c79b5
			}
4c79b5
4c79b5
			$sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
4c79b5
				'forum_id'			=> ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
4c79b5
				'poster_id'			=> $data['poster_id'],
4c79b5
				'icon_id'			=> $data['icon_id'],
4c79b5
				'post_approved'		=> (!$post_approval) ? 0 : $data['post_approved'],
4c79b5
				'enable_bbcode'		=> $data['enable_bbcode'],
4c79b5
				'enable_smilies'	=> $data['enable_smilies'],
4c79b5
				'enable_magic_url'	=> $data['enable_urls'],
4c79b5
				'enable_sig'		=> $data['enable_sig'],
4c79b5
				'post_username'		=> ($username && $data['poster_id'] == ANONYMOUS) ? $username : '',
4c79b5
				'post_subject'		=> $subject,
4c79b5
				'post_checksum'		=> $data['message_md5'],
4c79b5
				'post_attachment'	=> (!empty($data['attachment_data'])) ? 1 : 0,
4c79b5
				'bbcode_bitfield'	=> $data['bbcode_bitfield'],
4c79b5
				'bbcode_uid'		=> $data['bbcode_uid'],
4c79b5
				'post_edit_locked'	=> $data['post_edit_locked'])
4c79b5
			);
4c79b5
4c79b5
			if ($update_message)
4c79b5
			{
4c79b5
				$sql_data[POSTS_TABLE]['sql']['post_text'] = $data['message'];
4c79b5
			}
4c79b5
4c79b5
		break;
4c79b5
	}
4c79b5
4c79b5
	$post_approved = $sql_data[POSTS_TABLE]['sql']['post_approved'];
4c79b5
	$topic_row = array();
4c79b5
4c79b5
	// And the topic ladies and gentlemen
4c79b5
	switch ($post_mode)
4c79b5
	{
4c79b5
		case 'post':
4c79b5
			$sql_data[TOPICS_TABLE]['sql'] = array(
4c79b5
				'topic_poster'				=> (int) $user->data['user_id'],
4c79b5
				'topic_time'				=> $current_time,
4c79b5
				'forum_id'					=> ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
4c79b5
				'icon_id'					=> $data['icon_id'],
4c79b5
				'topic_approved'			=> $post_approval,
4c79b5
				'topic_title'				=> $subject,
4c79b5
				'topic_first_poster_name'	=> (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
4c79b5
				'topic_first_poster_colour'	=> $user->data['user_colour'],
4c79b5
				'topic_type'				=> $topic_type,
4c79b5
				'topic_time_limit'			=> ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
4c79b5
				'topic_attachment'			=> (!empty($data['attachment_data'])) ? 1 : 0,
4c79b5
			);
4c79b5
4c79b5
			if (isset($poll['poll_options']) && !empty($poll['poll_options']))
4c79b5
			{
4c79b5
				$sql_data[TOPICS_TABLE]['sql'] = array_merge($sql_data[TOPICS_TABLE]['sql'], array(
4c79b5
					'poll_title'		=> $poll['poll_title'],
4c79b5
					'poll_start'		=> ($poll['poll_start']) ? $poll['poll_start'] : $current_time,
4c79b5
					'poll_max_options'	=> $poll['poll_max_options'],
4c79b5
					'poll_length'		=> ($poll['poll_length'] * 86400),
4c79b5
					'poll_vote_change'	=> $poll['poll_vote_change'])
4c79b5
				);
4c79b5
			}
4c79b5
4c79b5
			$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' : '');
4c79b5
4c79b5
			if ($topic_type != POST_GLOBAL)
4c79b5
			{
4c79b5
				if ($post_approval)
4c79b5
				{
4c79b5
					$sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
4c79b5
				}
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($post_approval) ? ', forum_topics = forum_topics + 1' : '');
4c79b5
			}
4c79b5
		break;
4c79b5
4c79b5
		case 'reply':
4c79b5
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_replies_real = topic_replies_real + 1, topic_bumped = 0, topic_bumper = 0' . (($post_approval) ? ', topic_replies = topic_replies + 1' : '') . ((!empty($data['attachment_data']) || (isset($data['topic_attachment']) && $data['topic_attachment'])) ? ', topic_attachment = 1' : '');
4c79b5
			$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' : '');
4c79b5
4c79b5
			if ($post_approval && $topic_type != POST_GLOBAL)
4c79b5
			{
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
4c79b5
			}
4c79b5
		break;
4c79b5
4c79b5
		case 'edit_topic':
4c79b5
		case 'edit_first_post':
4c79b5
4c79b5
			$sql_data[TOPICS_TABLE]['sql'] = array(
4c79b5
				'forum_id'					=> ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
4c79b5
				'icon_id'					=> $data['icon_id'],
4c79b5
				'topic_approved'			=> (!$post_approval) ? 0 : $data['topic_approved'],
4c79b5
				'topic_title'				=> $subject,
4c79b5
				'topic_first_poster_name'	=> $username,
4c79b5
				'topic_type'				=> $topic_type,
4c79b5
				'topic_time_limit'			=> ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
4c79b5
				'poll_title'				=> (isset($poll['poll_options'])) ? $poll['poll_title'] : '',
4c79b5
				'poll_start'				=> (isset($poll['poll_options'])) ? (($poll['poll_start']) ? $poll['poll_start'] : $current_time) : 0,
4c79b5
				'poll_max_options'			=> (isset($poll['poll_options'])) ? $poll['poll_max_options'] : 1,
4c79b5
				'poll_length'				=> (isset($poll['poll_options'])) ? ($poll['poll_length'] * 86400) : 0,
4c79b5
				'poll_vote_change'			=> (isset($poll['poll_vote_change'])) ? $poll['poll_vote_change'] : 0,
4c79b5
4c79b5
				'topic_attachment'			=> (!empty($data['attachment_data'])) ? 1 : (isset($data['topic_attachment']) ? $data['topic_attachment'] : 0),
4c79b5
			);
4c79b5
4c79b5
			// Correctly set back the topic replies and forum posts... only if the topic was approved before and now gets disapproved
4c79b5
			if (!$post_approval && $data['topic_approved'])
4c79b5
			{
4c79b5
				// Do we need to grab some topic informations?
4c79b5
				if (!sizeof($topic_row))
4c79b5
				{
4c79b5
					$sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_approved
4c79b5
						FROM ' . TOPICS_TABLE . '
4c79b5
						WHERE topic_id = ' . $data['topic_id'];
4c79b5
					$result = $db->sql_query($sql);
4c79b5
					$topic_row = $db->sql_fetchrow($result);
4c79b5
					$db->sql_freeresult($result);
4c79b5
				}
4c79b5
4c79b5
				// If this is the only post remaining we do not need to decrement topic_replies.
4c79b5
				// Also do not decrement if first post - then the topic_replies will not be adjusted if approving the topic again.
4c79b5
4c79b5
				// If this is an edited topic or the first post the topic gets completely disapproved later on...
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics = forum_topics - 1';
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - ' . ($topic_row['topic_replies'] + 1);
4c79b5
4c79b5
				set_config('num_topics', $config['num_topics'] - 1, true);
4c79b5
				set_config('num_posts', $config['num_posts'] - ($topic_row['topic_replies'] + 1), true);
4c79b5
4c79b5
				// Only decrement this post, since this is the one non-approved now
4c79b5
				if ($auth->acl_get('f_postcount', $data['forum_id']))
4c79b5
				{
4c79b5
					$sql_data[USERS_TABLE]['stat'][] = 'user_posts = user_posts - 1';
4c79b5
				}
4c79b5
			}
4c79b5
4c79b5
		break;
4c79b5
4c79b5
		case 'edit':
4c79b5
		case 'edit_last_post':
4c79b5
4c79b5
			// Correctly set back the topic replies and forum posts... but only if the post was approved before.
4c79b5
			if (!$post_approval && $data['post_approved'])
4c79b5
			{
4c79b5
				$sql_data[TOPICS_TABLE]['stat'][] = 'topic_replies = topic_replies - 1';
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - 1';
4c79b5
4c79b5
				set_config('num_posts', $config['num_posts'] - 1, true);
4c79b5
4c79b5
				if ($auth->acl_get('f_postcount', $data['forum_id']))
4c79b5
				{
4c79b5
					$sql_data[USERS_TABLE]['stat'][] = 'user_posts = user_posts - 1';
4c79b5
				}
4c79b5
			}
4c79b5
4c79b5
		break;
4c79b5
	}
4c79b5
4c79b5
	// Submit new topic
4c79b5
	if ($post_mode == 'post')
4c79b5
	{
4c79b5
		$sql = 'INSERT INTO ' . TOPICS_TABLE . ' ' .
4c79b5
			$db->sql_build_array('INSERT', $sql_data[TOPICS_TABLE]['sql']);
4c79b5
		$db->sql_query($sql);
4c79b5
4c79b5
		$data['topic_id'] = $db->sql_nextid();
4c79b5
4c79b5
		$sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
4c79b5
			'topic_id' => $data['topic_id'])
4c79b5
		);
4c79b5
		unset($sql_data[TOPICS_TABLE]['sql']);
4c79b5
	}
4c79b5
4c79b5
	// Submit new post
4c79b5
	if ($post_mode == 'post' || $post_mode == 'reply')
4c79b5
	{
4c79b5
		if ($post_mode == 'reply')
4c79b5
		{
4c79b5
			$sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
4c79b5
				'topic_id' => $data['topic_id'])
4c79b5
			);
4c79b5
		}
4c79b5
4c79b5
		$sql = 'INSERT INTO ' . POSTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_data[POSTS_TABLE]['sql']);
4c79b5
		$db->sql_query($sql);
4c79b5
		$data['post_id'] = $db->sql_nextid();
4c79b5
4c79b5
		if ($post_mode == 'post')
4c79b5
		{
4c79b5
			$sql_data[TOPICS_TABLE]['sql'] = array(
4c79b5
				'topic_first_post_id'		=> $data['post_id'],
4c79b5
				'topic_last_post_id'		=> $data['post_id'],
4c79b5
				'topic_last_post_time'		=> $current_time,
4c79b5
				'topic_last_poster_id'		=> (int) $user->data['user_id'],
4c79b5
				'topic_last_poster_name'	=> (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
4c79b5
				'topic_last_poster_colour'	=> $user->data['user_colour'],
4c79b5
				'topic_last_post_subject'	=> (string) $subject,
4c79b5
			);
4c79b5
		}
4c79b5
4c79b5
		unset($sql_data[POSTS_TABLE]['sql']);
4c79b5
	}
4c79b5
4c79b5
	$make_global = false;
4c79b5
4c79b5
	// Are we globalising or unglobalising?
4c79b5
	if ($post_mode == 'edit_first_post' || $post_mode == 'edit_topic')
4c79b5
	{
4c79b5
		if (!sizeof($topic_row))
4c79b5
		{
4c79b5
			$sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_approved, topic_last_post_id
4c79b5
				FROM ' . TOPICS_TABLE . '
4c79b5
				WHERE topic_id = ' . $data['topic_id'];
4c79b5
			$result = $db->sql_query($sql);
4c79b5
			$topic_row = $db->sql_fetchrow($result);
4c79b5
			$db->sql_freeresult($result);
4c79b5
		}
4c79b5
4c79b5
		// globalise/unglobalise?
4c79b5
		if (($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL) || ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL))
4c79b5
		{
4c79b5
			if (!empty($sql_data[FORUMS_TABLE]['stat']) && implode('', $sql_data[FORUMS_TABLE]['stat']))
4c79b5
			{
4c79b5
				$db->sql_query('UPDATE ' . FORUMS_TABLE . ' SET ' . implode(', ', $sql_data[FORUMS_TABLE]['stat']) . ' WHERE forum_id = ' . $data['forum_id']);
4c79b5
			}
4c79b5
4c79b5
			$make_global = true;
4c79b5
			$sql_data[FORUMS_TABLE]['stat'] = array();
4c79b5
		}
4c79b5
4c79b5
		// globalise
4c79b5
		if ($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL)
4c79b5
		{
4c79b5
			// Decrement topic/post count
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - ' . ($topic_row['topic_replies_real'] + 1);
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real - 1' . (($topic_row['topic_approved']) ? ', forum_topics = forum_topics - 1' : '');
4c79b5
4c79b5
			// Update forum_ids for all posts
4c79b5
			$sql = 'UPDATE ' . POSTS_TABLE . '
4c79b5
				SET forum_id = 0
4c79b5
				WHERE topic_id = ' . $data['topic_id'];
4c79b5
			$db->sql_query($sql);
4c79b5
		}
4c79b5
		// unglobalise
4c79b5
		else if ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL)
4c79b5
		{
4c79b5
			// Increment topic/post count
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + ' . ($topic_row['topic_replies_real'] + 1);
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($topic_row['topic_approved']) ? ', forum_topics = forum_topics + 1' : '');
4c79b5
4c79b5
			// Update forum_ids for all posts
4c79b5
			$sql = 'UPDATE ' . POSTS_TABLE . '
4c79b5
				SET forum_id = ' . $data['forum_id'] . '
4c79b5
				WHERE topic_id = ' . $data['topic_id'];
4c79b5
			$db->sql_query($sql);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Update the topics table
4c79b5
	if (isset($sql_data[TOPICS_TABLE]['sql']))
4c79b5
	{
4c79b5
		$sql = 'UPDATE ' . TOPICS_TABLE . '
4c79b5
			SET ' . $db->sql_build_array('UPDATE', $sql_data[TOPICS_TABLE]['sql']) . '
4c79b5
			WHERE topic_id = ' . $data['topic_id'];
4c79b5
		$db->sql_query($sql);
4c79b5
	}
4c79b5
4c79b5
	// Update the posts table
4c79b5
	if (isset($sql_data[POSTS_TABLE]['sql']))
4c79b5
	{
4c79b5
		$sql = 'UPDATE ' . POSTS_TABLE . '
4c79b5
			SET ' . $db->sql_build_array('UPDATE', $sql_data[POSTS_TABLE]['sql']) . '
4c79b5
			WHERE post_id = ' . $data['post_id'];
4c79b5
		$db->sql_query($sql);
4c79b5
	}
4c79b5
4c79b5
	// Update Poll Tables
4c79b5
	if (isset($poll['poll_options']) && !empty($poll['poll_options']))
4c79b5
	{
4c79b5
		$cur_poll_options = array();
4c79b5
4c79b5
		if ($poll['poll_start'] && $mode == 'edit')
4c79b5
		{
4c79b5
			$sql = 'SELECT *
4c79b5
				FROM ' . POLL_OPTIONS_TABLE . '
4c79b5
				WHERE topic_id = ' . $data['topic_id'] . '
4c79b5
				ORDER BY poll_option_id';
4c79b5
			$result = $db->sql_query($sql);
4c79b5
4c79b5
			$cur_poll_options = array();
4c79b5
			while ($row = $db->sql_fetchrow($result))
4c79b5
			{
4c79b5
				$cur_poll_options[] = $row;
4c79b5
			}
4c79b5
			$db->sql_freeresult($result);
4c79b5
		}
4c79b5
4c79b5
		$sql_insert_ary = array();
4c79b5
4c79b5
		for ($i = 0, $size = sizeof($poll['poll_options']); $i < $size; $i++)
4c79b5
		{
4c79b5
			if (strlen(trim($poll['poll_options'][$i])))
4c79b5
			{
4c79b5
				if (empty($cur_poll_options[$i]))
4c79b5
				{
4c79b5
					// If we add options we need to put them to the end to be able to preserve votes...
4c79b5
					$sql_insert_ary[] = array(
4c79b5
						'poll_option_id'	=> (int) sizeof($cur_poll_options) + 1 + sizeof($sql_insert_ary),
4c79b5
						'topic_id'			=> (int) $data['topic_id'],
4c79b5
						'poll_option_text'	=> (string) $poll['poll_options'][$i]
4c79b5
					);
4c79b5
				}
4c79b5
				else if ($poll['poll_options'][$i] != $cur_poll_options[$i])
4c79b5
				{
4c79b5
					$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . "
4c79b5
						SET poll_option_text = '" . $db->sql_escape($poll['poll_options'][$i]) . "'
4c79b5
						WHERE poll_option_id = " . $cur_poll_options[$i]['poll_option_id'] . '
4c79b5
							AND topic_id = ' . $data['topic_id'];
4c79b5
					$db->sql_query($sql);
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		$db->sql_multi_insert(POLL_OPTIONS_TABLE, $sql_insert_ary);
4c79b5
4c79b5
		if (sizeof($poll['poll_options']) < sizeof($cur_poll_options))
4c79b5
		{
4c79b5
			$sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . '
4c79b5
				WHERE poll_option_id > ' . sizeof($poll['poll_options']) . '
4c79b5
					AND topic_id = ' . $data['topic_id'];
4c79b5
			$db->sql_query($sql);
4c79b5
		}
4c79b5
4c79b5
		// 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
4c79b5
		if ($mode == 'edit' && sizeof($poll['poll_options']) != sizeof($cur_poll_options))
4c79b5
		{
4c79b5
			$db->sql_query('DELETE FROM ' . POLL_VOTES_TABLE . ' WHERE topic_id = ' . $data['topic_id']);
4c79b5
			$db->sql_query('UPDATE ' . POLL_OPTIONS_TABLE . ' SET poll_option_total = 0 WHERE topic_id = ' . $data['topic_id']);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Submit Attachments
4c79b5
	if (!empty($data['attachment_data']) && $data['post_id'] && in_array($mode, array('post', 'reply', 'quote', 'edit')))
4c79b5
	{
4c79b5
		$space_taken = $files_added = 0;
4c79b5
		$orphan_rows = array();
4c79b5
4c79b5
		foreach ($data['attachment_data'] as $pos => $attach_row)
4c79b5
		{
4c79b5
			$orphan_rows[(int) $attach_row['attach_id']] = array();
4c79b5
		}
4c79b5
4c79b5
		if (sizeof($orphan_rows))
4c79b5
		{
4c79b5
			$sql = 'SELECT attach_id, filesize, physical_filename
4c79b5
				FROM ' . ATTACHMENTS_TABLE . '
4c79b5
				WHERE ' . $db->sql_in_set('attach_id', array_keys($orphan_rows)) . '
4c79b5
					AND is_orphan = 1
4c79b5
					AND poster_id = ' . $user->data['user_id'];
4c79b5
			$result = $db->sql_query($sql);
4c79b5
4c79b5
			$orphan_rows = array();
4c79b5
			while ($row = $db->sql_fetchrow($result))
4c79b5
			{
4c79b5
				$orphan_rows[$row['attach_id']] = $row;
4c79b5
			}
4c79b5
			$db->sql_freeresult($result);
4c79b5
		}
4c79b5
4c79b5
		foreach ($data['attachment_data'] as $pos => $attach_row)
4c79b5
		{
4c79b5
			if ($attach_row['is_orphan'] && !isset($orphan_rows[$attach_row['attach_id']]))
4c79b5
			{
4c79b5
				continue;
4c79b5
			}
4c79b5
4c79b5
			if (!$attach_row['is_orphan'])
4c79b5
			{
4c79b5
				// update entry in db if attachment already stored in db and filespace
4c79b5
				$sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
4c79b5
					SET attach_comment = '" . $db->sql_escape($attach_row['attach_comment']) . "'
4c79b5
					WHERE attach_id = " . (int) $attach_row['attach_id'] . '
4c79b5
						AND is_orphan = 0';
4c79b5
				$db->sql_query($sql);
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				// insert attachment into db
4c79b5
				if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
4c79b5
				{
4c79b5
					continue;
4c79b5
				}
4c79b5
4c79b5
				$space_taken += $orphan_rows[$attach_row['attach_id']]['filesize'];
4c79b5
				$files_added++;
4c79b5
4c79b5
				$attach_sql = array(
4c79b5
					'post_msg_id'		=> $data['post_id'],
4c79b5
					'topic_id'			=> $data['topic_id'],
4c79b5
					'is_orphan'			=> 0,
4c79b5
					'poster_id'			=> $poster_id,
4c79b5
					'attach_comment'	=> $attach_row['attach_comment'],
4c79b5
				);
4c79b5
4c79b5
				$sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $attach_sql) . '
4c79b5
					WHERE attach_id = ' . $attach_row['attach_id'] . '
4c79b5
						AND is_orphan = 1
4c79b5
						AND poster_id = ' . $user->data['user_id'];
4c79b5
				$db->sql_query($sql);
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		if ($space_taken && $files_added)
4c79b5
		{
4c79b5
			set_config('upload_dir_size', $config['upload_dir_size'] + $space_taken, true);
4c79b5
			set_config('num_files', $config['num_files'] + $files_added, true);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// we need to update the last forum information
4c79b5
	// only applicable if the topic is not global and it is approved
4c79b5
	// we also check to make sure we are not dealing with globaling the latest topic (pretty rare but still needs to be checked)
4c79b5
	if ($topic_type != POST_GLOBAL && !$make_global && ($post_approved || !$data['post_approved']))
4c79b5
	{
4c79b5
		// the last post makes us update the forum table. This can happen if...
4c79b5
		// We make a new topic
4c79b5
		// We reply to a topic
4c79b5
		// We edit the last post in a topic and this post is the latest in the forum (maybe)
4c79b5
		// We edit the only post in the topic
4c79b5
		// We edit the first post in the topic and all the other posts are not approved
4c79b5
		if (($post_mode == 'post' || $post_mode == 'reply') && $post_approved)
4c79b5
		{
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . $data['post_id'];
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($subject) . "'";
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . $current_time;
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $user->data['user_id'];
4c79b5
			$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'] : '')) . "'";
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($user->data['user_colour']) . "'";
4c79b5
		}
4c79b5
		else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies']))
4c79b5
		{
4c79b5
			// this does not _necessarily_ mean that we must update the info again,
4c79b5
			// it just means that we might have to
4c79b5
			$sql = 'SELECT forum_last_post_id, forum_last_post_subject
4c79b5
				FROM ' . FORUMS_TABLE . '
4c79b5
				WHERE forum_id = ' . (int) $data['forum_id'];
4c79b5
			$result = $db->sql_query($sql);
4c79b5
			$row = $db->sql_fetchrow($result);
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			// this post is the latest post in the forum, better update
4c79b5
			if ($row['forum_last_post_id'] == $data['post_id'])
4c79b5
			{
4c79b5
				// If post approved and subject changed, or poster is anonymous, we need to update the forum_last* rows
4c79b5
				if ($post_approved && ($row['forum_last_post_subject'] !== $subject || $data['poster_id'] == ANONYMOUS))
4c79b5
				{
4c79b5
					// the post's subject changed
4c79b5
					if ($row['forum_last_post_subject'] !== $subject)
4c79b5
					{
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_subject = \'' . $db->sql_escape($subject) . '\'';
4c79b5
					}
4c79b5
4c79b5
					// Update the user name if poster is anonymous... just in case an admin changed it
4c79b5
					if ($data['poster_id'] == ANONYMOUS)
4c79b5
					{
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape($username) . "'";
4c79b5
					}
4c79b5
				}
4c79b5
				else if ($data['post_approved'] !== $post_approved)
4c79b5
				{
4c79b5
					// we need a fresh change of socks, everything has become invalidated
4c79b5
					$sql = 'SELECT MAX(topic_last_post_id) as last_post_id
4c79b5
						FROM ' . TOPICS_TABLE . '
4c79b5
						WHERE forum_id = ' . (int) $data['forum_id'] . '
4c79b5
							AND topic_approved = 1';
4c79b5
					$result = $db->sql_query($sql);
4c79b5
					$row = $db->sql_fetchrow($result);
4c79b5
					$db->sql_freeresult($result);
4c79b5
4c79b5
					// any posts left in this forum?
4c79b5
					if (!empty($row['last_post_id']))
4c79b5
					{
4c79b5
						$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
4c79b5
							FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
4c79b5
							WHERE p.poster_id = u.user_id
4c79b5
								AND p.post_id = ' . (int) $row['last_post_id'];
4c79b5
						$result = $db->sql_query($sql);
4c79b5
						$row = $db->sql_fetchrow($result);
4c79b5
						$db->sql_freeresult($result);
4c79b5
4c79b5
						// salvation, a post is found! jam it into the forums table
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						// just our luck, the last topic in the forum has just been turned unapproved...
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = 0';
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = ''";
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = 0';
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = 0';
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = ''";
4c79b5
						$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = ''";
4c79b5
					}
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
	else if ($make_global)
4c79b5
	{
4c79b5
		// somebody decided to be a party pooper, we must recalculate the whole shebang (maybe)
4c79b5
		$sql = 'SELECT forum_last_post_id
4c79b5
			FROM ' . FORUMS_TABLE . '
4c79b5
			WHERE forum_id = ' . (int) $data['forum_id'];
4c79b5
		$result = $db->sql_query($sql);
4c79b5
		$forum_row = $db->sql_fetchrow($result);
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		// we made a topic global, go get new data
4c79b5
		if ($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL && $forum_row['forum_last_post_id'] == $topic_row['topic_last_post_id'])
4c79b5
		{
4c79b5
			// we need a fresh change of socks, everything has become invalidated
4c79b5
			$sql = 'SELECT MAX(topic_last_post_id) as last_post_id
4c79b5
				FROM ' . TOPICS_TABLE . '
4c79b5
				WHERE forum_id = ' . (int) $data['forum_id'] . '
4c79b5
					AND topic_approved = 1';
4c79b5
			$result = $db->sql_query($sql);
4c79b5
			$row = $db->sql_fetchrow($result);
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			// any posts left in this forum?
4c79b5
			if (!empty($row['last_post_id']))
4c79b5
			{
4c79b5
				$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
4c79b5
					FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
4c79b5
					WHERE p.poster_id = u.user_id
4c79b5
						AND p.post_id = ' . (int) $row['last_post_id'];
4c79b5
				$result = $db->sql_query($sql);
4c79b5
				$row = $db->sql_fetchrow($result);
4c79b5
				$db->sql_freeresult($result);
4c79b5
4c79b5
				// salvation, a post is found! jam it into the forums table
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				// just our luck, the last topic in the forum has just been globalized...
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = 0';
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = ''";
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = 0';
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = 0';
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = ''";
4c79b5
				$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = ''";
4c79b5
			}
4c79b5
		}
4c79b5
		else if ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL && $forum_row['forum_last_post_id'] < $topic_row['topic_last_post_id'])
4c79b5
		{
4c79b5
			// this post has a higher id, it is newer
4c79b5
			$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
4c79b5
				FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
4c79b5
				WHERE p.poster_id = u.user_id
4c79b5
					AND p.post_id = ' . (int) $topic_row['topic_last_post_id'];
4c79b5
			$result = $db->sql_query($sql);
4c79b5
			$row = $db->sql_fetchrow($result);
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			// salvation, a post is found! jam it into the forums table
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
4c79b5
			$sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// topic sync time!
4c79b5
	// simply, we update if it is a reply or the last post is edited
4c79b5
	if ($post_approved)
4c79b5
	{
4c79b5
		// reply requires the whole thing
4c79b5
		if ($post_mode == 'reply')
4c79b5
		{
4c79b5
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_id = ' . (int) $data['post_id'];
4c79b5
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_poster_id = ' . (int) $user->data['user_id'];
4c79b5
			$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'] : '')) . "'";
4c79b5
			$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_colour = '" . (($user->data['user_id'] != ANONYMOUS) ? $db->sql_escape($user->data['user_colour']) : '') . "'";
4c79b5
			$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
4c79b5
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_time = ' . (int) $current_time;
4c79b5
		}
4c79b5
		else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies']))
4c79b5
		{
4c79b5
			// only the subject can be changed from edit
4c79b5
			$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
4c79b5
4c79b5
			// Maybe not only the subject, but also changing anonymous usernames. ;)
4c79b5
			if ($data['poster_id'] == ANONYMOUS)
4c79b5
			{
4c79b5
				$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape($username) . "'";
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
	else if (!$data['post_approved'] && ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies'])))
4c79b5
	{
4c79b5
		// like having the rug pulled from under us
4c79b5
		$sql = 'SELECT MAX(post_id) as last_post_id
4c79b5
			FROM ' . POSTS_TABLE . '
4c79b5
			WHERE topic_id = ' . (int) $data['topic_id'] . '
4c79b5
				AND post_approved = 1';
4c79b5
		$result = $db->sql_query($sql);
4c79b5
		$row = $db->sql_fetchrow($result);
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		// any posts left in this forum?
4c79b5
		if (!empty($row['last_post_id']))
4c79b5
		{
4c79b5
			$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
4c79b5
				FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
4c79b5
				WHERE p.poster_id = u.user_id
4c79b5
					AND p.post_id = ' . (int) $row['last_post_id'];
4c79b5
			$result = $db->sql_query($sql);
4c79b5
			$row = $db->sql_fetchrow($result);
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			// salvation, a post is found! jam it into the topics table
4c79b5
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_id = ' . (int) $row['post_id'];
4c79b5
			$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
4c79b5
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_time = ' . (int) $row['post_time'];
4c79b5
			$sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_poster_id = ' . (int) $row['poster_id'];
4c79b5
			$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
4c79b5
			$sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Update total post count, do not consider moderated posts/topics
4c79b5
	if ($post_approval)
4c79b5
	{
4c79b5
		if ($post_mode == 'post')
4c79b5
		{
4c79b5
			set_config('num_topics', $config['num_topics'] + 1, true);
4c79b5
			set_config('num_posts', $config['num_posts'] + 1, true);
4c79b5
		}
4c79b5
4c79b5
		if ($post_mode == 'reply')
4c79b5
		{
4c79b5
			set_config('num_posts', $config['num_posts'] + 1, true);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Update forum stats
4c79b5
	$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);
4c79b5
4c79b5
	foreach ($sql_data as $table => $update_ary)
4c79b5
	{
4c79b5
		if (isset($update_ary['stat']) && implode('', $update_ary['stat']))
4c79b5
		{
4c79b5
			$sql = "UPDATE $table SET " . implode(', ', $update_ary['stat']) . ' WHERE ' . $where_sql[$table];
4c79b5
			$db->sql_query($sql);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Delete topic shadows (if any exist). We do not need a shadow topic for an global announcement
4c79b5
	if ($make_global)
4c79b5
	{
4c79b5
		$sql = 'DELETE FROM ' . TOPICS_TABLE . '
4c79b5
			WHERE topic_moved_id = ' . $data['topic_id'];
4c79b5
		$db->sql_query($sql);
4c79b5
	}
4c79b5
4c79b5
	// Committing the transaction before updating search index
4c79b5
	$db->sql_transaction('commit');
4c79b5
4c79b5
	// Delete draft if post was loaded...
4c79b5
	$draft_id = request_var('draft_loaded', 0);
4c79b5
	if ($draft_id)
4c79b5
	{
4c79b5
		$sql = 'DELETE FROM ' . DRAFTS_TABLE . "
4c79b5
			WHERE draft_id = $draft_id
4c79b5
				AND user_id = {$user->data['user_id']}";
4c79b5
		$db->sql_query($sql);
4c79b5
	}
4c79b5
4c79b5
	// Index message contents
4c79b5
	if ($update_message && $data['enable_indexing'])
4c79b5
	{
4c79b5
		// Select the search method and do some additional checks to ensure it can actually be utilised
4c79b5
		$search_type = basename($config['search_type']);
4c79b5
4c79b5
		if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
4c79b5
		{
4c79b5
			trigger_error('NO_SUCH_SEARCH_MODULE');
4c79b5
		}
4c79b5
4c79b5
		if (!class_exists($search_type))
4c79b5
		{
4c79b5
			include("{$phpbb_root_path}includes/search/$search_type.$phpEx");
4c79b5
		}
4c79b5
4c79b5
		$error = false;
4c79b5
		$search = new $search_type($error);
4c79b5
4c79b5
		if ($error)
4c79b5
		{
4c79b5
			trigger_error($error);
4c79b5
		}
4c79b5
4c79b5
		$search->index($mode, $data['post_id'], $data['message'], $subject, $poster_id, ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']);
4c79b5
	}
4c79b5
4c79b5
	// Topic Notification, do not change if moderator is changing other users posts...
4c79b5
	if ($user->data['user_id'] == $poster_id)
4c79b5
	{
4c79b5
		if (!$data['notify_set'] && $data['notify'])
4c79b5
		{
4c79b5
			$sql = 'INSERT INTO ' . TOPICS_WATCH_TABLE . ' (user_id, topic_id)
4c79b5
				VALUES (' . $user->data['user_id'] . ', ' . $data['topic_id'] . ')';
4c79b5
			$db->sql_query($sql);
4c79b5
		}
4c79b5
		else if ($data['notify_set'] && !$data['notify'])
4c79b5
		{
4c79b5
			$sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . '
4c79b5
				WHERE user_id = ' . $user->data['user_id'] . '
4c79b5
					AND topic_id = ' . $data['topic_id'];
4c79b5
			$db->sql_query($sql);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	if ($mode == 'post' || $mode == 'reply' || $mode == 'quote')
4c79b5
	{
4c79b5
		// Mark this topic as posted to
4c79b5
		markread('post', $data['forum_id'], $data['topic_id'], $data['post_time']);
4c79b5
	}
4c79b5
4c79b5
	// Mark this topic as read
4c79b5
	// We do not use post_time here, this is intended (post_time can have a date in the past if editing a message)
4c79b5
	markread('topic', $data['forum_id'], $data['topic_id'], time());
4c79b5
4c79b5
	//
4c79b5
	if ($config['load_db_lastread'] && $user->data['is_registered'])
4c79b5
	{
4c79b5
		$sql = 'SELECT mark_time
4c79b5
			FROM ' . FORUMS_TRACK_TABLE . '
4c79b5
			WHERE user_id = ' . $user->data['user_id'] . '
4c79b5
				AND forum_id = ' . $data['forum_id'];
4c79b5
		$result = $db->sql_query($sql);
4c79b5
		$f_mark_time = (int) $db->sql_fetchfield('mark_time');
4c79b5
		$db->sql_freeresult($result);
4c79b5
	}
4c79b5
	else if ($config['load_anon_lastread'] || $user->data['is_registered'])
4c79b5
	{
4c79b5
		$f_mark_time = false;
4c79b5
	}
4c79b5
4c79b5
	if (($config['load_db_lastread'] && $user->data['is_registered']) || $config['load_anon_lastread'] || $user->data['is_registered'])
4c79b5
	{
4c79b5
		// Update forum info
4c79b5
		$sql = 'SELECT forum_last_post_time
4c79b5
			FROM ' . FORUMS_TABLE . '
4c79b5
			WHERE forum_id = ' . $data['forum_id'];
4c79b5
		$result = $db->sql_query($sql);
4c79b5
		$forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		update_forum_tracking_info($data['forum_id'], $forum_last_post_time, $f_mark_time, false);
4c79b5
	}
4c79b5
4c79b5
	// Send Notifications
4c79b5
	if ($mode != 'edit' && $mode != 'delete' && $post_approval)
4c79b5
	{
4c79b5
		user_notification($mode, $subject, $data['topic_title'], $data['forum_name'], $data['forum_id'], $data['topic_id'], $data['post_id']);
4c79b5
	}
4c79b5
4c79b5
	$params = $add_anchor = '';
4c79b5
4c79b5
	if ($post_approval)
4c79b5
	{
4c79b5
		$params .= '&t=' . $data['topic_id'];
4c79b5
4c79b5
		if ($mode != 'post')
4c79b5
		{
4c79b5
			$params .= '&p=' . $data['post_id'];
4c79b5
			$add_anchor = '#p' . $data['post_id'];
4c79b5
		}
4c79b5
	}
4c79b5
	else if ($mode != 'post' && $post_mode != 'edit_first_post' && $post_mode != 'edit_topic')
4c79b5
	{
4c79b5
		$params .= '&t=' . $data['topic_id'];
4c79b5
	}
4c79b5
4c79b5
	$url = (!$params) ? "{$phpbb_root_path}viewforum.$phpEx" : "{$phpbb_root_path}viewtopic.$phpEx";
4c79b5
	$url = append_sid($url, 'f=' . $data['forum_id'] . $params) . $add_anchor;
4c79b5
4c79b5
	return $url;
4c79b5
}
4c79b5
4c79b5
?>