Blame Identity/Webenv/phpBB/3.0.4/includes/acp/acp_styles.php

ef5584
ef5584
/**
ef5584
*
ef5584
* @package acp
ef5584
* @version $Id: acp_styles.php 9152 2008-12-02 16:49:59Z acydburn $
ef5584
* @copyright (c) 2005 phpBB Group
ef5584
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
ef5584
*
ef5584
*/
ef5584
ef5584
/**
ef5584
* @ignore
ef5584
*/
ef5584
if (!defined('IN_PHPBB'))
ef5584
{
ef5584
	exit;
ef5584
}
ef5584
ef5584
/**
ef5584
* @package acp
ef5584
*/
ef5584
class acp_styles
ef5584
{
ef5584
	var $u_action;
ef5584
ef5584
	var $style_cfg;
ef5584
	var $template_cfg;
ef5584
	var $theme_cfg;
ef5584
	var $imageset_cfg;
ef5584
	var $imageset_keys;
ef5584
ef5584
	function main($id, $mode)
ef5584
	{
ef5584
		global $db, $user, $auth, $template, $cache;
ef5584
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
ef5584
ef5584
		// Hardcoded template bitfield to add for new templates
ef5584
		$bitfield = new bitfield();
ef5584
		$bitfield->set(0);
ef5584
		$bitfield->set(3);
ef5584
		$bitfield->set(8);
ef5584
		$bitfield->set(9);
ef5584
		$bitfield->set(11);
ef5584
		$bitfield->set(12);
ef5584
		define('TEMPLATE_BITFIELD', $bitfield->get_base64());
ef5584
		unset($bitfield);
ef5584
ef5584
		$user->add_lang('acp/styles');
ef5584
ef5584
		$this->tpl_name = 'acp_styles';
ef5584
		$this->page_title = 'ACP_CAT_STYLES';
ef5584
ef5584
		$action = request_var('action', '');
ef5584
		$action = (isset($_POST['add'])) ? 'add' : $action;
ef5584
		$style_id = request_var('id', 0);
ef5584
ef5584
		// Fill the configuration variables
ef5584
		$this->style_cfg = $this->template_cfg = $this->theme_cfg = $this->imageset_cfg = '
ef5584
#
ef5584
# phpBB {MODE} configuration file
ef5584
#
ef5584
# @package phpBB3
ef5584
# @copyright (c) 2005 phpBB Group
ef5584
# @license http://opensource.org/licenses/gpl-license.php GNU Public License
ef5584
#
ef5584
#
ef5584
# At the left is the name, please do not change this
ef5584
# At the right the value is entered
ef5584
# For on/off options the valid values are on, off, 1, 0, true and false
ef5584
#
ef5584
# Values get trimmed, if you want to add a space in front or at the end of
ef5584
# the value, then enclose the value with single or double quotes.
ef5584
# Single and double quotes do not need to be escaped.
ef5584
#
ef5584
#
ef5584
ef5584
# General Information about this {MODE}
ef5584
name = {NAME}
ef5584
copyright = {COPYRIGHT}
ef5584
version = {VERSION}
ef5584
';
ef5584
ef5584
		$this->theme_cfg .= '
ef5584
# Some configuration options
ef5584
ef5584
#
ef5584
# You have to turn this option on if you want to use the
ef5584
# path template variables ({T_IMAGESET_PATH} for example) within
ef5584
# your css file.
ef5584
# This is mostly the case if you want to use language specific
ef5584
# images within your css file.
ef5584
#
ef5584
parse_css_file = {PARSE_CSS_FILE}
ef5584
';
ef5584
ef5584
		$this->template_cfg .= '
ef5584
# Some configuration options
ef5584
ef5584
#
ef5584
# You can use this function to inherit templates from another template.
ef5584
# The template of the given name has to be installed.
ef5584
# Templates cannot inherit from inheriting templates.
ef5584
#';
ef5584
ef5584
		$this->imageset_keys = array(
ef5584
			'logos' => array(
ef5584
				'site_logo',
ef5584
			),
ef5584
			'buttons'	=> array(
ef5584
				'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',
ef5584
			),
ef5584
			'icons'		=> array(
ef5584
				'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',
ef5584
			),
ef5584
			'forums'	=> array(
ef5584
				'forum_link', 'forum_read', 'forum_read_locked', 'forum_read_subforum', 'forum_unread', 'forum_unread_locked', 'forum_unread_subforum', 'subforum_read', 'subforum_unread'
ef5584
			),
ef5584
			'folders'	=> array(
ef5584
				'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',
ef5584
			),
ef5584
			'polls'		=> array(
ef5584
				'poll_left', 'poll_center', 'poll_right',
ef5584
			),
ef5584
			'ui'		=> array(
ef5584
				'upload_bar',
ef5584
			),
ef5584
			'user'		=> array(
ef5584
				'user_icon1', 'user_icon2', 'user_icon3', 'user_icon4', 'user_icon5', 'user_icon6', 'user_icon7', 'user_icon8', 'user_icon9', 'user_icon10',
ef5584
			),
ef5584
		);
ef5584
ef5584
		// Execute overall actions
ef5584
		switch ($action)
ef5584
		{
ef5584
			case 'delete':
ef5584
				if ($style_id)
ef5584
				{
ef5584
					$this->remove($mode, $style_id);
ef5584
					return;
ef5584
				}
ef5584
			break;
ef5584
ef5584
			case 'export':
ef5584
				if ($style_id)
ef5584
				{
ef5584
					$this->export($mode, $style_id);
ef5584
					return;
ef5584
				}
ef5584
			break;
ef5584
ef5584
			case 'install':
ef5584
				$this->install($mode);
ef5584
				return;
ef5584
			break;
ef5584
ef5584
			case 'add':
ef5584
				$this->add($mode);
ef5584
				return;
ef5584
			break;
ef5584
ef5584
			case 'details':
ef5584
				if ($style_id)
ef5584
				{
ef5584
					$this->details($mode, $style_id);
ef5584
					return;
ef5584
				}
ef5584
			break;
ef5584
ef5584
			case 'edit':
ef5584
				if ($style_id)
ef5584
				{
ef5584
					switch ($mode)
ef5584
					{
ef5584
						case 'imageset':
ef5584
							return $this->edit_imageset($style_id);
ef5584
						case 'template':
ef5584
							return $this->edit_template($style_id);
ef5584
						case 'theme':
ef5584
							return $this->edit_theme($style_id);
ef5584
					}
ef5584
				}
ef5584
			break;
ef5584
ef5584
			case 'cache':
ef5584
				if ($style_id)
ef5584
				{
ef5584
					switch ($mode)
ef5584
					{
ef5584
						case 'template':
ef5584
							return $this->template_cache($style_id);
ef5584
					}
ef5584
				}
ef5584
			break;
ef5584
		}
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'style':
ef5584
ef5584
				switch ($action)
ef5584
				{
ef5584
					case 'activate':
ef5584
					case 'deactivate':
ef5584
ef5584
						if ($style_id == $config['default_style'])
ef5584
						{
ef5584
							trigger_error($user->lang['DEACTIVATE_DEFAULT'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
						}
ef5584
ef5584
						$sql = 'UPDATE ' . STYLES_TABLE . '
ef5584
							SET style_active = ' . (($action == 'activate') ? 1 : 0) . '
ef5584
							WHERE style_id = ' . $style_id;
ef5584
						$db->sql_query($sql);
ef5584
ef5584
						// Set style to default for any member using deactivated style
ef5584
						if ($action == 'deactivate')
ef5584
						{
ef5584
							$sql = 'UPDATE ' . USERS_TABLE . '
ef5584
								SET user_style = ' . $config['default_style'] . "
ef5584
								WHERE user_style = $style_id";
ef5584
							$db->sql_query($sql);
ef5584
ef5584
							$sql = 'UPDATE ' . FORUMS_TABLE . '
ef5584
								SET forum_style = 0
ef5584
								WHERE forum_style = ' . $style_id;
ef5584
							$db->sql_query($sql);
ef5584
						}
ef5584
					break;
ef5584
				}
ef5584
ef5584
				$this->frontend('style', array('details'), array('export', 'delete'));
ef5584
			break;
ef5584
ef5584
			case 'template':
ef5584
ef5584
				switch ($action)
ef5584
				{
ef5584
					// Refresh template data stored in db and clear cache
ef5584
					case 'refresh':
ef5584
ef5584
						$sql = 'SELECT *
ef5584
							FROM ' . STYLES_TEMPLATE_TABLE . "
ef5584
							WHERE template_id = $style_id";
ef5584
						$result = $db->sql_query($sql);
ef5584
						$template_row = $db->sql_fetchrow($result);
ef5584
						$db->sql_freeresult($result);
ef5584
ef5584
						if (!$template_row)
ef5584
						{
ef5584
							trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
						}
ef5584
ef5584
						if (confirm_box(true))
ef5584
						{
ef5584
							$template_refreshed = '';
ef5584
ef5584
							// Only refresh database if the template is stored in the database
ef5584
							if ($template_row['template_storedb'] && file_exists("{$phpbb_root_path}styles/{$template_row['template_path']}/template/"))
ef5584
							{
ef5584
								$filelist = array('' => array());
ef5584
ef5584
								$sql = 'SELECT template_filename, template_mtime
ef5584
									FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
ef5584
									WHERE template_id = $style_id";
ef5584
								$result = $db->sql_query($sql);
ef5584
ef5584
								while ($row = $db->sql_fetchrow($result))
ef5584
								{
ef5584
//									if (@filemtime("{$phpbb_root_path}styles/{$template_row['template_path']}/template/" . $row['template_filename']) > $row['template_mtime'])
ef5584
//									{
ef5584
										// get folder info from the filename
ef5584
										if (($slash_pos = strrpos($row['template_filename'], '/')) === false)
ef5584
										{
ef5584
											$filelist[''][] = $row['template_filename'];
ef5584
										}
ef5584
										else
ef5584
										{
ef5584
											$filelist[substr($row['template_filename'], 0, $slash_pos + 1)][] = substr($row['template_filename'], $slash_pos + 1, strlen($row['template_filename']) - $slash_pos - 1);
ef5584
										}
ef5584
//									}
ef5584
								}
ef5584
								$db->sql_freeresult($result);
ef5584
ef5584
								$this->store_templates('update', $style_id, $template_row['template_path'], $filelist);
ef5584
								unset($filelist);
ef5584
ef5584
								$template_refreshed = $user->lang['TEMPLATE_REFRESHED'] . '
';
ef5584
								add_log('admin', 'LOG_TEMPLATE_REFRESHED', $template_row['template_name']);
ef5584
							}
ef5584
ef5584
							$this->clear_template_cache($template_row);
ef5584
ef5584
							trigger_error($template_refreshed . $user->lang['TEMPLATE_CACHE_CLEARED'] . adm_back_link($this->u_action));
ef5584
						}
ef5584
						else
ef5584
						{
ef5584
							confirm_box(false, ($template_row['template_storedb']) ? $user->lang['CONFIRM_TEMPLATE_REFRESH'] : $user->lang['CONFIRM_TEMPLATE_CLEAR_CACHE'], build_hidden_fields(array(
ef5584
								'i'			=> $id,
ef5584
								'mode'		=> $mode,
ef5584
								'action'	=> $action,
ef5584
								'id'		=> $style_id
ef5584
							)));
ef5584
						}
ef5584
ef5584
					break;
ef5584
				}
ef5584
ef5584
				$this->frontend('template', array('edit', 'cache', 'details'), array('refresh', 'export', 'delete'));
ef5584
			break;
ef5584
ef5584
			case 'theme':
ef5584
ef5584
				switch ($action)
ef5584
				{
ef5584
					// Refresh theme data stored in the database
ef5584
					case 'refresh':
ef5584
ef5584
						$sql = 'SELECT *
ef5584
							FROM ' . STYLES_THEME_TABLE . "
ef5584
							WHERE theme_id = $style_id";
ef5584
						$result = $db->sql_query($sql);
ef5584
						$theme_row = $db->sql_fetchrow($result);
ef5584
						$db->sql_freeresult($result);
ef5584
ef5584
						if (!$theme_row)
ef5584
						{
ef5584
							trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
						}
ef5584
ef5584
						if (!$theme_row['theme_storedb'])
ef5584
						{
ef5584
							trigger_error($user->lang['THEME_ERR_REFRESH_FS'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
						}
ef5584
ef5584
						if (confirm_box(true))
ef5584
						{
ef5584
							if ($theme_row['theme_storedb'] && file_exists("{$phpbb_root_path}styles/{$theme_row['theme_path']}/theme/stylesheet.css"))
ef5584
							{
ef5584
								// Save CSS contents
ef5584
								$sql_ary = array(
ef5584
									'theme_mtime'	=> (int) filemtime("{$phpbb_root_path}styles/{$theme_row['theme_path']}/theme/stylesheet.css"),
ef5584
									'theme_data'	=> $this->db_theme_data($theme_row)
ef5584
								);
ef5584
ef5584
								$sql = 'UPDATE ' . STYLES_THEME_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
ef5584
									WHERE theme_id = $style_id";
ef5584
								$db->sql_query($sql);
ef5584
ef5584
								$cache->destroy('sql', STYLES_THEME_TABLE);
ef5584
ef5584
								add_log('admin', 'LOG_THEME_REFRESHED', $theme_row['theme_name']);
ef5584
								trigger_error($user->lang['THEME_REFRESHED'] . adm_back_link($this->u_action));
ef5584
							}
ef5584
						}
ef5584
						else
ef5584
						{
ef5584
							confirm_box(false, $user->lang['CONFIRM_THEME_REFRESH'], build_hidden_fields(array(
ef5584
								'i'			=> $id,
ef5584
								'mode'		=> $mode,
ef5584
								'action'	=> $action,
ef5584
								'id'		=> $style_id
ef5584
							)));
ef5584
						}
ef5584
					break;
ef5584
				}
ef5584
ef5584
				$this->frontend('theme', array('edit', 'details'), array('refresh', 'export', 'delete'));
ef5584
			break;
ef5584
ef5584
			case 'imageset':
ef5584
ef5584
				switch ($action)
ef5584
				{
ef5584
					case 'refresh':
ef5584
ef5584
						$sql = 'SELECT *
ef5584
							FROM ' . STYLES_IMAGESET_TABLE . "
ef5584
							WHERE imageset_id = $style_id";
ef5584
						$result = $db->sql_query($sql);
ef5584
						$imageset_row = $db->sql_fetchrow($result);
ef5584
						$db->sql_freeresult($result);
ef5584
ef5584
						if (!$imageset_row)
ef5584
						{
ef5584
							trigger_error($user->lang['NO_IMAGESET'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
						}
ef5584
ef5584
						if (confirm_box(true))
ef5584
						{
ef5584
							$sql_ary = array();
ef5584
ef5584
							$imageset_definitions = array();
ef5584
							foreach ($this->imageset_keys as $topic => $key_array)
ef5584
							{
ef5584
								$imageset_definitions = array_merge($imageset_definitions, $key_array);
ef5584
							}
ef5584
ef5584
							$cfg_data_imageset = parse_cfg_file("{$phpbb_root_path}styles/{$imageset_row['imageset_path']}/imageset/imageset.cfg");
ef5584
ef5584
							$db->sql_transaction('begin');
ef5584
ef5584
							$sql = 'DELETE FROM ' . STYLES_IMAGESET_DATA_TABLE . '
ef5584
								WHERE imageset_id = ' . $style_id;
ef5584
							$result = $db->sql_query($sql);
ef5584
ef5584
							foreach ($cfg_data_imageset as $image_name => $value)
ef5584
							{
ef5584
								if (strpos($value, '*') !== false)
ef5584
								{
ef5584
									if (substr($value, -1, 1) === '*')
ef5584
									{
ef5584
										list($image_filename, $image_height) = explode('*', $value);
ef5584
										$image_width = 0;
ef5584
									}
ef5584
									else
ef5584
									{
ef5584
										list($image_filename, $image_height, $image_width) = explode('*', $value);
ef5584
									}
ef5584
								}
ef5584
								else
ef5584
								{
ef5584
									$image_filename = $value;
ef5584
									$image_height = $image_width = 0;
ef5584
								}
ef5584
ef5584
								if (strpos($image_name, 'img_') === 0 && $image_filename)
ef5584
								{
ef5584
									$image_name = substr($image_name, 4);
ef5584
									if (in_array($image_name, $imageset_definitions))
ef5584
									{
ef5584
										$sql_ary[] = array(
ef5584
											'image_name'		=> (string) $image_name,
ef5584
											'image_filename'	=> (string) $image_filename,
ef5584
											'image_height'		=> (int) $image_height,
ef5584
											'image_width'		=> (int) $image_width,
ef5584
											'imageset_id'		=> (int) $style_id,
ef5584
											'image_lang'		=> '',
ef5584
										);
ef5584
									}
ef5584
								}
ef5584
							}
ef5584
ef5584
							$sql = 'SELECT lang_dir
ef5584
								FROM ' . LANG_TABLE;
ef5584
							$result = $db->sql_query($sql);
ef5584
ef5584
							while ($row = $db->sql_fetchrow($result))
ef5584
							{
ef5584
								if (@file_exists("{$phpbb_root_path}styles/{$imageset_row['imageset_path']}/imageset/{$row['lang_dir']}/imageset.cfg"))
ef5584
								{
ef5584
									$cfg_data_imageset_data = parse_cfg_file("{$phpbb_root_path}styles/{$imageset_row['imageset_path']}/imageset/{$row['lang_dir']}/imageset.cfg");
ef5584
									foreach ($cfg_data_imageset_data as $image_name => $value)
ef5584
									{
ef5584
										if (strpos($value, '*') !== false)
ef5584
										{
ef5584
											if (substr($value, -1, 1) === '*')
ef5584
											{
ef5584
												list($image_filename, $image_height) = explode('*', $value);
ef5584
												$image_width = 0;
ef5584
											}
ef5584
											else
ef5584
											{
ef5584
												list($image_filename, $image_height, $image_width) = explode('*', $value);
ef5584
											}
ef5584
										}
ef5584
										else
ef5584
										{
ef5584
											$image_filename = $value;
ef5584
											$image_height = $image_width = 0;
ef5584
										}
ef5584
ef5584
										if (strpos($image_name, 'img_') === 0 && $image_filename)
ef5584
										{
ef5584
											$image_name = substr($image_name, 4);
ef5584
											if (in_array($image_name, $imageset_definitions))
ef5584
											{
ef5584
												$sql_ary[] = array(
ef5584
													'image_name'		=> (string) $image_name,
ef5584
													'image_filename'	=> (string) $image_filename,
ef5584
													'image_height'		=> (int) $image_height,
ef5584
													'image_width'		=> (int) $image_width,
ef5584
													'imageset_id'		=> (int) $style_id,
ef5584
													'image_lang'		=> (string) $row['lang_dir'],
ef5584
												);
ef5584
											}
ef5584
										}
ef5584
									}
ef5584
								}
ef5584
							}
ef5584
							$db->sql_freeresult($result);
ef5584
ef5584
							$db->sql_multi_insert(STYLES_IMAGESET_DATA_TABLE, $sql_ary);
ef5584
ef5584
							$db->sql_transaction('commit');
ef5584
ef5584
							$cache->destroy('sql', STYLES_IMAGESET_DATA_TABLE);
ef5584
ef5584
							add_log('admin', 'LOG_IMAGESET_REFRESHED', $imageset_row['imageset_name']);
ef5584
							trigger_error($user->lang['IMAGESET_REFRESHED'] . adm_back_link($this->u_action));
ef5584
						}
ef5584
						else
ef5584
						{
ef5584
							confirm_box(false, $user->lang['CONFIRM_IMAGESET_REFRESH'], build_hidden_fields(array(
ef5584
								'i'			=> $id,
ef5584
								'mode'		=> $mode,
ef5584
								'action'	=> $action,
ef5584
								'id'		=> $style_id
ef5584
							)));
ef5584
						}
ef5584
					break;
ef5584
				}
ef5584
ef5584
				$this->frontend('imageset', array('edit', 'details'), array('refresh', 'export', 'delete'));
ef5584
			break;
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build Frontend with supplied options
ef5584
	*/
ef5584
	function frontend($mode, $options, $actions)
ef5584
	{
ef5584
		global $user, $template, $db, $config, $phpbb_root_path, $phpEx;
ef5584
ef5584
		$sql_from = '';
ef5584
		$style_count = array();
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'style':
ef5584
				$sql_from = STYLES_TABLE;
ef5584
ef5584
				$sql = 'SELECT user_style, COUNT(user_style) AS style_count
ef5584
					FROM ' . USERS_TABLE . '
ef5584
					GROUP BY user_style';
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$style_count[$row['user_style']] = $row['style_count'];
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
			break;
ef5584
ef5584
			case 'template':
ef5584
				$sql_from = STYLES_TEMPLATE_TABLE;
ef5584
			break;
ef5584
ef5584
			case 'theme':
ef5584
				$sql_from = STYLES_THEME_TABLE;
ef5584
			break;
ef5584
ef5584
			case 'imageset':
ef5584
				$sql_from = STYLES_IMAGESET_TABLE;
ef5584
			break;
ef5584
		}
ef5584
ef5584
		$l_prefix = strtoupper($mode);
ef5584
ef5584
		$this->page_title = 'ACP_' . $l_prefix . 'S';
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'S_FRONTEND'		=> true,
ef5584
			'S_STYLE'			=> ($mode == 'style') ? true : false,
ef5584
ef5584
			'L_TITLE'			=> $user->lang[$this->page_title],
ef5584
			'L_EXPLAIN'			=> $user->lang[$this->page_title . '_EXPLAIN'],
ef5584
			'L_NAME'			=> $user->lang[$l_prefix . '_NAME'],
ef5584
			'L_INSTALLED'		=> $user->lang['INSTALLED_' . $l_prefix],
ef5584
			'L_UNINSTALLED'		=> $user->lang['UNINSTALLED_' . $l_prefix],
ef5584
			'L_NO_UNINSTALLED'	=> $user->lang['NO_UNINSTALLED_' . $l_prefix],
ef5584
			'L_CREATE'			=> $user->lang['CREATE_' . $l_prefix],
ef5584
ef5584
			'U_ACTION'			=> $this->u_action,
ef5584
			)
ef5584
		);
ef5584
ef5584
		$sql = "SELECT *
ef5584
			FROM $sql_from";
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$installed = array();
ef5584
ef5584
		$basis_options = '<option class="sep" value="">' . $user->lang['OPTIONAL_BASIS'] . '</option>';
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$installed[] = $row[$mode . '_name'];
ef5584
			$basis_options .= '<option value="' . $row[$mode . '_id'] . '">' . $row[$mode . '_name'] . '</option>';
ef5584
ef5584
			$stylevis = ($mode == 'style' && !$row['style_active']) ? 'activate' : 'deactivate';
ef5584
ef5584
			$s_options = array();
ef5584
			foreach ($options as $option)
ef5584
			{
ef5584
				$s_options[] = '' . $user->lang[strtoupper($option)] . '';
ef5584
			}
ef5584
ef5584
			$s_actions = array();
ef5584
			foreach ($actions as $option)
ef5584
			{
ef5584
				$s_actions[] = '' . $user->lang[strtoupper($option)] . '';
ef5584
			}
ef5584
ef5584
			$template->assign_block_vars('installed', array(
ef5584
				'S_DEFAULT_STYLE'		=> ($mode == 'style' && $row['style_id'] == $config['default_style']) ? true : false,
ef5584
				'U_EDIT'				=> $this->u_action . '&action=' . (($mode == 'style') ? 'details' : 'edit') . '&id=' . $row[$mode . '_id'],
ef5584
				'U_STYLE_ACT_DEACT'		=> $this->u_action . '&action=' . $stylevis . '&id=' . $row[$mode . '_id'],
ef5584
				'L_STYLE_ACT_DEACT'		=> $user->lang['STYLE_' . strtoupper($stylevis)],
ef5584
				'S_OPTIONS'				=> implode(' | ', $s_options),
ef5584
				'S_ACTIONS'				=> implode(' | ', $s_actions),
ef5584
				'U_PREVIEW'				=> ($mode == 'style') ? append_sid("{$phpbb_root_path}index.$phpEx", "$mode=" . $row[$mode . '_id']) : '',
ef5584
ef5584
				'NAME'					=> $row[$mode . '_name'],
ef5584
				'STYLE_COUNT'			=> ($mode == 'style' && isset($style_count[$row['style_id']])) ? $style_count[$row['style_id']] : 0,
ef5584
				)
ef5584
			);
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		// Grab uninstalled items
ef5584
		$new_ary = $cfg = array();
ef5584
ef5584
		$dp = @opendir("{$phpbb_root_path}styles");
ef5584
ef5584
		if ($dp)
ef5584
		{
ef5584
			while (($file = readdir($dp)) !== false)
ef5584
			{
ef5584
				$subpath = ($mode != 'style') ? "$mode/" : '';
ef5584
				if ($file[0] != '.' && file_exists("{$phpbb_root_path}styles/$file/$subpath$mode.cfg"))
ef5584
				{
ef5584
					if ($cfg = file("{$phpbb_root_path}styles/$file/$subpath$mode.cfg"))
ef5584
					{
ef5584
						$items = parse_cfg_file('', $cfg);
ef5584
						$name = (isset($items['name'])) ? trim($items['name']) : false;
ef5584
ef5584
						if ($name && !in_array($name, $installed))
ef5584
						{
ef5584
							$new_ary[] = array(
ef5584
								'path'		=> $file,
ef5584
								'name'		=> $name,
ef5584
								'copyright'	=> $items['copyright'],
ef5584
							);
ef5584
						}
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
			closedir($dp);
ef5584
		}
ef5584
ef5584
		unset($installed);
ef5584
ef5584
		if (sizeof($new_ary))
ef5584
		{
ef5584
			foreach ($new_ary as $cfg)
ef5584
			{
ef5584
				$template->assign_block_vars('uninstalled', array(
ef5584
					'NAME'			=> $cfg['name'],
ef5584
					'COPYRIGHT'		=> $cfg['copyright'],
ef5584
					'U_INSTALL'		=> $this->u_action . '&action=install&path=' . urlencode($cfg['path']))
ef5584
				);
ef5584
			}
ef5584
		}
ef5584
		unset($new_ary);
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'S_BASIS_OPTIONS'		=> $basis_options)
ef5584
		);
ef5584
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Provides a template editor which allows saving changes to template files on the filesystem or in the database.
ef5584
	*
ef5584
	* @param int $template_id specifies which template set is being edited
ef5584
	*/
ef5584
	function edit_template($template_id)
ef5584
	{
ef5584
		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template, $safe_mode;
ef5584
ef5584
		if (defined('PHPBB_DISABLE_ACP_EDITOR'))
ef5584
		{
ef5584
			trigger_error($user->lang['EDITOR_DISABLED'] . adm_back_link($this->u_action));
ef5584
		}
ef5584
ef5584
		$this->page_title = 'EDIT_TEMPLATE';
ef5584
ef5584
		$filelist = $filelist_cats = array();
ef5584
ef5584
		$template_data	= utf8_normalize_nfc(request_var('template_data', '', true));
ef5584
		$template_data	= htmlspecialchars_decode($template_data);
ef5584
		$template_file	= utf8_normalize_nfc(request_var('template_file', '', true));
ef5584
		$text_rows		= max(5, min(999, request_var('text_rows', 20)));
ef5584
		$save_changes	= (isset($_POST['save'])) ? true : false;
ef5584
ef5584
		// make sure template_file path doesn't go upwards
ef5584
		$template_file = str_replace('..', '.', $template_file);
ef5584
ef5584
		// Retrieve some information about the template
ef5584
		$sql = 'SELECT template_storedb, template_path, template_name
ef5584
			FROM ' . STYLES_TEMPLATE_TABLE . "
ef5584
			WHERE template_id = $template_id";
ef5584
		$result = $db->sql_query($sql);
ef5584
		$template_info = $db->sql_fetchrow($result);
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		if (!$template_info)
ef5584
		{
ef5584
			trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		if ($save_changes && !check_form_key('acp_styles'))
ef5584
		{
ef5584
			trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
		else if (!$save_changes)
ef5584
		{
ef5584
			add_form_key('acp_styles');
ef5584
		}
ef5584
ef5584
		// save changes to the template if the user submitted any
ef5584
		if ($save_changes && $template_file)
ef5584
		{
ef5584
			// Get the filesystem location of the current file
ef5584
			$file = "{$phpbb_root_path}styles/{$template_info['template_path']}/template/$template_file";
ef5584
			$additional = '';
ef5584
ef5584
			// If the template is stored on the filesystem try to write the file else store it in the database
ef5584
			if (!$safe_mode && !$template_info['template_storedb'] && file_exists($file) && @is_writable($file))
ef5584
			{
ef5584
				if (!($fp = @fopen($file, 'wb')))
ef5584
				{
ef5584
					trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
				}
ef5584
				fwrite($fp, $template_data);
ef5584
				fclose($fp);
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$db->sql_transaction('begin');
ef5584
ef5584
				// If it's not stored in the db yet, then update the template setting and store all template files in the db
ef5584
				if (!$template_info['template_storedb'])
ef5584
				{
ef5584
					if ($this->get_super('template', $template_id))
ef5584
					{
ef5584
						$this->store_in_db('template', $super['template_id']);
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						$this->store_in_db('template', $template_id);
ef5584
					}
ef5584
ef5584
					add_log('admin', 'LOG_TEMPLATE_EDIT_DETAILS', $template_info['template_name']);
ef5584
					$additional .= '
' . $user->lang['EDIT_TEMPLATE_STORED_DB'];
ef5584
				}
ef5584
ef5584
				// Update the template_data table entry for this template file
ef5584
				$sql = 'UPDATE ' . STYLES_TEMPLATE_DATA_TABLE . "
ef5584
					SET template_data = '" . $db->sql_escape($template_data) . "', template_mtime = " . time() . "
ef5584
					WHERE template_id = $template_id
ef5584
						AND template_filename = '" . $db->sql_escape($template_file) . "'";
ef5584
				$db->sql_query($sql);
ef5584
ef5584
				$db->sql_transaction('commit');
ef5584
			}
ef5584
ef5584
			// destroy the cached version of the template (filename without extension)
ef5584
			$this->clear_template_cache($template_info, array(substr($template_file, 0, -5)));
ef5584
ef5584
			$cache->destroy('sql', STYLES_TABLE);
ef5584
ef5584
			add_log('admin', 'LOG_TEMPLATE_EDIT', $template_info['template_name'], $template_file);
ef5584
			trigger_error($user->lang['TEMPLATE_FILE_UPDATED'] . $additional . adm_back_link($this->u_action . "&action=edit&id=$template_id&text_rows=$text_rows&template_file=$template_file"));
ef5584
		}
ef5584
ef5584
		// Generate a category array containing template filenames
ef5584
		if (!$template_info['template_storedb'])
ef5584
		{
ef5584
			$template_path = "{$phpbb_root_path}styles/{$template_info['template_path']}/template";
ef5584
ef5584
			$filelist = filelist($template_path, '', 'html');
ef5584
			$filelist[''] = array_diff($filelist[''], array('bbcode.html'));
ef5584
ef5584
			if ($template_file)
ef5584
			{
ef5584
				if (!file_exists($template_path . "/$template_file") || !($template_data = file_get_contents($template_path . "/$template_file")))
ef5584
				{
ef5584
					trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$sql = 'SELECT *
ef5584
				FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
ef5584
				WHERE template_id = $template_id";
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			$filelist = array('' => array());
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$file_info = pathinfo($row['template_filename']);
ef5584
ef5584
				if (($file_info['basename'] != 'bbcode') && ($file_info['extension'] == 'html'))
ef5584
				{
ef5584
					if (($file_info['dirname'] == '.') || empty($file_info['dirname']))
ef5584
					{
ef5584
						$filelist[''][] = $row['template_filename'];
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						$filelist[$file_info['dirname'] . '/'][] = $file_info['basename'];
ef5584
					}
ef5584
				}
ef5584
ef5584
				if ($row['template_filename'] == $template_file)
ef5584
				{
ef5584
					$template_data = $row['template_data'];
ef5584
				}
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
			unset($file_info);
ef5584
		}
ef5584
ef5584
		// Now create the categories
ef5584
		$filelist_cats[''] = array();
ef5584
		foreach ($filelist as $pathfile => $file_ary)
ef5584
		{
ef5584
			// Use the directory name as category name
ef5584
			if (!empty($pathfile))
ef5584
			{
ef5584
				$filelist_cats[$pathfile] = array();
ef5584
				foreach ($file_ary as $file)
ef5584
				{
ef5584
					$filelist_cats[$pathfile][$pathfile . $file] = $file;
ef5584
				}
ef5584
			}
ef5584
			// or if it's in the main category use the word before the first underscore to group files
ef5584
			else
ef5584
			{
ef5584
				$cats = array();
ef5584
				foreach ($file_ary as $file)
ef5584
				{
ef5584
					$cats[] = substr($file, 0, strpos($file, '_'));
ef5584
					$filelist_cats[substr($file, 0, strpos($file, '_'))][$file] = $file;
ef5584
				}
ef5584
ef5584
				$cats = array_values(array_unique($cats));
ef5584
ef5584
				// we don't need any single element categories so put them into the misc '' category
ef5584
				for ($i = 0, $n = sizeof($cats); $i < $n; $i++)
ef5584
				{
ef5584
					if (sizeof($filelist_cats[$cats[$i]]) == 1 && $cats[$i] !== '')
ef5584
					{
ef5584
						$filelist_cats[''][key($filelist_cats[$cats[$i]])] = current($filelist_cats[$cats[$i]]);
ef5584
						unset($filelist_cats[$cats[$i]]);
ef5584
					}
ef5584
				}
ef5584
				unset($cats);
ef5584
			}
ef5584
		}
ef5584
		unset($filelist);
ef5584
ef5584
		// Generate list of categorised template files
ef5584
		$tpl_options = '';
ef5584
		ksort($filelist_cats);
ef5584
		foreach ($filelist_cats as $category => $tpl_ary)
ef5584
		{
ef5584
			ksort($tpl_ary);
ef5584
ef5584
			if (!empty($category))
ef5584
			{
ef5584
				$tpl_options .= '<option class="sep" value="">' . $category . '</option>';
ef5584
			}
ef5584
ef5584
			foreach ($tpl_ary as $filename => $file)
ef5584
			{
ef5584
				$selected = ($template_file == $filename) ? ' selected="selected"' : '';
ef5584
				$tpl_options .= '<option value="' . $filename . '"' . $selected . '>' . $file . '</option>';
ef5584
			}
ef5584
		}
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'S_EDIT_TEMPLATE'	=> true,
ef5584
			'S_HIDDEN_FIELDS'	=> build_hidden_fields(array('template_file' => $template_file)),
ef5584
			'S_TEMPLATES'		=> $tpl_options,
ef5584
ef5584
			'U_ACTION'			=> $this->u_action . "&action=edit&id=$template_id&text_rows=$text_rows",
ef5584
			'U_BACK'			=> $this->u_action,
ef5584
ef5584
			'L_EDIT'			=> $user->lang['EDIT_TEMPLATE'],
ef5584
			'L_EDIT_EXPLAIN'	=> $user->lang['EDIT_TEMPLATE_EXPLAIN'],
ef5584
			'L_EDITOR'			=> $user->lang['TEMPLATE_EDITOR'],
ef5584
			'L_EDITOR_HEIGHT'	=> $user->lang['TEMPLATE_EDITOR_HEIGHT'],
ef5584
			'L_FILE'			=> $user->lang['TEMPLATE_FILE'],
ef5584
			'L_SELECT'			=> $user->lang['SELECT_TEMPLATE'],
ef5584
			'L_SELECTED'		=> $user->lang['SELECTED_TEMPLATE'],
ef5584
			'L_SELECTED_FILE'	=> $user->lang['SELECTED_TEMPLATE_FILE'],
ef5584
ef5584
			'SELECTED_TEMPLATE'	=> $template_info['template_name'],
ef5584
			'TEMPLATE_FILE'		=> $template_file,
ef5584
			'TEMPLATE_DATA'		=> utf8_htmlspecialchars($template_data),
ef5584
			'TEXT_ROWS'			=> $text_rows)
ef5584
		);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Allows the admin to view cached versions of template files and clear single template cache files
ef5584
	*
ef5584
	* @param int $template_id specifies which template's cache is shown
ef5584
	*/
ef5584
	function template_cache($template_id)
ef5584
	{
ef5584
		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template;
ef5584
ef5584
		$source		= str_replace('/', '.', request_var('source', ''));
ef5584
		$file_ary	= array_diff(request_var('delete', array('')), array(''));
ef5584
		$submit		= isset($_POST['submit']) ? true : false;
ef5584
ef5584
		$sql = 'SELECT *
ef5584
			FROM ' . STYLES_TEMPLATE_TABLE . "
ef5584
			WHERE template_id = $template_id";
ef5584
		$result = $db->sql_query($sql);
ef5584
		$template_row = $db->sql_fetchrow($result);
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		if (!$template_row)
ef5584
		{
ef5584
			trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		// User wants to delete one or more files ...
ef5584
		if ($submit && $file_ary)
ef5584
		{
ef5584
			$this->clear_template_cache($template_row, $file_ary);
ef5584
			trigger_error($user->lang['TEMPLATE_CACHE_CLEARED'] . adm_back_link($this->u_action . "&action=cache&id=$template_id"));
ef5584
		}
ef5584
ef5584
		$cache_prefix = 'tpl_' . str_replace('_', '-', $template_row['template_path']);
ef5584
ef5584
		// Someone wants to see the cached source ... so we'll highlight it,
ef5584
		// add line numbers and indent it appropriately. This could be nasty
ef5584
		// on larger source files ...
ef5584
		if ($source && file_exists("{$phpbb_root_path}cache/{$cache_prefix}_$source.html.$phpEx"))
ef5584
		{
ef5584
			adm_page_header($user->lang['TEMPLATE_CACHE']);
ef5584
ef5584
			$template->set_filenames(array(
ef5584
				'body'	=> 'viewsource.html')
ef5584
			);
ef5584
ef5584
			$template->assign_vars(array(
ef5584
				'FILENAME'	=> str_replace('.', '/', $source) . '.html')
ef5584
			);
ef5584
ef5584
			$code = str_replace(array("\r\n", "\r"), array("\n", "\n"), file_get_contents("{$phpbb_root_path}cache/{$cache_prefix}_$source.html.$phpEx"));
ef5584
ef5584
			$conf = array('highlight.bg', 'highlight.comment', 'highlight.default', 'highlight.html', 'highlight.keyword', 'highlight.string');
ef5584
			foreach ($conf as $ini_var)
ef5584
			{
ef5584
				@ini_set($ini_var, str_replace('highlight.', 'syntax', $ini_var));
ef5584
			}
ef5584
ef5584
			$marker = 'MARKER' . time();
ef5584
			$code = highlight_string(str_replace("\n", $marker, $code), true);
ef5584
			$code = str_replace($marker, "\n", $code);
ef5584
			$str_from = array('', '', '','[', ']', '.', ':');
ef5584
			$str_to = array('', '', '', '[', ']', '.', ':');
ef5584
ef5584
			$code = str_replace($str_from, $str_to, $code);
ef5584
			$code = preg_replace('#^()\n?(.*?)\n?()$#ism', '$1$2$3', $code);
ef5584
			$code = substr($code, strlen(''));
ef5584
			$code = substr($code, 0, -1 * strlen(''));
ef5584
			$code = explode("\n", $code);
ef5584
ef5584
			foreach ($code as $key => $line)
ef5584
			{
ef5584
				$template->assign_block_vars('source', array(
ef5584
					'LINENUM'	=> $key + 1,
ef5584
					'LINE'		=> preg_replace('#([^ ;]) ([^ &])#', '$1 $2', $line))
ef5584
				);
ef5584
				unset($code[$key]);
ef5584
			}
ef5584
ef5584
			adm_page_footer();
ef5584
		}
ef5584
ef5584
		$filemtime = array();
ef5584
		if ($template_row['template_storedb'])
ef5584
		{
ef5584
			$ids = array();
ef5584
			if (isset($template_row['template_inherits_id']) && $template_row['template_inherits_id'])
ef5584
			{
ef5584
				$ids[] = $template_row['template_inherits_id'];
ef5584
			}
ef5584
			$ids[] = $template_row['template_id'];
ef5584
ef5584
			$filemtime 			= array();
ef5584
			$file_template_db	= array();
ef5584
ef5584
			foreach ($ids as $id)
ef5584
			{
ef5584
				$sql = 'SELECT template_filename, template_mtime
ef5584
					FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
ef5584
					WHERE template_id = $id";
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$filemtime[$row['template_filename']] = $row['template_mtime'];
ef5584
					$file_template_db[$row['template_filename']] = $id;
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Get a list of cached template files and then retrieve additional information about them
ef5584
		$file_ary = $this->template_cache_filelist($template_row['template_path']);
ef5584
ef5584
		foreach ($file_ary as $file)
ef5584
		{
ef5584
			$file 		= str_replace('/', '.', $file);
ef5584
ef5584
			// perform some dirty guessing to get the path right.
ef5584
			// We assume that three dots in a row were '../'
ef5584
			$tpl_file 	= str_replace('.', '/', $file);
ef5584
			$tpl_file 	= str_replace('///', '../', $tpl_file);
ef5584
ef5584
			$filename = "{$cache_prefix}_$file.html.$phpEx";
ef5584
ef5584
			if (!file_exists("{$phpbb_root_path}cache/$filename"))
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			$file_tpl = "{$phpbb_root_path}styles/{$template_row['template_path']}/template/$tpl_file.html";
ef5584
			$inherited = false;
ef5584
ef5584
			if (isset($template_row['template_inherits_id']) && $template_row['template_inherits_id'])
ef5584
			{
ef5584
				if (!$template_row['template_storedb'])
ef5584
				{
ef5584
					if (!file_exists($file_tpl))
ef5584
					{
ef5584
						$file_tpl = "{$phpbb_root_path}styles/{$template_row['template_inherit_path']}/template/$tpl_file.html";
ef5584
						$inherited = true;
ef5584
					}
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					if ($file_template_db[$file . '.html'] == $template_row['template_inherits_id'])
ef5584
					{
ef5584
						$file_tpl = "{$phpbb_root_path}styles/{$template_row['template_inherit_path']}/template/$tpl_file.html";
ef5584
						$inherited = true;
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
ef5584
ef5584
			$template->assign_block_vars('file', array(
ef5584
				'U_VIEWSOURCE'	=> $this->u_action . "&action=cache&id=$template_id&source=$file",
ef5584
ef5584
				'CACHED'		=> $user->format_date(filemtime("{$phpbb_root_path}cache/$filename")),
ef5584
				'FILENAME'		=> $file,
ef5584
				'FILENAME_PATH'	=> $file_tpl,
ef5584
				'FILESIZE'		=> sprintf('%.1f ' . $user->lang['KIB'], filesize("{$phpbb_root_path}cache/$filename") / 1024),
ef5584
				'MODIFIED'		=> $user->format_date((!$template_row['template_storedb']) ? filemtime($file_tpl) : $filemtime[$file . '.html']))
ef5584
			);
ef5584
		}
ef5584
		unset($filemtime);
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'S_CACHE'			=> true,
ef5584
			'S_TEMPLATE'		=> true,
ef5584
ef5584
			'U_ACTION'			=> $this->u_action . "&action=cache&id=$template_id",
ef5584
			'U_BACK'			=> $this->u_action)
ef5584
		);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Provides a css editor and a basic easier to use stylesheet editing tool for less experienced (or lazy) users
ef5584
	*
ef5584
	* @param int $theme_id specifies which theme is being edited
ef5584
	*/
ef5584
	function edit_theme($theme_id)
ef5584
	{
ef5584
		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template, $safe_mode;
ef5584
ef5584
		$this->page_title = 'EDIT_THEME';
ef5584
ef5584
		$filelist = $filelist_cats = array();
ef5584
ef5584
		$theme_data		= utf8_normalize_nfc(request_var('template_data', '', true));
ef5584
		$theme_data		= htmlspecialchars_decode($theme_data);
ef5584
		$theme_file		= utf8_normalize_nfc(request_var('template_file', '', true));
ef5584
		$text_rows		= max(5, min(999, request_var('text_rows', 20)));
ef5584
		$save_changes	= (isset($_POST['save'])) ? true : false;
ef5584
ef5584
		// make sure theme_file path doesn't go upwards
ef5584
		$theme_file = str_replace('..', '.', $theme_file);
ef5584
ef5584
		// Retrieve some information about the theme
ef5584
		$sql = 'SELECT theme_storedb, theme_path, theme_name, theme_data
ef5584
			FROM ' . STYLES_THEME_TABLE . "
ef5584
			WHERE theme_id = $theme_id";
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		if (!($theme_info = $db->sql_fetchrow($result)))
ef5584
		{
ef5584
			trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		// save changes to the theme if the user submitted any
ef5584
		if ($save_changes)
ef5584
		{
ef5584
			// Get the filesystem location of the current file
ef5584
			$file = "{$phpbb_root_path}styles/{$theme_info['theme_path']}/theme/$theme_file";
ef5584
			$additional = '';
ef5584
			$message = $user->lang['THEME_UPDATED'];
ef5584
ef5584
			// If the theme is stored on the filesystem try to write the file else store it in the database
ef5584
			if (!$safe_mode && !$theme_info['theme_storedb'] && file_exists($file) && @is_writable($file))
ef5584
			{
ef5584
				if (!($fp = @fopen($file, 'wb')))
ef5584
				{
ef5584
					trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
				}
ef5584
				fwrite($fp, $theme_data);
ef5584
				fclose($fp);
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				// Write stylesheet to db
ef5584
				$sql_ary = array(
ef5584
					'theme_mtime'		=> time(),
ef5584
					'theme_storedb'		=> 1,
ef5584
					'theme_data'		=> $this->db_theme_data($theme_info, $theme_data),
ef5584
				);
ef5584
				$sql = 'UPDATE ' . STYLES_THEME_TABLE . '
ef5584
					SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
ef5584
					WHERE theme_id = ' . $theme_id;
ef5584
				$db->sql_query($sql);
ef5584
ef5584
				$cache->destroy('sql', STYLES_THEME_TABLE);
ef5584
ef5584
				// notify the user if the theme was not stored in the db before his modification
ef5584
				if (!$theme_info['theme_storedb'])
ef5584
				{
ef5584
					add_log('admin', 'LOG_THEME_EDIT_DETAILS', $theme_info['theme_name']);
ef5584
					$message .= '
' . $user->lang['EDIT_THEME_STORED_DB'];
ef5584
				}
ef5584
			}
ef5584
			$cache->destroy('sql', STYLES_THEME_TABLE);
ef5584
			add_log('admin', (!$theme_info['theme_storedb']) ? 'LOG_THEME_EDIT_FILE' : 'LOG_THEME_EDIT', $theme_info['theme_name'], (!$theme_info['theme_storedb']) ? $theme_file : '');
ef5584
ef5584
			trigger_error($message . adm_back_link($this->u_action . "&action=edit&id=$theme_id&template_file=$theme_file&text_rows=$text_rows"));
ef5584
		}
ef5584
ef5584
		// Generate a category array containing theme filenames
ef5584
		if (!$theme_info['theme_storedb'])
ef5584
		{
ef5584
			$theme_path = "{$phpbb_root_path}styles/{$theme_info['theme_path']}/theme";
ef5584
ef5584
			$filelist = filelist($theme_path, '', 'css');
ef5584
ef5584
			if ($theme_file)
ef5584
			{
ef5584
				if (!file_exists($theme_path . "/$theme_file") || !($theme_data = file_get_contents($theme_path . "/$theme_file")))
ef5584
				{
ef5584
					trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$theme_data = &$theme_info['theme_data'];
ef5584
		}
ef5584
ef5584
		// Now create the categories
ef5584
		$filelist_cats[''] = array();
ef5584
		foreach ($filelist as $pathfile => $file_ary)
ef5584
		{
ef5584
			// Use the directory name as category name
ef5584
			if (!empty($pathfile))
ef5584
			{
ef5584
				$filelist_cats[$pathfile] = array();
ef5584
				foreach ($file_ary as $file)
ef5584
				{
ef5584
					$filelist_cats[$pathfile][$pathfile . $file] = $file;
ef5584
				}
ef5584
			}
ef5584
			// or if it's in the main category use the word before the first underscore to group files
ef5584
			else
ef5584
			{
ef5584
				$cats = array();
ef5584
				foreach ($file_ary as $file)
ef5584
				{
ef5584
					$cats[] = substr($file, 0, strpos($file, '_'));
ef5584
					$filelist_cats[substr($file, 0, strpos($file, '_'))][$file] = $file;
ef5584
				}
ef5584
ef5584
				$cats = array_values(array_unique($cats));
ef5584
ef5584
				// we don't need any single element categories so put them into the misc '' category
ef5584
				for ($i = 0, $n = sizeof($cats); $i < $n; $i++)
ef5584
				{
ef5584
					if (sizeof($filelist_cats[$cats[$i]]) == 1 && $cats[$i] !== '')
ef5584
					{
ef5584
						$filelist_cats[''][key($filelist_cats[$cats[$i]])] = current($filelist_cats[$cats[$i]]);
ef5584
						unset($filelist_cats[$cats[$i]]);
ef5584
					}
ef5584
				}
ef5584
				unset($cats);
ef5584
			}
ef5584
		}
ef5584
		unset($filelist);
ef5584
ef5584
		// Generate list of categorised theme files
ef5584
		$tpl_options = '';
ef5584
		ksort($filelist_cats);
ef5584
		foreach ($filelist_cats as $category => $tpl_ary)
ef5584
		{
ef5584
			ksort($tpl_ary);
ef5584
ef5584
			if (!empty($category))
ef5584
			{
ef5584
				$tpl_options .= '<option class="sep" value="">' . $category . '</option>';
ef5584
			}
ef5584
ef5584
			foreach ($tpl_ary as $filename => $file)
ef5584
			{
ef5584
				$selected = ($theme_file == $filename) ? ' selected="selected"' : '';
ef5584
				$tpl_options .= '<option value="' . $filename . '"' . $selected . '>' . $file . '</option>';
ef5584
			}
ef5584
		}
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'S_EDIT_THEME'		=> true,
ef5584
			'S_HIDDEN_FIELDS'	=> build_hidden_fields(array('template_file' => $theme_file)),
ef5584
			'S_THEME_IN_DB'		=> $theme_info['theme_storedb'],
ef5584
			'S_TEMPLATES'		=> $tpl_options,
ef5584
ef5584
			'U_ACTION'			=> $this->u_action . "&action=edit&id=$theme_id&text_rows=$text_rows",
ef5584
			'U_BACK'			=> $this->u_action,
ef5584
ef5584
			'L_EDIT'			=> $user->lang['EDIT_THEME'],
ef5584
			'L_EDIT_EXPLAIN'	=> $user->lang['EDIT_THEME_EXPLAIN'],
ef5584
			'L_EDITOR'			=> $user->lang['THEME_EDITOR'],
ef5584
			'L_EDITOR_HEIGHT'	=> $user->lang['THEME_EDITOR_HEIGHT'],
ef5584
			'L_FILE'			=> $user->lang['THEME_FILE'],
ef5584
			'L_SELECT'			=> $user->lang['SELECT_THEME'],
ef5584
			'L_SELECTED'		=> $user->lang['SELECTED_THEME'],
ef5584
			'L_SELECTED_FILE'	=> $user->lang['SELECTED_THEME_FILE'],
ef5584
ef5584
			'SELECTED_TEMPLATE'	=> $theme_info['theme_name'],
ef5584
			'TEMPLATE_FILE'		=> $theme_file,
ef5584
			'TEMPLATE_DATA'		=> utf8_htmlspecialchars($theme_data),
ef5584
			'TEXT_ROWS'			=> $text_rows)
ef5584
		);
ef5584
	}
ef5584
ef5584
ef5584
	/**
ef5584
	* Edit imagesets
ef5584
	*
ef5584
	* @param int $imageset_id specifies which imageset is being edited
ef5584
	*/
ef5584
	function edit_imageset($imageset_id)
ef5584
	{
ef5584
		global $db, $user, $phpbb_root_path, $cache, $template;
ef5584
ef5584
		$this->page_title = 'EDIT_IMAGESET';
ef5584
ef5584
		$update		= (isset($_POST['update'])) ? true : false;
ef5584
ef5584
		$imgname	= request_var('imgname', '');
ef5584
		$imgpath	= request_var('imgpath', '');
ef5584
		$imgsize	= request_var('imgsize', false);
ef5584
		$imgwidth	= request_var('imgwidth', 0);
ef5584
		$imgheight	= request_var('imgheight', 0);
ef5584
ef5584
		$imgname	= preg_replace('#[^a-z0-9\-+_]#i', '', $imgname);
ef5584
		$imgpath	= str_replace('..', '.', $imgpath);
ef5584
ef5584
		if ($imageset_id)
ef5584
		{
ef5584
			$sql = 'SELECT imageset_path, imageset_name
ef5584
				FROM ' . STYLES_IMAGESET_TABLE . "
ef5584
				WHERE imageset_id = $imageset_id";
ef5584
			$result = $db->sql_query($sql);
ef5584
			$imageset_row = $db->sql_fetchrow($result);
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			$imageset_path		= $imageset_row['imageset_path'];
ef5584
			$imageset_name		= $imageset_row['imageset_name'];
ef5584
ef5584
			$sql_extra = '';
ef5584
			if (strpos($imgname, '-') !== false)
ef5584
			{
ef5584
				list($imgname, $imgnamelang) = explode('-', $imgname);
ef5584
				$sql_extra = " AND image_lang IN ('" . $db->sql_escape($imgnamelang) . "', '')";
ef5584
			}
ef5584
ef5584
			$sql = 'SELECT image_filename, image_width, image_height, image_lang, image_id
ef5584
				FROM ' . STYLES_IMAGESET_DATA_TABLE . "
ef5584
				WHERE imageset_id = $imageset_id
ef5584
					AND image_name = '" . $db->sql_escape($imgname) . "'$sql_extra";
ef5584
			$result = $db->sql_query($sql);
ef5584
			$imageset_data_row = $db->sql_fetchrow($result);
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			$image_filename	= $imageset_data_row['image_filename'];
ef5584
			$image_width	= $imageset_data_row['image_width'];
ef5584
			$image_height	= $imageset_data_row['image_height'];
ef5584
			$image_lang		= $imageset_data_row['image_lang'];
ef5584
			$image_id		= $imageset_data_row['image_id'];
ef5584
ef5584
			if (!$imageset_row)
ef5584
			{
ef5584
				trigger_error($user->lang['NO_IMAGESET'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
			}
ef5584
ef5584
			// Check to see whether the selected image exists in the table
ef5584
			$valid_name = ($update) ? false : true;
ef5584
ef5584
			foreach ($this->imageset_keys as $category => $img_ary)
ef5584
			{
ef5584
				if (in_array($imgname, $img_ary))
ef5584
				{
ef5584
					$valid_name = true;
ef5584
					break;
ef5584
				}
ef5584
			}
ef5584
ef5584
			if ($update && isset($_POST['imgpath']))
ef5584
			{
ef5584
				if ($valid_name)
ef5584
				{
ef5584
					// If imgwidth and imgheight are non-zero grab the actual size
ef5584
					// from the image itself ... we ignore width settings for the poll center image
ef5584
					$imgwidth	= request_var('imgwidth', 0);
ef5584
					$imgheight	= request_var('imgheight', 0);
ef5584
					$imglang = '';
ef5584
ef5584
					if ($imgpath && !file_exists("{$phpbb_root_path}styles/$imageset_path/imageset/$imgpath"))
ef5584
					{
ef5584
						trigger_error($user->lang['NO_IMAGE_ERROR'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
					}
ef5584
ef5584
					if ($imgsize && $imgpath)
ef5584
					{
ef5584
						if (!$imgwidth || !$imgheight)
ef5584
						{
ef5584
							list($imgwidth_file, $imgheight_file) = getimagesize("{$phpbb_root_path}styles/$imageset_path/imageset/$imgpath");
ef5584
							$imgwidth = ($imgwidth) ? $imgwidth : $imgwidth_file;
ef5584
							$imgheight = ($imgheight) ? $imgheight : $imgheight_file;
ef5584
						}
ef5584
						$imgwidth	= ($imgname != 'poll_center') ? (int) $imgwidth : 0;
ef5584
						$imgheight	= (int) $imgheight;
ef5584
					}
ef5584
ef5584
ef5584
					if (strpos($imgpath, '/') !== false)
ef5584
					{
ef5584
						list($imglang, $imgfilename) = explode('/', $imgpath);
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						$imgfilename = $imgpath;
ef5584
					}
ef5584
ef5584
					$sql_ary = array(
ef5584
						'image_filename'	=> (string) $imgfilename,
ef5584
						'image_width'		=> (int) $imgwidth,
ef5584
						'image_height'		=> (int) $imgheight,
ef5584
						'image_lang'		=> (string) $imglang,
ef5584
					);
ef5584
ef5584
					// already exists
ef5584
					if ($imageset_data_row)
ef5584
					{
ef5584
						$sql = 'UPDATE ' . STYLES_IMAGESET_DATA_TABLE . '
ef5584
							SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
ef5584
							WHERE image_id = $image_id";
ef5584
						$db->sql_query($sql);
ef5584
					}
ef5584
					// does not exist
ef5584
					else if (!$imageset_data_row)
ef5584
					{
ef5584
						$sql_ary['image_name']	= $imgname;
ef5584
						$sql_ary['imageset_id']	= (int) $imageset_id;
ef5584
						$db->sql_query('INSERT INTO ' . STYLES_IMAGESET_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
ef5584
					}
ef5584
ef5584
					$cache->destroy('sql', STYLES_IMAGESET_DATA_TABLE);
ef5584
ef5584
					add_log('admin', 'LOG_IMAGESET_EDIT', $imageset_name);
ef5584
ef5584
					$template->assign_var('SUCCESS', true);
ef5584
ef5584
					$image_filename = $imgfilename;
ef5584
					$image_width	= $imgwidth;
ef5584
					$image_height	= $imgheight;
ef5584
					$image_lang		= $imglang;
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
ef5584
		$imglang = '';
ef5584
		$imagesetlist = array('nolang' => array(), 'lang' => array());
ef5584
		$langs = array();
ef5584
ef5584
		$dir = "{$phpbb_root_path}styles/$imageset_path/imageset";
ef5584
		$dp = @opendir($dir);
ef5584
ef5584
		if ($dp)
ef5584
		{
ef5584
			while (($file = readdir($dp)) !== false)
ef5584
			{
ef5584
				if ($file[0] != '.' && strtoupper($file) != 'CVS' && !is_file($dir . '/' . $file) && !is_link($dir . '/' . $file))
ef5584
				{
ef5584
					$langs[] = $file;
ef5584
				}
ef5584
				else if (preg_match('#\.(?:gif|jpg|png)$#', $file))
ef5584
				{
ef5584
					$imagesetlist['nolang'][] = $file;
ef5584
				}
ef5584
			}
ef5584
ef5584
			if ($sql_extra)
ef5584
			{
ef5584
				$dp2 = @opendir("$dir/$imgnamelang");
ef5584
ef5584
				if ($dp2)
ef5584
				{
ef5584
					while (($file2 = readdir($dp2)) !== false)
ef5584
					{
ef5584
						if (preg_match('#\.(?:gif|jpg|png)$#', $file2))
ef5584
						{
ef5584
							$imagesetlist['lang'][] = "$imgnamelang/$file2";
ef5584
						}
ef5584
					}
ef5584
					closedir($dp2);
ef5584
				}
ef5584
			}
ef5584
			closedir($dp);
ef5584
		}
ef5584
ef5584
		// Generate list of image options
ef5584
		$img_options = '';
ef5584
		foreach ($this->imageset_keys as $category => $img_ary)
ef5584
		{
ef5584
			$template->assign_block_vars('category', array(
ef5584
				'NAME'			=> $user->lang['IMG_CAT_' . strtoupper($category)]
ef5584
			));
ef5584
ef5584
			foreach ($img_ary as $img)
ef5584
			{
ef5584
				if ($category == 'buttons')
ef5584
				{
ef5584
					foreach ($langs as $language)
ef5584
					{
ef5584
						$template->assign_block_vars('category.images', array(
ef5584
							'SELECTED'			=> ($img == $imgname && $language == $imgnamelang),
ef5584
							'VALUE'				=> $img . '-' . $language,
ef5584
							'TEXT'				=> $user->lang['IMG_' . strtoupper($img)] . ' [ ' . $language . ' ]'
ef5584
						));
ef5584
					}
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$template->assign_block_vars('category.images', array(
ef5584
						'SELECTED'			=> ($img == $imgname),
ef5584
						'VALUE'				=> $img,
ef5584
						'TEXT'				=> (($category == 'custom') ? $img : $user->lang['IMG_' . strtoupper($img)])
ef5584
					));
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Make sure the list of possible images is sorted alphabetically
ef5584
		sort($imagesetlist['lang']);
ef5584
		sort($imagesetlist['nolang']);
ef5584
ef5584
		$image_found = false;
ef5584
		$img_val = '';
ef5584
		foreach ($imagesetlist as $type => $img_ary)
ef5584
		{
ef5584
			if ($type !== 'lang' || $sql_extra)
ef5584
			{
ef5584
				$template->assign_block_vars('imagesetlist', array(
ef5584
					'TYPE'	=> ($type == 'lang')
ef5584
				));
ef5584
			}
ef5584
ef5584
			foreach ($img_ary as $img)
ef5584
			{
ef5584
				$imgtext = preg_replace('/^([^\/]+\/)/', '', $img);
ef5584
				$selected = (!empty($imgname) && strpos($image_filename, $imgtext) !== false);
ef5584
				if ($selected)
ef5584
				{
ef5584
					$image_found = true;
ef5584
					$img_val = htmlspecialchars($img);
ef5584
				}
ef5584
				$template->assign_block_vars('imagesetlist.images', array(
ef5584
					'SELECTED'			=> $selected,
ef5584
					'TEXT'				=> $imgtext,
ef5584
					'VALUE'				=> htmlspecialchars($img)
ef5584
				));
ef5584
			}
ef5584
		}
ef5584
ef5584
		$imgsize_bool = (!empty($imgname) && $image_width && $image_height) ? true : false;
ef5584
		$image_request = '../styles/' . $imageset_path . '/imageset/' . ($image_lang ? $imgnamelang . '/' : '') . $image_filename;
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'S_EDIT_IMAGESET'	=> true,
ef5584
			'L_TITLE'			=> $user->lang[$this->page_title],
ef5584
			'L_EXPLAIN'			=> $user->lang[$this->page_title . '_EXPLAIN'],
ef5584
			'IMAGE_OPTIONS'		=> $img_options,
ef5584
			'IMAGE_SIZE'		=> $image_width,
ef5584
			'IMAGE_HEIGHT'		=> $image_height,
ef5584
			'IMAGE_REQUEST'		=> (empty($image_filename)) ? 'images/no_image.png' : $image_request,
ef5584
			'U_ACTION'			=> $this->u_action . "&action=edit&id=$imageset_id",
ef5584
			'U_BACK'			=> $this->u_action,
ef5584
			'NAME'				=> $imageset_name,
ef5584
			'A_NAME'			=> addslashes($imageset_name),
ef5584
			'ERROR'				=> !$valid_name,
ef5584
			'IMG_SRC'			=> ($image_found) ? '../styles/' . $imageset_path . '/imageset/' . $img_val : 'images/no_image.png',
ef5584
			'IMAGE_SELECT'		=> $image_found
ef5584
		));
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Remove style/template/theme/imageset
ef5584
	*/
ef5584
	function remove($mode, $style_id)
ef5584
	{
ef5584
		global $db, $template, $user, $phpbb_root_path, $cache, $config;
ef5584
ef5584
		$new_id = request_var('new_id', 0);
ef5584
		$update = (isset($_POST['update'])) ? true : false;
ef5584
		$sql_where = '';
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'style':
ef5584
				$sql_from = STYLES_TABLE;
ef5584
				$sql_select = 'style_name';
ef5584
				$sql_where = 'AND style_active = 1';
ef5584
			break;
ef5584
ef5584
			case 'template':
ef5584
				$sql_from = STYLES_TEMPLATE_TABLE;
ef5584
				$sql_select = 'template_name, template_path, template_storedb';
ef5584
			break;
ef5584
ef5584
			case 'theme':
ef5584
				$sql_from = STYLES_THEME_TABLE;
ef5584
				$sql_select = 'theme_name, theme_path, theme_storedb';
ef5584
			break;
ef5584
ef5584
			case 'imageset':
ef5584
				$sql_from = STYLES_IMAGESET_TABLE;
ef5584
				$sql_select = 'imageset_name, imageset_path';
ef5584
			break;
ef5584
		}
ef5584
ef5584
		if ($mode === 'template' && ($conflicts = $this->check_inheritance($mode, $style_id)))
ef5584
		{
ef5584
			$l_type = strtoupper($mode);
ef5584
			$msg = $user->lang[$l_type . '_DELETE_DEPENDENT'];
ef5584
			foreach ($conflicts as $id => $values)
ef5584
			{
ef5584
				$msg .= '
' . $values['template_name'];
ef5584
			}
ef5584
ef5584
			trigger_error($msg . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		$l_prefix = strtoupper($mode);
ef5584
ef5584
		$sql = "SELECT $sql_select
ef5584
			FROM $sql_from
ef5584
			WHERE {$mode}_id = $style_id";
ef5584
		$result = $db->sql_query($sql);
ef5584
		$style_row = $db->sql_fetchrow($result);
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		if (!$style_row)
ef5584
		{
ef5584
			trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		$sql = "SELECT {$mode}_id, {$mode}_name
ef5584
			FROM $sql_from
ef5584
			WHERE {$mode}_id <> $style_id
ef5584
			$sql_where
ef5584
			ORDER BY {$mode}_name ASC";
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$s_options = '';
ef5584
ef5584
		if ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			do
ef5584
			{
ef5584
				$s_options .= '<option value="' . $row[$mode . '_id'] . '">' . $row[$mode . '_name'] . '</option>';
ef5584
			}
ef5584
			while ($row = $db->sql_fetchrow($result));
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			trigger_error($user->lang['ONLY_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		if ($update)
ef5584
		{
ef5584
			$sql = "DELETE FROM $sql_from
ef5584
				WHERE {$mode}_id = $style_id";
ef5584
			$db->sql_query($sql);
ef5584
ef5584
			if ($mode == 'style')
ef5584
			{
ef5584
				$sql = 'UPDATE ' . USERS_TABLE . "
ef5584
					SET user_style = $new_id
ef5584
					WHERE user_style = $style_id";
ef5584
				$db->sql_query($sql);
ef5584
ef5584
				$sql = 'UPDATE ' . FORUMS_TABLE . "
ef5584
					SET forum_style = $new_id
ef5584
					WHERE forum_style = $style_id";
ef5584
				$db->sql_query($sql);
ef5584
ef5584
				if ($style_id == $config['default_style'])
ef5584
				{
ef5584
					set_config('default_style', $new_id);
ef5584
				}
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				if ($mode == 'imageset')
ef5584
				{
ef5584
					$sql = 'DELETE FROM ' . STYLES_IMAGESET_DATA_TABLE . "
ef5584
						WHERE imageset_id = $style_id";
ef5584
					$db->sql_query($sql);
ef5584
				}
ef5584
				$sql = 'UPDATE ' . STYLES_TABLE . "
ef5584
					SET {$mode}_id = $new_id
ef5584
					WHERE {$mode}_id = $style_id";
ef5584
				$db->sql_query($sql);
ef5584
			}
ef5584
ef5584
			$cache->destroy('sql', STYLES_TABLE);
ef5584
ef5584
			add_log('admin', 'LOG_' . $l_prefix . '_DELETE', $style_row[$mode . '_name']);
ef5584
			$message = ($mode != 'style') ? $l_prefix . '_DELETED_FS' : $l_prefix . '_DELETED';
ef5584
			trigger_error($user->lang[$message] . adm_back_link($this->u_action));
ef5584
		}
ef5584
ef5584
		$this->page_title = 'DELETE_' . $l_prefix;
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'S_DELETE'			=> true,
ef5584
			'S_REPLACE_OPTIONS'	=> $s_options,
ef5584
ef5584
			'L_TITLE'			=> $user->lang[$this->page_title],
ef5584
			'L_EXPLAIN'			=> $user->lang[$this->page_title . '_EXPLAIN'],
ef5584
			'L_NAME'			=> $user->lang[$l_prefix . '_NAME'],
ef5584
			'L_REPLACE'			=> $user->lang['REPLACE_' . $l_prefix],
ef5584
			'L_REPLACE_EXPLAIN'	=> $user->lang['REPLACE_' . $l_prefix . '_EXPLAIN'],
ef5584
ef5584
			'U_ACTION'		=> $this->u_action . "&action=delete&id=$style_id",
ef5584
			'U_BACK'		=> $this->u_action,
ef5584
ef5584
			'NAME'			=> $style_row[$mode . '_name'],
ef5584
			)
ef5584
		);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Export style or style elements
ef5584
	*/
ef5584
	function export($mode, $style_id)
ef5584
	{
ef5584
		global $db, $template, $user, $phpbb_root_path, $cache, $phpEx, $config;
ef5584
ef5584
		$update = (isset($_POST['update'])) ? true : false;
ef5584
ef5584
		$inc_template = request_var('inc_template', 0);
ef5584
		$inc_theme = request_var('inc_theme', 0);
ef5584
		$inc_imageset = request_var('inc_imageset', 0);
ef5584
		$store = request_var('store', 0);
ef5584
		$format = request_var('format', '');
ef5584
ef5584
		$error = array();
ef5584
		$methods = array('tar');
ef5584
ef5584
		$available_methods = array('tar.gz' => 'zlib', 'tar.bz2' => 'bz2', 'zip' => 'zlib');
ef5584
		foreach ($available_methods as $type => $module)
ef5584
		{
ef5584
			if (!@extension_loaded($module))
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			$methods[] = $type;
ef5584
		}
ef5584
ef5584
		if (!in_array($format, $methods))
ef5584
		{
ef5584
			$format = 'tar';
ef5584
		}
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'style':
ef5584
				if ($update && ($inc_template + $inc_theme + $inc_imageset) < 1)
ef5584
				{
ef5584
					$error[] = $user->lang['STYLE_ERR_MORE_ELEMENTS'];
ef5584
				}
ef5584
ef5584
				$name = 'style_name';
ef5584
ef5584
				$sql_select = 's.style_id, s.style_name, s.style_copyright';
ef5584
				$sql_select .= ($inc_template) ? ', t.*' : ', t.template_name';
ef5584
				$sql_select .= ($inc_theme) ? ', c.*' : ', c.theme_name';
ef5584
				$sql_select .= ($inc_imageset) ? ', i.*' : ', i.imageset_name';
ef5584
				$sql_from = STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . ' i';
ef5584
				$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";
ef5584
ef5584
				$l_prefix = 'STYLE';
ef5584
			break;
ef5584
ef5584
			case 'template':
ef5584
				$name = 'template_name';
ef5584
ef5584
				$sql_select = '*';
ef5584
				$sql_from = STYLES_TEMPLATE_TABLE;
ef5584
				$sql_where = "template_id = $style_id";
ef5584
ef5584
				$l_prefix = 'TEMPLATE';
ef5584
			break;
ef5584
ef5584
			case 'theme':
ef5584
				$name = 'theme_name';
ef5584
ef5584
				$sql_select = '*';
ef5584
				$sql_from = STYLES_THEME_TABLE;
ef5584
				$sql_where = "theme_id = $style_id";
ef5584
ef5584
				$l_prefix = 'THEME';
ef5584
			break;
ef5584
ef5584
			case 'imageset':
ef5584
				$name = 'imageset_name';
ef5584
ef5584
				$sql_select = '*';
ef5584
				$sql_from = STYLES_IMAGESET_TABLE;
ef5584
				$sql_where = "imageset_id = $style_id";
ef5584
ef5584
				$l_prefix = 'IMAGESET';
ef5584
			break;
ef5584
		}
ef5584
ef5584
		if ($update && !sizeof($error))
ef5584
		{
ef5584
			$sql = "SELECT $sql_select
ef5584
				FROM $sql_from
ef5584
				WHERE $sql_where";
ef5584
			$result = $db->sql_query($sql);
ef5584
			$style_row = $db->sql_fetchrow($result);
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			if (!$style_row)
ef5584
			{
ef5584
				trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
			}
ef5584
ef5584
			$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');
ef5584
ef5584
			foreach ($var_ary as $var)
ef5584
			{
ef5584
				if (!isset($style_row[$var]))
ef5584
				{
ef5584
					$style_row[$var] = '';
ef5584
				}
ef5584
			}
ef5584
ef5584
			$files = $data = array();
ef5584
ef5584
			if ($mode == 'style')
ef5584
			{
ef5584
				$style_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['style_name'], $style_row['style_copyright'], $config['version']), $this->style_cfg);
ef5584
ef5584
				$style_cfg .= (!$inc_template) ? "\nrequired_template = {$style_row['template_name']}" : '';
ef5584
				$style_cfg .= (!$inc_theme) ? "\nrequired_theme = {$style_row['theme_name']}" : '';
ef5584
				$style_cfg .= (!$inc_imageset) ? "\nrequired_imageset = {$style_row['imageset_name']}" : '';
ef5584
ef5584
				$data[] = array(
ef5584
					'src'		=> $style_cfg,
ef5584
					'prefix'	=> 'style.cfg'
ef5584
				);
ef5584
ef5584
				unset($style_cfg);
ef5584
			}
ef5584
ef5584
			// Export template core code
ef5584
			if ($mode == 'template' || $inc_template)
ef5584
			{
ef5584
				$template_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['template_name'], $style_row['template_copyright'], $config['version']), $this->template_cfg);
ef5584
ef5584
				$use_template_name = '';
ef5584
ef5584
				// Add the inherit from variable, depending on it's use...
ef5584
				if ($style_row['template_inherits_id'])
ef5584
				{
ef5584
					// Get the template name
ef5584
					$sql = 'SELECT template_name
ef5584
						FROM ' . STYLES_TEMPLATE_TABLE . '
ef5584
						WHERE template_id = ' . (int) $style_row['template_inherits_id'];
ef5584
					$result = $db->sql_query($sql);
ef5584
					$use_template_name = (string) $db->sql_fetchfield('template_name');
ef5584
					$db->sql_freeresult($result);
ef5584
				}
ef5584
ef5584
				$template_cfg .= ($use_template_name) ? "\ninherit_from = $use_template_name" : "\n#inherit_from = ";
ef5584
				$template_cfg .= "\n\nbbcode_bitfield = {$style_row['bbcode_bitfield']}";
ef5584
ef5584
				$data[] = array(
ef5584
					'src'		=> $template_cfg,
ef5584
					'prefix'	=> 'template/template.cfg'
ef5584
				);
ef5584
ef5584
				// This is potentially nasty memory-wise ...
ef5584
				if (!$style_row['template_storedb'])
ef5584
				{
ef5584
					$files[] = array(
ef5584
						'src'		=> "styles/{$style_row['template_path']}/template/",
ef5584
						'prefix-'	=> "styles/{$style_row['template_path']}/",
ef5584
						'prefix+'	=> false,
ef5584
						'exclude'	=> 'template.cfg'
ef5584
					);
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$sql = 'SELECT template_filename, template_data
ef5584
						FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
ef5584
						WHERE template_id = {$style_row['template_id']}";
ef5584
					$result = $db->sql_query($sql);
ef5584
ef5584
					while ($row = $db->sql_fetchrow($result))
ef5584
					{
ef5584
						$data[] = array(
ef5584
							'src' => $row['template_data'],
ef5584
							'prefix' => 'template/' . $row['template_filename']
ef5584
						);
ef5584
					}
ef5584
					$db->sql_freeresult($result);
ef5584
				}
ef5584
				unset($template_cfg);
ef5584
			}
ef5584
ef5584
			// Export theme core code
ef5584
			if ($mode == 'theme' || $inc_theme)
ef5584
			{
ef5584
				$theme_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['theme_name'], $style_row['theme_copyright'], $config['version']), $this->theme_cfg);
ef5584
ef5584
				// Read old cfg file
ef5584
				$items = $cache->obtain_cfg_items($style_row);
ef5584
				$items = $items['theme'];
ef5584
ef5584
				if (!isset($items['parse_css_file']))
ef5584
				{
ef5584
					$items['parse_css_file'] = 'off';
ef5584
				}
ef5584
ef5584
				$theme_cfg = str_replace(array('{PARSE_CSS_FILE}'), array($items['parse_css_file']), $theme_cfg);
ef5584
ef5584
				$files[] = array(
ef5584
					'src'		=> "styles/{$style_row['theme_path']}/theme/",
ef5584
					'prefix-'	=> "styles/{$style_row['theme_path']}/",
ef5584
					'prefix+'	=> false,
ef5584
					'exclude'	=> ($style_row['theme_storedb']) ? 'stylesheet.css,theme.cfg' : 'theme.cfg'
ef5584
				);
ef5584
ef5584
				$data[] = array(
ef5584
					'src'		=> $theme_cfg,
ef5584
					'prefix'	=> 'theme/theme.cfg'
ef5584
				);
ef5584
ef5584
				if ($style_row['theme_storedb'])
ef5584
				{
ef5584
					$data[] = array(
ef5584
						'src'		=> $style_row['theme_data'],
ef5584
						'prefix'	=> 'theme/stylesheet.css'
ef5584
					);
ef5584
				}
ef5584
ef5584
				unset($items, $theme_cfg);
ef5584
			}
ef5584
ef5584
			// Export imageset core code
ef5584
			if ($mode == 'imageset' || $inc_imageset)
ef5584
			{
ef5584
				$imageset_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['imageset_name'], $style_row['imageset_copyright'], $config['version']), $this->imageset_cfg);
ef5584
ef5584
				$imageset_main = array();
ef5584
ef5584
				$sql = 'SELECT image_filename, image_name, image_height, image_width
ef5584
					FROM ' . STYLES_IMAGESET_DATA_TABLE . "
ef5584
					WHERE imageset_id = $style_id
ef5584
						AND image_lang = ''";
ef5584
				$result = $db->sql_query($sql);
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$imageset_main[$row['image_name']] = $row['image_filename'] . ($row['image_height'] ? '*' . $row['image_height']: '') . ($row['image_width'] ? '*' . $row['image_width']: '');
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
				foreach ($this->imageset_keys as $topic => $key_array)
ef5584
				{
ef5584
					foreach ($key_array as $key)
ef5584
					{
ef5584
						if (isset($imageset_main[$key]))
ef5584
						{
ef5584
							$imageset_cfg .= "\nimg_" . $key . ' = ' . str_replace("styles/{$style_row['imageset_path']}/imageset/", '{PATH}', $imageset_main[$key]);
ef5584
						}
ef5584
					}
ef5584
				}
ef5584
ef5584
				$files[] = array(
ef5584
					'src'		=> "styles/{$style_row['imageset_path']}/imageset/",
ef5584
					'prefix-'	=> "styles/{$style_row['imageset_path']}/",
ef5584
					'prefix+'	=> false,
ef5584
					'exclude'	=> 'imageset.cfg'
ef5584
				);
ef5584
ef5584
				$data[] = array(
ef5584
					'src'		=> trim($imageset_cfg),
ef5584
					'prefix'	=> 'imageset/imageset.cfg'
ef5584
				);
ef5584
ef5584
				end($data);
ef5584
ef5584
				$imageset_root = "{$phpbb_root_path}styles/{$style_row['imageset_path']}/imageset/";
ef5584
ef5584
				if ($dh = @opendir($imageset_root))
ef5584
				{
ef5584
					while (($fname = readdir($dh)) !== false)
ef5584
					{
ef5584
						if ($fname[0] != '.' && $fname != 'CVS' && is_dir("$imageset_root$fname"))
ef5584
						{
ef5584
							$files[key($files)]['exclude'] .= ',' . $fname . '/imageset.cfg';
ef5584
						}
ef5584
					}
ef5584
					closedir($dh);
ef5584
				}
ef5584
ef5584
				$imageset_lang = array();
ef5584
ef5584
				$sql = 'SELECT image_filename, image_name, image_height, image_width, image_lang
ef5584
					FROM ' . STYLES_IMAGESET_DATA_TABLE . "
ef5584
					WHERE imageset_id = $style_id
ef5584
						AND image_lang <> ''";
ef5584
				$result = $db->sql_query($sql);
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$imageset_lang[$row['image_lang']][$row['image_name']] = $row['image_filename'] . ($row['image_height'] ? '*' . $row['image_height']: '') . ($row['image_width'] ? '*' . $row['image_width']: '');
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
				foreach ($imageset_lang as $lang => $imageset_localized)
ef5584
				{
ef5584
					$imageset_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['imageset_name'], $style_row['imageset_copyright'], $config['version']), $this->imageset_cfg);
ef5584
ef5584
					foreach ($this->imageset_keys as $topic => $key_array)
ef5584
					{
ef5584
						foreach ($key_array as $key)
ef5584
						{
ef5584
							if (isset($imageset_localized[$key]))
ef5584
							{
ef5584
								$imageset_cfg .= "\nimg_" . $key . ' = ' . str_replace("styles/{$style_row['imageset_path']}/imageset/", '{PATH}', $imageset_localized[$key]);
ef5584
							}
ef5584
						}
ef5584
					}
ef5584
ef5584
					$data[] = array(
ef5584
						'src'		=> trim($imageset_cfg),
ef5584
						'prefix'	=> 'imageset/' . $lang . '/imageset.cfg'
ef5584
					);
ef5584
				}
ef5584
ef5584
				unset($imageset_cfg);
ef5584
			}
ef5584
ef5584
			switch ($format)
ef5584
			{
ef5584
				case 'tar':
ef5584
					$ext = '.tar';
ef5584
					$mimetype = 'x-tar';
ef5584
					$compress = 'compress_tar';
ef5584
				break;
ef5584
ef5584
				case 'zip':
ef5584
					$ext = '.zip';
ef5584
					$mimetype = 'zip';
ef5584
				break;
ef5584
ef5584
				case 'tar.gz':
ef5584
					$ext = '.tar.gz';
ef5584
					$mimetype = 'x-gzip';
ef5584
				break;
ef5584
ef5584
				case 'tar.bz2':
ef5584
					$ext = '.tar.bz2';
ef5584
					$mimetype = 'x-bzip2';
ef5584
				break;
ef5584
ef5584
				default:
ef5584
					$error[] = $user->lang[$l_prefix . '_ERR_ARCHIVE'];
ef5584
			}
ef5584
ef5584
			if (!sizeof($error))
ef5584
			{
ef5584
				include($phpbb_root_path . 'includes/functions_compress.' . $phpEx);
ef5584
ef5584
				if ($mode == 'style')
ef5584
				{
ef5584
					$path = preg_replace('#[^\w-]+#', '_', $style_row['style_name']);
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$path = $style_row[$mode . '_path'];
ef5584
				}
ef5584
ef5584
				if ($format == 'zip')
ef5584
				{
ef5584
					$compress = new compress_zip('w', $phpbb_root_path . "store/$path$ext");
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$compress = new compress_tar('w', $phpbb_root_path . "store/$path$ext", $ext);
ef5584
				}
ef5584
ef5584
				if (sizeof($files))
ef5584
				{
ef5584
					foreach ($files as $file_ary)
ef5584
					{
ef5584
						$compress->add_file($file_ary['src'], $file_ary['prefix-'], $file_ary['prefix+'], $file_ary['exclude']);
ef5584
					}
ef5584
				}
ef5584
ef5584
				if (sizeof($data))
ef5584
				{
ef5584
					foreach ($data as $data_ary)
ef5584
					{
ef5584
						$compress->add_data($data_ary['src'], $data_ary['prefix']);
ef5584
					}
ef5584
				}
ef5584
ef5584
				$compress->close();
ef5584
ef5584
				add_log('admin', 'LOG_' . $l_prefix . '_EXPORT', $style_row[$mode . '_name']);
ef5584
ef5584
				if (!$store)
ef5584
				{
ef5584
					$compress->download($path);
ef5584
					@unlink("{$phpbb_root_path}store/$path$ext");
ef5584
					exit;
ef5584
				}
ef5584
ef5584
				trigger_error(sprintf($user->lang[$l_prefix . '_EXPORTED'], "store/$path$ext") . adm_back_link($this->u_action));
ef5584
			}
ef5584
		}
ef5584
ef5584
		$sql = "SELECT {$mode}_id, {$mode}_name
ef5584
			FROM " . (($mode == 'style') ? STYLES_TABLE : $sql_from) . "
ef5584
			WHERE {$mode}_id = $style_id";
ef5584
		$result = $db->sql_query($sql);
ef5584
		$style_row = $db->sql_fetchrow($result);
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		if (!$style_row)
ef5584
		{
ef5584
			trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		$this->page_title = $l_prefix . '_EXPORT';
ef5584
ef5584
		$format_buttons = '';
ef5584
		foreach ($methods as $method)
ef5584
		{
ef5584
			$format_buttons .= '<label><input type="radio"' . ((!$format_buttons) ? ' id="format"' : '') . ' class="radio" value="' . $method . '" name="format"' . (($method == $format) ? ' checked="checked"' : '') . ' /> ' . $method . '</label>';
ef5584
		}
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'S_EXPORT'		=> true,
ef5584
			'S_ERROR_MSG'	=> (sizeof($error)) ? true : false,
ef5584
			'S_STYLE'		=> ($mode == 'style') ? true : false,
ef5584
ef5584
			'L_TITLE'		=> $user->lang[$this->page_title],
ef5584
			'L_EXPLAIN'		=> $user->lang[$this->page_title . '_EXPLAIN'],
ef5584
			'L_NAME'		=> $user->lang[$l_prefix . '_NAME'],
ef5584
ef5584
			'U_ACTION'		=> $this->u_action . '&action=export&id=' . $style_id,
ef5584
			'U_BACK'		=> $this->u_action,
ef5584
ef5584
			'ERROR_MSG'			=> (sizeof($error)) ? implode('
', $error) : '',
ef5584
			'NAME'				=> $style_row[$mode . '_name'],
ef5584
			'FORMAT_BUTTONS'	=> $format_buttons)
ef5584
		);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Display details
ef5584
	*/
ef5584
	function details($mode, $style_id)
ef5584
	{
ef5584
		global $template, $db, $config, $user, $safe_mode, $cache, $phpbb_root_path;
ef5584
ef5584
		$update = (isset($_POST['update'])) ? true : false;
ef5584
		$l_type = strtoupper($mode);
ef5584
ef5584
		$error = array();
ef5584
		$element_ary = array('template' => STYLES_TEMPLATE_TABLE, 'theme' => STYLES_THEME_TABLE, 'imageset' => STYLES_IMAGESET_TABLE);
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'style':
ef5584
				$sql_from = STYLES_TABLE;
ef5584
			break;
ef5584
ef5584
			case 'template':
ef5584
				$sql_from = STYLES_TEMPLATE_TABLE;
ef5584
			break;
ef5584
ef5584
			case 'theme':
ef5584
				$sql_from = STYLES_THEME_TABLE;
ef5584
			break;
ef5584
ef5584
			case 'imageset':
ef5584
				$sql_from = STYLES_IMAGESET_TABLE;
ef5584
			break;
ef5584
		}
ef5584
ef5584
		$sql = "SELECT *
ef5584
			FROM $sql_from
ef5584
			WHERE {$mode}_id = $style_id";
ef5584
		$result = $db->sql_query($sql);
ef5584
		$style_row = $db->sql_fetchrow($result);
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		if (!$style_row)
ef5584
		{
ef5584
			trigger_error($user->lang['NO_' . $l_type] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		$style_row['style_default'] = ($mode == 'style' && $config['default_style'] == $style_id) ? 1 : 0;
ef5584
ef5584
		if ($update)
ef5584
		{
ef5584
			$name = utf8_normalize_nfc(request_var('name', '', true));
ef5584
			$copyright = utf8_normalize_nfc(request_var('copyright', '', true));
ef5584
ef5584
			$template_id = request_var('template_id', 0);
ef5584
			$theme_id = request_var('theme_id', 0);
ef5584
			$imageset_id = request_var('imageset_id', 0);
ef5584
ef5584
			$style_active = request_var('style_active', 0);
ef5584
			$style_default = request_var('style_default', 0);
ef5584
			$store_db = request_var('store_db', 0);
ef5584
ef5584
			// If the admin selected the style to be the default style, but forgot to activate it... we will do it for him
ef5584
			if ($style_default)
ef5584
			{
ef5584
				$style_active = 1;
ef5584
			}
ef5584
ef5584
			$sql = "SELECT {$mode}_id, {$mode}_name
ef5584
				FROM $sql_from
ef5584
				WHERE {$mode}_id <> $style_id
ef5584
				AND LOWER({$mode}_name) = '" . $db->sql_escape(strtolower($name)) . "'";
ef5584
			$result = $db->sql_query($sql);
ef5584
			$conflict = $db->sql_fetchrow($result);
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			if ($mode == 'style' && (!$template_id || !$theme_id || !$imageset_id))
ef5584
			{
ef5584
				$error[] = $user->lang['STYLE_ERR_NO_IDS'];
ef5584
			}
ef5584
ef5584
			if ($mode == 'style' && $style_row['style_active'] && !$style_active && $config['default_style'] == $style_id)
ef5584
			{
ef5584
				$error[] = $user->lang['DEACTIVATE_DEFAULT'];
ef5584
			}
ef5584
ef5584
			if (!$name || $conflict)
ef5584
			{
ef5584
				$error[] = $user->lang[$l_type . '_ERR_STYLE_NAME'];
ef5584
			}
ef5584
ef5584
			if ($mode === 'theme' || $mode === 'template')
ef5584
			{
ef5584
				// a rather elaborate check we have to do here once to avoid trouble later
ef5584
				$check = "{$phpbb_root_path}styles/" . $style_row["{$mode}_path"] . (($mode === 'theme') ? '/theme/stylesheet.css' : '/template');
ef5584
				if (($style_row["{$mode}_storedb"] != $store_db) && !$store_db && ($safe_mode || !@is_writable($check)))
ef5584
				{
ef5584
					$error[] = $user->lang['EDIT_' . strtoupper($mode) . '_STORED_DB'];
ef5584
					$store_db = 1;
ef5584
				}
ef5584
ef5584
				// themes which have to be parsed have to go into db
ef5584
				if ($mode == 'theme')
ef5584
				{
ef5584
					$cfg = parse_cfg_file("{$phpbb_root_path}styles/" . $style_row["{$mode}_path"] . "/theme/theme.cfg");
ef5584
ef5584
					if (isset($cfg['parse_css_file']) && $cfg['parse_css_file'] && !$store_db)
ef5584
					{
ef5584
						$error[] = $user->lang['EDIT_THEME_STORE_PARSED'];
ef5584
						$store_db = 1;
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
ef5584
			if (!sizeof($error))
ef5584
			{
ef5584
				// Check length settings
ef5584
				if (utf8_strlen($name) > 30)
ef5584
				{
ef5584
					$error[] = $user->lang[$l_type . '_ERR_NAME_LONG'];
ef5584
				}
ef5584
ef5584
				if (utf8_strlen($copyright) > 60)
ef5584
				{
ef5584
					$error[] = $user->lang[$l_type . '_ERR_COPY_LONG'];
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
ef5584
		if ($update && sizeof($error))
ef5584
		{
ef5584
			$style_row = array_merge($style_row, array(
ef5584
				'template_id'			=> $template_id,
ef5584
				'theme_id'				=> $theme_id,
ef5584
				'imageset_id'			=> $imageset_id,
ef5584
				'style_active'			=> $style_active,
ef5584
				$mode . '_storedb'		=> $store_db,
ef5584
				$mode . '_name'			=> $name,
ef5584
				$mode . '_copyright'	=> $copyright)
ef5584
			);
ef5584
		}
ef5584
ef5584
		// User has submitted form and no errors have occurred
ef5584
		if ($update && !sizeof($error))
ef5584
		{
ef5584
			$sql_ary = array(
ef5584
				$mode . '_name'			=> $name,
ef5584
				$mode . '_copyright'	=> $copyright
ef5584
			);
ef5584
ef5584
			switch ($mode)
ef5584
			{
ef5584
				case 'style':
ef5584
ef5584
					$sql_ary += array(
ef5584
						'template_id'		=> (int) $template_id,
ef5584
						'theme_id'			=> (int) $theme_id,
ef5584
						'imageset_id'		=> (int) $imageset_id,
ef5584
						'style_active'		=> (int) $style_active,
ef5584
					);
ef5584
				break;
ef5584
ef5584
				case 'imageset':
ef5584
				break;
ef5584
ef5584
				case 'theme':
ef5584
ef5584
					if ($style_row['theme_storedb'] != $store_db)
ef5584
					{
ef5584
						$theme_data = '';
ef5584
ef5584
						if (!$style_row['theme_storedb'])
ef5584
						{
ef5584
							$theme_data = $this->db_theme_data($style_row);
ef5584
						}
ef5584
						else if (!$store_db && !$safe_mode && @is_writable("{$phpbb_root_path}styles/{$style_row['theme_path']}/theme/stylesheet.css"))
ef5584
						{
ef5584
							$store_db = 1;
ef5584
							$theme_data = $style_row['theme_data'];
ef5584
ef5584
							if ($fp = @fopen("{$phpbb_root_path}styles/{$style_row['theme_path']}/theme/stylesheet.css", 'wb'))
ef5584
							{
ef5584
								$store_db = (@fwrite($fp, str_replace("styles/{$style_row['theme_path']}/theme/", './', $theme_data))) ? 0 : 1;
ef5584
							}
ef5584
							fclose($fp);
ef5584
						}
ef5584
ef5584
						$sql_ary += array(
ef5584
							'theme_mtime'	=> ($store_db) ? filemtime("{$phpbb_root_path}styles/{$style_row['theme_path']}/theme/stylesheet.css") : 0,
ef5584
							'theme_storedb'	=> $store_db,
ef5584
							'theme_data'	=> ($store_db) ? $theme_data : '',
ef5584
						);
ef5584
					}
ef5584
				break;
ef5584
ef5584
				case 'template':
ef5584
ef5584
					if ($style_row['template_storedb'] != $store_db)
ef5584
					{
ef5584
						if ($super = $this->get_super($mode, $style_row['template_id']))
ef5584
						{
ef5584
							$error[] = (sprintf($user->lang["{$l_type}_INHERITS"], $super['template_name']));
ef5584
							$sql_ary = array();
ef5584
						}
ef5584
						else
ef5584
						{
ef5584
							if (!$store_db && !$safe_mode && @is_writable("{$phpbb_root_path}styles/{$style_row['template_path']}/template"))
ef5584
							{
ef5584
								$err = $this->store_in_fs('template', $style_row['template_id']);
ef5584
								if ($err)
ef5584
								{
ef5584
									$error += $err;
ef5584
								}
ef5584
							}
ef5584
							else if ($store_db)
ef5584
							{
ef5584
								$this->store_in_db('template', $style_row['template_id']);
ef5584
							}
ef5584
							else
ef5584
							{
ef5584
								// We no longer store within the db, but are also not able to update the file structure
ef5584
								// Since the admin want to switch this, we adhere to his decision. But we also need to remove the cache
ef5584
								$sql = 'DELETE FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
ef5584
									WHERE template_id = $style_id";
ef5584
								$db->sql_query($sql);
ef5584
							}
ef5584
ef5584
							$sql_ary += array(
ef5584
								'template_storedb'	=> $store_db,
ef5584
							);
ef5584
						}
ef5584
					}
ef5584
				break;
ef5584
			}
ef5584
ef5584
			if (sizeof($sql_ary))
ef5584
			{
ef5584
				$sql = "UPDATE $sql_from
ef5584
					SET " . $db->sql_build_array('UPDATE', $sql_ary) . "
ef5584
					WHERE {$mode}_id = $style_id";
ef5584
				$db->sql_query($sql);
ef5584
ef5584
				// Making this the default style?
ef5584
				if ($mode == 'style' && $style_default)
ef5584
				{
ef5584
					set_config('default_style', $style_id);
ef5584
				}
ef5584
			}
ef5584
ef5584
			$cache->destroy('sql', STYLES_TABLE);
ef5584
ef5584
			add_log('admin', 'LOG_' . $l_type . '_EDIT_DETAILS', $name);
ef5584
			if (sizeof($error))
ef5584
			{
ef5584
				trigger_error(implode('
', $error) . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				trigger_error($user->lang[$l_type . '_DETAILS_UPDATED'] . adm_back_link($this->u_action));
ef5584
			}
ef5584
		}
ef5584
ef5584
		if ($mode == 'style')
ef5584
		{
ef5584
			foreach ($element_ary as $element => $table)
ef5584
			{
ef5584
				$sql = "SELECT {$element}_id, {$element}_name
ef5584
					FROM $table
ef5584
					ORDER BY {$element}_id ASC";
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				${$element . '_options'} = '';
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$selected = ($row[$element . '_id'] == $style_row[$element . '_id']) ? ' selected="selected"' : '';
ef5584
					${$element . '_options'} .= '<option value="' . $row[$element . '_id'] . '"' . $selected . '>' . $row[$element . '_name'] . '</option>';
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
			}
ef5584
		}
ef5584
ef5584
ef5584
		if ($mode == 'template')
ef5584
		{
ef5584
			$super = array();
ef5584
			if (isset($style_row[$mode . '_inherits_id']) && $style_row['template_inherits_id'])
ef5584
			{
ef5584
				$super = $this->get_super($mode, $style_row['template_id']);
ef5584
			}
ef5584
		}
ef5584
ef5584
		$this->page_title = 'EDIT_DETAILS_' . $l_type;
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'S_DETAILS'				=> true,
ef5584
			'S_ERROR_MSG'			=> (sizeof($error)) ? true : false,
ef5584
			'S_STYLE'				=> ($mode == 'style') ? true : false,
ef5584
			'S_TEMPLATE'			=> ($mode == 'template') ? true : false,
ef5584
			'S_THEME'				=> ($mode == 'theme') ? true : false,
ef5584
			'S_IMAGESET'			=> ($mode == 'imageset') ? true : false,
ef5584
			'S_STORE_DB'			=> (isset($style_row[$mode . '_storedb'])) ? $style_row[$mode . '_storedb'] : 0,
ef5584
			'S_STORE_DB_DISABLED'	=> (isset($style_row[$mode . '_inherits_id'])) ? $style_row[$mode . '_inherits_id'] : 0,
ef5584
			'S_STYLE_ACTIVE'		=> (isset($style_row['style_active'])) ? $style_row['style_active'] : 0,
ef5584
			'S_STYLE_DEFAULT'		=> (isset($style_row['style_default'])) ? $style_row['style_default'] : 0,
ef5584
			'S_SUPERTEMPLATE'		=> (isset($style_row[$mode . '_inherits_id']) && $style_row[$mode . '_inherits_id']) ? $super['template_name'] : 0,
ef5584
ef5584
			'S_TEMPLATE_OPTIONS'	=> ($mode == 'style') ? $template_options : '',
ef5584
			'S_THEME_OPTIONS'		=> ($mode == 'style') ? $theme_options : '',
ef5584
			'S_IMAGESET_OPTIONS'	=> ($mode == 'style') ? $imageset_options : '',
ef5584
ef5584
			'U_ACTION'		=> $this->u_action . '&action=details&id=' . $style_id,
ef5584
			'U_BACK'		=> $this->u_action,
ef5584
ef5584
			'L_TITLE'				=> $user->lang[$this->page_title],
ef5584
			'L_EXPLAIN'				=> $user->lang[$this->page_title . '_EXPLAIN'],
ef5584
			'L_NAME'				=> $user->lang[$l_type . '_NAME'],
ef5584
			'L_LOCATION'			=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION'] : '',
ef5584
			'L_LOCATION_EXPLAIN'	=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION_EXPLAIN'] : '',
ef5584
ef5584
			'ERROR_MSG'		=> (sizeof($error)) ? implode('
', $error) : '',
ef5584
			'NAME'			=> $style_row[$mode . '_name'],
ef5584
			'COPYRIGHT'		=> $style_row[$mode . '_copyright'],
ef5584
			)
ef5584
		);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Load css file contents
ef5584
	*/
ef5584
	function load_css_file($path, $filename)
ef5584
	{
ef5584
		global $phpbb_root_path;
ef5584
ef5584
		$file = "{$phpbb_root_path}styles/$path/theme/$filename";
ef5584
ef5584
		if (file_exists($file) && ($content = file_get_contents($file)))
ef5584
		{
ef5584
			$content = trim($content);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$content = '';
ef5584
		}
ef5584
		if (defined('DEBUG'))
ef5584
		{
ef5584
			$content = "/* BEGIN @include $filename */ \n $content \n /* END @include $filename */ \n";
ef5584
		}
ef5584
ef5584
		return $content;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Returns a string containing the value that should be used for the theme_data column in the theme database table.
ef5584
	* Includes contents of files loaded via @import
ef5584
	*
ef5584
	* @param array $theme_row is an associative array containing the theme's current database entry
ef5584
	* @param mixed $stylesheet can either be the new content for the stylesheet or false to load from the standard file
ef5584
	* @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']}"
ef5584
	*
ef5584
	* @return string Stylesheet data for theme_data column in the theme table
ef5584
	*/
ef5584
	function db_theme_data($theme_row, $stylesheet = false, $root_path = '')
ef5584
	{
ef5584
		global $phpbb_root_path;
ef5584
ef5584
		if (!$root_path)
ef5584
		{
ef5584
			$root_path = $phpbb_root_path . 'styles/' . $theme_row['theme_path'];
ef5584
		}
ef5584
ef5584
		if (!$stylesheet)
ef5584
		{
ef5584
			$stylesheet = '';
ef5584
			if (file_exists($root_path . '/theme/stylesheet.css'))
ef5584
			{
ef5584
				$stylesheet = file_get_contents($root_path . '/theme/stylesheet.css');
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Match CSS imports
ef5584
		$matches = array();
ef5584
		preg_match_all('/@import url\(["\'](.*)["\']\);/i', $stylesheet, $matches);
ef5584
ef5584
		if (sizeof($matches))
ef5584
		{
ef5584
			foreach ($matches[0] as $idx => $match)
ef5584
			{
ef5584
				$stylesheet = str_replace($match, acp_styles::load_css_file($theme_row['theme_path'], $matches[1][$idx]), $stylesheet);
ef5584
			}
ef5584
		}
ef5584
ef5584
		// adjust paths
ef5584
		return str_replace('./', 'styles/' . $theme_row['theme_path'] . '/theme/', $stylesheet);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Store template files into db
ef5584
	*/
ef5584
	function store_templates($mode, $style_id, $template_path, $filelist)
ef5584
	{
ef5584
		global $phpbb_root_path, $phpEx, $db;
ef5584
ef5584
		$template_path = $template_path . '/template/';
ef5584
		$includes = array();
ef5584
		foreach ($filelist as $pathfile => $file_ary)
ef5584
		{
ef5584
			foreach ($file_ary as $file)
ef5584
			{
ef5584
				if (!($fp = @fopen("{$phpbb_root_path}styles/$template_path$pathfile$file", 'r')))
ef5584
				{
ef5584
					trigger_error("Could not open {$phpbb_root_path}styles/$template_path$pathfile$file", E_USER_ERROR);
ef5584
				}
ef5584
				$template_data = fread($fp, filesize("{$phpbb_root_path}styles/$template_path$pathfile$file"));
ef5584
				fclose($fp);
ef5584
ef5584
				if (preg_match_all('##is', $template_data, $matches))
ef5584
				{
ef5584
					foreach ($matches[1] as $match)
ef5584
					{
ef5584
						$includes[trim($match)][] = $file;
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
ef5584
		foreach ($filelist as $pathfile => $file_ary)
ef5584
		{
ef5584
			foreach ($file_ary as $file)
ef5584
			{
ef5584
				// Skip index.
ef5584
				if (strpos($file, 'index.') === 0)
ef5584
				{
ef5584
					continue;
ef5584
				}
ef5584
ef5584
				// We could do this using extended inserts ... but that could be one
ef5584
				// heck of a lot of data ...
ef5584
				$sql_ary = array(
ef5584
					'template_id'			=> (int) $style_id,
ef5584
					'template_filename'		=> "$pathfile$file",
ef5584
					'template_included'		=> (isset($includes[$file])) ? implode(':', $includes[$file]) . ':' : '',
ef5584
					'template_mtime'		=> (int) filemtime("{$phpbb_root_path}styles/$template_path$pathfile$file"),
ef5584
					'template_data'			=> (string) file_get_contents("{$phpbb_root_path}styles/$template_path$pathfile$file"),
ef5584
				);
ef5584
ef5584
				if ($mode == 'insert')
ef5584
				{
ef5584
					$sql = 'INSERT INTO ' . STYLES_TEMPLATE_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$sql = 'UPDATE ' . STYLES_TEMPLATE_DATA_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
ef5584
						WHERE template_id = $style_id
ef5584
							AND template_filename = '" . $db->sql_escape("$pathfile$file") . "'";
ef5584
				}
ef5584
				$db->sql_query($sql);
ef5584
			}
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Returns an array containing all template filenames for one template that are currently cached.
ef5584
	*
ef5584
	* @param string $template_path contains the name of the template's folder in /styles/
ef5584
	*
ef5584
	* @return array of filenames that exist in /styles/$template_path/template/ (without extension!)
ef5584
	*/
ef5584
	function template_cache_filelist($template_path)
ef5584
	{
ef5584
		global $phpbb_root_path, $phpEx, $user;
ef5584
ef5584
		$cache_prefix = 'tpl_' . str_replace('_', '-', $template_path);
ef5584
ef5584
		if (!($dp = @opendir("{$phpbb_root_path}cache")))
ef5584
		{
ef5584
			trigger_error($user->lang['TEMPLATE_ERR_CACHE_READ'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		$file_ary = array();
ef5584
		while ($file = readdir($dp))
ef5584
		{
ef5584
			if ($file[0] == '.')
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			if (is_file($phpbb_root_path . 'cache/' . $file) && (strpos($file, $cache_prefix) === 0))
ef5584
			{
ef5584
				$file_ary[] = str_replace('.', '/', preg_replace('#^' . preg_quote($cache_prefix, '#') . '_(.*?)\.html\.' . $phpEx . '$#i', '\1', $file));
ef5584
			}
ef5584
		}
ef5584
		closedir($dp);
ef5584
ef5584
		return $file_ary;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Destroys cached versions of template files
ef5584
	*
ef5584
	* @param array $template_row contains the template's row in the STYLES_TEMPLATE_TABLE database table
ef5584
	* @param mixed $file_ary is optional and may contain an array of template file names which should be refreshed in the cache.
ef5584
	*	The file names should be the original template file names and not the cache file names.
ef5584
	*/
ef5584
	function clear_template_cache($template_row, $file_ary = false)
ef5584
	{
ef5584
		global $phpbb_root_path, $phpEx, $user;
ef5584
ef5584
		$cache_prefix = 'tpl_' . str_replace('_', '-', $template_row['template_path']);
ef5584
ef5584
		if (!$file_ary || !is_array($file_ary))
ef5584
		{
ef5584
			$file_ary = $this->template_cache_filelist($template_row['template_path']);
ef5584
			$log_file_list = $user->lang['ALL_FILES'];
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$log_file_list = implode(', ', $file_ary);
ef5584
		}
ef5584
ef5584
		foreach ($file_ary as $file)
ef5584
		{
ef5584
			$file = str_replace('/', '.', $file);
ef5584
ef5584
			$file = "{$phpbb_root_path}cache/{$cache_prefix}_$file.html.$phpEx";
ef5584
			if (file_exists($file) && is_file($file))
ef5584
			{
ef5584
				@unlink($file);
ef5584
			}
ef5584
		}
ef5584
		unset($file_ary);
ef5584
ef5584
		add_log('admin', 'LOG_TEMPLATE_CACHE_CLEARED', $template_row['template_name'], $log_file_list);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Install Style/Template/Theme/Imageset
ef5584
	*/
ef5584
	function install($mode)
ef5584
	{
ef5584
		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template;
ef5584
ef5584
		$l_type = strtoupper($mode);
ef5584
ef5584
		$error = $installcfg = $style_row = array();
ef5584
		$root_path = $cfg_file = '';
ef5584
		$element_ary = array('template' => STYLES_TEMPLATE_TABLE, 'theme' => STYLES_THEME_TABLE, 'imageset' => STYLES_IMAGESET_TABLE);
ef5584
ef5584
		$install_path = request_var('path', '');
ef5584
		$update = (isset($_POST['update'])) ? true : false;
ef5584
ef5584
		// Installing, obtain cfg file contents
ef5584
		if ($install_path)
ef5584
		{
ef5584
			$root_path = $phpbb_root_path . 'styles/' . $install_path . '/';
ef5584
			$cfg_file = ($mode == 'style') ? "$root_path$mode.cfg" : "$root_path$mode/$mode.cfg";
ef5584
ef5584
			if (!file_exists($cfg_file))
ef5584
			{
ef5584
				$error[] = $user->lang[$l_type . '_ERR_NOT_' . $l_type];
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$installcfg = parse_cfg_file($cfg_file);
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Installing
ef5584
		if (sizeof($installcfg))
ef5584
		{
ef5584
			$name		= $installcfg['name'];
ef5584
			$copyright	= $installcfg['copyright'];
ef5584
			$version	= $installcfg['version'];
ef5584
ef5584
			$style_row = array(
ef5584
				$mode . '_id'			=> 0,
ef5584
				$mode . '_name'			=> '',
ef5584
				$mode . '_copyright'	=> ''
ef5584
			);
ef5584
ef5584
			switch ($mode)
ef5584
			{
ef5584
				case 'style':
ef5584
ef5584
					$style_row = array(
ef5584
						'style_id'			=> 0,
ef5584
						'style_name'		=> $installcfg['name'],
ef5584
						'style_copyright'	=> $installcfg['copyright']
ef5584
					);
ef5584
ef5584
					$reqd_template = (isset($installcfg['required_template'])) ? $installcfg['required_template'] : false;
ef5584
					$reqd_theme = (isset($installcfg['required_theme'])) ? $installcfg['required_theme'] : false;
ef5584
					$reqd_imageset = (isset($installcfg['required_imageset'])) ? $installcfg['required_imageset'] : false;
ef5584
ef5584
					// Check to see if each element is already installed, if it is grab the id
ef5584
					foreach ($element_ary as $element => $table)
ef5584
					{
ef5584
						$style_row = array_merge($style_row, array(
ef5584
							$element . '_id'			=> 0,
ef5584
							$element . '_name'			=> '',
ef5584
							$element . '_copyright'		=> '')
ef5584
						);
ef5584
ef5584
			 			$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']);
ef5584
ef5584
						if (!$style_row[$element . '_name'])
ef5584
						{
ef5584
							$style_row[$element . '_name'] = $reqd_template;
ef5584
						}
ef5584
ef5584
						// Merge other information to installcfg... if present
ef5584
						$cfg_file = $phpbb_root_path . 'styles/' . $install_path . '/' . $element . '/' . $element . '.cfg';
ef5584
ef5584
						if (file_exists($cfg_file))
ef5584
						{
ef5584
							$cfg_contents = parse_cfg_file($cfg_file);
ef5584
ef5584
							// Merge only specific things. We may need them later.
ef5584
							foreach (array('inherit_from', 'parse_css_file') as $key)
ef5584
							{
ef5584
								if (!empty($cfg_contents[$key]) && !isset($installcfg[$key]))
ef5584
								{
ef5584
									$installcfg[$key] = $cfg_contents[$key];
ef5584
								}
ef5584
							}
ef5584
						}
ef5584
					}
ef5584
ef5584
				break;
ef5584
ef5584
				case 'template':
ef5584
					$this->test_installed('template', $error, $root_path, false, $style_row['template_id'], $style_row['template_name'], $style_row['template_copyright']);
ef5584
				break;
ef5584
ef5584
				case 'theme':
ef5584
					$this->test_installed('theme', $error, $root_path, false, $style_row['theme_id'], $style_row['theme_name'], $style_row['theme_copyright']);
ef5584
				break;
ef5584
ef5584
				case 'imageset':
ef5584
					$this->test_installed('imageset', $error, $root_path, false, $style_row['imageset_id'], $style_row['imageset_name'], $style_row['imageset_copyright']);
ef5584
				break;
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			trigger_error($user->lang['NO_' . $l_type] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		$style_row['store_db'] = request_var('store_db', 0);
ef5584
		$style_row['style_active'] = request_var('style_active', 1);
ef5584
		$style_row['style_default'] = request_var('style_default', 0);
ef5584
ef5584
		// User has submitted form and no errors have occurred
ef5584
		if ($update && !sizeof($error))
ef5584
		{
ef5584
			if ($mode == 'style')
ef5584
			{
ef5584
				foreach ($element_ary as $element => $table)
ef5584
				{
ef5584
					${$element . '_root_path'} = (${'reqd_' . $element}) ? $phpbb_root_path . 'styles/' . ${'reqd_' . $element} . '/' : false;
ef5584
					${$element . '_path'} = (${'reqd_' . $element}) ? ${'reqd_' . $element} : false;
ef5584
				}
ef5584
				$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);
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$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']);
ef5584
			}
ef5584
ef5584
			if (!sizeof($error))
ef5584
			{
ef5584
				$cache->destroy('sql', STYLES_TABLE);
ef5584
ef5584
				$message = ($style_row['store_db']) ? '_ADDED_DB' : '_ADDED';
ef5584
				trigger_error($user->lang[$l_type . $message] . adm_back_link($this->u_action));
ef5584
			}
ef5584
		}
ef5584
ef5584
		$this->page_title = 'INSTALL_' . $l_type;
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'S_DETAILS'			=> true,
ef5584
			'S_INSTALL'			=> true,
ef5584
			'S_ERROR_MSG'		=> (sizeof($error)) ? true : false,
ef5584
			'S_LOCATION'		=> (isset($installcfg['inherit_from']) && $installcfg['inherit_from']) ? false : true,
ef5584
			'S_STYLE'			=> ($mode == 'style') ? true : false,
ef5584
			'S_TEMPLATE'		=> ($mode == 'template') ? true : false,
ef5584
			'S_SUPERTEMPLATE'	=> (isset($installcfg['inherit_from'])) ? $installcfg['inherit_from'] : '',
ef5584
			'S_THEME'			=> ($mode == 'theme') ? true : false,
ef5584
ef5584
			'S_STORE_DB'			=> (isset($style_row[$mode . '_storedb'])) ? $style_row[$mode . '_storedb'] : 0,
ef5584
			'S_STYLE_ACTIVE'		=> (isset($style_row['style_active'])) ? $style_row['style_active'] : 0,
ef5584
			'S_STYLE_DEFAULT'		=> (isset($style_row['style_default'])) ? $style_row['style_default'] : 0,
ef5584
ef5584
			'U_ACTION'			=> $this->u_action . "&action=install&path=" . urlencode($install_path),
ef5584
			'U_BACK'			=> $this->u_action,
ef5584
ef5584
			'L_TITLE'				=> $user->lang[$this->page_title],
ef5584
			'L_EXPLAIN'				=> $user->lang[$this->page_title . '_EXPLAIN'],
ef5584
			'L_NAME'				=> $user->lang[$l_type . '_NAME'],
ef5584
			'L_LOCATION'			=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION'] : '',
ef5584
			'L_LOCATION_EXPLAIN'	=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION_EXPLAIN'] : '',
ef5584
ef5584
			'ERROR_MSG'			=> (sizeof($error)) ? implode('
', $error) : '',
ef5584
			'NAME'				=> $style_row[$mode . '_name'],
ef5584
			'COPYRIGHT'			=> $style_row[$mode . '_copyright'],
ef5584
			'TEMPLATE_NAME'		=> ($mode == 'style') ? $style_row['template_name'] : '',
ef5584
			'THEME_NAME'		=> ($mode == 'style') ? $style_row['theme_name'] : '',
ef5584
			'IMAGESET_NAME'		=> ($mode == 'style') ? $style_row['imageset_name'] : '')
ef5584
		);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Add new style
ef5584
	*/
ef5584
	function add($mode)
ef5584
	{
ef5584
		global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template;
ef5584
ef5584
		$l_type = strtoupper($mode);
ef5584
		$element_ary = array('template' => STYLES_TEMPLATE_TABLE, 'theme' => STYLES_THEME_TABLE, 'imageset' => STYLES_IMAGESET_TABLE);
ef5584
		$error = array();
ef5584
ef5584
		$style_row = array(
ef5584
			$mode . '_name'			=> utf8_normalize_nfc(request_var('name', '', true)),
ef5584
			$mode . '_copyright'	=> utf8_normalize_nfc(request_var('copyright', '', true)),
ef5584
			'template_id'			=> 0,
ef5584
			'theme_id'				=> 0,
ef5584
			'imageset_id'			=> 0,
ef5584
			'store_db'				=> request_var('store_db', 0),
ef5584
			'style_active'			=> request_var('style_active', 1),
ef5584
			'style_default'			=> request_var('style_default', 0),
ef5584
		);
ef5584
ef5584
		$basis = request_var('basis', 0);
ef5584
		$update = (isset($_POST['update'])) ? true : false;
ef5584
ef5584
		if ($basis)
ef5584
		{
ef5584
			switch ($mode)
ef5584
			{
ef5584
				case 'style':
ef5584
					$sql_select = 'template_id, theme_id, imageset_id';
ef5584
					$sql_from = STYLES_TABLE;
ef5584
				break;
ef5584
ef5584
				case 'template':
ef5584
					$sql_select = 'template_id';
ef5584
					$sql_from = STYLES_TEMPLATE_TABLE;
ef5584
				break;
ef5584
ef5584
				case 'theme':
ef5584
					$sql_select = 'theme_id';
ef5584
					$sql_from = STYLES_THEME_TABLE;
ef5584
				break;
ef5584
ef5584
				case 'imageset':
ef5584
					$sql_select = 'imageset_id';
ef5584
					$sql_from = STYLES_IMAGESET_TABLE;
ef5584
				break;
ef5584
			}
ef5584
ef5584
			$sql = "SELECT $sql_select
ef5584
				FROM $sql_from
ef5584
				WHERE {$mode}_id = $basis";
ef5584
			$result = $db->sql_query($sql);
ef5584
			$row = $db->sql_fetchrow($result);
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			if (!$row)
ef5584
			{
ef5584
				$error[] = $user->lang['NO_' . $l_type];
ef5584
			}
ef5584
ef5584
			if (!sizeof($error))
ef5584
			{
ef5584
				$style_row['template_id']	= (isset($row['template_id'])) ? $row['template_id'] : $style_row['template_id'];
ef5584
				$style_row['theme_id']		= (isset($row['theme_id'])) ? $row['theme_id'] : $style_row['theme_id'];
ef5584
				$style_row['imageset_id']	= (isset($row['imageset_id'])) ? $row['imageset_id'] : $style_row['imageset_id'];
ef5584
			}
ef5584
		}
ef5584
ef5584
		if ($update)
ef5584
		{
ef5584
			$style_row['template_id'] = request_var('template_id', $style_row['template_id']);
ef5584
			$style_row['theme_id'] = request_var('theme_id', $style_row['theme_id']);
ef5584
			$style_row['imageset_id'] = request_var('imageset_id', $style_row['imageset_id']);
ef5584
ef5584
			if ($mode == 'style' && (!$style_row['template_id'] || !$style_row['theme_id'] || !$style_row['imageset_id']))
ef5584
			{
ef5584
				$error[] = $user->lang['STYLE_ERR_NO_IDS'];
ef5584
			}
ef5584
		}
ef5584
ef5584
		// User has submitted form and no errors have occurred
ef5584
		if ($update && !sizeof($error))
ef5584
		{
ef5584
			if ($mode == 'style')
ef5584
			{
ef5584
				$style_row['style_id'] = 0;
ef5584
ef5584
				$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);
ef5584
			}
ef5584
ef5584
			if (!sizeof($error))
ef5584
			{
ef5584
				$cache->destroy('sql', STYLES_TABLE);
ef5584
ef5584
				$message = ($style_row['store_db']) ? '_ADDED_DB' : '_ADDED';
ef5584
				trigger_error($user->lang[$l_type . $message] . adm_back_link($this->u_action));
ef5584
			}
ef5584
		}
ef5584
ef5584
		if ($mode == 'style')
ef5584
		{
ef5584
			foreach ($element_ary as $element => $table)
ef5584
			{
ef5584
				$sql = "SELECT {$element}_id, {$element}_name
ef5584
					FROM $table
ef5584
					ORDER BY {$element}_id ASC";
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				${$element . '_options'} = '';
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$selected = ($row[$element . '_id'] == $style_row[$element . '_id']) ? ' selected="selected"' : '';
ef5584
					${$element . '_options'} .= '<option value="' . $row[$element . '_id'] . '"' . $selected . '>' . $row[$element . '_name'] . '</option>';
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
			}
ef5584
		}
ef5584
ef5584
		$this->page_title = 'ADD_' . $l_type;
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'S_DETAILS'			=> true,
ef5584
			'S_ADD'				=> true,
ef5584
			'S_ERROR_MSG'		=> (sizeof($error)) ? true : false,
ef5584
			'S_STYLE'			=> ($mode == 'style') ? true : false,
ef5584
			'S_TEMPLATE'		=> ($mode == 'template') ? true : false,
ef5584
			'S_THEME'			=> ($mode == 'theme') ? true : false,
ef5584
			'S_BASIS'			=> ($basis) ? true : false,
ef5584
ef5584
			'S_STORE_DB'			=> (isset($style_row['storedb'])) ? $style_row['storedb'] : 0,
ef5584
			'S_STYLE_ACTIVE'		=> (isset($style_row['style_active'])) ? $style_row['style_active'] : 0,
ef5584
			'S_STYLE_DEFAULT'		=> (isset($style_row['style_default'])) ? $style_row['style_default'] : 0,
ef5584
			'S_TEMPLATE_OPTIONS'	=> ($mode == 'style') ? $template_options : '',
ef5584
			'S_THEME_OPTIONS'		=> ($mode == 'style') ? $theme_options : '',
ef5584
			'S_IMAGESET_OPTIONS'	=> ($mode == 'style') ? $imageset_options : '',
ef5584
ef5584
			'U_ACTION'			=> $this->u_action . '&action=add&basis=' . $basis,
ef5584
			'U_BACK'			=> $this->u_action,
ef5584
ef5584
			'L_TITLE'				=> $user->lang[$this->page_title],
ef5584
			'L_EXPLAIN'				=> $user->lang[$this->page_title . '_EXPLAIN'],
ef5584
			'L_NAME'				=> $user->lang[$l_type . '_NAME'],
ef5584
			'L_LOCATION'			=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION'] : '',
ef5584
			'L_LOCATION_EXPLAIN'	=> ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION_EXPLAIN'] : '',
ef5584
ef5584
			'ERROR_MSG'			=> (sizeof($error)) ? implode('
', $error) : '',
ef5584
			'NAME'				=> $style_row[$mode . '_name'],
ef5584
			'COPYRIGHT'			=> $style_row[$mode . '_copyright'])
ef5584
		);
ef5584
ef5584
	}
ef5584
ef5584
	/**
ef5584
ef5584
					$reqd_template = (isset($installcfg['required_template'])) ? $installcfg['required_template'] : false;
ef5584
					$reqd_theme = (isset($installcfg['required_theme'])) ? $installcfg['required_theme'] : false;
ef5584
					$reqd_imageset = (isset($installcfg['required_imageset'])) ? $installcfg['required_imageset'] : false;
ef5584
ef5584
					// Check to see if each element is already installed, if it is grab the id
ef5584
					foreach ($element_ary as $element => $table)
ef5584
					{
ef5584
						$style_row = array_merge($style_row, array(
ef5584
							$element . '_id'			=> 0,
ef5584
							$element . '_name'			=> '',
ef5584
							$element . '_copyright'		=> '')
ef5584
						);
ef5584
ef5584
			 			$this->test_installed($element, $error, $root_path, ${'reqd_' . $element}, $style_row[$element . '_id'], $style_row[$element . '_name'], $style_row[$element . '_copyright']);
ef5584
	* Is this element installed? If not, grab its cfg details
ef5584
	*/
ef5584
	function test_installed($element, &$error, $root_path, $reqd_name, &$id, &$name, &$copyright)
ef5584
	{
ef5584
		global $db, $user;
ef5584
ef5584
		switch ($element)
ef5584
		{
ef5584
			case 'template':
ef5584
				$sql_from = STYLES_TEMPLATE_TABLE;
ef5584
			break;
ef5584
ef5584
			case 'theme':
ef5584
				$sql_from = STYLES_THEME_TABLE;
ef5584
			break;
ef5584
ef5584
			case 'imageset':
ef5584
				$sql_from = STYLES_IMAGESET_TABLE;
ef5584
			break;
ef5584
		}
ef5584
ef5584
		$l_element = strtoupper($element);
ef5584
ef5584
		$chk_name = ($reqd_name !== false) ? $reqd_name : $name;
ef5584
ef5584
		$sql = "SELECT {$element}_id, {$element}_name
ef5584
			FROM $sql_from
ef5584
			WHERE {$element}_name = '" . $db->sql_escape($chk_name) . "'";
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		if ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$name = $row[$element . '_name'];
ef5584
			$id = $row[$element . '_id'];
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			if (!($cfg = @file("$root_path$element/$element.cfg")))
ef5584
			{
ef5584
				$error[] = sprintf($user->lang['REQUIRES_' . $l_element], $reqd_name);
ef5584
				return false;
ef5584
			}
ef5584
ef5584
			$cfg = parse_cfg_file("$root_path$element/$element.cfg", $cfg);
ef5584
ef5584
			$name = $cfg['name'];
ef5584
			$copyright = $cfg['copyright'];
ef5584
			$id = 0;
ef5584
ef5584
			unset($cfg);
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Install/Add style
ef5584
	*/
ef5584
	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)
ef5584
	{
ef5584
		global $config, $db, $user;
ef5584
ef5584
		$element_ary = array('template', 'theme', 'imageset');
ef5584
ef5584
		if (!$name)
ef5584
		{
ef5584
			$error[] = $user->lang['STYLE_ERR_STYLE_NAME'];
ef5584
		}
ef5584
ef5584
		// Check length settings
ef5584
		if (utf8_strlen($name) > 30)
ef5584
		{
ef5584
			$error[] = $user->lang['STYLE_ERR_NAME_LONG'];
ef5584
		}
ef5584
ef5584
		if (utf8_strlen($copyright) > 60)
ef5584
		{
ef5584
			$error[] = $user->lang['STYLE_ERR_COPY_LONG'];
ef5584
		}
ef5584
ef5584
		// Check if the name already exist
ef5584
		$sql = 'SELECT style_id
ef5584
			FROM ' . STYLES_TABLE . "
ef5584
			WHERE style_name = '" . $db->sql_escape($name) . "'";
ef5584
		$result = $db->sql_query($sql);
ef5584
		$row = $db->sql_fetchrow($result);
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		if ($row)
ef5584
		{
ef5584
			$error[] = $user->lang['STYLE_ERR_NAME_EXIST'];
ef5584
		}
ef5584
ef5584
		if (sizeof($error))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		foreach ($element_ary as $element)
ef5584
		{
ef5584
			// Zero id value ... need to install element ... run usual checks
ef5584
			// and do the install if necessary
ef5584
			if (!$style_row[$element . '_id'])
ef5584
			{
ef5584
				$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']);
ef5584
			}
ef5584
		}
ef5584
ef5584
		if (!$style_row['template_id'] || !$style_row['theme_id'] || !$style_row['imageset_id'])
ef5584
		{
ef5584
			$error[] = $user->lang['STYLE_ERR_NO_IDS'];
ef5584
		}
ef5584
ef5584
		if (sizeof($error))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		$db->sql_transaction('begin');
ef5584
ef5584
		$sql_ary = array(
ef5584
			'style_name'		=> $name,
ef5584
			'style_copyright'	=> $copyright,
ef5584
			'style_active'		=> (int) $active,
ef5584
			'template_id'		=> (int) $style_row['template_id'],
ef5584
			'theme_id'			=> (int) $style_row['theme_id'],
ef5584
			'imageset_id'		=> (int) $style_row['imageset_id'],
ef5584
		);
ef5584
ef5584
		$sql = 'INSERT INTO ' . STYLES_TABLE . '
ef5584
			' . $db->sql_build_array('INSERT', $sql_ary);
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		$id = $db->sql_nextid();
ef5584
ef5584
		if ($default)
ef5584
		{
ef5584
			$sql = 'UPDATE ' . USERS_TABLE . "
ef5584
				SET user_style = $id
ef5584
				WHERE user_style = " . $config['default_style'];
ef5584
			$db->sql_query($sql);
ef5584
ef5584
			set_config('default_style', $id);
ef5584
		}
ef5584
ef5584
		$db->sql_transaction('commit');
ef5584
ef5584
		add_log('admin', 'LOG_STYLE_ADD', $name);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Install/add an element, doing various checks as we go
ef5584
	*/
ef5584
	function install_element($mode, &$error, $action, $root_path, &$id, $name, $path, $copyright, $store_db = 0)
ef5584
	{
ef5584
		global $phpbb_root_path, $db, $user;
ef5584
ef5584
		// we parse the cfg here (again)
ef5584
		$cfg_data = parse_cfg_file("$root_path$mode/$mode.cfg");
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'template':
ef5584
				$sql_from = STYLES_TEMPLATE_TABLE;
ef5584
			break;
ef5584
ef5584
			case 'theme':
ef5584
				$sql_from = STYLES_THEME_TABLE;
ef5584
			break;
ef5584
ef5584
			case 'imageset':
ef5584
				$sql_from = STYLES_IMAGESET_TABLE;
ef5584
			break;
ef5584
		}
ef5584
ef5584
		$l_type = strtoupper($mode);
ef5584
ef5584
		if (!$name)
ef5584
		{
ef5584
			$error[] = $user->lang[$l_type . '_ERR_STYLE_NAME'];
ef5584
		}
ef5584
ef5584
		// Check length settings
ef5584
		if (utf8_strlen($name) > 30)
ef5584
		{
ef5584
			$error[] = $user->lang[$l_type . '_ERR_NAME_LONG'];
ef5584
		}
ef5584
ef5584
		if (utf8_strlen($copyright) > 60)
ef5584
		{
ef5584
			$error[] = $user->lang[$l_type . '_ERR_COPY_LONG'];
ef5584
		}
ef5584
ef5584
		// Check if the name already exist
ef5584
		$sql = "SELECT {$mode}_id
ef5584
			FROM $sql_from
ef5584
			WHERE {$mode}_name = '" . $db->sql_escape($name) . "'";
ef5584
		$result = $db->sql_query($sql);
ef5584
		$row = $db->sql_fetchrow($result);
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
ef5584
		if ($row)
ef5584
		{
ef5584
			// If it exist, we just use the style on installation
ef5584
			if ($action == 'install')
ef5584
			{
ef5584
				$id = $row[$mode . '_id'];
ef5584
				return false;
ef5584
			}
ef5584
ef5584
			$error[] = $user->lang[$l_type . '_ERR_NAME_EXIST'];
ef5584
		}
ef5584
ef5584
		if (isset($cfg_data['inherit_from']) && $cfg_data['inherit_from'])
ef5584
		{
ef5584
			$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path, {$mode}_storedb
ef5584
				FROM $sql_from
ef5584
				WHERE {$mode}_name = '" . $db->sql_escape($cfg_data['inherit_from']) . "'
ef5584
					AND {$mode}_inherits_id = 0";
ef5584
			$result = $db->sql_query($sql);
ef5584
			$row = $db->sql_fetchrow($result);
ef5584
			$db->sql_freeresult($result);
ef5584
			if (!$row)
ef5584
			{
ef5584
				$error[] = sprintf($user->lang[$l_type . '_ERR_REQUIRED_OR_INCOMPLETE'], $cfg_data['inherit_from']);
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$inherit_id = $row["{$mode}_id"];
ef5584
				$inherit_path = $row["{$mode}_path"];
ef5584
				$cfg_data['store_db'] = $row["{$mode}_storedb"];
ef5584
				$store_db = $row["{$mode}_storedb"];
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$inherit_id = 0;
ef5584
			$inherit_path = '';
ef5584
		}
ef5584
ef5584
ef5584
		if (sizeof($error))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		$sql_ary = array(
ef5584
			$mode . '_name'			=> $name,
ef5584
			$mode . '_copyright'	=> $copyright,
ef5584
			$mode . '_path'			=> $path,
ef5584
		);
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'template':
ef5584
				// We check if the template author defined a different bitfield
ef5584
				if (!empty($cfg_data['template_bitfield']))
ef5584
				{
ef5584
					$sql_ary['bbcode_bitfield'] = $cfg_data['template_bitfield'];
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$sql_ary['bbcode_bitfield'] = TEMPLATE_BITFIELD;
ef5584
				}
ef5584
ef5584
				// We set a pre-defined bitfield here which we may use further in 3.2
ef5584
				$sql_ary += array(
ef5584
					'template_storedb'		=> $store_db,
ef5584
				);
ef5584
				if (isset($cfg_data['inherit_from']) && $cfg_data['inherit_from'])
ef5584
				{
ef5584
					$sql_ary += array(
ef5584
						'template_inherits_id'	=> $inherit_id,
ef5584
						'template_inherit_path' => $inherit_path,
ef5584
					);
ef5584
				}
ef5584
			break;
ef5584
ef5584
			case 'theme':
ef5584
				// We are only interested in the theme configuration for now
ef5584
ef5584
				if (isset($cfg_data['parse_css_file']) && $cfg_data['parse_css_file'])
ef5584
				{
ef5584
					$store_db = 1;
ef5584
				}
ef5584
ef5584
				$sql_ary += array(
ef5584
					'theme_storedb'	=> $store_db,
ef5584
					'theme_data'	=> ($store_db) ? $this->db_theme_data($sql_ary, false, $root_path) : '',
ef5584
					'theme_mtime'	=> (int) filemtime("{$phpbb_root_path}styles/$path/theme/stylesheet.css")
ef5584
				);
ef5584
			break;
ef5584
ef5584
			// all the heavy lifting is done later
ef5584
			case 'imageset':
ef5584
			break;
ef5584
		}
ef5584
ef5584
		$db->sql_transaction('begin');
ef5584
ef5584
		$sql = "INSERT INTO $sql_from
ef5584
			" . $db->sql_build_array('INSERT', $sql_ary);
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		$id = $db->sql_nextid();
ef5584
ef5584
		if ($mode == 'template' && $store_db)
ef5584
		{
ef5584
			$filelist = filelist("{$root_path}template", '', 'html');
ef5584
			$this->store_templates('insert', $id, $path, $filelist);
ef5584
		}
ef5584
		else if ($mode == 'imageset')
ef5584
		{
ef5584
			$cfg_data = parse_cfg_file("$root_path$mode/imageset.cfg");
ef5584
ef5584
			$imageset_definitions = array();
ef5584
			foreach ($this->imageset_keys as $topic => $key_array)
ef5584
			{
ef5584
				$imageset_definitions = array_merge($imageset_definitions, $key_array);
ef5584
			}
ef5584
ef5584
			foreach ($cfg_data as $key => $value)
ef5584
			{
ef5584
				if (strpos($value, '*') !== false)
ef5584
				{
ef5584
					if (substr($value, -1, 1) === '*')
ef5584
					{
ef5584
						list($image_filename, $image_height) = explode('*', $value);
ef5584
						$image_width = 0;
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						list($image_filename, $image_height, $image_width) = explode('*', $value);
ef5584
					}
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$image_filename = $value;
ef5584
					$image_height = $image_width = 0;
ef5584
				}
ef5584
ef5584
				if (strpos($key, 'img_') === 0 && $image_filename)
ef5584
				{
ef5584
					$key = substr($key, 4);
ef5584
					if (in_array($key, $imageset_definitions))
ef5584
					{
ef5584
						$sql_ary = array(
ef5584
							'image_name'		=> $key,
ef5584
							'image_filename'	=> str_replace('{PATH}', "styles/$path/imageset/", trim($image_filename)),
ef5584
							'image_height'		=> (int) $image_height,
ef5584
							'image_width'		=> (int) $image_width,
ef5584
							'imageset_id'		=> (int) $id,
ef5584
							'image_lang'		=> '',
ef5584
						);
ef5584
						$db->sql_query('INSERT INTO ' . STYLES_IMAGESET_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
			unset($cfg_data);
ef5584
ef5584
			$sql = 'SELECT lang_dir
ef5584
				FROM ' . LANG_TABLE;
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				if (@file_exists("$root_path$mode/{$row['lang_dir']}/imageset.cfg"))
ef5584
				{
ef5584
					$cfg_data_imageset_data = parse_cfg_file("$root_path$mode/{$row['lang_dir']}/imageset.cfg");
ef5584
					foreach ($cfg_data_imageset_data as $image_name => $value)
ef5584
					{
ef5584
						if (strpos($value, '*') !== false)
ef5584
						{
ef5584
							if (substr($value, -1, 1) === '*')
ef5584
							{
ef5584
								list($image_filename, $image_height) = explode('*', $value);
ef5584
								$image_width = 0;
ef5584
							}
ef5584
							else
ef5584
							{
ef5584
								list($image_filename, $image_height, $image_width) = explode('*', $value);
ef5584
							}
ef5584
						}
ef5584
						else
ef5584
						{
ef5584
							$image_filename = $value;
ef5584
							$image_height = $image_width = 0;
ef5584
						}
ef5584
ef5584
						if (strpos($image_name, 'img_') === 0 && $image_filename)
ef5584
						{
ef5584
							$image_name = substr($image_name, 4);
ef5584
							if (in_array($image_name, $imageset_definitions))
ef5584
							{
ef5584
								$sql_ary = array(
ef5584
									'image_name'		=> $image_name,
ef5584
									'image_filename'	=> $image_filename,
ef5584
									'image_height'		=> (int) $image_height,
ef5584
									'image_width'		=> (int) $image_width,
ef5584
									'imageset_id'		=> (int) $id,
ef5584
									'image_lang'		=> $row['lang_dir'],
ef5584
								);
ef5584
								$db->sql_query('INSERT INTO ' . STYLES_IMAGESET_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
ef5584
							}
ef5584
						}
ef5584
					}
ef5584
					unset($cfg_data_imageset_data);
ef5584
				}
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
		}
ef5584
ef5584
		$db->sql_transaction('commit');
ef5584
ef5584
		$log = ($store_db) ? 'LOG_' . $l_type . '_ADD_DB' : 'LOG_' . $l_type . '_ADD_FS';
ef5584
		add_log('admin', $log, $name);
ef5584
ef5584
		// Return store_db in case it had to be altered
ef5584
		return $store_db;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Checks downwards dependencies
ef5584
	*
ef5584
	* @access public
ef5584
	* @param string $mode The element type to check - only template is supported
ef5584
	* @param int $id The template id
ef5584
	* @returns false if no component inherits, array with name, path and id for each subtemplate otherwise
ef5584
	*/
ef5584
	function check_inheritance($mode, $id)
ef5584
	{
ef5584
		global $db;
ef5584
ef5584
		$l_type = strtoupper($mode);
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'template':
ef5584
				$sql_from = STYLES_TEMPLATE_TABLE;
ef5584
			break;
ef5584
ef5584
			case 'theme':
ef5584
				$sql_from = STYLES_THEME_TABLE;
ef5584
			break;
ef5584
ef5584
			case 'imageset':
ef5584
				$sql_from = STYLES_IMAGESET_TABLE;
ef5584
			break;
ef5584
		}
ef5584
ef5584
		$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
ef5584
			FROM $sql_from
ef5584
			WHERE {$mode}_inherits_id = " . (int) $id;
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$names = array();
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
ef5584
			$names[$row["{$mode}_id"]] = array(
ef5584
				"{$mode}_id" => $row["{$mode}_id"],
ef5584
				"{$mode}_name" => $row["{$mode}_name"],
ef5584
				"{$mode}_path" => $row["{$mode}_path"],
ef5584
			);
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		if (sizeof($names))
ef5584
		{
ef5584
			return $names;
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Checks upwards dependencies
ef5584
	*
ef5584
	* @access public
ef5584
	* @param string $mode The element type to check - only template is supported
ef5584
	* @param int $id The template id
ef5584
	* @returns false if the component does not inherit, array with name, path and id otherwise
ef5584
	*/
ef5584
	function get_super($mode, $id)
ef5584
	{
ef5584
		global $db;
ef5584
ef5584
		$l_type = strtoupper($mode);
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'template':
ef5584
				$sql_from = STYLES_TEMPLATE_TABLE;
ef5584
			break;
ef5584
ef5584
			case 'theme':
ef5584
				$sql_from = STYLES_THEME_TABLE;
ef5584
			break;
ef5584
ef5584
			case 'imageset':
ef5584
				$sql_from = STYLES_IMAGESET_TABLE;
ef5584
			break;
ef5584
		}
ef5584
ef5584
ef5584
		$sql = "SELECT {$mode}_inherits_id
ef5584
			FROM $sql_from
ef5584
			WHERE {$mode}_id = " . (int) $id;
ef5584
		$result = $db->sql_query_limit($sql, 1);
ef5584
ef5584
		if ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$db->sql_freeresult($result);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		$super_id = $row["{$mode}_inherits_id"];
ef5584
ef5584
		$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
ef5584
			FROM $sql_from
ef5584
			WHERE {$mode}_id = " . (int) $super_id;
ef5584
ef5584
		$result = $db->sql_query_limit($sql, 1);
ef5584
		if ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$db->sql_freeresult($result);
ef5584
			return $row;
ef5584
		}
ef5584
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Moves a template set and its subtemplates to the database
ef5584
	*
ef5584
	* @access public
ef5584
	* @param string $mode The component to move - only template is supported
ef5584
	* @param int $id The template id
ef5584
	*/
ef5584
	function store_in_db($mode, $id)
ef5584
	{
ef5584
		global $db, $user;
ef5584
ef5584
		$error = array();
ef5584
		$l_type = strtoupper($mode);
ef5584
		if ($super = $this->get_super($mode, $id))
ef5584
		{
ef5584
			$error[] = (sprintf($user->lang["{$l_type}_INHERITS"], $super['template_name']));
ef5584
			return $error;
ef5584
		}
ef5584
ef5584
		$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
ef5584
			FROM " . STYLES_TEMPLATE_TABLE . '
ef5584
			WHERE template_id = ' . (int) $id;
ef5584
ef5584
		$result = $db->sql_query_limit($sql, 1);
ef5584
		if ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$db->sql_freeresult($result);
ef5584
			$subs = $this->check_inheritance($mode, $id);
ef5584
ef5584
			$this->_store_in_db($mode, $id, $row["{$mode}_path"]);
ef5584
			if ($subs && sizeof($subs))
ef5584
			{
ef5584
				foreach ($subs as $sub_id => $sub)
ef5584
				{
ef5584
					if ($err = $this->_store_in_db($mode, $sub["{$mode}_id"], $sub["{$mode}_path"]))
ef5584
					{
ef5584
						$error[] = $err;
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
		if (sizeof($error))
ef5584
		{
ef5584
			return $error;
ef5584
		}
ef5584
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Moves a template set to the database
ef5584
	*
ef5584
	* @access private
ef5584
	* @param string $mode The component to move - only template is supported
ef5584
	* @param int $id The template id
ef5584
	* @param string $path TThe path to the template files
ef5584
	*/
ef5584
	function _store_in_db($mode, $id, $path)
ef5584
	{
ef5584
		global $phpbb_root_path, $db;
ef5584
ef5584
		$filelist = filelist("{$phpbb_root_path}styles/{$path}/template", '', 'html');
ef5584
		$this->store_templates('insert', $id, $path, $filelist);
ef5584
ef5584
		// Okay, we do the query here -shouldn't be triggered often.
ef5584
		$sql = 'UPDATE ' . STYLES_TEMPLATE_TABLE . '
ef5584
						SET template_storedb = 1
ef5584
						WHERE template_id = ' . $id;
ef5584
		$db->sql_query($sql);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Moves a template set and its subtemplates to the filesystem
ef5584
	*
ef5584
	* @access public
ef5584
	* @param string $mode The component to move - only template is supported
ef5584
	* @param int $id The template id
ef5584
	*/
ef5584
	function store_in_fs($mode, $id)
ef5584
	{
ef5584
		global $db, $user;
ef5584
ef5584
		$error = array();
ef5584
		$l_type = strtoupper($mode);
ef5584
		if ($super = $this->get_super($mode, $id))
ef5584
		{
ef5584
			$error[] = (sprintf($user->lang["{$l_type}_INHERITS"], $super['template_name']));
ef5584
			return($error);
ef5584
		}
ef5584
ef5584
		$sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
ef5584
			FROM " . STYLES_TEMPLATE_TABLE . '
ef5584
			WHERE template_id = ' . (int) $id;
ef5584
ef5584
		$result = $db->sql_query_limit($sql, 1);
ef5584
		if ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$db->sql_freeresult($result);
ef5584
			if (!sizeof($error))
ef5584
			{
ef5584
				$subs = $this->check_inheritance($mode, $id);
ef5584
ef5584
				$this->_store_in_fs($mode, $id, $row["{$mode}_path"]);
ef5584
ef5584
				if ($subs && sizeof($subs))
ef5584
				{
ef5584
					foreach ($subs as $sub_id => $sub)
ef5584
					{
ef5584
						$this->_store_in_fs($mode, $sub["{$mode}_id"], $sub["{$mode}_path"]);
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
			if (sizeof($error))
ef5584
			{
ef5584
				$this->store_in_db($id, $mode);
ef5584
				return $error;
ef5584
			}
ef5584
		}
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Moves a template set to the filesystem
ef5584
	*
ef5584
	* @access private
ef5584
	* @param string $mode The component to move - only template is supported
ef5584
	* @param int $id The template id
ef5584
	* @param string $path The path to the template
ef5584
	*/
ef5584
	function _store_in_fs($mode, $id, $path)
ef5584
	{
ef5584
		global $phpbb_root_path, $db, $user, $safe_mode;
ef5584
ef5584
		$store_db = 0;
ef5584
		$error = array();
ef5584
		if (!$safe_mode && @is_writable("{$phpbb_root_path}styles/{$path}/template"))
ef5584
		{
ef5584
			$sql = 'SELECT *
ef5584
					FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
ef5584
					WHERE template_id = $id";
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				if (!($fp = @fopen("{$phpbb_root_path}styles/{$path}/template/" . $row['template_filename'], 'wb')))
ef5584
				{
ef5584
					$store_db = 1;
ef5584
					$error[] = $user->lang['EDIT_TEMPLATE_STORED_DB'];
ef5584
					break;
ef5584
				}
ef5584
ef5584
				fwrite($fp, $row['template_data']);
ef5584
				fclose($fp);
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			if (!$store_db)
ef5584
			{
ef5584
				$sql = 'DELETE FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
ef5584
						WHERE template_id = $id";
ef5584
				$db->sql_query($sql);
ef5584
			}
ef5584
		}
ef5584
		if (sizeof($error))
ef5584
		{
ef5584
			return $error;
ef5584
		}
ef5584
		$sql = 'UPDATE ' . STYLES_TEMPLATE_TABLE . '
ef5584
				SET template_storedb = 0
ef5584
				WHERE template_id = ' . $id;
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		return false;
ef5584
	}
ef5584
ef5584
}
ef5584
ef5584
?>