Themes.php 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103
  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();
  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();
  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 ($_GET['th'] == 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']);
  709. }
  710. function EnableTheme()
  711. {
  712. checkSession('get');
  713. isAllowedTo('admin_forum');
  714. validateToken('admin-tre', 'request');
  715. $enable = '1';
  716. // The theme's ID must be an integer.
  717. $themeID = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
  718. // Are we disabling it?
  719. if (isset($_GET['disabled']))
  720. $enable = '0';
  721. // Make the query.
  722. $smcFunc['db_query']('', '
  723. UPDATE {db_prefix}themes
  724. SET value = {string:enable}
  725. WHERE id_theme = {int:theme}',
  726. array(
  727. 'enable' => $enable,
  728. 'theme' => $themeID,
  729. )
  730. );
  731. redirectexit('action=admin;area=theme;sa=list;' . $context['session_var'] . '=' . $context['session_id']);
  732. }
  733. /**
  734. * Choose a theme from a list.
  735. * allows an user or administrator to pick a new theme with an interface.
  736. * - can edit everyone's (u = 0), guests' (u = -1), or a specific user's.
  737. * - uses the Themes template. (pick sub template.)
  738. * - accessed with ?action=admin;area=theme;sa=pick.
  739. * @todo thought so... Might be better to split this file in ManageThemes and Themes,
  740. * with centralized admin permissions on ManageThemes.
  741. */
  742. function PickTheme()
  743. {
  744. global $txt, $context, $modSettings, $user_info, $language, $smcFunc, $settings, $scripturl;
  745. loadLanguage('Profile');
  746. loadTemplate('Themes');
  747. // Build the link tree.
  748. $context['linktree'][] = array(
  749. 'url' => $scripturl . '?action=theme;sa=pick;u=' . (!empty($_REQUEST['u']) ? (int) $_REQUEST['u'] : 0),
  750. 'name' => $txt['theme_pick'],
  751. );
  752. $context['default_theme_id'] = $modSettings['theme_default'];
  753. $_SESSION['id_theme'] = 0;
  754. if (isset($_GET['id']))
  755. $_GET['th'] = $_GET['id'];
  756. // Saving a variant cause JS doesn't work - pretend it did ;)
  757. if (isset($_POST['save']))
  758. {
  759. // Which theme?
  760. foreach ($_POST['save'] as $k => $v)
  761. $_GET['th'] = (int) $k;
  762. if (isset($_POST['vrt'][$k]))
  763. $_GET['vrt'] = $_POST['vrt'][$k];
  764. }
  765. // Have we made a desicion, or are we just browsing?
  766. if (isset($_GET['th']))
  767. {
  768. checkSession('get');
  769. $_GET['th'] = (int) $_GET['th'];
  770. // Save for this user.
  771. if (!isset($_REQUEST['u']) || !allowedTo('admin_forum'))
  772. {
  773. updateMemberData($user_info['id'], array('id_theme' => (int) $_GET['th']));
  774. // A variants to save for the user?
  775. if (!empty($_GET['vrt']))
  776. {
  777. $smcFunc['db_insert']('replace',
  778. '{db_prefix}themes',
  779. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  780. array($_GET['th'], $user_info['id'], 'theme_variant', $_GET['vrt']),
  781. array('id_theme', 'id_member', 'variable')
  782. );
  783. cache_put_data('theme_settings-' . $_GET['th'] . ':' . $user_info['id'], null, 90);
  784. $_SESSION['id_variant'] = 0;
  785. }
  786. redirectexit('action=profile;area=theme');
  787. }
  788. // If changing members or guests - and there's a variant - assume changing default variant.
  789. if (!empty($_GET['vrt']) && ($_REQUEST['u'] == '0' || $_REQUEST['u'] == '-1'))
  790. {
  791. $smcFunc['db_insert']('replace',
  792. '{db_prefix}themes',
  793. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  794. array($_GET['th'], 0, 'default_variant', $_GET['vrt']),
  795. array('id_theme', 'id_member', 'variable')
  796. );
  797. // Make it obvious that it's changed
  798. cache_put_data('theme_settings-' . $_GET['th'], null, 90);
  799. }
  800. // For everyone.
  801. if ($_REQUEST['u'] == '0')
  802. {
  803. updateMemberData(null, array('id_theme' => (int) $_GET['th']));
  804. // Remove any custom variants.
  805. if (!empty($_GET['vrt']))
  806. {
  807. $smcFunc['db_query']('', '
  808. DELETE FROM {db_prefix}themes
  809. WHERE id_theme = {int:current_theme}
  810. AND variable = {string:theme_variant}',
  811. array(
  812. 'current_theme' => (int) $_GET['th'],
  813. 'theme_variant' => 'theme_variant',
  814. )
  815. );
  816. }
  817. redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
  818. }
  819. // Change the default/guest theme.
  820. elseif ($_REQUEST['u'] == '-1')
  821. {
  822. updateSettings(array('theme_guests' => (int) $_GET['th']));
  823. redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
  824. }
  825. // Change a specific member's theme.
  826. else
  827. {
  828. // The forum's default theme is always 0 and we
  829. if (isset($_GET['th']) && $_GET['th'] == 0)
  830. $_GET['th'] = $modSettings['theme_guests'];
  831. updateMemberData((int) $_REQUEST['u'], array('id_theme' => (int) $_GET['th']));
  832. if (!empty($_GET['vrt']))
  833. {
  834. $smcFunc['db_insert']('replace',
  835. '{db_prefix}themes',
  836. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  837. array($_GET['th'], (int) $_REQUEST['u'], 'theme_variant', $_GET['vrt']),
  838. array('id_theme', 'id_member', 'variable')
  839. );
  840. cache_put_data('theme_settings-' . $_GET['th'] . ':' . (int) $_REQUEST['u'], null, 90);
  841. if ($user_info['id'] == $_REQUEST['u'])
  842. $_SESSION['id_variant'] = 0;
  843. }
  844. redirectexit('action=profile;u=' . (int) $_REQUEST['u'] . ';area=theme');
  845. }
  846. }
  847. // Figure out who the member of the minute is, and what theme they've chosen.
  848. if (!isset($_REQUEST['u']) || !allowedTo('admin_forum'))
  849. {
  850. $context['current_member'] = $user_info['id'];
  851. $context['current_theme'] = $user_info['theme'];
  852. }
  853. // Everyone can't chose just one.
  854. elseif ($_REQUEST['u'] == '0')
  855. {
  856. $context['current_member'] = 0;
  857. $context['current_theme'] = 0;
  858. }
  859. // Guests and such...
  860. elseif ($_REQUEST['u'] == '-1')
  861. {
  862. $context['current_member'] = -1;
  863. $context['current_theme'] = $modSettings['theme_guests'];
  864. }
  865. // Someones else :P.
  866. else
  867. {
  868. $context['current_member'] = (int) $_REQUEST['u'];
  869. $request = $smcFunc['db_query']('', '
  870. SELECT id_theme
  871. FROM {db_prefix}members
  872. WHERE id_member = {int:current_member}
  873. LIMIT 1',
  874. array(
  875. 'current_member' => $context['current_member'],
  876. )
  877. );
  878. list ($context['current_theme']) = $smcFunc['db_fetch_row']($request);
  879. $smcFunc['db_free_result']($request);
  880. }
  881. // Get the theme name and descriptions.
  882. $context['available_themes'] = array();
  883. if (!empty($modSettings['knownThemes']))
  884. {
  885. $request = $smcFunc['db_query']('', '
  886. SELECT id_theme, variable, value
  887. FROM {db_prefix}themes
  888. WHERE variable IN ({string:name}, {string:theme_url}, {string:theme_dir}, {string:images_url}, {string:disable_user_variant})' . (!allowedTo('admin_forum') ? '
  889. AND id_theme IN ({array_string:known_themes})' : '') . '
  890. AND id_theme != {int:default_theme}
  891. AND id_member = {int:no_member}',
  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. )
  902. );
  903. while ($row = $smcFunc['db_fetch_assoc']($request))
  904. {
  905. if (!isset($context['available_themes'][$row['id_theme']]))
  906. $context['available_themes'][$row['id_theme']] = array(
  907. 'id' => $row['id_theme'],
  908. 'selected' => $context['current_theme'] == $row['id_theme'],
  909. 'num_users' => 0
  910. );
  911. $context['available_themes'][$row['id_theme']][$row['variable']] = $row['value'];
  912. }
  913. $smcFunc['db_free_result']($request);
  914. }
  915. // Okay, this is a complicated problem: the default theme is 1, but they aren't allowed to access 1!
  916. if (!isset($context['available_themes'][$modSettings['theme_guests']]))
  917. {
  918. $context['available_themes'][0] = array(
  919. 'num_users' => 0
  920. );
  921. $guest_theme = 0;
  922. }
  923. else
  924. $guest_theme = $modSettings['theme_guests'];
  925. $request = $smcFunc['db_query']('', '
  926. SELECT id_theme, COUNT(*) AS the_count
  927. FROM {db_prefix}members
  928. GROUP BY id_theme
  929. ORDER BY id_theme DESC',
  930. array(
  931. )
  932. );
  933. while ($row = $smcFunc['db_fetch_assoc']($request))
  934. {
  935. // Figure out which theme it is they are REALLY using.
  936. if (!empty($modSettings['knownThemes']) && !in_array($row['id_theme'], explode(',',$modSettings['knownThemes'])))
  937. $row['id_theme'] = $guest_theme;
  938. elseif (empty($modSettings['theme_allow']))
  939. $row['id_theme'] = $guest_theme;
  940. if (isset($context['available_themes'][$row['id_theme']]))
  941. $context['available_themes'][$row['id_theme']]['num_users'] += $row['the_count'];
  942. else
  943. $context['available_themes'][$guest_theme]['num_users'] += $row['the_count'];
  944. }
  945. $smcFunc['db_free_result']($request);
  946. // Get any member variant preferences.
  947. $variant_preferences = array();
  948. if ($context['current_member'] > 0)
  949. {
  950. $request = $smcFunc['db_query']('', '
  951. SELECT id_theme, value
  952. FROM {db_prefix}themes
  953. WHERE variable = {string:theme_variant}
  954. AND id_member IN ({array_int:id_member})
  955. ORDER BY id_member ASC',
  956. array(
  957. 'theme_variant' => 'theme_variant',
  958. 'id_member' => isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'pick' ? array(-1, $context['current_member']) : array(-1),
  959. )
  960. );
  961. while ($row = $smcFunc['db_fetch_assoc']($request))
  962. $variant_preferences[$row['id_theme']] = $row['value'];
  963. $smcFunc['db_free_result']($request);
  964. }
  965. // Save the setting first.
  966. $current_images_url = $settings['images_url'];
  967. $current_theme_variants = !empty($settings['theme_variants']) ? $settings['theme_variants'] : array();
  968. foreach ($context['available_themes'] as $id_theme => $theme_data)
  969. {
  970. // Don't try to load the forum or board default theme's data... it doesn't have any!
  971. if ($id_theme == 0)
  972. continue;
  973. // The thumbnail needs the correct path.
  974. $settings['images_url'] = &$theme_data['images_url'];
  975. if (file_exists($theme_data['theme_dir'] . '/languages/Settings.' . $user_info['language'] . '.php'))
  976. include($theme_data['theme_dir'] . '/languages/Settings.' . $user_info['language'] . '.php');
  977. elseif (file_exists($theme_data['theme_dir'] . '/languages/Settings.' . $language . '.php'))
  978. include($theme_data['theme_dir'] . '/languages/Settings.' . $language . '.php');
  979. else
  980. {
  981. $txt['theme_thumbnail_href'] = $theme_data['images_url'] . '/thumbnail.png';
  982. $txt['theme_description'] = '';
  983. }
  984. $context['available_themes'][$id_theme]['thumbnail_href'] = $txt['theme_thumbnail_href'];
  985. $context['available_themes'][$id_theme]['description'] = $txt['theme_description'];
  986. // Are there any variants?
  987. if (file_exists($theme_data['theme_dir'] . '/index.template.php') && (empty($theme_data['disable_user_variant']) || allowedTo('admin_forum')))
  988. {
  989. $file_contents = implode('', file($theme_data['theme_dir'] . '/index.template.php'));
  990. if (preg_match('~\$settings\[\'theme_variants\'\]\s*=(.+?);~', $file_contents, $matches))
  991. {
  992. $settings['theme_variants'] = array();
  993. // Fill settings up.
  994. eval('global $settings;' . $matches[0]);
  995. if (!empty($settings['theme_variants']))
  996. {
  997. loadLanguage('Settings');
  998. $context['available_themes'][$id_theme]['variants'] = array();
  999. foreach ($settings['theme_variants'] as $variant)
  1000. $context['available_themes'][$id_theme]['variants'][$variant] = array(
  1001. 'label' => isset($txt['variant_' . $variant]) ? $txt['variant_' . $variant] : $variant,
  1002. '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'),
  1003. );
  1004. $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]));
  1005. if (!isset($context['available_themes'][$id_theme]['variants'][$context['available_themes'][$id_theme]['selected_variant']]['thumbnail']))
  1006. $context['available_themes'][$id_theme]['selected_variant'] = $settings['theme_variants'][0];
  1007. $context['available_themes'][$id_theme]['thumbnail_href'] = $context['available_themes'][$id_theme]['variants'][$context['available_themes'][$id_theme]['selected_variant']]['thumbnail'];
  1008. // Allow themes to override the text.
  1009. $context['available_themes'][$id_theme]['pick_label'] = isset($txt['variant_pick']) ? $txt['variant_pick'] : $txt['theme_pick_variant'];
  1010. }
  1011. }
  1012. }
  1013. }
  1014. // Then return it.
  1015. $settings['images_url'] = $current_images_url;
  1016. $settings['theme_variants'] = $current_theme_variants;
  1017. // As long as we're not doing the default theme...
  1018. if (!isset($_REQUEST['u']) || $_REQUEST['u'] >= 0)
  1019. {
  1020. if ($guest_theme != 0)
  1021. $context['available_themes'][0] = $context['available_themes'][$guest_theme];
  1022. $context['available_themes'][0]['id'] = 0;
  1023. $context['available_themes'][0]['name'] = $txt['theme_forum_default'];
  1024. $context['available_themes'][0]['selected'] = $context['current_theme'] == 0;
  1025. $context['available_themes'][0]['description'] = $txt['theme_global_description'];
  1026. }
  1027. ksort($context['available_themes']);
  1028. $context['page_title'] = $txt['theme_pick'];
  1029. $context['sub_template'] = 'pick';
  1030. }
  1031. /**
  1032. * Installs new themes, either from a gzip or copy of the default.
  1033. * - puts themes in $boardurl/Themes.
  1034. * - assumes the gzip has a root directory in it. (ie default.)
  1035. * Requires admin_forum.
  1036. * Accessed with ?action=admin;area=theme;sa=install.
  1037. */
  1038. function ThemeInstall()
  1039. {
  1040. global $sourcedir, $txt, $context, $boarddir, $boardurl;
  1041. global $themedir, $themeurl, $smcFunc;
  1042. checkSession('request');
  1043. isAllowedTo('admin_forum');
  1044. require_once($sourcedir . '/Subs-Package.php');
  1045. // Make it easier to change the path and url.
  1046. $themedir = $boarddir . '/Themes';
  1047. $themeurl = $boardurl . '/Themes';
  1048. loadTemplate('Themes');
  1049. $subActions = array(
  1050. 'file' => 'InstallFile',
  1051. 'copy' => 'InstallCopy',
  1052. 'dir' => 'InstallDir',
  1053. );
  1054. // Is there a function to call?
  1055. if (isset($_GET['do']) && !empty($_GET['do']) && isset($subActions[$_GET['do']]))
  1056. {
  1057. $action = $smcFunc['htmlspecialchars'](trim($_GET['do']));
  1058. // Got any info from the specific form?
  1059. if (!isset($_POST['save_'. $action]))
  1060. fatal_lang_error('theme_install_no_action', false);
  1061. validateToken('admin-t-'. $action);
  1062. // Hopefully the themes directory is writable, or we might have a problem.
  1063. if (!is_writable($themedir))
  1064. fatal_lang_error('theme_install_write_error', 'critical');
  1065. // Call the function and handle the result.
  1066. $result = $subActions[$action]();
  1067. // Everything went better than expected!
  1068. if (!empty($result))
  1069. {
  1070. $context['sub_template'] = 'installed';
  1071. $context['page_title'] = $txt['theme_installed'];
  1072. $context['installed_theme'] = $result;
  1073. }
  1074. }
  1075. // Nope, show a nice error.
  1076. else
  1077. fatal_lang_error('theme_install_no_action', false);
  1078. }
  1079. function InstallFile()
  1080. {
  1081. global $themedir, $themeurl, $context;
  1082. // Set a temp dir for dumping all required files on it.
  1083. $dirtemp = $themedir .'/temp';
  1084. // Create the temp dir.
  1085. mkdir($dirtemp, 0777);
  1086. // Hopefully the temp directory is writable, or we might have a problem.
  1087. if (!is_writable($dirtemp))
  1088. fatal_lang_error('theme_install_write_error', 'critical');
  1089. // This happens when the admin session is gone and the user has to login again.
  1090. if (!isset($_FILES) || !isset($_FILES['theme_gz']) || empty($_FILES['theme_gz']))
  1091. redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
  1092. // Another error check layer, something went wrong with the upload.
  1093. if (isset($_FILES['theme_gz']['error']) && $_FILES['theme_gz']['error'] != 0)
  1094. fatal_lang_error('theme_install_error_file_'. $_FILES['theme_gz']['error'], false);
  1095. // Get the theme's name.
  1096. $name = strtok(basename($_FILES['theme_gz']['name']));
  1097. $name = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $name);
  1098. // Start setting some vars.
  1099. $context['to_install'] = array(
  1100. 'theme_dir' => $themedir . '/' . $name,
  1101. 'theme_url' => $themeurl . '/' . $name,
  1102. 'images_url' => $themeurl . '/' . $name . '/images',
  1103. 'name' => $name,
  1104. );
  1105. // Extract the file on the proper themes dir.
  1106. $extracted = read_tgz_file($_FILES['theme_gz']['tmp_name'], $dirtemp, false, true);
  1107. if ($extracted)
  1108. {
  1109. // Read its info form the XML file.
  1110. $theme_info = get_theme_info($dirtemp);
  1111. $context['to_install'] += $theme_info;
  1112. // Install the theme. theme_install() will take care of possible errors.
  1113. $context['to_install']['id'] = theme_install($context['to_install']);
  1114. // Rename the temp dir to the actual theme name.
  1115. rename($dirtemp, $context['to_install']['theme_dir']);
  1116. // return all the info.
  1117. return $context['to_install'];
  1118. }
  1119. else
  1120. fatal_lang_error('theme_install_error_title', false);
  1121. }
  1122. function InstallCopy()
  1123. {
  1124. global $themedir, $themeurl, $settings, $smcFunc, $context;
  1125. global $forum_version;
  1126. // There's gotta be something to work with.
  1127. if (!isset($_REQUEST['copy']) || empty($_REQUEST['copy']))
  1128. fatal_lang_error('theme_install_error_title', false);
  1129. // Get a cleaner version.
  1130. $name = preg_replace('~[^A-Za-z0-9_\- ]~', '', $_REQUEST['copy']);
  1131. // Is there a theme already named like this?
  1132. if (file_exists($themedir .'/'. $name))
  1133. fatal_lang_error('theme_install_already_dir', false);
  1134. // This is a brand new theme so set all possible values.
  1135. $context['to_install'] = array(
  1136. 'theme_dir' => $themedir . '/' . $name,
  1137. 'theme_url' => $themeurl . '/' . $name,
  1138. 'name' => $name,
  1139. 'images_url' => $themeurl . '/' . $name . '/images',
  1140. 'version' => '1.0',
  1141. 'install_for' => '2.1 - 2.1.99, '. strtr($forum_version, array('SMF ' => '')),
  1142. );
  1143. // Create the specific dir.
  1144. umask(0);
  1145. mkdir($context['to_install']['theme_dir'], 0777);
  1146. // Buy some time.
  1147. @set_time_limit(600);
  1148. if (function_exists('apache_reset_timeout'))
  1149. @apache_reset_timeout();
  1150. // Create subdirectories for css and javascript files.
  1151. mkdir($context['to_install']['theme_dir'] . '/css', 0777);
  1152. mkdir($context['to_install']['theme_dir'] . '/scripts', 0777);
  1153. // Copy over the default non-theme files.
  1154. $to_copy = array('/index.php', '/index.template.php', '/css/index.css', '/css/rtl.css', '/css/admin.css', '/scripts/theme.js');
  1155. foreach ($to_copy as $file)
  1156. {
  1157. copy($settings['default_theme_dir'] . $file, $context['to_install']['theme_dir'] . $file);
  1158. @chmod($context['to_install']['theme_dir'] . $file, 0777);
  1159. }
  1160. // And now the entire images directory!
  1161. copytree($settings['default_theme_dir'] . '/images', $context['to_install']['theme_dir'] . '/images');
  1162. package_flush_cache();
  1163. // Lets get some data for the new theme.
  1164. $request = $smcFunc['db_query']('', '
  1165. SELECT variable, value
  1166. FROM {db_prefix}themes
  1167. WHERE variable IN ({string:theme_templates}, {string:theme_layers})
  1168. AND id_member = {int:no_member}
  1169. AND id_theme = {int:default_theme}',
  1170. array(
  1171. 'no_member' => 0,
  1172. 'default_theme' => 1,
  1173. 'theme_templates' => 'theme_templates',
  1174. 'theme_layers' => 'theme_layers',
  1175. )
  1176. );
  1177. while ($row = $smcFunc['db_fetch_assoc']($request))
  1178. {
  1179. if ($row['variable'] == 'theme_templates')
  1180. $theme_templates = $row['value'];
  1181. elseif ($row['variable'] == 'theme_layers')
  1182. $theme_layers = $row['value'];
  1183. else
  1184. continue;
  1185. }
  1186. $smcFunc['db_free_result']($request);
  1187. $context['to_install'] += array(
  1188. 'theme_layers' => empty($theme_layers) ? 'html,body' : $theme_layers,
  1189. 'theme_templates' => empty($theme_templates) ? 'index' : $theme_templates,
  1190. );
  1191. // Lets add a theme_info.xml to this theme.
  1192. $xml_info = '<' . '?xml version="1.0"?' . '>
  1193. <theme-info xmlns="http://www.simplemachines.org/xml/theme-info" xmlns:smf="http://www.simplemachines.org/">
  1194. <!-- For the id, always use something unique - put your name, a colon, and then the package name. -->
  1195. <id>smf:' . $smcFunc['strtolower']($context['to_install']['name']) . '</id>
  1196. <!-- The theme\'s version, please try to use semantic versioning. -->
  1197. <version>1.0</version>
  1198. <!-- Install for, the SMF versions this theme was designed for. Uses the same wildcards used in the packager manager. This field is mandatory. -->
  1199. <install for="'. $context['to_install']['install_for'] .'" />
  1200. <!-- Theme name, used purely for aesthetics. -->
  1201. <name>' . $context['to_install']['name'] . '</name>
  1202. <!-- Author: your email address or contact information. The name attribute is optional. -->
  1203. <author name="Simple Machines">[email protected]</author>
  1204. <!-- Website... where to get updates and more information. -->
  1205. <website>http://www.simplemachines.org/</website>
  1206. <!-- Template layers to use, defaults to "html,body". -->
  1207. <layers>' . $context['to_install']['theme_layers'] . '</layers>
  1208. <!-- Templates to load on startup. Default is "index". -->
  1209. <templates>' . $context['to_install']['theme_templates'] . '</templates>
  1210. <!-- Base this theme off another? Default is blank, or no. It could be "default". -->
  1211. <based-on></based-on>
  1212. </theme-info>';
  1213. // Now write it.
  1214. $fp = @fopen($context['to_install']['theme_dir'] . '/theme_info.xml', 'w+');
  1215. if ($fp)
  1216. {
  1217. fwrite($fp, $xml_info);
  1218. fclose($fp);
  1219. }
  1220. // Install the theme. theme_install() will take care of possible errors.
  1221. $context['to_install']['id'] = theme_install($context['to_install']);
  1222. // return the info.
  1223. return $context['to_install'];
  1224. }
  1225. function InstallDir()
  1226. {
  1227. global $themedir, $themeurl, $context;
  1228. // Cannot use the theme dir as a theme dir.
  1229. if (!isset($_REQUEST['theme_dir']) || empty($_REQUEST['theme_dir']) || rtrim(realpath($_REQUEST['theme_dir']), '/\\') == realpath($themedir))
  1230. fatal_lang_error('theme_install_invalid_dir', false);
  1231. // Check is there is "something" on the dir.
  1232. elseif (!is_dir($_REQUEST['theme_dir']) || !file_exists($_REQUEST['theme_dir'] . '/theme_info.xml'))
  1233. fatal_lang_error('theme_install_error', false);
  1234. $name = basename($_REQUEST['theme_dir']);
  1235. $name = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $name);
  1236. // All good! set some needed vars.
  1237. $context['to_install'] = array(
  1238. 'dir' => $_REQUEST['theme_dir'],
  1239. 'theme_url' => $themeurl . '/' . $name,
  1240. 'name' => $name,
  1241. 'images_url' => $themeurl . '/' . $name . '/images',
  1242. );
  1243. // Read its info form the XML file.
  1244. $context['to_install'] += $theme_info;
  1245. // Install the theme. theme_install() will take care of possible errors.
  1246. $context['to_install']['id'] = theme_install($context['to_install']);
  1247. // return the info.
  1248. return $context['to_install'];
  1249. }
  1250. /**
  1251. * Possibly the simplest and best example of how to use the template system.
  1252. * - allows the theme to take care of actions.
  1253. * - happens if $settings['catch_action'] is set and action isn't found
  1254. * in the action array.
  1255. * - can use a template, layers, sub_template, filename, and/or function.
  1256. * @todo look at this
  1257. */
  1258. function WrapAction()
  1259. {
  1260. global $context, $settings, $sourcedir;
  1261. // Load any necessary template(s)?
  1262. if (isset($settings['catch_action']['template']))
  1263. {
  1264. // Load both the template and language file. (but don't fret if the language file isn't there...)
  1265. loadTemplate($settings['catch_action']['template']);
  1266. loadLanguage($settings['catch_action']['template'], '', false);
  1267. }
  1268. // Any special layers?
  1269. if (isset($settings['catch_action']['layers']))
  1270. $context['template_layers'] = $settings['catch_action']['layers'];
  1271. // Just call a function?
  1272. if (isset($settings['catch_action']['function']))
  1273. {
  1274. if (isset($settings['catch_action']['filename']))
  1275. template_include($sourcedir . '/' . $settings['catch_action']['filename'], true);
  1276. $settings['catch_action']['function']();
  1277. }
  1278. // And finally, the main sub template ;).
  1279. elseif (isset($settings['catch_action']['sub_template']))
  1280. $context['sub_template'] = $settings['catch_action']['sub_template'];
  1281. }
  1282. /**
  1283. * Set an option via javascript.
  1284. * - sets a theme option without outputting anything.
  1285. * - can be used with javascript, via a dummy image... (which doesn't require
  1286. * the page to reload.)
  1287. * - requires someone who is logged in.
  1288. * - accessed via ?action=jsoption;var=variable;val=value;session_var=sess_id.
  1289. * - does not log access to the Who's Online log. (in index.php..)
  1290. */
  1291. function SetJavaScript()
  1292. {
  1293. global $settings, $user_info, $smcFunc, $options;
  1294. // Check the session id.
  1295. checkSession('get');
  1296. // This good-for-nothing pixel is being used to keep the session alive.
  1297. if (empty($_GET['var']) || !isset($_GET['val']))
  1298. redirectexit($settings['images_url'] . '/blank.png');
  1299. // Sorry, guests can't go any further than this..
  1300. if ($user_info['is_guest'] || $user_info['id'] == 0)
  1301. obExit(false);
  1302. $reservedVars = array(
  1303. 'actual_theme_url',
  1304. 'actual_images_url',
  1305. 'base_theme_dir',
  1306. 'base_theme_url',
  1307. 'default_images_url',
  1308. 'default_theme_dir',
  1309. 'default_theme_url',
  1310. 'default_template',
  1311. 'images_url',
  1312. 'number_recent_posts',
  1313. 'smiley_sets_default',
  1314. 'theme_dir',
  1315. 'theme_id',
  1316. 'theme_layers',
  1317. 'theme_templates',
  1318. 'theme_url',
  1319. 'name',
  1320. );
  1321. // Can't change reserved vars.
  1322. if (in_array(strtolower($_GET['var']), $reservedVars))
  1323. redirectexit($settings['images_url'] . '/blank.png');
  1324. // Use a specific theme?
  1325. if (isset($_GET['th']) || isset($_GET['id']))
  1326. {
  1327. // Invalidate the current themes cache too.
  1328. cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 60);
  1329. $settings['theme_id'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
  1330. }
  1331. // If this is the admin preferences the passed value will just be an element of it.
  1332. if ($_GET['var'] == 'admin_preferences')
  1333. {
  1334. $options['admin_preferences'] = !empty($options['admin_preferences']) ? unserialize($options['admin_preferences']) : array();
  1335. // New thingy...
  1336. if (isset($_GET['admin_key']) && strlen($_GET['admin_key']) < 5)
  1337. $options['admin_preferences'][$_GET['admin_key']] = $_GET['val'];
  1338. // Change the value to be something nice,
  1339. $_GET['val'] = serialize($options['admin_preferences']);
  1340. }
  1341. // Update the option.
  1342. $smcFunc['db_insert']('replace',
  1343. '{db_prefix}themes',
  1344. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  1345. array($settings['theme_id'], $user_info['id'], $_GET['var'], is_array($_GET['val']) ? implode(',', $_GET['val']) : $_GET['val']),
  1346. array('id_theme', 'id_member', 'variable')
  1347. );
  1348. cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 60);
  1349. // Don't output anything...
  1350. redirectexit($settings['images_url'] . '/blank.png');
  1351. }
  1352. /**
  1353. * Shows an interface for editing the templates.
  1354. * - uses the Themes template and edit_template/edit_style sub template.
  1355. * - accessed via ?action=admin;area=theme;sa=edit
  1356. */
  1357. function EditTheme()
  1358. {
  1359. global $context, $settings, $scripturl, $boarddir, $smcFunc;
  1360. // @todo Should this be removed?
  1361. if (isset($_REQUEST['preview']))
  1362. die('die() with fire');
  1363. isAllowedTo('admin_forum');
  1364. loadTemplate('Themes');
  1365. $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) @$_GET['id'];
  1366. if (empty($_GET['th']))
  1367. {
  1368. $request = $smcFunc['db_query']('', '
  1369. SELECT id_theme, variable, value
  1370. FROM {db_prefix}themes
  1371. WHERE variable IN ({string:name}, {string:theme_dir}, {string:theme_templates}, {string:theme_layers})
  1372. AND id_member = {int:no_member}',
  1373. array(
  1374. 'name' => 'name',
  1375. 'theme_dir' => 'theme_dir',
  1376. 'theme_templates' => 'theme_templates',
  1377. 'theme_layers' => 'theme_layers',
  1378. 'no_member' => 0,
  1379. )
  1380. );
  1381. $context['themes'] = array();
  1382. while ($row = $smcFunc['db_fetch_assoc']($request))
  1383. {
  1384. if (!isset($context['themes'][$row['id_theme']]))
  1385. $context['themes'][$row['id_theme']] = array(
  1386. 'id' => $row['id_theme'],
  1387. 'num_default_options' => 0,
  1388. 'num_members' => 0,
  1389. );
  1390. $context['themes'][$row['id_theme']][$row['variable']] = $row['value'];
  1391. }
  1392. $smcFunc['db_free_result']($request);
  1393. foreach ($context['themes'] as $key => $theme)
  1394. {
  1395. // There has to be a Settings template!
  1396. if (!file_exists($theme['theme_dir'] . '/index.template.php') && !file_exists($theme['theme_dir'] . '/css/index.css'))
  1397. unset($context['themes'][$key]);
  1398. else
  1399. {
  1400. if (!isset($theme['theme_templates']))
  1401. $templates = array('index');
  1402. else
  1403. $templates = explode(',', $theme['theme_templates']);
  1404. foreach ($templates as $template)
  1405. if (file_exists($theme['theme_dir'] . '/' . $template . '.template.php'))
  1406. {
  1407. // Fetch the header... a good 256 bytes should be more than enough.
  1408. $fp = fopen($theme['theme_dir'] . '/' . $template . '.template.php', 'rb');
  1409. $header = fread($fp, 256);
  1410. fclose($fp);
  1411. // Can we find a version comment, at all?
  1412. if (preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $header, $match) == 1)
  1413. {
  1414. $ver = $match[1];
  1415. if (!isset($context['themes'][$key]['version']) || $context['themes'][$key]['version'] > $ver)
  1416. $context['themes'][$key]['version'] = $ver;
  1417. }
  1418. }
  1419. $context['themes'][$key]['can_edit_style'] = file_exists($theme['theme_dir'] . '/css/index.css');
  1420. }
  1421. }
  1422. $context['sub_template'] = 'edit_list';
  1423. return 'no_themes';
  1424. }
  1425. $context['session_error'] = false;
  1426. // Get the directory of the theme we are editing.
  1427. $request = $smcFunc['db_query']('', '
  1428. SELECT value, id_theme
  1429. FROM {db_prefix}themes
  1430. WHERE variable = {string:theme_dir}
  1431. AND id_theme = {int:current_theme}
  1432. LIMIT 1',
  1433. array(
  1434. 'current_theme' => $_GET['th'],
  1435. 'theme_dir' => 'theme_dir',
  1436. )
  1437. );
  1438. list ($theme_dir, $context['theme_id']) = $smcFunc['db_fetch_row']($request);
  1439. $smcFunc['db_free_result']($request);
  1440. if (!file_exists($theme_dir . '/index.template.php') && !file_exists($theme_dir . '/css/index.css'))
  1441. fatal_lang_error('theme_edit_missing', false);
  1442. if (!isset($_REQUEST['filename']))
  1443. {
  1444. if (isset($_GET['directory']))
  1445. {
  1446. if (substr($_GET['directory'], 0, 1) == '.')
  1447. $_GET['directory'] = '';
  1448. else
  1449. {
  1450. $_GET['directory'] = preg_replace(array('~^[\./\\:\0\n\r]+~', '~[\\\\]~', '~/[\./]+~'), array('', '/', '/'), $_GET['directory']);
  1451. $temp = realpath($theme_dir . '/' . $_GET['directory']);
  1452. if (empty($temp) || substr($temp, 0, strlen(realpath($theme_dir))) != realpath($theme_dir))
  1453. $_GET['directory'] = '';
  1454. }
  1455. }
  1456. if (isset($_GET['directory']) && $_GET['directory'] != '')
  1457. {
  1458. $context['theme_files'] = get_file_listing($theme_dir . '/' . $_GET['directory'], $_GET['directory'] . '/');
  1459. $temp = dirname($_GET['directory']);
  1460. array_unshift($context['theme_files'], array(
  1461. 'filename' => $temp == '.' || $temp == '' ? '/ (..)' : $temp . ' (..)',
  1462. 'is_writable' => is_writable($theme_dir . '/' . $temp),
  1463. 'is_directory' => true,
  1464. 'is_template' => false,
  1465. 'is_image' => false,
  1466. 'is_editable' => false,
  1467. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . $temp,
  1468. 'size' => '',
  1469. ));
  1470. }
  1471. else
  1472. $context['theme_files'] = get_file_listing($theme_dir, '');
  1473. $context['sub_template'] = 'edit_browse';
  1474. return;
  1475. }
  1476. else
  1477. {
  1478. if (substr($_REQUEST['filename'], 0, 1) == '.')
  1479. $_REQUEST['filename'] = '';
  1480. else
  1481. {
  1482. $_REQUEST['filename'] = preg_replace(array('~^[\./\\:\0\n\r]+~', '~[\\\\]~', '~/[\./]+~'), array('', '/', '/'), $_REQUEST['filename']);
  1483. $temp = realpath($theme_dir . '/' . $_REQUEST['filename']);
  1484. if (empty($temp) || substr($temp, 0, strlen(realpath($theme_dir))) != realpath($theme_dir))
  1485. $_REQUEST['filename'] = '';
  1486. }
  1487. if (empty($_REQUEST['filename']))
  1488. fatal_lang_error('theme_edit_missing', false);
  1489. }
  1490. if (isset($_POST['save']))
  1491. {
  1492. if (checkSession('post', '', false) == '' && validateToken('admin-te-' . md5($_GET['th'] . '-' . $_REQUEST['filename']), 'post', false) == true)
  1493. {
  1494. if (is_array($_POST['entire_file']))
  1495. $_POST['entire_file'] = implode("\n", $_POST['entire_file']);
  1496. $_POST['entire_file'] = rtrim(strtr($_POST['entire_file'], array("\r" => '', ' ' => "\t")));
  1497. // Check for a parse error!
  1498. if (substr($_REQUEST['filename'], -13) == '.template.php' && is_writable($theme_dir) && ini_get('display_errors'))
  1499. {
  1500. $request = $smcFunc['db_query']('', '
  1501. SELECT value
  1502. FROM {db_prefix}themes
  1503. WHERE variable = {string:theme_url}
  1504. AND id_theme = {int:current_theme}
  1505. LIMIT 1',
  1506. array(
  1507. 'current_theme' => $_GET['th'],
  1508. 'theme_url' => 'theme_url',
  1509. )
  1510. );
  1511. list ($theme_url) = $smcFunc['db_fetch_row']($request);
  1512. $smcFunc['db_free_result']($request);
  1513. $fp = fopen($theme_dir . '/tmp_' . session_id() . '.php', 'w');
  1514. fwrite($fp, $_POST['entire_file']);
  1515. fclose($fp);
  1516. $error = @file_get_contents($theme_url . '/tmp_' . session_id() . '.php');
  1517. if (preg_match('~ <b>(\d+)</b><br( /)?' . '>$~i', $error) != 0)
  1518. $error_file = $theme_dir . '/tmp_' . session_id() . '.php';
  1519. else
  1520. unlink($theme_dir . '/tmp_' . session_id() . '.php');
  1521. }
  1522. if (!isset($error_file))
  1523. {
  1524. $fp = fopen($theme_dir . '/' . $_REQUEST['filename'], 'w');
  1525. fwrite($fp, $_POST['entire_file']);
  1526. fclose($fp);
  1527. redirectexit('action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . dirname($_REQUEST['filename']));
  1528. }
  1529. }
  1530. // Session timed out.
  1531. else
  1532. {
  1533. loadLanguage('Errors');
  1534. $context['session_error'] = true;
  1535. $context['sub_template'] = 'edit_file';
  1536. // Recycle the submitted data.
  1537. if (is_array($_POST['entire_file']))
  1538. $context['entire_file'] = $smcFunc['htmlspecialchars'](implode("\n", $_POST['entire_file']));
  1539. else
  1540. $context['entire_file'] = $smcFunc['htmlspecialchars']($_POST['entire_file']);
  1541. $context['edit_filename'] = $smcFunc['htmlspecialchars']($_POST['filename']);
  1542. // You were able to submit it, so it's reasonable to assume you are allowed to save.
  1543. $context['allow_save'] = true;
  1544. // Re-create the token so that it can be used
  1545. createToken('admin-te-' . md5($_GET['th'] . '-' . $_REQUEST['filename']));
  1546. return;
  1547. }
  1548. }
  1549. $context['allow_save'] = is_writable($theme_dir . '/' . $_REQUEST['filename']);
  1550. $context['allow_save_filename'] = strtr($theme_dir . '/' . $_REQUEST['filename'], array($boarddir => '...'));
  1551. $context['edit_filename'] = $smcFunc['htmlspecialchars']($_REQUEST['filename']);
  1552. if (substr($_REQUEST['filename'], -4) == '.css')
  1553. {
  1554. $context['sub_template'] = 'edit_style';
  1555. $context['entire_file'] = $smcFunc['htmlspecialchars'](strtr(file_get_contents($theme_dir . '/' . $_REQUEST['filename']), array("\t" => ' ')));
  1556. }
  1557. elseif (substr($_REQUEST['filename'], -13) == '.template.php')
  1558. {
  1559. $context['sub_template'] = 'edit_template';
  1560. if (!isset($error_file))
  1561. $file_data = file($theme_dir . '/' . $_REQUEST['filename']);
  1562. else
  1563. {
  1564. if (preg_match('~(<b>.+?</b>:.+?<b>).+?(</b>.+?<b>\d+</b>)<br( /)?' . '>$~i', $error, $match) != 0)
  1565. $context['parse_error'] = $match[1] . $_REQUEST['filename'] . $match[2];
  1566. $file_data = file($error_file);
  1567. unlink($error_file);
  1568. }
  1569. $j = 0;
  1570. $context['file_parts'] = array(array('lines' => 0, 'line' => 1, 'data' => ''));
  1571. for ($i = 0, $n = count($file_data); $i < $n; $i++)
  1572. {
  1573. if (isset($file_data[$i + 1]) && substr($file_data[$i + 1], 0, 9) == 'function ')
  1574. {
  1575. // Try to format the functions a little nicer...
  1576. $context['file_parts'][$j]['data'] = trim($context['file_parts'][$j]['data']) . "\n";
  1577. if (empty($context['file_parts'][$j]['lines']))
  1578. unset($context['file_parts'][$j]);
  1579. $context['file_parts'][++$j] = array('lines' => 0, 'line' => $i + 1, 'data' => '');
  1580. }
  1581. $context['file_parts'][$j]['lines']++;
  1582. $context['file_parts'][$j]['data'] .= $smcFunc['htmlspecialchars'](strtr($file_data[$i], array("\t" => ' ')));
  1583. }
  1584. $context['entire_file'] = $smcFunc['htmlspecialchars'](strtr(implode('', $file_data), array("\t" => ' ')));
  1585. }
  1586. else
  1587. {
  1588. $context['sub_template'] = 'edit_file';
  1589. $context['entire_file'] = $smcFunc['htmlspecialchars'](strtr(file_get_contents($theme_dir . '/' . $_REQUEST['filename']), array("\t" => ' ')));
  1590. }
  1591. // Create a special token to allow editing of multiple files.
  1592. createToken('admin-te-' . md5($_GET['th'] . '-' . $_REQUEST['filename']));
  1593. }
  1594. /**
  1595. * Generates a file listing for a given directory
  1596. *
  1597. * @param type $path
  1598. * @param type $relative
  1599. * @return type
  1600. */
  1601. function get_file_listing($path, $relative)
  1602. {
  1603. global $scripturl, $txt, $context;
  1604. // Is it even a directory?
  1605. if (!is_dir($path))
  1606. fatal_lang_error('error_invalid_dir', 'critical');
  1607. $dir = dir($path);
  1608. $entries = array();
  1609. while ($entry = $dir->read())
  1610. $entries[] = $entry;
  1611. $dir->close();
  1612. natcasesort($entries);
  1613. $listing1 = array();
  1614. $listing2 = array();
  1615. foreach ($entries as $entry)
  1616. {
  1617. // Skip all dot files, including .htaccess.
  1618. if (substr($entry, 0, 1) == '.' || $entry == 'CVS')
  1619. continue;
  1620. if (is_dir($path . '/' . $entry))
  1621. $listing1[] = array(
  1622. 'filename' => $entry,
  1623. 'is_writable' => is_writable($path . '/' . $entry),
  1624. 'is_directory' => true,
  1625. 'is_template' => false,
  1626. 'is_image' => false,
  1627. 'is_editable' => false,
  1628. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . $relative . $entry,
  1629. 'size' => '',
  1630. );
  1631. else
  1632. {
  1633. $size = filesize($path . '/' . $entry);
  1634. if ($size > 2048 || $size == 1024)
  1635. $size = comma_format($size / 1024) . ' ' . $txt['themeadmin_edit_kilobytes'];
  1636. else
  1637. $size = comma_format($size) . ' ' . $txt['themeadmin_edit_bytes'];
  1638. $listing2[] = array(
  1639. 'filename' => $entry,
  1640. 'is_writable' => is_writable($path . '/' . $entry),
  1641. 'is_directory' => false,
  1642. 'is_template' => preg_match('~\.template\.php$~', $entry) != 0,
  1643. 'is_image' => preg_match('~\.(jpg|jpeg|gif|bmp|png)$~', $entry) != 0,
  1644. '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,
  1645. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;filename=' . $relative . $entry,
  1646. 'size' => $size,
  1647. 'last_modified' => timeformat(filemtime($path . '/' . $entry)),
  1648. );
  1649. }
  1650. }
  1651. return array_merge($listing1, $listing2);
  1652. }
  1653. /**
  1654. * Makes a copy of a template file in a new location
  1655. * @uses Themes template, copy_template sub-template.
  1656. */
  1657. function CopyTemplate()
  1658. {
  1659. global $context, $settings, $smcFunc;
  1660. isAllowedTo('admin_forum');
  1661. loadTemplate('Themes');
  1662. $context[$context['admin_menu_name']]['current_subsection'] = 'edit';
  1663. $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
  1664. $request = $smcFunc['db_query']('', '
  1665. SELECT th1.value, th1.id_theme, th2.value
  1666. FROM {db_prefix}themes AS th1
  1667. LEFT JOIN {db_prefix}themes AS th2 ON (th2.variable = {string:base_theme_dir} AND th2.id_theme = {int:current_theme})
  1668. WHERE th1.variable = {string:theme_dir}
  1669. AND th1.id_theme = {int:current_theme}
  1670. LIMIT 1',
  1671. array(
  1672. 'current_theme' => $_GET['th'],
  1673. 'base_theme_dir' => 'base_theme_dir',
  1674. 'theme_dir' => 'theme_dir',
  1675. )
  1676. );
  1677. list ($theme_dir, $context['theme_id'], $base_theme_dir) = $smcFunc['db_fetch_row']($request);
  1678. $smcFunc['db_free_result']($request);
  1679. if (isset($_REQUEST['template']) && preg_match('~[\./\\\\:\0]~', $_REQUEST['template']) == 0)
  1680. {
  1681. if (!empty($base_theme_dir) && file_exists($base_theme_dir . '/' . $_REQUEST['template'] . '.template.php'))
  1682. $filename = $base_theme_dir . '/' . $_REQUEST['template'] . '.template.php';
  1683. elseif (file_exists($settings['default_theme_dir'] . '/' . $_REQUEST['template'] . '.template.php'))
  1684. $filename = $settings['default_theme_dir'] . '/' . $_REQUEST['template'] . '.template.php';
  1685. else
  1686. fatal_lang_error('no_access', false);
  1687. $fp = fopen($theme_dir . '/' . $_REQUEST['template'] . '.template.php', 'w');
  1688. fwrite($fp, file_get_contents($filename));
  1689. fclose($fp);
  1690. redirectexit('action=admin;area=theme;th=' . $context['theme_id'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=copy');
  1691. }
  1692. elseif (isset($_REQUEST['lang_file']) && preg_match('~^[^\./\\\\:\0]\.[^\./\\\\:\0]$~', $_REQUEST['lang_file']) != 0)
  1693. {
  1694. if (!empty($base_theme_dir) && file_exists($base_theme_dir . '/languages/' . $_REQUEST['lang_file'] . '.php'))
  1695. $filename = $base_theme_dir . '/languages/' . $_REQUEST['template'] . '.php';
  1696. elseif (file_exists($settings['default_theme_dir'] . '/languages/' . $_REQUEST['template'] . '.php'))
  1697. $filename = $settings['default_theme_dir'] . '/languages/' . $_REQUEST['template'] . '.php';
  1698. else
  1699. fatal_lang_error('no_access', false);
  1700. $fp = fopen($theme_dir . '/languages/' . $_REQUEST['lang_file'] . '.php', 'w');
  1701. fwrite($fp, file_get_contents($filename));
  1702. fclose($fp);
  1703. redirectexit('action=admin;area=theme;th=' . $context['theme_id'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=copy');
  1704. }
  1705. $templates = array();
  1706. $lang_files = array();
  1707. $dir = dir($settings['default_theme_dir']);
  1708. while ($entry = $dir->read())
  1709. {
  1710. if (substr($entry, -13) == '.template.php')
  1711. $templates[] = substr($entry, 0, -13);
  1712. }
  1713. $dir->close();
  1714. $dir = dir($settings['default_theme_dir'] . '/languages');
  1715. while ($entry = $dir->read())
  1716. {
  1717. if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches))
  1718. $lang_files[] = $matches[1];
  1719. }
  1720. $dir->close();
  1721. if (!empty($base_theme_dir))
  1722. {
  1723. $dir = dir($base_theme_dir);
  1724. while ($entry = $dir->read())
  1725. {
  1726. if (substr($entry, -13) == '.template.php' && !in_array(substr($entry, 0, -13), $templates))
  1727. $templates[] = substr($entry, 0, -13);
  1728. }
  1729. $dir->close();
  1730. if (file_exists($base_theme_dir . '/languages'))
  1731. {
  1732. $dir = dir($base_theme_dir . '/languages');
  1733. while ($entry = $dir->read())
  1734. {
  1735. if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches) && !in_array($matches[1], $lang_files))
  1736. $lang_files[] = $matches[1];
  1737. }
  1738. $dir->close();
  1739. }
  1740. }
  1741. natcasesort($templates);
  1742. natcasesort($lang_files);
  1743. $context['available_templates'] = array();
  1744. foreach ($templates as $template)
  1745. $context['available_templates'][$template] = array(
  1746. 'filename' => $template . '.template.php',
  1747. 'value' => $template,
  1748. 'already_exists' => false,
  1749. 'can_copy' => is_writable($theme_dir),
  1750. );
  1751. $context['available_language_files'] = array();
  1752. foreach ($lang_files as $file)
  1753. $context['available_language_files'][$file] = array(
  1754. 'filename' => $file . '.php',
  1755. 'value' => $file,
  1756. 'already_exists' => false,
  1757. 'can_copy' => file_exists($theme_dir . '/languages') ? is_writable($theme_dir . '/languages') : is_writable($theme_dir),
  1758. );
  1759. $dir = dir($theme_dir);
  1760. while ($entry = $dir->read())
  1761. {
  1762. if (substr($entry, -13) == '.template.php' && isset($context['available_templates'][substr($entry, 0, -13)]))
  1763. {
  1764. $context['available_templates'][substr($entry, 0, -13)]['already_exists'] = true;
  1765. $context['available_templates'][substr($entry, 0, -13)]['can_copy'] = is_writable($theme_dir . '/' . $entry);
  1766. }
  1767. }
  1768. $dir->close();
  1769. if (file_exists($theme_dir . '/languages'))
  1770. {
  1771. $dir = dir($theme_dir . '/languages');
  1772. while ($entry = $dir->read())
  1773. {
  1774. if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches) && isset($context['available_language_files'][$matches[1]]))
  1775. {
  1776. $context['available_language_files'][$matches[1]]['already_exists'] = true;
  1777. $context['available_language_files'][$matches[1]]['can_copy'] = is_writable($theme_dir . '/languages/' . $entry);
  1778. }
  1779. }
  1780. $dir->close();
  1781. }
  1782. $context['sub_template'] = 'copy_template';
  1783. }
  1784. ?>