includes/functions_convert.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/functions_convert.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,2475 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package install
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2006 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* Default avatar width/height
    1.24 +* @ignore
    1.25 +*/
    1.26 +define('DEFAULT_AVATAR_X', 80);
    1.27 +define('DEFAULT_AVATAR_Y', 80);
    1.28 +
    1.29 +// Global functions - all functions can be used by convertors
    1.30 +
    1.31 +// SIMPLE FUNCTIONS
    1.32 +
    1.33 +/**
    1.34 +* Return the preceding value
    1.35 +*/
    1.36 +function dec($var)
    1.37 +{
    1.38 +	return --$var;
    1.39 +}
    1.40 +
    1.41 +/**
    1.42 +* Return the next value
    1.43 +*/
    1.44 +function inc($var)
    1.45 +{
    1.46 +	return ++$var;
    1.47 +}
    1.48 +
    1.49 +/**
    1.50 +* Return whether the value is positive
    1.51 +*/
    1.52 +function is_positive($n)
    1.53 +{
    1.54 +	return ($n > 0) ? 1 : 0;
    1.55 +}
    1.56 +
    1.57 +/**
    1.58 +* Boolean inverse of the value
    1.59 +*/
    1.60 +function not($var)
    1.61 +{
    1.62 +	return ($var) ? 0 : 1;
    1.63 +}
    1.64 +
    1.65 +/**
    1.66 +* Convert a textual value to it's equivalent boolean value
    1.67 +*
    1.68 +* @param string $str String to convert (converts yes, on, y, 1 and true to boolean true)
    1.69 +* @return boolean The equivalent value
    1.70 +*/
    1.71 +function str_to_bool($str)
    1.72 +{
    1.73 +	$str = strtolower($str);
    1.74 +	return ($str == 'yes' || $str == 'on' || $str == 'y' || $str == 'true' || $str == '1') ? true : false;
    1.75 +}
    1.76 +
    1.77 +/**
    1.78 +* Function to mimic php's empty() function (it is the same)
    1.79 +*/
    1.80 +function is_empty($mixed)
    1.81 +{
    1.82 +	return empty($mixed);
    1.83 +}
    1.84 +
    1.85 +/**
    1.86 +* Convert the name of a user's primary group to the appropriate equivalent phpBB group id
    1.87 +*
    1.88 +* @param string $status The name of the group
    1.89 +* @return int The group_id corresponding to the equivalent group
    1.90 +*/
    1.91 +function str_to_primary_group($status)
    1.92 +{
    1.93 +	switch (ucfirst(strtolower($status)))
    1.94 +	{
    1.95 +		case 'Administrator':
    1.96 +			return get_group_id('administrators');
    1.97 +		break;
    1.98 +
    1.99 +		case 'Super moderator':
   1.100 +		case 'Global moderator':
   1.101 +		case 'Moderator':
   1.102 +			return get_group_id('global_moderators');
   1.103 +		break;
   1.104 +
   1.105 +		case 'Guest':
   1.106 +		case 'Anonymous':
   1.107 +			return get_group_id('guests');
   1.108 +		break;
   1.109 +
   1.110 +		default:
   1.111 +			return get_group_id('registered');
   1.112 +		break;
   1.113 +	}
   1.114 +}
   1.115 +
   1.116 +/**
   1.117 +* Convert a boolean into the appropriate phpBB constant indicating whether the item is locked
   1.118 +*/
   1.119 +function is_item_locked($bool)
   1.120 +{
   1.121 +	return ($bool) ? ITEM_LOCKED : ITEM_UNLOCKED;
   1.122 +}
   1.123 +
   1.124 +/**
   1.125 +* Convert a value from days to seconds
   1.126 +*/
   1.127 +function days_to_seconds($days)
   1.128 +{
   1.129 +	return ($days * 86400);
   1.130 +}
   1.131 +
   1.132 +/**
   1.133 +* Determine whether a user is anonymous and return the appropriate new user_id
   1.134 +*/
   1.135 +function is_user_anonymous($user_id)
   1.136 +{
   1.137 +	return ($user_id > ANONYMOUS) ? $user_id : ANONYMOUS;
   1.138 +}
   1.139 +
   1.140 +/**
   1.141 +* Generate a key value based on existing values
   1.142 +*
   1.143 +* @param int $pad Amount to add to the maximum value
   1.144 +* @return int Key value
   1.145 +*/
   1.146 +function auto_id($pad = 0)
   1.147 +{
   1.148 +	global $auto_id, $convert_row;
   1.149 +
   1.150 +	if (!empty($convert_row['max_id']))
   1.151 +	{
   1.152 +		return $convert_row['max_id'] + $pad;
   1.153 +	}
   1.154 +
   1.155 +	return $auto_id + $pad;
   1.156 +}
   1.157 +
   1.158 +/**
   1.159 +* Convert a boolean into the appropriate phpBB constant indicating whether the user is active
   1.160 +*/
   1.161 +function set_user_type($user_active)
   1.162 +{
   1.163 +	return ($user_active) ? USER_NORMAL : USER_INACTIVE;
   1.164 +}
   1.165 +
   1.166 +/**
   1.167 +* Convert a value from minutes to hours
   1.168 +*/
   1.169 +function minutes_to_hours($minutes)
   1.170 +{
   1.171 +	return ($minutes / 3600);
   1.172 +}
   1.173 +
   1.174 +/**
   1.175 +* Return the group_id for a given group name
   1.176 +*/
   1.177 +function get_group_id($group_name)
   1.178 +{
   1.179 +	global $db, $group_mapping;
   1.180 +
   1.181 +	if (empty($group_mapping))
   1.182 +	{
   1.183 +		$sql = 'SELECT group_name, group_id
   1.184 +			FROM ' . GROUPS_TABLE;
   1.185 +		$result = $db->sql_query($sql);
   1.186 +
   1.187 +		$group_mapping = array();
   1.188 +		while ($row = $db->sql_fetchrow($result))
   1.189 +		{
   1.190 +			$group_mapping[strtoupper($row['group_name'])] = (int) $row['group_id'];
   1.191 +		}
   1.192 +		$db->sql_freeresult($result);
   1.193 +	}
   1.194 +
   1.195 +	if (!sizeof($group_mapping))
   1.196 +	{
   1.197 +		add_default_groups();
   1.198 +		return get_group_id($group_name);
   1.199 +	}
   1.200 +
   1.201 +	if (isset($group_mapping[strtoupper($group_name)]))
   1.202 +	{
   1.203 +		return $group_mapping[strtoupper($group_name)];
   1.204 +	}
   1.205 +
   1.206 +	return $group_mapping['REGISTERED'];
   1.207 +}
   1.208 +
   1.209 +/**
   1.210 +* Generate the email hash stored in the users table
   1.211 +*
   1.212 +* Note: Deprecated, calls should directly go to phpbb_email_hash()
   1.213 +*/
   1.214 +function gen_email_hash($email)
   1.215 +{
   1.216 +	return phpbb_email_hash($email);
   1.217 +}
   1.218 +
   1.219 +/**
   1.220 +* Convert a boolean into the appropriate phpBB constant indicating whether the topic is locked
   1.221 +*/
   1.222 +function is_topic_locked($bool)
   1.223 +{
   1.224 +	return (!empty($bool)) ? ITEM_LOCKED : ITEM_UNLOCKED;
   1.225 +}
   1.226 +
   1.227 +/**
   1.228 +* Generate a bbcode_uid value
   1.229 +*/
   1.230 +function make_uid($timestamp)
   1.231 +{
   1.232 +	static $last_timestamp, $last_uid;
   1.233 +
   1.234 +	if (empty($last_timestamp) || $timestamp != $last_timestamp)
   1.235 +	{
   1.236 +		$last_uid = substr(base_convert(unique_id(), 16, 36), 0, BBCODE_UID_LEN);
   1.237 +	}
   1.238 +	$last_timestamp = $timestamp;
   1.239 +	return $last_uid;
   1.240 +}
   1.241 +
   1.242 +
   1.243 +/**
   1.244 +* Validate a website address
   1.245 +*/
   1.246 +function validate_website($url)
   1.247 +{
   1.248 +	if ($url === 'http://')
   1.249 +	{
   1.250 +		return '';
   1.251 +	}
   1.252 +	else if (!preg_match('#^[a-z0-9]+://#i', $url) && strlen($url) > 0)
   1.253 +	{
   1.254 +		return 'http://' . $url;
   1.255 +	}
   1.256 +	return $url;
   1.257 +}
   1.258 +
   1.259 +/**
   1.260 +* Convert nulls to zeros for fields which allowed a NULL value in the source but not the destination
   1.261 +*/
   1.262 +function null_to_zero($value)
   1.263 +{
   1.264 +	return ($value === NULL) ? 0 : $value;
   1.265 +}
   1.266 +
   1.267 +/**
   1.268 +* Convert nulls to empty strings for fields which allowed a NULL value in the source but not the destination
   1.269 +*/
   1.270 +function null_to_str($value)
   1.271 +{
   1.272 +	return ($value === NULL) ? '' : $value;
   1.273 +}
   1.274 +
   1.275 +// EXTENDED FUNCTIONS
   1.276 +
   1.277 +/**
   1.278 +* Get old config value
   1.279 +*/
   1.280 +function get_config_value($config_name)
   1.281 +{
   1.282 +	static $convert_config;
   1.283 +
   1.284 +	if (!isset($convert_config))
   1.285 +	{
   1.286 +		$convert_config = get_config();
   1.287 +	}
   1.288 +
   1.289 +	if (!isset($convert_config[$config_name]))
   1.290 +	{
   1.291 +		return false;
   1.292 +	}
   1.293 +
   1.294 +	return (empty($convert_config[$config_name])) ? '' : $convert_config[$config_name];
   1.295 +}
   1.296 +
   1.297 +/**
   1.298 +* Convert an IP address from the hexadecimal notation to normal dotted-quad notation
   1.299 +*/
   1.300 +function decode_ip($int_ip)
   1.301 +{
   1.302 +	if (!$int_ip)
   1.303 +	{
   1.304 +		return $int_ip;
   1.305 +	}
   1.306 +
   1.307 +	$hexipbang = explode('.', chunk_split($int_ip, 2, '.'));
   1.308 +
   1.309 +	// 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...
   1.310 +	if (sizeof($hexipbang) < 4)
   1.311 +	{
   1.312 +		return $int_ip;
   1.313 +	}
   1.314 +
   1.315 +	return hexdec($hexipbang[0]) . '.' . hexdec($hexipbang[1]) . '.' . hexdec($hexipbang[2]) . '.' . hexdec($hexipbang[3]);
   1.316 +}
   1.317 +
   1.318 +/**
   1.319 +* Reverse the encoding of wild-carded bans
   1.320 +*/
   1.321 +function decode_ban_ip($int_ip)
   1.322 +{
   1.323 +	return str_replace('255', '*', decode_ip($int_ip));
   1.324 +}
   1.325 +
   1.326 +/**
   1.327 +* Determine the MIME-type of a specified filename
   1.328 +* This does not actually inspect the file, but simply uses the file extension
   1.329 +*/
   1.330 +function mimetype($filename)
   1.331 +{
   1.332 +	if (!preg_match('/\.([a-z0-9]+)$/i', $filename, $m))
   1.333 +	{
   1.334 +		return 'application/octet-stream';
   1.335 +	}
   1.336 +
   1.337 +	switch (strtolower($m[1]))
   1.338 +	{
   1.339 +		case 'zip':		return 'application/zip';
   1.340 +		case 'jpeg':	return 'image/jpeg';
   1.341 +		case 'jpg':		return 'image/jpeg';
   1.342 +		case 'jpe':		return 'image/jpeg';
   1.343 +		case 'png':		return 'image/png';
   1.344 +		case 'gif':		return 'image/gif';
   1.345 +		case 'htm':
   1.346 +		case 'html':	return 'text/html';
   1.347 +		case 'tif':		return 'image/tiff';
   1.348 +		case 'tiff':	return 'image/tiff';
   1.349 +		case 'ras':		return 'image/x-cmu-raster';
   1.350 +		case 'pnm':		return 'image/x-portable-anymap';
   1.351 +		case 'pbm':		return 'image/x-portable-bitmap';
   1.352 +		case 'pgm':		return 'image/x-portable-graymap';
   1.353 +		case 'ppm':		return 'image/x-portable-pixmap';
   1.354 +		case 'rgb':		return 'image/x-rgb';
   1.355 +		case 'xbm':		return 'image/x-xbitmap';
   1.356 +		case 'xpm':		return 'image/x-xpixmap';
   1.357 +		case 'xwd':		return 'image/x-xwindowdump';
   1.358 +		case 'z':		return 'application/x-compress';
   1.359 +		case 'gtar':	return 'application/x-gtar';
   1.360 +		case 'tgz':		return 'application/x-gtar';
   1.361 +		case 'gz':		return 'application/x-gzip';
   1.362 +		case 'tar':		return 'application/x-tar';
   1.363 +		case 'xls':		return 'application/excel';
   1.364 +		case 'pdf':		return 'application/pdf';
   1.365 +		case 'ppt':		return 'application/powerpoint';
   1.366 +		case 'rm':		return 'application/vnd.rn-realmedia';
   1.367 +		case 'wma':		return 'audio/x-ms-wma';
   1.368 +		case 'swf':		return 'application/x-shockwave-flash';
   1.369 +		case 'ief':		return 'image/ief';
   1.370 +		case 'doc':
   1.371 +		case 'dot':
   1.372 +		case 'wrd':		return 'application/msword';
   1.373 +		case 'ai':
   1.374 +		case 'eps':
   1.375 +		case 'ps':		return 'application/postscript';
   1.376 +		case 'asc':
   1.377 +		case 'txt':
   1.378 +		case 'c':
   1.379 +		case 'cc':
   1.380 +		case 'h':
   1.381 +		case 'hh':
   1.382 +		case 'cpp':
   1.383 +		case 'hpp':
   1.384 +		case 'php':
   1.385 +		case 'php3':	return 'text/plain';
   1.386 +		default: 		return 'application/octet-stream';
   1.387 +	}
   1.388 +}
   1.389 +
   1.390 +/**
   1.391 +* Obtain the dimensions of all remotely hosted avatars
   1.392 +* This should only be called from execute_last
   1.393 +* There can be significant network overhead if there are a large number of remote avatars
   1.394 +* @todo Look at the option of allowing the user to decide whether this is called or to force the dimensions
   1.395 +*/
   1.396 +function remote_avatar_dims()
   1.397 +{
   1.398 +	global $db;
   1.399 +
   1.400 +	$sql = 'SELECT user_id, user_avatar
   1.401 +		FROM ' . USERS_TABLE . '
   1.402 +		WHERE user_avatar_type = ' . AVATAR_REMOTE;
   1.403 +	$result = $db->sql_query($sql);
   1.404 +
   1.405 +	$remote_avatars = array();
   1.406 +	while ($row = $db->sql_fetchrow($result))
   1.407 +	{
   1.408 +		$remote_avatars[(int) $row['user_id']] = $row['user_avatar'];
   1.409 +	}
   1.410 +	$db->sql_freeresult($result);
   1.411 +
   1.412 +	foreach ($remote_avatars as $user_id => $avatar)
   1.413 +	{
   1.414 +		$width = (int) get_remote_avatar_dim($avatar, 0);
   1.415 +		$height = (int) get_remote_avatar_dim($avatar, 1);
   1.416 +
   1.417 +		$sql = 'UPDATE ' . USERS_TABLE . '
   1.418 +			SET user_avatar_width = ' . (int) $width . ', user_avatar_height = ' . (int) $height . '
   1.419 +			WHERE user_id = ' . $user_id;
   1.420 +		$db->sql_query($sql);
   1.421 +	}
   1.422 +}
   1.423 +
   1.424 +function import_avatar_gallery($gallery_name = '', $subdirs_as_galleries = false)
   1.425 +{
   1.426 +	global $config, $convert, $phpbb_root_path, $user;
   1.427 +
   1.428 +	$relative_path = empty($convert->convertor['source_path_absolute']);
   1.429 +
   1.430 +	if (empty($convert->convertor['avatar_gallery_path']))
   1.431 +	{
   1.432 +		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_GALLERY_PATH'], 'import_avatar_gallery()'), __LINE__, __FILE__);
   1.433 +	}
   1.434 +
   1.435 +	$src_path = relative_base(path($convert->convertor['avatar_gallery_path'], $relative_path), $relative_path);
   1.436 +
   1.437 +	if (is_dir($src_path))
   1.438 +	{
   1.439 +		// Do not die on failure... safe mode restrictions may be in effect.
   1.440 +		copy_dir($convert->convertor['avatar_gallery_path'], path($config['avatar_gallery_path']) . $gallery_name, !$subdirs_as_galleries, false, false, $relative_path);
   1.441 +
   1.442 +		// only doing 1 level deep. (ibf 1.x)
   1.443 +		// notes: ibf has 2 tiers: directly in the avatar directory for base gallery (handled in the above statement), plus subdirs(handled below).
   1.444 +		// 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.
   1.445 +		if ($subdirs_as_galleries)
   1.446 +		{
   1.447 +			$dirlist = array();
   1.448 +			if ($handle = @opendir($src_path))
   1.449 +			{
   1.450 +				while ($entry = readdir($handle))
   1.451 +				{
   1.452 +					if ($entry[0] == '.' || $entry == 'CVS' || $entry == 'index.htm')
   1.453 +					{
   1.454 +						continue;
   1.455 +					}
   1.456 +
   1.457 +					if (is_dir($src_path . $entry))
   1.458 +					{
   1.459 +						$dirlist[] = $entry;
   1.460 +					}
   1.461 +				}
   1.462 +				closedir($handle);
   1.463 +			}
   1.464 +			else if ($dir = @dir($src_path))
   1.465 +			{
   1.466 +				while ($entry = $dir->read())
   1.467 +				{
   1.468 +					if ($entry[0] == '.' || $entry == 'CVS' || $entry == 'index.htm')
   1.469 +					{
   1.470 +						continue;
   1.471 +					}
   1.472 +
   1.473 +					if (is_dir($src_path . $entry))
   1.474 +					{
   1.475 +						$dirlist[] = $entry;
   1.476 +					}
   1.477 +				}
   1.478 +				$dir->close();
   1.479 +			}
   1.480 +
   1.481 +			for ($i = 0; $i < sizeof($dirlist); ++$i)
   1.482 +			{
   1.483 +				$dir = $dirlist[$i];
   1.484 +
   1.485 +				// Do not die on failure... safe mode restrictions may be in effect.
   1.486 +				copy_dir(path($convert->convertor['avatar_gallery_path'], $relative_path) . $dir, path($config['avatar_gallery_path']) . $dir, true, false, false, $relative_path);
   1.487 +			}
   1.488 +		}
   1.489 +	}
   1.490 +}
   1.491 +
   1.492 +function import_attachment_files($category_name = '')
   1.493 +{
   1.494 +	global $config, $convert, $phpbb_root_path, $db, $user;
   1.495 +
   1.496 +	$sql = 'SELECT config_value AS upload_path
   1.497 +		FROM ' . CONFIG_TABLE . "
   1.498 +		WHERE config_name = 'upload_path'";
   1.499 +	$result = $db->sql_query($sql);
   1.500 +	$config['upload_path'] = $db->sql_fetchfield('upload_path');
   1.501 +	$db->sql_freeresult($result);
   1.502 +
   1.503 +	$relative_path = empty($convert->convertor['source_path_absolute']);
   1.504 +
   1.505 +	if (empty($convert->convertor['upload_path']))
   1.506 +	{
   1.507 +		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_UPLOAD_DIR'], 'import_attachment_files()'), __LINE__, __FILE__);
   1.508 +	}
   1.509 +
   1.510 +	if (is_dir(relative_base(path($convert->convertor['upload_path'], $relative_path), $relative_path)))
   1.511 +	{
   1.512 +		copy_dir($convert->convertor['upload_path'], path($config['upload_path']) . $category_name, true, false, true, $relative_path);
   1.513 +	}
   1.514 +}
   1.515 +
   1.516 +function attachment_forum_perms($forum_id)
   1.517 +{
   1.518 +	if (!is_array($forum_id))
   1.519 +	{
   1.520 +		$forum_id = array($forum_id);
   1.521 +	}
   1.522 +
   1.523 +	return serialize($forum_id);
   1.524 +}
   1.525 +
   1.526 +// base64todec function
   1.527 +// -> from php manual?
   1.528 +function base64_unpack($string)
   1.529 +{
   1.530 +	$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-';
   1.531 +	$base = strlen($chars);
   1.532 +
   1.533 +	$length = strlen($string);
   1.534 +	$number = 0;
   1.535 +
   1.536 +	for ($i = 1; $i <= $length; $i++)
   1.537 +	{
   1.538 +		$pos = $length - $i;
   1.539 +		$operand = strpos($chars, substr($string, $pos, 1));
   1.540 +		$exponent = pow($base, $i-1);
   1.541 +		$dec_value = $operand * $exponent;
   1.542 +		$number += $dec_value;
   1.543 +	}
   1.544 +
   1.545 +	return $number;
   1.546 +}
   1.547 +
   1.548 +function _import_check($config_var, $source, $use_target)
   1.549 +{
   1.550 +	global $convert, $config;
   1.551 +
   1.552 +	$result = array(
   1.553 +		'orig_source'	=> $source,
   1.554 +		'copied'		=> false,
   1.555 +		'relative_path'	=> (empty($convert->convertor['source_path_absolute'])) ? true : false,
   1.556 +	);
   1.557 +
   1.558 +	// copy file will prepend $phpBB_root_path
   1.559 +	$target = $config[$config_var] . '/' . utf8_basename(($use_target === false) ? $source : $use_target);
   1.560 +
   1.561 +	if (!empty($convert->convertor[$config_var]) && strpos($source, $convert->convertor[$config_var]) !== 0)
   1.562 +	{
   1.563 +		$source = $convert->convertor[$config_var] . $source;
   1.564 +	}
   1.565 +
   1.566 +	$result['source'] = $source;
   1.567 +
   1.568 +	if (file_exists(relative_base($source, $result['relative_path'], __LINE__, __FILE__)))
   1.569 +	{
   1.570 +		$result['copied'] = copy_file($source, $target, false, false, $result['relative_path']);
   1.571 +	}
   1.572 +
   1.573 +	if ($result['copied'])
   1.574 +	{
   1.575 +		$result['target'] = utf8_basename($target);
   1.576 +	}
   1.577 +	else
   1.578 +	{
   1.579 +		$result['target'] = ($use_target !== false) ? $result['orig_source'] : utf8_basename($target);
   1.580 +	}
   1.581 +
   1.582 +	return $result;
   1.583 +}
   1.584 +
   1.585 +function import_attachment($source, $use_target = false)
   1.586 +{
   1.587 +	if (empty($source))
   1.588 +	{
   1.589 +		return '';
   1.590 +	}
   1.591 +
   1.592 +	global $convert, $phpbb_root_path, $config, $user;
   1.593 +
   1.594 +	if (empty($convert->convertor['upload_path']))
   1.595 +	{
   1.596 +		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_UPLOAD_DIR'], 'import_attachment()'), __LINE__, __FILE__);
   1.597 +	}
   1.598 +
   1.599 +	$result = _import_check('upload_path', $source, $use_target);
   1.600 +
   1.601 +	if ($result['copied'])
   1.602 +	{
   1.603 +		// Thumbnails?
   1.604 +		if (is_array($convert->convertor['thumbnails']))
   1.605 +		{
   1.606 +			$thumb_dir = $convert->convertor['thumbnails'][0];
   1.607 +			$thumb_prefix = $convert->convertor['thumbnails'][1];
   1.608 +			$thumb_source = $thumb_dir . $thumb_prefix . utf8_basename($result['source']);
   1.609 +
   1.610 +			if (strpos($thumb_source, $convert->convertor['upload_path']) !== 0)
   1.611 +			{
   1.612 +				$thumb_source = $convert->convertor['upload_path'] . $thumb_source;
   1.613 +			}
   1.614 +			$thumb_target = $config['upload_path'] . '/thumb_' . $result['target'];
   1.615 +
   1.616 +			if (file_exists(relative_base($thumb_source, $result['relative_path'], __LINE__, __FILE__)))
   1.617 +			{
   1.618 +				copy_file($thumb_source, $thumb_target, false, false, $result['relative_path']);
   1.619 +			}
   1.620 +		}
   1.621 +	}
   1.622 +
   1.623 +	return $result['target'];
   1.624 +}
   1.625 +
   1.626 +function import_rank($source, $use_target = false)
   1.627 +{
   1.628 +	if (empty($source))
   1.629 +	{
   1.630 +		return '';
   1.631 +	}
   1.632 +
   1.633 +	global $convert, $phpbb_root_path, $config, $user;
   1.634 +
   1.635 +	if (!isset($convert->convertor['ranks_path']))
   1.636 +	{
   1.637 +		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_RANKS_PATH'], 'import_rank()'), __LINE__, __FILE__);
   1.638 +	}
   1.639 +
   1.640 +	$result = _import_check('ranks_path', $source, $use_target);
   1.641 +	return $result['target'];
   1.642 +}
   1.643 +
   1.644 +function import_smiley($source, $use_target = false)
   1.645 +{
   1.646 +	if (empty($source))
   1.647 +	{
   1.648 +		return '';
   1.649 +	}
   1.650 +
   1.651 +	global $convert, $phpbb_root_path, $config, $user;
   1.652 +
   1.653 +	if (!isset($convert->convertor['smilies_path']))
   1.654 +	{
   1.655 +		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_SMILIES_PATH'], 'import_smiley()'), __LINE__, __FILE__);
   1.656 +	}
   1.657 +
   1.658 +	$result = _import_check('smilies_path', $source, $use_target);
   1.659 +	return $result['target'];
   1.660 +}
   1.661 +
   1.662 +/*
   1.663 +*/
   1.664 +function import_avatar($source, $use_target = false, $user_id = false)
   1.665 +{
   1.666 +	if (empty($source) || preg_match('#^https?:#i', $source) || preg_match('#blank\.(gif|png)$#i', $source))
   1.667 +	{
   1.668 +		return;
   1.669 +	}
   1.670 +
   1.671 +	global $convert, $phpbb_root_path, $config, $user;
   1.672 +
   1.673 +	if (!isset($convert->convertor['avatar_path']))
   1.674 +	{
   1.675 +		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_AVATAR_PATH'], 'import_avatar()'), __LINE__, __FILE__);
   1.676 +	}
   1.677 +
   1.678 +	if ($use_target === false && $user_id !== false)
   1.679 +	{
   1.680 +		$use_target = $config['avatar_salt'] . '_' . $user_id . '.' . substr(strrchr($source, '.'), 1);
   1.681 +	}
   1.682 +
   1.683 +	$result = _import_check('avatar_path', $source, $use_target);
   1.684 +
   1.685 +	return ((!empty($user_id)) ? $user_id : $use_target) . '.' . substr(strrchr($source, '.'), 1);
   1.686 +}
   1.687 +
   1.688 +/**
   1.689 +* @todo all image dimension functions below (there are a *lot*) should get revisited and converted to one or two functions (no more needed, really).
   1.690 +*/
   1.691 +
   1.692 +/**
   1.693 +* Calculate the size of the specified image
   1.694 +* Called from the following functions for calculating the size of specific image types
   1.695 +*/
   1.696 +function get_image_dim($source)
   1.697 +{
   1.698 +	if (empty($source))
   1.699 +	{
   1.700 +		return array(0, 0);
   1.701 +	}
   1.702 +
   1.703 +	global $convert;
   1.704 +
   1.705 +	$relative_path = empty($convert->convertor['source_path_absolute']);
   1.706 +
   1.707 +	if (file_exists(relative_base($source, $relative_path)))
   1.708 +	{
   1.709 +		$image = relative_base($source, $relative_path);
   1.710 +		return @getimagesize($image);
   1.711 +	}
   1.712 +
   1.713 +	return false;
   1.714 +}
   1.715 +
   1.716 +/**
   1.717 +* Obtain the width of the specified smilie
   1.718 +*/
   1.719 +function get_smiley_width($src)
   1.720 +{
   1.721 +	return get_smiley_dim($src, 0);
   1.722 +}
   1.723 +
   1.724 +/**
   1.725 +* Obtain the height of the specified smilie
   1.726 +*/
   1.727 +function get_smiley_height($src)
   1.728 +{
   1.729 +	return get_smiley_dim($src, 1);
   1.730 +}
   1.731 +
   1.732 +/**
   1.733 +* Obtain the size of the specified smilie (using the cache if possible) and cache the value
   1.734 +*/
   1.735 +function get_smiley_dim($source, $axis)
   1.736 +{
   1.737 +	if (empty($source))
   1.738 +	{
   1.739 +		return 15;
   1.740 +	}
   1.741 +
   1.742 +	static $smiley_cache = array();
   1.743 +
   1.744 +	if (isset($smiley_cache[$source]))
   1.745 +	{
   1.746 +		return $smiley_cache[$source][$axis];
   1.747 +	}
   1.748 +
   1.749 +	global $convert, $phpbb_root_path, $config, $user;
   1.750 +
   1.751 +	$orig_source = $source;
   1.752 +
   1.753 +	if (!isset($convert->convertor['smilies_path']))
   1.754 +	{
   1.755 +		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_SMILIES_PATH'], 'get_smiley_dim()'), __LINE__, __FILE__);
   1.756 +	}
   1.757 +
   1.758 +	if (!empty($convert->convertor['smilies_path']) && strpos($source, $convert->convertor['smilies_path']) !== 0)
   1.759 +	{
   1.760 +		$source = $convert->convertor['smilies_path'] . $source;
   1.761 +	}
   1.762 +
   1.763 +	$smiley_cache[$orig_source] = get_image_dim($source);
   1.764 +
   1.765 +	if (empty($smiley_cache[$orig_source]) || empty($smiley_cache[$orig_source][0]) || empty($smiley_cache[$orig_source][1]))
   1.766 +	{
   1.767 +		$smiley_cache[$orig_source] = array(15, 15);
   1.768 +		return 15;
   1.769 +	}
   1.770 +
   1.771 +	return $smiley_cache[$orig_source][$axis];
   1.772 +}
   1.773 +
   1.774 +/**
   1.775 +* Obtain the width of the specified avatar
   1.776 +*/
   1.777 +function get_avatar_width($src, $func = false, $arg1 = false, $arg2 = false)
   1.778 +{
   1.779 +	return get_avatar_dim($src, 0, $func, $arg1, $arg2);
   1.780 +}
   1.781 +
   1.782 +/**
   1.783 +* Obtain the height of the specified avatar
   1.784 +*/
   1.785 +function get_avatar_height($src, $func = false, $arg1 = false, $arg2 = false)
   1.786 +{
   1.787 +	return get_avatar_dim($src, 1, $func, $arg1, $arg2);
   1.788 +}
   1.789 +
   1.790 +/**
   1.791 +*/
   1.792 +function get_avatar_dim($src, $axis, $func = false, $arg1 = false, $arg2 = false)
   1.793 +{
   1.794 +	$avatar_type = AVATAR_UPLOAD;
   1.795 +
   1.796 +	if ($func)
   1.797 +	{
   1.798 +		if ($arg1 || $arg2)
   1.799 +		{
   1.800 +			$ary = array($arg1);
   1.801 +
   1.802 +			if ($arg2)
   1.803 +			{
   1.804 +				$ary[] = $arg2;
   1.805 +			}
   1.806 +
   1.807 +			$avatar_type = call_user_func_array($func, $ary);
   1.808 +		}
   1.809 +		else
   1.810 +		{
   1.811 +			$avatar_type = call_user_func($func);
   1.812 +		}
   1.813 +	}
   1.814 +
   1.815 +	switch ($avatar_type)
   1.816 +	{
   1.817 +		case AVATAR_UPLOAD:
   1.818 +			return get_upload_avatar_dim($src, $axis);
   1.819 +		break;
   1.820 +
   1.821 +		case AVATAR_GALLERY:
   1.822 +			return get_gallery_avatar_dim($src, $axis);
   1.823 +		break;
   1.824 +
   1.825 +		case AVATAR_REMOTE:
   1.826 +			 // see notes on this functions usage and (hopefully) model $func to avoid this accordingly
   1.827 +			return get_remote_avatar_dim($src, $axis);
   1.828 +		break;
   1.829 +
   1.830 +		default:
   1.831 +			$default_x = (defined('DEFAULT_AVATAR_X_CUSTOM')) ? DEFAULT_AVATAR_X_CUSTOM : DEFAULT_AVATAR_X;
   1.832 +			$default_y = (defined('DEFAULT_AVATAR_Y_CUSTOM')) ? DEFAULT_AVATAR_Y_CUSTOM : DEFAULT_AVATAR_Y;
   1.833 +
   1.834 +			return $axis ? $default_y : $default_x;
   1.835 +		break;
   1.836 +	}
   1.837 +}
   1.838 +
   1.839 +/**
   1.840 +* Obtain the size of the specified uploaded avatar (using the cache if possible) and cache the value
   1.841 +*/
   1.842 +function get_upload_avatar_dim($source, $axis)
   1.843 +{
   1.844 +	static $cachedims = false;
   1.845 +	static $cachekey = false;
   1.846 +
   1.847 +	if (empty($source))
   1.848 +	{
   1.849 +		return 0;
   1.850 +	}
   1.851 +
   1.852 +	if ($cachekey == $source)
   1.853 +	{
   1.854 +		return $cachedims[$axis];
   1.855 +	}
   1.856 +
   1.857 +	$orig_source = $source;
   1.858 +
   1.859 +	if (substr($source, 0, 7) == 'upload:')
   1.860 +	{
   1.861 +		$source = substr($source, 7);
   1.862 +	}
   1.863 +
   1.864 +	global $convert, $phpbb_root_path, $config, $user;
   1.865 +
   1.866 +	if (!isset($convert->convertor['avatar_path']))
   1.867 +	{
   1.868 +		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_AVATAR_PATH'], 'get_upload_avatar_dim()'), __LINE__, __FILE__);
   1.869 +	}
   1.870 +
   1.871 +	if (!empty($convert->convertor['avatar_path']) && strpos($source, $convert->convertor['avatar_path']) !== 0)
   1.872 +	{
   1.873 +		$source = path($convert->convertor['avatar_path'], empty($convert->convertor['source_path_absolute'])) . $source;
   1.874 +	}
   1.875 +
   1.876 +	$cachedims = get_image_dim($source);
   1.877 +
   1.878 +	if (empty($cachedims) || empty($cachedims[0]) || empty($cachedims[1]))
   1.879 +	{
   1.880 +		$default_x = (defined('DEFAULT_AVATAR_X_CUSTOM')) ? DEFAULT_AVATAR_X_CUSTOM : DEFAULT_AVATAR_X;
   1.881 +		$default_y = (defined('DEFAULT_AVATAR_Y_CUSTOM')) ? DEFAULT_AVATAR_Y_CUSTOM : DEFAULT_AVATAR_Y;
   1.882 +
   1.883 +		$cachedims = array($default_x, $default_y);
   1.884 +	}
   1.885 +
   1.886 +	return $cachedims[$axis];
   1.887 +}
   1.888 +
   1.889 +/**
   1.890 +* Obtain the size of the specified gallery avatar (using the cache if possible) and cache the value
   1.891 +*/
   1.892 +function get_gallery_avatar_dim($source, $axis)
   1.893 +{
   1.894 +	if (empty($source))
   1.895 +	{
   1.896 +		return 0;
   1.897 +	}
   1.898 +
   1.899 +	static $avatar_cache = array();
   1.900 +
   1.901 +	if (isset($avatar_cache[$source]))
   1.902 +	{
   1.903 +		return $avatar_cache[$source][$axis];
   1.904 +	}
   1.905 +
   1.906 +	global $convert, $phpbb_root_path, $config, $user;
   1.907 +
   1.908 +	$orig_source = $source;
   1.909 +
   1.910 +	if (!isset($convert->convertor['avatar_gallery_path']))
   1.911 +	{
   1.912 +		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_GALLERY_PATH'], 'get_gallery_avatar_dim()'), __LINE__, __FILE__);
   1.913 +	}
   1.914 +
   1.915 +	if (!empty($convert->convertor['avatar_gallery_path']) && strpos($source, $convert->convertor['avatar_gallery_path']) !== 0)
   1.916 +	{
   1.917 +		$source = path($convert->convertor['avatar_gallery_path'], empty($convert->convertor['source_path_absolute'])) . $source;
   1.918 +	}
   1.919 +
   1.920 +	$avatar_cache[$orig_source] = get_image_dim($source);
   1.921 +
   1.922 +	if (empty($avatar_cache[$orig_source]) || empty($avatar_cache[$orig_source][0]) || empty($avatar_cache[$orig_source][1]))
   1.923 +	{
   1.924 +		$default_x = (defined('DEFAULT_AVATAR_X_CUSTOM')) ? DEFAULT_AVATAR_X_CUSTOM : DEFAULT_AVATAR_X;
   1.925 +		$default_y = (defined('DEFAULT_AVATAR_Y_CUSTOM')) ? DEFAULT_AVATAR_Y_CUSTOM : DEFAULT_AVATAR_Y;
   1.926 +
   1.927 +		$avatar_cache[$orig_source] = array($default_x, $default_y);
   1.928 +	}
   1.929 +
   1.930 +	return $avatar_cache[$orig_source][$axis];
   1.931 +}
   1.932 +
   1.933 +/**
   1.934 +* Obtain the size of the specified remote avatar (using the cache if possible) and cache the value
   1.935 +* Whilst it's unlikely that remote avatars will be duplicated, it is possible so caching seems the best option
   1.936 +* This should only be called from a post processing step due to the possibility of network timeouts
   1.937 +*/
   1.938 +function get_remote_avatar_dim($src, $axis)
   1.939 +{
   1.940 +	if (empty($src))
   1.941 +	{
   1.942 +		return 0;
   1.943 +	}
   1.944 +
   1.945 +	static $remote_avatar_cache = array();
   1.946 +
   1.947 +	// an ugly hack: we assume that the dimensions of each remote avatar are accessed exactly twice (x and y)
   1.948 +	if (isset($remote_avatar_cache[$src]))
   1.949 +	{
   1.950 +		$retval = $remote_avatar_cache[$src][$axis];
   1.951 +		unset($remote_avatar_cache);
   1.952 +		return $retval;
   1.953 +	}
   1.954 +
   1.955 +	$url_info = @parse_url($src);
   1.956 +	if (empty($url_info['host']))
   1.957 +	{
   1.958 +		return 0;
   1.959 +	}
   1.960 +	$host = $url_info['host'];
   1.961 +	$port = (isset($url_info['port'])) ? $url_info['port'] : 0;
   1.962 +	$protocol = (isset($url_info['scheme'])) ? $url_info['scheme'] : 'http';
   1.963 +	if (empty($port))
   1.964 +	{
   1.965 +		switch(strtolower($protocol))
   1.966 +		{
   1.967 +			case 'ftp':
   1.968 +				$port = 21;
   1.969 +				break;
   1.970 +
   1.971 +			case 'https':
   1.972 +				$port = 443;
   1.973 +				break;
   1.974 +
   1.975 +			default:
   1.976 +				$port = 80;
   1.977 +		}
   1.978 +	}
   1.979 +
   1.980 +	$timeout = @ini_get('default_socket_timeout');
   1.981 +	@ini_set('default_socket_timeout', 2);
   1.982 +
   1.983 +	// We're just trying to reach the server to avoid timeouts
   1.984 +	$fp = @fsockopen($host, $port, $errno, $errstr, 1);
   1.985 +	if ($fp)
   1.986 +	{
   1.987 +		$remote_avatar_cache[$src] = @getimagesize($src);
   1.988 +		fclose($fp);
   1.989 +	}
   1.990 +
   1.991 +	$default_x 	= (defined('DEFAULT_AVATAR_X_CUSTOM')) ? DEFAULT_AVATAR_X_CUSTOM : DEFAULT_AVATAR_X;
   1.992 +	$default_y 	= (defined('DEFAULT_AVATAR_Y_CUSTOM')) ? DEFAULT_AVATAR_Y_CUSTOM : DEFAULT_AVATAR_Y;
   1.993 +	$default 	= array($default_x, $default_y);
   1.994 +
   1.995 +	if (empty($remote_avatar_cache[$src]) || empty($remote_avatar_cache[$src][0]) || empty($remote_avatar_cache[$src][1]))
   1.996 +	{
   1.997 +		$remote_avatar_cache[$src] = $default;
   1.998 +	}
   1.999 +	else
  1.1000 +	{
  1.1001 +		// We trust gallery and uploaded avatars to conform to the size settings; we might have to adjust here
  1.1002 +		if ($remote_avatar_cache[$src][0] > $default_x || $remote_avatar_cache[$src][1] > $default_y)
  1.1003 +		{
  1.1004 +			$bigger = ($remote_avatar_cache[$src][0] > $remote_avatar_cache[$src][1]) ? 0 : 1;
  1.1005 +			$ratio = $default[$bigger] / $remote_avatar_cache[$src][$bigger];
  1.1006 +			$remote_avatar_cache[$src][0] = (int)($remote_avatar_cache[$src][0] * $ratio);
  1.1007 +			$remote_avatar_cache[$src][1] = (int)($remote_avatar_cache[$src][1] * $ratio);
  1.1008 +		}
  1.1009 +	}
  1.1010 +
  1.1011 +	@ini_set('default_socket_timeout', $timeout);
  1.1012 +	return $remote_avatar_cache[$src][$axis];
  1.1013 +}
  1.1014 +
  1.1015 +function set_user_options()
  1.1016 +{
  1.1017 +	global $convert_row;
  1.1018 +
  1.1019 +	// Key need to be set in row, else default value is chosen
  1.1020 +	$keyoptions = array(
  1.1021 +		'viewimg'		=> array('bit' => 0, 'default' => 1),
  1.1022 +		'viewflash'		=> array('bit' => 1, 'default' => 1),
  1.1023 +		'viewsmilies'	=> array('bit' => 2, 'default' => 1),
  1.1024 +		'viewsigs'		=> array('bit' => 3, 'default' => 1),
  1.1025 +		'viewavatars'	=> array('bit' => 4, 'default' => 1),
  1.1026 +		'viewcensors'	=> array('bit' => 5, 'default' => 1),
  1.1027 +		'attachsig'		=> array('bit' => 6, 'default' => 0),
  1.1028 +		'bbcode'		=> array('bit' => 8, 'default' => 1),
  1.1029 +		'smilies'		=> array('bit' => 9, 'default' => 1),
  1.1030 +		'popuppm'		=> array('bit' => 10, 'default' => 0),
  1.1031 +		'sig_bbcode'	=> array('bit' => 15, 'default' => 1),
  1.1032 +		'sig_smilies'	=> array('bit' => 16, 'default' => 1),
  1.1033 +		'sig_links'		=> array('bit' => 17, 'default' => 1),
  1.1034 +	);
  1.1035 +
  1.1036 +	$option_field = 0;
  1.1037 +
  1.1038 +	foreach ($keyoptions as $key => $key_ary)
  1.1039 +	{
  1.1040 +		$value = (isset($convert_row[$key])) ? (int) $convert_row[$key] : $key_ary['default'];
  1.1041 +
  1.1042 +		if ($value && !($option_field & 1 << $key_ary['bit']))
  1.1043 +		{
  1.1044 +			$option_field += 1 << $key_ary['bit'];
  1.1045 +		}
  1.1046 +	}
  1.1047 +
  1.1048 +	return $option_field;
  1.1049 +}
  1.1050 +
  1.1051 +/**
  1.1052 +* Index messages on the fly as we convert them
  1.1053 +* @todo naderman, can you check that this works with the new search plugins as it's use is currently disabled (and thus untested)
  1.1054 +function search_indexing($message = '')
  1.1055 +{
  1.1056 +	global $fulltext_search, $convert_row;
  1.1057 +
  1.1058 +	if (!isset($convert_row['post_id']))
  1.1059 +	{
  1.1060 +		return;
  1.1061 +	}
  1.1062 +
  1.1063 +	if (!$message)
  1.1064 +	{
  1.1065 +		if (!isset($convert_row['message']))
  1.1066 +		{
  1.1067 +			return;
  1.1068 +		}
  1.1069 +
  1.1070 +		$message = $convert_row['message'];
  1.1071 +	}
  1.1072 +
  1.1073 +	$title = (isset($convert_row['title'])) ? $convert_row['title'] : '';
  1.1074 +
  1.1075 +	$fulltext_search->index('post', $convert_row['post_id'], $message, $title, $convert_row['poster_id'], $convert_row['forum_id']);
  1.1076 +}
  1.1077 +*/
  1.1078 +
  1.1079 +function make_unique_filename($filename)
  1.1080 +{
  1.1081 +	if (!strlen($filename))
  1.1082 +	{
  1.1083 +		$filename = md5(unique_id()) . '.dat';
  1.1084 +	}
  1.1085 +	else if ($filename[0] == '.')
  1.1086 +	{
  1.1087 +		$filename = md5(unique_id()) . $filename;
  1.1088 +	}
  1.1089 +	else if (preg_match('/\.([a-z]+)$/i', $filename, $m))
  1.1090 +	{
  1.1091 +		$filename = preg_replace('/\.([a-z]+)$/i', '_' . md5(unique_id()) . '.\1', $filename);
  1.1092 +	}
  1.1093 +	else
  1.1094 +	{
  1.1095 +		$filename .= '_' . md5(unique_id()) . '.dat';
  1.1096 +	}
  1.1097 +
  1.1098 +	return $filename;
  1.1099 +}
  1.1100 +
  1.1101 +function words_unique(&$words)
  1.1102 +{
  1.1103 +	reset($words);
  1.1104 +	$return_array = array();
  1.1105 +
  1.1106 +	$word = current($words);
  1.1107 +	do
  1.1108 +	{
  1.1109 +		$return_array[$word] = $word;
  1.1110 +	}
  1.1111 +	while ($word = next($words));
  1.1112 +
  1.1113 +	return $return_array;
  1.1114 +}
  1.1115 +
  1.1116 +/**
  1.1117 +* Adds a user to the specified group and optionally makes them a group leader
  1.1118 +* This function does not create the group if it does not exist and so should only be called after the groups have been created
  1.1119 +*/
  1.1120 +function add_user_group($group_id, $user_id, $group_leader=false)
  1.1121 +{
  1.1122 +	global $convert, $phpbb_root_path, $config, $user, $db;
  1.1123 +
  1.1124 +	$sql = 'INSERT INTO ' . USER_GROUP_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  1.1125 +		'group_id'		=> $group_id,
  1.1126 +		'user_id'		=> $user_id,
  1.1127 +		'group_leader'	=> ($group_leader) ? 1 : 0,
  1.1128 +		'user_pending'	=> 0));
  1.1129 +	$db->sql_query($sql);
  1.1130 +}
  1.1131 +
  1.1132 +// STANDALONE FUNCTIONS
  1.1133 +
  1.1134 +/**
  1.1135 +* Add users to the pre-defined "special" groups
  1.1136 +*
  1.1137 +* @param string $group The name of the special group to add to
  1.1138 +* @param string $select_query An SQL query to retrieve the user(s) to add to the group
  1.1139 +*/
  1.1140 +function user_group_auth($group, $select_query, $use_src_db)
  1.1141 +{
  1.1142 +	global $convert, $phpbb_root_path, $config, $user, $db, $src_db, $same_db;
  1.1143 +
  1.1144 +	if (!in_array($group, array('guests', 'registered', 'registered_coppa', 'global_moderators', 'administrators', 'bots')))
  1.1145 +	{
  1.1146 +		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_WRONG_GROUP'], $group, 'user_group_auth()'), __LINE__, __FILE__, true);
  1.1147 +		return;
  1.1148 +	}
  1.1149 +
  1.1150 +	$sql = 'SELECT group_id
  1.1151 +		FROM ' . GROUPS_TABLE . "
  1.1152 +		WHERE group_name = '" . $db->sql_escape(strtoupper($group)) . "'";
  1.1153 +	$result = $db->sql_query($sql);
  1.1154 +	$group_id = (int) $db->sql_fetchfield('group_id');
  1.1155 +	$db->sql_freeresult($result);
  1.1156 +
  1.1157 +	if (!$group_id)
  1.1158 +	{
  1.1159 +		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_GROUP'], $group, 'user_group_auth()'), __LINE__, __FILE__, true);
  1.1160 +		return;
  1.1161 +	}
  1.1162 +
  1.1163 +	if ($same_db || !$use_src_db)
  1.1164 +	{
  1.1165 +		$sql = 'INSERT INTO ' . USER_GROUP_TABLE . ' (user_id, group_id, user_pending)
  1.1166 +			' . str_replace('{' . strtoupper($group) . '}', $group_id . ', 0', $select_query);
  1.1167 +		$db->sql_query($sql);
  1.1168 +	}
  1.1169 +	else
  1.1170 +	{
  1.1171 +		$result = $src_db->sql_query(str_replace('{' . strtoupper($group) . '}', $group_id . ' ', $select_query));
  1.1172 +		while ($row = $src_db->sql_fetchrow($result))
  1.1173 +		{
  1.1174 +			// this might become quite a lot of INSERTS unfortunately
  1.1175 +			$sql = 'INSERT INTO ' . USER_GROUP_TABLE . " (user_id, group_id, user_pending)
  1.1176 +				VALUES ({$row['user_id']}, $group_id, 0)";
  1.1177 +			$db->sql_query($sql);
  1.1178 +		}
  1.1179 +		$src_db->sql_freeresult($result);
  1.1180 +	}
  1.1181 +}
  1.1182 +
  1.1183 +/**
  1.1184 +* Retrieves configuration information from the source forum and caches it as an array
  1.1185 +* Both database and file driven configuration formats can be handled
  1.1186 +* (the type used is specified in $config_schema, see convert_phpbb20.php for more details)
  1.1187 +*/
  1.1188 +function get_config()
  1.1189 +{
  1.1190 +	static $convert_config;
  1.1191 +	global $user;
  1.1192 +
  1.1193 +	if (isset($convert_config))
  1.1194 +	{
  1.1195 +		return $convert_config;
  1.1196 +	}
  1.1197 +
  1.1198 +	global $src_db, $same_db, $phpbb_root_path, $config;
  1.1199 +	global $convert;
  1.1200 +
  1.1201 +	if ($convert->config_schema['table_format'] != 'file')
  1.1202 +	{
  1.1203 +		if ($convert->mysql_convert && $same_db)
  1.1204 +		{
  1.1205 +			$src_db->sql_query("SET NAMES 'binary'");
  1.1206 +		}
  1.1207 +
  1.1208 +		$sql = 'SELECT * FROM ' . $convert->src_table_prefix . $convert->config_schema['table_name'];
  1.1209 +		$result = $src_db->sql_query($sql);
  1.1210 +		$row = $src_db->sql_fetchrow($result);
  1.1211 +
  1.1212 +		if (!$row)
  1.1213 +		{
  1.1214 +			$convert->p_master->error($user->lang['CONV_ERROR_GET_CONFIG'], __LINE__, __FILE__);
  1.1215 +		}
  1.1216 +	}
  1.1217 +
  1.1218 +	if (is_array($convert->config_schema['table_format']))
  1.1219 +	{
  1.1220 +		$convert_config = array();
  1.1221 +		list($key, $val) = each($convert->config_schema['table_format']);
  1.1222 +
  1.1223 +		do
  1.1224 +		{
  1.1225 +			$convert_config[$row[$key]] = $row[$val];
  1.1226 +		}
  1.1227 +		while ($row = $src_db->sql_fetchrow($result));
  1.1228 +		$src_db->sql_freeresult($result);
  1.1229 +
  1.1230 +		if ($convert->mysql_convert && $same_db)
  1.1231 +		{
  1.1232 +			$src_db->sql_query("SET NAMES 'utf8'");
  1.1233 +		}
  1.1234 +	}
  1.1235 +	else if ($convert->config_schema['table_format'] == 'file')
  1.1236 +	{
  1.1237 +		$filename = $convert->options['forum_path'] . '/' . $convert->config_schema['filename'];
  1.1238 +		if (!file_exists($filename))
  1.1239 +		{
  1.1240 +			$convert->p_master->error($user->lang['FILE_NOT_FOUND'] . ': ' . $filename, __LINE__, __FILE__);
  1.1241 +		}
  1.1242 +
  1.1243 +		if (isset($convert->config_schema['array_name']))
  1.1244 +		{
  1.1245 +			unset($convert->config_schema['array_name']);
  1.1246 +		}
  1.1247 +
  1.1248 +		$convert_config = extract_variables_from_file($filename);
  1.1249 +		if (!empty($convert->config_schema['array_name']))
  1.1250 +		{
  1.1251 +			$convert_config = $convert_config[$convert->config_schema['array_name']];
  1.1252 +		}
  1.1253 +	}
  1.1254 +	else
  1.1255 +	{
  1.1256 +		$convert_config = $row;
  1.1257 +		if ($convert->mysql_convert && $same_db)
  1.1258 +		{
  1.1259 +			$src_db->sql_query("SET NAMES 'utf8'");
  1.1260 +		}
  1.1261 +	}
  1.1262 +
  1.1263 +	if (!sizeof($convert_config))
  1.1264 +	{
  1.1265 +		$convert->p_master->error($user->lang['CONV_ERROR_CONFIG_EMPTY'], __LINE__, __FILE__);
  1.1266 +	}
  1.1267 +
  1.1268 +	return $convert_config;
  1.1269 +}
  1.1270 +
  1.1271 +/**
  1.1272 +* Transfers the relevant configuration information from the source forum
  1.1273 +* The mapping of fields is specified in $config_schema, see convert_phpbb20.php for more details
  1.1274 +*/
  1.1275 +function restore_config($schema)
  1.1276 +{
  1.1277 +	global $db, $config;
  1.1278 +
  1.1279 +	$convert_config = get_config();
  1.1280 +
  1.1281 +	foreach ($schema['settings'] as $config_name => $src)
  1.1282 +	{
  1.1283 +		if (preg_match('/(.*)\((.*)\)/', $src, $m))
  1.1284 +		{
  1.1285 +			$var = (empty($m[2]) || empty($convert_config[$m[2]])) ? "''" : "'" . addslashes($convert_config[$m[2]]) . "'";
  1.1286 +			$exec = '$config_value = ' . $m[1] . '(' . $var . ');';
  1.1287 +			eval($exec);
  1.1288 +		}
  1.1289 +		else
  1.1290 +		{
  1.1291 +			if ($schema['table_format'] != 'file' || empty($schema['array_name']))
  1.1292 +			{
  1.1293 +				$config_value = (isset($convert_config[$src])) ? $convert_config[$src] : '';
  1.1294 +			}
  1.1295 +			else if (!empty($schema['array_name']))
  1.1296 +			{
  1.1297 +				$src_ary = $schema['array_name'];
  1.1298 +				$config_value = (isset($convert_config[$src_ary][$src])) ? $convert_config[$src_ary][$src] : '';
  1.1299 +			}
  1.1300 +   		}
  1.1301 +
  1.1302 +		if ($config_value !== '')
  1.1303 +		{
  1.1304 +			// Most are...
  1.1305 +			if (is_string($config_value))
  1.1306 +			{
  1.1307 +				$config_value = truncate_string(utf8_htmlspecialchars($config_value), 255, 255, false);
  1.1308 +			}
  1.1309 +
  1.1310 +			set_config($config_name, $config_value);
  1.1311 +		}
  1.1312 +	}
  1.1313 +}
  1.1314 +
  1.1315 +/**
  1.1316 +* Update the count of PM's in custom folders for all users
  1.1317 +*/
  1.1318 +function update_folder_pm_count()
  1.1319 +{
  1.1320 +	global $db, $convert, $user;
  1.1321 +
  1.1322 +	$sql = 'SELECT user_id, folder_id, COUNT(msg_id) as num_messages
  1.1323 +		FROM ' . PRIVMSGS_TO_TABLE . '
  1.1324 +		WHERE folder_id NOT IN (' . PRIVMSGS_NO_BOX . ', ' . PRIVMSGS_HOLD_BOX . ', ' . PRIVMSGS_INBOX . ', ' . PRIVMSGS_OUTBOX . ', ' . PRIVMSGS_SENTBOX . ')
  1.1325 +		GROUP BY folder_id, user_id';
  1.1326 +	$result = $db->sql_query($sql);
  1.1327 +
  1.1328 +	while ($row = $db->sql_fetchrow($result))
  1.1329 +	{
  1.1330 +		$db->sql_query('UPDATE ' . PRIVMSGS_FOLDER_TABLE . ' SET pm_count = ' . $row['num_messages'] . '
  1.1331 +			WHERE user_id = ' . $row['user_id'] . ' AND folder_id = ' . $row['folder_id']);
  1.1332 +	}
  1.1333 +	$db->sql_freeresult($result);
  1.1334 +}
  1.1335 +
  1.1336 +// Functions mainly used by the main convertor script
  1.1337 +
  1.1338 +function path($path, $path_relative = true)
  1.1339 +{
  1.1340 +	if ($path === false)
  1.1341 +	{
  1.1342 +		return '';
  1.1343 +	}
  1.1344 +
  1.1345 +	if (substr($path, -1) != '/')
  1.1346 +	{
  1.1347 +		$path .= '/';
  1.1348 +	}
  1.1349 +
  1.1350 +	if (!$path_relative)
  1.1351 +	{
  1.1352 +		return $path;
  1.1353 +	}
  1.1354 +
  1.1355 +	if (substr($path, 0, 1) == '/')
  1.1356 +	{
  1.1357 +		$path = substr($path, 1);
  1.1358 +	}
  1.1359 +
  1.1360 +	return $path;
  1.1361 +}
  1.1362 +
  1.1363 +/**
  1.1364 +* Extract the variables defined in a configuration file
  1.1365 +* @todo As noted by Xore we need to look at this from a security perspective
  1.1366 +*/
  1.1367 +function extract_variables_from_file($_filename)
  1.1368 +{
  1.1369 +	include($_filename);
  1.1370 +
  1.1371 +	$vars = get_defined_vars();
  1.1372 +	unset($vars['_filename']);
  1.1373 +
  1.1374 +	return $vars;
  1.1375 +}
  1.1376 +
  1.1377 +function get_path($src_path, $src_url, $test_file)
  1.1378 +{
  1.1379 +	global $config, $phpbb_root_path, $phpEx;
  1.1380 +
  1.1381 +	$board_config = get_config();
  1.1382 +
  1.1383 +	$test_file = preg_replace('/\.php$/i', ".$phpEx", $test_file);
  1.1384 +	$src_path = path($src_path);
  1.1385 +
  1.1386 +	if (@file_exists($phpbb_root_path . $src_path . $test_file))
  1.1387 +	{
  1.1388 +		return $src_path;
  1.1389 +	}
  1.1390 +
  1.1391 +	if (!empty($src_url) && !empty($board_config['server_name']))
  1.1392 +	{
  1.1393 +		if (!preg_match('#https?://([^/]+)(.*)#i', $src_url, $m))
  1.1394 +		{
  1.1395 +			return false;
  1.1396 +		}
  1.1397 +
  1.1398 +		if ($m[1] != $board_config['server_name'])
  1.1399 +		{
  1.1400 +			return false;
  1.1401 +		}
  1.1402 +
  1.1403 +		$url_parts = explode('/', $m[2]);
  1.1404 +		if (substr($src_url, -1) != '/')
  1.1405 +		{
  1.1406 +			if (preg_match('/.*\.([a-z0-9]{3,4})$/i', $url_parts[sizeof($url_parts) - 1]))
  1.1407 +			{
  1.1408 +				$url_parts[sizeof($url_parts) - 1] = '';
  1.1409 +			}
  1.1410 +			else
  1.1411 +			{
  1.1412 +				$url_parts[] = '';
  1.1413 +			}
  1.1414 +		}
  1.1415 +
  1.1416 +		$script_path = $board_config['script_path'];
  1.1417 +		if (substr($script_path, -1) == '/')
  1.1418 +		{
  1.1419 +			$script_path = substr($script_path, 0, -1);
  1.1420 +		}
  1.1421 +
  1.1422 +		$path_array = array();
  1.1423 +
  1.1424 +		$phpbb_parts = explode('/', $script_path);
  1.1425 +		for ($i = 0; $i < sizeof($url_parts); ++$i)
  1.1426 +		{
  1.1427 +			if ($i < sizeof($phpbb_parts[$i]) && $url_parts[$i] == $phpbb_parts[$i])
  1.1428 +			{
  1.1429 +				$path_array[] = $url_parts[$i];
  1.1430 +				unset($url_parts[$i]);
  1.1431 +			}
  1.1432 +			else
  1.1433 +			{
  1.1434 +				$path = '';
  1.1435 +				for ($j = $i; $j < sizeof($phpbb_parts); ++$j)
  1.1436 +				{
  1.1437 +					$path .= '../';
  1.1438 +				}
  1.1439 +				$path .= implode('/', $url_parts);
  1.1440 +				break;
  1.1441 +			}
  1.1442 +		}
  1.1443 +
  1.1444 +		if (!empty($path))
  1.1445 +		{
  1.1446 +			if (@file_exists($phpbb_root_path . $path . $test_file))
  1.1447 +			{
  1.1448 +				return $path;
  1.1449 +			}
  1.1450 +		}
  1.1451 +	}
  1.1452 +
  1.1453 +	return false;
  1.1454 +}
  1.1455 +
  1.1456 +function compare_table($tables, $tablename, &$prefixes)
  1.1457 +{
  1.1458 +	for ($i = 0, $table_size = sizeof($tables); $i < $table_size; ++$i)
  1.1459 +	{
  1.1460 +		if (preg_match('/(.*)' . $tables[$i] . '$/', $tablename, $m))
  1.1461 +		{
  1.1462 +			if (empty($m[1]))
  1.1463 +			{
  1.1464 +				$m[1] = '*';
  1.1465 +			}
  1.1466 +
  1.1467 +			if (isset($prefixes[$m[1]]))
  1.1468 +			{
  1.1469 +				$prefixes[$m[1]]++;
  1.1470 +			}
  1.1471 +			else
  1.1472 +			{
  1.1473 +				$prefixes[$m[1]] = 1;
  1.1474 +			}
  1.1475 +		}
  1.1476 +	}
  1.1477 +}
  1.1478 +
  1.1479 +/**
  1.1480 +* Grant permissions to a specified user or group
  1.1481 +*
  1.1482 +* @param string $ug_type user|group|user_role|group_role
  1.1483 +* @param mixed $forum_id forum ids (array|int|0) -> 0 == all forums
  1.1484 +* @param mixed $ug_id [int] user_id|group_id : [string] usergroup name
  1.1485 +* @param mixed $acl_list [string] acl entry : [array] acl entries : [string] role entry
  1.1486 +* @param int $setting ACL_YES|ACL_NO|ACL_NEVER
  1.1487 +*/
  1.1488 +function mass_auth($ug_type, $forum_id, $ug_id, $acl_list, $setting = ACL_NO)
  1.1489 +{
  1.1490 +	global $db, $convert, $user, $config;
  1.1491 +	static $acl_option_ids, $group_ids;
  1.1492 +
  1.1493 +	if (($ug_type == 'group' || $ug_type == 'group_role') && is_string($ug_id))
  1.1494 +	{
  1.1495 +		if (!isset($group_ids[$ug_id]))
  1.1496 +		{
  1.1497 +			$sql = 'SELECT group_id
  1.1498 +				FROM ' . GROUPS_TABLE . "
  1.1499 +				WHERE group_name = '" . $db->sql_escape(strtoupper($ug_id)) . "'";
  1.1500 +			$result = $db->sql_query_limit($sql, 1);
  1.1501 +			$id = (int) $db->sql_fetchfield('group_id');
  1.1502 +			$db->sql_freeresult($result);
  1.1503 +
  1.1504 +			if (!$id)
  1.1505 +			{
  1.1506 +				return;
  1.1507 +			}
  1.1508 +
  1.1509 +			$group_ids[$ug_id] = $id;
  1.1510 +		}
  1.1511 +
  1.1512 +		$ug_id = (int) $group_ids[$ug_id];
  1.1513 +	}
  1.1514 +
  1.1515 +	$table = ($ug_type == 'user' || $ug_type == 'user_role') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
  1.1516 +	$id_field = ($ug_type == 'user' || $ug_type == 'user_role') ? 'user_id' : 'group_id';
  1.1517 +
  1.1518 +	// Role based permissions are the simplest to handle so check for them first
  1.1519 +	if ($ug_type == 'user_role' || $ug_type == 'group_role')
  1.1520 +	{
  1.1521 +		if (is_numeric($forum_id))
  1.1522 +		{
  1.1523 +			$sql = 'SELECT role_id
  1.1524 +				FROM ' . ACL_ROLES_TABLE . "
  1.1525 +				WHERE role_name = 'ROLE_" . $db->sql_escape($acl_list) . "'";
  1.1526 +			$result = $db->sql_query_limit($sql, 1);
  1.1527 +			$row = $db->sql_fetchrow($result);
  1.1528 +			$db->sql_freeresult($result);
  1.1529 +
  1.1530 +			// If we have no role id there is something wrong here
  1.1531 +			if ($row)
  1.1532 +			{
  1.1533 +				$sql = "INSERT INTO $table ($id_field, forum_id, auth_role_id) VALUES ($ug_id, $forum_id, " . $row['role_id'] . ')';
  1.1534 +				$db->sql_query($sql);
  1.1535 +			}
  1.1536 +		}
  1.1537 +
  1.1538 +		return;
  1.1539 +	}
  1.1540 +
  1.1541 +	// Build correct parameters
  1.1542 +	$auth = array();
  1.1543 +
  1.1544 +	if (!is_array($acl_list))
  1.1545 +	{
  1.1546 +		$auth = array($acl_list => $setting);
  1.1547 +	}
  1.1548 +	else
  1.1549 +	{
  1.1550 +		foreach ($acl_list as $auth_option)
  1.1551 +		{
  1.1552 +			$auth[$auth_option] = $setting;
  1.1553 +		}
  1.1554 +	}
  1.1555 +	unset($acl_list);
  1.1556 +
  1.1557 +	if (!is_array($forum_id))
  1.1558 +	{
  1.1559 +		$forum_id = array($forum_id);
  1.1560 +	}
  1.1561 +
  1.1562 +	// Set any flags as required
  1.1563 +	foreach ($auth as $auth_option => $acl_setting)
  1.1564 +	{
  1.1565 +		$flag = substr($auth_option, 0, strpos($auth_option, '_') + 1);
  1.1566 +		if (empty($auth[$flag]))
  1.1567 +		{
  1.1568 +			$auth[$flag] = $acl_setting;
  1.1569 +		}
  1.1570 +	}
  1.1571 +
  1.1572 +	if (!is_array($acl_option_ids) || empty($acl_option_ids))
  1.1573 +	{
  1.1574 +		$sql = 'SELECT auth_option_id, auth_option
  1.1575 +			FROM ' . ACL_OPTIONS_TABLE;
  1.1576 +		$result = $db->sql_query($sql);
  1.1577 +
  1.1578 +		while ($row = $db->sql_fetchrow($result))
  1.1579 +		{
  1.1580 +			$acl_option_ids[$row['auth_option']] = $row['auth_option_id'];
  1.1581 +		}
  1.1582 +		$db->sql_freeresult($result);
  1.1583 +	}
  1.1584 +
  1.1585 +	$sql_forum = 'AND ' . $db->sql_in_set('a.forum_id', array_map('intval', $forum_id), false, true);
  1.1586 +
  1.1587 +	$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";
  1.1588 +	$result = $db->sql_query($sql);
  1.1589 +
  1.1590 +	$cur_auth = array();
  1.1591 +	while ($row = $db->sql_fetchrow($result))
  1.1592 +	{
  1.1593 +		$cur_auth[$row['forum_id']][$row['auth_option_id']] = $row['auth_setting'];
  1.1594 +	}
  1.1595 +	$db->sql_freeresult($result);
  1.1596 +
  1.1597 +	$sql_ary = array();
  1.1598 +	foreach ($forum_id as $forum)
  1.1599 +	{
  1.1600 +		foreach ($auth as $auth_option => $setting)
  1.1601 +		{
  1.1602 +			$auth_option_id = $acl_option_ids[$auth_option];
  1.1603 +
  1.1604 +			if (!$auth_option_id)
  1.1605 +			{
  1.1606 +				continue;
  1.1607 +			}
  1.1608 +
  1.1609 +			switch ($setting)
  1.1610 +			{
  1.1611 +				case ACL_NO:
  1.1612 +					if (isset($cur_auth[$forum][$auth_option_id]))
  1.1613 +					{
  1.1614 +						$sql_ary['delete'][] = "DELETE FROM $table
  1.1615 +							WHERE forum_id = $forum
  1.1616 +								AND auth_option_id = $auth_option_id
  1.1617 +								AND $id_field = $ug_id";
  1.1618 +					}
  1.1619 +				break;
  1.1620 +
  1.1621 +				default:
  1.1622 +					if (!isset($cur_auth[$forum][$auth_option_id]))
  1.1623 +					{
  1.1624 +						$sql_ary['insert'][] = "$ug_id, $forum, $auth_option_id, $setting";
  1.1625 +					}
  1.1626 +					else if ($cur_auth[$forum][$auth_option_id] != $setting)
  1.1627 +					{
  1.1628 +						$sql_ary['update'][] = "UPDATE " . $table . "
  1.1629 +							SET auth_setting = $setting
  1.1630 +							WHERE $id_field = $ug_id
  1.1631 +								AND forum_id = $forum
  1.1632 +								AND auth_option_id = $auth_option_id";
  1.1633 +					}
  1.1634 +			}
  1.1635 +		}
  1.1636 +	}
  1.1637 +	unset($cur_auth);
  1.1638 +
  1.1639 +	$sql = '';
  1.1640 +	foreach ($sql_ary as $sql_type => $sql_subary)
  1.1641 +	{
  1.1642 +		switch ($sql_type)
  1.1643 +		{
  1.1644 +			case 'insert':
  1.1645 +				switch ($db->sql_layer)
  1.1646 +				{
  1.1647 +					case 'mysql':
  1.1648 +					case 'mysql4':
  1.1649 +						$sql = 'VALUES ' . implode(', ', preg_replace('#^(.*?)$#', '(\1)', $sql_subary));
  1.1650 +					break;
  1.1651 +
  1.1652 +					case 'mssql':
  1.1653 +					case 'sqlite':
  1.1654 +					case 'mssqlnative':
  1.1655 +						$sql = implode(' UNION ALL ', preg_replace('#^(.*?)$#', 'SELECT \1', $sql_subary));
  1.1656 +					break;
  1.1657 +
  1.1658 +					default:
  1.1659 +						foreach ($sql_subary as $sql)
  1.1660 +						{
  1.1661 +							$sql = "INSERT INTO $table ($id_field, forum_id, auth_option_id, auth_setting) VALUES ($sql)";
  1.1662 +							$db->sql_query($sql);
  1.1663 +							$sql = '';
  1.1664 +						}
  1.1665 +				}
  1.1666 +
  1.1667 +				if ($sql != '')
  1.1668 +				{
  1.1669 +					$sql = "INSERT INTO $table ($id_field, forum_id, auth_option_id, auth_setting) $sql";
  1.1670 +					$db->sql_query($sql);
  1.1671 +				}
  1.1672 +			break;
  1.1673 +
  1.1674 +			case 'update':
  1.1675 +			case 'delete':
  1.1676 +				foreach ($sql_subary as $sql)
  1.1677 +				{
  1.1678 +					$db->sql_query($sql);
  1.1679 +					$sql = '';
  1.1680 +				}
  1.1681 +			break;
  1.1682 +		}
  1.1683 +		unset($sql_ary[$sql_type]);
  1.1684 +	}
  1.1685 +	unset($sql_ary);
  1.1686 +
  1.1687 +}
  1.1688 +
  1.1689 +/**
  1.1690 +* Update the count of unread private messages for all users
  1.1691 +*/
  1.1692 +function update_unread_count()
  1.1693 +{
  1.1694 +	global $db;
  1.1695 +
  1.1696 +	$sql = 'SELECT user_id, COUNT(msg_id) as num_messages
  1.1697 +		FROM ' . PRIVMSGS_TO_TABLE . '
  1.1698 +		WHERE pm_unread = 1
  1.1699 +			AND folder_id <> ' . PRIVMSGS_OUTBOX . '
  1.1700 +		GROUP BY user_id';
  1.1701 +	$result = $db->sql_query($sql);
  1.1702 +
  1.1703 +	while ($row = $db->sql_fetchrow($result))
  1.1704 +	{
  1.1705 +		$db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_unread_privmsg = ' . $row['num_messages'] . '
  1.1706 +			WHERE user_id = ' . $row['user_id']);
  1.1707 +	}
  1.1708 +	$db->sql_freeresult($result);
  1.1709 +}
  1.1710 +
  1.1711 +/**
  1.1712 +* Add any of the pre-defined "special" groups which are missing from the database
  1.1713 +*/
  1.1714 +function add_default_groups()
  1.1715 +{
  1.1716 +	global $db;
  1.1717 +
  1.1718 +	$default_groups = array(
  1.1719 +		'GUESTS'			=> array('', 0, 0),
  1.1720 +		'REGISTERED'		=> array('', 0, 0),
  1.1721 +		'REGISTERED_COPPA'	=> array('', 0, 0),
  1.1722 +		'GLOBAL_MODERATORS'	=> array('00AA00', 1, 0),
  1.1723 +		'ADMINISTRATORS'	=> array('AA0000', 1, 1),
  1.1724 +		'BOTS'				=> array('9E8DA7', 0, 0),
  1.1725 +		'NEWLY_REGISTERED'		=> array('', 0, 0),
  1.1726 +	);
  1.1727 +
  1.1728 +	$sql = 'SELECT *
  1.1729 +		FROM ' . GROUPS_TABLE . '
  1.1730 +		WHERE ' . $db->sql_in_set('group_name', array_keys($default_groups));
  1.1731 +	$result = $db->sql_query($sql);
  1.1732 +
  1.1733 +	while ($row = $db->sql_fetchrow($result))
  1.1734 +	{
  1.1735 +		unset($default_groups[strtoupper($row['group_name'])]);
  1.1736 +	}
  1.1737 +	$db->sql_freeresult($result);
  1.1738 +
  1.1739 +	$sql_ary = array();
  1.1740 +
  1.1741 +	foreach ($default_groups as $name => $data)
  1.1742 +	{
  1.1743 +		$sql_ary[] = array(
  1.1744 +			'group_name'			=> (string) $name,
  1.1745 +			'group_desc'			=> '',
  1.1746 +			'group_desc_uid'		=> '',
  1.1747 +			'group_desc_bitfield'	=> '',
  1.1748 +			'group_type'			=> GROUP_SPECIAL,
  1.1749 +			'group_colour'			=> (string) $data[0],
  1.1750 +			'group_legend'			=> (int) $data[1],
  1.1751 +			'group_founder_manage'	=> (int) $data[2]
  1.1752 +		);
  1.1753 +	}
  1.1754 +
  1.1755 +	if (sizeof($sql_ary))
  1.1756 +	{
  1.1757 +		$db->sql_multi_insert(GROUPS_TABLE, $sql_ary);
  1.1758 +	}
  1.1759 +}
  1.1760 +
  1.1761 +
  1.1762 +/**
  1.1763 +* Sync post count. We might need to do this in batches.
  1.1764 +*/
  1.1765 +function sync_post_count($offset, $limit)
  1.1766 +{
  1.1767 +	global $db;
  1.1768 +	$sql = 'SELECT COUNT(post_id) AS num_posts, poster_id
  1.1769 +			FROM ' . POSTS_TABLE . '
  1.1770 +			WHERE post_postcount = 1
  1.1771 +				AND post_approved = 1
  1.1772 +			GROUP BY poster_id
  1.1773 +			ORDER BY poster_id';
  1.1774 +	$result = $db->sql_query_limit($sql, $limit, $offset);
  1.1775 +
  1.1776 +	while ($row = $db->sql_fetchrow($result))
  1.1777 +	{
  1.1778 +		$db->sql_query('UPDATE ' . USERS_TABLE . " SET user_posts = {$row['num_posts']} WHERE user_id = {$row['poster_id']}");
  1.1779 +	}
  1.1780 +	$db->sql_freeresult($result);
  1.1781 +}
  1.1782 +
  1.1783 +/**
  1.1784 +* Add the search bots into the database
  1.1785 +* This code should be used in execute_last if the source database did not have bots
  1.1786 +* If you are converting bots this function should not be called
  1.1787 +* @todo We might want to look at sharing the bot list between the install code and this code for consistancy
  1.1788 +*/
  1.1789 +function add_bots()
  1.1790 +{
  1.1791 +	global $db, $convert, $user, $config, $phpbb_root_path, $phpEx;
  1.1792 +
  1.1793 +	$db->sql_query($convert->truncate_statement . BOTS_TABLE);
  1.1794 +
  1.1795 +	$sql = 'SELECT group_id FROM ' . GROUPS_TABLE . " WHERE group_name = 'BOTS'";
  1.1796 +	$result = $db->sql_query($sql);
  1.1797 +	$group_id = (int) $db->sql_fetchfield('group_id', false, $result);
  1.1798 +	$db->sql_freeresult($result);
  1.1799 +
  1.1800 +	if (!$group_id)
  1.1801 +	{
  1.1802 +		add_default_groups();
  1.1803 +
  1.1804 +		$sql = 'SELECT group_id FROM ' . GROUPS_TABLE . " WHERE group_name = 'BOTS'";
  1.1805 +		$result = $db->sql_query($sql);
  1.1806 +		$group_id = (int) $db->sql_fetchfield('group_id', false, $result);
  1.1807 +		$db->sql_freeresult($result);
  1.1808 +
  1.1809 +		if (!$group_id)
  1.1810 +		{
  1.1811 +			global $install;
  1.1812 +			$install->error($user->lang['CONV_ERROR_INCONSISTENT_GROUPS'], __LINE__, __FILE__);
  1.1813 +		}
  1.1814 +	}
  1.1815 +
  1.1816 +	$bots = array(
  1.1817 +		'AdsBot [Google]'			=> array('AdsBot-Google', ''),
  1.1818 +		'Alexa [Bot]'				=> array('ia_archiver', ''),
  1.1819 +		'Alta Vista [Bot]'			=> array('Scooter/', ''),
  1.1820 +		'Ask Jeeves [Bot]'			=> array('Ask Jeeves', ''),
  1.1821 +		'Baidu [Spider]'			=> array('Baiduspider+(', ''),
  1.1822 +		'Bing [Bot]'				=> array('bingbot/', ''),
  1.1823 +		'Exabot [Bot]'				=> array('Exabot/', ''),
  1.1824 +		'FAST Enterprise [Crawler]'	=> array('FAST Enterprise Crawler', ''),
  1.1825 +		'FAST WebCrawler [Crawler]'	=> array('FAST-WebCrawler/', ''),
  1.1826 +		'Francis [Bot]'				=> array('http://www.neomo.de/', ''),
  1.1827 +		'Gigabot [Bot]'				=> array('Gigabot/', ''),
  1.1828 +		'Google Adsense [Bot]'		=> array('Mediapartners-Google', ''),
  1.1829 +		'Google Desktop'			=> array('Google Desktop', ''),
  1.1830 +		'Google Feedfetcher'		=> array('Feedfetcher-Google', ''),
  1.1831 +		'Google [Bot]'				=> array('Googlebot', ''),
  1.1832 +		'Heise IT-Markt [Crawler]'	=> array('heise-IT-Markt-Crawler', ''),
  1.1833 +		'Heritrix [Crawler]'		=> array('heritrix/1.', ''),
  1.1834 +		'IBM Research [Bot]'		=> array('ibm.com/cs/crawler', ''),
  1.1835 +		'ICCrawler - ICjobs'		=> array('ICCrawler - ICjobs', ''),
  1.1836 +		'ichiro [Crawler]'			=> array('ichiro/2', ''),
  1.1837 +		'Majestic-12 [Bot]'			=> array('MJ12bot/', ''),
  1.1838 +		'Metager [Bot]'				=> array('MetagerBot/', ''),
  1.1839 +		'MSN NewsBlogs'				=> array('msnbot-NewsBlogs/', ''),
  1.1840 +		'MSN [Bot]'					=> array('msnbot/', ''),
  1.1841 +		'MSNbot Media'				=> array('msnbot-media/', ''),
  1.1842 +		'NG-Search [Bot]'			=> array('NG-Search/', ''),
  1.1843 +		'Nutch [Bot]'				=> array('http://lucene.apache.org/nutch/', ''),
  1.1844 +		'Nutch/CVS [Bot]'			=> array('NutchCVS/', ''),
  1.1845 +		'OmniExplorer [Bot]'		=> array('OmniExplorer_Bot/', ''),
  1.1846 +		'Online link [Validator]'	=> array('online link validator', ''),
  1.1847 +		'psbot [Picsearch]'			=> array('psbot/0', ''),
  1.1848 +		'Seekport [Bot]'			=> array('Seekbot/', ''),
  1.1849 +		'Sensis [Crawler]'			=> array('Sensis Web Crawler', ''),
  1.1850 +		'SEO Crawler'				=> array('SEO search Crawler/', ''),
  1.1851 +		'Seoma [Crawler]'			=> array('Seoma [SEO Crawler]', ''),
  1.1852 +		'SEOSearch [Crawler]'		=> array('SEOsearch/', ''),
  1.1853 +		'Snappy [Bot]'				=> array('Snappy/1.1 ( http://www.urltrends.com/ )', ''),
  1.1854 +		'Steeler [Crawler]'			=> array('http://www.tkl.iis.u-tokyo.ac.jp/~crawler/', ''),
  1.1855 +		'Synoo [Bot]'				=> array('SynooBot/', ''),
  1.1856 +		'Telekom [Bot]'				=> array('crawleradmin.t-info@telekom.de', ''),
  1.1857 +		'TurnitinBot [Bot]'			=> array('TurnitinBot/', ''),
  1.1858 +		'Voyager [Bot]'				=> array('voyager/1.0', ''),
  1.1859 +		'W3 [Sitesearch]'			=> array('W3 SiteSearch Crawler', ''),
  1.1860 +		'W3C [Linkcheck]'			=> array('W3C-checklink/', ''),
  1.1861 +		'W3C [Validator]'			=> array('W3C_*Validator', ''),
  1.1862 +		'WiseNut [Bot]'				=> array('http://www.WISEnutbot.com', ''),
  1.1863 +		'YaCy [Bot]'				=> array('yacybot', ''),
  1.1864 +		'Yahoo MMCrawler [Bot]'		=> array('Yahoo-MMCrawler/', ''),
  1.1865 +		'Yahoo Slurp [Bot]'			=> array('Yahoo! DE Slurp', ''),
  1.1866 +		'Yahoo [Bot]'				=> array('Yahoo! Slurp', ''),
  1.1867 +		'YahooSeeker [Bot]'			=> array('YahooSeeker/', ''),
  1.1868 +	);
  1.1869 +
  1.1870 +	if (!function_exists('user_add'))
  1.1871 +	{
  1.1872 +		include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  1.1873 +	}
  1.1874 +
  1.1875 +	foreach ($bots as $bot_name => $bot_ary)
  1.1876 +	{
  1.1877 +		$user_row = array(
  1.1878 +			'user_type'				=> USER_IGNORE,
  1.1879 +			'group_id'				=> $group_id,
  1.1880 +			'username'				=> $bot_name,
  1.1881 +			'user_regdate'			=> time(),
  1.1882 +			'user_password'			=> '',
  1.1883 +			'user_colour'			=> '9E8DA7',
  1.1884 +			'user_email'			=> '',
  1.1885 +			'user_lang'				=> $config['default_lang'],
  1.1886 +			'user_style'			=> 1,
  1.1887 +			'user_timezone'			=> 0,
  1.1888 +			'user_allow_massemail'	=> 0,
  1.1889 +		);
  1.1890 +
  1.1891 +		$user_id = user_add($user_row);
  1.1892 +
  1.1893 +		if ($user_id)
  1.1894 +		{
  1.1895 +			$sql = 'INSERT INTO ' . BOTS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  1.1896 +				'bot_active'	=> 1,
  1.1897 +				'bot_name'		=> $bot_name,
  1.1898 +				'user_id'		=> $user_id,
  1.1899 +				'bot_agent'		=> $bot_ary[0],
  1.1900 +				'bot_ip'		=> $bot_ary[1])
  1.1901 +			);
  1.1902 +			$db->sql_query($sql);
  1.1903 +		}
  1.1904 +	}
  1.1905 +}
  1.1906 +
  1.1907 +/**
  1.1908 +* Update any dynamic configuration variables after the conversion is finished
  1.1909 +* @todo Confirm that this updates all relevant values since it has not necessarily been kept in sync with all changes
  1.1910 +*/
  1.1911 +function update_dynamic_config()
  1.1912 +{
  1.1913 +	global $db, $config;
  1.1914 +
  1.1915 +	// Get latest username
  1.1916 +	$sql = 'SELECT user_id, username, user_colour
  1.1917 +		FROM ' . USERS_TABLE . '
  1.1918 +		WHERE user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')';
  1.1919 +
  1.1920 +	if (!empty($config['increment_user_id']))
  1.1921 +	{
  1.1922 +		$sql .= ' AND user_id <> ' . $config['increment_user_id'];
  1.1923 +	}
  1.1924 +
  1.1925 +	$sql .= ' ORDER BY user_id DESC';
  1.1926 +
  1.1927 +	$result = $db->sql_query_limit($sql, 1);
  1.1928 +	$row = $db->sql_fetchrow($result);
  1.1929 +	$db->sql_freeresult($result);
  1.1930 +
  1.1931 +	if ($row)
  1.1932 +	{
  1.1933 +		set_config('newest_user_id', $row['user_id'], true);
  1.1934 +		set_config('newest_username', $row['username'], true);
  1.1935 +		set_config('newest_user_colour', $row['user_colour'], true);
  1.1936 +	}
  1.1937 +
  1.1938 +//	Also do not reset record online user/date. There will be old data or the fresh data from the schema.
  1.1939 +//	set_config('record_online_users', 1, true);
  1.1940 +//	set_config('record_online_date', time(), true);
  1.1941 +
  1.1942 +	$sql = 'SELECT COUNT(post_id) AS stat
  1.1943 +		FROM ' . POSTS_TABLE . '
  1.1944 +		WHERE post_approved = 1';
  1.1945 +	$result = $db->sql_query($sql);
  1.1946 +	$row = $db->sql_fetchrow($result);
  1.1947 +	$db->sql_freeresult($result);
  1.1948 +
  1.1949 +	set_config('num_posts', (int) $row['stat'], true);
  1.1950 +
  1.1951 +	$sql = 'SELECT COUNT(topic_id) AS stat
  1.1952 +		FROM ' . TOPICS_TABLE . '
  1.1953 +		WHERE topic_approved = 1';
  1.1954 +	$result = $db->sql_query($sql);
  1.1955 +	$row = $db->sql_fetchrow($result);
  1.1956 +	$db->sql_freeresult($result);
  1.1957 +
  1.1958 +	set_config('num_topics', (int) $row['stat'], true);
  1.1959 +
  1.1960 +	$sql = 'SELECT COUNT(user_id) AS stat
  1.1961 +		FROM ' . USERS_TABLE . '
  1.1962 +		WHERE user_type IN (' . USER_NORMAL . ',' . USER_FOUNDER . ')';
  1.1963 +	$result = $db->sql_query($sql);
  1.1964 +	$row = $db->sql_fetchrow($result);
  1.1965 +	$db->sql_freeresult($result);
  1.1966 +
  1.1967 +	set_config('num_users', (int) $row['stat'], true);
  1.1968 +
  1.1969 +	$sql = 'SELECT COUNT(attach_id) as stat
  1.1970 +		FROM ' . ATTACHMENTS_TABLE . '
  1.1971 +		WHERE is_orphan = 0';
  1.1972 +	$result = $db->sql_query($sql);
  1.1973 +	set_config('num_files', (int) $db->sql_fetchfield('stat'), true);
  1.1974 +	$db->sql_freeresult($result);
  1.1975 +
  1.1976 +	$sql = 'SELECT SUM(filesize) as stat
  1.1977 +		FROM ' . ATTACHMENTS_TABLE . '
  1.1978 +		WHERE is_orphan = 0';
  1.1979 +	$result = $db->sql_query($sql);
  1.1980 +	set_config('upload_dir_size', (float) $db->sql_fetchfield('stat'), true);
  1.1981 +	$db->sql_freeresult($result);
  1.1982 +
  1.1983 +	/**
  1.1984 +	* We do not resync users post counts - this can be done by the admin after conversion if wanted.
  1.1985 +	$sql = 'SELECT COUNT(post_id) AS num_posts, poster_id
  1.1986 +		FROM ' . POSTS_TABLE . '
  1.1987 +		WHERE post_postcount = 1
  1.1988 +		GROUP BY poster_id';
  1.1989 +	$result = $db->sql_query($sql);
  1.1990 +
  1.1991 +	while ($row = $db->sql_fetchrow($result))
  1.1992 +	{
  1.1993 +		$db->sql_query('UPDATE ' . USERS_TABLE . " SET user_posts = {$row['num_posts']} WHERE user_id = {$row['poster_id']}");
  1.1994 +	}
  1.1995 +	$db->sql_freeresult($result);
  1.1996 +	*/
  1.1997 +}
  1.1998 +
  1.1999 +/**
  1.2000 +* Updates topics_posted entries
  1.2001 +*/
  1.2002 +function update_topics_posted()
  1.2003 +{
  1.2004 +	global $db, $config;
  1.2005 +
  1.2006 +	switch ($db->sql_layer)
  1.2007 +	{
  1.2008 +		case 'sqlite':
  1.2009 +		case 'firebird':
  1.2010 +			$db->sql_query('DELETE FROM ' . TOPICS_POSTED_TABLE);
  1.2011 +		break;
  1.2012 +
  1.2013 +		default:
  1.2014 +			$db->sql_query('TRUNCATE TABLE ' . TOPICS_POSTED_TABLE);
  1.2015 +		break;
  1.2016 +	}
  1.2017 +
  1.2018 +	// This can get really nasty... therefore we only do the last six months
  1.2019 +	$get_from_time = time() - (6 * 4 * 7 * 24 * 60 * 60);
  1.2020 +
  1.2021 +	// Select forum ids, do not include categories
  1.2022 +	$sql = 'SELECT forum_id
  1.2023 +		FROM ' . FORUMS_TABLE . '
  1.2024 +		WHERE forum_type <> ' . FORUM_CAT;
  1.2025 +	$result = $db->sql_query($sql);
  1.2026 +
  1.2027 +	$forum_ids = array();
  1.2028 +	while ($row = $db->sql_fetchrow($result))
  1.2029 +	{
  1.2030 +		$forum_ids[] = $row['forum_id'];
  1.2031 +	}
  1.2032 +	$db->sql_freeresult($result);
  1.2033 +
  1.2034 +	// Any global announcements? ;)
  1.2035 +	$forum_ids[] = 0;
  1.2036 +
  1.2037 +	// Now go through the forums and get us some topics...
  1.2038 +	foreach ($forum_ids as $forum_id)
  1.2039 +	{
  1.2040 +		$sql = 'SELECT p.poster_id, p.topic_id
  1.2041 +			FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . ' t
  1.2042 +			WHERE t.forum_id = ' . $forum_id . '
  1.2043 +				AND t.topic_moved_id = 0
  1.2044 +				AND t.topic_last_post_time > ' . $get_from_time . '
  1.2045 +				AND t.topic_id = p.topic_id
  1.2046 +				AND p.poster_id <> ' . ANONYMOUS . '
  1.2047 +			GROUP BY p.poster_id, p.topic_id';
  1.2048 +		$result = $db->sql_query($sql);
  1.2049 +
  1.2050 +		$posted = array();
  1.2051 +		while ($row = $db->sql_fetchrow($result))
  1.2052 +		{
  1.2053 +			$posted[$row['poster_id']][] = $row['topic_id'];
  1.2054 +		}
  1.2055 +		$db->sql_freeresult($result);
  1.2056 +
  1.2057 +		$sql_ary = array();
  1.2058 +		foreach ($posted as $user_id => $topic_row)
  1.2059 +		{
  1.2060 +			foreach ($topic_row as $topic_id)
  1.2061 +			{
  1.2062 +				$sql_ary[] = array(
  1.2063 +					'user_id'		=> (int) $user_id,
  1.2064 +					'topic_id'		=> (int) $topic_id,
  1.2065 +					'topic_posted'	=> 1,
  1.2066 +				);
  1.2067 +			}
  1.2068 +		}
  1.2069 +		unset($posted);
  1.2070 +
  1.2071 +		if (sizeof($sql_ary))
  1.2072 +		{
  1.2073 +			$db->sql_multi_insert(TOPICS_POSTED_TABLE, $sql_ary);
  1.2074 +		}
  1.2075 +	}
  1.2076 +}
  1.2077 +
  1.2078 +/**
  1.2079 +* Ensure that all users have a default group specified and update related information such as their colour
  1.2080 +*/
  1.2081 +function fix_empty_primary_groups()
  1.2082 +{
  1.2083 +	global $db;
  1.2084 +
  1.2085 +	// Set group ids for users not already having it
  1.2086 +	$sql = 'UPDATE ' . USERS_TABLE . ' SET group_id = ' . get_group_id('registered') . '
  1.2087 +		WHERE group_id = 0 AND user_type = ' . USER_INACTIVE;
  1.2088 +	$db->sql_query($sql);
  1.2089 +
  1.2090 +	$sql = 'UPDATE ' . USERS_TABLE . ' SET group_id = ' . get_group_id('registered') . '
  1.2091 +		WHERE group_id = 0 AND user_type = ' . USER_NORMAL;
  1.2092 +	$db->sql_query($sql);
  1.2093 +
  1.2094 +	$db->sql_query('UPDATE ' . USERS_TABLE . ' SET group_id = ' . get_group_id('guests') . ' WHERE user_id = ' . ANONYMOUS);
  1.2095 +
  1.2096 +	$sql = 'SELECT user_id FROM ' . USER_GROUP_TABLE . ' WHERE group_id = ' . get_group_id('administrators');
  1.2097 +	$result = $db->sql_query($sql);
  1.2098 +
  1.2099 +	$user_ids = array();
  1.2100 +	while ($row = $db->sql_fetchrow($result))
  1.2101 +	{
  1.2102 +		$user_ids[] = $row['user_id'];
  1.2103 +	}
  1.2104 +	$db->sql_freeresult($result);
  1.2105 +
  1.2106 +	if (sizeof($user_ids))
  1.2107 +	{
  1.2108 +		$db->sql_query('UPDATE ' . USERS_TABLE . ' SET group_id = ' . get_group_id('administrators') . '
  1.2109 +			WHERE group_id = 0 AND ' . $db->sql_in_set('user_id', $user_ids));
  1.2110 +	}
  1.2111 +
  1.2112 +	$sql = 'SELECT user_id FROM ' . USER_GROUP_TABLE . ' WHERE group_id = ' . get_group_id('global_moderators');
  1.2113 +
  1.2114 +	$user_ids = array();
  1.2115 +	while ($row = $db->sql_fetchrow($result))
  1.2116 +	{
  1.2117 +		$user_ids[] = $row['user_id'];
  1.2118 +	}
  1.2119 +	$db->sql_freeresult($result);
  1.2120 +
  1.2121 +	if (sizeof($user_ids))
  1.2122 +	{
  1.2123 +		$db->sql_query('UPDATE ' . USERS_TABLE . ' SET group_id = ' . get_group_id('global_moderators') . '
  1.2124 +			WHERE group_id = 0 AND ' . $db->sql_in_set('user_id', $user_ids));
  1.2125 +	}
  1.2126 +
  1.2127 +	// Set user colour
  1.2128 +	$sql = 'SELECT group_id, group_colour FROM ' . GROUPS_TABLE . "
  1.2129 +		WHERE group_colour <> ''";
  1.2130 +	$result = $db->sql_query($sql);
  1.2131 +
  1.2132 +	while ($row = $db->sql_fetchrow($result))
  1.2133 +	{
  1.2134 +		$db->sql_query('UPDATE ' . USERS_TABLE . " SET user_colour = '{$row['group_colour']}' WHERE group_id = {$row['group_id']}");
  1.2135 +	}
  1.2136 +	$db->sql_freeresult($result);
  1.2137 +}
  1.2138 +
  1.2139 +/**
  1.2140 +* Cleanly remove invalid user entries after converting the users table...
  1.2141 +*/
  1.2142 +function remove_invalid_users()
  1.2143 +{
  1.2144 +	global $convert, $db, $phpEx, $phpbb_root_path;
  1.2145 +
  1.2146 +	// username_clean is UNIQUE
  1.2147 +	$sql = 'SELECT user_id
  1.2148 +		FROM ' . USERS_TABLE . "
  1.2149 +		WHERE username_clean = ''";
  1.2150 +	$result = $db->sql_query($sql);
  1.2151 +	$row = $db->sql_fetchrow($result);
  1.2152 +	$db->sql_freeresult($result);
  1.2153 +
  1.2154 +	if ($row)
  1.2155 +	{
  1.2156 +		if (!function_exists('user_delete'))
  1.2157 +		{
  1.2158 +			include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  1.2159 +		}
  1.2160 +
  1.2161 +		user_delete('remove', $row['user_id']);
  1.2162 +	}
  1.2163 +}
  1.2164 +
  1.2165 +function convert_bbcode($message, $convert_size = true, $extended_bbcodes = false)
  1.2166 +{
  1.2167 +	static $orig, $repl, $origx, $replx, $str_from, $str_to;
  1.2168 +
  1.2169 +	if (empty($orig))
  1.2170 +	{
  1.2171 +		$orig = $repl = array();
  1.2172 +
  1.2173 +		$orig[] = '#\[(php|sql)\](.*?)\[/(php|sql)\]#is';
  1.2174 +		$repl[] = '[code]\2[/code]';
  1.2175 +
  1.2176 +		$orig[] = '#\[font=[^\]]+\](.*?)\[/font\]#is';
  1.2177 +		$repl[] = '\1';
  1.2178 +
  1.2179 +		$orig[] = '#\[align=[a-z]+\](.*?)\[/align\]#is';
  1.2180 +		$repl[] = '\1';
  1.2181 +
  1.2182 +		$orig[] = '#\[/list=.*?\]#is';
  1.2183 +		$repl[] = '[/list]';
  1.2184 +
  1.2185 +		$origx = array(
  1.2186 +			'#\[glow[^\]]+\](.*?)\[/glow\]#is',
  1.2187 +			'#\[shadow[^\]]+\](.*?)\[/shadow\]#is',
  1.2188 +			'#\[flash[^\]]+\](.*?)\[/flash\]#is'
  1.2189 +		);
  1.2190 +
  1.2191 +		$replx = array(
  1.2192 +			'\1',
  1.2193 +			'\1',
  1.2194 +			'[url=\1]Flash[/url]'
  1.2195 +		);
  1.2196 +
  1.2197 +		$str_from = array(
  1.2198 +			'[ftp]',	'[/ftp]',
  1.2199 +			'[ftp=',	'[/ftp]',
  1.2200 +			'[pre]',	'[/pre]',
  1.2201 +			'[table]',	'[/table]',
  1.2202 +			'[td]',		'[/td]',
  1.2203 +			'[tr]',		'[/tr]',
  1.2204 +			'[s]',		'[/s]',
  1.2205 +			'[left]',	'[/left]',
  1.2206 +			'[right]',	'[/right]',
  1.2207 +			'[center]',	'[/center]',
  1.2208 +			'[sub]',	'[/sub]',
  1.2209 +			'[sup]',	'[/sup]',
  1.2210 +			'[tt]',		'[/tt]',
  1.2211 +			'[move]',	'[/move]',
  1.2212 +			'[hr]'
  1.2213 +		);
  1.2214 +
  1.2215 +		$str_to = array(
  1.2216 +			'[url]',	'[/url]',
  1.2217 +			'[url=',	'[/url]',
  1.2218 +			'[code]',	'[/code]',
  1.2219 +			"\n",		'',
  1.2220 +			'',			'',
  1.2221 +			"\n",		'',
  1.2222 +			'',			'',
  1.2223 +			'',			'',
  1.2224 +			'',			'',
  1.2225 +			'',			'',
  1.2226 +			'',			'',
  1.2227 +			'',			'',
  1.2228 +			'',			'',
  1.2229 +			'',			'',
  1.2230 +			"\n\n"
  1.2231 +		);
  1.2232 +
  1.2233 +		for ($i = 0; $i < sizeof($str_from); ++$i)
  1.2234 +		{
  1.2235 +			$origx[] = '#\\' . str_replace(']', '\\]', $str_from[$i]) . '#is';
  1.2236 +			$replx[] = $str_to[$i];
  1.2237 +		}
  1.2238 +	}
  1.2239 +
  1.2240 +	if (preg_match_all('#\[email=([^\]]+)\](.*?)\[/email\]#i', $message, $m))
  1.2241 +	{
  1.2242 +		for ($i = 0; $i < sizeof($m[1]); ++$i)
  1.2243 +		{
  1.2244 +			if ($m[1][$i] == $m[2][$i])
  1.2245 +			{
  1.2246 +				$message = str_replace($m[0][$i], '[email]' . $m[1][$i] . '[/email]', $message);
  1.2247 +			}
  1.2248 +			else
  1.2249 +			{
  1.2250 +				$message = str_replace($m[0][$i], $m[2][$i] . ' ([email]' . $m[1][$i] . '[/email])', $message);
  1.2251 +			}
  1.2252 +		}
  1.2253 +	}
  1.2254 +
  1.2255 +	if ($convert_size && preg_match('#\[size=[0-9]+\].*?\[/size\]#i', $message))
  1.2256 +	{
  1.2257 +		$size = array(9, 9, 12, 15, 18, 24, 29, 29, 29, 29);
  1.2258 +		$message = preg_replace('#\[size=([0-9]+)\](.*?)\[/size\]#i', '[size=\1]\2[/size]', $message);
  1.2259 +		$message = preg_replace('#\[size=[0-9]{2,}\](.*?)\[/size\]#i', '[size=29]\1[/size]', $message);
  1.2260 +
  1.2261 +		for ($i = sizeof($size); $i; )
  1.2262 +		{
  1.2263 +			$i--;
  1.2264 +			$message = str_replace('[size=' . $i . ']', '[size=' . $size[$i] . ']', $message);
  1.2265 +		}
  1.2266 +	}
  1.2267 +
  1.2268 +	if ($extended_bbcodes)
  1.2269 +	{
  1.2270 +		$message = preg_replace($origx, $replx, $message);
  1.2271 +	}
  1.2272 +
  1.2273 +	$message = preg_replace($orig, $repl, $message);
  1.2274 +	return $message;
  1.2275 +}
  1.2276 +
  1.2277 +
  1.2278 +function copy_file($src, $trg, $overwrite = false, $die_on_failure = true, $source_relative_path = true)
  1.2279 +{
  1.2280 +	global $convert, $phpbb_root_path, $config, $user, $db;
  1.2281 +
  1.2282 +	if (substr($trg, -1) == '/')
  1.2283 +	{
  1.2284 +		$trg .= utf8_basename($src);
  1.2285 +	}
  1.2286 +	$src_path = relative_base($src, $source_relative_path, __LINE__, __FILE__);
  1.2287 +	$trg_path = $trg;
  1.2288 +
  1.2289 +	if (!$overwrite && @file_exists($trg_path))
  1.2290 +	{
  1.2291 +		return true;
  1.2292 +	}
  1.2293 +
  1.2294 +	if (!@file_exists($src_path))
  1.2295 +	{
  1.2296 +		return;
  1.2297 +	}
  1.2298 +
  1.2299 +	$path = $phpbb_root_path;
  1.2300 +	$parts = explode('/', $trg);
  1.2301 +	unset($parts[sizeof($parts) - 1]);
  1.2302 +
  1.2303 +	for ($i = 0; $i < sizeof($parts); ++$i)
  1.2304 +	{
  1.2305 +		$path .= $parts[$i] . '/';
  1.2306 +
  1.2307 +		if (!is_dir($path))
  1.2308 +		{
  1.2309 +			@mkdir($path, 0777);
  1.2310 +		}
  1.2311 +	}
  1.2312 +
  1.2313 +	if (!phpbb_is_writable($path))
  1.2314 +	{
  1.2315 +		@chmod($path, 0777);
  1.2316 +	}
  1.2317 +
  1.2318 +	if (!@copy($src_path, $phpbb_root_path . $trg_path))
  1.2319 +	{
  1.2320 +		$convert->p_master->error(sprintf($user->lang['COULD_NOT_COPY'], $src_path, $phpbb_root_path . $trg_path), __LINE__, __FILE__, !$die_on_failure);
  1.2321 +		return;
  1.2322 +	}
  1.2323 +
  1.2324 +	if ($perm = @fileperms($src_path))
  1.2325 +	{
  1.2326 +		@chmod($phpbb_root_path . $trg_path, $perm);
  1.2327 +	}
  1.2328 +
  1.2329 +	return true;
  1.2330 +}
  1.2331 +
  1.2332 +function copy_dir($src, $trg, $copy_subdirs = true, $overwrite = false, $die_on_failure = true, $source_relative_path = true)
  1.2333 +{
  1.2334 +	global $convert, $phpbb_root_path, $config, $user, $db;
  1.2335 +
  1.2336 +	$dirlist = $filelist = $bad_dirs = array();
  1.2337 +	$src = path($src, $source_relative_path);
  1.2338 +	$trg = path($trg);
  1.2339 +	$src_path = relative_base($src, $source_relative_path, __LINE__, __FILE__);
  1.2340 +	$trg_path = $phpbb_root_path . $trg;
  1.2341 +
  1.2342 +	if (!is_dir($trg_path))
  1.2343 +	{
  1.2344 +		@mkdir($trg_path, 0777);
  1.2345 +		@chmod($trg_path, 0777);
  1.2346 +	}
  1.2347 +
  1.2348 +	if (!phpbb_is_writable($trg_path))
  1.2349 +	{
  1.2350 +		$bad_dirs[] = path($config['script_path']) . $trg;
  1.2351 +	}
  1.2352 +
  1.2353 +	if ($handle = @opendir($src_path))
  1.2354 +	{
  1.2355 +		while ($entry = readdir($handle))
  1.2356 +		{
  1.2357 +			if ($entry[0] == '.' || $entry == 'CVS' || $entry == 'index.htm')
  1.2358 +			{
  1.2359 +				continue;
  1.2360 +			}
  1.2361 +
  1.2362 +			if (is_dir($src_path . $entry))
  1.2363 +			{
  1.2364 +				$dirlist[] = $entry;
  1.2365 +			}
  1.2366 +			else
  1.2367 +			{
  1.2368 +				$filelist[] = $entry;
  1.2369 +			}
  1.2370 +		}
  1.2371 +		closedir($handle);
  1.2372 +	}
  1.2373 +	else if ($dir = @dir($src_path))
  1.2374 +	{
  1.2375 +		while ($entry = $dir->read())
  1.2376 +		{
  1.2377 +			if ($entry[0] == '.' || $entry == 'CVS' || $entry == 'index.htm')
  1.2378 +			{
  1.2379 +				continue;
  1.2380 +			}
  1.2381 +
  1.2382 +			if (is_dir($src_path . $entry))
  1.2383 +			{
  1.2384 +				$dirlist[] = $entry;
  1.2385 +			}
  1.2386 +			else
  1.2387 +			{
  1.2388 +				$filelist[] = $entry;
  1.2389 +			}
  1.2390 +		}
  1.2391 +		$dir->close();
  1.2392 +	}
  1.2393 +	else
  1.2394 +	{
  1.2395 +		$convert->p_master->error(sprintf($user->lang['CONV_ERROR_COULD_NOT_READ'], relative_base($src, $source_relative_path)), __LINE__, __FILE__);
  1.2396 +	}
  1.2397 +
  1.2398 +	if ($copy_subdirs)
  1.2399 +	{
  1.2400 +		for ($i = 0; $i < sizeof($dirlist); ++$i)
  1.2401 +		{
  1.2402 +			$dir = $dirlist[$i];
  1.2403 +
  1.2404 +			if ($dir == 'CVS')
  1.2405 +			{
  1.2406 +				continue;
  1.2407 +			}
  1.2408 +
  1.2409 +			if (!is_dir($trg_path . $dir))
  1.2410 +			{
  1.2411 +				@mkdir($trg_path . $dir, 0777);
  1.2412 +				@chmod($trg_path . $dir, 0777);
  1.2413 +			}
  1.2414 +
  1.2415 +			if (!phpbb_is_writable($trg_path . $dir))
  1.2416 +			{
  1.2417 +				$bad_dirs[] = $trg . $dir;
  1.2418 +				$bad_dirs[] = $trg_path . $dir;
  1.2419 +			}
  1.2420 +
  1.2421 +			if (!sizeof($bad_dirs))
  1.2422 +			{
  1.2423 +				copy_dir($src . $dir, $trg . $dir, true, $overwrite, $die_on_failure, $source_relative_path);
  1.2424 +			}
  1.2425 +		}
  1.2426 +	}
  1.2427 +
  1.2428 +	if (sizeof($bad_dirs))
  1.2429 +	{
  1.2430 +		$str = (sizeof($bad_dirs) == 1) ? $user->lang['MAKE_FOLDER_WRITABLE'] : $user->lang['MAKE_FOLDERS_WRITABLE'];
  1.2431 +		sort($bad_dirs);
  1.2432 +		$convert->p_master->error(sprintf($str, implode('<br />', $bad_dirs)), __LINE__, __FILE__);
  1.2433 +	}
  1.2434 +
  1.2435 +	for ($i = 0; $i < sizeof($filelist); ++$i)
  1.2436 +	{
  1.2437 +		copy_file($src . $filelist[$i], $trg . $filelist[$i], $overwrite, $die_on_failure, $source_relative_path);
  1.2438 +	}
  1.2439 +}
  1.2440 +
  1.2441 +function relative_base($path, $is_relative = true, $line = false, $file = false)
  1.2442 +{
  1.2443 +	global $convert, $phpbb_root_path, $config, $user, $db;
  1.2444 +
  1.2445 +	if (!$is_relative)
  1.2446 +	{
  1.2447 +		return $path;
  1.2448 +	}
  1.2449 +
  1.2450 +	if (empty($convert->options['forum_path']) && $is_relative)
  1.2451 +	{
  1.2452 +		$line = $line ? $line : __LINE__;
  1.2453 +		$file = $file ? $file : __FILE__;
  1.2454 +
  1.2455 +		$convert->p_master->error($user->lang['CONV_ERROR_NO_FORUM_PATH'], $line, $file);
  1.2456 +	}
  1.2457 +
  1.2458 +	return $convert->options['forum_path'] . '/' . $path;
  1.2459 +}
  1.2460 +
  1.2461 +function get_smiley_display()
  1.2462 +{
  1.2463 +	static $smiley_count = 0;
  1.2464 +	$smiley_count++;
  1.2465 +	return ($smiley_count < 50) ? 1 : 0;
  1.2466 +}
  1.2467 +
  1.2468 +
  1.2469 +function fill_dateformat($user_dateformat)
  1.2470 +{
  1.2471 +	global $config;
  1.2472 +
  1.2473 +	return ((empty($user_dateformat)) ? $config['default_dateformat'] : $user_dateformat);
  1.2474 +}
  1.2475 +
  1.2476 +
  1.2477 +
  1.2478 +?>
  1.2479 \ No newline at end of file