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

d6e8d8
d6e8d8
/**
d6e8d8
*
d6e8d8
* @package install
d6e8d8
* @version $Id: functions_convert.php 8876 2008-09-18 14:26:56Z acydburn $
d6e8d8
* @copyright (c) 2006 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
* Default avatar width/height
d6e8d8
* @ignore
d6e8d8
*/
d6e8d8
define('DEFAULT_AVATAR_X', 80);
d6e8d8
define('DEFAULT_AVATAR_Y', 80);
d6e8d8
d6e8d8
// Global functions - all functions can be used by convertors
d6e8d8
d6e8d8
// SIMPLE FUNCTIONS
d6e8d8
d6e8d8
/**
d6e8d8
* Return the preceding value
d6e8d8
*/
d6e8d8
function dec($var)
d6e8d8
{
d6e8d8
	return --$var;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Return the next value
d6e8d8
*/
d6e8d8
function inc($var)
d6e8d8
{
d6e8d8
	return ++$var;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Return whether the value is positive
d6e8d8
*/
d6e8d8
function is_positive($n)
d6e8d8
{
d6e8d8
	return ($n > 0) ? 1 : 0;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Boolean inverse of the value
d6e8d8
*/
d6e8d8
function not($var)
d6e8d8
{
d6e8d8
	return ($var) ? 0 : 1;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Convert a textual value to it's equivalent boolean value
d6e8d8
*
d6e8d8
* @param string $str String to convert (converts yes, on, y, 1 and true to boolean true)
d6e8d8
* @return boolean The equivalent value
d6e8d8
*/
d6e8d8
function str_to_bool($str)
d6e8d8
{
d6e8d8
	$str = strtolower($str);
d6e8d8
	return ($str == 'yes' || $str == 'on' || $str == 'y' || $str == 'true' || $str == '1') ? true : false;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Function to mimic php's empty() function (it is the same)
d6e8d8
*/
d6e8d8
function is_empty($mixed)
d6e8d8
{
d6e8d8
	return empty($mixed);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Convert the name of a user's primary group to the appropriate equivalent phpBB group id
d6e8d8
*
d6e8d8
* @param string $status The name of the group
d6e8d8
* @return int The group_id corresponding to the equivalent group
d6e8d8
*/
d6e8d8
function str_to_primary_group($status)
d6e8d8
{
d6e8d8
	switch (ucfirst(strtolower($status)))
d6e8d8
	{
d6e8d8
		case 'Administrator':
d6e8d8
			return get_group_id('administrators');
d6e8d8
		break;
d6e8d8
d6e8d8
		case 'Super moderator':
d6e8d8
		case 'Global moderator':
d6e8d8
		case 'Moderator':
d6e8d8
			return get_group_id('global_moderators');
d6e8d8
		break;
d6e8d8
d6e8d8
		case 'Guest':
d6e8d8
		case 'Anonymous':
d6e8d8
			return get_group_id('guests');
d6e8d8
		break;
d6e8d8
d6e8d8
		default:
d6e8d8
			return get_group_id('registered');
d6e8d8
		break;
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Convert a boolean into the appropriate phpBB constant indicating whether the item is locked
d6e8d8
*/
d6e8d8
function is_item_locked($bool)
d6e8d8
{
d6e8d8
	return ($bool) ? ITEM_LOCKED : ITEM_UNLOCKED;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Convert a value from days to seconds
d6e8d8
*/
d6e8d8
function days_to_seconds($days)
d6e8d8
{
d6e8d8
	return ($days * 86400);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Determine whether a user is anonymous and return the appropriate new user_id
d6e8d8
*/
d6e8d8
function is_user_anonymous($user_id)
d6e8d8
{
d6e8d8
	return ($user_id > ANONYMOUS) ? $user_id : ANONYMOUS;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Generate a key value based on existing values
d6e8d8
*
d6e8d8
* @param int $pad Amount to add to the maximum value
d6e8d8
* @return int Key value
d6e8d8
*/
d6e8d8
function auto_id($pad = 0)
d6e8d8
{
d6e8d8
	global $auto_id, $convert_row;
d6e8d8
d6e8d8
	if (!empty($convert_row['max_id']))
d6e8d8
	{
d6e8d8
		return $convert_row['max_id'] + $pad;
d6e8d8
	}
d6e8d8
d6e8d8
	return $auto_id + $pad;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Convert a boolean into the appropriate phpBB constant indicating whether the user is active
d6e8d8
*/
d6e8d8
function set_user_type($user_active)
d6e8d8
{
d6e8d8
	return ($user_active) ? USER_NORMAL : USER_INACTIVE;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Convert a value from minutes to hours
d6e8d8
*/
d6e8d8
function minutes_to_hours($minutes)
d6e8d8
{
d6e8d8
	return ($minutes / 3600);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Return the group_id for a given group name
d6e8d8
*/
d6e8d8
function get_group_id($group_name)
d6e8d8
{
d6e8d8
	global $db, $group_mapping;
d6e8d8
d6e8d8
	if (empty($group_mapping))
d6e8d8
	{
d6e8d8
		$sql = 'SELECT group_name, group_id
d6e8d8
			FROM ' . GROUPS_TABLE;
d6e8d8
		$result = $db->sql_query($sql);
d6e8d8
d6e8d8
		$group_mapping = array();
d6e8d8
		while ($row = $db->sql_fetchrow($result))
d6e8d8
		{
d6e8d8
			$group_mapping[strtoupper($row['group_name'])] = (int) $row['group_id'];
d6e8d8
		}
d6e8d8
		$db->sql_freeresult($result);
d6e8d8
	}
d6e8d8
d6e8d8
	if (!sizeof($group_mapping))
d6e8d8
	{
d6e8d8
		add_default_groups();
d6e8d8
		return get_group_id($group_name);
d6e8d8
	}
d6e8d8
d6e8d8
	if (isset($group_mapping[strtoupper($group_name)]))
d6e8d8
	{
d6e8d8
		return $group_mapping[strtoupper($group_name)];
d6e8d8
	}
d6e8d8
d6e8d8
	return $group_mapping['REGISTERED'];
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Generate the email hash stored in the users table
d6e8d8
*/
d6e8d8
function gen_email_hash($email)
d6e8d8
{
d6e8d8
	return (crc32(strtolower($email)) . strlen($email));
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Convert a boolean into the appropriate phpBB constant indicating whether the topic is locked
d6e8d8
*/
d6e8d8
function is_topic_locked($bool)
d6e8d8
{
d6e8d8
	return (!empty($bool)) ? ITEM_LOCKED : ITEM_UNLOCKED;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Generate a bbcode_uid value
d6e8d8
*/
d6e8d8
function make_uid($timestamp)
d6e8d8
{
d6e8d8
	static $last_timestamp, $last_uid;
d6e8d8
d6e8d8
	if (empty($last_timestamp) || $timestamp != $last_timestamp)
d6e8d8
	{
d6e8d8
		$last_uid = substr(base_convert(unique_id(), 16, 36), 0, BBCODE_UID_LEN);
d6e8d8
	}
d6e8d8
	$last_timestamp = $timestamp;
d6e8d8
	return $last_uid;
d6e8d8
}
d6e8d8
d6e8d8
d6e8d8
/**
d6e8d8
* Validate a website address
d6e8d8
*/
d6e8d8
function validate_website($url)
d6e8d8
{
d6e8d8
	if ($url === 'http://')
d6e8d8
	{
d6e8d8
		return '';
d6e8d8
	}
d6e8d8
	else if (!preg_match('#^[a-z0-9]+://#i', $url) && strlen($url) > 0)
d6e8d8
	{
d6e8d8
		return 'http://' . $url;
d6e8d8
	}
d6e8d8
	return $url;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Convert nulls to zeros for fields which allowed a NULL value in the source but not the destination
d6e8d8
*/
d6e8d8
function null_to_zero($value)
d6e8d8
{
d6e8d8
	return ($value === NULL) ? 0 : $value;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Convert nulls to empty strings for fields which allowed a NULL value in the source but not the destination
d6e8d8
*/
d6e8d8
function null_to_str($value)
d6e8d8
{
d6e8d8
	return ($value === NULL) ? '' : $value;
d6e8d8
}
d6e8d8
d6e8d8
// EXTENDED FUNCTIONS
d6e8d8
d6e8d8
/**
d6e8d8
* Get old config value
d6e8d8
*/
d6e8d8
function get_config_value($config_name)
d6e8d8
{
d6e8d8
	static $convert_config;
d6e8d8
d6e8d8
	if (!isset($convert_config))
d6e8d8
	{
d6e8d8
		$convert_config = get_config();
d6e8d8
	}
d6e8d8
d6e8d8
	if (!isset($convert_config[$config_name]))
d6e8d8
	{
d6e8d8
		return false;
d6e8d8
	}
d6e8d8
d6e8d8
	return (empty($convert_config[$config_name])) ? '' : $convert_config[$config_name];
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Convert an IP address from the hexadecimal notation to normal dotted-quad notation
d6e8d8
*/
d6e8d8
function decode_ip($int_ip)
d6e8d8
{
d6e8d8
	if (!$int_ip)
d6e8d8
	{
d6e8d8
		return $int_ip;
d6e8d8
	}
d6e8d8
d6e8d8
	$hexipbang = explode('.', chunk_split($int_ip, 2, '.'));
d6e8d8
d6e8d8
	// Any mod changing the way ips are stored? Then we are not able to convert and enter the ip "as is" to not "destroy" anything...
d6e8d8
	if (sizeof($hexipbang) < 4)
d6e8d8
	{
d6e8d8
		return $int_ip;
d6e8d8
	}
d6e8d8
d6e8d8
	return hexdec($hexipbang[0]) . '.' . hexdec($hexipbang[1]) . '.' . hexdec($hexipbang[2]) . '.' . hexdec($hexipbang[3]);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Reverse the encoding of wild-carded bans
d6e8d8
*/
d6e8d8
function decode_ban_ip($int_ip)
d6e8d8
{
d6e8d8
	return str_replace('255', '*', decode_ip($int_ip));
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Determine the MIME-type of a specified filename
d6e8d8
* This does not actually inspect the file, but simply uses the file extension
d6e8d8
*/
d6e8d8
function mimetype($filename)
d6e8d8
{
d6e8d8
	if (!preg_match('/\.([a-z0-9]+)$/i', $filename, $m))
d6e8d8
	{
d6e8d8
		return 'application/octet-stream';
d6e8d8
	}
d6e8d8
d6e8d8
	switch (strtolower($m[1]))
d6e8d8
	{
d6e8d8
		case 'zip':		return 'application/zip';
d6e8d8
		case 'jpeg':	return 'image/jpeg';
d6e8d8
		case 'jpg':		return 'image/jpeg';
d6e8d8
		case 'jpe':		return 'image/jpeg';
d6e8d8
		case 'png':		return 'image/png';
d6e8d8
		case 'gif':		return 'image/gif';
d6e8d8
		case 'htm':
d6e8d8
		case 'html':	return 'text/html';
d6e8d8
		case 'tif':		return 'image/tiff';
d6e8d8
		case 'tiff':	return 'image/tiff';
d6e8d8
		case 'ras':		return 'image/x-cmu-raster';
d6e8d8
		case 'pnm':		return 'image/x-portable-anymap';
d6e8d8
		case 'pbm':		return 'image/x-portable-bitmap';
d6e8d8
		case 'pgm':		return 'image/x-portable-graymap';
d6e8d8
		case 'ppm':		return 'image/x-portable-pixmap';
d6e8d8
		case 'rgb':		return 'image/x-rgb';
d6e8d8
		case 'xbm':		return 'image/x-xbitmap';
d6e8d8
		case 'xpm':		return 'image/x-xpixmap';
d6e8d8
		case 'xwd':		return 'image/x-xwindowdump';
d6e8d8
		case 'z':		return 'application/x-compress';
d6e8d8
		case 'gtar':	return 'application/x-gtar';
d6e8d8
		case 'tgz':		return 'application/x-gtar';
d6e8d8
		case 'gz':		return 'application/x-gzip';
d6e8d8
		case 'tar':		return 'application/x-tar';
d6e8d8
		case 'xls':		return 'application/excel';
d6e8d8
		case 'pdf':		return 'application/pdf';
d6e8d8
		case 'ppt':		return 'application/powerpoint';
d6e8d8
		case 'rm':		return 'application/vnd.rn-realmedia';
d6e8d8
		case 'wma':		return 'audio/x-ms-wma';
d6e8d8
		case 'swf':		return 'application/x-shockwave-flash';
d6e8d8
		case 'ief':		return 'image/ief';
d6e8d8
		case 'doc':
d6e8d8
		case 'dot':
d6e8d8
		case 'wrd':		return 'application/msword';
d6e8d8
		case 'ai':
d6e8d8
		case 'eps':
d6e8d8
		case 'ps':		return 'application/postscript';
d6e8d8
		case 'asc':
d6e8d8
		case 'txt':
d6e8d8
		case 'c':
d6e8d8
		case 'cc':
d6e8d8
		case 'h':
d6e8d8
		case 'hh':
d6e8d8
		case 'cpp':
d6e8d8
		case 'hpp':
d6e8d8
		case 'php':
d6e8d8
		case 'php3':	return 'text/plain';
d6e8d8
		default: 		return 'application/octet-stream';
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Obtain the dimensions of all remotely hosted avatars
d6e8d8
* This should only be called from execute_last
d6e8d8
* There can be significant network overhead if there are a large number of remote avatars
d6e8d8
* @todo Look at the option of allowing the user to decide whether this is called or to force the dimensions
d6e8d8
*/
d6e8d8
function remote_avatar_dims()
d6e8d8
{
d6e8d8
	global $db;
d6e8d8
d6e8d8
	$sql = 'SELECT user_id, user_avatar
d6e8d8
		FROM ' . USERS_TABLE . '
d6e8d8
		WHERE user_avatar_type = ' . AVATAR_REMOTE;
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
d6e8d8
	$remote_avatars = array();
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		$remote_avatars[(int) $row['user_id']] = $row['user_avatar'];
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	foreach ($remote_avatars as $user_id => $avatar)
d6e8d8
	{
d6e8d8
		$width = (int) get_remote_avatar_dim($avatar, 0);
d6e8d8
		$height = (int) get_remote_avatar_dim($avatar, 1);
d6e8d8
d6e8d8
		$sql = 'UPDATE ' . USERS_TABLE . '
d6e8d8
			SET user_avatar_width = ' . (int) $width . ', user_avatar_height = ' . (int) $height . '
d6e8d8
			WHERE user_id = ' . $user_id;
d6e8d8
		$db->sql_query($sql);
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
function import_avatar_gallery($gallery_name = '', $subdirs_as_galleries = false)
d6e8d8
{
d6e8d8
	global $config, $convert, $phpbb_root_path, $user;
d6e8d8
d6e8d8
	$relative_path = empty($convert->convertor['source_path_absolute']);
d6e8d8
d6e8d8
	if (empty($convert->convertor['avatar_gallery_path']))
d6e8d8
	{
d6e8d8
		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_GALLERY_PATH'], 'import_avatar_gallery()'), __LINE__, __FILE__);
d6e8d8
	}
d6e8d8
d6e8d8
	$src_path = relative_base(path($convert->convertor['avatar_gallery_path'], $relative_path), $relative_path);
d6e8d8
d6e8d8
	if (is_dir($src_path))
d6e8d8
	{
d6e8d8
		// Do not die on failure... safe mode restrictions may be in effect.
d6e8d8
		copy_dir($convert->convertor['avatar_gallery_path'], path($config['avatar_gallery_path']) . $gallery_name, !$subdirs_as_galleries, false, false, $relative_path);
d6e8d8
d6e8d8
		// only doing 1 level deep. (ibf 1.x)
d6e8d8
		// notes: ibf has 2 tiers: directly in the avatar directory for base gallery (handled in the above statement), plus subdirs(handled below).
d6e8d8
		// recursive subdirs ignored. -- i don't know if other forums support recursive galleries. if they do, this following code could be upgraded to be recursive.
d6e8d8
		if ($subdirs_as_galleries)
d6e8d8
		{
d6e8d8
			$dirlist = array();
d6e8d8
			if ($handle = @opendir($src_path))
d6e8d8
			{
d6e8d8
				while ($entry = readdir($handle))
d6e8d8
				{
d6e8d8
					if ($entry[0] == '.' || $entry == 'CVS' || $entry == 'index.htm')
d6e8d8
					{
d6e8d8
						continue;
d6e8d8
					}
d6e8d8
d6e8d8
					if (is_dir($src_path . $entry))
d6e8d8
					{
d6e8d8
						$dirlist[] = $entry;
d6e8d8
					}
d6e8d8
				}
d6e8d8
				closedir($handle);
d6e8d8
			}
d6e8d8
			else if ($dir = @dir($src_path))
d6e8d8
			{
d6e8d8
				while ($entry = $dir->read())
d6e8d8
				{
d6e8d8
					if ($entry[0] == '.' || $entry == 'CVS' || $entry == 'index.htm')
d6e8d8
					{
d6e8d8
						continue;
d6e8d8
					}
d6e8d8
d6e8d8
					if (is_dir($src_path . $entry))
d6e8d8
					{
d6e8d8
						$dirlist[] = $entry;
d6e8d8
					}
d6e8d8
				}
d6e8d8
				$dir->close();
d6e8d8
			}
d6e8d8
d6e8d8
			for ($i = 0; $i < sizeof($dirlist); ++$i)
d6e8d8
			{
d6e8d8
				$dir = $dirlist[$i];
d6e8d8
d6e8d8
				// Do not die on failure... safe mode restrictions may be in effect.
d6e8d8
				copy_dir(path($convert->convertor['avatar_gallery_path'], $relative_path) . $dir, path($config['avatar_gallery_path']) . $dir, true, false, false, $relative_path);
d6e8d8
			}
d6e8d8
		}
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
function import_attachment_files($category_name = '')
d6e8d8
{
d6e8d8
	global $config, $convert, $phpbb_root_path, $db, $user;
d6e8d8
d6e8d8
	$sql = 'SELECT config_value AS upload_path
d6e8d8
		FROM ' . CONFIG_TABLE . "
d6e8d8
		WHERE config_name = 'upload_path'";
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
	$config['upload_path'] = $db->sql_fetchfield('upload_path');
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	$relative_path = empty($convert->convertor['source_path_absolute']);
d6e8d8
d6e8d8
	if (empty($convert->convertor['upload_path']))
d6e8d8
	{
d6e8d8
		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_UPLOAD_DIR'], 'import_attachment_files()'), __LINE__, __FILE__);
d6e8d8
	}
d6e8d8
d6e8d8
	if (is_dir(relative_base(path($convert->convertor['upload_path'], $relative_path), $relative_path)))
d6e8d8
	{
d6e8d8
		copy_dir($convert->convertor['upload_path'], path($config['upload_path']) . $category_name, true, false, true, $relative_path);
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
function attachment_forum_perms($forum_id)
d6e8d8
{
d6e8d8
	if (!is_array($forum_id))
d6e8d8
	{
d6e8d8
		$forum_id = array($forum_id);
d6e8d8
	}
d6e8d8
d6e8d8
	return serialize($forum_id);
d6e8d8
}
d6e8d8
d6e8d8
// base64todec function
d6e8d8
// -> from php manual?
d6e8d8
function base64_unpack($string)
d6e8d8
{
d6e8d8
	$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-';
d6e8d8
	$base = strlen($chars);
d6e8d8
d6e8d8
	$length = strlen($string);
d6e8d8
	$number = 0;
d6e8d8
d6e8d8
	for ($i = 1; $i <= $length; $i++)
d6e8d8
	{
d6e8d8
		$pos = $length - $i;
d6e8d8
		$operand = strpos($chars, substr($string, $pos, 1));
d6e8d8
		$exponent = pow($base, $i-1);
d6e8d8
		$dec_value = $operand * $exponent;
d6e8d8
		$number += $dec_value;
d6e8d8
	}
d6e8d8
d6e8d8
	return $number;
d6e8d8
}
d6e8d8
d6e8d8
function _import_check($config_var, $source, $use_target)
d6e8d8
{
d6e8d8
	global $convert, $config;
d6e8d8
d6e8d8
	$result = array(
d6e8d8
		'orig_source'	=> $source,
d6e8d8
		'copied'		=> false,
d6e8d8
		'relative_path'	=> (empty($convert->convertor['source_path_absolute'])) ? true : false,
d6e8d8
	);
d6e8d8
d6e8d8
	// copy file will prepend $phpBB_root_path
d6e8d8
	$target = $config[$config_var] . '/' . basename(($use_target === false) ? $source : $use_target);
d6e8d8
d6e8d8
	if (!empty($convert->convertor[$config_var]) && strpos($source, $convert->convertor[$config_var]) !== 0)
d6e8d8
	{
d6e8d8
		$source = $convert->convertor[$config_var] . $source;
d6e8d8
	}
d6e8d8
d6e8d8
	$result['source'] = $source;
d6e8d8
d6e8d8
	if (file_exists(relative_base($source, $result['relative_path'], __LINE__, __FILE__)))
d6e8d8
	{
d6e8d8
		$result['copied'] = copy_file($source, $target, false, false, $result['relative_path']);
d6e8d8
	}
d6e8d8
d6e8d8
	if ($result['copied'])
d6e8d8
	{
d6e8d8
		$result['target'] = basename($target);
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		$result['target'] = ($use_target !== false) ? $result['orig_source'] : basename($target);
d6e8d8
	}
d6e8d8
d6e8d8
	return $result;
d6e8d8
}
d6e8d8
d6e8d8
function import_attachment($source, $use_target = false)
d6e8d8
{
d6e8d8
	if (empty($source))
d6e8d8
	{
d6e8d8
		return '';
d6e8d8
	}
d6e8d8
d6e8d8
	global $convert, $phpbb_root_path, $config, $user;
d6e8d8
d6e8d8
	if (empty($convert->convertor['upload_path']))
d6e8d8
	{
d6e8d8
		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_UPLOAD_DIR'], 'import_attachment()'), __LINE__, __FILE__);
d6e8d8
	}
d6e8d8
d6e8d8
	$result = _import_check('upload_path', $source, $use_target);
d6e8d8
d6e8d8
	if ($result['copied'])
d6e8d8
	{
d6e8d8
		// Thumbnails?
d6e8d8
		if (is_array($convert->convertor['thumbnails']))
d6e8d8
		{
d6e8d8
			$thumb_dir = $convert->convertor['thumbnails'][0];
d6e8d8
			$thumb_prefix = $convert->convertor['thumbnails'][1];
d6e8d8
			$thumb_source = $thumb_dir . $thumb_prefix . basename($result['source']);
d6e8d8
d6e8d8
			if (strpos($thumb_source, $convert->convertor['upload_path']) !== 0)
d6e8d8
			{
d6e8d8
				$thumb_source = $convert->convertor['upload_path'] . $thumb_source;
d6e8d8
			}
d6e8d8
			$thumb_target = $config['upload_path'] . '/thumb_' . $result['target'];
d6e8d8
d6e8d8
			if (file_exists(relative_base($thumb_source, $result['relative_path'], __LINE__, __FILE__)))
d6e8d8
			{
d6e8d8
				copy_file($thumb_source, $thumb_target, false, false, $result['relative_path']);
d6e8d8
			}
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	return $result['target'];
d6e8d8
}
d6e8d8
d6e8d8
function import_rank($source, $use_target = false)
d6e8d8
{
d6e8d8
	if (empty($source))
d6e8d8
	{
d6e8d8
		return '';
d6e8d8
	}
d6e8d8
d6e8d8
	global $convert, $phpbb_root_path, $config, $user;
d6e8d8
d6e8d8
	if (!isset($convert->convertor['ranks_path']))
d6e8d8
	{
d6e8d8
		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_RANKS_PATH'], 'import_rank()'), __LINE__, __FILE__);
d6e8d8
	}
d6e8d8
d6e8d8
	$result = _import_check('ranks_path', $source, $use_target);
d6e8d8
	return $result['target'];
d6e8d8
}
d6e8d8
d6e8d8
function import_smiley($source, $use_target = false)
d6e8d8
{
d6e8d8
	if (empty($source))
d6e8d8
	{
d6e8d8
		return '';
d6e8d8
	}
d6e8d8
d6e8d8
	global $convert, $phpbb_root_path, $config, $user;
d6e8d8
d6e8d8
	if (!isset($convert->convertor['smilies_path']))
d6e8d8
	{
d6e8d8
		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_SMILIES_PATH'], 'import_smiley()'), __LINE__, __FILE__);
d6e8d8
	}
d6e8d8
d6e8d8
	$result = _import_check('smilies_path', $source, $use_target);
d6e8d8
	return $result['target'];
d6e8d8
}
d6e8d8
d6e8d8
/*
d6e8d8
*/
d6e8d8
function import_avatar($source, $use_target = false, $user_id = false)
d6e8d8
{
d6e8d8
	if (empty($source) || preg_match('#^https?:#i', $source) || preg_match('#blank\.(gif|png)$#i', $source))
d6e8d8
	{
d6e8d8
		return;
d6e8d8
	}
d6e8d8
d6e8d8
	global $convert, $phpbb_root_path, $config, $user;
d6e8d8
d6e8d8
	if (!isset($convert->convertor['avatar_path']))
d6e8d8
	{
d6e8d8
		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_AVATAR_PATH'], 'import_avatar()'), __LINE__, __FILE__);
d6e8d8
	}
d6e8d8
d6e8d8
	if ($use_target === false && $user_id !== false)
d6e8d8
	{
d6e8d8
		$use_target = $config['avatar_salt'] . '_' . $user_id . '.' . substr(strrchr($source, '.'), 1);
d6e8d8
	}
d6e8d8
d6e8d8
	$result = _import_check('avatar_path', $source, $use_target);
d6e8d8
d6e8d8
	return ((!empty($user_id)) ? $user_id : $use_target) . '.' . substr(strrchr($source, '.'), 1);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* @todo all image dimension functions below (there are a *lot*) should get revisited and converted to one or two functions (no more needed, really).
d6e8d8
*/
d6e8d8
d6e8d8
/**
d6e8d8
* Calculate the size of the specified image
d6e8d8
* Called from the following functions for calculating the size of specific image types
d6e8d8
*/
d6e8d8
function get_image_dim($source)
d6e8d8
{
d6e8d8
	if (empty($source))
d6e8d8
	{
d6e8d8
		return array(0, 0);
d6e8d8
	}
d6e8d8
d6e8d8
	global $convert;
d6e8d8
d6e8d8
	$relative_path = empty($convert->convertor['source_path_absolute']);
d6e8d8
d6e8d8
	if (file_exists(relative_base($source, $relative_path)))
d6e8d8
	{
d6e8d8
		$image = relative_base($source, $relative_path);
d6e8d8
		return @getimagesize($image);
d6e8d8
	}
d6e8d8
d6e8d8
	return false;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Obtain the width of the specified smilie
d6e8d8
*/
d6e8d8
function get_smiley_width($src)
d6e8d8
{
d6e8d8
	return get_smiley_dim($src, 0);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Obtain the height of the specified smilie
d6e8d8
*/
d6e8d8
function get_smiley_height($src)
d6e8d8
{
d6e8d8
	return get_smiley_dim($src, 1);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Obtain the size of the specified smilie (using the cache if possible) and cache the value
d6e8d8
*/
d6e8d8
function get_smiley_dim($source, $axis)
d6e8d8
{
d6e8d8
	if (empty($source))
d6e8d8
	{
d6e8d8
		return 15;
d6e8d8
	}
d6e8d8
d6e8d8
	static $smiley_cache = array();
d6e8d8
d6e8d8
	if (isset($smiley_cache[$source]))
d6e8d8
	{
d6e8d8
		return $smiley_cache[$source][$axis];
d6e8d8
	}
d6e8d8
d6e8d8
	global $convert, $phpbb_root_path, $config, $user;
d6e8d8
d6e8d8
	$orig_source = $source;
d6e8d8
d6e8d8
	if (!isset($convert->convertor['smilies_path']))
d6e8d8
	{
d6e8d8
		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_SMILIES_PATH'], 'get_smiley_dim()'), __LINE__, __FILE__);
d6e8d8
	}
d6e8d8
d6e8d8
	if (!empty($convert->convertor['smilies_path']) && strpos($source, $convert->convertor['smilies_path']) !== 0)
d6e8d8
	{
d6e8d8
		$source = $convert->convertor['smilies_path'] . $source;
d6e8d8
	}
d6e8d8
d6e8d8
	$smiley_cache[$orig_source] = get_image_dim($source);
d6e8d8
d6e8d8
	if (empty($smiley_cache[$orig_source]) || empty($smiley_cache[$orig_source][0]) || empty($smiley_cache[$orig_source][1]))
d6e8d8
	{
d6e8d8
		$smiley_cache[$orig_source] = array(15, 15);
d6e8d8
		return 15;
d6e8d8
	}
d6e8d8
d6e8d8
	return $smiley_cache[$orig_source][$axis];
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Obtain the width of the specified avatar
d6e8d8
*/
d6e8d8
function get_avatar_width($src, $func = false, $arg1 = false, $arg2 = false)
d6e8d8
{
d6e8d8
	return get_avatar_dim($src, 0, $func, $arg1, $arg2);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Obtain the height of the specified avatar
d6e8d8
*/
d6e8d8
function get_avatar_height($src, $func = false, $arg1 = false, $arg2 = false)
d6e8d8
{
d6e8d8
	return get_avatar_dim($src, 1, $func, $arg1, $arg2);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
*/
d6e8d8
function get_avatar_dim($src, $axis, $func = false, $arg1 = false, $arg2 = false)
d6e8d8
{
d6e8d8
	$avatar_type = AVATAR_UPLOAD;
d6e8d8
d6e8d8
	if ($func)
d6e8d8
	{
d6e8d8
		if ($arg1 || $arg2)
d6e8d8
		{
d6e8d8
			$ary = array($arg1);
d6e8d8
d6e8d8
			if ($arg2)
d6e8d8
			{
d6e8d8
				$ary[] = $arg2;
d6e8d8
			}
d6e8d8
d6e8d8
			$avatar_type = call_user_func_array($func, $ary);
d6e8d8
		}
d6e8d8
		else
d6e8d8
		{
d6e8d8
			$avatar_type = call_user_func($func);
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	switch ($avatar_type)
d6e8d8
	{
d6e8d8
		case AVATAR_UPLOAD:
d6e8d8
			return get_upload_avatar_dim($src, $axis);
d6e8d8
		break;
d6e8d8
d6e8d8
		case AVATAR_GALLERY:
d6e8d8
			return get_gallery_avatar_dim($src, $axis);
d6e8d8
		break;
d6e8d8
d6e8d8
		case AVATAR_REMOTE:
d6e8d8
			 // see notes on this functions usage and (hopefully) model $func to avoid this accordingly
d6e8d8
			return get_remote_avatar_dim($src, $axis);
d6e8d8
		break;
d6e8d8
d6e8d8
		default:
d6e8d8
			$default_x = (defined('DEFAULT_AVATAR_X_CUSTOM')) ? DEFAULT_AVATAR_X_CUSTOM : DEFAULT_AVATAR_X;
d6e8d8
			$default_y = (defined('DEFAULT_AVATAR_Y_CUSTOM')) ? DEFAULT_AVATAR_Y_CUSTOM : DEFAULT_AVATAR_Y;
d6e8d8
d6e8d8
			return $axis ? $default_y : $default_x;
d6e8d8
		break;
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Obtain the size of the specified uploaded avatar (using the cache if possible) and cache the value
d6e8d8
*/
d6e8d8
function get_upload_avatar_dim($source, $axis)
d6e8d8
{
d6e8d8
	static $cachedims = false;
d6e8d8
	static $cachekey = false;
d6e8d8
d6e8d8
	if (empty($source))
d6e8d8
	{
d6e8d8
		return 0;
d6e8d8
	}
d6e8d8
d6e8d8
	if ($cachekey == $source)
d6e8d8
	{
d6e8d8
		return $cachedims[$axis];
d6e8d8
	}
d6e8d8
d6e8d8
	$orig_source = $source;
d6e8d8
d6e8d8
	if (substr($source, 0, 7) == 'upload:')
d6e8d8
	{
d6e8d8
		$source = substr($source, 7);
d6e8d8
	}
d6e8d8
d6e8d8
	global $convert, $phpbb_root_path, $config, $user;
d6e8d8
d6e8d8
	if (!isset($convert->convertor['avatar_path']))
d6e8d8
	{
d6e8d8
		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_AVATAR_PATH'], 'get_upload_avatar_dim()'), __LINE__, __FILE__);
d6e8d8
	}
d6e8d8
d6e8d8
	if (!empty($convert->convertor['avatar_path']) && strpos($source, $convert->convertor['avatar_path']) !== 0)
d6e8d8
	{
d6e8d8
		$source = path($convert->convertor['avatar_path'], empty($convert->convertor['source_path_absolute'])) . $source;
d6e8d8
	}
d6e8d8
d6e8d8
	$cachedims = get_image_dim($source);
d6e8d8
d6e8d8
	if (empty($cachedims) || empty($cachedims[0]) || empty($cachedims[1]))
d6e8d8
	{
d6e8d8
		$default_x = (defined('DEFAULT_AVATAR_X_CUSTOM')) ? DEFAULT_AVATAR_X_CUSTOM : DEFAULT_AVATAR_X;
d6e8d8
		$default_y = (defined('DEFAULT_AVATAR_Y_CUSTOM')) ? DEFAULT_AVATAR_Y_CUSTOM : DEFAULT_AVATAR_Y;
d6e8d8
d6e8d8
		$cachedims = array($default_x, $default_y);
d6e8d8
	}
d6e8d8
d6e8d8
	return $cachedims[$axis];
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Obtain the size of the specified gallery avatar (using the cache if possible) and cache the value
d6e8d8
*/
d6e8d8
function get_gallery_avatar_dim($source, $axis)
d6e8d8
{
d6e8d8
	if (empty($source))
d6e8d8
	{
d6e8d8
		return 0;
d6e8d8
	}
d6e8d8
d6e8d8
	static $avatar_cache = array();
d6e8d8
d6e8d8
	if (isset($avatar_cache[$source]))
d6e8d8
	{
d6e8d8
		return $avatar_cache[$source][$axis];
d6e8d8
	}
d6e8d8
d6e8d8
	global $convert, $phpbb_root_path, $config, $user;
d6e8d8
d6e8d8
	$orig_source = $source;
d6e8d8
d6e8d8
	if (!isset($convert->convertor['avatar_gallery_path']))
d6e8d8
	{
d6e8d8
		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_GALLERY_PATH'], 'get_gallery_avatar_dim()'), __LINE__, __FILE__);
d6e8d8
	}
d6e8d8
d6e8d8
	if (!empty($convert->convertor['avatar_gallery_path']) && strpos($source, $convert->convertor['avatar_gallery_path']) !== 0)
d6e8d8
	{
d6e8d8
		$source = path($convert->convertor['avatar_gallery_path'], empty($convert->convertor['source_path_absolute'])) . $source;
d6e8d8
	}
d6e8d8
d6e8d8
	$avatar_cache[$orig_source] = get_image_dim($source);
d6e8d8
d6e8d8
	if (empty($avatar_cache[$orig_source]) || empty($avatar_cache[$orig_source][0]) || empty($avatar_cache[$orig_source][1]))
d6e8d8
	{
d6e8d8
		$default_x = (defined('DEFAULT_AVATAR_X_CUSTOM')) ? DEFAULT_AVATAR_X_CUSTOM : DEFAULT_AVATAR_X;
d6e8d8
		$default_y = (defined('DEFAULT_AVATAR_Y_CUSTOM')) ? DEFAULT_AVATAR_Y_CUSTOM : DEFAULT_AVATAR_Y;
d6e8d8
d6e8d8
		$avatar_cache[$orig_source] = array($default_x, $default_y);
d6e8d8
	}
d6e8d8
d6e8d8
	return $avatar_cache[$orig_source][$axis];
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Obtain the size of the specified remote avatar (using the cache if possible) and cache the value
d6e8d8
* Whilst it's unlikely that remote avatars will be duplicated, it is possible so caching seems the best option
d6e8d8
* This should only be called from a post processing step due to the possibility of network timeouts
d6e8d8
*/
d6e8d8
function get_remote_avatar_dim($src, $axis)
d6e8d8
{
d6e8d8
	if (empty($src))
d6e8d8
	{
d6e8d8
		return 0;
d6e8d8
	}
d6e8d8
d6e8d8
	static $remote_avatar_cache = array();
d6e8d8
d6e8d8
	// an ugly hack: we assume that the dimensions of each remote avatar are accessed exactly twice (x and y)
d6e8d8
	if (isset($remote_avatar_cache[$src]))
d6e8d8
	{
d6e8d8
		$retval = $remote_avatar_cache[$src][$axis];
d6e8d8
		unset($remote_avatar_cache);
d6e8d8
		return $retval;
d6e8d8
	}
d6e8d8
d6e8d8
	$url_info = @parse_url($src);
d6e8d8
	if (empty($url_info['host']))
d6e8d8
	{
d6e8d8
		return 0;
d6e8d8
	}
d6e8d8
	$host = $url_info['host'];
d6e8d8
	$port = (isset($url_info['port'])) ? $url_info['port'] : 0;
d6e8d8
	$protocol = (isset($url_info['scheme'])) ? $url_info['scheme'] : 'http';
d6e8d8
	if (empty($port))
d6e8d8
	{
d6e8d8
		switch(strtolower($protocol))
d6e8d8
		{
d6e8d8
			case 'ftp':
d6e8d8
				$port = 21;
d6e8d8
				break;
d6e8d8
d6e8d8
			case 'https':
d6e8d8
				$port = 443;
d6e8d8
				break;
d6e8d8
d6e8d8
			default:
d6e8d8
				$port = 80;
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	$timeout = @ini_get('default_socket_timeout');
d6e8d8
	@ini_set('default_socket_timeout', 2);
d6e8d8
d6e8d8
	// We're just trying to reach the server to avoid timeouts
d6e8d8
	$fp = @fsockopen($host, $port, $errno, $errstr, 1);
d6e8d8
	if ($fp)
d6e8d8
	{
d6e8d8
		$remote_avatar_cache[$src] = @getimagesize($src);
d6e8d8
		fclose($fp);
d6e8d8
	}
d6e8d8
d6e8d8
	$default_x 	= (defined('DEFAULT_AVATAR_X_CUSTOM')) ? DEFAULT_AVATAR_X_CUSTOM : DEFAULT_AVATAR_X;
d6e8d8
	$default_y 	= (defined('DEFAULT_AVATAR_Y_CUSTOM')) ? DEFAULT_AVATAR_Y_CUSTOM : DEFAULT_AVATAR_Y;
d6e8d8
	$default 	= array($default_x, $default_y);
d6e8d8
d6e8d8
	if (empty($remote_avatar_cache[$src]) || empty($remote_avatar_cache[$src][0]) || empty($remote_avatar_cache[$src][1]))
d6e8d8
	{
d6e8d8
		$remote_avatar_cache[$src] = $default;
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		// We trust gallery and uploaded avatars to conform to the size settings; we might have to adjust here
d6e8d8
		if ($remote_avatar_cache[$src][0] > $default_x || $remote_avatar_cache[$src][1] > $default_y)
d6e8d8
		{
d6e8d8
			$bigger = ($remote_avatar_cache[$src][0] > $remote_avatar_cache[$src][1]) ? 0 : 1;
d6e8d8
			$ratio = $default[$bigger] / $remote_avatar_cache[$src][$bigger];
d6e8d8
			$remote_avatar_cache[$src][0] = (int)($remote_avatar_cache[$src][0] * $ratio);
d6e8d8
			$remote_avatar_cache[$src][1] = (int)($remote_avatar_cache[$src][1] * $ratio);
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	@ini_set('default_socket_timeout', $timeout);
d6e8d8
	return $remote_avatar_cache[$src][$axis];
d6e8d8
}
d6e8d8
d6e8d8
function set_user_options()
d6e8d8
{
d6e8d8
	global $convert_row;
d6e8d8
d6e8d8
	// Key need to be set in row, else default value is chosen
d6e8d8
	$keyoptions = array(
d6e8d8
		'viewimg'		=> array('bit' => 0, 'default' => 1),
d6e8d8
		'viewflash'		=> array('bit' => 1, 'default' => 1),
d6e8d8
		'viewsmilies'	=> array('bit' => 2, 'default' => 1),
d6e8d8
		'viewsigs'		=> array('bit' => 3, 'default' => 1),
d6e8d8
		'viewavatars'	=> array('bit' => 4, 'default' => 1),
d6e8d8
		'viewcensors'	=> array('bit' => 5, 'default' => 1),
d6e8d8
		'attachsig'		=> array('bit' => 6, 'default' => 0),
d6e8d8
		'bbcode'		=> array('bit' => 8, 'default' => 1),
d6e8d8
		'smilies'		=> array('bit' => 9, 'default' => 1),
d6e8d8
		'popuppm'		=> array('bit' => 10, 'default' => 0),
d6e8d8
	);
d6e8d8
d6e8d8
	$option_field = 0;
d6e8d8
d6e8d8
	foreach ($keyoptions as $key => $key_ary)
d6e8d8
	{
d6e8d8
		$value = (isset($convert_row[$key])) ? (int) $convert_row[$key] : $key_ary['default'];
d6e8d8
d6e8d8
		if ($value && !($option_field & 1 << $key_ary['bit']))
d6e8d8
		{
d6e8d8
			$option_field += 1 << $key_ary['bit'];
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	return $option_field;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Index messages on the fly as we convert them
d6e8d8
* @todo naderman, can you check that this works with the new search plugins as it's use is currently disabled (and thus untested)
d6e8d8
function search_indexing($message = '')
d6e8d8
{
d6e8d8
	global $fulltext_search, $convert_row;
d6e8d8
d6e8d8
	if (!isset($convert_row['post_id']))
d6e8d8
	{
d6e8d8
		return;
d6e8d8
	}
d6e8d8
d6e8d8
	if (!$message)
d6e8d8
	{
d6e8d8
		if (!isset($convert_row['message']))
d6e8d8
		{
d6e8d8
			return;
d6e8d8
		}
d6e8d8
d6e8d8
		$message = $convert_row['message'];
d6e8d8
	}
d6e8d8
d6e8d8
	$title = (isset($convert_row['title'])) ? $convert_row['title'] : '';
d6e8d8
d6e8d8
	$fulltext_search->index('post', $convert_row['post_id'], $message, $title, $convert_row['poster_id'], $convert_row['forum_id']);
d6e8d8
}
d6e8d8
*/
d6e8d8
d6e8d8
function make_unique_filename($filename)
d6e8d8
{
d6e8d8
	if (!strlen($filename))
d6e8d8
	{
d6e8d8
		$filename = md5(unique_id()) . '.dat';
d6e8d8
	}
d6e8d8
	else if ($filename[0] == '.')
d6e8d8
	{
d6e8d8
		$filename = md5(unique_id()) . $filename;
d6e8d8
	}
d6e8d8
	else if (preg_match('/\.([a-z]+)$/i', $filename, $m))
d6e8d8
	{
d6e8d8
		$filename = preg_replace('/\.([a-z]+)$/i', '_' . md5(unique_id()) . '.\1', $filename);
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		$filename .= '_' . md5(unique_id()) . '.dat';
d6e8d8
	}
d6e8d8
d6e8d8
	return $filename;
d6e8d8
}
d6e8d8
d6e8d8
function words_unique(&$words)
d6e8d8
{
d6e8d8
	reset($words);
d6e8d8
	$return_array = array();
d6e8d8
d6e8d8
	$word = current($words);
d6e8d8
	do
d6e8d8
	{
d6e8d8
		$return_array[$word] = $word;
d6e8d8
	}
d6e8d8
	while ($word = next($words));
d6e8d8
d6e8d8
	return $return_array;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Adds a user to the specified group and optionally makes them a group leader
d6e8d8
* This function does not create the group if it does not exist and so should only be called after the groups have been created
d6e8d8
*/
d6e8d8
function add_user_group($group_id, $user_id, $group_leader=false)
d6e8d8
{
d6e8d8
	global $convert, $phpbb_root_path, $config, $user, $db;
d6e8d8
d6e8d8
	$sql = 'INSERT INTO ' . USER_GROUP_TABLE . ' ' . $db->sql_build_array('INSERT', array(
d6e8d8
		'group_id'		=> $group_id,
d6e8d8
		'user_id'		=> $user_id,
d6e8d8
		'group_leader'	=> ($group_leader) ? 1 : 0,
d6e8d8
		'user_pending'	=> 0));
d6e8d8
	$db->sql_query($sql);
d6e8d8
}
d6e8d8
d6e8d8
// STANDALONE FUNCTIONS
d6e8d8
d6e8d8
/**
d6e8d8
* Add users to the pre-defined "special" groups
d6e8d8
*
d6e8d8
* @param string $group The name of the special group to add to
d6e8d8
* @param string $select_query An SQL query to retrieve the user(s) to add to the group
d6e8d8
*/
d6e8d8
function user_group_auth($group, $select_query, $use_src_db)
d6e8d8
{
d6e8d8
	global $convert, $phpbb_root_path, $config, $user, $db, $src_db, $same_db;
d6e8d8
d6e8d8
	if (!in_array($group, array('guests', 'registered', 'registered_coppa', 'global_moderators', 'administrators', 'bots')))
d6e8d8
	{
d6e8d8
		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_WRONG_GROUP'], $group, 'user_group_auth()'), __LINE__, __FILE__, true);
d6e8d8
		return;
d6e8d8
	}
d6e8d8
d6e8d8
	$sql = 'SELECT group_id
d6e8d8
		FROM ' . GROUPS_TABLE . "
d6e8d8
		WHERE group_name = '" . $db->sql_escape(strtoupper($group)) . "'";
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
	$group_id = (int) $db->sql_fetchfield('group_id');
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	if (!$group_id)
d6e8d8
	{
d6e8d8
		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_GROUP'], $group, 'user_group_auth()'), __LINE__, __FILE__, true);
d6e8d8
		return;
d6e8d8
	}
d6e8d8
d6e8d8
	if ($same_db || !$use_src_db)
d6e8d8
	{
d6e8d8
		$sql = 'INSERT INTO ' . USER_GROUP_TABLE . ' (user_id, group_id, user_pending)
d6e8d8
			' . str_replace('{' . strtoupper($group) . '}', $group_id . ', 0', $select_query);
d6e8d8
		$db->sql_query($sql);
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		$result = $src_db->sql_query(str_replace('{' . strtoupper($group) . '}', $group_id . ' ', $select_query));
d6e8d8
		while ($row = $src_db->sql_fetchrow($result))
d6e8d8
		{
d6e8d8
			// this might become quite a lot of INSERTS unfortunately
d6e8d8
			$sql = 'INSERT INTO ' . USER_GROUP_TABLE . " (user_id, group_id, user_pending)
d6e8d8
				VALUES ({$row['user_id']}, $group_id, 0)";
d6e8d8
			$db->sql_query($sql);
d6e8d8
		}
d6e8d8
		$src_db->sql_freeresult($result);
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Retrieves configuration information from the source forum and caches it as an array
d6e8d8
* Both database and file driven configuration formats can be handled
d6e8d8
* (the type used is specified in $config_schema, see convert_phpbb20.php for more details)
d6e8d8
*/
d6e8d8
function get_config()
d6e8d8
{
d6e8d8
	static $convert_config;
d6e8d8
	global $user;
d6e8d8
d6e8d8
	if (isset($convert_config))
d6e8d8
	{
d6e8d8
		return $convert_config;
d6e8d8
	}
d6e8d8
d6e8d8
	global $src_db, $same_db, $phpbb_root_path, $config;
d6e8d8
	global $convert;
d6e8d8
d6e8d8
	if ($convert->config_schema['table_format'] != 'file')
d6e8d8
	{
d6e8d8
		if ($convert->mysql_convert && $same_db)
d6e8d8
		{
d6e8d8
			$src_db->sql_query("SET NAMES 'binary'");
d6e8d8
		}
d6e8d8
d6e8d8
		$sql = 'SELECT * FROM ' . $convert->src_table_prefix . $convert->config_schema['table_name'];
d6e8d8
		$result = $src_db->sql_query($sql);
d6e8d8
		$row = $src_db->sql_fetchrow($result);
d6e8d8
d6e8d8
		if (!$row)
d6e8d8
		{
d6e8d8
			$convert->p_master->error($user->lang['CONV_ERROR_GET_CONFIG'], __LINE__, __FILE__);
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	if (is_array($convert->config_schema['table_format']))
d6e8d8
	{
d6e8d8
		$convert_config = array();
d6e8d8
		list($key, $val) = each($convert->config_schema['table_format']);
d6e8d8
d6e8d8
		do
d6e8d8
		{
d6e8d8
			$convert_config[$row[$key]] = $row[$val];
d6e8d8
		}
d6e8d8
		while ($row = $src_db->sql_fetchrow($result));
d6e8d8
		$src_db->sql_freeresult($result);
d6e8d8
d6e8d8
		if ($convert->mysql_convert && $same_db)
d6e8d8
		{
d6e8d8
			$src_db->sql_query("SET NAMES 'utf8'");
d6e8d8
		}
d6e8d8
	}
d6e8d8
	else if ($convert->config_schema['table_format'] == 'file')
d6e8d8
	{
d6e8d8
		$filename = $convert->options['forum_path'] . '/' . $convert->config_schema['filename'];
d6e8d8
		if (!file_exists($filename))
d6e8d8
		{
d6e8d8
			$convert->p_master->error($user->lang['FILE_NOT_FOUND'] . ': ' . $filename, __LINE__, __FILE__);
d6e8d8
		}
d6e8d8
d6e8d8
		$convert_config = extract_variables_from_file($filename);
d6e8d8
		if (!empty($convert->config_schema['array_name']))
d6e8d8
		{
d6e8d8
			$convert_config = $convert_config[$convert->config_schema['array_name']];
d6e8d8
		}
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		$convert_config = $row;
d6e8d8
		if ($convert->mysql_convert && $same_db)
d6e8d8
		{
d6e8d8
			$src_db->sql_query("SET NAMES 'utf8'");
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	if (!sizeof($convert_config))
d6e8d8
	{
d6e8d8
		$convert->p_master->error($user->lang['CONV_ERROR_CONFIG_EMPTY'], __LINE__, __FILE__);
d6e8d8
	}
d6e8d8
d6e8d8
	return $convert_config;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Transfers the relevant configuration information from the source forum
d6e8d8
* The mapping of fields is specified in $config_schema, see convert_phpbb20.php for more details
d6e8d8
*/
d6e8d8
function restore_config($schema)
d6e8d8
{
d6e8d8
	global $db, $config;
d6e8d8
d6e8d8
	$convert_config = get_config();
d6e8d8
	foreach ($schema['settings'] as $config_name => $src)
d6e8d8
	{
d6e8d8
		if (preg_match('/(.*)\((.*)\)/', $src, $m))
d6e8d8
		{
d6e8d8
			$var = (empty($m[2]) || empty($convert_config[$m[2]])) ? "''" : "'" . addslashes($convert_config[$m[2]]) . "'";
d6e8d8
			$exec = '$config_value = ' . $m[1] . '(' . $var . ');';
d6e8d8
			eval($exec);
d6e8d8
		}
d6e8d8
		else
d6e8d8
		{
d6e8d8
			$config_value = (isset($convert_config[$src])) ? $convert_config[$src] : '';
d6e8d8
		}
d6e8d8
d6e8d8
		if ($config_value !== '')
d6e8d8
		{
d6e8d8
			// Most are...
d6e8d8
			if (is_string($config_value))
d6e8d8
			{
d6e8d8
				$config_value = truncate_string(utf8_htmlspecialchars($config_value), 255, 255, false);
d6e8d8
			}
d6e8d8
d6e8d8
			set_config($config_name, $config_value);
d6e8d8
		}
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Update the count of PM's in custom folders for all users
d6e8d8
*/
d6e8d8
function update_folder_pm_count()
d6e8d8
{
d6e8d8
	global $db, $convert, $user;
d6e8d8
d6e8d8
	$sql = 'SELECT user_id, folder_id, COUNT(msg_id) as num_messages
d6e8d8
		FROM ' . PRIVMSGS_TO_TABLE . '
d6e8d8
		WHERE folder_id NOT IN (' . PRIVMSGS_NO_BOX . ', ' . PRIVMSGS_HOLD_BOX . ', ' . PRIVMSGS_INBOX . ', ' . PRIVMSGS_OUTBOX . ', ' . PRIVMSGS_SENTBOX . ')
d6e8d8
		GROUP BY folder_id, user_id';
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		$db->sql_query('UPDATE ' . PRIVMSGS_FOLDER_TABLE . ' SET pm_count = ' . $row['num_messages'] . '
d6e8d8
			WHERE user_id = ' . $row['user_id'] . ' AND folder_id = ' . $row['folder_id']);
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
}
d6e8d8
d6e8d8
// Functions mainly used by the main convertor script
d6e8d8
d6e8d8
function path($path, $path_relative = true)
d6e8d8
{
d6e8d8
	if ($path === false)
d6e8d8
	{
d6e8d8
		return '';
d6e8d8
	}
d6e8d8
d6e8d8
	if (substr($path, -1) != '/')
d6e8d8
	{
d6e8d8
		$path .= '/';
d6e8d8
	}
d6e8d8
d6e8d8
	if (!$path_relative)
d6e8d8
	{
d6e8d8
		return $path;
d6e8d8
	}
d6e8d8
d6e8d8
	if (substr($path, 0, 1) == '/')
d6e8d8
	{
d6e8d8
		$path = substr($path, 1);
d6e8d8
	}
d6e8d8
d6e8d8
	return $path;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Extract the variables defined in a configuration file
d6e8d8
* @todo As noted by Xore we need to look at this from a security perspective
d6e8d8
*/
d6e8d8
function extract_variables_from_file($_filename)
d6e8d8
{
d6e8d8
	include($_filename);
d6e8d8
d6e8d8
	$vars = get_defined_vars();
d6e8d8
	unset($vars['_filename']);
d6e8d8
d6e8d8
	return $vars;
d6e8d8
}
d6e8d8
d6e8d8
function get_path($src_path, $src_url, $test_file)
d6e8d8
{
d6e8d8
	global $config, $phpbb_root_path, $phpEx;
d6e8d8
d6e8d8
	$board_config = get_config();
d6e8d8
d6e8d8
	$test_file = preg_replace('/\.php$/i', ".$phpEx", $test_file);
d6e8d8
	$src_path = path($src_path);
d6e8d8
d6e8d8
	if (@file_exists($phpbb_root_path . $src_path . $test_file))
d6e8d8
	{
d6e8d8
		return $src_path;
d6e8d8
	}
d6e8d8
d6e8d8
	if (!empty($src_url) && !empty($board_config['server_name']))
d6e8d8
	{
d6e8d8
		if (!preg_match('#https?://([^/]+)(.*)#i', $src_url, $m))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		if ($m[1] != $board_config['server_name'])
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		$url_parts = explode('/', $m[2]);
d6e8d8
		if (substr($src_url, -1) != '/')
d6e8d8
		{
d6e8d8
			if (preg_match('/.*\.([a-z0-9]{3,4})$/i', $url_parts[sizeof($url_parts) - 1]))
d6e8d8
			{
d6e8d8
				$url_parts[sizeof($url_parts) - 1] = '';
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				$url_parts[] = '';
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		$script_path = $board_config['script_path'];
d6e8d8
		if (substr($script_path, -1) == '/')
d6e8d8
		{
d6e8d8
			$script_path = substr($script_path, 0, -1);
d6e8d8
		}
d6e8d8
d6e8d8
		$path_array = array();
d6e8d8
d6e8d8
		$phpbb_parts = explode('/', $script_path);
d6e8d8
		for ($i = 0; $i < sizeof($url_parts); ++$i)
d6e8d8
		{
d6e8d8
			if ($i < sizeof($phpbb_parts[$i]) && $url_parts[$i] == $phpbb_parts[$i])
d6e8d8
			{
d6e8d8
				$path_array[] = $url_parts[$i];
d6e8d8
				unset($url_parts[$i]);
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				$path = '';
d6e8d8
				for ($j = $i; $j < sizeof($phpbb_parts); ++$j)
d6e8d8
				{
d6e8d8
					$path .= '../';
d6e8d8
				}
d6e8d8
				$path .= implode('/', $url_parts);
d6e8d8
				break;
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		if (!empty($path))
d6e8d8
		{
d6e8d8
			if (@file_exists($phpbb_root_path . $path . $test_file))
d6e8d8
			{
d6e8d8
				return $path;
d6e8d8
			}
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	return false;
d6e8d8
}
d6e8d8
d6e8d8
function compare_table($tables, $tablename, &$prefixes)
d6e8d8
{
d6e8d8
	for ($i = 0, $table_size = sizeof($tables); $i < $table_size; ++$i)
d6e8d8
	{
d6e8d8
		if (preg_match('/(.*)' . $tables[$i] . '$/', $tablename, $m))
d6e8d8
		{
d6e8d8
			if (empty($m[1]))
d6e8d8
			{
d6e8d8
				$m[1] = '*';
d6e8d8
			}
d6e8d8
d6e8d8
			if (isset($prefixes[$m[1]]))
d6e8d8
			{
d6e8d8
				$prefixes[$m[1]]++;
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				$prefixes[$m[1]] = 1;
d6e8d8
			}
d6e8d8
		}
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Grant permissions to a specified user or group
d6e8d8
*
d6e8d8
* @param string $ug_type user|group|user_role|group_role
d6e8d8
* @param mixed $forum_id forum ids (array|int|0) -> 0 == all forums
d6e8d8
* @param mixed $ug_id [int] user_id|group_id : [string] usergroup name
d6e8d8
* @param mixed $acl_list [string] acl entry : [array] acl entries : [string] role entry
d6e8d8
* @param int $setting ACL_YES|ACL_NO|ACL_NEVER
d6e8d8
*/
d6e8d8
function mass_auth($ug_type, $forum_id, $ug_id, $acl_list, $setting = ACL_NO)
d6e8d8
{
d6e8d8
	global $db, $convert, $user, $config;
d6e8d8
	static $acl_option_ids, $group_ids;
d6e8d8
d6e8d8
	if (($ug_type == 'group' || $ug_type == 'group_role') && is_string($ug_id))
d6e8d8
	{
d6e8d8
		if (!isset($group_ids[$ug_id]))
d6e8d8
		{
d6e8d8
			$sql = 'SELECT group_id
d6e8d8
				FROM ' . GROUPS_TABLE . "
d6e8d8
				WHERE group_name = '" . $db->sql_escape(strtoupper($ug_id)) . "'";
d6e8d8
			$result = $db->sql_query_limit($sql, 1);
d6e8d8
			$id = (int) $db->sql_fetchfield('group_id');
d6e8d8
			$db->sql_freeresult($result);
d6e8d8
d6e8d8
			if (!$id)
d6e8d8
			{
d6e8d8
				return;
d6e8d8
			}
d6e8d8
d6e8d8
			$group_ids[$ug_id] = $id;
d6e8d8
		}
d6e8d8
d6e8d8
		$ug_id = (int) $group_ids[$ug_id];
d6e8d8
	}
d6e8d8
d6e8d8
	$table = ($ug_type == 'user' || $ug_type == 'user_role') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
d6e8d8
	$id_field = ($ug_type == 'user' || $ug_type == 'user_role') ? 'user_id' : 'group_id';
d6e8d8
d6e8d8
	// Role based permissions are the simplest to handle so check for them first
d6e8d8
	if ($ug_type == 'user_role' || $ug_type == 'group_role')
d6e8d8
	{
d6e8d8
		if (is_numeric($forum_id))
d6e8d8
		{
d6e8d8
			$sql = 'SELECT role_id
d6e8d8
				FROM ' . ACL_ROLES_TABLE . "
d6e8d8
				WHERE role_name = 'ROLE_" . $db->sql_escape($acl_list) . "'";
d6e8d8
			$result = $db->sql_query_limit($sql, 1);
d6e8d8
			$row = $db->sql_fetchrow($result);
d6e8d8
			$db->sql_freeresult($result);
d6e8d8
d6e8d8
			// If we have no role id there is something wrong here
d6e8d8
			if ($row)
d6e8d8
			{
d6e8d8
				$sql = "INSERT INTO $table ($id_field, forum_id, auth_role_id) VALUES ($ug_id, $forum_id, " . $row['role_id'] . ')';
d6e8d8
				$db->sql_query($sql);
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		return;
d6e8d8
	}
d6e8d8
d6e8d8
	// Build correct parameters
d6e8d8
	$auth = array();
d6e8d8
d6e8d8
	if (!is_array($acl_list))
d6e8d8
	{
d6e8d8
		$auth = array($acl_list => $setting);
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		foreach ($acl_list as $auth_option)
d6e8d8
		{
d6e8d8
			$auth[$auth_option] = $setting;
d6e8d8
		}
d6e8d8
	}
d6e8d8
	unset($acl_list);
d6e8d8
d6e8d8
	if (!is_array($forum_id))
d6e8d8
	{
d6e8d8
		$forum_id = array($forum_id);
d6e8d8
	}
d6e8d8
d6e8d8
	// Set any flags as required
d6e8d8
	foreach ($auth as $auth_option => $acl_setting)
d6e8d8
	{
d6e8d8
		$flag = substr($auth_option, 0, strpos($auth_option, '_') + 1);
d6e8d8
		if (empty($auth[$flag]))
d6e8d8
		{
d6e8d8
			$auth[$flag] = $acl_setting;
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	if (!is_array($acl_option_ids) || empty($acl_option_ids))
d6e8d8
	{
d6e8d8
		$sql = 'SELECT auth_option_id, auth_option
d6e8d8
			FROM ' . ACL_OPTIONS_TABLE;
d6e8d8
		$result = $db->sql_query($sql);
d6e8d8
d6e8d8
		while ($row = $db->sql_fetchrow($result))
d6e8d8
		{
d6e8d8
			$acl_option_ids[$row['auth_option']] = $row['auth_option_id'];
d6e8d8
		}
d6e8d8
		$db->sql_freeresult($result);
d6e8d8
	}
d6e8d8
d6e8d8
	$sql_forum = 'AND ' . $db->sql_in_set('a.forum_id', array_map('intval', $forum_id), false, true);
d6e8d8
d6e8d8
	$sql = ($ug_type == 'user') ? 'SELECT o.auth_option_id, o.auth_option, a.forum_id, a.auth_setting FROM ' . ACL_USERS_TABLE . ' a, ' . ACL_OPTIONS_TABLE . " o WHERE a.auth_option_id = o.auth_option_id $sql_forum AND a.user_id = $ug_id" : 'SELECT o.auth_option_id, o.auth_option, a.forum_id, a.auth_setting FROM ' . ACL_GROUPS_TABLE . ' a, ' . ACL_OPTIONS_TABLE . " o WHERE a.auth_option_id = o.auth_option_id $sql_forum AND a.group_id = $ug_id";
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
d6e8d8
	$cur_auth = array();
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		$cur_auth[$row['forum_id']][$row['auth_option_id']] = $row['auth_setting'];
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	$sql_ary = array();
d6e8d8
	foreach ($forum_id as $forum)
d6e8d8
	{
d6e8d8
		foreach ($auth as $auth_option => $setting)
d6e8d8
		{
d6e8d8
			$auth_option_id = $acl_option_ids[$auth_option];
d6e8d8
d6e8d8
			if (!$auth_option_id)
d6e8d8
			{
d6e8d8
				continue;
d6e8d8
			}
d6e8d8
d6e8d8
			switch ($setting)
d6e8d8
			{
d6e8d8
				case ACL_NO:
d6e8d8
					if (isset($cur_auth[$forum][$auth_option_id]))
d6e8d8
					{
d6e8d8
						$sql_ary['delete'][] = "DELETE FROM $table
d6e8d8
							WHERE forum_id = $forum
d6e8d8
								AND auth_option_id = $auth_option_id
d6e8d8
								AND $id_field = $ug_id";
d6e8d8
					}
d6e8d8
				break;
d6e8d8
d6e8d8
				default:
d6e8d8
					if (!isset($cur_auth[$forum][$auth_option_id]))
d6e8d8
					{
d6e8d8
						$sql_ary['insert'][] = "$ug_id, $forum, $auth_option_id, $setting";
d6e8d8
					}
d6e8d8
					else if ($cur_auth[$forum][$auth_option_id] != $setting)
d6e8d8
					{
d6e8d8
						$sql_ary['update'][] = "UPDATE " . $table . "
d6e8d8
							SET auth_setting = $setting
d6e8d8
							WHERE $id_field = $ug_id
d6e8d8
								AND forum_id = $forum
d6e8d8
								AND auth_option_id = $auth_option_id";
d6e8d8
					}
d6e8d8
			}
d6e8d8
		}
d6e8d8
	}
d6e8d8
	unset($cur_auth);
d6e8d8
d6e8d8
	$sql = '';
d6e8d8
	foreach ($sql_ary as $sql_type => $sql_subary)
d6e8d8
	{
d6e8d8
		switch ($sql_type)
d6e8d8
		{
d6e8d8
			case 'insert':
d6e8d8
				switch ($db->sql_layer)
d6e8d8
				{
d6e8d8
					case 'mysql':
d6e8d8
					case 'mysql4':
d6e8d8
						$sql = 'VALUES ' . implode(', ', preg_replace('#^(.*?)$#', '(\1)', $sql_subary));
d6e8d8
					break;
d6e8d8
d6e8d8
					case 'mssql':
d6e8d8
					case 'sqlite':
d6e8d8
						$sql = implode(' UNION ALL ', preg_replace('#^(.*?)$#', 'SELECT \1', $sql_subary));
d6e8d8
					break;
d6e8d8
d6e8d8
					default:
d6e8d8
						foreach ($sql_subary as $sql)
d6e8d8
						{
d6e8d8
							$sql = "INSERT INTO $table ($id_field, forum_id, auth_option_id, auth_setting) VALUES ($sql)";
d6e8d8
							$db->sql_query($sql);
d6e8d8
							$sql = '';
d6e8d8
						}
d6e8d8
				}
d6e8d8
d6e8d8
				if ($sql != '')
d6e8d8
				{
d6e8d8
					$sql = "INSERT INTO $table ($id_field, forum_id, auth_option_id, auth_setting) $sql";
d6e8d8
					$db->sql_query($sql);
d6e8d8
				}
d6e8d8
			break;
d6e8d8
d6e8d8
			case 'update':
d6e8d8
			case 'delete':
d6e8d8
				foreach ($sql_subary as $sql)
d6e8d8
				{
d6e8d8
					$db->sql_query($sql);
d6e8d8
					$sql = '';
d6e8d8
				}
d6e8d8
			break;
d6e8d8
		}
d6e8d8
		unset($sql_ary[$sql_type]);
d6e8d8
	}
d6e8d8
	unset($sql_ary);
d6e8d8
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Update the count of unread private messages for all users
d6e8d8
*/
d6e8d8
function update_unread_count()
d6e8d8
{
d6e8d8
	global $db;
d6e8d8
d6e8d8
	$sql = 'SELECT user_id, COUNT(msg_id) as num_messages
d6e8d8
		FROM ' . PRIVMSGS_TO_TABLE . '
d6e8d8
		WHERE pm_unread = 1
d6e8d8
			AND folder_id <> ' . PRIVMSGS_OUTBOX . '
d6e8d8
		GROUP BY user_id';
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		$db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_unread_privmsg = ' . $row['num_messages'] . '
d6e8d8
			WHERE user_id = ' . $row['user_id']);
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Add any of the pre-defined "special" groups which are missing from the database
d6e8d8
*/
d6e8d8
function add_default_groups()
d6e8d8
{
d6e8d8
	global $db;
d6e8d8
d6e8d8
	$default_groups = array(
d6e8d8
		'GUESTS'			=> array('', 0, 0),
d6e8d8
		'REGISTERED'		=> array('', 0, 0),
d6e8d8
		'REGISTERED_COPPA'	=> array('', 0, 0),
d6e8d8
		'GLOBAL_MODERATORS'	=> array('00AA00', 1, 0),
d6e8d8
		'ADMINISTRATORS'	=> array('AA0000', 1, 1),
d6e8d8
		'BOTS'				=> array('9E8DA7', 0, 0)
d6e8d8
	);
d6e8d8
d6e8d8
	$sql = 'SELECT *
d6e8d8
		FROM ' . GROUPS_TABLE . '
d6e8d8
		WHERE ' . $db->sql_in_set('group_name', array_keys($default_groups));
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		unset($default_groups[strtoupper($row['group_name'])]);
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	$sql_ary = array();
d6e8d8
d6e8d8
	foreach ($default_groups as $name => $data)
d6e8d8
	{
d6e8d8
		$sql_ary[] = array(
d6e8d8
			'group_name'			=> (string) $name,
d6e8d8
			'group_desc'			=> '',
d6e8d8
			'group_desc_uid'		=> '',
d6e8d8
			'group_desc_bitfield'	=> '',
d6e8d8
			'group_type'			=> GROUP_SPECIAL,
d6e8d8
			'group_colour'			=> (string) $data[0],
d6e8d8
			'group_legend'			=> (int) $data[1],
d6e8d8
			'group_founder_manage'	=> (int) $data[2]
d6e8d8
		);
d6e8d8
	}
d6e8d8
d6e8d8
	if (sizeof($sql_ary))
d6e8d8
	{
d6e8d8
		$db->sql_multi_insert(GROUPS_TABLE, $sql_ary);
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
d6e8d8
/**
d6e8d8
* Sync post count. We might need to do this in batches.
d6e8d8
*/
d6e8d8
function sync_post_count($offset, $limit)
d6e8d8
{
d6e8d8
	global $db;
d6e8d8
	$sql = 'SELECT COUNT(post_id) AS num_posts, poster_id
d6e8d8
			FROM ' . POSTS_TABLE . '
d6e8d8
			WHERE post_postcount = 1
d6e8d8
				AND post_approved = 1
d6e8d8
			GROUP BY poster_id
d6e8d8
			ORDER BY poster_id';
d6e8d8
	$result = $db->sql_query_limit($sql, $limit, $offset);
d6e8d8
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		$db->sql_query('UPDATE ' . USERS_TABLE . " SET user_posts = {$row['num_posts']} WHERE user_id = {$row['poster_id']}");
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Add the search bots into the database
d6e8d8
* This code should be used in execute_last if the source database did not have bots
d6e8d8
* If you are converting bots this function should not be called
d6e8d8
* @todo We might want to look at sharing the bot list between the install code and this code for consistancy
d6e8d8
*/
d6e8d8
function add_bots()
d6e8d8
{
d6e8d8
	global $db, $convert, $user, $config, $phpbb_root_path, $phpEx;
d6e8d8
d6e8d8
	$db->sql_query($convert->truncate_statement . BOTS_TABLE);
d6e8d8
d6e8d8
	$sql = 'SELECT group_id FROM ' . GROUPS_TABLE . " WHERE group_name = 'BOTS'";
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
	$group_id = (int) $db->sql_fetchfield('group_id', false, $result);
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	if (!$group_id)
d6e8d8
	{
d6e8d8
		add_default_groups();
d6e8d8
d6e8d8
		$sql = 'SELECT group_id FROM ' . GROUPS_TABLE . " WHERE group_name = 'BOTS'";
d6e8d8
		$result = $db->sql_query($sql);
d6e8d8
		$group_id = (int) $db->sql_fetchfield('group_id', false, $result);
d6e8d8
		$db->sql_freeresult($result);
d6e8d8
d6e8d8
		if (!$group_id)
d6e8d8
		{
d6e8d8
			global $install;
d6e8d8
			$install->error($user->lang['CONV_ERROR_INCONSISTENT_GROUPS'], __LINE__, __FILE__);
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	$bots = array(
d6e8d8
		'AdsBot [Google]'			=> array('AdsBot-Google', ''),
d6e8d8
		'Alexa [Bot]'				=> array('ia_archiver', ''),
d6e8d8
		'Alta Vista [Bot]'			=> array('Scooter/', ''),
d6e8d8
		'Ask Jeeves [Bot]'			=> array('Ask Jeeves', ''),
d6e8d8
		'Baidu [Spider]'			=> array('Baiduspider+(', ''),
d6e8d8
		'Exabot [Bot]'				=> array('Exabot/', ''),
d6e8d8
		'FAST Enterprise [Crawler]'	=> array('FAST Enterprise Crawler', ''),
d6e8d8
		'FAST WebCrawler [Crawler]'	=> array('FAST-WebCrawler/', ''),
d6e8d8
		'Francis [Bot]'				=> array('http://www.neomo.de/', ''),
d6e8d8
		'Gigabot [Bot]'				=> array('Gigabot/', ''),
d6e8d8
		'Google Adsense [Bot]'		=> array('Mediapartners-Google', ''),
d6e8d8
		'Google Desktop'			=> array('Google Desktop', ''),
d6e8d8
		'Google Feedfetcher'		=> array('Feedfetcher-Google', ''),
d6e8d8
		'Google [Bot]'				=> array('Googlebot', ''),
d6e8d8
		'Heise IT-Markt [Crawler]'	=> array('heise-IT-Markt-Crawler', ''),
d6e8d8
		'Heritrix [Crawler]'		=> array('heritrix/1.', ''),
d6e8d8
		'IBM Research [Bot]'		=> array('ibm.com/cs/crawler', ''),
d6e8d8
		'ICCrawler - ICjobs'		=> array('ICCrawler - ICjobs', ''),
d6e8d8
		'ichiro [Crawler]'			=> array('ichiro/2', ''),
d6e8d8
		'Majestic-12 [Bot]'			=> array('MJ12bot/', ''),
d6e8d8
		'Metager [Bot]'				=> array('MetagerBot/', ''),
d6e8d8
		'MSN NewsBlogs'				=> array('msnbot-NewsBlogs/', ''),
d6e8d8
		'MSN [Bot]'					=> array('msnbot/', ''),
d6e8d8
		'MSNbot Media'				=> array('msnbot-media/', ''),
d6e8d8
		'NG-Search [Bot]'			=> array('NG-Search/', ''),
d6e8d8
		'Nutch [Bot]'				=> array('http://lucene.apache.org/nutch/', ''),
d6e8d8
		'Nutch/CVS [Bot]'			=> array('NutchCVS/', ''),
d6e8d8
		'OmniExplorer [Bot]'		=> array('OmniExplorer_Bot/', ''),
d6e8d8
		'Online link [Validator]'	=> array('online link validator', ''),
d6e8d8
		'psbot [Picsearch]'			=> array('psbot/0', ''),
d6e8d8
		'Seekport [Bot]'			=> array('Seekbot/', ''),
d6e8d8
		'Sensis [Crawler]'			=> array('Sensis Web Crawler', ''),
d6e8d8
		'SEO Crawler'				=> array('SEO search Crawler/', ''),
d6e8d8
		'Seoma [Crawler]'			=> array('Seoma [SEO Crawler]', ''),
d6e8d8
		'SEOSearch [Crawler]'		=> array('SEOsearch/', ''),
d6e8d8
		'Snappy [Bot]'				=> array('Snappy/1.1 ( http://www.urltrends.com/ )', ''),
d6e8d8
		'Steeler [Crawler]'			=> array('http://www.tkl.iis.u-tokyo.ac.jp/~crawler/', ''),
d6e8d8
		'Synoo [Bot]'				=> array('SynooBot/', ''),
d6e8d8
		'Telekom [Bot]'				=> array('crawleradmin.t-info@telekom.de', ''),
d6e8d8
		'TurnitinBot [Bot]'			=> array('TurnitinBot/', ''),
d6e8d8
		'Voyager [Bot]'				=> array('voyager/1.0', ''),
d6e8d8
		'W3 [Sitesearch]'			=> array('W3 SiteSearch Crawler', ''),
d6e8d8
		'W3C [Linkcheck]'			=> array('W3C-checklink/', ''),
d6e8d8
		'W3C [Validator]'			=> array('W3C_*Validator', ''),
d6e8d8
		'WiseNut [Bot]'				=> array('http://www.WISEnutbot.com', ''),
d6e8d8
		'YaCy [Bot]'				=> array('yacybot', ''),
d6e8d8
		'Yahoo MMCrawler [Bot]'		=> array('Yahoo-MMCrawler/', ''),
d6e8d8
		'Yahoo Slurp [Bot]'			=> array('Yahoo! DE Slurp', ''),
d6e8d8
		'Yahoo [Bot]'				=> array('Yahoo! Slurp', ''),
d6e8d8
		'YahooSeeker [Bot]'			=> array('YahooSeeker/', ''),
d6e8d8
	);
d6e8d8
d6e8d8
	if (!function_exists('user_add'))
d6e8d8
	{
d6e8d8
		include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
d6e8d8
	}
d6e8d8
d6e8d8
	foreach ($bots as $bot_name => $bot_ary)
d6e8d8
	{
d6e8d8
		$user_row = array(
d6e8d8
			'user_type'				=> USER_IGNORE,
d6e8d8
			'group_id'				=> $group_id,
d6e8d8
			'username'				=> $bot_name,
d6e8d8
			'user_regdate'			=> time(),
d6e8d8
			'user_password'			=> '',
d6e8d8
			'user_colour'			=> '9E8DA7',
d6e8d8
			'user_email'			=> '',
d6e8d8
			'user_lang'				=> $config['default_lang'],
d6e8d8
			'user_style'			=> 1,
d6e8d8
			'user_timezone'			=> 0,
d6e8d8
			'user_allow_massemail'	=> 0,
d6e8d8
		);
d6e8d8
d6e8d8
		$user_id = user_add($user_row);
d6e8d8
d6e8d8
		if ($user_id)
d6e8d8
		{
d6e8d8
			$sql = 'INSERT INTO ' . BOTS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
d6e8d8
				'bot_active'	=> 1,
d6e8d8
				'bot_name'		=> $bot_name,
d6e8d8
				'user_id'		=> $user_id,
d6e8d8
				'bot_agent'		=> $bot_ary[0],
d6e8d8
				'bot_ip'		=> $bot_ary[1])
d6e8d8
			);
d6e8d8
			$db->sql_query($sql);
d6e8d8
		}
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Update any dynamic configuration variables after the conversion is finished
d6e8d8
* @todo Confirm that this updates all relevant values since it has not necessarily been kept in sync with all changes
d6e8d8
*/
d6e8d8
function update_dynamic_config()
d6e8d8
{
d6e8d8
	global $db, $config;
d6e8d8
d6e8d8
	// Get latest username
d6e8d8
	$sql = 'SELECT user_id, username, user_colour
d6e8d8
		FROM ' . USERS_TABLE . '
d6e8d8
		WHERE user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')';
d6e8d8
d6e8d8
	if (!empty($config['increment_user_id']))
d6e8d8
	{
d6e8d8
		$sql .= ' AND user_id <> ' . $config['increment_user_id'];
d6e8d8
	}
d6e8d8
d6e8d8
	$sql .= ' ORDER BY user_id DESC';
d6e8d8
d6e8d8
	$result = $db->sql_query_limit($sql, 1);
d6e8d8
	$row = $db->sql_fetchrow($result);
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	if ($row)
d6e8d8
	{
d6e8d8
		set_config('newest_user_id', $row['user_id'], true);
d6e8d8
		set_config('newest_username', $row['username'], true);
d6e8d8
		set_config('newest_user_colour', $row['user_colour'], true);
d6e8d8
	}
d6e8d8
d6e8d8
//	Also do not reset record online user/date. There will be old data or the fresh data from the schema.
d6e8d8
//	set_config('record_online_users', 1, true);
d6e8d8
//	set_config('record_online_date', time(), true);
d6e8d8
d6e8d8
	$sql = 'SELECT COUNT(post_id) AS stat
d6e8d8
		FROM ' . POSTS_TABLE . '
d6e8d8
		WHERE post_approved = 1';
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
	$row = $db->sql_fetchrow($result);
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	set_config('num_posts', (int) $row['stat'], true);
d6e8d8
d6e8d8
	$sql = 'SELECT COUNT(topic_id) AS stat
d6e8d8
		FROM ' . TOPICS_TABLE . '
d6e8d8
		WHERE topic_approved = 1';
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
	$row = $db->sql_fetchrow($result);
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	set_config('num_topics', (int) $row['stat'], true);
d6e8d8
d6e8d8
	$sql = 'SELECT COUNT(user_id) AS stat
d6e8d8
		FROM ' . USERS_TABLE . '
d6e8d8
		WHERE user_type IN (' . USER_NORMAL . ',' . USER_FOUNDER . ')';
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
	$row = $db->sql_fetchrow($result);
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	set_config('num_users', (int) $row['stat'], true);
d6e8d8
d6e8d8
	$sql = 'SELECT COUNT(attach_id) as stat
d6e8d8
		FROM ' . ATTACHMENTS_TABLE . '
d6e8d8
		WHERE is_orphan = 0';
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
	set_config('num_files', (int) $db->sql_fetchfield('stat'), true);
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	$sql = 'SELECT SUM(filesize) as stat
d6e8d8
		FROM ' . ATTACHMENTS_TABLE . '
d6e8d8
		WHERE is_orphan = 0';
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
	set_config('upload_dir_size', (float) $db->sql_fetchfield('stat'), true);
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	/**
d6e8d8
	* We do not resync users post counts - this can be done by the admin after conversion if wanted.
d6e8d8
	$sql = 'SELECT COUNT(post_id) AS num_posts, poster_id
d6e8d8
		FROM ' . POSTS_TABLE . '
d6e8d8
		WHERE post_postcount = 1
d6e8d8
		GROUP BY poster_id';
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		$db->sql_query('UPDATE ' . USERS_TABLE . " SET user_posts = {$row['num_posts']} WHERE user_id = {$row['poster_id']}");
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
	*/
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Updates topics_posted entries
d6e8d8
*/
d6e8d8
function update_topics_posted()
d6e8d8
{
d6e8d8
	global $db, $config;
d6e8d8
d6e8d8
	switch ($db->sql_layer)
d6e8d8
	{
d6e8d8
		case 'sqlite':
d6e8d8
		case 'firebird':
d6e8d8
			$db->sql_query('DELETE FROM ' . TOPICS_POSTED_TABLE);
d6e8d8
		break;
d6e8d8
d6e8d8
		default:
d6e8d8
			$db->sql_query('TRUNCATE TABLE ' . TOPICS_POSTED_TABLE);
d6e8d8
		break;
d6e8d8
	}
d6e8d8
d6e8d8
	// This can get really nasty... therefore we only do the last six months
d6e8d8
	$get_from_time = time() - (6 * 4 * 7 * 24 * 60 * 60);
d6e8d8
d6e8d8
	// Select forum ids, do not include categories
d6e8d8
	$sql = 'SELECT forum_id
d6e8d8
		FROM ' . FORUMS_TABLE . '
d6e8d8
		WHERE forum_type <> ' . FORUM_CAT;
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
d6e8d8
	$forum_ids = array();
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		$forum_ids[] = $row['forum_id'];
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	// Any global announcements? ;)
d6e8d8
	$forum_ids[] = 0;
d6e8d8
d6e8d8
	// Now go through the forums and get us some topics...
d6e8d8
	foreach ($forum_ids as $forum_id)
d6e8d8
	{
d6e8d8
		$sql = 'SELECT p.poster_id, p.topic_id
d6e8d8
			FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . ' t
d6e8d8
			WHERE t.forum_id = ' . $forum_id . '
d6e8d8
				AND t.topic_moved_id = 0
d6e8d8
				AND t.topic_last_post_time > ' . $get_from_time . '
d6e8d8
				AND t.topic_id = p.topic_id
d6e8d8
				AND p.poster_id <> ' . ANONYMOUS . '
d6e8d8
			GROUP BY p.poster_id, p.topic_id';
d6e8d8
		$result = $db->sql_query($sql);
d6e8d8
d6e8d8
		$posted = array();
d6e8d8
		while ($row = $db->sql_fetchrow($result))
d6e8d8
		{
d6e8d8
			$posted[$row['poster_id']][] = $row['topic_id'];
d6e8d8
		}
d6e8d8
		$db->sql_freeresult($result);
d6e8d8
d6e8d8
		$sql_ary = array();
d6e8d8
		foreach ($posted as $user_id => $topic_row)
d6e8d8
		{
d6e8d8
			foreach ($topic_row as $topic_id)
d6e8d8
			{
d6e8d8
				$sql_ary[] = array(
d6e8d8
					'user_id'		=> (int) $user_id,
d6e8d8
					'topic_id'		=> (int) $topic_id,
d6e8d8
					'topic_posted'	=> 1,
d6e8d8
				);
d6e8d8
			}
d6e8d8
		}
d6e8d8
		unset($posted);
d6e8d8
d6e8d8
		if (sizeof($sql_ary))
d6e8d8
		{
d6e8d8
			$db->sql_multi_insert(TOPICS_POSTED_TABLE, $sql_ary);
d6e8d8
		}
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Ensure that all users have a default group specified and update related information such as their colour
d6e8d8
*/
d6e8d8
function fix_empty_primary_groups()
d6e8d8
{
d6e8d8
	global $db;
d6e8d8
d6e8d8
	// Set group ids for users not already having it
d6e8d8
	$sql = 'UPDATE ' . USERS_TABLE . ' SET group_id = ' . get_group_id('registered') . '
d6e8d8
		WHERE group_id = 0 AND user_type = ' . USER_INACTIVE;
d6e8d8
	$db->sql_query($sql);
d6e8d8
d6e8d8
	$sql = 'UPDATE ' . USERS_TABLE . ' SET group_id = ' . get_group_id('registered') . '
d6e8d8
		WHERE group_id = 0 AND user_type = ' . USER_NORMAL;
d6e8d8
	$db->sql_query($sql);
d6e8d8
d6e8d8
	$db->sql_query('UPDATE ' . USERS_TABLE . ' SET group_id = ' . get_group_id('guests') . ' WHERE user_id = ' . ANONYMOUS);
d6e8d8
d6e8d8
	$sql = 'SELECT user_id FROM ' . USER_GROUP_TABLE . ' WHERE group_id = ' . get_group_id('administrators');
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
d6e8d8
	$user_ids = array();
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		$user_ids[] = $row['user_id'];
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	if (sizeof($user_ids))
d6e8d8
	{
d6e8d8
		$db->sql_query('UPDATE ' . USERS_TABLE . ' SET group_id = ' . get_group_id('administrators') . '
d6e8d8
			WHERE group_id = 0 AND ' . $db->sql_in_set('user_id', $user_ids));
d6e8d8
	}
d6e8d8
d6e8d8
	$sql = 'SELECT user_id FROM ' . USER_GROUP_TABLE . ' WHERE group_id = ' . get_group_id('global_moderators');
d6e8d8
d6e8d8
	$user_ids = array();
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		$user_ids[] = $row['user_id'];
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	if (sizeof($user_ids))
d6e8d8
	{
d6e8d8
		$db->sql_query('UPDATE ' . USERS_TABLE . ' SET group_id = ' . get_group_id('global_moderators') . '
d6e8d8
			WHERE group_id = 0 AND ' . $db->sql_in_set('user_id', $user_ids));
d6e8d8
	}
d6e8d8
d6e8d8
	// Set user colour
d6e8d8
	$sql = 'SELECT group_id, group_colour FROM ' . GROUPS_TABLE . "
d6e8d8
		WHERE group_colour <> ''";
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		$db->sql_query('UPDATE ' . USERS_TABLE . " SET user_colour = '{$row['group_colour']}' WHERE group_id = {$row['group_id']}");
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Cleanly remove invalid user entries after converting the users table...
d6e8d8
*/
d6e8d8
function remove_invalid_users()
d6e8d8
{
d6e8d8
	global $convert, $db, $phpEx, $phpbb_root_path;
d6e8d8
d6e8d8
	// username_clean is UNIQUE
d6e8d8
	$sql = 'SELECT user_id
d6e8d8
		FROM ' . USERS_TABLE . "
d6e8d8
		WHERE username_clean = ''";
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
	$row = $db->sql_fetchrow($result);
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	if ($row)
d6e8d8
	{
d6e8d8
		if (!function_exists('user_delete'))
d6e8d8
		{
d6e8d8
			include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
d6e8d8
		}
d6e8d8
d6e8d8
		user_delete('remove', $row['user_id']);
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
function convert_bbcode($message, $convert_size = true, $extended_bbcodes = false)
d6e8d8
{
d6e8d8
	static $orig, $repl, $origx, $replx, $str_from, $str_to;
d6e8d8
d6e8d8
	if (empty($orig))
d6e8d8
	{
d6e8d8
		$orig = $repl = array();
d6e8d8
d6e8d8
		$orig[] = '#\[(php|sql)\](.*?)\[/(php|sql)\]#is';
d6e8d8
		$repl[] = '[code]\2[/code]';
d6e8d8
d6e8d8
		$orig[] = '#\[font=[^\]]+\](.*?)\[/font\]#is';
d6e8d8
		$repl[] = '\1';
d6e8d8
d6e8d8
		$orig[] = '#\[align=[a-z]+\](.*?)\[/align\]#is';
d6e8d8
		$repl[] = '\1';
d6e8d8
d6e8d8
		$orig[] = '#\[/list=.*?\]#is';
d6e8d8
		$repl[] = '[/list]';
d6e8d8
d6e8d8
		$origx = array(
d6e8d8
			'#\[glow[^\]]+\](.*?)\[/glow\]#is',
d6e8d8
			'#\[shadow[^\]]+\](.*?)\[/shadow\]#is',
d6e8d8
			'#\[flash[^\]]+\](.*?)\[/flash\]#is'
d6e8d8
		);
d6e8d8
d6e8d8
		$replx = array(
d6e8d8
			'\1',
d6e8d8
			'\1',
d6e8d8
			'[url=\1]Flash[/url]'
d6e8d8
		);
d6e8d8
d6e8d8
		$str_from = array(
d6e8d8
			'[ftp]',	'[/ftp]',
d6e8d8
			'[ftp=',	'[/ftp]',
d6e8d8
			'[pre]',	'[/pre]',
d6e8d8
			'[table]',	'[/table]',
d6e8d8
			'[td]',		'[/td]',
d6e8d8
			'[tr]',		'[/tr]',
d6e8d8
			'[s]',		'[/s]',
d6e8d8
			'[left]',	'[/left]',
d6e8d8
			'[right]',	'[/right]',
d6e8d8
			'[center]',	'[/center]',
d6e8d8
			'[sub]',	'[/sub]',
d6e8d8
			'[sup]',	'[/sup]',
d6e8d8
			'[tt]',		'[/tt]',
d6e8d8
			'[move]',	'[/move]',
d6e8d8
			'[hr]'
d6e8d8
		);
d6e8d8
d6e8d8
		$str_to = array(
d6e8d8
			'[url]',	'[/url]',
d6e8d8
			'[url=',	'[/url]',
d6e8d8
			'[code]',	'[/code]',
d6e8d8
			"\n",		'',
d6e8d8
			'',			'',
d6e8d8
			"\n",		'',
d6e8d8
			'',			'',
d6e8d8
			'',			'',
d6e8d8
			'',			'',
d6e8d8
			'',			'',
d6e8d8
			'',			'',
d6e8d8
			'',			'',
d6e8d8
			'',			'',
d6e8d8
			'',			'',
d6e8d8
			"\n\n"
d6e8d8
		);
d6e8d8
d6e8d8
		for ($i = 0; $i < sizeof($str_from); ++$i)
d6e8d8
		{
d6e8d8
			$origx[] = '#\\' . str_replace(']', '\\]', $str_from[$i]) . '#is';
d6e8d8
			$replx[] = $str_to[$i];
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	if (preg_match_all('#\[email=([^\]]+)\](.*?)\[/email\]#i', $message, $m))
d6e8d8
	{
d6e8d8
		for ($i = 0; $i < sizeof($m[1]); ++$i)
d6e8d8
		{
d6e8d8
			if ($m[1][$i] == $m[2][$i])
d6e8d8
			{
d6e8d8
				$message = str_replace($m[0][$i], '[email]' . $m[1][$i] . '[/email]', $message);
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				$message = str_replace($m[0][$i], $m[2][$i] . ' ([email]' . $m[1][$i] . '[/email])', $message);
d6e8d8
			}
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	if ($convert_size && preg_match('#\[size=[0-9]+\].*?\[/size\]#i', $message))
d6e8d8
	{
d6e8d8
		$size = array(9, 9, 12, 15, 18, 24, 29, 29, 29, 29);
d6e8d8
		$message = preg_replace('#\[size=([0-9]+)\](.*?)\[/size\]#i', '[size=\1]\2[/size]', $message);
d6e8d8
		$message = preg_replace('#\[size=[0-9]{2,}\](.*?)\[/size\]#i', '[size=29]\1[/size]', $message);
d6e8d8
d6e8d8
		for ($i = sizeof($size); $i; )
d6e8d8
		{
d6e8d8
			$i--;
d6e8d8
			$message = str_replace('[size=' . $i . ']', '[size=' . $size[$i] . ']', $message);
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	if ($extended_bbcodes)
d6e8d8
	{
d6e8d8
		$message = preg_replace($origx, $replx, $message);
d6e8d8
	}
d6e8d8
d6e8d8
	$message = preg_replace($orig, $repl, $message);
d6e8d8
	return $message;
d6e8d8
}
d6e8d8
d6e8d8
d6e8d8
function copy_file($src, $trg, $overwrite = false, $die_on_failure = true, $source_relative_path = true)
d6e8d8
{
d6e8d8
	global $convert, $phpbb_root_path, $config, $user, $db;
d6e8d8
d6e8d8
	if (substr($trg, -1) == '/')
d6e8d8
	{
d6e8d8
		$trg .= basename($src);
d6e8d8
	}
d6e8d8
	$src_path = relative_base($src, $source_relative_path, __LINE__, __FILE__);
d6e8d8
	$trg_path = $trg;
d6e8d8
d6e8d8
	if (!$overwrite && @file_exists($trg_path))
d6e8d8
	{
d6e8d8
		return true;
d6e8d8
	}
d6e8d8
d6e8d8
	if (!@file_exists($src_path))
d6e8d8
	{
d6e8d8
		return;
d6e8d8
	}
d6e8d8
d6e8d8
	$path = $phpbb_root_path;
d6e8d8
	$parts = explode('/', $trg);
d6e8d8
	unset($parts[sizeof($parts) - 1]);
d6e8d8
d6e8d8
	for ($i = 0; $i < sizeof($parts); ++$i)
d6e8d8
	{
d6e8d8
		$path .= $parts[$i] . '/';
d6e8d8
d6e8d8
		if (!is_dir($path))
d6e8d8
		{
d6e8d8
			@mkdir($path, 0777);
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	if (!is_writable($path))
d6e8d8
	{
d6e8d8
		@chmod($path, 0777);
d6e8d8
	}
d6e8d8
d6e8d8
	if (!@copy($src_path, $phpbb_root_path . $trg_path))
d6e8d8
	{
d6e8d8
		$convert->p_master->error(sprintf($user->lang['COULD_NOT_COPY'], $src_path, $phpbb_root_path . $trg_path), __LINE__, __FILE__, !$die_on_failure);
d6e8d8
		return;
d6e8d8
	}
d6e8d8
d6e8d8
	if ($perm = @fileperms($src_path))
d6e8d8
	{
d6e8d8
		@chmod($phpbb_root_path . $trg_path, $perm);
d6e8d8
	}
d6e8d8
d6e8d8
	return true;
d6e8d8
}
d6e8d8
d6e8d8
function copy_dir($src, $trg, $copy_subdirs = true, $overwrite = false, $die_on_failure = true, $source_relative_path = true)
d6e8d8
{
d6e8d8
	global $convert, $phpbb_root_path, $config, $user, $db;
d6e8d8
d6e8d8
	$dirlist = $filelist = $bad_dirs = array();
d6e8d8
	$src = path($src, $source_relative_path);
d6e8d8
	$trg = path($trg);
d6e8d8
	$src_path = relative_base($src, $source_relative_path, __LINE__, __FILE__);
d6e8d8
	$trg_path = $phpbb_root_path . $trg;
d6e8d8
d6e8d8
	if (!is_dir($trg_path))
d6e8d8
	{
d6e8d8
		@mkdir($trg_path, 0777);
d6e8d8
		@chmod($trg_path, 0777);
d6e8d8
	}
d6e8d8
d6e8d8
	if (!@is_writable($trg_path))
d6e8d8
	{
d6e8d8
		$bad_dirs[] = path($config['script_path']) . $trg;
d6e8d8
	}
d6e8d8
d6e8d8
	if ($handle = @opendir($src_path))
d6e8d8
	{
d6e8d8
		while ($entry = readdir($handle))
d6e8d8
		{
d6e8d8
			if ($entry[0] == '.' || $entry == 'CVS' || $entry == 'index.htm')
d6e8d8
			{
d6e8d8
				continue;
d6e8d8
			}
d6e8d8
d6e8d8
			if (is_dir($src_path . $entry))
d6e8d8
			{
d6e8d8
				$dirlist[] = $entry;
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				$filelist[] = $entry;
d6e8d8
			}
d6e8d8
		}
d6e8d8
		closedir($handle);
d6e8d8
	}
d6e8d8
	else if ($dir = @dir($src_path))
d6e8d8
	{
d6e8d8
		while ($entry = $dir->read())
d6e8d8
		{
d6e8d8
			if ($entry[0] == '.' || $entry == 'CVS' || $entry == 'index.htm')
d6e8d8
			{
d6e8d8
				continue;
d6e8d8
			}
d6e8d8
d6e8d8
			if (is_dir($src_path . $entry))
d6e8d8
			{
d6e8d8
				$dirlist[] = $entry;
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				$filelist[] = $entry;
d6e8d8
			}
d6e8d8
		}
d6e8d8
		$dir->close();
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_COULD_NOT_READ'], relative_base($src, $source_relative_path)), __LINE__, __FILE__);
d6e8d8
	}
d6e8d8
d6e8d8
	if ($copy_subdirs)
d6e8d8
	{
d6e8d8
		for ($i = 0; $i < sizeof($dirlist); ++$i)
d6e8d8
		{
d6e8d8
			$dir = $dirlist[$i];
d6e8d8
d6e8d8
			if ($dir == 'CVS')
d6e8d8
			{
d6e8d8
				continue;
d6e8d8
			}
d6e8d8
d6e8d8
			if (!is_dir($trg_path . $dir))
d6e8d8
			{
d6e8d8
				@mkdir($trg_path . $dir, 0777);
d6e8d8
				@chmod($trg_path . $dir, 0777);
d6e8d8
			}
d6e8d8
d6e8d8
			if (!@is_writable($trg_path . $dir))
d6e8d8
			{
d6e8d8
				$bad_dirs[] = $trg . $dir;
d6e8d8
				$bad_dirs[] = $trg_path . $dir;
d6e8d8
			}
d6e8d8
d6e8d8
			if (!sizeof($bad_dirs))
d6e8d8
			{
d6e8d8
				copy_dir($src . $dir, $trg . $dir, true, $overwrite, $die_on_failure, $source_relative_path);
d6e8d8
			}
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	if (sizeof($bad_dirs))
d6e8d8
	{
d6e8d8
		$str = (sizeof($bad_dirs) == 1) ? $user->lang['MAKE_FOLDER_WRITABLE'] : $user->lang['MAKE_FOLDERS_WRITABLE'];
d6e8d8
		sort($bad_dirs);
d6e8d8
		$convert->p_master->error(sprintf($str, implode('
', $bad_dirs)), __LINE__, __FILE__);
d6e8d8
	}
d6e8d8
d6e8d8
	for ($i = 0; $i < sizeof($filelist); ++$i)
d6e8d8
	{
d6e8d8
		copy_file($src . $filelist[$i], $trg . $filelist[$i], $overwrite, $die_on_failure, $source_relative_path);
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
function relative_base($path, $is_relative = true, $line = false, $file = false)
d6e8d8
{
d6e8d8
	global $convert, $phpbb_root_path, $config, $user, $db;
d6e8d8
d6e8d8
	if (!$is_relative)
d6e8d8
	{
d6e8d8
		return $path;
d6e8d8
	}
d6e8d8
d6e8d8
	if (empty($convert->options['forum_path']) && $is_relative)
d6e8d8
	{
d6e8d8
		$line = $line ? $line : __LINE__;
d6e8d8
		$file = $file ? $file : __FILE__;
d6e8d8
d6e8d8
		$convert->p_master->error($user->lang['CONV_ERROR_NO_FORUM_PATH'], $line, $file);
d6e8d8
	}
d6e8d8
d6e8d8
	return $convert->options['forum_path'] . '/' . $path;
d6e8d8
}
d6e8d8
d6e8d8
function get_smiley_display()
d6e8d8
{
d6e8d8
	static $smiley_count = 0;
d6e8d8
	$smiley_count++;
d6e8d8
	return ($smiley_count < 50) ? 1 : 0;
d6e8d8
}
d6e8d8
d6e8d8
d6e8d8
function fill_dateformat($user_dateformat)
d6e8d8
{
d6e8d8
	global $config;
d6e8d8
d6e8d8
	return ((empty($user_dateformat)) ? $config['default_dateformat'] : $user_dateformat);
d6e8d8
}
d6e8d8
d6e8d8
d6e8d8
d6e8d8
?>