Themes.php 67 KB

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