PageRenderTime 65ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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, $match) == 1)
  1274. {
  1275. $install_info['images_url'] = $install_info['theme_url'] . '/' . $match[1];
  1276. $explicit_images = true;
  1277. }
  1278. if (preg_match('~<extra>(?:<!\[CDATA\[)?(.+?)(?:\]\]>)?</extra>~', $theme_info, $match) == 1)
  1279. $install_info += unserialize($match[1]);
  1280. }
  1281. if (isset($install_info['based_on']))
  1282. {
  1283. if ($install_info['based_on'] == 'default')
  1284. {
  1285. $install_info['theme_url'] = $settings['default_theme_url'];
  1286. $install_info['images_url'] = $settings['default_images_url'];
  1287. }
  1288. elseif ($install_info['based_on'] != '')
  1289. {
  1290. $install_info['based_on'] = preg_replace('~[^A-Za-z0-9\-_ ]~', '', $install_info['based_on']);
  1291. $request = $smcFunc['db_query']('', '
  1292. SELECT th.value AS base_theme_dir, th2.value AS base_theme_url' . (!empty($explicit_images) ? '' : ', th3.value AS images_url') . '
  1293. FROM {db_prefix}themes AS th
  1294. INNER JOIN {db_prefix}themes AS th2 ON (th2.id_theme = th.id_theme
  1295. AND th2.id_member = {int:no_member}
  1296. AND th2.variable = {string:theme_url})' . (!empty($explicit_images) ? '' : '
  1297. INNER JOIN {db_prefix}themes AS th3 ON (th3.id_theme = th.id_theme
  1298. AND th3.id_member = {int:no_member}
  1299. AND th3.variable = {string:images_url})') . '
  1300. WHERE th.id_member = {int:no_member}
  1301. AND (th.value LIKE {string:based_on} OR th.value LIKE {string:based_on_path})
  1302. AND th.variable = {string:theme_dir}
  1303. LIMIT 1',
  1304. array(
  1305. 'no_member' => 0,
  1306. 'theme_url' => 'theme_url',
  1307. 'images_url' => 'images_url',
  1308. 'theme_dir' => 'theme_dir',
  1309. 'based_on' => '%/' . $install_info['based_on'],
  1310. 'based_on_path' => '%' . "\\" . $install_info['based_on'],
  1311. )
  1312. );
  1313. $temp = $smcFunc['db_fetch_assoc']($request);
  1314. $smcFunc['db_free_result']($request);
  1315. // !!! An error otherwise?
  1316. if (is_array($temp))
  1317. {
  1318. $install_info = $temp + $install_info;
  1319. if (empty($explicit_images) && !empty($install_info['base_theme_url']))
  1320. $install_info['theme_url'] = $install_info['base_theme_url'];
  1321. }
  1322. }
  1323. unset($install_info['based_on']);
  1324. }
  1325. // Find the newest id_theme.
  1326. $result = $smcFunc['db_query']('', '
  1327. SELECT MAX(id_theme)
  1328. FROM {db_prefix}themes',
  1329. array(
  1330. )
  1331. );
  1332. list ($id_theme) = $smcFunc['db_fetch_row']($result);
  1333. $smcFunc['db_free_result']($result);
  1334. // This will be theme number...
  1335. $id_theme++;
  1336. $inserts = array();
  1337. foreach ($install_info as $var => $val)
  1338. $inserts[] = array($id_theme, $var, $val);
  1339. if (!empty($inserts))
  1340. $smcFunc['db_insert']('insert',
  1341. '{db_prefix}themes',
  1342. array('id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  1343. $inserts,
  1344. array('id_theme', 'variable')
  1345. );
  1346. updateSettings(array('knownThemes' => strtr($modSettings['knownThemes'] . ',' . $id_theme, array(',,' => ','))));
  1347. }
  1348. redirectexit('action=admin;area=theme;sa=install;theme_id=' . $id_theme . ';' . $context['session_var'] . '=' . $context['session_id']);
  1349. }
  1350. // Possibly the simplest and best example of how to ues the template system.
  1351. function WrapAction()
  1352. {
  1353. global $context, $settings, $sourcedir;
  1354. // Load any necessary template(s)?
  1355. if (isset($settings['catch_action']['template']))
  1356. {
  1357. // Load both the template and language file. (but don't fret if the language file isn't there...)
  1358. loadTemplate($settings['catch_action']['template']);
  1359. loadLanguage($settings['catch_action']['template'], '', false);
  1360. }
  1361. // Any special layers?
  1362. if (isset($settings['catch_action']['layers']))
  1363. $context['template_layers'] = $settings['catch_action']['layers'];
  1364. // Just call a function?
  1365. if (isset($settings['catch_action']['function']))
  1366. {
  1367. if (isset($settings['catch_action']['filename']))
  1368. template_include($sourcedir . '/' . $settings['catch_action']['filename'], true);
  1369. $settings['catch_action']['function']();
  1370. }
  1371. // And finally, the main sub template ;).
  1372. elseif (isset($settings['catch_action']['sub_template']))
  1373. $context['sub_template'] = $settings['catch_action']['sub_template'];
  1374. }
  1375. // Set an option via javascript.
  1376. function SetJavaScript()
  1377. {
  1378. global $settings, $user_info, $smcFunc, $options;
  1379. // Check the session id.
  1380. checkSession('get');
  1381. // This good-for-nothing pixel is being used to keep the session alive.
  1382. if (empty($_GET['var']) || !isset($_GET['val']))
  1383. redirectexit($settings['images_url'] . '/blank.gif');
  1384. // Sorry, guests can't go any further than this..
  1385. if ($user_info['is_guest'] || $user_info['id'] == 0)
  1386. obExit(false);
  1387. $reservedVars = array(
  1388. 'actual_theme_url',
  1389. 'actual_images_url',
  1390. 'base_theme_dir',
  1391. 'base_theme_url',
  1392. 'default_images_url',
  1393. 'default_theme_dir',
  1394. 'default_theme_url',
  1395. 'default_template',
  1396. 'images_url',
  1397. 'number_recent_posts',
  1398. 'smiley_sets_default',
  1399. 'theme_dir',
  1400. 'theme_id',
  1401. 'theme_layers',
  1402. 'theme_templates',
  1403. 'theme_url',
  1404. 'name',
  1405. );
  1406. // Can't change reserved vars.
  1407. if (in_array(strtolower($_GET['var']), $reservedVars))
  1408. redirectexit($settings['images_url'] . '/blank.gif');
  1409. // Use a specific theme?
  1410. if (isset($_GET['th']) || isset($_GET['id']))
  1411. {
  1412. // Invalidate the current themes cache too.
  1413. cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 60);
  1414. $settings['theme_id'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
  1415. }
  1416. // If this is the admin preferences the passed value will just be an element of it.
  1417. if ($_GET['var'] == 'admin_preferences')
  1418. {
  1419. $options['admin_preferences'] = !empty($options['admin_preferences']) ? unserialize($options['admin_preferences']) : array();
  1420. // New thingy...
  1421. if (isset($_GET['admin_key']) && strlen($_GET['admin_key']) < 5)
  1422. $options['admin_preferences'][$_GET['admin_key']] = $_GET['val'];
  1423. // Change the value to be something nice,
  1424. $_GET['val'] = serialize($options['admin_preferences']);
  1425. }
  1426. // Update the option.
  1427. $smcFunc['db_insert']('replace',
  1428. '{db_prefix}themes',
  1429. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  1430. array($settings['theme_id'], $user_info['id'], $_GET['var'], is_array($_GET['val']) ? implode(',', $_GET['val']) : $_GET['val']),
  1431. array('id_theme', 'id_member', 'variable')
  1432. );
  1433. cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 60);
  1434. // Don't output anything...
  1435. redirectexit($settings['images_url'] . '/blank.gif');
  1436. }
  1437. function EditTheme()
  1438. {
  1439. global $context, $settings, $scripturl, $boarddir, $smcFunc;
  1440. if (isset($_REQUEST['preview']))
  1441. {
  1442. // !!! Should this be removed?
  1443. die;
  1444. }
  1445. isAllowedTo('admin_forum');
  1446. loadTemplate('Themes');
  1447. $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) @$_GET['id'];
  1448. if (empty($_GET['th']))
  1449. {
  1450. $request = $smcFunc['db_query']('', '
  1451. SELECT id_theme, variable, value
  1452. FROM {db_prefix}themes
  1453. WHERE variable IN ({string:name}, {string:theme_dir}, {string:theme_templates}, {string:theme_layers})
  1454. AND id_member = {int:no_member}',
  1455. array(
  1456. 'name' => 'name',
  1457. 'theme_dir' => 'theme_dir',
  1458. 'theme_templates' => 'theme_templates',
  1459. 'theme_layers' => 'theme_layers',
  1460. 'no_member' => 0,
  1461. )
  1462. );
  1463. $context['themes'] = array();
  1464. while ($row = $smcFunc['db_fetch_assoc']($request))
  1465. {
  1466. if (!isset($context['themes'][$row['id_theme']]))
  1467. $context['themes'][$row['id_theme']] = array(
  1468. 'id' => $row['id_theme'],
  1469. 'num_default_options' => 0,
  1470. 'num_members' => 0,
  1471. );
  1472. $context['themes'][$row['id_theme']][$row['variable']] = $row['value'];
  1473. }
  1474. $smcFunc['db_free_result']($request);
  1475. foreach ($context['themes'] as $key => $theme)
  1476. {
  1477. // There has to be a Settings template!
  1478. if (!file_exists($theme['theme_dir'] . '/index.template.php') && !file_exists($theme['theme_dir'] . '/css/index.css'))
  1479. unset($context['themes'][$key]);
  1480. else
  1481. {
  1482. if (!isset($theme['theme_templates']))
  1483. $templates = array('index');
  1484. else
  1485. $templates = explode(',', $theme['theme_templates']);
  1486. foreach ($templates as $template)
  1487. if (file_exists($theme['theme_dir'] . '/' . $template . '.template.php'))
  1488. {
  1489. // Fetch the header... a good 256 bytes should be more than enough.
  1490. $fp = fopen($theme['theme_dir'] . '/' . $template . '.template.php', 'rb');
  1491. $header = fread($fp, 256);
  1492. fclose($fp);
  1493. // Can we find a version comment, at all?
  1494. if (preg_match('~(?://|/\*)\s*Version:\s+(.+?);\s*' . $template . '(?:[\s]{2}|\*/)~i', $header, $match) == 1)
  1495. {
  1496. $ver = $match[1];
  1497. if (!isset($context['themes'][$key]['version']) || $context['themes'][$key]['version'] > $ver)
  1498. $context['themes'][$key]['version'] = $ver;
  1499. }
  1500. }
  1501. $context['themes'][$key]['can_edit_style'] = file_exists($theme['theme_dir'] . '/css/index.css');
  1502. }
  1503. }
  1504. $context['sub_template'] = 'edit_list';
  1505. return 'no_themes';
  1506. }
  1507. $context['session_error'] = false;
  1508. // Get the directory of the theme we are editing.
  1509. $request = $smcFunc['db_query']('', '
  1510. SELECT value, id_theme
  1511. FROM {db_prefix}themes
  1512. WHERE variable = {string:theme_dir}
  1513. AND id_theme = {int:current_theme}
  1514. LIMIT 1',
  1515. array(
  1516. 'current_theme' => $_GET['th'],
  1517. 'theme_dir' => 'theme_dir',
  1518. )
  1519. );
  1520. list ($theme_dir, $context['theme_id']) = $smcFunc['db_fetch_row']($request);
  1521. $smcFunc['db_free_result']($request);
  1522. if (!isset($_REQUEST['filename']))
  1523. {
  1524. if (isset($_GET['directory']))
  1525. {
  1526. if (substr($_GET['directory'], 0, 1) == '.')
  1527. $_GET['directory'] = '';
  1528. else
  1529. {
  1530. $_GET['directory'] = preg_replace(array('~^[\./\\:\0\n\r]+~', '~[\\\\]~', '~/[\./]+~'), array('', '/', '/'), $_GET['directory']);
  1531. $temp = realpath($theme_dir . '/' . $_GET['directory']);
  1532. if (empty($temp) || substr($temp, 0, strlen(realpath($theme_dir))) != realpath($theme_dir))
  1533. $_GET['directory'] = '';
  1534. }
  1535. }
  1536. if (isset($_GET['directory']) && $_GET['directory'] != '')
  1537. {
  1538. $context['theme_files'] = get_file_listing($theme_dir . '/' . $_GET['directory'], $_GET['directory'] . '/');
  1539. $temp = dirname($_GET['directory']);
  1540. array_unshift($context['theme_files'], array(
  1541. 'filename' => $temp == '.' || $temp == '' ? '/ (..)' : $temp . ' (..)',
  1542. 'is_writable' => is_writable($theme_dir . '/' . $temp),
  1543. 'is_directory' => true,
  1544. 'is_template' => false,
  1545. 'is_image' => false,
  1546. 'is_editable' => false,
  1547. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . $temp,
  1548. 'size' => '',
  1549. ));
  1550. }
  1551. else
  1552. $context['theme_files'] = get_file_listing($theme_dir, '');
  1553. $context['sub_template'] = 'edit_browse';
  1554. return;
  1555. }
  1556. else
  1557. {
  1558. if (substr($_REQUEST['filename'], 0, 1) == '.')
  1559. $_REQUEST['filename'] = '';
  1560. else
  1561. {
  1562. $_REQUEST['filename'] = preg_replace(array('~^[\./\\:\0\n\r]+~', '~[\\\\]~', '~/[\./]+~'), array('', '/', '/'), $_REQUEST['filename']);
  1563. $temp = realpath($theme_dir . '/' . $_REQUEST['filename']);
  1564. if (empty($temp) || substr($temp, 0, strlen(realpath($theme_dir))) != realpath($theme_dir))
  1565. $_REQUEST['filename'] = '';
  1566. }
  1567. if (empty($_REQUEST['filename']))
  1568. fatal_lang_error('theme_edit_missing', false);
  1569. }
  1570. if (isset($_POST['submit']))
  1571. {
  1572. if (checkSession('post', '', false) == '')
  1573. {
  1574. if (is_array($_POST['entire_file']))
  1575. $_POST['entire_file'] = implode("\n", $_POST['entire_file']);
  1576. $_POST['entire_file'] = rtrim(strtr($_POST['entire_file'], array("\r" => '', ' ' => "\t")));
  1577. // Check for a parse error!
  1578. if (substr($_REQUEST['filename'], -13) == '.template.php' && is_writable($theme_dir) && @ini_get('display_errors'))
  1579. {
  1580. $request = $smcFunc['db_query']('', '
  1581. SELECT value
  1582. FROM {db_prefix}themes
  1583. WHERE variable = {string:theme_url}
  1584. AND id_theme = {int:current_theme}
  1585. LIMIT 1',
  1586. array(
  1587. 'current_theme' => $_GET['th'],
  1588. 'theme_url' => 'theme_url',
  1589. )
  1590. );
  1591. list ($theme_url) = $smcFunc['db_fetch_row']($request);
  1592. $smcFunc['db_free_result']($request);
  1593. $fp = fopen($theme_dir . '/tmp_' . session_id() . '.php', 'w');
  1594. fwrite($fp, $_POST['entire_file']);
  1595. fclose($fp);
  1596. // !!! Use fetch_web_data()?
  1597. $error = @file_get_contents($theme_url . '/tmp_' . session_id() . '.php');
  1598. if (preg_match('~ <b>(\d+)</b><br( /)?' . '>$~i', $error) != 0)
  1599. $error_file = $theme_dir . '/tmp_' . session_id() . '.php';
  1600. else
  1601. unlink($theme_dir . '/tmp_' . session_id() . '.php');
  1602. }
  1603. if (!isset($error_file))
  1604. {
  1605. $fp = fopen($theme_dir . '/' . $_REQUEST['filename'], 'w');
  1606. fwrite($fp, $_POST['entire_file']);
  1607. fclose($fp);
  1608. redirectexit('action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . dirname($_REQUEST['filename']));
  1609. }
  1610. }
  1611. // Session timed out.
  1612. else
  1613. {
  1614. loadLanguage('Errors');
  1615. $context['session_error'] = true;
  1616. $context['sub_template'] = 'edit_file';
  1617. // Recycle the submitted data.
  1618. $context['entire_file'] = htmlspecialchars($_POST['entire_file']);
  1619. // You were able to submit it, so it's reasonable to assume you are allowed to save.
  1620. $context['allow_save'] = true;
  1621. return;
  1622. }
  1623. }
  1624. $context['allow_save'] = is_writable($theme_dir . '/' . $_REQUEST['filename']);
  1625. $context['allow_save_filename'] = strtr($theme_dir . '/' . $_REQUEST['filename'], array($boarddir => '...'));
  1626. $context['edit_filename'] = htmlspecialchars($_REQUEST['filename']);
  1627. if (substr($_REQUEST['filename'], -4) == '.css')
  1628. {
  1629. $context['sub_template'] = 'edit_style';
  1630. $context['entire_file'] = htmlspecialchars(strtr(file_get_contents($theme_dir . '/' . $_REQUEST['filename']), array("\t" => ' ')));
  1631. }
  1632. elseif (substr($_REQUEST['filename'], -13) == '.template.php')
  1633. {
  1634. $context['sub_template'] = 'edit_template';
  1635. if (!isset($error_file))
  1636. $file_data = file($theme_dir . '/' . $_REQUEST['filename']);
  1637. else
  1638. {
  1639. if (preg_match('~(<b>.+?</b>:.+?<b>).+?(</b>.+?<b>\d+</b>)<br( /)?' . '>$~i', $error, $match) != 0)
  1640. $context['parse_error'] = $match[1] . $_REQUEST['filename'] . $match[2];
  1641. $file_data = file($error_file);
  1642. unlink($error_file);
  1643. }
  1644. $j = 0;
  1645. $context['file_parts'] = array(array('lines' => 0, 'line' => 1, 'data' => ''));
  1646. for ($i = 0, $n = count($file_data); $i < $n; $i++)
  1647. {
  1648. if (isset($file_data[$i + 1]) && substr($file_data[$i + 1], 0, 9) == 'function ')
  1649. {
  1650. // Try to format the functions a little nicer...
  1651. $context['file_parts'][$j]['data'] = trim($context['file_parts'][$j]['data']) . "\n";
  1652. if (empty($context['file_parts'][$j]['lines']))
  1653. unset($context['file_parts'][$j]);
  1654. $context['file_parts'][++$j] = array('lines' => 0, 'line' => $i + 1, 'data' => '');
  1655. }
  1656. $context['file_parts'][$j]['lines']++;
  1657. $context['file_parts'][$j]['data'] .= htmlspecialchars(strtr($file_data[$i], array("\t" => ' ')));
  1658. }
  1659. $context['entire_file'] = htmlspecialchars(strtr(implode('', $file_data), array("\t" => ' ')));
  1660. }
  1661. else
  1662. {
  1663. $context['sub_template'] = 'edit_file';
  1664. $context['entire_file'] = htmlspecialchars(strtr(file_get_contents($theme_dir . '/' . $_REQUEST['filename']), array("\t" => ' ')));
  1665. }
  1666. }
  1667. function get_file_listing($path, $relative)
  1668. {
  1669. global $scripturl, $txt, $context;
  1670. // Is it even a directory?
  1671. if (!is_dir($path))
  1672. fatal_lang_error('error_invalid_dir', 'critical');
  1673. $dir = dir($path);
  1674. $entries = array();
  1675. while ($entry = $dir->read())
  1676. $entries[] = $entry;
  1677. $dir->close();
  1678. natcasesort($entries);
  1679. $listing1 = array();
  1680. $listing2 = array();
  1681. foreach ($entries as $entry)
  1682. {
  1683. // Skip all dot files, including .htaccess.
  1684. if (substr($entry, 0, 1) == '.' || $entry == 'CVS')
  1685. continue;
  1686. if (is_dir($path . '/' . $entry))
  1687. $listing1[] = array(
  1688. 'filename' => $entry,
  1689. 'is_writable' => is_writable($path . '/' . $entry),
  1690. 'is_directory' => true,
  1691. 'is_template' => false,
  1692. 'is_image' => false,
  1693. 'is_editable' => false,
  1694. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . $relative . $entry,
  1695. 'size' => '',
  1696. );
  1697. else
  1698. {
  1699. $size = filesize($path . '/' . $entry);
  1700. if ($size > 2048 || $size == 1024)
  1701. $size = comma_format($size / 1024) . ' ' . $txt['themeadmin_edit_kilobytes'];
  1702. else
  1703. $size = comma_format($size) . ' ' . $txt['themeadmin_edit_bytes'];
  1704. $listing2[] = array(
  1705. 'filename' => $entry,
  1706. 'is_writable' => is_writable($path . '/' . $entry),
  1707. 'is_directory' => false,
  1708. 'is_template' => preg_match('~\.template\.php$~', $entry) != 0,
  1709. 'is_image' => preg_match('~\.(jpg|jpeg|gif|bmp|png)$~', $entry) != 0,
  1710. 'is_editable' => is_writable($path . '/' . $entry) && preg_match('~\.(php|pl|css|js|vbs|xml|xslt|txt|xsl|html|htm|shtm|shtml|asp|aspx|cgi|py)$~', $entry) != 0,
  1711. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;filename=' . $relative . $entry,
  1712. 'size' => $size,
  1713. 'last_modified' => timeformat(filemtime($path . '/' . $entry)),
  1714. );
  1715. }
  1716. }
  1717. return array_merge($listing1, $listing2);
  1718. }
  1719. function CopyTemplate()
  1720. {
  1721. global $context, $settings, $smcFunc;
  1722. isAllowedTo('admin_forum');
  1723. loadTemplate('Themes');
  1724. $context[$context['admin_menu_name']]['current_subsection'] = 'edit';
  1725. $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
  1726. $request = $smcFunc['db_query']('', '
  1727. SELECT th1.value, th1.id_theme, th2.value
  1728. FROM {db_prefix}themes AS th1
  1729. LEFT JOIN {db_prefix}themes AS th2 ON (th2.variable = {string:base_theme_dir} AND th2.id_theme = {int:current_theme})
  1730. WHERE th1.variable = {string:theme_dir}
  1731. AND th1.id_theme = {int:current_theme}
  1732. LIMIT 1',
  1733. array(
  1734. 'current_theme' => $_GET['th'],
  1735. 'base_theme_dir' => 'base_theme_dir',
  1736. 'theme_dir' => 'theme_dir',
  1737. )
  1738. );
  1739. list ($theme_dir, $context['theme_id'], $base_theme_dir) = $smcFunc['db_fetch_row']($request);
  1740. $smcFunc['db_free_result']($request);
  1741. if (isset($_REQUEST['template']) && preg_match('~[\./\\\\:\0]~', $_REQUEST['template']) == 0)
  1742. {
  1743. if (!empty($base_theme_dir) && file_exists($base_theme_dir . '/' . $_REQUEST['template'] . '.template.php'))
  1744. $filename = $base_theme_dir . '/' . $_REQUEST['template'] . '.template.php';
  1745. elseif (file_exists($settings['default_theme_dir'] . '/' . $_REQUEST['template'] . '.template.php'))
  1746. $filename = $settings['default_theme_dir'] . '/' . $_REQUEST['template'] . '.template.php';
  1747. else
  1748. fatal_lang_error('no_access', false);
  1749. $fp = fopen($theme_dir . '/' . $_REQUEST['template'] . '.template.php', 'w');
  1750. fwrite($fp, file_get_contents($filename));
  1751. fclose($fp);
  1752. redirectexit('action=admin;area=theme;th=' . $context['theme_id'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=copy');
  1753. }
  1754. elseif (isset($_REQUEST['lang_file']) && preg_match('~^[^\./\\\\:\0]\.[^\./\\\\:\0]$~', $_REQUEST['lang_file']) != 0)
  1755. {
  1756. if (!empty($base_theme_dir) && file_exists($base_theme_dir . '/languages/' . $_REQUEST['lang_file'] . '.php'))
  1757. $filename = $base_theme_dir . '/languages/' . $_REQUEST['template'] . '.php';
  1758. elseif (file_exists($settings['default_theme_dir'] . '/languages/' . $_REQUEST['template'] . '.php'))
  1759. $filename = $settings['default_theme_dir'] . '/languages/' . $_REQUEST['template'] . '.php';
  1760. else
  1761. fatal_lang_error('no_access', false);
  1762. $fp = fopen($theme_dir . '/languages/' . $_REQUEST['lang_file'] . '.php', 'w');
  1763. fwrite($fp, file_get_contents($filename));
  1764. fclose($fp);
  1765. redirectexit('action=admin;area=theme;th=' . $context['theme_id'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=copy');
  1766. }
  1767. $templates = array();
  1768. $lang_files = array();
  1769. $dir = dir($settings['default_theme_dir']);
  1770. while ($entry = $dir->read())
  1771. {
  1772. if (substr($entry, -13) == '.template.php')
  1773. $templates[] = substr($entry, 0, -13);
  1774. }
  1775. $dir->close();
  1776. $dir = dir($settings['default_theme_dir'] . '/languages');
  1777. while ($entry = $dir->read())
  1778. {
  1779. if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches))
  1780. $lang_files[] = $matches[1];
  1781. }
  1782. $dir->close();
  1783. if (!empty($base_theme_dir))
  1784. {
  1785. $dir = dir($base_theme_dir);
  1786. while ($entry = $dir->read())
  1787. {
  1788. if (substr($entry, -13) == '.template.php' && !in_array(substr($entry, 0, -13), $templates))
  1789. $templates[] = substr($entry, 0, -13);
  1790. }
  1791. $dir->close();
  1792. if (file_exists($base_theme_dir . '/languages'))
  1793. {
  1794. $dir = dir($base_theme_dir . '/languages');
  1795. while ($entry = $dir->read())
  1796. {
  1797. if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches) && !in_array($matches[1], $lang_files))
  1798. $lang_files[] = $matches[1];
  1799. }
  1800. $dir->close();
  1801. }
  1802. }
  1803. natcasesort($templates);
  1804. natcasesort($lang_files);
  1805. $context['available_templates'] = array();
  1806. foreach ($templates as $template)
  1807. $context['available_templates'][$template] = array(
  1808. 'filename' => $template . '.template.php',
  1809. 'value' => $template,
  1810. 'already_exists' => false,
  1811. 'can_copy' => is_writable($theme_dir),
  1812. );
  1813. $context['available_language_files'] = array();
  1814. foreach ($lang_files as $file)
  1815. $context['available_language_files'][$file] = array(
  1816. 'filename' => $file . '.php',
  1817. 'value' => $file,
  1818. 'already_exists' => false,
  1819. 'can_copy' => file_exists($theme_dir . '/languages') ? is_writable($theme_dir . '/languages') : is_writable($theme_dir),
  1820. );
  1821. $dir = dir($theme_dir);
  1822. while ($entry = $dir->read())
  1823. {
  1824. if (substr($entry, -13) == '.template.php' && isset($context['available_templates'][substr($entry, 0, -13)]))
  1825. {
  1826. $context['available_templates'][substr($entry, 0, -13)]['already_exists'] = true;
  1827. $context['available_templates'][substr($entry, 0, -13)]['can_copy'] = is_writable($theme_dir . '/' . $entry);
  1828. }
  1829. }
  1830. $dir->close();
  1831. if (file_exists($theme_dir . '/languages'))
  1832. {
  1833. $dir = dir($theme_dir . '/languages');
  1834. while ($entry = $dir->read())
  1835. {
  1836. if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches) && isset($context['available_language_files'][$matches[1]]))
  1837. {
  1838. $context['available_language_files'][$matches[1]]['already_exists'] = true;
  1839. $context['available_language_files'][$matches[1]]['can_copy'] = is_writable($theme_dir . '/languages/' . $entry);
  1840. }
  1841. }
  1842. $dir->close();
  1843. }
  1844. $context['sub_template'] = 'copy_template';
  1845. }
  1846. function convert_template($output_dir, $old_template = '')
  1847. {
  1848. global $boarddir;
  1849. if ($old_template == '')
  1850. {
  1851. // Step 1: Get the template.php file.
  1852. if (file_exists($boarddir . '/template.php'))
  1853. $old_template = file_get_contents($boarddir . '/template.php');
  1854. elseif (file_exists($boarddir . '/template.html'))
  1855. $old_template = file_get_contents($boarddir . '/template.html');
  1856. else
  1857. fatal_lang_error('theme_convert_error');
  1858. }
  1859. // Step 2: Change any single quotes to \'.
  1860. $old_template = strtr($old_template, array('\'' => '\\\''));
  1861. // Step 3: Parse out any existing PHP code.
  1862. $old_template = preg_replace('~\<\?php(.*)\?\>~es', 'phpcodefix(\'$1\')', $old_template);
  1863. // Step 4: Now we add the beginning and end...
  1864. $old_template = '<?php
  1865. // Version: 2.0 RC2; index
  1866. // Initialize the template... mainly little settings.
  1867. function template_init()
  1868. {
  1869. global $context, $settings, $options, $txt;
  1870. /* Use images from default theme when using templates from the default theme?
  1871. if this is always, images from the default theme will be used.
  1872. if this is defaults, images from the default theme will only be used with default templates.
  1873. if this is never, images from the default theme will not be used. */
  1874. $settings[\'use_default_images\'] = \'never\';
  1875. }
  1876. // The main sub template above the content.
  1877. function template_main_above()
  1878. {
  1879. global $context, $settings, $options, $scripturl, $txt;
  1880. // Show right to left and the character set for ease of translating.
  1881. echo ' . '\'' . $old_template . '\'' . ';
  1882. }
  1883. // Show a linktree. This is that thing that shows "My Community | General Category | General Discussion"..
  1884. function theme_linktree($force_show = false)
  1885. {
  1886. global $context, $settings, $options;
  1887. // If linktree is empty, just return - also allow an override.
  1888. if (empty($context[\'linktree\']) || (!empty($context[\'dont_default_linktree\']) && !$force_show))
  1889. return;
  1890. // Reverse the linktree in right to left mode.
  1891. if ($context[\'right_to_left\'])
  1892. $context[\'linktree\'] = array_reverse($context[\'linktree\'], true);
  1893. // Folder style or inline? Inline has a smaller font.
  1894. echo \'<span class="nav"\', $settings[\'linktree_inline\'] ? \' style="font-size: smaller;"\' : \'\', \'>\';
  1895. // Each tree item has a URL and name. Some may have extra_before and extra_after.
  1896. foreach ($context[\'linktree\'] as $link_num => $tree)
  1897. {
  1898. // Show the | | |-[] Folders.
  1899. if (empty($settings[\'linktree_inline\']))
  1900. {
  1901. if ($link_num > 0)
  1902. echo str_repeat(\'<img src="\' . $settings[\'images_url\'] . \'/icons/linktree_main.gif" alt="| " border="0" />\', $link_num - 1), \'<img src="\' . $settings[\'images_url\'] . \'/icons/linktree_side.gif" alt="|-" border="0" />\';
  1903. echo \'<img src="\' . $settings[\'images_url\'] . \'/icons/folder_open.gif" alt="+" border="0" />&nbsp; \';
  1904. }
  1905. if (isset($tree[\'extra_before\']))
  1906. echo $tree[\'extra_before\'];
  1907. echo \'<strong>\', $settings[\'linktree_link\'] && isset($tree[\'url\']) ? \'<a href="\' . $tree[\'url\'] . \'" class="nav">\' . $tree[\'name\'] . \'</a>\' : $tree[\'name\'], \'</strong>\';
  1908. if (isset($tree[\'extra_after\']))
  1909. echo $tree[\'extra_after\'];
  1910. // Don\'t show a separator for the last one.
  1911. if ($link_num != count($context[\'linktree\']) - 1)
  1912. echo $settings[\'linktree_inline\'] ? \' &nbsp;|&nbsp; \' : \'<br />\';
  1913. }
  1914. echo \'</span>\';
  1915. }
  1916. // Show the menu up top. Something like [home] [help] [profile] [logout]...
  1917. function template_menu()
  1918. {
  1919. global $context, $settings, $options, $scripturl, $txt;
  1920. foreach ($context[\'menu_buttons\'] as $act => $button)
  1921. {
  1922. $classes = array();
  1923. if (!empty($button[\'active_button\']))
  1924. $classes[] = \'active\';
  1925. if (!empty($button[\'is_last\']))
  1926. $classes[] = \'last\';
  1927. /* IE6 can\'t do multiple class selectors */
  1928. if ($context[\'browser\'][\'is_ie6\'] && !empty($button[\'active_button\']) && !empty($button[\'is_last\']))
  1929. $classes[] = \'lastactive\';
  1930. $classes = implode(\' \', $classes);
  1931. echo \'
  1932. <a id="button_\', $act, \'"\', !empty($classes) ? \' class="\' . $classes . \'"\' : \'\', \' title="\', !empty($button[\'alttitle\']) ? $button[\'alttitle\'] : $button[\'title\'], \'" href="\', $button[\'href\'], \'">\', ($settings[\'use_image_buttons\'] ? \'<img src="\' . $settings[\'images_url\'] . \'/\' . $context[\'user\'][\'language\'] . \'/home.gif" alt="\' . $txt[\'home\'] . \'" border="0" />\' : $txt[\'home\']), \'</a>\', (empty($button[\'is_last\']) ? $context[\'menu_separator\'] : \'\');
  1933. }
  1934. }
  1935. ?>';
  1936. // Step 5: Do the html tag.
  1937. $old_template = preg_replace('~\<html\>~i', '<html\', $context[\'right_to_left\'] ? \' dir="rtl"\' : \'\', \'>', $old_template);
  1938. // Step 6: The javascript stuff.
  1939. $old_template = preg_replace('~\<head\>~i', '<head>
  1940. <script type="text/javascript" src="\', $settings[\'default_theme_url\'], \'/scripts/script.js"></script>
  1941. <script type="text/javascript"><!-- // --><![CDATA[
  1942. var smf_theme_url = "\', $settings[\'theme_url\'], \'";
  1943. var smf_default_theme_url = "\', $settings[\'default_theme_url\'], \'";
  1944. var smf_images_url = "\', $settings[\'images_url\'], \'";
  1945. var smf_scripturl = "\', $scripturl, \'";
  1946. var smf_iso_case_folding = \', $context[\'server\'][\'iso_case_folding\'] ? \'true\' : \'false\', \';
  1947. var smf_charset = "\', $context[\'character_set\'], \'";\', $context[\'show_pm_popup\'] ? \'
  1948. if (confirm("\' . $txt[\'show_personal_messages\'] . \'"))
  1949. window.open(smf_prepareScriptUrl(smf_scripturl) + "action=pm");\' : \'\', \'
  1950. var ajax_notification_text = "\', $txt[\'ajax_in_progress\'], \'";
  1951. var ajax_notification_cancel_text = "\', $txt[\'modify_cancel\'], \'";
  1952. // ]]></script>
  1953. \' . $context[\'html_headers\'] . \'
  1954. // Please don\'t index these Mr Robot.
  1955. if (!empty($context[\'robot_no_index\']))
  1956. echo \'
  1957. <meta name="robots" content="noindex" />\';
  1958. // Present a canonical url for search engines to prevent duplicate content in their indices.
  1959. if (!empty($context[\'canonical_url\']))
  1960. echo \'
  1961. <link rel="canonical" href="\', $context[\'canonical_url\'], \'" />\';
  1962. // Show all the relative links, such as help, search, contents, and the like.
  1963. echo \'
  1964. <link rel="help" href="\', $scripturl, \'?action=help" />
  1965. <link rel="search" href="\' . $scripturl . \'?action=search" />
  1966. <link rel="contents" href="\', $scripturl, \'" />\';
  1967. // If RSS feeds are enabled, advertise the presence of one.
  1968. if (!empty($modSettings[\'xmlnews_enable\']) && (!empty($modSettings[\'allow_guestAccess\']) || $context[\'user\'][\'is_logged\']))
  1969. echo \'
  1970. <link rel="alternate" type="application/rss+xml" title="\', $context[\'forum_name_html_safe\'], \' - \', $txt[\'rss\'], \'" href="\', $scripturl, \'?type=rss;action=.xml" />\';
  1971. // If we\'re viewing a topic, these should be the previous and next topics, respectively.
  1972. if (!empty($context[\'current_topic\']))
  1973. echo \'
  1974. <link rel="prev" href="\', $scripturl, \'?topic=\', $context[\'current_topic\'], \'.0;prev_next=prev" />
  1975. <link rel="next" href="\', $scripturl, \'?topic=\', $context[\'current_topic\'], \'.0;prev_next=next" />\';
  1976. // If we\'re in a board, or a topic for that matter, the index will be the board\'s index.
  1977. if (!empty($context[\'current_board\']))
  1978. echo \'
  1979. <link rel="index" href="\', $scripturl, \'?board=\', $context[\'current_board\'], \'.0" />\';', $old_template);
  1980. // Step 7: The character set.
  1981. $old_template = preg_replace('~\<meta[^>]+http-equiv=["]?Content-Type["]?[^>]*?\>~i', '<meta http-equiv="Content-Type" content="text/html; charset=\', $context[\'character_set\'], \'" />', $old_template);
  1982. // Step 8: The wonderous <yabb ...> tags.
  1983. $tags = array(
  1984. // <yabb title>
  1985. 'title' => '\' . $context[\'page_title\'] . \'',
  1986. // <yabb boardname>
  1987. 'boardname' => '\' . $context[\'forum_name\'] . \'',
  1988. // <yabb uname>
  1989. 'uname' => '\';
  1990. // If the user is logged in, display stuff like their name, new messages, etc.
  1991. if ($context[\'user\'][\'is_logged\'])
  1992. {
  1993. echo \'
  1994. \', $txt[\'hello_member\'], \' <strong>\', $context[\'user\'][\'name\'], \'</strong>, \';
  1995. // Are there any members waiting for approval?
  1996. if (!empty($context[\'unapproved_members\']))
  1997. echo \'<br />
  1998. \', $context[\'unapproved_members\'] == 1 ? $txt[\'approve_thereis\'] : $txt[\'approve_thereare\'], \' <a href="\', $scripturl, \'?action=admin;area=viewmembers;sa=browse;type=approve">\', $context[\'unapproved_members\'] == 1 ? $txt[\'approve_member\'] : $context[\'unapproved_members\'] . \' \' . $txt[\'approve_members\'], \'</a> \', $txt[\'approve_members_waiting\'];
  1999. // Is the forum in maintenance mode?
  2000. if ($context[\'in_maintenance\'] && $context[\'user\'][\'is_admin\'])
  2001. echo \'<br />
  2002. <strong>\', $txt[\'maintain_mode_on\'], \'</strong>\';
  2003. }
  2004. // Otherwise they\'re a guest - so politely ask them to register or login.
  2005. else
  2006. echo \'
  2007. \', $txt[\'welcome_guest\'];
  2008. echo ' . '\'',
  2009. // <yabb im>
  2010. 'im' => '\';
  2011. if ($context[\'user\'][\'is_logged\'] && $context[\'allow_pm\'])
  2012. echo $txt[\'msg_alert_you_have\'], \' <a href="\', $scripturl, \'?action=pm">\', $context[\'user\'][\'messages\'], \' \', ($context[\'user\'][\'messages\'] != 1 ? $txt[\'msg_alert_messages\'] : $txt[\'message_lowercase\']), \'</a>\', $txt[\'newmessages4\'], \' \', $context[\'user\'][\'unread_messages\'], \' \', ($context[\'user\'][\'unread_messages\'] == 1 ? $txt[\'newmessages0\'] : $txt[\'newmessages1\']), \'.\';
  2013. echo ' . '\'',
  2014. // <yabb time>
  2015. 'time' => '\' . $context[\'current_time\'] . \'',
  2016. // <yabb menu>
  2017. 'menu' => '\';
  2018. // Show the menu here, according to the menu sub template.
  2019. template_menu();
  2020. echo ' . '\'',
  2021. // <yabb position>
  2022. 'position' => '\' . $context[\'page_title\'] . \'',
  2023. // <yabb news>
  2024. 'news' => '\';
  2025. // Show a random news item? (or you could pick one from news_lines...)
  2026. if (!empty($settings[\'enable_news\']))
  2027. echo \'<strong>\', $txt[\'news\'], \':</strong> \', $context[\'random_news_line\'];
  2028. echo ' . '\'',
  2029. // <yabb main>
  2030. 'main' => '\';
  2031. }
  2032. function template_main_below()
  2033. {
  2034. global $context, $settings, $options, $scripturl, $txt;
  2035. echo ' .'\'',
  2036. // <yabb vbStyleLogin>
  2037. 'vbstylelogin' => '\';
  2038. // Show a vB style login for quick login?
  2039. if ($context[\'show_quick_login\'])
  2040. echo \'
  2041. <table cellspacing="0" cellpadding="0" border="0" align="center" width="90%">
  2042. <tr><td nowrap="nowrap" align="right">
  2043. <form action="\', $scripturl, \'?action=login2" method="post" accept-charset="', $context['character_set'], '"><br />
  2044. <input type="text" name="user" size="7" class="input_text" />
  2045. <input type="password" name="passwrd" size="7" class="input_password" />
  2046. <select name="cookielength">
  2047. <option value="60">\', $txt[\'one_hour\'], \'</option>
  2048. <option value="1440">\', $txt[\'one_day\'], \'</option>
  2049. <option value="10080">\', $txt[\'one_week\'], \'</option>
  2050. <option value="43200">\', $txt[\'one_month\'], \'</option>
  2051. <option value="-1" selected="selected">\', $txt[\'forever\'], \'</option>
  2052. </select>
  2053. <input type="submit" value="\', $txt[\'login\'], \'" class="button_submit" /><br />
  2054. \', $txt[\'quick_login_dec\'], \'
  2055. </form>
  2056. </td></tr>
  2057. </table>\';
  2058. else
  2059. echo \'<br />\';
  2060. echo ' . '\'',
  2061. // <yabb copyright>
  2062. 'copyright' => '\', theme_copyright(), \'',
  2063. );
  2064. foreach ($tags as $yy => $val)
  2065. $old_template = preg_replace('~\<yabb\s+' . $yy . '\>~i', $val, $old_template);
  2066. // Step 9: Add the time creation code.
  2067. $old_template = preg_replace('~\</body\>~i', '\';
  2068. // Show the load time?
  2069. if ($context[\'show_load_time\'])
  2070. echo \'
  2071. <div class="centertext smalltext">
  2072. \', $txt[\'page_created\'], $context[\'load_time\'], $txt[\'seconds_with\'], $context[\'load_queries\'], $txt[\'queries\'], \'
  2073. </div>\';
  2074. echo \'</body>', $old_template);
  2075. // Step 10: Try to make the style changes. (function because it's a lot of work...)
  2076. $style = makeStyleChanges($old_template);
  2077. $fp = @fopen($output_dir . '/index.template.php', 'w');
  2078. fwrite($fp, $old_template);
  2079. fclose($fp);
  2080. }
  2081. // This is here because it's sorta complex.
  2082. function phpcodefix($string)
  2083. {
  2084. global $smcFunc;
  2085. // First remove the slashes from the single quotes.
  2086. $string = strtr(stripslashes($string), array('\\\'' => '\''));
  2087. // Now add on an end echo and begin echo ;).
  2088. $string = '\';
  2089. ' . $string . '
  2090. echo \'';
  2091. return $string;
  2092. }
  2093. function makeStyleChanges(&$old_template)
  2094. {
  2095. if (preg_match('~</style>~i', $old_template) == 0)
  2096. return false;
  2097. preg_match('~(<style[^<]+)(</style>)~is', $old_template, $style);
  2098. if (empty($style[1]))
  2099. return false;
  2100. $new_style = $style[1];
  2101. // Add some extra stuff...
  2102. $new_style .= '
  2103. .quoteheader, .codeheader {color: black; text-decoration: none; font-style: normal; font-weight: bold;}
  2104. .smalltext {font-size: 8pt;}
  2105. .normaltext {font-size: 10pt;}
  2106. .largetext {font-size: 12pt;}
  2107. input.check {background-color: transparent;}';
  2108. // Add some stuff to .code and .quote...
  2109. $new_style = preg_replace('~(\.code\s*[{][^}]+)}~is', '$1; border: 1px solid black; margin: 1px; padding: 1px;}', $new_style);
  2110. $new_style = preg_replace('~(\.quote\s*[{][^}]+)}~is', '$1; border: 1px solid black; margin: 1px; padding: 1px;}', $new_style);
  2111. $new_style = preg_replace('~(\.code,\s*\.quote\s*[{][^}]+)}~is', '$1; border: 1px solid black; margin: 1px; padding: 1px;}', $new_style);
  2112. // Copy from .text1 => .titlebg.
  2113. preg_match('~\.text1\s*[{]([^}]+)}~is', $new_style, $temp);
  2114. if (isset($temp[1]))
  2115. {
  2116. $new_style = preg_replace('~\.titlebg(\s*[{])([^}]+)}~is', '.titlebg, tr.titlebg th, tr.titlebg td, .titlebg a:link, .titlebg a:visited, .titlebg a:hover$1' . $temp[1] . ';$2}', $new_style);
  2117. $new_style = preg_replace('~\.text1\s*[{]([^}]+)}~is', '', $new_style);
  2118. }
  2119. else
  2120. $new_style = preg_replace('~\.titlebg(\s*[{][^}]+)}~is', '.titlebg, tr.titlebg th, tr.titlebg td, .titlebg a:link, .titlebg a:visited, .titlebg a:hover$1}', $new_style);
  2121. // Look for the background-color of bordercolor... if it's not found, try black. (dumb guess!)
  2122. preg_match('~\.bordercolor\s*[{]([^}]+)}~is', $new_style, $temp);
  2123. if (!empty($temp[1]))
  2124. preg_match('~background(?:-color)?:\s*([^;}\s]+)~is', $temp[1], $temp);
  2125. if (empty($temp[1]))
  2126. $temp[1] = 'black';
  2127. $new_style .= '
  2128. .tborder {border: 1px solid ' . $temp[1] . ';}';
  2129. $old_template = str_replace($style[0], $new_style . "\n" . $style[2], $old_template);
  2130. return true;
  2131. }
  2132. ?>