2013-06-03 17:02:08 +02:00
|
|
|
<?php
|
2013-06-07 13:29:11 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-07-12 15:37:36 +02:00
|
|
|
/**
|
2013-10-23 15:10:33 +02:00
|
|
|
* This file is part of Icinga Web 2.
|
2013-08-16 15:05:03 +02:00
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
2013-07-12 15:37:36 +02:00
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
2013-08-16 15:05:03 +02:00
|
|
|
*
|
2013-07-12 15:37:36 +02:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2013-08-16 15:05:03 +02:00
|
|
|
*
|
2013-07-12 15:37:36 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2013-08-16 15:05:03 +02:00
|
|
|
*
|
2013-07-12 15:37:36 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2013-08-16 15:05:03 +02:00
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*
|
2013-07-12 15:37:36 +02:00
|
|
|
*/
|
2013-06-07 13:29:11 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-08-16 15:05:03 +02:00
|
|
|
|
2013-08-12 15:58:26 +02:00
|
|
|
namespace Icinga\Web\Controller;
|
2013-06-03 17:02:08 +02:00
|
|
|
|
2013-09-24 12:48:30 +02:00
|
|
|
use \Exception;
|
2013-08-30 15:50:49 +02:00
|
|
|
use \Zend_Controller_Action;
|
|
|
|
use \Zend_Controller_Request_Abstract;
|
|
|
|
use \Zend_Controller_Response_Abstract;
|
|
|
|
use \Zend_Controller_Action_HelperBroker;
|
2014-01-23 16:03:47 +01:00
|
|
|
use Icinga\Authentication\Manager as AuthManager;
|
|
|
|
use Icinga\Application\Benchmark;
|
2014-01-29 16:25:08 +01:00
|
|
|
use Icinga\Util\Translator;
|
2014-01-23 16:03:47 +01:00
|
|
|
use Icinga\Web\Widget\Tabs;
|
|
|
|
use Icinga\Web\Url;
|
2014-02-05 18:18:22 +01:00
|
|
|
use Icinga\Application\Logger;
|
|
|
|
use Icinga\Web\Request;
|
2013-06-03 17:02:08 +02:00
|
|
|
|
2014-01-29 18:09:26 +01:00
|
|
|
use Icinga\File\Pdf;
|
|
|
|
use \DOMDocument;
|
|
|
|
|
2013-06-03 17:02:08 +02:00
|
|
|
/**
|
|
|
|
* Base class for all core action controllers
|
|
|
|
*
|
|
|
|
* All Icinga Web core controllers should extend this class
|
|
|
|
*/
|
2013-08-30 15:50:49 +02:00
|
|
|
class ActionController extends Zend_Controller_Action
|
2013-06-03 17:02:08 +02:00
|
|
|
{
|
2013-08-08 16:22:22 +02:00
|
|
|
/**
|
2013-09-02 18:33:19 +02:00
|
|
|
* Whether the controller requires the user to be authenticated
|
2013-08-16 15:05:03 +02:00
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2013-08-30 15:50:49 +02:00
|
|
|
protected $requiresAuthentication = true;
|
2013-08-08 16:22:22 +02:00
|
|
|
|
2013-06-03 17:02:08 +02:00
|
|
|
/**
|
|
|
|
* The constructor starts benchmarking, loads the configuration and sets
|
|
|
|
* other useful controller properties
|
|
|
|
*
|
2013-08-30 15:50:49 +02:00
|
|
|
* @param Zend_Controller_Request_Abstract $request
|
|
|
|
* @param Zend_Controller_Response_Abstract $response
|
|
|
|
* @param array $invokeArgs Any additional invocation arguments
|
2013-06-03 17:02:08 +02:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2013-08-30 15:50:49 +02:00
|
|
|
Zend_Controller_Request_Abstract $request,
|
|
|
|
Zend_Controller_Response_Abstract $response,
|
2013-06-03 17:02:08 +02:00
|
|
|
array $invokeArgs = array()
|
|
|
|
) {
|
2013-08-30 15:50:49 +02:00
|
|
|
$this->setRequest($request)
|
|
|
|
->setResponse($response)
|
|
|
|
->_setInvokeArgs($invokeArgs);
|
|
|
|
$this->_helper = new Zend_Controller_Action_HelperBroker($this);
|
2014-01-29 18:09:26 +01:00
|
|
|
$this->_helper->addPath('../application/controllers/helpers');
|
2013-07-12 11:58:58 +02:00
|
|
|
|
2013-08-30 15:50:49 +02:00
|
|
|
// when noInit is set (e.g. for testing), authentication and init is skipped
|
|
|
|
if (isset($invokeArgs['noInit'])) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-03 17:02:08 +02:00
|
|
|
|
2013-09-02 18:33:19 +02:00
|
|
|
if ($this->requiresLogin() === false) {
|
2013-08-08 16:22:22 +02:00
|
|
|
$this->view->tabs = new Tabs();
|
2013-06-03 17:02:08 +02:00
|
|
|
$this->init();
|
2013-08-30 15:50:49 +02:00
|
|
|
} else {
|
2013-10-04 12:54:42 +02:00
|
|
|
$this->redirectToLogin($this->getRequestUrl());
|
2013-06-03 17:02:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-22 14:06:59 +01:00
|
|
|
/**
|
|
|
|
* Return restriction information for an eventually authenticated user
|
|
|
|
*
|
|
|
|
* @param string $name Permission name
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
public function getRestrictions($name)
|
|
|
|
{
|
|
|
|
return AuthManager::getInstance()->getRestrictions($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the user currently authenticated has the given permission
|
|
|
|
*
|
|
|
|
* @param string $name Permission name
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasPermission($name)
|
|
|
|
{
|
|
|
|
return AuthManager::getInstance()->hasPermission($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws an exception if user lacks the given permission
|
|
|
|
*
|
|
|
|
* @param string $name Permission name
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function assertPermission($name)
|
|
|
|
{
|
|
|
|
if (! AuthManager::getInstance()->hasPermission($name)) {
|
|
|
|
// TODO: Shall this be an Auth Exception? Or a 404?
|
|
|
|
throw new Exception(sprintf('Auth error, no permission for "%s"', $name));
|
|
|
|
}
|
|
|
|
}
|
2013-09-11 17:19:18 +02:00
|
|
|
|
2013-09-02 18:33:19 +02:00
|
|
|
/**
|
|
|
|
* Check whether the controller requires a login. That is when the controller requires authentication and the
|
|
|
|
* user is currently not authenticated
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @see requiresAuthentication
|
|
|
|
*/
|
|
|
|
protected function requiresLogin()
|
2013-08-30 15:50:49 +02:00
|
|
|
{
|
|
|
|
if (!$this->requiresAuthentication) {
|
2013-09-02 18:33:19 +02:00
|
|
|
return false;
|
2013-08-30 15:50:49 +02:00
|
|
|
}
|
|
|
|
|
2013-11-20 12:01:40 +01:00
|
|
|
return !AuthManager::getInstance()->isAuthenticated();
|
2013-08-30 15:50:49 +02:00
|
|
|
}
|
2013-09-02 18:33:19 +02:00
|
|
|
|
2013-06-03 17:02:08 +02:00
|
|
|
/**
|
2013-08-16 15:05:03 +02:00
|
|
|
* Return the tabs
|
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @return Tabs
|
2013-08-16 15:05:03 +02:00
|
|
|
*/
|
2013-08-08 16:22:22 +02:00
|
|
|
public function getTabs()
|
2013-06-03 17:02:08 +02:00
|
|
|
{
|
2013-08-08 16:22:22 +02:00
|
|
|
return $this->view->tabs;
|
2013-06-03 17:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-29 16:25:08 +01:00
|
|
|
* Translate a string
|
2013-06-03 17:02:08 +02:00
|
|
|
*
|
2014-01-29 16:25:08 +01:00
|
|
|
* Autoselects the module domain, if any, and falls back to the global one if no translation could be found.
|
2013-06-03 17:02:08 +02:00
|
|
|
*
|
2014-01-29 16:25:08 +01:00
|
|
|
* @param string $text The string to translate
|
|
|
|
*
|
|
|
|
* @return string The translated string
|
2013-06-03 17:02:08 +02:00
|
|
|
*/
|
2014-01-29 16:25:08 +01:00
|
|
|
public function translate($text)
|
2013-06-03 17:02:08 +02:00
|
|
|
{
|
2014-01-29 16:25:08 +01:00
|
|
|
$module = $this->getRequest()->getModuleName();
|
|
|
|
$domain = $module === 'default' ? 'icinga' : $module;
|
|
|
|
return Translator::translate($text, $domain);
|
2013-06-03 17:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-30 15:50:49 +02:00
|
|
|
* Redirect to the login path
|
2013-10-04 12:54:42 +02:00
|
|
|
*
|
|
|
|
* @param string $afterLogin The action to call when the login was successful. Defaults to '/index/welcome'
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
2013-06-03 17:02:08 +02:00
|
|
|
*/
|
2013-10-23 16:01:52 +02:00
|
|
|
protected function redirectToLogin($afterLogin = '/index')
|
2013-06-03 17:02:08 +02:00
|
|
|
{
|
2013-09-11 17:19:18 +02:00
|
|
|
if ($this->getRequest()->isXmlHttpRequest()) {
|
2014-01-23 16:03:47 +01:00
|
|
|
$this->_response->setHttpResponseCode(401);
|
|
|
|
$this->_helper->json(array(
|
|
|
|
'exception' => 'You are not logged in',
|
|
|
|
'redirectTo' => Url::fromPath('/authentication/login')->getAbsoluteUrl()
|
|
|
|
));
|
2013-09-11 17:28:36 +02:00
|
|
|
}
|
2013-08-30 15:50:49 +02:00
|
|
|
$url = Url::fromPath('/authentication/login');
|
2013-10-04 12:54:42 +02:00
|
|
|
$url->setParam('redirect', $afterLogin);
|
2014-01-23 16:03:47 +01:00
|
|
|
$this->redirectNow($url);
|
2013-06-03 17:02:08 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 12:54:42 +02:00
|
|
|
/**
|
|
|
|
* Return the URI that can be used to request the current action
|
|
|
|
*
|
2013-10-20 16:28:53 +02:00
|
|
|
* @return string return the path to this action: <Module>/<Controller>/<Action>?<Query>
|
2013-10-04 12:54:42 +02:00
|
|
|
*/
|
|
|
|
public function getRequestUrl()
|
|
|
|
{
|
2013-10-20 16:28:53 +02:00
|
|
|
$base = $this->_request->getModuleName() . '/' .
|
|
|
|
$this->_request->getControllerName() . '/' .
|
|
|
|
$this->_request->getActionName();
|
|
|
|
return $_SERVER['QUERY_STRING'] !== '' ? $base . '?' . $_SERVER['QUERY_STRING'] : $base;
|
2013-10-04 12:54:42 +02:00
|
|
|
}
|
|
|
|
|
2013-08-08 16:22:22 +02:00
|
|
|
/**
|
|
|
|
* Redirect to a specific url, updating the browsers URL field
|
2013-08-16 15:05:03 +02:00
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @param Url|string $url The target to redirect to
|
2013-08-08 16:22:22 +02:00
|
|
|
**/
|
|
|
|
public function redirectNow($url)
|
2013-06-03 17:02:08 +02:00
|
|
|
{
|
2013-07-12 11:58:58 +02:00
|
|
|
if ($url instanceof Url) {
|
2013-08-06 11:53:42 +02:00
|
|
|
$url = $url->getRelativeUrl();
|
2013-08-21 09:37:16 +02:00
|
|
|
} else {
|
|
|
|
$url = Url::fromPath($url)->getRelativeUrl();
|
2013-07-12 11:58:58 +02:00
|
|
|
}
|
2013-06-03 17:02:08 +02:00
|
|
|
$this->_helper->Redirector->gotoUrlAndExit($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-08 16:22:22 +02:00
|
|
|
* Detect whether the current request requires changes in the layout and apply them before rendering
|
2013-06-03 17:02:08 +02:00
|
|
|
*
|
2013-08-08 16:22:22 +02:00
|
|
|
* @see Zend_Controller_Action::postDispatch()
|
2013-06-03 17:02:08 +02:00
|
|
|
*/
|
|
|
|
public function postDispatch()
|
|
|
|
{
|
|
|
|
Benchmark::measure('Action::postDispatch()');
|
2013-06-07 13:29:11 +02:00
|
|
|
|
2013-06-03 17:02:08 +02:00
|
|
|
if ($this->_request->isXmlHttpRequest()) {
|
2013-10-17 19:48:46 +02:00
|
|
|
$target = ($this->getParam('render') === 'detail') ? 'inline' : 'body';
|
2013-10-22 12:08:44 +02:00
|
|
|
if ($target !== 'inline') {
|
|
|
|
$target = ($this->getParam('view') === 'compact') ? 'inline' : 'body';
|
|
|
|
}
|
2013-10-17 19:48:46 +02:00
|
|
|
$this->_helper->layout()->setLayout($target);
|
2013-06-03 17:02:08 +02:00
|
|
|
}
|
2014-01-27 13:22:29 +01:00
|
|
|
if ($user = $this->getRequest()->getUser()) {
|
|
|
|
if ($user->getPreferences()->get('app.showBenchmark') === true) {
|
|
|
|
Benchmark::measure('Response ready');
|
|
|
|
$this->_helper->layout()->benchmark = $this->renderBenchmark();
|
|
|
|
}
|
2014-01-24 12:20:13 +01:00
|
|
|
}
|
2014-01-29 18:09:26 +01:00
|
|
|
if ($this->_request->getParam('format') === 'pdf' && $this->_request->getControllerName() !== 'static') {
|
|
|
|
$html = $this->view->render(
|
|
|
|
$this->_request->getControllerName() . '/' . $this->_request->getActionName() . '.phtml'
|
|
|
|
);
|
|
|
|
$this->sendAsPdf($html);
|
2014-02-05 18:18:22 +01:00
|
|
|
die();
|
2014-01-29 18:09:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function sendAsPdf($body)
|
|
|
|
{
|
2014-02-05 18:18:22 +01:00
|
|
|
if (!headers_sent()) {
|
|
|
|
$css = $this->view->getHelper('action')->action('stylesheet', 'static', 'application');
|
|
|
|
$pdf = new PDF();
|
|
|
|
$pdf->renderPage($body, $css);
|
|
|
|
$pdf->stream($this->_request->getControllerName() . '-' . $this->_request->getActionName() . '.pdf');
|
|
|
|
} else {
|
|
|
|
Logger::error('Could not send pdf-response, content already written to output.');
|
|
|
|
die();
|
2014-01-29 18:09:26 +01:00
|
|
|
}
|
2014-01-24 12:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the benchmark
|
|
|
|
*
|
|
|
|
* @return string Benchmark HTML
|
|
|
|
*/
|
|
|
|
protected function renderBenchmark()
|
|
|
|
{
|
|
|
|
return Benchmark::renderToHtml();
|
2013-06-03 17:02:08 +02:00
|
|
|
}
|
2013-08-27 17:49:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to call compatible methods from older zend versions
|
|
|
|
*
|
|
|
|
* Methods like getParam and redirect are _getParam/_redirect in older Zend versions (which reside for example
|
|
|
|
* in Debian Wheezy). Using those methods without the "_" causes the application to fail on those platforms, but
|
|
|
|
* using the version with "_" forces us to use deprecated code. So we try to catch this issue by looking for methods
|
|
|
|
* with the same name, but with a "_" prefix prepended.
|
|
|
|
*
|
2013-09-24 12:48:30 +02:00
|
|
|
* @param string $name The method name to check
|
|
|
|
* @param mixed $params The method parameters
|
|
|
|
* @return mixed Anything the method returns
|
2013-08-27 17:49:25 +02:00
|
|
|
*/
|
|
|
|
public function __call($name, $params)
|
|
|
|
{
|
2013-09-24 12:48:30 +02:00
|
|
|
$deprecatedMethod = '_' . $name;
|
2013-08-27 17:49:25 +02:00
|
|
|
|
|
|
|
if (method_exists($this, $deprecatedMethod)) {
|
|
|
|
return call_user_func_array(array($this, $deprecatedMethod), $params);
|
|
|
|
}
|
2013-09-24 12:48:30 +02:00
|
|
|
|
2014-01-24 16:41:37 +01:00
|
|
|
return parent::__call($name, $params);
|
2013-08-27 17:49:25 +02:00
|
|
|
}
|
2013-06-03 17:02:08 +02:00
|
|
|
}
|