Themes.php 71 KB

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