/usr/share/php/Icinga/Web/Controller/ActionController.php is in php-icinga 2.1.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Controller;
use Zend_Controller_Action;
use Zend_Controller_Action_HelperBroker;
use Zend_Controller_Request_Abstract;
use Zend_Controller_Response_Abstract;
use Icinga\Application\Benchmark;
use Icinga\Application\Config;
use Icinga\Authentication\Auth;
use Icinga\Exception\Http\HttpMethodNotAllowedException;
use Icinga\Exception\IcingaException;
use Icinga\Exception\ProgrammingError;
use Icinga\File\Pdf;
use Icinga\Forms\AutoRefreshForm;
use Icinga\Security\SecurityException;
use Icinga\Util\Translator;
use Icinga\Web\Notification;
use Icinga\Web\Session;
use Icinga\Web\Url;
use Icinga\Web\UrlParams;
use Icinga\Web\Widget\Tabs;
use Icinga\Web\Window;
/**
* Base class for all core action controllers
*
* All Icinga Web core controllers should extend this class
*
* @method \Icinga\Web\Request getRequest() {
* {@inheritdoc}
* @return \Icinga\Web\Request
* }
*
* @method \Icinga\Web\Response getResponse() {
* {@inheritdoc}
* @return \Icinga\Web\Response
* }
*/
class ActionController extends Zend_Controller_Action
{
/**
* Whether the controller requires the user to be authenticated
*
* @var bool
*/
protected $requiresAuthentication = true;
/**
* The current module's name
*
* @var string
*/
private $moduleName;
private $autorefreshInterval;
private $reloadCss = false;
private $window;
private $rerenderLayout = false;
private $xhrLayout = 'inline';
/**
* The inner layout (inside the body) to use
*
* @var string
*/
protected $innerLayout = 'body';
/**
* Authentication manager
*
* @var Auth|null
*/
private $auth;
/**
* URL parameters
*
* @var UrlParams
*/
protected $params;
/**
* The constructor starts benchmarking, loads the configuration and sets
* other useful controller properties
*
* @param Zend_Controller_Request_Abstract $request
* @param Zend_Controller_Response_Abstract $response
* @param array $invokeArgs Any additional invocation arguments
*/
public function __construct(
Zend_Controller_Request_Abstract $request,
Zend_Controller_Response_Abstract $response,
array $invokeArgs = array()
) {
$this->params = UrlParams::fromQueryString();
$this->setRequest($request)
->setResponse($response)
->_setInvokeArgs($invokeArgs);
$this->_helper = new Zend_Controller_Action_HelperBroker($this);
$this->handlerBrowserWindows();
$moduleName = $this->getModuleName();
$this->view->translationDomain = $moduleName !== 'default' ? $moduleName : 'icinga';
$this->_helper->layout()->isIframe = $request->getUrl()->shift('isIframe');
$this->_helper->layout()->showFullscreen = $request->getUrl()->shift('showFullscreen');
$this->_helper->layout()->moduleName = $moduleName;
$this->view->compact = $request->getParam('view') === 'compact';
if ($request->getUrl()->shift('showCompact')) {
$this->view->compact = true;
}
if ($this->rerenderLayout = $request->getUrl()->shift('renderLayout')) {
$this->xhrLayout = $this->innerLayout;
}
if ($request->getUrl()->shift('_disableLayout')) {
$this->_helper->layout()->disableLayout();
}
if ($this->requiresLogin()) {
$this->redirectToLogin(Url::fromRequest());
}
$this->view->tabs = new Tabs();
$this->prepareInit();
$this->init();
}
/**
* Prepare controller initialization
*
* As it should not be required for controllers to call the parent's init() method, base controllers should use
* prepareInit() in order to prepare the controller initialization.
*
* @see \Zend_Controller_Action::init() For the controller initialization method.
*/
protected function prepareInit()
{
}
/**
* Get the authentication manager
*
* @return Auth
*/
public function Auth()
{
if ($this->auth === null) {
$this->auth = Auth::getInstance();
}
return $this->auth;
}
/**
* Whether the current user has the given permission
*
* @param string $permission Name of the permission
*
* @return bool
*/
public function hasPermission($permission)
{
return $this->Auth()->hasPermission($permission);
}
/**
* Assert that the current user has the given permission
*
* @param string $permission Name of the permission
*
* @throws SecurityException If the current user lacks the given permission
*/
public function assertPermission($permission)
{
if ($this->requiresAuthentication && ! $this->Auth()->hasPermission($permission)) {
throw new SecurityException('No permission for %s', $permission);
}
}
/**
* Return the current module's name
*
* @return string
*/
public function getModuleName()
{
if ($this->moduleName === null) {
$this->moduleName = $this->getRequest()->getModuleName();
}
return $this->moduleName;
}
public function Config($file = null)
{
if ($file === null) {
return Config::app();
} else {
return Config::app($file);
}
}
public function Window()
{
if ($this->window === null) {
$this->window = new Window(
$this->_request->getHeader('X-Icinga-WindowId', Window::UNDEFINED)
);
}
return $this->window;
}
protected function handlerBrowserWindows()
{
if ($this->isXhr()) {
$id = $this->_request->getHeader('X-Icinga-WindowId', null);
if ($id === Window::UNDEFINED) {
$this->window = new Window($id);
$this->_response->setHeader('X-Icinga-WindowId', Window::generateId());
}
}
}
protected function reloadCss()
{
$this->reloadCss = true;
return $this;
}
/**
* Respond with HTTP 405 if the current request's method is not one of the given methods
*
* @param string $httpMethod Unlimited number of allowed HTTP methods
*
* @throws HttpMethodNotAllowedException If the request method is not one of the given methods
*/
public function assertHttpMethod($httpMethod)
{
$httpMethods = array_flip(array_map('strtoupper', func_get_args()));
if (! isset($httpMethods[$this->getRequest()->getMethod()])) {
$e = new HttpMethodNotAllowedException($this->translate('Method Not Allowed'));
$e->setAllowedMethods(implode(', ', array_keys($httpMethods)));
throw $e;
}
}
/**
* Return restriction information for an eventually authenticated user
*
* @param string $name Permission name
* @return Array
*/
public function getRestrictions($name)
{
return $this->Auth()->getRestrictions($name);
}
/**
* 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()
{
if (!$this->requiresAuthentication) {
return false;
}
return !$this->Auth()->isAuthenticated();
}
/**
* Return the tabs
*
* @return Tabs
*/
public function getTabs()
{
return $this->view->tabs;
}
/**
* Translate a string
*
* Autoselects the module domain, if any, and falls back to the global one if no translation could be found.
*
* @param string $text The string to translate
* @param string|null $context Optional parameter for context based translation
*
* @return string The translated string
*/
public function translate($text, $context = null)
{
return Translator::translate($text, $this->view->translationDomain, $context);
}
/**
* Translate a plural string
*
* @param string $textSingular The string in singular form to translate
* @param string $textPlural The string in plural form to translate
* @param string $number The number to get the plural or singular string
* @param string|null $context Optional parameter for context based translation
*
* @return string The translated string
*/
public function translatePlural($textSingular, $textPlural, $number, $context = null)
{
return Translator::translatePlural($textSingular, $textPlural, $number, $this->view->translationDomain, $context);
}
protected function ignoreXhrBody()
{
if ($this->isXhr()) {
$this->getResponse()->setHeader('X-Icinga-Container', 'ignore');
}
}
public function setAutorefreshInterval($interval)
{
if (! is_int($interval) || $interval < 1) {
throw new ProgrammingError(
'Setting autorefresh interval smaller than 1 second is not allowed'
);
}
$this->autorefreshInterval = $interval;
$this->_helper->layout()->autorefreshInterval = $interval;
return $this;
}
public function disableAutoRefresh()
{
$this->autorefreshInterval = null;
$this->_helper->layout()->autorefreshInterval = null;
return $this;
}
/**
* Redirect to login
*
* XHR will always redirect to __SELF__ if an URL to redirect to after successful login is set. __SELF__ instructs
* JavaScript to redirect to the current window's URL if it's an auto-refresh request or to redirect to the URL
* which required login if it's not an auto-refreshing one.
*
* XHR will respond with HTTP status code 403 Forbidden.
*
* @param Url|string $redirect URL to redirect to after successful login
*/
protected function redirectToLogin($redirect = null)
{
$login = Url::fromPath('authentication/login');
if ($this->isXhr()) {
if ($redirect !== null) {
$login->setParam('redirect', '__SELF__');
}
$this->_response->setHttpResponseCode(403);
} elseif ($redirect !== null) {
if (! $redirect instanceof Url) {
$redirect = Url::fromPath($redirect);
}
if (($relativeUrl = $redirect->getRelativeUrl())) {
$login->setParam('redirect', $relativeUrl);
}
}
$this->rerenderLayout()->redirectNow($login);
}
protected function rerenderLayout()
{
$this->rerenderLayout = true;
$this->xhrLayout = 'layout';
return $this;
}
public function isXhr()
{
return $this->getRequest()->isXmlHttpRequest();
}
protected function redirectXhr($url)
{
if (! $url instanceof Url) {
$url = Url::fromPath($url);
}
if ($this->rerenderLayout) {
$this->getResponse()->setHeader('X-Icinga-Rerender-Layout', 'yes');
}
if ($this->reloadCss) {
$this->getResponse()->setHeader('X-Icinga-Reload-Css', 'now');
}
$this->shutdownSession();
$this->getResponse()
->setHeader('X-Icinga-Redirect', rawurlencode($url->getAbsoluteUrl()))
->sendHeaders();
exit;
}
protected function redirectHttp($url)
{
if (! $url instanceof Url) {
$url = Url::fromPath($url);
}
$this->shutdownSession();
$this->_helper->Redirector->gotoUrlAndExit($url->getRelativeUrl());
}
/**
* Redirect to a specific url, updating the browsers URL field
*
* @param Url|string $url The target to redirect to
**/
public function redirectNow($url)
{
if ($this->isXhr()) {
$this->redirectXhr($url);
} else {
$this->redirectHttp($url);
}
}
/**
* @see Zend_Controller_Action::preDispatch()
*/
public function preDispatch()
{
$form = new AutoRefreshForm();
$form->handleRequest();
$this->_helper->layout()->autoRefreshForm = $form;
}
/**
* Detect whether the current request requires changes in the layout and apply them before rendering
*
* @see Zend_Controller_Action::postDispatch()
*/
public function postDispatch()
{
Benchmark::measure('Action::postDispatch()');
$req = $this->getRequest();
$layout = $this->_helper->layout();
$layout->innerLayout = $this->innerLayout;
if ($user = $req->getUser()) {
// Cast preference app.show_benchmark to bool because preferences loaded from a preferences storage are
// always strings
if ((bool) $user->getPreferences()->getValue('icingaweb', 'show_benchmark', false) === true) {
if (!$this->_helper->viewRenderer->getNoRender()) {
$layout->benchmark = $this->renderBenchmark();
}
}
if ((bool) $user->getPreferences()->getValue('icingaweb', 'auto_refresh', true) === false) {
$this->disableAutoRefresh();
}
}
if ($req->getParam('format') === 'pdf') {
$this->shutdownSession();
$this->sendAsPdf();
exit;
}
if ($this->isXhr()) {
$this->postDispatchXhr();
}
$this->shutdownSession();
}
protected function postDispatchXhr()
{
$layout = $this->_helper->layout();
$layout->setLayout($this->xhrLayout);
$resp = $this->getResponse();
$notifications = Notification::getInstance();
if ($notifications->hasMessages()) {
$notificationList = array();
foreach ($notifications->popMessages() as $m) {
$notificationList[] = rawurlencode($m->type . ' ' . $m->message);
}
$resp->setHeader('X-Icinga-Notification', implode('&', $notificationList), true);
}
if ($this->reloadCss) {
$resp->setHeader('X-Icinga-CssReload', 'now', true);
}
if ($this->view->title) {
if (preg_match('~[\r\n]~', $this->view->title)) {
// TODO: Innocent exception and error log for hack attempts
throw new IcingaException('No way, guy');
}
$resp->setHeader(
'X-Icinga-Title',
rawurlencode($this->view->title . ' :: Icinga Web'),
true
);
} else {
$resp->setHeader('X-Icinga-Title', rawurlencode('Icinga Web'), true);
}
if ($this->rerenderLayout) {
$this->getResponse()->setHeader('X-Icinga-Container', 'layout', true);
}
if ($this->autorefreshInterval !== null) {
$resp->setHeader('X-Icinga-Refresh', $this->autorefreshInterval, true);
}
}
protected function sendAsPdf()
{
$pdf = new Pdf();
$pdf->renderControllerAction($this);
}
protected function shutdownSession()
{
$session = Session::getSession();
if ($session->hasChanged()) {
$session->write();
}
}
/**
* Render the benchmark
*
* @return string Benchmark HTML
*/
protected function renderBenchmark()
{
$this->_helper->viewRenderer->postDispatch();
Benchmark::measure('Response ready');
return Benchmark::renderToHtml();
}
/**
* 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.
*
* @param string $name The method name to check
* @param mixed $params The method parameters
* @return mixed Anything the method returns
*/
public function __call($name, $params)
{
$deprecatedMethod = '_' . $name;
if (method_exists($this, $deprecatedMethod)) {
return call_user_func_array(array($this, $deprecatedMethod), $params);
}
return parent::__call($name, $params);
}
}
|