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