1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21: 
 22: App::import('Controller', 'App');
 23: 
 24:  25:  26:  27:  28:  29:  30:  31: 
 32: class CakeErrorController extends AppController {
 33:     var $name = 'CakeError';
 34: 
 35:  36:  37:  38:  39: 
 40:     var $uses = array();
 41: 
 42:  43:  44:  45:  46:  47: 
 48:     function __construct() {
 49:         parent::__construct();
 50:         $this->_set(Router::getPaths());
 51:         $this->params = Router::getParams();
 52:         $this->constructClasses();
 53:         $this->Component->initialize($this);
 54:         $this->_set(array('cacheAction' => false, 'viewPath' => 'errors'));
 55:     }
 56: }
 57: 
 58:  59:  60:  61:  62:  63:  64:  65:  66:  67: 
 68: class ErrorHandler extends Object {
 69: 
 70:  71:  72:  73:  74:  75: 
 76:     var $controller = null;
 77: 
 78:  79:  80:  81:  82:  83: 
 84:     function __construct($method, $messages) {
 85:         App::import('Core', 'Sanitize');
 86:         static $__previousError = null;
 87: 
 88:         if ($__previousError != array($method, $messages)) {
 89:             $__previousError = array($method, $messages);
 90:             $this->controller =& new CakeErrorController();
 91:         } else {
 92:             $this->controller =& new Controller();
 93:             $this->controller->viewPath = 'errors';
 94:         }
 95:         $options = array('escape' => false);
 96:         $messages = Sanitize::clean($messages, $options);
 97: 
 98:         if (!isset($messages[0])) {
 99:             $messages = array($messages);
100:         }
101: 
102:         if (method_exists($this->controller, 'apperror')) {
103:             return $this->controller->appError($method, $messages);
104:         }
105: 
106:         if (!in_array(strtolower($method), array_map('strtolower', get_class_methods($this)))) {
107:             $method = 'error';
108:         }
109: 
110:         if ($method !== 'error') {
111:             if (Configure::read('debug') == 0) {
112:                 $parentClass = get_parent_class($this);
113:                 if (strtolower($parentClass) != 'errorhandler') {
114:                     $method = 'error404';
115:                 }
116:                 $parentMethods = array_map('strtolower', get_class_methods($parentClass));
117:                 if (in_array(strtolower($method), $parentMethods)) {
118:                     $method = 'error404';
119:                 }
120:                 if (isset($messages[0]['code']) && $messages[0]['code'] == 500) {
121:                     $method = 'error500';
122:                 }
123:             }
124:         }
125:         $this->dispatchMethod($method, $messages);
126:         $this->_stop();
127:     }
128: 
129: 130: 131: 132: 133: 134: 
135:     function error($params) {
136:         extract($params, EXTR_OVERWRITE);
137:         $this->controller->set(array(
138:             'code' => $code,
139:             'name' => $name,
140:             'message' => $message,
141:             'title' => $code . ' ' . $name
142:         ));
143:         $this->_outputMessage('error404');
144:     }
145: 
146: 147: 148: 149: 150: 151: 
152:     function error404($params) {
153:         extract($params, EXTR_OVERWRITE);
154: 
155:         if (!isset($url)) {
156:             $url = $this->controller->here;
157:         }
158:         $url = Router::normalize($url);
159:         $this->controller->header("HTTP/1.0 404 Not Found");
160:         $this->controller->set(array(
161:             'code' => '404',
162:             'name' => __('Not Found', true),
163:             'message' => h($url),
164:             'base' => $this->controller->base
165:         ));
166:         $this->_outputMessage('error404');
167:     }
168: 
169: 170: 171: 172: 173: 174: 
175:     function error500($params) {
176:         extract($params, EXTR_OVERWRITE);
177: 
178:         if (!isset($url)) {
179:             $url = $this->controller->here;
180:         }
181:         $url = Router::normalize($url);
182:         $this->controller->header("HTTP/1.0 500 Internal Server Error");
183:         $this->controller->set(array(
184:             'code' => '500',
185:             'name' => __('An Internal Error Has Occurred', true),
186:             'message' => h($url),
187:             'base' => $this->controller->base
188:         ));
189:         $this->_outputMessage('error500');
190:     }
191: 192: 193: 194: 195: 196: 
197:     function missingController($params) {
198:         extract($params, EXTR_OVERWRITE);
199: 
200:         $controllerName = str_replace('Controller', '', $className);
201:         $this->controller->set(array(
202:             'controller' => $className,
203:             'controllerName' => $controllerName,
204:             'title' => __('Missing Controller', true)
205:         ));
206:         $this->_outputMessage('missingController');
207:     }
208: 
209: 210: 211: 212: 213: 214: 
215:     function missingAction($params) {
216:         extract($params, EXTR_OVERWRITE);
217: 
218:         $controllerName = str_replace('Controller', '', $className);
219:         $this->controller->set(array(
220:             'controller' => $className,
221:             'controllerName' => $controllerName,
222:             'action' => $action,
223:             'title' => __('Missing Method in Controller', true)
224:         ));
225:         $this->_outputMessage('missingAction');
226:     }
227: 
228: 229: 230: 231: 232: 233: 
234:     function privateAction($params) {
235:         extract($params, EXTR_OVERWRITE);
236: 
237:         $this->controller->set(array(
238:             'controller' => $className,
239:             'action' => $action,
240:             'title' => __('Trying to access private method in class', true)
241:         ));
242:         $this->_outputMessage('privateAction');
243:     }
244: 
245: 246: 247: 248: 249: 250: 
251:     function missingTable($params) {
252:         extract($params, EXTR_OVERWRITE);
253: 
254:         $this->controller->header("HTTP/1.0 500 Internal Server Error");
255:         $this->controller->set(array(
256:             'code' => '500',
257:             'model' => $className,
258:             'table' => $table,
259:             'title' => __('Missing Database Table', true)
260:         ));
261:         $this->_outputMessage('missingTable');
262:     }
263: 
264: 265: 266: 267: 268: 269: 
270:     function missingDatabase($params = array()) {
271:         $this->controller->header("HTTP/1.0 500 Internal Server Error");
272:         $this->controller->set(array(
273:             'code' => '500',
274:             'title' => __('Scaffold Missing Database Connection', true)
275:         ));
276:         $this->_outputMessage('missingScaffolddb');
277:     }
278: 
279: 280: 281: 282: 283: 284: 
285:     function missingView($params) {
286:         extract($params, EXTR_OVERWRITE);
287: 
288:         $this->controller->set(array(
289:             'controller' => $className,
290:             'action' => $action,
291:             'file' => $file,
292:             'title' => __('Missing View', true)
293:         ));
294:         $this->_outputMessage('missingView');
295:     }
296: 
297: 298: 299: 300: 301: 302: 
303:     function missingLayout($params) {
304:         extract($params, EXTR_OVERWRITE);
305: 
306:         $this->controller->layout = 'default';
307:         $this->controller->set(array(
308:             'file' => $file,
309:             'title' => __('Missing Layout', true)
310:         ));
311:         $this->_outputMessage('missingLayout');
312:     }
313: 
314: 315: 316: 317: 318: 319: 
320:     function missingConnection($params) {
321:         extract($params, EXTR_OVERWRITE);
322: 
323:         $this->controller->header("HTTP/1.0 500 Internal Server Error");
324:         $this->controller->set(array(
325:             'code' => '500',
326:             'model' => $className,
327:             'title' => __('Missing Database Connection', true)
328:         ));
329:         $this->_outputMessage('missingConnection');
330:     }
331: 
332: 333: 334: 335: 336: 337: 
338:     function missingHelperFile($params) {
339:         extract($params, EXTR_OVERWRITE);
340: 
341:         $this->controller->set(array(
342:             'helperClass' => Inflector::camelize($helper) . "Helper",
343:             'file' => $file,
344:             'title' => __('Missing Helper File', true)
345:         ));
346:         $this->_outputMessage('missingHelperFile');
347:     }
348: 
349: 350: 351: 352: 353: 354: 
355:     function missingHelperClass($params) {
356:         extract($params, EXTR_OVERWRITE);
357: 
358:         $this->controller->set(array(
359:             'helperClass' => Inflector::camelize($helper) . "Helper",
360:             'file' => $file,
361:             'title' => __('Missing Helper Class', true)
362:         ));
363:         $this->_outputMessage('missingHelperClass');
364:     }
365: 
366: 367: 368: 369: 370: 371: 
372:     function missingBehaviorFile($params) {
373:         extract($params, EXTR_OVERWRITE);
374: 
375:         $this->controller->set(array(
376:             'behaviorClass' => Inflector::camelize($behavior) . "Behavior",
377:             'file' => $file,
378:             'title' => __('Missing Behavior File', true)
379:         ));
380:         $this->_outputMessage('missingBehaviorFile');
381:     }
382: 
383: 384: 385: 386: 387: 388: 
389:     function missingBehaviorClass($params) {
390:         extract($params, EXTR_OVERWRITE);
391: 
392:         $this->controller->set(array(
393:             'behaviorClass' => Inflector::camelize($behavior) . "Behavior",
394:             'file' => $file,
395:             'title' => __('Missing Behavior Class', true)
396:         ));
397:         $this->_outputMessage('missingBehaviorClass');
398:     }
399: 
400: 401: 402: 403: 404: 405: 
406:     function missingComponentFile($params) {
407:         extract($params, EXTR_OVERWRITE);
408: 
409:         $this->controller->set(array(
410:             'controller' => $className,
411:             'component' => $component,
412:             'file' => $file,
413:             'title' => __('Missing Component File', true)
414:         ));
415:         $this->_outputMessage('missingComponentFile');
416:     }
417: 
418: 419: 420: 421: 422: 423: 
424:     function missingComponentClass($params) {
425:         extract($params, EXTR_OVERWRITE);
426: 
427:         $this->controller->set(array(
428:             'controller' => $className,
429:             'component' => $component,
430:             'file' => $file,
431:             'title' => __('Missing Component Class', true)
432:         ));
433:         $this->_outputMessage('missingComponentClass');
434:     }
435: 
436: 437: 438: 439: 440: 441: 
442:     function missingModel($params) {
443:         extract($params, EXTR_OVERWRITE);
444: 
445:         $this->controller->set(array(
446:             'model' => $className,
447:             'title' => __('Missing Model', true)
448:         ));
449:         $this->_outputMessage('missingModel');
450:     }
451: 
452: 453: 454: 455: 456: 
457:     function _outputMessage($template) {
458:         $this->controller->render($template);
459:         $this->controller->afterFilter();
460:         echo $this->controller->output;
461:     }
462: }
463: