Themes.php 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  1. <?php
  2. /**
  3. * This file concerns itself almost completely with theme administration.
  4. * Its tasks include changing theme settings, installing and removing
  5. * themes, choosing the current theme, and editing themes.
  6. *
  7. * @todo Update this for the new package manager?
  8. *
  9. * Creating and distributing theme packages:
  10. * There isn't that much required to package and distribute your own themes...
  11. * just do the following:
  12. * - create a theme_info.xml file, with the root element theme-info.
  13. * - its name should go in a name element, just like description.
  14. * - your name should go in author. (email in the email attribute.)
  15. * - any support website for the theme should be in website.
  16. * - layers and templates (non-default) should go in those elements ;).
  17. * - if the images dir isn't images, specify in the images element.
  18. * - any extra rows for themes should go in extra, serialized. (as in array(variable => value).)
  19. * - tar and gzip the directory - and you're done!
  20. * - please include any special license in a license.txt file.
  21. *
  22. * Simple Machines Forum (SMF)
  23. *
  24. * @package SMF
  25. * @author Simple Machines http://www.simplemachines.org
  26. * @copyright 2013 Simple Machines and individual contributors
  27. * @license http://www.simplemachines.org/about/smf/license.php BSD
  28. *
  29. * @version 2.1 Alpha 1
  30. */
  31. if (!defined('SMF'))
  32. die('No direct access...');
  33. /**
  34. * Subaction handler - manages the action and delegates control to the proper
  35. * sub-action.
  36. * It loads both the Themes and Settings language files.
  37. * Checks the session by GET or POST to verify the sent data.
  38. * Requires the user not be a guest. (@todo what?)
  39. * Accessed via ?action=admin;area=theme.
  40. */
  41. function ThemesMain()
  42. {
  43. global $txt, $context, $scripturl, $sourcedir;
  44. // Load the important language files...
  45. loadLanguage('Themes');
  46. loadLanguage('Settings');
  47. loadLanguage('Drafts');
  48. // No funny business - guests only.
  49. is_not_guest();
  50. require_once($sourcedir . '/Subs-Themes.php');
  51. // Default the page title to Theme Administration by default.
  52. $context['page_title'] = $txt['themeadmin_title'];
  53. // Theme administration, removal, choice, or installation...
  54. $subActions = array(
  55. 'admin' => 'ThemeAdmin',
  56. 'list' => 'ThemeList',
  57. 'reset' => 'SetThemeOptions',
  58. 'options' => 'SetThemeOptions',
  59. 'install' => 'ThemeInstall',
  60. 'remove' => 'RemoveTheme',
  61. 'pick' => 'PickTheme',
  62. 'edit' => 'EditTheme',
  63. 'enable' => 'EnableTheme',
  64. 'copy' => 'CopyTemplate',
  65. );
  66. // @todo Layout Settings?
  67. if (!empty($context['admin_menu_name']))
  68. {
  69. $context[$context['admin_menu_name']]['tab_data'] = array(
  70. 'title' => $txt['themeadmin_title'],
  71. 'help' => 'themes',
  72. 'description' => $txt['themeadmin_description'],
  73. 'tabs' => array(
  74. 'admin' => array(
  75. 'description' => $txt['themeadmin_admin_desc'],
  76. ),
  77. 'list' => array(
  78. 'description' => $txt['themeadmin_list_desc'],
  79. ),
  80. 'reset' => array(
  81. 'description' => $txt['themeadmin_reset_desc'],
  82. ),
  83. 'edit' => array(
  84. 'description' => $txt['themeadmin_edit_desc'],
  85. ),
  86. ),
  87. );
  88. }
  89. // Follow the sa or just go to administration.
  90. if (isset($_GET['sa']) && !empty($subActions[$_GET['sa']]))
  91. $subActions[$_GET['sa']]();
  92. else
  93. $subActions['admin']();
  94. }
  95. /**
  96. * This function allows administration of themes and their settings,
  97. * as well as global theme settings.
  98. * - sets the settings theme_allow, theme_guests, and knownThemes.
  99. * - requires the admin_forum permission.
  100. * - accessed with ?action=admin;area=theme;sa=admin.
  101. *
  102. * @uses Themes template
  103. * @uses Admin language file
  104. */
  105. function ThemeAdmin()
  106. {
  107. global $context, $boarddir, $modSettings, $smcFunc;
  108. loadLanguage('Admin');
  109. isAllowedTo('admin_forum');
  110. loadTemplate('Themes');
  111. // List all installed and enabled themes.
  112. get_all_themes(true);
  113. // Can we create a new theme?
  114. $context['can_create_new'] = is_writable($boarddir . '/Themes');
  115. $context['new_theme_dir'] = substr(realpath($boarddir . '/Themes/default'), 0, -7);
  116. // Look for a non existent theme directory. (ie theme87.)
  117. $theme_dir = $boarddir . '/Themes/theme';
  118. $i = 1;
  119. while (file_exists($theme_dir . $i))
  120. $i++;
  121. $context['new_theme_name'] = 'theme' . $i;
  122. // A bunch of tokens for a bunch of forms.
  123. createToken('admin-tm');
  124. createToken('admin-t-file');
  125. createToken('admin-t-copy');
  126. createToken('admin-t-dir');
  127. // Are handling any settings?
  128. if (isset($_POST['save']))
  129. {
  130. checkSession();
  131. validateToken('admin-tm');
  132. if (isset($_POST['options']['known_themes']))
  133. foreach ($_POST['options']['known_themes'] as $key => $id)
  134. $_POST['options']['known_themes'][$key] = (int) $id;
  135. else
  136. fatal_lang_error('themes_none_selectable', false);
  137. if (!in_array($_POST['options']['theme_guests'], $_POST['options']['known_themes']))
  138. fatal_lang_error('themes_default_selectable', false);
  139. // Commit the new settings.
  140. updateSettings(array(
  141. 'theme_allow' => $_POST['options']['theme_allow'],
  142. 'theme_guests' => $_POST['options']['theme_guests'],
  143. 'knownThemes' => implode(',', $_POST['options']['known_themes']),
  144. ));
  145. if ((int) $_POST['theme_reset'] == 0 || in_array($_POST['theme_reset'], $_POST['options']['known_themes']))
  146. updateMemberData(null, array('id_theme' => (int) $_POST['theme_reset']));
  147. redirectexit('action=admin;area=theme;' . $context['session_var'] . '=' . $context['session_id'] . ';sa=admin');
  148. }
  149. }
  150. /**
  151. * This function lists the available themes and provides an interface to reset
  152. * the paths of all the installed themes.
  153. */
  154. function ThemeList()
  155. {
  156. global $context, $boarddir, $boardurl, $smcFunc;
  157. loadLanguage('Admin');
  158. isAllowedTo('admin_forum');
  159. if (isset($_REQUEST['th']))
  160. return SetThemeSettings();
  161. if (isset($_POST['save']))
  162. {
  163. checkSession();
  164. validateToken('admin-tl');
  165. $themes = get_all_themes(false);
  166. $setValues = array();
  167. foreach ($themes as $id => $theme)
  168. {
  169. if (file_exists($_POST['reset_dir'] . '/' . basename($theme['theme_dir'])))
  170. {
  171. $setValues[] = array($id, 0, 'theme_dir', realpath($_POST['reset_dir'] . '/' . basename($theme['theme_dir'])));
  172. $setValues[] = array($id, 0, 'theme_url', $_POST['reset_url'] . '/' . basename($theme['theme_dir']));
  173. $setValues[] = array($id, 0, 'images_url', $_POST['reset_url'] . '/' . basename($theme['theme_dir']) . '/' . basename($theme['images_url']));
  174. }
  175. if (isset($theme['base_theme_dir']) && file_exists($_POST['reset_dir'] . '/' . basename($theme['base_theme_dir'])))
  176. {
  177. $setValues[] = array($id, 0, 'base_theme_dir', realpath($_POST['reset_dir'] . '/' . basename($theme['base_theme_dir'])));
  178. $setValues[] = array($id, 0, 'base_theme_url', $_POST['reset_url'] . '/' . basename($theme['base_theme_dir']));
  179. $setValues[] = array($id, 0, 'base_images_url', $_POST['reset_url'] . '/' . basename($theme['base_theme_dir']) . '/' . basename($theme['base_images_url']));
  180. }
  181. cache_put_data('theme_settings-' . $id, null, 90);
  182. }
  183. if (!empty($setValues))
  184. {
  185. $smcFunc['db_insert']('replace',
  186. '{db_prefix}themes',
  187. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  188. $setValues,
  189. array('id_theme', 'variable', 'id_member')
  190. );
  191. }
  192. redirectexit('action=admin;area=theme;sa=list;' . $context['session_var'] . '=' . $context['session_id']);
  193. }
  194. loadTemplate('Themes');
  195. // Get all installed themes.
  196. get_all_themes(false);
  197. $context['reset_dir'] = realpath($boarddir . '/Themes');
  198. $context['reset_url'] = $boardurl . '/Themes';
  199. $context['sub_template'] = 'list_themes';
  200. createToken('admin-tl');
  201. createToken('admin-tr', 'request');
  202. createToken('admin-tre', 'request');
  203. }
  204. /**
  205. * Administrative global settings.
  206. */
  207. function SetThemeOptions()
  208. {
  209. global $txt, $context, $settings, $modSettings, $smcFunc;
  210. $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (isset($_GET['id']) ? (int) $_GET['id'] : 0);
  211. isAllowedTo('admin_forum');
  212. if (empty($_GET['th']) && empty($_GET['id']))
  213. {
  214. $request = $smcFunc['db_query']('', '
  215. SELECT id_theme, variable, value
  216. FROM {db_prefix}themes
  217. WHERE variable IN ({string:name}, {string:theme_dir})
  218. AND id_member = {int:no_member}',
  219. array(
  220. 'no_member' => 0,
  221. 'name' => 'name',
  222. 'theme_dir' => 'theme_dir',
  223. )
  224. );
  225. $context['themes'] = array();
  226. while ($row = $smcFunc['db_fetch_assoc']($request))
  227. {
  228. if (!isset($context['themes'][$row['id_theme']]))
  229. $context['themes'][$row['id_theme']] = array(
  230. 'id' => $row['id_theme'],
  231. 'num_default_options' => 0,
  232. 'num_members' => 0,
  233. );
  234. $context['themes'][$row['id_theme']][$row['variable']] = $row['value'];
  235. }
  236. $smcFunc['db_free_result']($request);
  237. $request = $smcFunc['db_query']('', '
  238. SELECT id_theme, COUNT(*) AS value
  239. FROM {db_prefix}themes
  240. WHERE id_member = {int:guest_member}
  241. GROUP BY id_theme',
  242. array(
  243. 'guest_member' => -1,
  244. )
  245. );
  246. while ($row = $smcFunc['db_fetch_assoc']($request))
  247. $context['themes'][$row['id_theme']]['num_default_options'] = $row['value'];
  248. $smcFunc['db_free_result']($request);
  249. // Need to make sure we don't do custom fields.
  250. $request = $smcFunc['db_query']('', '
  251. SELECT col_name
  252. FROM {db_prefix}custom_fields',
  253. array(
  254. )
  255. );
  256. $customFields = array();
  257. while ($row = $smcFunc['db_fetch_assoc']($request))
  258. $customFields[] = $row['col_name'];
  259. $smcFunc['db_free_result']($request);
  260. $customFieldsQuery = empty($customFields) ? '' : ('AND variable NOT IN ({array_string:custom_fields})');
  261. $request = $smcFunc['db_query']('themes_count', '
  262. SELECT COUNT(DISTINCT id_member) AS value, id_theme
  263. FROM {db_prefix}themes
  264. WHERE id_member > {int:no_member}
  265. ' . $customFieldsQuery . '
  266. GROUP BY id_theme',
  267. array(
  268. 'no_member' => 0,
  269. 'custom_fields' => empty($customFields) ? array() : $customFields,
  270. )
  271. );
  272. while ($row = $smcFunc['db_fetch_assoc']($request))
  273. $context['themes'][$row['id_theme']]['num_members'] = $row['value'];
  274. $smcFunc['db_free_result']($request);
  275. // There has to be a Settings template!
  276. foreach ($context['themes'] as $k => $v)
  277. if (empty($v['theme_dir']) || (!file_exists($v['theme_dir'] . '/Settings.template.php') && empty($v['num_members'])))
  278. unset($context['themes'][$k]);
  279. loadTemplate('Themes');
  280. $context['sub_template'] = 'reset_list';
  281. createToken('admin-stor', 'request');
  282. return;
  283. }
  284. // Submit?
  285. if (isset($_POST['submit']) && empty($_POST['who']))
  286. {
  287. checkSession();
  288. validateToken('admin-sto');
  289. if (empty($_POST['options']))
  290. $_POST['options'] = array();
  291. if (empty($_POST['default_options']))
  292. $_POST['default_options'] = array();
  293. // Set up the sql query.
  294. $setValues = array();
  295. foreach ($_POST['options'] as $opt => $val)
  296. $setValues[] = array(-1, $_GET['th'], $opt, is_array($val) ? implode(',', $val) : $val);
  297. $old_settings = array();
  298. foreach ($_POST['default_options'] as $opt => $val)
  299. {
  300. $old_settings[] = $opt;
  301. $setValues[] = array(-1, 1, $opt, is_array($val) ? implode(',', $val) : $val);
  302. }
  303. // If we're actually inserting something..
  304. if (!empty($setValues))
  305. {
  306. // Are there options in non-default themes set that should be cleared?
  307. if (!empty($old_settings))
  308. $smcFunc['db_query']('', '
  309. DELETE FROM {db_prefix}themes
  310. WHERE id_theme != {int:default_theme}
  311. AND id_member = {int:guest_member}
  312. AND variable IN ({array_string:old_settings})',
  313. array(
  314. 'default_theme' => 1,
  315. 'guest_member' => -1,
  316. 'old_settings' => $old_settings,
  317. )
  318. );
  319. $smcFunc['db_insert']('replace',
  320. '{db_prefix}themes',
  321. array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  322. $setValues,
  323. array('id_theme', 'variable', 'id_member')
  324. );
  325. }
  326. cache_put_data('theme_settings-' . $_GET['th'], null, 90);
  327. cache_put_data('theme_settings-1', null, 90);
  328. redirectexit('action=admin;area=theme;' . $context['session_var'] . '=' . $context['session_id'] . ';sa=reset');
  329. }
  330. elseif (isset($_POST['submit']) && $_POST['who'] == 1)
  331. {
  332. checkSession();
  333. validateToken('admin-sto');
  334. $_POST['options'] = empty($_POST['options']) ? array() : $_POST['options'];
  335. $_POST['options_master'] = empty($_POST['options_master']) ? array() : $_POST['options_master'];
  336. $_POST['default_options'] = empty($_POST['default_options']) ? array() : $_POST['default_options'];
  337. $_POST['default_options_master'] = empty($_POST['default_options_master']) ? array() : $_POST['default_options_master'];
  338. $old_settings = array();
  339. foreach ($_POST['default_options'] as $opt => $val)
  340. {
  341. if ($_POST['default_options_master'][$opt] == 0)
  342. continue;
  343. elseif ($_POST['default_options_master'][$opt] == 1)
  344. {
  345. // Delete then insert for ease of database compatibility!
  346. $smcFunc['db_query']('substring', '
  347. DELETE FROM {db_prefix}themes
  348. WHERE id_theme = {int:default_theme}
  349. AND id_member != {int:no_member}
  350. AND variable = SUBSTRING({string:option}, 1, 255)',
  351. array(
  352. 'default_theme' => 1,
  353. 'no_member' => 0,
  354. 'option' => $opt,
  355. )
  356. );
  357. $smcFunc['db_query']('substring', '
  358. INSERT INTO {db_prefix}themes
  359. (id_member, id_theme, variable, value)
  360. SELECT id_member, 1, SUBSTRING({string:option}, 1, 255), SUBSTRING({string:value}, 1, 65534)
  361. FROM {db_prefix}members',
  362. array(
  363. 'option' => $opt,
  364. 'value' => (is_array($val) ? implode(',', $val) : $val),
  365. )
  366. );
  367. $old_settings[] = $opt;
  368. }
  369. elseif ($_POST['default_options_master'][$opt] == 2)
  370. {
  371. $smcFunc['db_query']('', '
  372. DELETE FROM {db_prefix}themes
  373. WHERE variable = {string:option_name}
  374. AND id_member > {int:no_member}',
  375. array(
  376. 'no_member' => 0,
  377. 'option_name' => $opt,
  378. )
  379. );
  380. }
  381. }
  382. // Delete options from other themes.
  383. if (!empty($old_settings))
  384. $smcFunc['db_query']('', '
  385. DELETE FROM {db_prefix}themes
  386. WHERE id_theme != {int:default_theme}
  387. AND id_member > {int:no_member}
  388. AND variable IN ({array_string:old_settings})',
  389. array(
  390. 'default_theme' => 1,
  391. 'no_member' => 0,
  392. 'old_settings' => $old_settings,
  393. )
  394. );
  395. foreach ($_POST['options'] as $opt => $val)
  396. {
  397. if ($_POST['options_master'][$opt] == 0)
  398. continue;
  399. elseif ($_POST['options_master'][$opt] == 1)
  400. {
  401. // Delete then insert for ease of database compatibility - again!
  402. $smcFunc['db_query']('substring', '
  403. DELETE FROM {db_prefix}themes
  404. WHERE id_theme = {int:current_theme}
  405. AND id_member != {int:no_member}
  406. AND variable = SUBSTRING({string:option}, 1, 255)',
  407. array(
  408. 'current_theme' => $_GET['th'],
  409. 'no_member' => 0,
  410. 'option' => $opt,
  411. )
  412. );
  413. $smcFunc['db_query']('substring', '
  414. INSERT INTO {db_prefix}themes
  415. (id_member, id_theme, variable, value)
  416. SELECT id_member, {int:current_theme}, SUBSTRING({string:option}, 1, 255), SUBSTRING({string:value}, 1, 65534)
  417. FROM {db_prefix}members',
  418. array(
  419. 'current_theme' => $_GET['th'],
  420. 'option' => $opt,
  421. 'value' => (is_array($val) ? implode(',', $val) : $val),
  422. )
  423. );
  424. }
  425. elseif ($_POST['options_master'][$opt] == 2)
  426. {
  427. $smcFunc['db_query']('', '
  428. DELETE FROM {db_prefix}themes
  429. WHERE variable = {string:option}
  430. AND id_member > {int:no_member}
  431. AND id_theme = {int:current_theme}',
  432. array(
  433. 'no_member' => 0,
  434. 'current_theme' => $_GET['th'],
  435. 'option' => $opt,
  436. )
  437. );
  438. }
  439. }
  440. redirectexit('action=admin;area=theme;' . $context['session_var'] . '=' . $context['session_id'] . ';sa=reset');
  441. }
  442. elseif (!empty($_GET['who']) && $_GET['who'] == 2)
  443. {
  444. checkSession('get');
  445. validateToken('admin-stor', 'request');
  446. // Don't delete custom fields!!
  447. if ($_GET['th'] == 1)
  448. {
  449. $request = $smcFunc['db_query']('', '
  450. SELECT col_name
  451. FROM {db_prefix}custom_fields',
  452. array(
  453. )
  454. );
  455. $customFields = array();
  456. while ($row = $smcFunc['db_fetch_assoc']($request))
  457. $customFields[] = $row['col_name'];
  458. $smcFunc['db_free_result']($request);
  459. }
  460. $customFieldsQuery = empty($customFields) ? '' : ('AND variable NOT IN ({array_string:custom_fields})');
  461. $smcFunc['db_query']('', '
  462. DELETE FROM {db_prefix}themes
  463. WHERE id_member > {int:no_member}
  464. AND id_theme = {int:current_theme}
  465. ' . $customFieldsQuery,
  466. array(
  467. 'no_member' => 0,
  468. 'current_theme' => $_GET['th'],
  469. 'custom_fields' => empty($customFields) ? array() : $customFields,
  470. )
  471. );
  472. redirectexit('action=admin;area=theme;' . $context['session_var'] . '=' . $context['session_id'] . ';sa=reset');
  473. }
  474. $old_id = $settings['theme_id'];
  475. $old_settings = $settings;
  476. loadTheme($_GET['th'], false);
  477. loadLanguage('Profile');
  478. // @todo Should we just move these options so they are no longer theme dependant?
  479. loadLanguage('PersonalMessage');
  480. // Let the theme take care of the settings.
  481. loadTemplate('Settings');
  482. loadSubTemplate('options');
  483. $context['sub_template'] = 'set_options';
  484. $context['page_title'] = $txt['theme_settings'];
  485. $context['options'] = $context['theme_options'];
  486. $context['theme_settings'] = $settings;
  487. if (empty($_REQUEST['who']))
  488. {
  489. $request = $smcFunc['db_query']('', '
  490. SELECT variable, value
  491. FROM {db_prefix}themes
  492. WHERE id_theme IN (1, {int:current_theme})
  493. AND id_member = {int:guest_member}',
  494. array(
  495. 'current_theme' => $_GET['th'],
  496. 'guest_member' => -1,
  497. )
  498. );
  499. $context['theme_options'] = array();
  500. while ($row = $smcFunc['db_fetch_assoc']($request))
  501. $context['theme_options'][$row['variable']] = $row['value'];
  502. $smcFunc['db_free_result']($request);
  503. $context['theme_options_reset'] = false;
  504. }
  505. else
  506. {
  507. $context['theme_options'] = array();
  508. $context['theme_options_reset'] = true;
  509. }
  510. foreach ($context['options'] as $i => $setting)
  511. {
  512. // Is this disabled?
  513. if ($setting['id'] == 'calendar_start_day' && empty($modSettings['cal_enabled']))
  514. {
  515. unset($context['options'][$i]);
  516. continue;
  517. }
  518. elseif (($setting['id'] == 'topics_per_page' || $setting['id'] == 'messages_per_page') && !empty($modSettings['disableCustomPerPage']))
  519. {
  520. unset($context['options'][$i]);
  521. continue;
  522. }
  523. if (!isset($setting['type']) || $setting['type'] == 'bool')
  524. $context['options'][$i]['type'] = 'checkbox';
  525. elseif ($setting['type'] == 'int' || $setting['type'] == 'integer')
  526. $context['options'][$i]['type'] = 'number';
  527. elseif ($setting['type'] == 'string')
  528. $context['options'][$i]['type'] = 'text';
  529. if (isset($setting['options']))
  530. $context['options'][$i]['type'] = 'list';
  531. $context['options'][$i]['value'] = !isset($context['theme_options'][$setting['id']]) ? '' : $context['theme_options'][$setting['id']];
  532. }
  533. // Restore the existing theme.
  534. loadTheme($old_id, false);
  535. $settings = $old_settings;
  536. loadTemplate('Themes');
  537. createToken('admin-sto');
  538. }
  539. /**
  540. * Administrative global settings.
  541. * - saves and requests global theme settings. ($settings)
  542. * - loads the Admin language file.
  543. * - calls ThemeAdmin() if no theme is specified. (the theme center.)
  544. * - requires admin_forum permission.
  545. * - accessed with ?action=admin;area=theme;sa=list&th=xx.
  546. */
  547. function SetThemeSettings()
  548. {
  549. global $txt, $context, $settings, $modSettings, $sourcedir, $smcFunc;
  550. if (empty($_GET['th']) && empty($_GET['id']))
  551. return ThemeAdmin();
  552. $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
  553. // Select the best fitting tab.
  554. $context[$context['admin_menu_name']]['current_subsection'] = 'list';
  555. loadLanguage('Admin');
  556. isAllowedTo('admin_forum');
  557. // Validate inputs/user.
  558. if (empty($_GET['th']))
  559. fatal_lang_error('no_theme', false);
  560. // Fetch the smiley sets...
  561. $sets = explode(',', 'none,' . $modSettings['smiley_sets_known']);
  562. $set_names = explode("\n", $txt['smileys_none'] . "\n" . $modSettings['smiley_sets_names']);
  563. $context['smiley_sets'] = array(
  564. '' => $txt['smileys_no_default']
  565. );
  566. foreach ($sets as $i => $set)
  567. $context['smiley_sets'][$set] = $smcFunc['htmlspecialchars']($set_names[$i]);
  568. $old_id = $settings['theme_id'];
  569. $old_settings = $settings;
  570. loadTheme($_GET['th'], false);
  571. // Sadly we really do need to init the template.
  572. loadSubTemplate('init', 'ignore');
  573. // Also load the actual themes language file - in case of special settings.
  574. loadLanguage('Settings', '', true, true);
  575. // And the custom language strings...
  576. loadLanguage('ThemeStrings', '', false, true);
  577. // Let the theme take care of the settings.
  578. loadTemplate('Settings');
  579. loadSubTemplate('settings');
  580. // Load the variants separately...
  581. $settings['theme_variants'] = array();
  582. if (file_exists($settings['theme_dir'] . '/index.template.php'))
  583. {
  584. $file_contents = implode('', file($settings['theme_dir'] . '/index.template.php'));
  585. if (preg_match('~\$settings\[\'theme_variants\'\]\s*=(.+?);~', $file_contents, $matches))
  586. eval('global $settings;' . $matches[0]);
  587. }
  588. // Submitting!
  589. if (isset($_POST['save']))
  590. {
  591. checkSession();
  592. validateToken('admin-sts');
  593. if (empty($_POST['options']))
  594. $_POST['options'] = array();
  595. if (empty($_POST['default_options']))
  596. $_POST['default_options'] = array();
  597. // Make sure items are cast correctly.
  598. foreach ($context['theme_settings'] as $item)
  599. {
  600. // Disregard this item if this is just a separator.
  601. if (!is_array($item))
  602. continue;
  603. foreach (array('options', 'default_options') as $option)
  604. {
  605. if (!isset($_POST[$option][$item['id']]))
  606. continue;
  607. // Checkbox.
  608. elseif (empty($item['type']))
  609. $_POST[$option][$item['id']] = $_POST[$option][$item['id']] ? 1 : 0;
  610. // Number
  611. elseif ($item['type'] == 'number')
  612. $_POST[$option][$item['id']] = (int) $_POST[$option][$item['id']];
  613. }
  614. }
  615. // Set up the sql query.
  616. $inserts = array();
  617. foreach ($_POST['options'] as $opt => $val)
  618. $inserts[] = array(0, $_GET['th'], $opt, is_array($val) ? implode(',', $val) : $val);
  619. foreach ($_POST['default_options'] as $opt => $val)
  620. $inserts[] = array(0, 1, $opt, is_array($val) ? implode(',', $val) : $val);
  621. // If we're actually inserting something..
  622. if (!empty($inserts))
  623. {
  624. $smcFunc['db_insert']('replace',
  625. '{db_prefix}themes',
  626. array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  627. $inserts,
  628. array('id_member', 'id_theme', 'variable')
  629. );
  630. }
  631. cache_put_data('theme_settings-' . $_GET['th'], null, 90);
  632. cache_put_data('theme_settings-1', null, 90);
  633. // Invalidate the cache.
  634. updateSettings(array('settings_updated' => time()));
  635. redirectexit('action=admin;area=theme;sa=list;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id']);
  636. }
  637. $context['sub_template'] = 'set_settings';
  638. $context['page_title'] = $txt['theme_settings'];
  639. foreach ($settings as $setting => $dummy)
  640. {
  641. if (!in_array($setting, array('theme_url', 'theme_dir', 'images_url', 'template_dirs')))
  642. $settings[$setting] = htmlspecialchars__recursive($settings[$setting]);
  643. }
  644. $context['settings'] = $context['theme_settings'];
  645. $context['theme_settings'] = $settings;
  646. foreach ($context['settings'] as $i => $setting)
  647. {
  648. // Separators are dummies, so leave them alone.
  649. if (!is_array($setting))
  650. continue;
  651. if (!isset($setting['type']) || $setting['type'] == 'bool')
  652. $context['settings'][$i]['type'] = 'checkbox';
  653. elseif ($setting['type'] == 'int' || $setting['type'] == 'integer')
  654. $context['settings'][$i]['type'] = 'number';
  655. elseif ($setting['type'] == 'string')
  656. $context['settings'][$i]['type'] = 'text';
  657. if (isset($setting['options']))
  658. $context['settings'][$i]['type'] = 'list';
  659. $context['settings'][$i]['value'] = !isset($settings[$setting['id']]) ? '' : $settings[$setting['id']];
  660. }
  661. // Do we support variants?
  662. if (!empty($settings['theme_variants']))
  663. {
  664. $context['theme_variants'] = array();
  665. foreach ($settings['theme_variants'] as $variant)
  666. {
  667. // Have any text, old chap?
  668. $context['theme_variants'][$variant] = array(
  669. 'label' => isset($txt['variant_' . $variant]) ? $txt['variant_' . $variant] : $variant,
  670. 'thumbnail' => !file_exists($settings['theme_dir'] . '/images/thumbnail.png') || file_exists($settings['theme_dir'] . '/images/thumbnail_' . $variant . '.png') ? $settings['images_url'] . '/thumbnail_' . $variant . '.png' : ($settings['images_url'] . '/thumbnail.png'),
  671. );
  672. }
  673. $context['default_variant'] = !empty($settings['default_variant']) && isset($context['theme_variants'][$settings['default_variant']]) ? $settings['default_variant'] : $settings['theme_variants'][0];
  674. }
  675. // Restore the current theme.
  676. loadTheme($old_id, false);
  677. // Reinit just incase.
  678. loadSubTemplate('init', 'ignore');
  679. $settings = $old_settings;
  680. loadTemplate('Themes');
  681. // We like Kenny better than Token.
  682. createToken('admin-sts');
  683. }
  684. /**
  685. * Remove a theme from the database.
  686. * - removes an installed theme.
  687. * - requires an administrator.
  688. * - accessed with ?action=admin;area=theme;sa=remove.
  689. */
  690. function RemoveTheme()
  691. {
  692. global $modSettings, $context, $smcFunc;
  693. checkSession('get');
  694. isAllowedTo('admin_forum');
  695. validateToken('admin-tr', 'request');
  696. // The theme's ID must be an integer.
  697. $themeID = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
  698. // You can't delete the default theme!
  699. if ($themeID == 1)
  700. fatal_lang_error('no_access', false);
  701. $theme_info = get_single_theme($themeID);
  702. // Remove it from the DB.
  703. remove_theme($themeID);
  704. // And remove all its files and folders too.
  705. if (!empty($theme_info) && !empty($theme_info['theme_dir']))
  706. remove_dir($theme_info['theme_dir']);
  707. // Go back to the list page.
  708. redirectexit('action=admin;area=theme;sa=list;' . $context['session_var'] . '=' . $context['session_id'] .';done=removing');
  709. }
  710. function EnableTheme()
  711. {
  712. global $modSettings, $context;
  713. checkSession('get');
  714. isAllowedTo('admin_forum');
  715. validateToken('admin-tre', 'request');
  716. // The theme's ID must be an string.
  717. $themeID = isset($_GET['th']) ? (string) trim($_GET['th']) : (string) trim($_GET['id']);
  718. // Get the current list.
  719. $enableThemes = explode(',', $modSettings['enableThemes']);
  720. // Are we disabling it?
  721. if (isset($_GET['disabled']))
  722. $enableThemes = array_diff($enableThemes, array($themeID));
  723. // Nope? then enable it!
  724. else
  725. $enableThemes[] = (string) $themeID;
  726. // Update the setting.
  727. $enableThemes = strtr(implode(',', $enableThemes), array(',,' => ','));
  728. updateSettings(array('enableThemes' => $enableThemes));
  729. // Done!
  730. redirectexit('action=admin;area=theme;sa=list;' . $context['session_var'] . '=' . $context['session_id'] .';done='. (isset($_GET['disabled']) ? 'disabling' : 'enabling'));
  731. }
  732. /**
  733. * Choose a theme from a list.
  734. * allows an user or administrator to pick a new theme with an interface.
  735. * - can edit everyone's (u = 0), guests' (u = -1), or a specific user's.
  736. * - uses the Themes template. (pick sub template.)
  737. * - accessed with ?action=admin;area=theme;sa=pick.
  738. * @todo thought so... Might be better to split this file in ManageThemes and Themes,
  739. * with centralized admin permissions on ManageThemes.
  740. */
  741. function PickTheme()
  742. {
  743. global $txt, $context, $modSettings, $user_info, $language, $smcFunc, $settings, $scripturl;
  744. loadLanguage('Profile');
  745. loadTemplate('Themes');
  746. // Build the link tree.
  747. $context['linktree'][] = array(
  748. 'url' => $scripturl . '?action=theme;sa=pick;u=' . (!empty($_REQUEST['u']) ? (int) $_REQUEST['u'] : 0),
  749. 'name' => $txt['theme_pick'],
  750. );
  751. $context['default_theme_id'] = $modSettings['theme_default'];
  752. $_SESSION['id_theme'] = 0;
  753. if (isset($_GET['id']))
  754. $_GET['th'] = $_GET['id'];
  755. // Saving a variant cause JS doesn't work - pretend it did ;)
  756. if (isset($_POST['save']))
  757. {
  758. // Which theme?
  759. foreach ($_POST['save'] as $k => $v)
  760. $_GET['th'] = (int) $k;
  761. if (isset($_POST['vrt'][$k]))
  762. $_GET['vrt'] = $_POST['vrt'][$k];
  763. }
  764. // Have we made a decision, or are we just browsing?
  765. if (isset($_GET['th']))
  766. {
  767. checkSession('get');
  768. $_GET['th'] = (int) $_GET['th'];
  769. // Save for this user.
  770. if (!isset($_REQUEST['u']) || !allowedTo('admin_forum'))
  771. {
  772. updateMemberData($user_info['id'], array('id_theme' => (int) $_GET['th']));
  773. // A variants to save for the user?
  774. if (!empty($_GET['vrt']))
  775. {
  776. $smcFunc['db_insert']('replace',
  777. '{db_prefix}themes',
  778. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  779. array($_GET['th'], $user_info['id'], 'theme_variant', $_GET['vrt']),
  780. array('id_theme', 'id_member', 'variable')
  781. );
  782. cache_put_data('theme_settings-' . $_GET['th'] . ':' . $user_info['id'], null, 90);
  783. $_SESSION['id_variant'] = 0;
  784. }
  785. redirectexit('action=profile;area=theme');
  786. }
  787. // If changing members or guests - and there's a variant - assume changing default variant.
  788. if (!empty($_GET['vrt']) && ($_REQUEST['u'] == '0' || $_REQUEST['u'] == '-1'))
  789. {
  790. $smcFunc['db_insert']('replace',
  791. '{db_prefix}themes',
  792. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  793. array($_GET['th'], 0, 'default_variant', $_GET['vrt']),
  794. array('id_theme', 'id_member', 'variable')
  795. );
  796. // Make it obvious that it's changed
  797. cache_put_data('theme_settings-' . $_GET['th'], null, 90);
  798. }
  799. // For everyone.
  800. if ($_REQUEST['u'] == '0')
  801. {
  802. updateMemberData(null, array('id_theme' => (int) $_GET['th']));
  803. // Remove any custom variants.
  804. if (!empty($_GET['vrt']))
  805. {
  806. $smcFunc['db_query']('', '
  807. DELETE FROM {db_prefix}themes
  808. WHERE id_theme = {int:current_theme}
  809. AND variable = {string:theme_variant}',
  810. array(
  811. 'current_theme' => (int) $_GET['th'],
  812. 'theme_variant' => 'theme_variant',
  813. )
  814. );
  815. }
  816. redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
  817. }
  818. // Change the default/guest theme.
  819. elseif ($_REQUEST['u'] == '-1')
  820. {
  821. updateSettings(array('theme_guests' => (int) $_GET['th']));
  822. redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
  823. }
  824. // Change a specific member's theme.
  825. else
  826. {
  827. // The forum's default theme is always 0 and we
  828. if (isset($_GET['th']) && $_GET['th'] == 0)
  829. $_GET['th'] = $modSettings['theme_guests'];
  830. updateMemberData((int) $_REQUEST['u'], array('id_theme' => (int) $_GET['th']));
  831. if (!empty($_GET['vrt']))
  832. {
  833. $smcFunc['db_insert']('replace',
  834. '{db_prefix}themes',
  835. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  836. array($_GET['th'], (int) $_REQUEST['u'], 'theme_variant', $_GET['vrt']),
  837. array('id_theme', 'id_member', 'variable')
  838. );
  839. cache_put_data('theme_settings-' . $_GET['th'] . ':' . (int) $_REQUEST['u'], null, 90);
  840. if ($user_info['id'] == $_REQUEST['u'])
  841. $_SESSION['id_variant'] = 0;
  842. }
  843. redirectexit('action=profile;u=' . (int) $_REQUEST['u'] . ';area=theme');
  844. }
  845. }
  846. // Figure out who the member of the minute is, and what theme they've chosen.
  847. if (!isset($_REQUEST['u']) || !allowedTo('admin_forum'))
  848. {
  849. $context['current_member'] = $user_info['id'];
  850. $context['current_theme'] = $user_info['theme'];
  851. }
  852. // Everyone can't chose just one.
  853. elseif ($_REQUEST['u'] == '0')
  854. {
  855. $context['current_member'] = 0;
  856. $context['current_theme'] = 0;
  857. }
  858. // Guests and such...
  859. elseif ($_REQUEST['u'] == '-1')
  860. {
  861. $context['current_member'] = -1;
  862. $context['current_theme'] = $modSettings['theme_guests'];
  863. }
  864. // Someones else :P.
  865. else
  866. {
  867. $context['current_member'] = (int) $_REQUEST['u'];
  868. $request = $smcFunc['db_query']('', '
  869. SELECT id_theme
  870. FROM {db_prefix}members
  871. WHERE id_member = {int:current_member}
  872. LIMIT 1',
  873. array(
  874. 'current_member' => $context['current_member'],
  875. )
  876. );
  877. list ($context['current_theme']) = $smcFunc['db_fetch_row']($request);
  878. $smcFunc['db_free_result']($request);
  879. }
  880. // Get the theme name and descriptions.
  881. $context['available_themes'] = array();
  882. if (!empty($modSettings['knownThemes']))
  883. {
  884. $request = $smcFunc['db_query']('', '
  885. SELECT id_theme, variable, value
  886. FROM {db_prefix}themes
  887. WHERE variable IN ({string:name}, {string:theme_url}, {string:theme_dir}, {string:images_url}, {string:disable_user_variant})' . (!allowedTo('admin_forum') ? '
  888. AND id_theme IN ({array_string:known_themes})' : '') . '
  889. AND id_theme != {int:default_theme}
  890. AND id_member = {int:no_member}
  891. AND id_theme IN ({array_string:enable_themes})',
  892. array(
  893. 'default_theme' => 0,
  894. 'name' => 'name',
  895. 'no_member' => 0,
  896. 'theme_url' => 'theme_url',
  897. 'theme_dir' => 'theme_dir',
  898. 'images_url' => 'images_url',
  899. 'disable_user_variant' => 'disable_user_variant',
  900. 'known_themes' => explode(',', $modSettings['knownThemes']),
  901. 'enable_themes' => explode(',', $modSettings['enableThemes']),
  902. )
  903. );
  904. while ($row = $smcFunc['db_fetch_assoc']($request))
  905. {
  906. if (!isset($context['available_themes'][$row['id_theme']]))
  907. $context['available_themes'][$row['id_theme']] = array(
  908. 'id' => $row['id_theme'],
  909. 'selected' => $context['current_theme'] == $row['id_theme'],
  910. 'num_users' => 0
  911. );
  912. $context['available_themes'][$row['id_theme']][$row['variable']] = $row['value'];
  913. }
  914. $smcFunc['db_free_result']($request);
  915. }
  916. // Okay, this is a complicated problem: the default theme is 1, but they aren't allowed to access 1!
  917. if (!isset($context['available_themes'][$modSettings['theme_guests']]))
  918. {
  919. $context['available_themes'][0] = array(
  920. 'num_users' => 0
  921. );
  922. $guest_theme = 0;
  923. }
  924. else
  925. $guest_theme = $modSettings['theme_guests'];
  926. $request = $smcFunc['db_query']('', '
  927. SELECT id_theme, COUNT(*) AS the_count
  928. FROM {db_prefix}members
  929. GROUP BY id_theme
  930. ORDER BY id_theme DESC',
  931. array(
  932. )
  933. );
  934. while ($row = $smcFunc['db_fetch_assoc']($request))
  935. {
  936. // Figure out which theme it is they are REALLY using.
  937. if (!empty($modSettings['knownThemes']) && !in_array($row['id_theme'], explode(',',$modSettings['knownThemes'])))
  938. $row['id_theme'] = $guest_theme;
  939. elseif (empty($modSettings['theme_allow']))
  940. $row['id_theme'] = $guest_theme;
  941. if (isset($context['available_themes'][$row['id_theme']]))
  942. $context['available_themes'][$row['id_theme']]['num_users'] += $row['the_count'];
  943. else
  944. $context['available_themes'][$guest_theme]['num_users'] += $row['the_count'];
  945. }
  946. $smcFunc['db_free_result']($request);
  947. // Get any member variant preferences.
  948. $variant_preferences = array();
  949. if ($context['current_member'] > 0)
  950. {
  951. $request = $smcFunc['db_query']('', '
  952. SELECT id_theme, value
  953. FROM {db_prefix}themes
  954. WHERE variable = {string:theme_variant}
  955. AND id_member IN ({array_int:id_member})
  956. ORDER BY id_member ASC',
  957. array(
  958. 'theme_variant' => 'theme_variant',
  959. 'id_member' => isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'pick' ? array(-1, $context['current_member']) : array(-1),
  960. )
  961. );
  962. while ($row = $smcFunc['db_fetch_assoc']($request))
  963. $variant_preferences[$row['id_theme']] = $row['value'];
  964. $smcFunc['db_free_result']($request);
  965. }
  966. // Save the setting first.
  967. $current_images_url = $settings['images_url'];
  968. $current_theme_variants = !empty($settings['theme_variants']) ? $settings['theme_variants'] : array();
  969. foreach ($context['available_themes'] as $id_theme => $theme_data)
  970. {
  971. // Don't try to load the forum or board default theme's data... it doesn't have any!
  972. if ($id_theme == 0)
  973. continue;
  974. // The thumbnail needs the correct path.
  975. $settings['images_url'] = &$theme_data['images_url'];
  976. if (file_exists($theme_data['theme_dir'] . '/languages/Settings.' . $user_info['language'] . '.php'))
  977. include($theme_data['theme_dir'] . '/languages/Settings.' . $user_info['language'] . '.php');
  978. elseif (file_exists($theme_data['theme_dir'] . '/languages/Settings.' . $language . '.php'))
  979. include($theme_data['theme_dir'] . '/languages/Settings.' . $language . '.php');
  980. else
  981. {
  982. $txt['theme_thumbnail_href'] = $theme_data['images_url'] . '/thumbnail.png';
  983. $txt['theme_description'] = '';
  984. }
  985. $context['available_themes'][$id_theme]['thumbnail_href'] = $txt['theme_thumbnail_href'];
  986. $context['available_themes'][$id_theme]['description'] = $txt['theme_description'];
  987. // Are there any variants?
  988. if (file_exists($theme_data['theme_dir'] . '/index.template.php') && (empty($theme_data['disable_user_variant']) || allowedTo('admin_forum')))
  989. {
  990. $file_contents = implode('', file($theme_data['theme_dir'] . '/index.template.php'));
  991. if (preg_match('~\$settings\[\'theme_variants\'\]\s*=(.+?);~', $file_contents, $matches))
  992. {
  993. $settings['theme_variants'] = array();
  994. // Fill settings up.
  995. eval('global $settings;' . $matches[0]);
  996. if (!empty($settings['theme_variants']))
  997. {
  998. loadLanguage('Settings');
  999. $context['available_themes'][$id_theme]['variants'] = array();
  1000. foreach ($settings['theme_variants'] as $variant)
  1001. $context['available_themes'][$id_theme]['variants'][$variant] = array(
  1002. 'label' => isset($txt['variant_' . $variant]) ? $txt['variant_' . $variant] : $variant,
  1003. 'thumbnail' => !file_exists($theme_data['theme_dir'] . '/images/thumbnail.png') || file_exists($theme_data['theme_dir'] . '/images/thumbnail_' . $variant . '.png') ? $theme_data['images_url'] . '/thumbnail_' . $variant . '.png' : ($theme_data['images_url'] . '/thumbnail.png'),
  1004. );
  1005. $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]));
  1006. if (!isset($context['available_themes'][$id_theme]['variants'][$context['available_themes'][$id_theme]['selected_variant']]['thumbnail']))
  1007. $context['available_themes'][$id_theme]['selected_variant'] = $settings['theme_variants'][0];
  1008. $context['available_themes'][$id_theme]['thumbnail_href'] = $context['available_themes'][$id_theme]['variants'][$context['available_themes'][$id_theme]['selected_variant']]['thumbnail'];
  1009. // Allow themes to override the text.
  1010. $context['available_themes'][$id_theme]['pick_label'] = isset($txt['variant_pick']) ? $txt['variant_pick'] : $txt['theme_pick_variant'];
  1011. }
  1012. }
  1013. }
  1014. }
  1015. // Then return it.
  1016. $settings['images_url'] = $current_images_url;
  1017. $settings['theme_variants'] = $current_theme_variants;
  1018. // As long as we're not doing the default theme...
  1019. if (!isset($_REQUEST['u']) || $_REQUEST['u'] >= 0)
  1020. {
  1021. if ($guest_theme != 0)
  1022. $context['available_themes'][0] = $context['available_themes'][$guest_theme];
  1023. $context['available_themes'][0]['id'] = 0;
  1024. $context['available_themes'][0]['name'] = $txt['theme_forum_default'];
  1025. $context['available_themes'][0]['selected'] = $context['current_theme'] == 0;
  1026. $context['available_themes'][0]['description'] = $txt['theme_global_description'];
  1027. }
  1028. ksort($context['available_themes']);
  1029. $context['page_title'] = $txt['theme_pick'];
  1030. $context['sub_template'] = 'pick';
  1031. }
  1032. /**
  1033. * Installs new themes, calls the respective function according to the install type.
  1034. * - puts themes in $boardurl/Themes.
  1035. * - assumes the gzip has a root directory in it. (ie default.)
  1036. * Requires admin_forum.
  1037. * Accessed with ?action=admin;area=theme;sa=install.
  1038. */
  1039. function ThemeInstall()
  1040. {
  1041. global $sourcedir, $txt, $context, $boarddir, $boardurl;
  1042. global $themedir, $themeurl, $smcFunc;
  1043. checkSession('request');
  1044. isAllowedTo('admin_forum');
  1045. require_once($sourcedir . '/Subs-Package.php');
  1046. // Make it easier to change the path and url.
  1047. $themedir = $boarddir . '/Themes';
  1048. $themeurl = $boardurl . '/Themes';
  1049. loadTemplate('Themes');
  1050. $subActions = array(
  1051. 'file' => 'InstallFile',
  1052. 'copy' => 'InstallCopy',
  1053. 'dir' => 'InstallDir',
  1054. );
  1055. // Is there a function to call?
  1056. if (isset($_GET['do']) && !empty($_GET['do']) && isset($subActions[$_GET['do']]))
  1057. {
  1058. $action = $smcFunc['htmlspecialchars'](trim($_GET['do']));
  1059. // Got any info from the specific form?
  1060. if (!isset($_POST['save_'. $action]))
  1061. fatal_lang_error('theme_install_no_action', false);
  1062. validateToken('admin-t-'. $action);
  1063. // Hopefully the themes directory is writable, or we might have a problem.
  1064. if (!is_writable($themedir))
  1065. fatal_lang_error('theme_install_write_error', 'critical');
  1066. // Call the function and handle the result.
  1067. $result = $subActions[$action]();
  1068. // Everything went better than expected!
  1069. if (!empty($result))
  1070. {
  1071. $context['sub_template'] = 'installed';
  1072. $context['page_title'] = $txt['theme_installed'];
  1073. $context['installed_theme'] = $result;
  1074. }
  1075. }
  1076. // Nope, show a nice error.
  1077. else
  1078. fatal_lang_error('theme_install_no_action', false);
  1079. }
  1080. /**
  1081. * Installs a theme from a theme package.
  1082. *
  1083. * Stores the theme files on a temp dir, on success it renames the dir to the new theme's name. Ends execution with fatal_lang_error() on any error.
  1084. * @return array The newly created theme's info.
  1085. */
  1086. function InstallFile()
  1087. {
  1088. global $themedir, $themeurl, $context;
  1089. // Set a temp dir for dumping all required files on it.
  1090. $dirtemp = $themedir .'/temp';
  1091. // Create the temp dir.
  1092. mkdir($dirtemp, 0777);
  1093. // Hopefully the temp directory is writable, or we might have a problem.
  1094. if (!is_writable($dirtemp))
  1095. {
  1096. // Lets give it a try.
  1097. @chmod($dirtmp, '0755');
  1098. // How about now?
  1099. if (!is_writable($dirtemp))
  1100. fatal_lang_error('theme_install_write_error', 'critical');
  1101. }
  1102. // This happens when the admin session is gone and the user has to login again.
  1103. if (!isset($_FILES) || !isset($_FILES['theme_gz']) || empty($_FILES['theme_gz']))
  1104. redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
  1105. // Another error check layer, something went wrong with the upload.
  1106. if (isset($_FILES['theme_gz']['error']) && $_FILES['theme_gz']['error'] != 0)
  1107. fatal_lang_error('theme_install_error_file_'. $_FILES['theme_gz']['error'], false);
  1108. // Get the theme's name.
  1109. $name = strtok(basename($_FILES['theme_gz']['name']));
  1110. $name = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $name);
  1111. // Start setting some vars.
  1112. $context['to_install'] = array(
  1113. 'theme_dir' => $themedir . '/' . $name,
  1114. 'theme_url' => $themeurl . '/' . $name,
  1115. 'images_url' => $themeurl . '/' . $name . '/images',
  1116. 'name' => $name,
  1117. );
  1118. // Extract the file on the proper themes dir.
  1119. $extracted = read_tgz_file($_FILES['theme_gz']['tmp_name'], $dirtemp, false, true);
  1120. if ($extracted)
  1121. {
  1122. // Read its info form the XML file.
  1123. $theme_info = get_theme_info($dirtemp);
  1124. $context['to_install'] += $theme_info;
  1125. // Install the theme. theme_install() will take care of possible errors.
  1126. $context['to_install']['id'] = theme_install($context['to_install']);
  1127. // Rename the temp dir to the actual theme name.
  1128. rename($dirtemp, $context['to_install']['theme_dir']);
  1129. // return all the info.
  1130. return $context['to_install'];
  1131. }
  1132. else
  1133. fatal_lang_error('theme_install_error_title', false);
  1134. }
  1135. /**
  1136. * Makes a copy form the default theme, assigns a name for it and installs it.
  1137. *
  1138. * Creates a new .xml file containing all the theme's info.
  1139. * @return array The newly created theme's info.
  1140. */
  1141. function InstallCopy()
  1142. {
  1143. global $themedir, $themeurl, $settings, $smcFunc, $context;
  1144. global $forum_version;
  1145. // There's gotta be something to work with.
  1146. if (!isset($_REQUEST['copy']) || empty($_REQUEST['copy']))
  1147. fatal_lang_error('theme_install_error_title', false);
  1148. // Get a cleaner version.
  1149. $name = preg_replace('~[^A-Za-z0-9_\- ]~', '', $_REQUEST['copy']);
  1150. // Is there a theme already named like this?
  1151. if (file_exists($themedir .'/'. $name))
  1152. fatal_lang_error('theme_install_already_dir', false);
  1153. // This is a brand new theme so set all possible values.
  1154. $context['to_install'] = array(
  1155. 'theme_dir' => $themedir . '/' . $name,
  1156. 'theme_url' => $themeurl . '/' . $name,
  1157. 'name' => $name,
  1158. 'images_url' => $themeurl . '/' . $name . '/images',
  1159. 'version' => '1.0',
  1160. 'install_for' => '2.1 - 2.1.99, '. strtr($forum_version, array('SMF ' => '')),
  1161. 'based_on' => '',
  1162. 'based_on_dir' => $themedir . '/default',
  1163. );
  1164. // Create the specific dir.
  1165. umask(0);
  1166. mkdir($context['to_install']['theme_dir'], 0777);
  1167. // Buy some time.
  1168. @set_time_limit(600);
  1169. if (function_exists('apache_reset_timeout'))
  1170. @apache_reset_timeout();
  1171. // Create subdirectories for css and javascript files.
  1172. mkdir($context['to_install']['theme_dir'] . '/css', 0777);
  1173. mkdir($context['to_install']['theme_dir'] . '/scripts', 0777);
  1174. // Copy over the default non-theme files.
  1175. $to_copy = array('/index.php', '/index.template.php', '/css/index.css', '/css/rtl.css', '/css/admin.css', '/scripts/theme.js');
  1176. foreach ($to_copy as $file)
  1177. {
  1178. copy($settings['default_theme_dir'] . $file, $context['to_install']['theme_dir'] . $file);
  1179. @chmod($context['to_install']['theme_dir'] . $file, 0777);
  1180. }
  1181. // And now the entire images directory!
  1182. copytree($settings['default_theme_dir'] . '/images', $context['to_install']['theme_dir'] . '/images');
  1183. package_flush_cache();
  1184. // Lets get some data for the new theme.
  1185. $request = $smcFunc['db_query']('', '
  1186. SELECT variable, value
  1187. FROM {db_prefix}themes
  1188. WHERE variable IN ({string:theme_templates}, {string:theme_layers})
  1189. AND id_member = {int:no_member}
  1190. AND id_theme = {int:default_theme}',
  1191. array(
  1192. 'no_member' => 0,
  1193. 'default_theme' => 1,
  1194. 'theme_templates' => 'theme_templates',
  1195. 'theme_layers' => 'theme_layers',
  1196. )
  1197. );
  1198. while ($row = $smcFunc['db_fetch_assoc']($request))
  1199. {
  1200. if ($row['variable'] == 'theme_templates')
  1201. $theme_templates = $row['value'];
  1202. elseif ($row['variable'] == 'theme_layers')
  1203. $theme_layers = $row['value'];
  1204. else
  1205. continue;
  1206. }
  1207. $smcFunc['db_free_result']($request);
  1208. $context['to_install'] += array(
  1209. 'theme_layers' => empty($theme_layers) ? 'html,body' : $theme_layers,
  1210. 'theme_templates' => empty($theme_templates) ? 'index' : $theme_templates,
  1211. );
  1212. // Lets add a theme_info.xml to this theme.
  1213. $xml_info = '<' . '?xml version="1.0"?' . '>
  1214. <theme-info xmlns="http://www.simplemachines.org/xml/theme-info" xmlns:smf="http://www.simplemachines.org/">
  1215. <!-- For the id, always use something unique - put your name, a colon, and then the package name. -->
  1216. <id>smf:' . $smcFunc['strtolower']($context['to_install']['name']) . '</id>
  1217. <!-- The theme\'s version, please try to use semantic versioning. -->
  1218. <version>1.0</version>
  1219. <!-- Install for, the SMF versions this theme was designed for. Uses the same wildcards used in the packager manager. This field is mandatory. -->
  1220. <install for="'. $context['to_install']['install_for'] .'" />
  1221. <!-- Theme name, used purely for aesthetics. -->
  1222. <name>' . $context['to_install']['name'] . '</name>
  1223. <!-- Author: your email address or contact information. The name attribute is optional. -->
  1224. <author name="Simple Machines">[email protected]</author>
  1225. <!-- Website... where to get updates and more information. -->
  1226. <website>http://www.simplemachines.org/</website>
  1227. <!-- Template layers to use, defaults to "html,body". -->
  1228. <layers>' . $context['to_install']['theme_layers'] . '</layers>
  1229. <!-- Templates to load on startup. Default is "index". -->
  1230. <templates>' . $context['to_install']['theme_templates'] . '</templates>
  1231. <!-- Base this theme off another? Default is blank, or no. It could be "default". -->
  1232. <based-on></based-on>
  1233. </theme-info>';
  1234. // Now write it.
  1235. $fp = @fopen($context['to_install']['theme_dir'] . '/theme_info.xml', 'w+');
  1236. if ($fp)
  1237. {
  1238. fwrite($fp, $xml_info);
  1239. fclose($fp);
  1240. }
  1241. // Install the theme. theme_install() will take care of possible errors.
  1242. $context['to_install']['id'] = theme_install($context['to_install']);
  1243. // return the info.
  1244. return $context['to_install'];
  1245. }
  1246. /**
  1247. * Install a theme from a specific dir
  1248. *
  1249. * Assumes the dir is located on the main Themes dir. Ends execution with fatal_lang_error() on any error.
  1250. * @return array The newly created theme's info.
  1251. */
  1252. function InstallDir()
  1253. {
  1254. global $themedir, $themeurl, $context;
  1255. // Cannot use the theme dir as a theme dir.
  1256. if (!isset($_REQUEST['theme_dir']) || empty($_REQUEST['theme_dir']) || rtrim(realpath($_REQUEST['theme_dir']), '/\\') == realpath($themedir))
  1257. fatal_lang_error('theme_install_invalid_dir', false);
  1258. // Check is there is "something" on the dir.
  1259. elseif (!is_dir($_REQUEST['theme_dir']) || !file_exists($_REQUEST['theme_dir'] . '/theme_info.xml'))
  1260. fatal_lang_error('theme_install_error', false);
  1261. $name = basename($_REQUEST['theme_dir']);
  1262. $name = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $name);
  1263. // All good! set some needed vars.
  1264. $context['to_install'] = array(
  1265. 'dir' => $_REQUEST['theme_dir'],
  1266. 'theme_url' => $themeurl . '/' . $name,
  1267. 'name' => $name,
  1268. 'images_url' => $themeurl . '/' . $name . '/images',
  1269. );
  1270. // Read its info form the XML file.
  1271. $theme_info = get_theme_info($context['to_install']['theme_dir']);
  1272. $context['to_install'] += $theme_info;
  1273. // Install the theme. theme_install() will take care of possible errors.
  1274. $context['to_install']['id'] = theme_install($context['to_install']);
  1275. // return the info.
  1276. return $context['to_install'];
  1277. }
  1278. /**
  1279. * Possibly the simplest and best example of how to use the template system.
  1280. * - allows the theme to take care of actions.
  1281. * - happens if $settings['catch_action'] is set and action isn't found
  1282. * in the action array.
  1283. * - can use a template, layers, sub_template, filename, and/or function.
  1284. * @todo look at this
  1285. */
  1286. function WrapAction()
  1287. {
  1288. global $context, $settings, $sourcedir;
  1289. // Load any necessary template(s)?
  1290. if (isset($settings['catch_action']['template']))
  1291. {
  1292. // Load both the template and language file. (but don't fret if the language file isn't there...)
  1293. loadTemplate($settings['catch_action']['template']);
  1294. loadLanguage($settings['catch_action']['template'], '', false);
  1295. }
  1296. // Any special layers?
  1297. if (isset($settings['catch_action']['layers']))
  1298. $context['template_layers'] = $settings['catch_action']['layers'];
  1299. // Just call a function?
  1300. if (isset($settings['catch_action']['function']))
  1301. {
  1302. if (isset($settings['catch_action']['filename']))
  1303. template_include($sourcedir . '/' . $settings['catch_action']['filename'], true);
  1304. $settings['catch_action']['function']();
  1305. }
  1306. // And finally, the main sub template ;).
  1307. elseif (isset($settings['catch_action']['sub_template']))
  1308. $context['sub_template'] = $settings['catch_action']['sub_template'];
  1309. }
  1310. /**
  1311. * Set an option via javascript.
  1312. * - sets a theme option without outputting anything.
  1313. * - can be used with javascript, via a dummy image... (which doesn't require
  1314. * the page to reload.)
  1315. * - requires someone who is logged in.
  1316. * - accessed via ?action=jsoption;var=variable;val=value;session_var=sess_id.
  1317. * - does not log access to the Who's Online log. (in index.php..)
  1318. */
  1319. function SetJavaScript()
  1320. {
  1321. global $settings, $user_info, $smcFunc, $options;
  1322. // Check the session id.
  1323. checkSession('get');
  1324. // This good-for-nothing pixel is being used to keep the session alive.
  1325. if (empty($_GET['var']) || !isset($_GET['val']))
  1326. redirectexit($settings['images_url'] . '/blank.png');
  1327. // Sorry, guests can't go any further than this..
  1328. if ($user_info['is_guest'] || $user_info['id'] == 0)
  1329. obExit(false);
  1330. $reservedVars = array(
  1331. 'actual_theme_url',
  1332. 'actual_images_url',
  1333. 'base_theme_dir',
  1334. 'base_theme_url',
  1335. 'default_images_url',
  1336. 'default_theme_dir',
  1337. 'default_theme_url',
  1338. 'default_template',
  1339. 'images_url',
  1340. 'number_recent_posts',
  1341. 'smiley_sets_default',
  1342. 'theme_dir',
  1343. 'theme_id',
  1344. 'theme_layers',
  1345. 'theme_templates',
  1346. 'theme_url',
  1347. 'name',
  1348. );
  1349. // Can't change reserved vars.
  1350. if (in_array(strtolower($_GET['var']), $reservedVars))
  1351. redirectexit($settings['images_url'] . '/blank.png');
  1352. // Use a specific theme?
  1353. if (isset($_GET['th']) || isset($_GET['id']))
  1354. {
  1355. // Invalidate the current themes cache too.
  1356. cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 60);
  1357. $settings['theme_id'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
  1358. }
  1359. // If this is the admin preferences the passed value will just be an element of it.
  1360. if ($_GET['var'] == 'admin_preferences')
  1361. {
  1362. $options['admin_preferences'] = !empty($options['admin_preferences']) ? unserialize($options['admin_preferences']) : array();
  1363. // New thingy...
  1364. if (isset($_GET['admin_key']) && strlen($_GET['admin_key']) < 5)
  1365. $options['admin_preferences'][$_GET['admin_key']] = $_GET['val'];
  1366. // Change the value to be something nice,
  1367. $_GET['val'] = serialize($options['admin_preferences']);
  1368. }
  1369. // Update the option.
  1370. $smcFunc['db_insert']('replace',
  1371. '{db_prefix}themes',
  1372. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  1373. array($settings['theme_id'], $user_info['id'], $_GET['var'], is_array($_GET['val']) ? implode(',', $_GET['val']) : $_GET['val']),
  1374. array('id_theme', 'id_member', 'variable')
  1375. );
  1376. cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 60);
  1377. // Don't output anything...
  1378. redirectexit($settings['images_url'] . '/blank.png');
  1379. }
  1380. /**
  1381. * Shows an interface for editing the templates.
  1382. * - uses the Themes template and edit_template/edit_style sub template.
  1383. * - accessed via ?action=admin;area=theme;sa=edit
  1384. */
  1385. function EditTheme()
  1386. {
  1387. global $context, $settings, $scripturl, $boarddir, $smcFunc;
  1388. // @todo Should this be removed?
  1389. if (isset($_REQUEST['preview']))
  1390. die('die() with fire');
  1391. isAllowedTo('admin_forum');
  1392. loadTemplate('Themes');
  1393. $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) @$_GET['id'];
  1394. if (empty($_GET['th']))
  1395. {
  1396. get_all_themes();
  1397. foreach ($context['themes'] as $key => $theme)
  1398. {
  1399. // There has to be a Settings template!
  1400. if (!file_exists($theme['theme_dir'] . '/index.template.php') && !file_exists($theme['theme_dir'] . '/css/index.css'))
  1401. unset($context['themes'][$key]);
  1402. else
  1403. $context['themes'][$key]['can_edit_style'] = file_exists($theme['theme_dir'] . '/css/index.css');
  1404. }
  1405. $context['sub_template'] = 'edit_list';
  1406. return 'no_themes';
  1407. }
  1408. $context['session_error'] = false;
  1409. // Get the directory of the theme we are editing.
  1410. $currentTheme = get_single_theme($_GET['th']);
  1411. $context['theme_id'] = $currentTheme['id'];
  1412. if (!file_exists($currentTheme['theme_dir'] . '/index.template.php') && !file_exists($currentTheme['theme_dir'] . '/css/index.css'))
  1413. fatal_lang_error('theme_edit_missing', false);
  1414. if (!isset($_REQUEST['filename']))
  1415. {
  1416. if (isset($_GET['directory']))
  1417. {
  1418. if (substr($_GET['directory'], 0, 1) == '.')
  1419. $_GET['directory'] = '';
  1420. else
  1421. {
  1422. $_GET['directory'] = preg_replace(array('~^[\./\\:\0\n\r]+~', '~[\\\\]~', '~/[\./]+~'), array('', '/', '/'), $_GET['directory']);
  1423. $temp = realpath($currentTheme['theme_dir'] . '/' . $_GET['directory']);
  1424. if (empty($temp) || substr($temp, 0, strlen(realpath($theme_dir))) != realpath($theme_dir))
  1425. $_GET['directory'] = '';
  1426. }
  1427. }
  1428. if (isset($_GET['directory']) && $_GET['directory'] != '')
  1429. {
  1430. $context['theme_files'] = get_file_listing($currentTheme['theme_dir'] . '/' . $_GET['directory'], $_GET['directory'] . '/');
  1431. $temp = dirname($_GET['directory']);
  1432. array_unshift($context['theme_files'], array(
  1433. 'filename' => $temp == '.' || $temp == '' ? '/ (..)' : $temp . ' (..)',
  1434. 'is_writable' => is_writable($currentTheme['theme_dir'] . '/' . $temp),
  1435. 'is_directory' => true,
  1436. 'is_template' => false,
  1437. 'is_image' => false,
  1438. 'is_editable' => false,
  1439. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . $temp,
  1440. 'size' => '',
  1441. ));
  1442. }
  1443. else
  1444. $context['theme_files'] = get_file_listing($currentTheme['theme_dir'], '');
  1445. $context['sub_template'] = 'edit_browse';
  1446. return;
  1447. }
  1448. else
  1449. {
  1450. if (substr($_REQUEST['filename'], 0, 1) == '.')
  1451. $_REQUEST['filename'] = '';
  1452. else
  1453. {
  1454. $_REQUEST['filename'] = preg_replace(array('~^[\./\\:\0\n\r]+~', '~[\\\\]~', '~/[\./]+~'), array('', '/', '/'), $_REQUEST['filename']);
  1455. $temp = realpath($currentTheme['theme_dir'] . '/' . $_REQUEST['filename']);
  1456. if (empty($temp) || substr($temp, 0, strlen(realpath($theme_dir))) != realpath($currentTheme['theme_dir']))
  1457. $_REQUEST['filename'] = '';
  1458. }
  1459. if (empty($_REQUEST['filename']))
  1460. fatal_lang_error('theme_edit_missing', false);
  1461. }
  1462. if (isset($_POST['save']))
  1463. {
  1464. if (checkSession('post', '', false) == '' && validateToken('admin-te-' . md5($_GET['th'] . '-' . $_REQUEST['filename']), 'post', false) == true)
  1465. {
  1466. if (is_array($_POST['entire_file']))
  1467. $_POST['entire_file'] = implode("\n", $_POST['entire_file']);
  1468. $_POST['entire_file'] = rtrim(strtr($_POST['entire_file'], array("\r" => '', ' ' => "\t")));
  1469. // Check for a parse error!
  1470. if (substr($_REQUEST['filename'], -13) == '.template.php' && is_writable($currentTheme['theme_dir']) && ini_get('display_errors'))
  1471. {
  1472. $fp = fopen($currentTheme['theme_dir'] . '/tmp_' . session_id() . '.php', 'w');
  1473. fwrite($fp, $_POST['entire_file']);
  1474. fclose($fp);
  1475. $error = @file_get_contents($currentTheme['theme_url'] . '/tmp_' . session_id() . '.php');
  1476. if (preg_match('~ <b>(\d+)</b><br( /)?' . '>$~i', $error) != 0)
  1477. $error_file = $currentTheme['theme_dir'] . '/tmp_' . session_id() . '.php';
  1478. else
  1479. unlink($currentTheme['theme_dir'] . '/tmp_' . session_id() . '.php');
  1480. }
  1481. if (!isset($error_file))
  1482. {
  1483. $fp = fopen($currentTheme['theme_dir'] . '/' . $_REQUEST['filename'], 'w');
  1484. fwrite($fp, $_POST['entire_file']);
  1485. fclose($fp);
  1486. redirectexit('action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . dirname($_REQUEST['filename']));
  1487. }
  1488. }
  1489. // Session timed out.
  1490. else
  1491. {
  1492. loadLanguage('Errors');
  1493. $context['session_error'] = true;
  1494. $context['sub_template'] = 'edit_file';
  1495. // Recycle the submitted data.
  1496. if (is_array($_POST['entire_file']))
  1497. $context['entire_file'] = $smcFunc['htmlspecialchars'](implode("\n", $_POST['entire_file']));
  1498. else
  1499. $context['entire_file'] = $smcFunc['htmlspecialchars']($_POST['entire_file']);
  1500. $context['edit_filename'] = $smcFunc['htmlspecialchars']($_POST['filename']);
  1501. // You were able to submit it, so it's reasonable to assume you are allowed to save.
  1502. $context['allow_save'] = true;
  1503. // Re-create the token so that it can be used
  1504. createToken('admin-te-' . md5($_GET['th'] . '-' . $_REQUEST['filename']));
  1505. return;
  1506. }
  1507. }
  1508. $context['allow_save'] = is_writable($currentTheme['theme_dir'] . '/' . $_REQUEST['filename']);
  1509. $context['allow_save_filename'] = strtr($currentTheme['theme_dir'] . '/' . $_REQUEST['filename'], array($boarddir => '...'));
  1510. $context['edit_filename'] = $smcFunc['htmlspecialchars']($_REQUEST['filename']);
  1511. if (substr($_REQUEST['filename'], -4) == '.css')
  1512. {
  1513. $context['sub_template'] = 'edit_style';
  1514. $context['entire_file'] = $smcFunc['htmlspecialchars'](strtr(file_get_contents($currentTheme['theme_dir'] . '/' . $_REQUEST['filename']), array("\t" => ' ')));
  1515. }
  1516. elseif (substr($_REQUEST['filename'], -13) == '.template.php')
  1517. {
  1518. $context['sub_template'] = 'edit_template';
  1519. if (!isset($error_file))
  1520. $file_data = file($currentTheme['theme_dir'] . '/' . $_REQUEST['filename']);
  1521. else
  1522. {
  1523. if (preg_match('~(<b>.+?</b>:.+?<b>).+?(</b>.+?<b>\d+</b>)<br( /)?' . '>$~i', $error, $match) != 0)
  1524. $context['parse_error'] = $match[1] . $_REQUEST['filename'] . $match[2];
  1525. $file_data = file($error_file);
  1526. unlink($error_file);
  1527. }
  1528. $j = 0;
  1529. $context['file_parts'] = array(array('lines' => 0, 'line' => 1, 'data' => ''));
  1530. for ($i = 0, $n = count($file_data); $i < $n; $i++)
  1531. {
  1532. if (isset($file_data[$i + 1]) && substr($file_data[$i + 1], 0, 9) == 'function ')
  1533. {
  1534. // Try to format the functions a little nicer...
  1535. $context['file_parts'][$j]['data'] = trim($context['file_parts'][$j]['data']) . "\n";
  1536. if (empty($context['file_parts'][$j]['lines']))
  1537. unset($context['file_parts'][$j]);
  1538. $context['file_parts'][++$j] = array('lines' => 0, 'line' => $i + 1, 'data' => '');
  1539. }
  1540. $context['file_parts'][$j]['lines']++;
  1541. $context['file_parts'][$j]['data'] .= $smcFunc['htmlspecialchars'](strtr($file_data[$i], array("\t" => ' ')));
  1542. }
  1543. $context['entire_file'] = $smcFunc['htmlspecialchars'](strtr(implode('', $file_data), array("\t" => ' ')));
  1544. }
  1545. else
  1546. {
  1547. $context['sub_template'] = 'edit_file';
  1548. $context['entire_file'] = $smcFunc['htmlspecialchars'](strtr(file_get_contents($theme_dir . '/' . $_REQUEST['filename']), array("\t" => ' ')));
  1549. }
  1550. // Create a special token to allow editing of multiple files.
  1551. createToken('admin-te-' . md5($_GET['th'] . '-' . $_REQUEST['filename']));
  1552. }
  1553. /**
  1554. * Makes a copy of a template file in a new location
  1555. * @uses Themes template, copy_template sub-template.
  1556. */
  1557. function CopyTemplate()
  1558. {
  1559. global $context, $settings, $smcFunc;
  1560. isAllowedTo('admin_forum');
  1561. loadTemplate('Themes');
  1562. $context[$context['admin_menu_name']]['current_subsection'] = 'edit';
  1563. $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
  1564. if (empty($_GET['th']))
  1565. fatal_lang_error('theme_install_invalid_id');
  1566. // Get the theme info.
  1567. $theme = get_single_theme($_GET['th']);
  1568. $context['theme_id'] = $theme['id'];
  1569. if (isset($_REQUEST['template']) && preg_match('~[\./\\\\:\0]~', $_REQUEST['template']) == 0)
  1570. {
  1571. if (file_exists($settings['default_theme_dir'] . '/' . $_REQUEST['template'] . '.template.php'))
  1572. $filename = $settings['default_theme_dir'] . '/' . $_REQUEST['template'] . '.template.php';
  1573. else
  1574. fatal_lang_error('no_access', false);
  1575. $fp = fopen($theme['theme_dir'] . '/' . $_REQUEST['template'] . '.template.php', 'w');
  1576. fwrite($fp, file_get_contents($filename));
  1577. fclose($fp);
  1578. redirectexit('action=admin;area=theme;th=' . $context['theme_id'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=copy');
  1579. }
  1580. elseif (isset($_REQUEST['lang_file']) && preg_match('~^[^\./\\\\:\0]\.[^\./\\\\:\0]$~', $_REQUEST['lang_file']) != 0)
  1581. {
  1582. if (file_exists($settings['default_theme_dir'] . '/languages/' . $_REQUEST['template'] . '.php'))
  1583. $filename = $settings['default_theme_dir'] . '/languages/' . $_REQUEST['template'] . '.php';
  1584. else
  1585. fatal_lang_error('no_access', false);
  1586. $fp = fopen($theme['theme_dir'] . '/languages/' . $_REQUEST['lang_file'] . '.php', 'w');
  1587. fwrite($fp, file_get_contents($filename));
  1588. fclose($fp);
  1589. redirectexit('action=admin;area=theme;th=' . $context['theme_id'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=copy');
  1590. }
  1591. $templates = array();
  1592. $lang_files = array();
  1593. $dir = dir($settings['default_theme_dir']);
  1594. while ($entry = $dir->read())
  1595. {
  1596. if (substr($entry, -13) == '.template.php')
  1597. $templates[] = substr($entry, 0, -13);
  1598. }
  1599. $dir->close();
  1600. $dir = dir($settings['default_theme_dir'] . '/languages');
  1601. while ($entry = $dir->read())
  1602. {
  1603. if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches))
  1604. $lang_files[] = $matches[1];
  1605. }
  1606. $dir->close();
  1607. natcasesort($templates);
  1608. natcasesort($lang_files);
  1609. $context['available_templates'] = array();
  1610. foreach ($templates as $template)
  1611. $context['available_templates'][$template] = array(
  1612. 'filename' => $template . '.template.php',
  1613. 'value' => $template,
  1614. 'already_exists' => false,
  1615. 'can_copy' => is_writable($theme['theme_dir']),
  1616. );
  1617. $context['available_language_files'] = array();
  1618. foreach ($lang_files as $file)
  1619. $context['available_language_files'][$file] = array(
  1620. 'filename' => $file . '.php',
  1621. 'value' => $file,
  1622. 'already_exists' => false,
  1623. 'can_copy' => file_exists($theme['theme_dir'] . '/languages') ? is_writable($theme['theme_dir'] . '/languages') : is_writable($theme['theme_dir']),
  1624. );
  1625. $dir = dir($theme['theme_dir']);
  1626. while ($entry = $dir->read())
  1627. {
  1628. if (substr($entry, -13) == '.template.php' && isset($context['available_templates'][substr($entry, 0, -13)]))
  1629. {
  1630. $context['available_templates'][substr($entry, 0, -13)]['already_exists'] = true;
  1631. $context['available_templates'][substr($entry, 0, -13)]['can_copy'] = is_writable($theme['theme_dir'] . '/' . $entry);
  1632. }
  1633. }
  1634. $dir->close();
  1635. if (file_exists($theme['theme_dir'] . '/languages'))
  1636. {
  1637. $dir = dir($theme['theme_dir'] . '/languages');
  1638. while ($entry = $dir->read())
  1639. {
  1640. if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches) && isset($context['available_language_files'][$matches[1]]))
  1641. {
  1642. $context['available_language_files'][$matches[1]]['already_exists'] = true;
  1643. $context['available_language_files'][$matches[1]]['can_copy'] = is_writable($theme['theme_dir'] . '/languages/' . $entry);
  1644. }
  1645. }
  1646. $dir->close();
  1647. }
  1648. $context['sub_template'] = 'copy_template';
  1649. }
  1650. ?>