From 8ca6cc32b2c789a3149861159ad258f2cb9491e3 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:39:39 +0200 Subject: Adding upstream version 2.11.4. Signed-off-by: Daniel Baumann --- library/Icinga/Web/View.php | 254 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 254 insertions(+) create mode 100644 library/Icinga/Web/View.php (limited to 'library/Icinga/Web/View.php') diff --git a/library/Icinga/Web/View.php b/library/Icinga/Web/View.php new file mode 100644 index 0000000..ec16940 --- /dev/null +++ b/library/Icinga/Web/View.php @@ -0,0 +1,254 @@ +getLibraryDir('Icinga/Web/View/Helper'); + + parent::__construct($config); + } + + /** + * Initialize the view + * + * @see Zend_View_Abstract::init + */ + 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 + */ + public function escape($value) + { + return htmlspecialchars($value ?? '', ENT_COMPAT | ENT_SUBSTITUTE | ENT_HTML5, 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 $this + */ + public function addHelperFunction($name, Closure $function) + { + if ($this->hasHelperFunction($name)) { + throw new ProgrammingError( + 'Cannot assign the same helper function twice: "%s"', + $name + ); + } + + $this->helperFunctions[$name] = $function; + return $this; + } + + /** + * Set or overwrite a helper function + * + * @param string $name + * @param Closure $function + * + * @return $this + */ + public function setHelperFunction($name, Closure $function) + { + $this->helperFunctions[$name] = $function; + return $this; + } + + /** + * Drop a helper function + * + * @param string $name + * + * @return $this + */ + public function dropHelperFunction($name) + { + unset($this->helperFunctions[$name]); + 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 + ); + } + + /** + * Load helpers + */ + private function loadGlobalHelpers() + { + $pattern = dirname(__FILE__) . '/View/helpers/*.php'; + $files = glob($pattern); + foreach ($files as $file) { + require_once $file; + } + } + + /** + * 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); + } + + /** + * Use to include the view script in a scope that only allows public + * members. + * + * @return mixed + * + * @see Zend_View_Abstract::run + */ + protected function _run() + { + foreach ($this->getVars() as $k => $v) { + // Exporting global variables to view scripts: + $$k = $v; + } + + include func_get_arg(0); + } + + /** + * Accesses a helper object from within a script + * + * @param string $name + * @param array $args + * + * @return string + */ + public function __call($name, $args) + { + if ($this->hasHelperFunction($name)) { + return $this->callHelperFunction($name, $args); + } else { + return parent::__call($name, $args); + } + } +} -- cgit v1.2.3