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

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