Themes.php 63 KB

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