blob: 41c0a9b94d4c48ed4e1b37314fa8f63daf4431c8 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
17
18/**
19 * Loader Class
admine334c472006-10-21 19:44:22 +000020 *
adminb0dd10f2006-08-25 17:25:49 +000021 * Loads views and files
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @author Rick Ellis
26 * @category Loader
27 * @link http://www.codeigniter.com/user_guide/libraries/loader.html
28 */
29class CI_Loader {
30
admin34f7f2d2006-10-10 08:06:42 +000031 // All these are set automatically. Don't mess with them.
admincf493902006-10-10 07:12:31 +000032 var $_ci_ob_level;
33 var $_ci_view_path = '';
admin34f7f2d2006-10-10 08:06:42 +000034 var $_ci_is_php5 = FALSE;
admin1e303982006-10-10 16:35:43 +000035 var $_ci_is_instance = FALSE; // Whether we should use $this or $CI =& get_instance()
admincf493902006-10-10 07:12:31 +000036 var $_ci_cached_vars = array();
admin9139b0c2006-10-31 17:59:16 +000037 var $_ci_classes = array();
admincf493902006-10-10 07:12:31 +000038 var $_ci_models = array();
39 var $_ci_helpers = array();
40 var $_ci_plugins = array();
41 var $_ci_scripts = array();
42 var $_ci_varmap = array('unit_test' => 'unit', 'user_agent' => 'agent');
43
adminb0dd10f2006-08-25 17:25:49 +000044
45 /**
46 * Constructor
47 *
admin34f7f2d2006-10-10 08:06:42 +000048 * Sets the path to the view files and gets the initial output buffering level
adminb0dd10f2006-08-25 17:25:49 +000049 *
50 * @access public
51 */
52 function CI_Loader()
admincf493902006-10-10 07:12:31 +000053 {
admin34f7f2d2006-10-10 08:06:42 +000054 $this->_ci_is_php5 = (floor(phpversion()) >= 5) ? TRUE : FALSE;
admincf493902006-10-10 07:12:31 +000055 $this->_ci_view_path = APPPATH.'views/';
56 $this->_ci_ob_level = ob_get_level();
adminb0dd10f2006-08-25 17:25:49 +000057
58 log_message('debug', "Loader Class Initialized");
59 }
adminb0dd10f2006-08-25 17:25:49 +000060
61 // --------------------------------------------------------------------
62
63 /**
64 * Class Loader
65 *
66 * This function lets users load and instantiate classes.
67 * It is designed to be called from a user's app controllers.
68 *
69 * @access public
70 * @param string the name of the class
admin80b2bd92006-10-12 18:30:36 +000071 * @param mixed the optional parameters
adminb0dd10f2006-08-25 17:25:49 +000072 * @return void
73 */
adminbe013b32006-11-04 05:07:03 +000074 function library($library = '', $params = NULL)
adminb0dd10f2006-08-25 17:25:49 +000075 {
adminbe013b32006-11-04 05:07:03 +000076 if ($library == '')
77 {
78 return FALSE;
79 }
80
81 if (is_array($library))
82 {
83 foreach ($library as $class)
84 {
85 $this->_ci_load_class($class, $params);
86 }
87 }
88 else
89 {
90 $this->_ci_load_class($library, $params);
91 }
92
admin8f0a8f62006-10-07 01:17:25 +000093 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +000094 }
adminb0dd10f2006-08-25 17:25:49 +000095
96 // --------------------------------------------------------------------
97
98 /**
99 * Model Loader
100 *
101 * This function lets users load and instantiate models.
102 *
103 * @access public
104 * @param string the name of the class
105 * @param mixed any initialization parameters
106 * @return void
107 */
108 function model($model, $name = '', $db_conn = FALSE)
109 {
110 if ($model == '')
111 return;
112
admincf493902006-10-10 07:12:31 +0000113 // Is the model in a sub-folder? If so, parse out the filename and path.
admin8f0a8f62006-10-07 01:17:25 +0000114 if (strpos($model, '/') === FALSE)
115 {
116 $path = '';
117 }
118 else
119 {
120 $x = explode('/', $model);
121 $model = end($x);
122 unset($x[count($x)-1]);
123 $path = implode('/', $x).'/';
124 }
125
126 if ($name == '')
127 {
128 $name = $model;
129 }
130
admincf493902006-10-10 07:12:31 +0000131 if (in_array($name, $this->_ci_models, TRUE))
admin8f0a8f62006-10-07 01:17:25 +0000132 {
133 return;
admincf493902006-10-10 07:12:31 +0000134 }
admin8f0a8f62006-10-07 01:17:25 +0000135
admin0aef2222006-10-12 18:00:22 +0000136 $CI =& get_instance();
137 if (isset($CI->$name))
admin8f0a8f62006-10-07 01:17:25 +0000138 {
admin0aef2222006-10-12 18:00:22 +0000139 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
admin8f0a8f62006-10-07 01:17:25 +0000140 }
141
142 $model = strtolower($model);
143
144 if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
145 {
146 show_error('Unable to locate the model you have specified: '.$model);
147 }
148
149 if ($db_conn !== FALSE)
150 {
151 if ($db_conn === TRUE)
152 $db_conn = '';
153
admin0aef2222006-10-12 18:00:22 +0000154 $CI->load->database($db_conn, FALSE, TRUE);
admin8f0a8f62006-10-07 01:17:25 +0000155 }
156
157 if ( ! class_exists('Model'))
158 {
159 require_once(BASEPATH.'libraries/Model'.EXT);
160 }
161
162 require_once(APPPATH.'models/'.$path.$model.EXT);
163
164 $model = ucfirst($model);
admin34f7f2d2006-10-10 08:06:42 +0000165
admin0aef2222006-10-12 18:00:22 +0000166 $CI->$name = new $model();
167 $CI->$name->_assign_libraries();
admincf493902006-10-10 07:12:31 +0000168
admin0aef2222006-10-12 18:00:22 +0000169 $this->_ci_models[] = $name;
adminb0dd10f2006-08-25 17:25:49 +0000170 }
admin0aef2222006-10-12 18:00:22 +0000171
adminb0dd10f2006-08-25 17:25:49 +0000172 // --------------------------------------------------------------------
173
174 /**
175 * Database Loader
176 *
177 * @access public
178 * @param string the DB credentials
179 * @param bool whether to return the DB object
180 * @param bool whether to enable active record (this allows us to override the config setting)
admina5e812c2006-09-25 02:17:30 +0000181 * @return object
adminb0dd10f2006-08-25 17:25:49 +0000182 */
admin8f0a8f62006-10-07 01:17:25 +0000183 function database($params = '', $return = FALSE, $active_record = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000184 {
admin0aef2222006-10-12 18:00:22 +0000185 // Do we even need to load the database class?
186 if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE)
187 {
188 return FALSE;
189 }
190
admin8f0a8f62006-10-07 01:17:25 +0000191 require_once(BASEPATH.'database/DB'.EXT);
adminfd2750b2006-10-06 17:29:12 +0000192
adminb0dd10f2006-08-25 17:25:49 +0000193 if ($return === TRUE)
194 {
admin0aef2222006-10-12 18:00:22 +0000195 return DB($params, $active_record);
adminb0dd10f2006-08-25 17:25:49 +0000196 }
admin0aef2222006-10-12 18:00:22 +0000197
admin2b47ea42006-10-24 00:15:02 +0000198 // Grab the super object
admin0aef2222006-10-12 18:00:22 +0000199 $CI =& get_instance();
admin2b47ea42006-10-24 00:15:02 +0000200
201 // Initialize the db variable. Needed to prevent
202 // reference errors with some configurations
203 $CI->db = '';
204
205 // Load the DB class
206 $CI->db =& DB($params, $active_record);
207
208 // Assign the DB object to any existing models
admin0aef2222006-10-12 18:00:22 +0000209 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +0000210 }
admin0aef2222006-10-12 18:00:22 +0000211
212 // --------------------------------------------------------------------
213
214 /**
215 * Load the Utilities Class
216 *
217 * @access public
admine334c472006-10-21 19:44:22 +0000218 * @return string
admin0aef2222006-10-12 18:00:22 +0000219 */
220 function dbutil()
221 {
222 if ( ! class_exists('CI_DB'))
223 {
224 $this->database();
225 }
adminb0dd10f2006-08-25 17:25:49 +0000226
admin0aef2222006-10-12 18:00:22 +0000227 $CI =& get_instance();
228
229 require_once(BASEPATH.'database/DB_utility'.EXT);
230 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
231 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
232
233 $CI->dbutil = new $class();
234 $CI->load->_ci_assign_to_models();
235 }
236
adminb0dd10f2006-08-25 17:25:49 +0000237 // --------------------------------------------------------------------
238
239 /**
240 * Load View
241 *
242 * This function is used to load a "view" file. It has three parameters:
243 *
244 * 1. The name of the "view" file to be included.
245 * 2. An associative array of data to be extracted for use in the view.
246 * 3. TRUE/FALSE - whether to return the data or load it. In
adminb807f322006-10-20 04:55:37 +0000247 * some cases it's advantageous to be able to return data so that
adminb0dd10f2006-08-25 17:25:49 +0000248 * a developer can process it in some way.
249 *
250 * @access public
251 * @param string
252 * @param array
253 * @param bool
254 * @return void
255 */
256 function view($view, $vars = array(), $return = FALSE)
257 {
258 return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return));
259 }
adminb0dd10f2006-08-25 17:25:49 +0000260
261 // --------------------------------------------------------------------
262
263 /**
264 * Load File
265 *
266 * This is a generic file loader
267 *
268 * @access public
269 * @param string
270 * @param bool
271 * @return string
272 */
273 function file($path, $return = FALSE)
274 {
275 return $this->_ci_load(array('path' => $path, 'return' => $return));
276 }
adminb0dd10f2006-08-25 17:25:49 +0000277
278 // --------------------------------------------------------------------
279
280 /**
281 * Set Variables
282 *
adminb807f322006-10-20 04:55:37 +0000283 * Once variables are set they become available within
adminb0dd10f2006-08-25 17:25:49 +0000284 * the controller class and its "view" files.
285 *
286 * @access public
287 * @param array
288 * @return void
289 */
290 function vars($vars = array())
291 {
292 $vars = $this->_ci_object_to_array($vars);
293
294 if (is_array($vars) AND count($vars) > 0)
295 {
296 foreach ($vars as $key => $val)
297 {
admincf493902006-10-10 07:12:31 +0000298 $this->_ci_cached_vars[$key] = $val;
adminb0dd10f2006-08-25 17:25:49 +0000299 }
300 }
301 }
adminb0dd10f2006-08-25 17:25:49 +0000302
303 // --------------------------------------------------------------------
304
305 /**
306 * Load Helper
307 *
308 * This function loads the specified helper file.
309 *
310 * @access public
311 * @param mixed
312 * @return void
313 */
314 function helper($helpers = array())
315 {
316 if ( ! is_array($helpers))
317 {
318 $helpers = array($helpers);
319 }
320
321 foreach ($helpers as $helper)
admin9139b0c2006-10-31 17:59:16 +0000322 {
323 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
324
admincf493902006-10-10 07:12:31 +0000325 if (isset($this->_ci_helpers[$helper]))
adminb0dd10f2006-08-25 17:25:49 +0000326 {
327 continue;
328 }
admin9139b0c2006-10-31 17:59:16 +0000329
admin480da812006-09-20 21:11:04 +0000330 if (file_exists(APPPATH.'helpers/'.$helper.EXT))
admin9139b0c2006-10-31 17:59:16 +0000331 {
332 include_once(APPPATH.'helpers/'.$helper.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000333 }
admin480da812006-09-20 21:11:04 +0000334 else
335 {
336 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
337 {
adminf3a62042006-10-29 20:24:11 +0000338 include(BASEPATH.'helpers/'.$helper.EXT);
admin480da812006-09-20 21:11:04 +0000339 }
340 else
341 {
342 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
343 }
344 }
adminb0dd10f2006-08-25 17:25:49 +0000345
admincf493902006-10-10 07:12:31 +0000346 $this->_ci_helpers[$helper] = TRUE;
admin9139b0c2006-10-31 17:59:16 +0000347
adminb0dd10f2006-08-25 17:25:49 +0000348 }
349
350 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
351 }
adminb0dd10f2006-08-25 17:25:49 +0000352
353 // --------------------------------------------------------------------
354
355 /**
356 * Load Helpers
357 *
358 * This is simply an alias to the above function in case the
359 * user has written the plural form of this function.
360 *
361 * @access public
362 * @param array
363 * @return void
364 */
365 function helpers($helpers = array())
366 {
367 $this->helper($helpers);
368 }
adminb0dd10f2006-08-25 17:25:49 +0000369
370 // --------------------------------------------------------------------
371
372 /**
373 * Load Plugin
374 *
375 * This function loads the specified plugin.
376 *
377 * @access public
378 * @param array
379 * @return void
380 */
381 function plugin($plugins = array())
382 {
383 if ( ! is_array($plugins))
384 {
385 $plugins = array($plugins);
386 }
387
388 foreach ($plugins as $plugin)
admin9139b0c2006-10-31 17:59:16 +0000389 {
390 $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi');
391
admincf493902006-10-10 07:12:31 +0000392 if (isset($this->_ci_plugins[$plugin]))
adminb0dd10f2006-08-25 17:25:49 +0000393 {
394 continue;
395 }
admin9139b0c2006-10-31 17:59:16 +0000396
admin480da812006-09-20 21:11:04 +0000397 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000398 {
adminf3a62042006-10-29 20:24:11 +0000399 include(APPPATH.'plugins/'.$plugin.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000400 }
admin480da812006-09-20 21:11:04 +0000401 else
402 {
403 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
404 {
adminf3a62042006-10-29 20:24:11 +0000405 include(BASEPATH.'plugins/'.$plugin.EXT);
admin480da812006-09-20 21:11:04 +0000406 }
407 else
408 {
409 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
410 }
411 }
adminb0dd10f2006-08-25 17:25:49 +0000412
admincf493902006-10-10 07:12:31 +0000413 $this->_ci_plugins[$plugin] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000414 }
415
416 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
417 }
adminfd2750b2006-10-06 17:29:12 +0000418
419 // --------------------------------------------------------------------
adminb0dd10f2006-08-25 17:25:49 +0000420
adminfd2750b2006-10-06 17:29:12 +0000421 /**
422 * Load Plugins
423 *
424 * This is simply an alias to the above function in case the
425 * user has written the plural form of this function.
426 *
427 * @access public
428 * @param array
429 * @return void
430 */
431 function plugins($plugins = array())
432 {
433 $this->plugin($plugins);
434 }
435
adminb0dd10f2006-08-25 17:25:49 +0000436 // --------------------------------------------------------------------
437
438 /**
439 * Load Script
440 *
441 * This function loads the specified include file from the
admin83b05a82006-09-25 21:06:46 +0000442 * application/scripts/ folder.
443 *
444 * NOTE: This feature has been deprecated but it will remain available
445 * for legacy users.
adminb0dd10f2006-08-25 17:25:49 +0000446 *
447 * @access public
448 * @param array
449 * @return void
450 */
451 function script($scripts = array())
452 {
453 if ( ! is_array($scripts))
454 {
455 $scripts = array($scripts);
456 }
457
458 foreach ($scripts as $script)
admin9139b0c2006-10-31 17:59:16 +0000459 {
460 $script = strtolower(str_replace(EXT, '', $script));
461
admincf493902006-10-10 07:12:31 +0000462 if (isset($this->_ci_scripts[$script]))
adminb0dd10f2006-08-25 17:25:49 +0000463 {
464 continue;
465 }
adminb0dd10f2006-08-25 17:25:49 +0000466
467 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
468 {
469 show_error('Unable to load the requested script: scripts/'.$script.EXT);
470 }
471
adminf3a62042006-10-29 20:24:11 +0000472 include(APPPATH.'scripts/'.$script.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000473 }
474
475 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
476 }
adminfd2750b2006-10-06 17:29:12 +0000477
adminb0dd10f2006-08-25 17:25:49 +0000478 // --------------------------------------------------------------------
479
480 /**
481 * Loads a language file
482 *
483 * @access public
484 * @param string
485 * @return void
486 */
487 function language($file = '', $lang = '', $return = FALSE)
488 {
admin0aef2222006-10-12 18:00:22 +0000489 $CI =& get_instance();
490 return $CI->lang->load($file, $lang, $return);
adminb0dd10f2006-08-25 17:25:49 +0000491 }
adminb0dd10f2006-08-25 17:25:49 +0000492
493 // --------------------------------------------------------------------
494
495 /**
496 * Loads a config file
497 *
498 * @access public
499 * @param string
500 * @return void
501 */
502 function config($file = '')
admincf493902006-10-10 07:12:31 +0000503 {
admin0aef2222006-10-12 18:00:22 +0000504 $CI =& get_instance();
505 $CI->config->load($file);
adminb0dd10f2006-08-25 17:25:49 +0000506 }
admin8f0a8f62006-10-07 01:17:25 +0000507
adminb0dd10f2006-08-25 17:25:49 +0000508 // --------------------------------------------------------------------
509
510 /**
admin8f0a8f62006-10-07 01:17:25 +0000511 * Scaffolding Loader
adminb0dd10f2006-08-25 17:25:49 +0000512 *
admin8f0a8f62006-10-07 01:17:25 +0000513 * This initializing function works a bit different than the
514 * others. It doesn't load the class. Instead, it simply
515 * sets a flag indicating that scaffolding is allowed to be
516 * used. The actual scaffolding function below is
517 * called by the front controller based on whether the
518 * second segment of the URL matches the "secret" scaffolding
519 * word stored in the application/config/routes.php
520 *
521 * @access public
adminb0dd10f2006-08-25 17:25:49 +0000522 * @param string
523 * @return void
admin8f0a8f62006-10-07 01:17:25 +0000524 */
525 function scaffolding($table = '')
526 {
527 if ($table === FALSE)
528 {
529 show_error('You must include the name of the table you would like access when you initialize scaffolding');
530 }
admincf493902006-10-10 07:12:31 +0000531
admin0aef2222006-10-12 18:00:22 +0000532 $CI =& get_instance();
533 $CI->_ci_scaffolding = TRUE;
534 $CI->_ci_scaff_table = $table;
adminb0dd10f2006-08-25 17:25:49 +0000535 }
admin8f0a8f62006-10-07 01:17:25 +0000536
adminb0dd10f2006-08-25 17:25:49 +0000537 // --------------------------------------------------------------------
admin8f0a8f62006-10-07 01:17:25 +0000538
adminb0dd10f2006-08-25 17:25:49 +0000539 /**
540 * Loader
541 *
admin8f0a8f62006-10-07 01:17:25 +0000542 * This function is used to load views and files.
adminb0dd10f2006-08-25 17:25:49 +0000543 *
544 * @access private
545 * @param array
546 * @return void
547 */
548 function _ci_load($data)
admin212a3fa2006-09-23 04:22:39 +0000549 {
admin9139b0c2006-10-31 17:59:16 +0000550 // Set the default data variables
551 foreach (array('view', 'vars', 'path', 'return') as $val)
552 {
553 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
554 }
555
556 // Set the path to the requested file
557 if ($path == '')
558 {
559 $ext = pathinfo($view, PATHINFO_EXTENSION);
560 $file = ($ext == '') ? $view.EXT : $view;
561 $path = $this->_ci_view_path.$file;
562 }
563 else
564 {
565 $x = explode('/', $path);
566 $file = end($x);
567 }
568
569 if ( ! file_exists($path))
570 {
571 show_error('Unable to load the requested file: '.$file);
572 }
573
adminb807f322006-10-20 04:55:37 +0000574 // This allows anything loaded using $this->load (views, files, etc.)
adminb0dd10f2006-08-25 17:25:49 +0000575 // to become accessible from within the Controller and Model functions.
admincf493902006-10-10 07:12:31 +0000576 // Only needed when running PHP 5
577
admin1e303982006-10-10 16:35:43 +0000578 if ($this->_ci_is_instance())
adminb0dd10f2006-08-25 17:25:49 +0000579 {
admincf493902006-10-10 07:12:31 +0000580 $CI =& get_instance();
581 foreach (get_object_vars($CI) as $key => $var)
adminb0dd10f2006-08-25 17:25:49 +0000582 {
admincf493902006-10-10 07:12:31 +0000583 if ( ! isset($this->$key))
584 {
585 $this->$key =& $CI->$key;
586 }
admine79dc712006-09-26 03:52:45 +0000587 }
588 }
admincf493902006-10-10 07:12:31 +0000589
adminb0dd10f2006-08-25 17:25:49 +0000590 /*
admin83b05a82006-09-25 21:06:46 +0000591 * Extract and cache variables
adminb0dd10f2006-08-25 17:25:49 +0000592 *
admine334c472006-10-21 19:44:22 +0000593 * You can either set variables using the dedicated $this->load_vars()
594 * function or via the second parameter of this function. We'll merge
595 * the two types and cache them so that views that are embedded within
admin212a3fa2006-09-23 04:22:39 +0000596 * other views can have access to these variables.
adminb0dd10f2006-08-25 17:25:49 +0000597 */
adminb0dd10f2006-08-25 17:25:49 +0000598 if (is_array($vars))
599 {
admincf493902006-10-10 07:12:31 +0000600 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $vars);
adminb0dd10f2006-08-25 17:25:49 +0000601 }
admincf493902006-10-10 07:12:31 +0000602 extract($this->_ci_cached_vars);
adminb0dd10f2006-08-25 17:25:49 +0000603
adminb0dd10f2006-08-25 17:25:49 +0000604 /*
605 * Buffer the output
606 *
607 * We buffer the output for two reasons:
608 * 1. Speed. You get a significant speed boost.
admine334c472006-10-21 19:44:22 +0000609 * 2. So that the final rendered template can be
adminb0dd10f2006-08-25 17:25:49 +0000610 * post-processed by the output class. Why do we
admine334c472006-10-21 19:44:22 +0000611 * need post processing? For one thing, in order to
adminb0dd10f2006-08-25 17:25:49 +0000612 * show the elapsed page load time. Unless we
613 * can intercept the content right before it's sent to
adminb807f322006-10-20 04:55:37 +0000614 * the browser and then stop the timer it won't be accurate.
adminb0dd10f2006-08-25 17:25:49 +0000615 */
adminb0dd10f2006-08-25 17:25:49 +0000616 ob_start();
admin30578ea2006-10-17 02:31:06 +0000617
618 // If the PHP installation does not support short tags we'll
619 // do a little string replacement, changing the short tags
620 // to standard PHP echo statements.
admin30578ea2006-10-17 02:31:06 +0000621
admin49439ff2006-10-30 04:39:12 +0000622 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
admin81a36812006-10-17 03:40:51 +0000623 {
624 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($path))).'<?php ');
admin30578ea2006-10-17 02:31:06 +0000625 }
626 else
627 {
628 include($path);
629 }
630
adminb0dd10f2006-08-25 17:25:49 +0000631 log_message('debug', 'File loaded: '.$path);
632
admin49439ff2006-10-30 04:39:12 +0000633 // Return the file data if requested
adminb0dd10f2006-08-25 17:25:49 +0000634 if ($return === TRUE)
635 {
636 $buffer = ob_get_contents();
adminbe013b32006-11-04 05:07:03 +0000637 @ob_end_clean();
adminb0dd10f2006-08-25 17:25:49 +0000638 return $buffer;
639 }
640
641 /*
642 * Flush the buffer... or buff the flusher?
643 *
admin212a3fa2006-09-23 04:22:39 +0000644 * In order to permit views to be nested within
admine334c472006-10-21 19:44:22 +0000645 * other views, we need to flush the content back out whenever
646 * we are beyond the first level of output buffering so that
647 * it can be seen and included properly by the first included
adminb0dd10f2006-08-25 17:25:49 +0000648 * template and any subsequent ones. Oy!
649 *
admincf493902006-10-10 07:12:31 +0000650 */
651 if (ob_get_level() > $this->_ci_ob_level + 1)
adminb0dd10f2006-08-25 17:25:49 +0000652 {
653 ob_end_flush();
654 }
655 else
656 {
admin34f7f2d2006-10-10 08:06:42 +0000657 // PHP 4 requires that we use a global
admincf493902006-10-10 07:12:31 +0000658 global $OUT;
659 $OUT->set_output(ob_get_contents());
adminbe013b32006-11-04 05:07:03 +0000660 @ob_end_clean();
adminb0dd10f2006-08-25 17:25:49 +0000661 }
662 }
admin8f0a8f62006-10-07 01:17:25 +0000663
664 // --------------------------------------------------------------------
665
666 /**
667 * Load class
668 *
669 * This function loads the requested class.
670 *
671 * @access private
672 * @param string the item that is being loaded
673 * @param mixed any additional parameters
674 * @return void
675 */
admin5a14ea12006-10-12 20:42:55 +0000676 function _ci_load_class($class, $params = NULL)
admin8f0a8f62006-10-07 01:17:25 +0000677 {
adminbe013b32006-11-04 05:07:03 +0000678 // Get the class name
679 $class = str_replace(EXT, '', $class);
admin10c3f412006-10-08 07:21:12 +0000680
adminbe013b32006-11-04 05:07:03 +0000681 // We'll test for both lowercase and capitalized versions of the file name
682 foreach (array(ucfirst($class), strtolower($class)) as $class)
admin0625e192006-10-20 22:16:54 +0000683 {
adminbe013b32006-11-04 05:07:03 +0000684 // Is this a class extension request?
685 if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
admin8f0a8f62006-10-07 01:17:25 +0000686 {
adminbe013b32006-11-04 05:07:03 +0000687 if ( ! file_exists(BASEPATH.'libraries/'.ucfirst($class).EXT))
688 {
689 log_message('error', "Unable to load the requested class: ".$class);
690 show_error("Unable to load the requested class: ".$class);
691 }
692
693 include(BASEPATH.'libraries/'.ucfirst($class).EXT);
694 include(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
695
696 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params);
admin8f0a8f62006-10-07 01:17:25 +0000697 }
adminbe013b32006-11-04 05:07:03 +0000698
699 // Lets search for the requested library file and load it.
700 $is_duplicate = FALSE;
701 for ($i = 1; $i < 3; $i++)
admin69d39842006-10-31 18:01:00 +0000702 {
adminbe013b32006-11-04 05:07:03 +0000703 $path = ($i % 2) ? APPPATH : BASEPATH;
704 $fp = $path.'libraries/'.$class.EXT;
705
706 // Does the file exist? No? Bummer...
707 if ( ! file_exists($fp))
708 {
709 continue;
710 }
711
712 // Safety: Was the class already loaded by a previous call?
713 if (in_array($fp, $this->_ci_classes))
714 {
715 $is_duplicate = TRUE;
716 continue;
717 }
718
719 include($fp);
720 $this->_ci_classes[] = $fp;
721 return $this->_ci_init_class($class, '', $params);
admin69d39842006-10-31 18:01:00 +0000722 }
adminbe013b32006-11-04 05:07:03 +0000723 } // END FOREACH
724
admin9139b0c2006-10-31 17:59:16 +0000725 // If we got this far we were unable to find the requested class.
726 // We do not issue errors if the load call failed due to a duplicate request
727 if ($is_duplicate == FALSE)
728 {
729 log_message('error', "Unable to load the requested class: ".$class);
730 show_error("Unable to load the requested class: ".$class);
731 }
admin8f0a8f62006-10-07 01:17:25 +0000732 }
733
734 // --------------------------------------------------------------------
735
736 /**
737 * Instantiates a class
738 *
739 * @access private
740 * @param string
741 * @param string
742 * @return null
743 */
admin30578ea2006-10-17 02:31:06 +0000744 function _ci_init_class($class, $prefix = '', $config = FALSE)
admin8f0a8f62006-10-07 01:17:25 +0000745 {
746 // Is there an associated config file for this class?
adminca3dafb2006-10-23 01:24:11 +0000747 if ($config === NULL)
admine334c472006-10-21 19:44:22 +0000748 {
adminca3dafb2006-10-23 01:24:11 +0000749 $config = NULL;
admin80b2bd92006-10-12 18:30:36 +0000750 if (file_exists(APPPATH.'config/'.$class.EXT))
751 {
adminf3a62042006-10-29 20:24:11 +0000752 include(APPPATH.'config/'.$class.EXT);
admin80b2bd92006-10-12 18:30:36 +0000753 }
admin8f0a8f62006-10-07 01:17:25 +0000754 }
755
756 if ($prefix == '')
757 {
admin10c3f412006-10-08 07:21:12 +0000758 $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
admin8f0a8f62006-10-07 01:17:25 +0000759 }
760 else
761 {
762 $name = $prefix.$class;
763 }
admin2f0bac82006-10-09 17:36:45 +0000764
adminb1fddc02006-10-09 21:29:07 +0000765 // Set the variable name we will assign the class to
admin80b2bd92006-10-12 18:30:36 +0000766 $class = strtolower($class);
767 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
768
admincf493902006-10-10 07:12:31 +0000769 // Instantiate the class
admin0aef2222006-10-12 18:00:22 +0000770 $CI =& get_instance();
771 if ($config !== NULL)
admin8f0a8f62006-10-07 01:17:25 +0000772 {
admin0aef2222006-10-12 18:00:22 +0000773 $CI->$classvar = new $name($config);
admin8f0a8f62006-10-07 01:17:25 +0000774 }
775 else
admin0aef2222006-10-12 18:00:22 +0000776 {
777 $CI->$classvar = new $name;
778 }
admin8f0a8f62006-10-07 01:17:25 +0000779 }
adminb0dd10f2006-08-25 17:25:49 +0000780
781 // --------------------------------------------------------------------
782
783 /**
784 * Autoloader
785 *
admine334c472006-10-21 19:44:22 +0000786 * The config/autoload.php file contains an array that permits sub-systems,
admin8f0a8f62006-10-07 01:17:25 +0000787 * libraries, plugins, and helpers to be loaded automatically.
adminb0dd10f2006-08-25 17:25:49 +0000788 *
789 * @access private
790 * @param array
791 * @return void
792 */
admin8f0a8f62006-10-07 01:17:25 +0000793 function _ci_autoloader()
794 {
adminf3a62042006-10-29 20:24:11 +0000795 include(APPPATH.'config/autoload'.EXT);
admin8f0a8f62006-10-07 01:17:25 +0000796
797 if ( ! isset($autoload))
798 {
799 return FALSE;
800 }
801
802 // Load any custome config file
803 if (count($autoload['config']) > 0)
admincf493902006-10-10 07:12:31 +0000804 {
admin0aef2222006-10-12 18:00:22 +0000805 $CI =& get_instance();
806 foreach ($autoload['config'] as $key => $val)
admin8f0a8f62006-10-07 01:17:25 +0000807 {
admin0aef2222006-10-12 18:00:22 +0000808 $CI->config->load($val);
admin8f0a8f62006-10-07 01:17:25 +0000809 }
810 }
811
812 // Load plugins, helpers, and scripts
813 foreach (array('helper', 'plugin', 'script') as $type)
814 {
815 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
816 {
817 $this->$type($autoload[$type]);
818 }
819 }
820
821 // A little tweak to remain backward compatible
822 // The $autoload['core'] item was deprecated
823 if ( ! isset($autoload['libraries']))
824 {
825 $autoload['libraries'] = $autoload['core'];
826 }
827
828 // Load libraries
829 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
830 {
admine334c472006-10-21 19:44:22 +0000831 // Load the database driver.
admin8f0a8f62006-10-07 01:17:25 +0000832 if (in_array('database', $autoload['libraries']))
833 {
834 $this->database();
835 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
836 }
837
admine334c472006-10-21 19:44:22 +0000838 // Load the model class.
admin8f0a8f62006-10-07 01:17:25 +0000839 if (in_array('model', $autoload['libraries']))
840 {
841 $this->model();
842 $autoload['libraries'] = array_diff($autoload['libraries'], array('model'));
843 }
admin10c3f412006-10-08 07:21:12 +0000844
845 // Load scaffolding
admin8f0a8f62006-10-07 01:17:25 +0000846 if (in_array('scaffolding', $autoload['libraries']))
847 {
848 $this->scaffolding();
849 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
850 }
851
admin10c3f412006-10-08 07:21:12 +0000852 // Load all other libraries
admin8f0a8f62006-10-07 01:17:25 +0000853 foreach ($autoload['libraries'] as $item)
854 {
855 $this->library($item);
856 }
857 }
858 }
859
860 // --------------------------------------------------------------------
861
862 /**
863 * Assign to Models
864 *
865 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
adminbd6bee72006-10-21 19:39:00 +0000866 * will be available to models, if any exist.
admin8f0a8f62006-10-07 01:17:25 +0000867 *
admin34f7f2d2006-10-10 08:06:42 +0000868 * @access private
admin8f0a8f62006-10-07 01:17:25 +0000869 * @param object
870 * @return array
871 */
872 function _ci_assign_to_models()
adminb0dd10f2006-08-25 17:25:49 +0000873 {
admin2bc13852006-10-24 23:16:17 +0000874 if (count($this->_ci_models) == 0)
875 {
876 return;
877 }
878
admin1e303982006-10-10 16:35:43 +0000879 if ($this->_ci_is_instance())
admincf493902006-10-10 07:12:31 +0000880 {
881 $CI =& get_instance();
882 foreach ($this->_ci_models as $model)
883 {
admin0aef2222006-10-12 18:00:22 +0000884 $CI->$model->_assign_libraries();
admincf493902006-10-10 07:12:31 +0000885 }
886 }
887 else
admin0aef2222006-10-12 18:00:22 +0000888 {
admincf493902006-10-10 07:12:31 +0000889 foreach ($this->_ci_models as $model)
890 {
admin0aef2222006-10-12 18:00:22 +0000891 $this->$model->_assign_libraries();
admincf493902006-10-10 07:12:31 +0000892 }
893 }
admin8f0a8f62006-10-07 01:17:25 +0000894 }
adminb0dd10f2006-08-25 17:25:49 +0000895
896 // --------------------------------------------------------------------
897
898 /**
899 * Object to Array
900 *
adminbd6bee72006-10-21 19:39:00 +0000901 * Takes an object as input and converts the class variables to array key/vals
adminb0dd10f2006-08-25 17:25:49 +0000902 *
admin34f7f2d2006-10-10 08:06:42 +0000903 * @access private
adminb0dd10f2006-08-25 17:25:49 +0000904 * @param object
905 * @return array
906 */
907 function _ci_object_to_array($object)
908 {
admin2e5872a2006-09-06 02:32:55 +0000909 return (is_object($object)) ? get_object_vars($object) : $object;
adminb0dd10f2006-08-25 17:25:49 +0000910 }
admin8f0a8f62006-10-07 01:17:25 +0000911
admin34f7f2d2006-10-10 08:06:42 +0000912 // --------------------------------------------------------------------
913
914 /**
915 * Determines whether we should use the CI instance or $this
916 *
917 * @access private
918 * @return bool
919 */
admin1e303982006-10-10 16:35:43 +0000920 function _ci_is_instance()
admin34f7f2d2006-10-10 08:06:42 +0000921 {
922 if ($this->_ci_is_php5 == TRUE)
923 {
924 return TRUE;
925 }
926
927 global $CI;
928 return (is_object($CI)) ? TRUE : FALSE;
929 }
adminb0dd10f2006-08-25 17:25:49 +0000930
931}
adminb0dd10f2006-08-25 17:25:49 +0000932?>