Subs-Themes.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <?php
  2. /**
  3. * Helper file for handling themes.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines
  9. *
  10. * @copyright 2013 Simple Machines and individual contributors
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.1 Alpha 1
  14. */
  15. if (!defined('SMF'))
  16. die('No direct access...');
  17. /**
  18. * Gets a single theme's info.
  19. *
  20. * @param int $id The theme ID to get the info from.
  21. * @return array The theme info as an array.
  22. */
  23. function get_single_theme($id)
  24. {
  25. global $smcFunc, $modSettings;
  26. // No data, no fun!
  27. if (empty($id))
  28. return false;
  29. // Make sure $id is an int.
  30. $id = (int) $id;
  31. // List of all possible values.
  32. $themeValues = array(
  33. 'theme_dir',
  34. 'images_url',
  35. 'theme_url',
  36. 'name',
  37. 'theme_layers',
  38. 'theme_templates',
  39. 'version',
  40. 'install_for',
  41. 'based_on',
  42. );
  43. // Make changes if you really want it.
  44. call_integration_hook('integrate_get_single_theme', array(&$themeValues, $id));
  45. $single = array(
  46. 'id' => $id,
  47. );
  48. // Make our known/enable themes a little easier to work with.
  49. $knownThemes = !empty($modSettings['knownThemes']) ? explode(',',$modSettings['knownThemes']) : array();
  50. $enableThemes = !empty($modSettings['enableThemes']) ? explode(',',$modSettings['enableThemes']) : array();
  51. $request = $smcFunc['db_query']('', '
  52. SELECT id_theme, variable, value
  53. FROM {db_prefix}themes
  54. WHERE variable IN ({array_string:theme_values})
  55. AND id_theme = ({int:id_theme})
  56. AND id_member = {int:no_member}',
  57. array(
  58. 'theme_values' => $themeValues,
  59. 'id_theme' => $id,
  60. 'no_member' => 0,
  61. )
  62. );
  63. while ($row = $smcFunc['db_fetch_assoc']($request))
  64. {
  65. $single[$row['variable']] = $row['value'];
  66. // Fix the path and tell if its a valid one.
  67. if ($row['variable'] == 'theme_dir')
  68. {
  69. $single['theme_dir'] = realpath($row['value']);
  70. $single['valid_path'] = file_exists($row['value']) && is_dir($row['value']);
  71. }
  72. }
  73. // Is this theme installed and enabled?
  74. $single['known'] = in_array($single['id'], $knownThemes);
  75. $single['enable'] = in_array($single['id'], $enableThemes);
  76. // It should at least return if the theme is a known one or if its enable.
  77. return $single;
  78. }
  79. /**
  80. * Loads and returns all installed themes.
  81. *
  82. * Stores all themes on $context['themes'] for easier use.
  83. * @param bool $enable_only false by default for getting all themes. If true the function will return all themes that are currently enable.
  84. * @return array With the theme's IDs as key.
  85. */
  86. function get_all_themes($enable_only = false)
  87. {
  88. global $modSettings, $context, $smcFunc;
  89. // Make our known/enable themes a little easier to work with.
  90. $knownThemes = !empty($modSettings['knownThemes']) ? explode(',',$modSettings['knownThemes']) : array();
  91. $enableThemes = !empty($modSettings['enableThemes']) ? explode(',',$modSettings['enableThemes']) : array();
  92. // List of all possible themes values.
  93. $themeValues = array(
  94. 'theme_dir',
  95. 'images_url',
  96. 'theme_url',
  97. 'name',
  98. 'theme_layers',
  99. 'theme_templates',
  100. 'version',
  101. 'install_for',
  102. 'based_on',
  103. );
  104. // Make changes if you really want it.
  105. call_integration_hook('integrate_get_all_themes', array(&$themeValues, $enable_only));
  106. // So, what is it going to be?
  107. $query_where = $enable_only ? $enableThemes : $knownThemes;
  108. // Perform the query as requested.
  109. $request = $smcFunc['db_query']('', '
  110. SELECT id_theme, variable, value
  111. FROM {db_prefix}themes
  112. WHERE variable IN ({array_string:theme_values})
  113. AND id_theme IN ({array_string:query_where})
  114. AND id_member = {int:no_member}',
  115. array(
  116. 'query_where' => $query_where,
  117. 'theme_values' => $themeValues,
  118. 'no_member' => 0,
  119. )
  120. );
  121. $context['themes'] = array();
  122. while ($row = $smcFunc['db_fetch_assoc']($request))
  123. {
  124. $context['themes'][$row['id_theme']]['id'] = (int) $row['id_theme'];
  125. // Fix the path and tell if its a valid one.
  126. if ($row['variable'] == 'theme_dir')
  127. {
  128. $context['themes'][$row['id_theme']][$row['variable']] = realpath($row['value']);
  129. $context['themes'][$row['id_theme']]['valid_path'] = file_exists(realpath($row['value'])) && is_dir(realpath($row['value']));
  130. }
  131. $context['themes'][$row['id_theme']]['known'] = in_array($row['id_theme'], $knownThemes);
  132. $context['themes'][$row['id_theme']]['enable'] = in_array($row['id_theme'], $enableThemes);
  133. $context['themes'][$row['id_theme']][$row['variable']] = $row['value'];
  134. }
  135. $smcFunc['db_free_result']($request);
  136. }
  137. /**
  138. * Reads an .xml file and returns the data as an array
  139. *
  140. * Removes the entire theme if the .xml file couldn't be found or read.
  141. * @param string $path The absolute path to the xml file.
  142. * @return array An array with all the info extracted from the xml file.
  143. */
  144. function get_theme_info($path)
  145. {
  146. global $sourcedir, $forum_version, $txt, $scripturl, $context;
  147. global $explicit_images;
  148. if (empty($path))
  149. return false;
  150. $xml_data = array();
  151. $explicit_images = false;
  152. // Perhaps they are trying to install a mod, lets tell them nicely this is the wrong function.
  153. if (file_exists($path . '/package-info.xml'))
  154. {
  155. loadLanguage('Errors');
  156. // We need to delete the dir otherwise the next time you try to install a theme you will get the same error.
  157. remove_dir($path);
  158. $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']);
  159. fatal_lang_error('package_theme_upload_error_broken', false, $txt['package_get_error_is_mod']);
  160. }
  161. // Parse theme-info.xml into an xmlArray.
  162. require_once($sourcedir . '/Class-Package.php');
  163. $theme_info_xml = new xmlArray(file_get_contents($path . '/theme_info.xml'));
  164. // Error message, there isn't any valid info.
  165. if (!$theme_info_xml->exists('theme-info[0]'))
  166. {
  167. remove_dir($path);
  168. fatal_lang_error('package_get_error_packageinfo_corrupt', false);
  169. }
  170. // Check for compatibility with 2.1 or greater.
  171. if (!$theme_info_xml->exists('theme-info/install'))
  172. {
  173. remove_dir($path);
  174. fatal_lang_error('package_get_error_theme_not_compatible', false, $forum_version);
  175. }
  176. // So, we have an install tag which is cool and stuff but we also need to check it and match your current SMF version...
  177. $the_version = strtr($forum_version, array('SMF ' => ''));
  178. $install_versions = $theme_info_xml->path('theme-info/install/@for');
  179. // The theme isn't compatible with the current SMF version.
  180. if (!$install_versions || !matchPackageVersion($the_version, $install_versions))
  181. {
  182. remove_dir($path);
  183. fatal_lang_error('package_get_error_theme_not_compatible', false, $forum_version);
  184. }
  185. $theme_info_xml = $theme_info_xml->path('theme-info[0]');
  186. $theme_info_xml = $theme_info_xml->to_array();
  187. $xml_elements = array(
  188. 'theme_layers' => 'layers',
  189. 'theme_templates' => 'templates',
  190. 'based_on' => 'based-on',
  191. 'version' => 'version',
  192. );
  193. // Assign the values to be stored.
  194. foreach ($xml_elements as $var => $name)
  195. if (!empty($theme_info_xml[$name]))
  196. $xml_data[$var] = $theme_info_xml[$name];
  197. // Add the supported versions.
  198. $xml_data['install_for'] = $install_versions;
  199. // Overwrite the default images folder.
  200. if (!empty($theme_info_xml['images']))
  201. {
  202. $xml_data['images_url'] = $path . '/' . $theme_info_xml['images'];
  203. $explicit_images = true;
  204. }
  205. if (!empty($theme_info_xml['extra']))
  206. $xml_data += unserialize($theme_info_xml['extra']);
  207. return $xml_data;
  208. }
  209. /**
  210. * Inserts a theme's data to the DataBase.
  211. *
  212. * Ends execution with fatal_lang_error() if an error appears.
  213. * @param array $to_install An array containing all values to be stored into the DB.
  214. * @return int The newly created theme ID.
  215. */
  216. function theme_install($to_install = array())
  217. {
  218. global $smcFunc, $context, $themedir, $themeurl, $modSettings;
  219. global $settings, $explicit_images;
  220. // External use? no problem!
  221. if ($to_install)
  222. $context['to_install'] = $to_install;
  223. // One last check.
  224. if (empty($context['to_install']['theme_dir']) || basename($context['to_install']['theme_dir']) == 'Themes')
  225. fatal_lang_error('theme_install_invalid_dir', false);
  226. // OK, is this a newer version of an already installed theme?
  227. if (!empty($context['to_install']['version']))
  228. {
  229. $to_update = array();
  230. $request = $smcFunc['db_query']('', '
  231. SELECT id_theme, variable, value
  232. FROM {db_prefix}themes
  233. WHERE id_member = {int:no_member}
  234. AND variable = {string:name}
  235. AND value LIKE {string:name_value}
  236. LIMIT 1',
  237. array(
  238. 'no_member' => 0,
  239. 'name' => 'name',
  240. 'version' => 'version',
  241. 'name_value' => '%'. $context['to_install']['name'] .'%',
  242. )
  243. );
  244. $to_update = $smcFunc['db_fetch_assoc']($request);
  245. $smcFunc['db_free_result']($request);
  246. // Got something, lets figure it out what to do next.
  247. if (!empty($to_update) && !empty($to_update['version']))
  248. switch (compareVersions($context['to_install']['version'], $to_update['version']))
  249. {
  250. case 0: // This is exactly the same theme.
  251. case -1: // The one being installed is older than the one already installed.
  252. fatal_lang_error('package_get_error_theme_no_new_version', false, array($context['to_install']['version'], $to_update['version']));
  253. break;
  254. case 1: // Got a newer version, update the old entry.
  255. $smcFunc['db_query']('', '
  256. UPDATE {db_prefix}themes
  257. SET value = {string:new_value}
  258. WHERE variable = {string:version}
  259. AND id_theme = {int:id_theme}',
  260. array(
  261. 'new_value' => $context['to_install']['version'],
  262. 'version' => 'version',
  263. 'id_theme' => $to_update['id_theme'],
  264. )
  265. );
  266. // Done with the update, tell the user about it.
  267. $context['to_install']['updated'] = true;
  268. $context['to_install']['id'] = $to_update['id_theme'];
  269. return $context['to_install'];
  270. break; // Just for reference.
  271. default: // Any other possible result.
  272. fatal_lang_error('package_get_error_theme_no_new_version', false, array($context['to_install']['version'], $to_update['version']));
  273. }
  274. }
  275. if (!empty($context['to_install']['based_on']))
  276. {
  277. // No need for elaborated stuff when the theme is based on the default one.
  278. if ($context['to_install']['based_on'] == 'default')
  279. {
  280. $context['to_install']['theme_url'] = $settings['default_theme_url'];
  281. $context['to_install']['images_url'] = $settings['default_images_url'];
  282. }
  283. // Custom theme based on another custom theme, lets get some info.
  284. elseif ($context['to_install']['based_on'] != '')
  285. {
  286. $context['to_install']['based_on'] = preg_replace('~[^A-Za-z0-9\-_ ]~', '', $context['to_install']['based_on']);
  287. // Get the theme info first.
  288. $request = $smcFunc['db_query']('', '
  289. SELECT id_theme
  290. FROM {db_prefix}themes
  291. WHERE id_member = {int:no_member}
  292. AND (value LIKE {string:based_on} OR value LIKE {string:based_on_path})
  293. LIMIT 1',
  294. array(
  295. 'no_member' => 0,
  296. 'based_on' => '%/' . $context['to_install']['based_on'],
  297. 'based_on_path' => '%' . "\\" . $context['to_install']['based_on'],
  298. )
  299. );
  300. $based_on = $smcFunc['db_fetch_assoc']($request);
  301. $smcFunc['db_free_result']($request);
  302. $request = $smcFunc['db_query']('', '
  303. SELECT variable, value
  304. FROM {db_prefix}themes
  305. WHERE variable IN ({array_string:theme_values})
  306. AND id_theme = ({int:based_on})
  307. LIMIT 1',
  308. array(
  309. 'no_member' => 0,
  310. 'theme__values' => array('theme_url', 'images_url', 'theme_dir',),
  311. 'based_on' => $based_on['id_theme'],
  312. )
  313. );
  314. $temp = $smcFunc['db_fetch_assoc']($request);
  315. $smcFunc['db_free_result']($request);
  316. // Found the based on theme info, add it to the current one being installed.
  317. if (is_array($temp))
  318. {
  319. $context['to_install']['base_theme_url'] = $temp['theme_url'];
  320. $context['to_install']['base_theme_dir'] = $temp['theme_dir'];
  321. if (empty($explicit_images) && !empty($context['to_install']['base_theme_url']))
  322. $context['to_install']['theme_url'] = $context['to_install']['base_theme_url'];
  323. }
  324. // Nope, sorry, couldn't find any theme already installed.
  325. else
  326. fatal_lang_error('package_get_error_theme_no_based_on_found', false, $context['to_install']['based_on']);
  327. }
  328. unset($context['to_install']['based_on']);
  329. }
  330. // Find the newest id_theme.
  331. $result = $smcFunc['db_query']('', '
  332. SELECT MAX(id_theme)
  333. FROM {db_prefix}themes',
  334. array(
  335. )
  336. );
  337. list ($id_theme) = $smcFunc['db_fetch_row']($result);
  338. $smcFunc['db_free_result']($result);
  339. // This will be theme number...
  340. $id_theme++;
  341. // Last minute changes? although, the actual array is a context value you might want to use the new ID.
  342. call_integration_hook('integrate_theme_install', array(&$context['to_install'], $id_theme));
  343. $inserts = array();
  344. foreach ($context['to_install'] as $var => $val)
  345. $inserts[] = array($id_theme, $var, $val);
  346. if (!empty($inserts))
  347. $smcFunc['db_insert']('insert',
  348. '{db_prefix}themes',
  349. array('id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  350. $inserts,
  351. array('id_theme', 'variable')
  352. );
  353. // Update the known and enable Theme's settings.
  354. $known = strtr($modSettings['knownThemes'] . ',' . $id_theme, array(',,' => ','));
  355. $enable = strtr($modSettings['enableThemes'] . ',' . $id_theme, array(',,' => ','));
  356. updateSettings(array('knownThemes' => $enable, 'enableThemes' => $known));
  357. return $id_theme;
  358. }
  359. /**
  360. * Removes a directory from the themes dir.
  361. *
  362. * This is a recursive function, it will call itself if there are subdirs inside the main directory.
  363. * @param string $path The absolute path to the directory to be removed
  364. * @return bool true when success, false on error.
  365. */
  366. function remove_dir($path)
  367. {
  368. if (empty($path))
  369. return false;
  370. if (is_dir($path))
  371. {
  372. $objects = scandir($path);
  373. foreach ($objects as $object)
  374. if ($object != '.' && $object != '..')
  375. {
  376. if (filetype($path .'/'. $object) == 'dir')
  377. remove_dir($path .'/'.$object);
  378. else
  379. unlink($path .'/'. $object);
  380. }
  381. }
  382. reset($objects);
  383. rmdir($path);
  384. }
  385. /**
  386. * Removes a theme from the DB, includes all possible places where the theme might be used.
  387. *
  388. * @param int $themeID The theme ID
  389. * @return bool true when success, false on error.
  390. */
  391. function remove_theme($themeID)
  392. {
  393. global $smcFunc, $modSetting;
  394. // Can't delete the default theme, sorry!
  395. if (empty($themeID) || $themeID == 1)
  396. return false;
  397. $known = explode(',', $modSettings['knownThemes']);
  398. $enable = explode(',', $modSettings['enableThemes']);
  399. // Remove it from the themes table.
  400. $smcFunc['db_query']('', '
  401. DELETE FROM {db_prefix}themes
  402. WHERE id_theme = {int:current_theme}',
  403. array(
  404. 'current_theme' => $themeID,
  405. )
  406. );
  407. // Update users preferences.
  408. $smcFunc['db_query']('', '
  409. UPDATE {db_prefix}members
  410. SET id_theme = {int:default_theme}
  411. WHERE id_theme = {int:current_theme}',
  412. array(
  413. 'default_theme' => 0,
  414. 'current_theme' => $themeID,
  415. )
  416. );
  417. // Some boards may have it as preferred theme.
  418. $smcFunc['db_query']('', '
  419. UPDATE {db_prefix}boards
  420. SET id_theme = {int:default_theme}
  421. WHERE id_theme = {int:current_theme}',
  422. array(
  423. 'default_theme' => 0,
  424. 'current_theme' => $themeID,
  425. )
  426. );
  427. // Remove it from the list of known themes.
  428. $known = array_diff($known, array($themeID));
  429. // And the enable list too.
  430. $enable = array_diff($enable, array($themeID));
  431. // Back to good old comma separated string.
  432. $known = strtr(implode(',', $known), array(',,' => ','));
  433. $enable = strtr(implode(',', $enable), array(',,' => ','));
  434. // Update the enableThemes list.
  435. updateSettings(array('enableThemes' => $enable, 'knownThemes' => $known));
  436. // Fix it if the theme was the overall default theme.
  437. if ($modSettings['theme_guests'] == $themeID)
  438. updateSettings(array('theme_guests' => '1'));
  439. return true;
  440. }
  441. /**
  442. * Generates a file listing for a given directory
  443. *
  444. * @param type $path
  445. * @param type $relative
  446. * @return type
  447. */
  448. function get_file_listing($path, $relative)
  449. {
  450. global $scripturl, $txt, $context;
  451. // Is it even a directory?
  452. if (!is_dir($path))
  453. fatal_lang_error('error_invalid_dir', 'critical');
  454. $dir = dir($path);
  455. $entries = array();
  456. while ($entry = $dir->read())
  457. $entries[] = $entry;
  458. $dir->close();
  459. natcasesort($entries);
  460. $listing1 = array();
  461. $listing2 = array();
  462. foreach ($entries as $entry)
  463. {
  464. // Skip all dot files, including .htaccess.
  465. if (substr($entry, 0, 1) == '.' || $entry == 'CVS')
  466. continue;
  467. if (is_dir($path . '/' . $entry))
  468. $listing1[] = array(
  469. 'filename' => $entry,
  470. 'is_writable' => is_writable($path . '/' . $entry),
  471. 'is_directory' => true,
  472. 'is_template' => false,
  473. 'is_image' => false,
  474. 'is_editable' => false,
  475. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . $relative . $entry,
  476. 'size' => '',
  477. );
  478. else
  479. {
  480. $size = filesize($path . '/' . $entry);
  481. if ($size > 2048 || $size == 1024)
  482. $size = comma_format($size / 1024) . ' ' . $txt['themeadmin_edit_kilobytes'];
  483. else
  484. $size = comma_format($size) . ' ' . $txt['themeadmin_edit_bytes'];
  485. $listing2[] = array(
  486. 'filename' => $entry,
  487. 'is_writable' => is_writable($path . '/' . $entry),
  488. 'is_directory' => false,
  489. 'is_template' => preg_match('~\.template\.php$~', $entry) != 0,
  490. 'is_image' => preg_match('~\.(jpg|jpeg|gif|bmp|png)$~', $entry) != 0,
  491. '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,
  492. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;filename=' . $relative . $entry,
  493. 'size' => $size,
  494. 'last_modified' => timeformat(filemtime($path . '/' . $entry)),
  495. );
  496. }
  497. }
  498. return array_merge($listing1, $listing2);
  499. }
  500. ?>