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

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