PageRenderTime 69ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/forum/Sources/Themes.php

https://github.com/leftnode/nooges.com
PHP | 2508 lines | 1935 code | 308 blank | 265 comment | 301 complexity | 564f1dc790a20e495d6fd5f666a1c915 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**********************************************************************************
  3. * Themes.php *
  4. ***********************************************************************************
  5. * SMF: Simple Machines Forum *
  6. * Open-Source Project Inspired by Zef Hemel (zef@zefhemel.com) *
  7. * =============================================================================== *
  8. * Software Version: SMF 2.0 RC2 *
  9. * Software by: Simple Machines (http://www.simplemachines.org) *
  10. * Copyright 2006-2009 by: Simple Machines LLC (http://www.simplemachines.org) *
  11. * 2001-2006 by: Lewis Media (http://www.lewismedia.com) *
  12. * Support, News, Updates at: http://www.simplemachines.org *
  13. ***********************************************************************************
  14. * This program is free software; you may redistribute it and/or modify it under *
  15. * the terms of the provided license as published by Simple Machines LLC. *
  16. * *
  17. * This program is distributed in the hope that it is and will be useful, but *
  18. * WITHOUT ANY WARRANTIES; without even any implied warranty of MERCHANTABILITY *
  19. * or FITNESS FOR A PARTICULAR PURPOSE. *
  20. * *
  21. * See the "license.txt" file for details of the Simple Machines license. *
  22. * The latest version can always be found at http://www.simplemachines.org. *
  23. **********************************************************************************/
  24. if (!defined('SMF'))
  25. die('Hacking attempt...');
  26. /* This file concerns itself almost completely with theme administration.
  27. Its tasks include changing theme settings, installing and removing
  28. themes, choosing the current theme, and editing themes. This is done in:
  29. void ThemesMain()
  30. - manages the action and delegates control to the proper sub action.
  31. - loads both the Themes and Settings language files.
  32. - checks the session by GET or POST to verify the sent data.
  33. - requires the user not be a guest.
  34. - is accessed via ?action=admin;area=theme.
  35. void ThemeAdmin()
  36. - administrates themes and their settings, as well as global theme
  37. settings.
  38. - sets the settings theme_allow, theme_guests, and knownThemes.
  39. - loads the template Themes.
  40. - requires the admin_forum permission.
  41. - accessed with ?action=admin;area=theme;sa=admin.
  42. void ThemeList()
  43. - lists the available themes.
  44. - provides an interface to reset the paths of all the installed themes.
  45. void SetThemeOptions()
  46. // !!!
  47. void SetThemeSettings()
  48. - saves and requests global theme settings. ($settings)
  49. - loads the Admin language file.
  50. - calls ThemeAdmin() if no theme is specified. (the theme center.)
  51. - requires an administrator.
  52. - accessed with ?action=admin;area=theme;sa=settings&id=xx.
  53. void RemoveTheme()
  54. - removes an installed theme.
  55. - requires an administrator.
  56. - accessed with ?action=admin;area=theme;sa=remove.
  57. void PickTheme()
  58. - allows user or administrator to pick a new theme with an interface.
  59. - can edit everyone's (u = 0), guests' (u = -1), or a specific user's.
  60. - uses the Themes template. (pick sub template.)
  61. - accessed with ?action=admin;area=theme;sa=pick.
  62. void ThemeInstall()
  63. - installs new themes, either from a gzip or copy of the default.
  64. - requires an administrator.
  65. - puts themes in $boardurl/Themes.
  66. - assumes the gzip has a root directory in it. (ie default.)
  67. - accessed with ?action=admin;area=theme;sa=install.
  68. void WrapAction()
  69. - allows the theme to take care of actions.
  70. - happens if $settings['catch_action'] is set and action isn't found
  71. in the action array.
  72. - can use a template, layers, sub_template, filename, and/or function.
  73. void SetJavaScript()
  74. - sets a theme option without outputting anything.
  75. - can be used with javascript, via a dummy image... (which doesn't
  76. require the page to reload.)
  77. - requires someone who is logged in.
  78. - accessed via ?action=jsoption;var=variable;val=value;session_var=sess_id.
  79. - does not log access to the Who's Online log. (in index.php..)
  80. void EditTheme()
  81. - shows an interface for editing the templates.
  82. - uses the Themes template and edit_template/edit_style sub template.
  83. - accessed via ?action=admin;area=theme;sa=edit
  84. function convert_template($output_dir, $old_template = '')
  85. // !!!
  86. function phpcodefix(string string)
  87. // !!!
  88. function makeStyleChanges(&$old_template)
  89. // !!!
  90. // !!! Update this for the new package manager?
  91. Creating and distributing theme packages:
  92. ---------------------------------------------------------------------------
  93. There isn't that much required to package and distribute your own
  94. themes... just do the following:
  95. - create a theme_info.xml file, with the root element theme-info.
  96. - its name should go in a name element, just like description.
  97. - your name should go in author. (email in the email attribute.)
  98. - any support website for the theme should be in website.
  99. - layers and templates (non-default) should go in those elements ;).
  100. - if the images dir isn't images, specify in the images element.
  101. - any extra rows for themes should go in extra, serialized.
  102. (as in array(variable => value).)
  103. - tar and gzip the directory - and you're done!
  104. - please include any special license in a license.txt file.
  105. // !!! Thumbnail?
  106. */
  107. // Subaction handler.
  108. function ThemesMain()
  109. {
  110. global $txt, $context, $scripturl;
  111. // Load the important language files...
  112. loadLanguage('Themes');
  113. loadLanguage('Settings');
  114. // No funny business - guests only.
  115. is_not_guest();
  116. // Default the page title to Theme Administration by default.
  117. $context['page_title'] = $txt['themeadmin_title'];
  118. // Theme administration, removal, choice, or installation...
  119. $subActions = array(
  120. 'admin' => 'ThemeAdmin',
  121. 'list' => 'ThemeList',
  122. 'reset' => 'SetThemeOptions',
  123. 'settings' => 'SetThemeSettings',
  124. 'options' => 'SetThemeOptions',
  125. 'install' => 'ThemeInstall',
  126. 'remove' => 'RemoveTheme',
  127. 'pick' => 'PickTheme',
  128. 'edit' => 'EditTheme',
  129. 'copy' => 'CopyTemplate',
  130. );
  131. // !!! Layout Settings?
  132. if (!empty($context['admin_menu_name']))
  133. {
  134. $context[$context['admin_menu_name']]['tab_data'] = array(
  135. 'title' => $txt['themeadmin_title'],
  136. 'help' => 'themes',
  137. 'description' => $txt['themeadmin_description'],
  138. 'tabs' => array(
  139. 'admin' => array(
  140. 'description' => $txt['themeadmin_admin_desc'],
  141. ),
  142. 'list' => array(
  143. 'description' => $txt['themeadmin_list_desc'],
  144. ),
  145. 'reset' => array(
  146. 'description' => $txt['themeadmin_reset_desc'],
  147. ),
  148. 'edit' => array(
  149. 'description' => $txt['themeadmin_edit_desc'],
  150. ),
  151. ),
  152. );
  153. }
  154. // Follow the sa or just go to administration.
  155. if (isset($_GET['sa']) && !empty($subActions[$_GET['sa']]))
  156. $subActions[$_GET['sa']]();
  157. else
  158. $subActions['admin']();
  159. }
  160. function ThemeAdmin()
  161. {
  162. global $context, $boarddir, $modSettings, $smcFunc;
  163. loadLanguage('Admin');
  164. isAllowedTo('admin_forum');
  165. // If we aren't submitting - that is, if we are about to...
  166. if (!isset($_POST['submit']))
  167. {
  168. loadTemplate('Themes');
  169. // Make our known themes a little easier to work with.
  170. $knownThemes = !empty($modSettings['knownThemes']) ? explode(',',$modSettings['knownThemes']) : array();
  171. // Load up all the themes.
  172. $request = $smcFunc['db_query']('', '
  173. SELECT id_theme, value AS name
  174. FROM {db_prefix}themes
  175. WHERE variable = {string:name}
  176. AND id_member = {int:no_member}
  177. ORDER BY id_theme',
  178. array(
  179. 'no_member' => 0,
  180. 'name' => 'name',
  181. )
  182. );
  183. $context['themes'] = array();
  184. while ($row = $smcFunc['db_fetch_assoc']($request))
  185. $context['themes'][] = array(
  186. 'id' => $row['id_theme'],
  187. 'name' => $row['name'],
  188. 'known' => in_array($row['id_theme'], $knownThemes),
  189. );
  190. $smcFunc['db_free_result']($request);
  191. // Can we create a new theme?
  192. $context['can_create_new'] = is_writable($boarddir . '/Themes');
  193. $context['new_theme_dir'] = substr(realpath($boarddir . '/Themes/default'), 0, -7);
  194. // Look for a non existent theme directory. (ie theme87.)
  195. $theme_dir = $boarddir . '/Themes/theme';
  196. $i = 1;
  197. while (file_exists($theme_dir . $i))
  198. $i++;
  199. $context['new_theme_name'] = 'theme' . $i;
  200. }
  201. else
  202. {
  203. checkSession();
  204. if (isset($_POST['options']['known_themes']))
  205. foreach ($_POST['options']['known_themes'] as $key => $id)
  206. $_POST['options']['known_themes'][$key] = (int) $id;
  207. else
  208. fatal_lang_error('themes_none_selectable', false);
  209. if (!in_array($_POST['options']['theme_guests'], $_POST['options']['known_themes']))
  210. fatal_lang_error('themes_default_selectable', false);
  211. // Commit the new settings.
  212. updateSettings(array(
  213. 'theme_allow' => $_POST['options']['theme_allow'],
  214. 'theme_guests' => $_POST['options']['theme_guests'],
  215. 'knownThemes' => implode(',', $_POST['options']['known_themes']),
  216. ));
  217. if ((int) $_POST['theme_reset'] == 0 || in_array($_POST['theme_reset'], $_POST['options']['known_themes']))
  218. updateMemberData(null, array('id_theme' => (int) $_POST['theme_reset']));
  219. redirectexit('action=admin;area=theme;' . $context['session_var'] . '=' . $context['session_id'] . ';sa=admin');
  220. }
  221. }
  222. function ThemeList()
  223. {
  224. global $context, $boarddir, $boardurl, $smcFunc;
  225. loadLanguage('Admin');
  226. isAllowedTo('admin_forum');
  227. if (isset($_POST['submit']))
  228. {
  229. checkSession();
  230. $request = $smcFunc['db_query']('', '
  231. SELECT id_theme, variable, value
  232. FROM {db_prefix}themes
  233. WHERE variable IN ({string:theme_dir}, {string:theme_url}, {string:images_url}, {string:base_theme_dir}, {string:base_theme_url}, {string:base_images_url})
  234. AND id_member = {int:no_member}',
  235. array(
  236. 'no_member' => 0,
  237. 'theme_dir' => 'theme_dir',
  238. 'theme_url' => 'theme_url',
  239. 'images_url' => 'images_url',
  240. 'base_theme_dir' => 'base_theme_dir',
  241. 'base_theme_url' => 'base_theme_url',
  242. 'base_images_url' => 'base_images_url',
  243. )
  244. );
  245. $themes = array();
  246. while ($row = $smcFunc['db_fetch_assoc']($request))
  247. $themes[$row['id_theme']][$row['variable']] = $row['value'];
  248. $smcFunc['db_free_result']($request);
  249. $setValues = array();
  250. foreach ($themes as $id => $theme)
  251. {
  252. if (file_exists($_POST['reset_dir'] . '/' . basename($theme['theme_dir'])))
  253. {
  254. $setValues[] = array($id, 0, 'theme_dir', realpath($_POST['reset_dir'] . '/' . basename($theme['theme_dir'])));
  255. $setValues[] = array($id, 0, 'theme_url', $_POST['reset_url'] . '/' . basename($theme['theme_dir']));
  256. $setValues[] = array($id, 0, 'images_url', $_POST['reset_url'] . '/' . basename($theme['theme_dir']) . '/' . basename($theme['images_url']));
  257. }
  258. if (isset($theme['base_theme_dir']) && file_exists($_POST['reset_dir'] . '/' . basename($theme['base_theme_dir'])))
  259. {
  260. $setValues[] = array($id, 0, 'base_theme_dir', realpath($_POST['reset_dir'] . '/' . basename($theme['base_theme_dir'])));
  261. $setValues[] = array($id, 0, 'base_theme_url', $_POST['reset_url'] . '/' . basename($theme['base_theme_dir']));
  262. $setValues[] = array($id, 0, 'base_images_url', $_POST['reset_url'] . '/' . basename($theme['base_theme_dir']) . '/' . basename($theme['base_images_url']));
  263. }
  264. cache_put_data('theme_settings-' . $id, null, 90);
  265. }
  266. if (!empty($setValues))
  267. {
  268. $smcFunc['db_insert']('replace',
  269. '{db_prefix}themes',
  270. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  271. $setValues,
  272. array('id_theme', 'variable', 'id_member')
  273. );
  274. }
  275. redirectexit('action=admin;area=theme;sa=list;' . $context['session_var'] . '=' . $context['session_id']);
  276. }
  277. loadTemplate('Themes');
  278. $request = $smcFunc['db_query']('', '
  279. SELECT id_theme, variable, value
  280. FROM {db_prefix}themes
  281. WHERE variable IN ({string:name}, {string:theme_dir}, {string:theme_url}, {string:images_url})
  282. AND id_member = {int:no_member}',
  283. array(
  284. 'no_member' => 0,
  285. 'name' => 'name',
  286. 'theme_dir' => 'theme_dir',
  287. 'theme_url' => 'theme_url',
  288. 'images_url' => 'images_url',
  289. )
  290. );
  291. $context['themes'] = array();
  292. while ($row = $smcFunc['db_fetch_assoc']($request))
  293. {
  294. if (!isset($context['themes'][$row['id_theme']]))
  295. $context['themes'][$row['id_theme']] = array(
  296. 'id' => $row['id_theme'],
  297. );
  298. $context['themes'][$row['id_theme']][$row['variable']] = $row['value'];
  299. }
  300. $smcFunc['db_free_result']($request);
  301. foreach ($context['themes'] as $i => $theme)
  302. {
  303. $context['themes'][$i]['theme_dir'] = realpath($context['themes'][$i]['theme_dir']);
  304. if (file_exists($context['themes'][$i]['theme_dir'] . '/index.template.php'))
  305. {
  306. // Fetch the header... a good 256 bytes should be more than enough.
  307. $fp = fopen($context['themes'][$i]['theme_dir'] . '/index.template.php', 'rb');
  308. $header = fread($fp, 256);
  309. fclose($fp);
  310. // Can we find a version comment, at all?
  311. if (preg_match('~(?://|/\*)\s*Version:\s+(.+?);\s*index(?:[\s]{2}|\*/)~i', $header, $match) == 1)
  312. $context['themes'][$i]['version'] = $match[1];
  313. }
  314. $context['themes'][$i]['valid_path'] = file_exists($context['themes'][$i]['theme_dir']) && is_dir($context['themes'][$i]['theme_dir']);
  315. }
  316. $context['reset_dir'] = realpath($boarddir . '/Themes');
  317. $context['reset_url'] = $boardurl . '/Themes';
  318. $context['sub_template'] = 'list_themes';
  319. }
  320. // Administrative global settings.
  321. function SetThemeOptions()
  322. {
  323. global $txt, $context, $settings, $modSettings, $smcFunc;
  324. $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (isset($_GET['id']) ? (int) $_GET['id'] : 0);
  325. isAllowedTo('admin_forum');
  326. if (empty($_GET['th']) && empty($_GET['id']))
  327. {
  328. $request = $smcFunc['db_query']('', '
  329. SELECT id_theme, variable, value
  330. FROM {db_prefix}themes
  331. WHERE variable IN ({string:name}, {string:theme_dir})
  332. AND id_member = {int:no_member}',
  333. array(
  334. 'no_member' => 0,
  335. 'name' => 'name',
  336. 'theme_dir' => 'theme_dir',
  337. )
  338. );
  339. $context['themes'] = array();
  340. while ($row = $smcFunc['db_fetch_assoc']($request))
  341. {
  342. if (!isset($context['themes'][$row['id_theme']]))
  343. $context['themes'][$row['id_theme']] = array(
  344. 'id' => $row['id_theme'],
  345. 'num_default_options' => 0,
  346. 'num_members' => 0,
  347. );
  348. $context['themes'][$row['id_theme']][$row['variable']] = $row['value'];
  349. }
  350. $smcFunc['db_free_result']($request);
  351. $request = $smcFunc['db_query']('', '
  352. SELECT id_theme, COUNT(*) AS value
  353. FROM {db_prefix}themes
  354. WHERE id_member = {int:guest_member}
  355. GROUP BY id_theme',
  356. array(
  357. 'guest_member' => -1,
  358. )
  359. );
  360. while ($row = $smcFunc['db_fetch_assoc']($request))
  361. $context['themes'][$row['id_theme']]['num_default_options'] = $row['value'];
  362. $smcFunc['db_free_result']($request);
  363. // Need to make sure we don't do custom fields.
  364. $request = $smcFunc['db_query']('', '
  365. SELECT col_name
  366. FROM {db_prefix}custom_fields',
  367. array(
  368. )
  369. );
  370. $customFields = array();
  371. while ($row = $smcFunc['db_fetch_assoc']($request))
  372. $customFields[] = $row['col_name'];
  373. $smcFunc['db_free_result']($request);
  374. $customFieldsQuery = empty($customFields) ? '' : ('AND variable NOT IN ({array_string:custom_fields})');
  375. $request = $smcFunc['db_query']('themes_count', '
  376. SELECT COUNT(DISTINCT id_member) AS value, id_theme
  377. FROM {db_prefix}themes
  378. WHERE id_member > {int:no_member}
  379. ' . $customFieldsQuery . '
  380. GROUP BY id_theme',
  381. array(
  382. 'no_member' => 0,
  383. 'custom_fields' => empty($customFields) ? array() : $customFields,
  384. )
  385. );
  386. while ($row = $smcFunc['db_fetch_assoc']($request))
  387. $context['themes'][$row['id_theme']]['num_members'] = $row['value'];
  388. $smcFunc['db_free_result']($request);
  389. // There has to be a Settings template!
  390. foreach ($context['themes'] as $k => $v)
  391. if (empty($v['theme_dir']) || (!file_exists($v['theme_dir'] . '/Settings.template.php') && empty($v['num_members'])))
  392. unset($context['themes'][$k]);
  393. loadTemplate('Themes');
  394. $context['sub_template'] = 'reset_list';
  395. return;
  396. }
  397. // Submit?
  398. if (isset($_POST['submit']) && empty($_POST['who']))
  399. {
  400. checkSession();
  401. if (empty($_POST['options']))
  402. $_POST['options'] = array();
  403. if (empty($_POST['default_options']))
  404. $_POST['default_options'] = array();
  405. // Set up the sql query.
  406. $setValues = array();
  407. foreach ($_POST['options'] as $opt => $val)
  408. $setValues[] = array(-1, $_GET['th'], $opt, is_array($val) ? implode(',', $val) : $val);
  409. $old_settings = array();
  410. foreach ($_POST['default_options'] as $opt => $val)
  411. {
  412. $old_settings[] = $opt;
  413. $setValues[] = array(-1, 1, $opt, is_array($val) ? implode(',', $val) : $val);
  414. }
  415. // If we're actually inserting something..
  416. if (!empty($setValues))
  417. {
  418. // Are there options in non-default themes set that should be cleared?
  419. if (!empty($old_settings))
  420. $smcFunc['db_query']('', '
  421. DELETE FROM {db_prefix}themes
  422. WHERE id_theme != {int:default_theme}
  423. AND id_member = {int:guest_member}
  424. AND variable IN ({array_string:old_settings})',
  425. array(
  426. 'default_theme' => 1,
  427. 'guest_member' => -1,
  428. 'old_settings' => $old_settings,
  429. )
  430. );
  431. $smcFunc['db_insert']('replace',
  432. '{db_prefix}themes',
  433. array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  434. $setValues,
  435. array('id_theme', 'variable', 'id_member')
  436. );
  437. }
  438. cache_put_data('theme_settings-' . $_GET['th'], null, 90);
  439. cache_put_data('theme_settings-1', null, 90);
  440. redirectexit('action=admin;area=theme;' . $context['session_var'] . '=' . $context['session_id'] . ';sa=reset');
  441. }
  442. elseif (isset($_POST['submit']) && $_POST['who'] == 1)
  443. {
  444. checkSession();
  445. $_POST['options'] = empty($_POST['options']) ? array() : $_POST['options'];
  446. $_POST['options_master'] = empty($_POST['options_master']) ? array() : $_POST['options_master'];
  447. $_POST['default_options'] = empty($_POST['default_options']) ? array() : $_POST['default_options'];
  448. $_POST['default_options_master'] = empty($_POST['default_options_master']) ? array() : $_POST['default_options_master'];
  449. $old_settings = array();
  450. foreach ($_POST['default_options'] as $opt => $val)
  451. {
  452. if ($_POST['default_options_master'][$opt] == 0)
  453. continue;
  454. elseif ($_POST['default_options_master'][$opt] == 1)
  455. {
  456. // Delete then insert for ease of database compatibility!
  457. $smcFunc['db_query']('substring', '
  458. DELETE FROM {db_prefix}themes
  459. WHERE id_theme = {int:default_theme}
  460. AND id_member != {int:no_member}
  461. AND variable = SUBSTRING({string:option}, 1, 255)',
  462. array(
  463. 'default_theme' => 1,
  464. 'no_member' => 0,
  465. 'option' => $opt,
  466. )
  467. );
  468. $smcFunc['db_query']('substring', '
  469. INSERT INTO {db_prefix}themes
  470. (id_member, id_theme, variable, value)
  471. SELECT id_member, 1, SUBSTRING({string:option}, 1, 255), SUBSTRING({string:value}, 1, 65534)
  472. FROM {db_prefix}members',
  473. array(
  474. 'option' => $opt,
  475. 'value' => (is_array($val) ? implode(',', $val) : $val),
  476. )
  477. );
  478. $old_settings[] = $opt;
  479. }
  480. elseif ($_POST['default_options_master'][$opt] == 2)
  481. {
  482. $smcFunc['db_query']('', '
  483. DELETE FROM {db_prefix}themes
  484. WHERE variable = {string:option_name}
  485. AND id_member > {int:no_member}',
  486. array(
  487. 'no_member' => 0,
  488. 'option_name' => $opt,
  489. )
  490. );
  491. }
  492. }
  493. // Delete options from other themes.
  494. if (!empty($old_settings))
  495. $smcFunc['db_query']('', '
  496. DELETE FROM {db_prefix}themes
  497. WHERE id_theme != {int:default_theme}
  498. AND id_member > {int:no_member}
  499. AND variable IN ({array_string:old_settings})',
  500. array(
  501. 'default_theme' => 1,
  502. 'no_member' => 0,
  503. 'old_settings' => $old_settings,
  504. )
  505. );
  506. foreach ($_POST['options'] as $opt => $val)
  507. {
  508. if ($_POST['options_master'][$opt] == 0)
  509. continue;
  510. elseif ($_POST['options_master'][$opt] == 1)
  511. {
  512. // Delete then insert for ease of database compatibility - again!
  513. $smcFunc['db_query']('substring', '
  514. DELETE FROM {db_prefix}themes
  515. WHERE id_theme = {int:current_theme}
  516. AND id_member != {int:no_member}
  517. AND variable = SUBSTRING({string:option}, 1, 255)',
  518. array(
  519. 'current_theme' => $_GET['th'],
  520. 'no_member' => 0,
  521. 'option' => $opt,
  522. )
  523. );
  524. $smcFunc['db_query']('substring', '
  525. INSERT INTO {db_prefix}themes
  526. (id_member, id_theme, variable, value)
  527. SELECT id_member, {int:current_theme}, SUBSTRING({string:option}, 1, 255), SUBSTRING({string:value}, 1, 65534)
  528. FROM {db_prefix}members',
  529. array(
  530. 'current_theme' => $_GET['th'],
  531. 'option' => $opt,
  532. 'value' => (is_array($val) ? implode(',', $val) : $val),
  533. )
  534. );
  535. }
  536. elseif ($_POST['options_master'][$opt] == 2)
  537. {
  538. $smcFunc['db_query']('', '
  539. DELETE FROM {db_prefix}themes
  540. WHERE variable = {string:option}
  541. AND id_member > {int:no_member}
  542. AND id_theme = {int:current_theme}',
  543. array(
  544. 'no_member' => 0,
  545. 'current_theme' => $_GET['th'],
  546. 'option' => $opt,
  547. )
  548. );
  549. }
  550. }
  551. redirectexit('action=admin;area=theme;' . $context['session_var'] . '=' . $context['session_id'] . ';sa=reset');
  552. }
  553. elseif (!empty($_GET['who']) && $_GET['who'] == 2)
  554. {
  555. checkSession('get');
  556. // Don't delete custom fields!!
  557. if ($_GET['th'] == 1)
  558. {
  559. $request = $smcFunc['db_query']('', '
  560. SELECT col_name
  561. FROM {db_prefix}custom_fields',
  562. array(
  563. )
  564. );
  565. $customFields = array();
  566. while ($row = $smcFunc['db_fetch_assoc']($request))
  567. $customFields[] = $row['col_name'];
  568. $smcFunc['db_free_result']($request);
  569. }
  570. $customFieldsQuery = empty($customFields) ? '' : ('AND variable NOT IN ({array_string:custom_fields})');
  571. $smcFunc['db_query']('', '
  572. DELETE FROM {db_prefix}themes
  573. WHERE id_member > {int:no_member}
  574. AND id_theme = {int:current_theme}
  575. ' . $customFieldsQuery,
  576. array(
  577. 'no_member' => 0,
  578. 'current_theme' => $_GET['th'],
  579. 'custom_fields' => empty($customFields) ? array() : $customFields,
  580. )
  581. );
  582. redirectexit('action=admin;area=theme;' . $context['session_var'] . '=' . $context['session_id'] . ';sa=reset');
  583. }
  584. $old_id = $settings['theme_id'];
  585. $old_settings = $settings;
  586. loadTheme($_GET['th'], false);
  587. loadLanguage('Profile');
  588. //!!! Should we just move these options so they are no longer theme dependant?
  589. loadLanguage('PersonalMessage');
  590. // Let the theme take care of the settings.
  591. loadTemplate('Settings');
  592. loadSubTemplate('options');
  593. $context['sub_template'] = 'set_options';
  594. $context['page_title'] = $txt['theme_settings'];
  595. $context['options'] = $context['theme_options'];
  596. $context['theme_settings'] = $settings;
  597. if (empty($_REQUEST['who']))
  598. {
  599. $request = $smcFunc['db_query']('', '
  600. SELECT variable, value
  601. FROM {db_prefix}themes
  602. WHERE id_theme IN (1, {int:current_theme})
  603. AND id_member = {int:guest_member}',
  604. array(
  605. 'current_theme' => $_GET['th'],
  606. 'guest_member' => -1,
  607. )
  608. );
  609. $context['theme_options'] = array();
  610. while ($row = $smcFunc['db_fetch_assoc']($request))
  611. $context['theme_options'][$row['variable']] = $row['value'];
  612. $smcFunc['db_free_result']($request);
  613. $context['theme_options_reset'] = false;
  614. }
  615. else
  616. {
  617. $context['theme_options'] = array();
  618. $context['theme_options_reset'] = true;
  619. }
  620. foreach ($context['options'] as $i => $setting)
  621. {
  622. // Is this disabled?
  623. if ($setting['id'] == 'calendar_start_day' && empty($modSettings['cal_enabled']))
  624. {
  625. unset($context['options'][$i]);
  626. continue;
  627. }
  628. elseif (($setting['id'] == 'topics_per_page' || $setting['id'] == 'messages_per_page') && !empty($modSettings['disableCustomPerPage']))
  629. {
  630. unset($context['options'][$i]);
  631. continue;
  632. }
  633. if (!isset($setting['type']) || $setting['type'] == 'bool')
  634. $context['options'][$i]['type'] = 'checkbox';
  635. elseif ($setting['type'] == 'int' || $setting['type'] == 'integer')
  636. $context['options'][$i]['type'] = 'number';
  637. elseif ($setting['type'] == 'string')
  638. $context['options'][$i]['type'] = 'text';
  639. if (isset($setting['options']))
  640. $context['options'][$i]['type'] = 'list';
  641. $context['options'][$i]['value'] = !isset($context['theme_options'][$setting['id']]) ? '' : $context['theme_options'][$setting['id']];
  642. }
  643. // Restore the existing theme.
  644. loadTheme($old_id, false);
  645. $settings = $old_settings;
  646. loadTemplate('Themes');
  647. }
  648. // Administrative global settings.
  649. function SetThemeSettings()
  650. {
  651. global $txt, $context, $settings, $modSettings, $sourcedir, $smcFunc;
  652. if (empty($_GET['th']) && empty($_GET['id']))
  653. return ThemeAdmin();
  654. $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
  655. // Select the best fitting tab.
  656. $context[$context['admin_menu_name']]['current_subsection'] = 'list';
  657. loadLanguage('Admin');
  658. isAllowedTo('admin_forum');
  659. // Validate inputs/user.
  660. if (empty($_GET['th']))
  661. fatal_lang_error('no_theme', false);
  662. // Fetch the smiley sets...
  663. $sets = explode(',', 'none,' . $modSettings['smiley_sets_known']);
  664. $set_names = explode("\n", $txt['smileys_none'] . "\n" . $modSettings['smiley_sets_names']);
  665. $context['smiley_sets'] = array(
  666. '' => $txt['smileys_no_default']
  667. );
  668. foreach ($sets as $i => $set)
  669. $context['smiley_sets'][$set] = $set_names[$i];
  670. $old_id = $settings['theme_id'];
  671. $old_settings = $settings;
  672. loadTheme($_GET['th'], false);
  673. // Sadly we really do need to init the template.
  674. loadSubTemplate('init', 'ignore');
  675. // Also load the actual themes language file - in case of special settings.
  676. loadLanguage('Settings', '', true, true);
  677. // Let the theme take care of the settings.
  678. loadTemplate('Settings');
  679. loadSubTemplate('settings');
  680. // Submitting!
  681. if (isset($_POST['submit']))
  682. {
  683. checkSession();
  684. if (empty($_POST['options']))
  685. $_POST['options'] = array();
  686. if (empty($_POST['default_options']))
  687. $_POST['default_options'] = array();
  688. // Make sure items are cast correctly.
  689. foreach ($context['theme_settings'] as $item)
  690. {
  691. // Disregard this item if this is just a separator.
  692. if (!is_array($item))
  693. continue;
  694. foreach (array('options', 'default_options') as $option)
  695. {
  696. if (!isset($_POST[$option][$item['id']]))
  697. continue;
  698. // Checkbox.
  699. elseif (empty($item['type']))
  700. $_POST[$option][$item['id']] = $_POST[$option][$item['id']] ? 1 : 0;
  701. // Number
  702. elseif ($item['type'] == 'number')
  703. $_POST[$option][$item['id']] = (int) $_POST[$option][$item['id']];
  704. }
  705. }
  706. // Set up the sql query.
  707. $inserts = array();
  708. foreach ($_POST['options'] as $opt => $val)
  709. $inserts[] = array(0, $_GET['th'], $opt, is_array($val) ? implode(',', $val) : $val);
  710. foreach ($_POST['default_options'] as $opt => $val)
  711. $inserts[] = array(0, 1, $opt, is_array($val) ? implode(',', $val) : $val);
  712. // If we're actually inserting something..
  713. if (!empty($inserts))
  714. {
  715. $smcFunc['db_insert']('replace',
  716. '{db_prefix}themes',
  717. array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  718. $inserts,
  719. array('id_member', 'id_theme', 'variable')
  720. );
  721. }
  722. cache_put_data('theme_settings-' . $_GET['th'], null, 90);
  723. cache_put_data('theme_settings-1', null, 90);
  724. // Invalidate the cache.
  725. updateSettings(array('settings_updated' => time()));
  726. redirectexit('action=admin;area=theme;sa=settings;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id']);
  727. }
  728. $context['sub_template'] = 'set_settings';
  729. $context['page_title'] = $txt['theme_settings'];
  730. foreach ($settings as $setting => $dummy)
  731. {
  732. if (!in_array($setting, array('theme_url', 'theme_dir', 'images_url', 'template_dirs')))
  733. $settings[$setting] = htmlspecialchars__recursive($settings[$setting]);
  734. }
  735. $context['settings'] = $context['theme_settings'];
  736. $context['theme_settings'] = $settings;
  737. foreach ($context['settings'] as $i => $setting)
  738. {
  739. // Separators are dummies, so leave them alone.
  740. if (!is_array($setting))
  741. continue;
  742. if (!isset($setting['type']) || $setting['type'] == 'bool')
  743. $context['settings'][$i]['type'] = 'checkbox';
  744. elseif ($setting['type'] == 'int' || $setting['type'] == 'integer')
  745. $context['settings'][$i]['type'] = 'number';
  746. elseif ($setting['type'] == 'string')
  747. $context['settings'][$i]['type'] = 'text';
  748. if (isset($setting['options']))
  749. $context['settings'][$i]['type'] = 'list';
  750. $context['settings'][$i]['value'] = !isset($settings[$setting['id']]) ? '' : $settings[$setting['id']];
  751. }
  752. // Do we support variants?
  753. if (!empty($settings['theme_variants']))
  754. {
  755. $context['theme_variants'] = array();
  756. foreach ($settings['theme_variants'] as $variant)
  757. {
  758. // Have any text, old chap?
  759. $context['theme_variants'][$variant] = array(
  760. 'label' => isset($txt['variant_' . $variant]) ? $txt['variant_' . $variant] : $variant,
  761. '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'),
  762. );
  763. }
  764. $context['default_variant'] = !empty($settings['default_variant']) && isset($context['theme_variants'][$settings['default_variant']]) ? $settings['default_variant'] : $settings['theme_variants'][0];
  765. }
  766. // Restore the current theme.
  767. loadTheme($old_id, false);
  768. // Reinit just incase.
  769. loadSubTemplate('init', 'ignore');
  770. $settings = $old_settings;
  771. loadTemplate('Themes');
  772. }
  773. // Remove a theme from the database.
  774. function RemoveTheme()
  775. {
  776. global $modSettings, $context, $smcFunc;
  777. checkSession('get');
  778. isAllowedTo('admin_forum');
  779. // The theme's ID must be an integer.
  780. $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
  781. // You can't delete the default theme!
  782. if ($_GET['th'] == 1)
  783. fatal_lang_error('no_access', false);
  784. $known = explode(',', $modSettings['knownThemes']);
  785. for ($i = 0, $n = count($known); $i < $n; $i++)
  786. {
  787. if ($known[$i] == $_GET['th'])
  788. unset($known[$i]);
  789. }
  790. $smcFunc['db_query']('', '
  791. DELETE FROM {db_prefix}themes
  792. WHERE id_theme = {int:current_theme}',
  793. array(
  794. 'current_theme' => $_GET['th'],
  795. )
  796. );
  797. $smcFunc['db_query']('', '
  798. UPDATE {db_prefix}members
  799. SET id_theme = {int:default_theme}
  800. WHERE id_theme = {int:current_theme}',
  801. array(
  802. 'default_theme' => 0,
  803. 'current_theme' => $_GET['th'],
  804. )
  805. );
  806. $smcFunc['db_query']('', '
  807. UPDATE {db_prefix}boards
  808. SET id_theme = {int:default_theme}
  809. WHERE id_theme = {int:current_theme}',
  810. array(
  811. 'default_theme' => 0,
  812. 'current_theme' => $_GET['th'],
  813. )
  814. );
  815. $known = strtr(implode(',', $known), array(',,' => ','));
  816. // Fix it if the theme was the overall default theme.
  817. if ($modSettings['theme_guests'] == $_GET['th'])
  818. updateSettings(array('theme_guests' => '1', 'knownThemes' => $known));
  819. else
  820. updateSettings(array('knownThemes' => $known));
  821. // Remove any cached language files to keep space minimum!
  822. clean_cache('lang');
  823. redirectexit('action=admin;area=theme;sa=list;' . $context['session_var'] . '=' . $context['session_id']);
  824. }
  825. // Choose a theme from a list.
  826. function PickTheme()
  827. {
  828. global $txt, $context, $modSettings, $user_info, $language, $smcFunc, $settings;
  829. loadLanguage('Profile');
  830. loadTemplate('Themes');
  831. $_SESSION['id_theme'] = 0;
  832. if (isset($_GET['id']))
  833. $_GET['th'] = $_GET['id'];
  834. // Saving a variant cause JS doesn't work - pretend it did ;)
  835. if (isset($_POST['save']))
  836. {
  837. // Which theme?
  838. foreach ($_POST['save'] as $k => $v)
  839. $_GET['th'] = (int) $k;
  840. if (isset($_POST['vrt'][$k]))
  841. $_GET['vrt'] = $_POST['vrt'][$k];
  842. }
  843. // Have we made a desicion, or are we just browsing?
  844. if (isset($_GET['th']))
  845. {
  846. checkSession('get');
  847. $_GET['th'] = (int) $_GET['th'];
  848. // Save for this user.
  849. if (!isset($_REQUEST['u']) || !allowedTo('admin_forum'))
  850. {
  851. updateMemberData($user_info['id'], array('id_theme' => (int) $_GET['th']));
  852. // A variants to save for the user?
  853. if (!empty($_GET['vrt']))
  854. {
  855. $smcFunc['db_insert']('replace',
  856. '{db_prefix}themes',
  857. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  858. array($_GET['th'], $user_info['id'], 'theme_variant', $_GET['vrt']),
  859. array('id_theme', 'id_member', 'variable')
  860. );
  861. cache_put_data('theme_settings-' . $_GET['th'] . ':' . $user_info['id'], null, 90);
  862. $_SESSION['id_variant'] = 0;
  863. }
  864. redirectexit('action=profile;area=theme');
  865. }
  866. // If changing members or guests - and there's a variant - assume changing default variant.
  867. if (!empty($_GET['vrt']) && ($_REQUEST['u'] == '0' || $_REQUEST['u'] == '-1'))
  868. {
  869. $smcFunc['db_insert']('replace',
  870. '{db_prefix}themes',
  871. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  872. array($_GET['th'], 0, 'default_variant', $_GET['vrt']),
  873. array('id_theme', 'id_member', 'variable')
  874. );
  875. // Make it obvious that it's changed
  876. cache_put_data('theme_settings-' . $_GET['th'], null, 90);
  877. }
  878. // For everyone.
  879. if ($_REQUEST['u'] == '0')
  880. {
  881. updateMemberData(null, array('id_theme' => (int) $_GET['th']));
  882. // Remove any custom variants.
  883. if (!empty($_GET['vrt']))
  884. {
  885. $smcFunc['db_query']('', '
  886. DELETE FROM {db_prefix}themes
  887. WHERE id_theme = {int:current_theme}
  888. AND variable = {string:theme_variant}',
  889. array(
  890. 'current_theme' => (int) $_GET['th'],
  891. 'theme_variant' => 'theme_variant',
  892. )
  893. );
  894. }
  895. redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
  896. }
  897. // Change the default/guest theme.
  898. elseif ($_REQUEST['u'] == '-1')
  899. {
  900. updateSettings(array('theme_guests' => (int) $_GET['th']));
  901. redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
  902. }
  903. // Change a specific member's theme.
  904. else
  905. {
  906. updateMemberData((int) $_REQUEST['u'], array('id_theme' => (int) $_GET['th']));
  907. if (!empty($_GET['vrt']))
  908. {
  909. $smcFunc['db_insert']('replace',
  910. '{db_prefix}themes',
  911. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  912. array($_GET['th'], (int) $_REQUEST['u'], 'theme_variant', $_GET['vrt']),
  913. array('id_theme', 'id_member', 'variable')
  914. );
  915. cache_put_data('theme_settings-' . $_GET['th'] . ':' . (int) $_REQUEST['u'], null, 90);
  916. if ($user_info['id'] == $_REQUEST['u'])
  917. $_SESSION['id_variant'] = 0;
  918. }
  919. redirectexit('action=profile;u=' . (int) $_REQUEST['u'] . ';area=theme');
  920. }
  921. }
  922. // Figure out who the member of the minute is, and what theme they've chosen.
  923. if (!isset($_REQUEST['u']) || !allowedTo('admin_forum'))
  924. {
  925. $context['current_member'] = $user_info['id'];
  926. $context['current_theme'] = $user_info['theme'];
  927. }
  928. // Everyone can't chose just one.
  929. elseif ($_REQUEST['u'] == '0')
  930. {
  931. $context['current_member'] = 0;
  932. $context['current_theme'] = 0;
  933. }
  934. // Guests and such...
  935. elseif ($_REQUEST['u'] == '-1')
  936. {
  937. $context['current_member'] = -1;
  938. $context['current_theme'] = $modSettings['theme_guests'];
  939. }
  940. // Someones else :P.
  941. else
  942. {
  943. $context['current_member'] = (int) $_REQUEST['u'];
  944. $request = $smcFunc['db_query']('', '
  945. SELECT id_theme
  946. FROM {db_prefix}members
  947. WHERE id_member = {int:current_member}
  948. LIMIT 1',
  949. array(
  950. 'current_member' => $context['current_member'],
  951. )
  952. );
  953. list ($context['current_theme']) = $smcFunc['db_fetch_row']($request);
  954. $smcFunc['db_free_result']($request);
  955. }
  956. // Get the theme name and descriptions.
  957. $context['available_themes'] = array();
  958. if (!empty($modSettings['knownThemes']))
  959. {
  960. $request = $smcFunc['db_query']('', '
  961. SELECT id_theme, variable, value
  962. FROM {db_prefix}themes
  963. WHERE variable IN ({string:name}, {string:theme_url}, {string:theme_dir}, {string:images_url}, {string:disable_user_variant})' . (!allowedTo('admin_forum') ? '
  964. AND id_theme IN ({array_string:known_themes})' : '') . '
  965. AND id_theme != {int:default_theme}
  966. AND id_member = {int:no_member}',
  967. array(
  968. 'default_theme' => 0,
  969. 'name' => 'name',
  970. 'no_member' => 0,
  971. 'theme_url' => 'theme_url',
  972. 'theme_dir' => 'theme_dir',
  973. 'images_url' => 'images_url',
  974. 'disable_user_variant' => 'disable_user_variant',
  975. 'known_themes' => explode(',', $modSettings['knownThemes']),
  976. )
  977. );
  978. while ($row = $smcFunc['db_fetch_assoc']($request))
  979. {
  980. if (!isset($context['available_themes'][$row['id_theme']]))
  981. $context['available_themes'][$row['id_theme']] = array(
  982. 'id' => $row['id_theme'],
  983. 'selected' => $context['current_theme'] == $row['id_theme'],
  984. 'num_users' => 0
  985. );
  986. $context['available_themes'][$row['id_theme']][$row['variable']] = $row['value'];
  987. }
  988. $smcFunc['db_free_result']($request);
  989. }
  990. // Okay, this is a complicated problem: the default theme is 1, but they aren't allowed to access 1!
  991. if (!isset($context['available_themes'][$modSettings['theme_guests']]))
  992. {
  993. $context['available_themes'][0] = array(
  994. 'num_users' => 0
  995. );
  996. $guest_theme = 0;
  997. }
  998. else
  999. $guest_theme = $modSettings['theme_guests'];
  1000. $request = $smcFunc['db_query']('', '
  1001. SELECT id_theme, COUNT(*) AS the_count
  1002. FROM {db_prefix}members
  1003. GROUP BY id_theme
  1004. ORDER BY id_theme DESC',
  1005. array(
  1006. )
  1007. );
  1008. while ($row = $smcFunc['db_fetch_assoc']($request))
  1009. {
  1010. // Figure out which theme it is they are REALLY using.
  1011. if (!empty($modSettings['knownThemes']) && !in_array($row['id_theme'], explode(',',$modSettings['knownThemes'])))
  1012. $row['id_theme'] = $guest_theme;
  1013. elseif (empty($modSettings['theme_allow']))
  1014. $row['id_theme'] = $guest_theme;
  1015. if (isset($context['available_themes'][$row['id_theme']]))
  1016. $context['available_themes'][$row['id_theme']]['num_users'] += $row['the_count'];
  1017. else
  1018. $context['available_themes'][$guest_theme]['num_users'] += $row['the_count'];
  1019. }
  1020. $smcFunc['db_free_result']($request);
  1021. // Get any member variant preferences.
  1022. $variant_preferences = array();
  1023. if ($context['current_member'] > 0)
  1024. {
  1025. $request = $smcFunc['db_query']('', '
  1026. SELECT id_theme, value
  1027. FROM {db_prefix}themes
  1028. WHERE variable = {string:theme_variant}',
  1029. array(
  1030. 'theme_variant' => 'theme_variant',
  1031. )
  1032. );
  1033. while ($row = $smcFunc['db_fetch_assoc']($request))
  1034. $variant_preferences[$row['id_theme']] = $row['value'];
  1035. $smcFunc['db_free_result']($request);
  1036. }
  1037. // Save the setting first.
  1038. $current_images_url = $settings['images_url'];
  1039. $current_theme_variants = !empty($settings['theme_variants']) ? $settings['theme_variants'] : array();
  1040. foreach ($context['available_themes'] as $id_theme => $theme_data)
  1041. {
  1042. // Don't try to load the forum or board default theme's data... it doesn't have any!
  1043. if ($id_theme == 0)
  1044. continue;
  1045. // The thumbnail needs the correct path.
  1046. $settings['images_url'] = &$theme_data['images_url'];
  1047. if (file_exists($theme_data['theme_dir'] . '/languages/Settings.' . $user_info['language'] . '.php'))
  1048. include($theme_data['theme_dir'] . '/languages/Settings.' . $user_info['language'] . '.php');
  1049. elseif (file_exists($theme_data['theme_dir'] . '/languages/Settings.' . $language . '.php'))
  1050. include($theme_data['theme_dir'] . '/languages/Settings.' . $language . '.php');
  1051. else
  1052. {
  1053. $txt['theme_thumbnail_href'] = $theme_data['images_url'] . '/thumbnail.gif';
  1054. $txt['theme_description'] = '';
  1055. }
  1056. $context['available_themes'][$id_theme]['thumbnail_href'] = $txt['theme_thumbnail_href'];
  1057. $context['available_themes'][$id_theme]['description'] = $txt['theme_description'];
  1058. // Are there any variants?
  1059. if (file_exists($theme_data['theme_dir'] . '/index.template.php') && empty($theme_data['disable_user_variant']))
  1060. {
  1061. $file_contents = implode('', file($theme_data['theme_dir'] . '/index.template.php'));
  1062. if (preg_match('~\$settings\[\'theme_variants\'\]\s*=(.+?);~', $file_contents, $matches))
  1063. {
  1064. $settings['theme_variants'] = array();
  1065. // Fill settings up.
  1066. eval('global $settings;' . $matches[0]);
  1067. if (!empty($settings['theme_variants']))
  1068. {
  1069. loadLanguage('Settings');
  1070. $context['available_themes'][$id_theme]['variants'] = array();
  1071. foreach ($settings['theme_variants'] as $variant)
  1072. $context['available_themes'][$id_theme]['variants'][$variant] = array(
  1073. 'label' => isset($txt['variant_' . $variant]) ? $txt['variant_' . $variant] : $variant,
  1074. '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'),
  1075. );
  1076. $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]));
  1077. if (!isset($context['available_themes'][$id_theme]['variants'][$context['available_themes'][$id_theme]['selected_variant']]['thumbnail']))
  1078. $context['available_themes'][$id_theme]['selected_variant'] = $settings['theme_variants'][0];
  1079. $context['available_themes'][$id_theme]['thumbnail_href'] = $context['available_themes'][$id_theme]['variants'][$context['available_themes'][$id_theme]['selected_variant']]['thumbnail'];
  1080. // Allow themes to override the text.
  1081. $context['available_themes'][$id_theme]['pick_label'] = isset($txt['variant_pick']) ? $txt['variant_pick'] : $txt['theme_pick_variant'];
  1082. }
  1083. }
  1084. }
  1085. }
  1086. // Then return it.
  1087. $settings['images_url'] = $current_images_url;
  1088. $settings['theme_variants'] = $current_theme_variants;
  1089. // As long as we're not doing the default theme...
  1090. if (!isset($_REQUEST['u']) || $_REQUEST['u'] >= 0)
  1091. {
  1092. if ($guest_theme != 0)
  1093. $context['available_themes'][0] = $context['available_themes'][$guest_theme];
  1094. $context['available_themes'][0]['id'] = 0;
  1095. $context['available_themes'][0]['name'] = $txt['theme_forum_default'];
  1096. $context['available_themes'][0]['selected'] = $context['current_theme'] == 0;
  1097. $context['available_themes'][0]['description'] = $txt['theme_global_description'];
  1098. }
  1099. ksort($context['available_themes']);
  1100. $context['page_title'] = $txt['theme_pick'];
  1101. $context['sub_template'] = 'pick';
  1102. }
  1103. function ThemeInstall()
  1104. {
  1105. global $sourcedir, $boarddir, $boardurl, $txt, $context, $settings, $modSettings, $smcFunc;
  1106. checkSession('request');
  1107. isAllowedTo('admin_forum');
  1108. checkSession('request');
  1109. require_once($sourcedir . '/Subs-Package.php');
  1110. loadTemplate('Themes');
  1111. if (isset($_GET['theme_id']))
  1112. {
  1113. $result = $smcFunc['db_query']('', '
  1114. SELECT value
  1115. FROM {db_prefix}themes
  1116. WHERE id_theme = {int:current_theme}
  1117. AND id_member = {int:no_member}
  1118. AND variable = {string:name}
  1119. LIMIT 1',
  1120. array(
  1121. 'current_theme' => (int) $_GET['theme_id'],
  1122. 'no_member' => 0,
  1123. 'name' => 'name',
  1124. )
  1125. );
  1126. list ($theme_name) = $smcFunc['db_fetch_row']($result);
  1127. $smcFunc['db_free_result']($result);
  1128. $context['sub_template'] = 'installed';
  1129. $context['page_title'] = $txt['theme_installed'];
  1130. $context['installed_theme'] = array(
  1131. 'id' => (int) $_GET['theme_id'],
  1132. 'name' => $theme_name,
  1133. );
  1134. return;
  1135. }
  1136. if ((!empty($_FILES['theme_gz']) && (!isset($_FILES['theme_gz']['error']) || $_FILES['theme_gz']['error'] != 4)) || !empty($_REQUEST['theme_gz']))
  1137. $method = 'upload';
  1138. elseif (isset($_REQUEST['theme_dir']) && rtrim(realpath($_REQUEST['theme_dir']), '/\\') != realpath($boarddir . '/Themes') && file_exists($_REQUEST['theme_dir']))
  1139. $method = 'path';
  1140. else
  1141. $method = 'copy';
  1142. if (!empty($_REQUEST['copy']) && $method == 'copy')
  1143. {
  1144. // Hopefully the themes directory is writable, or we might have a problem.
  1145. if (!is_writable($boarddir . '/Themes'))
  1146. fatal_lang_error('theme_install_write_error', 'critical');
  1147. $theme_dir = $boarddir . '/Themes/' . preg_replace('~[^A-Za-z0-9_\- ]~', '', $_REQUEST['copy']);
  1148. umask(0);
  1149. mkdir($theme_dir, 0777);
  1150. @set_time_limit(600);
  1151. if (function_exists('apache_reset_timeout'))
  1152. @apache_reset_timeout();
  1153. // Create subdirectories for css and javascript files.
  1154. mkdir($theme_dir . '/css', 0777);
  1155. mkdir($theme_dir . '/scripts', 0777);
  1156. // Copy over the default non-theme files.
  1157. $to_copy = array('/index.php', '/index.template.php', '/css/index.css', '/css/rtl.css', '/scripts/theme.js');
  1158. foreach ($to_copy as $file)
  1159. {
  1160. copy($settings['default_theme_dir'] . $file, $theme_dir . $file);
  1161. @chmod($theme_dir . $file, 0777);
  1162. }
  1163. // And now the entire images directory!
  1164. copytree($settings['default_theme_dir'] . '/images', $theme_dir . '/images');
  1165. package_flush_cache();
  1166. $theme_name = $_REQUEST['copy'];
  1167. $images_url = $boardurl . '/Themes/' . basename($theme_dir) . '/images';
  1168. $theme_dir = realpath($theme_dir);
  1169. // Lets get some data for the new theme.
  1170. $request = $smcFunc['db_query']('', '
  1171. SELECT variable, value
  1172. FROM {db_prefix}themes
  1173. WHERE variable IN ({string:theme_templates}, {string:theme_layers})
  1174. AND id_member = {int:no_member}
  1175. AND id_theme = {int:default_theme}',
  1176. array(
  1177. 'no_member' => 0,
  1178. 'default_theme' => 1,
  1179. 'theme_templates' => 'theme_templates',
  1180. 'theme_layers' => 'theme_layers',
  1181. )
  1182. );
  1183. while ($row = $smcFunc['db_fetch_assoc']($request))
  1184. {
  1185. if ($row['variable'] == 'theme_templates')
  1186. $theme_templates = $row['value'];
  1187. elseif ($row['variable'] == 'theme_layers')
  1188. $theme_layers = $row['value'];
  1189. else
  1190. continue;
  1191. }
  1192. $smcFunc['db_free_result']($request);
  1193. // Lets add a theme_info.xml to this theme.
  1194. $xml_info = '<' . '?xml version="1.0"?' . '>
  1195. <theme-info xmlns="http://www.simplemachines.org/xml/theme-info" xmlns:smf="http://www.simplemachines.org/">
  1196. <!-- For the id, always use something unique - put your name, a colon, and then the package name. -->
  1197. <id>smf:' . $smcFunc['strtolower'](str_replace(array(' '), '_', $_REQUEST['copy'])) . '</id>
  1198. <version>' . $modSettings['smfVersion'] . '</version>
  1199. <!-- Theme name, used purely for aesthetics. -->
  1200. <name>' . $_REQUEST['copy'] . '</name>
  1201. <!-- Author: your email address or contact information. The name attribute is optional. -->
  1202. <author name="Simple Machines">info@simplemachines.org</author>
  1203. <!-- Website... where to get updates and more information. -->
  1204. <website>http://www.simplemachines.org/</website>
  1205. <!-- Template layers to use, defaults to "html,body". -->
  1206. <layers>' . (empty($theme_layers) ? 'html,body' : $theme_layers) . '</layers>
  1207. <!-- Templates to load on startup. Default is "index". -->
  1208. <templates>' . (empty($theme_templates) ? 'index' : $theme_templates) . '</templates>
  1209. <!-- Base this theme off another? Default is blank, or no. It could be "default". -->
  1210. <based-on></based-on>
  1211. </theme-info>';
  1212. // Now write it.
  1213. $fp = @fopen($theme_dir . '/theme_info.xml', 'w+');
  1214. if ($fp)
  1215. {
  1216. fwrite($fp, $xml_info);
  1217. fclose($fp);
  1218. }
  1219. }
  1220. elseif (isset($_REQUEST['theme_dir']) && $method == 'path')
  1221. {
  1222. if (!is_dir($_REQUEST['theme_dir']) || !file_exists($_REQUEST['theme_dir'] . '/theme_info.xml'))
  1223. fatal_lang_error('theme_install_error', false);
  1224. $theme_name = basename($_REQUEST['theme_dir']);
  1225. $theme_dir = $_REQUEST['theme_dir'];
  1226. }
  1227. elseif ($method = 'upload')
  1228. {
  1229. // Hopefully the themes directory is writable, or we might have a problem.
  1230. if (!is_writable($boarddir . '/Themes'))
  1231. fatal_lang_error('theme_install_write_error', 'critical');
  1232. require_once($sourcedir . '/Subs-Package.php');
  1233. // Set the default settings...
  1234. $theme_name = strtok(basename(isset($_FILES['theme_gz']) ? $_FILES['theme_gz']['name'] : $_REQUEST['theme_gz']), '.');
  1235. $theme_name = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $theme_name);
  1236. $theme_dir = $boarddir . '/Themes/' . $theme_name;
  1237. if (isset($_FILES['theme_gz']) && is_uploaded_file($_FILES['theme_gz']['tmp_name']) && (@ini_get('open_basedir') != '' || file_exists($_FILES['theme_gz']['tmp_name'])))
  1238. $extracted = read_tgz_file($_FILES['theme_gz']['tmp_name'], $boarddir . '/Themes/' . $theme_name, false, true);
  1239. elseif (isset($_REQUEST['theme_gz']))
  1240. {
  1241. // Check that the theme is from simplemachines.org, for now... maybe add mirroring later.
  1242. if (preg_match('~^http://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['theme_gz']) == 0 || strpos($_REQUEST['theme_gz'], 'dlattach') !== false)
  1243. fatal_lang_error('not_on_simplemachines');
  1244. $extracted = read_tgz_file($_REQUEST['theme_gz'], $boarddir . '/Themes/' . $theme_name, false, true);
  1245. }
  1246. else
  1247. redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
  1248. }
  1249. // Something go wrong?
  1250. if ($theme_dir != '' && basename($theme_dir) != 'Themes')
  1251. {
  1252. // Defaults.
  1253. $install_info = array(
  1254. 'theme_url' => $boardurl . '/Themes/' . basename($theme_dir),
  1255. 'images_url' => isset($images_url) ? $images_url : $boardurl . '/Themes/' . basename($theme_dir) . '/images',
  1256. 'theme_dir' => $theme_dir,
  1257. 'name' => $theme_name
  1258. );
  1259. if (file_exists($theme_dir . '/theme_info.xml'))
  1260. {
  1261. $theme_info = file_get_contents($theme_dir . '/theme_info.xml');
  1262. $xml_elements = array(
  1263. 'name' => 'name',
  1264. 'theme_layers' => 'layers',
  1265. 'theme_templates' => 'templates',
  1266. 'based_on' => 'based-on',
  1267. );
  1268. foreach ($xml_elements as $var => $name)
  1269. {
  1270. if (preg_match('~<' . $name . '>(?:<!\[CDATA\[)?(.+?)(?:\]\]>)?</' . $name . '>~', $theme_info, $match) == 1)
  1271. $install_info[$var] = $match[1];
  1272. }
  1273. if (preg_match('~<images>(?:<!\[CDATA\[)?(.+?)(?:\]\]>)?</images>~', $theme_info, $

Large files files are truncated, but you can click here to view the full file