blob: 7fb3eab417db08aa78d41f18dd78edb9b13445ce [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;
Timothy Warren40403d22012-04-19 16:38:50 -040048
David Behlercda768a2011-08-14 23:52:48 +020049 /**
50 * List of paths to load views from
51 *
52 * @var array
David Behlercda768a2011-08-14 23:52:48 +020053 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040054 protected $_ci_view_paths = array();
Timothy Warren40403d22012-04-19 16:38:50 -040055
David Behlercda768a2011-08-14 23:52:48 +020056 /**
57 * List of paths to load libraries from
58 *
59 * @var array
David Behlercda768a2011-08-14 23:52:48 +020060 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040061 protected $_ci_library_paths = array();
Timothy Warren40403d22012-04-19 16:38:50 -040062
David Behlercda768a2011-08-14 23:52:48 +020063 /**
64 * List of paths to load models from
65 *
66 * @var array
David Behlercda768a2011-08-14 23:52:48 +020067 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040068 protected $_ci_model_paths = array();
Timothy Warren40403d22012-04-19 16:38:50 -040069
David Behlercda768a2011-08-14 23:52:48 +020070 /**
71 * List of paths to load helpers from
72 *
73 * @var array
David Behlercda768a2011-08-14 23:52:48 +020074 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040075 protected $_ci_helper_paths = array();
Timothy Warren40403d22012-04-19 16:38:50 -040076
David Behlercda768a2011-08-14 23:52:48 +020077 /**
78 * List of loaded base classes
David Behlercda768a2011-08-14 23:52:48 +020079 *
80 * @var array
David Behlercda768a2011-08-14 23:52:48 +020081 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040082 protected $_base_classes = array(); // Set by the controller class
Timothy Warren40403d22012-04-19 16:38:50 -040083
David Behlercda768a2011-08-14 23:52:48 +020084 /**
85 * List of cached variables
86 *
87 * @var array
David Behlercda768a2011-08-14 23:52:48 +020088 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040089 protected $_ci_cached_vars = array();
Timothy Warren40403d22012-04-19 16:38:50 -040090
David Behlercda768a2011-08-14 23:52:48 +020091 /**
92 * List of loaded classes
93 *
94 * @var array
David Behlercda768a2011-08-14 23:52:48 +020095 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040096 protected $_ci_classes = array();
Timothy Warren40403d22012-04-19 16:38:50 -040097
David Behlercda768a2011-08-14 23:52:48 +020098 /**
99 * List of loaded files
100 *
101 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200102 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400103 protected $_ci_loaded_files = array();
Timothy Warren40403d22012-04-19 16:38:50 -0400104
David Behlercda768a2011-08-14 23:52:48 +0200105 /**
106 * List of loaded models
107 *
108 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200109 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400110 protected $_ci_models = array();
Timothy Warren40403d22012-04-19 16:38:50 -0400111
David Behlercda768a2011-08-14 23:52:48 +0200112 /**
113 * List of loaded helpers
114 *
115 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200116 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400117 protected $_ci_helpers = array();
Timothy Warren40403d22012-04-19 16:38:50 -0400118
David Behlercda768a2011-08-14 23:52:48 +0200119 /**
120 * List of class name mappings
121 *
122 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200123 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400124 protected $_ci_varmap = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400125 'unit_test' => 'unit',
126 'user_agent' => 'agent'
127 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000128
129 /**
130 * Constructor
131 *
132 * Sets the path to the view files and gets the initial output buffering level
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 */
Greg Akerf5c84022011-04-19 17:13:03 -0500134 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200135 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500136 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -0600137 $this->_ci_library_paths = array(APPPATH, BASEPATH);
138 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
139 $this->_ci_model_paths = array(APPPATH);
Joe Cianflone8eef9c72011-08-21 10:39:06 -0400140 $this->_ci_view_paths = array(VIEWPATH => TRUE);
David Behlercda768a2011-08-14 23:52:48 +0200141
Andrey Andreevd7297352012-01-07 22:53:14 +0200142 log_message('debug', 'Loader Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200146
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500147 /**
Shane Pearson6adfe632011-08-10 16:42:53 -0500148 * Initialize the Loader
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500149 *
150 * This method is called once in CI_Controller.
151 *
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500152 * @return object
153 */
Shane Pearson6adfe632011-08-10 16:42:53 -0500154 public function initialize()
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500155 {
Shane Pearson6adfe632011-08-10 16:42:53 -0500156 $this->_ci_classes = array();
157 $this->_ci_loaded_files = array();
158 $this->_ci_models = array();
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500159 $this->_base_classes =& is_loaded();
Shane Pearson6adfe632011-08-10 16:42:53 -0500160
161 $this->_ci_autoloader();
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500162 return $this;
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Is Loaded
169 *
170 * A utility function to test if a class is in the self::$_ci_classes array.
171 * This function returns the object name if the class tested for is loaded,
172 * and returns FALSE if it isn't.
173 *
174 * It is mainly used in the form_helper -> _get_validation_object()
175 *
176 * @param string class being checked for
177 * @return mixed class object name on the CI SuperObject or FALSE
178 */
179 public function is_loaded($class)
180 {
181 if (isset($this->_ci_classes[$class]))
182 {
183 return $this->_ci_classes[$class];
184 }
David Behlercda768a2011-08-14 23:52:48 +0200185
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500186 return FALSE;
187 }
188
189 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 /**
192 * Class Loader
193 *
194 * This function lets users load and instantiate classes.
195 * It is designed to be called from a user's app controllers.
196 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 * @param string the name of the class
198 * @param mixed the optional parameters
199 * @param string an optional object name
200 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200201 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500202 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Greg Akerce433962010-10-12 09:29:35 -0500204 if (is_array($library))
205 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100206 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500207 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600208 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500209 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000210
Greg Akerce433962010-10-12 09:29:35 -0500211 return;
212 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000213
Derek Jones32bf1862010-03-02 13:46:07 -0600214 if ($library == '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
216 return FALSE;
217 }
218
Derek Jones32bf1862010-03-02 13:46:07 -0600219 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
221 $params = NULL;
222 }
223
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600224 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
226
227 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 /**
230 * Model Loader
231 *
232 * This function lets users load and instantiate models.
233 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 * @param string the name of the class
235 * @param string name for the model
236 * @param bool database connection
237 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200238 */
Greg Akerf5c84022011-04-19 17:13:03 -0500239 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200240 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 if (is_array($model))
242 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500243 foreach ($model as $babe)
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
Barry Mienydd671972010-10-04 16:33:58 +0200245 $this->model($babe);
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247 return;
248 }
249
250 if ($model == '')
251 {
252 return;
253 }
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Jones32bf1862010-03-02 13:46:07 -0600255 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600258 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
Derek Jones32bf1862010-03-02 13:46:07 -0600260 // The path is in front of the last slash
Andrey Andreevd47baab2012-01-09 16:56:46 +0200261 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600262
263 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200264 $model = substr($model, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 if ($name == '')
268 {
269 $name = $model;
270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 if (in_array($name, $this->_ci_models, TRUE))
273 {
274 return;
275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 $CI =& get_instance();
278 if (isset($CI->$name))
279 {
280 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000284
Derek Jones32bf1862010-03-02 13:46:07 -0600285 foreach ($this->_ci_model_paths as $mod_path)
286 {
Greg Aker3a746652011-04-19 10:59:47 -0500287 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600288 {
289 continue;
290 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000291
Andrey Andreev94af3552012-03-26 23:10:42 +0300292 if ($db_conn !== FALSE && ! class_exists('CI_DB'))
Derek Jones32bf1862010-03-02 13:46:07 -0600293 {
294 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500295 {
Derek Jones32bf1862010-03-02 13:46:07 -0600296 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500297 }
Derek Jones32bf1862010-03-02 13:46:07 -0600298
299 $CI->load->database($db_conn, FALSE, TRUE);
300 }
301
Greg Akerbce13482010-10-11 15:37:16 -0500302 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600303 {
304 load_class('Model', 'core');
305 }
306
Greg Aker3a746652011-04-19 10:59:47 -0500307 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600308
309 $model = ucfirst($model);
Derek Jones32bf1862010-03-02 13:46:07 -0600310 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600311 $this->_ci_models[] = $name;
312 return;
313 }
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Jones32bf1862010-03-02 13:46:07 -0600315 // couldn't find the model
316 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 /**
322 * Database Loader
323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @param string the DB credentials
325 * @param bool whether to return the DB object
326 * @param bool whether to enable active record (this allows us to override the config setting)
327 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200328 */
Greg Akerf5c84022011-04-19 17:13:03 -0500329 public function database($params = '', $return = FALSE, $active_record = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
331 // Grab the super object
332 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // Do we even need to load the database class?
Andrey Andreev94af3552012-03-26 23:10:42 +0300335 if (class_exists('CI_DB') && $return == FALSE && $active_record == NULL && isset($CI->db) && is_object($CI->db))
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200338 }
339
Greg Aker3a746652011-04-19 10:59:47 -0500340 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000341
342 if ($return === TRUE)
343 {
344 return DB($params, $active_record);
345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Andrey Andreevd7297352012-01-07 22:53:14 +0200347 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // reference errors with some configurations
349 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // Load the DB class
Barry Mienydd671972010-10-04 16:33:58 +0200352 $CI->db =& DB($params, $active_record);
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // --------------------------------------------------------------------
356
357 /**
358 * Load the Utilities Class
359 *
Barry Mienydd671972010-10-04 16:33:58 +0200360 * @return string
361 */
Greg Akerf5c84022011-04-19 17:13:03 -0500362 public function dbutil()
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
364 if ( ! class_exists('CI_DB'))
365 {
366 $this->database();
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 $CI =& get_instance();
370
371 // for backwards compatibility, load dbforge so we can extend dbutils off it
372 // this use is deprecated and strongly discouraged
373 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200374
Greg Aker3a746652011-04-19 10:59:47 -0500375 require_once(BASEPATH.'database/DB_utility.php');
376 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
378
Pascal Kriete58560022010-11-10 16:01:20 -0500379 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 }
Barry Mienydd671972010-10-04 16:33:58 +0200381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 // --------------------------------------------------------------------
383
384 /**
385 * Load the Database Forge Class
386 *
Barry Mienydd671972010-10-04 16:33:58 +0200387 * @return string
388 */
Greg Akerf5c84022011-04-19 17:13:03 -0500389 public function dbforge()
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
391 if ( ! class_exists('CI_DB'))
392 {
393 $this->database();
394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200397
Greg Aker3a746652011-04-19 10:59:47 -0500398 require_once(BASEPATH.'database/DB_forge.php');
399 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
401
402 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 /**
408 * Load View
409 *
Andrey Andreev94af3552012-03-26 23:10:42 +0300410 * This function is used to load a "view" file. It has three parameters:
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 *
412 * 1. The name of the "view" file to be included.
413 * 2. An associative array of data to be extracted for use in the view.
Andrey Andreev94af3552012-03-26 23:10:42 +0300414 * 3. TRUE/FALSE - whether to return the data or load it. In
415 * some cases it's advantageous to be able to return data so that
416 * a developer can process it in some way.
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 * @param string
419 * @param array
420 * @param bool
421 * @return void
422 */
Greg Akerf5c84022011-04-19 17:13:03 -0500423 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
425 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 /**
431 * Load File
432 *
433 * This is a generic file loader
434 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 * @param string
436 * @param bool
437 * @return string
438 */
Greg Akerf5c84022011-04-19 17:13:03 -0500439 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 /**
447 * Set Variables
448 *
449 * Once variables are set they become available within
450 * the controller class and its "view" files.
451 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 * @param array
David Behlercda768a2011-08-14 23:52:48 +0200453 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 * @return void
455 */
Greg Akerf5c84022011-04-19 17:13:03 -0500456 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300458 if ($val != '' && is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
460 $vars = array($vars => $val);
461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200464
Andrey Andreev94af3552012-03-26 23:10:42 +0300465 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
467 foreach ($vars as $key => $val)
468 {
469 $this->_ci_cached_vars[$key] = $val;
470 }
471 }
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600477 * Get Variable
478 *
479 * Check if a variable is set and retrieve it.
480 *
481 * @param array
482 * @return void
483 */
484 public function get_var($key)
485 {
486 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
487 }
488
489 // --------------------------------------------------------------------
490
491 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600492 * Get Variables
493 *
494 * Retrieve all loaded variables
495 *
496 * @return array
497 */
498 public function get_vars()
499 {
500 return $this->_ci_cached_vars;
501 }
502
503 // --------------------------------------------------------------------
504
505 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * Load Helper
507 *
508 * This function loads the specified helper file.
509 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * @param mixed
511 * @return void
512 */
Greg Akerf5c84022011-04-19 17:13:03 -0500513 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200514 {
Derek Jones32bf1862010-03-02 13:46:07 -0600515 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200516 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 if (isset($this->_ci_helpers[$helper]))
518 {
519 continue;
520 }
Derek Jones32bf1862010-03-02 13:46:07 -0600521
Greg Aker3a746652011-04-19 10:59:47 -0500522 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000523
Barry Mienydd671972010-10-04 16:33:58 +0200524 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 if (file_exists($ext_helper))
526 {
Greg Aker3a746652011-04-19 10:59:47 -0500527 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 if ( ! file_exists($base_helper))
530 {
Greg Aker3a746652011-04-19 10:59:47 -0500531 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 include_once($ext_helper);
535 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Jones32bf1862010-03-02 13:46:07 -0600537 $this->_ci_helpers[$helper] = TRUE;
538 log_message('debug', 'Helper loaded: '.$helper);
539 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Jones32bf1862010-03-02 13:46:07 -0600542 // Try to load the helper
543 foreach ($this->_ci_helper_paths as $path)
544 {
Greg Aker3a746652011-04-19 10:59:47 -0500545 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200546 {
Greg Aker3a746652011-04-19 10:59:47 -0500547 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600548
549 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200550 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600551 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
553 }
554
Derek Jones32bf1862010-03-02 13:46:07 -0600555 // unable to load the helper
556 if ( ! isset($this->_ci_helpers[$helper]))
557 {
Greg Aker3a746652011-04-19 10:59:47 -0500558 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 }
Barry Mienydd671972010-10-04 16:33:58 +0200562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 /**
566 * Load Helpers
567 *
568 * This is simply an alias to the above function in case the
569 * user has written the plural form of this function.
570 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 * @param array
572 * @return void
573 */
Greg Akerf5c84022011-04-19 17:13:03 -0500574 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 {
576 $this->helper($helpers);
577 }
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * Loads a language file
583 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 * @param array
585 * @param string
586 * @return void
587 */
Greg Akerf5c84022011-04-19 17:13:03 -0500588 public function language($file = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 {
590 $CI =& get_instance();
591
592 if ( ! is_array($file))
593 {
594 $file = array($file);
595 }
596
597 foreach ($file as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200598 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 $CI->lang->load($langfile, $lang);
600 }
601 }
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 /**
606 * Loads a config file
607 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200609 * @param bool
610 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 * @return void
612 */
Greg Akerf5c84022011-04-19 17:13:03 -0500613 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200614 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 $CI =& get_instance();
616 $CI->config->load($file, $use_sections, $fail_gracefully);
617 }
618
619 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 /**
Derek Jones8dca0412010-03-05 13:01:44 -0600622 * Driver
623 *
624 * Loads a driver library
625 *
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800626 * @param mixed the name of the class or array of classes
Derek Jones8dca0412010-03-05 13:01:44 -0600627 * @param mixed the optional parameters
628 * @param string an optional object name
629 * @return void
630 */
Greg Akerf5c84022011-04-19 17:13:03 -0500631 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600632 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800633 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800634 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800635 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800636 {
637 $this->driver($driver);
638 }
639 return FALSE;
640 }
641
Derek Jones8dca0412010-03-05 13:01:44 -0600642 if ( ! class_exists('CI_Driver_Library'))
643 {
644 // we aren't instantiating an object here, that'll be done by the Library itself
Greg Aker3a746652011-04-19 10:59:47 -0500645 require BASEPATH.'libraries/Driver.php';
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600646 }
Barry Mienydd671972010-10-04 16:33:58 +0200647
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200648 if ($library == '')
649 {
650 return FALSE;
651 }
652
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600653 // We can save the loader some time since Drivers will *always* be in a subfolder,
654 // and typically identically named to the library
655 if ( ! strpos($library, '/'))
656 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500657 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600658 }
Barry Mienydd671972010-10-04 16:33:58 +0200659
Derek Jones8dca0412010-03-05 13:01:44 -0600660 return $this->library($library, $params, $object_name);
661 }
662
663 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200664
Derek Jones8dca0412010-03-05 13:01:44 -0600665 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600666 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 *
Derek Jones32bf1862010-03-02 13:46:07 -0600668 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 * @param string
Andrey Andreev94af3552012-03-26 23:10:42 +0300671 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600673 */
Greg Akerf5c84022011-04-19 17:13:03 -0500674 public function add_package_path($path, $view_cascade=TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600675 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500676 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200677
Derek Jones32bf1862010-03-02 13:46:07 -0600678 array_unshift($this->_ci_library_paths, $path);
679 array_unshift($this->_ci_model_paths, $path);
680 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200681
Greg Akerf5c84022011-04-19 17:13:03 -0500682 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
683
Derek Jones32bf1862010-03-02 13:46:07 -0600684 // Add config file path
685 $config =& $this->_ci_get_component('config');
686 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 }
688
689 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600690
691 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000692 * Get Package Paths
693 *
694 * Return a list of all package paths, by default it will ignore BASEPATH.
695 *
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000696 * @param string
697 * @return void
698 */
Greg Akerf5c84022011-04-19 17:13:03 -0500699 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000700 {
701 return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
702 }
703
704 // --------------------------------------------------------------------
705
706 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600707 * Remove Package Path
708 *
709 * Remove a path from the library, model, and helper path arrays if it exists
710 * If no path is provided, the most recently added path is removed.
711 *
Andrey Andreev94af3552012-03-26 23:10:42 +0300712 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200713 * @param bool
Andrey Andreev94af3552012-03-26 23:10:42 +0300714 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600715 */
Greg Akerf5c84022011-04-19 17:13:03 -0500716 public function remove_package_path($path = '', $remove_config_path = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600717 {
718 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Jones32bf1862010-03-02 13:46:07 -0600720 if ($path == '')
721 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200722 array_shift($this->_ci_library_paths);
723 array_shift($this->_ci_model_paths);
724 array_shift($this->_ci_helper_paths);
725 array_shift($this->_ci_view_paths);
726 array_shift($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600727 }
728 else
729 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500730 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600731 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
732 {
733 if (($key = array_search($path, $this->{$var})) !== FALSE)
734 {
735 unset($this->{$var}[$key]);
736 }
737 }
David Behlercda768a2011-08-14 23:52:48 +0200738
Greg Akerf5c84022011-04-19 17:13:03 -0500739 if (isset($this->_ci_view_paths[$path.'views/']))
740 {
741 unset($this->_ci_view_paths[$path.'views/']);
742 }
Barry Mienydd671972010-10-04 16:33:58 +0200743
Derek Jones32bf1862010-03-02 13:46:07 -0600744 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
745 {
746 unset($config->_config_paths[$key]);
747 }
748 }
Barry Mienydd671972010-10-04 16:33:58 +0200749
Derek Jones32bf1862010-03-02 13:46:07 -0600750 // make sure the application default paths are still in the array
751 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
752 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
753 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500754 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600755 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
756 }
757
758 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 /**
761 * Loader
762 *
763 * This function is used to load views and files.
764 * Variables are prefixed with _ci_ to avoid symbol collision with
765 * variables made available to view files
766 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 * @param array
768 * @return void
769 */
Greg Akerf5c84022011-04-19 17:13:03 -0500770 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 {
772 // Set the default data variables
773 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
774 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300775 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 }
David Behlercda768a2011-08-14 23:52:48 +0200777
Greg Akerf5c84022011-04-19 17:13:03 -0500778 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000779
780 // Set the path to the requested file
Greg Aker8807be32011-04-21 13:06:15 -0500781 if ($_ci_path != '')
782 {
783 $_ci_x = explode('/', $_ci_path);
784 $_ci_file = end($_ci_x);
785 }
786 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 {
788 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Greg Aker3a746652011-04-19 10:59:47 -0500789 $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500790
791 foreach ($this->_ci_view_paths as $view_file => $cascade)
792 {
793 if (file_exists($view_file.$_ci_file))
794 {
795 $_ci_path = $view_file.$_ci_file;
796 $file_exists = TRUE;
797 break;
798 }
David Behlercda768a2011-08-14 23:52:48 +0200799
Greg Akerf5c84022011-04-19 17:13:03 -0500800 if ( ! $cascade)
801 {
802 break;
David Behlercda768a2011-08-14 23:52:48 +0200803 }
Greg Akerf5c84022011-04-19 17:13:03 -0500804 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Greg Akerf5c84022011-04-19 17:13:03 -0500807 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
809 show_error('Unable to load the requested file: '.$_ci_file);
810 }
Barry Mienydd671972010-10-04 16:33:58 +0200811
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 // This allows anything loaded using $this->load (views, files, etc.)
813 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500814 $_ci_CI =& get_instance();
815 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500817 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500819 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 }
821 }
822
823 /*
824 * Extract and cache variables
825 *
826 * You can either set variables using the dedicated $this->load_vars()
827 * function or via the second parameter of this function. We'll merge
828 * the two types and cache them so that views that are embedded within
829 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200830 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 if (is_array($_ci_vars))
832 {
833 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
834 }
835 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 /*
838 * Buffer the output
839 *
840 * We buffer the output for two reasons:
841 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200842 * 2. So that the final rendered template can be post-processed by
843 * the output class. Why do we need post processing? For one thing,
844 * in order to show the elapsed page load time. Unless we can
845 * intercept the content right before it's sent to the browser and
846 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 */
848 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200849
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 // If the PHP installation does not support short tags we'll
851 // do a little string replacement, changing the short tags
852 // to standard PHP echo statements.
Andrey Andreevf5e8e1c2012-03-06 13:11:27 +0200853 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 +0000854 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200855 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 }
857 else
858 {
859 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
860 }
Barry Mienydd671972010-10-04 16:33:58 +0200861
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 // Return the file data if requested
865 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200866 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 $buffer = ob_get_contents();
868 @ob_end_clean();
869 return $buffer;
870 }
871
872 /*
873 * Flush the buffer... or buff the flusher?
874 *
875 * In order to permit views to be nested within
876 * other views, we need to flush the content back out whenever
877 * we are beyond the first level of output buffering so that
878 * it can be seen and included properly by the first included
879 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200880 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 if (ob_get_level() > $this->_ci_ob_level + 1)
882 {
883 ob_end_flush();
884 }
885 else
886 {
Greg Aker22f1a632010-11-10 15:34:35 -0600887 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 @ob_end_clean();
889 }
890 }
891
892 // --------------------------------------------------------------------
893
894 /**
895 * Load class
896 *
897 * This function loads the requested class.
898 *
Barry Mienydd671972010-10-04 16:33:58 +0200899 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 * @param mixed any additional parameters
901 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200902 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 */
Greg Akerf5c84022011-04-19 17:13:03 -0500904 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200905 {
906 // Get the class name, and while we're at it trim any slashes.
907 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500909 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // Was the path included with the class name?
912 // We look for a slash to determine this
913 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600914 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
Derek Jones32bf1862010-03-02 13:46:07 -0600916 // Extract the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200917 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200918
Derek Jones32bf1862010-03-02 13:46:07 -0600919 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200920 $class = substr($class, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 }
922
923 // We'll test for both lowercase and capitalized versions of the file name
924 foreach (array(ucfirst($class), strtolower($class)) as $class)
925 {
Greg Aker3a746652011-04-19 10:59:47 -0500926 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000927
Barry Mienydd671972010-10-04 16:33:58 +0200928 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 if (file_exists($subclass))
930 {
Greg Aker3a746652011-04-19 10:59:47 -0500931 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 if ( ! file_exists($baseclass))
934 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200935 log_message('error', 'Unable to load the requested class: '.$class);
936 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 }
938
Andrey Andreevd7297352012-01-07 22:53:14 +0200939 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 if (in_array($subclass, $this->_ci_loaded_files))
941 {
942 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200943 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 // return a new instance of the object
945 if ( ! is_null($object_name))
946 {
947 $CI =& get_instance();
948 if ( ! isset($CI->$object_name))
949 {
Barry Mienydd671972010-10-04 16:33:58 +0200950 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
952 }
Barry Mienydd671972010-10-04 16:33:58 +0200953
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200955 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 return;
957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
959 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 include_once($subclass);
961 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200962
963 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 }
Barry Mienydd671972010-10-04 16:33:58 +0200965
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600967 $is_duplicate = FALSE;
968 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
Greg Aker3a746652011-04-19 10:59:47 -0500970 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600971
Andrey Andreevd7297352012-01-07 22:53:14 +0200972 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 if ( ! file_exists($filepath))
974 {
975 continue;
976 }
Barry Mienydd671972010-10-04 16:33:58 +0200977
Andrey Andreevd7297352012-01-07 22:53:14 +0200978 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 if (in_array($filepath, $this->_ci_loaded_files))
980 {
981 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200982 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 // return a new instance of the object
984 if ( ! is_null($object_name))
985 {
986 $CI =& get_instance();
987 if ( ! isset($CI->$object_name))
988 {
989 return $this->_ci_init_class($class, '', $params, $object_name);
990 }
991 }
Barry Mienydd671972010-10-04 16:33:58 +0200992
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200994 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 return;
996 }
Barry Mienydd671972010-10-04 16:33:58 +0200997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 include_once($filepath);
999 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +02001000 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 }
Derek Jones32bf1862010-03-02 13:46:07 -06001002
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 } // END FOREACH
1004
Andrey Andreevd7297352012-01-07 22:53:14 +02001005 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 if ($subdir == '')
1007 {
1008 $path = strtolower($class).'/'.$class;
1009 return $this->_ci_load_class($path, $params);
1010 }
Barry Mienydd671972010-10-04 16:33:58 +02001011
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 // If we got this far we were unable to find the requested class.
1013 // We do not issue errors if the load call failed due to a duplicate request
1014 if ($is_duplicate == FALSE)
1015 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001016 log_message('error', 'Unable to load the requested class: '.$class);
1017 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 }
1019 }
Barry Mienydd671972010-10-04 16:33:58 +02001020
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 // --------------------------------------------------------------------
1022
1023 /**
1024 * Instantiates a class
1025 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 * @param string
1027 * @param string
David Behlercda768a2011-08-14 23:52:48 +02001028 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 * @param string an optional object name
Andrey Andreev94af3552012-03-26 23:10:42 +03001030 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001032 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001033 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001034 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 if ($config === NULL)
1036 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001037 // Fetch the config paths containing any package paths
1038 $config_component = $this->_ci_get_component('config');
1039
1040 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001042 // Break on the first found file, thus package files
1043 // are not overridden by default paths
1044 foreach ($config_component->_config_paths as $path)
1045 {
1046 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001047 // are case-sensitive with regard to file names. Check for environment
1048 // first, global next
Andrey Andreev94af3552012-03-26 23:10:42 +03001049 if (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001050 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001051 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001052 break;
1053 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001054 elseif (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001055 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001056 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001057 break;
1058 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001059 elseif (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001060 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001061 include($path.'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001062 break;
1063 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001064 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001065 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001066 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001067 break;
1068 }
1069 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 }
1071 }
Barry Mienydd671972010-10-04 16:33:58 +02001072
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +02001074 {
1075 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 {
1077 $name = 'CI_'.$class;
1078 }
Barry Mienydd671972010-10-04 16:33:58 +02001079 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 {
1081 $name = config_item('subclass_prefix').$class;
1082 }
1083 else
1084 {
1085 $name = $class;
1086 }
1087 }
1088 else
1089 {
1090 $name = $prefix.$class;
1091 }
Barry Mienydd671972010-10-04 16:33:58 +02001092
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 // Is the class name valid?
1094 if ( ! class_exists($name))
1095 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001096 log_message('error', 'Non-existent class: '.$name);
1097 show_error('Non-existent class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 }
Barry Mienydd671972010-10-04 16:33:58 +02001099
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001101 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001103
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 if (is_null($object_name))
1105 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001106 $classvar = isset($this->_ci_varmap[$class]) ? $this->_ci_varmap[$class] : $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 }
1108 else
1109 {
1110 $classvar = $object_name;
1111 }
1112
Barry Mienydd671972010-10-04 16:33:58 +02001113 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 $this->_ci_classes[$class] = $classvar;
1115
Barry Mienydd671972010-10-04 16:33:58 +02001116 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 $CI =& get_instance();
1118 if ($config !== NULL)
1119 {
1120 $CI->$classvar = new $name($config);
1121 }
1122 else
Barry Mienydd671972010-10-04 16:33:58 +02001123 {
Andrey Andreeva11b16b2012-03-28 12:22:04 +03001124 $CI->$classvar = new $name();
Barry Mienydd671972010-10-04 16:33:58 +02001125 }
1126 }
1127
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001129
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 /**
1131 * Autoloader
1132 *
1133 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -06001134 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 * @return void
1137 */
Shane Pearson665baec2011-08-22 18:52:19 -05001138 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001139 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001140 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
Greg Akerd96f8822011-12-27 16:23:47 -06001141 {
1142 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
1143 }
1144 else
1145 {
1146 include(APPPATH.'config/autoload.php');
1147 }
Barry Mienydd671972010-10-04 16:33:58 +02001148
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 if ( ! isset($autoload))
1150 {
1151 return FALSE;
1152 }
Barry Mienydd671972010-10-04 16:33:58 +02001153
Phil Sturgeon9730c752010-12-15 10:50:15 +00001154 // Autoload packages
1155 if (isset($autoload['packages']))
1156 {
1157 foreach ($autoload['packages'] as $package_path)
1158 {
1159 $this->add_package_path($package_path);
1160 }
1161 }
1162
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 // Load any custom config file
1164 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001165 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 $CI =& get_instance();
1167 foreach ($autoload['config'] as $key => $val)
1168 {
1169 $CI->config->load($val);
1170 }
Barry Mienydd671972010-10-04 16:33:58 +02001171 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001172
Derek Jonesc6da5032010-03-09 20:44:27 -06001173 // Autoload helpers and languages
1174 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001175 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001176 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 {
1178 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001179 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 }
1181
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001183 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 {
1185 // Load the database driver.
1186 if (in_array('database', $autoload['libraries']))
1187 {
1188 $this->database();
1189 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1190 }
Barry Mienydd671972010-10-04 16:33:58 +02001191
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 // Load all other libraries
1193 foreach ($autoload['libraries'] as $item)
1194 {
1195 $this->library($item);
1196 }
Barry Mienydd671972010-10-04 16:33:58 +02001197 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001198
1199 // Autoload models
1200 if (isset($autoload['model']))
1201 {
1202 $this->model($autoload['model']);
1203 }
Barry Mienydd671972010-10-04 16:33:58 +02001204 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001205
1206 // --------------------------------------------------------------------
1207
1208 /**
1209 * Object to Array
1210 *
1211 * Takes an object as input and converts the class variables to array key/vals
1212 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 * @param object
1214 * @return array
1215 */
Greg Akerf5c84022011-04-19 17:13:03 -05001216 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001218 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 }
1220
1221 // --------------------------------------------------------------------
1222
1223 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001224 * Get a reference to a specific library or model
1225 *
David Behlercda768a2011-08-14 23:52:48 +02001226 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001227 * @return bool
1228 */
Greg Akerf5c84022011-04-19 17:13:03 -05001229 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001230 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001231 $CI =& get_instance();
1232 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001233 }
1234
1235 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001236
Derek Jones32bf1862010-03-02 13:46:07 -06001237 /**
1238 * Prep filename
1239 *
1240 * This function preps the name of various items to make loading them more reliable.
1241 *
Derek Jones32bf1862010-03-02 13:46:07 -06001242 * @param mixed
David Behlercda768a2011-08-14 23:52:48 +02001243 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001244 * @return array
1245 */
Greg Akerf5c84022011-04-19 17:13:03 -05001246 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001247 {
1248 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001249 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001250 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001251 }
1252 else
1253 {
1254 foreach ($filename as $key => $val)
1255 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001256 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001257 }
Barry Mienydd671972010-10-04 16:33:58 +02001258
Derek Jones32bf1862010-03-02 13:46:07 -06001259 return $filename;
1260 }
1261 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263}
1264
1265/* End of file Loader.php */
Andrey Andreev94af3552012-03-26 23:10:42 +03001266/* Location: ./system/core/Loader.php */