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