Themes.php 76 KB

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