Subs-Themes.php 17 KB

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