Blame Identity/Webenv/phpBB/3.0.4/includes/functions_posting.php

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