2013-06-14 13:51:44 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-06-14 13:51:44 +02:00
|
|
|
|
2015-08-27 13:25:23 +02:00
|
|
|
namespace Icinga\Controllers;
|
|
|
|
|
2015-08-27 13:25:46 +02:00
|
|
|
use Zend_Controller_Plugin_ErrorHandler;
|
2015-01-30 09:31:05 +01:00
|
|
|
use Icinga\Application\Icinga;
|
2014-10-31 10:27:17 +01:00
|
|
|
use Icinga\Application\Logger;
|
2015-08-24 15:59:25 +02:00
|
|
|
use Icinga\Exception\Http\HttpBadRequestException;
|
2015-05-22 09:15:52 +02:00
|
|
|
use Icinga\Exception\Http\HttpMethodNotAllowedException;
|
2015-05-22 09:25:49 +02:00
|
|
|
use Icinga\Exception\Http\HttpNotFoundException;
|
2015-04-07 12:27:19 +02:00
|
|
|
use Icinga\Exception\MissingParameterException;
|
2015-01-30 09:31:05 +01:00
|
|
|
use Icinga\Security\SecurityException;
|
2014-03-25 13:10:02 +01:00
|
|
|
use Icinga\Web\Controller\ActionController;
|
2013-06-14 13:51:44 +02:00
|
|
|
|
|
|
|
/**
|
2013-08-16 14:56:23 +02:00
|
|
|
* Application wide controller for displaying exceptions
|
2013-06-14 13:51:44 +02:00
|
|
|
*/
|
|
|
|
class ErrorController extends ActionController
|
|
|
|
{
|
2015-08-20 15:49:37 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-02-14 12:12:46 +01:00
|
|
|
protected $requiresAuthentication = false;
|
|
|
|
|
2013-06-14 13:51:44 +02:00
|
|
|
/**
|
2013-08-16 14:56:23 +02:00
|
|
|
* Display exception
|
2013-06-14 13:51:44 +02:00
|
|
|
*/
|
|
|
|
public function errorAction()
|
|
|
|
{
|
2014-01-23 16:03:47 +01:00
|
|
|
$error = $this->_getParam('error_handler');
|
|
|
|
$exception = $error->exception;
|
2015-08-20 15:49:37 +02:00
|
|
|
/** @var \Exception $exception */
|
2014-08-29 10:57:05 +02:00
|
|
|
Logger::error($exception);
|
|
|
|
Logger::error('Stacktrace: %s', $exception->getTraceAsString());
|
|
|
|
|
2014-01-22 14:57:54 +01:00
|
|
|
switch ($error->type) {
|
2013-06-14 13:51:44 +02:00
|
|
|
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
|
|
|
|
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
|
|
|
|
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
|
2014-03-25 13:10:02 +01:00
|
|
|
$modules = Icinga::app()->getModuleManager();
|
|
|
|
$path = ltrim($this->_request->get('PATH_INFO'), '/');
|
|
|
|
$path = preg_split('~/~', $path);
|
|
|
|
$path = array_shift($path);
|
2013-06-14 13:51:44 +02:00
|
|
|
$this->getResponse()->setHttpResponseCode(404);
|
2015-05-20 10:31:58 +02:00
|
|
|
$this->view->message = $this->translate('Page not found.');
|
2015-05-27 11:05:06 +02:00
|
|
|
if ($this->Auth()->isAuthenticated() && $modules->hasInstalled($path) && ! $modules->hasEnabled($path)) {
|
2014-03-25 13:10:02 +01:00
|
|
|
$this->view->message .= ' ' . sprintf(
|
|
|
|
$this->translate('Enabling the "%s" module might help!'),
|
|
|
|
$path
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-14 13:51:44 +02:00
|
|
|
break;
|
|
|
|
default:
|
2015-04-07 12:27:19 +02:00
|
|
|
switch (true) {
|
2015-05-22 09:15:52 +02:00
|
|
|
case $exception instanceof HttpMethodNotAllowedException:
|
|
|
|
$this->getResponse()->setHttpResponseCode(405);
|
|
|
|
$this->getResponse()->setHeader('Allow', $exception->getAllowedMethods());
|
|
|
|
break;
|
2015-05-21 16:56:27 +02:00
|
|
|
case $exception instanceof HttpNotFoundException:
|
|
|
|
$this->getResponse()->setHttpResponseCode(404);
|
2015-04-07 12:27:19 +02:00
|
|
|
break;
|
|
|
|
case $exception instanceof MissingParameterException:
|
|
|
|
$this->getResponse()->setHttpResponseCode(400);
|
|
|
|
$this->getResponse()->setHeader(
|
|
|
|
'X-Status-Reason',
|
|
|
|
'Missing parameter ' . $exception->getParameter()
|
|
|
|
);
|
|
|
|
break;
|
2015-08-24 15:59:25 +02:00
|
|
|
case $exception instanceof HttpBadRequestException:
|
|
|
|
$this->getResponse()->setHttpResponseCode(400);
|
|
|
|
break;
|
2015-05-21 16:56:27 +02:00
|
|
|
case $exception instanceof SecurityException:
|
|
|
|
$this->getResponse()->setHttpResponseCode(403);
|
|
|
|
break;
|
2015-04-07 12:27:19 +02:00
|
|
|
default:
|
|
|
|
$this->getResponse()->setHttpResponseCode(500);
|
|
|
|
break;
|
|
|
|
}
|
2014-01-23 16:03:47 +01:00
|
|
|
$this->view->message = $exception->getMessage();
|
2015-04-07 12:27:19 +02:00
|
|
|
if ($this->getInvokeArg('displayExceptions')) {
|
2014-03-25 13:10:02 +01:00
|
|
|
$this->view->stackTrace = $exception->getTraceAsString();
|
|
|
|
}
|
2013-06-14 13:51:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-08-20 15:49:37 +02:00
|
|
|
|
2015-08-20 16:02:25 +02:00
|
|
|
if ($this->getRequest()->isApiRequest()) {
|
2015-08-20 15:49:37 +02:00
|
|
|
// @TODO(el): Use ApiResponse class for unified response handling.
|
2015-08-20 16:05:45 +02:00
|
|
|
$this->getResponse()->sendJson(array(
|
2015-08-20 15:49:37 +02:00
|
|
|
'status' => 'error',
|
|
|
|
'message' => $this->view->message
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2014-01-22 14:57:54 +01:00
|
|
|
$this->view->request = $error->request;
|
2013-06-14 13:51:44 +02:00
|
|
|
}
|
|
|
|
}
|