Subs-Themes.php 17 KB

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