error.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: error_8php-source.html 675 2008-12-26 00:27:14Z 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: 675 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 uses('sanitize');
00030 /**
00031  * Short description for file.
00032  *
00033  * Long description for file
00034  *
00035  * @package     cake
00036  * @subpackage  cake.cake.libs
00037  */
00038 class ErrorHandler extends Object {
00039     var $controller = null;
00040 
00041 /**
00042  * Class constructor.
00043  *
00044  * @param string $method
00045  * @param array $messages
00046  * @return unknown
00047  */
00048     function __construct($method, $messages) {
00049         parent::__construct();
00050         static $__previousError = null;
00051         $allow = array('.', '/', '_', ' ', '-', '~');
00052         if (substr(PHP_OS,0,3) == "WIN") {
00053             $allow = array_merge($allow, array('\\', ':') );
00054         }
00055         $clean = new Sanitize();
00056         $messages = $clean->paranoid($messages, $allow);
00057         if (!class_exists('Dispatcher')) {
00058             require CAKE . 'dispatcher.php';
00059         }
00060         $this->__dispatch =& new Dispatcher();
00061 
00062         if ($__previousError != array($method, $messages)) {
00063             $__previousError = array($method, $messages);
00064 
00065             if (!class_exists('AppController')) {
00066                 loadController(null);
00067             }
00068 
00069             $this->controller =& new AppController();
00070             if (!empty($this->controller->uses)) {
00071                 $this->controller->constructClasses();
00072             }
00073             $this->controller->_initComponents();
00074             $this->controller->cacheAction = false;
00075             $this->__dispatch->start($this->controller);
00076 
00077             if (method_exists($this->controller, 'apperror')) {
00078                 return $this->controller->appError($method, $messages);
00079             }
00080         } else {
00081             $this->controller =& new Controller();
00082             $this->controller->cacheAction = false;
00083         }
00084         if (Configure::read() > 0 || $method == 'error') {
00085             call_user_func_array(array(&$this, $method), $messages);
00086         } else {
00087             call_user_func_array(array(&$this, 'error404'), $messages);
00088         }
00089     }
00090 /**
00091  * Displays an error page (e.g. 404 Not found).
00092  *
00093  * @param array $params
00094  */
00095     function error($params) {
00096         extract($params);
00097         $this->controller->base = $base;
00098         $this->controller->webroot = $this->_webroot();
00099         $this->controller->viewPath='errors';
00100         $this->controller->set(array('code' => $code,
00101                                         'name' => $name,
00102                                         'message' => $message,
00103                                         'title' => $code . ' ' . $name));
00104         $this->controller->render('error404');
00105         exit();
00106     }
00107 /**
00108  * Convenience method to display a 404 page.
00109  *
00110  * @param array $params
00111  */
00112     function error404($params) {
00113         extract($params);
00114 
00115         if (!isset($url)) {
00116             $url = $action;
00117         }
00118         if (!isset($message)) {
00119             $message = '';
00120         }
00121         if (!isset($base)) {
00122             $base = '';
00123         }
00124 
00125         header("HTTP/1.0 404 Not Found");
00126         $this->error(array('code' => '404',
00127                             'name' => 'Not found',
00128                             'message' => sprintf("The requested address %s was not found on this server.", $url, $message),
00129                             'base' => $base));
00130         exit();
00131     }
00132 /**
00133  * Renders the Missing Controller web page.
00134  *
00135  * @param array $params
00136  */
00137     function missingController($params) {
00138         extract($params);
00139         $this->controller->base = $base;
00140         $this->controller->webroot = $webroot;
00141         $this->controller->viewPath ='errors';
00142         $controllerName = str_replace('Controller', '', $className);
00143         $this->controller->set(array('controller' => $className,
00144                                         'controllerName' => $controllerName,
00145                                         'title' => 'Missing Controller'));
00146         $this->controller->render('missingController');
00147         exit();
00148     }
00149 /**
00150  * Renders the Missing Action web page.
00151  *
00152  * @param array $params
00153  */
00154     function missingAction($params) {
00155         extract($params);
00156         $this->controller->base = $base;
00157         $this->controller->webroot = $webroot;
00158         $this->controller->viewPath = 'errors';
00159         $this->controller->set(array('controller' => $className,
00160                                         'action' => $action,
00161                                         'title' => 'Missing Method in Controller'));
00162         $this->controller->render('missingAction');
00163         exit();
00164     }
00165 /**
00166  * Renders the Private Action web page.
00167  *
00168  * @param array $params
00169  */
00170     function privateAction($params) {
00171         extract($params);
00172         $this->controller->base = $base;
00173         $this->controller->webroot = $webroot;
00174         $this->controller->viewPath = 'errors';
00175         $this->controller->set(array('controller' => $className,
00176                                         'action' => $action,
00177                                         'title' => 'Trying to access private method in class'));
00178         $this->controller->render('privateAction');
00179         exit();
00180     }
00181 /**
00182  * Renders the Missing Table web page.
00183  *
00184  * @param array $params
00185  */
00186     function missingTable($params) {
00187         extract($params);
00188         $this->controller->viewPath = 'errors';
00189         $this->controller->webroot = $this->_webroot();
00190         $this->controller->set(array('model' => $className,
00191                                         'table' => $table,
00192                                         'title' => 'Missing Database Table'));
00193         $this->controller->render('missingTable');
00194         exit();
00195     }
00196 /**
00197  * Renders the Missing Database web page.
00198  *
00199  * @param array $params
00200  */
00201     function missingDatabase($params = array()) {
00202         extract($params);
00203         $this->controller->viewPath = 'errors';
00204         $this->controller->webroot = $this->_webroot();
00205         $this->controller->set(array('title' => 'Scaffold Missing Database Connection'));
00206         $this->controller->render('missingScaffolddb');
00207         exit();
00208     }
00209 /**
00210  * Renders the Missing View web page.
00211  *
00212  * @param array $params
00213  */
00214     function missingView($params) {
00215         extract($params);
00216         $this->controller->base = $base;
00217         $this->controller->viewPath = 'errors';
00218         $this->controller->webroot = $this->_webroot();
00219         $this->controller->set(array('controller' => $className,
00220                                         'action' => $action,
00221                                         'file' => $file,
00222                                         'title' => 'Missing View'));
00223         $this->controller->render('missingView');
00224         exit();
00225     }
00226 /**
00227  * Renders the Missing Layout web page.
00228  *
00229  * @param array $params
00230  */
00231     function missingLayout($params) {
00232         extract($params);
00233         $this->controller->base = $base;
00234         $this->controller->viewPath = 'errors';
00235         $this->controller->webroot = $this->_webroot();
00236         $this->controller->layout = 'default';
00237         $this->controller->set(array('file'  => $file,
00238                                         'title' => 'Missing Layout'));
00239         $this->controller->render('missingLayout');
00240         exit();
00241     }
00242 /**
00243  * Renders the Database Connection web page.
00244  *
00245  * @param array $params
00246  */
00247     function missingConnection($params) {
00248         extract($params);
00249         $this->controller->viewPath = 'errors';
00250         $this->controller->webroot = $this->_webroot();
00251         $this->controller->set(array('model' => $className,
00252                                         'title' => 'Missing Database Connection'));
00253         $this->controller->render('missingConnection');
00254         exit();
00255     }
00256 /**
00257  * Renders the Missing Helper file web page.
00258  *
00259  * @param array $params
00260  */
00261     function missingHelperFile($params) {
00262         extract($params);
00263         $this->controller->base = $base;
00264         $this->controller->viewPath = 'errors';
00265         $this->controller->webroot = $this->_webroot();
00266         $this->controller->set(array('helperClass' => Inflector::camelize($helper) . "Helper",
00267                                         'file' => $file,
00268                                         'title' => 'Missing Helper File'));
00269         $this->controller->render('missingHelperFile');
00270         exit();
00271     }
00272 /**
00273  * Renders the Missing Helper class web page.
00274  *
00275  * @param array $params
00276  */
00277     function missingHelperClass($params) {
00278         extract($params);
00279         $this->controller->base = $base;
00280         $this->controller->viewPath = 'errors';
00281         $this->controller->webroot = $this->_webroot();
00282         $this->controller->set(array('helperClass' => Inflector::camelize($helper) . "Helper",
00283                                         'file' => $file,
00284                                         'title' => 'Missing Helper Class'));
00285         $this->controller->render('missingHelperClass');
00286         exit();
00287     }
00288 /**
00289  * Renders the Missing Component file web page.
00290  *
00291  * @param array $params
00292  */
00293     function missingComponentFile($params) {
00294         extract($params);
00295         $this->controller->base = $base;
00296         $this->controller->viewPath = 'errors';
00297         $this->controller->webroot = $this->_webroot();
00298         $this->controller->set(array('controller' => $className,
00299                                         'component' => $component,
00300                                         'file' => $file,
00301                                         'title' => 'Missing Component File'));
00302         $this->controller->render('missingComponentFile');
00303         exit();
00304     }
00305 /**
00306  * Renders the Missing Component class web page.
00307  *
00308  * @param array $params
00309  */
00310     function missingComponentClass($params) {
00311         extract($params);
00312         $this->controller->base = $base;
00313         $this->controller->viewPath = 'errors';
00314         $this->controller->webroot = $this->_webroot();
00315         $this->controller->set(array('controller' => $className,
00316                                         'component' => $component,
00317                                         'file' => $file,
00318                                         'title' => 'Missing Component Class'));
00319         $this->controller->render('missingComponentClass');
00320         exit();
00321     }
00322 /**
00323  * Renders the Missing Model class web page.
00324  *
00325  * @param unknown_type $params
00326  */
00327     function missingModel($params) {
00328         extract($params);
00329         $this->controller->base = $base;
00330         $this->controller->viewPath = 'errors';
00331         $this->controller->webroot = $this->_webroot();
00332         $this->controller->set(array('model' => $className,
00333                                         'title' => 'Missing Model'));
00334         $this->controller->render('missingModel');
00335         exit();
00336     }
00337 /**
00338  * Path to the web root.
00339  *
00340  * @return string full web root path
00341  */
00342     function _webroot() {
00343         $this->__dispatch->baseUrl();
00344         return $this->__dispatch->webroot;
00345     }
00346 }
00347 ?>