Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
| 3 | * CodeIgniter
|
| 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
| 19 | * Loader Class
|
| 20 | *
|
| 21 | * Loads views and files
|
| 22 | *
|
| 23 | * @package CodeIgniter
|
| 24 | * @subpackage Libraries
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 25 | * @author ExpressionEngine Dev Team
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 26 | * @category Loader
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 27 | * @link http://codeigniter.com/user_guide/libraries/loader.html
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 28 | */
|
| 29 | class CI_Loader {
|
| 30 |
|
| 31 | // All these are set automatically. Don't mess with them.
|
| 32 | var $_ci_ob_level;
|
| 33 | var $_ci_view_path = '';
|
| 34 | var $_ci_is_php5 = FALSE;
|
| 35 | var $_ci_is_instance = FALSE; // Whether we should use $this or $CI =& get_instance()
|
| 36 | var $_ci_cached_vars = array();
|
| 37 | var $_ci_classes = array();
|
| 38 | 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 |
|
| 44 |
|
| 45 | /**
|
| 46 | * Constructor
|
| 47 | *
|
| 48 | * Sets the path to the view files and gets the initial output buffering level
|
| 49 | *
|
| 50 | * @access public
|
| 51 | */
|
| 52 | function CI_Loader()
|
| 53 | {
|
| 54 | $this->_ci_is_php5 = (floor(phpversion()) >= 5) ? TRUE : FALSE;
|
| 55 | $this->_ci_view_path = APPPATH.'views/';
|
| 56 | $this->_ci_ob_level = ob_get_level();
|
| 57 |
|
| 58 | log_message('debug', "Loader Class Initialized");
|
| 59 | }
|
| 60 |
|
| 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
|
| 71 | * @param mixed the optional parameters
|
| 72 | * @return void
|
| 73 | */
|
| 74 | function library($library = '', $params = NULL)
|
| 75 | {
|
| 76 | 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 |
|
| 93 | $this->_ci_assign_to_models();
|
| 94 | }
|
| 95 |
|
| 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 (is_array($model))
|
| 111 | {
|
| 112 | foreach($model as $babe)
|
| 113 | {
|
| 114 | $this->model($babe);
|
| 115 | }
|
| 116 | return;
|
| 117 | }
|
| 118 |
|
| 119 | if ($model == '')
|
| 120 | {
|
| 121 | return;
|
| 122 | }
|
| 123 |
|
| 124 | // Is the model in a sub-folder? If so, parse out the filename and path.
|
| 125 | if (strpos($model, '/') === FALSE)
|
| 126 | {
|
| 127 | $path = '';
|
| 128 | }
|
| 129 | else
|
| 130 | {
|
| 131 | $x = explode('/', $model);
|
| 132 | $model = end($x);
|
| 133 | unset($x[count($x)-1]);
|
| 134 | $path = implode('/', $x).'/';
|
| 135 | }
|
| 136 |
|
| 137 | if ($name == '')
|
| 138 | {
|
| 139 | $name = $model;
|
| 140 | }
|
| 141 |
|
| 142 | if (in_array($name, $this->_ci_models, TRUE))
|
| 143 | {
|
| 144 | return;
|
| 145 | }
|
| 146 |
|
| 147 | $CI =& get_instance();
|
| 148 | if (isset($CI->$name))
|
| 149 | {
|
| 150 | show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
|
| 151 | }
|
| 152 |
|
| 153 | $model = strtolower($model);
|
| 154 |
|
| 155 | if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
|
| 156 | {
|
| 157 | show_error('Unable to locate the model you have specified: '.$model);
|
| 158 | }
|
| 159 |
|
| 160 | if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
|
| 161 | {
|
| 162 | if ($db_conn === TRUE)
|
| 163 | $db_conn = '';
|
| 164 |
|
| 165 | $CI->load->database($db_conn, FALSE, TRUE);
|
| 166 | }
|
| 167 |
|
| 168 | if ( ! class_exists('Model'))
|
| 169 | {
|
| 170 | require_once(BASEPATH.'libraries/Model'.EXT);
|
| 171 | }
|
| 172 |
|
| 173 | require_once(APPPATH.'models/'.$path.$model.EXT);
|
| 174 |
|
| 175 | $model = ucfirst($model);
|
| 176 |
|
| 177 | $CI->$name = new $model();
|
| 178 | $CI->$name->_assign_libraries();
|
| 179 |
|
| 180 | $this->_ci_models[] = $name;
|
| 181 | }
|
| 182 |
|
| 183 | // --------------------------------------------------------------------
|
| 184 |
|
| 185 | /**
|
| 186 | * Database Loader
|
| 187 | *
|
| 188 | * @access public
|
| 189 | * @param string the DB credentials
|
| 190 | * @param bool whether to return the DB object
|
| 191 | * @param bool whether to enable active record (this allows us to override the config setting)
|
| 192 | * @return object
|
| 193 | */
|
| 194 | function database($params = '', $return = FALSE, $active_record = FALSE)
|
| 195 | {
|
| 196 | // Do we even need to load the database class?
|
| 197 | if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE)
|
| 198 | {
|
| 199 | return FALSE;
|
| 200 | }
|
| 201 |
|
| 202 | require_once(BASEPATH.'database/DB'.EXT);
|
| 203 |
|
| 204 | if ($return === TRUE)
|
| 205 | {
|
| 206 | return DB($params, $active_record);
|
| 207 | }
|
| 208 |
|
| 209 | // Grab the super object
|
| 210 | $CI =& get_instance();
|
| 211 |
|
| 212 | // Initialize the db variable. Needed to prevent
|
| 213 | // reference errors with some configurations
|
| 214 | $CI->db = '';
|
| 215 |
|
| 216 | // Load the DB class
|
| 217 | $CI->db =& DB($params, $active_record);
|
| 218 |
|
| 219 | // Assign the DB object to any existing models
|
| 220 | $this->_ci_assign_to_models();
|
| 221 | }
|
| 222 |
|
| 223 | // --------------------------------------------------------------------
|
| 224 |
|
| 225 | /**
|
| 226 | * Load the Utilities Class
|
| 227 | *
|
| 228 | * @access public
|
| 229 | * @return string
|
| 230 | */
|
| 231 | function dbutil()
|
| 232 | {
|
| 233 | if ( ! class_exists('CI_DB'))
|
| 234 | {
|
| 235 | $this->database();
|
| 236 | }
|
| 237 |
|
| 238 | $CI =& get_instance();
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 239 |
|
| 240 | // for backwards compatibility, load dbforge so we can extend dbutils off it
|
| 241 | // this use is deprecated and strongly discouraged
|
| 242 | $CI->load->dbforge();
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 243 |
|
| 244 | require_once(BASEPATH.'database/DB_utility'.EXT);
|
| 245 | require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
|
| 246 | $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
|
| 247 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 248 | $CI->dbutil =& new $class();
|
| 249 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 250 | $CI->load->_ci_assign_to_models();
|
| 251 | }
|
| 252 |
|
| 253 | // --------------------------------------------------------------------
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 254 |
|
| 255 | /**
|
| 256 | * Load the Database Forge Class
|
| 257 | *
|
| 258 | * @access public
|
| 259 | * @return string
|
| 260 | */
|
| 261 | function dbforge()
|
| 262 | {
|
| 263 | if ( ! class_exists('CI_DB'))
|
| 264 | {
|
| 265 | $this->database();
|
| 266 | }
|
| 267 |
|
| 268 | $CI =& get_instance();
|
| 269 |
|
| 270 | require_once(BASEPATH.'database/DB_forge'.EXT);
|
| 271 | require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge'.EXT);
|
| 272 | $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
|
| 273 |
|
| 274 | $CI->dbforge = new $class();
|
| 275 | }
|
| 276 |
|
| 277 | // --------------------------------------------------------------------
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 278 |
|
| 279 | /**
|
| 280 | * Load View
|
| 281 | *
|
| 282 | * This function is used to load a "view" file. It has three parameters:
|
| 283 | *
|
| 284 | * 1. The name of the "view" file to be included.
|
| 285 | * 2. An associative array of data to be extracted for use in the view.
|
| 286 | * 3. TRUE/FALSE - whether to return the data or load it. In
|
| 287 | * some cases it's advantageous to be able to return data so that
|
| 288 | * a developer can process it in some way.
|
| 289 | *
|
| 290 | * @access public
|
| 291 | * @param string
|
| 292 | * @param array
|
| 293 | * @param bool
|
| 294 | * @return void
|
| 295 | */
|
| 296 | function view($view, $vars = array(), $return = FALSE)
|
| 297 | {
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 298 | return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 299 | }
|
| 300 |
|
| 301 | // --------------------------------------------------------------------
|
| 302 |
|
| 303 | /**
|
| 304 | * Load File
|
| 305 | *
|
| 306 | * This is a generic file loader
|
| 307 | *
|
| 308 | * @access public
|
| 309 | * @param string
|
| 310 | * @param bool
|
| 311 | * @return string
|
| 312 | */
|
| 313 | function file($path, $return = FALSE)
|
| 314 | {
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 315 | return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 316 | }
|
| 317 |
|
| 318 | // --------------------------------------------------------------------
|
| 319 |
|
| 320 | /**
|
| 321 | * Set Variables
|
| 322 | *
|
| 323 | * Once variables are set they become available within
|
| 324 | * the controller class and its "view" files.
|
| 325 | *
|
| 326 | * @access public
|
| 327 | * @param array
|
| 328 | * @return void
|
| 329 | */
|
| 330 | function vars($vars = array())
|
| 331 | {
|
| 332 | $vars = $this->_ci_object_to_array($vars);
|
| 333 |
|
| 334 | if (is_array($vars) AND count($vars) > 0)
|
| 335 | {
|
| 336 | foreach ($vars as $key => $val)
|
| 337 | {
|
| 338 | $this->_ci_cached_vars[$key] = $val;
|
| 339 | }
|
| 340 | }
|
| 341 | }
|
| 342 |
|
| 343 | // --------------------------------------------------------------------
|
| 344 |
|
| 345 | /**
|
| 346 | * Load Helper
|
| 347 | *
|
| 348 | * This function loads the specified helper file.
|
| 349 | *
|
| 350 | * @access public
|
| 351 | * @param mixed
|
| 352 | * @return void
|
| 353 | */
|
| 354 | function helper($helpers = array())
|
| 355 | {
|
| 356 | if ( ! is_array($helpers))
|
| 357 | {
|
| 358 | $helpers = array($helpers);
|
| 359 | }
|
| 360 |
|
| 361 | foreach ($helpers as $helper)
|
| 362 | {
|
| 363 | $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
|
| 364 |
|
| 365 | if (isset($this->_ci_helpers[$helper]))
|
| 366 | {
|
| 367 | continue;
|
| 368 | }
|
| 369 |
|
| 370 | if (file_exists(APPPATH.'helpers/'.$helper.EXT))
|
| 371 | {
|
| 372 | include_once(APPPATH.'helpers/'.$helper.EXT);
|
| 373 | }
|
| 374 | else
|
| 375 | {
|
| 376 | if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
|
| 377 | {
|
| 378 | include(BASEPATH.'helpers/'.$helper.EXT);
|
| 379 | }
|
| 380 | else
|
| 381 | {
|
| 382 | show_error('Unable to load the requested file: helpers/'.$helper.EXT);
|
| 383 | }
|
| 384 | }
|
| 385 |
|
| 386 | $this->_ci_helpers[$helper] = TRUE;
|
| 387 |
|
| 388 | }
|
| 389 |
|
| 390 | log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
|
| 391 | }
|
| 392 |
|
| 393 | // --------------------------------------------------------------------
|
| 394 |
|
| 395 | /**
|
| 396 | * Load Helpers
|
| 397 | *
|
| 398 | * This is simply an alias to the above function in case the
|
| 399 | * user has written the plural form of this function.
|
| 400 | *
|
| 401 | * @access public
|
| 402 | * @param array
|
| 403 | * @return void
|
| 404 | */
|
| 405 | function helpers($helpers = array())
|
| 406 | {
|
| 407 | $this->helper($helpers);
|
| 408 | }
|
| 409 |
|
| 410 | // --------------------------------------------------------------------
|
| 411 |
|
| 412 | /**
|
| 413 | * Load Plugin
|
| 414 | *
|
| 415 | * This function loads the specified plugin.
|
| 416 | *
|
| 417 | * @access public
|
| 418 | * @param array
|
| 419 | * @return void
|
| 420 | */
|
| 421 | function plugin($plugins = array())
|
| 422 | {
|
| 423 | if ( ! is_array($plugins))
|
| 424 | {
|
| 425 | $plugins = array($plugins);
|
| 426 | }
|
| 427 |
|
| 428 | foreach ($plugins as $plugin)
|
| 429 | {
|
Derek Allard | 17f7406 | 2008-01-16 01:22:44 +0000 | [diff] [blame] | 430 | $plugin = strtolower(str_replace(EXT, '', str_replace('_pi', '', $plugin)).'_pi');
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 431 |
|
| 432 | if (isset($this->_ci_plugins[$plugin]))
|
| 433 | {
|
| 434 | continue;
|
| 435 | }
|
| 436 |
|
| 437 | if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
|
| 438 | {
|
| 439 | include(APPPATH.'plugins/'.$plugin.EXT);
|
| 440 | }
|
| 441 | else
|
| 442 | {
|
| 443 | if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
|
| 444 | {
|
| 445 | include(BASEPATH.'plugins/'.$plugin.EXT);
|
| 446 | }
|
| 447 | else
|
| 448 | {
|
| 449 | show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
|
| 450 | }
|
| 451 | }
|
| 452 |
|
| 453 | $this->_ci_plugins[$plugin] = TRUE;
|
| 454 | }
|
| 455 |
|
| 456 | log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
|
| 457 | }
|
| 458 |
|
| 459 | // --------------------------------------------------------------------
|
| 460 |
|
| 461 | /**
|
| 462 | * Load Plugins
|
| 463 | *
|
| 464 | * This is simply an alias to the above function in case the
|
| 465 | * user has written the plural form of this function.
|
| 466 | *
|
| 467 | * @access public
|
| 468 | * @param array
|
| 469 | * @return void
|
| 470 | */
|
| 471 | function plugins($plugins = array())
|
| 472 | {
|
| 473 | $this->plugin($plugins);
|
| 474 | }
|
| 475 |
|
| 476 | // --------------------------------------------------------------------
|
| 477 |
|
| 478 | /**
|
| 479 | * Load Script
|
| 480 | *
|
| 481 | * This function loads the specified include file from the
|
| 482 | * application/scripts/ folder.
|
| 483 | *
|
| 484 | * NOTE: This feature has been deprecated but it will remain available
|
| 485 | * for legacy users.
|
| 486 | *
|
| 487 | * @access public
|
| 488 | * @param array
|
| 489 | * @return void
|
| 490 | */
|
| 491 | function script($scripts = array())
|
| 492 | {
|
| 493 | if ( ! is_array($scripts))
|
| 494 | {
|
| 495 | $scripts = array($scripts);
|
| 496 | }
|
| 497 |
|
| 498 | foreach ($scripts as $script)
|
| 499 | {
|
| 500 | $script = strtolower(str_replace(EXT, '', $script));
|
| 501 |
|
| 502 | if (isset($this->_ci_scripts[$script]))
|
| 503 | {
|
| 504 | continue;
|
| 505 | }
|
| 506 |
|
| 507 | if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
|
| 508 | {
|
| 509 | show_error('Unable to load the requested script: scripts/'.$script.EXT);
|
| 510 | }
|
| 511 |
|
| 512 | include(APPPATH.'scripts/'.$script.EXT);
|
| 513 | }
|
| 514 |
|
| 515 | log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
|
| 516 | }
|
| 517 |
|
| 518 | // --------------------------------------------------------------------
|
| 519 |
|
| 520 | /**
|
| 521 | * Loads a language file
|
| 522 | *
|
| 523 | * @access public
|
| 524 | * @param array
|
| 525 | * @param string
|
| 526 | * @return void
|
| 527 | */
|
| 528 | function language($file = array(), $lang = '')
|
| 529 | {
|
| 530 | $CI =& get_instance();
|
| 531 |
|
| 532 | if ( ! is_array($file))
|
| 533 | {
|
| 534 | $file = array($file);
|
| 535 | }
|
| 536 |
|
| 537 | foreach ($file as $langfile)
|
| 538 | {
|
| 539 | $CI->lang->load($langfile, $lang);
|
| 540 | }
|
| 541 | }
|
| 542 |
|
| 543 | /**
|
| 544 | * Loads language files for scaffolding
|
| 545 | *
|
| 546 | * @access public
|
| 547 | * @param string
|
| 548 | * @return arra
|
| 549 | */
|
| 550 | function scaffold_language($file = '', $lang = '', $return = FALSE)
|
| 551 | {
|
| 552 | $CI =& get_instance();
|
| 553 | return $CI->lang->load($file, $lang, $return);
|
| 554 | }
|
| 555 |
|
| 556 | // --------------------------------------------------------------------
|
| 557 |
|
| 558 | /**
|
| 559 | * Loads a config file
|
| 560 | *
|
| 561 | * @access public
|
| 562 | * @param string
|
| 563 | * @return void
|
| 564 | */
|
| 565 | function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
|
| 566 | {
|
| 567 | $CI =& get_instance();
|
| 568 | $CI->config->load($file, $use_sections, $fail_gracefully);
|
| 569 | }
|
| 570 |
|
| 571 | // --------------------------------------------------------------------
|
| 572 |
|
| 573 | /**
|
| 574 | * Scaffolding Loader
|
| 575 | *
|
| 576 | * This initializing function works a bit different than the
|
| 577 | * others. It doesn't load the class. Instead, it simply
|
| 578 | * sets a flag indicating that scaffolding is allowed to be
|
| 579 | * used. The actual scaffolding function below is
|
| 580 | * called by the front controller based on whether the
|
| 581 | * second segment of the URL matches the "secret" scaffolding
|
| 582 | * word stored in the application/config/routes.php
|
| 583 | *
|
| 584 | * @access public
|
| 585 | * @param string
|
| 586 | * @return void
|
| 587 | */
|
| 588 | function scaffolding($table = '')
|
| 589 | {
|
| 590 | if ($table === FALSE)
|
| 591 | {
|
| 592 | show_error('You must include the name of the table you would like to access when you initialize scaffolding');
|
| 593 | }
|
| 594 |
|
| 595 | $CI =& get_instance();
|
| 596 | $CI->_ci_scaffolding = TRUE;
|
| 597 | $CI->_ci_scaff_table = $table;
|
| 598 | }
|
| 599 |
|
| 600 | // --------------------------------------------------------------------
|
| 601 |
|
| 602 | /**
|
| 603 | * Loader
|
| 604 | *
|
| 605 | * This function is used to load views and files.
|
| 606 | *
|
| 607 | * @access private
|
| 608 | * @param array
|
| 609 | * @return void
|
| 610 | */
|
| 611 | function _ci_load($data)
|
| 612 | {
|
| 613 | // Set the default data variables
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 614 | foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $val)
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 615 | {
|
| 616 | $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
|
| 617 | }
|
| 618 |
|
| 619 | // Set the path to the requested file
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 620 | if ($_ci_path == '')
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 621 | {
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 622 | $ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
|
| 623 | $file = ($ext == '') ? $_ci_view.EXT : $_ci_view;
|
| 624 | $_ci_path = $this->_ci_view_path.$file;
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 625 | }
|
| 626 | else
|
| 627 | {
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 628 | $x = explode('/', $_ci_path);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 629 | $file = end($x);
|
| 630 | }
|
| 631 |
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 632 | if ( ! file_exists($_ci_path))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 633 | {
|
| 634 | show_error('Unable to load the requested file: '.$file);
|
| 635 | }
|
| 636 |
|
| 637 | // This allows anything loaded using $this->load (views, files, etc.)
|
| 638 | // to become accessible from within the Controller and Model functions.
|
| 639 | // Only needed when running PHP 5
|
| 640 |
|
| 641 | if ($this->_ci_is_instance())
|
| 642 | {
|
| 643 | $CI =& get_instance();
|
| 644 | foreach (get_object_vars($CI) as $key => $var)
|
| 645 | {
|
| 646 | if ( ! isset($this->$key))
|
| 647 | {
|
| 648 | $this->$key =& $CI->$key;
|
| 649 | }
|
| 650 | }
|
| 651 | }
|
| 652 |
|
| 653 | /*
|
| 654 | * Extract and cache variables
|
| 655 | *
|
| 656 | * You can either set variables using the dedicated $this->load_vars()
|
| 657 | * function or via the second parameter of this function. We'll merge
|
| 658 | * the two types and cache them so that views that are embedded within
|
| 659 | * other views can have access to these variables.
|
| 660 | */
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 661 | if (is_array($_ci_vars))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 662 | {
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 663 | $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 664 | }
|
| 665 | extract($this->_ci_cached_vars);
|
| 666 |
|
| 667 | /*
|
| 668 | * Buffer the output
|
| 669 | *
|
| 670 | * We buffer the output for two reasons:
|
| 671 | * 1. Speed. You get a significant speed boost.
|
| 672 | * 2. So that the final rendered template can be
|
| 673 | * post-processed by the output class. Why do we
|
| 674 | * need post processing? For one thing, in order to
|
| 675 | * show the elapsed page load time. Unless we
|
| 676 | * can intercept the content right before it's sent to
|
| 677 | * the browser and then stop the timer it won't be accurate.
|
| 678 | */
|
| 679 | ob_start();
|
| 680 |
|
| 681 | // If the PHP installation does not support short tags we'll
|
| 682 | // do a little string replacement, changing the short tags
|
| 683 | // to standard PHP echo statements.
|
| 684 |
|
| 685 | if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
|
| 686 | {
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 687 | echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))).'<?php ');
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 688 | }
|
| 689 | else
|
| 690 | {
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 691 | include($_ci_path);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 692 | }
|
| 693 |
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 694 | log_message('debug', 'File loaded: '.$_ci_path);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 695 |
|
| 696 | // Return the file data if requested
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 697 | if ($_ci_return === TRUE)
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 698 | {
|
| 699 | $buffer = ob_get_contents();
|
| 700 | @ob_end_clean();
|
| 701 | return $buffer;
|
| 702 | }
|
| 703 |
|
| 704 | /*
|
| 705 | * Flush the buffer... or buff the flusher?
|
| 706 | *
|
| 707 | * In order to permit views to be nested within
|
| 708 | * other views, we need to flush the content back out whenever
|
| 709 | * we are beyond the first level of output buffering so that
|
| 710 | * it can be seen and included properly by the first included
|
| 711 | * template and any subsequent ones. Oy!
|
| 712 | *
|
| 713 | */
|
| 714 | if (ob_get_level() > $this->_ci_ob_level + 1)
|
| 715 | {
|
| 716 | ob_end_flush();
|
| 717 | }
|
| 718 | else
|
| 719 | {
|
| 720 | // PHP 4 requires that we use a global
|
| 721 | global $OUT;
|
Derek Allard | 66f0724 | 2008-01-18 22:36:14 +0000 | [diff] [blame] | 722 | $OUT->append_output(ob_get_contents());
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 723 | @ob_end_clean();
|
| 724 | }
|
| 725 | }
|
| 726 |
|
| 727 | // --------------------------------------------------------------------
|
| 728 |
|
| 729 | /**
|
| 730 | * Load class
|
| 731 | *
|
| 732 | * This function loads the requested class.
|
| 733 | *
|
| 734 | * @access private
|
| 735 | * @param string the item that is being loaded
|
| 736 | * @param mixed any additional parameters
|
| 737 | * @return void
|
| 738 | */
|
| 739 | function _ci_load_class($class, $params = NULL)
|
| 740 | {
|
| 741 | // Get the class name
|
| 742 | $class = str_replace(EXT, '', $class);
|
| 743 |
|
| 744 | // We'll test for both lowercase and capitalized versions of the file name
|
| 745 | foreach (array(ucfirst($class), strtolower($class)) as $class)
|
| 746 | {
|
| 747 | $subclass = APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT;
|
| 748 |
|
| 749 | // Is this a class extension request?
|
| 750 | if (file_exists($subclass))
|
| 751 | {
|
| 752 | $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
|
| 753 |
|
| 754 | if ( ! file_exists($baseclass))
|
| 755 | {
|
| 756 | log_message('error', "Unable to load the requested class: ".$class);
|
| 757 | show_error("Unable to load the requested class: ".$class);
|
| 758 | }
|
| 759 |
|
| 760 | // Safety: Was the class already loaded by a previous call?
|
| 761 | if (in_array($subclass, $this->_ci_classes))
|
| 762 | {
|
| 763 | $is_duplicate = TRUE;
|
| 764 | log_message('debug', $class." class already loaded. Second attempt ignored.");
|
| 765 | return;
|
| 766 | }
|
| 767 |
|
| 768 | include($baseclass);
|
| 769 | include($subclass);
|
| 770 | $this->_ci_classes[] = $subclass;
|
| 771 |
|
| 772 | return $this->_ci_init_class($class, config_item('subclass_prefix'), $params);
|
| 773 | }
|
| 774 |
|
| 775 | // Lets search for the requested library file and load it.
|
| 776 | $is_duplicate = FALSE;
|
| 777 | for ($i = 1; $i < 3; $i++)
|
| 778 | {
|
| 779 | $path = ($i % 2) ? APPPATH : BASEPATH;
|
| 780 | $filepath = $path.'libraries/'.$class.EXT;
|
| 781 |
|
| 782 | // Does the file exist? No? Bummer...
|
| 783 | if ( ! file_exists($filepath))
|
| 784 | {
|
| 785 | continue;
|
| 786 | }
|
| 787 |
|
| 788 | // Safety: Was the class already loaded by a previous call?
|
| 789 | if (in_array($filepath, $this->_ci_classes))
|
| 790 | {
|
| 791 | $is_duplicate = TRUE;
|
| 792 | log_message('debug', $class." class already loaded. Second attempt ignored.");
|
| 793 | return;
|
| 794 | }
|
| 795 |
|
| 796 | include($filepath);
|
| 797 | $this->_ci_classes[] = $filepath;
|
| 798 | return $this->_ci_init_class($class, '', $params);
|
| 799 | }
|
| 800 | } // END FOREACH
|
| 801 |
|
| 802 | // If we got this far we were unable to find the requested class.
|
| 803 | // We do not issue errors if the load call failed due to a duplicate request
|
| 804 | if ($is_duplicate == FALSE)
|
| 805 | {
|
| 806 | log_message('error', "Unable to load the requested class: ".$class);
|
| 807 | show_error("Unable to load the requested class: ".$class);
|
| 808 | }
|
| 809 | }
|
| 810 |
|
| 811 | // --------------------------------------------------------------------
|
| 812 |
|
| 813 | /**
|
| 814 | * Instantiates a class
|
| 815 | *
|
| 816 | * @access private
|
| 817 | * @param string
|
| 818 | * @param string
|
| 819 | * @return null
|
| 820 | */
|
| 821 | function _ci_init_class($class, $prefix = '', $config = FALSE)
|
| 822 | {
|
Derek Allard | 0796b8d | 2008-01-17 03:41:37 +0000 | [diff] [blame] | 823 | $class = strtolower($class);
|
| 824 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 825 | // Is there an associated config file for this class?
|
| 826 | if ($config === NULL)
|
| 827 | {
|
| 828 | if (file_exists(APPPATH.'config/'.$class.EXT))
|
| 829 | {
|
| 830 | include(APPPATH.'config/'.$class.EXT);
|
| 831 | }
|
| 832 | }
|
| 833 |
|
| 834 | if ($prefix == '')
|
| 835 | {
|
| 836 | $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
|
| 837 | }
|
| 838 | else
|
| 839 | {
|
| 840 | $name = $prefix.$class;
|
| 841 | }
|
| 842 |
|
Derek Allard | 0796b8d | 2008-01-17 03:41:37 +0000 | [diff] [blame] | 843 | // Set the variable name we will assign the class to
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 844 | $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
|
| 845 |
|
| 846 | // Instantiate the class
|
| 847 | $CI =& get_instance();
|
| 848 | if ($config !== NULL)
|
| 849 | {
|
| 850 | $CI->$classvar = new $name($config);
|
| 851 | }
|
| 852 | else
|
| 853 | {
|
| 854 | $CI->$classvar = new $name;
|
| 855 | }
|
| 856 | }
|
| 857 |
|
| 858 | // --------------------------------------------------------------------
|
| 859 |
|
| 860 | /**
|
| 861 | * Autoloader
|
| 862 | *
|
| 863 | * The config/autoload.php file contains an array that permits sub-systems,
|
| 864 | * libraries, plugins, and helpers to be loaded automatically.
|
| 865 | *
|
| 866 | * @access private
|
| 867 | * @param array
|
| 868 | * @return void
|
| 869 | */
|
| 870 | function _ci_autoloader()
|
| 871 | {
|
| 872 | include(APPPATH.'config/autoload'.EXT);
|
| 873 |
|
| 874 | if ( ! isset($autoload))
|
| 875 | {
|
| 876 | return FALSE;
|
| 877 | }
|
| 878 |
|
| 879 | // Load any custom config file
|
| 880 | if (count($autoload['config']) > 0)
|
| 881 | {
|
| 882 | $CI =& get_instance();
|
| 883 | foreach ($autoload['config'] as $key => $val)
|
| 884 | {
|
| 885 | $CI->config->load($val);
|
| 886 | }
|
| 887 | }
|
| 888 |
|
| 889 | // Autoload plugins, helpers, scripts and languages
|
| 890 | foreach (array('helper', 'plugin', 'script', 'language') as $type)
|
| 891 | {
|
| 892 | if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
|
| 893 | {
|
| 894 | $this->$type($autoload[$type]);
|
| 895 | }
|
| 896 | }
|
| 897 |
|
Derek Allard | 22259b5 | 2008-01-22 01:47:11 +0000 | [diff] [blame^] | 898 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 899 |
|
| 900 | // A little tweak to remain backward compatible
|
| 901 | // The $autoload['core'] item was deprecated
|
| 902 | if ( ! isset($autoload['libraries']))
|
| 903 | {
|
| 904 | $autoload['libraries'] = $autoload['core'];
|
| 905 | }
|
| 906 |
|
| 907 | // Load libraries
|
| 908 | if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
|
| 909 | {
|
| 910 | // Load the database driver.
|
| 911 | if (in_array('database', $autoload['libraries']))
|
| 912 | {
|
| 913 | $this->database();
|
| 914 | $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
|
| 915 | }
|
| 916 |
|
| 917 | // Load scaffolding
|
| 918 | if (in_array('scaffolding', $autoload['libraries']))
|
| 919 | {
|
| 920 | $this->scaffolding();
|
| 921 | $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
|
| 922 | }
|
| 923 |
|
| 924 | // Load all other libraries
|
| 925 | foreach ($autoload['libraries'] as $item)
|
| 926 | {
|
| 927 | $this->library($item);
|
| 928 | }
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 929 | }
|
Derek Allard | 22259b5 | 2008-01-22 01:47:11 +0000 | [diff] [blame^] | 930 |
|
| 931 | // Autoload models
|
| 932 | if (isset($autoload['model']))
|
| 933 | {
|
| 934 | $this->model($autoload['model']);
|
| 935 | }
|
| 936 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 937 | }
|
| 938 |
|
| 939 | // --------------------------------------------------------------------
|
| 940 |
|
| 941 | /**
|
| 942 | * Assign to Models
|
| 943 | *
|
| 944 | * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
|
| 945 | * will be available to models, if any exist.
|
| 946 | *
|
| 947 | * @access private
|
| 948 | * @param object
|
| 949 | * @return array
|
| 950 | */
|
| 951 | function _ci_assign_to_models()
|
| 952 | {
|
| 953 | if (count($this->_ci_models) == 0)
|
| 954 | {
|
| 955 | return;
|
| 956 | }
|
| 957 |
|
| 958 | if ($this->_ci_is_instance())
|
| 959 | {
|
| 960 | $CI =& get_instance();
|
| 961 | foreach ($this->_ci_models as $model)
|
| 962 | {
|
| 963 | $CI->$model->_assign_libraries();
|
| 964 | }
|
| 965 | }
|
| 966 | else
|
| 967 | {
|
| 968 | foreach ($this->_ci_models as $model)
|
| 969 | {
|
| 970 | $this->$model->_assign_libraries();
|
| 971 | }
|
| 972 | }
|
| 973 | }
|
| 974 |
|
| 975 | // --------------------------------------------------------------------
|
| 976 |
|
| 977 | /**
|
| 978 | * Object to Array
|
| 979 | *
|
| 980 | * Takes an object as input and converts the class variables to array key/vals
|
| 981 | *
|
| 982 | * @access private
|
| 983 | * @param object
|
| 984 | * @return array
|
| 985 | */
|
| 986 | function _ci_object_to_array($object)
|
| 987 | {
|
| 988 | return (is_object($object)) ? get_object_vars($object) : $object;
|
| 989 | }
|
| 990 |
|
| 991 | // --------------------------------------------------------------------
|
| 992 |
|
| 993 | /**
|
| 994 | * Determines whether we should use the CI instance or $this
|
| 995 | *
|
| 996 | * @access private
|
| 997 | * @return bool
|
| 998 | */
|
| 999 | function _ci_is_instance()
|
| 1000 | {
|
| 1001 | if ($this->_ci_is_php5 == TRUE)
|
| 1002 | {
|
| 1003 | return TRUE;
|
| 1004 | }
|
| 1005 |
|
| 1006 | global $CI;
|
| 1007 | return (is_object($CI)) ? TRUE : FALSE;
|
| 1008 | }
|
| 1009 |
|
| 1010 | }
|
| 1011 | ?> |