Common.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Common Functions
00020  *
00021  * Loads the base classes and executes the request.
00022  *
00023  * @package             CodeIgniter
00024  * @subpackage  codeigniter
00025  * @category    Common Functions
00026  * @author              ExpressionEngine Dev Team
00027  * @link                http://codeigniter.com/user_guide/
00028  */
00029 
00030 // ------------------------------------------------------------------------
00031 
00032 /**
00033  * Tests for file writability
00034  *
00035  * is_writable() returns TRUE on Windows servers
00036  * when you really can't write to the file
00037  * as the OS reports to PHP as FALSE only if the
00038  * read-only attribute is marked.  Ugh?
00039  *
00040  * @access      private
00041  * @return      void
00042  */
00043 function is_really_writable($file)
00044 {
00045         if (is_dir($file))
00046         {
00047                 $file = rtrim($file, '/').'/'.md5(rand(1,100));
00048 
00049                 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
00050                 {
00051                         return FALSE;
00052                 }
00053 
00054                 fclose($fp);
00055                 @chmod($file, DIR_WRITE_MODE);
00056                 @unlink($file);
00057                 return TRUE;
00058         }
00059         elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
00060         {
00061                 return FALSE;
00062         }
00063 
00064         fclose($fp);
00065         return TRUE;
00066 }
00067 
00068 // ------------------------------------------------------------------------
00069 
00070 /**
00071 * Class registry
00072 *
00073 * This function acts as a singleton.  If the requested class does not
00074 * exist it is instantiated and set to a static variable.  If it has
00075 * previously been instantiated the variable is returned.
00076 *
00077 * @access       public
00078 * @param        string  the class name being requested
00079 * @param        bool    optional flag that lets classes get loaded but not instantiated
00080 * @return       object
00081 */
00082 function &load_class($class, $instantiate = TRUE)
00083 {
00084         static $objects = array();
00085 
00086         // Does the class exist?  If so, we're done...
00087         if (isset($objects[$class]))
00088         {
00089                 return $objects[$class];
00090         }
00091 
00092         // If the requested class does not exist in the application/libraries
00093         // folder we'll load the native class from the system/libraries folder. 
00094         if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
00095         {
00096                 require(BASEPATH.'libraries/'.$class.EXT);      
00097                 require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
00098                 $is_subclass = TRUE;    
00099         }
00100         else
00101         {
00102                 if (file_exists(APPPATH.'libraries/'.$class.EXT))
00103                 {
00104                         require(APPPATH.'libraries/'.$class.EXT);       
00105                         $is_subclass = FALSE;   
00106                 }
00107                 else
00108                 {
00109                         require(BASEPATH.'libraries/'.$class.EXT);
00110                         $is_subclass = FALSE;
00111                 }
00112         }
00113 
00114         if ($instantiate == FALSE)
00115         {
00116                 $objects[$class] = TRUE;
00117                 return $objects[$class];
00118         }
00119 
00120         if ($is_subclass == TRUE)
00121         {
00122                 $name = config_item('subclass_prefix').$class;
00123                 $objects[$class] =& new $name();
00124                 return $objects[$class];
00125         }
00126 
00127         $name = ($class != 'Controller') ? 'CI_'.$class : $class;
00128         
00129         $objects[$class] =& new $name();
00130         return $objects[$class];
00131 }
00132 
00133 /**
00134 * Loads the main config.php file
00135 *
00136 * @access       private
00137 * @return       array
00138 */
00139 function &get_config()
00140 {
00141         static $main_conf;
00142 
00143         if ( ! isset($main_conf))
00144         {
00145                 if ( ! file_exists(APPPATH.'config/config'.EXT))
00146                 {
00147                         exit('The configuration file config'.EXT.' does not exist.');
00148                 }
00149 
00150                 require(APPPATH.'config/config'.EXT);
00151 
00152                 if ( ! isset($config) OR ! is_array($config))
00153                 {
00154                         exit('Your config file does not appear to be formatted correctly.');
00155                 }
00156 
00157                 $main_conf[0] =& $config;
00158         }
00159         return $main_conf[0];
00160 }
00161 
00162 /**
00163 * Gets a config item
00164 *
00165 * @access       public
00166 * @return       mixed
00167 */
00168 function config_item($item)
00169 {
00170         static $config_item = array();
00171 
00172         if ( ! isset($config_item[$item]))
00173         {
00174                 $config =& get_config();
00175 
00176                 if ( ! isset($config[$item]))
00177                 {
00178                         return FALSE;
00179                 }
00180                 $config_item[$item] = $config[$item];
00181         }
00182 
00183         return $config_item[$item];
00184 }
00185 
00186 
00187 /**
00188 * Error Handler
00189 *
00190 * This function lets us invoke the exception class and
00191 * display errors using the standard error template located
00192 * in application/errors/errors.php
00193 * This function will send the error page directly to the
00194 * browser and exit.
00195 *
00196 * @access       public
00197 * @return       void
00198 */
00199 function show_error($message)
00200 {
00201         $error =& load_class('Exceptions');
00202         echo $error->show_error('An Error Was Encountered', $message);
00203         exit;
00204 }
00205 
00206 
00207 /**
00208 * 404 Page Handler
00209 *
00210 * This function is similar to the show_error() function above
00211 * However, instead of the standard error template it displays
00212 * 404 errors.
00213 *
00214 * @access       public
00215 * @return       void
00216 */
00217 function show_404($page = '')
00218 {
00219         $error =& load_class('Exceptions');
00220         $error->show_404($page);
00221         exit;
00222 }
00223 
00224 
00225 /**
00226 * Error Logging Interface
00227 *
00228 * We use this as a simple mechanism to access the logging
00229 * class and send messages to be logged.
00230 *
00231 * @access       public
00232 * @return       void
00233 */
00234 function log_message($level = 'error', $message, $php_error = FALSE)
00235 {
00236         static $LOG;
00237         
00238         $config =& get_config();
00239         if ($config['log_threshold'] == 0)
00240         {
00241                 return;
00242         }
00243 
00244         $LOG =& load_class('Log');      
00245         $LOG->write_log($level, $message, $php_error);
00246 }
00247 
00248 /**
00249 * Exception Handler
00250 *
00251 * This is the custom exception handler that is declaired at the top
00252 * of Codeigniter.php.  The main reason we use this is permit
00253 * PHP errors to be logged in our own log files since we may
00254 * not have access to server logs. Since this function
00255 * effectively intercepts PHP errors, however, we also need
00256 * to display errors based on the current error_reporting level.
00257 * We do that with the use of a PHP error template.
00258 *
00259 * @access       private
00260 * @return       void
00261 */
00262 function _exception_handler($severity, $message, $filepath, $line)
00263 {       
00264          // We don't bother with "strict" notices since they will fill up
00265          // the log file with information that isn't normally very
00266          // helpful.  For example, if you are running PHP 5 and you
00267          // use version 4 style class functions (without prefixes
00268          // like "public", "private", etc.) you'll get notices telling
00269          // you that these have been deprecated.
00270         
00271         if ($severity == E_STRICT)
00272         {
00273                 return;
00274         }
00275 
00276         $error =& load_class('Exceptions');
00277 
00278         // Should we display the error?
00279         // We'll get the current error_reporting level and add its bits
00280         // with the severity bits to find out.
00281         
00282         if (($severity & error_reporting()) == $severity)
00283         {
00284                 $error->show_php_error($severity, $message, $filepath, $line);
00285         }
00286         
00287         // Should we log the error?  No?  We're done...
00288         $config =& get_config();
00289         if ($config['log_threshold'] == 0)
00290         {
00291                 return;
00292         }
00293 
00294         $error->log_exception($severity, $message, $filepath, $line);
00295 }
00296 
00297 
00298 
00299 /* End of file Common.php */
00300 /* Location: ./system/codeigniter/Common.php */