Blame Identity/Models/Html/phpBB/3.0.4/includes/functions_posting.php

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