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

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