2013-06-14 13:51:44 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
|
2013-06-14 13:51:44 +02:00
|
|
|
|
2015-08-27 13:25:23 +02:00
|
|
|
namespace Icinga\Controllers;
|
|
|
|
|
2018-01-22 10:28:47 +01:00
|
|
|
use Icinga\Exception\IcingaException;
|
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;
|
2017-07-05 06:26:58 +02:00
|
|
|
use Icinga\Exception\Http\HttpExceptionInterface;
|
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;
|
2016-01-25 07:44:48 +01:00
|
|
|
use Icinga\Web\Url;
|
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
|
|
|
|
{
|
2020-11-13 17:43:40 +01:00
|
|
|
/**
|
|
|
|
* Regular expression to match exceptions resulting from missing functions/classes
|
|
|
|
*/
|
2021-06-07 09:40:40 +02:00
|
|
|
const MISSING_DEP_ERROR =
|
|
|
|
"/Uncaught Error:.*(?:undefined function (\S+)|Class ['\"]([^']+)['\"] not found).* in ([^:]+)/";
|
2020-11-13 17:43:40 +01:00
|
|
|
|
2015-08-20 15:49:37 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-02-14 12:12:46 +01:00
|
|
|
protected $requiresAuthentication = false;
|
|
|
|
|
2017-06-29 09:51:24 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->rerenderLayout = $this->params->has('renderLayout');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-09-22 18:21:25 +02:00
|
|
|
if (! ($isAuthenticated = $this->Auth()->isAuthenticated())) {
|
2015-12-10 13:02:25 +01:00
|
|
|
$this->innerLayout = 'guest-error';
|
2015-09-22 18:21:25 +02:00
|
|
|
}
|
|
|
|
|
2020-11-13 17:43:40 +01:00
|
|
|
$modules = Icinga::app()->getModuleManager();
|
|
|
|
$sourcePath = ltrim($this->_request->get('PATH_INFO'), '/');
|
|
|
|
$pathParts = preg_split('~/~', $sourcePath);
|
|
|
|
$moduleName = array_shift($pathParts);
|
|
|
|
|
|
|
|
$module = null;
|
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:
|
|
|
|
$this->getResponse()->setHttpResponseCode(404);
|
2018-01-26 15:11:28 +01:00
|
|
|
$this->view->messages = array($this->translate('Page not found.'));
|
2015-09-22 18:21:25 +02:00
|
|
|
if ($isAuthenticated) {
|
2020-11-13 17:43:40 +01:00
|
|
|
if ($modules->hasInstalled($moduleName) && ! $modules->hasEnabled($moduleName)) {
|
2018-01-26 15:11:28 +01:00
|
|
|
$this->view->messages[0] .= ' ' . sprintf(
|
2015-09-22 18:21:25 +02:00
|
|
|
$this->translate('Enabling the "%s" module might help!'),
|
2020-11-13 17:43:40 +01:00
|
|
|
$moduleName
|
2015-09-22 18:21:25 +02:00
|
|
|
);
|
|
|
|
}
|
2014-03-25 13:10:02 +01:00
|
|
|
}
|
|
|
|
|
2013-06-14 13:51:44 +02:00
|
|
|
break;
|
|
|
|
default:
|
2015-04-07 12:27:19 +02:00
|
|
|
switch (true) {
|
2017-07-05 06:26:58 +02:00
|
|
|
case $exception instanceof HttpExceptionInterface:
|
|
|
|
$this->getResponse()->setHttpResponseCode($exception->getStatusCode());
|
|
|
|
foreach ($exception->getHeaders() as $name => $value) {
|
|
|
|
$this->getResponse()->setHeader($name, $value, true);
|
|
|
|
}
|
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-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);
|
2020-11-13 17:43:40 +01:00
|
|
|
$module = $modules->hasLoaded($moduleName) ? $modules->getModule($moduleName) : null;
|
2018-01-22 10:28:47 +01:00
|
|
|
Logger::error("%s\n%s", $exception, IcingaException::getConfidentialTraceAsString($exception));
|
2015-04-07 12:27:19 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-01-19 11:39:22 +01:00
|
|
|
|
2020-11-13 17:43:40 +01:00
|
|
|
// Try to narrow down why the request has failed
|
|
|
|
if (preg_match(self::MISSING_DEP_ERROR, $exception->getMessage(), $match)) {
|
|
|
|
$sourcePath = $match[3];
|
|
|
|
foreach ($modules->listLoadedModules() as $name) {
|
|
|
|
$candidate = $modules->getModule($name);
|
|
|
|
$modulePath = $candidate->getBaseDir();
|
|
|
|
if (substr($sourcePath, 0, strlen($modulePath)) === $modulePath) {
|
|
|
|
$module = $candidate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 09:40:40 +02:00
|
|
|
if (preg_match('/^(?:Icinga\\\Module\\\(\w+)|(\w+)\\\(\w+))/', $match[1] ?: $match[2], $natch)) {
|
2020-11-13 17:43:40 +01:00
|
|
|
$this->view->requiredModule = isset($natch[1]) ? strtolower($natch[1]) : null;
|
2021-06-07 09:40:40 +02:00
|
|
|
$this->view->requiredVendor = isset($natch[2]) ? $natch[2] : null;
|
|
|
|
$this->view->requiredProject = isset($natch[3]) ? $natch[3] : null;
|
2020-11-13 17:43:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 11:39:22 +01:00
|
|
|
$this->view->messages = array();
|
|
|
|
|
2015-04-07 12:27:19 +02:00
|
|
|
if ($this->getInvokeArg('displayExceptions')) {
|
2018-01-19 11:39:22 +01:00
|
|
|
$this->view->stackTraces = array();
|
|
|
|
|
|
|
|
do {
|
|
|
|
$this->view->messages[] = $exception->getMessage();
|
2018-01-22 10:28:47 +01:00
|
|
|
$this->view->stackTraces[] = IcingaException::getConfidentialTraceAsString($exception);
|
2018-01-19 11:39:22 +01:00
|
|
|
$exception = $exception->getPrevious();
|
|
|
|
} while ($exception !== null);
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
$this->view->messages[] = $exception->getMessage();
|
|
|
|
$exception = $exception->getPrevious();
|
|
|
|
} while ($exception !== null);
|
2014-03-25 13:10:02 +01:00
|
|
|
}
|
2015-12-10 13:02:25 +01:00
|
|
|
|
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-09-07 13:37:00 +02:00
|
|
|
$this->getResponse()->json()
|
2018-01-26 15:11:28 +01:00
|
|
|
->setErrorMessage($this->view->messages[0])
|
2015-09-07 13:37:00 +02:00
|
|
|
->sendResponse();
|
2015-08-20 15:49:37 +02:00
|
|
|
}
|
|
|
|
|
2020-11-13 17:43:40 +01:00
|
|
|
$this->view->module = $module;
|
2014-01-22 14:57:54 +01:00
|
|
|
$this->view->request = $error->request;
|
2016-01-25 07:44:48 +01:00
|
|
|
if (! $isAuthenticated) {
|
|
|
|
$this->view->hideControls = true;
|
|
|
|
} else {
|
|
|
|
$this->view->hideControls = false;
|
|
|
|
$this->getTabs()->add('error', array(
|
|
|
|
'active' => true,
|
|
|
|
'label' => $this->translate('Error'),
|
|
|
|
'url' => Url::fromRequest()
|
|
|
|
));
|
|
|
|
}
|
2013-06-14 13:51:44 +02:00
|
|
|
}
|
|
|
|
}
|