2
0

Subs-Themes.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 1: // Got a newer version, update the old entry.
  251. $smcFunc['db_query']('', '
  252. UPDATE {db_prefix}themes
  253. SET value = {string:new_value}
  254. WHERE variable = {string:version}
  255. AND id_theme = {int:id_theme}',
  256. array(
  257. 'new_value' => $context['to_install']['version'],
  258. 'version' => 'version',
  259. 'id_theme' => $to_update['id_theme'],
  260. )
  261. );
  262. // Done with the update, tell the user about it.
  263. $context['to_install']['updated'] = true;
  264. return $to_update['id_theme'];
  265. break; // Just for reference.
  266. case 0: // This is exactly the same theme.
  267. case -1: // The one being installed is older than the one already installed.
  268. default: // Any other possible result.
  269. fatal_lang_error('package_get_error_theme_no_new_version', false, array($context['to_install']['version'], $to_update['version']));
  270. }
  271. }
  272. if (!empty($context['to_install']['based_on']))
  273. {
  274. // No need for elaborated stuff when the theme is based on the default one.
  275. if ($context['to_install']['based_on'] == 'default')
  276. {
  277. $context['to_install']['theme_url'] = $settings['default_theme_url'];
  278. $context['to_install']['images_url'] = $settings['default_images_url'];
  279. }
  280. // Custom theme based on another custom theme, lets get some info.
  281. elseif ($context['to_install']['based_on'] != '')
  282. {
  283. $context['to_install']['based_on'] = preg_replace('~[^A-Za-z0-9\-_ ]~', '', $context['to_install']['based_on']);
  284. // Get the theme info first.
  285. $request = $smcFunc['db_query']('', '
  286. SELECT id_theme
  287. FROM {db_prefix}themes
  288. WHERE id_member = {int:no_member}
  289. AND (value LIKE {string:based_on} OR value LIKE {string:based_on_path})
  290. LIMIT 1',
  291. array(
  292. 'no_member' => 0,
  293. 'based_on' => '%/' . $context['to_install']['based_on'],
  294. 'based_on_path' => '%' . "\\" . $context['to_install']['based_on'],
  295. )
  296. );
  297. $based_on = $smcFunc['db_fetch_assoc']($request);
  298. $smcFunc['db_free_result']($request);
  299. $request = $smcFunc['db_query']('', '
  300. SELECT variable, value
  301. FROM {db_prefix}themes
  302. WHERE variable IN ({array_string:theme_values})
  303. AND id_theme = ({int:based_on})
  304. LIMIT 1',
  305. array(
  306. 'no_member' => 0,
  307. 'theme__values' => array('theme_url', 'images_url', 'theme_dir',),
  308. 'based_on' => $based_on['id_theme'],
  309. )
  310. );
  311. $temp = $smcFunc['db_fetch_assoc']($request);
  312. $smcFunc['db_free_result']($request);
  313. // Found the based on theme info, add it to the current one being installed.
  314. if (is_array($temp))
  315. {
  316. $context['to_install']['base_theme_url'] = $temp['theme_url'];
  317. $context['to_install']['base_theme_dir'] = $temp['theme_dir'];
  318. if (empty($explicit_images) && !empty($context['to_install']['base_theme_url']))
  319. $context['to_install']['theme_url'] = $context['to_install']['base_theme_url'];
  320. }
  321. // Nope, sorry, couldn't find any theme already installed.
  322. else
  323. fatal_lang_error('package_get_error_theme_no_based_on_found', false, $context['to_install']['based_on']);
  324. }
  325. unset($context['to_install']['based_on']);
  326. }
  327. // Find the newest id_theme.
  328. $result = $smcFunc['db_query']('', '
  329. SELECT MAX(id_theme)
  330. FROM {db_prefix}themes',
  331. array(
  332. )
  333. );
  334. list ($id_theme) = $smcFunc['db_fetch_row']($result);
  335. $smcFunc['db_free_result']($result);
  336. // This will be theme number...
  337. $id_theme++;
  338. // Last minute changes? although, the actual array is a context value you might want to use the new ID.
  339. call_integration_hook('integrate_theme_install', array(&$context['to_install'], $id_theme));
  340. $inserts = array();
  341. foreach ($context['to_install'] as $var => $val)
  342. $inserts[] = array($id_theme, $var, $val);
  343. if (!empty($inserts))
  344. $smcFunc['db_insert']('insert',
  345. '{db_prefix}themes',
  346. array('id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  347. $inserts,
  348. array('id_theme', 'variable')
  349. );
  350. // Update the known and enable Theme's settings.
  351. $known = strtr($modSettings['knownThemes'] . ',' . $id_theme, array(',,' => ','));
  352. $enable = strtr($modSettings['enableThemes'] . ',' . $id_theme, array(',,' => ','));
  353. updateSettings(array('knownThemes' => $known, 'enableThemes' => $enable));
  354. return $id_theme;
  355. }
  356. /**
  357. * Removes a directory from the themes dir.
  358. *
  359. * This is a recursive function, it will call itself if there are subdirs inside the main directory.
  360. * @param string $path The absolute path to the directory to be removed
  361. * @return bool true when success, false on error.
  362. */
  363. function remove_dir($path)
  364. {
  365. if (empty($path))
  366. return false;
  367. if (is_dir($path))
  368. {
  369. $objects = scandir($path);
  370. foreach ($objects as $object)
  371. if ($object != '.' && $object != '..')
  372. {
  373. if (filetype($path .'/'. $object) == 'dir')
  374. remove_dir($path .'/'.$object);
  375. else
  376. unlink($path .'/'. $object);
  377. }
  378. }
  379. reset($objects);
  380. rmdir($path);
  381. }
  382. /**
  383. * Removes a theme from the DB, includes all possible places where the theme might be used.
  384. *
  385. * @param int $themeID The theme ID
  386. * @return bool true when success, false on error.
  387. */
  388. function remove_theme($themeID)
  389. {
  390. global $smcFunc, $modSetting;
  391. // Can't delete the default theme, sorry!
  392. if (empty($themeID) || $themeID == 1)
  393. return false;
  394. $known = explode(',', $modSettings['knownThemes']);
  395. $enable = explode(',', $modSettings['enableThemes']);
  396. // Remove it from the themes table.
  397. $smcFunc['db_query']('', '
  398. DELETE FROM {db_prefix}themes
  399. WHERE id_theme = {int:current_theme}',
  400. array(
  401. 'current_theme' => $themeID,
  402. )
  403. );
  404. // Update users preferences.
  405. $smcFunc['db_query']('', '
  406. UPDATE {db_prefix}members
  407. SET id_theme = {int:default_theme}
  408. WHERE id_theme = {int:current_theme}',
  409. array(
  410. 'default_theme' => 0,
  411. 'current_theme' => $themeID,
  412. )
  413. );
  414. // Some boards may have it as preferred theme.
  415. $smcFunc['db_query']('', '
  416. UPDATE {db_prefix}boards
  417. SET id_theme = {int:default_theme}
  418. WHERE id_theme = {int:current_theme}',
  419. array(
  420. 'default_theme' => 0,
  421. 'current_theme' => $themeID,
  422. )
  423. );
  424. // Remove it from the list of known themes.
  425. $known = array_diff($known, array($themeID));
  426. // And the enable list too.
  427. $enable = array_diff($enable, array($themeID));
  428. // Back to good old comma separated string.
  429. $known = strtr(implode(',', $known), array(',,' => ','));
  430. $enable = strtr(implode(',', $enable), array(',,' => ','));
  431. // Update the enableThemes list.
  432. updateSettings(array('enableThemes' => $enable, 'knownThemes' => $known));
  433. // Fix it if the theme was the overall default theme.
  434. if ($modSettings['theme_guests'] == $themeID)
  435. updateSettings(array('theme_guests' => '1'));
  436. return true;
  437. }
  438. /**
  439. * Generates a file listing for a given directory
  440. *
  441. * @param type $path
  442. * @param type $relative
  443. * @return type
  444. */
  445. function get_file_listing($path, $relative)
  446. {
  447. global $scripturl, $txt, $context;
  448. // Is it even a directory?
  449. if (!is_dir($path))
  450. fatal_lang_error('error_invalid_dir', 'critical');
  451. $dir = dir($path);
  452. $entries = array();
  453. while ($entry = $dir->read())
  454. $entries[] = $entry;
  455. $dir->close();
  456. natcasesort($entries);
  457. $listing1 = array();
  458. $listing2 = array();
  459. foreach ($entries as $entry)
  460. {
  461. // Skip all dot files, including .htaccess.
  462. if (substr($entry, 0, 1) == '.' || $entry == 'CVS')
  463. continue;
  464. if (is_dir($path . '/' . $entry))
  465. $listing1[] = array(
  466. 'filename' => $entry,
  467. 'is_writable' => is_writable($path . '/' . $entry),
  468. 'is_directory' => true,
  469. 'is_template' => false,
  470. 'is_image' => false,
  471. 'is_editable' => false,
  472. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . $relative . $entry,
  473. 'size' => '',
  474. );
  475. else
  476. {
  477. $size = filesize($path . '/' . $entry);
  478. if ($size > 2048 || $size == 1024)
  479. $size = comma_format($size / 1024) . ' ' . $txt['themeadmin_edit_kilobytes'];
  480. else
  481. $size = comma_format($size) . ' ' . $txt['themeadmin_edit_bytes'];
  482. $listing2[] = array(
  483. 'filename' => $entry,
  484. 'is_writable' => is_writable($path . '/' . $entry),
  485. 'is_directory' => false,
  486. 'is_template' => preg_match('~\.template\.php$~', $entry) != 0,
  487. 'is_image' => preg_match('~\.(jpg|jpeg|gif|bmp|png)$~', $entry) != 0,
  488. '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,
  489. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;filename=' . $relative . $entry,
  490. 'size' => $size,
  491. 'last_modified' => timeformat(filemtime($path . '/' . $entry)),
  492. );
  493. }
  494. }
  495. return array_merge($listing1, $listing2);
  496. }
  497. ?>