includes/acp/acp_styles.php
changeset 0 ceef6d64c21d
equal deleted inserted replaced
-1:000000000000 0:ceef6d64c21d
       
     1 <?php
       
     2 /**
       
     3 *
       
     4 * @package acp
       
     5 * @version $Id$
       
     6 * @copyright (c) 2005 phpBB Group
       
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
       
     8 *
       
     9 */
       
    10 
       
    11 /**
       
    12 * @ignore
       
    13 */
       
    14 if (!defined('IN_PHPBB'))
       
    15 {
       
    16 	exit;
       
    17 }
       
    18 
       
    19 /**
       
    20 * @package acp
       
    21 */
       
    22 class acp_styles
       
    23 {
       
    24 	var $u_action;
       
    25 
       
    26 	var $style_cfg;
       
    27 	var $template_cfg;
       
    28 	var $theme_cfg;
       
    29 	var $imageset_cfg;
       
    30 	var $imageset_keys;
       
    31 
       
    32 	function main($id, $mode)
       
    33 	{
       
    34 		global $db, $user, $auth, $template, $cache;
       
    35 		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
       
    36 
       
    37 		// Hardcoded template bitfield to add for new templates
       
    38 		$bitfield = new bitfield();
       
    39 		$bitfield->set(0);
       
    40 		$bitfield->set(1);
       
    41 		$bitfield->set(2);
       
    42 		$bitfield->set(3);
       
    43 		$bitfield->set(4);
       
    44 		$bitfield->set(8);
       
    45 		$bitfield->set(9);
       
    46 		$bitfield->set(11);
       
    47 		$bitfield->set(12);
       
    48 		define('TEMPLATE_BITFIELD', $bitfield->get_base64());
       
    49 		unset($bitfield);
       
    50 
       
    51 		$user->add_lang('acp/styles');
       
    52 
       
    53 		$this->tpl_name = 'acp_styles';
       
    54 		$this->page_title = 'ACP_CAT_STYLES';
       
    55 
       
    56 		$action = request_var('action', '');
       
    57 		$action = (isset($_POST['add'])) ? 'add' : $action;
       
    58 		$style_id = request_var('id', 0);
       
    59 
       
    60 		// Fill the configuration variables
       
    61 		$this->style_cfg = $this->template_cfg = $this->theme_cfg = $this->imageset_cfg = '
       
    62 #
       
    63 # phpBB {MODE} configuration file
       
    64 #
       
    65 # @package phpBB3
       
    66 # @copyright (c) 2005 phpBB Group
       
    67 # @license http://opensource.org/licenses/gpl-license.php GNU Public License
       
    68 #
       
    69 #
       
    70 # At the left is the name, please do not change this
       
    71 # At the right the value is entered
       
    72 # For on/off options the valid values are on, off, 1, 0, true and false
       
    73 #
       
    74 # Values get trimmed, if you want to add a space in front or at the end of
       
    75 # the value, then enclose the value with single or double quotes.
       
    76 # Single and double quotes do not need to be escaped.
       
    77 #
       
    78 #
       
    79 
       
    80 # General Information about this {MODE}
       
    81 name = {NAME}
       
    82 copyright = {COPYRIGHT}
       
    83 version = {VERSION}
       
    84 ';
       
    85 
       
    86 		$this->theme_cfg .= '
       
    87 # Some configuration options
       
    88 
       
    89 #
       
    90 # You have to turn this option on if you want to use the
       
    91 # path template variables ({T_IMAGESET_PATH} for example) within
       
    92 # your css file.
       
    93 # This is mostly the case if you want to use language specific
       
    94 # images within your css file.
       
    95 #
       
    96 parse_css_file = {PARSE_CSS_FILE}
       
    97 ';
       
    98 
       
    99 		$this->template_cfg .= '
       
   100 # Some configuration options
       
   101 
       
   102 #
       
   103 # You can use this function to inherit templates from another template.
       
   104 # The template of the given name has to be installed.
       
   105 # Templates cannot inherit from inheriting templates.
       
   106 #';
       
   107 
       
   108 		$this->imageset_keys = array(
       
   109 			'logos' => array(
       
   110 				'site_logo',
       
   111 			),
       
   112 			'buttons'	=> array(
       
   113 				'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',
       
   114 			),
       
   115 			'icons'		=> array(
       
   116 				'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',
       
   117 			),
       
   118 			'forums'	=> array(
       
   119 				'forum_link', 'forum_read', 'forum_read_locked', 'forum_read_subforum', 'forum_unread', 'forum_unread_locked', 'forum_unread_subforum', 'subforum_read', 'subforum_unread'
       
   120 			),
       
   121 			'folders'	=> array(
       
   122 				'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',
       
   123 			),
       
   124 			'polls'		=> array(
       
   125 				'poll_left', 'poll_center', 'poll_right',
       
   126 			),
       
   127 			'ui'		=> array(
       
   128 				'upload_bar',
       
   129 			),
       
   130 			'user'		=> array(
       
   131 				'user_icon1', 'user_icon2', 'user_icon3', 'user_icon4', 'user_icon5', 'user_icon6', 'user_icon7', 'user_icon8', 'user_icon9', 'user_icon10',
       
   132 			),
       
   133 		);
       
   134 
       
   135 		// Execute overall actions
       
   136 		switch ($action)
       
   137 		{
       
   138 			case 'delete':
       
   139 				if ($style_id)
       
   140 				{
       
   141 					$this->remove($mode, $style_id);
       
   142 					return;
       
   143 				}
       
   144 			break;
       
   145 
       
   146 			case 'export':
       
   147 				if ($style_id)
       
   148 				{
       
   149 					$this->export($mode, $style_id);
       
   150 					return;
       
   151 				}
       
   152 			break;
       
   153 
       
   154 			case 'install':
       
   155 				$this->install($mode);
       
   156 				return;
       
   157 			break;
       
   158 
       
   159 			case 'add':
       
   160 				$this->add($mode);
       
   161 				return;
       
   162 			break;
       
   163 
       
   164 			case 'details':
       
   165 				if ($style_id)
       
   166 				{
       
   167 					$this->details($mode, $style_id);
       
   168 					return;
       
   169 				}
       
   170 			break;
       
   171 
       
   172 			case 'edit':
       
   173 				if ($style_id)
       
   174 				{
       
   175 					switch ($mode)
       
   176 					{
       
   177 						case 'imageset':
       
   178 							return $this->edit_imageset($style_id);
       
   179 						case 'template':
       
   180 							return $this->edit_template($style_id);
       
   181 						case 'theme':
       
   182 							return $this->edit_theme($style_id);
       
   183 					}
       
   184 				}
       
   185 			break;
       
   186 
       
   187 			case 'cache':
       
   188 				if ($style_id)
       
   189 				{
       
   190 					switch ($mode)
       
   191 					{
       
   192 						case 'template':
       
   193 							return $this->template_cache($style_id);
       
   194 					}
       
   195 				}
       
   196 			break;
       
   197 		}
       
   198 
       
   199 		switch ($mode)
       
   200 		{
       
   201 			case 'style':
       
   202 
       
   203 				switch ($action)
       
   204 				{
       
   205 					case 'activate':
       
   206 					case 'deactivate':
       
   207 
       
   208 						if ($style_id == $config['default_style'])
       
   209 						{
       
   210 							trigger_error($user->lang['DEACTIVATE_DEFAULT'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   211 						}
       
   212 
       
   213 						if (($action == 'deactivate' && confirm_box(true)) || $action == 'activate')
       
   214 						{
       
   215 							$sql = 'UPDATE ' . STYLES_TABLE . '
       
   216 								SET style_active = ' . (($action == 'activate') ? 1 : 0) . '
       
   217 								WHERE style_id = ' . $style_id;
       
   218 							$db->sql_query($sql);
       
   219 
       
   220 							// Set style to default for any member using deactivated style
       
   221 							if ($action == 'deactivate')
       
   222 							{
       
   223 								$sql = 'UPDATE ' . USERS_TABLE . '
       
   224 									SET user_style = ' . $config['default_style'] . "
       
   225 									WHERE user_style = $style_id";
       
   226 								$db->sql_query($sql);
       
   227 
       
   228 								$sql = 'UPDATE ' . FORUMS_TABLE . '
       
   229 									SET forum_style = 0
       
   230 									WHERE forum_style = ' . $style_id;
       
   231 								$db->sql_query($sql);
       
   232 							}
       
   233 						}
       
   234 						else if ($action == 'deactivate')
       
   235 						{
       
   236 							$s_hidden_fields = array(
       
   237 								'i'			=> $id,
       
   238 								'mode'		=> $mode,
       
   239 								'action'	=> $action,
       
   240 								'style_id'	=> $style_id,
       
   241 							);
       
   242 							confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields($s_hidden_fields));
       
   243 						}
       
   244 					break;
       
   245 				}
       
   246 
       
   247 				$this->frontend('style', array('details'), array('export', 'delete'));
       
   248 			break;
       
   249 
       
   250 			case 'template':
       
   251 
       
   252 				switch ($action)
       
   253 				{
       
   254 					// Refresh template data stored in db and clear cache
       
   255 					case 'refresh':
       
   256 
       
   257 						$sql = 'SELECT *
       
   258 							FROM ' . STYLES_TEMPLATE_TABLE . "
       
   259 							WHERE template_id = $style_id";
       
   260 						$result = $db->sql_query($sql);
       
   261 						$template_row = $db->sql_fetchrow($result);
       
   262 						$db->sql_freeresult($result);
       
   263 
       
   264 						if (!$template_row)
       
   265 						{
       
   266 							trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   267 						}
       
   268 
       
   269 						if (confirm_box(true))
       
   270 						{
       
   271 							$template_refreshed = '';
       
   272 
       
   273 							// Only refresh database if the template is stored in the database
       
   274 							if ($template_row['template_storedb'] && file_exists("{$phpbb_root_path}styles/{$template_row['template_path']}/template/"))
       
   275 							{
       
   276 								$filelist = array('' => array());
       
   277 
       
   278 								$sql = 'SELECT template_filename, template_mtime
       
   279 									FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
       
   280 									WHERE template_id = $style_id";
       
   281 								$result = $db->sql_query($sql);
       
   282 
       
   283 								while ($row = $db->sql_fetchrow($result))
       
   284 								{
       
   285 //									if (@filemtime("{$phpbb_root_path}styles/{$template_row['template_path']}/template/" . $row['template_filename']) > $row['template_mtime'])
       
   286 //									{
       
   287 										// get folder info from the filename
       
   288 										if (($slash_pos = strrpos($row['template_filename'], '/')) === false)
       
   289 										{
       
   290 											$filelist[''][] = $row['template_filename'];
       
   291 										}
       
   292 										else
       
   293 										{
       
   294 											$filelist[substr($row['template_filename'], 0, $slash_pos + 1)][] = substr($row['template_filename'], $slash_pos + 1, strlen($row['template_filename']) - $slash_pos - 1);
       
   295 										}
       
   296 //									}
       
   297 								}
       
   298 								$db->sql_freeresult($result);
       
   299 
       
   300 								$this->store_templates('update', $style_id, $template_row['template_path'], $filelist);
       
   301 								unset($filelist);
       
   302 
       
   303 								$template_refreshed = $user->lang['TEMPLATE_REFRESHED'] . '<br />';
       
   304 								add_log('admin', 'LOG_TEMPLATE_REFRESHED', $template_row['template_name']);
       
   305 							}
       
   306 
       
   307 							$this->clear_template_cache($template_row);
       
   308 
       
   309 							trigger_error($template_refreshed . $user->lang['TEMPLATE_CACHE_CLEARED'] . adm_back_link($this->u_action));
       
   310 						}
       
   311 						else
       
   312 						{
       
   313 							confirm_box(false, ($template_row['template_storedb']) ? $user->lang['CONFIRM_TEMPLATE_REFRESH'] : $user->lang['CONFIRM_TEMPLATE_CLEAR_CACHE'], build_hidden_fields(array(
       
   314 								'i'			=> $id,
       
   315 								'mode'		=> $mode,
       
   316 								'action'	=> $action,
       
   317 								'id'		=> $style_id
       
   318 							)));
       
   319 						}
       
   320 
       
   321 					break;
       
   322 				}
       
   323 
       
   324 				$this->frontend('template', array('edit', 'cache', 'details'), array('refresh', 'export', 'delete'));
       
   325 			break;
       
   326 
       
   327 			case 'theme':
       
   328 
       
   329 				switch ($action)
       
   330 				{
       
   331 					// Refresh theme data stored in the database
       
   332 					case 'refresh':
       
   333 
       
   334 						$sql = 'SELECT *
       
   335 							FROM ' . STYLES_THEME_TABLE . "
       
   336 							WHERE theme_id = $style_id";
       
   337 						$result = $db->sql_query($sql);
       
   338 						$theme_row = $db->sql_fetchrow($result);
       
   339 						$db->sql_freeresult($result);
       
   340 
       
   341 						if (!$theme_row)
       
   342 						{
       
   343 							trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   344 						}
       
   345 
       
   346 						if (!$theme_row['theme_storedb'])
       
   347 						{
       
   348 							trigger_error($user->lang['THEME_ERR_REFRESH_FS'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   349 						}
       
   350 
       
   351 						if (confirm_box(true))
       
   352 						{
       
   353 							if ($theme_row['theme_storedb'] && file_exists("{$phpbb_root_path}styles/{$theme_row['theme_path']}/theme/stylesheet.css"))
       
   354 							{
       
   355 								// Save CSS contents
       
   356 								$sql_ary = array(
       
   357 									'theme_mtime'	=> (int) filemtime("{$phpbb_root_path}styles/{$theme_row['theme_path']}/theme/stylesheet.css"),
       
   358 									'theme_data'	=> $this->db_theme_data($theme_row)
       
   359 								);
       
   360 
       
   361 								$sql = 'UPDATE ' . STYLES_THEME_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
       
   362 									WHERE theme_id = $style_id";
       
   363 								$db->sql_query($sql);
       
   364 
       
   365 								$cache->destroy('sql', STYLES_THEME_TABLE);
       
   366 
       
   367 								add_log('admin', 'LOG_THEME_REFRESHED', $theme_row['theme_name']);
       
   368 								trigger_error($user->lang['THEME_REFRESHED'] . adm_back_link($this->u_action));
       
   369 							}
       
   370 						}
       
   371 						else
       
   372 						{
       
   373 							confirm_box(false, $user->lang['CONFIRM_THEME_REFRESH'], build_hidden_fields(array(
       
   374 								'i'			=> $id,
       
   375 								'mode'		=> $mode,
       
   376 								'action'	=> $action,
       
   377 								'id'		=> $style_id
       
   378 							)));
       
   379 						}
       
   380 					break;
       
   381 				}
       
   382 
       
   383 				$this->frontend('theme', array('edit', 'details'), array('refresh', 'export', 'delete'));
       
   384 			break;
       
   385 
       
   386 			case 'imageset':
       
   387 
       
   388 				switch ($action)
       
   389 				{
       
   390 					case 'refresh':
       
   391 
       
   392 						$sql = 'SELECT *
       
   393 							FROM ' . STYLES_IMAGESET_TABLE . "
       
   394 							WHERE imageset_id = $style_id";
       
   395 						$result = $db->sql_query($sql);
       
   396 						$imageset_row = $db->sql_fetchrow($result);
       
   397 						$db->sql_freeresult($result);
       
   398 
       
   399 						if (!$imageset_row)
       
   400 						{
       
   401 							trigger_error($user->lang['NO_IMAGESET'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   402 						}
       
   403 
       
   404 						if (confirm_box(true))
       
   405 						{
       
   406 							$sql_ary = array();
       
   407 
       
   408 							$imageset_definitions = array();
       
   409 							foreach ($this->imageset_keys as $topic => $key_array)
       
   410 							{
       
   411 								$imageset_definitions = array_merge($imageset_definitions, $key_array);
       
   412 							}
       
   413 
       
   414 							$cfg_data_imageset = parse_cfg_file("{$phpbb_root_path}styles/{$imageset_row['imageset_path']}/imageset/imageset.cfg");
       
   415 
       
   416 							$db->sql_transaction('begin');
       
   417 
       
   418 							$sql = 'DELETE FROM ' . STYLES_IMAGESET_DATA_TABLE . '
       
   419 								WHERE imageset_id = ' . $style_id;
       
   420 							$result = $db->sql_query($sql);
       
   421 
       
   422 							foreach ($cfg_data_imageset as $image_name => $value)
       
   423 							{
       
   424 								if (strpos($value, '*') !== false)
       
   425 								{
       
   426 									if (substr($value, -1, 1) === '*')
       
   427 									{
       
   428 										list($image_filename, $image_height) = explode('*', $value);
       
   429 										$image_width = 0;
       
   430 									}
       
   431 									else
       
   432 									{
       
   433 										list($image_filename, $image_height, $image_width) = explode('*', $value);
       
   434 									}
       
   435 								}
       
   436 								else
       
   437 								{
       
   438 									$image_filename = $value;
       
   439 									$image_height = $image_width = 0;
       
   440 								}
       
   441 
       
   442 								if (strpos($image_name, 'img_') === 0 && $image_filename)
       
   443 								{
       
   444 									$image_name = substr($image_name, 4);
       
   445 									if (in_array($image_name, $imageset_definitions))
       
   446 									{
       
   447 										$sql_ary[] = array(
       
   448 											'image_name'		=> (string) $image_name,
       
   449 											'image_filename'	=> (string) $image_filename,
       
   450 											'image_height'		=> (int) $image_height,
       
   451 											'image_width'		=> (int) $image_width,
       
   452 											'imageset_id'		=> (int) $style_id,
       
   453 											'image_lang'		=> '',
       
   454 										);
       
   455 									}
       
   456 								}
       
   457 							}
       
   458 
       
   459 							$sql = 'SELECT lang_dir
       
   460 								FROM ' . LANG_TABLE;
       
   461 							$result = $db->sql_query($sql);
       
   462 
       
   463 							while ($row = $db->sql_fetchrow($result))
       
   464 							{
       
   465 								if (@file_exists("{$phpbb_root_path}styles/{$imageset_row['imageset_path']}/imageset/{$row['lang_dir']}/imageset.cfg"))
       
   466 								{
       
   467 									$cfg_data_imageset_data = parse_cfg_file("{$phpbb_root_path}styles/{$imageset_row['imageset_path']}/imageset/{$row['lang_dir']}/imageset.cfg");
       
   468 									foreach ($cfg_data_imageset_data as $image_name => $value)
       
   469 									{
       
   470 										if (strpos($value, '*') !== false)
       
   471 										{
       
   472 											if (substr($value, -1, 1) === '*')
       
   473 											{
       
   474 												list($image_filename, $image_height) = explode('*', $value);
       
   475 												$image_width = 0;
       
   476 											}
       
   477 											else
       
   478 											{
       
   479 												list($image_filename, $image_height, $image_width) = explode('*', $value);
       
   480 											}
       
   481 										}
       
   482 										else
       
   483 										{
       
   484 											$image_filename = $value;
       
   485 											$image_height = $image_width = 0;
       
   486 										}
       
   487 
       
   488 										if (strpos($image_name, 'img_') === 0 && $image_filename)
       
   489 										{
       
   490 											$image_name = substr($image_name, 4);
       
   491 											if (in_array($image_name, $imageset_definitions))
       
   492 											{
       
   493 												$sql_ary[] = array(
       
   494 													'image_name'		=> (string) $image_name,
       
   495 													'image_filename'	=> (string) $image_filename,
       
   496 													'image_height'		=> (int) $image_height,
       
   497 													'image_width'		=> (int) $image_width,
       
   498 													'imageset_id'		=> (int) $style_id,
       
   499 													'image_lang'		=> (string) $row['lang_dir'],
       
   500 												);
       
   501 											}
       
   502 										}
       
   503 									}
       
   504 								}
       
   505 							}
       
   506 							$db->sql_freeresult($result);
       
   507 
       
   508 							$db->sql_multi_insert(STYLES_IMAGESET_DATA_TABLE, $sql_ary);
       
   509 
       
   510 							$db->sql_transaction('commit');
       
   511 
       
   512 							$cache->destroy('sql', STYLES_IMAGESET_DATA_TABLE);
       
   513 							$cache->destroy('imageset_site_logo_md5');
       
   514 
       
   515 							add_log('admin', 'LOG_IMAGESET_REFRESHED', $imageset_row['imageset_name']);
       
   516 							trigger_error($user->lang['IMAGESET_REFRESHED'] . adm_back_link($this->u_action));
       
   517 						}
       
   518 						else
       
   519 						{
       
   520 							confirm_box(false, $user->lang['CONFIRM_IMAGESET_REFRESH'], build_hidden_fields(array(
       
   521 								'i'			=> $id,
       
   522 								'mode'		=> $mode,
       
   523 								'action'	=> $action,
       
   524 								'id'		=> $style_id
       
   525 							)));
       
   526 						}
       
   527 					break;
       
   528 				}
       
   529 
       
   530 				$this->frontend('imageset', array('edit', 'details'), array('refresh', 'export', 'delete'));
       
   531 			break;
       
   532 		}
       
   533 	}
       
   534 
       
   535 	/**
       
   536 	* Build Frontend with supplied options
       
   537 	*/
       
   538 	function frontend($mode, $options, $actions)
       
   539 	{
       
   540 		global $user, $template, $db, $config, $phpbb_root_path, $phpEx;
       
   541 
       
   542 		$sql_from = '';
       
   543 		$style_count = array();
       
   544 
       
   545 		switch ($mode)
       
   546 		{
       
   547 			case 'style':
       
   548 				$sql_from = STYLES_TABLE;
       
   549 
       
   550 				$sql = 'SELECT user_style, COUNT(user_style) AS style_count
       
   551 					FROM ' . USERS_TABLE . '
       
   552 					GROUP BY user_style';
       
   553 				$result = $db->sql_query($sql);
       
   554 
       
   555 				while ($row = $db->sql_fetchrow($result))
       
   556 				{
       
   557 					$style_count[$row['user_style']] = $row['style_count'];
       
   558 				}
       
   559 				$db->sql_freeresult($result);
       
   560 
       
   561 			break;
       
   562 
       
   563 			case 'template':
       
   564 				$sql_from = STYLES_TEMPLATE_TABLE;
       
   565 			break;
       
   566 
       
   567 			case 'theme':
       
   568 				$sql_from = STYLES_THEME_TABLE;
       
   569 			break;
       
   570 
       
   571 			case 'imageset':
       
   572 				$sql_from = STYLES_IMAGESET_TABLE;
       
   573 			break;
       
   574 		}
       
   575 
       
   576 		$l_prefix = strtoupper($mode);
       
   577 
       
   578 		$this->page_title = 'ACP_' . $l_prefix . 'S';
       
   579 
       
   580 		$template->assign_vars(array(
       
   581 			'S_FRONTEND'		=> true,
       
   582 			'S_STYLE'			=> ($mode == 'style') ? true : false,
       
   583 
       
   584 			'L_TITLE'			=> $user->lang[$this->page_title],
       
   585 			'L_EXPLAIN'			=> $user->lang[$this->page_title . '_EXPLAIN'],
       
   586 			'L_NAME'			=> $user->lang[$l_prefix . '_NAME'],
       
   587 			'L_INSTALLED'		=> $user->lang['INSTALLED_' . $l_prefix],
       
   588 			'L_UNINSTALLED'		=> $user->lang['UNINSTALLED_' . $l_prefix],
       
   589 			'L_NO_UNINSTALLED'	=> $user->lang['NO_UNINSTALLED_' . $l_prefix],
       
   590 			'L_CREATE'			=> $user->lang['CREATE_' . $l_prefix],
       
   591 
       
   592 			'U_ACTION'			=> $this->u_action,
       
   593 			)
       
   594 		);
       
   595 
       
   596 		$sql = "SELECT *
       
   597 			FROM $sql_from";
       
   598 		$result = $db->sql_query($sql);
       
   599 
       
   600 		$installed = array();
       
   601 
       
   602 		$basis_options = '<option class="sep" value="">' . $user->lang['OPTIONAL_BASIS'] . '</option>';
       
   603 		while ($row = $db->sql_fetchrow($result))
       
   604 		{
       
   605 			$installed[] = $row[$mode . '_name'];
       
   606 			$basis_options .= '<option value="' . $row[$mode . '_id'] . '">' . $row[$mode . '_name'] . '</option>';
       
   607 
       
   608 			$stylevis = ($mode == 'style' && !$row['style_active']) ? 'activate' : 'deactivate';
       
   609 
       
   610 			$s_options = array();
       
   611 			foreach ($options as $option)
       
   612 			{
       
   613 				$s_options[] = '<a href="' . $this->u_action . "&amp;action=$option&amp;id=" . $row[$mode . '_id'] . '">' . $user->lang[strtoupper($option)] . '</a>';
       
   614 			}
       
   615 
       
   616 			$s_actions = array();
       
   617 			foreach ($actions as $option)
       
   618 			{
       
   619 				$s_actions[] = '<a href="' . $this->u_action . "&amp;action=$option&amp;id=" . $row[$mode . '_id'] . '">' . $user->lang[strtoupper($option)] . '</a>';
       
   620 			}
       
   621 
       
   622 			$template->assign_block_vars('installed', array(
       
   623 				'S_DEFAULT_STYLE'		=> ($mode == 'style' && $row['style_id'] == $config['default_style']) ? true : false,
       
   624 				'U_EDIT'				=> $this->u_action . '&amp;action=' . (($mode == 'style') ? 'details' : 'edit') . '&amp;id=' . $row[$mode . '_id'],
       
   625 				'U_STYLE_ACT_DEACT'		=> $this->u_action . '&amp;action=' . $stylevis . '&amp;id=' . $row[$mode . '_id'],
       
   626 				'L_STYLE_ACT_DEACT'		=> $user->lang['STYLE_' . strtoupper($stylevis)],
       
   627 				'S_OPTIONS'				=> implode(' | ', $s_options),
       
   628 				'S_ACTIONS'				=> implode(' | ', $s_actions),
       
   629 				'U_PREVIEW'				=> ($mode == 'style') ? append_sid("{$phpbb_root_path}index.$phpEx", "$mode=" . $row[$mode . '_id']) : '',
       
   630 
       
   631 				'NAME'					=> $row[$mode . '_name'],
       
   632 				'STYLE_COUNT'			=> ($mode == 'style' && isset($style_count[$row['style_id']])) ? $style_count[$row['style_id']] : 0,
       
   633 				)
       
   634 			);
       
   635 		}
       
   636 		$db->sql_freeresult($result);
       
   637 
       
   638 		// Grab uninstalled items
       
   639 		$new_ary = $cfg = array();
       
   640 
       
   641 		$dp = @opendir("{$phpbb_root_path}styles");
       
   642 
       
   643 		if ($dp)
       
   644 		{
       
   645 			while (($file = readdir($dp)) !== false)
       
   646 			{
       
   647 				if ($file[0] == '.' || !is_dir($phpbb_root_path . 'styles/' . $file))
       
   648 				{
       
   649 					continue;
       
   650 				}
       
   651 
       
   652 				$subpath = ($mode != 'style') ? "$mode/" : '';
       
   653 				if (file_exists("{$phpbb_root_path}styles/$file/$subpath$mode.cfg"))
       
   654 				{
       
   655 					if ($cfg = file("{$phpbb_root_path}styles/$file/$subpath$mode.cfg"))
       
   656 					{
       
   657 						$items = parse_cfg_file('', $cfg);
       
   658 						$name = (isset($items['name'])) ? trim($items['name']) : false;
       
   659 
       
   660 						if ($name && !in_array($name, $installed))
       
   661 						{
       
   662 							$new_ary[] = array(
       
   663 								'path'		=> $file,
       
   664 								'name'		=> $name,
       
   665 								'copyright'	=> $items['copyright'],
       
   666 							);
       
   667 						}
       
   668 					}
       
   669 				}
       
   670 			}
       
   671 			closedir($dp);
       
   672 		}
       
   673 
       
   674 		unset($installed);
       
   675 
       
   676 		if (sizeof($new_ary))
       
   677 		{
       
   678 			foreach ($new_ary as $cfg)
       
   679 			{
       
   680 				$template->assign_block_vars('uninstalled', array(
       
   681 					'NAME'			=> $cfg['name'],
       
   682 					'COPYRIGHT'		=> $cfg['copyright'],
       
   683 					'U_INSTALL'		=> $this->u_action . '&amp;action=install&amp;path=' . urlencode($cfg['path']))
       
   684 				);
       
   685 			}
       
   686 		}
       
   687 		unset($new_ary);
       
   688 
       
   689 		$template->assign_vars(array(
       
   690 			'S_BASIS_OPTIONS'		=> $basis_options)
       
   691 		);
       
   692 
       
   693 	}
       
   694 
       
   695 	/**
       
   696 	* Provides a template editor which allows saving changes to template files on the filesystem or in the database.
       
   697 	*
       
   698 	* @param int $template_id specifies which template set is being edited
       
   699 	*/
       
   700 	function edit_template($template_id)
       
   701 	{
       
   702 		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template, $safe_mode;
       
   703 
       
   704 		if (defined('PHPBB_DISABLE_ACP_EDITOR'))
       
   705 		{
       
   706 			trigger_error($user->lang['EDITOR_DISABLED'] . adm_back_link($this->u_action));
       
   707 		}
       
   708 
       
   709 		$this->page_title = 'EDIT_TEMPLATE';
       
   710 
       
   711 		$filelist = $filelist_cats = array();
       
   712 
       
   713 		$template_data	= utf8_normalize_nfc(request_var('template_data', '', true));
       
   714 		$template_data	= htmlspecialchars_decode($template_data);
       
   715 		$template_file	= utf8_normalize_nfc(request_var('template_file', '', true));
       
   716 		$text_rows		= max(5, min(999, request_var('text_rows', 20)));
       
   717 		$save_changes	= (isset($_POST['save'])) ? true : false;
       
   718 
       
   719 		// make sure template_file path doesn't go upwards
       
   720 		$template_file = preg_replace('#\.{2,}#', '.', $template_file);
       
   721 
       
   722 		// Retrieve some information about the template
       
   723 		$sql = 'SELECT template_storedb, template_path, template_name
       
   724 			FROM ' . STYLES_TEMPLATE_TABLE . "
       
   725 			WHERE template_id = $template_id";
       
   726 		$result = $db->sql_query($sql);
       
   727 		$template_info = $db->sql_fetchrow($result);
       
   728 		$db->sql_freeresult($result);
       
   729 
       
   730 		if (!$template_info)
       
   731 		{
       
   732 			trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   733 		}
       
   734 
       
   735 		if ($save_changes && !check_form_key('acp_styles'))
       
   736 		{
       
   737 			trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   738 		}
       
   739 		else if (!$save_changes)
       
   740 		{
       
   741 			add_form_key('acp_styles');
       
   742 		}
       
   743 
       
   744 		// save changes to the template if the user submitted any
       
   745 		if ($save_changes && $template_file)
       
   746 		{
       
   747 			// Get the filesystem location of the current file
       
   748 			$file = "{$phpbb_root_path}styles/{$template_info['template_path']}/template/$template_file";
       
   749 			$additional = '';
       
   750 
       
   751 			// If the template is stored on the filesystem try to write the file else store it in the database
       
   752 			if (!$safe_mode && !$template_info['template_storedb'] && file_exists($file) && phpbb_is_writable($file))
       
   753 			{
       
   754 				if (!($fp = @fopen($file, 'wb')))
       
   755 				{
       
   756 					// File exists and is writeable, but still not able to be written to
       
   757 					trigger_error(sprintf($user->lang['TEMPLATE_FILE_NOT_WRITABLE'], htmlspecialchars($template_file)) . adm_back_link($this->u_action), E_USER_WARNING);
       
   758 				}
       
   759 				fwrite($fp, $template_data);
       
   760 				fclose($fp);
       
   761 			}
       
   762 			else
       
   763 			{
       
   764 				$db->sql_transaction('begin');
       
   765 
       
   766 				// If it's not stored in the db yet, then update the template setting and store all template files in the db
       
   767 				if (!$template_info['template_storedb'])
       
   768 				{
       
   769 					if ($super = $this->get_super('template', $template_id))
       
   770 					{
       
   771 						$this->store_in_db('template', $super['template_id']);
       
   772 					}
       
   773 					else
       
   774 					{
       
   775 						$this->store_in_db('template', $template_id);
       
   776 					}
       
   777 
       
   778 					add_log('admin', 'LOG_TEMPLATE_EDIT_DETAILS', $template_info['template_name']);
       
   779 					$additional .= '<br />' . $user->lang['EDIT_TEMPLATE_STORED_DB'];
       
   780 				}
       
   781 
       
   782 				// Update the template_data table entry for this template file
       
   783 				$sql = 'UPDATE ' . STYLES_TEMPLATE_DATA_TABLE . "
       
   784 					SET template_data = '" . $db->sql_escape($template_data) . "', template_mtime = " . time() . "
       
   785 					WHERE template_id = $template_id
       
   786 						AND template_filename = '" . $db->sql_escape($template_file) . "'";
       
   787 				$db->sql_query($sql);
       
   788 
       
   789 				$db->sql_transaction('commit');
       
   790 			}
       
   791 
       
   792 			// destroy the cached version of the template (filename without extension)
       
   793 			$this->clear_template_cache($template_info, array(substr($template_file, 0, -5)));
       
   794 
       
   795 			$cache->destroy('sql', STYLES_TABLE);
       
   796 
       
   797 			add_log('admin', 'LOG_TEMPLATE_EDIT', $template_info['template_name'], $template_file);
       
   798 			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"));
       
   799 		}
       
   800 
       
   801 		// Generate a category array containing template filenames
       
   802 		if (!$template_info['template_storedb'])
       
   803 		{
       
   804 			$template_path = "{$phpbb_root_path}styles/{$template_info['template_path']}/template";
       
   805 
       
   806 			$filelist = filelist($template_path, '', 'html');
       
   807 			$filelist[''] = array_diff($filelist[''], array('bbcode.html'));
       
   808 
       
   809 			if ($template_file)
       
   810 			{
       
   811 				if (!file_exists($template_path . "/$template_file") || !($template_data = file_get_contents($template_path . "/$template_file")))
       
   812 				{
       
   813 					trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   814 				}
       
   815 			}
       
   816 		}
       
   817 		else
       
   818 		{
       
   819 			$sql = 'SELECT *
       
   820 				FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
       
   821 				WHERE template_id = $template_id";
       
   822 			$result = $db->sql_query($sql);
       
   823 
       
   824 			$filelist = array('' => array());
       
   825 			while ($row = $db->sql_fetchrow($result))
       
   826 			{
       
   827 				$file_info = pathinfo($row['template_filename']);
       
   828 
       
   829 				if (($file_info['basename'] != 'bbcode') && ($file_info['extension'] == 'html'))
       
   830 				{
       
   831 					if (($file_info['dirname'] == '.') || empty($file_info['dirname']))
       
   832 					{
       
   833 						$filelist[''][] = $row['template_filename'];
       
   834 					}
       
   835 					else
       
   836 					{
       
   837 						$filelist[$file_info['dirname'] . '/'][] = $file_info['basename'];
       
   838 					}
       
   839 				}
       
   840 
       
   841 				if ($row['template_filename'] == $template_file)
       
   842 				{
       
   843 					$template_data = $row['template_data'];
       
   844 				}
       
   845 			}
       
   846 			$db->sql_freeresult($result);
       
   847 			unset($file_info);
       
   848 		}
       
   849 
       
   850 		if (empty($filelist['']))
       
   851 		{
       
   852 			trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   853 		}
       
   854 
       
   855 		// Now create the categories
       
   856 		$filelist_cats[''] = array();
       
   857 		foreach ($filelist as $pathfile => $file_ary)
       
   858 		{
       
   859 			// Use the directory name as category name
       
   860 			if (!empty($pathfile))
       
   861 			{
       
   862 				$filelist_cats[$pathfile] = array();
       
   863 				foreach ($file_ary as $file)
       
   864 				{
       
   865 					$filelist_cats[$pathfile][$pathfile . $file] = $file;
       
   866 				}
       
   867 			}
       
   868 			// or if it's in the main category use the word before the first underscore to group files
       
   869 			else
       
   870 			{
       
   871 				$cats = array();
       
   872 				foreach ($file_ary as $file)
       
   873 				{
       
   874 					$cats[] = substr($file, 0, strpos($file, '_'));
       
   875 					$filelist_cats[substr($file, 0, strpos($file, '_'))][$file] = $file;
       
   876 				}
       
   877 
       
   878 				$cats = array_values(array_unique($cats));
       
   879 
       
   880 				// we don't need any single element categories so put them into the misc '' category
       
   881 				for ($i = 0, $n = sizeof($cats); $i < $n; $i++)
       
   882 				{
       
   883 					if (sizeof($filelist_cats[$cats[$i]]) == 1 && $cats[$i] !== '')
       
   884 					{
       
   885 						$filelist_cats[''][key($filelist_cats[$cats[$i]])] = current($filelist_cats[$cats[$i]]);
       
   886 						unset($filelist_cats[$cats[$i]]);
       
   887 					}
       
   888 				}
       
   889 				unset($cats);
       
   890 			}
       
   891 		}
       
   892 		unset($filelist);
       
   893 
       
   894 		// Generate list of categorised template files
       
   895 		$tpl_options = '';
       
   896 		ksort($filelist_cats);
       
   897 		foreach ($filelist_cats as $category => $tpl_ary)
       
   898 		{
       
   899 			ksort($tpl_ary);
       
   900 
       
   901 			if (!empty($category))
       
   902 			{
       
   903 				$tpl_options .= '<option class="sep" value="">' . $category . '</option>';
       
   904 			}
       
   905 
       
   906 			foreach ($tpl_ary as $filename => $file)
       
   907 			{
       
   908 				$selected = ($template_file == $filename) ? ' selected="selected"' : '';
       
   909 				$tpl_options .= '<option value="' . $filename . '"' . $selected . '>' . $file . '</option>';
       
   910 			}
       
   911 		}
       
   912 
       
   913 		$template->assign_vars(array(
       
   914 			'S_EDIT_TEMPLATE'	=> true,
       
   915 			'S_HIDDEN_FIELDS'	=> build_hidden_fields(array('template_file' => $template_file)),
       
   916 			'S_TEMPLATES'		=> $tpl_options,
       
   917 
       
   918 			'U_ACTION'			=> $this->u_action . "&amp;action=edit&amp;id=$template_id&amp;text_rows=$text_rows",
       
   919 			'U_BACK'			=> $this->u_action,
       
   920 
       
   921 			'L_EDIT'			=> $user->lang['EDIT_TEMPLATE'],
       
   922 			'L_EDIT_EXPLAIN'	=> $user->lang['EDIT_TEMPLATE_EXPLAIN'],
       
   923 			'L_EDITOR'			=> $user->lang['TEMPLATE_EDITOR'],
       
   924 			'L_EDITOR_HEIGHT'	=> $user->lang['TEMPLATE_EDITOR_HEIGHT'],
       
   925 			'L_FILE'			=> $user->lang['TEMPLATE_FILE'],
       
   926 			'L_SELECT'			=> $user->lang['SELECT_TEMPLATE'],
       
   927 			'L_SELECTED'		=> $user->lang['SELECTED_TEMPLATE'],
       
   928 			'L_SELECTED_FILE'	=> $user->lang['SELECTED_TEMPLATE_FILE'],
       
   929 
       
   930 			'SELECTED_TEMPLATE'	=> $template_info['template_name'],
       
   931 			'TEMPLATE_FILE'		=> $template_file,
       
   932 			'TEMPLATE_DATA'		=> utf8_htmlspecialchars($template_data),
       
   933 			'TEXT_ROWS'			=> $text_rows)
       
   934 		);
       
   935 	}
       
   936 
       
   937 	/**
       
   938 	* Allows the admin to view cached versions of template files and clear single template cache files
       
   939 	*
       
   940 	* @param int $template_id specifies which template's cache is shown
       
   941 	*/
       
   942 	function template_cache($template_id)
       
   943 	{
       
   944 		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template;
       
   945 
       
   946 		$source		= str_replace('/', '.', request_var('source', ''));
       
   947 		$file_ary	= array_diff(request_var('delete', array('')), array(''));
       
   948 		$submit		= isset($_POST['submit']) ? true : false;
       
   949 
       
   950 		$sql = 'SELECT *
       
   951 			FROM ' . STYLES_TEMPLATE_TABLE . "
       
   952 			WHERE template_id = $template_id";
       
   953 		$result = $db->sql_query($sql);
       
   954 		$template_row = $db->sql_fetchrow($result);
       
   955 		$db->sql_freeresult($result);
       
   956 
       
   957 		if (!$template_row)
       
   958 		{
       
   959 			trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   960 		}
       
   961 
       
   962 		// User wants to delete one or more files ...
       
   963 		if ($submit && $file_ary)
       
   964 		{
       
   965 			$this->clear_template_cache($template_row, $file_ary);
       
   966 			trigger_error($user->lang['TEMPLATE_CACHE_CLEARED'] . adm_back_link($this->u_action . "&amp;action=cache&amp;id=$template_id"));
       
   967 		}
       
   968 
       
   969 		$cache_prefix = 'tpl_' . str_replace('_', '-', $template_row['template_path']);
       
   970 
       
   971 		// Someone wants to see the cached source ... so we'll highlight it,
       
   972 		// add line numbers and indent it appropriately. This could be nasty
       
   973 		// on larger source files ...
       
   974 		if ($source && file_exists("{$phpbb_root_path}cache/{$cache_prefix}_$source.html.$phpEx"))
       
   975 		{
       
   976 			adm_page_header($user->lang['TEMPLATE_CACHE']);
       
   977 
       
   978 			$template->set_filenames(array(
       
   979 				'body'	=> 'viewsource.html')
       
   980 			);
       
   981 
       
   982 			$template->assign_vars(array(
       
   983 				'FILENAME'	=> str_replace('.', '/', $source) . '.html')
       
   984 			);
       
   985 
       
   986 			$code = str_replace(array("\r\n", "\r"), array("\n", "\n"), file_get_contents("{$phpbb_root_path}cache/{$cache_prefix}_$source.html.$phpEx"));
       
   987 
       
   988 			$conf = array('highlight.bg', 'highlight.comment', 'highlight.default', 'highlight.html', 'highlight.keyword', 'highlight.string');
       
   989 			foreach ($conf as $ini_var)
       
   990 			{
       
   991 				@ini_set($ini_var, str_replace('highlight.', 'syntax', $ini_var));
       
   992 			}
       
   993 
       
   994 			$marker = 'MARKER' . time();
       
   995 			$code = highlight_string(str_replace("\n", $marker, $code), true);
       
   996 			$code = str_replace($marker, "\n", $code);
       
   997 			$str_from = array('<span style="color: ', '<font color="syntax', '</font>', '<code>', '</code>','[', ']', '.', ':');
       
   998 			$str_to = array('<span class="', '<span class="syntax', '</span>', '', '', '&#91;', '&#93;', '&#46;', '&#58;');
       
   999 
       
  1000 			$code = str_replace($str_from, $str_to, $code);
       
  1001 			$code = preg_replace('#^(<span class="[a-z_]+">)\n?(.*?)\n?(</span>)$#ism', '$1$2$3', $code);
       
  1002 			$code = substr($code, strlen('<span class="syntaxhtml">'));
       
  1003 			$code = substr($code, 0, -1 * strlen('</ span>'));
       
  1004 			$code = explode("\n", $code);
       
  1005 
       
  1006 			foreach ($code as $key => $line)
       
  1007 			{
       
  1008 				$template->assign_block_vars('source', array(
       
  1009 					'LINENUM'	=> $key + 1,
       
  1010 					'LINE'		=> preg_replace('#([^ ;])&nbsp;([^ &])#', '$1 $2', $line))
       
  1011 				);
       
  1012 				unset($code[$key]);
       
  1013 			}
       
  1014 
       
  1015 			adm_page_footer();
       
  1016 		}
       
  1017 
       
  1018 		$filemtime = array();
       
  1019 		if ($template_row['template_storedb'])
       
  1020 		{
       
  1021 			$ids = array();
       
  1022 			if (isset($template_row['template_inherits_id']) && $template_row['template_inherits_id'])
       
  1023 			{
       
  1024 				$ids[] = $template_row['template_inherits_id'];
       
  1025 			}
       
  1026 			$ids[] = $template_row['template_id'];
       
  1027 
       
  1028 			$filemtime 			= array();
       
  1029 			$file_template_db	= array();
       
  1030 
       
  1031 			foreach ($ids as $id)
       
  1032 			{
       
  1033 				$sql = 'SELECT template_filename, template_mtime
       
  1034 					FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
       
  1035 					WHERE template_id = $id";
       
  1036 				$result = $db->sql_query($sql);
       
  1037 
       
  1038 				while ($row = $db->sql_fetchrow($result))
       
  1039 				{
       
  1040 					$filemtime[$row['template_filename']] = $row['template_mtime'];
       
  1041 					$file_template_db[$row['template_filename']] = $id;
       
  1042 				}
       
  1043 				$db->sql_freeresult($result);
       
  1044 			}
       
  1045 		}
       
  1046 
       
  1047 		// Get a list of cached template files and then retrieve additional information about them
       
  1048 		$file_ary = $this->template_cache_filelist($template_row['template_path']);
       
  1049 
       
  1050 		foreach ($file_ary as $file)
       
  1051 		{
       
  1052 			$file		= str_replace('/', '.', $file);
       
  1053 
       
  1054 			// perform some dirty guessing to get the path right.
       
  1055 			// We assume that three dots in a row were '../'
       
  1056 			$tpl_file	= str_replace('.', '/', $file);
       
  1057 			$tpl_file	= str_replace('///', '../', $tpl_file);
       
  1058 
       
  1059 			$filename = "{$cache_prefix}_$file.html.$phpEx";
       
  1060 
       
  1061 			if (!file_exists("{$phpbb_root_path}cache/$filename"))
       
  1062 			{
       
  1063 				continue;
       
  1064 			}
       
  1065 
       
  1066 			$file_tpl = "{$phpbb_root_path}styles/{$template_row['template_path']}/template/$tpl_file.html";
       
  1067 			$inherited = false;
       
  1068 
       
  1069 			if (isset($template_row['template_inherits_id']) && $template_row['template_inherits_id'])
       
  1070 			{
       
  1071 				if (!$template_row['template_storedb'])
       
  1072 				{
       
  1073 					if (!file_exists($file_tpl))
       
  1074 					{
       
  1075 						$file_tpl = "{$phpbb_root_path}styles/{$template_row['template_inherit_path']}/template/$tpl_file.html";
       
  1076 						$inherited = true;
       
  1077 					}
       
  1078 				}
       
  1079 				else
       
  1080 				{
       
  1081 					if ($file_template_db[$file . '.html'] == $template_row['template_inherits_id'])
       
  1082 					{
       
  1083 						$file_tpl = "{$phpbb_root_path}styles/{$template_row['template_inherit_path']}/template/$tpl_file.html";
       
  1084 						$inherited = true;
       
  1085 					}
       
  1086 				}
       
  1087 			}
       
  1088 
       
  1089 			// Correct the filename if it is stored in database and the file is in a subfolder.
       
  1090 			if ($template_row['template_storedb'])
       
  1091 			{
       
  1092 				$file = str_replace('.', '/', $file);
       
  1093 			}
       
  1094 
       
  1095 			$template->assign_block_vars('file', array(
       
  1096 				'U_VIEWSOURCE'	=> $this->u_action . "&amp;action=cache&amp;id=$template_id&amp;source=$file",
       
  1097 
       
  1098 				'CACHED'		=> $user->format_date(filemtime("{$phpbb_root_path}cache/$filename")),
       
  1099 				'FILENAME'		=> $file,
       
  1100 				'FILENAME_PATH'	=> $file_tpl,
       
  1101 				'FILESIZE'		=> get_formatted_filesize(filesize("{$phpbb_root_path}cache/$filename")),
       
  1102 				'MODIFIED'		=> $user->format_date((!$template_row['template_storedb']) ? filemtime($file_tpl) : $filemtime[$file . '.html']))
       
  1103 			);
       
  1104 		}
       
  1105 		unset($filemtime);
       
  1106 
       
  1107 		$template->assign_vars(array(
       
  1108 			'S_CACHE'			=> true,
       
  1109 			'S_TEMPLATE'		=> true,
       
  1110 
       
  1111 			'U_ACTION'			=> $this->u_action . "&amp;action=cache&amp;id=$template_id",
       
  1112 			'U_BACK'			=> $this->u_action)
       
  1113 		);
       
  1114 	}
       
  1115 
       
  1116 	/**
       
  1117 	* Provides a css editor and a basic easier to use stylesheet editing tool for less experienced (or lazy) users
       
  1118 	*
       
  1119 	* @param int $theme_id specifies which theme is being edited
       
  1120 	*/
       
  1121 	function edit_theme($theme_id)
       
  1122 	{
       
  1123 		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template, $safe_mode;
       
  1124 
       
  1125 		$this->page_title = 'EDIT_THEME';
       
  1126 
       
  1127 		$filelist = $filelist_cats = array();
       
  1128 
       
  1129 		$theme_data		= utf8_normalize_nfc(request_var('template_data', '', true));
       
  1130 		$theme_data		= htmlspecialchars_decode($theme_data);
       
  1131 		$theme_file		= utf8_normalize_nfc(request_var('template_file', '', true));
       
  1132 		$text_rows		= max(5, min(999, request_var('text_rows', 20)));
       
  1133 		$save_changes	= (isset($_POST['save'])) ? true : false;
       
  1134 
       
  1135 		// make sure theme_file path doesn't go upwards
       
  1136 		$theme_file = str_replace('..', '.', $theme_file);
       
  1137 
       
  1138 		// Retrieve some information about the theme
       
  1139 		$sql = 'SELECT theme_storedb, theme_path, theme_name, theme_data
       
  1140 			FROM ' . STYLES_THEME_TABLE . "
       
  1141 			WHERE theme_id = $theme_id";
       
  1142 		$result = $db->sql_query($sql);
       
  1143 
       
  1144 		if (!($theme_info = $db->sql_fetchrow($result)))
       
  1145 		{
       
  1146 			trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
       
  1147 		}
       
  1148 		$db->sql_freeresult($result);
       
  1149 
       
  1150 		// save changes to the theme if the user submitted any
       
  1151 		if ($save_changes)
       
  1152 		{
       
  1153 			// Get the filesystem location of the current file
       
  1154 			$file = "{$phpbb_root_path}styles/{$theme_info['theme_path']}/theme/$theme_file";
       
  1155 			$additional = '';
       
  1156 			$message = $user->lang['THEME_UPDATED'];
       
  1157 
       
  1158 			// If the theme is stored on the filesystem try to write the file else store it in the database
       
  1159 			if (!$safe_mode && !$theme_info['theme_storedb'] && file_exists($file) && phpbb_is_writable($file))
       
  1160 			{
       
  1161 				if (!($fp = @fopen($file, 'wb')))
       
  1162 				{
       
  1163 					trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
       
  1164 				}
       
  1165 				fwrite($fp, $theme_data);
       
  1166 				fclose($fp);
       
  1167 			}
       
  1168 			else
       
  1169 			{
       
  1170 				// Write stylesheet to db
       
  1171 				$sql_ary = array(
       
  1172 					'theme_mtime'		=> time(),
       
  1173 					'theme_storedb'		=> 1,
       
  1174 					'theme_data'		=> $this->db_theme_data($theme_info, $theme_data),
       
  1175 				);
       
  1176 				$sql = 'UPDATE ' . STYLES_THEME_TABLE . '
       
  1177 					SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
       
  1178 					WHERE theme_id = ' . $theme_id;
       
  1179 				$db->sql_query($sql);
       
  1180 
       
  1181 				$cache->destroy('sql', STYLES_THEME_TABLE);
       
  1182 
       
  1183 				// notify the user if the theme was not stored in the db before his modification
       
  1184 				if (!$theme_info['theme_storedb'])
       
  1185 				{
       
  1186 					add_log('admin', 'LOG_THEME_EDIT_DETAILS', $theme_info['theme_name']);
       
  1187 					$message .= '<br />' . $user->lang['EDIT_THEME_STORED_DB'];
       
  1188 				}
       
  1189 			}
       
  1190 			$cache->destroy('sql', STYLES_THEME_TABLE);
       
  1191 			add_log('admin', (!$theme_info['theme_storedb']) ? 'LOG_THEME_EDIT_FILE' : 'LOG_THEME_EDIT', $theme_info['theme_name'], (!$theme_info['theme_storedb']) ? $theme_file : '');
       
  1192 
       
  1193 			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"));
       
  1194 		}
       
  1195 
       
  1196 		// Generate a category array containing theme filenames
       
  1197 		if (!$theme_info['theme_storedb'])
       
  1198 		{
       
  1199 			$theme_path = "{$phpbb_root_path}styles/{$theme_info['theme_path']}/theme";
       
  1200 
       
  1201 			$filelist = filelist($theme_path, '', 'css');
       
  1202 
       
  1203 			if ($theme_file)
       
  1204 			{
       
  1205 				if (!file_exists($theme_path . "/$theme_file") || !($theme_data = file_get_contents($theme_path . "/$theme_file")))
       
  1206 				{
       
  1207 					trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
       
  1208 				}
       
  1209 			}
       
  1210 		}
       
  1211 		else
       
  1212 		{
       
  1213 			$theme_data = &$theme_info['theme_data'];
       
  1214 		}
       
  1215 
       
  1216 		// Now create the categories
       
  1217 		$filelist_cats[''] = array();
       
  1218 		foreach ($filelist as $pathfile => $file_ary)
       
  1219 		{
       
  1220 			// Use the directory name as category name
       
  1221 			if (!empty($pathfile))
       
  1222 			{
       
  1223 				$filelist_cats[$pathfile] = array();
       
  1224 				foreach ($file_ary as $file)
       
  1225 				{
       
  1226 					$filelist_cats[$pathfile][$pathfile . $file] = $file;
       
  1227 				}
       
  1228 			}
       
  1229 			// or if it's in the main category use the word before the first underscore to group files
       
  1230 			else
       
  1231 			{
       
  1232 				$cats = array();
       
  1233 				foreach ($file_ary as $file)
       
  1234 				{
       
  1235 					$cats[] = substr($file, 0, strpos($file, '_'));
       
  1236 					$filelist_cats[substr($file, 0, strpos($file, '_'))][$file] = $file;
       
  1237 				}
       
  1238 
       
  1239 				$cats = array_values(array_unique($cats));
       
  1240 
       
  1241 				// we don't need any single element categories so put them into the misc '' category
       
  1242 				for ($i = 0, $n = sizeof($cats); $i < $n; $i++)
       
  1243 				{
       
  1244 					if (sizeof($filelist_cats[$cats[$i]]) == 1 && $cats[$i] !== '')
       
  1245 					{
       
  1246 						$filelist_cats[''][key($filelist_cats[$cats[$i]])] = current($filelist_cats[$cats[$i]]);
       
  1247 						unset($filelist_cats[$cats[$i]]);
       
  1248 					}
       
  1249 				}
       
  1250 				unset($cats);
       
  1251 			}
       
  1252 		}
       
  1253 		unset($filelist);
       
  1254 
       
  1255 		// Generate list of categorised theme files
       
  1256 		$tpl_options = '';
       
  1257 		ksort($filelist_cats);
       
  1258 		foreach ($filelist_cats as $category => $tpl_ary)
       
  1259 		{
       
  1260 			ksort($tpl_ary);
       
  1261 
       
  1262 			if (!empty($category))
       
  1263 			{
       
  1264 				$tpl_options .= '<option class="sep" value="">' . $category . '</option>';
       
  1265 			}
       
  1266 
       
  1267 			foreach ($tpl_ary as $filename => $file)
       
  1268 			{
       
  1269 				$selected = ($theme_file == $filename) ? ' selected="selected"' : '';
       
  1270 				$tpl_options .= '<option value="' . $filename . '"' . $selected . '>' . $file . '</option>';
       
  1271 			}
       
  1272 		}
       
  1273 
       
  1274 		$template->assign_vars(array(
       
  1275 			'S_EDIT_THEME'		=> true,
       
  1276 			'S_HIDDEN_FIELDS'	=> build_hidden_fields(array('template_file' => $theme_file)),
       
  1277 			'S_THEME_IN_DB'		=> $theme_info['theme_storedb'],
       
  1278 			'S_TEMPLATES'		=> $tpl_options,
       
  1279 
       
  1280 			'U_ACTION'			=> $this->u_action . "&amp;action=edit&amp;id=$theme_id&amp;text_rows=$text_rows",
       
  1281 			'U_BACK'			=> $this->u_action,
       
  1282 
       
  1283 			'L_EDIT'			=> $user->lang['EDIT_THEME'],
       
  1284 			'L_EDIT_EXPLAIN'	=> $user->lang['EDIT_THEME_EXPLAIN'],
       
  1285 			'L_EDITOR'			=> $user->lang['THEME_EDITOR'],
       
  1286 			'L_EDITOR_HEIGHT'	=> $user->lang['THEME_EDITOR_HEIGHT'],
       
  1287 			'L_FILE'			=> $user->lang['THEME_FILE'],
       
  1288 			'L_SELECT'			=> $user->lang['SELECT_THEME'],
       
  1289 			'L_SELECTED'		=> $user->lang['SELECTED_THEME'],
       
  1290 			'L_SELECTED_FILE'	=> $user->lang['SELECTED_THEME_FILE'],
       
  1291 
       
  1292 			'SELECTED_TEMPLATE'	=> $theme_info['theme_name'],
       
  1293 			'TEMPLATE_FILE'		=> $theme_file,
       
  1294 			'TEMPLATE_DATA'		=> utf8_htmlspecialchars($theme_data),
       
  1295 			'TEXT_ROWS'			=> $text_rows)
       
  1296 		);
       
  1297 	}
       
  1298 
       
  1299 	/**
       
  1300 	* Edit imagesets
       
  1301 	*
       
  1302 	* @param int $imageset_id specifies which imageset is being edited
       
  1303 	*/
       
  1304 	function edit_imageset($imageset_id)
       
  1305 	{
       
  1306 		global $db, $user, $phpbb_root_path, $cache, $template;
       
  1307 
       
  1308 		$this->page_title = 'EDIT_IMAGESET';
       
  1309 
       
  1310 		if (!$imageset_id)
       
  1311 		{
       
  1312 			trigger_error($user->lang['NO_IMAGESET'] . adm_back_link($this->u_action), E_USER_WARNING);
       
  1313 		}
       
  1314 
       
  1315 		$update		= (isset($_POST['update'])) ? true : false;
       
  1316 
       
  1317 		$imgname	= request_var('imgname', 'site_logo');
       
  1318 		$imgname	= preg_replace('#[^a-z0-9\-+_]#i', '', $imgname);
       
  1319 		$sql_extra = $imgnamelang = '';
       
  1320 
       
  1321 		$sql = 'SELECT imageset_path, imageset_name
       
  1322 			FROM ' . STYLES_IMAGESET_TABLE . "
       
  1323 			WHERE imageset_id = $imageset_id";
       
  1324 		$result = $db->sql_query($sql);
       
  1325 		$imageset_row = $db->sql_fetchrow($result);
       
  1326 		$db->sql_freeresult($result);
       
  1327 
       
  1328 		if (!$imageset_row)
       
  1329 		{
       
  1330 			trigger_error($user->lang['NO_IMAGESET'] . adm_back_link($this->u_action), E_USER_WARNING);
       
  1331 		}
       
  1332 
       
  1333 		$imageset_path		= $imageset_row['imageset_path'];
       
  1334 		$imageset_name		= $imageset_row['imageset_name'];
       
  1335 
       
  1336 		if (strpos($imgname, '-') !== false)
       
  1337 		{
       
  1338 			list($imgname, $imgnamelang) = explode('-', $imgname);
       
  1339 			$sql_extra = " AND image_lang IN ('" . $db->sql_escape($imgnamelang) . "', '')";
       
  1340 		}
       
  1341 
       
  1342 		$sql = 'SELECT image_filename, image_width, image_height, image_lang, image_id
       
  1343 			FROM ' . STYLES_IMAGESET_DATA_TABLE . "
       
  1344 			WHERE imageset_id = $imageset_id
       
  1345 				AND image_name = '" . $db->sql_escape($imgname) . "'$sql_extra";
       
  1346 		$result = $db->sql_query($sql);
       
  1347 		$imageset_data_row = $db->sql_fetchrow($result);
       
  1348 		$db->sql_freeresult($result);
       
  1349 
       
  1350 		$image_filename	= $imageset_data_row['image_filename'];
       
  1351 		$image_width	= $imageset_data_row['image_width'];
       
  1352 		$image_height	= $imageset_data_row['image_height'];
       
  1353 		$image_lang		= $imageset_data_row['image_lang'];
       
  1354 		$image_id		= $imageset_data_row['image_id'];
       
  1355 		$imgsize		= ($imageset_data_row['image_width'] && $imageset_data_row['image_height']) ? 1 : 0;
       
  1356 
       
  1357 		// Check to see whether the selected image exists in the table
       
  1358 		$valid_name = ($update) ? false : true;
       
  1359 
       
  1360 		foreach ($this->imageset_keys as $category => $img_ary)
       
  1361 		{
       
  1362 			if (in_array($imgname, $img_ary))
       
  1363 			{
       
  1364 				$valid_name = true;
       
  1365 				break;
       
  1366 			}
       
  1367 		}
       
  1368 
       
  1369 		if ($update && isset($_POST['imgpath']) && $valid_name)
       
  1370 		{
       
  1371 			// If imgwidth and imgheight are non-zero grab the actual size
       
  1372 			// from the image itself ... we ignore width settings for the poll center image
       
  1373 			$imgwidth	= request_var('imgwidth', 0);
       
  1374 			$imgheight	= request_var('imgheight', 0);
       
  1375 			$imgsize	= request_var('imgsize', 0);
       
  1376 			$imgpath	= request_var('imgpath', '');
       
  1377 			$imgpath	= str_replace('..', '.', $imgpath);
       
  1378 
       
  1379 			// If no dimensions selected, we reset width and height to 0 ;)
       
  1380 			if (!$imgsize)
       
  1381 			{
       
  1382 				$imgwidth = $imgheight = 0;
       
  1383 			}
       
  1384 
       
  1385 			$imglang = '';
       
  1386 
       
  1387 			if ($imgpath && !file_exists("{$phpbb_root_path}styles/$imageset_path/imageset/$imgpath"))
       
  1388 			{
       
  1389 				trigger_error($user->lang['NO_IMAGE_ERROR'] . adm_back_link($this->u_action), E_USER_WARNING);
       
  1390 			}
       
  1391 
       
  1392 			// Determine width/height. If dimensions included and no width/height given, we detect them automatically...
       
  1393 			if ($imgsize && $imgpath)
       
  1394 			{
       
  1395 				if (!$imgwidth || !$imgheight)
       
  1396 				{
       
  1397 					list($imgwidth_file, $imgheight_file) = getimagesize("{$phpbb_root_path}styles/$imageset_path/imageset/$imgpath");
       
  1398 					$imgwidth = ($imgwidth) ? $imgwidth : $imgwidth_file;
       
  1399 					$imgheight = ($imgheight) ? $imgheight : $imgheight_file;
       
  1400 				}
       
  1401 				$imgwidth	= ($imgname != 'poll_center') ? (int) $imgwidth : 0;
       
  1402 				$imgheight	= (int) $imgheight;
       
  1403 			}
       
  1404 
       
  1405 			if (strpos($imgpath, '/') !== false)
       
  1406 			{
       
  1407 				list($imglang, $imgfilename) = explode('/', $imgpath);
       
  1408 			}
       
  1409 			else
       
  1410 			{
       
  1411 				$imgfilename = $imgpath;
       
  1412 			}
       
  1413 
       
  1414 			$sql_ary = array(
       
  1415 				'image_filename'	=> (string) $imgfilename,
       
  1416 				'image_width'		=> (int) $imgwidth,
       
  1417 				'image_height'		=> (int) $imgheight,
       
  1418 				'image_lang'		=> (string) $imglang,
       
  1419 			);
       
  1420 
       
  1421 			// already exists
       
  1422 			if ($imageset_data_row)
       
  1423 			{
       
  1424 				$sql = 'UPDATE ' . STYLES_IMAGESET_DATA_TABLE . '
       
  1425 					SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
       
  1426 					WHERE image_id = $image_id";
       
  1427 				$db->sql_query($sql);
       
  1428 			}
       
  1429 			// does not exist
       
  1430 			else if (!$imageset_data_row)
       
  1431 			{
       
  1432 				$sql_ary['image_name']	= $imgname;
       
  1433 				$sql_ary['imageset_id']	= (int) $imageset_id;
       
  1434 				$db->sql_query('INSERT INTO ' . STYLES_IMAGESET_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
       
  1435 			}
       
  1436 
       
  1437 			$cache->destroy('sql', STYLES_IMAGESET_DATA_TABLE);
       
  1438 
       
  1439 			add_log('admin', 'LOG_IMAGESET_EDIT', $imageset_name);
       
  1440 
       
  1441 			$template->assign_var('SUCCESS', true);
       
  1442 
       
  1443 			$image_filename = $imgfilename;
       
  1444 			$image_width	= $imgwidth;
       
  1445 			$image_height	= $imgheight;
       
  1446 			$image_lang		= $imglang;
       
  1447 		}
       
  1448 
       
  1449 		$imglang = '';
       
  1450 		$imagesetlist = array('nolang' => array(), 'lang' => array());
       
  1451 		$langs = array();
       
  1452 
       
  1453 		$dir = "{$phpbb_root_path}styles/$imageset_path/imageset";
       
  1454 		$dp = @opendir($dir);
       
  1455 
       
  1456 		if ($dp)
       
  1457 		{
       
  1458 			while (($file = readdir($dp)) !== false)
       
  1459 			{
       
  1460 				if ($file[0] != '.' && strtoupper($file) != 'CVS' && !is_file($dir . '/' . $file) && !is_link($dir . '/' . $file))
       
  1461 				{
       
  1462 					$langs[] = $file;
       
  1463 				}
       
  1464 				else if (preg_match('#\.(?:gif|jpg|png)$#', $file))
       
  1465 				{
       
  1466 					$imagesetlist['nolang'][] = $file;
       
  1467 				}
       
  1468 			}
       
  1469 
       
  1470 			if ($sql_extra)
       
  1471 			{
       
  1472 				$dp2 = @opendir("$dir/$imgnamelang");
       
  1473 
       
  1474 				if ($dp2)
       
  1475 				{
       
  1476 					while (($file2 = readdir($dp2)) !== false)
       
  1477 					{
       
  1478 						if (preg_match('#\.(?:gif|jpg|png)$#', $file2))
       
  1479 						{
       
  1480 							$imagesetlist['lang'][] = "$imgnamelang/$file2";
       
  1481 						}
       
  1482 					}
       
  1483 					closedir($dp2);
       
  1484 				}
       
  1485 			}
       
  1486 			closedir($dp);
       
  1487 		}
       
  1488 
       
  1489 		// Generate list of image options
       
  1490 		$img_options = '';
       
  1491 		foreach ($this->imageset_keys as $category => $img_ary)
       
  1492 		{
       
  1493 			$template->assign_block_vars('category', array(
       
  1494 				'NAME'			=> $user->lang['IMG_CAT_' . strtoupper($category)]
       
  1495 			));
       
  1496 
       
  1497 			foreach ($img_ary as $img)
       
  1498 			{
       
  1499 				if ($category == 'buttons')
       
  1500 				{
       
  1501 					foreach ($langs as $language)
       
  1502 					{
       
  1503 						$template->assign_block_vars('category.images', array(
       
  1504 							'SELECTED'			=> ($img == $imgname && $language == $imgnamelang),
       
  1505 							'VALUE'				=> $img . '-' . $language,
       
  1506 							'TEXT'				=> $user->lang['IMG_' . strtoupper($img)] . ' [ ' . $language . ' ]'
       
  1507 						));
       
  1508 					}
       
  1509 				}
       
  1510 				else
       
  1511 				{
       
  1512 					$template->assign_block_vars('category.images', array(
       
  1513 						'SELECTED'			=> ($img == $imgname),
       
  1514 						'VALUE'				=> $img,
       
  1515 						'TEXT'				=> (($category == 'custom') ? $img : $user->lang['IMG_' . strtoupper($img)])
       
  1516 					));
       
  1517 				}
       
  1518 			}
       
  1519 		}
       
  1520 
       
  1521 		// Make sure the list of possible images is sorted alphabetically
       
  1522 		sort($imagesetlist['lang']);
       
  1523 		sort($imagesetlist['nolang']);
       
  1524 
       
  1525 		$image_found = false;
       
  1526 		$img_val = '';
       
  1527 		foreach ($imagesetlist as $type => $img_ary)
       
  1528 		{
       
  1529 			if ($type !== 'lang' || $sql_extra)
       
  1530 			{
       
  1531 				$template->assign_block_vars('imagesetlist', array(
       
  1532 					'TYPE'	=> ($type == 'lang')
       
  1533 				));
       
  1534 			}
       
  1535 
       
  1536 			foreach ($img_ary as $img)
       
  1537 			{
       
  1538 				$imgtext = preg_replace('/^([^\/]+\/)/', '', $img);
       
  1539 				$selected = (!empty($imgname) && strpos($image_filename, $imgtext) !== false);
       
  1540 				if ($selected)
       
  1541 				{
       
  1542 					$image_found = true;
       
  1543 					$img_val = htmlspecialchars($img);
       
  1544 				}
       
  1545 				$template->assign_block_vars('imagesetlist.images', array(
       
  1546 					'SELECTED'			=> $selected,
       
  1547 					'TEXT'				=> $imgtext,
       
  1548 					'VALUE'				=> htmlspecialchars($img)
       
  1549 				));
       
  1550 			}
       
  1551 		}
       
  1552 
       
  1553 		$imgsize_bool = (!empty($imgname) && $image_width && $image_height) ? true : false;
       
  1554 		$image_request = '../styles/' . $imageset_path . '/imageset/' . ($image_lang ? $imgnamelang . '/' : '') . $image_filename;
       
  1555 
       
  1556 		$template->assign_vars(array(
       
  1557 			'S_EDIT_IMAGESET'	=> true,
       
  1558 			'L_TITLE'			=> $user->lang[$this->page_title],
       
  1559 			'L_EXPLAIN'			=> $user->lang[$this->page_title . '_EXPLAIN'],
       
  1560 			'IMAGE_OPTIONS'		=> $img_options,
       
  1561 			'IMAGE_SIZE'		=> $image_width,
       
  1562 			'IMAGE_HEIGHT'		=> $image_height,
       
  1563 			'IMAGE_REQUEST'		=> (empty($image_filename)) ? 'images/no_image.png' : $image_request,
       
  1564 			'U_ACTION'			=> $this->u_action . "&amp;action=edit&amp;id=$imageset_id",
       
  1565 			'U_BACK'			=> $this->u_action,
       
  1566 			'NAME'				=> $imageset_name,
       
  1567 			'A_NAME'			=> addslashes($imageset_name),
       
  1568 			'PATH'				=> $imageset_path,
       
  1569 			'A_PATH'			=> addslashes($imageset_path),
       
  1570 			'ERROR'				=> !$valid_name,
       
  1571 			'IMG_SRC'			=> ($image_found) ? '../styles/' . $imageset_path . '/imageset/' . $img_val : 'images/no_image.png',
       
  1572 			'IMAGE_SELECT'		=> $image_found
       
  1573 		));
       
  1574 	}
       
  1575 
       
  1576 	/**
       
  1577 	* Remove style/template/theme/imageset
       
  1578 	*/
       
  1579 	function remove($mode, $style_id)
       
  1580 	{
       
  1581 		global $db, $template, $user, $phpbb_root_path, $cache, $config;
       
  1582 
       
  1583 		$new_id = request_var('new_id', 0);
       
  1584 		$update = (isset($_POST['update'])) ? true : false;
       
  1585 		$sql_where = '';
       
  1586 
       
  1587 		switch ($mode)
       
  1588 		{
       
  1589 			case 'style':
       
  1590 				$sql_from = STYLES_TABLE;
       
  1591 				$sql_select = 'style_id, style_name, template_id, theme_id, imageset_id';
       
  1592 				$sql_where = 'AND style_active = 1';
       
  1593 			break;
       
  1594 
       
  1595 			case 'template':
       
  1596 				$sql_from = STYLES_TEMPLATE_TABLE;
       
  1597 				$sql_select = 'template_id, template_name, template_path, template_storedb';
       
  1598 			break;
       
  1599 
       
  1600 			case 'theme':
       
  1601 				$sql_from = STYLES_THEME_TABLE;
       
  1602 				$sql_select = 'theme_id, theme_name, theme_path, theme_storedb';
       
  1603 			break;
       
  1604 
       
  1605 			case 'imageset':
       
  1606 				$sql_from = STYLES_IMAGESET_TABLE;
       
  1607 				$sql_select = 'imageset_id, imageset_name, imageset_path';
       
  1608 			break;
       
  1609 		}
       
  1610 
       
  1611 		if ($mode === 'template' && ($conflicts = $this->check_inheritance($mode, $style_id)))
       
  1612 		{
       
  1613 			$l_type = strtoupper($mode);
       
  1614 			$msg = $user->lang[$l_type . '_DELETE_DEPENDENT'];
       
  1615 			foreach ($conflicts as $id => $values)
       
  1616 			{
       
  1617 				$msg .= '<br />' . $values['template_name'];
       
  1618 			}
       
  1619 
       
  1620 			trigger_error($msg . adm_back_link($this->u_action), E_USER_WARNING);
       
  1621 		}
       
  1622 
       
  1623 		$l_prefix = strtoupper($mode);
       
  1624 
       
  1625 		$sql = "SELECT $sql_select
       
  1626 			FROM $sql_from
       
  1627 			WHERE {$mode}_id = $style_id";
       
  1628 		$result = $db->sql_query($sql);
       
  1629 		$style_row = $db->sql_fetchrow($result);
       
  1630 		$db->sql_freeresult($result);
       
  1631 
       
  1632 		if (!$style_row)
       
  1633 		{
       
  1634 			trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
       
  1635 		}
       
  1636 
       
  1637 		$s_only_component = $this->display_component_options($mode, $style_row[$mode . '_id'], $style_row);
       
  1638 
       
  1639 		if ($s_only_component)
       
  1640 		{
       
  1641 			trigger_error($user->lang['ONLY_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
       
  1642 		}
       
  1643 
       
  1644 		if ($update)
       
  1645 		{
       
  1646 			if ($mode == 'style')
       
  1647 			{
       
  1648 				$sql = "DELETE FROM $sql_from
       
  1649 					WHERE {$mode}_id = $style_id";
       
  1650 				$db->sql_query($sql);
       
  1651 
       
  1652 				$sql = 'UPDATE ' . USERS_TABLE . "
       
  1653 					SET user_style = $new_id
       
  1654 					WHERE user_style = $style_id";
       
  1655 				$db->sql_query($sql);
       
  1656 
       
  1657 				$sql = 'UPDATE ' . FORUMS_TABLE . "
       
  1658 					SET forum_style = $new_id
       
  1659 					WHERE forum_style = $style_id";
       
  1660 				$db->sql_query($sql);
       
  1661 
       
  1662 				if ($style_id == $config['default_style'])
       
  1663 				{
       
  1664 					set_config('default_style', $new_id);
       
  1665 				}
       
  1666 
       
  1667 				// Remove the components
       
  1668 				$components = array('template', 'theme', 'imageset');
       
  1669 				foreach ($components as $component)
       
  1670 				{
       
  1671 					$new_id = request_var('new_' . $component . '_id', 0);
       
  1672 					$component_id = $style_row[$component . '_id'];
       
  1673 					$this->remove_component($component, $component_id, $new_id, $style_id);
       
  1674 				}
       
  1675 			}
       
  1676 			else
       
  1677 			{
       
  1678 				$this->remove_component($mode, $style_id, $new_id);
       
  1679 			}
       
  1680 
       
  1681 			$cache->destroy('sql', STYLES_TABLE);
       
  1682 
       
  1683 			add_log('admin', 'LOG_' . $l_prefix . '_DELETE', $style_row[$mode . '_name']);
       
  1684 			$message = ($mode != 'style') ? $l_prefix . '_DELETED_FS' : $l_prefix . '_DELETED';
       
  1685 			trigger_error($user->lang[$message] . adm_back_link($this->u_action));
       
  1686 		}
       
  1687 
       
  1688 		$this->page_title = 'DELETE_' . $l_prefix;
       
  1689 
       
  1690 		$template->assign_vars(array(
       
  1691 			'S_DELETE'			=> true,
       
  1692 
       
  1693 			'L_TITLE'			=> $user->lang[$this->page_title],
       
  1694 			'L_EXPLAIN'			=> $user->lang[$this->page_title . '_EXPLAIN'],
       
  1695 			'L_NAME'			=> $user->lang[$l_prefix . '_NAME'],
       
  1696 			'L_REPLACE'			=> $user->lang['REPLACE_' . $l_prefix],
       
  1697 			'L_REPLACE_EXPLAIN'	=> $user->lang['REPLACE_' . $l_prefix . '_EXPLAIN'],
       
  1698 
       
  1699 			'U_ACTION'		=> $this->u_action . "&amp;action=delete&amp;id=$style_id",
       
  1700 			'U_BACK'		=> $this->u_action,
       
  1701 
       
  1702 			'NAME'			=> $style_row[$mode . '_name'],
       
  1703 			)
       
  1704 		);
       
  1705 
       
  1706 		if ($mode == 'style')
       
  1707 		{
       
  1708 			$template->assign_vars(array(
       
  1709 				'S_DELETE_STYLE'		=> true,
       
  1710 			));
       
  1711 		}
       
  1712 	}
       
  1713 
       
  1714 	/**
       
  1715 	* Remove template/theme/imageset entry from the database
       
  1716 	*/
       
  1717 	function remove_component($component, $component_id, $new_id, $style_id = false)
       
  1718 	{
       
  1719 		global $db;
       
  1720 
       
  1721 		if (($new_id == 0) || ($component === 'template' && ($conflicts = $this->check_inheritance($component, $component_id))))
       
  1722 		{
       
  1723 			// We can not delete the template, as the user wants to keep the component or an other template is inheriting from this one.
       
  1724 			return;
       
  1725 		}
       
  1726 
       
  1727 		$component_in_use = array();
       
  1728 		if ($component != 'style')
       
  1729 		{
       
  1730 			$component_in_use = $this->component_in_use($component, $component_id, $style_id);
       
  1731 		}
       
  1732 
       
  1733 		if (($new_id == -1) && !empty($component_in_use))
       
  1734 		{
       
  1735 			// We can not delete the component, as it is still in use
       
  1736 			return;
       
  1737 		}
       
  1738 
       
  1739 		if ($component == 'imageset')
       
  1740 		{
       
  1741 			$sql = 'DELETE FROM ' . STYLES_IMAGESET_DATA_TABLE . "
       
  1742 				WHERE imageset_id = $component_id";
       
  1743 			$db->sql_query($sql);
       
  1744 		}
       
  1745 
       
  1746 		switch ($component)
       
  1747 		{
       
  1748 			case 'template':
       
  1749 				$sql_from = STYLES_TEMPLATE_TABLE;
       
  1750 			break;
       
  1751 
       
  1752 			case 'theme':
       
  1753 				$sql_from = STYLES_THEME_TABLE;
       
  1754 			break;
       
  1755 
       
  1756 			case 'imageset':
       
  1757 				$sql_from = STYLES_IMAGESET_TABLE;;
       
  1758 			break;
       
  1759 		}
       
  1760 
       
  1761 		$sql = "DELETE FROM $sql_from
       
  1762 			WHERE {$component}_id = $component_id";
       
  1763 		$db->sql_query($sql);
       
  1764 
       
  1765 		$sql = 'UPDATE ' . STYLES_TABLE . "
       
  1766 			SET {$component}_id = $new_id
       
  1767 			WHERE {$component}_id = $component_id";
       
  1768 		$db->sql_query($sql);
       
  1769 	}
       
  1770 
       
  1771 	/**
       
  1772 	* Display the options which can be used to replace a style/template/theme/imageset
       
  1773 	*
       
  1774 	* @return boolean Returns true if the component is the only component and can not be deleted.
       
  1775 	*/
       
  1776 	function display_component_options($component, $component_id, $style_row = false, $style_id = false)
       
  1777 	{
       
  1778 		global $db, $template, $user;
       
  1779 
       
  1780 		$is_only_component = true;
       
  1781 		$component_in_use = array();
       
  1782 		if ($component != 'style')
       
  1783 		{
       
  1784 			$component_in_use = $this->component_in_use($component, $component_id, $style_id);
       
  1785 		}
       
  1786 
       
  1787 		$sql_where = '';
       
  1788 		switch ($component)
       
  1789 		{
       
  1790 			case 'style':
       
  1791 				$sql_from = STYLES_TABLE;
       
  1792 				$sql_where = 'WHERE style_active = 1';
       
  1793 			break;
       
  1794 
       
  1795 			case 'template':
       
  1796 				$sql_from = STYLES_TEMPLATE_TABLE;
       
  1797 				$sql_where = 'WHERE template_inherits_id <> ' . $component_id;
       
  1798 			break;
       
  1799 
       
  1800 			case 'theme':
       
  1801 				$sql_from = STYLES_THEME_TABLE;
       
  1802 			break;
       
  1803 
       
  1804 			case 'imageset':
       
  1805 				$sql_from = STYLES_IMAGESET_TABLE;
       
  1806 			break;
       
  1807 		}
       
  1808 
       
  1809 		$s_options = '';
       
  1810 		if (($component != 'style') && empty($component_in_use))
       
  1811 		{
       
  1812 			// If it is not in use, there must be another component
       
  1813 			$is_only_component = false;
       
  1814 
       
  1815 			$sql = "SELECT {$component}_id, {$component}_name
       
  1816 				FROM $sql_from
       
  1817 				WHERE {$component}_id = {$component_id}";
       
  1818 			$result = $db->sql_query($sql);
       
  1819 			$row = $db->sql_fetchrow($result);
       
  1820 			$db->sql_freeresult($result);
       
  1821 
       
  1822 			$s_options .= '<option value="-1" selected="selected">' . $user->lang['DELETE_' . strtoupper($component)] . '</option>';
       
  1823 			$s_options .= '<option value="0">' . sprintf($user->lang['KEEP_' . strtoupper($component)], $row[$component . '_name']) . '</option>';
       
  1824 		}
       
  1825 		else
       
  1826 		{
       
  1827 			$sql = "SELECT {$component}_id, {$component}_name
       
  1828 				FROM $sql_from
       
  1829 				$sql_where
       
  1830 				ORDER BY {$component}_name ASC";
       
  1831 			$result = $db->sql_query($sql);
       
  1832 
       
  1833 			$s_keep_option = $s_options = '';
       
  1834 			while ($row = $db->sql_fetchrow($result))
       
  1835 			{
       
  1836 				if ($row[$component . '_id'] != $component_id)
       
  1837 				{
       
  1838 					$is_only_component = false;
       
  1839 					$s_options .= '<option value="' . $row[$component . '_id'] . '">' . sprintf($user->lang['REPLACE_WITH_OPTION'], $row[$component . '_name']) . '</option>';
       
  1840 				}
       
  1841 				else if ($component != 'style')
       
  1842 				{
       
  1843 					$s_keep_option = '<option value="0" selected="selected">' . sprintf($user->lang['KEEP_' . strtoupper($component)], $row[$component . '_name']) . '</option>';
       
  1844 				}
       
  1845 			}
       
  1846 			$db->sql_freeresult($result);
       
  1847 			$s_options = $s_keep_option . $s_options;
       
  1848 		}
       
  1849 
       
  1850 		if (!$style_row)
       
  1851 		{
       
  1852 			$template->assign_var('S_REPLACE_' . strtoupper($component) . '_OPTIONS', $s_options);
       
  1853 		}
       
  1854 		else
       
  1855 		{
       
  1856 			$template->assign_var('S_REPLACE_OPTIONS', $s_options);
       
  1857 			if ($component == 'style')
       
  1858 			{
       
  1859 				$components = array('template', 'theme', 'imageset');
       
  1860 				foreach ($components as $component)
       
  1861 				{
       
  1862 					$this->display_component_options($component, $style_row[$component . '_id'], false, $component_id, true);
       
  1863 				}
       
  1864 			}
       
  1865 		}
       
  1866 
       
  1867 		return $is_only_component;
       
  1868 	}
       
  1869 
       
  1870 	/**
       
  1871 	* Check whether the component is still used by another style or component
       
  1872 	*/
       
  1873 	function component_in_use($component, $component_id, $style_id = false)
       
  1874 	{
       
  1875 		global $db;
       
  1876 
       
  1877 		$component_in_use = array();
       
  1878 
       
  1879 		if ($style_id)
       
  1880 		{
       
  1881 			$sql = 'SELECT style_id, style_name
       
  1882 				FROM ' . STYLES_TABLE . "
       
  1883 				WHERE {$component}_id = {$component_id}
       
  1884 					AND style_id <> {$style_id}
       
  1885 				ORDER BY style_name ASC";
       
  1886 		}
       
  1887 		else
       
  1888 		{
       
  1889 			$sql = 'SELECT style_id, style_name
       
  1890 				FROM ' . STYLES_TABLE . "
       
  1891 				WHERE {$component}_id = {$component_id}
       
  1892 				ORDER BY style_name ASC";
       
  1893 		}
       
  1894 		$result = $db->sql_query($sql);
       
  1895 		while ($row = $db->sql_fetchrow($result))
       
  1896 		{
       
  1897 			$component_in_use[] = $row['style_name'];
       
  1898 		}
       
  1899 		$db->sql_freeresult($result);
       
  1900 
       
  1901 		if ($component === 'template' && ($conflicts = $this->check_inheritance($component, $component_id)))
       
  1902 		{
       
  1903 			foreach ($conflicts as $temp_id => $conflict_data)
       
  1904 			{
       
  1905 				$component_in_use[] = $conflict_data['template_name'];
       
  1906 			}
       
  1907 		}
       
  1908 
       
  1909 		return $component_in_use;
       
  1910 	}
       
  1911 
       
  1912 	/**
       
  1913 	* Export style or style elements
       
  1914 	*/
       
  1915 	function export($mode, $style_id)
       
  1916 	{
       
  1917 		global $db, $template, $user, $phpbb_root_path, $cache, $phpEx, $config;
       
  1918 
       
  1919 		$update = (isset($_POST['update'])) ? true : false;
       
  1920 
       
  1921 		$inc_template = request_var('inc_template', 0);
       
  1922 		$inc_theme = request_var('inc_theme', 0);
       
  1923 		$inc_imageset = request_var('inc_imageset', 0);
       
  1924 		$store = request_var('store', 0);
       
  1925 		$format = request_var('format', '');
       
  1926 
       
  1927 		$error = array();
       
  1928 		$methods = array('tar');
       
  1929 
       
  1930 		$available_methods = array('tar.gz' => 'zlib', 'tar.bz2' => 'bz2', 'zip' => 'zlib');
       
  1931 		foreach ($available_methods as $type => $module)
       
  1932 		{
       
  1933 			if (!@extension_loaded($module))
       
  1934 			{
       
  1935 				continue;
       
  1936 			}
       
  1937 
       
  1938 			$methods[] = $type;
       
  1939 		}
       
  1940 
       
  1941 		if (!in_array($format, $methods))
       
  1942 		{
       
  1943 			$format = 'tar';
       
  1944 		}
       
  1945 
       
  1946 		switch ($mode)
       
  1947 		{
       
  1948 			case 'style':
       
  1949 				if ($update && ($inc_template + $inc_theme + $inc_imageset) < 1)
       
  1950 				{
       
  1951 					$error[] = $user->lang['STYLE_ERR_MORE_ELEMENTS'];
       
  1952 				}
       
  1953 
       
  1954 				$name = 'style_name';
       
  1955 
       
  1956 				$sql_select = 's.style_id, s.style_name, s.style_copyright';
       
  1957 				$sql_select .= ($inc_template) ? ', t.*' : ', t.template_name';
       
  1958 				$sql_select .= ($inc_theme) ? ', c.*' : ', c.theme_name';
       
  1959 				$sql_select .= ($inc_imageset) ? ', i.*' : ', i.imageset_name';
       
  1960 				$sql_from = STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . ' i';
       
  1961 				$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";
       
  1962 
       
  1963 				$l_prefix = 'STYLE';
       
  1964 			break;
       
  1965 
       
  1966 			case 'template':
       
  1967 				$name = 'template_name';
       
  1968 
       
  1969 				$sql_select = '*';
       
  1970 				$sql_from = STYLES_TEMPLATE_TABLE;
       
  1971 				$sql_where = "template_id = $style_id";
       
  1972 
       
  1973 				$l_prefix = 'TEMPLATE';
       
  1974 			break;
       
  1975 
       
  1976 			case 'theme':
       
  1977 				$name = 'theme_name';
       
  1978 
       
  1979 				$sql_select = '*';
       
  1980 				$sql_from = STYLES_THEME_TABLE;
       
  1981 				$sql_where = "theme_id = $style_id";
       
  1982 
       
  1983 				$l_prefix = 'THEME';
       
  1984 			break;
       
  1985 
       
  1986 			case 'imageset':
       
  1987 				$name = 'imageset_name';
       
  1988 
       
  1989 				$sql_select = '*';
       
  1990 				$sql_from = STYLES_IMAGESET_TABLE;
       
  1991 				$sql_where = "imageset_id = $style_id";
       
  1992 
       
  1993 				$l_prefix = 'IMAGESET';
       
  1994 			break;
       
  1995 		}
       
  1996 
       
  1997 		if ($update && !sizeof($error))
       
  1998 		{
       
  1999 			$sql = "SELECT $sql_select
       
  2000 				FROM $sql_from
       
  2001 				WHERE $sql_where";
       
  2002 			$result = $db->sql_query($sql);
       
  2003 			$style_row = $db->sql_fetchrow($result);
       
  2004 			$db->sql_freeresult($result);
       
  2005 
       
  2006 			if (!$style_row)
       
  2007 			{
       
  2008 				trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
       
  2009 			}
       
  2010 
       
  2011 			$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');
       
  2012 
       
  2013 			foreach ($var_ary as $var)
       
  2014 			{
       
  2015 				if (!isset($style_row[$var]))
       
  2016 				{
       
  2017 					$style_row[$var] = '';
       
  2018 				}
       
  2019 			}
       
  2020 
       
  2021 			$files = $data = array();
       
  2022 
       
  2023 			if ($mode == 'style')
       
  2024 			{
       
  2025 				$style_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['style_name'], $style_row['style_copyright'], $config['version']), $this->style_cfg);
       
  2026 
       
  2027 				$style_cfg .= (!$inc_template) ? "\nrequired_template = {$style_row['template_name']}" : '';
       
  2028 				$style_cfg .= (!$inc_theme) ? "\nrequired_theme = {$style_row['theme_name']}" : '';
       
  2029 				$style_cfg .= (!$inc_imageset) ? "\nrequired_imageset = {$style_row['imageset_name']}" : '';
       
  2030 
       
  2031 				$data[] = array(
       
  2032 					'src'		=> $style_cfg,
       
  2033 					'prefix'	=> 'style.cfg'
       
  2034 				);
       
  2035 
       
  2036 				unset($style_cfg);
       
  2037 			}
       
  2038 
       
  2039 			// Export template core code
       
  2040 			if ($mode == 'template' || $inc_template)
       
  2041 			{
       
  2042 				$template_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['template_name'], $style_row['template_copyright'], $config['version']), $this->template_cfg);
       
  2043 
       
  2044 				$use_template_name = '';
       
  2045 
       
  2046 				// Add the inherit from variable, depending on it's use...
       
  2047 				if ($style_row['template_inherits_id'])
       
  2048 				{
       
  2049 					// Get the template name
       
  2050 					$sql = 'SELECT template_name
       
  2051 						FROM ' . STYLES_TEMPLATE_TABLE . '
       
  2052 						WHERE template_id = ' . (int) $style_row['template_inherits_id'];
       
  2053 					$result = $db->sql_query($sql);
       
  2054 					$use_template_name = (string) $db->sql_fetchfield('template_name');
       
  2055 					$db->sql_freeresult($result);
       
  2056 				}
       
  2057 
       
  2058 				$template_cfg .= ($use_template_name) ? "\ninherit_from = $use_template_name" : "\n#inherit_from = ";
       
  2059 				$template_cfg .= "\n\nbbcode_bitfield = {$style_row['bbcode_bitfield']}";
       
  2060 
       
  2061 				$data[] = array(
       
  2062 					'src'		=> $template_cfg,
       
  2063 					'prefix'	=> 'template/template.cfg'
       
  2064 				);
       
  2065 
       
  2066 				// This is potentially nasty memory-wise ...
       
  2067 				if (!$style_row['template_storedb'])
       
  2068 				{
       
  2069 					$files[] = array(
       
  2070 						'src'		=> "styles/{$style_row['template_path']}/template/",
       
  2071 						'prefix-'	=> "styles/{$style_row['template_path']}/",
       
  2072 						'prefix+'	=> false,
       
  2073 						'exclude'	=> 'template.cfg'
       
  2074 					);
       
  2075 				}
       
  2076 				else
       
  2077 				{
       
  2078 					$sql = 'SELECT template_filename, template_data
       
  2079 						FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
       
  2080 						WHERE template_id = {$style_row['template_id']}";
       
  2081 					$result = $db->sql_query($sql);
       
  2082 
       
  2083 					while ($row = $db->sql_fetchrow($result))
       
  2084 					{
       
  2085 						$data[] = array(
       
  2086 							'src' => $row['template_data'],
       
  2087 							'prefix' => 'template/' . $row['template_filename']
       
  2088 						);
       
  2089 					}
       
  2090 					$db->sql_freeresult($result);
       
  2091 				}
       
  2092 				unset($template_cfg);
       
  2093 			}
       
  2094 
       
  2095 			// Export theme core code
       
  2096 			if ($mode == 'theme' || $inc_theme)
       
  2097 			{
       
  2098 				$theme_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['theme_name'], $style_row['theme_copyright'], $config['version']), $this->theme_cfg);
       
  2099 
       
  2100 				// Read old cfg file
       
  2101 				$items = $cache->obtain_cfg_items($style_row);
       
  2102 				$items = $items['theme'];
       
  2103 
       
  2104 				if (!isset($items['parse_css_file']))
       
  2105 				{
       
  2106 					$items['parse_css_file'] = 'off';
       
  2107 				}
       
  2108 
       
  2109 				$theme_cfg = str_replace(array('{PARSE_CSS_FILE}'), array($items['parse_css_file']), $theme_cfg);
       
  2110 
       
  2111 				$files[] = array(
       
  2112 					'src'		=> "styles/{$style_row['theme_path']}/theme/",
       
  2113 					'prefix-'	=> "styles/{$style_row['theme_path']}/",
       
  2114 					'prefix+'	=> false,
       
  2115 					'exclude'	=> ($style_row['theme_storedb']) ? 'stylesheet.css,theme.cfg' : 'theme.cfg'
       
  2116 				);
       
  2117 
       
  2118 				$data[] = array(
       
  2119 					'src'		=> $theme_cfg,
       
  2120 					'prefix'	=> 'theme/theme.cfg'
       
  2121 				);
       
  2122 
       
  2123 				if ($style_row['theme_storedb'])
       
  2124 				{
       
  2125 					$data[] = array(
       
  2126 						'src'		=> $style_row['theme_data'],
       
  2127 						'prefix'	=> 'theme/stylesheet.css'
       
  2128 					);
       
  2129 				}
       
  2130 
       
  2131 				unset($items, $theme_cfg);
       
  2132 			}
       
  2133 
       
  2134 			// Export imageset core code
       
  2135 			if ($mode == 'imageset' || $inc_imageset)
       
  2136 			{
       
  2137 				$imageset_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['imageset_name'], $style_row['imageset_copyright'], $config['version']), $this->imageset_cfg);
       
  2138 
       
  2139 				$imageset_main = array();
       
  2140 
       
  2141 				$sql = 'SELECT image_filename, image_name, image_height, image_width
       
  2142 					FROM ' . STYLES_IMAGESET_DATA_TABLE . "
       
  2143 					WHERE imageset_id = $style_id
       
  2144 						AND image_lang = ''";
       
  2145 				$result = $db->sql_query($sql);
       
  2146 				while ($row = $db->sql_fetchrow($result))
       
  2147 				{
       
  2148 					$imageset_main[$row['image_name']] = $row['image_filename'] . ($row['image_height'] ? '*' . $row['image_height']: '') . ($row['image_width'] ? '*' . $row['image_width']: '');
       
  2149 				}
       
  2150 				$db->sql_freeresult($result);
       
  2151 
       
  2152 				foreach ($this->imageset_keys as $topic => $key_array)
       
  2153 				{
       
  2154 					foreach ($key_array as $key)
       
  2155 					{
       
  2156 						if (isset($imageset_main[$key]))
       
  2157 						{
       
  2158 							$imageset_cfg .= "\nimg_" . $key . ' = ' . str_replace("styles/{$style_row['imageset_path']}/imageset/", '{PATH}', $imageset_main[$key]);
       
  2159 						}
       
  2160 					}
       
  2161 				}
       
  2162 
       
  2163 				$files[] = array(
       
  2164 					'src'		=> "styles/{$style_row['imageset_path']}/imageset/",
       
  2165 					'prefix-'	=> "styles/{$style_row['imageset_path']}/",
       
  2166 					'prefix+'	=> false,
       
  2167 					'exclude'	=> 'imageset.cfg'
       
  2168 				);
       
  2169 
       
  2170 				$data[] = array(
       
  2171 					'src'		=> trim($imageset_cfg),
       
  2172 					'prefix'	=> 'imageset/imageset.cfg'
       
  2173 				);
       
  2174 
       
  2175 				end($data);
       
  2176 
       
  2177 				$imageset_root = "{$phpbb_root_path}styles/{$style_row['imageset_path']}/imageset/";
       
  2178 
       
  2179 				if ($dh = @opendir($imageset_root))
       
  2180 				{
       
  2181 					while (($fname = readdir($dh)) !== false)
       
  2182 					{
       
  2183 						if ($fname[0] != '.' && $fname != 'CVS' && is_dir("$imageset_root$fname"))
       
  2184 						{
       
  2185 							$files[key($files)]['exclude'] .= ',' . $fname . '/imageset.cfg';
       
  2186 						}
       
  2187 					}
       
  2188 					closedir($dh);
       
  2189 				}
       
  2190 
       
  2191 				$imageset_lang = array();
       
  2192 
       
  2193 				$sql = 'SELECT image_filename, image_name, image_height, image_width, image_lang
       
  2194 					FROM ' . STYLES_IMAGESET_DATA_TABLE . "
       
  2195 					WHERE imageset_id = $style_id
       
  2196 						AND image_lang <> ''";
       
  2197 				$result = $db->sql_query($sql);
       
  2198 				while ($row = $db->sql_fetchrow($result))
       
  2199 				{
       
  2200 					$imageset_lang[$row['image_lang']][$row['image_name']] = $row['image_filename'] . ($row['image_height'] ? '*' . $row['image_height']: '') . ($row['image_width'] ? '*' . $row['image_width']: '');
       
  2201 				}
       
  2202 				$db->sql_freeresult($result);
       
  2203 
       
  2204 				foreach ($imageset_lang as $lang => $imageset_localized)
       
  2205 				{
       
  2206 					$imageset_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['imageset_name'], $style_row['imageset_copyright'], $config['version']), $this->imageset_cfg);
       
  2207 
       
  2208 					foreach ($this->imageset_keys as $topic => $key_array)
       
  2209 					{
       
  2210 						foreach ($key_array as $key)
       
  2211 						{
       
  2212 							if (isset($imageset_localized[$key]))
       
  2213 							{
       
  2214 								$imageset_cfg .= "\nimg_" . $key . ' = ' . str_replace("styles/{$style_row['imageset_path']}/imageset/", '{PATH}', $imageset_localized[$key]);
       
  2215 							}
       
  2216 						}
       
  2217 					}
       
  2218 
       
  2219 					$data[] = array(
       
  2220 						'src'		=> trim($imageset_cfg),
       
  2221 						'prefix'	=> 'imageset/' . $lang . '/imageset.cfg'
       
  2222 					);
       
  2223 				}
       
  2224 
       
  2225 				unset($imageset_cfg);
       
  2226 			}
       
  2227 
       
  2228 			switch ($format)
       
  2229 			{
       
  2230 				case 'tar':
       
  2231 					$ext = '.tar';
       
  2232 				break;
       
  2233 
       
  2234 				case 'zip':
       
  2235 					$ext = '.zip';
       
  2236 				break;
       
  2237 
       
  2238 				case 'tar.gz':
       
  2239 					$ext = '.tar.gz';
       
  2240 				break;
       
  2241 
       
  2242 				case 'tar.bz2':
       
  2243 					$ext = '.tar.bz2';
       
  2244 				break;
       
  2245 
       
  2246 				default:
       
  2247 					$error[] = $user->lang[$l_prefix . '_ERR_ARCHIVE'];
       
  2248 			}
       
  2249 
       
  2250 			if (!sizeof($error))
       
  2251 			{
       
  2252 				include($phpbb_root_path . 'includes/functions_compress.' . $phpEx);
       
  2253 
       
  2254 				if ($mode == 'style')
       
  2255 				{
       
  2256 					$path = preg_replace('#[^\w-]+#', '_', $style_row['style_name']);
       
  2257 				}
       
  2258 				else
       
  2259 				{
       
  2260 					$path = $style_row[$mode . '_path'];
       
  2261 				}
       
  2262 
       
  2263 				if ($format == 'zip')
       
  2264 				{
       
  2265 					$compress = new compress_zip('w', $phpbb_root_path . "store/$path$ext");
       
  2266 				}
       
  2267 				else
       
  2268 				{
       
  2269 					$compress = new compress_tar('w', $phpbb_root_path . "store/$path$ext", $ext);
       
  2270 				}
       
  2271 
       
  2272 				if (sizeof($files))
       
  2273 				{
       
  2274 					foreach ($files as $file_ary)
       
  2275 					{
       
  2276 						$compress->add_file($file_ary['src'], $file_ary['prefix-'], $file_ary['prefix+'], $file_ary['exclude']);
       
  2277 					}
       
  2278 				}
       
  2279 
       
  2280 				if (sizeof($data))
       
  2281 				{
       
  2282 					foreach ($data as $data_ary)
       
  2283 					{
       
  2284 						$compress->add_data($data_ary['src'], $data_ary['prefix']);
       
  2285 					}
       
  2286 				}
       
  2287 
       
  2288 				$compress->close();
       
  2289 
       
  2290 				add_log('admin', 'LOG_' . $l_prefix . '_EXPORT', $style_row[$mode . '_name']);
       
  2291 
       
  2292 				if (!$store)
       
  2293 				{
       
  2294 					$compress->download($path);
       
  2295 					@unlink("{$phpbb_root_path}store/$path$ext");
       
  2296 					exit;
       
  2297 				}
       
  2298 
       
  2299 				trigger_error(sprintf($user->lang[$l_prefix . '_EXPORTED'], "store/$path$ext") . adm_back_link($this->u_action));
       
  2300 			}
       
  2301 		}
       
  2302 
       
  2303 		$sql = "SELECT {$mode}_id, {$mode}_name
       
  2304 			FROM " . (($mode == 'style') ? STYLES_TABLE : $sql_from) . "
       
  2305 			WHERE {$mode}_id = $style_id";
       
  2306 		$result = $db->sql_query($sql);
       
  2307 		$style_row = $db->sql_fetchrow($result);
       
  2308 		$db->sql_freeresult($result);
       
  2309 
       
  2310 		if (!$style_row)
       
  2311 		{
       
  2312 			trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
       
  2313 		}
       
  2314 
       
  2315 		$this->page_title = $l_prefix . '_EXPORT';
       
  2316 
       
  2317 		$format_buttons = '';
       
  2318 		foreach ($methods as $method)
       
  2319 		{
       
  2320 			$format_buttons .= '<label><input type="radio"' . ((!$format_buttons) ? ' id="format"' : '') . ' class="radio" value="' . $method . '" name="format"' . (($method == $format) ? ' checked="checked"' : '') . ' /> ' . $method . '</label>';
       
  2321 		}
       
  2322 
       
  2323 		$template->assign_vars(array(
       
  2324 			'S_EXPORT'		=> true,
       
  2325 			'S_ERROR_MSG'	=> (sizeof($error)) ? true : false,
       
  2326 			'S_STYLE'		=> ($mode == 'style') ? true : false,
       
  2327 
       
  2328 			'L_TITLE'		=> $user->lang[$this->page_title],
       
  2329 			'L_EXPLAIN'		=> $user->lang[$this->page_title . '_EXPLAIN'],
       
  2330 			'L_NAME'		=> $user->lang[$l_prefix . '_NAME'],
       
  2331 
       
  2332 			'U_ACTION'		=> $this->u_action . '&amp;action=export&amp;id=' . $style_id,
       
  2333 			'U_BACK'		=> $this->u_action,
       
  2334 
       
  2335 			'ERROR_MSG'			=> (sizeof($error)) ? implode('<br />', $error) : '',
       
  2336 			'NAME'				=> $style_row[$mode . '_name'],
       
  2337 			'FORMAT_BUTTONS'	=> $format_buttons)
       
  2338 		);
       
  2339 	}
       
  2340 
       
  2341 	/**
       
  2342 	* Display details
       
  2343 	*/
       
  2344 	function details($mode, $style_id)
       
  2345 	{
       
  2346 		global $template, $db, $config, $user, $safe_mode, $cache, $phpbb_root_path;
       
  2347 
       
  2348 		$update = (isset($_POST['update'])) ? true : false;
       
  2349 		$l_type = strtoupper($mode);
       
  2350 
       
  2351 		$error = array();
       
  2352 		$element_ary = array('template' => STYLES_TEMPLATE_TABLE, 'theme' => STYLES_THEME_TABLE, 'imageset' => STYLES_IMAGESET_TABLE);
       
  2353 
       
  2354 		switch ($mode)
       
  2355 		{
       
  2356 			case 'style':
       
  2357 				$sql_from = STYLES_TABLE;
       
  2358 			break;
       
  2359 
       
  2360 			case 'template':
       
  2361 				$sql_from = STYLES_TEMPLATE_TABLE;
       
  2362 			break;
       
  2363 
       
  2364 			case 'theme':
       
  2365 				$sql_from = STYLES_THEME_TABLE;
       
  2366 			break;
       
  2367 
       
  2368 			case 'imageset':
       
  2369 				$sql_from = STYLES_IMAGESET_TABLE;
       
  2370 			break;
       
  2371 		}
       
  2372 
       
  2373 		$sql = "SELECT *
       
  2374 			FROM $sql_from
       
  2375 			WHERE {$mode}_id = $style_id";
       
  2376 		$result = $db->sql_query($sql);
       
  2377 		$style_row = $db->sql_fetchrow($result);
       
  2378 		$db->sql_freeresult($result);
       
  2379 
       
  2380 		if (!$style_row)
       
  2381 		{
       
  2382 			trigger_error($user->lang['NO_' . $l_type] . adm_back_link($this->u_action), E_USER_WARNING);
       
  2383 		}
       
  2384 
       
  2385 		$style_row['style_default'] = ($mode == 'style' && $config['default_style'] == $style_id) ? 1 : 0;
       
  2386 
       
  2387 		if ($update)
       
  2388 		{
       
  2389 			$name = utf8_normalize_nfc(request_var('name', '', true));
       
  2390 			$copyright = utf8_normalize_nfc(request_var('copyright', '', true));
       
  2391 
       
  2392 			$template_id = request_var('template_id', 0);
       
  2393 			$theme_id = request_var('theme_id', 0);
       
  2394 			$imageset_id = request_var('imageset_id', 0);
       
  2395 
       
  2396 			$style_active = request_var('style_active', 0);
       
  2397 			$style_default = request_var('style_default', 0);
       
  2398 			$store_db = request_var('store_db', 0);
       
  2399 
       
  2400 			// If the admin selected the style to be the default style, but forgot to activate it... we will do it for him
       
  2401 			if ($style_default)
       
  2402 			{
       
  2403 				$style_active = 1;
       
  2404 			}
       
  2405 
       
  2406 			$sql = "SELECT {$mode}_id, {$mode}_name
       
  2407 				FROM $sql_from
       
  2408 				WHERE {$mode}_id <> $style_id
       
  2409 				AND LOWER({$mode}_name) = '" . $db->sql_escape(strtolower($name)) . "'";
       
  2410 			$result = $db->sql_query($sql);
       
  2411 			$conflict = $db->sql_fetchrow($result);
       
  2412 			$db->sql_freeresult($result);
       
  2413 
       
  2414 			if ($mode == 'style' && (!$template_id || !$theme_id || !$imageset_id))
       
  2415 			{
       
  2416 				$error[] = $user->lang['STYLE_ERR_NO_IDS'];
       
  2417 			}
       
  2418 
       
  2419 			if ($mode == 'style' && $style_row['style_active'] && !$style_active && $config['default_style'] == $style_id)
       
  2420 			{
       
  2421 				$error[] = $user->lang['DEACTIVATE_DEFAULT'];
       
  2422 			}
       
  2423 
       
  2424 			if (!$name || $conflict)
       
  2425 			{
       
  2426 				$error[] = $user->lang[$l_type . '_ERR_STYLE_NAME'];
       
  2427 			}
       
  2428 
       
  2429 			if ($mode === 'theme' || $mode === 'template')
       
  2430 			{
       
  2431 				// a rather elaborate check we have to do here once to avoid trouble later
       
  2432 				$check = "{$phpbb_root_path}styles/" . $style_row["{$mode}_path"] . (($mode === 'theme') ? '/theme/stylesheet.css' : '/template');
       
  2433 				if (($style_row["{$mode}_storedb"] != $store_db) && !$store_db && ($safe_mode || !phpbb_is_writable($check)))
       
  2434 				{
       
  2435 					$error[] = $user->lang['EDIT_' . strtoupper($mode) . '_STORED_DB'];
       
  2436 					$store_db = 1;
       
  2437 				}
       
  2438 
       
  2439 				// themes which have to be parsed have to go into db
       
  2440 				if ($mode == 'theme')
       
  2441 				{
       
  2442 					$cfg = parse_cfg_file("{$phpbb_root_path}styles/" . $style_row["{$mode}_path"] . "/theme/theme.cfg");
       
  2443 
       
  2444 					if (isset($cfg['parse_css_file']) && $cfg['parse_css_file'] && !$store_db)
       
  2445 					{
       
  2446 						$error[] = $user->lang['EDIT_THEME_STORE_PARSED'];
       
  2447 						$store_db = 1;
       
  2448 					}
       
  2449 				}
       
  2450 			}
       
  2451 
       
  2452 			if (!sizeof($error))
       
  2453 			{
       
  2454 				// Check length settings
       
  2455 				if (utf8_strlen($name) > 30)
       
  2456 				{
       
  2457 					$error[] = $user->lang[$l_type . '_ERR_NAME_LONG'];
       
  2458 				}
       
  2459 
       
  2460 				if (utf8_strlen($copyright) > 60)
       
  2461 				{
       
  2462 					$error[] = $user->lang[$l_type . '_ERR_COPY_LONG'];
       
  2463 				}
       
  2464 			}
       
  2465 		}
       
  2466 
       
  2467 		if ($update && sizeof($error))
       
  2468 		{
       
  2469 			$style_row = array_merge($style_row, array(
       
  2470 				'template_id'			=> $template_id,
       
  2471 				'theme_id'				=> $theme_id,
       
  2472 				'imageset_id'			=> $imageset_id,
       
  2473 				'style_active'			=> $style_active,
       
  2474 				$mode . '_storedb'		=> $store_db,
       
  2475 				$mode . '_name'			=> $name,
       
  2476 				$mode . '_copyright'	=> $copyright)
       
  2477 			);
       
  2478 		}
       
  2479 
       
  2480 		// User has submitted form and no errors have occurred
       
  2481 		if ($update && !sizeof($error))
       
  2482 		{
       
  2483 			$sql_ary = array(
       
  2484 				$mode . '_name'			=> $name,
       
  2485 				$mode . '_copyright'	=> $copyright
       
  2486 			);
       
  2487 
       
  2488 			switch ($mode)
       
  2489 			{
       
  2490 				case 'style':
       
  2491 
       
  2492 					$sql_ary += array(
       
  2493 						'template_id'		=> (int) $template_id,
       
  2494 						'theme_id'			=> (int) $theme_id,
       
  2495 						'imageset_id'		=> (int) $imageset_id,
       
  2496 						'style_active'		=> (int) $style_active,
       
  2497 					);
       
  2498 				break;
       
  2499 
       
  2500 				case 'imageset':
       
  2501 				break;
       
  2502 
       
  2503 				case 'theme':
       
  2504 
       
  2505 					if ($style_row['theme_storedb'] != $store_db)
       
  2506 					{
       
  2507 						$theme_data = '';
       
  2508 
       
  2509 						if (!$style_row['theme_storedb'])
       
  2510 						{
       
  2511 							$theme_data = $this->db_theme_data($style_row);
       
  2512 						}
       
  2513 						else if (!$store_db && !$safe_mode && phpbb_is_writable("{$phpbb_root_path}styles/{$style_row['theme_path']}/theme/stylesheet.css"))
       
  2514 						{
       
  2515 							$store_db = 1;
       
  2516 							$theme_data = $style_row['theme_data'];
       
  2517 
       
  2518 							if ($fp = @fopen("{$phpbb_root_path}styles/{$style_row['theme_path']}/theme/stylesheet.css", 'wb'))
       
  2519 							{
       
  2520 								$store_db = (@fwrite($fp, str_replace("styles/{$style_row['theme_path']}/theme/", './', $theme_data))) ? 0 : 1;
       
  2521 							}
       
  2522 							fclose($fp);
       
  2523 						}
       
  2524 
       
  2525 						$sql_ary += array(
       
  2526 							'theme_mtime'	=> ($store_db) ? filemtime("{$phpbb_root_path}styles/{$style_row['theme_path']}/theme/stylesheet.css") : 0,
       
  2527 							'theme_storedb'	=> $store_db,
       
  2528 							'theme_data'	=> ($store_db) ? $theme_data : '',
       
  2529 						);
       
  2530 					}
       
  2531 				break;
       
  2532 
       
  2533 				case 'template':
       
  2534 
       
  2535 					if ($style_row['template_storedb'] != $store_db)
       
  2536 					{
       
  2537 						if ($super = $this->get_super($mode, $style_row['template_id']))
       
  2538 						{
       
  2539 							$error[] = (sprintf($user->lang["{$l_type}_INHERITS"], $super['template_name']));
       
  2540 							$sql_ary = array();
       
  2541 						}
       
  2542 						else
       
  2543 						{
       
  2544 							if (!$store_db && !$safe_mode && phpbb_is_writable("{$phpbb_root_path}styles/{$style_row['template_path']}/template"))
       
  2545 							{
       
  2546 								$err = $this->store_in_fs('template', $style_row['template_id']);
       
  2547 								if ($err)
       
  2548 								{
       
  2549 									$error += $err;
       
  2550 								}
       
  2551 							}
       
  2552 							else if ($store_db)
       
  2553 							{
       
  2554 								$this->store_in_db('template', $style_row['template_id']);
       
  2555 							}
       
  2556 							else
       
  2557 							{
       
  2558 								// We no longer store within the db, but are also not able to update the file structure
       
  2559 								// Since the admin want to switch this, we adhere to his decision. But we also need to remove the cache
       
  2560 								$sql = 'DELETE FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
       
  2561 									WHERE template_id = $style_id";
       
  2562 								$db->sql_query($sql);
       
  2563 							}
       
  2564 
       
  2565 							$sql_ary += array(
       
  2566 								'template_storedb'	=> $store_db,
       
  2567 							);
       
  2568 						}
       
  2569 					}
       
  2570 				break;
       
  2571 			}
       
  2572 
       
  2573 			if (sizeof($sql_ary))
       
  2574 			{
       
  2575 				$sql = "UPDATE $sql_from
       
  2576 					SET " . $db->sql_build_array('UPDATE', $sql_ary) . "
       
  2577 					WHERE {$mode}_id = $style_id";
       
  2578 				$db->sql_query($sql);
       
  2579 
       
  2580 				// Making this the default style?
       
  2581 				if ($mode == 'style' && $style_default)
       
  2582 				{
       
  2583 					set_config('default_style', $style_id);
       
  2584 				}
       
  2585 			}
       
  2586 
       
  2587 			$cache->destroy('sql', STYLES_TABLE);
       
  2588 
       
  2589 			add_log('admin', 'LOG_' . $l_type . '_EDIT_DETAILS', $name);
       
  2590 			if (sizeof($error))
       
  2591 			{
       
  2592 				trigger_error(implode('<br />', $error) . adm_back_link($this->u_action), E_USER_WARNING);
       
  2593 			}
       
  2594 			else
       
  2595 			{
       
  2596 				trigger_error($user->lang[$l_type . '_DETAILS_UPDATED'] . adm_back_link($this->u_action));
       
  2597 			}
       
  2598 		}
       
  2599 
       
  2600 		if ($mode == 'style')
       
  2601 		{
       
  2602 			foreach ($element_ary as $element => $table)
       
  2603 			{
       
  2604 				$sql = "SELECT {$element}_id, {$element}_name
       
  2605 					FROM $table
       
  2606 					ORDER BY {$element}_id ASC";
       
  2607 				$result = $db->sql_query($sql);
       
  2608 
       
  2609 				${$element . '_options'} = '';
       
  2610 				while ($row = $db->sql_fetchrow($result))
       
  2611 				{
       
  2612 					$selected = ($row[$element . '_id'] == $style_row[$element . '_id']) ? ' selected="selected"' : '';
       
  2613 					${$element . '_options'} .= '<option value="' . $row[$element . '_id'] . '"' . $selected . '>' . $row[$element . '_name'] . '</option>';
       
  2614 				}
       
  2615 				$db->sql_freeresult($result);
       
  2616 			}
       
  2617 		}
       
  2618 
       
  2619 		if ($mode == 'template')
       
  2620 		{
       
  2621 			$super = array();
       
  2622 			if (isset($style_row[$mode . '_inherits_id']) && $style_row['template_inherits_id'])
       
  2623 			{
       
  2624 				$super = $this->get_super($mode, $style_row['template_id']);
       
  2625 			}
       
  2626 		}
       
  2627 
       
  2628 		$this->page_title = 'EDIT_DETAILS_' . $l_type;
       
  2629 
       
  2630 		$template->assign_vars(array(
       
  2631 			'S_DETAILS'				=> true,
       
  2632 			'S_ERROR_MSG'			=> (sizeof($error)) ? true : false,
       
  2633 			'S_STYLE'				=> ($mode == 'style') ? true : false,
       
  2634 			'S_TEMPLATE'			=> ($mode == 'template') ? true : false,
       
  2635 			'S_THEME'				=> ($mode == 'theme') ? true : false,
       
  2636 			'S_IMAGESET'			=> ($mode == 'imageset') ? true : false,
       
  2637 			'S_STORE_DB'			=> (isset($style_row[$mode . '_storedb'])) ? $style_row[$mode . '_storedb'] : 0,
       
  2638 			'S_STORE_DB_DISABLED'	=> (isset($style_row[$mode . '_inherits_id'])) ? $style_row[$mode . '_inherits_id'] : 0,
       
  2639 			'S_STYLE_ACTIVE'		=> (isset($style_row['style_active'])) ? $style_row['style_active'] : 0,
       
  2640 			'S_STYLE_DEFAULT'		=> (isset($style_row['style_default'])) ? $style_row['style_default'] : 0,
       
  2641 			'S_SUPERTEMPLATE'		=> (isset($style_row[$mode . '_inherits_id']) && $style_row[$mode . '_inherits_id']) ? $super['template_name'] : 0,
       
  2642 
       
  2643 			'S_TEMPLATE_OPTIONS'	=> ($mode == 'style') ? $template_options : '',
       
  2644 			'S_THEME_OPTIONS'		=> ($mode == 'style') ? $theme_options : '',
       
  2645 			'S_IMAGESET_OPTIONS'	=> ($mode == 'style') ? $imageset_options : '',
       
  2646 
       
  2647 			'U_ACTION'		=> $this->u_action . '&amp;action=details&amp;id=' . $style_id,
       
  2648 			'U_BACK'		=> $this->u_action,
       
  2649 
       
  2650 			'L_TITLE'				=> $user->lang[$this->page_title],
       
  2651 			'L_EXPLAIN'				=> $user->lang[$this->page_title . '_EXPLAIN'],
       
  2652 			'L_NAME'				=> $user->lang[$l_type . '_NAME'],
       
  2653 			'L_LOCATION'			=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION'] : '',
       
  2654 			'L_LOCATION_EXPLAIN'	=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION_EXPLAIN'] : '',
       
  2655 
       
  2656 			'ERROR_MSG'		=> (sizeof($error)) ? implode('<br />', $error) : '',
       
  2657 			'NAME'			=> $style_row[$mode . '_name'],
       
  2658 			'COPYRIGHT'		=> $style_row[$mode . '_copyright'],
       
  2659 			)
       
  2660 		);
       
  2661 	}
       
  2662 
       
  2663 	/**
       
  2664 	* Load css file contents
       
  2665 	*/
       
  2666 	function load_css_file($path, $filename)
       
  2667 	{
       
  2668 		global $phpbb_root_path;
       
  2669 
       
  2670 		$file = "{$phpbb_root_path}styles/$path/theme/$filename";
       
  2671 
       
  2672 		if (file_exists($file) && ($content = file_get_contents($file)))
       
  2673 		{
       
  2674 			$content = trim($content);
       
  2675 		}
       
  2676 		else
       
  2677 		{
       
  2678 			$content = '';
       
  2679 		}
       
  2680 		if (defined('DEBUG'))
       
  2681 		{
       
  2682 			$content = "/* BEGIN @include $filename */ \n $content \n /* END @include $filename */ \n";
       
  2683 		}
       
  2684 
       
  2685 		return $content;
       
  2686 	}
       
  2687 
       
  2688 	/**
       
  2689 	* Returns a string containing the value that should be used for the theme_data column in the theme database table.
       
  2690 	* Includes contents of files loaded via @import
       
  2691 	*
       
  2692 	* @param array $theme_row is an associative array containing the theme's current database entry
       
  2693 	* @param mixed $stylesheet can either be the new content for the stylesheet or false to load from the standard file
       
  2694 	* @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']}"
       
  2695 	*
       
  2696 	* @return string Stylesheet data for theme_data column in the theme table
       
  2697 	*/
       
  2698 	function db_theme_data($theme_row, $stylesheet = false, $root_path = '')
       
  2699 	{
       
  2700 		global $phpbb_root_path;
       
  2701 
       
  2702 		if (!$root_path)
       
  2703 		{
       
  2704 			$root_path = $phpbb_root_path . 'styles/' . $theme_row['theme_path'];
       
  2705 		}
       
  2706 
       
  2707 		if (!$stylesheet)
       
  2708 		{
       
  2709 			$stylesheet = '';
       
  2710 			if (file_exists($root_path . '/theme/stylesheet.css'))
       
  2711 			{
       
  2712 				$stylesheet = file_get_contents($root_path . '/theme/stylesheet.css');
       
  2713 			}
       
  2714 		}
       
  2715 
       
  2716 		// Match CSS imports
       
  2717 		$matches = array();
       
  2718 		preg_match_all('/@import url\((["\'])(.*)\1\);/i', $stylesheet, $matches);
       
  2719 
       
  2720 		// remove commented stylesheets (very simple parser, allows only whitespace
       
  2721 		// around an @import statement)
       
  2722 		preg_match_all('#/\*\s*@import url\((["\'])(.*)\1\);\s\*/#i', $stylesheet, $commented);
       
  2723 		$matches[2] = array_diff($matches[2], $commented[2]);
       
  2724 
       
  2725 		if (sizeof($matches))
       
  2726 		{
       
  2727 			foreach ($matches[0] as $idx => $match)
       
  2728 			{
       
  2729 				if (isset($matches[2][$idx]))
       
  2730 				{
       
  2731 					$stylesheet = str_replace($match, acp_styles::load_css_file($theme_row['theme_path'], $matches[2][$idx]), $stylesheet);
       
  2732 				}
       
  2733 			}
       
  2734 		}
       
  2735 
       
  2736 		// adjust paths
       
  2737 		return str_replace('./', 'styles/' . $theme_row['theme_path'] . '/theme/', $stylesheet);
       
  2738 	}
       
  2739 
       
  2740 	/**
       
  2741 	* Store template files into db
       
  2742 	*/
       
  2743 	function store_templates($mode, $style_id, $template_path, $filelist)
       
  2744 	{
       
  2745 		global $phpbb_root_path, $phpEx, $db;
       
  2746 
       
  2747 		$template_path = $template_path . '/template/';
       
  2748 		$includes = array();
       
  2749 		foreach ($filelist as $pathfile => $file_ary)
       
  2750 		{
       
  2751 			foreach ($file_ary as $file)
       
  2752 			{
       
  2753 				if (!($fp = @fopen("{$phpbb_root_path}styles/$template_path$pathfile$file", 'r')))
       
  2754 				{
       
  2755 					trigger_error("Could not open {$phpbb_root_path}styles/$template_path$pathfile$file", E_USER_ERROR);
       
  2756 				}
       
  2757 
       
  2758 				$filesize = filesize("{$phpbb_root_path}styles/$template_path$pathfile$file");
       
  2759 
       
  2760 				if ($filesize)
       
  2761 				{
       
  2762 					$template_data = fread($fp, $filesize);
       
  2763 				}
       
  2764 
       
  2765 				fclose($fp);
       
  2766 
       
  2767 				if (!$filesize)
       
  2768 				{
       
  2769 					// File is empty
       
  2770 					continue;
       
  2771 				}
       
  2772 
       
  2773 				if (preg_match_all('#<!-- INCLUDE (.*?\.html) -->#is', $template_data, $matches))
       
  2774 				{
       
  2775 					foreach ($matches[1] as $match)
       
  2776 					{
       
  2777 						$includes[trim($match)][] = $file;
       
  2778 					}
       
  2779 				}
       
  2780 			}
       
  2781 		}
       
  2782 
       
  2783 		foreach ($filelist as $pathfile => $file_ary)
       
  2784 		{
       
  2785 			foreach ($file_ary as $file)
       
  2786 			{
       
  2787 				// Skip index.
       
  2788 				if (strpos($file, 'index.') === 0)
       
  2789 				{
       
  2790 					continue;
       
  2791 				}
       
  2792 
       
  2793 				// We could do this using extended inserts ... but that could be one
       
  2794 				// heck of a lot of data ...
       
  2795 				$sql_ary = array(
       
  2796 					'template_id'			=> (int) $style_id,
       
  2797 					'template_filename'		=> "$pathfile$file",
       
  2798 					'template_included'		=> (isset($includes[$file])) ? implode(':', $includes[$file]) . ':' : '',
       
  2799 					'template_mtime'		=> (int) filemtime("{$phpbb_root_path}styles/$template_path$pathfile$file"),
       
  2800 					'template_data'			=> (string) file_get_contents("{$phpbb_root_path}styles/$template_path$pathfile$file"),
       
  2801 				);
       
  2802 
       
  2803 				if ($mode == 'insert')
       
  2804 				{
       
  2805 					$sql = 'INSERT INTO ' . STYLES_TEMPLATE_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
       
  2806 				}
       
  2807 				else
       
  2808 				{
       
  2809 					$sql = 'UPDATE ' . STYLES_TEMPLATE_DATA_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
       
  2810 						WHERE template_id = $style_id
       
  2811 							AND template_filename = '" . $db->sql_escape("$pathfile$file") . "'";
       
  2812 				}
       
  2813 				$db->sql_query($sql);
       
  2814 			}
       
  2815 		}
       
  2816 	}
       
  2817 
       
  2818 	/**
       
  2819 	* Returns an array containing all template filenames for one template that are currently cached.
       
  2820 	*
       
  2821 	* @param string $template_path contains the name of the template's folder in /styles/
       
  2822 	*
       
  2823 	* @return array of filenames that exist in /styles/$template_path/template/ (without extension!)
       
  2824 	*/
       
  2825 	function template_cache_filelist($template_path)
       
  2826 	{
       
  2827 		global $phpbb_root_path, $phpEx, $user;
       
  2828 
       
  2829 		$cache_prefix = 'tpl_' . str_replace('_', '-', $template_path);
       
  2830 
       
  2831 		if (!($dp = @opendir("{$phpbb_root_path}cache")))
       
  2832 		{
       
  2833 			trigger_error($user->lang['TEMPLATE_ERR_CACHE_READ'] . adm_back_link($this->u_action), E_USER_WARNING);
       
  2834 		}
       
  2835 
       
  2836 		$file_ary = array();
       
  2837 		while ($file = readdir($dp))
       
  2838 		{
       
  2839 			if ($file[0] == '.')
       
  2840 			{
       
  2841 				continue;
       
  2842 			}
       
  2843 
       
  2844 			if (is_file($phpbb_root_path . 'cache/' . $file) && (strpos($file, $cache_prefix) === 0))
       
  2845 			{
       
  2846 				$file_ary[] = str_replace('.', '/', preg_replace('#^' . preg_quote($cache_prefix, '#') . '_(.*?)\.html\.' . $phpEx . '$#i', '\1', $file));
       
  2847 			}
       
  2848 		}
       
  2849 		closedir($dp);
       
  2850 
       
  2851 		return $file_ary;
       
  2852 	}
       
  2853 
       
  2854 	/**
       
  2855 	* Destroys cached versions of template files
       
  2856 	*
       
  2857 	* @param array $template_row contains the template's row in the STYLES_TEMPLATE_TABLE database table
       
  2858 	* @param mixed $file_ary is optional and may contain an array of template file names which should be refreshed in the cache.
       
  2859 	*	The file names should be the original template file names and not the cache file names.
       
  2860 	*/
       
  2861 	function clear_template_cache($template_row, $file_ary = false)
       
  2862 	{
       
  2863 		global $phpbb_root_path, $phpEx, $user;
       
  2864 
       
  2865 		$cache_prefix = 'tpl_' . str_replace('_', '-', $template_row['template_path']);
       
  2866 
       
  2867 		if (!$file_ary || !is_array($file_ary))
       
  2868 		{
       
  2869 			$file_ary = $this->template_cache_filelist($template_row['template_path']);
       
  2870 			$log_file_list = $user->lang['ALL_FILES'];
       
  2871 		}
       
  2872 		else
       
  2873 		{
       
  2874 			$log_file_list = implode(', ', $file_ary);
       
  2875 		}
       
  2876 
       
  2877 		foreach ($file_ary as $file)
       
  2878 		{
       
  2879 			$file = str_replace('/', '.', $file);
       
  2880 
       
  2881 			$file = "{$phpbb_root_path}cache/{$cache_prefix}_$file.html.$phpEx";
       
  2882 			if (file_exists($file) && is_file($file))
       
  2883 			{
       
  2884 				@unlink($file);
       
  2885 			}
       
  2886 		}
       
  2887 		unset($file_ary);
       
  2888 
       
  2889 		add_log('admin', 'LOG_TEMPLATE_CACHE_CLEARED', $template_row['template_name'], $log_file_list);
       
  2890 	}
       
  2891 
       
  2892 	/**
       
  2893 	* Install Style/Template/Theme/Imageset
       
  2894 	*/
       
  2895 	function install($mode)
       
  2896 	{
       
  2897 		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template;
       
  2898 
       
  2899 		$l_type = strtoupper($mode);
       
  2900 
       
  2901 		$error = $installcfg = $style_row = array();
       
  2902 		$root_path = $cfg_file = '';
       
  2903 		$element_ary = array('template' => STYLES_TEMPLATE_TABLE, 'theme' => STYLES_THEME_TABLE, 'imageset' => STYLES_IMAGESET_TABLE);
       
  2904 
       
  2905 		$install_path = request_var('path', '');
       
  2906 		$update = (isset($_POST['update'])) ? true : false;
       
  2907 
       
  2908 		// Installing, obtain cfg file contents
       
  2909 		if ($install_path)
       
  2910 		{
       
  2911 			$root_path = $phpbb_root_path . 'styles/' . $install_path . '/';
       
  2912 			$cfg_file = ($mode == 'style') ? "$root_path$mode.cfg" : "$root_path$mode/$mode.cfg";
       
  2913 
       
  2914 			if (!file_exists($cfg_file))
       
  2915 			{
       
  2916 				$error[] = $user->lang[$l_type . '_ERR_NOT_' . $l_type];
       
  2917 			}
       
  2918 			else
       
  2919 			{
       
  2920 				$installcfg = parse_cfg_file($cfg_file);
       
  2921 			}
       
  2922 		}
       
  2923 
       
  2924 		// Installing
       
  2925 		if (sizeof($installcfg))
       
  2926 		{
       
  2927 			$name		= $installcfg['name'];
       
  2928 			$copyright	= $installcfg['copyright'];
       
  2929 			$version	= $installcfg['version'];
       
  2930 
       
  2931 			$style_row = array(
       
  2932 				$mode . '_id'			=> 0,
       
  2933 				$mode . '_name'			=> '',
       
  2934 				$mode . '_copyright'	=> ''
       
  2935 			);
       
  2936 
       
  2937 			switch ($mode)
       
  2938 			{
       
  2939 				case 'style':
       
  2940 
       
  2941 					$style_row = array(
       
  2942 						'style_id'			=> 0,
       
  2943 						'style_name'		=> $installcfg['name'],
       
  2944 						'style_copyright'	=> $installcfg['copyright']
       
  2945 					);
       
  2946 
       
  2947 					$reqd_template = (isset($installcfg['required_template'])) ? $installcfg['required_template'] : false;
       
  2948 					$reqd_theme = (isset($installcfg['required_theme'])) ? $installcfg['required_theme'] : false;
       
  2949 					$reqd_imageset = (isset($installcfg['required_imageset'])) ? $installcfg['required_imageset'] : false;
       
  2950 
       
  2951 					// Check to see if each element is already installed, if it is grab the id
       
  2952 					foreach ($element_ary as $element => $table)
       
  2953 					{
       
  2954 						$style_row = array_merge($style_row, array(
       
  2955 							$element . '_id'			=> 0,
       
  2956 							$element . '_name'			=> '',
       
  2957 							$element . '_copyright'		=> '')
       
  2958 						);
       
  2959 
       
  2960 			 			$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']);
       
  2961 
       
  2962 						if (!$style_row[$element . '_name'])
       
  2963 						{
       
  2964 							$style_row[$element . '_name'] = $reqd_template;
       
  2965 						}
       
  2966 
       
  2967 						// Merge other information to installcfg... if present
       
  2968 						$cfg_file = $phpbb_root_path . 'styles/' . $install_path . '/' . $element . '/' . $element . '.cfg';
       
  2969 
       
  2970 						if (file_exists($cfg_file))
       
  2971 						{
       
  2972 							$cfg_contents = parse_cfg_file($cfg_file);
       
  2973 
       
  2974 							// Merge only specific things. We may need them later.
       
  2975 							foreach (array('inherit_from', 'parse_css_file') as $key)
       
  2976 							{
       
  2977 								if (!empty($cfg_contents[$key]) && !isset($installcfg[$key]))
       
  2978 								{
       
  2979 									$installcfg[$key] = $cfg_contents[$key];
       
  2980 								}
       
  2981 							}
       
  2982 						}
       
  2983 					}
       
  2984 
       
  2985 				break;
       
  2986 
       
  2987 				case 'template':
       
  2988 					$this->test_installed('template', $error, $root_path, false, $style_row['template_id'], $style_row['template_name'], $style_row['template_copyright']);
       
  2989 				break;
       
  2990 
       
  2991 				case 'theme':
       
  2992 					$this->test_installed('theme', $error, $root_path, false, $style_row['theme_id'], $style_row['theme_name'], $style_row['theme_copyright']);
       
  2993 				break;
       
  2994 
       
  2995 				case 'imageset':
       
  2996 					$this->test_installed('imageset', $error, $root_path, false, $style_row['imageset_id'], $style_row['imageset_name'], $style_row['imageset_copyright']);
       
  2997 				break;
       
  2998 			}
       
  2999 		}
       
  3000 		else
       
  3001 		{
       
  3002 			trigger_error($user->lang['NO_' . $l_type] . adm_back_link($this->u_action), E_USER_WARNING);
       
  3003 		}
       
  3004 
       
  3005 		$style_row['store_db'] = request_var('store_db', 0);
       
  3006 		$style_row['style_active'] = request_var('style_active', 1);
       
  3007 		$style_row['style_default'] = request_var('style_default', 0);
       
  3008 
       
  3009 		// User has submitted form and no errors have occurred
       
  3010 		if ($update && !sizeof($error))
       
  3011 		{
       
  3012 			if ($mode == 'style')
       
  3013 			{
       
  3014 				foreach ($element_ary as $element => $table)
       
  3015 				{
       
  3016 					${$element . '_root_path'} = (${'reqd_' . $element}) ? $phpbb_root_path . 'styles/' . ${'reqd_' . $element} . '/' : false;
       
  3017 					${$element . '_path'} = (${'reqd_' . $element}) ? ${'reqd_' . $element} : false;
       
  3018 				}
       
  3019 				$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);
       
  3020 			}
       
  3021 			else
       
  3022 			{
       
  3023 				$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']);
       
  3024 			}
       
  3025 
       
  3026 			if (!sizeof($error))
       
  3027 			{
       
  3028 				$cache->destroy('sql', STYLES_TABLE);
       
  3029 
       
  3030 				$message = ($style_row['store_db']) ? '_ADDED_DB' : '_ADDED';
       
  3031 				trigger_error($user->lang[$l_type . $message] . adm_back_link($this->u_action));
       
  3032 			}
       
  3033 		}
       
  3034 
       
  3035 		$this->page_title = 'INSTALL_' . $l_type;
       
  3036 
       
  3037 		$template->assign_vars(array(
       
  3038 			'S_DETAILS'			=> true,
       
  3039 			'S_INSTALL'			=> true,
       
  3040 			'S_ERROR_MSG'		=> (sizeof($error)) ? true : false,
       
  3041 			'S_LOCATION'		=> (isset($installcfg['inherit_from']) && $installcfg['inherit_from']) ? false : true,
       
  3042 			'S_STYLE'			=> ($mode == 'style') ? true : false,
       
  3043 			'S_TEMPLATE'		=> ($mode == 'template') ? true : false,
       
  3044 			'S_SUPERTEMPLATE'	=> (isset($installcfg['inherit_from'])) ? $installcfg['inherit_from'] : '',
       
  3045 			'S_THEME'			=> ($mode == 'theme') ? true : false,
       
  3046 
       
  3047 			'S_STORE_DB'			=> (isset($style_row[$mode . '_storedb'])) ? $style_row[$mode . '_storedb'] : 0,
       
  3048 			'S_STYLE_ACTIVE'		=> (isset($style_row['style_active'])) ? $style_row['style_active'] : 0,
       
  3049 			'S_STYLE_DEFAULT'		=> (isset($style_row['style_default'])) ? $style_row['style_default'] : 0,
       
  3050 
       
  3051 			'U_ACTION'			=> $this->u_action . "&amp;action=install&amp;path=" . urlencode($install_path),
       
  3052 			'U_BACK'			=> $this->u_action,
       
  3053 
       
  3054 			'L_TITLE'				=> $user->lang[$this->page_title],
       
  3055 			'L_EXPLAIN'				=> $user->lang[$this->page_title . '_EXPLAIN'],
       
  3056 			'L_NAME'				=> $user->lang[$l_type . '_NAME'],
       
  3057 			'L_LOCATION'			=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION'] : '',
       
  3058 			'L_LOCATION_EXPLAIN'	=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION_EXPLAIN'] : '',
       
  3059 
       
  3060 			'ERROR_MSG'			=> (sizeof($error)) ? implode('<br />', $error) : '',
       
  3061 			'NAME'				=> $style_row[$mode . '_name'],
       
  3062 			'COPYRIGHT'			=> $style_row[$mode . '_copyright'],
       
  3063 			'TEMPLATE_NAME'		=> ($mode == 'style') ? $style_row['template_name'] : '',
       
  3064 			'THEME_NAME'		=> ($mode == 'style') ? $style_row['theme_name'] : '',
       
  3065 			'IMAGESET_NAME'		=> ($mode == 'style') ? $style_row['imageset_name'] : '')
       
  3066 		);
       
  3067 	}
       
  3068 
       
  3069 	/**
       
  3070 	* Add new style
       
  3071 	*/
       
  3072 	function add($mode)
       
  3073 	{
       
  3074 		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template;
       
  3075 
       
  3076 		$l_type = strtoupper($mode);
       
  3077 		$element_ary = array('template' => STYLES_TEMPLATE_TABLE, 'theme' => STYLES_THEME_TABLE, 'imageset' => STYLES_IMAGESET_TABLE);
       
  3078 		$error = array();
       
  3079 
       
  3080 		$style_row = array(
       
  3081 			$mode . '_name'			=> utf8_normalize_nfc(request_var('name', '', true)),
       
  3082 			$mode . '_copyright'	=> utf8_normalize_nfc(request_var('copyright', '', true)),
       
  3083 			'template_id'			=> 0,
       
  3084 			'theme_id'				=> 0,
       
  3085 			'imageset_id'			=> 0,
       
  3086 			'store_db'				=> request_var('store_db', 0),
       
  3087 			'style_active'			=> request_var('style_active', 1),
       
  3088 			'style_default'			=> request_var('style_default', 0),
       
  3089 		);
       
  3090 
       
  3091 		$basis = request_var('basis', 0);
       
  3092 		$update = (isset($_POST['update'])) ? true : false;
       
  3093 
       
  3094 		if ($basis)
       
  3095 		{
       
  3096 			switch ($mode)
       
  3097 			{
       
  3098 				case 'style':
       
  3099 					$sql_select = 'template_id, theme_id, imageset_id';
       
  3100 					$sql_from = STYLES_TABLE;
       
  3101 				break;
       
  3102 
       
  3103 				case 'template':
       
  3104 					$sql_select = 'template_id';
       
  3105 					$sql_from = STYLES_TEMPLATE_TABLE;
       
  3106 				break;
       
  3107 
       
  3108 				case 'theme':
       
  3109 					$sql_select = 'theme_id';
       
  3110 					$sql_from = STYLES_THEME_TABLE;
       
  3111 				break;
       
  3112 
       
  3113 				case 'imageset':
       
  3114 					$sql_select = 'imageset_id';
       
  3115 					$sql_from = STYLES_IMAGESET_TABLE;
       
  3116 				break;
       
  3117 			}
       
  3118 
       
  3119 			$sql = "SELECT $sql_select
       
  3120 				FROM $sql_from
       
  3121 				WHERE {$mode}_id = $basis";
       
  3122 			$result = $db->sql_query($sql);
       
  3123 			$row = $db->sql_fetchrow($result);
       
  3124 			$db->sql_freeresult($result);
       
  3125 
       
  3126 			if (!$row)
       
  3127 			{
       
  3128 				$error[] = $user->lang['NO_' . $l_type];
       
  3129 			}
       
  3130 
       
  3131 			if (!sizeof($error))
       
  3132 			{
       
  3133 				$style_row['template_id']	= (isset($row['template_id'])) ? $row['template_id'] : $style_row['template_id'];
       
  3134 				$style_row['theme_id']		= (isset($row['theme_id'])) ? $row['theme_id'] : $style_row['theme_id'];
       
  3135 				$style_row['imageset_id']	= (isset($row['imageset_id'])) ? $row['imageset_id'] : $style_row['imageset_id'];
       
  3136 			}
       
  3137 		}
       
  3138 
       
  3139 		if ($update)
       
  3140 		{
       
  3141 			$style_row['template_id'] = request_var('template_id', $style_row['template_id']);
       
  3142 			$style_row['theme_id'] = request_var('theme_id', $style_row['theme_id']);
       
  3143 			$style_row['imageset_id'] = request_var('imageset_id', $style_row['imageset_id']);
       
  3144 
       
  3145 			if ($mode == 'style' && (!$style_row['template_id'] || !$style_row['theme_id'] || !$style_row['imageset_id']))
       
  3146 			{
       
  3147 				$error[] = $user->lang['STYLE_ERR_NO_IDS'];
       
  3148 			}
       
  3149 		}
       
  3150 
       
  3151 		// User has submitted form and no errors have occurred
       
  3152 		if ($update && !sizeof($error))
       
  3153 		{
       
  3154 			if ($mode == 'style')
       
  3155 			{
       
  3156 				$style_row['style_id'] = 0;
       
  3157 
       
  3158 				$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);
       
  3159 			}
       
  3160 
       
  3161 			if (!sizeof($error))
       
  3162 			{
       
  3163 				$cache->destroy('sql', STYLES_TABLE);
       
  3164 
       
  3165 				$message = ($style_row['store_db']) ? '_ADDED_DB' : '_ADDED';
       
  3166 				trigger_error($user->lang[$l_type . $message] . adm_back_link($this->u_action));
       
  3167 			}
       
  3168 		}
       
  3169 
       
  3170 		if ($mode == 'style')
       
  3171 		{
       
  3172 			foreach ($element_ary as $element => $table)
       
  3173 			{
       
  3174 				$sql = "SELECT {$element}_id, {$element}_name
       
  3175 					FROM $table
       
  3176 					ORDER BY {$element}_id ASC";
       
  3177 				$result = $db->sql_query($sql);
       
  3178 
       
  3179 				${$element . '_options'} = '';
       
  3180 				while ($row = $db->sql_fetchrow($result))
       
  3181 				{
       
  3182 					$selected = ($row[$element . '_id'] == $style_row[$element . '_id']) ? ' selected="selected"' : '';
       
  3183 					${$element . '_options'} .= '<option value="' . $row[$element . '_id'] . '"' . $selected . '>' . $row[$element . '_name'] . '</option>';
       
  3184 				}
       
  3185 				$db->sql_freeresult($result);
       
  3186 			}
       
  3187 		}
       
  3188 
       
  3189 		$this->page_title = 'ADD_' . $l_type;
       
  3190 
       
  3191 		$template->assign_vars(array(
       
  3192 			'S_DETAILS'			=> true,
       
  3193 			'S_ADD'				=> true,
       
  3194 			'S_ERROR_MSG'		=> (sizeof($error)) ? true : false,
       
  3195 			'S_STYLE'			=> ($mode == 'style') ? true : false,
       
  3196 			'S_TEMPLATE'		=> ($mode == 'template') ? true : false,
       
  3197 			'S_THEME'			=> ($mode == 'theme') ? true : false,
       
  3198 			'S_BASIS'			=> ($basis) ? true : false,
       
  3199 
       
  3200 			'S_STORE_DB'			=> (isset($style_row['storedb'])) ? $style_row['storedb'] : 0,
       
  3201 			'S_STYLE_ACTIVE'		=> (isset($style_row['style_active'])) ? $style_row['style_active'] : 0,
       
  3202 			'S_STYLE_DEFAULT'		=> (isset($style_row['style_default'])) ? $style_row['style_default'] : 0,
       
  3203 			'S_TEMPLATE_OPTIONS'	=> ($mode == 'style') ? $template_options : '',
       
  3204 			'S_THEME_OPTIONS'		=> ($mode == 'style') ? $theme_options : '',
       
  3205 			'S_IMAGESET_OPTIONS'	=> ($mode == 'style') ? $imageset_options : '',
       
  3206 
       
  3207 			'U_ACTION'			=> $this->u_action . '&amp;action=add&amp;basis=' . $basis,
       
  3208 			'U_BACK'			=> $this->u_action,
       
  3209 
       
  3210 			'L_TITLE'				=> $user->lang[$this->page_title],
       
  3211 			'L_EXPLAIN'				=> $user->lang[$this->page_title . '_EXPLAIN'],
       
  3212 			'L_NAME'				=> $user->lang[$l_type . '_NAME'],
       
  3213 			'L_LOCATION'			=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION'] : '',
       
  3214 			'L_LOCATION_EXPLAIN'	=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION_EXPLAIN'] : '',
       
  3215 
       
  3216 			'ERROR_MSG'			=> (sizeof($error)) ? implode('<br />', $error) : '',
       
  3217 			'NAME'				=> $style_row[$mode . '_name'],
       
  3218 			'COPYRIGHT'			=> $style_row[$mode . '_copyright'])
       
  3219 		);
       
  3220 
       
  3221 	}
       
  3222 
       
  3223 	/**
       
  3224 
       
  3225 					$reqd_template = (isset($installcfg['required_template'])) ? $installcfg['required_template'] : false;
       
  3226 					$reqd_theme = (isset($installcfg['required_theme'])) ? $installcfg['required_theme'] : false;
       
  3227 					$reqd_imageset = (isset($installcfg['required_imageset'])) ? $installcfg['required_imageset'] : false;
       
  3228 
       
  3229 					// Check to see if each element is already installed, if it is grab the id
       
  3230 					foreach ($element_ary as $element => $table)
       
  3231 					{
       
  3232 						$style_row = array_merge($style_row, array(
       
  3233 							$element . '_id'			=> 0,
       
  3234 							$element . '_name'			=> '',
       
  3235 							$element . '_copyright'		=> '')
       
  3236 						);
       
  3237 
       
  3238 			 			$this->test_installed($element, $error, $root_path, ${'reqd_' . $element}, $style_row[$element . '_id'], $style_row[$element . '_name'], $style_row[$element . '_copyright']);
       
  3239 	* Is this element installed? If not, grab its cfg details
       
  3240 	*/
       
  3241 	function test_installed($element, &$error, $root_path, $reqd_name, &$id, &$name, &$copyright)
       
  3242 	{
       
  3243 		global $db, $user;
       
  3244 
       
  3245 		switch ($element)
       
  3246 		{
       
  3247 			case 'template':
       
  3248 				$sql_from = STYLES_TEMPLATE_TABLE;
       
  3249 			break;
       
  3250 
       
  3251 			case 'theme':
       
  3252 				$sql_from = STYLES_THEME_TABLE;
       
  3253 			break;
       
  3254 
       
  3255 			case 'imageset':
       
  3256 				$sql_from = STYLES_IMAGESET_TABLE;
       
  3257 			break;
       
  3258 		}
       
  3259 
       
  3260 		$l_element = strtoupper($element);
       
  3261 
       
  3262 		$chk_name = ($reqd_name !== false) ? $reqd_name : $name;
       
  3263 
       
  3264 		$sql = "SELECT {$element}_id, {$element}_name
       
  3265 			FROM $sql_from
       
  3266 			WHERE {$element}_name = '" . $db->sql_escape($chk_name) . "'";
       
  3267 		$result = $db->sql_query($sql);
       
  3268 
       
  3269 		if ($row = $db->sql_fetchrow($result))
       
  3270 		{
       
  3271 			$name = $row[$element . '_name'];
       
  3272 			$id = $row[$element . '_id'];
       
  3273 		}
       
  3274 		else
       
  3275 		{
       
  3276 			if (!($cfg = @file("$root_path$element/$element.cfg")))
       
  3277 			{
       
  3278 				$error[] = sprintf($user->lang['REQUIRES_' . $l_element], $reqd_name);
       
  3279 				return false;
       
  3280 			}
       
  3281 
       
  3282 			$cfg = parse_cfg_file("$root_path$element/$element.cfg", $cfg);
       
  3283 
       
  3284 			$name = $cfg['name'];
       
  3285 			$copyright = $cfg['copyright'];
       
  3286 			$id = 0;
       
  3287 
       
  3288 			unset($cfg);
       
  3289 		}
       
  3290 		$db->sql_freeresult($result);
       
  3291 	}
       
  3292 
       
  3293 	/**
       
  3294 	* Install/Add style
       
  3295 	*/
       
  3296 	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)
       
  3297 	{
       
  3298 		global $config, $db, $user;
       
  3299 
       
  3300 		$element_ary = array('template', 'theme', 'imageset');
       
  3301 
       
  3302 		if (!$name)
       
  3303 		{
       
  3304 			$error[] = $user->lang['STYLE_ERR_STYLE_NAME'];
       
  3305 		}
       
  3306 
       
  3307 		// Check length settings
       
  3308 		if (utf8_strlen($name) > 30)
       
  3309 		{
       
  3310 			$error[] = $user->lang['STYLE_ERR_NAME_LONG'];
       
  3311 		}
       
  3312 
       
  3313 		if (utf8_strlen($copyright) > 60)
       
  3314 		{
       
  3315 			$error[] = $user->lang['STYLE_ERR_COPY_LONG'];
       
  3316 		}
       
  3317 
       
  3318 		// Check if the name already exist
       
  3319 		$sql = 'SELECT style_id
       
  3320 			FROM ' . STYLES_TABLE . "
       
  3321 			WHERE style_name = '" . $db->sql_escape($name) . "'";
       
  3322 		$result = $db->sql_query($sql);
       
  3323 		$row = $db->sql_fetchrow($result);
       
  3324 		$db->sql_freeresult($result);
       
  3325 
       
  3326 		if ($row)
       
  3327 		{
       
  3328 			$error[] = $user->lang['STYLE_ERR_NAME_EXIST'];
       
  3329 		}
       
  3330 
       
  3331 		if (sizeof($error))
       
  3332 		{
       
  3333 			return false;
       
  3334 		}
       
  3335 
       
  3336 		foreach ($element_ary as $element)
       
  3337 		{
       
  3338 			// Zero id value ... need to install element ... run usual checks
       
  3339 			// and do the install if necessary
       
  3340 			if (!$style_row[$element . '_id'])
       
  3341 			{
       
  3342 				$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']);
       
  3343 			}
       
  3344 		}
       
  3345 
       
  3346 		if (!$style_row['template_id'] || !$style_row['theme_id'] || !$style_row['imageset_id'])
       
  3347 		{
       
  3348 			$error[] = $user->lang['STYLE_ERR_NO_IDS'];
       
  3349 		}
       
  3350 
       
  3351 		if (sizeof($error))
       
  3352 		{
       
  3353 			return false;
       
  3354 		}
       
  3355 
       
  3356 		$db->sql_transaction('begin');
       
  3357 
       
  3358 		$sql_ary = array(
       
  3359 			'style_name'		=> $name,
       
  3360 			'style_copyright'	=> $copyright,
       
  3361 			'style_active'		=> (int) $active,
       
  3362 			'template_id'		=> (int) $style_row['template_id'],
       
  3363 			'theme_id'			=> (int) $style_row['theme_id'],
       
  3364 			'imageset_id'		=> (int) $style_row['imageset_id'],
       
  3365 		);
       
  3366 
       
  3367 		$sql = 'INSERT INTO ' . STYLES_TABLE . '
       
  3368 			' . $db->sql_build_array('INSERT', $sql_ary);
       
  3369 		$db->sql_query($sql);
       
  3370 
       
  3371 		$id = $db->sql_nextid();
       
  3372 
       
  3373 		if ($default)
       
  3374 		{
       
  3375 			$sql = 'UPDATE ' . USERS_TABLE . "
       
  3376 				SET user_style = $id
       
  3377 				WHERE user_style = " . $config['default_style'];
       
  3378 			$db->sql_query($sql);
       
  3379 
       
  3380 			set_config('default_style', $id);
       
  3381 		}
       
  3382 
       
  3383 		$db->sql_transaction('commit');
       
  3384 
       
  3385 		add_log('admin', 'LOG_STYLE_ADD', $name);
       
  3386 	}
       
  3387 
       
  3388 	/**
       
  3389 	* Install/add an element, doing various checks as we go
       
  3390 	*/
       
  3391 	function install_element($mode, &$error, $action, $root_path, &$id, $name, $path, $copyright, $store_db = 0)
       
  3392 	{
       
  3393 		global $phpbb_root_path, $db, $user;
       
  3394 
       
  3395 		// we parse the cfg here (again)
       
  3396 		$cfg_data = parse_cfg_file("$root_path$mode/$mode.cfg");
       
  3397 
       
  3398 		switch ($mode)
       
  3399 		{
       
  3400 			case 'template':
       
  3401 				$sql_from = STYLES_TEMPLATE_TABLE;
       
  3402 			break;
       
  3403 
       
  3404 			case 'theme':
       
  3405 				$sql_from = STYLES_THEME_TABLE;
       
  3406 			break;
       
  3407 
       
  3408 			case 'imageset':
       
  3409 				$sql_from = STYLES_IMAGESET_TABLE;
       
  3410 			break;
       
  3411 		}
       
  3412 
       
  3413 		$l_type = strtoupper($mode);
       
  3414 
       
  3415 		if (!$name)
       
  3416 		{
       
  3417 			$error[] = $user->lang[$l_type . '_ERR_STYLE_NAME'];
       
  3418 		}
       
  3419 
       
  3420 		// Check length settings
       
  3421 		if (utf8_strlen($name) > 30)
       
  3422 		{
       
  3423 			$error[] = $user->lang[$l_type . '_ERR_NAME_LONG'];
       
  3424 		}
       
  3425 
       
  3426 		if (utf8_strlen($copyright) > 60)
       
  3427 		{
       
  3428 			$error[] = $user->lang[$l_type . '_ERR_COPY_LONG'];
       
  3429 		}
       
  3430 
       
  3431 		// Check if the name already exist
       
  3432 		$sql = "SELECT {$mode}_id
       
  3433 			FROM $sql_from
       
  3434 			WHERE {$mode}_name = '" . $db->sql_escape($name) . "'";
       
  3435 		$result = $db->sql_query($sql);
       
  3436 		$row = $db->sql_fetchrow($result);
       
  3437 		$db->sql_freeresult($result);
       
  3438 
       
  3439 		if ($row)
       
  3440 		{
       
  3441 			// If it exist, we just use the style on installation
       
  3442 			if ($action == 'install')
       
  3443 			{
       
  3444 				$id = $row[$mode . '_id'];
       
  3445 				return false;
       
  3446 			}
       
  3447 
       
  3448 			$error[] = $user->lang[$l_type . '_ERR_NAME_EXIST'];
       
  3449 		}
       
  3450 
       
  3451 		if (isset($cfg_data['inherit_from']) && $cfg_data['inherit_from'])
       
  3452 		{
       
  3453 			if ($mode === 'template')
       
  3454 			{
       
  3455 				$select_bf = ', bbcode_bitfield';
       
  3456 			}
       
  3457 			else
       
  3458 			{
       
  3459 				$select_bf = '';
       
  3460 			}
       
  3461 
       
  3462 			$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path, {$mode}_storedb $select_bf
       
  3463 				FROM $sql_from
       
  3464 				WHERE {$mode}_name = '" . $db->sql_escape($cfg_data['inherit_from']) . "'
       
  3465 					AND {$mode}_inherits_id = 0";
       
  3466 			$result = $db->sql_query($sql);
       
  3467 			$row = $db->sql_fetchrow($result);
       
  3468 			$db->sql_freeresult($result);
       
  3469 			if (!$row)
       
  3470 			{
       
  3471 				$error[] = sprintf($user->lang[$l_type . '_ERR_REQUIRED_OR_INCOMPLETE'], $cfg_data['inherit_from']);
       
  3472 			}
       
  3473 			else
       
  3474 			{
       
  3475 				$inherit_id = $row["{$mode}_id"];
       
  3476 				$inherit_path = $row["{$mode}_path"];
       
  3477 				$inherit_bf = ($mode === 'template') ? $row["bbcode_bitfield"] : false;
       
  3478 				$cfg_data['store_db'] = $row["{$mode}_storedb"];
       
  3479 				$store_db = $row["{$mode}_storedb"];
       
  3480 			}
       
  3481 		}
       
  3482 		else
       
  3483 		{
       
  3484 			$inherit_id = 0;
       
  3485 			$inherit_path = '';
       
  3486 			$inherit_bf = false;
       
  3487 		}
       
  3488 
       
  3489 		if (sizeof($error))
       
  3490 		{
       
  3491 			return false;
       
  3492 		}
       
  3493 
       
  3494 		$sql_ary = array(
       
  3495 			$mode . '_name'			=> $name,
       
  3496 			$mode . '_copyright'	=> $copyright,
       
  3497 			$mode . '_path'			=> $path,
       
  3498 		);
       
  3499 
       
  3500 		switch ($mode)
       
  3501 		{
       
  3502 			case 'template':
       
  3503 				// We check if the template author defined a different bitfield
       
  3504 				if (!empty($cfg_data['template_bitfield']))
       
  3505 				{
       
  3506 					$sql_ary['bbcode_bitfield'] = $cfg_data['template_bitfield'];
       
  3507 				}
       
  3508 				else if ($inherit_bf)
       
  3509 				{
       
  3510 					$sql_ary['bbcode_bitfield'] = $inherit_bf;
       
  3511 				}
       
  3512 				else
       
  3513 				{
       
  3514 					$sql_ary['bbcode_bitfield'] = TEMPLATE_BITFIELD;
       
  3515 				}
       
  3516 
       
  3517 				// We set a pre-defined bitfield here which we may use further in 3.2
       
  3518 				$sql_ary += array(
       
  3519 					'template_storedb'		=> $store_db,
       
  3520 				);
       
  3521 				if (isset($cfg_data['inherit_from']) && $cfg_data['inherit_from'])
       
  3522 				{
       
  3523 					$sql_ary += array(
       
  3524 						'template_inherits_id'	=> $inherit_id,
       
  3525 						'template_inherit_path' => $inherit_path,
       
  3526 					);
       
  3527 				}
       
  3528 			break;
       
  3529 
       
  3530 			case 'theme':
       
  3531 				// We are only interested in the theme configuration for now
       
  3532 
       
  3533 				if (isset($cfg_data['parse_css_file']) && $cfg_data['parse_css_file'])
       
  3534 				{
       
  3535 					$store_db = 1;
       
  3536 				}
       
  3537 
       
  3538 				$sql_ary += array(
       
  3539 					'theme_storedb'	=> $store_db,
       
  3540 					'theme_data'	=> ($store_db) ? $this->db_theme_data($sql_ary, false, $root_path) : '',
       
  3541 					'theme_mtime'	=> (int) filemtime("{$phpbb_root_path}styles/$path/theme/stylesheet.css")
       
  3542 				);
       
  3543 			break;
       
  3544 
       
  3545 			// all the heavy lifting is done later
       
  3546 			case 'imageset':
       
  3547 			break;
       
  3548 		}
       
  3549 
       
  3550 		$db->sql_transaction('begin');
       
  3551 
       
  3552 		$sql = "INSERT INTO $sql_from
       
  3553 			" . $db->sql_build_array('INSERT', $sql_ary);
       
  3554 		$db->sql_query($sql);
       
  3555 
       
  3556 		$id = $db->sql_nextid();
       
  3557 
       
  3558 		if ($mode == 'template' && $store_db)
       
  3559 		{
       
  3560 			$filelist = filelist("{$root_path}template", '', 'html');
       
  3561 			$this->store_templates('insert', $id, $path, $filelist);
       
  3562 		}
       
  3563 		else if ($mode == 'imageset')
       
  3564 		{
       
  3565 			$cfg_data = parse_cfg_file("$root_path$mode/imageset.cfg");
       
  3566 
       
  3567 			$imageset_definitions = array();
       
  3568 			foreach ($this->imageset_keys as $topic => $key_array)
       
  3569 			{
       
  3570 				$imageset_definitions = array_merge($imageset_definitions, $key_array);
       
  3571 			}
       
  3572 
       
  3573 			foreach ($cfg_data as $key => $value)
       
  3574 			{
       
  3575 				if (strpos($value, '*') !== false)
       
  3576 				{
       
  3577 					if (substr($value, -1, 1) === '*')
       
  3578 					{
       
  3579 						list($image_filename, $image_height) = explode('*', $value);
       
  3580 						$image_width = 0;
       
  3581 					}
       
  3582 					else
       
  3583 					{
       
  3584 						list($image_filename, $image_height, $image_width) = explode('*', $value);
       
  3585 					}
       
  3586 				}
       
  3587 				else
       
  3588 				{
       
  3589 					$image_filename = $value;
       
  3590 					$image_height = $image_width = 0;
       
  3591 				}
       
  3592 
       
  3593 				if (strpos($key, 'img_') === 0 && $image_filename)
       
  3594 				{
       
  3595 					$key = substr($key, 4);
       
  3596 					if (in_array($key, $imageset_definitions))
       
  3597 					{
       
  3598 						$sql_ary = array(
       
  3599 							'image_name'		=> $key,
       
  3600 							'image_filename'	=> str_replace('{PATH}', "styles/$path/imageset/", trim($image_filename)),
       
  3601 							'image_height'		=> (int) $image_height,
       
  3602 							'image_width'		=> (int) $image_width,
       
  3603 							'imageset_id'		=> (int) $id,
       
  3604 							'image_lang'		=> '',
       
  3605 						);
       
  3606 						$db->sql_query('INSERT INTO ' . STYLES_IMAGESET_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
       
  3607 					}
       
  3608 				}
       
  3609 			}
       
  3610 			unset($cfg_data);
       
  3611 
       
  3612 			$sql = 'SELECT lang_dir
       
  3613 				FROM ' . LANG_TABLE;
       
  3614 			$result = $db->sql_query($sql);
       
  3615 
       
  3616 			while ($row = $db->sql_fetchrow($result))
       
  3617 			{
       
  3618 				if (@file_exists("$root_path$mode/{$row['lang_dir']}/imageset.cfg"))
       
  3619 				{
       
  3620 					$cfg_data_imageset_data = parse_cfg_file("$root_path$mode/{$row['lang_dir']}/imageset.cfg");
       
  3621 					foreach ($cfg_data_imageset_data as $image_name => $value)
       
  3622 					{
       
  3623 						if (strpos($value, '*') !== false)
       
  3624 						{
       
  3625 							if (substr($value, -1, 1) === '*')
       
  3626 							{
       
  3627 								list($image_filename, $image_height) = explode('*', $value);
       
  3628 								$image_width = 0;
       
  3629 							}
       
  3630 							else
       
  3631 							{
       
  3632 								list($image_filename, $image_height, $image_width) = explode('*', $value);
       
  3633 							}
       
  3634 						}
       
  3635 						else
       
  3636 						{
       
  3637 							$image_filename = $value;
       
  3638 							$image_height = $image_width = 0;
       
  3639 						}
       
  3640 
       
  3641 						if (strpos($image_name, 'img_') === 0 && $image_filename)
       
  3642 						{
       
  3643 							$image_name = substr($image_name, 4);
       
  3644 							if (in_array($image_name, $imageset_definitions))
       
  3645 							{
       
  3646 								$sql_ary = array(
       
  3647 									'image_name'		=> $image_name,
       
  3648 									'image_filename'	=> $image_filename,
       
  3649 									'image_height'		=> (int) $image_height,
       
  3650 									'image_width'		=> (int) $image_width,
       
  3651 									'imageset_id'		=> (int) $id,
       
  3652 									'image_lang'		=> $row['lang_dir'],
       
  3653 								);
       
  3654 								$db->sql_query('INSERT INTO ' . STYLES_IMAGESET_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
       
  3655 							}
       
  3656 						}
       
  3657 					}
       
  3658 					unset($cfg_data_imageset_data);
       
  3659 				}
       
  3660 			}
       
  3661 			$db->sql_freeresult($result);
       
  3662 		}
       
  3663 
       
  3664 		$db->sql_transaction('commit');
       
  3665 
       
  3666 		$log = ($store_db) ? 'LOG_' . $l_type . '_ADD_DB' : 'LOG_' . $l_type . '_ADD_FS';
       
  3667 		add_log('admin', $log, $name);
       
  3668 
       
  3669 		// Return store_db in case it had to be altered
       
  3670 		return $store_db;
       
  3671 	}
       
  3672 
       
  3673 	/**
       
  3674 	* Checks downwards dependencies
       
  3675 	*
       
  3676 	* @access public
       
  3677 	* @param string $mode The element type to check - only template is supported
       
  3678 	* @param int $id The template id
       
  3679 	* @returns false if no component inherits, array with name, path and id for each subtemplate otherwise
       
  3680 	*/
       
  3681 	function check_inheritance($mode, $id)
       
  3682 	{
       
  3683 		global $db;
       
  3684 
       
  3685 		$l_type = strtoupper($mode);
       
  3686 
       
  3687 		switch ($mode)
       
  3688 		{
       
  3689 			case 'template':
       
  3690 				$sql_from = STYLES_TEMPLATE_TABLE;
       
  3691 			break;
       
  3692 
       
  3693 			case 'theme':
       
  3694 				$sql_from = STYLES_THEME_TABLE;
       
  3695 			break;
       
  3696 
       
  3697 			case 'imageset':
       
  3698 				$sql_from = STYLES_IMAGESET_TABLE;
       
  3699 			break;
       
  3700 		}
       
  3701 
       
  3702 		$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
       
  3703 			FROM $sql_from
       
  3704 			WHERE {$mode}_inherits_id = " . (int) $id;
       
  3705 		$result = $db->sql_query($sql);
       
  3706 
       
  3707 		$names = array();
       
  3708 		while ($row = $db->sql_fetchrow($result))
       
  3709 		{
       
  3710 
       
  3711 			$names[$row["{$mode}_id"]] = array(
       
  3712 				"{$mode}_id" => $row["{$mode}_id"],
       
  3713 				"{$mode}_name" => $row["{$mode}_name"],
       
  3714 				"{$mode}_path" => $row["{$mode}_path"],
       
  3715 			);
       
  3716 		}
       
  3717 		$db->sql_freeresult($result);
       
  3718 
       
  3719 		if (sizeof($names))
       
  3720 		{
       
  3721 			return $names;
       
  3722 		}
       
  3723 		else
       
  3724 		{
       
  3725 			return false;
       
  3726 		}
       
  3727 	}
       
  3728 
       
  3729 	/**
       
  3730 	* Checks upwards dependencies
       
  3731 	*
       
  3732 	* @access public
       
  3733 	* @param string $mode The element type to check - only template is supported
       
  3734 	* @param int $id The template id
       
  3735 	* @returns false if the component does not inherit, array with name, path and id otherwise
       
  3736 	*/
       
  3737 	function get_super($mode, $id)
       
  3738 	{
       
  3739 		global $db;
       
  3740 
       
  3741 		$l_type = strtoupper($mode);
       
  3742 
       
  3743 		switch ($mode)
       
  3744 		{
       
  3745 			case 'template':
       
  3746 				$sql_from = STYLES_TEMPLATE_TABLE;
       
  3747 			break;
       
  3748 
       
  3749 			case 'theme':
       
  3750 				$sql_from = STYLES_THEME_TABLE;
       
  3751 			break;
       
  3752 
       
  3753 			case 'imageset':
       
  3754 				$sql_from = STYLES_IMAGESET_TABLE;
       
  3755 			break;
       
  3756 		}
       
  3757 
       
  3758 		$sql = "SELECT {$mode}_inherits_id
       
  3759 			FROM $sql_from
       
  3760 			WHERE {$mode}_id = " . (int) $id;
       
  3761 		$result = $db->sql_query_limit($sql, 1);
       
  3762 
       
  3763 		if ($row = $db->sql_fetchrow($result))
       
  3764 		{
       
  3765 			$db->sql_freeresult($result);
       
  3766 		}
       
  3767 		else
       
  3768 		{
       
  3769 			return false;
       
  3770 		}
       
  3771 
       
  3772 		$super_id = $row["{$mode}_inherits_id"];
       
  3773 
       
  3774 		$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
       
  3775 			FROM $sql_from
       
  3776 			WHERE {$mode}_id = " . (int) $super_id;
       
  3777 
       
  3778 		$result = $db->sql_query_limit($sql, 1);
       
  3779 		if ($row = $db->sql_fetchrow($result))
       
  3780 		{
       
  3781 			$db->sql_freeresult($result);
       
  3782 			return $row;
       
  3783 		}
       
  3784 
       
  3785 		return false;
       
  3786 	}
       
  3787 
       
  3788 	/**
       
  3789 	* Moves a template set and its subtemplates to the database
       
  3790 	*
       
  3791 	* @access public
       
  3792 	* @param string $mode The component to move - only template is supported
       
  3793 	* @param int $id The template id
       
  3794 	*/
       
  3795 	function store_in_db($mode, $id)
       
  3796 	{
       
  3797 		global $db, $user;
       
  3798 
       
  3799 		$error = array();
       
  3800 		$l_type = strtoupper($mode);
       
  3801 		if ($super = $this->get_super($mode, $id))
       
  3802 		{
       
  3803 			$error[] = (sprintf($user->lang["{$l_type}_INHERITS"], $super['template_name']));
       
  3804 			return $error;
       
  3805 		}
       
  3806 
       
  3807 		$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
       
  3808 			FROM " . STYLES_TEMPLATE_TABLE . '
       
  3809 			WHERE template_id = ' . (int) $id;
       
  3810 
       
  3811 		$result = $db->sql_query_limit($sql, 1);
       
  3812 		if ($row = $db->sql_fetchrow($result))
       
  3813 		{
       
  3814 			$db->sql_freeresult($result);
       
  3815 			$subs = $this->check_inheritance($mode, $id);
       
  3816 
       
  3817 			$this->_store_in_db($mode, $id, $row["{$mode}_path"]);
       
  3818 			if ($subs && sizeof($subs))
       
  3819 			{
       
  3820 				foreach ($subs as $sub_id => $sub)
       
  3821 				{
       
  3822 					if ($err = $this->_store_in_db($mode, $sub["{$mode}_id"], $sub["{$mode}_path"]))
       
  3823 					{
       
  3824 						$error[] = $err;
       
  3825 					}
       
  3826 				}
       
  3827 			}
       
  3828 		}
       
  3829 		if (sizeof($error))
       
  3830 		{
       
  3831 			return $error;
       
  3832 		}
       
  3833 
       
  3834 		return false;
       
  3835 	}
       
  3836 
       
  3837 	/**
       
  3838 	* Moves a template set to the database
       
  3839 	*
       
  3840 	* @access private
       
  3841 	* @param string $mode The component to move - only template is supported
       
  3842 	* @param int $id The template id
       
  3843 	* @param string $path TThe path to the template files
       
  3844 	*/
       
  3845 	function _store_in_db($mode, $id, $path)
       
  3846 	{
       
  3847 		global $phpbb_root_path, $db;
       
  3848 
       
  3849 		$filelist = filelist("{$phpbb_root_path}styles/{$path}/template", '', 'html');
       
  3850 		$this->store_templates('insert', $id, $path, $filelist);
       
  3851 
       
  3852 		// Okay, we do the query here -shouldn't be triggered often.
       
  3853 		$sql = 'UPDATE ' . STYLES_TEMPLATE_TABLE . '
       
  3854 						SET template_storedb = 1
       
  3855 						WHERE template_id = ' . $id;
       
  3856 		$db->sql_query($sql);
       
  3857 	}
       
  3858 
       
  3859 	/**
       
  3860 	* Moves a template set and its subtemplates to the filesystem
       
  3861 	*
       
  3862 	* @access public
       
  3863 	* @param string $mode The component to move - only template is supported
       
  3864 	* @param int $id The template id
       
  3865 	*/
       
  3866 	function store_in_fs($mode, $id)
       
  3867 	{
       
  3868 		global $db, $user;
       
  3869 
       
  3870 		$error = array();
       
  3871 		$l_type = strtoupper($mode);
       
  3872 		if ($super = $this->get_super($mode, $id))
       
  3873 		{
       
  3874 			$error[] = (sprintf($user->lang["{$l_type}_INHERITS"], $super['template_name']));
       
  3875 			return($error);
       
  3876 		}
       
  3877 
       
  3878 		$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
       
  3879 			FROM " . STYLES_TEMPLATE_TABLE . '
       
  3880 			WHERE template_id = ' . (int) $id;
       
  3881 
       
  3882 		$result = $db->sql_query_limit($sql, 1);
       
  3883 		if ($row = $db->sql_fetchrow($result))
       
  3884 		{
       
  3885 			$db->sql_freeresult($result);
       
  3886 			if (!sizeof($error))
       
  3887 			{
       
  3888 				$subs = $this->check_inheritance($mode, $id);
       
  3889 
       
  3890 				$this->_store_in_fs($mode, $id, $row["{$mode}_path"]);
       
  3891 
       
  3892 				if ($subs && sizeof($subs))
       
  3893 				{
       
  3894 					foreach ($subs as $sub_id => $sub)
       
  3895 					{
       
  3896 						$this->_store_in_fs($mode, $sub["{$mode}_id"], $sub["{$mode}_path"]);
       
  3897 					}
       
  3898 				}
       
  3899 			}
       
  3900 			if (sizeof($error))
       
  3901 			{
       
  3902 				$this->store_in_db($id, $mode);
       
  3903 				return $error;
       
  3904 			}
       
  3905 		}
       
  3906 		return false;
       
  3907 	}
       
  3908 
       
  3909 	/**
       
  3910 	* Moves a template set to the filesystem
       
  3911 	*
       
  3912 	* @access private
       
  3913 	* @param string $mode The component to move - only template is supported
       
  3914 	* @param int $id The template id
       
  3915 	* @param string $path The path to the template
       
  3916 	*/
       
  3917 	function _store_in_fs($mode, $id, $path)
       
  3918 	{
       
  3919 		global $phpbb_root_path, $db, $user, $safe_mode;
       
  3920 
       
  3921 		$store_db = 0;
       
  3922 		$error = array();
       
  3923 		if (!$safe_mode && phpbb_is_writable("{$phpbb_root_path}styles/{$path}/template"))
       
  3924 		{
       
  3925 			$sql = 'SELECT *
       
  3926 					FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
       
  3927 					WHERE template_id = $id";
       
  3928 			$result = $db->sql_query($sql);
       
  3929 
       
  3930 			while ($row = $db->sql_fetchrow($result))
       
  3931 			{
       
  3932 				if (!($fp = @fopen("{$phpbb_root_path}styles/{$path}/template/" . $row['template_filename'], 'wb')))
       
  3933 				{
       
  3934 					$store_db = 1;
       
  3935 					$error[] = $user->lang['EDIT_TEMPLATE_STORED_DB'];
       
  3936 					break;
       
  3937 				}
       
  3938 
       
  3939 				fwrite($fp, $row['template_data']);
       
  3940 				fclose($fp);
       
  3941 			}
       
  3942 			$db->sql_freeresult($result);
       
  3943 
       
  3944 			if (!$store_db)
       
  3945 			{
       
  3946 				$sql = 'DELETE FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
       
  3947 						WHERE template_id = $id";
       
  3948 				$db->sql_query($sql);
       
  3949 			}
       
  3950 		}
       
  3951 		if (sizeof($error))
       
  3952 		{
       
  3953 			return $error;
       
  3954 		}
       
  3955 		$sql = 'UPDATE ' . STYLES_TEMPLATE_TABLE . '
       
  3956 				SET template_storedb = 0
       
  3957 				WHERE template_id = ' . $id;
       
  3958 		$db->sql_query($sql);
       
  3959 
       
  3960 		return false;
       
  3961 	}
       
  3962 
       
  3963 }
       
  3964 
       
  3965 ?>