Subs-Themes.php 17 KB

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