icingaweb2/library/Icinga/Web/View.php

227 lines
5.3 KiB
PHP
Raw Normal View History

2013-07-12 14:33:17 +02:00
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
2013-07-12 14:33:17 +02:00
namespace Icinga\Web;
2015-01-23 09:36:45 +01:00
use Closure;
use Zend_View_Abstract;
use Icinga\Authentication\Manager;
use Icinga\Exception\ProgrammingError;
use Icinga\Util\Translator;
2013-07-12 14:33:17 +02:00
2013-08-20 19:26:44 +02:00
/**
* Icinga view
*/
class View extends Zend_View_Abstract
2013-07-12 14:33:17 +02:00
{
/**
* Charset to be used - we only support UTF-8
*/
const CHARSET = 'UTF-8';
/**
* The flags we use for htmlspecialchars depend on our PHP version
*/
private $replaceFlags;
2013-08-20 19:26:44 +02:00
/**
* Flag to register stream wrapper
*
* @var bool
*/
private $useViewStream = false;
2013-07-12 14:33:17 +02:00
/**
* Registered helper functions
*/
private $helperFunctions = array();
2015-01-23 09:36:45 +01:00
/**
* Authentication manager
*
* @var \Icinga\Authentication\Manager|null
2015-01-23 09:36:45 +01:00
*/
private $auth;
2013-08-20 19:26:44 +02:00
/**
* Create a new view object
*
* @param array $config
* @see Zend_View_Abstract::__construct
*/
2013-07-12 14:33:17 +02:00
public function __construct($config = array())
{
2013-08-20 19:26:44 +02:00
$this->useViewStream = (bool) ini_get('short_open_tag') ? false : true;
if ($this->useViewStream) {
2013-07-12 14:33:17 +02:00
if (!in_array('zend.view', stream_get_wrappers())) {
stream_wrapper_register('zend.view', '\Icinga\Web\ViewStream');
}
}
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
$this->replaceFlags = ENT_COMPAT | ENT_SUBSTITUTE | ENT_HTML5;
} else {
$this->replaceFlags = ENT_COMPAT | ENT_IGNORE;
}
2013-07-12 14:33:17 +02:00
parent::__construct($config);
}
2013-08-20 19:26:44 +02:00
/**
* Initialize the view
*
* @see Zend_View_Abstract::init
*/
2013-07-12 14:33:17 +02:00
public function init()
{
$this->loadGlobalHelpers();
}
/**
* Escape the given value top be safely used in view scripts
*
* @param string $value The output to be escaped
* @return string
*/
2014-03-31 18:37:21 +02:00
public function escape($value)
{
return htmlspecialchars($value, $this->replaceFlags, self::CHARSET, true);
}
/**
* Whether a specific helper (closure) has been registered
*
* @param string $name The desired function name
* @return boolean
*/
public function hasHelperFunction($name)
{
return array_key_exists($name, $this->helperFunctions);
}
/**
* Add a new helper function
*
* @param string $name The desired function name
* @param Closure $function An anonymous function
* @return self
*/
public function addHelperFunction($name, Closure $function)
{
if ($this->hasHelperFunction($name)) {
throw new ProgrammingError(
'Cannot assign the same helper function twice: "%s"',
$name
2014-03-31 18:37:21 +02:00
);
}
$this->helperFunctions[$name] = $function;
return $this;
}
/**
* Call a helper function
*
* @param string $name The desired function name
* @param Array $args Function arguments
* @return mixed
*/
public function callHelperFunction($name, $args)
{
return call_user_func_array(
$this->helperFunctions[$name],
$args
);
}
public function translate($text, $context = null)
{
return Translator::translate($text, $this->translationDomain, $context);
}
2014-09-15 14:11:42 +02:00
/**
* Translate a plural string
*
* @see Translator::translatePlural()
*/
public function translatePlural($textSingular, $textPlural, $number, $context = null)
{
return Translator::translatePlural($textSingular, $textPlural, $number, $this->translationDomain, $context);
}
2013-08-20 19:26:44 +02:00
/**
* Load helpers
*/
private function loadGlobalHelpers()
2013-07-12 14:33:17 +02:00
{
$pattern = dirname(__FILE__) . '/View/helpers/*.php';
$files = glob($pattern);
foreach ($files as $file) {
require_once $file;
}
}
2015-01-23 09:36:45 +01:00
/**
* Get the authentication manager
*
* @return Manager
*/
public function Auth()
{
if ($this->auth === null) {
$this->auth = Manager::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);
}
2013-08-20 19:26:44 +02:00
/**
* Use to include the view script in a scope that only allows public
* members.
*
* @return mixed
*
* @see Zend_View_Abstract::run
*/
2013-07-12 14:33:17 +02:00
protected function _run()
{
foreach ($this->getVars() as $k => $v) {
// Exporting global variables to view scripts:
$$k = $v;
}
2013-08-20 19:26:44 +02:00
if ($this->useViewStream) {
include 'zend.view://' . func_get_arg(0);
2013-07-12 14:33:17 +02:00
} else {
include func_get_arg(0);
}
}
2013-08-20 19:26:44 +02:00
/**
* Accesses a helper object from within a script
*
* @param string $name
* @param array $args
*
* @return string
*/
2013-07-12 14:33:17 +02:00
public function __call($name, $args)
{
if ($this->hasHelperFunction($name)) {
return $this->callHelperFunction($name, $args);
2013-07-12 14:33:17 +02:00
} else {
return parent::__call($name, $args);
}
}
}