Subs-Themes.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 user its 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. updateSettings(array('knownThemes' => strtr($modSettings['knownThemes'] . ',' . $id_theme, array(',,' => ','))));
  355. updateSettings(array('enableThemes' => strtr($modSettings['enableThemes'] . ',' . $id_theme, array(',,' => ','))));
  356. return $id_theme;
  357. }
  358. /**
  359. * Removes a directory from the themes dir.
  360. *
  361. * This is a recursive function, it will call itself if there are subdirs inside the main directory.
  362. * @param string $path The absolute path to the directory to be removed
  363. * @return bool true when success, false on error.
  364. */
  365. function remove_dir($path)
  366. {
  367. if (empty($path))
  368. return false;
  369. if (is_dir($path))
  370. {
  371. $objects = scandir($path);
  372. foreach ($objects as $object)
  373. if ($object != '.' && $object != '..')
  374. {
  375. if (filetype($path .'/'. $object) == 'dir')
  376. remove_dir($path .'/'.$object);
  377. else
  378. unlink($path .'/'. $object);
  379. }
  380. }
  381. reset($objects);
  382. rmdir($path);
  383. }
  384. /**
  385. * Removes a theme from the DB, includes all possible places where the theme might be used.
  386. *
  387. * @param int $themeID The theme ID
  388. * @return bool true when success, false on error.
  389. */
  390. function remove_theme($themeID)
  391. {
  392. global $smcFunc, $modSetting;
  393. // Can't delete the default theme, sorry!
  394. if (empty($themeID) || $themeID == 1)
  395. return false;
  396. $known = explode(',', $modSettings['knownThemes']);
  397. $enable = explode(',', $modSettings['enableThemes']);
  398. // Remove it from the themes table.
  399. $smcFunc['db_query']('', '
  400. DELETE FROM {db_prefix}themes
  401. WHERE id_theme = {int:current_theme}',
  402. array(
  403. 'current_theme' => $themeID,
  404. )
  405. );
  406. // Update users preferences.
  407. $smcFunc['db_query']('', '
  408. UPDATE {db_prefix}members
  409. SET id_theme = {int:default_theme}
  410. WHERE id_theme = {int:current_theme}',
  411. array(
  412. 'default_theme' => 0,
  413. 'current_theme' => $themeID,
  414. )
  415. );
  416. // Some boards may have it as preferred theme.
  417. $smcFunc['db_query']('', '
  418. UPDATE {db_prefix}boards
  419. SET id_theme = {int:default_theme}
  420. WHERE id_theme = {int:current_theme}',
  421. array(
  422. 'default_theme' => 0,
  423. 'current_theme' => $themeID,
  424. )
  425. );
  426. // Remove it from the list of known themes.
  427. $known = array_diff($known, array($themeID));
  428. // And the enable list too.
  429. $enable = array_diff($enable, array($themeID));
  430. // Back to good old comma separated string.
  431. $known = strtr(implode(',', $known), array(',,' => ','));
  432. $enable = strtr(implode(',', $enable), array(',,' => ','));
  433. // Update the enableThemes list.
  434. updateSettings(array('enableThemes' => $enable));
  435. // Fix it if the theme was the overall default theme.
  436. if ($modSettings['theme_guests'] == $themeID)
  437. updateSettings(array('theme_guests' => '1', 'knownThemes' => $known));
  438. else
  439. updateSettings(array('knownThemes' => $known));
  440. return true;
  441. }
  442. /**
  443. * Generates a file listing for a given directory
  444. *
  445. * @param type $path
  446. * @param type $relative
  447. * @return type
  448. */
  449. function get_file_listing($path, $relative)
  450. {
  451. global $scripturl, $txt, $context;
  452. // Is it even a directory?
  453. if (!is_dir($path))
  454. fatal_lang_error('error_invalid_dir', 'critical');
  455. $dir = dir($path);
  456. $entries = array();
  457. while ($entry = $dir->read())
  458. $entries[] = $entry;
  459. $dir->close();
  460. natcasesort($entries);
  461. $listing1 = array();
  462. $listing2 = array();
  463. foreach ($entries as $entry)
  464. {
  465. // Skip all dot files, including .htaccess.
  466. if (substr($entry, 0, 1) == '.' || $entry == 'CVS')
  467. continue;
  468. if (is_dir($path . '/' . $entry))
  469. $listing1[] = array(
  470. 'filename' => $entry,
  471. 'is_writable' => is_writable($path . '/' . $entry),
  472. 'is_directory' => true,
  473. 'is_template' => false,
  474. 'is_image' => false,
  475. 'is_editable' => false,
  476. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;directory=' . $relative . $entry,
  477. 'size' => '',
  478. );
  479. else
  480. {
  481. $size = filesize($path . '/' . $entry);
  482. if ($size > 2048 || $size == 1024)
  483. $size = comma_format($size / 1024) . ' ' . $txt['themeadmin_edit_kilobytes'];
  484. else
  485. $size = comma_format($size) . ' ' . $txt['themeadmin_edit_bytes'];
  486. $listing2[] = array(
  487. 'filename' => $entry,
  488. 'is_writable' => is_writable($path . '/' . $entry),
  489. 'is_directory' => false,
  490. 'is_template' => preg_match('~\.template\.php$~', $entry) != 0,
  491. 'is_image' => preg_match('~\.(jpg|jpeg|gif|bmp|png)$~', $entry) != 0,
  492. '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,
  493. 'href' => $scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . $context['session_var'] . '=' . $context['session_id'] . ';sa=edit;filename=' . $relative . $entry,
  494. 'size' => $size,
  495. 'last_modified' => timeformat(filemtime($path . '/' . $entry)),
  496. );
  497. }
  498. }
  499. return array_merge($listing1, $listing2);
  500. }
  501. ?>