annotate forum/Sources/Themes.php @ 76:e3e11437ecea website

Add forum code
author Chris Cannam
date Sun, 07 Jul 2013 11:25:48 +0200
parents
children
rev   line source
Chris@76 1 <?php
Chris@76 2
Chris@76 3 /**
Chris@76 4 * Simple Machines Forum (SMF)
Chris@76 5 *
Chris@76 6 * @package SMF
Chris@76 7 * @author Simple Machines http://www.simplemachines.org
Chris@76 8 * @copyright 2011 Simple Machines
Chris@76 9 * @license http://www.simplemachines.org/about/smf/license.php BSD
Chris@76 10 *
Chris@76 11 * @version 2.0.4
Chris@76 12 */
Chris@76 13
Chris@76 14 if (!defined('SMF'))
Chris@76 15 die('Hacking attempt...');
Chris@76 16
Chris@76 17 /* This file concerns itself almost completely with theme administration.
Chris@76 18 Its tasks include changing theme settings, installing and removing
Chris@76 19 themes, choosing the current theme, and editing themes. This is done in:
Chris@76 20
Chris@76 21 void ThemesMain()
Chris@76 22 - manages the action and delegates control to the proper sub action.
Chris@76 23 - loads both the Themes and Settings language files.
Chris@76 24 - checks the session by GET or POST to verify the sent data.
Chris@76 25 - requires the user not be a guest.
Chris@76 26 - is accessed via ?action=admin;area=theme.
Chris@76 27
Chris@76 28 void ThemeAdmin()
Chris@76 29 - administrates themes and their settings, as well as global theme
Chris@76 30 settings.
Chris@76 31 - sets the settings theme_allow, theme_guests, and knownThemes.
Chris@76 32 - loads the template Themes.
Chris@76 33 - requires the admin_forum permission.
Chris@76 34 - accessed with ?action=admin;area=theme;sa=admin.
Chris@76 35
Chris@76 36 void ThemeList()
Chris@76 37 - lists the available themes.
Chris@76 38 - provides an interface to reset the paths of all the installed themes.
Chris@76 39
Chris@76 40 void SetThemeOptions()
Chris@76 41 // !!!
Chris@76 42
Chris@76 43 void SetThemeSettings()
Chris@76 44 - saves and requests global theme settings. ($settings)
Chris@76 45 - loads the Admin language file.
Chris@76 46 - calls ThemeAdmin() if no theme is specified. (the theme center.)
Chris@76 47 - requires an administrator.
Chris@76 48 - accessed with ?action=admin;area=theme;sa=settings&th=xx.
Chris@76 49
Chris@76 50 void RemoveTheme()
Chris@76 51 - removes an installed theme.
Chris@76 52 - requires an administrator.
Chris@76 53 - accessed with ?action=admin;area=theme;sa=remove.
Chris@76 54
Chris@76 55 void PickTheme()
Chris@76 56 - allows user or administrator to pick a new theme with an interface.
Chris@76 57 - can edit everyone's (u = 0), guests' (u = -1), or a specific user's.
Chris@76 58 - uses the Themes template. (pick sub template.)
Chris@76 59 - accessed with ?action=admin;area=theme;sa=pick.
Chris@76 60
Chris@76 61 void ThemeInstall()
Chris@76 62 - installs new themes, either from a gzip or copy of the default.
Chris@76 63 - requires an administrator.
Chris@76 64 - puts themes in $boardurl/Themes.
Chris@76 65 - assumes the gzip has a root directory in it. (ie default.)
Chris@76 66 - accessed with ?action=admin;area=theme;sa=install.
Chris@76 67
Chris@76 68 void WrapAction()
Chris@76 69 - allows the theme to take care of actions.
Chris@76 70 - happens if $settings['catch_action'] is set and action isn't found
Chris@76 71 in the action array.
Chris@76 72 - can use a template, layers, sub_template, filename, and/or function.
Chris@76 73
Chris@76 74 void SetJavaScript()
Chris@76 75 - sets a theme option without outputting anything.
Chris@76 76 - can be used with javascript, via a dummy image... (which doesn't
Chris@76 77 require the page to reload.)
Chris@76 78 - requires someone who is logged in.
Chris@76 79 - accessed via ?action=jsoption;var=variable;val=value;session_var=sess_id.
Chris@76 80 - does not log access to the Who's Online log. (in index.php..)
Chris@76 81
Chris@76 82 void EditTheme()
Chris@76 83 - shows an interface for editing the templates.
Chris@76 84 - uses the Themes template and edit_template/edit_style sub template.
Chris@76 85 - accessed via ?action=admin;area=theme;sa=edit
Chris@76 86
Chris@76 87 function convert_template($output_dir, $old_template = '')
Chris@76 88 // !!!
Chris@76 89
Chris@76 90 function phpcodefix(string string)
Chris@76 91 // !!!
Chris@76 92
Chris@76 93 function makeStyleChanges(&$old_template)
Chris@76 94 // !!!
Chris@76 95
Chris@76 96 // !!! Update this for the new package manager?
Chris@76 97 Creating and distributing theme packages:
Chris@76 98 ---------------------------------------------------------------------------
Chris@76 99 There isn't that much required to package and distribute your own
Chris@76 100 themes... just do the following:
Chris@76 101 - create a theme_info.xml file, with the root element theme-info.
Chris@76 102 - its name should go in a name element, just like description.
Chris@76 103 - your name should go in author. (email in the email attribute.)
Chris@76 104 - any support website for the theme should be in website.
Chris@76 105 - layers and templates (non-default) should go in those elements ;).
Chris@76 106 - if the images dir isn't images, specify in the images element.
Chris@76 107 - any extra rows for themes should go in extra, serialized.
Chris@76 108 (as in array(variable => value).)
Chris@76 109 - tar and gzip the directory - and you're done!
Chris@76 110 - please include any special license in a license.txt file.
Chris@76 111 // !!! Thumbnail?
Chris@76 112 */
Chris@76 113
Chris@76 114 // Subaction handler.
Chris@76 115 function ThemesMain()
Chris@76 116 {
Chris@76 117 global $txt, $context, $scripturl;
Chris@76 118
Chris@76 119 // Load the important language files...
Chris@76 120 loadLanguage('Themes');
Chris@76 121 loadLanguage('Settings');
Chris@76 122
Chris@76 123 // No funny business - guests only.
Chris@76 124 is_not_guest();
Chris@76 125
Chris@76 126 // Default the page title to Theme Administration by default.
Chris@76 127 $context['page_title'] = $txt['themeadmin_title'];
Chris@76 128
Chris@76 129 // Theme administration, removal, choice, or installation...
Chris@76 130 $subActions = array(
Chris@76 131 'admin' => 'ThemeAdmin',
Chris@76 132 'list' => 'ThemeList',
Chris@76 133 'reset' => 'SetThemeOptions',
Chris@76 134 'settings' => 'SetThemeSettings',
Chris@76 135 'options' => 'SetThemeOptions',
Chris@76 136 'install' => 'ThemeInstall',
Chris@76 137 'remove' => 'RemoveTheme',
Chris@76 138 'pick' => 'PickTheme',
Chris@76 139 'edit' => 'EditTheme',
Chris@76 140 'copy' => 'CopyTemplate',
Chris@76 141 );
Chris@76 142
Chris@76 143 // !!! Layout Settings?
Chris@76 144 if (!empty($context['admin_menu_name']))
Chris@76 145 {
Chris@76 146 $context[$context['admin_menu_name']]['tab_data'] = array(
Chris@76 147 'title' => $txt['themeadmin_title'],
Chris@76 148 'help' => 'themes',
Chris@76 149 'description' => $txt['themeadmin_description'],
Chris@76 150 'tabs' => array(
Chris@76 151 'admin' => array(
Chris@76 152 'description' => $txt['themeadmin_admin_desc'],
Chris@76 153 ),
Chris@76 154 'list' => array(
Chris@76 155 'description' => $txt['themeadmin_list_desc'],
Chris@76 156 ),
Chris@76 157 'reset' => array(
Chris@76 158 'description' => $txt['themeadmin_reset_desc'],
Chris@76 159 ),
Chris@76 160 'edit' => array(
Chris@76 161 'description' => $txt['themeadmin_edit_desc'],
Chris@76 162 ),
Chris@76 163 ),
Chris@76 164 );
Chris@76 165 }
Chris@76 166
Chris@76 167 // Follow the sa or just go to administration.
Chris@76 168 if (isset($_GET['sa']) && !empty($subActions[$_GET['sa']]))
Chris@76 169 $subActions[$_GET['sa']]();
Chris@76 170 else
Chris@76 171 $subActions['admin']();
Chris@76 172 }
Chris@76 173
Chris@76 174 function ThemeAdmin()
Chris@76 175 {
Chris@76 176 global $context, $boarddir, $modSettings, $smcFunc;
Chris@76 177
Chris@76 178 loadLanguage('Admin');
Chris@76 179 isAllowedTo('admin_forum');
Chris@76 180
Chris@76 181 // If we aren't submitting - that is, if we are about to...
Chris@76 182 if (!isset($_POST['submit']))
Chris@76 183 {
Chris@76 184 loadTemplate('Themes');
Chris@76 185
Chris@76 186 // Make our known themes a little easier to work with.
Chris@76 187 $knownThemes = !empty($modSettings['knownThemes']) ? explode(',',$modSettings['knownThemes']) : array();
Chris@76 188
Chris@76 189 // Load up all the themes.
Chris@76 190 $request = $smcFunc['db_query']('', '
Chris@76 191 SELECT id_theme, value AS name
Chris@76 192 FROM {db_prefix}themes
Chris@76 193 WHERE variable = {string:name}
Chris@76 194 AND id_member = {int:no_member}
Chris@76 195 ORDER BY id_theme',
Chris@76 196 array(
Chris@76 197 'no_member' => 0,
Chris@76 198 'name' => 'name',
Chris@76 199 )
Chris@76 200 );
Chris@76 201 $context['themes'] = array();
Chris@76 202 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 203 $context['themes'][] = array(
Chris@76 204 'id' => $row['id_theme'],
Chris@76 205 'name' => $row['name'],
Chris@76 206 'known' => in_array($row['id_theme'], $knownThemes),
Chris@76 207 );
Chris@76 208 $smcFunc['db_free_result']($request);
Chris@76 209
Chris@76 210 // Can we create a new theme?
Chris@76 211 $context['can_create_new'] = is_writable($boarddir . '/Themes');
Chris@76 212 $context['new_theme_dir'] = substr(realpath($boarddir . '/Themes/default'), 0, -7);
Chris@76 213
Chris@76 214 // Look for a non existent theme directory. (ie theme87.)
Chris@76 215 $theme_dir = $boarddir . '/Themes/theme';
Chris@76 216 $i = 1;
Chris@76 217 while (file_exists($theme_dir . $i))
Chris@76 218 $i++;
Chris@76 219 $context['new_theme_name'] = 'theme' . $i;
Chris@76 220 }
Chris@76 221 else
Chris@76 222 {
Chris@76 223 checkSession();
Chris@76 224
Chris@76 225 if (isset($_POST['options']['known_themes']))
Chris@76 226 foreach ($_POST['options']['known_themes'] as $key => $id)
Chris@76 227 $_POST['options']['known_themes'][$key] = (int) $id;
Chris@76 228 else
Chris@76 229 fatal_lang_error('themes_none_selectable', false);
Chris@76 230
Chris@76 231 if (!in_array($_POST['options']['theme_guests'], $_POST['options']['known_themes']))
Chris@76 232 fatal_lang_error('themes_default_selectable', false);
Chris@76 233
Chris@76 234 // Commit the new settings.
Chris@76 235 updateSettings(array(
Chris@76 236 'theme_allow' => $_POST['options']['theme_allow'],
Chris@76 237 'theme_guests' => $_POST['options']['theme_guests'],
Chris@76 238 'knownThemes' => implode(',', $_POST['options']['known_themes']),
Chris@76 239 ));
Chris@76 240 if ((int) $_POST['theme_reset'] == 0 || in_array($_POST['theme_reset'], $_POST['options']['known_themes']))
Chris@76 241 updateMemberData(null, array('id_theme' => (int) $_POST['theme_reset']));
Chris@76 242
Chris@76 243 redirectexit('action=admin;area=theme;' . $context['session_var'] . '=' . $context['session_id'] . ';sa=admin');
Chris@76 244 }
Chris@76 245 }
Chris@76 246
Chris@76 247 function ThemeList()
Chris@76 248 {
Chris@76 249 global $context, $boarddir, $boardurl, $smcFunc;
Chris@76 250
Chris@76 251 loadLanguage('Admin');
Chris@76 252 isAllowedTo('admin_forum');
Chris@76 253
Chris@76 254 if (isset($_POST['submit']))
Chris@76 255 {
Chris@76 256 checkSession();
Chris@76 257
Chris@76 258 $request = $smcFunc['db_query']('', '
Chris@76 259 SELECT id_theme, variable, value
Chris@76 260 FROM {db_prefix}themes
Chris@76 261 WHERE variable IN ({string:theme_dir}, {string:theme_url}, {string:images_url}, {string:base_theme_dir}, {string:base_theme_url}, {string:base_images_url})
Chris@76 262 AND id_member = {int:no_member}',
Chris@76 263 array(
Chris@76 264 'no_member' => 0,
Chris@76 265 'theme_dir' => 'theme_dir',
Chris@76 266 'theme_url' => 'theme_url',
Chris@76 267 'images_url' => 'images_url',
Chris@76 268 'base_theme_dir' => 'base_theme_dir',
Chris@76 269 'base_theme_url' => 'base_theme_url',
Chris@76 270 'base_images_url' => 'base_images_url',
Chris@76 271 )
Chris@76 272 );
Chris@76 273 $themes = array();
Chris@76 274 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 275 $themes[$row['id_theme']][$row['variable']] = $row['value'];
Chris@76 276 $smcFunc['db_free_result']($request);
Chris@76 277
Chris@76 278 $setValues = array();
Chris@76 279 foreach ($themes as $id => $theme)
Chris@76 280 {
Chris@76 281 if (file_exists($_POST['reset_dir'] . '/' . basename($theme['theme_dir'])))
Chris@76 282 {
Chris@76 283 $setValues[] = array($id, 0, 'theme_dir', realpath($_POST['reset_dir'] . '/' . basename($theme['theme_dir'])));
Chris@76 284 $setValues[] = array($id, 0, 'theme_url', $_POST['reset_url'] . '/' . basename($theme['theme_dir']));
Chris@76 285 $setValues[] = array($id, 0, 'images_url', $_POST['reset_url'] . '/' . basename($theme['theme_dir']) . '/' . basename($theme['images_url']));
Chris@76 286 }
Chris@76 287
Chris@76 288 if (isset($theme['base_theme_dir']) && file_exists($_POST['reset_dir'] . '/' . basename($theme['base_theme_dir'])))
Chris@76 289 {
Chris@76 290 $setValues[] = array($id, 0, 'base_theme_dir', realpath($_POST['reset_dir'] . '/' . basename($theme['base_theme_dir'])));
Chris@76 291 $setValues[] = array($id, 0, 'base_theme_url', $_POST['reset_url'] . '/' . basename($theme['base_theme_dir']));
Chris@76 292 $setValues[] = array($id, 0, 'base_images_url', $_POST['reset_url'] . '/' . basename($theme['base_theme_dir']) . '/' . basename($theme['base_images_url']));
Chris@76 293 }
Chris@76 294
Chris@76 295 cache_put_data('theme_settings-' . $id, null, 90);
Chris@76 296 }
Chris@76 297
Chris@76 298 if (!empty($setValues))
Chris@76 299 {
Chris@76 300 $smcFunc['db_insert']('replace',
Chris@76 301 '{db_prefix}themes',
Chris@76 302 array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
Chris@76 303 $setValues,
Chris@76 304 array('id_theme', 'variable', 'id_member')
Chris@76 305 );
Chris@76 306 }
Chris@76 307
Chris@76 308 redirectexit('action=admin;area=theme;sa=list;' . $context['session_var'] . '=' . $context['session_id']);
Chris@76 309 }
Chris@76 310
Chris@76 311 loadTemplate('Themes');
Chris@76 312
Chris@76 313 $request = $smcFunc['db_query']('', '
Chris@76 314 SELECT id_theme, variable, value
Chris@76 315 FROM {db_prefix}themes
Chris@76 316 WHERE variable IN ({string:name}, {string:theme_dir}, {string:theme_url}, {string:images_url})
Chris@76 317 AND id_member = {int:no_member}',
Chris@76 318 array(
Chris@76 319 'no_member' => 0,
Chris@76 320 'name' => 'name',
Chris@76 321 'theme_dir' => 'theme_dir',
Chris@76 322 'theme_url' => 'theme_url',
Chris@76 323 'images_url' => 'images_url',
Chris@76 324 )
Chris@76 325 );
Chris@76 326 $context['themes'] = array();
Chris@76 327 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 328 {
Chris@76 329 if (!isset($context['themes'][$row['id_theme']]))
Chris@76 330 $context['themes'][$row['id_theme']] = array(
Chris@76 331 'id' => $row['id_theme'],
Chris@76 332 );
Chris@76 333 $context['themes'][$row['id_theme']][$row['variable']] = $row['value'];
Chris@76 334 }
Chris@76 335 $smcFunc['db_free_result']($request);
Chris@76 336
Chris@76 337 foreach ($context['themes'] as $i => $theme)
Chris@76 338 {
Chris@76 339 $context['themes'][$i]['theme_dir'] = realpath($context['themes'][$i]['theme_dir']);
Chris@76 340
Chris@76 341 if (file_exists($context['themes'][$i]['theme_dir'] . '/index.template.php'))
Chris@76 342 {
Chris@76 343 // Fetch the header... a good 256 bytes should be more than enough.
Chris@76 344 $fp = fopen($context['themes'][$i]['theme_dir'] . '/index.template.php', 'rb');
Chris@76 345 $header = fread($fp, 256);
Chris@76 346 fclose($fp);
Chris@76 347
Chris@76 348 // Can we find a version comment, at all?
Chris@76 349 if (preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $header, $match) == 1)
Chris@76 350 $context['themes'][$i]['version'] = $match[1];
Chris@76 351 }
Chris@76 352
Chris@76 353 $context['themes'][$i]['valid_path'] = file_exists($context['themes'][$i]['theme_dir']) && is_dir($context['themes'][$i]['theme_dir']);
Chris@76 354 }
Chris@76 355
Chris@76 356 $context['reset_dir'] = realpath($boarddir . '/Themes');
Chris@76 357 $context['reset_url'] = $boardurl . '/Themes';
Chris@76 358
Chris@76 359 $context['sub_template'] = 'list_themes';
Chris@76 360 }
Chris@76 361
Chris@76 362 // Administrative global settings.
Chris@76 363 function SetThemeOptions()
Chris@76 364 {
Chris@76 365 global $txt, $context, $settings, $modSettings, $smcFunc;
Chris@76 366
Chris@76 367 $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (isset($_GET['id']) ? (int) $_GET['id'] : 0);
Chris@76 368
Chris@76 369 isAllowedTo('admin_forum');
Chris@76 370
Chris@76 371 if (empty($_GET['th']) && empty($_GET['id']))
Chris@76 372 {
Chris@76 373 $request = $smcFunc['db_query']('', '
Chris@76 374 SELECT id_theme, variable, value
Chris@76 375 FROM {db_prefix}themes
Chris@76 376 WHERE variable IN ({string:name}, {string:theme_dir})
Chris@76 377 AND id_member = {int:no_member}',
Chris@76 378 array(
Chris@76 379 'no_member' => 0,
Chris@76 380 'name' => 'name',
Chris@76 381 'theme_dir' => 'theme_dir',
Chris@76 382 )
Chris@76 383 );
Chris@76 384 $context['themes'] = array();
Chris@76 385 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 386 {
Chris@76 387 if (!isset($context['themes'][$row['id_theme']]))
Chris@76 388 $context['themes'][$row['id_theme']] = array(
Chris@76 389 'id' => $row['id_theme'],
Chris@76 390 'num_default_options' => 0,
Chris@76 391 'num_members' => 0,
Chris@76 392 );
Chris@76 393 $context['themes'][$row['id_theme']][$row['variable']] = $row['value'];
Chris@76 394 }
Chris@76 395 $smcFunc['db_free_result']($request);
Chris@76 396
Chris@76 397 $request = $smcFunc['db_query']('', '
Chris@76 398 SELECT id_theme, COUNT(*) AS value
Chris@76 399 FROM {db_prefix}themes
Chris@76 400 WHERE id_member = {int:guest_member}
Chris@76 401 GROUP BY id_theme',
Chris@76 402 array(
Chris@76 403 'guest_member' => -1,
Chris@76 404 )
Chris@76 405 );
Chris@76 406 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 407 $context['themes'][$row['id_theme']]['num_default_options'] = $row['value'];
Chris@76 408 $smcFunc['db_free_result']($request);
Chris@76 409
Chris@76 410 // Need to make sure we don't do custom fields.
Chris@76 411 $request = $smcFunc['db_query']('', '
Chris@76 412 SELECT col_name
Chris@76 413 FROM {db_prefix}custom_fields',
Chris@76 414 array(
Chris@76 415 )
Chris@76 416 );
Chris@76 417 $customFields = array();
Chris@76 418 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 419 $customFields[] = $row['col_name'];
Chris@76 420 $smcFunc['db_free_result']($request);
Chris@76 421 $customFieldsQuery = empty($customFields) ? '' : ('AND variable NOT IN ({array_string:custom_fields})');
Chris@76 422
Chris@76 423 $request = $smcFunc['db_query']('themes_count', '
Chris@76 424 SELECT COUNT(DISTINCT id_member) AS value, id_theme
Chris@76 425 FROM {db_prefix}themes
Chris@76 426 WHERE id_member > {int:no_member}
Chris@76 427 ' . $customFieldsQuery . '
Chris@76 428 GROUP BY id_theme',
Chris@76 429 array(
Chris@76 430 'no_member' => 0,
Chris@76 431 'custom_fields' => empty($customFields) ? array() : $customFields,
Chris@76 432 )
Chris@76 433 );
Chris@76 434 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 435 $context['themes'][$row['id_theme']]['num_members'] = $row['value'];
Chris@76 436 $smcFunc['db_free_result']($request);
Chris@76 437
Chris@76 438 // There has to be a Settings template!
Chris@76 439 foreach ($context['themes'] as $k => $v)
Chris@76 440 if (empty($v['theme_dir']) || (!file_exists($v['theme_dir'] . '/Settings.template.php') && empty($v['num_members'])))
Chris@76 441 unset($context['themes'][$k]);
Chris@76 442
Chris@76 443 loadTemplate('Themes');
Chris@76 444 $context['sub_template'] = 'reset_list';
Chris@76 445
Chris@76 446 return;
Chris@76 447 }
Chris@76 448
Chris@76 449 // Submit?
Chris@76 450 if (isset($_POST['submit']) && empty($_POST['who']))
Chris@76 451 {
Chris@76 452 checkSession();
Chris@76 453
Chris@76 454 if (empty($_POST['options']))
Chris@76 455 $_POST['options'] = array();
Chris@76 456 if (empty($_POST['default_options']))
Chris@76 457 $_POST['default_options'] = array();
Chris@76 458
Chris@76 459 // Set up the sql query.
Chris@76 460 $setValues = array();
Chris@76 461
Chris@76 462 foreach ($_POST['options'] as $opt => $val)
Chris@76 463 $setValues[] = array(-1, $_GET['th'], $opt, is_array($val) ? implode(',', $val) : $val);
Chris@76 464
Chris@76 465 $old_settings = array();
Chris@76 466 foreach ($_POST['default_options'] as $opt => $val)
Chris@76 467 {
Chris@76 468 $old_settings[] = $opt;
Chris@76 469
Chris@76 470 $setValues[] = array(-1, 1, $opt, is_array($val) ? implode(',', $val) : $val);
Chris@76 471 }
Chris@76 472
Chris@76 473 // If we're actually inserting something..
Chris@76 474 if (!empty($setValues))
Chris@76 475 {
Chris@76 476 // Are there options in non-default themes set that should be cleared?
Chris@76 477 if (!empty($old_settings))
Chris@76 478 $smcFunc['db_query']('', '
Chris@76 479 DELETE FROM {db_prefix}themes
Chris@76 480 WHERE id_theme != {int:default_theme}
Chris@76 481 AND id_member = {int:guest_member}
Chris@76 482 AND variable IN ({array_string:old_settings})',
Chris@76 483 array(
Chris@76 484 'default_theme' => 1,
Chris@76 485 'guest_member' => -1,
Chris@76 486 'old_settings' => $old_settings,
Chris@76 487 )
Chris@76 488 );
Chris@76 489
Chris@76 490 $smcFunc['db_insert']('replace',
Chris@76 491 '{db_prefix}themes',
Chris@76 492 array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
Chris@76 493 $setValues,
Chris@76 494 array('id_theme', 'variable', 'id_member')
Chris@76 495 );
Chris@76 496 }
Chris@76 497
Chris@76 498 cache_put_data('theme_settings-' . $_GET['th'], null, 90);
Chris@76 499 cache_put_data('theme_settings-1', null, 90);
Chris@76 500
Chris@76 501 redirectexit('action=admin;area=theme;' . $context['session_var'] . '=' . $context['session_id'] . ';sa=reset');
Chris@76 502 }
Chris@76 503 elseif (isset($_POST['submit']) && $_POST['who'] == 1)
Chris@76 504 {
Chris@76 505 checkSession();
Chris@76 506
Chris@76 507 $_POST['options'] = empty($_POST['options']) ? array() : $_POST['options'];
Chris@76 508 $_POST['options_master'] = empty($_POST['options_master']) ? array() : $_POST['options_master'];
Chris@76 509 $_POST['default_options'] = empty($_POST['default_options']) ? array() : $_POST['default_options'];
Chris@76 510 $_POST['default_options_master'] = empty($_POST['default_options_master']) ? array() : $_POST['default_options_master'];
Chris@76 511
Chris@76 512 $old_settings = array();
Chris@76 513 foreach ($_POST['default_options'] as $opt => $val)
Chris@76 514 {
Chris@76 515 if ($_POST['default_options_master'][$opt] == 0)
Chris@76 516 continue;
Chris@76 517 elseif ($_POST['default_options_master'][$opt] == 1)
Chris@76 518 {
Chris@76 519 // Delete then insert for ease of database compatibility!
Chris@76 520 $smcFunc['db_query']('substring', '
Chris@76 521 DELETE FROM {db_prefix}themes
Chris@76 522 WHERE id_theme = {int:default_theme}
Chris@76 523 AND id_member != {int:no_member}
Chris@76 524 AND variable = SUBSTRING({string:option}, 1, 255)',
Chris@76 525 array(
Chris@76 526 'default_theme' => 1,
Chris@76 527 'no_member' => 0,
Chris@76 528 'option' => $opt,
Chris@76 529 )
Chris@76 530 );
Chris@76 531 $smcFunc['db_query']('substring', '
Chris@76 532 INSERT INTO {db_prefix}themes
Chris@76 533 (id_member, id_theme, variable, value)
Chris@76 534 SELECT id_member, 1, SUBSTRING({string:option}, 1, 255), SUBSTRING({string:value}, 1, 65534)
Chris@76 535 FROM {db_prefix}members',
Chris@76 536 array(
Chris@76 537 'option' => $opt,
Chris@76 538 'value' => (is_array($val) ? implode(',', $val) : $val),
Chris@76 539 )
Chris@76 540 );
Chris@76 541
Chris@76 542 $old_settings[] = $opt;
Chris@76 543 }
Chris@76 544 elseif ($_POST['default_options_master'][$opt] == 2)
Chris@76 545 {
Chris@76 546 $smcFunc['db_query']('', '
Chris@76 547 DELETE FROM {db_prefix}themes
Chris@76 548 WHERE variable = {string:option_name}
Chris@76 549 AND id_member > {int:no_member}',
Chris@76 550 array(
Chris@76 551 'no_member' => 0,
Chris@76 552 'option_name' => $opt,
Chris@76 553 )
Chris@76 554 );
Chris@76 555 }
Chris@76 556 }
Chris@76 557
Chris@76 558 // Delete options from other themes.
Chris@76 559 if (!empty($old_settings))
Chris@76 560 $smcFunc['db_query']('', '
Chris@76 561 DELETE FROM {db_prefix}themes
Chris@76 562 WHERE id_theme != {int:default_theme}
Chris@76 563 AND id_member > {int:no_member}
Chris@76 564 AND variable IN ({array_string:old_settings})',
Chris@76 565 array(
Chris@76 566 'default_theme' => 1,
Chris@76 567 'no_member' => 0,
Chris@76 568 'old_settings' => $old_settings,
Chris@76 569 )
Chris@76 570 );
Chris@76 571
Chris@76 572 foreach ($_POST['options'] as $opt => $val)
Chris@76 573 {
Chris@76 574 if ($_POST['options_master'][$opt] == 0)
Chris@76 575 continue;
Chris@76 576 elseif ($_POST['options_master'][$opt] == 1)
Chris@76 577 {
Chris@76 578 // Delete then insert for ease of database compatibility - again!
Chris@76 579 $smcFunc['db_query']('substring', '
Chris@76 580 DELETE FROM {db_prefix}themes
Chris@76 581 WHERE id_theme = {int:current_theme}
Chris@76 582 AND id_member != {int:no_member}
Chris@76 583 AND variable = SUBSTRING({string:option}, 1, 255)',
Chris@76 584 array(
Chris@76 585 'current_theme' => $_GET['th'],
Chris@76 586 'no_member' => 0,
Chris@76 587 'option' => $opt,
Chris@76 588 )
Chris@76 589 );
Chris@76 590 $smcFunc['db_query']('substring', '
Chris@76 591 INSERT INTO {db_prefix}themes
Chris@76 592 (id_member, id_theme, variable, value)
Chris@76 593 SELECT id_member, {int:current_theme}, SUBSTRING({string:option}, 1, 255), SUBSTRING({string:value}, 1, 65534)
Chris@76 594 FROM {db_prefix}members',
Chris@76 595 array(
Chris@76 596 'current_theme' => $_GET['th'],
Chris@76 597 'option' => $opt,
Chris@76 598 'value' => (is_array($val) ? implode(',', $val) : $val),
Chris@76 599 )
Chris@76 600 );
Chris@76 601 }
Chris@76 602 elseif ($_POST['options_master'][$opt] == 2)
Chris@76 603 {
Chris@76 604 $smcFunc['db_query']('', '
Chris@76 605 DELETE FROM {db_prefix}themes
Chris@76 606 WHERE variable = {string:option}
Chris@76 607 AND id_member > {int:no_member}
Chris@76 608 AND id_theme = {int:current_theme}',
Chris@76 609 array(
Chris@76 610 'no_member' => 0,
Chris@76 611 'current_theme' => $_GET['th'],
Chris@76 612 'option' => $opt,
Chris@76 613 )
Chris@76 614 );
Chris@76 615 }
Chris@76 616 }
Chris@76 617
Chris@76 618 redirectexit('action=admin;area=theme;' . $context['session_var'] . '=' . $context['session_id'] . ';sa=reset');
Chris@76 619 }
Chris@76 620 elseif (!empty($_GET['who']) && $_GET['who'] == 2)
Chris@76 621 {
Chris@76 622 checkSession('get');
Chris@76 623
Chris@76 624 // Don't delete custom fields!!
Chris@76 625 if ($_GET['th'] == 1)
Chris@76 626 {
Chris@76 627 $request = $smcFunc['db_query']('', '
Chris@76 628 SELECT col_name
Chris@76 629 FROM {db_prefix}custom_fields',
Chris@76 630 array(
Chris@76 631 )
Chris@76 632 );
Chris@76 633 $customFields = array();
Chris@76 634 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 635 $customFields[] = $row['col_name'];
Chris@76 636 $smcFunc['db_free_result']($request);
Chris@76 637 }
Chris@76 638 $customFieldsQuery = empty($customFields) ? '' : ('AND variable NOT IN ({array_string:custom_fields})');
Chris@76 639
Chris@76 640 $smcFunc['db_query']('', '
Chris@76 641 DELETE FROM {db_prefix}themes
Chris@76 642 WHERE id_member > {int:no_member}
Chris@76 643 AND id_theme = {int:current_theme}
Chris@76 644 ' . $customFieldsQuery,
Chris@76 645 array(
Chris@76 646 'no_member' => 0,
Chris@76 647 'current_theme' => $_GET['th'],
Chris@76 648 'custom_fields' => empty($customFields) ? array() : $customFields,
Chris@76 649 )
Chris@76 650 );
Chris@76 651
Chris@76 652 redirectexit('action=admin;area=theme;' . $context['session_var'] . '=' . $context['session_id'] . ';sa=reset');
Chris@76 653 }
Chris@76 654
Chris@76 655 $old_id = $settings['theme_id'];
Chris@76 656 $old_settings = $settings;
Chris@76 657
Chris@76 658 loadTheme($_GET['th'], false);
Chris@76 659
Chris@76 660 loadLanguage('Profile');
Chris@76 661 //!!! Should we just move these options so they are no longer theme dependant?
Chris@76 662 loadLanguage('PersonalMessage');
Chris@76 663
Chris@76 664 // Let the theme take care of the settings.
Chris@76 665 loadTemplate('Settings');
Chris@76 666 loadSubTemplate('options');
Chris@76 667
Chris@76 668 $context['sub_template'] = 'set_options';
Chris@76 669 $context['page_title'] = $txt['theme_settings'];
Chris@76 670
Chris@76 671 $context['options'] = $context['theme_options'];
Chris@76 672 $context['theme_settings'] = $settings;
Chris@76 673
Chris@76 674 if (empty($_REQUEST['who']))
Chris@76 675 {
Chris@76 676 $request = $smcFunc['db_query']('', '
Chris@76 677 SELECT variable, value
Chris@76 678 FROM {db_prefix}themes
Chris@76 679 WHERE id_theme IN (1, {int:current_theme})
Chris@76 680 AND id_member = {int:guest_member}',
Chris@76 681 array(
Chris@76 682 'current_theme' => $_GET['th'],
Chris@76 683 'guest_member' => -1,
Chris@76 684 )
Chris@76 685 );
Chris@76 686 $context['theme_options'] = array();
Chris@76 687 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 688 $context['theme_options'][$row['variable']] = $row['value'];
Chris@76 689 $smcFunc['db_free_result']($request);
Chris@76 690
Chris@76 691 $context['theme_options_reset'] = false;
Chris@76 692 }
Chris@76 693 else
Chris@76 694 {
Chris@76 695 $context['theme_options'] = array();
Chris@76 696 $context['theme_options_reset'] = true;
Chris@76 697 }
Chris@76 698
Chris@76 699 foreach ($context['options'] as $i => $setting)
Chris@76 700 {
Chris@76 701 // Is this disabled?
Chris@76 702 if ($setting['id'] == 'calendar_start_day' && empty($modSettings['cal_enabled']))
Chris@76 703 {
Chris@76 704 unset($context['options'][$i]);
Chris@76 705 continue;
Chris@76 706 }
Chris@76 707 elseif (($setting['id'] == 'topics_per_page' || $setting['id'] == 'messages_per_page') && !empty($modSettings['disableCustomPerPage']))
Chris@76 708 {
Chris@76 709 unset($context['options'][$i]);
Chris@76 710 continue;
Chris@76 711 }
Chris@76 712
Chris@76 713 if (!isset($setting['type']) || $setting['type'] == 'bool')
Chris@76 714 $context['options'][$i]['type'] = 'checkbox';
Chris@76 715 elseif ($setting['type'] == 'int' || $setting['type'] == 'integer')
Chris@76 716 $context['options'][$i]['type'] = 'number';
Chris@76 717 elseif ($setting['type'] == 'string')
Chris@76 718 $context['options'][$i]['type'] = 'text';
Chris@76 719
Chris@76 720 if (isset($setting['options']))
Chris@76 721 $context['options'][$i]['type'] = 'list';
Chris@76 722
Chris@76 723 $context['options'][$i]['value'] = !isset($context['theme_options'][$setting['id']]) ? '' : $context['theme_options'][$setting['id']];
Chris@76 724 }
Chris@76 725
Chris@76 726 // Restore the existing theme.
Chris@76 727 loadTheme($old_id, false);
Chris@76 728 $settings = $old_settings;
Chris@76 729
Chris@76 730 loadTemplate('Themes');
Chris@76 731 }
Chris@76 732
Chris@76 733 // Administrative global settings.
Chris@76 734 function SetThemeSettings()
Chris@76 735 {
Chris@76 736 global $txt, $context, $settings, $modSettings, $sourcedir, $smcFunc;
Chris@76 737
Chris@76 738 if (empty($_GET['th']) && empty($_GET['id']))
Chris@76 739 return ThemeAdmin();
Chris@76 740 $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
Chris@76 741
Chris@76 742 // Select the best fitting tab.
Chris@76 743 $context[$context['admin_menu_name']]['current_subsection'] = 'list';
Chris@76 744
Chris@76 745 loadLanguage('Admin');
Chris@76 746 isAllowedTo('admin_forum');
Chris@76 747
Chris@76 748 // Validate inputs/user.
Chris@76 749 if (empty($_GET['th']))
Chris@76 750 fatal_lang_error('no_theme', false);
Chris@76 751
Chris@76 752 // Fetch the smiley sets...
Chris@76 753 $sets = explode(',', 'none,' . $modSettings['smiley_sets_known']);
Chris@76 754 $set_names = explode("\n", $txt['smileys_none'] . "\n" . $modSettings['smiley_sets_names']);
Chris@76 755 $context['smiley_sets'] = array(
Chris@76 756 '' => $txt['smileys_no_default']
Chris@76 757 );
Chris@76 758 foreach ($sets as $i => $set)
Chris@76 759 $context['smiley_sets'][$set] = htmlspecialchars($set_names[$i]);
Chris@76 760
Chris@76 761 $old_id = $settings['theme_id'];
Chris@76 762 $old_settings = $settings;
Chris@76 763
Chris@76 764 loadTheme($_GET['th'], false);
Chris@76 765
Chris@76 766 // Sadly we really do need to init the template.
Chris@76 767 loadSubTemplate('init', 'ignore');
Chris@76 768
Chris@76 769 // Also load the actual themes language file - in case of special settings.
Chris@76 770 loadLanguage('Settings', '', true, true);
Chris@76 771
Chris@76 772 // And the custom language strings...
Chris@76 773 loadLanguage('ThemeStrings', '', false, true);
Chris@76 774
Chris@76 775 // Let the theme take care of the settings.
Chris@76 776 loadTemplate('Settings');
Chris@76 777 loadSubTemplate('settings');
Chris@76 778
Chris@76 779 // Load the variants separately...
Chris@76 780 $settings['theme_variants'] = array();
Chris@76 781 if (file_exists($settings['theme_dir'] . '/index.template.php'))
Chris@76 782 {
Chris@76 783 $file_contents = implode('', file($settings['theme_dir'] . '/index.template.php'));
Chris@76 784 if (preg_match('~\$settings\[\'theme_variants\'\]\s*=(.+?);~', $file_contents, $matches))
Chris@76 785 eval('global $settings;' . $matches[0]);
Chris@76 786 }
Chris@76 787
Chris@76 788 // Submitting!
Chris@76 789 if (isset($_POST['submit']))
Chris@76 790 {
Chris@76 791 checkSession();
Chris@76 792
Chris@76 793 if (empty($_POST['options']))
Chris@76 794 $_POST['options'] = array();
Chris@76 795 if (empty($_POST['default_options']))
Chris@76 796 $_POST['default_options'] = array();
Chris@76 797
Chris@76 798 // Make sure items are cast correctly.
Chris@76 799 foreach ($context['theme_settings'] as $item)
Chris@76 800 {
Chris@76 801 // Disregard this item if this is just a separator.
Chris@76 802 if (!is_array($item))
Chris@76 803 continue;
Chris@76 804
Chris@76 805 foreach (array('options', 'default_options') as $option)
Chris@76 806 {
Chris@76 807 if (!isset($_POST[$option][$item['id']]))
Chris@76 808 continue;
Chris@76 809 // Checkbox.
Chris@76 810 elseif (empty($item['type']))
Chris@76 811 $_POST[$option][$item['id']] = $_POST[$option][$item['id']] ? 1 : 0;
Chris@76 812 // Number
Chris@76 813 elseif ($item['type'] == 'number')
Chris@76 814 $_POST[$option][$item['id']] = (int) $_POST[$option][$item['id']];
Chris@76 815 }
Chris@76 816 }
Chris@76 817
Chris@76 818 // Set up the sql query.
Chris@76 819 $inserts = array();
Chris@76 820 foreach ($_POST['options'] as $opt => $val)
Chris@76 821 $inserts[] = array(0, $_GET['th'], $opt, is_array($val) ? implode(',', $val) : $val);
Chris@76 822 foreach ($_POST['default_options'] as $opt => $val)
Chris@76 823 $inserts[] = array(0, 1, $opt, is_array($val) ? implode(',', $val) : $val);
Chris@76 824 // If we're actually inserting something..
Chris@76 825 if (!empty($inserts))
Chris@76 826 {
Chris@76 827 $smcFunc['db_insert']('replace',
Chris@76 828 '{db_prefix}themes',
Chris@76 829 array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
Chris@76 830 $inserts,
Chris@76 831 array('id_member', 'id_theme', 'variable')
Chris@76 832 );
Chris@76 833 }
Chris@76 834
Chris@76 835 cache_put_data('theme_settings-' . $_GET['th'], null, 90);
Chris@76 836 cache_put_data('theme_settings-1', null, 90);
Chris@76 837
Chris@76 838 // Invalidate the cache.
Chris@76 839 updateSettings(array('settings_updated' => time()));
Chris@76 840
Chris@76 841 redirectexit('action=admin;area=theme;sa=settings;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id']);
Chris@76 842 }
Chris@76 843
Chris@76 844 $context['sub_template'] = 'set_settings';
Chris@76 845 $context['page_title'] = $txt['theme_settings'];
Chris@76 846
Chris@76 847 foreach ($settings as $setting => $dummy)
Chris@76 848 {
Chris@76 849 if (!in_array($setting, array('theme_url', 'theme_dir', 'images_url', 'template_dirs')))
Chris@76 850 $settings[$setting] = htmlspecialchars__recursive($settings[$setting]);
Chris@76 851 }
Chris@76 852
Chris@76 853 $context['settings'] = $context['theme_settings'];
Chris@76 854 $context['theme_settings'] = $settings;
Chris@76 855
Chris@76 856 foreach ($context['settings'] as $i => $setting)
Chris@76 857 {
Chris@76 858 // Separators are dummies, so leave them alone.
Chris@76 859 if (!is_array($setting))
Chris@76 860 continue;
Chris@76 861
Chris@76 862 if (!isset($setting['type']) || $setting['type'] == 'bool')
Chris@76 863 $context['settings'][$i]['type'] = 'checkbox';
Chris@76 864 elseif ($setting['type'] == 'int' || $setting['type'] == 'integer')
Chris@76 865 $context['settings'][$i]['type'] = 'number';
Chris@76 866 elseif ($setting['type'] == 'string')
Chris@76 867 $context['settings'][$i]['type'] = 'text';
Chris@76 868
Chris@76 869 if (isset($setting['options']))
Chris@76 870 $context['settings'][$i]['type'] = 'list';
Chris@76 871
Chris@76 872 $context['settings'][$i]['value'] = !isset($settings[$setting['id']]) ? '' : $settings[$setting['id']];
Chris@76 873 }
Chris@76 874
Chris@76 875 // Do we support variants?
Chris@76 876 if (!empty($settings['theme_variants']))
Chris@76 877 {
Chris@76 878 $context['theme_variants'] = array();
Chris@76 879 foreach ($settings['theme_variants'] as $variant)
Chris@76 880 {
Chris@76 881 // Have any text, old chap?
Chris@76 882 $context['theme_variants'][$variant] = array(
Chris@76 883 'label' => isset($txt['variant_' . $variant]) ? $txt['variant_' . $variant] : $variant,
Chris@76 884 'thumbnail' => !file_exists($settings['theme_dir'] . '/images/thumbnail.gif') || file_exists($settings['theme_dir'] . '/images/thumbnail_' . $variant . '.gif') ? $settings['images_url'] . '/thumbnail_' . $variant . '.gif' : ($settings['images_url'] . '/thumbnail.gif'),
Chris@76 885 );
Chris@76 886 }
Chris@76 887 $context['default_variant'] = !empty($settings['default_variant']) && isset($context['theme_variants'][$settings['default_variant']]) ? $settings['default_variant'] : $settings['theme_variants'][0];
Chris@76 888 }
Chris@76 889
Chris@76 890 // Restore the current theme.
Chris@76 891 loadTheme($old_id, false);
Chris@76 892
Chris@76 893 // Reinit just incase.
Chris@76 894 loadSubTemplate('init', 'ignore');
Chris@76 895
Chris@76 896 $settings = $old_settings;
Chris@76 897
Chris@76 898 loadTemplate('Themes');
Chris@76 899 }
Chris@76 900
Chris@76 901 // Remove a theme from the database.
Chris@76 902 function RemoveTheme()
Chris@76 903 {
Chris@76 904 global $modSettings, $context, $smcFunc;
Chris@76 905
Chris@76 906 checkSession('get');
Chris@76 907
Chris@76 908 isAllowedTo('admin_forum');
Chris@76 909
Chris@76 910 // The theme's ID must be an integer.
Chris@76 911 $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
Chris@76 912
Chris@76 913 // You can't delete the default theme!
Chris@76 914 if ($_GET['th'] == 1)
Chris@76 915 fatal_lang_error('no_access', false);
Chris@76 916
Chris@76 917 $known = explode(',', $modSettings['knownThemes']);
Chris@76 918 for ($i = 0, $n = count($known); $i < $n; $i++)
Chris@76 919 {
Chris@76 920 if ($known[$i] == $_GET['th'])
Chris@76 921 unset($known[$i]);
Chris@76 922 }
Chris@76 923
Chris@76 924 $smcFunc['db_query']('', '
Chris@76 925 DELETE FROM {db_prefix}themes
Chris@76 926 WHERE id_theme = {int:current_theme}',
Chris@76 927 array(
Chris@76 928 'current_theme' => $_GET['th'],
Chris@76 929 )
Chris@76 930 );
Chris@76 931
Chris@76 932 $smcFunc['db_query']('', '
Chris@76 933 UPDATE {db_prefix}members
Chris@76 934 SET id_theme = {int:default_theme}
Chris@76 935 WHERE id_theme = {int:current_theme}',
Chris@76 936 array(
Chris@76 937 'default_theme' => 0,
Chris@76 938 'current_theme' => $_GET['th'],
Chris@76 939 )
Chris@76 940 );
Chris@76 941
Chris@76 942 $smcFunc['db_query']('', '
Chris@76 943 UPDATE {db_prefix}boards
Chris@76 944 SET id_theme = {int:default_theme}
Chris@76 945 WHERE id_theme = {int:current_theme}',
Chris@76 946 array(
Chris@76 947 'default_theme' => 0,
Chris@76 948 'current_theme' => $_GET['th'],
Chris@76 949 )
Chris@76 950 );
Chris@76 951
Chris@76 952 $known = strtr(implode(',', $known), array(',,' => ','));
Chris@76 953
Chris@76 954 // Fix it if the theme was the overall default theme.
Chris@76 955 if ($modSettings['theme_guests'] == $_GET['th'])
Chris@76 956 updateSettings(array('theme_guests' => '1', 'knownThemes' => $known));
Chris@76 957 else
Chris@76 958 updateSettings(array('knownThemes' => $known));
Chris@76 959
Chris@76 960 redirectexit('action=admin;area=theme;sa=list;' . $context['session_var'] . '=' . $context['session_id']);
Chris@76 961 }
Chris@76 962
Chris@76 963 // Choose a theme from a list.
Chris@76 964 function PickTheme()
Chris@76 965 {
Chris@76 966 global $txt, $context, $modSettings, $user_info, $language, $smcFunc, $settings, $scripturl;
Chris@76 967
Chris@76 968 loadLanguage('Profile');
Chris@76 969 loadTemplate('Themes');
Chris@76 970
Chris@76 971 // Build the link tree.
Chris@76 972 $context['linktree'][] = array(
Chris@76 973 'url' => $scripturl . '?action=theme;sa=pick;u=' . (!empty($_REQUEST['u']) ? (int) $_REQUEST['u'] : 0),
Chris@76 974 'name' => $txt['theme_pick'],
Chris@76 975 );
Chris@76 976
Chris@76 977 $_SESSION['id_theme'] = 0;
Chris@76 978
Chris@76 979 if (isset($_GET['id']))
Chris@76 980 $_GET['th'] = $_GET['id'];
Chris@76 981
Chris@76 982 // Saving a variant cause JS doesn't work - pretend it did ;)
Chris@76 983 if (isset($_POST['save']))
Chris@76 984 {
Chris@76 985 // Which theme?
Chris@76 986 foreach ($_POST['save'] as $k => $v)
Chris@76 987 $_GET['th'] = (int) $k;
Chris@76 988
Chris@76 989 if (isset($_POST['vrt'][$k]))
Chris@76 990 $_GET['vrt'] = $_POST['vrt'][$k];
Chris@76 991 }
Chris@76 992
Chris@76 993 // Have we made a desicion, or are we just browsing?
Chris@76 994 if (isset($_GET['th']))
Chris@76 995 {
Chris@76 996 checkSession('get');
Chris@76 997
Chris@76 998 $_GET['th'] = (int) $_GET['th'];
Chris@76 999
Chris@76 1000 // Save for this user.
Chris@76 1001 if (!isset($_REQUEST['u']) || !allowedTo('admin_forum'))
Chris@76 1002 {
Chris@76 1003 updateMemberData($user_info['id'], array('id_theme' => (int) $_GET['th']));
Chris@76 1004
Chris@76 1005 // A variants to save for the user?
Chris@76 1006 if (!empty($_GET['vrt']))
Chris@76 1007 {
Chris@76 1008 $smcFunc['db_insert']('replace',
Chris@76 1009 '{db_prefix}themes',
Chris@76 1010 array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
Chris@76 1011 array($_GET['th'], $user_info['id'], 'theme_variant', $_GET['vrt']),
Chris@76 1012 array('id_theme', 'id_member', 'variable')
Chris@76 1013 );
Chris@76 1014 cache_put_data('theme_settings-' . $_GET['th'] . ':' . $user_info['id'], null, 90);
Chris@76 1015
Chris@76 1016 $_SESSION['id_variant'] = 0;
Chris@76 1017 }
Chris@76 1018
Chris@76 1019 redirectexit('action=profile;area=theme');
Chris@76 1020 }
Chris@76 1021
Chris@76 1022 // If changing members or guests - and there's a variant - assume changing default variant.
Chris@76 1023 if (!empty($_GET['vrt']) && ($_REQUEST['u'] == '0' || $_REQUEST['u'] == '-1'))
Chris@76 1024 {
Chris@76 1025 $smcFunc['db_insert']('replace',
Chris@76 1026 '{db_prefix}themes',
Chris@76 1027 array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
Chris@76 1028 array($_GET['th'], 0, 'default_variant', $_GET['vrt']),
Chris@76 1029 array('id_theme', 'id_member', 'variable')
Chris@76 1030 );
Chris@76 1031
Chris@76 1032 // Make it obvious that it's changed
Chris@76 1033 cache_put_data('theme_settings-' . $_GET['th'], null, 90);
Chris@76 1034 }
Chris@76 1035
Chris@76 1036 // For everyone.
Chris@76 1037 if ($_REQUEST['u'] == '0')
Chris@76 1038 {
Chris@76 1039 updateMemberData(null, array('id_theme' => (int) $_GET['th']));
Chris@76 1040
Chris@76 1041 // Remove any custom variants.
Chris@76 1042 if (!empty($_GET['vrt']))
Chris@76 1043 {
Chris@76 1044 $smcFunc['db_query']('', '
Chris@76 1045 DELETE FROM {db_prefix}themes
Chris@76 1046 WHERE id_theme = {int:current_theme}
Chris@76 1047 AND variable = {string:theme_variant}',
Chris@76 1048 array(
Chris@76 1049 'current_theme' => (int) $_GET['th'],
Chris@76 1050 'theme_variant' => 'theme_variant',
Chris@76 1051 )
Chris@76 1052 );
Chris@76 1053 }
Chris@76 1054
Chris@76 1055 redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
Chris@76 1056 }
Chris@76 1057 // Change the default/guest theme.
Chris@76 1058 elseif ($_REQUEST['u'] == '-1')
Chris@76 1059 {
Chris@76 1060 updateSettings(array('theme_guests' => (int) $_GET['th']));
Chris@76 1061
Chris@76 1062 redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
Chris@76 1063 }
Chris@76 1064 // Change a specific member's theme.
Chris@76 1065 else
Chris@76 1066 {
Chris@76 1067 updateMemberData((int) $_REQUEST['u'], array('id_theme' => (int) $_GET['th']));
Chris@76 1068
Chris@76 1069 if (!empty($_GET['vrt']))
Chris@76 1070 {
Chris@76 1071 $smcFunc['db_insert']('replace',
Chris@76 1072 '{db_prefix}themes',
Chris@76 1073 array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
Chris@76 1074 array($_GET['th'], (int) $_REQUEST['u'], 'theme_variant', $_GET['vrt']),
Chris@76 1075 array('id_theme', 'id_member', 'variable')
Chris@76 1076 );
Chris@76 1077 cache_put_data('theme_settings-' . $_GET['th'] . ':' . (int) $_REQUEST['u'], null, 90);
Chris@76 1078
Chris@76 1079 if ($user_info['id'] == $_REQUEST['u'])
Chris@76 1080 $_SESSION['id_variant'] = 0;
Chris@76 1081 }
Chris@76 1082
Chris@76 1083 redirectexit('action=profile;u=' . (int) $_REQUEST['u'] . ';area=theme');
Chris@76 1084 }
Chris@76 1085 }
Chris@76 1086
Chris@76 1087 // Figure out who the member of the minute is, and what theme they've chosen.
Chris@76 1088 if (!isset($_REQUEST['u']) || !allowedTo('admin_forum'))
Chris@76 1089 {
Chris@76 1090 $context['current_member'] = $user_info['id'];
Chris@76 1091 $context['current_theme'] = $user_info['theme'];
Chris@76 1092 }
Chris@76 1093 // Everyone can't chose just one.
Chris@76 1094 elseif ($_REQUEST['u'] == '0')
Chris@76 1095 {
Chris@76 1096 $context['current_member'] = 0;
Chris@76 1097 $context['current_theme'] = 0;
Chris@76 1098 }
Chris@76 1099 // Guests and such...
Chris@76 1100 elseif ($_REQUEST['u'] == '-1')
Chris@76 1101 {
Chris@76 1102 $context['current_member'] = -1;
Chris@76 1103 $context['current_theme'] = $modSettings['theme_guests'];
Chris@76 1104 }
Chris@76 1105 // Someones else :P.
Chris@76 1106 else
Chris@76 1107 {
Chris@76 1108 $context['current_member'] = (int) $_REQUEST['u'];
Chris@76 1109
Chris@76 1110 $request = $smcFunc['db_query']('', '
Chris@76 1111 SELECT id_theme
Chris@76 1112 FROM {db_prefix}members
Chris@76 1113 WHERE id_member = {int:current_member}
Chris@76 1114 LIMIT 1',
Chris@76 1115 array(
Chris@76 1116 'current_member' => $context['current_member'],
Chris@76 1117 )
Chris@76 1118 );
Chris@76 1119 list ($context['current_theme']) = $smcFunc['db_fetch_row']($request);
Chris@76 1120 $smcFunc['db_free_result']($request);
Chris@76 1121 }
Chris@76 1122
Chris@76 1123 // Get the theme name and descriptions.
Chris@76 1124 $context['available_themes'] = array();
Chris@76 1125 if (!empty($modSettings['knownThemes']))
Chris@76 1126 {
Chris@76 1127 $request = $smcFunc['db_query']('', '
Chris@76 1128 SELECT id_theme, variable, value
Chris@76 1129 FROM {db_prefix}themes
Chris@76 1130 WHERE variable IN ({string:name}, {string:theme_url}, {string:theme_dir}, {string:images_url}, {string:disable_user_variant})' . (!allowedTo('admin_forum') ? '
Chris@76 1131 AND id_theme IN ({array_string:known_themes})' : '') . '
Chris@76 1132 AND id_theme != {int:default_theme}
Chris@76 1133 AND id_member = {int:no_member}',
Chris@76 1134 array(
Chris@76 1135 'default_theme' => 0,
Chris@76 1136 'name' => 'name',
Chris@76 1137 'no_member' => 0,
Chris@76 1138 'theme_url' => 'theme_url',
Chris@76 1139 'theme_dir' => 'theme_dir',
Chris@76 1140 'images_url' => 'images_url',
Chris@76 1141 'disable_user_variant' => 'disable_user_variant',
Chris@76 1142 'known_themes' => explode(',', $modSettings['knownThemes']),
Chris@76 1143 )
Chris@76 1144 );
Chris@76 1145 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 1146 {
Chris@76 1147 if (!isset($context['available_themes'][$row['id_theme']]))
Chris@76 1148 $context['available_themes'][$row['id_theme']] = array(
Chris@76 1149 'id' => $row['id_theme'],
Chris@76 1150 'selected' => $context['current_theme'] == $row['id_theme'],
Chris@76 1151 'num_users' => 0
Chris@76 1152 );
Chris@76 1153 $context['available_themes'][$row['id_theme']][$row['variable']] = $row['value'];
Chris@76 1154 }
Chris@76 1155 $smcFunc['db_free_result']($request);
Chris@76 1156 }
Chris@76 1157
Chris@76 1158 // Okay, this is a complicated problem: the default theme is 1, but they aren't allowed to access 1!
Chris@76 1159 if (!isset($context['available_themes'][$modSettings['theme_guests']]))
Chris@76 1160 {
Chris@76 1161 $context['available_themes'][0] = array(
Chris@76 1162 'num_users' => 0
Chris@76 1163 );
Chris@76 1164 $guest_theme = 0;
Chris@76 1165 }
Chris@76 1166 else
Chris@76 1167 $guest_theme = $modSettings['theme_guests'];
Chris@76 1168
Chris@76 1169 $request = $smcFunc['db_query']('', '
Chris@76 1170 SELECT id_theme, COUNT(*) AS the_count
Chris@76 1171 FROM {db_prefix}members
Chris@76 1172 GROUP BY id_theme
Chris@76 1173 ORDER BY id_theme DESC',
Chris@76 1174 array(
Chris@76 1175 )
Chris@76 1176 );
Chris@76 1177 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 1178 {
Chris@76 1179 // Figure out which theme it is they are REALLY using.
Chris@76 1180 if (!empty($modSettings['knownThemes']) && !in_array($row['id_theme'], explode(',',$modSettings['knownThemes'])))
Chris@76 1181 $row['id_theme'] = $guest_theme;
Chris@76 1182 elseif (empty($modSettings['theme_allow']))
Chris@76 1183 $row['id_theme'] = $guest_theme;
Chris@76 1184
Chris@76 1185 if (isset($context['available_themes'][$row['id_theme']]))
Chris@76 1186 $context['available_themes'][$row['id_theme']]['num_users'] += $row['the_count'];
Chris@76 1187 else
Chris@76 1188 $context['available_themes'][$guest_theme]['num_users'] += $row['the_count'];
Chris@76 1189 }
Chris@76 1190 $smcFunc['db_free_result']($request);
Chris@76 1191
Chris@76 1192 // Get any member variant preferences.
Chris@76 1193 $variant_preferences = array();
Chris@76 1194 if ($context['current_member'] > 0)
Chris@76 1195 {
Chris@76 1196 $request = $smcFunc['db_query']('', '
Chris@76 1197 SELECT id_theme, value
Chris@76 1198 FROM {db_prefix}themes
Chris@76 1199 WHERE variable = {string:theme_variant}',
Chris@76 1200 array(
Chris@76 1201 'theme_variant' => 'theme_variant',
Chris@76 1202 )
Chris@76 1203 );
Chris@76 1204 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 1205 $variant_preferences[$row['id_theme']] = $row['value'];
Chris@76 1206 $smcFunc['db_free_result']($request);
Chris@76 1207 }
Chris@76 1208
Chris@76 1209 // Save the setting first.
Chris@76 1210 $current_images_url = $settings['images_url'];
Chris@76 1211 $current_theme_variants = !empty($settings['theme_variants']) ? $settings['theme_variants'] : array();
Chris@76 1212
Chris@76 1213 foreach ($context['available_themes'] as $id_theme => $theme_data)
Chris@76 1214 {
Chris@76 1215 // Don't try to load the forum or board default theme's data... it doesn't have any!
Chris@76 1216 if ($id_theme == 0)
Chris@76 1217 continue;
Chris@76 1218
Chris@76 1219 // The thumbnail needs the correct path.
Chris@76 1220 $settings['images_url'] = &$theme_data['images_url'];
Chris@76 1221
Chris@76 1222 if (file_exists($theme_data['theme_dir'] . '/languages/Settings.' . $user_info['language'] . '.php'))
Chris@76 1223 include($theme_data['theme_dir'] . '/languages/Settings.' . $user_info['language'] . '.php');
Chris@76 1224 elseif (file_exists($theme_data['theme_dir'] . '/languages/Settings.' . $language . '.php'))
Chris@76 1225 include($theme_data['theme_dir'] . '/languages/Settings.' . $language . '.php');
Chris@76 1226 else
Chris@76 1227 {
Chris@76 1228 $txt['theme_thumbnail_href'] = $theme_data['images_url'] . '/thumbnail.gif';
Chris@76 1229 $txt['theme_description'] = '';
Chris@76 1230 }
Chris@76 1231
Chris@76 1232 $context['available_themes'][$id_theme]['thumbnail_href'] = $txt['theme_thumbnail_href'];
Chris@76 1233 $context['available_themes'][$id_theme]['description'] = $txt['theme_description'];
Chris@76 1234
Chris@76 1235 // Are there any variants?
Chris@76 1236 if (file_exists($theme_data['theme_dir'] . '/index.template.php') && empty($theme_data['disable_user_variant']))
Chris@76 1237 {
Chris@76 1238 $file_contents = implode('', file($theme_data['theme_dir'] . '/index.template.php'));
Chris@76 1239 if (preg_match('~\$settings\[\'theme_variants\'\]\s*=(.+?);~', $file_contents, $matches))
Chris@76 1240 {
Chris@76 1241 $settings['theme_variants'] = array();
Chris@76 1242
Chris@76 1243 // Fill settings up.
Chris@76 1244 eval('global $settings;' . $matches[0]);
Chris@76 1245
Chris@76 1246 if (!empty($settings['theme_variants']))
Chris@76 1247 {
Chris@76 1248 loadLanguage('Settings');
Chris@76 1249
Chris@76 1250 $context['available_themes'][$id_theme]['variants'] = array();
Chris@76 1251 foreach ($settings['theme_variants'] as $variant)
Chris@76 1252 $context['available_themes'][$id_theme]['variants'][$variant] = array(
Chris@76 1253 'label' => isset($txt['variant_' . $variant]) ? $txt['variant_' . $variant] : $variant,
Chris@76 1254 'thumbnail' => !file_exists($theme_data['theme_dir'] . '/images/thumbnail.gif') || file_exists($theme_data['theme_dir'] . '/images/thumbnail_' . $variant . '.gif') ? $theme_data['images_url'] . '/thumbnail_' . $variant . '.gif' : ($theme_data['images_url'] . '/thumbnail.gif'),
Chris@76 1255 );
Chris@76 1256
Chris@76 1257 $context['available_themes'][$id_theme]['selected_variant'] = isset($_GET['vrt']) ? $_GET['vrt'] : (!empty($variant_preferences[$id_theme]) ? $variant_preferences[$id_theme] : (!empty($settings['default_variant']) ? $settings['default_variant'] : $settings['theme_variants'][0]));
Chris@76 1258 if (!isset($context['available_themes'][$id_theme]['variants'][$context['available_themes'][$id_theme]['selected_variant']]['thumbnail']))
Chris@76 1259 $context['available_themes'][$id_theme]['selected_variant'] = $settings['theme_variants'][0];
Chris@76 1260
Chris@76 1261 $context['available_themes'][$id_theme]['thumbnail_href'] = $context['available_themes'][$id_theme]['variants'][$context['available_themes'][$id_theme]['selected_variant']]['thumbnail'];
Chris@76 1262 // Allow themes to override the text.
Chris@76 1263 $context['available_themes'][$id_theme]['pick_label'] = isset($txt['variant_pick']) ? $txt['variant_pick'] : $txt['theme_pick_variant'];
Chris@76 1264 }
Chris@76 1265 }
Chris@76 1266 }
Chris@76 1267 }
Chris@76 1268 // Then return it.
Chris@76 1269 $settings['images_url'] = $current_images_url;
Chris@76 1270 $settings['theme_variants'] = $current_theme_variants;
Chris@76 1271
Chris@76 1272 // As long as we're not doing the default theme...
Chris@76 1273 if (!isset($_REQUEST['u']) || $_REQUEST['u'] >= 0)
Chris@76 1274 {
Chris@76 1275 if ($guest_theme != 0)
Chris@76 1276 $context['available_themes'][0] = $context['available_themes'][$guest_theme];
Chris@76 1277
Chris@76 1278 $context['available_themes'][0]['id'] = 0;
Chris@76 1279 $context['available_themes'][0]['name'] = $txt['theme_forum_default'];
Chris@76 1280 $context['available_themes'][0]['selected'] = $context['current_theme'] == 0;
Chris@76 1281 $context['available_themes'][0]['description'] = $txt['theme_global_description'];
Chris@76 1282 }
Chris@76 1283
Chris@76 1284 ksort($context['available_themes']);
Chris@76 1285
Chris@76 1286 $context['page_title'] = $txt['theme_pick'];
Chris@76 1287 $context['sub_template'] = 'pick';
Chris@76 1288 }
Chris@76 1289
Chris@76 1290 function ThemeInstall()
Chris@76 1291 {
Chris@76 1292 global $sourcedir, $boarddir, $boardurl, $txt, $context, $settings, $modSettings, $smcFunc;
Chris@76 1293
Chris@76 1294 checkSession('request');
Chris@76 1295
Chris@76 1296 isAllowedTo('admin_forum');
Chris@76 1297 checkSession('request');
Chris@76 1298
Chris@76 1299 require_once($sourcedir . '/Subs-Package.php');
Chris@76 1300
Chris@76 1301 loadTemplate('Themes');
Chris@76 1302
Chris@76 1303 if (isset($_GET['theme_id']))
Chris@76 1304 {
Chris@76 1305 $result = $smcFunc['db_query']('', '
Chris@76 1306 SELECT value
Chris@76 1307 FROM {db_prefix}themes
Chris@76 1308 WHERE id_theme = {int:current_theme}
Chris@76 1309 AND id_member = {int:no_member}
Chris@76 1310 AND variable = {string:name}
Chris@76 1311 LIMIT 1',
Chris@76 1312 array(
Chris@76 1313 'current_theme' => (int) $_GET['theme_id'],
Chris@76 1314 'no_member' => 0,
Chris@76 1315 'name' => 'name',
Chris@76 1316 )
Chris@76 1317 );
Chris@76 1318 list ($theme_name) = $smcFunc['db_fetch_row']($result);
Chris@76 1319 $smcFunc['db_free_result']($result);
Chris@76 1320
Chris@76 1321 $context['sub_template'] = 'installed';
Chris@76 1322 $context['page_title'] = $txt['theme_installed'];
Chris@76 1323 $context['installed_theme'] = array(
Chris@76 1324 'id' => (int) $_GET['theme_id'],
Chris@76 1325 'name' => $theme_name,
Chris@76 1326 );
Chris@76 1327
Chris@76 1328 return;
Chris@76 1329 }
Chris@76 1330
Chris@76 1331 if ((!empty($_FILES['theme_gz']) && (!isset($_FILES['theme_gz']['error']) || $_FILES['theme_gz']['error'] != 4)) || !empty($_REQUEST['theme_gz']))
Chris@76 1332 $method = 'upload';
Chris@76 1333 elseif (isset($_REQUEST['theme_dir']) && rtrim(realpath($_REQUEST['theme_dir']), '/\\') != realpath($boarddir . '/Themes') && file_exists($_REQUEST['theme_dir']))
Chris@76 1334 $method = 'path';
Chris@76 1335 else
Chris@76 1336 $method = 'copy';
Chris@76 1337
Chris@76 1338 if (!empty($_REQUEST['copy']) && $method == 'copy')
Chris@76 1339 {
Chris@76 1340 // Hopefully the themes directory is writable, or we might have a problem.
Chris@76 1341 if (!is_writable($boarddir . '/Themes'))
Chris@76 1342 fatal_lang_error('theme_install_write_error', 'critical');
Chris@76 1343
Chris@76 1344 $theme_dir = $boarddir . '/Themes/' . preg_replace('~[^A-Za-z0-9_\- ]~', '', $_REQUEST['copy']);
Chris@76 1345
Chris@76 1346 umask(0);
Chris@76 1347 mkdir($theme_dir, 0777);
Chris@76 1348
Chris@76 1349 @set_time_limit(600);
Chris@76 1350 if (function_exists('apache_reset_timeout'))
Chris@76 1351 @apache_reset_timeout();
Chris@76 1352
Chris@76 1353 // Create subdirectories for css and javascript files.
Chris@76 1354 mkdir($theme_dir . '/css', 0777);
Chris@76 1355 mkdir($theme_dir . '/scripts', 0777);
Chris@76 1356
Chris@76 1357 // Copy over the default non-theme files.
Chris@76 1358 $to_copy = array('/index.php', '/index.template.php', '/css/index.css', '/css/rtl.css', '/scripts/theme.js');
Chris@76 1359 foreach ($to_copy as $file)
Chris@76 1360 {
Chris@76 1361 copy($settings['default_theme_dir'] . $file, $theme_dir . $file);
Chris@76 1362 @chmod($theme_dir . $file, 0777);
Chris@76 1363 }
Chris@76 1364
Chris@76 1365 // And now the entire images directory!
Chris@76 1366 copytree($settings['default_theme_dir'] . '/images', $theme_dir . '/images');
Chris@76 1367 package_flush_cache();
Chris@76 1368
Chris@76 1369 $theme_name = $_REQUEST['copy'];
Chris@76 1370 $images_url = $boardurl . '/Themes/' . basename($theme_dir) . '/images';
Chris@76 1371 $theme_dir = realpath($theme_dir);
Chris@76 1372
Chris@76 1373 // Lets get some data for the new theme.
Chris@76 1374 $request = $smcFunc['db_query']('', '
Chris@76 1375 SELECT variable, value
Chris@76 1376 FROM {db_prefix}themes
Chris@76 1377 WHERE variable IN ({string:theme_templates}, {string:theme_layers})
Chris@76 1378 AND id_member = {int:no_member}
Chris@76 1379 AND id_theme = {int:default_theme}',
Chris@76 1380 array(
Chris@76 1381 'no_member' => 0,
Chris@76 1382 'default_theme' => 1,
Chris@76 1383 'theme_templates' => 'theme_templates',
Chris@76 1384 'theme_layers' => 'theme_layers',
Chris@76 1385 )
Chris@76 1386 );
Chris@76 1387 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 1388 {
Chris@76 1389 if ($row['variable'] == 'theme_templates')
Chris@76 1390 $theme_templates = $row['value'];
Chris@76 1391 elseif ($row['variable'] == 'theme_layers')
Chris@76 1392 $theme_layers = $row['value'];
Chris@76 1393 else
Chris@76 1394 continue;
Chris@76 1395 }
Chris@76 1396 $smcFunc['db_free_result']($request);
Chris@76 1397
Chris@76 1398 // Lets add a theme_info.xml to this theme.
Chris@76 1399 $xml_info = '<' . '?xml version="1.0"?' . '>
Chris@76 1400 <theme-info xmlns="http://www.simplemachines.org/xml/theme-info" xmlns:smf="http://www.simplemachines.org/">
Chris@76 1401 <!-- For the id, always use something unique - put your name, a colon, and then the package name. -->
Chris@76 1402 <id>smf:' . $smcFunc['strtolower'](str_replace(array(' '), '_', $_REQUEST['copy'])) . '</id>
Chris@76 1403 <version>' . $modSettings['smfVersion'] . '</version>
Chris@76 1404 <!-- Theme name, used purely for aesthetics. -->
Chris@76 1405 <name>' . $_REQUEST['copy'] . '</name>
Chris@76 1406 <!-- Author: your email address or contact information. The name attribute is optional. -->
Chris@76 1407 <author name="Simple Machines">info@simplemachines.org</author>
Chris@76 1408 <!-- Website... where to get updates and more information. -->
Chris@76 1409 <website>http://www.simplemachines.org/</website>
Chris@76 1410 <!-- Template layers to use, defaults to "html,body". -->
Chris@76 1411 <layers>' . (empty($theme_layers) ? 'html,body' : $theme_layers) . '</layers>
Chris@76 1412 <!-- Templates to load on startup. Default is "index". -->
Chris@76 1413 <templates>' . (empty($theme_templates) ? 'index' : $theme_templates) . '</templates>
Chris@76 1414 <!-- Base this theme off another? Default is blank, or no. It could be "default". -->
Chris@76 1415 <based-on></based-on>
Chris@76 1416 </theme-info>';
Chris@76 1417
Chris@76 1418 // Now write it.
Chris@76 1419 $fp = @fopen($theme_dir . '/theme_info.xml', 'w+');
Chris@76 1420 if ($fp)
Chris@76 1421 {
Chris@76 1422 fwrite($fp, $xml_info);
Chris@76 1423 fclose($fp);
Chris@76 1424 }
Chris@76 1425 }
Chris@76 1426 elseif (isset($_REQUEST['theme_dir']) && $method == 'path')
Chris@76 1427 {
Chris@76 1428 if (!is_dir($_REQUEST['theme_dir']) || !file_exists($_REQUEST['theme_dir'] . '/theme_info.xml'))
Chris@76 1429 fatal_lang_error('theme_install_error', false);
Chris@76 1430
Chris@76 1431 $theme_name = basename($_REQUEST['theme_dir']);
Chris@76 1432 $theme_dir = $_REQUEST['theme_dir'];
Chris@76 1433 }
Chris@76 1434 elseif ($method = 'upload')
Chris@76 1435 {
Chris@76 1436 // Hopefully the themes directory is writable, or we might have a problem.
Chris@76 1437 if (!is_writable($boarddir . '/Themes'))
Chris@76 1438 fatal_lang_error('theme_install_write_error', 'critical');
Chris@76 1439
Chris@76 1440 require_once($sourcedir . '/Subs-Package.php');
Chris@76 1441
Chris@76 1442 // Set the default settings...
Chris@76 1443 $theme_name = strtok(basename(isset($_FILES['theme_gz']) ? $_FILES['theme_gz']['name'] : $_REQUEST['theme_gz']), '.');
Chris@76 1444 $theme_name = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $theme_name);
Chris@76 1445 $theme_dir = $boarddir . '/Themes/' . $theme_name;
Chris@76 1446
Chris@76 1447 if (isset($_FILES['theme_gz']) && is_uploaded_file($_FILES['theme_gz']['tmp_name']) && (@ini_get('open_basedir') != '' || file_exists($_FILES['theme_gz']['tmp_name'])))
Chris@76 1448 $extracted = read_tgz_file($_FILES['theme_gz']['tmp_name'], $boarddir . '/Themes/' . $theme_name, false, true);
Chris@76 1449 elseif (isset($_REQUEST['theme_gz']))
Chris@76 1450 {
Chris@76 1451 // Check that the theme is from simplemachines.org, for now... maybe add mirroring later.
Chris@76 1452 if (preg_match('~^http://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['theme_gz']) == 0 || strpos($_REQUEST['theme_gz'], 'dlattach') !== false)
Chris@76 1453 fatal_lang_error('not_on_simplemachines');
Chris@76 1454
Chris@76 1455 $extracted = read_tgz_file($_REQUEST['theme_gz'], $boarddir . '/Themes/' . $theme_name, false, true);
Chris@76 1456 }
Chris@76 1457 else
Chris@76 1458 redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
Chris@76 1459 }
Chris@76 1460
Chris@76 1461 // Something go wrong?
Chris@76 1462 if ($theme_dir != '' && basename($theme_dir) != 'Themes')
Chris@76 1463 {
Chris@76 1464 // Defaults.
Chris@76 1465 $install_info = array(
Chris@76 1466 'theme_url' => $boardurl . '/Themes/' . basename($theme_dir),
Chris@76 1467 'images_url' => isset($images_url) ? $images_url : $boardurl . '/Themes/' . basename($theme_dir) . '/images',
Chris@76 1468 'theme_dir' => $theme_dir,
Chris@76 1469 'name' => $theme_name
Chris@76 1470 );
Chris@76 1471
Chris@76 1472 if (file_exists($theme_dir . '/theme_info.xml'))
Chris@76 1473 {
Chris@76 1474 $theme_info = file_get_contents($theme_dir . '/theme_info.xml');
Chris@76 1475
Chris@76 1476 $xml_elements = array(
Chris@76 1477 'name' => 'name',
Chris@76 1478 'theme_layers' => 'layers',
Chris@76 1479 'theme_templates' => 'templates',
Chris@76 1480 'based_on' => 'based-on',
Chris@76 1481 );
Chris@76 1482 foreach ($xml_elements as $var => $name)
Chris@76 1483 {
Chris@76 1484 if (preg_match('~<' . $name . '>(?:<!\[CDATA\[)?(.+?)(?:\]\]>)?</' . $name . '>~', $theme_info, $match) == 1)
Chris@76 1485 $install_info[$var] = $match[1];
Chris@76 1486 }
Chris@76 1487
Chris@76 1488 if (preg_match('~<images>(?:<!\[CDATA\[)?(.+?)(?:\]\]>)?</images>~', $theme_info, $match) == 1)
Chris@76 1489 {
Chris@76 1490 $install_info['images_url'] = $install_info['theme_url'] . '/' . $match[1];
Chris@76 1491 $explicit_images = true;
Chris@76 1492 }
Chris@76 1493 if (preg_match('~<extra>(?:<!\[CDATA\[)?(.+?)(?:\]\]>)?</extra>~', $theme_info, $match) == 1)
Chris@76 1494 $install_info += unserialize($match[1]);
Chris@76 1495 }
Chris@76 1496
Chris@76 1497 if (isset($install_info['based_on']))
Chris@76 1498 {
Chris@76 1499 if ($install_info['based_on'] == 'default')
Chris@76 1500 {
Chris@76 1501 $install_info['theme_url'] = $settings['default_theme_url'];
Chris@76 1502 $install_info['images_url'] = $settings['default_images_url'];
Chris@76 1503 }
Chris@76 1504 elseif ($install_info['based_on'] != '')
Chris@76 1505 {
Chris@76 1506 $install_info['based_on'] = preg_replace('~[^A-Za-z0-9\-_ ]~', '', $install_info['based_on']);
Chris@76 1507
Chris@76 1508 $request = $smcFunc['db_query']('', '
Chris@76 1509 SELECT th.value AS base_theme_dir, th2.value AS base_theme_url' . (!empty($explicit_images) ? '' : ', th3.value AS images_url') . '
Chris@76 1510 FROM {db_prefix}themes AS th
Chris@76 1511 INNER JOIN {db_prefix}themes AS th2 ON (th2.id_theme = th.id_theme
Chris@76 1512 AND th2.id_member = {int:no_member}
Chris@76 1513 AND th2.variable = {string:theme_url})' . (!empty($explicit_images) ? '' : '
Chris@76 1514 INNER JOIN {db_prefix}themes AS th3 ON (th3.id_theme = th.id_theme
Chris@76 1515 AND th3.id_member = {int:no_member}
Chris@76 1516 AND th3.variable = {string:images_url})') . '
Chris@76 1517 WHERE th.id_member = {int:no_member}
Chris@76 1518 AND (th.value LIKE {string:based_on} OR th.value LIKE {string:based_on_path})
Chris@76 1519 AND th.variable = {string:theme_dir}
Chris@76 1520 LIMIT 1',
Chris@76 1521 array(
Chris@76 1522 'no_member' => 0,
Chris@76 1523 'theme_url' => 'theme_url',
Chris@76 1524 'images_url' => 'images_url',
Chris@76 1525 'theme_dir' => 'theme_dir',
Chris@76 1526 'based_on' => '%/' . $install_info['based_on'],
Chris@76 1527 'based_on_path' => '%' . "\\" . $install_info['based_on'],
Chris@76 1528 )
Chris@76 1529 );
Chris@76 1530 $temp = $smcFunc['db_fetch_assoc']($request);
Chris@76 1531 $smcFunc['db_free_result']($request);
Chris@76 1532
Chris@76 1533 // !!! An error otherwise?
Chris@76 1534 if (is_array($temp))
Chris@76 1535 {
Chris@76 1536 $install_info = $temp + $install_info;
Chris@76 1537
Chris@76 1538 if (empty($explicit_images) && !empty($install_info['base_theme_url']))
Chris@76 1539 $install_info['theme_url'] = $install_info['base_theme_url'];
Chris@76 1540 }
Chris@76 1541 }
Chris@76 1542
Chris@76 1543 unset($install_info['based_on']);
Chris@76 1544 }
Chris@76 1545
Chris@76 1546 // Find the newest id_theme.
Chris@76 1547 $result = $smcFunc['db_query']('', '
Chris@76 1548 SELECT MAX(id_theme)
Chris@76 1549 FROM {db_prefix}themes',
Chris@76 1550 array(
Chris@76 1551 )
Chris@76 1552 );
Chris@76 1553 list ($id_theme) = $smcFunc['db_fetch_row']($result);
Chris@76 1554 $smcFunc['db_free_result']($result);
Chris@76 1555
Chris@76 1556 // This will be theme number...
Chris@76 1557 $id_theme++;
Chris@76 1558
Chris@76 1559 $inserts = array();
Chris@76 1560 foreach ($install_info as $var => $val)
Chris@76 1561 $inserts[] = array($id_theme, $var, $val);
Chris@76 1562
Chris@76 1563 if (!empty($inserts))
Chris@76 1564 $smcFunc['db_insert']('insert',
Chris@76 1565 '{db_prefix}themes',
Chris@76 1566 array('id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
Chris@76 1567 $inserts,
Chris@76 1568 array('id_theme', 'variable')
Chris@76 1569 );
Chris@76 1570
Chris@76 1571 updateSettings(array('knownThemes' => strtr($modSettings['knownThemes'] . ',' . $id_theme, array(',,' => ','))));
Chris@76 1572 }
Chris@76 1573
Chris@76 1574 redirectexit('action=admin;area=theme;sa=install;theme_id=' . $id_theme . ';' . $context['session_var'] . '=' . $context['session_id']);
Chris@76 1575 }
Chris@76 1576
Chris@76 1577 // Possibly the simplest and best example of how to ues the template system.
Chris@76 1578 function WrapAction()
Chris@76 1579 {
Chris@76 1580 global $context, $settings, $sourcedir;
Chris@76 1581
Chris@76 1582 // Load any necessary template(s)?
Chris@76 1583 if (isset($settings['catch_action']['template']))
Chris@76 1584 {
Chris@76 1585 // Load both the template and language file. (but don't fret if the language file isn't there...)
Chris@76 1586 loadTemplate($settings['catch_action']['template']);
Chris@76 1587 loadLanguage($settings['catch_action']['template'], '', false);
Chris@76 1588 }
Chris@76 1589
Chris@76 1590 // Any special layers?
Chris@76 1591 if (isset($settings['catch_action']['layers']))
Chris@76 1592 $context['template_layers'] = $settings['catch_action']['layers'];
Chris@76 1593
Chris@76 1594 // Just call a function?
Chris@76 1595 if (isset($settings['catch_action']['function']))
Chris@76 1596 {
Chris@76 1597 if (isset($settings['catch_action']['filename']))
Chris@76 1598 template_include($sourcedir . '/' . $settings['catch_action']['filename'], true);
Chris@76 1599
Chris@76 1600 $settings['catch_action']['function']();
Chris@76 1601 }
Chris@76 1602 // And finally, the main sub template ;).
Chris@76 1603 elseif (isset($settings['catch_action']['sub_template']))
Chris@76 1604 $context['sub_template'] = $settings['catch_action']['sub_template'];
Chris@76 1605 }
Chris@76 1606
Chris@76 1607 // Set an option via javascript.
Chris@76 1608 function SetJavaScript()
Chris@76 1609 {
Chris@76 1610 global $settings, $user_info, $smcFunc, $options;
Chris@76 1611
Chris@76 1612 // Check the session id.
Chris@76 1613 checkSession('get');
Chris@76 1614
Chris@76 1615 // This good-for-nothing pixel is being used to keep the session alive.
Chris@76 1616 if (empty($_GET['var']) || !isset($_GET['val']))
Chris@76 1617 redirectexit($settings['images_url'] . '/blank.gif');
Chris@76 1618
Chris@76 1619 // Sorry, guests can't go any further than this..
Chris@76 1620 if ($user_info['is_guest'] || $user_info['id'] == 0)
Chris@76 1621 obExit(false);
Chris@76 1622
Chris@76 1623 $reservedVars = array(
Chris@76 1624 'actual_theme_url',
Chris@76 1625 'actual_images_url',
Chris@76 1626 'base_theme_dir',
Chris@76 1627 'base_theme_url',
Chris@76 1628 'default_images_url',
Chris@76 1629 'default_theme_dir',
Chris@76 1630 'default_theme_url',
Chris@76 1631 'default_template',
Chris@76 1632 'images_url',
Chris@76 1633 'number_recent_posts',
Chris@76 1634 'smiley_sets_default',
Chris@76 1635 'theme_dir',
Chris@76 1636 'theme_id',
Chris@76 1637 'theme_layers',
Chris@76 1638 'theme_templates',
Chris@76 1639 'theme_url',
Chris@76 1640 'name',
Chris@76 1641 );
Chris@76 1642
Chris@76 1643 // Can't change reserved vars.
Chris@76 1644 if (in_array(strtolower($_GET['var']), $reservedVars))
Chris@76 1645 redirectexit($settings['images_url'] . '/blank.gif');
Chris@76 1646
Chris@76 1647 // Use a specific theme?
Chris@76 1648 if (isset($_GET['th']) || isset($_GET['id']))
Chris@76 1649 {
Chris@76 1650 // Invalidate the current themes cache too.
Chris@76 1651 cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 60);
Chris@76 1652
Chris@76 1653 $settings['theme_id'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
Chris@76 1654 }
Chris@76 1655
Chris@76 1656 // If this is the admin preferences the passed value will just be an element of it.
Chris@76 1657 if ($_GET['var'] == 'admin_preferences')
Chris@76 1658 {
Chris@76 1659 $options['admin_preferences'] = !empty($options['admin_preferences']) ? unserialize($options['admin_preferences']) : array();
Chris@76 1660 // New thingy...
Chris@76 1661 if (isset($_GET['admin_key']) && strlen($_GET['admin_key']) < 5)
Chris@76 1662 $options['admin_preferences'][$_GET['admin_key']] = $_GET['val'];
Chris@76 1663
Chris@76 1664 // Change the value to be something nice,
Chris@76 1665 $_GET['val'] = serialize($options['admin_preferences']);
Chris@76 1666 }
Chris@76 1667
Chris@76 1668 // Update the option.
Chris@76 1669 $smcFunc['db_insert']('replace',
Chris@76 1670 '{db_prefix}themes',
Chris@76 1671 array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
Chris@76 1672 array($settings['theme_id'], $user_info['id'], $_GET['var'], is_array($_GET['val']) ? implode(',', $_GET['val']) : $_GET['val']),
Chris@76 1673 array('id_theme', 'id_member', 'variable')
Chris@76 1674 );
Chris@76 1675
Chris@76 1676 cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 60);
Chris@76 1677
Chris@76 1678 // Don't output anything...
Chris@76 1679 redirectexit($settings['images_url'] . '/blank.gif');
Chris@76 1680 }
Chris@76 1681
Chris@76 1682 function EditTheme()
Chris@76 1683 {
Chris@76 1684 global $context, $settings, $scripturl, $boarddir, $smcFunc;
Chris@76 1685
Chris@76 1686 if (isset($_REQUEST['preview']))
Chris@76 1687 {
Chris@76 1688 // !!! Should this be removed?
Chris@76 1689 die;
Chris@76 1690 }
Chris@76 1691
Chris@76 1692 isAllowedTo('admin_forum');
Chris@76 1693 loadTemplate('Themes');
Chris@76 1694
Chris@76 1695 $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) @$_GET['id'];
Chris@76 1696
Chris@76 1697 if (empty($_GET['th']))
Chris@76 1698 {
Chris@76 1699 $request = $smcFunc['db_query']('', '
Chris@76 1700 SELECT id_theme, variable, value
Chris@76 1701 FROM {db_prefix}themes
Chris@76 1702 WHERE variable IN ({string:name}, {string:theme_dir}, {string:theme_templates}, {string:theme_layers})
Chris@76 1703 AND id_member = {int:no_member}',
Chris@76 1704 array(
Chris@76 1705 'name' => 'name',
Chris@76 1706 'theme_dir' => 'theme_dir',
Chris@76 1707 'theme_templates' => 'theme_templates',
Chris@76 1708 'theme_layers' => 'theme_layers',
Chris@76 1709 'no_member' => 0,
Chris@76 1710 )
Chris@76 1711 );
Chris@76 1712 $context['themes'] = array();
Chris@76 1713 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 1714 {
Chris@76 1715 if (!isset($context['themes'][$row['id_theme']]))
Chris@76 1716 $context['themes'][$row['id_theme']] = array(
Chris@76 1717 'id' => $row['id_theme'],
Chris@76 1718 'num_default_options' => 0,
Chris@76 1719 'num_members' => 0,
Chris@76 1720 );
Chris@76 1721 $context['themes'][$row['id_theme']][$row['variable']] = $row['value'];
Chris@76 1722 }
Chris@76 1723 $smcFunc['db_free_result']($request);
Chris@76 1724
Chris@76 1725 foreach ($context['themes'] as $key => $theme)
Chris@76 1726 {
Chris@76 1727 // There has to be a Settings template!
Chris@76 1728 if (!file_exists($theme['theme_dir'] . '/index.template.php') && !file_exists($theme['theme_dir'] . '/css/index.css'))
Chris@76 1729 unset($context['themes'][$key]);
Chris@76 1730 else
Chris@76 1731 {
Chris@76 1732 if (!isset($theme['theme_templates']))
Chris@76 1733 $templates = array('index');
Chris@76 1734 else
Chris@76 1735 $templates = explode(',', $theme['theme_templates']);
Chris@76 1736
Chris@76 1737 foreach ($templates as $template)
Chris@76 1738 if (file_exists($theme['theme_dir'] . '/' . $template . '.template.php'))
Chris@76 1739 {
Chris@76 1740 // Fetch the header... a good 256 bytes should be more than enough.
Chris@76 1741 $fp = fopen($theme['theme_dir'] . '/' . $template . '.template.php', 'rb');
Chris@76 1742 $header = fread($fp, 256);
Chris@76 1743 fclose($fp);
Chris@76 1744
Chris@76 1745 // Can we find a version comment, at all?
Chris@76 1746 if (preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $header, $match) == 1)
Chris@76 1747 {
Chris@76 1748 $ver = $match[1];
Chris@76 1749 if (!isset($context['themes'][$key]['version']) || $context['themes'][$key]['version'] > $ver)
Chris@76 1750 $context['themes'][$key]['version'] = $ver;
Chris@76 1751 }
Chris@76 1752 }
Chris@76 1753
Chris@76 1754 $context['themes'][$key]['can_edit_style'] = file_exists($theme['theme_dir'] . '/css/index.css');
Chris@76 1755 }
Chris@76 1756 }
Chris@76 1757
Chris@76 1758 $context['sub_template'] = 'edit_list';
Chris@76 1759
Chris@76 1760 return 'no_themes';
Chris@76 1761 }
Chris@76 1762
Chris@76 1763 $context['session_error'] = false;
Chris@76 1764
Chris@76 1765 // Get the directory of the theme we are editing.
Chris@76 1766 $request = $smcFunc['db_query']('', '
Chris@76 1767 SELECT value, id_theme
Chris@76 1768 FROM {db_prefix}themes
Chris@76 1769 WHERE variable = {string:theme_dir}
Chris@76 1770 AND id_theme = {int:current_theme}
Chris@76 1771 LIMIT 1',
Chris@76 1772 array(
Chris@76 1773 'current_theme' => $_GET['th'],
Chris@76 1774 'theme_dir' => 'theme_dir',
Chris@76 1775 )
Chris@76 1776 );
Chris@76 1777 list ($theme_dir, $context['theme_id']) = $smcFunc['db_fetch_row']($request);
Chris@76 1778 $smcFunc['db_free_result']($request);
Chris@76 1779
Chris@76 1780 if (!file_exists($theme_dir . '/index.template.php') && !file_exists($theme_dir . '/css/index.css'))
Chris@76 1781 fatal_lang_error('theme_edit_missing', false);
Chris@76 1782
Chris@76 1783 if (!isset($_REQUEST['filename']))
Chris@76 1784 {
Chris@76 1785 if (isset($_GET['directory']))
Chris@76 1786 {
Chris@76 1787 if (substr($_GET['directory'], 0, 1) == '.')
Chris@76 1788 $_GET['directory'] = '';
Chris@76 1789 else
Chris@76 1790 {
Chris@76 1791 $_GET['directory'] = preg_replace(array('~^[\./\\:\0\n\r]+~', '~[\\\\]~', '~/[\./]+~'), array('', '/', '/'), $_GET['directory']);
Chris@76 1792
Chris@76 1793 $temp = realpath($theme_dir . '/' . $_GET['directory']);
Chris@76 1794 if (empty($temp) || substr($temp, 0, strlen(realpath($theme_dir))) != realpath($theme_dir))
Chris@76 1795 $_GET['directory'] = '';
Chris@76 1796 }
Chris@76 1797 }
Chris@76 1798
Chris@76 1799 if (isset($_GET['directory']) && $_GET['directory'] != '')
Chris@76 1800 {
Chris@76 1801 $context['theme_files'] = get_file_listing($theme_dir . '/' . $_GET['directory'], $_GET['directory'] . '/');
Chris@76 1802
Chris@76 1803 $temp = dirname($_GET['directory']);
Chris@76 1804 array_unshift($context['theme_files'], array(
Chris@76 1805 'filename' => $temp == '.' || $temp == '' ? '/ (..)' : $temp . ' (..)',
Chris@76 1806 'is_writable' => is_writable($theme_dir . '/' . $temp),
Chris@76 1807 'is_directory' => true,
Chris@76 1808 'is_template' => false,
Chris@76 1809 'is_image' => false,
Chris@76 1810 'is_editable' => false,
Chris@76 1811 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . $temp,
Chris@76 1812 'size' => '',
Chris@76 1813 ));
Chris@76 1814 }
Chris@76 1815 else
Chris@76 1816 $context['theme_files'] = get_file_listing($theme_dir, '');
Chris@76 1817
Chris@76 1818 $context['sub_template'] = 'edit_browse';
Chris@76 1819
Chris@76 1820 return;
Chris@76 1821 }
Chris@76 1822 else
Chris@76 1823 {
Chris@76 1824 if (substr($_REQUEST['filename'], 0, 1) == '.')
Chris@76 1825 $_REQUEST['filename'] = '';
Chris@76 1826 else
Chris@76 1827 {
Chris@76 1828 $_REQUEST['filename'] = preg_replace(array('~^[\./\\:\0\n\r]+~', '~[\\\\]~', '~/[\./]+~'), array('', '/', '/'), $_REQUEST['filename']);
Chris@76 1829
Chris@76 1830 $temp = realpath($theme_dir . '/' . $_REQUEST['filename']);
Chris@76 1831 if (empty($temp) || substr($temp, 0, strlen(realpath($theme_dir))) != realpath($theme_dir))
Chris@76 1832 $_REQUEST['filename'] = '';
Chris@76 1833 }
Chris@76 1834
Chris@76 1835 if (empty($_REQUEST['filename']))
Chris@76 1836 fatal_lang_error('theme_edit_missing', false);
Chris@76 1837 }
Chris@76 1838
Chris@76 1839 if (isset($_POST['submit']))
Chris@76 1840 {
Chris@76 1841 if (checkSession('post', '', false) == '')
Chris@76 1842 {
Chris@76 1843 if (is_array($_POST['entire_file']))
Chris@76 1844 $_POST['entire_file'] = implode("\n", $_POST['entire_file']);
Chris@76 1845 $_POST['entire_file'] = rtrim(strtr($_POST['entire_file'], array("\r" => '', ' ' => "\t")));
Chris@76 1846
Chris@76 1847 // Check for a parse error!
Chris@76 1848 if (substr($_REQUEST['filename'], -13) == '.template.php' && is_writable($theme_dir) && @ini_get('display_errors'))
Chris@76 1849 {
Chris@76 1850 $request = $smcFunc['db_query']('', '
Chris@76 1851 SELECT value
Chris@76 1852 FROM {db_prefix}themes
Chris@76 1853 WHERE variable = {string:theme_url}
Chris@76 1854 AND id_theme = {int:current_theme}
Chris@76 1855 LIMIT 1',
Chris@76 1856 array(
Chris@76 1857 'current_theme' => $_GET['th'],
Chris@76 1858 'theme_url' => 'theme_url',
Chris@76 1859 )
Chris@76 1860 );
Chris@76 1861 list ($theme_url) = $smcFunc['db_fetch_row']($request);
Chris@76 1862 $smcFunc['db_free_result']($request);
Chris@76 1863
Chris@76 1864 $fp = fopen($theme_dir . '/tmp_' . session_id() . '.php', 'w');
Chris@76 1865 fwrite($fp, $_POST['entire_file']);
Chris@76 1866 fclose($fp);
Chris@76 1867
Chris@76 1868 // !!! Use fetch_web_data()?
Chris@76 1869 $error = @file_get_contents($theme_url . '/tmp_' . session_id() . '.php');
Chris@76 1870 if (preg_match('~ <b>(\d+)</b><br( /)?' . '>$~i', $error) != 0)
Chris@76 1871 $error_file = $theme_dir . '/tmp_' . session_id() . '.php';
Chris@76 1872 else
Chris@76 1873 unlink($theme_dir . '/tmp_' . session_id() . '.php');
Chris@76 1874 }
Chris@76 1875
Chris@76 1876 if (!isset($error_file))
Chris@76 1877 {
Chris@76 1878 $fp = fopen($theme_dir . '/' . $_REQUEST['filename'], 'w');
Chris@76 1879 fwrite($fp, $_POST['entire_file']);
Chris@76 1880 fclose($fp);
Chris@76 1881
Chris@76 1882 redirectexit('action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . dirname($_REQUEST['filename']));
Chris@76 1883 }
Chris@76 1884 }
Chris@76 1885 // Session timed out.
Chris@76 1886 else
Chris@76 1887 {
Chris@76 1888 loadLanguage('Errors');
Chris@76 1889
Chris@76 1890 $context['session_error'] = true;
Chris@76 1891 $context['sub_template'] = 'edit_file';
Chris@76 1892
Chris@76 1893 // Recycle the submitted data.
Chris@76 1894 $context['entire_file'] = htmlspecialchars($_POST['entire_file']);
Chris@76 1895
Chris@76 1896 // You were able to submit it, so it's reasonable to assume you are allowed to save.
Chris@76 1897 $context['allow_save'] = true;
Chris@76 1898
Chris@76 1899 return;
Chris@76 1900 }
Chris@76 1901 }
Chris@76 1902
Chris@76 1903 $context['allow_save'] = is_writable($theme_dir . '/' . $_REQUEST['filename']);
Chris@76 1904 $context['allow_save_filename'] = strtr($theme_dir . '/' . $_REQUEST['filename'], array($boarddir => '...'));
Chris@76 1905 $context['edit_filename'] = htmlspecialchars($_REQUEST['filename']);
Chris@76 1906
Chris@76 1907 if (substr($_REQUEST['filename'], -4) == '.css')
Chris@76 1908 {
Chris@76 1909 $context['sub_template'] = 'edit_style';
Chris@76 1910
Chris@76 1911 $context['entire_file'] = htmlspecialchars(strtr(file_get_contents($theme_dir . '/' . $_REQUEST['filename']), array("\t" => ' ')));
Chris@76 1912 }
Chris@76 1913 elseif (substr($_REQUEST['filename'], -13) == '.template.php')
Chris@76 1914 {
Chris@76 1915 $context['sub_template'] = 'edit_template';
Chris@76 1916
Chris@76 1917 if (!isset($error_file))
Chris@76 1918 $file_data = file($theme_dir . '/' . $_REQUEST['filename']);
Chris@76 1919 else
Chris@76 1920 {
Chris@76 1921 if (preg_match('~(<b>.+?</b>:.+?<b>).+?(</b>.+?<b>\d+</b>)<br( /)?' . '>$~i', $error, $match) != 0)
Chris@76 1922 $context['parse_error'] = $match[1] . $_REQUEST['filename'] . $match[2];
Chris@76 1923 $file_data = file($error_file);
Chris@76 1924 unlink($error_file);
Chris@76 1925 }
Chris@76 1926
Chris@76 1927 $j = 0;
Chris@76 1928 $context['file_parts'] = array(array('lines' => 0, 'line' => 1, 'data' => ''));
Chris@76 1929 for ($i = 0, $n = count($file_data); $i < $n; $i++)
Chris@76 1930 {
Chris@76 1931 if (isset($file_data[$i + 1]) && substr($file_data[$i + 1], 0, 9) == 'function ')
Chris@76 1932 {
Chris@76 1933 // Try to format the functions a little nicer...
Chris@76 1934 $context['file_parts'][$j]['data'] = trim($context['file_parts'][$j]['data']) . "\n";
Chris@76 1935
Chris@76 1936 if (empty($context['file_parts'][$j]['lines']))
Chris@76 1937 unset($context['file_parts'][$j]);
Chris@76 1938 $context['file_parts'][++$j] = array('lines' => 0, 'line' => $i + 1, 'data' => '');
Chris@76 1939 }
Chris@76 1940
Chris@76 1941 $context['file_parts'][$j]['lines']++;
Chris@76 1942 $context['file_parts'][$j]['data'] .= htmlspecialchars(strtr($file_data[$i], array("\t" => ' ')));
Chris@76 1943 }
Chris@76 1944
Chris@76 1945 $context['entire_file'] = htmlspecialchars(strtr(implode('', $file_data), array("\t" => ' ')));
Chris@76 1946 }
Chris@76 1947 else
Chris@76 1948 {
Chris@76 1949 $context['sub_template'] = 'edit_file';
Chris@76 1950
Chris@76 1951 $context['entire_file'] = htmlspecialchars(strtr(file_get_contents($theme_dir . '/' . $_REQUEST['filename']), array("\t" => ' ')));
Chris@76 1952 }
Chris@76 1953 }
Chris@76 1954
Chris@76 1955 function get_file_listing($path, $relative)
Chris@76 1956 {
Chris@76 1957 global $scripturl, $txt, $context;
Chris@76 1958
Chris@76 1959 // Is it even a directory?
Chris@76 1960 if (!is_dir($path))
Chris@76 1961 fatal_lang_error('error_invalid_dir', 'critical');
Chris@76 1962
Chris@76 1963 $dir = dir($path);
Chris@76 1964 $entries = array();
Chris@76 1965 while ($entry = $dir->read())
Chris@76 1966 $entries[] = $entry;
Chris@76 1967 $dir->close();
Chris@76 1968
Chris@76 1969 natcasesort($entries);
Chris@76 1970
Chris@76 1971 $listing1 = array();
Chris@76 1972 $listing2 = array();
Chris@76 1973
Chris@76 1974 foreach ($entries as $entry)
Chris@76 1975 {
Chris@76 1976 // Skip all dot files, including .htaccess.
Chris@76 1977 if (substr($entry, 0, 1) == '.' || $entry == 'CVS')
Chris@76 1978 continue;
Chris@76 1979
Chris@76 1980 if (is_dir($path . '/' . $entry))
Chris@76 1981 $listing1[] = array(
Chris@76 1982 'filename' => $entry,
Chris@76 1983 'is_writable' => is_writable($path . '/' . $entry),
Chris@76 1984 'is_directory' => true,
Chris@76 1985 'is_template' => false,
Chris@76 1986 'is_image' => false,
Chris@76 1987 'is_editable' => false,
Chris@76 1988 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . $relative . $entry,
Chris@76 1989 'size' => '',
Chris@76 1990 );
Chris@76 1991 else
Chris@76 1992 {
Chris@76 1993 $size = filesize($path . '/' . $entry);
Chris@76 1994 if ($size > 2048 || $size == 1024)
Chris@76 1995 $size = comma_format($size / 1024) . ' ' . $txt['themeadmin_edit_kilobytes'];
Chris@76 1996 else
Chris@76 1997 $size = comma_format($size) . ' ' . $txt['themeadmin_edit_bytes'];
Chris@76 1998
Chris@76 1999 $listing2[] = array(
Chris@76 2000 'filename' => $entry,
Chris@76 2001 'is_writable' => is_writable($path . '/' . $entry),
Chris@76 2002 'is_directory' => false,
Chris@76 2003 'is_template' => preg_match('~\.template\.php$~', $entry) != 0,
Chris@76 2004 'is_image' => preg_match('~\.(jpg|jpeg|gif|bmp|png)$~', $entry) != 0,
Chris@76 2005 'is_editable' => is_writable($path . '/' . $entry) && preg_match('~\.(php|pl|css|js|vbs|xml|xslt|txt|xsl|html|htm|shtm|shtml|asp|aspx|cgi|py)$~', $entry) != 0,
Chris@76 2006 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;filename=' . $relative . $entry,
Chris@76 2007 'size' => $size,
Chris@76 2008 'last_modified' => timeformat(filemtime($path . '/' . $entry)),
Chris@76 2009 );
Chris@76 2010 }
Chris@76 2011 }
Chris@76 2012
Chris@76 2013 return array_merge($listing1, $listing2);
Chris@76 2014 }
Chris@76 2015
Chris@76 2016 function CopyTemplate()
Chris@76 2017 {
Chris@76 2018 global $context, $settings, $smcFunc;
Chris@76 2019
Chris@76 2020 isAllowedTo('admin_forum');
Chris@76 2021 loadTemplate('Themes');
Chris@76 2022
Chris@76 2023 $context[$context['admin_menu_name']]['current_subsection'] = 'edit';
Chris@76 2024
Chris@76 2025 $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
Chris@76 2026
Chris@76 2027 $request = $smcFunc['db_query']('', '
Chris@76 2028 SELECT th1.value, th1.id_theme, th2.value
Chris@76 2029 FROM {db_prefix}themes AS th1
Chris@76 2030 LEFT JOIN {db_prefix}themes AS th2 ON (th2.variable = {string:base_theme_dir} AND th2.id_theme = {int:current_theme})
Chris@76 2031 WHERE th1.variable = {string:theme_dir}
Chris@76 2032 AND th1.id_theme = {int:current_theme}
Chris@76 2033 LIMIT 1',
Chris@76 2034 array(
Chris@76 2035 'current_theme' => $_GET['th'],
Chris@76 2036 'base_theme_dir' => 'base_theme_dir',
Chris@76 2037 'theme_dir' => 'theme_dir',
Chris@76 2038 )
Chris@76 2039 );
Chris@76 2040 list ($theme_dir, $context['theme_id'], $base_theme_dir) = $smcFunc['db_fetch_row']($request);
Chris@76 2041 $smcFunc['db_free_result']($request);
Chris@76 2042
Chris@76 2043 if (isset($_REQUEST['template']) && preg_match('~[\./\\\\:\0]~', $_REQUEST['template']) == 0)
Chris@76 2044 {
Chris@76 2045 if (!empty($base_theme_dir) && file_exists($base_theme_dir . '/' . $_REQUEST['template'] . '.template.php'))
Chris@76 2046 $filename = $base_theme_dir . '/' . $_REQUEST['template'] . '.template.php';
Chris@76 2047 elseif (file_exists($settings['default_theme_dir'] . '/' . $_REQUEST['template'] . '.template.php'))
Chris@76 2048 $filename = $settings['default_theme_dir'] . '/' . $_REQUEST['template'] . '.template.php';
Chris@76 2049 else
Chris@76 2050 fatal_lang_error('no_access', false);
Chris@76 2051
Chris@76 2052 $fp = fopen($theme_dir . '/' . $_REQUEST['template'] . '.template.php', 'w');
Chris@76 2053 fwrite($fp, file_get_contents($filename));
Chris@76 2054 fclose($fp);
Chris@76 2055
Chris@76 2056 redirectexit('action=admin;area=theme;th=' . $context['theme_id'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=copy');
Chris@76 2057 }
Chris@76 2058 elseif (isset($_REQUEST['lang_file']) && preg_match('~^[^\./\\\\:\0]\.[^\./\\\\:\0]$~', $_REQUEST['lang_file']) != 0)
Chris@76 2059 {
Chris@76 2060 if (!empty($base_theme_dir) && file_exists($base_theme_dir . '/languages/' . $_REQUEST['lang_file'] . '.php'))
Chris@76 2061 $filename = $base_theme_dir . '/languages/' . $_REQUEST['template'] . '.php';
Chris@76 2062 elseif (file_exists($settings['default_theme_dir'] . '/languages/' . $_REQUEST['template'] . '.php'))
Chris@76 2063 $filename = $settings['default_theme_dir'] . '/languages/' . $_REQUEST['template'] . '.php';
Chris@76 2064 else
Chris@76 2065 fatal_lang_error('no_access', false);
Chris@76 2066
Chris@76 2067 $fp = fopen($theme_dir . '/languages/' . $_REQUEST['lang_file'] . '.php', 'w');
Chris@76 2068 fwrite($fp, file_get_contents($filename));
Chris@76 2069 fclose($fp);
Chris@76 2070
Chris@76 2071 redirectexit('action=admin;area=theme;th=' . $context['theme_id'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=copy');
Chris@76 2072 }
Chris@76 2073
Chris@76 2074 $templates = array();
Chris@76 2075 $lang_files = array();
Chris@76 2076
Chris@76 2077 $dir = dir($settings['default_theme_dir']);
Chris@76 2078 while ($entry = $dir->read())
Chris@76 2079 {
Chris@76 2080 if (substr($entry, -13) == '.template.php')
Chris@76 2081 $templates[] = substr($entry, 0, -13);
Chris@76 2082 }
Chris@76 2083 $dir->close();
Chris@76 2084
Chris@76 2085 $dir = dir($settings['default_theme_dir'] . '/languages');
Chris@76 2086 while ($entry = $dir->read())
Chris@76 2087 {
Chris@76 2088 if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches))
Chris@76 2089 $lang_files[] = $matches[1];
Chris@76 2090 }
Chris@76 2091 $dir->close();
Chris@76 2092
Chris@76 2093 if (!empty($base_theme_dir))
Chris@76 2094 {
Chris@76 2095 $dir = dir($base_theme_dir);
Chris@76 2096 while ($entry = $dir->read())
Chris@76 2097 {
Chris@76 2098 if (substr($entry, -13) == '.template.php' && !in_array(substr($entry, 0, -13), $templates))
Chris@76 2099 $templates[] = substr($entry, 0, -13);
Chris@76 2100 }
Chris@76 2101 $dir->close();
Chris@76 2102
Chris@76 2103 if (file_exists($base_theme_dir . '/languages'))
Chris@76 2104 {
Chris@76 2105 $dir = dir($base_theme_dir . '/languages');
Chris@76 2106 while ($entry = $dir->read())
Chris@76 2107 {
Chris@76 2108 if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches) && !in_array($matches[1], $lang_files))
Chris@76 2109 $lang_files[] = $matches[1];
Chris@76 2110 }
Chris@76 2111 $dir->close();
Chris@76 2112 }
Chris@76 2113 }
Chris@76 2114
Chris@76 2115 natcasesort($templates);
Chris@76 2116 natcasesort($lang_files);
Chris@76 2117
Chris@76 2118 $context['available_templates'] = array();
Chris@76 2119 foreach ($templates as $template)
Chris@76 2120 $context['available_templates'][$template] = array(
Chris@76 2121 'filename' => $template . '.template.php',
Chris@76 2122 'value' => $template,
Chris@76 2123 'already_exists' => false,
Chris@76 2124 'can_copy' => is_writable($theme_dir),
Chris@76 2125 );
Chris@76 2126 $context['available_language_files'] = array();
Chris@76 2127 foreach ($lang_files as $file)
Chris@76 2128 $context['available_language_files'][$file] = array(
Chris@76 2129 'filename' => $file . '.php',
Chris@76 2130 'value' => $file,
Chris@76 2131 'already_exists' => false,
Chris@76 2132 'can_copy' => file_exists($theme_dir . '/languages') ? is_writable($theme_dir . '/languages') : is_writable($theme_dir),
Chris@76 2133 );
Chris@76 2134
Chris@76 2135 $dir = dir($theme_dir);
Chris@76 2136 while ($entry = $dir->read())
Chris@76 2137 {
Chris@76 2138 if (substr($entry, -13) == '.template.php' && isset($context['available_templates'][substr($entry, 0, -13)]))
Chris@76 2139 {
Chris@76 2140 $context['available_templates'][substr($entry, 0, -13)]['already_exists'] = true;
Chris@76 2141 $context['available_templates'][substr($entry, 0, -13)]['can_copy'] = is_writable($theme_dir . '/' . $entry);
Chris@76 2142 }
Chris@76 2143 }
Chris@76 2144 $dir->close();
Chris@76 2145
Chris@76 2146 if (file_exists($theme_dir . '/languages'))
Chris@76 2147 {
Chris@76 2148 $dir = dir($theme_dir . '/languages');
Chris@76 2149 while ($entry = $dir->read())
Chris@76 2150 {
Chris@76 2151 if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches) && isset($context['available_language_files'][$matches[1]]))
Chris@76 2152 {
Chris@76 2153 $context['available_language_files'][$matches[1]]['already_exists'] = true;
Chris@76 2154 $context['available_language_files'][$matches[1]]['can_copy'] = is_writable($theme_dir . '/languages/' . $entry);
Chris@76 2155 }
Chris@76 2156 }
Chris@76 2157 $dir->close();
Chris@76 2158 }
Chris@76 2159
Chris@76 2160 $context['sub_template'] = 'copy_template';
Chris@76 2161 }
Chris@76 2162
Chris@76 2163 ?>