Themes.php 71 KB

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