Themes.php 64 KB

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