Themes.php 68 KB

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