Themes.php 68 KB

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