PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/modules/fuel/libraries/Fuel.php

http://github.com/daylightstudio/FUEL-CMS
PHP | 542 lines | 323 code | 71 blank | 148 comment | 44 complexity | 2a1fd38354dc4155a8fda076de38e322 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * FUEL CMS
  4. * http://www.getfuelcms.com
  5. *
  6. * An open source Content Management System based on the
  7. * Codeigniter framework (http://codeigniter.com)
  8. *
  9. * @package FUEL CMS
  10. * @author David McReynolds @ Daylight Studio
  11. * @copyright Copyright (c) 2018, Daylight Studio LLC.
  12. * @license http://docs.getfuelcms.com/general/license
  13. * @link http://www.getfuelcms.com
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * FUEL master object
  19. *
  20. * The master FUEL object that other objects attach to
  21. *
  22. * @package FUEL CMS
  23. * @subpackage Libraries
  24. * @category Libraries
  25. * @author David McReynolds @ Daylight Studio
  26. * @link http://docs.getfuelcms.com/libraries/fuel
  27. */
  28. // --------------------------------------------------------------------
  29. // include base library classes to extend
  30. require_once('Fuel_base_library.php');
  31. require_once('Fuel_advanced_module.php');
  32. //require_once('Fuel_modules.php');
  33. class Fuel extends Fuel_advanced_module {
  34. protected $name = 'FUEL'; // name of the advanced module... usually the same as the folder name
  35. protected $folder = 'fuel'; // name of the folder for the advanced module
  36. // attached objects
  37. protected $_attached = array();
  38. // objects to automatically attach
  39. protected $_auto_attach = array(
  40. 'admin',
  41. 'assets',
  42. 'auth',
  43. 'blocks',
  44. 'cache',
  45. 'categories',
  46. 'installer',
  47. 'language',
  48. 'layouts',
  49. 'logs',
  50. 'modules',
  51. 'navigation',
  52. 'notification',
  53. 'pages',
  54. 'parser',
  55. 'posts',
  56. 'pagevars',
  57. 'permissions',
  58. 'redirects',
  59. 'settings',
  60. 'sitevars',
  61. 'tags',
  62. 'users',
  63. );
  64. // the singleton instance
  65. private static $_instance;
  66. /**
  67. * Constructor
  68. *
  69. */
  70. public function __construct()
  71. {
  72. parent::__construct();
  73. self::$_instance =& $this;
  74. $this->fuel =& self::$_instance; // for compatibility
  75. $this->initialize();
  76. }
  77. // --------------------------------------------------------------------
  78. /**
  79. * Static method that returns the instance of the FUEL object.
  80. *
  81. * This object is auto-loaded and so you will most likely use $this->fuel instead of this method
  82. *
  83. * @access public
  84. * @return object
  85. */
  86. public static function &get_instance()
  87. {
  88. return self::$_instance;
  89. }
  90. // --------------------------------------------------------------------
  91. /**
  92. * Initialize the FUEL object
  93. *
  94. * Accepts an associative array as input containing the FUEL config parameters to set
  95. *
  96. * @access public
  97. * @param array Config preferences
  98. * @return void
  99. */
  100. public function initialize($config = array())
  101. {
  102. // load main fuel config
  103. $this->CI->load->module_config(FUEL_FOLDER, 'fuel', TRUE);
  104. if (!empty($config))
  105. {
  106. $this->set_config($config);
  107. }
  108. $this->_config = $this->CI->config->config['fuel'];
  109. // merge in any "attach" objects to include on the FUEL object
  110. $this->_auto_attach = array_merge($this->_auto_attach, $this->_config['attach']);
  111. // add package paths
  112. foreach($this->_config['modules_allowed'] as $module)
  113. {
  114. $this->CI->load->add_package_path(MODULES_PATH.$module);
  115. }
  116. // needed to add this here so views will load correctly
  117. $this->CI->load->add_package_path(APPPATH);
  118. }
  119. // --------------------------------------------------------------------
  120. /**
  121. * Sets a configuration value for FUEL (overwrites Fuel_advanced_module)
  122. *
  123. * @access public
  124. * @param mixed Can be a string that references the configuration key or an array of values
  125. * @param mixed The value of the key configuration item (only works if $item parameter is not an array) (optional)
  126. * @param string The module to set the configuration item. Default is fuel. (optional)
  127. * @return void
  128. */
  129. public function set_config($item, $value = NULL, $module = 'fuel')
  130. {
  131. $fuel_config = $this->CI->config->item($module);
  132. if (is_array($item))
  133. {
  134. foreach($item as $key => $val)
  135. {
  136. $fuel_config[$key] = $val;
  137. }
  138. }
  139. else
  140. {
  141. $fuel_config[$item] = $value;
  142. }
  143. $this->_config[$item] = $value;
  144. $this->CI->config->set_item($module, $fuel_config);
  145. }
  146. // --------------------------------------------------------------------
  147. /**
  148. * Returns the FUEL version
  149. *
  150. * @access public
  151. * @param string Value of what part of the version number to return. Options are "major", "minor", or "patch" (optional)
  152. * @return string
  153. */
  154. public function version($part = NULL)
  155. {
  156. $version = FUEL_VERSION;
  157. if (!empty($part))
  158. {
  159. $parts = explode('.', $version);
  160. switch($part)
  161. {
  162. case 'major':
  163. return $parts[0];
  164. break;
  165. case 'minor':
  166. if (isset($parts[1])) return $parts[1];
  167. break;
  168. case 'patch':
  169. if (isset($parts[2])) return $parts[2];
  170. break;
  171. }
  172. return '0';
  173. }
  174. return $version;
  175. }
  176. // --------------------------------------------------------------------
  177. /**
  178. * Installs FUEL via the command line
  179. *
  180. * @access public
  181. * @return boolean
  182. */
  183. public function install()
  184. {
  185. $cli = $this->installer->cli();
  186. if (!$cli->is_cli()) return FALSE;
  187. $module = strtolower($this->name());
  188. $this->installer->config($module);
  189. // $intro = array(
  190. // "The FUEL CMS installer is an easy way to setup the CMS with common configurations. It will do the following:",
  191. // "1) Automatically generate an encryption key for the fuel/application/config/config.php.",
  192. // "2) Set the session save path in fuel/application/config/config.php.",
  193. // "3) Enable the CMS admin by changing the 'admin_enabled' config value in fuel/application/config/MY_fuel.php.",
  194. // "4) Change the 'fuel_mode' config value in in fuel/application/config/MY_fuel.php to allow for pages to be created in the CMS.",
  195. // "5) Change the 'site_name' config value in the fuel/application/config/MY_fuel.php.",
  196. // "6) Setup your environments fuel/application/config/environments.php.",
  197. // "7) Will make the fuel/application/logs, fuel/application/cache and assets/images folders writable.",
  198. // "8) Update the fuel/application/config/database.php file with the inputted values.",
  199. // "9) Create a database and install the fuel_schema.sql file using your local MySQL connection.\n",
  200. // );
  201. $cli->write(lang('install_cli_intro'));
  202. // add the session_path key
  203. $session_path = $cli->prompt(lang('install_session_path'));
  204. if (!empty($session_path)) $this->installer->change_config('config', '$config[\'sess_save_path\'] = NULL;', '$config[\'sess_save_path\'] = \''.$session_path.'\';');
  205. // add the encryption key
  206. $this->installer->change_config('config', '$config[\'encryption_key\'] = \'\';', '$config[\'encryption_key\'] = \''.md5(uniqid()).'\';');
  207. // change the admin to be enabled
  208. $this->installer->change_config('MY_fuel', '$config[\'admin_enabled\'] = FALSE;', '$config[\'admin_enabled\'] = TRUE;');
  209. // change the fuel_model to "auto"
  210. $this->installer->change_config('MY_fuel', '$config[\'fuel_mode\'] = \'views\';', '$config[\'fuel_mode\'] = \'auto\';');
  211. // change the site_name config value
  212. $site_name = $cli->prompt(lang('install_site_name'));
  213. $this->installer->change_config('MY_fuel', '$config[\'site_name\'] = \'My Website\';', '$config[\'site_name\'] = \''.$site_name.'\';');
  214. // setup environments
  215. $staging_environment = $cli->prompt(lang('install_environments_testing'));
  216. $prod_environment = $cli->prompt(lang('install_environments_production'));
  217. $environment_search = "'development' => array('localhost*', '192.*', '*.dev'),\n\t\t\t\t);";
  218. $environment_replace = "'development' => array('localhost*', '192.*', '*.dev'),";
  219. if (!empty($staging_environment)) $environment_replace .= "\n\t\t\t\t'testing' => array('" . implode("', '", preg_split('#\s+#', str_replace(',', '', $staging_environment))) . "'),";
  220. if (!empty($prod_environment)) $environment_replace .= "\n\t\t\t\t'production' => array('" . implode("', '", preg_split('#\s+#', str_replace(',', '', $prod_environment))) . "'),";
  221. $environment_replace .= "\n\t\t\t\t);";
  222. $this->installer->change_config('environments', $environment_search, $environment_replace);
  223. // change file permissions for writable folders
  224. $perms = $cli->prompt(lang('install_permissions'));
  225. if (!empty($perms))
  226. {
  227. $this->CI->load->helper('directory');
  228. $writable_folders = array(
  229. APPPATH.'cache/',
  230. APPPATH.'logs/',
  231. WEB_ROOT.'assets/images/',
  232. );
  233. $perms = intval($perms, 8);
  234. foreach($writable_folders as $folder)
  235. {
  236. @chmodr($folder, $perms);
  237. if (!is_writable($folder))
  238. {
  239. $this->_add_error(lang('error_folder_not_writable', $folder));
  240. }
  241. }
  242. }
  243. // ask database questions
  244. $db_name = $cli->prompt(lang('install_db_name'));
  245. $db_user = $cli->prompt(lang('install_db_user'));
  246. $db_pwd = $cli->secret(lang('install_db_pwd'));
  247. // change database config
  248. if (!empty($db_name) AND !empty($db_user) AND !empty($db_pwd))
  249. {
  250. $this->installer->change_config('database', '$db[\'default\'][\'username\'] = \'\';', '$db[\'default\'][\'username\'] = \''.$db_user.'\';');
  251. $this->installer->change_config('database', '$db[\'default\'][\'password\'] = \'\';', '$db[\'default\'][\'password\'] = \''.$db_pwd.'\';');
  252. // now check the database connection and see if the database exists yet or not... if not create it
  253. $this->CI->load->dbutil();
  254. if (!$this->CI->dbutil->database_exists($db_name))
  255. {
  256. $this->CI->load->dbforge();
  257. $this->CI->dbforge->create_database($db_name);
  258. $this->installer->change_config('database', '$db[\'default\'][\'database\'] = \'\';', '$db[\'default\'][\'database\'] = \''.$db_name.'\';');
  259. $this->installer->install_sql();
  260. }
  261. else
  262. {
  263. // must do this afterward to prevent errors
  264. $this->installer->change_config('database', '$db[\'default\'][\'database\'] = \'\';', '$db[\'default\'][\'database\'] = \''.$db_name.'\';');
  265. }
  266. }
  267. $cli->write("\n...\n");
  268. if ($this->has_errors())
  269. {
  270. $cli->write(lang('install_success_with_errors', implode("\n", $this->fuel->errors())));
  271. }
  272. else
  273. {
  274. $cli->write(lang('install_success'));
  275. }
  276. $cli->new_line();
  277. $cli->write(lang('install_further_info'));
  278. return TRUE;
  279. }
  280. // --------------------------------------------------------------------
  281. /**
  282. * Updates FUEL to v1.4
  283. *
  284. * @access public
  285. * @return boolean
  286. */
  287. public function update()
  288. {
  289. $cli = $this->installer->cli();
  290. if (!$cli->is_cli()) return FALSE;
  291. if (!($this->version('major') == '1' AND $this->version('minor') == '4'))
  292. {
  293. $cli->write('You must be using version 1.4x to run the updater.');
  294. exit();
  295. }
  296. $this->CI->load->helper('directory');
  297. $this->CI->load->helper('file');
  298. // $intro = array(
  299. // "FUEL CMS 1.4x is built on CodeIgniter 3. If you are upgrading from 1.3x or earlier, this updater will help fix some of the common issues when upgrading including:",
  300. // "1) Upper-cased first letter for models, libraries and controller file names.",
  301. // "2) Will upper case common references to Base_module_model.php.",
  302. // "3) Update common method signatures in models and libraries like form_fields and _common_query and initialize to match their parents.",
  303. // "Run this ONLY if you are using GIT in case you need to roll back!"
  304. // "Do you wish to continue (y/n)",
  305. // );
  306. $continue = $cli->prompt(lang('update_cli_intro'));
  307. if (strtolower($continue) == 'y' || strtolower($continue) == 'yes')
  308. {
  309. $modules_path = MODULES_PATH;
  310. $module_paths = list_directories(MODULES_PATH, array(), TRUE, FALSE, FALSE);
  311. $module_paths[] = APPPATH;
  312. foreach($module_paths as $module_path)
  313. {
  314. $module = pathinfo($module_path, PATHINFO_BASENAME);
  315. if ($module == 'fuel') continue;
  316. // ucfirst file names
  317. $folders = array('controllers', 'libraries', 'models');
  318. foreach($folders as $folder)
  319. {
  320. // change controller file names to be ucfirst
  321. $path = $module_path.'/'.$folder;
  322. // $files = get_filenames($path, TRUE, FALSE);
  323. $files = directory_to_array($path, FALSE);
  324. $cmd1 = "git mv -f ";
  325. $cmd2 = "mv "; // in case GIT isn't being used
  326. foreach($files as $file)
  327. {
  328. $file = DIRECTORY_SEPARATOR.ltrim($file, '/');
  329. if (pathinfo($file, PATHINFO_EXTENSION) == 'php')
  330. {
  331. if ($folder == 'controllers' OR $folder == 'models')
  332. {
  333. // first ucfirst file names
  334. $newFile = pathinfo($file, PATHINFO_DIRNAME) .'/'. ucfirst(pathinfo($file, PATHINFO_BASENAME));
  335. if ($file != $newFile)
  336. {
  337. $mv = $file . " " .$newFile;
  338. exec($cmd1 . $mv);
  339. exec($cmd2 . $mv);
  340. }
  341. }
  342. // now search and replace common issues
  343. if ($folder == 'controllers')
  344. {
  345. // public function initialize($params = array())
  346. $find = array(
  347. '/controllers/module.php'
  348. );
  349. $replace = array(
  350. '/controllers/Module.php'
  351. );
  352. $this->_update_search_replace($file, $find, $replace);
  353. }
  354. elseif ($folder == 'models')
  355. {
  356. $find = array(
  357. 'models/base_module_model.php',
  358. 'function form_fields()',
  359. 'function form_fields($values = array())',
  360. 'function _common_query()',
  361. );
  362. $replace = array(
  363. 'models/Base_module_model.php',
  364. 'function form_fields($values = array(), $related = array())',
  365. 'function form_fields($values = array(), $related = array())',
  366. 'function _common_query($display_unpublished_if_logged_in = NULL)',
  367. );
  368. $this->_update_search_replace($file, $find, $replace);
  369. }
  370. elseif ($folder == 'libraries')
  371. {
  372. $find = array(
  373. 'function initialize($params)',
  374. );
  375. $replace = array(
  376. 'function initialize($params = array())',
  377. );
  378. $this->_update_search_replace($file, $find, $replace);
  379. }
  380. }
  381. }
  382. }
  383. $models_path = $module_path.'/models';
  384. $model_files = get_filenames($models_path);
  385. }
  386. }
  387. $cli->write(lang('update_success'));
  388. return TRUE;
  389. }
  390. protected function _update_search_replace($file, $find, $replace)
  391. {
  392. $f = read_file($file);
  393. foreach($find as $i => $_f)
  394. {
  395. if (strpos($f, $_f) !== FALSE)
  396. {
  397. $f = str_replace($find[$i], $replace[$i], $f);
  398. echo "REPLACED: ".$find[$i]." => ".$replace[$i]."\n";
  399. }
  400. }
  401. write_file($file, $f);
  402. }
  403. // --------------------------------------------------------------------
  404. /**
  405. * Magic method that will attach and return FUEL library objects
  406. *
  407. * @access public
  408. * @param string The object
  409. * @return object
  410. */
  411. public function __get($var)
  412. {
  413. if (!isset($this->_attached[$var]))
  414. {
  415. if (in_array($var, $this->_auto_attach))
  416. {
  417. $this->attach($var);
  418. }
  419. else if ($this->modules->allowed($var))
  420. {
  421. $init = array('name' => $var, 'folder' => $var);
  422. $fuel_class = 'Fuel_'.$var;
  423. if (file_exists(MODULES_PATH.$var.'/libraries/'.$fuel_class.'.php'))
  424. {
  425. $lib_class = strtolower($fuel_class);
  426. if (!isset($this->CI->$lib_class))
  427. {
  428. $this->CI->load->module_library($var, $lib_class, $init);
  429. }
  430. return $this->CI->$lib_class;
  431. }
  432. else
  433. {
  434. $module = new Fuel_advanced_module($init);
  435. $this->CI->$var = $module;
  436. return $module;
  437. }
  438. }
  439. else
  440. {
  441. // To resolve issue when called outside of admin
  442. $this->load_language('fuel');
  443. throw new Exception(lang('error_class_property_does_not_exist', $var));
  444. }
  445. }
  446. return $this->_attached[$var];
  447. }
  448. // --------------------------------------------------------------------
  449. /**
  450. * Magic method that will call any methods on an attached object that are "get"
  451. *
  452. * @access public
  453. * @param string The object
  454. * @param string An array of arguments
  455. * @return object
  456. */
  457. public function __call($name, $args)
  458. {
  459. $obj = $this->$name;
  460. if (method_exists($obj, 'get'))
  461. {
  462. return call_user_func_array(array($obj, 'get'), $args);
  463. }
  464. else
  465. {
  466. return $obj;
  467. }
  468. }
  469. }
  470. /* End of file Fuel.php */
  471. /* Location: ./modules/fuel/libraries/Fuel.php */