PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/admin/ThemeManager.php

https://code.google.com/p/enanocms/
PHP | 428 lines | 352 code | 52 blank | 24 comment | 70 complexity | 3229ba10bc39b37ff5babb31c66dc192 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
  4. * Copyright (C) 2006-2009 Dan Fuhry
  5. *
  6. * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  10. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
  11. */
  12. function page_Admin_ThemeManager($force_no_json = false)
  13. {
  14. global $db, $session, $paths, $template, $plugins; // Common objects
  15. global $lang;
  16. global $cache;
  17. if ( $session->auth_level < USER_LEVEL_ADMIN || $session->user_level < USER_LEVEL_ADMIN )
  18. {
  19. $login_link = makeUrlNS('Special', 'Login/' . $paths->nslist['Special'] . 'Administration', 'level=' . USER_LEVEL_ADMIN, true);
  20. echo '<h3>' . $lang->get('adm_err_not_auth_title') . '</h3>';
  21. echo '<p>' . $lang->get('adm_err_not_auth_body', array( 'login_link' => $login_link )) . '</p>';
  22. return;
  23. }
  24. $system_themes =& $template->system_themes;
  25. // Obtain the list of themes (both available and already installed) and the styles available for each
  26. $dh = @opendir(ENANO_ROOT . '/themes');
  27. if ( !$dh )
  28. die('Couldn\'t open themes directory');
  29. $themes = array();
  30. while ( $dr = @readdir($dh) )
  31. {
  32. if ( $dr == '.' || $dr == '..' )
  33. continue;
  34. if ( !is_dir(ENANO_ROOT . "/themes/$dr") )
  35. continue;
  36. if ( !file_exists(ENANO_ROOT . "/themes/$dr/theme.cfg") || !is_dir(ENANO_ROOT . "/themes/$dr/css") )
  37. continue;
  38. $cdh = @opendir(ENANO_ROOT . "/themes/$dr/css");
  39. if ( !$cdh )
  40. continue;
  41. require(ENANO_ROOT . "/themes/$dr/theme.cfg");
  42. global $theme;
  43. $themes[$dr] = array(
  44. 'css' => array(),
  45. 'theme_name' => $theme['theme_name']
  46. );
  47. while ( $cdr = @readdir($cdh) )
  48. {
  49. if ( $cdr == '.' || $cdr == '..' )
  50. continue;
  51. if ( preg_match('/\.css$/i', $cdr) )
  52. $themes[$dr]['css'][] = substr($cdr, 0, -4);
  53. }
  54. }
  55. // Decide which themes are not installed
  56. $installable = array_flip(array_keys($themes));
  57. // FIXME: sanitize directory names or check with preg_match()
  58. $where_clause = 'theme_id = \'' . implode('\' OR theme_id = \'', array_flip($installable)) . '\'';
  59. $q = $db->sql_query('SELECT theme_id, theme_name, enabled FROM ' . table_prefix . "themes WHERE $where_clause;");
  60. if ( !$q )
  61. $db->_die();
  62. while ( $row = $db->fetchrow() )
  63. {
  64. $tid =& $row['theme_id'];
  65. unset($installable[$tid]);
  66. $themes[$tid]['theme_name'] = $row['theme_name'];
  67. $themes[$tid]['enabled'] = ( $row['enabled'] == 1 );
  68. }
  69. foreach ( $system_themes as $st )
  70. {
  71. unset($installable[$st]);
  72. }
  73. $installable = array_flip($installable);
  74. // AJAX code
  75. if ( $paths->getParam(0) === 'action.json' && !$force_no_json )
  76. {
  77. return ajaxServlet_Admin_ThemeManager($themes);
  78. }
  79. // List installed themes
  80. ?>
  81. <div style="float: right;">
  82. <a href="#" id="systheme_toggler" onclick="ajaxToggleSystemThemes(); return false;"><?php echo $lang->get('acptm_btn_system_themes_show'); ?></a>
  83. </div>
  84. <?php
  85. echo '<h3>' . $lang->get('acptm_heading_edit_themes') . '</h3>';
  86. echo '<div id="theme_list_edit">';
  87. foreach ( $themes as $theme_id => $theme_data )
  88. {
  89. if ( in_array($theme_id, $installable) )
  90. continue;
  91. if ( file_exists(ENANO_ROOT . "/themes/$theme_id/preview.png") )
  92. {
  93. $preview_path = scriptPath . "/themes/$theme_id/preview.png";
  94. }
  95. else
  96. {
  97. $preview_path = scriptPath . "/images/themepreview.png";
  98. }
  99. $d = ( @$theme_data['enabled'] ) ? '' : ' themebutton_theme_disabled';
  100. $st = ( in_array($theme_id, $system_themes) ) ? ' themebutton_theme_system' : '';
  101. echo '<div class="themebutton' . $st . '' . $d . '" id="themebtn_edit_' . $theme_id . '" style="background-image: url(' . $preview_path . ');">';
  102. if ( in_array($theme_id, $system_themes) )
  103. {
  104. echo '<a class="tb-inner" href="#" onclick="return false;">
  105. ' . $lang->get('acptm_btn_theme_system') . '
  106. <span class="themename">' . htmlspecialchars($theme_data['theme_name']) . '</span>
  107. </a>';
  108. }
  109. else
  110. {
  111. echo '<a class="tb-inner" href="#" onclick="ajaxEditTheme(\'' . $theme_id . '\'); return false;">
  112. ' . $lang->get('acptm_btn_theme_edit') . '
  113. <span class="themename">' . htmlspecialchars($theme_data['theme_name']) . '</span>
  114. </a>';
  115. }
  116. echo '</div>';
  117. }
  118. echo '</div>';
  119. echo '<span class="menuclear"></span>';
  120. if ( count($installable) > 0 )
  121. {
  122. echo '<h3>' . $lang->get('acptm_heading_install_themes') . '</h3>';
  123. echo '<div id="theme_list_install">';
  124. foreach ( $installable as $i => $theme_id )
  125. {
  126. if ( file_exists(ENANO_ROOT . "/themes/$theme_id/preview.png") )
  127. {
  128. $preview_path = scriptPath . "/themes/$theme_id/preview.png";
  129. }
  130. else
  131. {
  132. $preview_path = scriptPath . "/images/themepreview.png";
  133. }
  134. echo '<div class="themebutton" id="themebtn_install_' . $theme_id . '" enano:themename="' . htmlspecialchars($themes[$theme_id]['theme_name']) . '" style="background-image: url(' . $preview_path . ');">';
  135. echo '<a class="tb-inner" href="#" onclick="ajaxInstallTheme(\'' . $theme_id . '\'); return false;">
  136. ' . $lang->get('acptm_btn_theme_install') . '
  137. <span class="themename">' . htmlspecialchars($themes[$theme_id]['theme_name']) . '</span>
  138. </a>';
  139. echo '</div>';
  140. }
  141. echo '</div>';
  142. echo '<span class="menuclear"></span>';
  143. }
  144. }
  145. function ajaxServlet_Admin_ThemeManager(&$themes)
  146. {
  147. global $db, $session, $paths, $template, $plugins; // Common objects
  148. global $lang;
  149. global $cache;
  150. if ( $session->auth_level < USER_LEVEL_ADMIN || $session->user_level < USER_LEVEL_ADMIN )
  151. {
  152. $login_link = makeUrlNS('Special', 'Login/' . $paths->nslist['Special'] . 'Administration', 'level=' . USER_LEVEL_ADMIN, true);
  153. echo '<h3>' . $lang->get('adm_err_not_auth_title') . '</h3>';
  154. echo '<p>' . $lang->get('adm_err_not_auth_body', array( 'login_link' => $login_link )) . '</p>';
  155. return;
  156. }
  157. if ( !isset($_POST['r']) )
  158. return false;
  159. try
  160. {
  161. $request = enano_json_decode($_POST['r']);
  162. }
  163. catch ( Exception $e )
  164. {
  165. die('Exception in JSON parser, probably invalid input.');
  166. }
  167. if ( !isset($request['mode']) )
  168. {
  169. die('No mode specified in JSON request.');
  170. }
  171. switch ( $request['mode'] )
  172. {
  173. case 'fetch_theme':
  174. $theme_id = $db->escape($request['theme_id']);
  175. if ( empty($theme_id) )
  176. die('Invalid theme_id');
  177. $q = $db->sql_query("SELECT theme_id, theme_name, default_style, enabled, group_policy, group_list FROM " . table_prefix . "themes WHERE theme_id = '$theme_id';");
  178. if ( !$q )
  179. $db->die_json();
  180. if ( $db->numrows() < 1 )
  181. die('BUG: no theme with that theme_id installed.');
  182. $row = $db->fetchrow();
  183. $row['enabled'] = ( $row['enabled'] == 1 );
  184. $row['css'] = @$themes[$theme_id]['css'];
  185. $row['default_style'] = preg_replace('/\.css$/', '', $row['default_style']);
  186. $row['is_default'] = ( getConfig('theme_default') === $theme_id );
  187. $row['group_list'] = ( empty($row['group_list']) ) ? array() : enano_json_decode($row['group_list']);
  188. // Build a list of group names
  189. $row['group_names'] = array();
  190. $q = $db->sql_query('SELECT group_id, group_name FROM ' . table_prefix . 'groups;');
  191. if ( !$q )
  192. $db->die_json();
  193. while ( $gr = $db->fetchrow() )
  194. {
  195. $row['group_names'][ intval($gr['group_id']) ] = $gr['group_name'];
  196. }
  197. $db->free_result();
  198. // Build a list of usernames
  199. $row['usernames'] = array();
  200. foreach ( $row['group_list'] as $el )
  201. {
  202. if ( !preg_match('/^u:([0-9]+)$/', $el, $match) )
  203. continue;
  204. $uid =& $match[1];
  205. $q = $db->sql_query('SELECT username FROM ' . table_prefix . "users WHERE user_id = $uid;");
  206. if ( !$q )
  207. $db->die_json();
  208. if ( $db->numrows() < 1 )
  209. {
  210. $db->free_result();
  211. continue;
  212. }
  213. list($username) = $db->fetchrow_num();
  214. $row['usernames'][$uid] = $username;
  215. $db->free_result();
  216. }
  217. echo enano_json_encode($row);
  218. break;
  219. case 'uid_lookup':
  220. $username = @$request['username'];
  221. if ( empty($username) )
  222. {
  223. die(enano_json_encode(array(
  224. 'mode' => 'error',
  225. 'error' => $lang->get('acptm_err_invalid_username')
  226. )));
  227. }
  228. $username = $db->escape(strtolower($username));
  229. $q = $db->sql_query('SELECT user_id, username FROM ' . table_prefix . "users WHERE " . ENANO_SQLFUNC_LOWERCASE . "(username) = '$username';");
  230. if ( !$q )
  231. $db->die_json();
  232. if ( $db->numrows() < 1 )
  233. {
  234. die(enano_json_encode(array(
  235. 'mode' => 'error',
  236. 'error' => $lang->get('acptm_err_username_not_found')
  237. )));
  238. }
  239. list($uid, $username_real) = $db->fetchrow_num();
  240. $db->free_result();
  241. echo enano_json_encode(array(
  242. 'uid' => $uid,
  243. 'username' => $username_real
  244. ));
  245. break;
  246. case 'save_theme':
  247. if ( !isset($request['theme_data']) )
  248. {
  249. die(enano_json_encode(array(
  250. 'mode' => 'error',
  251. 'error' => 'No theme data in request'
  252. )));
  253. }
  254. $theme_data =& $request['theme_data'];
  255. // Perform integrity check on theme data
  256. $chk_theme_exists = isset($themes[@$theme_data['theme_id']]);
  257. $theme_data['theme_name'] = trim(@$theme_data['theme_name']);
  258. $chk_name_good = !empty($theme_data['theme_name']);
  259. $chk_policy_good = in_array(@$theme_data['group_policy'], array('allow_all', 'whitelist', 'blacklist'));
  260. $chk_grouplist_good = true;
  261. foreach ( $theme_data['group_list'] as $acl_entry )
  262. {
  263. if ( !preg_match('/^(u|g):[0-9]+$/', $acl_entry) )
  264. {
  265. $chk_grouplist_good = false;
  266. break;
  267. }
  268. }
  269. $chk_style_good = @in_array(@$theme_data['default_style'], @$themes[@$theme_data['theme_id']]['css']);
  270. if ( !$chk_theme_exists || !$chk_name_good || !$chk_policy_good || !$chk_grouplist_good || !$chk_style_good )
  271. {
  272. die(enano_json_encode(array(
  273. 'mode' => 'error',
  274. 'error' => $lang->get('acptm_err_save_validation_failed')
  275. )));
  276. }
  277. $enable = ( $theme_data['enabled'] ) ? '1' : '0';
  278. $theme_default = getConfig('theme_default');
  279. $warn_default = ( $theme_default === $theme_data['theme_id'] || $theme_data['make_default'] ) ?
  280. ' ' . $lang->get('acptm_warn_access_with_default') . ' ' :
  281. ' ';
  282. if ( $enable == 0 && ( $theme_default === $theme_data['theme_id'] || $theme_data['make_default'] ) )
  283. {
  284. $enable = '1';
  285. $warn_default .= '<b>' . $lang->get('acptm_warn_cant_disable_default') . '</b>';
  286. }
  287. // We're good. Update the theme...
  288. $q = $db->sql_query('UPDATE ' . table_prefix . 'themes SET
  289. theme_name = \'' . $db->escape($theme_data['theme_name']) . '\',
  290. default_style = \'' . $db->escape($theme_data['default_style']) . '\',
  291. group_list = \'' . $db->escape(enano_json_encode($theme_data['group_list'])) . '\',
  292. group_policy = \'' . $db->escape($theme_data['group_policy']) . '\',
  293. enabled = ' . $enable . '
  294. WHERE theme_id = \'' . $db->escape($theme_data['theme_id']) . '\';');
  295. if ( !$q )
  296. $db->die_json();
  297. if ( $theme_data['make_default'] && !defined('ENANO_DEMO_MODE') )
  298. {
  299. setConfig('theme_default', $theme_data['theme_id']);
  300. }
  301. $cache->purge('themes');
  302. echo '<div class="info-box"><b>' . $lang->get('acptm_msg_save_success') . '</b>' . $warn_default . '</div>';
  303. page_Admin_ThemeManager(true);
  304. break;
  305. case 'install':
  306. $theme_id =& $request['theme_id'];
  307. if ( !isset($themes[$theme_id]) )
  308. {
  309. die(enano_json_encode(array(
  310. 'mode' => 'error',
  311. 'error' => 'Theme was deleted from themes/ directory or couldn\'t read theme metadata from filesystem'
  312. )));
  313. }
  314. if ( !isset($themes[$theme_id]['css'][0]) )
  315. {
  316. die(enano_json_encode(array(
  317. 'mode' => 'error',
  318. 'error' => 'Theme doesn\'t have any files in css/, thus it can\'t be installed. (translators: l10n?)'
  319. )));
  320. }
  321. // build dataset
  322. $theme_name = $db->escape($themes[$theme_id]['theme_name']);
  323. $default_style = $db->escape($themes[$theme_id]['css'][0]);
  324. $theme_id = $db->escape($theme_id);
  325. // insert it
  326. $q = $db->sql_query('INSERT INTO ' . table_prefix . "themes(theme_id, theme_name, default_style, enabled, group_list, group_policy)\n"
  327. . " VALUES( '$theme_id', '$theme_name', '$default_style', 1, '[]', 'allow_all' );");
  328. if ( !$q )
  329. $db->die_json();
  330. $cache->purge('themes');
  331. // The response isn't processed unless it's in JSON.
  332. echo 'Roger that, over and out.';
  333. break;
  334. case 'uninstall':
  335. $theme_id =& $request['theme_id'];
  336. $theme_default = getConfig('theme_default');
  337. // Validation
  338. if ( !isset($themes[$theme_id]) )
  339. {
  340. die(enano_json_encode(array(
  341. 'mode' => 'error',
  342. 'error' => 'Theme was deleted from themes/ directory or couldn\'t read theme metadata from filesystem'
  343. )));
  344. }
  345. if ( $theme_id == $theme_default )
  346. {
  347. die(enano_json_encode(array(
  348. 'mode' => 'error',
  349. 'error' => $lang->get('acptm_err_uninstalling_default')
  350. )));
  351. }
  352. if ( $theme_id == 'oxygen' )
  353. {
  354. die(enano_json_encode(array(
  355. 'mode' => 'error',
  356. 'error' => $lang->get('acptm_err_uninstalling_oxygen')
  357. )));
  358. }
  359. $theme_id = $db->escape($theme_id);
  360. $q = $db->sql_query('DELETE FROM ' . table_prefix . "themes WHERE theme_id = '$theme_id';");
  361. if ( !$q )
  362. $db->die_json();
  363. $cache->purge('themes');
  364. // Change all the users that were on that theme to the default
  365. $default_style = $template->named_theme_list[$theme_default]['default_style'];
  366. $default_style = preg_replace('/\.css$/', '', $default_style);
  367. $theme_default = $db->escape($theme_default);
  368. $default_style = $db->escape($default_style);
  369. $q = $db->sql_query('UPDATE ' . table_prefix . "users SET theme = '$theme_default', style = '$default_style' WHERE theme = '$theme_id';");
  370. if ( !$q )
  371. $db->die_json();
  372. echo '<div class="info-box">' . $lang->get('acptm_msg_uninstall_success') . '</div>';
  373. page_Admin_ThemeManager(true);
  374. break;
  375. }
  376. }