blob: 5db9886b49dc723a4d27f1f6fa76af280d04b990 [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 */
admin5a14ea12006-10-12 20:42:55 +000074 function library($class, $params = NULL)
adminb0dd10f2006-08-25 17:25:49 +000075 {
76 if ($class == '')
77 return;
78
admin80b2bd92006-10-12 18:30:36 +000079 $this->_ci_load_class($class, $params);
admin8f0a8f62006-10-07 01:17:25 +000080 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +000081 }
adminb0dd10f2006-08-25 17:25:49 +000082
83 // --------------------------------------------------------------------
84
85 /**
86 * Model Loader
87 *
88 * This function lets users load and instantiate models.
89 *
90 * @access public
91 * @param string the name of the class
92 * @param mixed any initialization parameters
93 * @return void
94 */
95 function model($model, $name = '', $db_conn = FALSE)
96 {
97 if ($model == '')
98 return;
99
admincf493902006-10-10 07:12:31 +0000100 // Is the model in a sub-folder? If so, parse out the filename and path.
admin8f0a8f62006-10-07 01:17:25 +0000101 if (strpos($model, '/') === FALSE)
102 {
103 $path = '';
104 }
105 else
106 {
107 $x = explode('/', $model);
108 $model = end($x);
109 unset($x[count($x)-1]);
110 $path = implode('/', $x).'/';
111 }
112
113 if ($name == '')
114 {
115 $name = $model;
116 }
117
admincf493902006-10-10 07:12:31 +0000118 if (in_array($name, $this->_ci_models, TRUE))
admin8f0a8f62006-10-07 01:17:25 +0000119 {
120 return;
admincf493902006-10-10 07:12:31 +0000121 }
admin8f0a8f62006-10-07 01:17:25 +0000122
admin0aef2222006-10-12 18:00:22 +0000123 $CI =& get_instance();
124 if (isset($CI->$name))
admin8f0a8f62006-10-07 01:17:25 +0000125 {
admin0aef2222006-10-12 18:00:22 +0000126 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 +0000127 }
128
129 $model = strtolower($model);
130
131 if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
132 {
133 show_error('Unable to locate the model you have specified: '.$model);
134 }
135
136 if ($db_conn !== FALSE)
137 {
138 if ($db_conn === TRUE)
139 $db_conn = '';
140
admin0aef2222006-10-12 18:00:22 +0000141 $CI->load->database($db_conn, FALSE, TRUE);
admin8f0a8f62006-10-07 01:17:25 +0000142 }
143
144 if ( ! class_exists('Model'))
145 {
146 require_once(BASEPATH.'libraries/Model'.EXT);
147 }
148
149 require_once(APPPATH.'models/'.$path.$model.EXT);
150
151 $model = ucfirst($model);
admin34f7f2d2006-10-10 08:06:42 +0000152
admin0aef2222006-10-12 18:00:22 +0000153 $CI->$name = new $model();
154 $CI->$name->_assign_libraries();
admincf493902006-10-10 07:12:31 +0000155
admin0aef2222006-10-12 18:00:22 +0000156 $this->_ci_models[] = $name;
adminb0dd10f2006-08-25 17:25:49 +0000157 }
admin0aef2222006-10-12 18:00:22 +0000158
adminb0dd10f2006-08-25 17:25:49 +0000159 // --------------------------------------------------------------------
160
161 /**
162 * Database Loader
163 *
164 * @access public
165 * @param string the DB credentials
166 * @param bool whether to return the DB object
167 * @param bool whether to enable active record (this allows us to override the config setting)
admina5e812c2006-09-25 02:17:30 +0000168 * @return object
adminb0dd10f2006-08-25 17:25:49 +0000169 */
admin8f0a8f62006-10-07 01:17:25 +0000170 function database($params = '', $return = FALSE, $active_record = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000171 {
admin0aef2222006-10-12 18:00:22 +0000172 // Do we even need to load the database class?
173 if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE)
174 {
175 return FALSE;
176 }
177
admin8f0a8f62006-10-07 01:17:25 +0000178 require_once(BASEPATH.'database/DB'.EXT);
adminfd2750b2006-10-06 17:29:12 +0000179
adminb0dd10f2006-08-25 17:25:49 +0000180 if ($return === TRUE)
181 {
admin0aef2222006-10-12 18:00:22 +0000182 return DB($params, $active_record);
adminb0dd10f2006-08-25 17:25:49 +0000183 }
admin0aef2222006-10-12 18:00:22 +0000184
admin2b47ea42006-10-24 00:15:02 +0000185 // Grab the super object
admin0aef2222006-10-12 18:00:22 +0000186 $CI =& get_instance();
admin2b47ea42006-10-24 00:15:02 +0000187
188 // Initialize the db variable. Needed to prevent
189 // reference errors with some configurations
190 $CI->db = '';
191
192 // Load the DB class
193 $CI->db =& DB($params, $active_record);
194
195 // Assign the DB object to any existing models
admin0aef2222006-10-12 18:00:22 +0000196 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +0000197 }
admin0aef2222006-10-12 18:00:22 +0000198
199 // --------------------------------------------------------------------
200
201 /**
202 * Load the Utilities Class
203 *
204 * @access public
admine334c472006-10-21 19:44:22 +0000205 * @return string
admin0aef2222006-10-12 18:00:22 +0000206 */
207 function dbutil()
208 {
209 if ( ! class_exists('CI_DB'))
210 {
211 $this->database();
212 }
adminb0dd10f2006-08-25 17:25:49 +0000213
admin0aef2222006-10-12 18:00:22 +0000214 $CI =& get_instance();
215
216 require_once(BASEPATH.'database/DB_utility'.EXT);
217 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
218 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
219
220 $CI->dbutil = new $class();
221 $CI->load->_ci_assign_to_models();
222 }
223
adminb0dd10f2006-08-25 17:25:49 +0000224 // --------------------------------------------------------------------
225
226 /**
227 * Load View
228 *
229 * This function is used to load a "view" file. It has three parameters:
230 *
231 * 1. The name of the "view" file to be included.
232 * 2. An associative array of data to be extracted for use in the view.
233 * 3. TRUE/FALSE - whether to return the data or load it. In
adminb807f322006-10-20 04:55:37 +0000234 * some cases it's advantageous to be able to return data so that
adminb0dd10f2006-08-25 17:25:49 +0000235 * a developer can process it in some way.
236 *
237 * @access public
238 * @param string
239 * @param array
240 * @param bool
241 * @return void
242 */
243 function view($view, $vars = array(), $return = FALSE)
244 {
245 return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return));
246 }
adminb0dd10f2006-08-25 17:25:49 +0000247
248 // --------------------------------------------------------------------
249
250 /**
251 * Load File
252 *
253 * This is a generic file loader
254 *
255 * @access public
256 * @param string
257 * @param bool
258 * @return string
259 */
260 function file($path, $return = FALSE)
261 {
262 return $this->_ci_load(array('path' => $path, 'return' => $return));
263 }
adminb0dd10f2006-08-25 17:25:49 +0000264
265 // --------------------------------------------------------------------
266
267 /**
268 * Set Variables
269 *
adminb807f322006-10-20 04:55:37 +0000270 * Once variables are set they become available within
adminb0dd10f2006-08-25 17:25:49 +0000271 * the controller class and its "view" files.
272 *
273 * @access public
274 * @param array
275 * @return void
276 */
277 function vars($vars = array())
278 {
279 $vars = $this->_ci_object_to_array($vars);
280
281 if (is_array($vars) AND count($vars) > 0)
282 {
283 foreach ($vars as $key => $val)
284 {
admincf493902006-10-10 07:12:31 +0000285 $this->_ci_cached_vars[$key] = $val;
adminb0dd10f2006-08-25 17:25:49 +0000286 }
287 }
288 }
adminb0dd10f2006-08-25 17:25:49 +0000289
290 // --------------------------------------------------------------------
291
292 /**
293 * Load Helper
294 *
295 * This function loads the specified helper file.
296 *
297 * @access public
298 * @param mixed
299 * @return void
300 */
301 function helper($helpers = array())
302 {
303 if ( ! is_array($helpers))
304 {
305 $helpers = array($helpers);
306 }
307
308 foreach ($helpers as $helper)
admin9139b0c2006-10-31 17:59:16 +0000309 {
310 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
311
admincf493902006-10-10 07:12:31 +0000312 if (isset($this->_ci_helpers[$helper]))
adminb0dd10f2006-08-25 17:25:49 +0000313 {
314 continue;
315 }
admin9139b0c2006-10-31 17:59:16 +0000316
admin480da812006-09-20 21:11:04 +0000317 if (file_exists(APPPATH.'helpers/'.$helper.EXT))
admin9139b0c2006-10-31 17:59:16 +0000318 {
319 include_once(APPPATH.'helpers/'.$helper.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000320 }
admin480da812006-09-20 21:11:04 +0000321 else
322 {
323 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
324 {
adminf3a62042006-10-29 20:24:11 +0000325 include(BASEPATH.'helpers/'.$helper.EXT);
admin480da812006-09-20 21:11:04 +0000326 }
327 else
328 {
329 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
330 }
331 }
adminb0dd10f2006-08-25 17:25:49 +0000332
admincf493902006-10-10 07:12:31 +0000333 $this->_ci_helpers[$helper] = TRUE;
admin9139b0c2006-10-31 17:59:16 +0000334
adminb0dd10f2006-08-25 17:25:49 +0000335 }
336
337 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
338 }
adminb0dd10f2006-08-25 17:25:49 +0000339
340 // --------------------------------------------------------------------
341
342 /**
343 * Load Helpers
344 *
345 * This is simply an alias to the above function in case the
346 * user has written the plural form of this function.
347 *
348 * @access public
349 * @param array
350 * @return void
351 */
352 function helpers($helpers = array())
353 {
354 $this->helper($helpers);
355 }
adminb0dd10f2006-08-25 17:25:49 +0000356
357 // --------------------------------------------------------------------
358
359 /**
360 * Load Plugin
361 *
362 * This function loads the specified plugin.
363 *
364 * @access public
365 * @param array
366 * @return void
367 */
368 function plugin($plugins = array())
369 {
370 if ( ! is_array($plugins))
371 {
372 $plugins = array($plugins);
373 }
374
375 foreach ($plugins as $plugin)
admin9139b0c2006-10-31 17:59:16 +0000376 {
377 $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi');
378
admincf493902006-10-10 07:12:31 +0000379 if (isset($this->_ci_plugins[$plugin]))
adminb0dd10f2006-08-25 17:25:49 +0000380 {
381 continue;
382 }
admin9139b0c2006-10-31 17:59:16 +0000383
admin480da812006-09-20 21:11:04 +0000384 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000385 {
adminf3a62042006-10-29 20:24:11 +0000386 include(APPPATH.'plugins/'.$plugin.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000387 }
admin480da812006-09-20 21:11:04 +0000388 else
389 {
390 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
391 {
adminf3a62042006-10-29 20:24:11 +0000392 include(BASEPATH.'plugins/'.$plugin.EXT);
admin480da812006-09-20 21:11:04 +0000393 }
394 else
395 {
396 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
397 }
398 }
adminb0dd10f2006-08-25 17:25:49 +0000399
admincf493902006-10-10 07:12:31 +0000400 $this->_ci_plugins[$plugin] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000401 }
402
403 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
404 }
adminfd2750b2006-10-06 17:29:12 +0000405
406 // --------------------------------------------------------------------
adminb0dd10f2006-08-25 17:25:49 +0000407
adminfd2750b2006-10-06 17:29:12 +0000408 /**
409 * Load Plugins
410 *
411 * This is simply an alias to the above function in case the
412 * user has written the plural form of this function.
413 *
414 * @access public
415 * @param array
416 * @return void
417 */
418 function plugins($plugins = array())
419 {
420 $this->plugin($plugins);
421 }
422
adminb0dd10f2006-08-25 17:25:49 +0000423 // --------------------------------------------------------------------
424
425 /**
426 * Load Script
427 *
428 * This function loads the specified include file from the
admin83b05a82006-09-25 21:06:46 +0000429 * application/scripts/ folder.
430 *
431 * NOTE: This feature has been deprecated but it will remain available
432 * for legacy users.
adminb0dd10f2006-08-25 17:25:49 +0000433 *
434 * @access public
435 * @param array
436 * @return void
437 */
438 function script($scripts = array())
439 {
440 if ( ! is_array($scripts))
441 {
442 $scripts = array($scripts);
443 }
444
445 foreach ($scripts as $script)
admin9139b0c2006-10-31 17:59:16 +0000446 {
447 $script = strtolower(str_replace(EXT, '', $script));
448
admincf493902006-10-10 07:12:31 +0000449 if (isset($this->_ci_scripts[$script]))
adminb0dd10f2006-08-25 17:25:49 +0000450 {
451 continue;
452 }
adminb0dd10f2006-08-25 17:25:49 +0000453
454 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
455 {
456 show_error('Unable to load the requested script: scripts/'.$script.EXT);
457 }
458
adminf3a62042006-10-29 20:24:11 +0000459 include(APPPATH.'scripts/'.$script.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000460 }
461
462 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
463 }
adminfd2750b2006-10-06 17:29:12 +0000464
adminb0dd10f2006-08-25 17:25:49 +0000465 // --------------------------------------------------------------------
466
467 /**
468 * Loads a language file
469 *
470 * @access public
471 * @param string
472 * @return void
473 */
474 function language($file = '', $lang = '', $return = FALSE)
475 {
admin0aef2222006-10-12 18:00:22 +0000476 $CI =& get_instance();
477 return $CI->lang->load($file, $lang, $return);
adminb0dd10f2006-08-25 17:25:49 +0000478 }
adminb0dd10f2006-08-25 17:25:49 +0000479
480 // --------------------------------------------------------------------
481
482 /**
483 * Loads a config file
484 *
485 * @access public
486 * @param string
487 * @return void
488 */
489 function config($file = '')
admincf493902006-10-10 07:12:31 +0000490 {
admin0aef2222006-10-12 18:00:22 +0000491 $CI =& get_instance();
492 $CI->config->load($file);
adminb0dd10f2006-08-25 17:25:49 +0000493 }
admin8f0a8f62006-10-07 01:17:25 +0000494
adminb0dd10f2006-08-25 17:25:49 +0000495 // --------------------------------------------------------------------
496
497 /**
admin8f0a8f62006-10-07 01:17:25 +0000498 * Scaffolding Loader
adminb0dd10f2006-08-25 17:25:49 +0000499 *
admin8f0a8f62006-10-07 01:17:25 +0000500 * This initializing function works a bit different than the
501 * others. It doesn't load the class. Instead, it simply
502 * sets a flag indicating that scaffolding is allowed to be
503 * used. The actual scaffolding function below is
504 * called by the front controller based on whether the
505 * second segment of the URL matches the "secret" scaffolding
506 * word stored in the application/config/routes.php
507 *
508 * @access public
adminb0dd10f2006-08-25 17:25:49 +0000509 * @param string
510 * @return void
admin8f0a8f62006-10-07 01:17:25 +0000511 */
512 function scaffolding($table = '')
513 {
514 if ($table === FALSE)
515 {
516 show_error('You must include the name of the table you would like access when you initialize scaffolding');
517 }
admincf493902006-10-10 07:12:31 +0000518
admin0aef2222006-10-12 18:00:22 +0000519 $CI =& get_instance();
520 $CI->_ci_scaffolding = TRUE;
521 $CI->_ci_scaff_table = $table;
adminb0dd10f2006-08-25 17:25:49 +0000522 }
admin8f0a8f62006-10-07 01:17:25 +0000523
adminb0dd10f2006-08-25 17:25:49 +0000524 // --------------------------------------------------------------------
admin8f0a8f62006-10-07 01:17:25 +0000525
adminb0dd10f2006-08-25 17:25:49 +0000526 /**
527 * Loader
528 *
admin8f0a8f62006-10-07 01:17:25 +0000529 * This function is used to load views and files.
adminb0dd10f2006-08-25 17:25:49 +0000530 *
531 * @access private
532 * @param array
533 * @return void
534 */
535 function _ci_load($data)
admin212a3fa2006-09-23 04:22:39 +0000536 {
admin9139b0c2006-10-31 17:59:16 +0000537 // Set the default data variables
538 foreach (array('view', 'vars', 'path', 'return') as $val)
539 {
540 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
541 }
542
543 // Set the path to the requested file
544 if ($path == '')
545 {
546 $ext = pathinfo($view, PATHINFO_EXTENSION);
547 $file = ($ext == '') ? $view.EXT : $view;
548 $path = $this->_ci_view_path.$file;
549 }
550 else
551 {
552 $x = explode('/', $path);
553 $file = end($x);
554 }
555
556 if ( ! file_exists($path))
557 {
558 show_error('Unable to load the requested file: '.$file);
559 }
560
adminb807f322006-10-20 04:55:37 +0000561 // This allows anything loaded using $this->load (views, files, etc.)
adminb0dd10f2006-08-25 17:25:49 +0000562 // to become accessible from within the Controller and Model functions.
admincf493902006-10-10 07:12:31 +0000563 // Only needed when running PHP 5
564
admin1e303982006-10-10 16:35:43 +0000565 if ($this->_ci_is_instance())
adminb0dd10f2006-08-25 17:25:49 +0000566 {
admincf493902006-10-10 07:12:31 +0000567 $CI =& get_instance();
568 foreach (get_object_vars($CI) as $key => $var)
adminb0dd10f2006-08-25 17:25:49 +0000569 {
admincf493902006-10-10 07:12:31 +0000570 if ( ! isset($this->$key))
571 {
572 $this->$key =& $CI->$key;
573 }
admine79dc712006-09-26 03:52:45 +0000574 }
575 }
admincf493902006-10-10 07:12:31 +0000576
adminb0dd10f2006-08-25 17:25:49 +0000577 /*
admin83b05a82006-09-25 21:06:46 +0000578 * Extract and cache variables
adminb0dd10f2006-08-25 17:25:49 +0000579 *
admine334c472006-10-21 19:44:22 +0000580 * You can either set variables using the dedicated $this->load_vars()
581 * function or via the second parameter of this function. We'll merge
582 * the two types and cache them so that views that are embedded within
admin212a3fa2006-09-23 04:22:39 +0000583 * other views can have access to these variables.
adminb0dd10f2006-08-25 17:25:49 +0000584 */
adminb0dd10f2006-08-25 17:25:49 +0000585 if (is_array($vars))
586 {
admincf493902006-10-10 07:12:31 +0000587 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $vars);
adminb0dd10f2006-08-25 17:25:49 +0000588 }
admincf493902006-10-10 07:12:31 +0000589 extract($this->_ci_cached_vars);
adminb0dd10f2006-08-25 17:25:49 +0000590
adminb0dd10f2006-08-25 17:25:49 +0000591 /*
592 * Buffer the output
593 *
594 * We buffer the output for two reasons:
595 * 1. Speed. You get a significant speed boost.
admine334c472006-10-21 19:44:22 +0000596 * 2. So that the final rendered template can be
adminb0dd10f2006-08-25 17:25:49 +0000597 * post-processed by the output class. Why do we
admine334c472006-10-21 19:44:22 +0000598 * need post processing? For one thing, in order to
adminb0dd10f2006-08-25 17:25:49 +0000599 * show the elapsed page load time. Unless we
600 * can intercept the content right before it's sent to
adminb807f322006-10-20 04:55:37 +0000601 * the browser and then stop the timer it won't be accurate.
adminb0dd10f2006-08-25 17:25:49 +0000602 */
adminb0dd10f2006-08-25 17:25:49 +0000603 ob_start();
admin30578ea2006-10-17 02:31:06 +0000604
605 // If the PHP installation does not support short tags we'll
606 // do a little string replacement, changing the short tags
607 // to standard PHP echo statements.
admin30578ea2006-10-17 02:31:06 +0000608
admin49439ff2006-10-30 04:39:12 +0000609 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
admin81a36812006-10-17 03:40:51 +0000610 {
611 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($path))).'<?php ');
admin30578ea2006-10-17 02:31:06 +0000612 }
613 else
614 {
615 include($path);
616 }
617
adminb0dd10f2006-08-25 17:25:49 +0000618 log_message('debug', 'File loaded: '.$path);
619
admin49439ff2006-10-30 04:39:12 +0000620 // Return the file data if requested
adminb0dd10f2006-08-25 17:25:49 +0000621 if ($return === TRUE)
622 {
623 $buffer = ob_get_contents();
admine334c472006-10-21 19:44:22 +0000624 ob_end_clean();
adminb0dd10f2006-08-25 17:25:49 +0000625
626 return $buffer;
627 }
628
629 /*
630 * Flush the buffer... or buff the flusher?
631 *
admin212a3fa2006-09-23 04:22:39 +0000632 * In order to permit views to be nested within
admine334c472006-10-21 19:44:22 +0000633 * other views, we need to flush the content back out whenever
634 * we are beyond the first level of output buffering so that
635 * it can be seen and included properly by the first included
adminb0dd10f2006-08-25 17:25:49 +0000636 * template and any subsequent ones. Oy!
637 *
admincf493902006-10-10 07:12:31 +0000638 */
639 if (ob_get_level() > $this->_ci_ob_level + 1)
adminb0dd10f2006-08-25 17:25:49 +0000640 {
641 ob_end_flush();
642 }
643 else
644 {
admin34f7f2d2006-10-10 08:06:42 +0000645 // PHP 4 requires that we use a global
admincf493902006-10-10 07:12:31 +0000646 global $OUT;
647 $OUT->set_output(ob_get_contents());
adminb0dd10f2006-08-25 17:25:49 +0000648 ob_end_clean();
649 }
650 }
admin8f0a8f62006-10-07 01:17:25 +0000651
652 // --------------------------------------------------------------------
653
654 /**
655 * Load class
656 *
657 * This function loads the requested class.
658 *
659 * @access private
660 * @param string the item that is being loaded
661 * @param mixed any additional parameters
662 * @return void
663 */
admin5a14ea12006-10-12 20:42:55 +0000664 function _ci_load_class($class, $params = NULL)
admin8f0a8f62006-10-07 01:17:25 +0000665 {
666 // Prep the class name
admin0625e192006-10-20 22:16:54 +0000667 $class = ucfirst(strtolower(str_replace(EXT, '', $class)));
admin10c3f412006-10-08 07:21:12 +0000668
admin0625e192006-10-20 22:16:54 +0000669 // Is this a class extension request?
670 if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
671 {
672 if ( ! file_exists(BASEPATH.'libraries/'.$class.EXT))
admin8f0a8f62006-10-07 01:17:25 +0000673 {
admin0625e192006-10-20 22:16:54 +0000674 log_message('error', "Unable to load the requested class: ".$class);
675 show_error("Unable to load the requested class: ".$class);
admin8f0a8f62006-10-07 01:17:25 +0000676 }
admin0625e192006-10-20 22:16:54 +0000677
adminf3a62042006-10-29 20:24:11 +0000678 include(BASEPATH.'libraries/'.ucfirst($class).EXT);
679 include(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
admin0625e192006-10-20 22:16:54 +0000680
admin570c1612006-10-20 22:28:39 +0000681 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params);
admin8f0a8f62006-10-07 01:17:25 +0000682 }
admin10c3f412006-10-08 07:21:12 +0000683
684 // Lets search for the requested library file and load it.
admin9139b0c2006-10-31 17:59:16 +0000685 $is_duplicate = FALSE;
admin0625e192006-10-20 22:16:54 +0000686 for ($i = 1; $i < 3; $i++)
admin10c3f412006-10-08 07:21:12 +0000687 {
admin9139b0c2006-10-31 17:59:16 +0000688 $path = ($i % 2) ? APPPATH : BASEPATH;
admin9139b0c2006-10-31 17:59:16 +0000689 $fp = $path.'libraries/'.$class.EXT;
690
admin69d39842006-10-31 18:01:00 +0000691 // Does the file exist? No? Bummer...
692 if ( ! file_exists($fp))
693 {
694 continue;
695 }
696
admin9139b0c2006-10-31 17:59:16 +0000697 // Safety: Was the class already loaded by a previous call?
admin69d39842006-10-31 18:01:00 +0000698 if (in_array($fp, $this->_ci_classes))
admin8f0a8f62006-10-07 01:17:25 +0000699 {
admin9139b0c2006-10-31 17:59:16 +0000700 $is_duplicate = TRUE;
701 continue;
admin8f0a8f62006-10-07 01:17:25 +0000702 }
admin9139b0c2006-10-31 17:59:16 +0000703
704 include($fp);
705 $this->_ci_classes[] = $fp;
706 return $this->_ci_init_class($class, '', $params);
admin8f0a8f62006-10-07 01:17:25 +0000707 }
admin9139b0c2006-10-31 17:59:16 +0000708
709 // If we got this far we were unable to find the requested class.
710 // We do not issue errors if the load call failed due to a duplicate request
711 if ($is_duplicate == FALSE)
712 {
713 log_message('error', "Unable to load the requested class: ".$class);
714 show_error("Unable to load the requested class: ".$class);
715 }
admin8f0a8f62006-10-07 01:17:25 +0000716 }
717
718 // --------------------------------------------------------------------
719
720 /**
721 * Instantiates a class
722 *
723 * @access private
724 * @param string
725 * @param string
726 * @return null
727 */
admin30578ea2006-10-17 02:31:06 +0000728 function _ci_init_class($class, $prefix = '', $config = FALSE)
admin8f0a8f62006-10-07 01:17:25 +0000729 {
730 // Is there an associated config file for this class?
adminca3dafb2006-10-23 01:24:11 +0000731 if ($config === NULL)
admine334c472006-10-21 19:44:22 +0000732 {
adminca3dafb2006-10-23 01:24:11 +0000733 $config = NULL;
admin80b2bd92006-10-12 18:30:36 +0000734 if (file_exists(APPPATH.'config/'.$class.EXT))
735 {
adminf3a62042006-10-29 20:24:11 +0000736 include(APPPATH.'config/'.$class.EXT);
admin80b2bd92006-10-12 18:30:36 +0000737 }
admin8f0a8f62006-10-07 01:17:25 +0000738 }
739
740 if ($prefix == '')
741 {
admin10c3f412006-10-08 07:21:12 +0000742 $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
admin8f0a8f62006-10-07 01:17:25 +0000743 }
744 else
745 {
746 $name = $prefix.$class;
747 }
admin2f0bac82006-10-09 17:36:45 +0000748
adminb1fddc02006-10-09 21:29:07 +0000749 // Set the variable name we will assign the class to
admin80b2bd92006-10-12 18:30:36 +0000750 $class = strtolower($class);
751 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
752
admincf493902006-10-10 07:12:31 +0000753 // Instantiate the class
admin0aef2222006-10-12 18:00:22 +0000754 $CI =& get_instance();
755 if ($config !== NULL)
admin8f0a8f62006-10-07 01:17:25 +0000756 {
admin0aef2222006-10-12 18:00:22 +0000757 $CI->$classvar = new $name($config);
admin8f0a8f62006-10-07 01:17:25 +0000758 }
759 else
admin0aef2222006-10-12 18:00:22 +0000760 {
761 $CI->$classvar = new $name;
762 }
admin8f0a8f62006-10-07 01:17:25 +0000763 }
adminb0dd10f2006-08-25 17:25:49 +0000764
765 // --------------------------------------------------------------------
766
767 /**
768 * Autoloader
769 *
admine334c472006-10-21 19:44:22 +0000770 * The config/autoload.php file contains an array that permits sub-systems,
admin8f0a8f62006-10-07 01:17:25 +0000771 * libraries, plugins, and helpers to be loaded automatically.
adminb0dd10f2006-08-25 17:25:49 +0000772 *
773 * @access private
774 * @param array
775 * @return void
776 */
admin8f0a8f62006-10-07 01:17:25 +0000777 function _ci_autoloader()
778 {
adminf3a62042006-10-29 20:24:11 +0000779 include(APPPATH.'config/autoload'.EXT);
admin8f0a8f62006-10-07 01:17:25 +0000780
781 if ( ! isset($autoload))
782 {
783 return FALSE;
784 }
785
786 // Load any custome config file
787 if (count($autoload['config']) > 0)
admincf493902006-10-10 07:12:31 +0000788 {
admin0aef2222006-10-12 18:00:22 +0000789 $CI =& get_instance();
790 foreach ($autoload['config'] as $key => $val)
admin8f0a8f62006-10-07 01:17:25 +0000791 {
admin0aef2222006-10-12 18:00:22 +0000792 $CI->config->load($val);
admin8f0a8f62006-10-07 01:17:25 +0000793 }
794 }
795
796 // Load plugins, helpers, and scripts
797 foreach (array('helper', 'plugin', 'script') as $type)
798 {
799 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
800 {
801 $this->$type($autoload[$type]);
802 }
803 }
804
805 // A little tweak to remain backward compatible
806 // The $autoload['core'] item was deprecated
807 if ( ! isset($autoload['libraries']))
808 {
809 $autoload['libraries'] = $autoload['core'];
810 }
811
812 // Load libraries
813 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
814 {
admine334c472006-10-21 19:44:22 +0000815 // Load the database driver.
admin8f0a8f62006-10-07 01:17:25 +0000816 if (in_array('database', $autoload['libraries']))
817 {
818 $this->database();
819 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
820 }
821
admine334c472006-10-21 19:44:22 +0000822 // Load the model class.
admin8f0a8f62006-10-07 01:17:25 +0000823 if (in_array('model', $autoload['libraries']))
824 {
825 $this->model();
826 $autoload['libraries'] = array_diff($autoload['libraries'], array('model'));
827 }
admin10c3f412006-10-08 07:21:12 +0000828
829 // Load scaffolding
admin8f0a8f62006-10-07 01:17:25 +0000830 if (in_array('scaffolding', $autoload['libraries']))
831 {
832 $this->scaffolding();
833 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
834 }
835
admin10c3f412006-10-08 07:21:12 +0000836 // Load all other libraries
admin8f0a8f62006-10-07 01:17:25 +0000837 foreach ($autoload['libraries'] as $item)
838 {
839 $this->library($item);
840 }
841 }
842 }
843
844 // --------------------------------------------------------------------
845
846 /**
847 * Assign to Models
848 *
849 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
adminbd6bee72006-10-21 19:39:00 +0000850 * will be available to models, if any exist.
admin8f0a8f62006-10-07 01:17:25 +0000851 *
admin34f7f2d2006-10-10 08:06:42 +0000852 * @access private
admin8f0a8f62006-10-07 01:17:25 +0000853 * @param object
854 * @return array
855 */
856 function _ci_assign_to_models()
adminb0dd10f2006-08-25 17:25:49 +0000857 {
admin2bc13852006-10-24 23:16:17 +0000858 if (count($this->_ci_models) == 0)
859 {
860 return;
861 }
862
admin1e303982006-10-10 16:35:43 +0000863 if ($this->_ci_is_instance())
admincf493902006-10-10 07:12:31 +0000864 {
865 $CI =& get_instance();
866 foreach ($this->_ci_models as $model)
867 {
admin0aef2222006-10-12 18:00:22 +0000868 $CI->$model->_assign_libraries();
admincf493902006-10-10 07:12:31 +0000869 }
870 }
871 else
admin0aef2222006-10-12 18:00:22 +0000872 {
admincf493902006-10-10 07:12:31 +0000873 foreach ($this->_ci_models as $model)
874 {
admin0aef2222006-10-12 18:00:22 +0000875 $this->$model->_assign_libraries();
admincf493902006-10-10 07:12:31 +0000876 }
877 }
admin8f0a8f62006-10-07 01:17:25 +0000878 }
adminb0dd10f2006-08-25 17:25:49 +0000879
880 // --------------------------------------------------------------------
881
882 /**
883 * Object to Array
884 *
adminbd6bee72006-10-21 19:39:00 +0000885 * Takes an object as input and converts the class variables to array key/vals
adminb0dd10f2006-08-25 17:25:49 +0000886 *
admin34f7f2d2006-10-10 08:06:42 +0000887 * @access private
adminb0dd10f2006-08-25 17:25:49 +0000888 * @param object
889 * @return array
890 */
891 function _ci_object_to_array($object)
892 {
admin2e5872a2006-09-06 02:32:55 +0000893 return (is_object($object)) ? get_object_vars($object) : $object;
adminb0dd10f2006-08-25 17:25:49 +0000894 }
admin8f0a8f62006-10-07 01:17:25 +0000895
admin34f7f2d2006-10-10 08:06:42 +0000896 // --------------------------------------------------------------------
897
898 /**
899 * Determines whether we should use the CI instance or $this
900 *
901 * @access private
902 * @return bool
903 */
admin1e303982006-10-10 16:35:43 +0000904 function _ci_is_instance()
admin34f7f2d2006-10-10 08:06:42 +0000905 {
906 if ($this->_ci_is_php5 == TRUE)
907 {
908 return TRUE;
909 }
910
911 global $CI;
912 return (is_object($CI)) ? TRUE : FALSE;
913 }
adminb0dd10f2006-08-25 17:25:49 +0000914
915}
adminb0dd10f2006-08-25 17:25:49 +0000916?>