error.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: libs_2error_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * Short description for file.
00005  *
00006  * Long description for file
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.libs
00023  * @since           CakePHP(tm) v 0.10.5.1732
00024  * @version         $Revision: 580 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 App::import('Controller', 'App');
00030 /**
00031  * Short description for file.
00032  *
00033  * Long description for file
00034  *
00035  * @package     cake
00036  * @subpackage  cake.cake.libs
00037  */
00038 class CakeErrorController extends AppController {
00039 
00040     var $name = 'CakeError';
00041 
00042     var $uses = array();
00043 
00044     function __construct() {
00045         parent::__construct();
00046         $this->_set(Router::getPaths());
00047         $this->params = Router::getParams();
00048         $this->constructClasses();
00049         $this->Component->initialize($this);
00050         $this->_set(array('cacheAction' => false, 'viewPath' => 'errors'));
00051     }
00052 
00053 }
00054 /**
00055  * Short description for file.
00056  *
00057  * Long description for file
00058  *
00059  * @package     cake
00060  * @subpackage  cake.cake.libs
00061  */
00062 class ErrorHandler extends Object {
00063 /**
00064  * Controller instance.
00065  *
00066  * @var object
00067  * @access public
00068  */
00069     var $controller = null;
00070 
00071 /**
00072  * Class constructor.
00073  *
00074  * @param string $method Method producing the error
00075  * @param array $messages Error messages
00076  */
00077     function __construct($method, $messages) {
00078         App::import('Core', 'Sanitize');
00079 
00080         $this->controller =& new CakeErrorController();
00081 
00082         $allow = array('.', '/', '_', ' ', '-', '~');
00083         if (substr(PHP_OS, 0, 3) == "WIN") {
00084             $allow = array_merge($allow, array('\\', ':'));
00085         }
00086 
00087         $messages = Sanitize::paranoid($messages, $allow);
00088 
00089         if (!isset($messages[0])) {
00090             $messages = array($messages);
00091         }
00092 
00093         if (method_exists($this->controller, 'apperror')) {
00094             return $this->controller->appError($method, $messages);
00095         }
00096 
00097         if (!in_array(strtolower($method), array_map('strtolower', get_class_methods($this)))) {
00098             $method = 'error';
00099         }
00100 
00101         if ($method !== 'error') {
00102             if (Configure::read() == 0){
00103                 $method = 'error404';
00104                 if(isset($code) && $code == 500) {
00105                     $method = 'error500';
00106                 }
00107             }
00108         }
00109         $this->dispatchMethod($method, $messages);
00110         $this->_stop();
00111     }
00112 /**
00113  * Displays an error page (e.g. 404 Not found).
00114  *
00115  * @param array $params Parameters for controller
00116  * @access public
00117  */
00118     function error($params) {
00119         extract($params, EXTR_OVERWRITE);
00120         $this->controller->set(array(
00121             'code' => $code,
00122             'name' => $name,
00123             'message' => $message,
00124             'title' => $code . ' ' . $name
00125         ));
00126         $this->__outputMessage('error404');
00127     }
00128 /**
00129  * Convenience method to display a 404 page.
00130  *
00131  * @param array $params Parameters for controller
00132  * @access public
00133  */
00134     function error404($params) {
00135         extract($params, EXTR_OVERWRITE);
00136 
00137         if (!isset($url)) {
00138             $url = $this->controller->here;
00139         }
00140         $url = Router::normalize($url);
00141         header("HTTP/1.0 404 Not Found");
00142         $this->controller->set(array(
00143             'code' => '404',
00144             'name' => __('Not Found', true),
00145             'message' => h($url),
00146             'base' => $this->controller->base
00147         ));
00148         $this->__outputMessage('error404');
00149     }
00150 /**
00151  * Renders the Missing Controller web page.
00152  *
00153  * @param array $params Parameters for controller
00154  * @access public
00155  */
00156     function missingController($params) {
00157         extract($params, EXTR_OVERWRITE);
00158 
00159         $controllerName = str_replace('Controller', '', $className);
00160         $this->controller->set(array(
00161             'controller' => $className,
00162             'controllerName' => $controllerName,
00163             'title' => __('Missing Controller', true)
00164         ));
00165         $this->__outputMessage('missingController');
00166     }
00167 /**
00168  * Renders the Missing Action web page.
00169  *
00170  * @param array $params Parameters for controller
00171  * @access public
00172  */
00173     function missingAction($params) {
00174         extract($params, EXTR_OVERWRITE);
00175 
00176         $controllerName = str_replace('Controller', '', $className);
00177         $this->controller->set(array(
00178             'controller' => $className,
00179             'controllerName' => $controllerName,
00180             'action' => $action,
00181             'title' => __('Missing Method in Controller', true)
00182         ));
00183         $this->__outputMessage('missingAction');
00184     }
00185 /**
00186  * Renders the Private Action web page.
00187  *
00188  * @param array $params Parameters for controller
00189  * @access public
00190  */
00191     function privateAction($params) {
00192         extract($params, EXTR_OVERWRITE);
00193 
00194         $this->controller->set(array(
00195             'controller' => $className,
00196             'action' => $action,
00197             'title' => __('Trying to access private method in class', true)
00198         ));
00199         $this->__outputMessage('privateAction');
00200     }
00201 /**
00202  * Renders the Missing Table web page.
00203  *
00204  * @param array $params Parameters for controller
00205  * @access public
00206  */
00207     function missingTable($params) {
00208         extract($params, EXTR_OVERWRITE);
00209 
00210         $this->controller->set(array(
00211             'model' => $className,
00212             'table' => $table,
00213             'title' => __('Missing Database Table', true)
00214         ));
00215         $this->__outputMessage('missingTable');
00216     }
00217 /**
00218  * Renders the Missing Database web page.
00219  *
00220  * @param array $params Parameters for controller
00221  * @access public
00222  */
00223     function missingDatabase($params = array()) {
00224         extract($params, EXTR_OVERWRITE);
00225 
00226         $this->controller->set(array(
00227             'title' => __('Scaffold Missing Database Connection', true)
00228         ));
00229         $this->__outputMessage('missingScaffolddb');
00230     }
00231 /**
00232  * Renders the Missing View web page.
00233  *
00234  * @param array $params Parameters for controller
00235  * @access public
00236  */
00237     function missingView($params) {
00238         extract($params, EXTR_OVERWRITE);
00239 
00240         $this->controller->set(array(
00241             'controller' => $className,
00242             'action' => $action,
00243             'file' => $file,
00244             'title' => __('Missing View', true)
00245         ));
00246         $this->__outputMessage('missingView');
00247     }
00248 /**
00249  * Renders the Missing Layout web page.
00250  *
00251  * @param array $params Parameters for controller
00252  * @access public
00253  */
00254     function missingLayout($params) {
00255         extract($params, EXTR_OVERWRITE);
00256 
00257         $this->controller->layout = 'default';
00258         $this->controller->set(array(
00259             'file' => $file,
00260             'title' => __('Missing Layout', true)
00261         ));
00262         $this->__outputMessage('missingLayout');
00263     }
00264 /**
00265  * Renders the Database Connection web page.
00266  *
00267  * @param array $params Parameters for controller
00268  * @access public
00269  */
00270     function missingConnection($params) {
00271         extract($params, EXTR_OVERWRITE);
00272 
00273         $this->controller->set(array(
00274             'model' => $className,
00275             'title' => __('Missing Database Connection', true)
00276         ));
00277         $this->__outputMessage('missingConnection');
00278     }
00279 /**
00280  * Renders the Missing Helper file web page.
00281  *
00282  * @param array $params Parameters for controller
00283  * @access public
00284  */
00285     function missingHelperFile($params) {
00286         extract($params, EXTR_OVERWRITE);
00287 
00288         $this->controller->set(array(
00289             'helperClass' => Inflector::camelize($helper) . "Helper",
00290             'file' => $file,
00291             'title' => __('Missing Helper File', true)
00292         ));
00293         $this->__outputMessage('missingHelperFile');
00294     }
00295 /**
00296  * Renders the Missing Helper class web page.
00297  *
00298  * @param array $params Parameters for controller
00299  * @access public
00300  */
00301     function missingHelperClass($params) {
00302         extract($params, EXTR_OVERWRITE);
00303 
00304         $this->controller->set(array(
00305             'helperClass' => Inflector::camelize($helper) . "Helper",
00306             'file' => $file,
00307             'title' => __('Missing Helper Class', true)
00308         ));
00309         $this->__outputMessage('missingHelperClass');
00310     }
00311 /**
00312  * Renders the Missing Component file web page.
00313  *
00314  * @param array $params Parameters for controller
00315  * @access public
00316  */
00317     function missingComponentFile($params) {
00318         extract($params, EXTR_OVERWRITE);
00319 
00320         $this->controller->set(array(
00321             'controller' => $className,
00322             'component' => $component,
00323             'file' => $file,
00324             'title' => __('Missing Component File', true)
00325         ));
00326         $this->__outputMessage('missingComponentFile');
00327     }
00328 /**
00329  * Renders the Missing Component class web page.
00330  *
00331  * @param array $params Parameters for controller
00332  * @access public
00333  */
00334     function missingComponentClass($params) {
00335         extract($params, EXTR_OVERWRITE);
00336 
00337         $this->controller->set(array(
00338             'controller' => $className,
00339             'component' => $component,
00340             'file' => $file,
00341             'title' => __('Missing Component Class', true)
00342         ));
00343         $this->__outputMessage('missingComponentClass');
00344     }
00345 /**
00346  * Renders the Missing Model class web page.
00347  *
00348  * @param unknown_type $params Parameters for controller
00349  * @access public
00350  */
00351     function missingModel($params) {
00352         extract($params, EXTR_OVERWRITE);
00353 
00354         $this->controller->set(array(
00355             'model' => $className,
00356             'title' => __('Missing Model', true)
00357         ));
00358         $this->__outputMessage('missingModel');
00359     }
00360 /**
00361  * Output message
00362  *
00363  * @access private
00364  */
00365     function __outputMessage($template) {
00366         $this->controller->render($template);
00367         $this->controller->afterFilter();
00368         echo $this->controller->output;
00369     }
00370 }
00371 
00372 ?>