includes/acp/acp_styles.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/acp/acp_styles.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,3965 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package acp
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 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 +* @package acp
    1.24 +*/
    1.25 +class acp_styles
    1.26 +{
    1.27 +	var $u_action;
    1.28 +
    1.29 +	var $style_cfg;
    1.30 +	var $template_cfg;
    1.31 +	var $theme_cfg;
    1.32 +	var $imageset_cfg;
    1.33 +	var $imageset_keys;
    1.34 +
    1.35 +	function main($id, $mode)
    1.36 +	{
    1.37 +		global $db, $user, $auth, $template, $cache;
    1.38 +		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
    1.39 +
    1.40 +		// Hardcoded template bitfield to add for new templates
    1.41 +		$bitfield = new bitfield();
    1.42 +		$bitfield->set(0);
    1.43 +		$bitfield->set(1);
    1.44 +		$bitfield->set(2);
    1.45 +		$bitfield->set(3);
    1.46 +		$bitfield->set(4);
    1.47 +		$bitfield->set(8);
    1.48 +		$bitfield->set(9);
    1.49 +		$bitfield->set(11);
    1.50 +		$bitfield->set(12);
    1.51 +		define('TEMPLATE_BITFIELD', $bitfield->get_base64());
    1.52 +		unset($bitfield);
    1.53 +
    1.54 +		$user->add_lang('acp/styles');
    1.55 +
    1.56 +		$this->tpl_name = 'acp_styles';
    1.57 +		$this->page_title = 'ACP_CAT_STYLES';
    1.58 +
    1.59 +		$action = request_var('action', '');
    1.60 +		$action = (isset($_POST['add'])) ? 'add' : $action;
    1.61 +		$style_id = request_var('id', 0);
    1.62 +
    1.63 +		// Fill the configuration variables
    1.64 +		$this->style_cfg = $this->template_cfg = $this->theme_cfg = $this->imageset_cfg = '
    1.65 +#
    1.66 +# phpBB {MODE} configuration file
    1.67 +#
    1.68 +# @package phpBB3
    1.69 +# @copyright (c) 2005 phpBB Group
    1.70 +# @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.71 +#
    1.72 +#
    1.73 +# At the left is the name, please do not change this
    1.74 +# At the right the value is entered
    1.75 +# For on/off options the valid values are on, off, 1, 0, true and false
    1.76 +#
    1.77 +# Values get trimmed, if you want to add a space in front or at the end of
    1.78 +# the value, then enclose the value with single or double quotes.
    1.79 +# Single and double quotes do not need to be escaped.
    1.80 +#
    1.81 +#
    1.82 +
    1.83 +# General Information about this {MODE}
    1.84 +name = {NAME}
    1.85 +copyright = {COPYRIGHT}
    1.86 +version = {VERSION}
    1.87 +';
    1.88 +
    1.89 +		$this->theme_cfg .= '
    1.90 +# Some configuration options
    1.91 +
    1.92 +#
    1.93 +# You have to turn this option on if you want to use the
    1.94 +# path template variables ({T_IMAGESET_PATH} for example) within
    1.95 +# your css file.
    1.96 +# This is mostly the case if you want to use language specific
    1.97 +# images within your css file.
    1.98 +#
    1.99 +parse_css_file = {PARSE_CSS_FILE}
   1.100 +';
   1.101 +
   1.102 +		$this->template_cfg .= '
   1.103 +# Some configuration options
   1.104 +
   1.105 +#
   1.106 +# You can use this function to inherit templates from another template.
   1.107 +# The template of the given name has to be installed.
   1.108 +# Templates cannot inherit from inheriting templates.
   1.109 +#';
   1.110 +
   1.111 +		$this->imageset_keys = array(
   1.112 +			'logos' => array(
   1.113 +				'site_logo',
   1.114 +			),
   1.115 +			'buttons'	=> array(
   1.116 +				'icon_back_top', 'icon_contact_aim', 'icon_contact_email', 'icon_contact_icq', 'icon_contact_jabber', 'icon_contact_msnm', 'icon_contact_pm', 'icon_contact_yahoo', 'icon_contact_www', 'icon_post_delete', 'icon_post_edit', 'icon_post_info', 'icon_post_quote', 'icon_post_report', 'icon_user_online', 'icon_user_offline', 'icon_user_profile', 'icon_user_search', 'icon_user_warn', 'button_pm_forward', 'button_pm_new', 'button_pm_reply', 'button_topic_locked', 'button_topic_new', 'button_topic_reply',
   1.117 +			),
   1.118 +			'icons'		=> array(
   1.119 +				'icon_post_target', 'icon_post_target_unread', 'icon_topic_attach', 'icon_topic_latest', 'icon_topic_newest', 'icon_topic_reported', 'icon_topic_unapproved', 'icon_friend', 'icon_foe',
   1.120 +			),
   1.121 +			'forums'	=> array(
   1.122 +				'forum_link', 'forum_read', 'forum_read_locked', 'forum_read_subforum', 'forum_unread', 'forum_unread_locked', 'forum_unread_subforum', 'subforum_read', 'subforum_unread'
   1.123 +			),
   1.124 +			'folders'	=> array(
   1.125 +				'topic_moved', 'topic_read', 'topic_read_mine', 'topic_read_hot', 'topic_read_hot_mine', 'topic_read_locked', 'topic_read_locked_mine', 'topic_unread', 'topic_unread_mine', 'topic_unread_hot', 'topic_unread_hot_mine', 'topic_unread_locked', 'topic_unread_locked_mine', 'sticky_read', 'sticky_read_mine', 'sticky_read_locked', 'sticky_read_locked_mine', 'sticky_unread', 'sticky_unread_mine', 'sticky_unread_locked', 'sticky_unread_locked_mine', 'announce_read', 'announce_read_mine', 'announce_read_locked', 'announce_read_locked_mine', 'announce_unread', 'announce_unread_mine', 'announce_unread_locked', 'announce_unread_locked_mine', 'global_read', 'global_read_mine', 'global_read_locked', 'global_read_locked_mine', 'global_unread', 'global_unread_mine', 'global_unread_locked', 'global_unread_locked_mine', 'pm_read', 'pm_unread',
   1.126 +			),
   1.127 +			'polls'		=> array(
   1.128 +				'poll_left', 'poll_center', 'poll_right',
   1.129 +			),
   1.130 +			'ui'		=> array(
   1.131 +				'upload_bar',
   1.132 +			),
   1.133 +			'user'		=> array(
   1.134 +				'user_icon1', 'user_icon2', 'user_icon3', 'user_icon4', 'user_icon5', 'user_icon6', 'user_icon7', 'user_icon8', 'user_icon9', 'user_icon10',
   1.135 +			),
   1.136 +		);
   1.137 +
   1.138 +		// Execute overall actions
   1.139 +		switch ($action)
   1.140 +		{
   1.141 +			case 'delete':
   1.142 +				if ($style_id)
   1.143 +				{
   1.144 +					$this->remove($mode, $style_id);
   1.145 +					return;
   1.146 +				}
   1.147 +			break;
   1.148 +
   1.149 +			case 'export':
   1.150 +				if ($style_id)
   1.151 +				{
   1.152 +					$this->export($mode, $style_id);
   1.153 +					return;
   1.154 +				}
   1.155 +			break;
   1.156 +
   1.157 +			case 'install':
   1.158 +				$this->install($mode);
   1.159 +				return;
   1.160 +			break;
   1.161 +
   1.162 +			case 'add':
   1.163 +				$this->add($mode);
   1.164 +				return;
   1.165 +			break;
   1.166 +
   1.167 +			case 'details':
   1.168 +				if ($style_id)
   1.169 +				{
   1.170 +					$this->details($mode, $style_id);
   1.171 +					return;
   1.172 +				}
   1.173 +			break;
   1.174 +
   1.175 +			case 'edit':
   1.176 +				if ($style_id)
   1.177 +				{
   1.178 +					switch ($mode)
   1.179 +					{
   1.180 +						case 'imageset':
   1.181 +							return $this->edit_imageset($style_id);
   1.182 +						case 'template':
   1.183 +							return $this->edit_template($style_id);
   1.184 +						case 'theme':
   1.185 +							return $this->edit_theme($style_id);
   1.186 +					}
   1.187 +				}
   1.188 +			break;
   1.189 +
   1.190 +			case 'cache':
   1.191 +				if ($style_id)
   1.192 +				{
   1.193 +					switch ($mode)
   1.194 +					{
   1.195 +						case 'template':
   1.196 +							return $this->template_cache($style_id);
   1.197 +					}
   1.198 +				}
   1.199 +			break;
   1.200 +		}
   1.201 +
   1.202 +		switch ($mode)
   1.203 +		{
   1.204 +			case 'style':
   1.205 +
   1.206 +				switch ($action)
   1.207 +				{
   1.208 +					case 'activate':
   1.209 +					case 'deactivate':
   1.210 +
   1.211 +						if ($style_id == $config['default_style'])
   1.212 +						{
   1.213 +							trigger_error($user->lang['DEACTIVATE_DEFAULT'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.214 +						}
   1.215 +
   1.216 +						if (($action == 'deactivate' && confirm_box(true)) || $action == 'activate')
   1.217 +						{
   1.218 +							$sql = 'UPDATE ' . STYLES_TABLE . '
   1.219 +								SET style_active = ' . (($action == 'activate') ? 1 : 0) . '
   1.220 +								WHERE style_id = ' . $style_id;
   1.221 +							$db->sql_query($sql);
   1.222 +
   1.223 +							// Set style to default for any member using deactivated style
   1.224 +							if ($action == 'deactivate')
   1.225 +							{
   1.226 +								$sql = 'UPDATE ' . USERS_TABLE . '
   1.227 +									SET user_style = ' . $config['default_style'] . "
   1.228 +									WHERE user_style = $style_id";
   1.229 +								$db->sql_query($sql);
   1.230 +
   1.231 +								$sql = 'UPDATE ' . FORUMS_TABLE . '
   1.232 +									SET forum_style = 0
   1.233 +									WHERE forum_style = ' . $style_id;
   1.234 +								$db->sql_query($sql);
   1.235 +							}
   1.236 +						}
   1.237 +						else if ($action == 'deactivate')
   1.238 +						{
   1.239 +							$s_hidden_fields = array(
   1.240 +								'i'			=> $id,
   1.241 +								'mode'		=> $mode,
   1.242 +								'action'	=> $action,
   1.243 +								'style_id'	=> $style_id,
   1.244 +							);
   1.245 +							confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields($s_hidden_fields));
   1.246 +						}
   1.247 +					break;
   1.248 +				}
   1.249 +
   1.250 +				$this->frontend('style', array('details'), array('export', 'delete'));
   1.251 +			break;
   1.252 +
   1.253 +			case 'template':
   1.254 +
   1.255 +				switch ($action)
   1.256 +				{
   1.257 +					// Refresh template data stored in db and clear cache
   1.258 +					case 'refresh':
   1.259 +
   1.260 +						$sql = 'SELECT *
   1.261 +							FROM ' . STYLES_TEMPLATE_TABLE . "
   1.262 +							WHERE template_id = $style_id";
   1.263 +						$result = $db->sql_query($sql);
   1.264 +						$template_row = $db->sql_fetchrow($result);
   1.265 +						$db->sql_freeresult($result);
   1.266 +
   1.267 +						if (!$template_row)
   1.268 +						{
   1.269 +							trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.270 +						}
   1.271 +
   1.272 +						if (confirm_box(true))
   1.273 +						{
   1.274 +							$template_refreshed = '';
   1.275 +
   1.276 +							// Only refresh database if the template is stored in the database
   1.277 +							if ($template_row['template_storedb'] && file_exists("{$phpbb_root_path}styles/{$template_row['template_path']}/template/"))
   1.278 +							{
   1.279 +								$filelist = array('' => array());
   1.280 +
   1.281 +								$sql = 'SELECT template_filename, template_mtime
   1.282 +									FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
   1.283 +									WHERE template_id = $style_id";
   1.284 +								$result = $db->sql_query($sql);
   1.285 +
   1.286 +								while ($row = $db->sql_fetchrow($result))
   1.287 +								{
   1.288 +//									if (@filemtime("{$phpbb_root_path}styles/{$template_row['template_path']}/template/" . $row['template_filename']) > $row['template_mtime'])
   1.289 +//									{
   1.290 +										// get folder info from the filename
   1.291 +										if (($slash_pos = strrpos($row['template_filename'], '/')) === false)
   1.292 +										{
   1.293 +											$filelist[''][] = $row['template_filename'];
   1.294 +										}
   1.295 +										else
   1.296 +										{
   1.297 +											$filelist[substr($row['template_filename'], 0, $slash_pos + 1)][] = substr($row['template_filename'], $slash_pos + 1, strlen($row['template_filename']) - $slash_pos - 1);
   1.298 +										}
   1.299 +//									}
   1.300 +								}
   1.301 +								$db->sql_freeresult($result);
   1.302 +
   1.303 +								$this->store_templates('update', $style_id, $template_row['template_path'], $filelist);
   1.304 +								unset($filelist);
   1.305 +
   1.306 +								$template_refreshed = $user->lang['TEMPLATE_REFRESHED'] . '<br />';
   1.307 +								add_log('admin', 'LOG_TEMPLATE_REFRESHED', $template_row['template_name']);
   1.308 +							}
   1.309 +
   1.310 +							$this->clear_template_cache($template_row);
   1.311 +
   1.312 +							trigger_error($template_refreshed . $user->lang['TEMPLATE_CACHE_CLEARED'] . adm_back_link($this->u_action));
   1.313 +						}
   1.314 +						else
   1.315 +						{
   1.316 +							confirm_box(false, ($template_row['template_storedb']) ? $user->lang['CONFIRM_TEMPLATE_REFRESH'] : $user->lang['CONFIRM_TEMPLATE_CLEAR_CACHE'], build_hidden_fields(array(
   1.317 +								'i'			=> $id,
   1.318 +								'mode'		=> $mode,
   1.319 +								'action'	=> $action,
   1.320 +								'id'		=> $style_id
   1.321 +							)));
   1.322 +						}
   1.323 +
   1.324 +					break;
   1.325 +				}
   1.326 +
   1.327 +				$this->frontend('template', array('edit', 'cache', 'details'), array('refresh', 'export', 'delete'));
   1.328 +			break;
   1.329 +
   1.330 +			case 'theme':
   1.331 +
   1.332 +				switch ($action)
   1.333 +				{
   1.334 +					// Refresh theme data stored in the database
   1.335 +					case 'refresh':
   1.336 +
   1.337 +						$sql = 'SELECT *
   1.338 +							FROM ' . STYLES_THEME_TABLE . "
   1.339 +							WHERE theme_id = $style_id";
   1.340 +						$result = $db->sql_query($sql);
   1.341 +						$theme_row = $db->sql_fetchrow($result);
   1.342 +						$db->sql_freeresult($result);
   1.343 +
   1.344 +						if (!$theme_row)
   1.345 +						{
   1.346 +							trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.347 +						}
   1.348 +
   1.349 +						if (!$theme_row['theme_storedb'])
   1.350 +						{
   1.351 +							trigger_error($user->lang['THEME_ERR_REFRESH_FS'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.352 +						}
   1.353 +
   1.354 +						if (confirm_box(true))
   1.355 +						{
   1.356 +							if ($theme_row['theme_storedb'] && file_exists("{$phpbb_root_path}styles/{$theme_row['theme_path']}/theme/stylesheet.css"))
   1.357 +							{
   1.358 +								// Save CSS contents
   1.359 +								$sql_ary = array(
   1.360 +									'theme_mtime'	=> (int) filemtime("{$phpbb_root_path}styles/{$theme_row['theme_path']}/theme/stylesheet.css"),
   1.361 +									'theme_data'	=> $this->db_theme_data($theme_row)
   1.362 +								);
   1.363 +
   1.364 +								$sql = 'UPDATE ' . STYLES_THEME_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
   1.365 +									WHERE theme_id = $style_id";
   1.366 +								$db->sql_query($sql);
   1.367 +
   1.368 +								$cache->destroy('sql', STYLES_THEME_TABLE);
   1.369 +
   1.370 +								add_log('admin', 'LOG_THEME_REFRESHED', $theme_row['theme_name']);
   1.371 +								trigger_error($user->lang['THEME_REFRESHED'] . adm_back_link($this->u_action));
   1.372 +							}
   1.373 +						}
   1.374 +						else
   1.375 +						{
   1.376 +							confirm_box(false, $user->lang['CONFIRM_THEME_REFRESH'], build_hidden_fields(array(
   1.377 +								'i'			=> $id,
   1.378 +								'mode'		=> $mode,
   1.379 +								'action'	=> $action,
   1.380 +								'id'		=> $style_id
   1.381 +							)));
   1.382 +						}
   1.383 +					break;
   1.384 +				}
   1.385 +
   1.386 +				$this->frontend('theme', array('edit', 'details'), array('refresh', 'export', 'delete'));
   1.387 +			break;
   1.388 +
   1.389 +			case 'imageset':
   1.390 +
   1.391 +				switch ($action)
   1.392 +				{
   1.393 +					case 'refresh':
   1.394 +
   1.395 +						$sql = 'SELECT *
   1.396 +							FROM ' . STYLES_IMAGESET_TABLE . "
   1.397 +							WHERE imageset_id = $style_id";
   1.398 +						$result = $db->sql_query($sql);
   1.399 +						$imageset_row = $db->sql_fetchrow($result);
   1.400 +						$db->sql_freeresult($result);
   1.401 +
   1.402 +						if (!$imageset_row)
   1.403 +						{
   1.404 +							trigger_error($user->lang['NO_IMAGESET'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.405 +						}
   1.406 +
   1.407 +						if (confirm_box(true))
   1.408 +						{
   1.409 +							$sql_ary = array();
   1.410 +
   1.411 +							$imageset_definitions = array();
   1.412 +							foreach ($this->imageset_keys as $topic => $key_array)
   1.413 +							{
   1.414 +								$imageset_definitions = array_merge($imageset_definitions, $key_array);
   1.415 +							}
   1.416 +
   1.417 +							$cfg_data_imageset = parse_cfg_file("{$phpbb_root_path}styles/{$imageset_row['imageset_path']}/imageset/imageset.cfg");
   1.418 +
   1.419 +							$db->sql_transaction('begin');
   1.420 +
   1.421 +							$sql = 'DELETE FROM ' . STYLES_IMAGESET_DATA_TABLE . '
   1.422 +								WHERE imageset_id = ' . $style_id;
   1.423 +							$result = $db->sql_query($sql);
   1.424 +
   1.425 +							foreach ($cfg_data_imageset as $image_name => $value)
   1.426 +							{
   1.427 +								if (strpos($value, '*') !== false)
   1.428 +								{
   1.429 +									if (substr($value, -1, 1) === '*')
   1.430 +									{
   1.431 +										list($image_filename, $image_height) = explode('*', $value);
   1.432 +										$image_width = 0;
   1.433 +									}
   1.434 +									else
   1.435 +									{
   1.436 +										list($image_filename, $image_height, $image_width) = explode('*', $value);
   1.437 +									}
   1.438 +								}
   1.439 +								else
   1.440 +								{
   1.441 +									$image_filename = $value;
   1.442 +									$image_height = $image_width = 0;
   1.443 +								}
   1.444 +
   1.445 +								if (strpos($image_name, 'img_') === 0 && $image_filename)
   1.446 +								{
   1.447 +									$image_name = substr($image_name, 4);
   1.448 +									if (in_array($image_name, $imageset_definitions))
   1.449 +									{
   1.450 +										$sql_ary[] = array(
   1.451 +											'image_name'		=> (string) $image_name,
   1.452 +											'image_filename'	=> (string) $image_filename,
   1.453 +											'image_height'		=> (int) $image_height,
   1.454 +											'image_width'		=> (int) $image_width,
   1.455 +											'imageset_id'		=> (int) $style_id,
   1.456 +											'image_lang'		=> '',
   1.457 +										);
   1.458 +									}
   1.459 +								}
   1.460 +							}
   1.461 +
   1.462 +							$sql = 'SELECT lang_dir
   1.463 +								FROM ' . LANG_TABLE;
   1.464 +							$result = $db->sql_query($sql);
   1.465 +
   1.466 +							while ($row = $db->sql_fetchrow($result))
   1.467 +							{
   1.468 +								if (@file_exists("{$phpbb_root_path}styles/{$imageset_row['imageset_path']}/imageset/{$row['lang_dir']}/imageset.cfg"))
   1.469 +								{
   1.470 +									$cfg_data_imageset_data = parse_cfg_file("{$phpbb_root_path}styles/{$imageset_row['imageset_path']}/imageset/{$row['lang_dir']}/imageset.cfg");
   1.471 +									foreach ($cfg_data_imageset_data as $image_name => $value)
   1.472 +									{
   1.473 +										if (strpos($value, '*') !== false)
   1.474 +										{
   1.475 +											if (substr($value, -1, 1) === '*')
   1.476 +											{
   1.477 +												list($image_filename, $image_height) = explode('*', $value);
   1.478 +												$image_width = 0;
   1.479 +											}
   1.480 +											else
   1.481 +											{
   1.482 +												list($image_filename, $image_height, $image_width) = explode('*', $value);
   1.483 +											}
   1.484 +										}
   1.485 +										else
   1.486 +										{
   1.487 +											$image_filename = $value;
   1.488 +											$image_height = $image_width = 0;
   1.489 +										}
   1.490 +
   1.491 +										if (strpos($image_name, 'img_') === 0 && $image_filename)
   1.492 +										{
   1.493 +											$image_name = substr($image_name, 4);
   1.494 +											if (in_array($image_name, $imageset_definitions))
   1.495 +											{
   1.496 +												$sql_ary[] = array(
   1.497 +													'image_name'		=> (string) $image_name,
   1.498 +													'image_filename'	=> (string) $image_filename,
   1.499 +													'image_height'		=> (int) $image_height,
   1.500 +													'image_width'		=> (int) $image_width,
   1.501 +													'imageset_id'		=> (int) $style_id,
   1.502 +													'image_lang'		=> (string) $row['lang_dir'],
   1.503 +												);
   1.504 +											}
   1.505 +										}
   1.506 +									}
   1.507 +								}
   1.508 +							}
   1.509 +							$db->sql_freeresult($result);
   1.510 +
   1.511 +							$db->sql_multi_insert(STYLES_IMAGESET_DATA_TABLE, $sql_ary);
   1.512 +
   1.513 +							$db->sql_transaction('commit');
   1.514 +
   1.515 +							$cache->destroy('sql', STYLES_IMAGESET_DATA_TABLE);
   1.516 +							$cache->destroy('imageset_site_logo_md5');
   1.517 +
   1.518 +							add_log('admin', 'LOG_IMAGESET_REFRESHED', $imageset_row['imageset_name']);
   1.519 +							trigger_error($user->lang['IMAGESET_REFRESHED'] . adm_back_link($this->u_action));
   1.520 +						}
   1.521 +						else
   1.522 +						{
   1.523 +							confirm_box(false, $user->lang['CONFIRM_IMAGESET_REFRESH'], build_hidden_fields(array(
   1.524 +								'i'			=> $id,
   1.525 +								'mode'		=> $mode,
   1.526 +								'action'	=> $action,
   1.527 +								'id'		=> $style_id
   1.528 +							)));
   1.529 +						}
   1.530 +					break;
   1.531 +				}
   1.532 +
   1.533 +				$this->frontend('imageset', array('edit', 'details'), array('refresh', 'export', 'delete'));
   1.534 +			break;
   1.535 +		}
   1.536 +	}
   1.537 +
   1.538 +	/**
   1.539 +	* Build Frontend with supplied options
   1.540 +	*/
   1.541 +	function frontend($mode, $options, $actions)
   1.542 +	{
   1.543 +		global $user, $template, $db, $config, $phpbb_root_path, $phpEx;
   1.544 +
   1.545 +		$sql_from = '';
   1.546 +		$style_count = array();
   1.547 +
   1.548 +		switch ($mode)
   1.549 +		{
   1.550 +			case 'style':
   1.551 +				$sql_from = STYLES_TABLE;
   1.552 +
   1.553 +				$sql = 'SELECT user_style, COUNT(user_style) AS style_count
   1.554 +					FROM ' . USERS_TABLE . '
   1.555 +					GROUP BY user_style';
   1.556 +				$result = $db->sql_query($sql);
   1.557 +
   1.558 +				while ($row = $db->sql_fetchrow($result))
   1.559 +				{
   1.560 +					$style_count[$row['user_style']] = $row['style_count'];
   1.561 +				}
   1.562 +				$db->sql_freeresult($result);
   1.563 +
   1.564 +			break;
   1.565 +
   1.566 +			case 'template':
   1.567 +				$sql_from = STYLES_TEMPLATE_TABLE;
   1.568 +			break;
   1.569 +
   1.570 +			case 'theme':
   1.571 +				$sql_from = STYLES_THEME_TABLE;
   1.572 +			break;
   1.573 +
   1.574 +			case 'imageset':
   1.575 +				$sql_from = STYLES_IMAGESET_TABLE;
   1.576 +			break;
   1.577 +		}
   1.578 +
   1.579 +		$l_prefix = strtoupper($mode);
   1.580 +
   1.581 +		$this->page_title = 'ACP_' . $l_prefix . 'S';
   1.582 +
   1.583 +		$template->assign_vars(array(
   1.584 +			'S_FRONTEND'		=> true,
   1.585 +			'S_STYLE'			=> ($mode == 'style') ? true : false,
   1.586 +
   1.587 +			'L_TITLE'			=> $user->lang[$this->page_title],
   1.588 +			'L_EXPLAIN'			=> $user->lang[$this->page_title . '_EXPLAIN'],
   1.589 +			'L_NAME'			=> $user->lang[$l_prefix . '_NAME'],
   1.590 +			'L_INSTALLED'		=> $user->lang['INSTALLED_' . $l_prefix],
   1.591 +			'L_UNINSTALLED'		=> $user->lang['UNINSTALLED_' . $l_prefix],
   1.592 +			'L_NO_UNINSTALLED'	=> $user->lang['NO_UNINSTALLED_' . $l_prefix],
   1.593 +			'L_CREATE'			=> $user->lang['CREATE_' . $l_prefix],
   1.594 +
   1.595 +			'U_ACTION'			=> $this->u_action,
   1.596 +			)
   1.597 +		);
   1.598 +
   1.599 +		$sql = "SELECT *
   1.600 +			FROM $sql_from";
   1.601 +		$result = $db->sql_query($sql);
   1.602 +
   1.603 +		$installed = array();
   1.604 +
   1.605 +		$basis_options = '<option class="sep" value="">' . $user->lang['OPTIONAL_BASIS'] . '</option>';
   1.606 +		while ($row = $db->sql_fetchrow($result))
   1.607 +		{
   1.608 +			$installed[] = $row[$mode . '_name'];
   1.609 +			$basis_options .= '<option value="' . $row[$mode . '_id'] . '">' . $row[$mode . '_name'] . '</option>';
   1.610 +
   1.611 +			$stylevis = ($mode == 'style' && !$row['style_active']) ? 'activate' : 'deactivate';
   1.612 +
   1.613 +			$s_options = array();
   1.614 +			foreach ($options as $option)
   1.615 +			{
   1.616 +				$s_options[] = '<a href="' . $this->u_action . "&amp;action=$option&amp;id=" . $row[$mode . '_id'] . '">' . $user->lang[strtoupper($option)] . '</a>';
   1.617 +			}
   1.618 +
   1.619 +			$s_actions = array();
   1.620 +			foreach ($actions as $option)
   1.621 +			{
   1.622 +				$s_actions[] = '<a href="' . $this->u_action . "&amp;action=$option&amp;id=" . $row[$mode . '_id'] . '">' . $user->lang[strtoupper($option)] . '</a>';
   1.623 +			}
   1.624 +
   1.625 +			$template->assign_block_vars('installed', array(
   1.626 +				'S_DEFAULT_STYLE'		=> ($mode == 'style' && $row['style_id'] == $config['default_style']) ? true : false,
   1.627 +				'U_EDIT'				=> $this->u_action . '&amp;action=' . (($mode == 'style') ? 'details' : 'edit') . '&amp;id=' . $row[$mode . '_id'],
   1.628 +				'U_STYLE_ACT_DEACT'		=> $this->u_action . '&amp;action=' . $stylevis . '&amp;id=' . $row[$mode . '_id'],
   1.629 +				'L_STYLE_ACT_DEACT'		=> $user->lang['STYLE_' . strtoupper($stylevis)],
   1.630 +				'S_OPTIONS'				=> implode(' | ', $s_options),
   1.631 +				'S_ACTIONS'				=> implode(' | ', $s_actions),
   1.632 +				'U_PREVIEW'				=> ($mode == 'style') ? append_sid("{$phpbb_root_path}index.$phpEx", "$mode=" . $row[$mode . '_id']) : '',
   1.633 +
   1.634 +				'NAME'					=> $row[$mode . '_name'],
   1.635 +				'STYLE_COUNT'			=> ($mode == 'style' && isset($style_count[$row['style_id']])) ? $style_count[$row['style_id']] : 0,
   1.636 +				)
   1.637 +			);
   1.638 +		}
   1.639 +		$db->sql_freeresult($result);
   1.640 +
   1.641 +		// Grab uninstalled items
   1.642 +		$new_ary = $cfg = array();
   1.643 +
   1.644 +		$dp = @opendir("{$phpbb_root_path}styles");
   1.645 +
   1.646 +		if ($dp)
   1.647 +		{
   1.648 +			while (($file = readdir($dp)) !== false)
   1.649 +			{
   1.650 +				if ($file[0] == '.' || !is_dir($phpbb_root_path . 'styles/' . $file))
   1.651 +				{
   1.652 +					continue;
   1.653 +				}
   1.654 +
   1.655 +				$subpath = ($mode != 'style') ? "$mode/" : '';
   1.656 +				if (file_exists("{$phpbb_root_path}styles/$file/$subpath$mode.cfg"))
   1.657 +				{
   1.658 +					if ($cfg = file("{$phpbb_root_path}styles/$file/$subpath$mode.cfg"))
   1.659 +					{
   1.660 +						$items = parse_cfg_file('', $cfg);
   1.661 +						$name = (isset($items['name'])) ? trim($items['name']) : false;
   1.662 +
   1.663 +						if ($name && !in_array($name, $installed))
   1.664 +						{
   1.665 +							$new_ary[] = array(
   1.666 +								'path'		=> $file,
   1.667 +								'name'		=> $name,
   1.668 +								'copyright'	=> $items['copyright'],
   1.669 +							);
   1.670 +						}
   1.671 +					}
   1.672 +				}
   1.673 +			}
   1.674 +			closedir($dp);
   1.675 +		}
   1.676 +
   1.677 +		unset($installed);
   1.678 +
   1.679 +		if (sizeof($new_ary))
   1.680 +		{
   1.681 +			foreach ($new_ary as $cfg)
   1.682 +			{
   1.683 +				$template->assign_block_vars('uninstalled', array(
   1.684 +					'NAME'			=> $cfg['name'],
   1.685 +					'COPYRIGHT'		=> $cfg['copyright'],
   1.686 +					'U_INSTALL'		=> $this->u_action . '&amp;action=install&amp;path=' . urlencode($cfg['path']))
   1.687 +				);
   1.688 +			}
   1.689 +		}
   1.690 +		unset($new_ary);
   1.691 +
   1.692 +		$template->assign_vars(array(
   1.693 +			'S_BASIS_OPTIONS'		=> $basis_options)
   1.694 +		);
   1.695 +
   1.696 +	}
   1.697 +
   1.698 +	/**
   1.699 +	* Provides a template editor which allows saving changes to template files on the filesystem or in the database.
   1.700 +	*
   1.701 +	* @param int $template_id specifies which template set is being edited
   1.702 +	*/
   1.703 +	function edit_template($template_id)
   1.704 +	{
   1.705 +		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template, $safe_mode;
   1.706 +
   1.707 +		if (defined('PHPBB_DISABLE_ACP_EDITOR'))
   1.708 +		{
   1.709 +			trigger_error($user->lang['EDITOR_DISABLED'] . adm_back_link($this->u_action));
   1.710 +		}
   1.711 +
   1.712 +		$this->page_title = 'EDIT_TEMPLATE';
   1.713 +
   1.714 +		$filelist = $filelist_cats = array();
   1.715 +
   1.716 +		$template_data	= utf8_normalize_nfc(request_var('template_data', '', true));
   1.717 +		$template_data	= htmlspecialchars_decode($template_data);
   1.718 +		$template_file	= utf8_normalize_nfc(request_var('template_file', '', true));
   1.719 +		$text_rows		= max(5, min(999, request_var('text_rows', 20)));
   1.720 +		$save_changes	= (isset($_POST['save'])) ? true : false;
   1.721 +
   1.722 +		// make sure template_file path doesn't go upwards
   1.723 +		$template_file = preg_replace('#\.{2,}#', '.', $template_file);
   1.724 +
   1.725 +		// Retrieve some information about the template
   1.726 +		$sql = 'SELECT template_storedb, template_path, template_name
   1.727 +			FROM ' . STYLES_TEMPLATE_TABLE . "
   1.728 +			WHERE template_id = $template_id";
   1.729 +		$result = $db->sql_query($sql);
   1.730 +		$template_info = $db->sql_fetchrow($result);
   1.731 +		$db->sql_freeresult($result);
   1.732 +
   1.733 +		if (!$template_info)
   1.734 +		{
   1.735 +			trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.736 +		}
   1.737 +
   1.738 +		if ($save_changes && !check_form_key('acp_styles'))
   1.739 +		{
   1.740 +			trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.741 +		}
   1.742 +		else if (!$save_changes)
   1.743 +		{
   1.744 +			add_form_key('acp_styles');
   1.745 +		}
   1.746 +
   1.747 +		// save changes to the template if the user submitted any
   1.748 +		if ($save_changes && $template_file)
   1.749 +		{
   1.750 +			// Get the filesystem location of the current file
   1.751 +			$file = "{$phpbb_root_path}styles/{$template_info['template_path']}/template/$template_file";
   1.752 +			$additional = '';
   1.753 +
   1.754 +			// If the template is stored on the filesystem try to write the file else store it in the database
   1.755 +			if (!$safe_mode && !$template_info['template_storedb'] && file_exists($file) && phpbb_is_writable($file))
   1.756 +			{
   1.757 +				if (!($fp = @fopen($file, 'wb')))
   1.758 +				{
   1.759 +					// File exists and is writeable, but still not able to be written to
   1.760 +					trigger_error(sprintf($user->lang['TEMPLATE_FILE_NOT_WRITABLE'], htmlspecialchars($template_file)) . adm_back_link($this->u_action), E_USER_WARNING);
   1.761 +				}
   1.762 +				fwrite($fp, $template_data);
   1.763 +				fclose($fp);
   1.764 +			}
   1.765 +			else
   1.766 +			{
   1.767 +				$db->sql_transaction('begin');
   1.768 +
   1.769 +				// If it's not stored in the db yet, then update the template setting and store all template files in the db
   1.770 +				if (!$template_info['template_storedb'])
   1.771 +				{
   1.772 +					if ($super = $this->get_super('template', $template_id))
   1.773 +					{
   1.774 +						$this->store_in_db('template', $super['template_id']);
   1.775 +					}
   1.776 +					else
   1.777 +					{
   1.778 +						$this->store_in_db('template', $template_id);
   1.779 +					}
   1.780 +
   1.781 +					add_log('admin', 'LOG_TEMPLATE_EDIT_DETAILS', $template_info['template_name']);
   1.782 +					$additional .= '<br />' . $user->lang['EDIT_TEMPLATE_STORED_DB'];
   1.783 +				}
   1.784 +
   1.785 +				// Update the template_data table entry for this template file
   1.786 +				$sql = 'UPDATE ' . STYLES_TEMPLATE_DATA_TABLE . "
   1.787 +					SET template_data = '" . $db->sql_escape($template_data) . "', template_mtime = " . time() . "
   1.788 +					WHERE template_id = $template_id
   1.789 +						AND template_filename = '" . $db->sql_escape($template_file) . "'";
   1.790 +				$db->sql_query($sql);
   1.791 +
   1.792 +				$db->sql_transaction('commit');
   1.793 +			}
   1.794 +
   1.795 +			// destroy the cached version of the template (filename without extension)
   1.796 +			$this->clear_template_cache($template_info, array(substr($template_file, 0, -5)));
   1.797 +
   1.798 +			$cache->destroy('sql', STYLES_TABLE);
   1.799 +
   1.800 +			add_log('admin', 'LOG_TEMPLATE_EDIT', $template_info['template_name'], $template_file);
   1.801 +			trigger_error($user->lang['TEMPLATE_FILE_UPDATED'] . $additional . adm_back_link($this->u_action . "&amp;action=edit&amp;id=$template_id&amp;text_rows=$text_rows&amp;template_file=$template_file"));
   1.802 +		}
   1.803 +
   1.804 +		// Generate a category array containing template filenames
   1.805 +		if (!$template_info['template_storedb'])
   1.806 +		{
   1.807 +			$template_path = "{$phpbb_root_path}styles/{$template_info['template_path']}/template";
   1.808 +
   1.809 +			$filelist = filelist($template_path, '', 'html');
   1.810 +			$filelist[''] = array_diff($filelist[''], array('bbcode.html'));
   1.811 +
   1.812 +			if ($template_file)
   1.813 +			{
   1.814 +				if (!file_exists($template_path . "/$template_file") || !($template_data = file_get_contents($template_path . "/$template_file")))
   1.815 +				{
   1.816 +					trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.817 +				}
   1.818 +			}
   1.819 +		}
   1.820 +		else
   1.821 +		{
   1.822 +			$sql = 'SELECT *
   1.823 +				FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
   1.824 +				WHERE template_id = $template_id";
   1.825 +			$result = $db->sql_query($sql);
   1.826 +
   1.827 +			$filelist = array('' => array());
   1.828 +			while ($row = $db->sql_fetchrow($result))
   1.829 +			{
   1.830 +				$file_info = pathinfo($row['template_filename']);
   1.831 +
   1.832 +				if (($file_info['basename'] != 'bbcode') && ($file_info['extension'] == 'html'))
   1.833 +				{
   1.834 +					if (($file_info['dirname'] == '.') || empty($file_info['dirname']))
   1.835 +					{
   1.836 +						$filelist[''][] = $row['template_filename'];
   1.837 +					}
   1.838 +					else
   1.839 +					{
   1.840 +						$filelist[$file_info['dirname'] . '/'][] = $file_info['basename'];
   1.841 +					}
   1.842 +				}
   1.843 +
   1.844 +				if ($row['template_filename'] == $template_file)
   1.845 +				{
   1.846 +					$template_data = $row['template_data'];
   1.847 +				}
   1.848 +			}
   1.849 +			$db->sql_freeresult($result);
   1.850 +			unset($file_info);
   1.851 +		}
   1.852 +
   1.853 +		if (empty($filelist['']))
   1.854 +		{
   1.855 +			trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.856 +		}
   1.857 +
   1.858 +		// Now create the categories
   1.859 +		$filelist_cats[''] = array();
   1.860 +		foreach ($filelist as $pathfile => $file_ary)
   1.861 +		{
   1.862 +			// Use the directory name as category name
   1.863 +			if (!empty($pathfile))
   1.864 +			{
   1.865 +				$filelist_cats[$pathfile] = array();
   1.866 +				foreach ($file_ary as $file)
   1.867 +				{
   1.868 +					$filelist_cats[$pathfile][$pathfile . $file] = $file;
   1.869 +				}
   1.870 +			}
   1.871 +			// or if it's in the main category use the word before the first underscore to group files
   1.872 +			else
   1.873 +			{
   1.874 +				$cats = array();
   1.875 +				foreach ($file_ary as $file)
   1.876 +				{
   1.877 +					$cats[] = substr($file, 0, strpos($file, '_'));
   1.878 +					$filelist_cats[substr($file, 0, strpos($file, '_'))][$file] = $file;
   1.879 +				}
   1.880 +
   1.881 +				$cats = array_values(array_unique($cats));
   1.882 +
   1.883 +				// we don't need any single element categories so put them into the misc '' category
   1.884 +				for ($i = 0, $n = sizeof($cats); $i < $n; $i++)
   1.885 +				{
   1.886 +					if (sizeof($filelist_cats[$cats[$i]]) == 1 && $cats[$i] !== '')
   1.887 +					{
   1.888 +						$filelist_cats[''][key($filelist_cats[$cats[$i]])] = current($filelist_cats[$cats[$i]]);
   1.889 +						unset($filelist_cats[$cats[$i]]);
   1.890 +					}
   1.891 +				}
   1.892 +				unset($cats);
   1.893 +			}
   1.894 +		}
   1.895 +		unset($filelist);
   1.896 +
   1.897 +		// Generate list of categorised template files
   1.898 +		$tpl_options = '';
   1.899 +		ksort($filelist_cats);
   1.900 +		foreach ($filelist_cats as $category => $tpl_ary)
   1.901 +		{
   1.902 +			ksort($tpl_ary);
   1.903 +
   1.904 +			if (!empty($category))
   1.905 +			{
   1.906 +				$tpl_options .= '<option class="sep" value="">' . $category . '</option>';
   1.907 +			}
   1.908 +
   1.909 +			foreach ($tpl_ary as $filename => $file)
   1.910 +			{
   1.911 +				$selected = ($template_file == $filename) ? ' selected="selected"' : '';
   1.912 +				$tpl_options .= '<option value="' . $filename . '"' . $selected . '>' . $file . '</option>';
   1.913 +			}
   1.914 +		}
   1.915 +
   1.916 +		$template->assign_vars(array(
   1.917 +			'S_EDIT_TEMPLATE'	=> true,
   1.918 +			'S_HIDDEN_FIELDS'	=> build_hidden_fields(array('template_file' => $template_file)),
   1.919 +			'S_TEMPLATES'		=> $tpl_options,
   1.920 +
   1.921 +			'U_ACTION'			=> $this->u_action . "&amp;action=edit&amp;id=$template_id&amp;text_rows=$text_rows",
   1.922 +			'U_BACK'			=> $this->u_action,
   1.923 +
   1.924 +			'L_EDIT'			=> $user->lang['EDIT_TEMPLATE'],
   1.925 +			'L_EDIT_EXPLAIN'	=> $user->lang['EDIT_TEMPLATE_EXPLAIN'],
   1.926 +			'L_EDITOR'			=> $user->lang['TEMPLATE_EDITOR'],
   1.927 +			'L_EDITOR_HEIGHT'	=> $user->lang['TEMPLATE_EDITOR_HEIGHT'],
   1.928 +			'L_FILE'			=> $user->lang['TEMPLATE_FILE'],
   1.929 +			'L_SELECT'			=> $user->lang['SELECT_TEMPLATE'],
   1.930 +			'L_SELECTED'		=> $user->lang['SELECTED_TEMPLATE'],
   1.931 +			'L_SELECTED_FILE'	=> $user->lang['SELECTED_TEMPLATE_FILE'],
   1.932 +
   1.933 +			'SELECTED_TEMPLATE'	=> $template_info['template_name'],
   1.934 +			'TEMPLATE_FILE'		=> $template_file,
   1.935 +			'TEMPLATE_DATA'		=> utf8_htmlspecialchars($template_data),
   1.936 +			'TEXT_ROWS'			=> $text_rows)
   1.937 +		);
   1.938 +	}
   1.939 +
   1.940 +	/**
   1.941 +	* Allows the admin to view cached versions of template files and clear single template cache files
   1.942 +	*
   1.943 +	* @param int $template_id specifies which template's cache is shown
   1.944 +	*/
   1.945 +	function template_cache($template_id)
   1.946 +	{
   1.947 +		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template;
   1.948 +
   1.949 +		$source		= str_replace('/', '.', request_var('source', ''));
   1.950 +		$file_ary	= array_diff(request_var('delete', array('')), array(''));
   1.951 +		$submit		= isset($_POST['submit']) ? true : false;
   1.952 +
   1.953 +		$sql = 'SELECT *
   1.954 +			FROM ' . STYLES_TEMPLATE_TABLE . "
   1.955 +			WHERE template_id = $template_id";
   1.956 +		$result = $db->sql_query($sql);
   1.957 +		$template_row = $db->sql_fetchrow($result);
   1.958 +		$db->sql_freeresult($result);
   1.959 +
   1.960 +		if (!$template_row)
   1.961 +		{
   1.962 +			trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.963 +		}
   1.964 +
   1.965 +		// User wants to delete one or more files ...
   1.966 +		if ($submit && $file_ary)
   1.967 +		{
   1.968 +			$this->clear_template_cache($template_row, $file_ary);
   1.969 +			trigger_error($user->lang['TEMPLATE_CACHE_CLEARED'] . adm_back_link($this->u_action . "&amp;action=cache&amp;id=$template_id"));
   1.970 +		}
   1.971 +
   1.972 +		$cache_prefix = 'tpl_' . str_replace('_', '-', $template_row['template_path']);
   1.973 +
   1.974 +		// Someone wants to see the cached source ... so we'll highlight it,
   1.975 +		// add line numbers and indent it appropriately. This could be nasty
   1.976 +		// on larger source files ...
   1.977 +		if ($source && file_exists("{$phpbb_root_path}cache/{$cache_prefix}_$source.html.$phpEx"))
   1.978 +		{
   1.979 +			adm_page_header($user->lang['TEMPLATE_CACHE']);
   1.980 +
   1.981 +			$template->set_filenames(array(
   1.982 +				'body'	=> 'viewsource.html')
   1.983 +			);
   1.984 +
   1.985 +			$template->assign_vars(array(
   1.986 +				'FILENAME'	=> str_replace('.', '/', $source) . '.html')
   1.987 +			);
   1.988 +
   1.989 +			$code = str_replace(array("\r\n", "\r"), array("\n", "\n"), file_get_contents("{$phpbb_root_path}cache/{$cache_prefix}_$source.html.$phpEx"));
   1.990 +
   1.991 +			$conf = array('highlight.bg', 'highlight.comment', 'highlight.default', 'highlight.html', 'highlight.keyword', 'highlight.string');
   1.992 +			foreach ($conf as $ini_var)
   1.993 +			{
   1.994 +				@ini_set($ini_var, str_replace('highlight.', 'syntax', $ini_var));
   1.995 +			}
   1.996 +
   1.997 +			$marker = 'MARKER' . time();
   1.998 +			$code = highlight_string(str_replace("\n", $marker, $code), true);
   1.999 +			$code = str_replace($marker, "\n", $code);
  1.1000 +			$str_from = array('<span style="color: ', '<font color="syntax', '</font>', '<code>', '</code>','[', ']', '.', ':');
  1.1001 +			$str_to = array('<span class="', '<span class="syntax', '</span>', '', '', '&#91;', '&#93;', '&#46;', '&#58;');
  1.1002 +
  1.1003 +			$code = str_replace($str_from, $str_to, $code);
  1.1004 +			$code = preg_replace('#^(<span class="[a-z_]+">)\n?(.*?)\n?(</span>)$#ism', '$1$2$3', $code);
  1.1005 +			$code = substr($code, strlen('<span class="syntaxhtml">'));
  1.1006 +			$code = substr($code, 0, -1 * strlen('</ span>'));
  1.1007 +			$code = explode("\n", $code);
  1.1008 +
  1.1009 +			foreach ($code as $key => $line)
  1.1010 +			{
  1.1011 +				$template->assign_block_vars('source', array(
  1.1012 +					'LINENUM'	=> $key + 1,
  1.1013 +					'LINE'		=> preg_replace('#([^ ;])&nbsp;([^ &])#', '$1 $2', $line))
  1.1014 +				);
  1.1015 +				unset($code[$key]);
  1.1016 +			}
  1.1017 +
  1.1018 +			adm_page_footer();
  1.1019 +		}
  1.1020 +
  1.1021 +		$filemtime = array();
  1.1022 +		if ($template_row['template_storedb'])
  1.1023 +		{
  1.1024 +			$ids = array();
  1.1025 +			if (isset($template_row['template_inherits_id']) && $template_row['template_inherits_id'])
  1.1026 +			{
  1.1027 +				$ids[] = $template_row['template_inherits_id'];
  1.1028 +			}
  1.1029 +			$ids[] = $template_row['template_id'];
  1.1030 +
  1.1031 +			$filemtime 			= array();
  1.1032 +			$file_template_db	= array();
  1.1033 +
  1.1034 +			foreach ($ids as $id)
  1.1035 +			{
  1.1036 +				$sql = 'SELECT template_filename, template_mtime
  1.1037 +					FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
  1.1038 +					WHERE template_id = $id";
  1.1039 +				$result = $db->sql_query($sql);
  1.1040 +
  1.1041 +				while ($row = $db->sql_fetchrow($result))
  1.1042 +				{
  1.1043 +					$filemtime[$row['template_filename']] = $row['template_mtime'];
  1.1044 +					$file_template_db[$row['template_filename']] = $id;
  1.1045 +				}
  1.1046 +				$db->sql_freeresult($result);
  1.1047 +			}
  1.1048 +		}
  1.1049 +
  1.1050 +		// Get a list of cached template files and then retrieve additional information about them
  1.1051 +		$file_ary = $this->template_cache_filelist($template_row['template_path']);
  1.1052 +
  1.1053 +		foreach ($file_ary as $file)
  1.1054 +		{
  1.1055 +			$file		= str_replace('/', '.', $file);
  1.1056 +
  1.1057 +			// perform some dirty guessing to get the path right.
  1.1058 +			// We assume that three dots in a row were '../'
  1.1059 +			$tpl_file	= str_replace('.', '/', $file);
  1.1060 +			$tpl_file	= str_replace('///', '../', $tpl_file);
  1.1061 +
  1.1062 +			$filename = "{$cache_prefix}_$file.html.$phpEx";
  1.1063 +
  1.1064 +			if (!file_exists("{$phpbb_root_path}cache/$filename"))
  1.1065 +			{
  1.1066 +				continue;
  1.1067 +			}
  1.1068 +
  1.1069 +			$file_tpl = "{$phpbb_root_path}styles/{$template_row['template_path']}/template/$tpl_file.html";
  1.1070 +			$inherited = false;
  1.1071 +
  1.1072 +			if (isset($template_row['template_inherits_id']) && $template_row['template_inherits_id'])
  1.1073 +			{
  1.1074 +				if (!$template_row['template_storedb'])
  1.1075 +				{
  1.1076 +					if (!file_exists($file_tpl))
  1.1077 +					{
  1.1078 +						$file_tpl = "{$phpbb_root_path}styles/{$template_row['template_inherit_path']}/template/$tpl_file.html";
  1.1079 +						$inherited = true;
  1.1080 +					}
  1.1081 +				}
  1.1082 +				else
  1.1083 +				{
  1.1084 +					if ($file_template_db[$file . '.html'] == $template_row['template_inherits_id'])
  1.1085 +					{
  1.1086 +						$file_tpl = "{$phpbb_root_path}styles/{$template_row['template_inherit_path']}/template/$tpl_file.html";
  1.1087 +						$inherited = true;
  1.1088 +					}
  1.1089 +				}
  1.1090 +			}
  1.1091 +
  1.1092 +			// Correct the filename if it is stored in database and the file is in a subfolder.
  1.1093 +			if ($template_row['template_storedb'])
  1.1094 +			{
  1.1095 +				$file = str_replace('.', '/', $file);
  1.1096 +			}
  1.1097 +
  1.1098 +			$template->assign_block_vars('file', array(
  1.1099 +				'U_VIEWSOURCE'	=> $this->u_action . "&amp;action=cache&amp;id=$template_id&amp;source=$file",
  1.1100 +
  1.1101 +				'CACHED'		=> $user->format_date(filemtime("{$phpbb_root_path}cache/$filename")),
  1.1102 +				'FILENAME'		=> $file,
  1.1103 +				'FILENAME_PATH'	=> $file_tpl,
  1.1104 +				'FILESIZE'		=> get_formatted_filesize(filesize("{$phpbb_root_path}cache/$filename")),
  1.1105 +				'MODIFIED'		=> $user->format_date((!$template_row['template_storedb']) ? filemtime($file_tpl) : $filemtime[$file . '.html']))
  1.1106 +			);
  1.1107 +		}
  1.1108 +		unset($filemtime);
  1.1109 +
  1.1110 +		$template->assign_vars(array(
  1.1111 +			'S_CACHE'			=> true,
  1.1112 +			'S_TEMPLATE'		=> true,
  1.1113 +
  1.1114 +			'U_ACTION'			=> $this->u_action . "&amp;action=cache&amp;id=$template_id",
  1.1115 +			'U_BACK'			=> $this->u_action)
  1.1116 +		);
  1.1117 +	}
  1.1118 +
  1.1119 +	/**
  1.1120 +	* Provides a css editor and a basic easier to use stylesheet editing tool for less experienced (or lazy) users
  1.1121 +	*
  1.1122 +	* @param int $theme_id specifies which theme is being edited
  1.1123 +	*/
  1.1124 +	function edit_theme($theme_id)
  1.1125 +	{
  1.1126 +		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template, $safe_mode;
  1.1127 +
  1.1128 +		$this->page_title = 'EDIT_THEME';
  1.1129 +
  1.1130 +		$filelist = $filelist_cats = array();
  1.1131 +
  1.1132 +		$theme_data		= utf8_normalize_nfc(request_var('template_data', '', true));
  1.1133 +		$theme_data		= htmlspecialchars_decode($theme_data);
  1.1134 +		$theme_file		= utf8_normalize_nfc(request_var('template_file', '', true));
  1.1135 +		$text_rows		= max(5, min(999, request_var('text_rows', 20)));
  1.1136 +		$save_changes	= (isset($_POST['save'])) ? true : false;
  1.1137 +
  1.1138 +		// make sure theme_file path doesn't go upwards
  1.1139 +		$theme_file = str_replace('..', '.', $theme_file);
  1.1140 +
  1.1141 +		// Retrieve some information about the theme
  1.1142 +		$sql = 'SELECT theme_storedb, theme_path, theme_name, theme_data
  1.1143 +			FROM ' . STYLES_THEME_TABLE . "
  1.1144 +			WHERE theme_id = $theme_id";
  1.1145 +		$result = $db->sql_query($sql);
  1.1146 +
  1.1147 +		if (!($theme_info = $db->sql_fetchrow($result)))
  1.1148 +		{
  1.1149 +			trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
  1.1150 +		}
  1.1151 +		$db->sql_freeresult($result);
  1.1152 +
  1.1153 +		// save changes to the theme if the user submitted any
  1.1154 +		if ($save_changes)
  1.1155 +		{
  1.1156 +			// Get the filesystem location of the current file
  1.1157 +			$file = "{$phpbb_root_path}styles/{$theme_info['theme_path']}/theme/$theme_file";
  1.1158 +			$additional = '';
  1.1159 +			$message = $user->lang['THEME_UPDATED'];
  1.1160 +
  1.1161 +			// If the theme is stored on the filesystem try to write the file else store it in the database
  1.1162 +			if (!$safe_mode && !$theme_info['theme_storedb'] && file_exists($file) && phpbb_is_writable($file))
  1.1163 +			{
  1.1164 +				if (!($fp = @fopen($file, 'wb')))
  1.1165 +				{
  1.1166 +					trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
  1.1167 +				}
  1.1168 +				fwrite($fp, $theme_data);
  1.1169 +				fclose($fp);
  1.1170 +			}
  1.1171 +			else
  1.1172 +			{
  1.1173 +				// Write stylesheet to db
  1.1174 +				$sql_ary = array(
  1.1175 +					'theme_mtime'		=> time(),
  1.1176 +					'theme_storedb'		=> 1,
  1.1177 +					'theme_data'		=> $this->db_theme_data($theme_info, $theme_data),
  1.1178 +				);
  1.1179 +				$sql = 'UPDATE ' . STYLES_THEME_TABLE . '
  1.1180 +					SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1.1181 +					WHERE theme_id = ' . $theme_id;
  1.1182 +				$db->sql_query($sql);
  1.1183 +
  1.1184 +				$cache->destroy('sql', STYLES_THEME_TABLE);
  1.1185 +
  1.1186 +				// notify the user if the theme was not stored in the db before his modification
  1.1187 +				if (!$theme_info['theme_storedb'])
  1.1188 +				{
  1.1189 +					add_log('admin', 'LOG_THEME_EDIT_DETAILS', $theme_info['theme_name']);
  1.1190 +					$message .= '<br />' . $user->lang['EDIT_THEME_STORED_DB'];
  1.1191 +				}
  1.1192 +			}
  1.1193 +			$cache->destroy('sql', STYLES_THEME_TABLE);
  1.1194 +			add_log('admin', (!$theme_info['theme_storedb']) ? 'LOG_THEME_EDIT_FILE' : 'LOG_THEME_EDIT', $theme_info['theme_name'], (!$theme_info['theme_storedb']) ? $theme_file : '');
  1.1195 +
  1.1196 +			trigger_error($message . adm_back_link($this->u_action . "&amp;action=edit&amp;id=$theme_id&amp;template_file=$theme_file&amp;text_rows=$text_rows"));
  1.1197 +		}
  1.1198 +
  1.1199 +		// Generate a category array containing theme filenames
  1.1200 +		if (!$theme_info['theme_storedb'])
  1.1201 +		{
  1.1202 +			$theme_path = "{$phpbb_root_path}styles/{$theme_info['theme_path']}/theme";
  1.1203 +
  1.1204 +			$filelist = filelist($theme_path, '', 'css');
  1.1205 +
  1.1206 +			if ($theme_file)
  1.1207 +			{
  1.1208 +				if (!file_exists($theme_path . "/$theme_file") || !($theme_data = file_get_contents($theme_path . "/$theme_file")))
  1.1209 +				{
  1.1210 +					trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
  1.1211 +				}
  1.1212 +			}
  1.1213 +		}
  1.1214 +		else
  1.1215 +		{
  1.1216 +			$theme_data = &$theme_info['theme_data'];
  1.1217 +		}
  1.1218 +
  1.1219 +		// Now create the categories
  1.1220 +		$filelist_cats[''] = array();
  1.1221 +		foreach ($filelist as $pathfile => $file_ary)
  1.1222 +		{
  1.1223 +			// Use the directory name as category name
  1.1224 +			if (!empty($pathfile))
  1.1225 +			{
  1.1226 +				$filelist_cats[$pathfile] = array();
  1.1227 +				foreach ($file_ary as $file)
  1.1228 +				{
  1.1229 +					$filelist_cats[$pathfile][$pathfile . $file] = $file;
  1.1230 +				}
  1.1231 +			}
  1.1232 +			// or if it's in the main category use the word before the first underscore to group files
  1.1233 +			else
  1.1234 +			{
  1.1235 +				$cats = array();
  1.1236 +				foreach ($file_ary as $file)
  1.1237 +				{
  1.1238 +					$cats[] = substr($file, 0, strpos($file, '_'));
  1.1239 +					$filelist_cats[substr($file, 0, strpos($file, '_'))][$file] = $file;
  1.1240 +				}
  1.1241 +
  1.1242 +				$cats = array_values(array_unique($cats));
  1.1243 +
  1.1244 +				// we don't need any single element categories so put them into the misc '' category
  1.1245 +				for ($i = 0, $n = sizeof($cats); $i < $n; $i++)
  1.1246 +				{
  1.1247 +					if (sizeof($filelist_cats[$cats[$i]]) == 1 && $cats[$i] !== '')
  1.1248 +					{
  1.1249 +						$filelist_cats[''][key($filelist_cats[$cats[$i]])] = current($filelist_cats[$cats[$i]]);
  1.1250 +						unset($filelist_cats[$cats[$i]]);
  1.1251 +					}
  1.1252 +				}
  1.1253 +				unset($cats);
  1.1254 +			}
  1.1255 +		}
  1.1256 +		unset($filelist);
  1.1257 +
  1.1258 +		// Generate list of categorised theme files
  1.1259 +		$tpl_options = '';
  1.1260 +		ksort($filelist_cats);
  1.1261 +		foreach ($filelist_cats as $category => $tpl_ary)
  1.1262 +		{
  1.1263 +			ksort($tpl_ary);
  1.1264 +
  1.1265 +			if (!empty($category))
  1.1266 +			{
  1.1267 +				$tpl_options .= '<option class="sep" value="">' . $category . '</option>';
  1.1268 +			}
  1.1269 +
  1.1270 +			foreach ($tpl_ary as $filename => $file)
  1.1271 +			{
  1.1272 +				$selected = ($theme_file == $filename) ? ' selected="selected"' : '';
  1.1273 +				$tpl_options .= '<option value="' . $filename . '"' . $selected . '>' . $file . '</option>';
  1.1274 +			}
  1.1275 +		}
  1.1276 +
  1.1277 +		$template->assign_vars(array(
  1.1278 +			'S_EDIT_THEME'		=> true,
  1.1279 +			'S_HIDDEN_FIELDS'	=> build_hidden_fields(array('template_file' => $theme_file)),
  1.1280 +			'S_THEME_IN_DB'		=> $theme_info['theme_storedb'],
  1.1281 +			'S_TEMPLATES'		=> $tpl_options,
  1.1282 +
  1.1283 +			'U_ACTION'			=> $this->u_action . "&amp;action=edit&amp;id=$theme_id&amp;text_rows=$text_rows",
  1.1284 +			'U_BACK'			=> $this->u_action,
  1.1285 +
  1.1286 +			'L_EDIT'			=> $user->lang['EDIT_THEME'],
  1.1287 +			'L_EDIT_EXPLAIN'	=> $user->lang['EDIT_THEME_EXPLAIN'],
  1.1288 +			'L_EDITOR'			=> $user->lang['THEME_EDITOR'],
  1.1289 +			'L_EDITOR_HEIGHT'	=> $user->lang['THEME_EDITOR_HEIGHT'],
  1.1290 +			'L_FILE'			=> $user->lang['THEME_FILE'],
  1.1291 +			'L_SELECT'			=> $user->lang['SELECT_THEME'],
  1.1292 +			'L_SELECTED'		=> $user->lang['SELECTED_THEME'],
  1.1293 +			'L_SELECTED_FILE'	=> $user->lang['SELECTED_THEME_FILE'],
  1.1294 +
  1.1295 +			'SELECTED_TEMPLATE'	=> $theme_info['theme_name'],
  1.1296 +			'TEMPLATE_FILE'		=> $theme_file,
  1.1297 +			'TEMPLATE_DATA'		=> utf8_htmlspecialchars($theme_data),
  1.1298 +			'TEXT_ROWS'			=> $text_rows)
  1.1299 +		);
  1.1300 +	}
  1.1301 +
  1.1302 +	/**
  1.1303 +	* Edit imagesets
  1.1304 +	*
  1.1305 +	* @param int $imageset_id specifies which imageset is being edited
  1.1306 +	*/
  1.1307 +	function edit_imageset($imageset_id)
  1.1308 +	{
  1.1309 +		global $db, $user, $phpbb_root_path, $cache, $template;
  1.1310 +
  1.1311 +		$this->page_title = 'EDIT_IMAGESET';
  1.1312 +
  1.1313 +		if (!$imageset_id)
  1.1314 +		{
  1.1315 +			trigger_error($user->lang['NO_IMAGESET'] . adm_back_link($this->u_action), E_USER_WARNING);
  1.1316 +		}
  1.1317 +
  1.1318 +		$update		= (isset($_POST['update'])) ? true : false;
  1.1319 +
  1.1320 +		$imgname	= request_var('imgname', 'site_logo');
  1.1321 +		$imgname	= preg_replace('#[^a-z0-9\-+_]#i', '', $imgname);
  1.1322 +		$sql_extra = $imgnamelang = '';
  1.1323 +
  1.1324 +		$sql = 'SELECT imageset_path, imageset_name
  1.1325 +			FROM ' . STYLES_IMAGESET_TABLE . "
  1.1326 +			WHERE imageset_id = $imageset_id";
  1.1327 +		$result = $db->sql_query($sql);
  1.1328 +		$imageset_row = $db->sql_fetchrow($result);
  1.1329 +		$db->sql_freeresult($result);
  1.1330 +
  1.1331 +		if (!$imageset_row)
  1.1332 +		{
  1.1333 +			trigger_error($user->lang['NO_IMAGESET'] . adm_back_link($this->u_action), E_USER_WARNING);
  1.1334 +		}
  1.1335 +
  1.1336 +		$imageset_path		= $imageset_row['imageset_path'];
  1.1337 +		$imageset_name		= $imageset_row['imageset_name'];
  1.1338 +
  1.1339 +		if (strpos($imgname, '-') !== false)
  1.1340 +		{
  1.1341 +			list($imgname, $imgnamelang) = explode('-', $imgname);
  1.1342 +			$sql_extra = " AND image_lang IN ('" . $db->sql_escape($imgnamelang) . "', '')";
  1.1343 +		}
  1.1344 +
  1.1345 +		$sql = 'SELECT image_filename, image_width, image_height, image_lang, image_id
  1.1346 +			FROM ' . STYLES_IMAGESET_DATA_TABLE . "
  1.1347 +			WHERE imageset_id = $imageset_id
  1.1348 +				AND image_name = '" . $db->sql_escape($imgname) . "'$sql_extra";
  1.1349 +		$result = $db->sql_query($sql);
  1.1350 +		$imageset_data_row = $db->sql_fetchrow($result);
  1.1351 +		$db->sql_freeresult($result);
  1.1352 +
  1.1353 +		$image_filename	= $imageset_data_row['image_filename'];
  1.1354 +		$image_width	= $imageset_data_row['image_width'];
  1.1355 +		$image_height	= $imageset_data_row['image_height'];
  1.1356 +		$image_lang		= $imageset_data_row['image_lang'];
  1.1357 +		$image_id		= $imageset_data_row['image_id'];
  1.1358 +		$imgsize		= ($imageset_data_row['image_width'] && $imageset_data_row['image_height']) ? 1 : 0;
  1.1359 +
  1.1360 +		// Check to see whether the selected image exists in the table
  1.1361 +		$valid_name = ($update) ? false : true;
  1.1362 +
  1.1363 +		foreach ($this->imageset_keys as $category => $img_ary)
  1.1364 +		{
  1.1365 +			if (in_array($imgname, $img_ary))
  1.1366 +			{
  1.1367 +				$valid_name = true;
  1.1368 +				break;
  1.1369 +			}
  1.1370 +		}
  1.1371 +
  1.1372 +		if ($update && isset($_POST['imgpath']) && $valid_name)
  1.1373 +		{
  1.1374 +			// If imgwidth and imgheight are non-zero grab the actual size
  1.1375 +			// from the image itself ... we ignore width settings for the poll center image
  1.1376 +			$imgwidth	= request_var('imgwidth', 0);
  1.1377 +			$imgheight	= request_var('imgheight', 0);
  1.1378 +			$imgsize	= request_var('imgsize', 0);
  1.1379 +			$imgpath	= request_var('imgpath', '');
  1.1380 +			$imgpath	= str_replace('..', '.', $imgpath);
  1.1381 +
  1.1382 +			// If no dimensions selected, we reset width and height to 0 ;)
  1.1383 +			if (!$imgsize)
  1.1384 +			{
  1.1385 +				$imgwidth = $imgheight = 0;
  1.1386 +			}
  1.1387 +
  1.1388 +			$imglang = '';
  1.1389 +
  1.1390 +			if ($imgpath && !file_exists("{$phpbb_root_path}styles/$imageset_path/imageset/$imgpath"))
  1.1391 +			{
  1.1392 +				trigger_error($user->lang['NO_IMAGE_ERROR'] . adm_back_link($this->u_action), E_USER_WARNING);
  1.1393 +			}
  1.1394 +
  1.1395 +			// Determine width/height. If dimensions included and no width/height given, we detect them automatically...
  1.1396 +			if ($imgsize && $imgpath)
  1.1397 +			{
  1.1398 +				if (!$imgwidth || !$imgheight)
  1.1399 +				{
  1.1400 +					list($imgwidth_file, $imgheight_file) = getimagesize("{$phpbb_root_path}styles/$imageset_path/imageset/$imgpath");
  1.1401 +					$imgwidth = ($imgwidth) ? $imgwidth : $imgwidth_file;
  1.1402 +					$imgheight = ($imgheight) ? $imgheight : $imgheight_file;
  1.1403 +				}
  1.1404 +				$imgwidth	= ($imgname != 'poll_center') ? (int) $imgwidth : 0;
  1.1405 +				$imgheight	= (int) $imgheight;
  1.1406 +			}
  1.1407 +
  1.1408 +			if (strpos($imgpath, '/') !== false)
  1.1409 +			{
  1.1410 +				list($imglang, $imgfilename) = explode('/', $imgpath);
  1.1411 +			}
  1.1412 +			else
  1.1413 +			{
  1.1414 +				$imgfilename = $imgpath;
  1.1415 +			}
  1.1416 +
  1.1417 +			$sql_ary = array(
  1.1418 +				'image_filename'	=> (string) $imgfilename,
  1.1419 +				'image_width'		=> (int) $imgwidth,
  1.1420 +				'image_height'		=> (int) $imgheight,
  1.1421 +				'image_lang'		=> (string) $imglang,
  1.1422 +			);
  1.1423 +
  1.1424 +			// already exists
  1.1425 +			if ($imageset_data_row)
  1.1426 +			{
  1.1427 +				$sql = 'UPDATE ' . STYLES_IMAGESET_DATA_TABLE . '
  1.1428 +					SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
  1.1429 +					WHERE image_id = $image_id";
  1.1430 +				$db->sql_query($sql);
  1.1431 +			}
  1.1432 +			// does not exist
  1.1433 +			else if (!$imageset_data_row)
  1.1434 +			{
  1.1435 +				$sql_ary['image_name']	= $imgname;
  1.1436 +				$sql_ary['imageset_id']	= (int) $imageset_id;
  1.1437 +				$db->sql_query('INSERT INTO ' . STYLES_IMAGESET_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
  1.1438 +			}
  1.1439 +
  1.1440 +			$cache->destroy('sql', STYLES_IMAGESET_DATA_TABLE);
  1.1441 +
  1.1442 +			add_log('admin', 'LOG_IMAGESET_EDIT', $imageset_name);
  1.1443 +
  1.1444 +			$template->assign_var('SUCCESS', true);
  1.1445 +
  1.1446 +			$image_filename = $imgfilename;
  1.1447 +			$image_width	= $imgwidth;
  1.1448 +			$image_height	= $imgheight;
  1.1449 +			$image_lang		= $imglang;
  1.1450 +		}
  1.1451 +
  1.1452 +		$imglang = '';
  1.1453 +		$imagesetlist = array('nolang' => array(), 'lang' => array());
  1.1454 +		$langs = array();
  1.1455 +
  1.1456 +		$dir = "{$phpbb_root_path}styles/$imageset_path/imageset";
  1.1457 +		$dp = @opendir($dir);
  1.1458 +
  1.1459 +		if ($dp)
  1.1460 +		{
  1.1461 +			while (($file = readdir($dp)) !== false)
  1.1462 +			{
  1.1463 +				if ($file[0] != '.' && strtoupper($file) != 'CVS' && !is_file($dir . '/' . $file) && !is_link($dir . '/' . $file))
  1.1464 +				{
  1.1465 +					$langs[] = $file;
  1.1466 +				}
  1.1467 +				else if (preg_match('#\.(?:gif|jpg|png)$#', $file))
  1.1468 +				{
  1.1469 +					$imagesetlist['nolang'][] = $file;
  1.1470 +				}
  1.1471 +			}
  1.1472 +
  1.1473 +			if ($sql_extra)
  1.1474 +			{
  1.1475 +				$dp2 = @opendir("$dir/$imgnamelang");
  1.1476 +
  1.1477 +				if ($dp2)
  1.1478 +				{
  1.1479 +					while (($file2 = readdir($dp2)) !== false)
  1.1480 +					{
  1.1481 +						if (preg_match('#\.(?:gif|jpg|png)$#', $file2))
  1.1482 +						{
  1.1483 +							$imagesetlist['lang'][] = "$imgnamelang/$file2";
  1.1484 +						}
  1.1485 +					}
  1.1486 +					closedir($dp2);
  1.1487 +				}
  1.1488 +			}
  1.1489 +			closedir($dp);
  1.1490 +		}
  1.1491 +
  1.1492 +		// Generate list of image options
  1.1493 +		$img_options = '';
  1.1494 +		foreach ($this->imageset_keys as $category => $img_ary)
  1.1495 +		{
  1.1496 +			$template->assign_block_vars('category', array(
  1.1497 +				'NAME'			=> $user->lang['IMG_CAT_' . strtoupper($category)]
  1.1498 +			));
  1.1499 +
  1.1500 +			foreach ($img_ary as $img)
  1.1501 +			{
  1.1502 +				if ($category == 'buttons')
  1.1503 +				{
  1.1504 +					foreach ($langs as $language)
  1.1505 +					{
  1.1506 +						$template->assign_block_vars('category.images', array(
  1.1507 +							'SELECTED'			=> ($img == $imgname && $language == $imgnamelang),
  1.1508 +							'VALUE'				=> $img . '-' . $language,
  1.1509 +							'TEXT'				=> $user->lang['IMG_' . strtoupper($img)] . ' [ ' . $language . ' ]'
  1.1510 +						));
  1.1511 +					}
  1.1512 +				}
  1.1513 +				else
  1.1514 +				{
  1.1515 +					$template->assign_block_vars('category.images', array(
  1.1516 +						'SELECTED'			=> ($img == $imgname),
  1.1517 +						'VALUE'				=> $img,
  1.1518 +						'TEXT'				=> (($category == 'custom') ? $img : $user->lang['IMG_' . strtoupper($img)])
  1.1519 +					));
  1.1520 +				}
  1.1521 +			}
  1.1522 +		}
  1.1523 +
  1.1524 +		// Make sure the list of possible images is sorted alphabetically
  1.1525 +		sort($imagesetlist['lang']);
  1.1526 +		sort($imagesetlist['nolang']);
  1.1527 +
  1.1528 +		$image_found = false;
  1.1529 +		$img_val = '';
  1.1530 +		foreach ($imagesetlist as $type => $img_ary)
  1.1531 +		{
  1.1532 +			if ($type !== 'lang' || $sql_extra)
  1.1533 +			{
  1.1534 +				$template->assign_block_vars('imagesetlist', array(
  1.1535 +					'TYPE'	=> ($type == 'lang')
  1.1536 +				));
  1.1537 +			}
  1.1538 +
  1.1539 +			foreach ($img_ary as $img)
  1.1540 +			{
  1.1541 +				$imgtext = preg_replace('/^([^\/]+\/)/', '', $img);
  1.1542 +				$selected = (!empty($imgname) && strpos($image_filename, $imgtext) !== false);
  1.1543 +				if ($selected)
  1.1544 +				{
  1.1545 +					$image_found = true;
  1.1546 +					$img_val = htmlspecialchars($img);
  1.1547 +				}
  1.1548 +				$template->assign_block_vars('imagesetlist.images', array(
  1.1549 +					'SELECTED'			=> $selected,
  1.1550 +					'TEXT'				=> $imgtext,
  1.1551 +					'VALUE'				=> htmlspecialchars($img)
  1.1552 +				));
  1.1553 +			}
  1.1554 +		}
  1.1555 +
  1.1556 +		$imgsize_bool = (!empty($imgname) && $image_width && $image_height) ? true : false;
  1.1557 +		$image_request = '../styles/' . $imageset_path . '/imageset/' . ($image_lang ? $imgnamelang . '/' : '') . $image_filename;
  1.1558 +
  1.1559 +		$template->assign_vars(array(
  1.1560 +			'S_EDIT_IMAGESET'	=> true,
  1.1561 +			'L_TITLE'			=> $user->lang[$this->page_title],
  1.1562 +			'L_EXPLAIN'			=> $user->lang[$this->page_title . '_EXPLAIN'],
  1.1563 +			'IMAGE_OPTIONS'		=> $img_options,
  1.1564 +			'IMAGE_SIZE'		=> $image_width,
  1.1565 +			'IMAGE_HEIGHT'		=> $image_height,
  1.1566 +			'IMAGE_REQUEST'		=> (empty($image_filename)) ? 'images/no_image.png' : $image_request,
  1.1567 +			'U_ACTION'			=> $this->u_action . "&amp;action=edit&amp;id=$imageset_id",
  1.1568 +			'U_BACK'			=> $this->u_action,
  1.1569 +			'NAME'				=> $imageset_name,
  1.1570 +			'A_NAME'			=> addslashes($imageset_name),
  1.1571 +			'PATH'				=> $imageset_path,
  1.1572 +			'A_PATH'			=> addslashes($imageset_path),
  1.1573 +			'ERROR'				=> !$valid_name,
  1.1574 +			'IMG_SRC'			=> ($image_found) ? '../styles/' . $imageset_path . '/imageset/' . $img_val : 'images/no_image.png',
  1.1575 +			'IMAGE_SELECT'		=> $image_found
  1.1576 +		));
  1.1577 +	}
  1.1578 +
  1.1579 +	/**
  1.1580 +	* Remove style/template/theme/imageset
  1.1581 +	*/
  1.1582 +	function remove($mode, $style_id)
  1.1583 +	{
  1.1584 +		global $db, $template, $user, $phpbb_root_path, $cache, $config;
  1.1585 +
  1.1586 +		$new_id = request_var('new_id', 0);
  1.1587 +		$update = (isset($_POST['update'])) ? true : false;
  1.1588 +		$sql_where = '';
  1.1589 +
  1.1590 +		switch ($mode)
  1.1591 +		{
  1.1592 +			case 'style':
  1.1593 +				$sql_from = STYLES_TABLE;
  1.1594 +				$sql_select = 'style_id, style_name, template_id, theme_id, imageset_id';
  1.1595 +				$sql_where = 'AND style_active = 1';
  1.1596 +			break;
  1.1597 +
  1.1598 +			case 'template':
  1.1599 +				$sql_from = STYLES_TEMPLATE_TABLE;
  1.1600 +				$sql_select = 'template_id, template_name, template_path, template_storedb';
  1.1601 +			break;
  1.1602 +
  1.1603 +			case 'theme':
  1.1604 +				$sql_from = STYLES_THEME_TABLE;
  1.1605 +				$sql_select = 'theme_id, theme_name, theme_path, theme_storedb';
  1.1606 +			break;
  1.1607 +
  1.1608 +			case 'imageset':
  1.1609 +				$sql_from = STYLES_IMAGESET_TABLE;
  1.1610 +				$sql_select = 'imageset_id, imageset_name, imageset_path';
  1.1611 +			break;
  1.1612 +		}
  1.1613 +
  1.1614 +		if ($mode === 'template' && ($conflicts = $this->check_inheritance($mode, $style_id)))
  1.1615 +		{
  1.1616 +			$l_type = strtoupper($mode);
  1.1617 +			$msg = $user->lang[$l_type . '_DELETE_DEPENDENT'];
  1.1618 +			foreach ($conflicts as $id => $values)
  1.1619 +			{
  1.1620 +				$msg .= '<br />' . $values['template_name'];
  1.1621 +			}
  1.1622 +
  1.1623 +			trigger_error($msg . adm_back_link($this->u_action), E_USER_WARNING);
  1.1624 +		}
  1.1625 +
  1.1626 +		$l_prefix = strtoupper($mode);
  1.1627 +
  1.1628 +		$sql = "SELECT $sql_select
  1.1629 +			FROM $sql_from
  1.1630 +			WHERE {$mode}_id = $style_id";
  1.1631 +		$result = $db->sql_query($sql);
  1.1632 +		$style_row = $db->sql_fetchrow($result);
  1.1633 +		$db->sql_freeresult($result);
  1.1634 +
  1.1635 +		if (!$style_row)
  1.1636 +		{
  1.1637 +			trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
  1.1638 +		}
  1.1639 +
  1.1640 +		$s_only_component = $this->display_component_options($mode, $style_row[$mode . '_id'], $style_row);
  1.1641 +
  1.1642 +		if ($s_only_component)
  1.1643 +		{
  1.1644 +			trigger_error($user->lang['ONLY_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
  1.1645 +		}
  1.1646 +
  1.1647 +		if ($update)
  1.1648 +		{
  1.1649 +			if ($mode == 'style')
  1.1650 +			{
  1.1651 +				$sql = "DELETE FROM $sql_from
  1.1652 +					WHERE {$mode}_id = $style_id";
  1.1653 +				$db->sql_query($sql);
  1.1654 +
  1.1655 +				$sql = 'UPDATE ' . USERS_TABLE . "
  1.1656 +					SET user_style = $new_id
  1.1657 +					WHERE user_style = $style_id";
  1.1658 +				$db->sql_query($sql);
  1.1659 +
  1.1660 +				$sql = 'UPDATE ' . FORUMS_TABLE . "
  1.1661 +					SET forum_style = $new_id
  1.1662 +					WHERE forum_style = $style_id";
  1.1663 +				$db->sql_query($sql);
  1.1664 +
  1.1665 +				if ($style_id == $config['default_style'])
  1.1666 +				{
  1.1667 +					set_config('default_style', $new_id);
  1.1668 +				}
  1.1669 +
  1.1670 +				// Remove the components
  1.1671 +				$components = array('template', 'theme', 'imageset');
  1.1672 +				foreach ($components as $component)
  1.1673 +				{
  1.1674 +					$new_id = request_var('new_' . $component . '_id', 0);
  1.1675 +					$component_id = $style_row[$component . '_id'];
  1.1676 +					$this->remove_component($component, $component_id, $new_id, $style_id);
  1.1677 +				}
  1.1678 +			}
  1.1679 +			else
  1.1680 +			{
  1.1681 +				$this->remove_component($mode, $style_id, $new_id);
  1.1682 +			}
  1.1683 +
  1.1684 +			$cache->destroy('sql', STYLES_TABLE);
  1.1685 +
  1.1686 +			add_log('admin', 'LOG_' . $l_prefix . '_DELETE', $style_row[$mode . '_name']);
  1.1687 +			$message = ($mode != 'style') ? $l_prefix . '_DELETED_FS' : $l_prefix . '_DELETED';
  1.1688 +			trigger_error($user->lang[$message] . adm_back_link($this->u_action));
  1.1689 +		}
  1.1690 +
  1.1691 +		$this->page_title = 'DELETE_' . $l_prefix;
  1.1692 +
  1.1693 +		$template->assign_vars(array(
  1.1694 +			'S_DELETE'			=> true,
  1.1695 +
  1.1696 +			'L_TITLE'			=> $user->lang[$this->page_title],
  1.1697 +			'L_EXPLAIN'			=> $user->lang[$this->page_title . '_EXPLAIN'],
  1.1698 +			'L_NAME'			=> $user->lang[$l_prefix . '_NAME'],
  1.1699 +			'L_REPLACE'			=> $user->lang['REPLACE_' . $l_prefix],
  1.1700 +			'L_REPLACE_EXPLAIN'	=> $user->lang['REPLACE_' . $l_prefix . '_EXPLAIN'],
  1.1701 +
  1.1702 +			'U_ACTION'		=> $this->u_action . "&amp;action=delete&amp;id=$style_id",
  1.1703 +			'U_BACK'		=> $this->u_action,
  1.1704 +
  1.1705 +			'NAME'			=> $style_row[$mode . '_name'],
  1.1706 +			)
  1.1707 +		);
  1.1708 +
  1.1709 +		if ($mode == 'style')
  1.1710 +		{
  1.1711 +			$template->assign_vars(array(
  1.1712 +				'S_DELETE_STYLE'		=> true,
  1.1713 +			));
  1.1714 +		}
  1.1715 +	}
  1.1716 +
  1.1717 +	/**
  1.1718 +	* Remove template/theme/imageset entry from the database
  1.1719 +	*/
  1.1720 +	function remove_component($component, $component_id, $new_id, $style_id = false)
  1.1721 +	{
  1.1722 +		global $db;
  1.1723 +
  1.1724 +		if (($new_id == 0) || ($component === 'template' && ($conflicts = $this->check_inheritance($component, $component_id))))
  1.1725 +		{
  1.1726 +			// We can not delete the template, as the user wants to keep the component or an other template is inheriting from this one.
  1.1727 +			return;
  1.1728 +		}
  1.1729 +
  1.1730 +		$component_in_use = array();
  1.1731 +		if ($component != 'style')
  1.1732 +		{
  1.1733 +			$component_in_use = $this->component_in_use($component, $component_id, $style_id);
  1.1734 +		}
  1.1735 +
  1.1736 +		if (($new_id == -1) && !empty($component_in_use))
  1.1737 +		{
  1.1738 +			// We can not delete the component, as it is still in use
  1.1739 +			return;
  1.1740 +		}
  1.1741 +
  1.1742 +		if ($component == 'imageset')
  1.1743 +		{
  1.1744 +			$sql = 'DELETE FROM ' . STYLES_IMAGESET_DATA_TABLE . "
  1.1745 +				WHERE imageset_id = $component_id";
  1.1746 +			$db->sql_query($sql);
  1.1747 +		}
  1.1748 +
  1.1749 +		switch ($component)
  1.1750 +		{
  1.1751 +			case 'template':
  1.1752 +				$sql_from = STYLES_TEMPLATE_TABLE;
  1.1753 +			break;
  1.1754 +
  1.1755 +			case 'theme':
  1.1756 +				$sql_from = STYLES_THEME_TABLE;
  1.1757 +			break;
  1.1758 +
  1.1759 +			case 'imageset':
  1.1760 +				$sql_from = STYLES_IMAGESET_TABLE;;
  1.1761 +			break;
  1.1762 +		}
  1.1763 +
  1.1764 +		$sql = "DELETE FROM $sql_from
  1.1765 +			WHERE {$component}_id = $component_id";
  1.1766 +		$db->sql_query($sql);
  1.1767 +
  1.1768 +		$sql = 'UPDATE ' . STYLES_TABLE . "
  1.1769 +			SET {$component}_id = $new_id
  1.1770 +			WHERE {$component}_id = $component_id";
  1.1771 +		$db->sql_query($sql);
  1.1772 +	}
  1.1773 +
  1.1774 +	/**
  1.1775 +	* Display the options which can be used to replace a style/template/theme/imageset
  1.1776 +	*
  1.1777 +	* @return boolean Returns true if the component is the only component and can not be deleted.
  1.1778 +	*/
  1.1779 +	function display_component_options($component, $component_id, $style_row = false, $style_id = false)
  1.1780 +	{
  1.1781 +		global $db, $template, $user;
  1.1782 +
  1.1783 +		$is_only_component = true;
  1.1784 +		$component_in_use = array();
  1.1785 +		if ($component != 'style')
  1.1786 +		{
  1.1787 +			$component_in_use = $this->component_in_use($component, $component_id, $style_id);
  1.1788 +		}
  1.1789 +
  1.1790 +		$sql_where = '';
  1.1791 +		switch ($component)
  1.1792 +		{
  1.1793 +			case 'style':
  1.1794 +				$sql_from = STYLES_TABLE;
  1.1795 +				$sql_where = 'WHERE style_active = 1';
  1.1796 +			break;
  1.1797 +
  1.1798 +			case 'template':
  1.1799 +				$sql_from = STYLES_TEMPLATE_TABLE;
  1.1800 +				$sql_where = 'WHERE template_inherits_id <> ' . $component_id;
  1.1801 +			break;
  1.1802 +
  1.1803 +			case 'theme':
  1.1804 +				$sql_from = STYLES_THEME_TABLE;
  1.1805 +			break;
  1.1806 +
  1.1807 +			case 'imageset':
  1.1808 +				$sql_from = STYLES_IMAGESET_TABLE;
  1.1809 +			break;
  1.1810 +		}
  1.1811 +
  1.1812 +		$s_options = '';
  1.1813 +		if (($component != 'style') && empty($component_in_use))
  1.1814 +		{
  1.1815 +			// If it is not in use, there must be another component
  1.1816 +			$is_only_component = false;
  1.1817 +
  1.1818 +			$sql = "SELECT {$component}_id, {$component}_name
  1.1819 +				FROM $sql_from
  1.1820 +				WHERE {$component}_id = {$component_id}";
  1.1821 +			$result = $db->sql_query($sql);
  1.1822 +			$row = $db->sql_fetchrow($result);
  1.1823 +			$db->sql_freeresult($result);
  1.1824 +
  1.1825 +			$s_options .= '<option value="-1" selected="selected">' . $user->lang['DELETE_' . strtoupper($component)] . '</option>';
  1.1826 +			$s_options .= '<option value="0">' . sprintf($user->lang['KEEP_' . strtoupper($component)], $row[$component . '_name']) . '</option>';
  1.1827 +		}
  1.1828 +		else
  1.1829 +		{
  1.1830 +			$sql = "SELECT {$component}_id, {$component}_name
  1.1831 +				FROM $sql_from
  1.1832 +				$sql_where
  1.1833 +				ORDER BY {$component}_name ASC";
  1.1834 +			$result = $db->sql_query($sql);
  1.1835 +
  1.1836 +			$s_keep_option = $s_options = '';
  1.1837 +			while ($row = $db->sql_fetchrow($result))
  1.1838 +			{
  1.1839 +				if ($row[$component . '_id'] != $component_id)
  1.1840 +				{
  1.1841 +					$is_only_component = false;
  1.1842 +					$s_options .= '<option value="' . $row[$component . '_id'] . '">' . sprintf($user->lang['REPLACE_WITH_OPTION'], $row[$component . '_name']) . '</option>';
  1.1843 +				}
  1.1844 +				else if ($component != 'style')
  1.1845 +				{
  1.1846 +					$s_keep_option = '<option value="0" selected="selected">' . sprintf($user->lang['KEEP_' . strtoupper($component)], $row[$component . '_name']) . '</option>';
  1.1847 +				}
  1.1848 +			}
  1.1849 +			$db->sql_freeresult($result);
  1.1850 +			$s_options = $s_keep_option . $s_options;
  1.1851 +		}
  1.1852 +
  1.1853 +		if (!$style_row)
  1.1854 +		{
  1.1855 +			$template->assign_var('S_REPLACE_' . strtoupper($component) . '_OPTIONS', $s_options);
  1.1856 +		}
  1.1857 +		else
  1.1858 +		{
  1.1859 +			$template->assign_var('S_REPLACE_OPTIONS', $s_options);
  1.1860 +			if ($component == 'style')
  1.1861 +			{
  1.1862 +				$components = array('template', 'theme', 'imageset');
  1.1863 +				foreach ($components as $component)
  1.1864 +				{
  1.1865 +					$this->display_component_options($component, $style_row[$component . '_id'], false, $component_id, true);
  1.1866 +				}
  1.1867 +			}
  1.1868 +		}
  1.1869 +
  1.1870 +		return $is_only_component;
  1.1871 +	}
  1.1872 +
  1.1873 +	/**
  1.1874 +	* Check whether the component is still used by another style or component
  1.1875 +	*/
  1.1876 +	function component_in_use($component, $component_id, $style_id = false)
  1.1877 +	{
  1.1878 +		global $db;
  1.1879 +
  1.1880 +		$component_in_use = array();
  1.1881 +
  1.1882 +		if ($style_id)
  1.1883 +		{
  1.1884 +			$sql = 'SELECT style_id, style_name
  1.1885 +				FROM ' . STYLES_TABLE . "
  1.1886 +				WHERE {$component}_id = {$component_id}
  1.1887 +					AND style_id <> {$style_id}
  1.1888 +				ORDER BY style_name ASC";
  1.1889 +		}
  1.1890 +		else
  1.1891 +		{
  1.1892 +			$sql = 'SELECT style_id, style_name
  1.1893 +				FROM ' . STYLES_TABLE . "
  1.1894 +				WHERE {$component}_id = {$component_id}
  1.1895 +				ORDER BY style_name ASC";
  1.1896 +		}
  1.1897 +		$result = $db->sql_query($sql);
  1.1898 +		while ($row = $db->sql_fetchrow($result))
  1.1899 +		{
  1.1900 +			$component_in_use[] = $row['style_name'];
  1.1901 +		}
  1.1902 +		$db->sql_freeresult($result);
  1.1903 +
  1.1904 +		if ($component === 'template' && ($conflicts = $this->check_inheritance($component, $component_id)))
  1.1905 +		{
  1.1906 +			foreach ($conflicts as $temp_id => $conflict_data)
  1.1907 +			{
  1.1908 +				$component_in_use[] = $conflict_data['template_name'];
  1.1909 +			}
  1.1910 +		}
  1.1911 +
  1.1912 +		return $component_in_use;
  1.1913 +	}
  1.1914 +
  1.1915 +	/**
  1.1916 +	* Export style or style elements
  1.1917 +	*/
  1.1918 +	function export($mode, $style_id)
  1.1919 +	{
  1.1920 +		global $db, $template, $user, $phpbb_root_path, $cache, $phpEx, $config;
  1.1921 +
  1.1922 +		$update = (isset($_POST['update'])) ? true : false;
  1.1923 +
  1.1924 +		$inc_template = request_var('inc_template', 0);
  1.1925 +		$inc_theme = request_var('inc_theme', 0);
  1.1926 +		$inc_imageset = request_var('inc_imageset', 0);
  1.1927 +		$store = request_var('store', 0);
  1.1928 +		$format = request_var('format', '');
  1.1929 +
  1.1930 +		$error = array();
  1.1931 +		$methods = array('tar');
  1.1932 +
  1.1933 +		$available_methods = array('tar.gz' => 'zlib', 'tar.bz2' => 'bz2', 'zip' => 'zlib');
  1.1934 +		foreach ($available_methods as $type => $module)
  1.1935 +		{
  1.1936 +			if (!@extension_loaded($module))
  1.1937 +			{
  1.1938 +				continue;
  1.1939 +			}
  1.1940 +
  1.1941 +			$methods[] = $type;
  1.1942 +		}
  1.1943 +
  1.1944 +		if (!in_array($format, $methods))
  1.1945 +		{
  1.1946 +			$format = 'tar';
  1.1947 +		}
  1.1948 +
  1.1949 +		switch ($mode)
  1.1950 +		{
  1.1951 +			case 'style':
  1.1952 +				if ($update && ($inc_template + $inc_theme + $inc_imageset) < 1)
  1.1953 +				{
  1.1954 +					$error[] = $user->lang['STYLE_ERR_MORE_ELEMENTS'];
  1.1955 +				}
  1.1956 +
  1.1957 +				$name = 'style_name';
  1.1958 +
  1.1959 +				$sql_select = 's.style_id, s.style_name, s.style_copyright';
  1.1960 +				$sql_select .= ($inc_template) ? ', t.*' : ', t.template_name';
  1.1961 +				$sql_select .= ($inc_theme) ? ', c.*' : ', c.theme_name';
  1.1962 +				$sql_select .= ($inc_imageset) ? ', i.*' : ', i.imageset_name';
  1.1963 +				$sql_from = STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . ' i';
  1.1964 +				$sql_where = "s.style_id = $style_id AND t.template_id = s.template_id AND c.theme_id = s.theme_id AND i.imageset_id = s.imageset_id";
  1.1965 +
  1.1966 +				$l_prefix = 'STYLE';
  1.1967 +			break;
  1.1968 +
  1.1969 +			case 'template':
  1.1970 +				$name = 'template_name';
  1.1971 +
  1.1972 +				$sql_select = '*';
  1.1973 +				$sql_from = STYLES_TEMPLATE_TABLE;
  1.1974 +				$sql_where = "template_id = $style_id";
  1.1975 +
  1.1976 +				$l_prefix = 'TEMPLATE';
  1.1977 +			break;
  1.1978 +
  1.1979 +			case 'theme':
  1.1980 +				$name = 'theme_name';
  1.1981 +
  1.1982 +				$sql_select = '*';
  1.1983 +				$sql_from = STYLES_THEME_TABLE;
  1.1984 +				$sql_where = "theme_id = $style_id";
  1.1985 +
  1.1986 +				$l_prefix = 'THEME';
  1.1987 +			break;
  1.1988 +
  1.1989 +			case 'imageset':
  1.1990 +				$name = 'imageset_name';
  1.1991 +
  1.1992 +				$sql_select = '*';
  1.1993 +				$sql_from = STYLES_IMAGESET_TABLE;
  1.1994 +				$sql_where = "imageset_id = $style_id";
  1.1995 +
  1.1996 +				$l_prefix = 'IMAGESET';
  1.1997 +			break;
  1.1998 +		}
  1.1999 +
  1.2000 +		if ($update && !sizeof($error))
  1.2001 +		{
  1.2002 +			$sql = "SELECT $sql_select
  1.2003 +				FROM $sql_from
  1.2004 +				WHERE $sql_where";
  1.2005 +			$result = $db->sql_query($sql);
  1.2006 +			$style_row = $db->sql_fetchrow($result);
  1.2007 +			$db->sql_freeresult($result);
  1.2008 +
  1.2009 +			if (!$style_row)
  1.2010 +			{
  1.2011 +				trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
  1.2012 +			}
  1.2013 +
  1.2014 +			$var_ary = array('style_id', 'style_name', 'style_copyright', 'template_id', 'template_name', 'template_path', 'template_copyright', 'template_storedb', 'template_inherits_id', 'bbcode_bitfield', 'theme_id', 'theme_name', 'theme_path', 'theme_copyright', 'theme_storedb', 'theme_mtime', 'theme_data', 'imageset_id', 'imageset_name', 'imageset_path', 'imageset_copyright');
  1.2015 +
  1.2016 +			foreach ($var_ary as $var)
  1.2017 +			{
  1.2018 +				if (!isset($style_row[$var]))
  1.2019 +				{
  1.2020 +					$style_row[$var] = '';
  1.2021 +				}
  1.2022 +			}
  1.2023 +
  1.2024 +			$files = $data = array();
  1.2025 +
  1.2026 +			if ($mode == 'style')
  1.2027 +			{
  1.2028 +				$style_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['style_name'], $style_row['style_copyright'], $config['version']), $this->style_cfg);
  1.2029 +
  1.2030 +				$style_cfg .= (!$inc_template) ? "\nrequired_template = {$style_row['template_name']}" : '';
  1.2031 +				$style_cfg .= (!$inc_theme) ? "\nrequired_theme = {$style_row['theme_name']}" : '';
  1.2032 +				$style_cfg .= (!$inc_imageset) ? "\nrequired_imageset = {$style_row['imageset_name']}" : '';
  1.2033 +
  1.2034 +				$data[] = array(
  1.2035 +					'src'		=> $style_cfg,
  1.2036 +					'prefix'	=> 'style.cfg'
  1.2037 +				);
  1.2038 +
  1.2039 +				unset($style_cfg);
  1.2040 +			}
  1.2041 +
  1.2042 +			// Export template core code
  1.2043 +			if ($mode == 'template' || $inc_template)
  1.2044 +			{
  1.2045 +				$template_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['template_name'], $style_row['template_copyright'], $config['version']), $this->template_cfg);
  1.2046 +
  1.2047 +				$use_template_name = '';
  1.2048 +
  1.2049 +				// Add the inherit from variable, depending on it's use...
  1.2050 +				if ($style_row['template_inherits_id'])
  1.2051 +				{
  1.2052 +					// Get the template name
  1.2053 +					$sql = 'SELECT template_name
  1.2054 +						FROM ' . STYLES_TEMPLATE_TABLE . '
  1.2055 +						WHERE template_id = ' . (int) $style_row['template_inherits_id'];
  1.2056 +					$result = $db->sql_query($sql);
  1.2057 +					$use_template_name = (string) $db->sql_fetchfield('template_name');
  1.2058 +					$db->sql_freeresult($result);
  1.2059 +				}
  1.2060 +
  1.2061 +				$template_cfg .= ($use_template_name) ? "\ninherit_from = $use_template_name" : "\n#inherit_from = ";
  1.2062 +				$template_cfg .= "\n\nbbcode_bitfield = {$style_row['bbcode_bitfield']}";
  1.2063 +
  1.2064 +				$data[] = array(
  1.2065 +					'src'		=> $template_cfg,
  1.2066 +					'prefix'	=> 'template/template.cfg'
  1.2067 +				);
  1.2068 +
  1.2069 +				// This is potentially nasty memory-wise ...
  1.2070 +				if (!$style_row['template_storedb'])
  1.2071 +				{
  1.2072 +					$files[] = array(
  1.2073 +						'src'		=> "styles/{$style_row['template_path']}/template/",
  1.2074 +						'prefix-'	=> "styles/{$style_row['template_path']}/",
  1.2075 +						'prefix+'	=> false,
  1.2076 +						'exclude'	=> 'template.cfg'
  1.2077 +					);
  1.2078 +				}
  1.2079 +				else
  1.2080 +				{
  1.2081 +					$sql = 'SELECT template_filename, template_data
  1.2082 +						FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
  1.2083 +						WHERE template_id = {$style_row['template_id']}";
  1.2084 +					$result = $db->sql_query($sql);
  1.2085 +
  1.2086 +					while ($row = $db->sql_fetchrow($result))
  1.2087 +					{
  1.2088 +						$data[] = array(
  1.2089 +							'src' => $row['template_data'],
  1.2090 +							'prefix' => 'template/' . $row['template_filename']
  1.2091 +						);
  1.2092 +					}
  1.2093 +					$db->sql_freeresult($result);
  1.2094 +				}
  1.2095 +				unset($template_cfg);
  1.2096 +			}
  1.2097 +
  1.2098 +			// Export theme core code
  1.2099 +			if ($mode == 'theme' || $inc_theme)
  1.2100 +			{
  1.2101 +				$theme_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['theme_name'], $style_row['theme_copyright'], $config['version']), $this->theme_cfg);
  1.2102 +
  1.2103 +				// Read old cfg file
  1.2104 +				$items = $cache->obtain_cfg_items($style_row);
  1.2105 +				$items = $items['theme'];
  1.2106 +
  1.2107 +				if (!isset($items['parse_css_file']))
  1.2108 +				{
  1.2109 +					$items['parse_css_file'] = 'off';
  1.2110 +				}
  1.2111 +
  1.2112 +				$theme_cfg = str_replace(array('{PARSE_CSS_FILE}'), array($items['parse_css_file']), $theme_cfg);
  1.2113 +
  1.2114 +				$files[] = array(
  1.2115 +					'src'		=> "styles/{$style_row['theme_path']}/theme/",
  1.2116 +					'prefix-'	=> "styles/{$style_row['theme_path']}/",
  1.2117 +					'prefix+'	=> false,
  1.2118 +					'exclude'	=> ($style_row['theme_storedb']) ? 'stylesheet.css,theme.cfg' : 'theme.cfg'
  1.2119 +				);
  1.2120 +
  1.2121 +				$data[] = array(
  1.2122 +					'src'		=> $theme_cfg,
  1.2123 +					'prefix'	=> 'theme/theme.cfg'
  1.2124 +				);
  1.2125 +
  1.2126 +				if ($style_row['theme_storedb'])
  1.2127 +				{
  1.2128 +					$data[] = array(
  1.2129 +						'src'		=> $style_row['theme_data'],
  1.2130 +						'prefix'	=> 'theme/stylesheet.css'
  1.2131 +					);
  1.2132 +				}
  1.2133 +
  1.2134 +				unset($items, $theme_cfg);
  1.2135 +			}
  1.2136 +
  1.2137 +			// Export imageset core code
  1.2138 +			if ($mode == 'imageset' || $inc_imageset)
  1.2139 +			{
  1.2140 +				$imageset_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['imageset_name'], $style_row['imageset_copyright'], $config['version']), $this->imageset_cfg);
  1.2141 +
  1.2142 +				$imageset_main = array();
  1.2143 +
  1.2144 +				$sql = 'SELECT image_filename, image_name, image_height, image_width
  1.2145 +					FROM ' . STYLES_IMAGESET_DATA_TABLE . "
  1.2146 +					WHERE imageset_id = $style_id
  1.2147 +						AND image_lang = ''";
  1.2148 +				$result = $db->sql_query($sql);
  1.2149 +				while ($row = $db->sql_fetchrow($result))
  1.2150 +				{
  1.2151 +					$imageset_main[$row['image_name']] = $row['image_filename'] . ($row['image_height'] ? '*' . $row['image_height']: '') . ($row['image_width'] ? '*' . $row['image_width']: '');
  1.2152 +				}
  1.2153 +				$db->sql_freeresult($result);
  1.2154 +
  1.2155 +				foreach ($this->imageset_keys as $topic => $key_array)
  1.2156 +				{
  1.2157 +					foreach ($key_array as $key)
  1.2158 +					{
  1.2159 +						if (isset($imageset_main[$key]))
  1.2160 +						{
  1.2161 +							$imageset_cfg .= "\nimg_" . $key . ' = ' . str_replace("styles/{$style_row['imageset_path']}/imageset/", '{PATH}', $imageset_main[$key]);
  1.2162 +						}
  1.2163 +					}
  1.2164 +				}
  1.2165 +
  1.2166 +				$files[] = array(
  1.2167 +					'src'		=> "styles/{$style_row['imageset_path']}/imageset/",
  1.2168 +					'prefix-'	=> "styles/{$style_row['imageset_path']}/",
  1.2169 +					'prefix+'	=> false,
  1.2170 +					'exclude'	=> 'imageset.cfg'
  1.2171 +				);
  1.2172 +
  1.2173 +				$data[] = array(
  1.2174 +					'src'		=> trim($imageset_cfg),
  1.2175 +					'prefix'	=> 'imageset/imageset.cfg'
  1.2176 +				);
  1.2177 +
  1.2178 +				end($data);
  1.2179 +
  1.2180 +				$imageset_root = "{$phpbb_root_path}styles/{$style_row['imageset_path']}/imageset/";
  1.2181 +
  1.2182 +				if ($dh = @opendir($imageset_root))
  1.2183 +				{
  1.2184 +					while (($fname = readdir($dh)) !== false)
  1.2185 +					{
  1.2186 +						if ($fname[0] != '.' && $fname != 'CVS' && is_dir("$imageset_root$fname"))
  1.2187 +						{
  1.2188 +							$files[key($files)]['exclude'] .= ',' . $fname . '/imageset.cfg';
  1.2189 +						}
  1.2190 +					}
  1.2191 +					closedir($dh);
  1.2192 +				}
  1.2193 +
  1.2194 +				$imageset_lang = array();
  1.2195 +
  1.2196 +				$sql = 'SELECT image_filename, image_name, image_height, image_width, image_lang
  1.2197 +					FROM ' . STYLES_IMAGESET_DATA_TABLE . "
  1.2198 +					WHERE imageset_id = $style_id
  1.2199 +						AND image_lang <> ''";
  1.2200 +				$result = $db->sql_query($sql);
  1.2201 +				while ($row = $db->sql_fetchrow($result))
  1.2202 +				{
  1.2203 +					$imageset_lang[$row['image_lang']][$row['image_name']] = $row['image_filename'] . ($row['image_height'] ? '*' . $row['image_height']: '') . ($row['image_width'] ? '*' . $row['image_width']: '');
  1.2204 +				}
  1.2205 +				$db->sql_freeresult($result);
  1.2206 +
  1.2207 +				foreach ($imageset_lang as $lang => $imageset_localized)
  1.2208 +				{
  1.2209 +					$imageset_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['imageset_name'], $style_row['imageset_copyright'], $config['version']), $this->imageset_cfg);
  1.2210 +
  1.2211 +					foreach ($this->imageset_keys as $topic => $key_array)
  1.2212 +					{
  1.2213 +						foreach ($key_array as $key)
  1.2214 +						{
  1.2215 +							if (isset($imageset_localized[$key]))
  1.2216 +							{
  1.2217 +								$imageset_cfg .= "\nimg_" . $key . ' = ' . str_replace("styles/{$style_row['imageset_path']}/imageset/", '{PATH}', $imageset_localized[$key]);
  1.2218 +							}
  1.2219 +						}
  1.2220 +					}
  1.2221 +
  1.2222 +					$data[] = array(
  1.2223 +						'src'		=> trim($imageset_cfg),
  1.2224 +						'prefix'	=> 'imageset/' . $lang . '/imageset.cfg'
  1.2225 +					);
  1.2226 +				}
  1.2227 +
  1.2228 +				unset($imageset_cfg);
  1.2229 +			}
  1.2230 +
  1.2231 +			switch ($format)
  1.2232 +			{
  1.2233 +				case 'tar':
  1.2234 +					$ext = '.tar';
  1.2235 +				break;
  1.2236 +
  1.2237 +				case 'zip':
  1.2238 +					$ext = '.zip';
  1.2239 +				break;
  1.2240 +
  1.2241 +				case 'tar.gz':
  1.2242 +					$ext = '.tar.gz';
  1.2243 +				break;
  1.2244 +
  1.2245 +				case 'tar.bz2':
  1.2246 +					$ext = '.tar.bz2';
  1.2247 +				break;
  1.2248 +
  1.2249 +				default:
  1.2250 +					$error[] = $user->lang[$l_prefix . '_ERR_ARCHIVE'];
  1.2251 +			}
  1.2252 +
  1.2253 +			if (!sizeof($error))
  1.2254 +			{
  1.2255 +				include($phpbb_root_path . 'includes/functions_compress.' . $phpEx);
  1.2256 +
  1.2257 +				if ($mode == 'style')
  1.2258 +				{
  1.2259 +					$path = preg_replace('#[^\w-]+#', '_', $style_row['style_name']);
  1.2260 +				}
  1.2261 +				else
  1.2262 +				{
  1.2263 +					$path = $style_row[$mode . '_path'];
  1.2264 +				}
  1.2265 +
  1.2266 +				if ($format == 'zip')
  1.2267 +				{
  1.2268 +					$compress = new compress_zip('w', $phpbb_root_path . "store/$path$ext");
  1.2269 +				}
  1.2270 +				else
  1.2271 +				{
  1.2272 +					$compress = new compress_tar('w', $phpbb_root_path . "store/$path$ext", $ext);
  1.2273 +				}
  1.2274 +
  1.2275 +				if (sizeof($files))
  1.2276 +				{
  1.2277 +					foreach ($files as $file_ary)
  1.2278 +					{
  1.2279 +						$compress->add_file($file_ary['src'], $file_ary['prefix-'], $file_ary['prefix+'], $file_ary['exclude']);
  1.2280 +					}
  1.2281 +				}
  1.2282 +
  1.2283 +				if (sizeof($data))
  1.2284 +				{
  1.2285 +					foreach ($data as $data_ary)
  1.2286 +					{
  1.2287 +						$compress->add_data($data_ary['src'], $data_ary['prefix']);
  1.2288 +					}
  1.2289 +				}
  1.2290 +
  1.2291 +				$compress->close();
  1.2292 +
  1.2293 +				add_log('admin', 'LOG_' . $l_prefix . '_EXPORT', $style_row[$mode . '_name']);
  1.2294 +
  1.2295 +				if (!$store)
  1.2296 +				{
  1.2297 +					$compress->download($path);
  1.2298 +					@unlink("{$phpbb_root_path}store/$path$ext");
  1.2299 +					exit;
  1.2300 +				}
  1.2301 +
  1.2302 +				trigger_error(sprintf($user->lang[$l_prefix . '_EXPORTED'], "store/$path$ext") . adm_back_link($this->u_action));
  1.2303 +			}
  1.2304 +		}
  1.2305 +
  1.2306 +		$sql = "SELECT {$mode}_id, {$mode}_name
  1.2307 +			FROM " . (($mode == 'style') ? STYLES_TABLE : $sql_from) . "
  1.2308 +			WHERE {$mode}_id = $style_id";
  1.2309 +		$result = $db->sql_query($sql);
  1.2310 +		$style_row = $db->sql_fetchrow($result);
  1.2311 +		$db->sql_freeresult($result);
  1.2312 +
  1.2313 +		if (!$style_row)
  1.2314 +		{
  1.2315 +			trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
  1.2316 +		}
  1.2317 +
  1.2318 +		$this->page_title = $l_prefix . '_EXPORT';
  1.2319 +
  1.2320 +		$format_buttons = '';
  1.2321 +		foreach ($methods as $method)
  1.2322 +		{
  1.2323 +			$format_buttons .= '<label><input type="radio"' . ((!$format_buttons) ? ' id="format"' : '') . ' class="radio" value="' . $method . '" name="format"' . (($method == $format) ? ' checked="checked"' : '') . ' /> ' . $method . '</label>';
  1.2324 +		}
  1.2325 +
  1.2326 +		$template->assign_vars(array(
  1.2327 +			'S_EXPORT'		=> true,
  1.2328 +			'S_ERROR_MSG'	=> (sizeof($error)) ? true : false,
  1.2329 +			'S_STYLE'		=> ($mode == 'style') ? true : false,
  1.2330 +
  1.2331 +			'L_TITLE'		=> $user->lang[$this->page_title],
  1.2332 +			'L_EXPLAIN'		=> $user->lang[$this->page_title . '_EXPLAIN'],
  1.2333 +			'L_NAME'		=> $user->lang[$l_prefix . '_NAME'],
  1.2334 +
  1.2335 +			'U_ACTION'		=> $this->u_action . '&amp;action=export&amp;id=' . $style_id,
  1.2336 +			'U_BACK'		=> $this->u_action,
  1.2337 +
  1.2338 +			'ERROR_MSG'			=> (sizeof($error)) ? implode('<br />', $error) : '',
  1.2339 +			'NAME'				=> $style_row[$mode . '_name'],
  1.2340 +			'FORMAT_BUTTONS'	=> $format_buttons)
  1.2341 +		);
  1.2342 +	}
  1.2343 +
  1.2344 +	/**
  1.2345 +	* Display details
  1.2346 +	*/
  1.2347 +	function details($mode, $style_id)
  1.2348 +	{
  1.2349 +		global $template, $db, $config, $user, $safe_mode, $cache, $phpbb_root_path;
  1.2350 +
  1.2351 +		$update = (isset($_POST['update'])) ? true : false;
  1.2352 +		$l_type = strtoupper($mode);
  1.2353 +
  1.2354 +		$error = array();
  1.2355 +		$element_ary = array('template' => STYLES_TEMPLATE_TABLE, 'theme' => STYLES_THEME_TABLE, 'imageset' => STYLES_IMAGESET_TABLE);
  1.2356 +
  1.2357 +		switch ($mode)
  1.2358 +		{
  1.2359 +			case 'style':
  1.2360 +				$sql_from = STYLES_TABLE;
  1.2361 +			break;
  1.2362 +
  1.2363 +			case 'template':
  1.2364 +				$sql_from = STYLES_TEMPLATE_TABLE;
  1.2365 +			break;
  1.2366 +
  1.2367 +			case 'theme':
  1.2368 +				$sql_from = STYLES_THEME_TABLE;
  1.2369 +			break;
  1.2370 +
  1.2371 +			case 'imageset':
  1.2372 +				$sql_from = STYLES_IMAGESET_TABLE;
  1.2373 +			break;
  1.2374 +		}
  1.2375 +
  1.2376 +		$sql = "SELECT *
  1.2377 +			FROM $sql_from
  1.2378 +			WHERE {$mode}_id = $style_id";
  1.2379 +		$result = $db->sql_query($sql);
  1.2380 +		$style_row = $db->sql_fetchrow($result);
  1.2381 +		$db->sql_freeresult($result);
  1.2382 +
  1.2383 +		if (!$style_row)
  1.2384 +		{
  1.2385 +			trigger_error($user->lang['NO_' . $l_type] . adm_back_link($this->u_action), E_USER_WARNING);
  1.2386 +		}
  1.2387 +
  1.2388 +		$style_row['style_default'] = ($mode == 'style' && $config['default_style'] == $style_id) ? 1 : 0;
  1.2389 +
  1.2390 +		if ($update)
  1.2391 +		{
  1.2392 +			$name = utf8_normalize_nfc(request_var('name', '', true));
  1.2393 +			$copyright = utf8_normalize_nfc(request_var('copyright', '', true));
  1.2394 +
  1.2395 +			$template_id = request_var('template_id', 0);
  1.2396 +			$theme_id = request_var('theme_id', 0);
  1.2397 +			$imageset_id = request_var('imageset_id', 0);
  1.2398 +
  1.2399 +			$style_active = request_var('style_active', 0);
  1.2400 +			$style_default = request_var('style_default', 0);
  1.2401 +			$store_db = request_var('store_db', 0);
  1.2402 +
  1.2403 +			// If the admin selected the style to be the default style, but forgot to activate it... we will do it for him
  1.2404 +			if ($style_default)
  1.2405 +			{
  1.2406 +				$style_active = 1;
  1.2407 +			}
  1.2408 +
  1.2409 +			$sql = "SELECT {$mode}_id, {$mode}_name
  1.2410 +				FROM $sql_from
  1.2411 +				WHERE {$mode}_id <> $style_id
  1.2412 +				AND LOWER({$mode}_name) = '" . $db->sql_escape(strtolower($name)) . "'";
  1.2413 +			$result = $db->sql_query($sql);
  1.2414 +			$conflict = $db->sql_fetchrow($result);
  1.2415 +			$db->sql_freeresult($result);
  1.2416 +
  1.2417 +			if ($mode == 'style' && (!$template_id || !$theme_id || !$imageset_id))
  1.2418 +			{
  1.2419 +				$error[] = $user->lang['STYLE_ERR_NO_IDS'];
  1.2420 +			}
  1.2421 +
  1.2422 +			if ($mode == 'style' && $style_row['style_active'] && !$style_active && $config['default_style'] == $style_id)
  1.2423 +			{
  1.2424 +				$error[] = $user->lang['DEACTIVATE_DEFAULT'];
  1.2425 +			}
  1.2426 +
  1.2427 +			if (!$name || $conflict)
  1.2428 +			{
  1.2429 +				$error[] = $user->lang[$l_type . '_ERR_STYLE_NAME'];
  1.2430 +			}
  1.2431 +
  1.2432 +			if ($mode === 'theme' || $mode === 'template')
  1.2433 +			{
  1.2434 +				// a rather elaborate check we have to do here once to avoid trouble later
  1.2435 +				$check = "{$phpbb_root_path}styles/" . $style_row["{$mode}_path"] . (($mode === 'theme') ? '/theme/stylesheet.css' : '/template');
  1.2436 +				if (($style_row["{$mode}_storedb"] != $store_db) && !$store_db && ($safe_mode || !phpbb_is_writable($check)))
  1.2437 +				{
  1.2438 +					$error[] = $user->lang['EDIT_' . strtoupper($mode) . '_STORED_DB'];
  1.2439 +					$store_db = 1;
  1.2440 +				}
  1.2441 +
  1.2442 +				// themes which have to be parsed have to go into db
  1.2443 +				if ($mode == 'theme')
  1.2444 +				{
  1.2445 +					$cfg = parse_cfg_file("{$phpbb_root_path}styles/" . $style_row["{$mode}_path"] . "/theme/theme.cfg");
  1.2446 +
  1.2447 +					if (isset($cfg['parse_css_file']) && $cfg['parse_css_file'] && !$store_db)
  1.2448 +					{
  1.2449 +						$error[] = $user->lang['EDIT_THEME_STORE_PARSED'];
  1.2450 +						$store_db = 1;
  1.2451 +					}
  1.2452 +				}
  1.2453 +			}
  1.2454 +
  1.2455 +			if (!sizeof($error))
  1.2456 +			{
  1.2457 +				// Check length settings
  1.2458 +				if (utf8_strlen($name) > 30)
  1.2459 +				{
  1.2460 +					$error[] = $user->lang[$l_type . '_ERR_NAME_LONG'];
  1.2461 +				}
  1.2462 +
  1.2463 +				if (utf8_strlen($copyright) > 60)
  1.2464 +				{
  1.2465 +					$error[] = $user->lang[$l_type . '_ERR_COPY_LONG'];
  1.2466 +				}
  1.2467 +			}
  1.2468 +		}
  1.2469 +
  1.2470 +		if ($update && sizeof($error))
  1.2471 +		{
  1.2472 +			$style_row = array_merge($style_row, array(
  1.2473 +				'template_id'			=> $template_id,
  1.2474 +				'theme_id'				=> $theme_id,
  1.2475 +				'imageset_id'			=> $imageset_id,
  1.2476 +				'style_active'			=> $style_active,
  1.2477 +				$mode . '_storedb'		=> $store_db,
  1.2478 +				$mode . '_name'			=> $name,
  1.2479 +				$mode . '_copyright'	=> $copyright)
  1.2480 +			);
  1.2481 +		}
  1.2482 +
  1.2483 +		// User has submitted form and no errors have occurred
  1.2484 +		if ($update && !sizeof($error))
  1.2485 +		{
  1.2486 +			$sql_ary = array(
  1.2487 +				$mode . '_name'			=> $name,
  1.2488 +				$mode . '_copyright'	=> $copyright
  1.2489 +			);
  1.2490 +
  1.2491 +			switch ($mode)
  1.2492 +			{
  1.2493 +				case 'style':
  1.2494 +
  1.2495 +					$sql_ary += array(
  1.2496 +						'template_id'		=> (int) $template_id,
  1.2497 +						'theme_id'			=> (int) $theme_id,
  1.2498 +						'imageset_id'		=> (int) $imageset_id,
  1.2499 +						'style_active'		=> (int) $style_active,
  1.2500 +					);
  1.2501 +				break;
  1.2502 +
  1.2503 +				case 'imageset':
  1.2504 +				break;
  1.2505 +
  1.2506 +				case 'theme':
  1.2507 +
  1.2508 +					if ($style_row['theme_storedb'] != $store_db)
  1.2509 +					{
  1.2510 +						$theme_data = '';
  1.2511 +
  1.2512 +						if (!$style_row['theme_storedb'])
  1.2513 +						{
  1.2514 +							$theme_data = $this->db_theme_data($style_row);
  1.2515 +						}
  1.2516 +						else if (!$store_db && !$safe_mode && phpbb_is_writable("{$phpbb_root_path}styles/{$style_row['theme_path']}/theme/stylesheet.css"))
  1.2517 +						{
  1.2518 +							$store_db = 1;
  1.2519 +							$theme_data = $style_row['theme_data'];
  1.2520 +
  1.2521 +							if ($fp = @fopen("{$phpbb_root_path}styles/{$style_row['theme_path']}/theme/stylesheet.css", 'wb'))
  1.2522 +							{
  1.2523 +								$store_db = (@fwrite($fp, str_replace("styles/{$style_row['theme_path']}/theme/", './', $theme_data))) ? 0 : 1;
  1.2524 +							}
  1.2525 +							fclose($fp);
  1.2526 +						}
  1.2527 +
  1.2528 +						$sql_ary += array(
  1.2529 +							'theme_mtime'	=> ($store_db) ? filemtime("{$phpbb_root_path}styles/{$style_row['theme_path']}/theme/stylesheet.css") : 0,
  1.2530 +							'theme_storedb'	=> $store_db,
  1.2531 +							'theme_data'	=> ($store_db) ? $theme_data : '',
  1.2532 +						);
  1.2533 +					}
  1.2534 +				break;
  1.2535 +
  1.2536 +				case 'template':
  1.2537 +
  1.2538 +					if ($style_row['template_storedb'] != $store_db)
  1.2539 +					{
  1.2540 +						if ($super = $this->get_super($mode, $style_row['template_id']))
  1.2541 +						{
  1.2542 +							$error[] = (sprintf($user->lang["{$l_type}_INHERITS"], $super['template_name']));
  1.2543 +							$sql_ary = array();
  1.2544 +						}
  1.2545 +						else
  1.2546 +						{
  1.2547 +							if (!$store_db && !$safe_mode && phpbb_is_writable("{$phpbb_root_path}styles/{$style_row['template_path']}/template"))
  1.2548 +							{
  1.2549 +								$err = $this->store_in_fs('template', $style_row['template_id']);
  1.2550 +								if ($err)
  1.2551 +								{
  1.2552 +									$error += $err;
  1.2553 +								}
  1.2554 +							}
  1.2555 +							else if ($store_db)
  1.2556 +							{
  1.2557 +								$this->store_in_db('template', $style_row['template_id']);
  1.2558 +							}
  1.2559 +							else
  1.2560 +							{
  1.2561 +								// We no longer store within the db, but are also not able to update the file structure
  1.2562 +								// Since the admin want to switch this, we adhere to his decision. But we also need to remove the cache
  1.2563 +								$sql = 'DELETE FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
  1.2564 +									WHERE template_id = $style_id";
  1.2565 +								$db->sql_query($sql);
  1.2566 +							}
  1.2567 +
  1.2568 +							$sql_ary += array(
  1.2569 +								'template_storedb'	=> $store_db,
  1.2570 +							);
  1.2571 +						}
  1.2572 +					}
  1.2573 +				break;
  1.2574 +			}
  1.2575 +
  1.2576 +			if (sizeof($sql_ary))
  1.2577 +			{
  1.2578 +				$sql = "UPDATE $sql_from
  1.2579 +					SET " . $db->sql_build_array('UPDATE', $sql_ary) . "
  1.2580 +					WHERE {$mode}_id = $style_id";
  1.2581 +				$db->sql_query($sql);
  1.2582 +
  1.2583 +				// Making this the default style?
  1.2584 +				if ($mode == 'style' && $style_default)
  1.2585 +				{
  1.2586 +					set_config('default_style', $style_id);
  1.2587 +				}
  1.2588 +			}
  1.2589 +
  1.2590 +			$cache->destroy('sql', STYLES_TABLE);
  1.2591 +
  1.2592 +			add_log('admin', 'LOG_' . $l_type . '_EDIT_DETAILS', $name);
  1.2593 +			if (sizeof($error))
  1.2594 +			{
  1.2595 +				trigger_error(implode('<br />', $error) . adm_back_link($this->u_action), E_USER_WARNING);
  1.2596 +			}
  1.2597 +			else
  1.2598 +			{
  1.2599 +				trigger_error($user->lang[$l_type . '_DETAILS_UPDATED'] . adm_back_link($this->u_action));
  1.2600 +			}
  1.2601 +		}
  1.2602 +
  1.2603 +		if ($mode == 'style')
  1.2604 +		{
  1.2605 +			foreach ($element_ary as $element => $table)
  1.2606 +			{
  1.2607 +				$sql = "SELECT {$element}_id, {$element}_name
  1.2608 +					FROM $table
  1.2609 +					ORDER BY {$element}_id ASC";
  1.2610 +				$result = $db->sql_query($sql);
  1.2611 +
  1.2612 +				${$element . '_options'} = '';
  1.2613 +				while ($row = $db->sql_fetchrow($result))
  1.2614 +				{
  1.2615 +					$selected = ($row[$element . '_id'] == $style_row[$element . '_id']) ? ' selected="selected"' : '';
  1.2616 +					${$element . '_options'} .= '<option value="' . $row[$element . '_id'] . '"' . $selected . '>' . $row[$element . '_name'] . '</option>';
  1.2617 +				}
  1.2618 +				$db->sql_freeresult($result);
  1.2619 +			}
  1.2620 +		}
  1.2621 +
  1.2622 +		if ($mode == 'template')
  1.2623 +		{
  1.2624 +			$super = array();
  1.2625 +			if (isset($style_row[$mode . '_inherits_id']) && $style_row['template_inherits_id'])
  1.2626 +			{
  1.2627 +				$super = $this->get_super($mode, $style_row['template_id']);
  1.2628 +			}
  1.2629 +		}
  1.2630 +
  1.2631 +		$this->page_title = 'EDIT_DETAILS_' . $l_type;
  1.2632 +
  1.2633 +		$template->assign_vars(array(
  1.2634 +			'S_DETAILS'				=> true,
  1.2635 +			'S_ERROR_MSG'			=> (sizeof($error)) ? true : false,
  1.2636 +			'S_STYLE'				=> ($mode == 'style') ? true : false,
  1.2637 +			'S_TEMPLATE'			=> ($mode == 'template') ? true : false,
  1.2638 +			'S_THEME'				=> ($mode == 'theme') ? true : false,
  1.2639 +			'S_IMAGESET'			=> ($mode == 'imageset') ? true : false,
  1.2640 +			'S_STORE_DB'			=> (isset($style_row[$mode . '_storedb'])) ? $style_row[$mode . '_storedb'] : 0,
  1.2641 +			'S_STORE_DB_DISABLED'	=> (isset($style_row[$mode . '_inherits_id'])) ? $style_row[$mode . '_inherits_id'] : 0,
  1.2642 +			'S_STYLE_ACTIVE'		=> (isset($style_row['style_active'])) ? $style_row['style_active'] : 0,
  1.2643 +			'S_STYLE_DEFAULT'		=> (isset($style_row['style_default'])) ? $style_row['style_default'] : 0,
  1.2644 +			'S_SUPERTEMPLATE'		=> (isset($style_row[$mode . '_inherits_id']) && $style_row[$mode . '_inherits_id']) ? $super['template_name'] : 0,
  1.2645 +
  1.2646 +			'S_TEMPLATE_OPTIONS'	=> ($mode == 'style') ? $template_options : '',
  1.2647 +			'S_THEME_OPTIONS'		=> ($mode == 'style') ? $theme_options : '',
  1.2648 +			'S_IMAGESET_OPTIONS'	=> ($mode == 'style') ? $imageset_options : '',
  1.2649 +
  1.2650 +			'U_ACTION'		=> $this->u_action . '&amp;action=details&amp;id=' . $style_id,
  1.2651 +			'U_BACK'		=> $this->u_action,
  1.2652 +
  1.2653 +			'L_TITLE'				=> $user->lang[$this->page_title],
  1.2654 +			'L_EXPLAIN'				=> $user->lang[$this->page_title . '_EXPLAIN'],
  1.2655 +			'L_NAME'				=> $user->lang[$l_type . '_NAME'],
  1.2656 +			'L_LOCATION'			=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION'] : '',
  1.2657 +			'L_LOCATION_EXPLAIN'	=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION_EXPLAIN'] : '',
  1.2658 +
  1.2659 +			'ERROR_MSG'		=> (sizeof($error)) ? implode('<br />', $error) : '',
  1.2660 +			'NAME'			=> $style_row[$mode . '_name'],
  1.2661 +			'COPYRIGHT'		=> $style_row[$mode . '_copyright'],
  1.2662 +			)
  1.2663 +		);
  1.2664 +	}
  1.2665 +
  1.2666 +	/**
  1.2667 +	* Load css file contents
  1.2668 +	*/
  1.2669 +	function load_css_file($path, $filename)
  1.2670 +	{
  1.2671 +		global $phpbb_root_path;
  1.2672 +
  1.2673 +		$file = "{$phpbb_root_path}styles/$path/theme/$filename";
  1.2674 +
  1.2675 +		if (file_exists($file) && ($content = file_get_contents($file)))
  1.2676 +		{
  1.2677 +			$content = trim($content);
  1.2678 +		}
  1.2679 +		else
  1.2680 +		{
  1.2681 +			$content = '';
  1.2682 +		}
  1.2683 +		if (defined('DEBUG'))
  1.2684 +		{
  1.2685 +			$content = "/* BEGIN @include $filename */ \n $content \n /* END @include $filename */ \n";
  1.2686 +		}
  1.2687 +
  1.2688 +		return $content;
  1.2689 +	}
  1.2690 +
  1.2691 +	/**
  1.2692 +	* Returns a string containing the value that should be used for the theme_data column in the theme database table.
  1.2693 +	* Includes contents of files loaded via @import
  1.2694 +	*
  1.2695 +	* @param array $theme_row is an associative array containing the theme's current database entry
  1.2696 +	* @param mixed $stylesheet can either be the new content for the stylesheet or false to load from the standard file
  1.2697 +	* @param string $root_path should only be used in case you want to use a different root path than "{$phpbb_root_path}styles/{$theme_row['theme_path']}"
  1.2698 +	*
  1.2699 +	* @return string Stylesheet data for theme_data column in the theme table
  1.2700 +	*/
  1.2701 +	function db_theme_data($theme_row, $stylesheet = false, $root_path = '')
  1.2702 +	{
  1.2703 +		global $phpbb_root_path;
  1.2704 +
  1.2705 +		if (!$root_path)
  1.2706 +		{
  1.2707 +			$root_path = $phpbb_root_path . 'styles/' . $theme_row['theme_path'];
  1.2708 +		}
  1.2709 +
  1.2710 +		if (!$stylesheet)
  1.2711 +		{
  1.2712 +			$stylesheet = '';
  1.2713 +			if (file_exists($root_path . '/theme/stylesheet.css'))
  1.2714 +			{
  1.2715 +				$stylesheet = file_get_contents($root_path . '/theme/stylesheet.css');
  1.2716 +			}
  1.2717 +		}
  1.2718 +
  1.2719 +		// Match CSS imports
  1.2720 +		$matches = array();
  1.2721 +		preg_match_all('/@import url\((["\'])(.*)\1\);/i', $stylesheet, $matches);
  1.2722 +
  1.2723 +		// remove commented stylesheets (very simple parser, allows only whitespace
  1.2724 +		// around an @import statement)
  1.2725 +		preg_match_all('#/\*\s*@import url\((["\'])(.*)\1\);\s\*/#i', $stylesheet, $commented);
  1.2726 +		$matches[2] = array_diff($matches[2], $commented[2]);
  1.2727 +
  1.2728 +		if (sizeof($matches))
  1.2729 +		{
  1.2730 +			foreach ($matches[0] as $idx => $match)
  1.2731 +			{
  1.2732 +				if (isset($matches[2][$idx]))
  1.2733 +				{
  1.2734 +					$stylesheet = str_replace($match, acp_styles::load_css_file($theme_row['theme_path'], $matches[2][$idx]), $stylesheet);
  1.2735 +				}
  1.2736 +			}
  1.2737 +		}
  1.2738 +
  1.2739 +		// adjust paths
  1.2740 +		return str_replace('./', 'styles/' . $theme_row['theme_path'] . '/theme/', $stylesheet);
  1.2741 +	}
  1.2742 +
  1.2743 +	/**
  1.2744 +	* Store template files into db
  1.2745 +	*/
  1.2746 +	function store_templates($mode, $style_id, $template_path, $filelist)
  1.2747 +	{
  1.2748 +		global $phpbb_root_path, $phpEx, $db;
  1.2749 +
  1.2750 +		$template_path = $template_path . '/template/';
  1.2751 +		$includes = array();
  1.2752 +		foreach ($filelist as $pathfile => $file_ary)
  1.2753 +		{
  1.2754 +			foreach ($file_ary as $file)
  1.2755 +			{
  1.2756 +				if (!($fp = @fopen("{$phpbb_root_path}styles/$template_path$pathfile$file", 'r')))
  1.2757 +				{
  1.2758 +					trigger_error("Could not open {$phpbb_root_path}styles/$template_path$pathfile$file", E_USER_ERROR);
  1.2759 +				}
  1.2760 +
  1.2761 +				$filesize = filesize("{$phpbb_root_path}styles/$template_path$pathfile$file");
  1.2762 +
  1.2763 +				if ($filesize)
  1.2764 +				{
  1.2765 +					$template_data = fread($fp, $filesize);
  1.2766 +				}
  1.2767 +
  1.2768 +				fclose($fp);
  1.2769 +
  1.2770 +				if (!$filesize)
  1.2771 +				{
  1.2772 +					// File is empty
  1.2773 +					continue;
  1.2774 +				}
  1.2775 +
  1.2776 +				if (preg_match_all('#<!-- INCLUDE (.*?\.html) -->#is', $template_data, $matches))
  1.2777 +				{
  1.2778 +					foreach ($matches[1] as $match)
  1.2779 +					{
  1.2780 +						$includes[trim($match)][] = $file;
  1.2781 +					}
  1.2782 +				}
  1.2783 +			}
  1.2784 +		}
  1.2785 +
  1.2786 +		foreach ($filelist as $pathfile => $file_ary)
  1.2787 +		{
  1.2788 +			foreach ($file_ary as $file)
  1.2789 +			{
  1.2790 +				// Skip index.
  1.2791 +				if (strpos($file, 'index.') === 0)
  1.2792 +				{
  1.2793 +					continue;
  1.2794 +				}
  1.2795 +
  1.2796 +				// We could do this using extended inserts ... but that could be one
  1.2797 +				// heck of a lot of data ...
  1.2798 +				$sql_ary = array(
  1.2799 +					'template_id'			=> (int) $style_id,
  1.2800 +					'template_filename'		=> "$pathfile$file",
  1.2801 +					'template_included'		=> (isset($includes[$file])) ? implode(':', $includes[$file]) . ':' : '',
  1.2802 +					'template_mtime'		=> (int) filemtime("{$phpbb_root_path}styles/$template_path$pathfile$file"),
  1.2803 +					'template_data'			=> (string) file_get_contents("{$phpbb_root_path}styles/$template_path$pathfile$file"),
  1.2804 +				);
  1.2805 +
  1.2806 +				if ($mode == 'insert')
  1.2807 +				{
  1.2808 +					$sql = 'INSERT INTO ' . STYLES_TEMPLATE_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
  1.2809 +				}
  1.2810 +				else
  1.2811 +				{
  1.2812 +					$sql = 'UPDATE ' . STYLES_TEMPLATE_DATA_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
  1.2813 +						WHERE template_id = $style_id
  1.2814 +							AND template_filename = '" . $db->sql_escape("$pathfile$file") . "'";
  1.2815 +				}
  1.2816 +				$db->sql_query($sql);
  1.2817 +			}
  1.2818 +		}
  1.2819 +	}
  1.2820 +
  1.2821 +	/**
  1.2822 +	* Returns an array containing all template filenames for one template that are currently cached.
  1.2823 +	*
  1.2824 +	* @param string $template_path contains the name of the template's folder in /styles/
  1.2825 +	*
  1.2826 +	* @return array of filenames that exist in /styles/$template_path/template/ (without extension!)
  1.2827 +	*/
  1.2828 +	function template_cache_filelist($template_path)
  1.2829 +	{
  1.2830 +		global $phpbb_root_path, $phpEx, $user;
  1.2831 +
  1.2832 +		$cache_prefix = 'tpl_' . str_replace('_', '-', $template_path);
  1.2833 +
  1.2834 +		if (!($dp = @opendir("{$phpbb_root_path}cache")))
  1.2835 +		{
  1.2836 +			trigger_error($user->lang['TEMPLATE_ERR_CACHE_READ'] . adm_back_link($this->u_action), E_USER_WARNING);
  1.2837 +		}
  1.2838 +
  1.2839 +		$file_ary = array();
  1.2840 +		while ($file = readdir($dp))
  1.2841 +		{
  1.2842 +			if ($file[0] == '.')
  1.2843 +			{
  1.2844 +				continue;
  1.2845 +			}
  1.2846 +
  1.2847 +			if (is_file($phpbb_root_path . 'cache/' . $file) && (strpos($file, $cache_prefix) === 0))
  1.2848 +			{
  1.2849 +				$file_ary[] = str_replace('.', '/', preg_replace('#^' . preg_quote($cache_prefix, '#') . '_(.*?)\.html\.' . $phpEx . '$#i', '\1', $file));
  1.2850 +			}
  1.2851 +		}
  1.2852 +		closedir($dp);
  1.2853 +
  1.2854 +		return $file_ary;
  1.2855 +	}
  1.2856 +
  1.2857 +	/**
  1.2858 +	* Destroys cached versions of template files
  1.2859 +	*
  1.2860 +	* @param array $template_row contains the template's row in the STYLES_TEMPLATE_TABLE database table
  1.2861 +	* @param mixed $file_ary is optional and may contain an array of template file names which should be refreshed in the cache.
  1.2862 +	*	The file names should be the original template file names and not the cache file names.
  1.2863 +	*/
  1.2864 +	function clear_template_cache($template_row, $file_ary = false)
  1.2865 +	{
  1.2866 +		global $phpbb_root_path, $phpEx, $user;
  1.2867 +
  1.2868 +		$cache_prefix = 'tpl_' . str_replace('_', '-', $template_row['template_path']);
  1.2869 +
  1.2870 +		if (!$file_ary || !is_array($file_ary))
  1.2871 +		{
  1.2872 +			$file_ary = $this->template_cache_filelist($template_row['template_path']);
  1.2873 +			$log_file_list = $user->lang['ALL_FILES'];
  1.2874 +		}
  1.2875 +		else
  1.2876 +		{
  1.2877 +			$log_file_list = implode(', ', $file_ary);
  1.2878 +		}
  1.2879 +
  1.2880 +		foreach ($file_ary as $file)
  1.2881 +		{
  1.2882 +			$file = str_replace('/', '.', $file);
  1.2883 +
  1.2884 +			$file = "{$phpbb_root_path}cache/{$cache_prefix}_$file.html.$phpEx";
  1.2885 +			if (file_exists($file) && is_file($file))
  1.2886 +			{
  1.2887 +				@unlink($file);
  1.2888 +			}
  1.2889 +		}
  1.2890 +		unset($file_ary);
  1.2891 +
  1.2892 +		add_log('admin', 'LOG_TEMPLATE_CACHE_CLEARED', $template_row['template_name'], $log_file_list);
  1.2893 +	}
  1.2894 +
  1.2895 +	/**
  1.2896 +	* Install Style/Template/Theme/Imageset
  1.2897 +	*/
  1.2898 +	function install($mode)
  1.2899 +	{
  1.2900 +		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template;
  1.2901 +
  1.2902 +		$l_type = strtoupper($mode);
  1.2903 +
  1.2904 +		$error = $installcfg = $style_row = array();
  1.2905 +		$root_path = $cfg_file = '';
  1.2906 +		$element_ary = array('template' => STYLES_TEMPLATE_TABLE, 'theme' => STYLES_THEME_TABLE, 'imageset' => STYLES_IMAGESET_TABLE);
  1.2907 +
  1.2908 +		$install_path = request_var('path', '');
  1.2909 +		$update = (isset($_POST['update'])) ? true : false;
  1.2910 +
  1.2911 +		// Installing, obtain cfg file contents
  1.2912 +		if ($install_path)
  1.2913 +		{
  1.2914 +			$root_path = $phpbb_root_path . 'styles/' . $install_path . '/';
  1.2915 +			$cfg_file = ($mode == 'style') ? "$root_path$mode.cfg" : "$root_path$mode/$mode.cfg";
  1.2916 +
  1.2917 +			if (!file_exists($cfg_file))
  1.2918 +			{
  1.2919 +				$error[] = $user->lang[$l_type . '_ERR_NOT_' . $l_type];
  1.2920 +			}
  1.2921 +			else
  1.2922 +			{
  1.2923 +				$installcfg = parse_cfg_file($cfg_file);
  1.2924 +			}
  1.2925 +		}
  1.2926 +
  1.2927 +		// Installing
  1.2928 +		if (sizeof($installcfg))
  1.2929 +		{
  1.2930 +			$name		= $installcfg['name'];
  1.2931 +			$copyright	= $installcfg['copyright'];
  1.2932 +			$version	= $installcfg['version'];
  1.2933 +
  1.2934 +			$style_row = array(
  1.2935 +				$mode . '_id'			=> 0,
  1.2936 +				$mode . '_name'			=> '',
  1.2937 +				$mode . '_copyright'	=> ''
  1.2938 +			);
  1.2939 +
  1.2940 +			switch ($mode)
  1.2941 +			{
  1.2942 +				case 'style':
  1.2943 +
  1.2944 +					$style_row = array(
  1.2945 +						'style_id'			=> 0,
  1.2946 +						'style_name'		=> $installcfg['name'],
  1.2947 +						'style_copyright'	=> $installcfg['copyright']
  1.2948 +					);
  1.2949 +
  1.2950 +					$reqd_template = (isset($installcfg['required_template'])) ? $installcfg['required_template'] : false;
  1.2951 +					$reqd_theme = (isset($installcfg['required_theme'])) ? $installcfg['required_theme'] : false;
  1.2952 +					$reqd_imageset = (isset($installcfg['required_imageset'])) ? $installcfg['required_imageset'] : false;
  1.2953 +
  1.2954 +					// Check to see if each element is already installed, if it is grab the id
  1.2955 +					foreach ($element_ary as $element => $table)
  1.2956 +					{
  1.2957 +						$style_row = array_merge($style_row, array(
  1.2958 +							$element . '_id'			=> 0,
  1.2959 +							$element . '_name'			=> '',
  1.2960 +							$element . '_copyright'		=> '')
  1.2961 +						);
  1.2962 +
  1.2963 +			 			$this->test_installed($element, $error, (${'reqd_' . $element}) ? $phpbb_root_path . 'styles/' . $reqd_template . '/' : $root_path, ${'reqd_' . $element}, $style_row[$element . '_id'], $style_row[$element . '_name'], $style_row[$element . '_copyright']);
  1.2964 +
  1.2965 +						if (!$style_row[$element . '_name'])
  1.2966 +						{
  1.2967 +							$style_row[$element . '_name'] = $reqd_template;
  1.2968 +						}
  1.2969 +
  1.2970 +						// Merge other information to installcfg... if present
  1.2971 +						$cfg_file = $phpbb_root_path . 'styles/' . $install_path . '/' . $element . '/' . $element . '.cfg';
  1.2972 +
  1.2973 +						if (file_exists($cfg_file))
  1.2974 +						{
  1.2975 +							$cfg_contents = parse_cfg_file($cfg_file);
  1.2976 +
  1.2977 +							// Merge only specific things. We may need them later.
  1.2978 +							foreach (array('inherit_from', 'parse_css_file') as $key)
  1.2979 +							{
  1.2980 +								if (!empty($cfg_contents[$key]) && !isset($installcfg[$key]))
  1.2981 +								{
  1.2982 +									$installcfg[$key] = $cfg_contents[$key];
  1.2983 +								}
  1.2984 +							}
  1.2985 +						}
  1.2986 +					}
  1.2987 +
  1.2988 +				break;
  1.2989 +
  1.2990 +				case 'template':
  1.2991 +					$this->test_installed('template', $error, $root_path, false, $style_row['template_id'], $style_row['template_name'], $style_row['template_copyright']);
  1.2992 +				break;
  1.2993 +
  1.2994 +				case 'theme':
  1.2995 +					$this->test_installed('theme', $error, $root_path, false, $style_row['theme_id'], $style_row['theme_name'], $style_row['theme_copyright']);
  1.2996 +				break;
  1.2997 +
  1.2998 +				case 'imageset':
  1.2999 +					$this->test_installed('imageset', $error, $root_path, false, $style_row['imageset_id'], $style_row['imageset_name'], $style_row['imageset_copyright']);
  1.3000 +				break;
  1.3001 +			}
  1.3002 +		}
  1.3003 +		else
  1.3004 +		{
  1.3005 +			trigger_error($user->lang['NO_' . $l_type] . adm_back_link($this->u_action), E_USER_WARNING);
  1.3006 +		}
  1.3007 +
  1.3008 +		$style_row['store_db'] = request_var('store_db', 0);
  1.3009 +		$style_row['style_active'] = request_var('style_active', 1);
  1.3010 +		$style_row['style_default'] = request_var('style_default', 0);
  1.3011 +
  1.3012 +		// User has submitted form and no errors have occurred
  1.3013 +		if ($update && !sizeof($error))
  1.3014 +		{
  1.3015 +			if ($mode == 'style')
  1.3016 +			{
  1.3017 +				foreach ($element_ary as $element => $table)
  1.3018 +				{
  1.3019 +					${$element . '_root_path'} = (${'reqd_' . $element}) ? $phpbb_root_path . 'styles/' . ${'reqd_' . $element} . '/' : false;
  1.3020 +					${$element . '_path'} = (${'reqd_' . $element}) ? ${'reqd_' . $element} : false;
  1.3021 +				}
  1.3022 +				$this->install_style($error, 'install', $root_path, $style_row['style_id'], $style_row['style_name'], $install_path, $style_row['style_copyright'], $style_row['style_active'], $style_row['style_default'], $style_row, $template_root_path, $template_path, $theme_root_path, $theme_path, $imageset_root_path, $imageset_path);
  1.3023 +			}
  1.3024 +			else
  1.3025 +			{
  1.3026 +				$style_row['store_db'] = $this->install_element($mode, $error, 'install', $root_path, $style_row[$mode . '_id'], $style_row[$mode . '_name'], $install_path, $style_row[$mode . '_copyright'], $style_row['store_db']);
  1.3027 +			}
  1.3028 +
  1.3029 +			if (!sizeof($error))
  1.3030 +			{
  1.3031 +				$cache->destroy('sql', STYLES_TABLE);
  1.3032 +
  1.3033 +				$message = ($style_row['store_db']) ? '_ADDED_DB' : '_ADDED';
  1.3034 +				trigger_error($user->lang[$l_type . $message] . adm_back_link($this->u_action));
  1.3035 +			}
  1.3036 +		}
  1.3037 +
  1.3038 +		$this->page_title = 'INSTALL_' . $l_type;
  1.3039 +
  1.3040 +		$template->assign_vars(array(
  1.3041 +			'S_DETAILS'			=> true,
  1.3042 +			'S_INSTALL'			=> true,
  1.3043 +			'S_ERROR_MSG'		=> (sizeof($error)) ? true : false,
  1.3044 +			'S_LOCATION'		=> (isset($installcfg['inherit_from']) && $installcfg['inherit_from']) ? false : true,
  1.3045 +			'S_STYLE'			=> ($mode == 'style') ? true : false,
  1.3046 +			'S_TEMPLATE'		=> ($mode == 'template') ? true : false,
  1.3047 +			'S_SUPERTEMPLATE'	=> (isset($installcfg['inherit_from'])) ? $installcfg['inherit_from'] : '',
  1.3048 +			'S_THEME'			=> ($mode == 'theme') ? true : false,
  1.3049 +
  1.3050 +			'S_STORE_DB'			=> (isset($style_row[$mode . '_storedb'])) ? $style_row[$mode . '_storedb'] : 0,
  1.3051 +			'S_STYLE_ACTIVE'		=> (isset($style_row['style_active'])) ? $style_row['style_active'] : 0,
  1.3052 +			'S_STYLE_DEFAULT'		=> (isset($style_row['style_default'])) ? $style_row['style_default'] : 0,
  1.3053 +
  1.3054 +			'U_ACTION'			=> $this->u_action . "&amp;action=install&amp;path=" . urlencode($install_path),
  1.3055 +			'U_BACK'			=> $this->u_action,
  1.3056 +
  1.3057 +			'L_TITLE'				=> $user->lang[$this->page_title],
  1.3058 +			'L_EXPLAIN'				=> $user->lang[$this->page_title . '_EXPLAIN'],
  1.3059 +			'L_NAME'				=> $user->lang[$l_type . '_NAME'],
  1.3060 +			'L_LOCATION'			=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION'] : '',
  1.3061 +			'L_LOCATION_EXPLAIN'	=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION_EXPLAIN'] : '',
  1.3062 +
  1.3063 +			'ERROR_MSG'			=> (sizeof($error)) ? implode('<br />', $error) : '',
  1.3064 +			'NAME'				=> $style_row[$mode . '_name'],
  1.3065 +			'COPYRIGHT'			=> $style_row[$mode . '_copyright'],
  1.3066 +			'TEMPLATE_NAME'		=> ($mode == 'style') ? $style_row['template_name'] : '',
  1.3067 +			'THEME_NAME'		=> ($mode == 'style') ? $style_row['theme_name'] : '',
  1.3068 +			'IMAGESET_NAME'		=> ($mode == 'style') ? $style_row['imageset_name'] : '')
  1.3069 +		);
  1.3070 +	}
  1.3071 +
  1.3072 +	/**
  1.3073 +	* Add new style
  1.3074 +	*/
  1.3075 +	function add($mode)
  1.3076 +	{
  1.3077 +		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template;
  1.3078 +
  1.3079 +		$l_type = strtoupper($mode);
  1.3080 +		$element_ary = array('template' => STYLES_TEMPLATE_TABLE, 'theme' => STYLES_THEME_TABLE, 'imageset' => STYLES_IMAGESET_TABLE);
  1.3081 +		$error = array();
  1.3082 +
  1.3083 +		$style_row = array(
  1.3084 +			$mode . '_name'			=> utf8_normalize_nfc(request_var('name', '', true)),
  1.3085 +			$mode . '_copyright'	=> utf8_normalize_nfc(request_var('copyright', '', true)),
  1.3086 +			'template_id'			=> 0,
  1.3087 +			'theme_id'				=> 0,
  1.3088 +			'imageset_id'			=> 0,
  1.3089 +			'store_db'				=> request_var('store_db', 0),
  1.3090 +			'style_active'			=> request_var('style_active', 1),
  1.3091 +			'style_default'			=> request_var('style_default', 0),
  1.3092 +		);
  1.3093 +
  1.3094 +		$basis = request_var('basis', 0);
  1.3095 +		$update = (isset($_POST['update'])) ? true : false;
  1.3096 +
  1.3097 +		if ($basis)
  1.3098 +		{
  1.3099 +			switch ($mode)
  1.3100 +			{
  1.3101 +				case 'style':
  1.3102 +					$sql_select = 'template_id, theme_id, imageset_id';
  1.3103 +					$sql_from = STYLES_TABLE;
  1.3104 +				break;
  1.3105 +
  1.3106 +				case 'template':
  1.3107 +					$sql_select = 'template_id';
  1.3108 +					$sql_from = STYLES_TEMPLATE_TABLE;
  1.3109 +				break;
  1.3110 +
  1.3111 +				case 'theme':
  1.3112 +					$sql_select = 'theme_id';
  1.3113 +					$sql_from = STYLES_THEME_TABLE;
  1.3114 +				break;
  1.3115 +
  1.3116 +				case 'imageset':
  1.3117 +					$sql_select = 'imageset_id';
  1.3118 +					$sql_from = STYLES_IMAGESET_TABLE;
  1.3119 +				break;
  1.3120 +			}
  1.3121 +
  1.3122 +			$sql = "SELECT $sql_select
  1.3123 +				FROM $sql_from
  1.3124 +				WHERE {$mode}_id = $basis";
  1.3125 +			$result = $db->sql_query($sql);
  1.3126 +			$row = $db->sql_fetchrow($result);
  1.3127 +			$db->sql_freeresult($result);
  1.3128 +
  1.3129 +			if (!$row)
  1.3130 +			{
  1.3131 +				$error[] = $user->lang['NO_' . $l_type];
  1.3132 +			}
  1.3133 +
  1.3134 +			if (!sizeof($error))
  1.3135 +			{
  1.3136 +				$style_row['template_id']	= (isset($row['template_id'])) ? $row['template_id'] : $style_row['template_id'];
  1.3137 +				$style_row['theme_id']		= (isset($row['theme_id'])) ? $row['theme_id'] : $style_row['theme_id'];
  1.3138 +				$style_row['imageset_id']	= (isset($row['imageset_id'])) ? $row['imageset_id'] : $style_row['imageset_id'];
  1.3139 +			}
  1.3140 +		}
  1.3141 +
  1.3142 +		if ($update)
  1.3143 +		{
  1.3144 +			$style_row['template_id'] = request_var('template_id', $style_row['template_id']);
  1.3145 +			$style_row['theme_id'] = request_var('theme_id', $style_row['theme_id']);
  1.3146 +			$style_row['imageset_id'] = request_var('imageset_id', $style_row['imageset_id']);
  1.3147 +
  1.3148 +			if ($mode == 'style' && (!$style_row['template_id'] || !$style_row['theme_id'] || !$style_row['imageset_id']))
  1.3149 +			{
  1.3150 +				$error[] = $user->lang['STYLE_ERR_NO_IDS'];
  1.3151 +			}
  1.3152 +		}
  1.3153 +
  1.3154 +		// User has submitted form and no errors have occurred
  1.3155 +		if ($update && !sizeof($error))
  1.3156 +		{
  1.3157 +			if ($mode == 'style')
  1.3158 +			{
  1.3159 +				$style_row['style_id'] = 0;
  1.3160 +
  1.3161 +				$this->install_style($error, 'add', '', $style_row['style_id'], $style_row['style_name'], '', $style_row['style_copyright'], $style_row['style_active'], $style_row['style_default'], $style_row);
  1.3162 +			}
  1.3163 +
  1.3164 +			if (!sizeof($error))
  1.3165 +			{
  1.3166 +				$cache->destroy('sql', STYLES_TABLE);
  1.3167 +
  1.3168 +				$message = ($style_row['store_db']) ? '_ADDED_DB' : '_ADDED';
  1.3169 +				trigger_error($user->lang[$l_type . $message] . adm_back_link($this->u_action));
  1.3170 +			}
  1.3171 +		}
  1.3172 +
  1.3173 +		if ($mode == 'style')
  1.3174 +		{
  1.3175 +			foreach ($element_ary as $element => $table)
  1.3176 +			{
  1.3177 +				$sql = "SELECT {$element}_id, {$element}_name
  1.3178 +					FROM $table
  1.3179 +					ORDER BY {$element}_id ASC";
  1.3180 +				$result = $db->sql_query($sql);
  1.3181 +
  1.3182 +				${$element . '_options'} = '';
  1.3183 +				while ($row = $db->sql_fetchrow($result))
  1.3184 +				{
  1.3185 +					$selected = ($row[$element . '_id'] == $style_row[$element . '_id']) ? ' selected="selected"' : '';
  1.3186 +					${$element . '_options'} .= '<option value="' . $row[$element . '_id'] . '"' . $selected . '>' . $row[$element . '_name'] . '</option>';
  1.3187 +				}
  1.3188 +				$db->sql_freeresult($result);
  1.3189 +			}
  1.3190 +		}
  1.3191 +
  1.3192 +		$this->page_title = 'ADD_' . $l_type;
  1.3193 +
  1.3194 +		$template->assign_vars(array(
  1.3195 +			'S_DETAILS'			=> true,
  1.3196 +			'S_ADD'				=> true,
  1.3197 +			'S_ERROR_MSG'		=> (sizeof($error)) ? true : false,
  1.3198 +			'S_STYLE'			=> ($mode == 'style') ? true : false,
  1.3199 +			'S_TEMPLATE'		=> ($mode == 'template') ? true : false,
  1.3200 +			'S_THEME'			=> ($mode == 'theme') ? true : false,
  1.3201 +			'S_BASIS'			=> ($basis) ? true : false,
  1.3202 +
  1.3203 +			'S_STORE_DB'			=> (isset($style_row['storedb'])) ? $style_row['storedb'] : 0,
  1.3204 +			'S_STYLE_ACTIVE'		=> (isset($style_row['style_active'])) ? $style_row['style_active'] : 0,
  1.3205 +			'S_STYLE_DEFAULT'		=> (isset($style_row['style_default'])) ? $style_row['style_default'] : 0,
  1.3206 +			'S_TEMPLATE_OPTIONS'	=> ($mode == 'style') ? $template_options : '',
  1.3207 +			'S_THEME_OPTIONS'		=> ($mode == 'style') ? $theme_options : '',
  1.3208 +			'S_IMAGESET_OPTIONS'	=> ($mode == 'style') ? $imageset_options : '',
  1.3209 +
  1.3210 +			'U_ACTION'			=> $this->u_action . '&amp;action=add&amp;basis=' . $basis,
  1.3211 +			'U_BACK'			=> $this->u_action,
  1.3212 +
  1.3213 +			'L_TITLE'				=> $user->lang[$this->page_title],
  1.3214 +			'L_EXPLAIN'				=> $user->lang[$this->page_title . '_EXPLAIN'],
  1.3215 +			'L_NAME'				=> $user->lang[$l_type . '_NAME'],
  1.3216 +			'L_LOCATION'			=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION'] : '',
  1.3217 +			'L_LOCATION_EXPLAIN'	=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION_EXPLAIN'] : '',
  1.3218 +
  1.3219 +			'ERROR_MSG'			=> (sizeof($error)) ? implode('<br />', $error) : '',
  1.3220 +			'NAME'				=> $style_row[$mode . '_name'],
  1.3221 +			'COPYRIGHT'			=> $style_row[$mode . '_copyright'])
  1.3222 +		);
  1.3223 +
  1.3224 +	}
  1.3225 +
  1.3226 +	/**
  1.3227 +
  1.3228 +					$reqd_template = (isset($installcfg['required_template'])) ? $installcfg['required_template'] : false;
  1.3229 +					$reqd_theme = (isset($installcfg['required_theme'])) ? $installcfg['required_theme'] : false;
  1.3230 +					$reqd_imageset = (isset($installcfg['required_imageset'])) ? $installcfg['required_imageset'] : false;
  1.3231 +
  1.3232 +					// Check to see if each element is already installed, if it is grab the id
  1.3233 +					foreach ($element_ary as $element => $table)
  1.3234 +					{
  1.3235 +						$style_row = array_merge($style_row, array(
  1.3236 +							$element . '_id'			=> 0,
  1.3237 +							$element . '_name'			=> '',
  1.3238 +							$element . '_copyright'		=> '')
  1.3239 +						);
  1.3240 +
  1.3241 +			 			$this->test_installed($element, $error, $root_path, ${'reqd_' . $element}, $style_row[$element . '_id'], $style_row[$element . '_name'], $style_row[$element . '_copyright']);
  1.3242 +	* Is this element installed? If not, grab its cfg details
  1.3243 +	*/
  1.3244 +	function test_installed($element, &$error, $root_path, $reqd_name, &$id, &$name, &$copyright)
  1.3245 +	{
  1.3246 +		global $db, $user;
  1.3247 +
  1.3248 +		switch ($element)
  1.3249 +		{
  1.3250 +			case 'template':
  1.3251 +				$sql_from = STYLES_TEMPLATE_TABLE;
  1.3252 +			break;
  1.3253 +
  1.3254 +			case 'theme':
  1.3255 +				$sql_from = STYLES_THEME_TABLE;
  1.3256 +			break;
  1.3257 +
  1.3258 +			case 'imageset':
  1.3259 +				$sql_from = STYLES_IMAGESET_TABLE;
  1.3260 +			break;
  1.3261 +		}
  1.3262 +
  1.3263 +		$l_element = strtoupper($element);
  1.3264 +
  1.3265 +		$chk_name = ($reqd_name !== false) ? $reqd_name : $name;
  1.3266 +
  1.3267 +		$sql = "SELECT {$element}_id, {$element}_name
  1.3268 +			FROM $sql_from
  1.3269 +			WHERE {$element}_name = '" . $db->sql_escape($chk_name) . "'";
  1.3270 +		$result = $db->sql_query($sql);
  1.3271 +
  1.3272 +		if ($row = $db->sql_fetchrow($result))
  1.3273 +		{
  1.3274 +			$name = $row[$element . '_name'];
  1.3275 +			$id = $row[$element . '_id'];
  1.3276 +		}
  1.3277 +		else
  1.3278 +		{
  1.3279 +			if (!($cfg = @file("$root_path$element/$element.cfg")))
  1.3280 +			{
  1.3281 +				$error[] = sprintf($user->lang['REQUIRES_' . $l_element], $reqd_name);
  1.3282 +				return false;
  1.3283 +			}
  1.3284 +
  1.3285 +			$cfg = parse_cfg_file("$root_path$element/$element.cfg", $cfg);
  1.3286 +
  1.3287 +			$name = $cfg['name'];
  1.3288 +			$copyright = $cfg['copyright'];
  1.3289 +			$id = 0;
  1.3290 +
  1.3291 +			unset($cfg);
  1.3292 +		}
  1.3293 +		$db->sql_freeresult($result);
  1.3294 +	}
  1.3295 +
  1.3296 +	/**
  1.3297 +	* Install/Add style
  1.3298 +	*/
  1.3299 +	function install_style(&$error, $action, $root_path, &$id, $name, $path, $copyright, $active, $default, &$style_row, $template_root_path = false, $template_path = false, $theme_root_path = false, $theme_path = false, $imageset_root_path = false, $imageset_path = false)
  1.3300 +	{
  1.3301 +		global $config, $db, $user;
  1.3302 +
  1.3303 +		$element_ary = array('template', 'theme', 'imageset');
  1.3304 +
  1.3305 +		if (!$name)
  1.3306 +		{
  1.3307 +			$error[] = $user->lang['STYLE_ERR_STYLE_NAME'];
  1.3308 +		}
  1.3309 +
  1.3310 +		// Check length settings
  1.3311 +		if (utf8_strlen($name) > 30)
  1.3312 +		{
  1.3313 +			$error[] = $user->lang['STYLE_ERR_NAME_LONG'];
  1.3314 +		}
  1.3315 +
  1.3316 +		if (utf8_strlen($copyright) > 60)
  1.3317 +		{
  1.3318 +			$error[] = $user->lang['STYLE_ERR_COPY_LONG'];
  1.3319 +		}
  1.3320 +
  1.3321 +		// Check if the name already exist
  1.3322 +		$sql = 'SELECT style_id
  1.3323 +			FROM ' . STYLES_TABLE . "
  1.3324 +			WHERE style_name = '" . $db->sql_escape($name) . "'";
  1.3325 +		$result = $db->sql_query($sql);
  1.3326 +		$row = $db->sql_fetchrow($result);
  1.3327 +		$db->sql_freeresult($result);
  1.3328 +
  1.3329 +		if ($row)
  1.3330 +		{
  1.3331 +			$error[] = $user->lang['STYLE_ERR_NAME_EXIST'];
  1.3332 +		}
  1.3333 +
  1.3334 +		if (sizeof($error))
  1.3335 +		{
  1.3336 +			return false;
  1.3337 +		}
  1.3338 +
  1.3339 +		foreach ($element_ary as $element)
  1.3340 +		{
  1.3341 +			// Zero id value ... need to install element ... run usual checks
  1.3342 +			// and do the install if necessary
  1.3343 +			if (!$style_row[$element . '_id'])
  1.3344 +			{
  1.3345 +				$this->install_element($element, $error, $action, (${$element . '_root_path'}) ? ${$element . '_root_path'} : $root_path, $style_row[$element . '_id'], $style_row[$element . '_name'], (${$element . '_path'}) ? ${$element . '_path'} : $path, $style_row[$element . '_copyright']);
  1.3346 +			}
  1.3347 +		}
  1.3348 +
  1.3349 +		if (!$style_row['template_id'] || !$style_row['theme_id'] || !$style_row['imageset_id'])
  1.3350 +		{
  1.3351 +			$error[] = $user->lang['STYLE_ERR_NO_IDS'];
  1.3352 +		}
  1.3353 +
  1.3354 +		if (sizeof($error))
  1.3355 +		{
  1.3356 +			return false;
  1.3357 +		}
  1.3358 +
  1.3359 +		$db->sql_transaction('begin');
  1.3360 +
  1.3361 +		$sql_ary = array(
  1.3362 +			'style_name'		=> $name,
  1.3363 +			'style_copyright'	=> $copyright,
  1.3364 +			'style_active'		=> (int) $active,
  1.3365 +			'template_id'		=> (int) $style_row['template_id'],
  1.3366 +			'theme_id'			=> (int) $style_row['theme_id'],
  1.3367 +			'imageset_id'		=> (int) $style_row['imageset_id'],
  1.3368 +		);
  1.3369 +
  1.3370 +		$sql = 'INSERT INTO ' . STYLES_TABLE . '
  1.3371 +			' . $db->sql_build_array('INSERT', $sql_ary);
  1.3372 +		$db->sql_query($sql);
  1.3373 +
  1.3374 +		$id = $db->sql_nextid();
  1.3375 +
  1.3376 +		if ($default)
  1.3377 +		{
  1.3378 +			$sql = 'UPDATE ' . USERS_TABLE . "
  1.3379 +				SET user_style = $id
  1.3380 +				WHERE user_style = " . $config['default_style'];
  1.3381 +			$db->sql_query($sql);
  1.3382 +
  1.3383 +			set_config('default_style', $id);
  1.3384 +		}
  1.3385 +
  1.3386 +		$db->sql_transaction('commit');
  1.3387 +
  1.3388 +		add_log('admin', 'LOG_STYLE_ADD', $name);
  1.3389 +	}
  1.3390 +
  1.3391 +	/**
  1.3392 +	* Install/add an element, doing various checks as we go
  1.3393 +	*/
  1.3394 +	function install_element($mode, &$error, $action, $root_path, &$id, $name, $path, $copyright, $store_db = 0)
  1.3395 +	{
  1.3396 +		global $phpbb_root_path, $db, $user;
  1.3397 +
  1.3398 +		// we parse the cfg here (again)
  1.3399 +		$cfg_data = parse_cfg_file("$root_path$mode/$mode.cfg");
  1.3400 +
  1.3401 +		switch ($mode)
  1.3402 +		{
  1.3403 +			case 'template':
  1.3404 +				$sql_from = STYLES_TEMPLATE_TABLE;
  1.3405 +			break;
  1.3406 +
  1.3407 +			case 'theme':
  1.3408 +				$sql_from = STYLES_THEME_TABLE;
  1.3409 +			break;
  1.3410 +
  1.3411 +			case 'imageset':
  1.3412 +				$sql_from = STYLES_IMAGESET_TABLE;
  1.3413 +			break;
  1.3414 +		}
  1.3415 +
  1.3416 +		$l_type = strtoupper($mode);
  1.3417 +
  1.3418 +		if (!$name)
  1.3419 +		{
  1.3420 +			$error[] = $user->lang[$l_type . '_ERR_STYLE_NAME'];
  1.3421 +		}
  1.3422 +
  1.3423 +		// Check length settings
  1.3424 +		if (utf8_strlen($name) > 30)
  1.3425 +		{
  1.3426 +			$error[] = $user->lang[$l_type . '_ERR_NAME_LONG'];
  1.3427 +		}
  1.3428 +
  1.3429 +		if (utf8_strlen($copyright) > 60)
  1.3430 +		{
  1.3431 +			$error[] = $user->lang[$l_type . '_ERR_COPY_LONG'];
  1.3432 +		}
  1.3433 +
  1.3434 +		// Check if the name already exist
  1.3435 +		$sql = "SELECT {$mode}_id
  1.3436 +			FROM $sql_from
  1.3437 +			WHERE {$mode}_name = '" . $db->sql_escape($name) . "'";
  1.3438 +		$result = $db->sql_query($sql);
  1.3439 +		$row = $db->sql_fetchrow($result);
  1.3440 +		$db->sql_freeresult($result);
  1.3441 +
  1.3442 +		if ($row)
  1.3443 +		{
  1.3444 +			// If it exist, we just use the style on installation
  1.3445 +			if ($action == 'install')
  1.3446 +			{
  1.3447 +				$id = $row[$mode . '_id'];
  1.3448 +				return false;
  1.3449 +			}
  1.3450 +
  1.3451 +			$error[] = $user->lang[$l_type . '_ERR_NAME_EXIST'];
  1.3452 +		}
  1.3453 +
  1.3454 +		if (isset($cfg_data['inherit_from']) && $cfg_data['inherit_from'])
  1.3455 +		{
  1.3456 +			if ($mode === 'template')
  1.3457 +			{
  1.3458 +				$select_bf = ', bbcode_bitfield';
  1.3459 +			}
  1.3460 +			else
  1.3461 +			{
  1.3462 +				$select_bf = '';
  1.3463 +			}
  1.3464 +
  1.3465 +			$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path, {$mode}_storedb $select_bf
  1.3466 +				FROM $sql_from
  1.3467 +				WHERE {$mode}_name = '" . $db->sql_escape($cfg_data['inherit_from']) . "'
  1.3468 +					AND {$mode}_inherits_id = 0";
  1.3469 +			$result = $db->sql_query($sql);
  1.3470 +			$row = $db->sql_fetchrow($result);
  1.3471 +			$db->sql_freeresult($result);
  1.3472 +			if (!$row)
  1.3473 +			{
  1.3474 +				$error[] = sprintf($user->lang[$l_type . '_ERR_REQUIRED_OR_INCOMPLETE'], $cfg_data['inherit_from']);
  1.3475 +			}
  1.3476 +			else
  1.3477 +			{
  1.3478 +				$inherit_id = $row["{$mode}_id"];
  1.3479 +				$inherit_path = $row["{$mode}_path"];
  1.3480 +				$inherit_bf = ($mode === 'template') ? $row["bbcode_bitfield"] : false;
  1.3481 +				$cfg_data['store_db'] = $row["{$mode}_storedb"];
  1.3482 +				$store_db = $row["{$mode}_storedb"];
  1.3483 +			}
  1.3484 +		}
  1.3485 +		else
  1.3486 +		{
  1.3487 +			$inherit_id = 0;
  1.3488 +			$inherit_path = '';
  1.3489 +			$inherit_bf = false;
  1.3490 +		}
  1.3491 +
  1.3492 +		if (sizeof($error))
  1.3493 +		{
  1.3494 +			return false;
  1.3495 +		}
  1.3496 +
  1.3497 +		$sql_ary = array(
  1.3498 +			$mode . '_name'			=> $name,
  1.3499 +			$mode . '_copyright'	=> $copyright,
  1.3500 +			$mode . '_path'			=> $path,
  1.3501 +		);
  1.3502 +
  1.3503 +		switch ($mode)
  1.3504 +		{
  1.3505 +			case 'template':
  1.3506 +				// We check if the template author defined a different bitfield
  1.3507 +				if (!empty($cfg_data['template_bitfield']))
  1.3508 +				{
  1.3509 +					$sql_ary['bbcode_bitfield'] = $cfg_data['template_bitfield'];
  1.3510 +				}
  1.3511 +				else if ($inherit_bf)
  1.3512 +				{
  1.3513 +					$sql_ary['bbcode_bitfield'] = $inherit_bf;
  1.3514 +				}
  1.3515 +				else
  1.3516 +				{
  1.3517 +					$sql_ary['bbcode_bitfield'] = TEMPLATE_BITFIELD;
  1.3518 +				}
  1.3519 +
  1.3520 +				// We set a pre-defined bitfield here which we may use further in 3.2
  1.3521 +				$sql_ary += array(
  1.3522 +					'template_storedb'		=> $store_db,
  1.3523 +				);
  1.3524 +				if (isset($cfg_data['inherit_from']) && $cfg_data['inherit_from'])
  1.3525 +				{
  1.3526 +					$sql_ary += array(
  1.3527 +						'template_inherits_id'	=> $inherit_id,
  1.3528 +						'template_inherit_path' => $inherit_path,
  1.3529 +					);
  1.3530 +				}
  1.3531 +			break;
  1.3532 +
  1.3533 +			case 'theme':
  1.3534 +				// We are only interested in the theme configuration for now
  1.3535 +
  1.3536 +				if (isset($cfg_data['parse_css_file']) && $cfg_data['parse_css_file'])
  1.3537 +				{
  1.3538 +					$store_db = 1;
  1.3539 +				}
  1.3540 +
  1.3541 +				$sql_ary += array(
  1.3542 +					'theme_storedb'	=> $store_db,
  1.3543 +					'theme_data'	=> ($store_db) ? $this->db_theme_data($sql_ary, false, $root_path) : '',
  1.3544 +					'theme_mtime'	=> (int) filemtime("{$phpbb_root_path}styles/$path/theme/stylesheet.css")
  1.3545 +				);
  1.3546 +			break;
  1.3547 +
  1.3548 +			// all the heavy lifting is done later
  1.3549 +			case 'imageset':
  1.3550 +			break;
  1.3551 +		}
  1.3552 +
  1.3553 +		$db->sql_transaction('begin');
  1.3554 +
  1.3555 +		$sql = "INSERT INTO $sql_from
  1.3556 +			" . $db->sql_build_array('INSERT', $sql_ary);
  1.3557 +		$db->sql_query($sql);
  1.3558 +
  1.3559 +		$id = $db->sql_nextid();
  1.3560 +
  1.3561 +		if ($mode == 'template' && $store_db)
  1.3562 +		{
  1.3563 +			$filelist = filelist("{$root_path}template", '', 'html');
  1.3564 +			$this->store_templates('insert', $id, $path, $filelist);
  1.3565 +		}
  1.3566 +		else if ($mode == 'imageset')
  1.3567 +		{
  1.3568 +			$cfg_data = parse_cfg_file("$root_path$mode/imageset.cfg");
  1.3569 +
  1.3570 +			$imageset_definitions = array();
  1.3571 +			foreach ($this->imageset_keys as $topic => $key_array)
  1.3572 +			{
  1.3573 +				$imageset_definitions = array_merge($imageset_definitions, $key_array);
  1.3574 +			}
  1.3575 +
  1.3576 +			foreach ($cfg_data as $key => $value)
  1.3577 +			{
  1.3578 +				if (strpos($value, '*') !== false)
  1.3579 +				{
  1.3580 +					if (substr($value, -1, 1) === '*')
  1.3581 +					{
  1.3582 +						list($image_filename, $image_height) = explode('*', $value);
  1.3583 +						$image_width = 0;
  1.3584 +					}
  1.3585 +					else
  1.3586 +					{
  1.3587 +						list($image_filename, $image_height, $image_width) = explode('*', $value);
  1.3588 +					}
  1.3589 +				}
  1.3590 +				else
  1.3591 +				{
  1.3592 +					$image_filename = $value;
  1.3593 +					$image_height = $image_width = 0;
  1.3594 +				}
  1.3595 +
  1.3596 +				if (strpos($key, 'img_') === 0 && $image_filename)
  1.3597 +				{
  1.3598 +					$key = substr($key, 4);
  1.3599 +					if (in_array($key, $imageset_definitions))
  1.3600 +					{
  1.3601 +						$sql_ary = array(
  1.3602 +							'image_name'		=> $key,
  1.3603 +							'image_filename'	=> str_replace('{PATH}', "styles/$path/imageset/", trim($image_filename)),
  1.3604 +							'image_height'		=> (int) $image_height,
  1.3605 +							'image_width'		=> (int) $image_width,
  1.3606 +							'imageset_id'		=> (int) $id,
  1.3607 +							'image_lang'		=> '',
  1.3608 +						);
  1.3609 +						$db->sql_query('INSERT INTO ' . STYLES_IMAGESET_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
  1.3610 +					}
  1.3611 +				}
  1.3612 +			}
  1.3613 +			unset($cfg_data);
  1.3614 +
  1.3615 +			$sql = 'SELECT lang_dir
  1.3616 +				FROM ' . LANG_TABLE;
  1.3617 +			$result = $db->sql_query($sql);
  1.3618 +
  1.3619 +			while ($row = $db->sql_fetchrow($result))
  1.3620 +			{
  1.3621 +				if (@file_exists("$root_path$mode/{$row['lang_dir']}/imageset.cfg"))
  1.3622 +				{
  1.3623 +					$cfg_data_imageset_data = parse_cfg_file("$root_path$mode/{$row['lang_dir']}/imageset.cfg");
  1.3624 +					foreach ($cfg_data_imageset_data as $image_name => $value)
  1.3625 +					{
  1.3626 +						if (strpos($value, '*') !== false)
  1.3627 +						{
  1.3628 +							if (substr($value, -1, 1) === '*')
  1.3629 +							{
  1.3630 +								list($image_filename, $image_height) = explode('*', $value);
  1.3631 +								$image_width = 0;
  1.3632 +							}
  1.3633 +							else
  1.3634 +							{
  1.3635 +								list($image_filename, $image_height, $image_width) = explode('*', $value);
  1.3636 +							}
  1.3637 +						}
  1.3638 +						else
  1.3639 +						{
  1.3640 +							$image_filename = $value;
  1.3641 +							$image_height = $image_width = 0;
  1.3642 +						}
  1.3643 +
  1.3644 +						if (strpos($image_name, 'img_') === 0 && $image_filename)
  1.3645 +						{
  1.3646 +							$image_name = substr($image_name, 4);
  1.3647 +							if (in_array($image_name, $imageset_definitions))
  1.3648 +							{
  1.3649 +								$sql_ary = array(
  1.3650 +									'image_name'		=> $image_name,
  1.3651 +									'image_filename'	=> $image_filename,
  1.3652 +									'image_height'		=> (int) $image_height,
  1.3653 +									'image_width'		=> (int) $image_width,
  1.3654 +									'imageset_id'		=> (int) $id,
  1.3655 +									'image_lang'		=> $row['lang_dir'],
  1.3656 +								);
  1.3657 +								$db->sql_query('INSERT INTO ' . STYLES_IMAGESET_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
  1.3658 +							}
  1.3659 +						}
  1.3660 +					}
  1.3661 +					unset($cfg_data_imageset_data);
  1.3662 +				}
  1.3663 +			}
  1.3664 +			$db->sql_freeresult($result);
  1.3665 +		}
  1.3666 +
  1.3667 +		$db->sql_transaction('commit');
  1.3668 +
  1.3669 +		$log = ($store_db) ? 'LOG_' . $l_type . '_ADD_DB' : 'LOG_' . $l_type . '_ADD_FS';
  1.3670 +		add_log('admin', $log, $name);
  1.3671 +
  1.3672 +		// Return store_db in case it had to be altered
  1.3673 +		return $store_db;
  1.3674 +	}
  1.3675 +
  1.3676 +	/**
  1.3677 +	* Checks downwards dependencies
  1.3678 +	*
  1.3679 +	* @access public
  1.3680 +	* @param string $mode The element type to check - only template is supported
  1.3681 +	* @param int $id The template id
  1.3682 +	* @returns false if no component inherits, array with name, path and id for each subtemplate otherwise
  1.3683 +	*/
  1.3684 +	function check_inheritance($mode, $id)
  1.3685 +	{
  1.3686 +		global $db;
  1.3687 +
  1.3688 +		$l_type = strtoupper($mode);
  1.3689 +
  1.3690 +		switch ($mode)
  1.3691 +		{
  1.3692 +			case 'template':
  1.3693 +				$sql_from = STYLES_TEMPLATE_TABLE;
  1.3694 +			break;
  1.3695 +
  1.3696 +			case 'theme':
  1.3697 +				$sql_from = STYLES_THEME_TABLE;
  1.3698 +			break;
  1.3699 +
  1.3700 +			case 'imageset':
  1.3701 +				$sql_from = STYLES_IMAGESET_TABLE;
  1.3702 +			break;
  1.3703 +		}
  1.3704 +
  1.3705 +		$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
  1.3706 +			FROM $sql_from
  1.3707 +			WHERE {$mode}_inherits_id = " . (int) $id;
  1.3708 +		$result = $db->sql_query($sql);
  1.3709 +
  1.3710 +		$names = array();
  1.3711 +		while ($row = $db->sql_fetchrow($result))
  1.3712 +		{
  1.3713 +
  1.3714 +			$names[$row["{$mode}_id"]] = array(
  1.3715 +				"{$mode}_id" => $row["{$mode}_id"],
  1.3716 +				"{$mode}_name" => $row["{$mode}_name"],
  1.3717 +				"{$mode}_path" => $row["{$mode}_path"],
  1.3718 +			);
  1.3719 +		}
  1.3720 +		$db->sql_freeresult($result);
  1.3721 +
  1.3722 +		if (sizeof($names))
  1.3723 +		{
  1.3724 +			return $names;
  1.3725 +		}
  1.3726 +		else
  1.3727 +		{
  1.3728 +			return false;
  1.3729 +		}
  1.3730 +	}
  1.3731 +
  1.3732 +	/**
  1.3733 +	* Checks upwards dependencies
  1.3734 +	*
  1.3735 +	* @access public
  1.3736 +	* @param string $mode The element type to check - only template is supported
  1.3737 +	* @param int $id The template id
  1.3738 +	* @returns false if the component does not inherit, array with name, path and id otherwise
  1.3739 +	*/
  1.3740 +	function get_super($mode, $id)
  1.3741 +	{
  1.3742 +		global $db;
  1.3743 +
  1.3744 +		$l_type = strtoupper($mode);
  1.3745 +
  1.3746 +		switch ($mode)
  1.3747 +		{
  1.3748 +			case 'template':
  1.3749 +				$sql_from = STYLES_TEMPLATE_TABLE;
  1.3750 +			break;
  1.3751 +
  1.3752 +			case 'theme':
  1.3753 +				$sql_from = STYLES_THEME_TABLE;
  1.3754 +			break;
  1.3755 +
  1.3756 +			case 'imageset':
  1.3757 +				$sql_from = STYLES_IMAGESET_TABLE;
  1.3758 +			break;
  1.3759 +		}
  1.3760 +
  1.3761 +		$sql = "SELECT {$mode}_inherits_id
  1.3762 +			FROM $sql_from
  1.3763 +			WHERE {$mode}_id = " . (int) $id;
  1.3764 +		$result = $db->sql_query_limit($sql, 1);
  1.3765 +
  1.3766 +		if ($row = $db->sql_fetchrow($result))
  1.3767 +		{
  1.3768 +			$db->sql_freeresult($result);
  1.3769 +		}
  1.3770 +		else
  1.3771 +		{
  1.3772 +			return false;
  1.3773 +		}
  1.3774 +
  1.3775 +		$super_id = $row["{$mode}_inherits_id"];
  1.3776 +
  1.3777 +		$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
  1.3778 +			FROM $sql_from
  1.3779 +			WHERE {$mode}_id = " . (int) $super_id;
  1.3780 +
  1.3781 +		$result = $db->sql_query_limit($sql, 1);
  1.3782 +		if ($row = $db->sql_fetchrow($result))
  1.3783 +		{
  1.3784 +			$db->sql_freeresult($result);
  1.3785 +			return $row;
  1.3786 +		}
  1.3787 +
  1.3788 +		return false;
  1.3789 +	}
  1.3790 +
  1.3791 +	/**
  1.3792 +	* Moves a template set and its subtemplates to the database
  1.3793 +	*
  1.3794 +	* @access public
  1.3795 +	* @param string $mode The component to move - only template is supported
  1.3796 +	* @param int $id The template id
  1.3797 +	*/
  1.3798 +	function store_in_db($mode, $id)
  1.3799 +	{
  1.3800 +		global $db, $user;
  1.3801 +
  1.3802 +		$error = array();
  1.3803 +		$l_type = strtoupper($mode);
  1.3804 +		if ($super = $this->get_super($mode, $id))
  1.3805 +		{
  1.3806 +			$error[] = (sprintf($user->lang["{$l_type}_INHERITS"], $super['template_name']));
  1.3807 +			return $error;
  1.3808 +		}
  1.3809 +
  1.3810 +		$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
  1.3811 +			FROM " . STYLES_TEMPLATE_TABLE . '
  1.3812 +			WHERE template_id = ' . (int) $id;
  1.3813 +
  1.3814 +		$result = $db->sql_query_limit($sql, 1);
  1.3815 +		if ($row = $db->sql_fetchrow($result))
  1.3816 +		{
  1.3817 +			$db->sql_freeresult($result);
  1.3818 +			$subs = $this->check_inheritance($mode, $id);
  1.3819 +
  1.3820 +			$this->_store_in_db($mode, $id, $row["{$mode}_path"]);
  1.3821 +			if ($subs && sizeof($subs))
  1.3822 +			{
  1.3823 +				foreach ($subs as $sub_id => $sub)
  1.3824 +				{
  1.3825 +					if ($err = $this->_store_in_db($mode, $sub["{$mode}_id"], $sub["{$mode}_path"]))
  1.3826 +					{
  1.3827 +						$error[] = $err;
  1.3828 +					}
  1.3829 +				}
  1.3830 +			}
  1.3831 +		}
  1.3832 +		if (sizeof($error))
  1.3833 +		{
  1.3834 +			return $error;
  1.3835 +		}
  1.3836 +
  1.3837 +		return false;
  1.3838 +	}
  1.3839 +
  1.3840 +	/**
  1.3841 +	* Moves a template set to the database
  1.3842 +	*
  1.3843 +	* @access private
  1.3844 +	* @param string $mode The component to move - only template is supported
  1.3845 +	* @param int $id The template id
  1.3846 +	* @param string $path TThe path to the template files
  1.3847 +	*/
  1.3848 +	function _store_in_db($mode, $id, $path)
  1.3849 +	{
  1.3850 +		global $phpbb_root_path, $db;
  1.3851 +
  1.3852 +		$filelist = filelist("{$phpbb_root_path}styles/{$path}/template", '', 'html');
  1.3853 +		$this->store_templates('insert', $id, $path, $filelist);
  1.3854 +
  1.3855 +		// Okay, we do the query here -shouldn't be triggered often.
  1.3856 +		$sql = 'UPDATE ' . STYLES_TEMPLATE_TABLE . '
  1.3857 +						SET template_storedb = 1
  1.3858 +						WHERE template_id = ' . $id;
  1.3859 +		$db->sql_query($sql);
  1.3860 +	}
  1.3861 +
  1.3862 +	/**
  1.3863 +	* Moves a template set and its subtemplates to the filesystem
  1.3864 +	*
  1.3865 +	* @access public
  1.3866 +	* @param string $mode The component to move - only template is supported
  1.3867 +	* @param int $id The template id
  1.3868 +	*/
  1.3869 +	function store_in_fs($mode, $id)
  1.3870 +	{
  1.3871 +		global $db, $user;
  1.3872 +
  1.3873 +		$error = array();
  1.3874 +		$l_type = strtoupper($mode);
  1.3875 +		if ($super = $this->get_super($mode, $id))
  1.3876 +		{
  1.3877 +			$error[] = (sprintf($user->lang["{$l_type}_INHERITS"], $super['template_name']));
  1.3878 +			return($error);
  1.3879 +		}
  1.3880 +
  1.3881 +		$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
  1.3882 +			FROM " . STYLES_TEMPLATE_TABLE . '
  1.3883 +			WHERE template_id = ' . (int) $id;
  1.3884 +
  1.3885 +		$result = $db->sql_query_limit($sql, 1);
  1.3886 +		if ($row = $db->sql_fetchrow($result))
  1.3887 +		{
  1.3888 +			$db->sql_freeresult($result);
  1.3889 +			if (!sizeof($error))
  1.3890 +			{
  1.3891 +				$subs = $this->check_inheritance($mode, $id);
  1.3892 +
  1.3893 +				$this->_store_in_fs($mode, $id, $row["{$mode}_path"]);
  1.3894 +
  1.3895 +				if ($subs && sizeof($subs))
  1.3896 +				{
  1.3897 +					foreach ($subs as $sub_id => $sub)
  1.3898 +					{
  1.3899 +						$this->_store_in_fs($mode, $sub["{$mode}_id"], $sub["{$mode}_path"]);
  1.3900 +					}
  1.3901 +				}
  1.3902 +			}
  1.3903 +			if (sizeof($error))
  1.3904 +			{
  1.3905 +				$this->store_in_db($id, $mode);
  1.3906 +				return $error;
  1.3907 +			}
  1.3908 +		}
  1.3909 +		return false;
  1.3910 +	}
  1.3911 +
  1.3912 +	/**
  1.3913 +	* Moves a template set to the filesystem
  1.3914 +	*
  1.3915 +	* @access private
  1.3916 +	* @param string $mode The component to move - only template is supported
  1.3917 +	* @param int $id The template id
  1.3918 +	* @param string $path The path to the template
  1.3919 +	*/
  1.3920 +	function _store_in_fs($mode, $id, $path)
  1.3921 +	{
  1.3922 +		global $phpbb_root_path, $db, $user, $safe_mode;
  1.3923 +
  1.3924 +		$store_db = 0;
  1.3925 +		$error = array();
  1.3926 +		if (!$safe_mode && phpbb_is_writable("{$phpbb_root_path}styles/{$path}/template"))
  1.3927 +		{
  1.3928 +			$sql = 'SELECT *
  1.3929 +					FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
  1.3930 +					WHERE template_id = $id";
  1.3931 +			$result = $db->sql_query($sql);
  1.3932 +
  1.3933 +			while ($row = $db->sql_fetchrow($result))
  1.3934 +			{
  1.3935 +				if (!($fp = @fopen("{$phpbb_root_path}styles/{$path}/template/" . $row['template_filename'], 'wb')))
  1.3936 +				{
  1.3937 +					$store_db = 1;
  1.3938 +					$error[] = $user->lang['EDIT_TEMPLATE_STORED_DB'];
  1.3939 +					break;
  1.3940 +				}
  1.3941 +
  1.3942 +				fwrite($fp, $row['template_data']);
  1.3943 +				fclose($fp);
  1.3944 +			}
  1.3945 +			$db->sql_freeresult($result);
  1.3946 +
  1.3947 +			if (!$store_db)
  1.3948 +			{
  1.3949 +				$sql = 'DELETE FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
  1.3950 +						WHERE template_id = $id";
  1.3951 +				$db->sql_query($sql);
  1.3952 +			}
  1.3953 +		}
  1.3954 +		if (sizeof($error))
  1.3955 +		{
  1.3956 +			return $error;
  1.3957 +		}
  1.3958 +		$sql = 'UPDATE ' . STYLES_TEMPLATE_TABLE . '
  1.3959 +				SET template_storedb = 0
  1.3960 +				WHERE template_id = ' . $id;
  1.3961 +		$db->sql_query($sql);
  1.3962 +
  1.3963 +		return false;
  1.3964 +	}
  1.3965 +
  1.3966 +}
  1.3967 +
  1.3968 +?>
  1.3969 \ No newline at end of file