Themes.php 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  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.0
  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. array(
  1027. 'theme_variant' => 'theme_variant',
  1028. )
  1029. );
  1030. while ($row = $smcFunc['db_fetch_assoc']($request))
  1031. $variant_preferences[$row['id_theme']] = $row['value'];
  1032. $smcFunc['db_free_result']($request);
  1033. }
  1034. // Save the setting first.
  1035. $current_images_url = $settings['images_url'];
  1036. $current_theme_variants = !empty($settings['theme_variants']) ? $settings['theme_variants'] : array();
  1037. foreach ($context['available_themes'] as $id_theme => $theme_data)
  1038. {
  1039. // Don't try to load the forum or board default theme's data... it doesn't have any!
  1040. if ($id_theme == 0)
  1041. continue;
  1042. // The thumbnail needs the correct path.
  1043. $settings['images_url'] = &$theme_data['images_url'];
  1044. if (file_exists($theme_data['theme_dir'] . '/languages/Settings.' . $user_info['language'] . '.php'))
  1045. include($theme_data['theme_dir'] . '/languages/Settings.' . $user_info['language'] . '.php');
  1046. elseif (file_exists($theme_data['theme_dir'] . '/languages/Settings.' . $language . '.php'))
  1047. include($theme_data['theme_dir'] . '/languages/Settings.' . $language . '.php');
  1048. else
  1049. {
  1050. $txt['theme_thumbnail_href'] = $theme_data['images_url'] . '/thumbnail.gif';
  1051. $txt['theme_description'] = '';
  1052. }
  1053. $context['available_themes'][$id_theme]['thumbnail_href'] = $txt['theme_thumbnail_href'];
  1054. $context['available_themes'][$id_theme]['description'] = $txt['theme_description'];
  1055. // Are there any variants?
  1056. if (file_exists($theme_data['theme_dir'] . '/index.template.php') && empty($theme_data['disable_user_variant']))
  1057. {
  1058. $file_contents = implode('', file($theme_data['theme_dir'] . '/index.template.php'));
  1059. if (preg_match('~\$settings\[\'theme_variants\'\]\s*=(.+?);~', $file_contents, $matches))
  1060. {
  1061. $settings['theme_variants'] = array();
  1062. // Fill settings up.
  1063. eval('global $settings;' . $matches[0]);
  1064. if (!empty($settings['theme_variants']))
  1065. {
  1066. loadLanguage('Settings');
  1067. $context['available_themes'][$id_theme]['variants'] = array();
  1068. foreach ($settings['theme_variants'] as $variant)
  1069. $context['available_themes'][$id_theme]['variants'][$variant] = array(
  1070. 'label' => isset($txt['variant_' . $variant]) ? $txt['variant_' . $variant] : $variant,
  1071. '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'),
  1072. );
  1073. $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]));
  1074. if (!isset($context['available_themes'][$id_theme]['variants'][$context['available_themes'][$id_theme]['selected_variant']]['thumbnail']))
  1075. $context['available_themes'][$id_theme]['selected_variant'] = $settings['theme_variants'][0];
  1076. $context['available_themes'][$id_theme]['thumbnail_href'] = $context['available_themes'][$id_theme]['variants'][$context['available_themes'][$id_theme]['selected_variant']]['thumbnail'];
  1077. // Allow themes to override the text.
  1078. $context['available_themes'][$id_theme]['pick_label'] = isset($txt['variant_pick']) ? $txt['variant_pick'] : $txt['theme_pick_variant'];
  1079. }
  1080. }
  1081. }
  1082. }
  1083. // Then return it.
  1084. $settings['images_url'] = $current_images_url;
  1085. $settings['theme_variants'] = $current_theme_variants;
  1086. // As long as we're not doing the default theme...
  1087. if (!isset($_REQUEST['u']) || $_REQUEST['u'] >= 0)
  1088. {
  1089. if ($guest_theme != 0)
  1090. $context['available_themes'][0] = $context['available_themes'][$guest_theme];
  1091. $context['available_themes'][0]['id'] = 0;
  1092. $context['available_themes'][0]['name'] = $txt['theme_forum_default'];
  1093. $context['available_themes'][0]['selected'] = $context['current_theme'] == 0;
  1094. $context['available_themes'][0]['description'] = $txt['theme_global_description'];
  1095. }
  1096. ksort($context['available_themes']);
  1097. $context['page_title'] = $txt['theme_pick'];
  1098. $context['sub_template'] = 'pick';
  1099. }
  1100. /**
  1101. * Installs new themes, either from a gzip or copy of the default.
  1102. * - puts themes in $boardurl/Themes.
  1103. * - assumes the gzip has a root directory in it. (ie default.)
  1104. * Requires admin_forum.
  1105. * Accessed with ?action=admin;area=theme;sa=install.
  1106. */
  1107. function ThemeInstall()
  1108. {
  1109. global $sourcedir, $boarddir, $boardurl, $txt, $context, $settings, $modSettings, $smcFunc;
  1110. checkSession('request');
  1111. isAllowedTo('admin_forum');
  1112. checkSession('request');
  1113. require_once($sourcedir . '/Subs-Package.php');
  1114. loadTemplate('Themes');
  1115. if (isset($_GET['theme_id']))
  1116. {
  1117. $result = $smcFunc['db_query']('', '
  1118. SELECT value
  1119. FROM {db_prefix}themes
  1120. WHERE id_theme = {int:current_theme}
  1121. AND id_member = {int:no_member}
  1122. AND variable = {string:name}
  1123. LIMIT 1',
  1124. array(
  1125. 'current_theme' => (int) $_GET['theme_id'],
  1126. 'no_member' => 0,
  1127. 'name' => 'name',
  1128. )
  1129. );
  1130. list ($theme_name) = $smcFunc['db_fetch_row']($result);
  1131. $smcFunc['db_free_result']($result);
  1132. $context['sub_template'] = 'installed';
  1133. $context['page_title'] = $txt['theme_installed'];
  1134. $context['installed_theme'] = array(
  1135. 'id' => (int) $_GET['theme_id'],
  1136. 'name' => $theme_name,
  1137. );
  1138. return;
  1139. }
  1140. if ((!empty($_FILES['theme_gz']) && (!isset($_FILES['theme_gz']['error']) || $_FILES['theme_gz']['error'] != 4)) || !empty($_REQUEST['theme_gz']))
  1141. $method = 'upload';
  1142. elseif (isset($_REQUEST['theme_dir']) && rtrim(realpath($_REQUEST['theme_dir']), '/\\') != realpath($boarddir . '/Themes') && file_exists($_REQUEST['theme_dir']))
  1143. $method = 'path';
  1144. else
  1145. $method = 'copy';
  1146. if (!empty($_REQUEST['copy']) && $method == 'copy')
  1147. {
  1148. // Hopefully the themes directory is writable, or we might have a problem.
  1149. if (!is_writable($boarddir . '/Themes'))
  1150. fatal_lang_error('theme_install_write_error', 'critical');
  1151. $theme_dir = $boarddir . '/Themes/' . preg_replace('~[^A-Za-z0-9_\- ]~', '', $_REQUEST['copy']);
  1152. umask(0);
  1153. mkdir($theme_dir, 0777);
  1154. @set_time_limit(600);
  1155. if (function_exists('apache_reset_timeout'))
  1156. @apache_reset_timeout();
  1157. // Create subdirectories for css and javascript files.
  1158. mkdir($theme_dir . '/css', 0777);
  1159. mkdir($theme_dir . '/scripts', 0777);
  1160. // Copy over the default non-theme files.
  1161. $to_copy = array('/index.php', '/index.template.php', '/css/index.css', '/css/rtl.css', '/scripts/theme.js');
  1162. foreach ($to_copy as $file)
  1163. {
  1164. copy($settings['default_theme_dir'] . $file, $theme_dir . $file);
  1165. @chmod($theme_dir . $file, 0777);
  1166. }
  1167. // And now the entire images directory!
  1168. copytree($settings['default_theme_dir'] . '/images', $theme_dir . '/images');
  1169. package_flush_cache();
  1170. $theme_name = $_REQUEST['copy'];
  1171. $images_url = $boardurl . '/Themes/' . basename($theme_dir) . '/images';
  1172. $theme_dir = realpath($theme_dir);
  1173. // Lets get some data for the new theme.
  1174. $request = $smcFunc['db_query']('', '
  1175. SELECT variable, value
  1176. FROM {db_prefix}themes
  1177. WHERE variable IN ({string:theme_templates}, {string:theme_layers})
  1178. AND id_member = {int:no_member}
  1179. AND id_theme = {int:default_theme}',
  1180. array(
  1181. 'no_member' => 0,
  1182. 'default_theme' => 1,
  1183. 'theme_templates' => 'theme_templates',
  1184. 'theme_layers' => 'theme_layers',
  1185. )
  1186. );
  1187. while ($row = $smcFunc['db_fetch_assoc']($request))
  1188. {
  1189. if ($row['variable'] == 'theme_templates')
  1190. $theme_templates = $row['value'];
  1191. elseif ($row['variable'] == 'theme_layers')
  1192. $theme_layers = $row['value'];
  1193. else
  1194. continue;
  1195. }
  1196. $smcFunc['db_free_result']($request);
  1197. // Lets add a theme_info.xml to this theme.
  1198. $xml_info = '<' . '?xml version="1.0"?' . '>
  1199. <theme-info xmlns="http://www.simplemachines.org/xml/theme-info" xmlns:smf="http://www.simplemachines.org/">
  1200. <!-- For the id, always use something unique - put your name, a colon, and then the package name. -->
  1201. <id>smf:' . $smcFunc['strtolower'](str_replace(array(' '), '_', $_REQUEST['copy'])) . '</id>
  1202. <version>' . $modSettings['smfVersion'] . '</version>
  1203. <!-- Theme name, used purely for aesthetics. -->
  1204. <name>' . $_REQUEST['copy'] . '</name>
  1205. <!-- Author: your email address or contact information. The name attribute is optional. -->
  1206. <author name="Simple Machines">[email protected]</author>
  1207. <!-- Website... where to get updates and more information. -->
  1208. <website>http://www.simplemachines.org/</website>
  1209. <!-- Template layers to use, defaults to "html,body". -->
  1210. <layers>' . (empty($theme_layers) ? 'html,body' : $theme_layers) . '</layers>
  1211. <!-- Templates to load on startup. Default is "index". -->
  1212. <templates>' . (empty($theme_templates) ? 'index' : $theme_templates) . '</templates>
  1213. <!-- Base this theme off another? Default is blank, or no. It could be "default". -->
  1214. <based-on></based-on>
  1215. </theme-info>';
  1216. // Now write it.
  1217. $fp = @fopen($theme_dir . '/theme_info.xml', 'w+');
  1218. if ($fp)
  1219. {
  1220. fwrite($fp, $xml_info);
  1221. fclose($fp);
  1222. }
  1223. }
  1224. elseif (isset($_REQUEST['theme_dir']) && $method == 'path')
  1225. {
  1226. if (!is_dir($_REQUEST['theme_dir']) || !file_exists($_REQUEST['theme_dir'] . '/theme_info.xml'))
  1227. fatal_lang_error('theme_install_error', false);
  1228. $theme_name = basename($_REQUEST['theme_dir']);
  1229. $theme_dir = $_REQUEST['theme_dir'];
  1230. }
  1231. elseif ($method = 'upload')
  1232. {
  1233. // Hopefully the themes directory is writable, or we might have a problem.
  1234. if (!is_writable($boarddir . '/Themes'))
  1235. fatal_lang_error('theme_install_write_error', 'critical');
  1236. require_once($sourcedir . '/Subs-Package.php');
  1237. // Set the default settings...
  1238. $theme_name = strtok(basename(isset($_FILES['theme_gz']) ? $_FILES['theme_gz']['name'] : $_REQUEST['theme_gz']), '.');
  1239. $theme_name = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $theme_name);
  1240. $theme_dir = $boarddir . '/Themes/' . $theme_name;
  1241. if (isset($_FILES['theme_gz']) && is_uploaded_file($_FILES['theme_gz']['tmp_name']) && (@ini_get('open_basedir') != '' || file_exists($_FILES['theme_gz']['tmp_name'])))
  1242. $extracted = read_tgz_file($_FILES['theme_gz']['tmp_name'], $boarddir . '/Themes/' . $theme_name, false, true);
  1243. elseif (isset($_REQUEST['theme_gz']))
  1244. {
  1245. // Check that the theme is from simplemachines.org, for now... maybe add mirroring later.
  1246. if (preg_match('~^http://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['theme_gz']) == 0 || strpos($_REQUEST['theme_gz'], 'dlattach') !== false)
  1247. fatal_lang_error('not_on_simplemachines');
  1248. $extracted = read_tgz_file($_REQUEST['theme_gz'], $boarddir . '/Themes/' . $theme_name, false, true);
  1249. }
  1250. else
  1251. redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
  1252. }
  1253. // Something go wrong?
  1254. if ($theme_dir != '' && basename($theme_dir) != 'Themes')
  1255. {
  1256. // Defaults.
  1257. $install_info = array(
  1258. 'theme_url' => $boardurl . '/Themes/' . basename($theme_dir),
  1259. 'images_url' => isset($images_url) ? $images_url : $boardurl . '/Themes/' . basename($theme_dir) . '/images',
  1260. 'theme_dir' => $theme_dir,
  1261. 'name' => $theme_name
  1262. );
  1263. if (file_exists($theme_dir . '/theme_info.xml'))
  1264. {
  1265. $theme_info = file_get_contents($theme_dir . '/theme_info.xml');
  1266. $xml_elements = array(
  1267. 'name' => 'name',
  1268. 'theme_layers' => 'layers',
  1269. 'theme_templates' => 'templates',
  1270. 'based_on' => 'based-on',
  1271. );
  1272. foreach ($xml_elements as $var => $name)
  1273. {
  1274. if (preg_match('~<' . $name . '>(?:<!\[CDATA\[)?(.+?)(?:\]\]>)?</' . $name . '>~', $theme_info, $match) == 1)
  1275. $install_info[$var] = $match[1];
  1276. }
  1277. if (preg_match('~<images>(?:<!\[CDATA\[)?(.+?)(?:\]\]>)?</images>~', $theme_info, $match) == 1)
  1278. {
  1279. $install_info['images_url'] = $install_info['theme_url'] . '/' . $match[1];
  1280. $explicit_images = true;
  1281. }
  1282. if (preg_match('~<extra>(?:<!\[CDATA\[)?(.+?)(?:\]\]>)?</extra>~', $theme_info, $match) == 1)
  1283. $install_info += unserialize($match[1]);
  1284. }
  1285. if (isset($install_info['based_on']))
  1286. {
  1287. if ($install_info['based_on'] == 'default')
  1288. {
  1289. $install_info['theme_url'] = $settings['default_theme_url'];
  1290. $install_info['images_url'] = $settings['default_images_url'];
  1291. }
  1292. elseif ($install_info['based_on'] != '')
  1293. {
  1294. $install_info['based_on'] = preg_replace('~[^A-Za-z0-9\-_ ]~', '', $install_info['based_on']);
  1295. $request = $smcFunc['db_query']('', '
  1296. SELECT th.value AS base_theme_dir, th2.value AS base_theme_url' . (!empty($explicit_images) ? '' : ', th3.value AS images_url') . '
  1297. FROM {db_prefix}themes AS th
  1298. INNER JOIN {db_prefix}themes AS th2 ON (th2.id_theme = th.id_theme
  1299. AND th2.id_member = {int:no_member}
  1300. AND th2.variable = {string:theme_url})' . (!empty($explicit_images) ? '' : '
  1301. INNER JOIN {db_prefix}themes AS th3 ON (th3.id_theme = th.id_theme
  1302. AND th3.id_member = {int:no_member}
  1303. AND th3.variable = {string:images_url})') . '
  1304. WHERE th.id_member = {int:no_member}
  1305. AND (th.value LIKE {string:based_on} OR th.value LIKE {string:based_on_path})
  1306. AND th.variable = {string:theme_dir}
  1307. LIMIT 1',
  1308. array(
  1309. 'no_member' => 0,
  1310. 'theme_url' => 'theme_url',
  1311. 'images_url' => 'images_url',
  1312. 'theme_dir' => 'theme_dir',
  1313. 'based_on' => '%/' . $install_info['based_on'],
  1314. 'based_on_path' => '%' . "\\" . $install_info['based_on'],
  1315. )
  1316. );
  1317. $temp = $smcFunc['db_fetch_assoc']($request);
  1318. $smcFunc['db_free_result']($request);
  1319. // @todo An error otherwise?
  1320. if (is_array($temp))
  1321. {
  1322. $install_info = $temp + $install_info;
  1323. if (empty($explicit_images) && !empty($install_info['base_theme_url']))
  1324. $install_info['theme_url'] = $install_info['base_theme_url'];
  1325. }
  1326. }
  1327. unset($install_info['based_on']);
  1328. }
  1329. // Find the newest id_theme.
  1330. $result = $smcFunc['db_query']('', '
  1331. SELECT MAX(id_theme)
  1332. FROM {db_prefix}themes',
  1333. array(
  1334. )
  1335. );
  1336. list ($id_theme) = $smcFunc['db_fetch_row']($result);
  1337. $smcFunc['db_free_result']($result);
  1338. // This will be theme number...
  1339. $id_theme++;
  1340. $inserts = array();
  1341. foreach ($install_info as $var => $val)
  1342. $inserts[] = array($id_theme, $var, $val);
  1343. if (!empty($inserts))
  1344. $smcFunc['db_insert']('insert',
  1345. '{db_prefix}themes',
  1346. array('id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  1347. $inserts,
  1348. array('id_theme', 'variable')
  1349. );
  1350. updateSettings(array('knownThemes' => strtr($modSettings['knownThemes'] . ',' . $id_theme, array(',,' => ','))));
  1351. }
  1352. redirectexit('action=admin;area=theme;sa=install;theme_id=' . $id_theme . ';' . $context['session_var'] . '=' . $context['session_id']);
  1353. }
  1354. /**
  1355. * Possibly the simplest and best example of how to use the template system.
  1356. * - allows the theme to take care of actions.
  1357. * - happens if $settings['catch_action'] is set and action isn't found
  1358. * in the action array.
  1359. * - can use a template, layers, sub_template, filename, and/or function.
  1360. * @todo look at this
  1361. */
  1362. function WrapAction()
  1363. {
  1364. global $context, $settings, $sourcedir;
  1365. // Load any necessary template(s)?
  1366. if (isset($settings['catch_action']['template']))
  1367. {
  1368. // Load both the template and language file. (but don't fret if the language file isn't there...)
  1369. loadTemplate($settings['catch_action']['template']);
  1370. loadLanguage($settings['catch_action']['template'], '', false);
  1371. }
  1372. // Any special layers?
  1373. if (isset($settings['catch_action']['layers']))
  1374. $context['template_layers'] = $settings['catch_action']['layers'];
  1375. // Just call a function?
  1376. if (isset($settings['catch_action']['function']))
  1377. {
  1378. if (isset($settings['catch_action']['filename']))
  1379. template_include($sourcedir . '/' . $settings['catch_action']['filename'], true);
  1380. $settings['catch_action']['function']();
  1381. }
  1382. // And finally, the main sub template ;).
  1383. elseif (isset($settings['catch_action']['sub_template']))
  1384. $context['sub_template'] = $settings['catch_action']['sub_template'];
  1385. }
  1386. /**
  1387. * Set an option via javascript.
  1388. * - sets a theme option without outputting anything.
  1389. * - can be used with javascript, via a dummy image... (which doesn't require
  1390. * the page to reload.)
  1391. * - requires someone who is logged in.
  1392. * - accessed via ?action=jsoption;var=variable;val=value;session_var=sess_id.
  1393. * - does not log access to the Who's Online log. (in index.php..)
  1394. */
  1395. function SetJavaScript()
  1396. {
  1397. global $settings, $user_info, $smcFunc, $options;
  1398. // Check the session id.
  1399. checkSession('get');
  1400. // This good-for-nothing pixel is being used to keep the session alive.
  1401. if (empty($_GET['var']) || !isset($_GET['val']))
  1402. redirectexit($settings['images_url'] . '/blank.gif');
  1403. // Sorry, guests can't go any further than this..
  1404. if ($user_info['is_guest'] || $user_info['id'] == 0)
  1405. obExit(false);
  1406. $reservedVars = array(
  1407. 'actual_theme_url',
  1408. 'actual_images_url',
  1409. 'base_theme_dir',
  1410. 'base_theme_url',
  1411. 'default_images_url',
  1412. 'default_theme_dir',
  1413. 'default_theme_url',
  1414. 'default_template',
  1415. 'images_url',
  1416. 'number_recent_posts',
  1417. 'smiley_sets_default',
  1418. 'theme_dir',
  1419. 'theme_id',
  1420. 'theme_layers',
  1421. 'theme_templates',
  1422. 'theme_url',
  1423. 'name',
  1424. );
  1425. // Can't change reserved vars.
  1426. if (in_array(strtolower($_GET['var']), $reservedVars))
  1427. redirectexit($settings['images_url'] . '/blank.gif');
  1428. // Use a specific theme?
  1429. if (isset($_GET['th']) || isset($_GET['id']))
  1430. {
  1431. // Invalidate the current themes cache too.
  1432. cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 60);
  1433. $settings['theme_id'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
  1434. }
  1435. // If this is the admin preferences the passed value will just be an element of it.
  1436. if ($_GET['var'] == 'admin_preferences')
  1437. {
  1438. $options['admin_preferences'] = !empty($options['admin_preferences']) ? unserialize($options['admin_preferences']) : array();
  1439. // New thingy...
  1440. if (isset($_GET['admin_key']) && strlen($_GET['admin_key']) < 5)
  1441. $options['admin_preferences'][$_GET['admin_key']] = $_GET['val'];
  1442. // Change the value to be something nice,
  1443. $_GET['val'] = serialize($options['admin_preferences']);
  1444. }
  1445. // Update the option.
  1446. $smcFunc['db_insert']('replace',
  1447. '{db_prefix}themes',
  1448. array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  1449. array($settings['theme_id'], $user_info['id'], $_GET['var'], is_array($_GET['val']) ? implode(',', $_GET['val']) : $_GET['val']),
  1450. array('id_theme', 'id_member', 'variable')
  1451. );
  1452. cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 60);
  1453. // Don't output anything...
  1454. redirectexit($settings['images_url'] . '/blank.gif');
  1455. }
  1456. /**
  1457. * Shows an interface for editing the templates.
  1458. * - uses the Themes template and edit_template/edit_style sub template.
  1459. * - accessed via ?action=admin;area=theme;sa=edit
  1460. */
  1461. function EditTheme()
  1462. {
  1463. global $context, $settings, $scripturl, $boarddir, $smcFunc;
  1464. // @todo Should this be removed?
  1465. if (isset($_REQUEST['preview']))
  1466. die('die() with fire');
  1467. isAllowedTo('admin_forum');
  1468. loadTemplate('Themes');
  1469. $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) @$_GET['id'];
  1470. if (empty($_GET['th']))
  1471. {
  1472. $request = $smcFunc['db_query']('', '
  1473. SELECT id_theme, variable, value
  1474. FROM {db_prefix}themes
  1475. WHERE variable IN ({string:name}, {string:theme_dir}, {string:theme_templates}, {string:theme_layers})
  1476. AND id_member = {int:no_member}',
  1477. array(
  1478. 'name' => 'name',
  1479. 'theme_dir' => 'theme_dir',
  1480. 'theme_templates' => 'theme_templates',
  1481. 'theme_layers' => 'theme_layers',
  1482. 'no_member' => 0,
  1483. )
  1484. );
  1485. $context['themes'] = array();
  1486. while ($row = $smcFunc['db_fetch_assoc']($request))
  1487. {
  1488. if (!isset($context['themes'][$row['id_theme']]))
  1489. $context['themes'][$row['id_theme']] = array(
  1490. 'id' => $row['id_theme'],
  1491. 'num_default_options' => 0,
  1492. 'num_members' => 0,
  1493. );
  1494. $context['themes'][$row['id_theme']][$row['variable']] = $row['value'];
  1495. }
  1496. $smcFunc['db_free_result']($request);
  1497. foreach ($context['themes'] as $key => $theme)
  1498. {
  1499. // There has to be a Settings template!
  1500. if (!file_exists($theme['theme_dir'] . '/index.template.php') && !file_exists($theme['theme_dir'] . '/css/index.css'))
  1501. unset($context['themes'][$key]);
  1502. else
  1503. {
  1504. if (!isset($theme['theme_templates']))
  1505. $templates = array('index');
  1506. else
  1507. $templates = explode(',', $theme['theme_templates']);
  1508. foreach ($templates as $template)
  1509. if (file_exists($theme['theme_dir'] . '/' . $template . '.template.php'))
  1510. {
  1511. // Fetch the header... a good 256 bytes should be more than enough.
  1512. $fp = fopen($theme['theme_dir'] . '/' . $template . '.template.php', 'rb');
  1513. $header = fread($fp, 256);
  1514. fclose($fp);
  1515. // Can we find a version comment, at all?
  1516. if (preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $header, $match) == 1)
  1517. {
  1518. $ver = $match[1];
  1519. if (!isset($context['themes'][$key]['version']) || $context['themes'][$key]['version'] > $ver)
  1520. $context['themes'][$key]['version'] = $ver;
  1521. }
  1522. }
  1523. $context['themes'][$key]['can_edit_style'] = file_exists($theme['theme_dir'] . '/css/index.css');
  1524. }
  1525. }
  1526. $context['sub_template'] = 'edit_list';
  1527. return 'no_themes';
  1528. }
  1529. $context['session_error'] = false;
  1530. // Get the directory of the theme we are editing.
  1531. $request = $smcFunc['db_query']('', '
  1532. SELECT value, id_theme
  1533. FROM {db_prefix}themes
  1534. WHERE variable = {string:theme_dir}
  1535. AND id_theme = {int:current_theme}
  1536. LIMIT 1',
  1537. array(
  1538. 'current_theme' => $_GET['th'],
  1539. 'theme_dir' => 'theme_dir',
  1540. )
  1541. );
  1542. list ($theme_dir, $context['theme_id']) = $smcFunc['db_fetch_row']($request);
  1543. $smcFunc['db_free_result']($request);
  1544. if (!isset($_REQUEST['filename']))
  1545. {
  1546. if (isset($_GET['directory']))
  1547. {
  1548. if (substr($_GET['directory'], 0, 1) == '.')
  1549. $_GET['directory'] = '';
  1550. else
  1551. {
  1552. $_GET['directory'] = preg_replace(array('~^[\./\\:\0\n\r]+~', '~[\\\\]~', '~/[\./]+~'), array('', '/', '/'), $_GET['directory']);
  1553. $temp = realpath($theme_dir . '/' . $_GET['directory']);
  1554. if (empty($temp) || substr($temp, 0, strlen(realpath($theme_dir))) != realpath($theme_dir))
  1555. $_GET['directory'] = '';
  1556. }
  1557. }
  1558. if (isset($_GET['directory']) && $_GET['directory'] != '')
  1559. {
  1560. $context['theme_files'] = get_file_listing($theme_dir . '/' . $_GET['directory'], $_GET['directory'] . '/');
  1561. $temp = dirname($_GET['directory']);
  1562. array_unshift($context['theme_files'], array(
  1563. 'filename' => $temp == '.' || $temp == '' ? '/ (..)' : $temp . ' (..)',
  1564. 'is_writable' => is_writable($theme_dir . '/' . $temp),
  1565. 'is_directory' => true,
  1566. 'is_template' => false,
  1567. 'is_image' => false,
  1568. 'is_editable' => false,
  1569. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . $temp,
  1570. 'size' => '',
  1571. ));
  1572. }
  1573. else
  1574. $context['theme_files'] = get_file_listing($theme_dir, '');
  1575. $context['sub_template'] = 'edit_browse';
  1576. return;
  1577. }
  1578. else
  1579. {
  1580. if (substr($_REQUEST['filename'], 0, 1) == '.')
  1581. $_REQUEST['filename'] = '';
  1582. else
  1583. {
  1584. $_REQUEST['filename'] = preg_replace(array('~^[\./\\:\0\n\r]+~', '~[\\\\]~', '~/[\./]+~'), array('', '/', '/'), $_REQUEST['filename']);
  1585. $temp = realpath($theme_dir . '/' . $_REQUEST['filename']);
  1586. if (empty($temp) || substr($temp, 0, strlen(realpath($theme_dir))) != realpath($theme_dir))
  1587. $_REQUEST['filename'] = '';
  1588. }
  1589. if (empty($_REQUEST['filename']))
  1590. fatal_lang_error('theme_edit_missing', false);
  1591. }
  1592. if (isset($_POST['submit']))
  1593. {
  1594. if (checkSession('post', '', false) == '' && validateToken('admin-te-' . md5($_GET['th'] . '-' . $_REQUEST['filename']), 'post', false) == true)
  1595. {
  1596. if (is_array($_POST['entire_file']))
  1597. $_POST['entire_file'] = implode("\n", $_POST['entire_file']);
  1598. $_POST['entire_file'] = rtrim(strtr($_POST['entire_file'], array("\r" => '', ' ' => "\t")));
  1599. // Check for a parse error!
  1600. if (substr($_REQUEST['filename'], -13) == '.template.php' && is_writable($theme_dir) && @ini_get('display_errors'))
  1601. {
  1602. $request = $smcFunc['db_query']('', '
  1603. SELECT value
  1604. FROM {db_prefix}themes
  1605. WHERE variable = {string:theme_url}
  1606. AND id_theme = {int:current_theme}
  1607. LIMIT 1',
  1608. array(
  1609. 'current_theme' => $_GET['th'],
  1610. 'theme_url' => 'theme_url',
  1611. )
  1612. );
  1613. list ($theme_url) = $smcFunc['db_fetch_row']($request);
  1614. $smcFunc['db_free_result']($request);
  1615. $fp = fopen($theme_dir . '/tmp_' . session_id() . '.php', 'w');
  1616. fwrite($fp, $_POST['entire_file']);
  1617. fclose($fp);
  1618. // @todo Use fetch_web_data()?
  1619. $error = @file_get_contents($theme_url . '/tmp_' . session_id() . '.php');
  1620. if (preg_match('~ <b>(\d+)</b><br( /)?' . '>$~i', $error) != 0)
  1621. $error_file = $theme_dir . '/tmp_' . session_id() . '.php';
  1622. else
  1623. unlink($theme_dir . '/tmp_' . session_id() . '.php');
  1624. }
  1625. if (!isset($error_file))
  1626. {
  1627. $fp = fopen($theme_dir . '/' . $_REQUEST['filename'], 'w');
  1628. fwrite($fp, $_POST['entire_file']);
  1629. fclose($fp);
  1630. redirectexit('action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . dirname($_REQUEST['filename']));
  1631. }
  1632. }
  1633. // Session timed out.
  1634. else
  1635. {
  1636. loadLanguage('Errors');
  1637. $context['session_error'] = true;
  1638. $context['sub_template'] = 'edit_file';
  1639. // Recycle the submitted data.
  1640. if (is_array($_POST['entire_file']))
  1641. $context['entire_file'] = htmlspecialchars(implode("\n", $_POST['entire_file']));
  1642. else
  1643. $context['entire_file'] = htmlspecialchars($_POST['entire_file']);
  1644. $context['edit_filename'] = htmlspecialchars($_POST['filename']);
  1645. // You were able to submit it, so it's reasonable to assume you are allowed to save.
  1646. $context['allow_save'] = true;
  1647. return;
  1648. }
  1649. }
  1650. $context['allow_save'] = is_writable($theme_dir . '/' . $_REQUEST['filename']);
  1651. $context['allow_save_filename'] = strtr($theme_dir . '/' . $_REQUEST['filename'], array($boarddir => '...'));
  1652. $context['edit_filename'] = htmlspecialchars($_REQUEST['filename']);
  1653. if (substr($_REQUEST['filename'], -4) == '.css')
  1654. {
  1655. $context['sub_template'] = 'edit_style';
  1656. $context['entire_file'] = htmlspecialchars(strtr(file_get_contents($theme_dir . '/' . $_REQUEST['filename']), array("\t" => ' ')));
  1657. }
  1658. elseif (substr($_REQUEST['filename'], -13) == '.template.php')
  1659. {
  1660. $context['sub_template'] = 'edit_template';
  1661. if (!isset($error_file))
  1662. $file_data = file($theme_dir . '/' . $_REQUEST['filename']);
  1663. else
  1664. {
  1665. if (preg_match('~(<b>.+?</b>:.+?<b>).+?(</b>.+?<b>\d+</b>)<br( /)?' . '>$~i', $error, $match) != 0)
  1666. $context['parse_error'] = $match[1] . $_REQUEST['filename'] . $match[2];
  1667. $file_data = file($error_file);
  1668. unlink($error_file);
  1669. }
  1670. $j = 0;
  1671. $context['file_parts'] = array(array('lines' => 0, 'line' => 1, 'data' => ''));
  1672. for ($i = 0, $n = count($file_data); $i < $n; $i++)
  1673. {
  1674. if (isset($file_data[$i + 1]) && substr($file_data[$i + 1], 0, 9) == 'function ')
  1675. {
  1676. // Try to format the functions a little nicer...
  1677. $context['file_parts'][$j]['data'] = trim($context['file_parts'][$j]['data']) . "\n";
  1678. if (empty($context['file_parts'][$j]['lines']))
  1679. unset($context['file_parts'][$j]);
  1680. $context['file_parts'][++$j] = array('lines' => 0, 'line' => $i + 1, 'data' => '');
  1681. }
  1682. $context['file_parts'][$j]['lines']++;
  1683. $context['file_parts'][$j]['data'] .= htmlspecialchars(strtr($file_data[$i], array("\t" => ' ')));
  1684. }
  1685. $context['entire_file'] = htmlspecialchars(strtr(implode('', $file_data), array("\t" => ' ')));
  1686. }
  1687. else
  1688. {
  1689. $context['sub_template'] = 'edit_file';
  1690. $context['entire_file'] = htmlspecialchars(strtr(file_get_contents($theme_dir . '/' . $_REQUEST['filename']), array("\t" => ' ')));
  1691. }
  1692. // Create a special token to allow editing of multiple files.
  1693. createToken('admin-te-' . md5($_GET['th'] . '-' . $_REQUEST['filename']));
  1694. }
  1695. function get_file_listing($path, $relative)
  1696. {
  1697. global $scripturl, $txt, $context;
  1698. // Is it even a directory?
  1699. if (!is_dir($path))
  1700. fatal_lang_error('error_invalid_dir', 'critical');
  1701. $dir = dir($path);
  1702. $entries = array();
  1703. while ($entry = $dir->read())
  1704. $entries[] = $entry;
  1705. $dir->close();
  1706. natcasesort($entries);
  1707. $listing1 = array();
  1708. $listing2 = array();
  1709. foreach ($entries as $entry)
  1710. {
  1711. // Skip all dot files, including .htaccess.
  1712. if (substr($entry, 0, 1) == '.' || $entry == 'CVS')
  1713. continue;
  1714. if (is_dir($path . '/' . $entry))
  1715. $listing1[] = array(
  1716. 'filename' => $entry,
  1717. 'is_writable' => is_writable($path . '/' . $entry),
  1718. 'is_directory' => true,
  1719. 'is_template' => false,
  1720. 'is_image' => false,
  1721. 'is_editable' => false,
  1722. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . $relative . $entry,
  1723. 'size' => '',
  1724. );
  1725. else
  1726. {
  1727. $size = filesize($path . '/' . $entry);
  1728. if ($size > 2048 || $size == 1024)
  1729. $size = comma_format($size / 1024) . ' ' . $txt['themeadmin_edit_kilobytes'];
  1730. else
  1731. $size = comma_format($size) . ' ' . $txt['themeadmin_edit_bytes'];
  1732. $listing2[] = array(
  1733. 'filename' => $entry,
  1734. 'is_writable' => is_writable($path . '/' . $entry),
  1735. 'is_directory' => false,
  1736. 'is_template' => preg_match('~\.template\.php$~', $entry) != 0,
  1737. 'is_image' => preg_match('~\.(jpg|jpeg|gif|bmp|png)$~', $entry) != 0,
  1738. '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,
  1739. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;filename=' . $relative . $entry,
  1740. 'size' => $size,
  1741. 'last_modified' => timeformat(filemtime($path . '/' . $entry)),
  1742. );
  1743. }
  1744. }
  1745. return array_merge($listing1, $listing2);
  1746. }
  1747. function CopyTemplate()
  1748. {
  1749. global $context, $settings, $smcFunc;
  1750. isAllowedTo('admin_forum');
  1751. loadTemplate('Themes');
  1752. $context[$context['admin_menu_name']]['current_subsection'] = 'edit';
  1753. $_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
  1754. $request = $smcFunc['db_query']('', '
  1755. SELECT th1.value, th1.id_theme, th2.value
  1756. FROM {db_prefix}themes AS th1
  1757. LEFT JOIN {db_prefix}themes AS th2 ON (th2.variable = {string:base_theme_dir} AND th2.id_theme = {int:current_theme})
  1758. WHERE th1.variable = {string:theme_dir}
  1759. AND th1.id_theme = {int:current_theme}
  1760. LIMIT 1',
  1761. array(
  1762. 'current_theme' => $_GET['th'],
  1763. 'base_theme_dir' => 'base_theme_dir',
  1764. 'theme_dir' => 'theme_dir',
  1765. )
  1766. );
  1767. list ($theme_dir, $context['theme_id'], $base_theme_dir) = $smcFunc['db_fetch_row']($request);
  1768. $smcFunc['db_free_result']($request);
  1769. if (isset($_REQUEST['template']) && preg_match('~[\./\\\\:\0]~', $_REQUEST['template']) == 0)
  1770. {
  1771. if (!empty($base_theme_dir) && file_exists($base_theme_dir . '/' . $_REQUEST['template'] . '.template.php'))
  1772. $filename = $base_theme_dir . '/' . $_REQUEST['template'] . '.template.php';
  1773. elseif (file_exists($settings['default_theme_dir'] . '/' . $_REQUEST['template'] . '.template.php'))
  1774. $filename = $settings['default_theme_dir'] . '/' . $_REQUEST['template'] . '.template.php';
  1775. else
  1776. fatal_lang_error('no_access', false);
  1777. $fp = fopen($theme_dir . '/' . $_REQUEST['template'] . '.template.php', 'w');
  1778. fwrite($fp, file_get_contents($filename));
  1779. fclose($fp);
  1780. redirectexit('action=admin;area=theme;th=' . $context['theme_id'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=copy');
  1781. }
  1782. elseif (isset($_REQUEST['lang_file']) && preg_match('~^[^\./\\\\:\0]\.[^\./\\\\:\0]$~', $_REQUEST['lang_file']) != 0)
  1783. {
  1784. if (!empty($base_theme_dir) && file_exists($base_theme_dir . '/languages/' . $_REQUEST['lang_file'] . '.php'))
  1785. $filename = $base_theme_dir . '/languages/' . $_REQUEST['template'] . '.php';
  1786. elseif (file_exists($settings['default_theme_dir'] . '/languages/' . $_REQUEST['template'] . '.php'))
  1787. $filename = $settings['default_theme_dir'] . '/languages/' . $_REQUEST['template'] . '.php';
  1788. else
  1789. fatal_lang_error('no_access', false);
  1790. $fp = fopen($theme_dir . '/languages/' . $_REQUEST['lang_file'] . '.php', 'w');
  1791. fwrite($fp, file_get_contents($filename));
  1792. fclose($fp);
  1793. redirectexit('action=admin;area=theme;th=' . $context['theme_id'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=copy');
  1794. }
  1795. $templates = array();
  1796. $lang_files = array();
  1797. $dir = dir($settings['default_theme_dir']);
  1798. while ($entry = $dir->read())
  1799. {
  1800. if (substr($entry, -13) == '.template.php')
  1801. $templates[] = substr($entry, 0, -13);
  1802. }
  1803. $dir->close();
  1804. $dir = dir($settings['default_theme_dir'] . '/languages');
  1805. while ($entry = $dir->read())
  1806. {
  1807. if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches))
  1808. $lang_files[] = $matches[1];
  1809. }
  1810. $dir->close();
  1811. if (!empty($base_theme_dir))
  1812. {
  1813. $dir = dir($base_theme_dir);
  1814. while ($entry = $dir->read())
  1815. {
  1816. if (substr($entry, -13) == '.template.php' && !in_array(substr($entry, 0, -13), $templates))
  1817. $templates[] = substr($entry, 0, -13);
  1818. }
  1819. $dir->close();
  1820. if (file_exists($base_theme_dir . '/languages'))
  1821. {
  1822. $dir = dir($base_theme_dir . '/languages');
  1823. while ($entry = $dir->read())
  1824. {
  1825. if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches) && !in_array($matches[1], $lang_files))
  1826. $lang_files[] = $matches[1];
  1827. }
  1828. $dir->close();
  1829. }
  1830. }
  1831. natcasesort($templates);
  1832. natcasesort($lang_files);
  1833. $context['available_templates'] = array();
  1834. foreach ($templates as $template)
  1835. $context['available_templates'][$template] = array(
  1836. 'filename' => $template . '.template.php',
  1837. 'value' => $template,
  1838. 'already_exists' => false,
  1839. 'can_copy' => is_writable($theme_dir),
  1840. );
  1841. $context['available_language_files'] = array();
  1842. foreach ($lang_files as $file)
  1843. $context['available_language_files'][$file] = array(
  1844. 'filename' => $file . '.php',
  1845. 'value' => $file,
  1846. 'already_exists' => false,
  1847. 'can_copy' => file_exists($theme_dir . '/languages') ? is_writable($theme_dir . '/languages') : is_writable($theme_dir),
  1848. );
  1849. $dir = dir($theme_dir);
  1850. while ($entry = $dir->read())
  1851. {
  1852. if (substr($entry, -13) == '.template.php' && isset($context['available_templates'][substr($entry, 0, -13)]))
  1853. {
  1854. $context['available_templates'][substr($entry, 0, -13)]['already_exists'] = true;
  1855. $context['available_templates'][substr($entry, 0, -13)]['can_copy'] = is_writable($theme_dir . '/' . $entry);
  1856. }
  1857. }
  1858. $dir->close();
  1859. if (file_exists($theme_dir . '/languages'))
  1860. {
  1861. $dir = dir($theme_dir . '/languages');
  1862. while ($entry = $dir->read())
  1863. {
  1864. if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches) && isset($context['available_language_files'][$matches[1]]))
  1865. {
  1866. $context['available_language_files'][$matches[1]]['already_exists'] = true;
  1867. $context['available_language_files'][$matches[1]]['can_copy'] = is_writable($theme_dir . '/languages/' . $entry);
  1868. }
  1869. }
  1870. $dir->close();
  1871. }
  1872. $context['sub_template'] = 'copy_template';
  1873. }
  1874. ?>