Subs-Themes.php 17 KB

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