00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 class CI_Loader {
00030
00031
00032 var $_ci_ob_level;
00033 var $_ci_view_path = '';
00034 var $_ci_is_php5 = FALSE;
00035 var $_ci_is_instance = FALSE;
00036 var $_ci_cached_vars = array();
00037 var $_ci_classes = array();
00038 var $_ci_models = array();
00039 var $_ci_helpers = array();
00040 var $_ci_plugins = array();
00041 var $_ci_scripts = array();
00042 var $_ci_varmap = array('unit_test' => 'unit', 'user_agent' => 'agent');
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 function CI_Loader()
00053 {
00054 $this->_ci_is_php5 = (floor(phpversion()) >= 5) ? TRUE : FALSE;
00055 $this->_ci_view_path = APPPATH.'views/';
00056 $this->_ci_ob_level = ob_get_level();
00057
00058 log_message('debug', "Loader Class Initialized");
00059 }
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 function library($library = '', $params = NULL)
00075 {
00076 if ($library == '')
00077 {
00078 return FALSE;
00079 }
00080
00081 if (is_array($library))
00082 {
00083 foreach ($library as $class)
00084 {
00085 $this->_ci_load_class($class, $params);
00086 }
00087 }
00088 else
00089 {
00090 $this->_ci_load_class($library, $params);
00091 }
00092
00093 $this->_ci_assign_to_models();
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 function model($model, $name = '', $db_conn = FALSE)
00109 {
00110 if (is_array($model))
00111 {
00112 foreach($model as $babe)
00113 {
00114 $this->model($babe);
00115 }
00116 return;
00117 }
00118
00119 if ($model == '')
00120 {
00121 return;
00122 }
00123
00124
00125 if (strpos($model, '/') === FALSE)
00126 {
00127 $path = '';
00128 }
00129 else
00130 {
00131 $x = explode('/', $model);
00132 $model = end($x);
00133 unset($x[count($x)-1]);
00134 $path = implode('/', $x).'/';
00135 }
00136
00137 if ($name == '')
00138 {
00139 $name = $model;
00140 }
00141
00142 if (in_array($name, $this->_ci_models, TRUE))
00143 {
00144 return;
00145 }
00146
00147 $CI =& get_instance();
00148 if (isset($CI->$name))
00149 {
00150 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
00151 }
00152
00153 $model = strtolower($model);
00154
00155 if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
00156 {
00157 show_error('Unable to locate the model you have specified: '.$model);
00158 }
00159
00160 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
00161 {
00162 if ($db_conn === TRUE)
00163 $db_conn = '';
00164
00165 $CI->load->database($db_conn, FALSE, TRUE);
00166 }
00167
00168 if ( ! class_exists('Model'))
00169 {
00170 load_class('Model', FALSE);
00171 }
00172
00173 require_once(APPPATH.'models/'.$path.$model.EXT);
00174
00175 $model = ucfirst($model);
00176
00177 $CI->$name = new $model();
00178 $CI->$name->_assign_libraries();
00179
00180 $this->_ci_models[] = $name;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 function database($params = '', $return = FALSE, $active_record = FALSE)
00195 {
00196
00197 $CI =& get_instance();
00198
00199
00200 if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE AND isset($CI->db) AND is_object($CI->db))
00201 {
00202 return FALSE;
00203 }
00204
00205 require_once(BASEPATH.'database/DB'.EXT);
00206
00207 if ($return === TRUE)
00208 {
00209 return DB($params, $active_record);
00210 }
00211
00212
00213
00214 $CI->db = '';
00215
00216
00217 $CI->db =& DB($params, $active_record);
00218
00219
00220 $this->_ci_assign_to_models();
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 function dbutil()
00232 {
00233 if ( ! class_exists('CI_DB'))
00234 {
00235 $this->database();
00236 }
00237
00238 $CI =& get_instance();
00239
00240
00241
00242 $CI->load->dbforge();
00243
00244 require_once(BASEPATH.'database/DB_utility'.EXT);
00245 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
00246 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
00247
00248 $CI->dbutil =& new $class();
00249
00250 $CI->load->_ci_assign_to_models();
00251 }
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 function dbforge()
00262 {
00263 if ( ! class_exists('CI_DB'))
00264 {
00265 $this->database();
00266 }
00267
00268 $CI =& get_instance();
00269
00270 require_once(BASEPATH.'database/DB_forge'.EXT);
00271 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge'.EXT);
00272 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
00273
00274 $CI->dbforge = new $class();
00275
00276 $CI->load->_ci_assign_to_models();
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 function view($view, $vars = array(), $return = FALSE)
00299 {
00300 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
00301 }
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 function file($path, $return = FALSE)
00316 {
00317 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
00318 }
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 function vars($vars = array())
00333 {
00334 $vars = $this->_ci_object_to_array($vars);
00335
00336 if (is_array($vars) AND count($vars) > 0)
00337 {
00338 foreach ($vars as $key => $val)
00339 {
00340 $this->_ci_cached_vars[$key] = $val;
00341 }
00342 }
00343 }
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 function helper($helpers = array())
00357 {
00358 if ( ! is_array($helpers))
00359 {
00360 $helpers = array($helpers);
00361 }
00362
00363 foreach ($helpers as $helper)
00364 {
00365 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
00366
00367 if (isset($this->_ci_helpers[$helper]))
00368 {
00369 continue;
00370 }
00371
00372 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.EXT;
00373
00374
00375 if (file_exists($ext_helper))
00376 {
00377 $base_helper = BASEPATH.'helpers/'.$helper.EXT;
00378
00379 if ( ! file_exists($base_helper))
00380 {
00381 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
00382 }
00383
00384 include_once($ext_helper);
00385 include_once($base_helper);
00386 }
00387 elseif (file_exists(APPPATH.'helpers/'.$helper.EXT))
00388 {
00389 include_once(APPPATH.'helpers/'.$helper.EXT);
00390 }
00391 else
00392 {
00393 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
00394 {
00395 include_once(BASEPATH.'helpers/'.$helper.EXT);
00396 }
00397 else
00398 {
00399 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
00400 }
00401 }
00402
00403 $this->_ci_helpers[$helper] = TRUE;
00404
00405 }
00406
00407 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
00408 }
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422 function helpers($helpers = array())
00423 {
00424 $this->helper($helpers);
00425 }
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438 function plugin($plugins = array())
00439 {
00440 if ( ! is_array($plugins))
00441 {
00442 $plugins = array($plugins);
00443 }
00444
00445 foreach ($plugins as $plugin)
00446 {
00447 $plugin = strtolower(str_replace(EXT, '', str_replace('_pi', '', $plugin)).'_pi');
00448
00449 if (isset($this->_ci_plugins[$plugin]))
00450 {
00451 continue;
00452 }
00453
00454 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
00455 {
00456 include_once(APPPATH.'plugins/'.$plugin.EXT);
00457 }
00458 else
00459 {
00460 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
00461 {
00462 include_once(BASEPATH.'plugins/'.$plugin.EXT);
00463 }
00464 else
00465 {
00466 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
00467 }
00468 }
00469
00470 $this->_ci_plugins[$plugin] = TRUE;
00471 }
00472
00473 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
00474 }
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488 function plugins($plugins = array())
00489 {
00490 $this->plugin($plugins);
00491 }
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508 function script($scripts = array())
00509 {
00510 if ( ! is_array($scripts))
00511 {
00512 $scripts = array($scripts);
00513 }
00514
00515 foreach ($scripts as $script)
00516 {
00517 $script = strtolower(str_replace(EXT, '', $script));
00518
00519 if (isset($this->_ci_scripts[$script]))
00520 {
00521 continue;
00522 }
00523
00524 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
00525 {
00526 show_error('Unable to load the requested script: scripts/'.$script.EXT);
00527 }
00528
00529 include_once(APPPATH.'scripts/'.$script.EXT);
00530 }
00531
00532 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
00533 }
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545 function language($file = array(), $lang = '')
00546 {
00547 $CI =& get_instance();
00548
00549 if ( ! is_array($file))
00550 {
00551 $file = array($file);
00552 }
00553
00554 foreach ($file as $langfile)
00555 {
00556 $CI->lang->load($langfile, $lang);
00557 }
00558 }
00559
00560
00561
00562
00563
00564
00565
00566
00567 function scaffold_language($file = '', $lang = '', $return = FALSE)
00568 {
00569 $CI =& get_instance();
00570 return $CI->lang->load($file, $lang, $return);
00571 }
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582 function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
00583 {
00584 $CI =& get_instance();
00585 $CI->config->load($file, $use_sections, $fail_gracefully);
00586 }
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605 function scaffolding($table = '')
00606 {
00607 if ($table === FALSE)
00608 {
00609 show_error('You must include the name of the table you would like to access when you initialize scaffolding');
00610 }
00611
00612 $CI =& get_instance();
00613 $CI->_ci_scaffolding = TRUE;
00614 $CI->_ci_scaff_table = $table;
00615 }
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630 function _ci_load($_ci_data)
00631 {
00632
00633 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
00634 {
00635 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
00636 }
00637
00638
00639 if ($_ci_path == '')
00640 {
00641 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
00642 $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
00643 $_ci_path = $this->_ci_view_path.$_ci_file;
00644 }
00645 else
00646 {
00647 $_ci_x = explode('/', $_ci_path);
00648 $_ci_file = end($_ci_x);
00649 }
00650
00651 if ( ! file_exists($_ci_path))
00652 {
00653 show_error('Unable to load the requested file: '.$_ci_file);
00654 }
00655
00656
00657
00658
00659
00660 if ($this->_ci_is_instance())
00661 {
00662 $_ci_CI =& get_instance();
00663 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
00664 {
00665 if ( ! isset($this->$_ci_key))
00666 {
00667 $this->$_ci_key =& $_ci_CI->$_ci_key;
00668 }
00669 }
00670 }
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680 if (is_array($_ci_vars))
00681 {
00682 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
00683 }
00684 extract($this->_ci_cached_vars);
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698 ob_start();
00699
00700
00701
00702
00703
00704 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
00705 {
00706 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
00707 }
00708 else
00709 {
00710 include($_ci_path);
00711 }
00712
00713 log_message('debug', 'File loaded: '.$_ci_path);
00714
00715
00716 if ($_ci_return === TRUE)
00717 {
00718 $buffer = ob_get_contents();
00719 @ob_end_clean();
00720 return $buffer;
00721 }
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733 if (ob_get_level() > $this->_ci_ob_level + 1)
00734 {
00735 ob_end_flush();
00736 }
00737 else
00738 {
00739
00740 global $OUT;
00741 $OUT->append_output(ob_get_contents());
00742 @ob_end_clean();
00743 }
00744 }
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758 function _ci_load_class($class, $params = NULL)
00759 {
00760
00761 $class = str_replace(EXT, '', $class);
00762
00763
00764 foreach (array(ucfirst($class), strtolower($class)) as $class)
00765 {
00766 $subclass = APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT;
00767
00768
00769 if (file_exists($subclass))
00770 {
00771 $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
00772
00773 if ( ! file_exists($baseclass))
00774 {
00775 log_message('error', "Unable to load the requested class: ".$class);
00776 show_error("Unable to load the requested class: ".$class);
00777 }
00778
00779
00780 if (in_array($subclass, $this->_ci_classes))
00781 {
00782 $is_duplicate = TRUE;
00783 log_message('debug', $class." class already loaded. Second attempt ignored.");
00784 return;
00785 }
00786
00787 include_once($baseclass);
00788 include_once($subclass);
00789 $this->_ci_classes[] = $subclass;
00790
00791 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params);
00792 }
00793
00794
00795 $is_duplicate = FALSE;
00796 for ($i = 1; $i < 3; $i++)
00797 {
00798 $path = ($i % 2) ? APPPATH : BASEPATH;
00799 $filepath = $path.'libraries/'.$class.EXT;
00800
00801
00802 if ( ! file_exists($filepath))
00803 {
00804 continue;
00805 }
00806
00807
00808 if (in_array($filepath, $this->_ci_classes))
00809 {
00810 $is_duplicate = TRUE;
00811 log_message('debug', $class." class already loaded. Second attempt ignored.");
00812 return;
00813 }
00814
00815 include_once($filepath);
00816 $this->_ci_classes[] = $filepath;
00817 return $this->_ci_init_class($class, '', $params);
00818 }
00819 }
00820
00821
00822
00823 if ($is_duplicate == FALSE)
00824 {
00825 log_message('error', "Unable to load the requested class: ".$class);
00826 show_error("Unable to load the requested class: ".$class);
00827 }
00828 }
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840 function _ci_init_class($class, $prefix = '', $config = FALSE)
00841 {
00842 $class = strtolower($class);
00843
00844
00845 if ($config === NULL)
00846 {
00847 if (file_exists(APPPATH.'config/'.$class.EXT))
00848 {
00849 include_once(APPPATH.'config/'.$class.EXT);
00850 }
00851 }
00852
00853 if ($prefix == '')
00854 {
00855 $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
00856 }
00857 else
00858 {
00859 $name = $prefix.$class;
00860 }
00861
00862
00863 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
00864
00865
00866 $CI =& get_instance();
00867 if ($config !== NULL)
00868 {
00869 $CI->$classvar = new $name($config);
00870 }
00871 else
00872 {
00873 $CI->$classvar = new $name;
00874 }
00875 }
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889 function _ci_autoloader()
00890 {
00891 include_once(APPPATH.'config/autoload'.EXT);
00892
00893 if ( ! isset($autoload))
00894 {
00895 return FALSE;
00896 }
00897
00898
00899 if (count($autoload['config']) > 0)
00900 {
00901 $CI =& get_instance();
00902 foreach ($autoload['config'] as $key => $val)
00903 {
00904 $CI->config->load($val);
00905 }
00906 }
00907
00908
00909 foreach (array('helper', 'plugin', 'language') as $type)
00910 {
00911 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
00912 {
00913 $this->$type($autoload[$type]);
00914 }
00915 }
00916
00917
00918
00919
00920
00921 if ( ! isset($autoload['libraries']))
00922 {
00923 $autoload['libraries'] = $autoload['core'];
00924 }
00925
00926
00927 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
00928 {
00929
00930 if (in_array('database', $autoload['libraries']))
00931 {
00932 $this->database();
00933 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
00934 }
00935
00936
00937 if (in_array('scaffolding', $autoload['libraries']))
00938 {
00939 $this->scaffolding();
00940 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
00941 }
00942
00943
00944 foreach ($autoload['libraries'] as $item)
00945 {
00946 $this->library($item);
00947 }
00948 }
00949
00950
00951 if (isset($autoload['model']))
00952 {
00953 $this->model($autoload['model']);
00954 }
00955
00956 }
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970 function _ci_assign_to_models()
00971 {
00972 if (count($this->_ci_models) == 0)
00973 {
00974 return;
00975 }
00976
00977 if ($this->_ci_is_instance())
00978 {
00979 $CI =& get_instance();
00980 foreach ($this->_ci_models as $model)
00981 {
00982 $CI->$model->_assign_libraries();
00983 }
00984 }
00985 else
00986 {
00987 foreach ($this->_ci_models as $model)
00988 {
00989 $this->$model->_assign_libraries();
00990 }
00991 }
00992 }
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005 function _ci_object_to_array($object)
01006 {
01007 return (is_object($object)) ? get_object_vars($object) : $object;
01008 }
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018 function _ci_is_instance()
01019 {
01020 if ($this->_ci_is_php5 == TRUE)
01021 {
01022 return TRUE;
01023 }
01024
01025 global $CI;
01026 return (is_object($CI)) ? TRUE : FALSE;
01027 }
01028
01029 }
01030
01031
01032