Themes.php 62 KB

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