Subs-Themes.php 17 KB

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