PageRenderTime 80ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Ionize/application/libraries/MY_Controller.php

http://you.googlecode.com/
PHP | 735 lines | 284 code | 163 blank | 288 comment | 36 complexity | 227ac9f12b7595a030597b462b9f3bcc MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Ionize
  4. *
  5. * @package Ionize
  6. * @author Ionize Dev Team
  7. * @license http://ionizecms.com/doc-license
  8. * @link http://ionizecms.com
  9. * @since Version 0.90
  10. */
  11. // ------------------------------------------------------------------------
  12. /**
  13. * MY_Controller Class
  14. *
  15. * Extends CodeIgniter Controller
  16. * Basic Model loads and settings set.
  17. *
  18. */
  19. class MY_Controller extends Controller
  20. {
  21. /* Template array
  22. * This array will be send to the view in case of standard output
  23. * Used by $this->output
  24. */
  25. protected $template = array();
  26. /* Default FTL tag
  27. */
  28. protected $context_tag = 'ion';
  29. /**
  30. * Contructor
  31. *
  32. */
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. // Check the database settings
  37. if ($this->test_database_config() === false)
  38. {
  39. redirect(base_url().'install/');
  40. die();
  41. }
  42. $this->load->database();
  43. // Models
  44. $this->load->model('base_model', '', true);
  45. $this->load->model('settings_model', '', true);
  46. // Helpers
  47. $this->load->helper('file');
  48. $this->load->helper('trace');
  49. /*
  50. * Language / Languages
  51. *
  52. */
  53. // Get all the website languages from DB and store them into config file "languages" key
  54. $languages = $this->settings_model->get_languages();
  55. // Put DB languages array to Settings
  56. Settings::set_languages($languages);
  57. if( Connect()->is('editors', true))
  58. {
  59. Settings::set_all_languages_online();
  60. }
  61. /*
  62. * Settings
  63. *
  64. */
  65. // Lang independant settings : google analytics string, filemanager, etc.
  66. // Each setting is accessible through :
  67. // Settings::get('setting_name');
  68. Settings::set_settings_from_list($this->settings_model->get_settings(), 'name','content');
  69. /*
  70. * Security : No access if install folder is already there
  71. *
  72. */
  73. // if (config_item('protect_installer') == TRUE)
  74. // {
  75. // Try to find the installer class
  76. $installer = glob(BASEPATH.'../*/class/installer'.EXT);
  77. // If installer class is already here, avoid site access
  78. if (!empty($installer))
  79. {
  80. // Get languages codes from availables languages folder/translation file
  81. $languages = $this->settings_model->get_admin_langs();
  82. if ( ! in_array(config_item('language_abbr'), $languages))
  83. $this->config->set_item('language_abbr', config_item('default_lang'));
  84. $this->lang->load('admin', config_item('language_abbr'));
  85. Theme::set_theme('admin');
  86. // Set the view to output
  87. $this->output('delete_installer');
  88. // Display the view directly
  89. $this->output->_display();
  90. // Don't do anything more
  91. die();
  92. }
  93. // }
  94. }
  95. // ------------------------------------------------------------------------
  96. /**
  97. * Outputs the global template regarding to the used library to do this stuff
  98. *
  99. * @param string The view name
  100. *
  101. */
  102. public function output($view)
  103. {
  104. Theme::output($view, $this->template);
  105. }
  106. // ------------------------------------------------------------------------
  107. /**
  108. * Returns true if this is an XMLHttpRequest (ie. Javascript).
  109. *
  110. * This requires a special header to be sent from the JS
  111. * (usually the Javascript frameworks' Ajax/XHR methods add it automatically):
  112. *
  113. * <code>
  114. * X-Requested-With: XMLHttpRequest
  115. * </code>
  116. *
  117. * @return bool
  118. */
  119. public function is_xhr()
  120. {
  121. return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
  122. }
  123. /**
  124. * Returns true if database settings are correct
  125. *
  126. */
  127. public function test_database_config()
  128. {
  129. require(config_item('base_path') . '/application/config/database.php');
  130. if ($db[$active_group]['hostname'] == '' || $db[$active_group]['username'] == '' || $db[$active_group]['database'] == '')
  131. {
  132. return false;
  133. }
  134. return true;
  135. }
  136. public function get_modules_config()
  137. {
  138. // Modules config include
  139. $config_files = glob(config_item('module_path').'*/config/config.php');
  140. if ( ! empty($config_files))
  141. {
  142. // Add each module config element to the main config
  143. foreach($config_files as $file)
  144. {
  145. include($file);
  146. if ( isset($config))
  147. {
  148. foreach($config as $k=>$v)
  149. $this->config->set_item($k, $v);
  150. unset($config);
  151. }
  152. }
  153. }
  154. }
  155. }
  156. // End MY_Controller
  157. // ------------------------------------------------------------------------
  158. class Base_Controller extends MY_Controller
  159. {
  160. /**
  161. * Constructor
  162. *
  163. */
  164. public function __construct()
  165. {
  166. parent::__construct();
  167. // $this->output->enable_profiler(true);
  168. // $this->output->cache(100);
  169. // Unlock filtering if admin or editor users is logged in
  170. // $this->load->library('connect');
  171. $this->connect = Connect::get_instance();
  172. // Libraries
  173. $this->load->library('structure');
  174. $this->load->library('widget');
  175. // FTL parser
  176. require_once APPPATH.'libraries/ftl/parser.php';
  177. // Models
  178. $this->load->model('structure_model', '', true);
  179. $this->load->model('menu_model', '', true);
  180. // Modules config
  181. $this->get_modules_config();
  182. /*
  183. * Theme
  184. *
  185. */
  186. // Set the current theme
  187. Theme::set_theme(Settings::get('theme'));
  188. // Theme config file
  189. // Overwrite Ionize standard config.
  190. if (is_file($file = Theme::get_theme_path().'config/config.php'))
  191. {
  192. include($file);
  193. if ( ! empty($config))
  194. {
  195. foreach($config as $k=>$v)
  196. $this->config->set_item($k, $v);
  197. unset($config);
  198. }
  199. }
  200. /*
  201. * Menus
  202. *
  203. */
  204. Settings::set('menus', $this->menu_model->get_list());
  205. /*
  206. * Language
  207. *
  208. */
  209. // Get all the website languages from DB and store them into config file "languages" key
  210. $languages = $this->settings_model->get_languages();
  211. // Put all DB languages array to Settings
  212. Settings::set_languages($languages);
  213. // Set all languages online if conected as editor or more
  214. if( Connect()->is('editors', true))
  215. {
  216. Settings::set_all_languages_online();
  217. }
  218. // Simple languages code array, used to detect if Routers found language is in DB languages
  219. $lang_codes = array();
  220. foreach(Settings::get_online_languages() as $language)
  221. {
  222. $online_lang_codes[] = $language['lang'];
  223. }
  224. // If Router detected that the lang code is not in DB languages, set it to the DB default one
  225. if ( ! in_array(config_item('language_abbr'), $online_lang_codes))
  226. {
  227. // Settings::get_lang('default') returns the DB default lang code
  228. Settings::set('current_lang', Settings::get_lang('default'));
  229. $this->config->set_item('language_abbr', Settings::get_lang('default'));
  230. }
  231. else
  232. {
  233. // Store the current lang code (found by Router) to Settings
  234. Settings::set('current_lang', config_item('language_abbr'));
  235. }
  236. // Lang dependant settings for the current language : Meta, etc.
  237. Settings::set_settings_from_list($this->settings_model->get_lang_settings(config_item('language_abbr')), 'name','content');
  238. /*
  239. * Static language
  240. *
  241. */
  242. $lang_folder = Theme::get_theme_path().'language/'.Settings::get_lang().'/';
  243. // Core languages files : Including
  244. $lang_files = glob(APPPATH.'language/'.Settings::get_lang().'/*_lang.php');
  245. // Theme languages files : Including. Can be empty
  246. $lf = glob(Theme::get_theme_path().'language/'.Settings::get_lang().'/*_lang.php');
  247. if ( !empty($lf))
  248. $lang_files = array_merge((Array)$lang_files, $lf);
  249. // Modules languages files : Including. Can be empty
  250. $mla = glob(config_item('module_path').'*/language/'.Settings::get_lang().'/*_lang.php');
  251. if ( !empty($mla))
  252. $lang_files = array_merge((Array)$lang_files, $mla);
  253. $modules = glob(config_item('module_path').'*');
  254. // Add the module path to the Finder
  255. if (!empty($modules))
  256. {
  257. foreach ($modules as $module)
  258. {
  259. Finder::add_path($module.'/');
  260. }
  261. }
  262. // Widgets languages translations loading
  263. // Now done by the Widget library
  264. // Load all modules lang files
  265. if ( ! empty($lang_files))
  266. {
  267. foreach($lang_files as $l)
  268. {
  269. if (is_file($l) && '.'.end(explode('.', $l)) == EXT )
  270. {
  271. include $l;
  272. if ( ! empty($lang))
  273. {
  274. $this->lang->language = array_merge($this->lang->language, $lang);
  275. unset($lang);
  276. }
  277. }
  278. }
  279. }
  280. }
  281. protected function parse($string, $context, $tag_prefix = 'ion')
  282. {
  283. $p = new FTL_Parser($context, array('tag_prefix' => $tag_prefix));
  284. return $p->parse($string);
  285. }
  286. protected function render($view, &$context = null, $return = false)
  287. {
  288. // Loads the view to parse
  289. $parsed = Theme::load($view);
  290. // We can now check if the file is a PHP one or a FTL one
  291. if (substr($parsed, 0, 5) == '<?php')
  292. {
  293. $parsed = $this->load->view($view, array(), true);
  294. }
  295. else
  296. {
  297. $parsed = $this->parse($parsed, $context);
  298. }
  299. // Returns the result or output it directly
  300. if ($return)
  301. return $parsed;
  302. else
  303. $this->output->set_output($parsed);
  304. }
  305. }
  306. // ------------------------------------------------------------------------
  307. /**
  308. * MY_Admin Class
  309. *
  310. * Extends MY_Controller
  311. *
  312. */
  313. class MY_Admin extends MY_Controller
  314. {
  315. /* Response message type
  316. * Used by controller to send answer to request
  317. * can be 'error', 'notice', 'success'
  318. *
  319. */
  320. public $message_type = '';
  321. /* Response message to the user
  322. * Human understandable message
  323. *
  324. */
  325. public $message = '';
  326. /* Array of HTMLDomElement to update with corresponding update URL
  327. * Array (
  328. * 'htmlDomElement' => 'controller/method/'
  329. * );
  330. *
  331. */
  332. public $update = array();
  333. /* Current element ID
  334. *
  335. */
  336. public $id;
  337. /* Javascript callback function
  338. * Not implemented yet
  339. *
  340. */
  341. public $callback;
  342. /**
  343. * Constructor
  344. *
  345. */
  346. public function __construct()
  347. {
  348. parent::__construct();
  349. // Redirect the not authorized user to the login panel. See /application/config/connect.php
  350. Connect()->restrict_type_redirect = array(
  351. 'uri' => '/admin/user/login',
  352. 'flash_msg' => 'You have been denied access to %s',
  353. 'flash_use_lang' => false,
  354. 'flash_var' => 'error');
  355. // $this->output->enable_profiler(true);
  356. // PHP standard session is mandatory for MCE FileManager authentication
  357. // and other external lib
  358. session_start();
  359. $_SESSION['isLoggedIn'] = true;
  360. // Librairies
  361. $this->connect = Connect::get_instance();
  362. // Current user
  363. $this->template['current_user'] = $this->connect->get_current_user();
  364. // Set the admin theme as current theme
  365. Theme::set_theme('admin');
  366. /*
  367. * Admin languages : Depending on installed translations in /application/languages/
  368. * The Admin translations are only stored in the translation file /application/languages/xx/admin_lang.php
  369. *
  370. */
  371. // Set admin lang codes array
  372. Settings::set('admin_languages', $this->settings_model->get_admin_langs());
  373. // Set Router's found language code as current language
  374. Settings::set('current_lang', config_item('language_abbr'));
  375. // Load the current language translations file
  376. $this->lang->load('admin', Settings::get_lang());
  377. /*
  378. * Modules config
  379. *
  380. */
  381. $this->get_modules_config();
  382. // Including all modules languages files
  383. // $lang_files = glob(config_item('module_path').'*/language/'.Settings::get_lang().'/*');
  384. // Load all modules lang files
  385. /*
  386. Look how to make Modules translations available in javascript
  387. Notice : $yhis->lang object is transmitted to JS through load->view('javascript_lang')
  388. if ( ! empty($lang_files))
  389. {
  390. foreach($lang_files as $l)
  391. {
  392. if (is_file($l))
  393. {
  394. // $logical_name = substr($l, strripos($l, '/') +1);
  395. // $logical_name = str_replace('_lang.php', '', $logical_name);
  396. // $this->lang->load($logical_name, Settings::get_lang());
  397. include $l;
  398. $this->lang->language = array_merge($this->lang->language, $lang);
  399. unset($lang);
  400. }
  401. }
  402. }
  403. */
  404. /*
  405. * Settings
  406. *
  407. */
  408. // @TODO : Remove this thing from the global CMS. Not more mandatory, but necessary for compatibility with historical version
  409. // Available menus
  410. // Each menu was a root node in which you can put several pages, wich are composing a menu.
  411. // Was never really implemented in ionize historical version, but already used as : menus[0]...
  412. Settings::set('menus', config_item('menus'));
  413. // Don't want to cache this content
  414. $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
  415. $this->output->set_header("Cache-Control: post-check=0, pre-check=0", false);
  416. $this->output->set_header("Pragma: no-cache");
  417. }
  418. // ------------------------------------------------------------------------
  419. /**
  420. * Sets an error message and call the response method
  421. *
  422. * @param string Message to the user
  423. * @param array Additional data to put to the answer. Optional.
  424. *
  425. */
  426. public function error($message, $addon_data = null)
  427. {
  428. $this->message_type = 'error';
  429. $this->message = $message;
  430. if ( !isset($this->redirect) )
  431. {
  432. $this->redirect = $_SERVER['HTTP_REFERER'];
  433. }
  434. $this->response($addon_data);
  435. exit();
  436. }
  437. // ------------------------------------------------------------------------
  438. /**
  439. * Sets a success message and call the response method
  440. *
  441. * @param string Message to the user
  442. * @param array Additional data to put to the answer. Optional.
  443. *
  444. */
  445. public function success($message, $addon_data = null)
  446. {
  447. $this->message_type = 'success';
  448. $this->message = $message;
  449. $this->response($addon_data);
  450. }
  451. // ------------------------------------------------------------------------
  452. /**
  453. * Sets a notice message and call the response method
  454. *
  455. * @param string Message to the user
  456. * @param array Additional data to put to the answer. Optional.
  457. *
  458. */
  459. public function notice($message, $addon_data = null)
  460. {
  461. $this->message_type = 'notice';
  462. $this->message = $message;
  463. $this->response($addon_data);
  464. }
  465. // ------------------------------------------------------------------------
  466. /**
  467. * Send an answer to the browser depending on the incoming request
  468. * If the request cames from XHR, sends a JSON object as response
  469. * else, check if redirect is defined and redirect
  470. *
  471. * @param array Additional data to put to the answer. Optional.
  472. *
  473. */
  474. public function response($addon_data = null)
  475. {
  476. /* XHR request : JSON answer
  477. * Sends a JSON javascript object
  478. *
  479. */
  480. if ($this->is_xhr() === true)
  481. {
  482. // Basic JSON answser
  483. $data = array (
  484. 'message_type' => $this->message_type,
  485. 'message' => $this->message,
  486. 'update' => $this->update,
  487. 'callback' => $this->callback
  488. );
  489. // Puts additional data to answer
  490. if ( ! empty($addon_data))
  491. {
  492. $data = array_merge($data, $addon_data);
  493. }
  494. // Adds element ID if isset
  495. if (isset($this->id) )
  496. {
  497. $data['id'] = $this->id;
  498. }
  499. echo json_encode($data);
  500. }
  501. }
  502. }
  503. /**
  504. * Base Admin Module Class
  505. *
  506. * All modules Admin class must extend this class
  507. *
  508. * @author Martin Wernstahl
  509. *
  510. */
  511. abstract class Module_Admin extends MY_Admin
  512. {
  513. protected $parent;
  514. /**
  515. * Constructor
  516. *
  517. * @param CI object The CI object ($this)
  518. *
  519. */
  520. final public function __construct(Controller $c)
  521. {
  522. $this->parent = $c;
  523. $this->construct();
  524. }
  525. /**
  526. * The deported construct function
  527. * Should be called instead of parent::__construct by inherited classes
  528. *
  529. */
  530. abstract protected function construct();
  531. // ------------------------------------------------------------------------
  532. public function __get($prop)
  533. {
  534. if(property_exists($this->parent, $prop))
  535. {
  536. return $this->parent->$prop;
  537. }
  538. else
  539. {
  540. throw new Exception('Missing property');
  541. }
  542. }
  543. // ------------------------------------------------------------------------
  544. public function __call($method, $param)
  545. {
  546. if(method_exists($this->parent, $method))
  547. {
  548. return call_user_func_array(array($this->parent, $method), $param);
  549. }
  550. else
  551. {
  552. throw new BadMethodCallException(get_class($this).'::'.$method);
  553. }
  554. }
  555. // ------------------------------------------------------------------------
  556. /**
  557. * Returns a simple view
  558. * Available from each module
  559. *
  560. */
  561. public function get($view = false)
  562. {
  563. $args = func_get_args();
  564. $args = implode('/', $args);
  565. $this->output($args);
  566. }
  567. }
  568. /* End of file MY_Controller.php */
  569. /* Location: ./application/libraries/MY_Controller.php */