PageRenderTime 55ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/Themes.php

https://github.com/smf-portal/SMF2.1
PHP | 2205 lines | 1680 code | 290 blank | 235 comment | 301 complexity | a85326323e5855469529713800af28aa MD5 | raw file

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

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

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