PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/xthemes/trunk/index.php

http://bitcero-modules.googlecode.com/
PHP | 361 lines | 291 code | 44 blank | 26 comment | 27 complexity | 7777a5716a9c800638dff6628cbdd2a8 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // $Id: index.php 14 2009-09-09 18:22:07Z i.bitcero $
  3. // --------------------------------------------------------------
  4. // xThemes for XOOPS
  5. // Module for manage themes by Red Mexico
  6. // Author: Eduardo Cortés <i.bitcero@gmail.com>
  7. // License: GPL v2
  8. // --------------------------------------------------------------
  9. define('RMCLOCATION','dashboard');
  10. require '../../include/cp_header.php';
  11. include_once 'include/functions.php';
  12. include_once 'class/theme.php';
  13. load_theme_locale($xoopsConfig['theme_set']);
  14. // Jquery
  15. function xt_show_init(){
  16. global $xoopsConfig;
  17. xt_menu_options();
  18. if (defined(RMCPATH))
  19. RMTemplate::get()->set_help('http://redmexico.com.mx/docs/xthemes');
  20. xoops_cp_header();
  21. // Check if installed theme is a valid I.Theme theme
  22. $theme_path = XOOPS_THEME_PATH.'/'.$xoopsConfig['theme_set'];
  23. $theme_url = XOOPS_THEME_URL.'/'.$xoopsConfig['theme_set'];
  24. if (false===($theme = xt_is_valid($xoopsConfig['theme_set']))){
  25. // Not a Red Mexico theme
  26. $not_valid = true;
  27. if(is_file($theme_path.'/screenshot.png')){
  28. $file = '/screenshot.png';
  29. } elseif(is_file($theme_path.'/screenshot.jpg')){
  30. $file = '/screenshot.jpg';
  31. } elseif(is_file($theme_path.'/screenshot.gif')){
  32. $file = '/screenshot.gif';
  33. } else {
  34. $file = '';
  35. }
  36. $theme_info = array(
  37. 'name' => $xoopsConfig['theme_set'],
  38. 'description' => __('This is XOOPS standar theme','xthemes'),
  39. 'version' => '',
  40. 'author' => __('Unknow','xthemes'),
  41. 'email' => __('unknow','xthemes'),
  42. 'url' => __('unknow','xthemes'),
  43. 'screenshot' => $file
  44. );
  45. } else {
  46. $not_valid = false;
  47. $theme_info = $theme->get_info();
  48. }
  49. //Search other available themes
  50. $themes = array();
  51. $i = 0; // Counter
  52. $tpath = XOOPS_ROOT_PATH.'/themes';
  53. $dir_themes = opendir(XOOPS_ROOT_PATH.'/themes');
  54. while (false !== ($t = readdir($dir_themes))){
  55. if ($t=='.' || $t=='..') continue;
  56. if (!is_dir(XOOPS_ROOT_PATH.'/themes/'.$t)) continue;
  57. if ($t==$xoopsConfig['theme_set']) continue;
  58. $cf = XOOPS_ROOT_PATH.'/themes/'.$t.'/config/theme.php';
  59. if (file_exists($cf)){
  60. include_once $cf;
  61. $valid = true;
  62. } else {
  63. $valid = false;
  64. }
  65. $rtheme = preg_replace('/\s+/', '', strtolower($t));
  66. $rtheme = str_replace('-','',$rtheme);
  67. $class = "XTheme".ucfirst($rtheme);
  68. if (class_exists($class)){
  69. $to = new $class();
  70. $themes[$i]['info'] = $to->get_info();
  71. $themes[$i]['info']['dir'] = $t;
  72. $themes[$i]['valid'] = true;
  73. } else {
  74. if(is_file($tpath."/$t/screenshot.png")){
  75. $file = "screenshot.png";
  76. } elseif(is_file($tpath."/$t/screenshot.jpg")){
  77. $file = "screenshot.jpg";
  78. } elseif(is_file($tpath."/$t/screenshot.gif")){
  79. $file = "screenshot.gif";
  80. } else {
  81. $file = '';
  82. }
  83. $themes[$i]['valid'] = false;
  84. $themes[$i]['info'] = array(
  85. 'name' => $t,
  86. 'description' => __('Not a valid <strong>xThemes</strong> Theme','xthemes'),
  87. 'version' => '',
  88. 'author' => __('Unknow','xthemes'),
  89. 'email' => __('unknow','xthemes'),
  90. 'url' => __('unknow','xthemes'),
  91. 'screenshot' => $file,
  92. 'dir' => $t
  93. );
  94. }
  95. $i++;
  96. }
  97. $xoTheme = $GLOBALS['xoTheme'];
  98. $xoTheme->addMeta('stylesheet', 'themes.css', array('href'=>'css/themes.css','type'=>'text/css'));
  99. //RMTemplate::get()->add_style('themes.css', 'xthemes');
  100. include_once 'templates/xt_index.php';
  101. xoops_cp_footer();
  102. }
  103. /**
  104. * Configure theme settings
  105. *
  106. * @var string
  107. */
  108. function xt_configure_show(){
  109. global $xoopsConfig, $xoopsTpl;
  110. if (false === ($theme = xt_is_valid($xoopsConfig['theme_set'])))
  111. redirectMsg('index.php', __('This theme cannot be configured by XThemes because is not a valid Theme','xthemes'), 1);
  112. if (!method_exists($theme, 'get_config'))
  113. redirectMsg('index.php', __('This theme has not configuration options', 'xthemes'), 1);
  114. xt_menu_options();
  115. xoops_cp_header();
  116. $xoTheme = $GLOBALS['xoTheme'];
  117. $xoTheme->addMeta('stylesheet', 'themes.css', array('href'=>'css/themes.css','type'=>'text/css'));
  118. $xoTheme->addMeta('script', 'include/js/xthemes.js', array('src'=>'include/js/xthemes.js','type'=>'text/javascript'));
  119. $element_info = $theme->get_info();
  120. $element = $xoopsConfig['theme_set'];
  121. $xt_show = 'theme';
  122. $current_settings = xt_get_current_config($xoopsConfig['theme_set'], true);
  123. include_once 'templates/xt_form_config.php';
  124. xoops_cp_footer();
  125. }
  126. /**
  127. * This funtion redirect to theme url owner
  128. */
  129. function xt_goto_url(){
  130. global $xoopsConfig;
  131. if (false === ($theme = xt_is_valid($xoopsConfig['theme_set'])))
  132. xt_redirect('index.php', 'This theme cannot be configured by XThemes because is not a valid Theme', 1);
  133. $element_info = $theme->get_info();
  134. if ($element_info['url']!=''){
  135. header('location: '.$element_info['url']);
  136. die();
  137. } else {
  138. header('location: index.php');
  139. die();
  140. }
  141. }
  142. /**
  143. * Activate a theme
  144. */
  145. function xt_install_theme(){
  146. global $xoopsConfig;
  147. $previous = $xoopsConfig['theme_set'];
  148. $dir = isset($_GET['theme']) ? $_GET['theme'] : '';
  149. $pdir = XOOPS_THEME_PATH.'/'.$dir;
  150. if (!is_file($pdir.'/config/theme.php'))
  151. redirectMsg('index.php', sprintf(__('Theme "%s" does not exists!','xthemes'), $dir), 1);
  152. if (false===($theme = xt_is_valid($dir)))
  153. redirectMsg('index.php', sprintf(__('Theme "%s" is not a valid XThemes theme!','xthemes'), $dir), 1);
  154. include_once $pdir.'/config/theme.php';
  155. $rtheme = preg_replace('/\s+/', '', strtolower($dir));
  156. $rtheme = str_replace('-','',$rtheme);
  157. $class = "XTheme".ucfirst($rtheme);
  158. if (!class_exists($class))
  159. redirectMsg('index.php', sprintf(__('Theme "%s" is not a valid XThemes theme!','xthemes'), $dir), 1);
  160. $db = Database::getInstance();
  161. //$theme = new $class();
  162. // Check requirements
  163. if (method_exists($theme, 'check_requirements')){
  164. if (!$theme->check_requirements())
  165. redirectMsg('index.php',sprintf(__('Theme "%s" return next errors:', 'xthemes'), $theme->name()).'<br /><br />'.implode("<br />", $theme->errors()), 1);
  166. }
  167. // Insert theme data
  168. if (!$db->queryF("UPDATE ".$db->prefix("config")." SET `conf_value`='$dir' WHERE `conf_name`='theme_set' AND conf_modid='0'"))
  169. redirectMsg('index.php',__('The theme has not been installed!','xthemes').'<br />'.$db->error(), 1);
  170. $db->queryF("UPDATE ".$db->prefix("config")." SET `conf_value`='".serialize(array($dir))." WHERE `conf_name`='theme_set_allowed' AND conf_modid='0'");
  171. $_SESSION['xoopsUserTheme'] = $dir;
  172. // Delete previous data if valid
  173. if (false !== ($previous = xt_is_valid($previous))){
  174. if (method_exists($previous, 'uninstall')){
  175. $messages = array();
  176. if (!$previous->uninstall()) $mssages = $previous->errors();
  177. }
  178. }
  179. // Run install method from plugin.
  180. // This method allows insertion on several data for plugin
  181. if (method_exists($theme, 'install'))
  182. $theme->install();
  183. redirectMsg('index.php', __('Theme installed successfully','xthemes').(count($messages)>0 ? " ".__("but there was some errors:",'xthemes')."<br />".implode("<br />".$messages) : ''), 0);
  184. }
  185. /**
  186. * Install a normal theme
  187. */
  188. function xt_install_normal(){
  189. global $xoopsConfig;
  190. $previous = $xoopsConfig['theme_set'];
  191. $dir = isset($_GET['theme']) ? $_GET['theme'] : '';
  192. $pdir = XOOPS_THEME_PATH.'/'.$dir;
  193. if (is_file($pdir.'/config/theme.php'))
  194. redirectMsg('index.php', sprintf(__('Ą"%s" is a xThemes theme!','xthemes'), $dir), 1);
  195. if (xt_is_valid($dir))
  196. redirectMsg('index.php', sprintf(__('Ą"%s" is a xThemes theme!','xthemes'), $dir), 1);
  197. $db = Database::getInstance();
  198. // Insert theme data
  199. if (!$db->queryF("UPDATE ".$db->prefix("config")." SET `conf_value`='$dir' WHERE `conf_name`='theme_set' AND conf_modid='0'"))
  200. redirectMsg('index.php', __('The theme has not been installedĄ','xthemes').'<br />'.$db->error(), 1);
  201. $_SESSION['xoopsUserTheme'] = $dir;
  202. // Delete previous data if valid
  203. if (false !== ($previous = xt_is_valid($previous))){
  204. if (method_exists($previous, 'uninstall')){
  205. $messages = array();
  206. if (!$previous->uninstall()) $mssages = $previous->errors();
  207. }
  208. }
  209. redirectMsg('index.php', __('Theme installed successfully','xthemes').(count($messages)>0 ? " ".__("but there was some errors:",'xthemes')."<br />".implode("<br />".$messages) : ''), 0);
  210. }
  211. /**
  212. * Check the settings and save data
  213. */
  214. function xt_save_settings(){
  215. global $xoopsConfig;
  216. $myts = MyTextSanitizer::getInstance();
  217. if (false === ($theme = xt_is_valid($xoopsConfig['theme_set'])))
  218. redirectMsg('index.php', __('This is a not valid theme','xthemes'), 1);
  219. $xt_to_save = array();
  220. $element = $_POST['element'];
  221. foreach ($_POST as $id => $v){
  222. if(substr($id, 0, 7)!='xtconf_') continue;
  223. if (method_exists($theme, 'verify_settings')){
  224. $xt_to_save[substr($id, 7)] = $theme->verify_settings($v);
  225. } else {
  226. $xt_to_save[substr($id, 7)] = $v;
  227. }
  228. }
  229. $db = Database::getInstance();
  230. // Delete current config
  231. $db->queryF("DELETE FROM ".$db->prefix("xtheme_config")." WHERE `element`='$element'");
  232. // Save data
  233. $sql = "INSERT INTO ".$db->prefix("xtheme_config")." (`name`,`value`,`type`,`element`) VALUES ('%s','%s','%s','$element')";
  234. $errors = array();
  235. array_walk_recursive($xt_to_save, 'clean_values');
  236. foreach ($xt_to_save as $id => $value){
  237. if (is_array($value)){
  238. $value = serialize($value);
  239. $type = 'array';
  240. } else {
  241. $value = $myts->addSlashes($value);
  242. $type = '';
  243. }
  244. if (!$db->queryF(sprintf($sql, $id, $value, $type)))
  245. $errors[] = $db->error();
  246. }
  247. RMEvents::get()->run_event('xtheme.save.settings', $xt_to_save, $errors);
  248. if (!empty($errors)){
  249. redirectMsg('index.php?op=config', __('There was errors during this operation:','xthemes').'<br />'.implode('<br />', $errors), 1);
  250. } else {
  251. redirectMsg('index.php?op=config', __('Settings updated!', 'xthemes'), false);
  252. }
  253. }
  254. function clean_values(&$value, $key){
  255. $value = htmlentities($value, ENT_QUOTES, 'UTF-8');
  256. }
  257. $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
  258. switch($op){
  259. case 'install-theme':
  260. xt_install_theme();
  261. break;
  262. case 'install-normal':
  263. xt_install_normal();
  264. break;
  265. case 'config':
  266. xt_configure_show();
  267. break;
  268. case 'save_settings':
  269. xt_save_settings();
  270. break;
  271. case 'plugins':
  272. xt_show_plugins();
  273. break;
  274. case 'activate-plugin':
  275. xt_activate_plugin();
  276. break;
  277. case 'config-plugin':
  278. xt_configure_plugin();
  279. break;
  280. case 'url':
  281. xt_goto_url();
  282. break;
  283. default:
  284. xt_show_init();
  285. break;
  286. }