Themes.php 68 KB

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