blob: 976a5a57553a955ad40d1607c15344a6f5ec4d79 [file] [log] [blame]
Andrey Andreevd7297352012-01-07 22:53:14 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevd7297352012-01-07 22:53:14 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevd7297352012-01-07 22:53:14 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Loader Class
30 *
31 * Loads views and files
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @category Loader
37 * @link http://codeigniter.com/user_guide/libraries/loader.html
38 */
39class CI_Loader {
40
41 // All these are set automatically. Don't mess with them.
David Behlercda768a2011-08-14 23:52:48 +020042 /**
43 * Nesting level of the output buffering mechanism
44 *
45 * @var int
David Behlercda768a2011-08-14 23:52:48 +020046 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050047 protected $_ci_ob_level;
David Behlercda768a2011-08-14 23:52:48 +020048 /**
49 * List of paths to load views from
50 *
51 * @var array
David Behlercda768a2011-08-14 23:52:48 +020052 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050053 protected $_ci_view_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020054 /**
55 * List of paths to load libraries from
56 *
57 * @var array
David Behlercda768a2011-08-14 23:52:48 +020058 */
Greg Akerf5c84022011-04-19 17:13:03 -050059 protected $_ci_library_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020060 /**
61 * List of paths to load models from
62 *
63 * @var array
David Behlercda768a2011-08-14 23:52:48 +020064 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050065 protected $_ci_model_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020066 /**
67 * List of paths to load helpers from
68 *
69 * @var array
David Behlercda768a2011-08-14 23:52:48 +020070 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050071 protected $_ci_helper_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020072 /**
73 * List of loaded base classes
David Behlercda768a2011-08-14 23:52:48 +020074 *
75 * @var array
David Behlercda768a2011-08-14 23:52:48 +020076 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050077 protected $_base_classes = array(); // Set by the controller class
David Behlercda768a2011-08-14 23:52:48 +020078 /**
79 * List of cached variables
80 *
81 * @var array
David Behlercda768a2011-08-14 23:52:48 +020082 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050083 protected $_ci_cached_vars = array();
David Behlercda768a2011-08-14 23:52:48 +020084 /**
85 * List of loaded classes
86 *
87 * @var array
David Behlercda768a2011-08-14 23:52:48 +020088 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050089 protected $_ci_classes = array();
David Behlercda768a2011-08-14 23:52:48 +020090 /**
91 * List of loaded files
92 *
93 * @var array
David Behlercda768a2011-08-14 23:52:48 +020094 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050095 protected $_ci_loaded_files = array();
David Behlercda768a2011-08-14 23:52:48 +020096 /**
97 * List of loaded models
98 *
99 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200100 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500101 protected $_ci_models = array();
David Behlercda768a2011-08-14 23:52:48 +0200102 /**
103 * List of loaded helpers
104 *
105 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200106 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500107 protected $_ci_helpers = array();
David Behlercda768a2011-08-14 23:52:48 +0200108 /**
109 * List of class name mappings
110 *
111 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200112 */
Andrey Andreevd7297352012-01-07 22:53:14 +0200113 protected $_ci_varmap = array(
114 'unit_test' => 'unit',
115 'user_agent' => 'agent'
116 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000117
118 /**
119 * Constructor
120 *
121 * Sets the path to the view files and gets the initial output buffering level
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 */
Greg Akerf5c84022011-04-19 17:13:03 -0500123 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200124 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500125 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -0600126 $this->_ci_library_paths = array(APPPATH, BASEPATH);
127 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
128 $this->_ci_model_paths = array(APPPATH);
Joe Cianflone8eef9c72011-08-21 10:39:06 -0400129 $this->_ci_view_paths = array(VIEWPATH => TRUE);
David Behlercda768a2011-08-14 23:52:48 +0200130
Andrey Andreevd7297352012-01-07 22:53:14 +0200131 log_message('debug', 'Loader Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200135
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500136 /**
Shane Pearson6adfe632011-08-10 16:42:53 -0500137 * Initialize the Loader
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500138 *
139 * This method is called once in CI_Controller.
140 *
David Behlercda768a2011-08-14 23:52:48 +0200141 * @param array
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500142 * @return object
143 */
Shane Pearson6adfe632011-08-10 16:42:53 -0500144 public function initialize()
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500145 {
Shane Pearson6adfe632011-08-10 16:42:53 -0500146 $this->_ci_classes = array();
147 $this->_ci_loaded_files = array();
148 $this->_ci_models = array();
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500149 $this->_base_classes =& is_loaded();
Shane Pearson6adfe632011-08-10 16:42:53 -0500150
151 $this->_ci_autoloader();
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500152 return $this;
153 }
154
155 // --------------------------------------------------------------------
156
157 /**
158 * Is Loaded
159 *
160 * A utility function to test if a class is in the self::$_ci_classes array.
161 * This function returns the object name if the class tested for is loaded,
162 * and returns FALSE if it isn't.
163 *
164 * It is mainly used in the form_helper -> _get_validation_object()
165 *
166 * @param string class being checked for
167 * @return mixed class object name on the CI SuperObject or FALSE
168 */
169 public function is_loaded($class)
170 {
171 if (isset($this->_ci_classes[$class]))
172 {
173 return $this->_ci_classes[$class];
174 }
David Behlercda768a2011-08-14 23:52:48 +0200175
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500176 return FALSE;
177 }
178
179 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 /**
182 * Class Loader
183 *
184 * This function lets users load and instantiate classes.
185 * It is designed to be called from a user's app controllers.
186 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @param string the name of the class
188 * @param mixed the optional parameters
189 * @param string an optional object name
190 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200191 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500192 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Greg Akerce433962010-10-12 09:29:35 -0500194 if (is_array($library))
195 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100196 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500197 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600198 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500199 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000200
Greg Akerce433962010-10-12 09:29:35 -0500201 return;
202 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000203
Derek Jones32bf1862010-03-02 13:46:07 -0600204 if ($library == '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 return FALSE;
207 }
208
Derek Jones32bf1862010-03-02 13:46:07 -0600209 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
211 $params = NULL;
212 }
213
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600214 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
216
217 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 /**
220 * Model Loader
221 *
222 * This function lets users load and instantiate models.
223 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 * @param string the name of the class
225 * @param string name for the model
226 * @param bool database connection
227 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200228 */
Greg Akerf5c84022011-04-19 17:13:03 -0500229 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200230 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 if (is_array($model))
232 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500233 foreach ($model as $babe)
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
Barry Mienydd671972010-10-04 16:33:58 +0200235 $this->model($babe);
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
237 return;
238 }
239
240 if ($model == '')
241 {
242 return;
243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Jones32bf1862010-03-02 13:46:07 -0600245 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600248 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
Derek Jones32bf1862010-03-02 13:46:07 -0600250 // The path is in front of the last slash
Andrey Andreevd47baab2012-01-09 16:56:46 +0200251 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600252
253 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200254 $model = substr($model, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 if ($name == '')
258 {
259 $name = $model;
260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 if (in_array($name, $this->_ci_models, TRUE))
263 {
264 return;
265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 $CI =& get_instance();
268 if (isset($CI->$name))
269 {
270 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000274
Derek Jones32bf1862010-03-02 13:46:07 -0600275 foreach ($this->_ci_model_paths as $mod_path)
276 {
Greg Aker3a746652011-04-19 10:59:47 -0500277 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600278 {
279 continue;
280 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000281
Andrey Andreev94af3552012-03-26 23:10:42 +0300282 if ($db_conn !== FALSE && ! class_exists('CI_DB'))
Derek Jones32bf1862010-03-02 13:46:07 -0600283 {
284 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500285 {
Derek Jones32bf1862010-03-02 13:46:07 -0600286 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500287 }
Derek Jones32bf1862010-03-02 13:46:07 -0600288
289 $CI->load->database($db_conn, FALSE, TRUE);
290 }
291
Greg Akerbce13482010-10-11 15:37:16 -0500292 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600293 {
294 load_class('Model', 'core');
295 }
296
Greg Aker3a746652011-04-19 10:59:47 -0500297 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600298
299 $model = ucfirst($model);
Derek Jones32bf1862010-03-02 13:46:07 -0600300 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600301 $this->_ci_models[] = $name;
302 return;
303 }
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Jones32bf1862010-03-02 13:46:07 -0600305 // couldn't find the model
306 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 /**
312 * Database Loader
313 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 * @param string the DB credentials
315 * @param bool whether to return the DB object
316 * @param bool whether to enable active record (this allows us to override the config setting)
317 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200318 */
Greg Akerf5c84022011-04-19 17:13:03 -0500319 public function database($params = '', $return = FALSE, $active_record = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 // Grab the super object
322 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // Do we even need to load the database class?
Andrey Andreev94af3552012-03-26 23:10:42 +0300325 if (class_exists('CI_DB') && $return == FALSE && $active_record == NULL && isset($CI->db) && is_object($CI->db))
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
327 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200328 }
329
Greg Aker3a746652011-04-19 10:59:47 -0500330 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000331
332 if ($return === TRUE)
333 {
334 return DB($params, $active_record);
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Andrey Andreevd7297352012-01-07 22:53:14 +0200337 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // reference errors with some configurations
339 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // Load the DB class
Barry Mienydd671972010-10-04 16:33:58 +0200342 $CI->db =& DB($params, $active_record);
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 // --------------------------------------------------------------------
346
347 /**
348 * Load the Utilities Class
349 *
Barry Mienydd671972010-10-04 16:33:58 +0200350 * @return string
351 */
Greg Akerf5c84022011-04-19 17:13:03 -0500352 public function dbutil()
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
354 if ( ! class_exists('CI_DB'))
355 {
356 $this->database();
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 $CI =& get_instance();
360
361 // for backwards compatibility, load dbforge so we can extend dbutils off it
362 // this use is deprecated and strongly discouraged
363 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200364
Greg Aker3a746652011-04-19 10:59:47 -0500365 require_once(BASEPATH.'database/DB_utility.php');
366 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
368
Pascal Kriete58560022010-11-10 16:01:20 -0500369 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 // --------------------------------------------------------------------
373
374 /**
375 * Load the Database Forge Class
376 *
Barry Mienydd671972010-10-04 16:33:58 +0200377 * @return string
378 */
Greg Akerf5c84022011-04-19 17:13:03 -0500379 public function dbforge()
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
381 if ( ! class_exists('CI_DB'))
382 {
383 $this->database();
384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200387
Greg Aker3a746652011-04-19 10:59:47 -0500388 require_once(BASEPATH.'database/DB_forge.php');
389 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
391
392 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 /**
398 * Load View
399 *
Andrey Andreev94af3552012-03-26 23:10:42 +0300400 * This function is used to load a "view" file. It has three parameters:
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 *
402 * 1. The name of the "view" file to be included.
403 * 2. An associative array of data to be extracted for use in the view.
Andrey Andreev94af3552012-03-26 23:10:42 +0300404 * 3. TRUE/FALSE - whether to return the data or load it. In
405 * some cases it's advantageous to be able to return data so that
406 * a developer can process it in some way.
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 * @param string
409 * @param array
410 * @param bool
411 * @return void
412 */
Greg Akerf5c84022011-04-19 17:13:03 -0500413 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
415 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 /**
421 * Load File
422 *
423 * This is a generic file loader
424 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 * @param string
426 * @param bool
427 * @return string
428 */
Greg Akerf5c84022011-04-19 17:13:03 -0500429 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
431 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 /**
437 * Set Variables
438 *
439 * Once variables are set they become available within
440 * the controller class and its "view" files.
441 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 * @param array
David Behlercda768a2011-08-14 23:52:48 +0200443 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * @return void
445 */
Greg Akerf5c84022011-04-19 17:13:03 -0500446 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300448 if ($val != '' && is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
450 $vars = array($vars => $val);
451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200454
Andrey Andreev94af3552012-03-26 23:10:42 +0300455 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
457 foreach ($vars as $key => $val)
458 {
459 $this->_ci_cached_vars[$key] = $val;
460 }
461 }
462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600467 * Get Variable
468 *
469 * Check if a variable is set and retrieve it.
470 *
471 * @param array
472 * @return void
473 */
474 public function get_var($key)
475 {
476 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
477 }
478
479 // --------------------------------------------------------------------
480
481 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600482 * Get Variables
483 *
484 * Retrieve all loaded variables
485 *
486 * @return array
487 */
488 public function get_vars()
489 {
490 return $this->_ci_cached_vars;
491 }
492
493 // --------------------------------------------------------------------
494
495 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * Load Helper
497 *
498 * This function loads the specified helper file.
499 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * @param mixed
501 * @return void
502 */
Greg Akerf5c84022011-04-19 17:13:03 -0500503 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200504 {
Derek Jones32bf1862010-03-02 13:46:07 -0600505 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200506 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 if (isset($this->_ci_helpers[$helper]))
508 {
509 continue;
510 }
Derek Jones32bf1862010-03-02 13:46:07 -0600511
Greg Aker3a746652011-04-19 10:59:47 -0500512 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000513
Barry Mienydd671972010-10-04 16:33:58 +0200514 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 if (file_exists($ext_helper))
516 {
Greg Aker3a746652011-04-19 10:59:47 -0500517 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 if ( ! file_exists($base_helper))
520 {
Greg Aker3a746652011-04-19 10:59:47 -0500521 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 include_once($ext_helper);
525 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Jones32bf1862010-03-02 13:46:07 -0600527 $this->_ci_helpers[$helper] = TRUE;
528 log_message('debug', 'Helper loaded: '.$helper);
529 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 }
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Jones32bf1862010-03-02 13:46:07 -0600532 // Try to load the helper
533 foreach ($this->_ci_helper_paths as $path)
534 {
Greg Aker3a746652011-04-19 10:59:47 -0500535 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200536 {
Greg Aker3a746652011-04-19 10:59:47 -0500537 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600538
539 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200540 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600541 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 }
543 }
544
Derek Jones32bf1862010-03-02 13:46:07 -0600545 // unable to load the helper
546 if ( ! isset($this->_ci_helpers[$helper]))
547 {
Greg Aker3a746652011-04-19 10:59:47 -0500548 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600549 }
Barry Mienydd671972010-10-04 16:33:58 +0200550 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 /**
556 * Load Helpers
557 *
558 * This is simply an alias to the above function in case the
559 * user has written the plural form of this function.
560 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * @param array
562 * @return void
563 */
Greg Akerf5c84022011-04-19 17:13:03 -0500564 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 {
566 $this->helper($helpers);
567 }
Barry Mienydd671972010-10-04 16:33:58 +0200568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 * Loads a language file
573 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 * @param array
575 * @param string
576 * @return void
577 */
Greg Akerf5c84022011-04-19 17:13:03 -0500578 public function language($file = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
580 $CI =& get_instance();
581
582 if ( ! is_array($file))
583 {
584 $file = array($file);
585 }
586
587 foreach ($file as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200588 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 $CI->lang->load($langfile, $lang);
590 }
591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 /**
596 * Loads a config file
597 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200599 * @param bool
600 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 * @return void
602 */
Greg Akerf5c84022011-04-19 17:13:03 -0500603 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200604 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 $CI =& get_instance();
606 $CI->config->load($file, $use_sections, $fail_gracefully);
607 }
608
609 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 /**
Derek Jones8dca0412010-03-05 13:01:44 -0600612 * Driver
613 *
614 * Loads a driver library
615 *
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800616 * @param mixed the name of the class or array of classes
Derek Jones8dca0412010-03-05 13:01:44 -0600617 * @param mixed the optional parameters
618 * @param string an optional object name
619 * @return void
620 */
Greg Akerf5c84022011-04-19 17:13:03 -0500621 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600622 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800623 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800624 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800625 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800626 {
627 $this->driver($driver);
628 }
629 return FALSE;
630 }
631
Derek Jones8dca0412010-03-05 13:01:44 -0600632 if ( ! class_exists('CI_Driver_Library'))
633 {
634 // we aren't instantiating an object here, that'll be done by the Library itself
Greg Aker3a746652011-04-19 10:59:47 -0500635 require BASEPATH.'libraries/Driver.php';
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600636 }
Barry Mienydd671972010-10-04 16:33:58 +0200637
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200638 if ($library == '')
639 {
640 return FALSE;
641 }
642
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600643 // We can save the loader some time since Drivers will *always* be in a subfolder,
644 // and typically identically named to the library
645 if ( ! strpos($library, '/'))
646 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500647 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600648 }
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Jones8dca0412010-03-05 13:01:44 -0600650 return $this->library($library, $params, $object_name);
651 }
652
653 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200654
Derek Jones8dca0412010-03-05 13:01:44 -0600655 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600656 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 *
Derek Jones32bf1862010-03-02 13:46:07 -0600658 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 * @param string
Andrey Andreev94af3552012-03-26 23:10:42 +0300661 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600663 */
Greg Akerf5c84022011-04-19 17:13:03 -0500664 public function add_package_path($path, $view_cascade=TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600665 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500666 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200667
Derek Jones32bf1862010-03-02 13:46:07 -0600668 array_unshift($this->_ci_library_paths, $path);
669 array_unshift($this->_ci_model_paths, $path);
670 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200671
Greg Akerf5c84022011-04-19 17:13:03 -0500672 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
673
Derek Jones32bf1862010-03-02 13:46:07 -0600674 // Add config file path
675 $config =& $this->_ci_get_component('config');
676 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 }
678
679 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600680
681 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000682 * Get Package Paths
683 *
684 * Return a list of all package paths, by default it will ignore BASEPATH.
685 *
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000686 * @param string
687 * @return void
688 */
Greg Akerf5c84022011-04-19 17:13:03 -0500689 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000690 {
691 return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
692 }
693
694 // --------------------------------------------------------------------
695
696 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600697 * Remove Package Path
698 *
699 * Remove a path from the library, model, and helper path arrays if it exists
700 * If no path is provided, the most recently added path is removed.
701 *
Andrey Andreev94af3552012-03-26 23:10:42 +0300702 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200703 * @param bool
Andrey Andreev94af3552012-03-26 23:10:42 +0300704 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600705 */
Greg Akerf5c84022011-04-19 17:13:03 -0500706 public function remove_package_path($path = '', $remove_config_path = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600707 {
708 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Jones32bf1862010-03-02 13:46:07 -0600710 if ($path == '')
711 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200712 array_shift($this->_ci_library_paths);
713 array_shift($this->_ci_model_paths);
714 array_shift($this->_ci_helper_paths);
715 array_shift($this->_ci_view_paths);
716 array_shift($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600717 }
718 else
719 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500720 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600721 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
722 {
723 if (($key = array_search($path, $this->{$var})) !== FALSE)
724 {
725 unset($this->{$var}[$key]);
726 }
727 }
David Behlercda768a2011-08-14 23:52:48 +0200728
Greg Akerf5c84022011-04-19 17:13:03 -0500729 if (isset($this->_ci_view_paths[$path.'views/']))
730 {
731 unset($this->_ci_view_paths[$path.'views/']);
732 }
Barry Mienydd671972010-10-04 16:33:58 +0200733
Derek Jones32bf1862010-03-02 13:46:07 -0600734 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
735 {
736 unset($config->_config_paths[$key]);
737 }
738 }
Barry Mienydd671972010-10-04 16:33:58 +0200739
Derek Jones32bf1862010-03-02 13:46:07 -0600740 // make sure the application default paths are still in the array
741 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
742 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
743 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500744 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600745 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
746 }
747
748 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200749
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 /**
751 * Loader
752 *
753 * This function is used to load views and files.
754 * Variables are prefixed with _ci_ to avoid symbol collision with
755 * variables made available to view files
756 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 * @param array
758 * @return void
759 */
Greg Akerf5c84022011-04-19 17:13:03 -0500760 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 {
762 // Set the default data variables
763 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
764 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300765 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 }
David Behlercda768a2011-08-14 23:52:48 +0200767
Greg Akerf5c84022011-04-19 17:13:03 -0500768 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000769
770 // Set the path to the requested file
Greg Aker8807be32011-04-21 13:06:15 -0500771 if ($_ci_path != '')
772 {
773 $_ci_x = explode('/', $_ci_path);
774 $_ci_file = end($_ci_x);
775 }
776 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 {
778 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Greg Aker3a746652011-04-19 10:59:47 -0500779 $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500780
781 foreach ($this->_ci_view_paths as $view_file => $cascade)
782 {
783 if (file_exists($view_file.$_ci_file))
784 {
785 $_ci_path = $view_file.$_ci_file;
786 $file_exists = TRUE;
787 break;
788 }
David Behlercda768a2011-08-14 23:52:48 +0200789
Greg Akerf5c84022011-04-19 17:13:03 -0500790 if ( ! $cascade)
791 {
792 break;
David Behlercda768a2011-08-14 23:52:48 +0200793 }
Greg Akerf5c84022011-04-19 17:13:03 -0500794 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 }
Barry Mienydd671972010-10-04 16:33:58 +0200796
Greg Akerf5c84022011-04-19 17:13:03 -0500797 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 {
799 show_error('Unable to load the requested file: '.$_ci_file);
800 }
Barry Mienydd671972010-10-04 16:33:58 +0200801
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 // This allows anything loaded using $this->load (views, files, etc.)
803 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500804 $_ci_CI =& get_instance();
805 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500807 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500809 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 }
811 }
812
813 /*
814 * Extract and cache variables
815 *
816 * You can either set variables using the dedicated $this->load_vars()
817 * function or via the second parameter of this function. We'll merge
818 * the two types and cache them so that views that are embedded within
819 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200820 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 if (is_array($_ci_vars))
822 {
823 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
824 }
825 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 /*
828 * Buffer the output
829 *
830 * We buffer the output for two reasons:
831 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200832 * 2. So that the final rendered template can be post-processed by
833 * the output class. Why do we need post processing? For one thing,
834 * in order to show the elapsed page load time. Unless we can
835 * intercept the content right before it's sent to the browser and
836 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 */
838 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200839
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 // If the PHP installation does not support short tags we'll
841 // do a little string replacement, changing the short tags
842 // to standard PHP echo statements.
Andrey Andreevf5e8e1c2012-03-06 13:11:27 +0200843 if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE && config_item('rewrite_short_tags') == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200845 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 }
847 else
848 {
849 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
850 }
Barry Mienydd671972010-10-04 16:33:58 +0200851
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200853
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 // Return the file data if requested
855 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200856 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 $buffer = ob_get_contents();
858 @ob_end_clean();
859 return $buffer;
860 }
861
862 /*
863 * Flush the buffer... or buff the flusher?
864 *
865 * In order to permit views to be nested within
866 * other views, we need to flush the content back out whenever
867 * we are beyond the first level of output buffering so that
868 * it can be seen and included properly by the first included
869 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200870 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 if (ob_get_level() > $this->_ci_ob_level + 1)
872 {
873 ob_end_flush();
874 }
875 else
876 {
Greg Aker22f1a632010-11-10 15:34:35 -0600877 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 @ob_end_clean();
879 }
880 }
881
882 // --------------------------------------------------------------------
883
884 /**
885 * Load class
886 *
887 * This function loads the requested class.
888 *
Barry Mienydd671972010-10-04 16:33:58 +0200889 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 * @param mixed any additional parameters
891 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200892 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 */
Greg Akerf5c84022011-04-19 17:13:03 -0500894 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200895 {
896 // Get the class name, and while we're at it trim any slashes.
897 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500899 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 // Was the path included with the class name?
902 // We look for a slash to determine this
903 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600904 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 {
Derek Jones32bf1862010-03-02 13:46:07 -0600906 // Extract the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200907 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200908
Derek Jones32bf1862010-03-02 13:46:07 -0600909 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200910 $class = substr($class, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 }
912
913 // We'll test for both lowercase and capitalized versions of the file name
914 foreach (array(ucfirst($class), strtolower($class)) as $class)
915 {
Greg Aker3a746652011-04-19 10:59:47 -0500916 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000917
Barry Mienydd671972010-10-04 16:33:58 +0200918 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 if (file_exists($subclass))
920 {
Greg Aker3a746652011-04-19 10:59:47 -0500921 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200922
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 if ( ! file_exists($baseclass))
924 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200925 log_message('error', 'Unable to load the requested class: '.$class);
926 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 }
928
Andrey Andreevd7297352012-01-07 22:53:14 +0200929 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 if (in_array($subclass, $this->_ci_loaded_files))
931 {
932 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200933 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 // return a new instance of the object
935 if ( ! is_null($object_name))
936 {
937 $CI =& get_instance();
938 if ( ! isset($CI->$object_name))
939 {
Barry Mienydd671972010-10-04 16:33:58 +0200940 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 }
942 }
Barry Mienydd671972010-10-04 16:33:58 +0200943
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200945 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 return;
947 }
Barry Mienydd671972010-10-04 16:33:58 +0200948
949 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 include_once($subclass);
951 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200952
953 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 }
Barry Mienydd671972010-10-04 16:33:58 +0200955
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600957 $is_duplicate = FALSE;
958 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
Greg Aker3a746652011-04-19 10:59:47 -0500960 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600961
Andrey Andreevd7297352012-01-07 22:53:14 +0200962 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 if ( ! file_exists($filepath))
964 {
965 continue;
966 }
Barry Mienydd671972010-10-04 16:33:58 +0200967
Andrey Andreevd7297352012-01-07 22:53:14 +0200968 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 if (in_array($filepath, $this->_ci_loaded_files))
970 {
971 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200972 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 // return a new instance of the object
974 if ( ! is_null($object_name))
975 {
976 $CI =& get_instance();
977 if ( ! isset($CI->$object_name))
978 {
979 return $this->_ci_init_class($class, '', $params, $object_name);
980 }
981 }
Barry Mienydd671972010-10-04 16:33:58 +0200982
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200984 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 return;
986 }
Barry Mienydd671972010-10-04 16:33:58 +0200987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 include_once($filepath);
989 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200990 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 }
Derek Jones32bf1862010-03-02 13:46:07 -0600992
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 } // END FOREACH
994
Andrey Andreevd7297352012-01-07 22:53:14 +0200995 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 if ($subdir == '')
997 {
998 $path = strtolower($class).'/'.$class;
999 return $this->_ci_load_class($path, $params);
1000 }
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 // If we got this far we were unable to find the requested class.
1003 // We do not issue errors if the load call failed due to a duplicate request
1004 if ($is_duplicate == FALSE)
1005 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001006 log_message('error', 'Unable to load the requested class: '.$class);
1007 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 }
1009 }
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 // --------------------------------------------------------------------
1012
1013 /**
1014 * Instantiates a class
1015 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 * @param string
1017 * @param string
David Behlercda768a2011-08-14 23:52:48 +02001018 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 * @param string an optional object name
Andrey Andreev94af3552012-03-26 23:10:42 +03001020 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001022 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001023 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001024 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 if ($config === NULL)
1026 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001027 // Fetch the config paths containing any package paths
1028 $config_component = $this->_ci_get_component('config');
1029
1030 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001032 // Break on the first found file, thus package files
1033 // are not overridden by default paths
1034 foreach ($config_component->_config_paths as $path)
1035 {
1036 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001037 // are case-sensitive with regard to file names. Check for environment
1038 // first, global next
Andrey Andreev94af3552012-03-26 23:10:42 +03001039 if (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001040 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001041 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001042 break;
1043 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001044 elseif (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001045 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001046 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001047 break;
1048 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001049 elseif (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001050 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001051 include($path.'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001052 break;
1053 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001054 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001055 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001056 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001057 break;
1058 }
1059 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 }
1061 }
Barry Mienydd671972010-10-04 16:33:58 +02001062
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +02001064 {
1065 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 {
1067 $name = 'CI_'.$class;
1068 }
Barry Mienydd671972010-10-04 16:33:58 +02001069 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 {
1071 $name = config_item('subclass_prefix').$class;
1072 }
1073 else
1074 {
1075 $name = $class;
1076 }
1077 }
1078 else
1079 {
1080 $name = $prefix.$class;
1081 }
Barry Mienydd671972010-10-04 16:33:58 +02001082
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 // Is the class name valid?
1084 if ( ! class_exists($name))
1085 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001086 log_message('error', 'Non-existent class: '.$name);
1087 show_error('Non-existent class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 }
Barry Mienydd671972010-10-04 16:33:58 +02001089
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001091 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001093
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 if (is_null($object_name))
1095 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001096 $classvar = isset($this->_ci_varmap[$class]) ? $this->_ci_varmap[$class] : $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 }
1098 else
1099 {
1100 $classvar = $object_name;
1101 }
1102
Barry Mienydd671972010-10-04 16:33:58 +02001103 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 $this->_ci_classes[$class] = $classvar;
1105
Barry Mienydd671972010-10-04 16:33:58 +02001106 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 $CI =& get_instance();
1108 if ($config !== NULL)
1109 {
1110 $CI->$classvar = new $name($config);
1111 }
1112 else
Barry Mienydd671972010-10-04 16:33:58 +02001113 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +02001115 }
1116 }
1117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001119
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 /**
1121 * Autoloader
1122 *
1123 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -06001124 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 * @param array
1127 * @return void
1128 */
Shane Pearson665baec2011-08-22 18:52:19 -05001129 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001130 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001131 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
Greg Akerd96f8822011-12-27 16:23:47 -06001132 {
1133 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
1134 }
1135 else
1136 {
1137 include(APPPATH.'config/autoload.php');
1138 }
Barry Mienydd671972010-10-04 16:33:58 +02001139
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 if ( ! isset($autoload))
1141 {
1142 return FALSE;
1143 }
Barry Mienydd671972010-10-04 16:33:58 +02001144
Phil Sturgeon9730c752010-12-15 10:50:15 +00001145 // Autoload packages
1146 if (isset($autoload['packages']))
1147 {
1148 foreach ($autoload['packages'] as $package_path)
1149 {
1150 $this->add_package_path($package_path);
1151 }
1152 }
1153
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 // Load any custom config file
1155 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001156 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 $CI =& get_instance();
1158 foreach ($autoload['config'] as $key => $val)
1159 {
1160 $CI->config->load($val);
1161 }
Barry Mienydd671972010-10-04 16:33:58 +02001162 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001163
Derek Jonesc6da5032010-03-09 20:44:27 -06001164 // Autoload helpers and languages
1165 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001166 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001167 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 {
1169 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001170 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 }
1172
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001174 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 {
1176 // Load the database driver.
1177 if (in_array('database', $autoload['libraries']))
1178 {
1179 $this->database();
1180 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1181 }
Barry Mienydd671972010-10-04 16:33:58 +02001182
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 // Load all other libraries
1184 foreach ($autoload['libraries'] as $item)
1185 {
1186 $this->library($item);
1187 }
Barry Mienydd671972010-10-04 16:33:58 +02001188 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001189
1190 // Autoload models
1191 if (isset($autoload['model']))
1192 {
1193 $this->model($autoload['model']);
1194 }
Barry Mienydd671972010-10-04 16:33:58 +02001195 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001196
1197 // --------------------------------------------------------------------
1198
1199 /**
1200 * Object to Array
1201 *
1202 * Takes an object as input and converts the class variables to array key/vals
1203 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 * @param object
1205 * @return array
1206 */
Greg Akerf5c84022011-04-19 17:13:03 -05001207 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001209 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 }
1211
1212 // --------------------------------------------------------------------
1213
1214 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001215 * Get a reference to a specific library or model
1216 *
David Behlercda768a2011-08-14 23:52:48 +02001217 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001218 * @return bool
1219 */
Greg Akerf5c84022011-04-19 17:13:03 -05001220 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001221 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001222 $CI =& get_instance();
1223 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001224 }
1225
1226 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001227
Derek Jones32bf1862010-03-02 13:46:07 -06001228 /**
1229 * Prep filename
1230 *
1231 * This function preps the name of various items to make loading them more reliable.
1232 *
Derek Jones32bf1862010-03-02 13:46:07 -06001233 * @param mixed
David Behlercda768a2011-08-14 23:52:48 +02001234 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001235 * @return array
1236 */
Greg Akerf5c84022011-04-19 17:13:03 -05001237 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001238 {
1239 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001240 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001241 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001242 }
1243 else
1244 {
1245 foreach ($filename as $key => $val)
1246 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001247 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001248 }
Barry Mienydd671972010-10-04 16:33:58 +02001249
Derek Jones32bf1862010-03-02 13:46:07 -06001250 return $filename;
1251 }
1252 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001253
Derek Allard2067d1a2008-11-13 22:59:24 +00001254}
1255
1256/* End of file Loader.php */
Andrey Andreev94af3552012-03-26 23:10:42 +03001257/* Location: ./system/core/Loader.php */