sigma.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,759 行 · 第 1/5 页

PHP
1,759
字号
   /**    * Constructor: builds some complex regular expressions and optionally     * sets the root directories.    *    * Make sure that you call this constructor if you derive your template    * class from this one.    *    * @param string  root directory for templates    * @param string  directory to cache "prepared" templates in    * @see   setRoot(), setCacheRoot()    */    function HTML_Template_Sigma($root = '', $cacheRoot = '')    {        // the class is inherited from PEAR to be able to use $this->setErrorHandling()        $this->PEAR();        $this->variablesRegExp       = '@' . $this->openingDelimiter . '(' . $this->variablenameRegExp . ')' .                                       '(:(' . $this->functionnameRegExp . '))?' . $this->closingDelimiter . '@sm';        $this->removeVariablesRegExp = '@'.$this->openingDelimiter.'\s*('.$this->variablenameRegExp.')\s*'.$this->closingDelimiter.'@sm';        $this->blockRegExp           = '@<!--\s+BEGIN\s+('.$this->blocknameRegExp.')\s+-->(.*)<!--\s+END\s+\1\s+-->@sm';        $this->functionRegExp        = '@' . $this->functionPrefix . '(' . $this->functionnameRegExp . ')\s*\(@sm';        $this->setRoot($root);        $this->setCacheRoot($cacheRoot);        $this->setCallbackFunction('h', 'htmlspecialchars');        $this->setCallbackFunction('u', 'urlencode');        $this->setCallbackFunction('j', array(&$this, '_jsEscape'));    }   /**    * Sets the file root for templates. The file root gets prefixed to all     * filenames passed to the object.    *     * @param    string  directory name    * @see      HTML_Template_Sigma()    * @access   public    */    function setRoot($root)    {        if (('' != $root) && ('/' != substr($root, -1))) {            $root .= '/';        }        $this->fileRoot = $root;    }   /**    * Sets the directory to cache "prepared" templates in, the directory should be writable for PHP.    *     * The "prepared" template contains an internal representation of template     * structure: essentially a serialized array of $_blocks, $_blockVariables,     * $_children and $_functions, may also contain $_triggers. This allows     * to bypass expensive calls to _buildBlockVariables() and especially     * _buildBlocks() when reading the "prepared" template instead of     * the "source" one.    *     * The files in this cache do not have any TTL and are regenerated when the    * source templates change.    *     * @param    string  directory name    * @see      HTML_Template_Sigma(), _getCached(), _writeCache()    * @access   public    */    function setCacheRoot($root)    {        if (empty($root)) {            return true;        } elseif (('' != $root) && ('/' != substr($root, -1))) {            $root .= '/';        }        $this->_cacheRoot = $root;    }   /**    * Sets the option for the template class    *     * @access public    * @param  string  option name    * @param  mixed   option value    * @return mixed   SIGMA_OK on success, error object on failure    */    function setOption($option, $value)    {        if (isset($this->_options[$option])) {            $this->_options[$option] = $value;            return SIGMA_OK;        }        return $this->raiseError($this->errorMessage(SIGMA_UNKNOWN_OPTION, $option), SIGMA_UNKNOWN_OPTION);    }   /**    * Returns a textual error message for an error code    *      * @access public    * @param  integer  error code    * @param  string   additional data to insert into message    * @return string   error message    */    function errorMessage($code, $data = null)    {        static $errorMessages;        if (!isset($errorMessages)) {            $errorMessages = array(                SIGMA_ERROR                 => 'unknown error',                SIGMA_OK                    => '',                SIGMA_TPL_NOT_FOUND         => 'Cannot read the template file \'%s\'',                SIGMA_BLOCK_NOT_FOUND       => 'Cannot find block \'%s\'',                SIGMA_BLOCK_DUPLICATE       => 'The name of a block must be unique within a template. Block \'%s\' found twice.',                SIGMA_CACHE_ERROR           => 'Cannot save template file \'%s\'',                SIGMA_UNKNOWN_OPTION        => 'Unknown option \'%s\'',                SIGMA_PLACEHOLDER_NOT_FOUND => 'Variable placeholder \'%s\' not found',                SIGMA_PLACEHOLDER_DUPLICATE => 'Placeholder \'%s\' should be unique, found in multiple blocks',                SIGMA_BLOCK_EXISTS          => 'Block \'%s\' already exists',                SIGMA_INVALID_CALLBACK      => 'Callback does not exist',                SIGMA_CALLBACK_SYNTAX_ERROR => 'Cannot parse template function: %s'            );        }        if (PEAR::isError($code)) {            $code = $code->getCode();        }        if (!isset($errorMessages[$code])) {            return $errorMessages[SIGMA_ERROR];        } else {            return (null === $data)? $errorMessages[$code]: sprintf($errorMessages[$code], $data);        }    }   /**    * Prints a block with all replacements done.    *     * @access  public    * @param   string  block name    * @see     get()    */    function show($block = '__global__')    {        print $this->get($block);    }   /**    * Returns a block with all replacements done.    *     * @param    string     block name    * @param    bool       whether to clear parsed block contents    * @return   string     block with all replacements done    * @throws   PEAR_Error    * @access   public    * @see      show()    */    function get($block = '__global__', $clear = false)    {        if (!isset($this->_blocks[$block])) {            return $this->raiseError($this->errorMessage(SIGMA_BLOCK_NOT_FOUND, $block), SIGMA_BLOCK_NOT_FOUND);        }        if ('__global__' == $block && !$this->flagGlobalParsed) {            $this->parse('__global__');        }        // return the parsed block, removing the unknown placeholders if needed        if (!isset($this->_parsedBlocks[$block])) {            return '';        } else {            $ret = $this->_parsedBlocks[$block];            if ($clear) {                unset($this->_parsedBlocks[$block]);            }            if ($this->removeUnknownVariables) {                $ret = preg_replace($this->removeVariablesRegExp, '', $ret);            }            if ($this->_options['preserve_data']) {                $ret = str_replace($this->openingDelimiter . '%preserved%' . $this->closingDelimiter, $this->openingDelimiter, $ret);            }            return $ret;        }    }   /**    * Parses the given block.    *        * @param    string    block name    * @param    boolean   true if the function is called recursively (do not set this to true yourself!)    * @param    boolean   true if parsing a "hidden" block (do not set this to true yourself!)    * @access   public    * @see      parseCurrentBlock()    * @throws   PEAR_Error    */    function parse($block = '__global__', $flagRecursion = false, $fakeParse = false)    {        static $vars;        if (!isset($this->_blocks[$block])) {            return $this->raiseError($this->errorMessage(SIGMA_BLOCK_NOT_FOUND, $block), SIGMA_BLOCK_NOT_FOUND);        }        if ('__global__' == $block) {            $this->flagGlobalParsed = true;        }        if (!isset($this->_parsedBlocks[$block])) {            $this->_parsedBlocks[$block] = '';        }        $outer = $this->_blocks[$block];        if (!$flagRecursion) {            $vars = array();        }        // block is not empty if its local var is substituted        $empty = true;        foreach ($this->_blockVariables[$block] as $allowedvar => $v) {            if (isset($this->_variables[$allowedvar])) {                $vars[$this->openingDelimiter . $allowedvar . $this->closingDelimiter] = $this->_variables[$allowedvar];                $empty = false;                // vital for checking "empty/nonempty" status                unset($this->_variables[$allowedvar]);            }        }        // processing of the inner blocks        if (isset($this->_children[$block])) {            foreach ($this->_children[$block] as $innerblock => $v) {                $placeholder = $this->openingDelimiter.'__'.$innerblock.'__'.$this->closingDelimiter;                if (isset($this->_hiddenBlocks[$innerblock])) {                    // don't bother actually parsing this inner block; but we _have_                    // to go through its local vars to prevent problems on next iteration                    $this->parse($innerblock, true, true);                    unset($this->_hiddenBlocks[$innerblock]);                    $outer = str_replace($placeholder, '', $outer);                } else {                    $this->parse($innerblock, true, $fakeParse);                    // block is not empty if its inner block is not empty                    if ('' != $this->_parsedBlocks[$innerblock]) {                        $empty = false;                    }                    $outer = str_replace($placeholder, $this->_parsedBlocks[$innerblock], $outer);                    $this->_parsedBlocks[$innerblock] = '';                }            }        }        // add "global" variables to the static array        foreach ($this->_globalVariables as $allowedvar => $value) {            if (isset($this->_blockVariables[$block][$allowedvar])) {                $vars[$this->openingDelimiter . $allowedvar . $this->closingDelimiter] = $value;            }        }        // if we are inside a hidden block, don't bother        if (!$fakeParse) {            if (0 != count($vars) && (!$flagRecursion || !empty($this->_functions[$block]))) {                $varKeys     = array_keys($vars);                $varValues   = $this->_options['preserve_data']? array_map(array(&$this, '_preserveOpeningDelimiter'), array_values($vars)): array_values($vars);            }            // check whether the block is considered "empty" and append parsed content if not            if (!$empty || ('__global__' == $block) || !$this->removeEmptyBlocks || isset($this->_touchedBlocks[$block])) {                // perform callbacks                if (!empty($this->_functions[$block])) {                    foreach ($this->_functions[$block] as $id => $data) {                        $placeholder = $this->openingDelimiter . '__function_' . $id . '__' . $this->closingDelimiter;                        // do not waste time calling function more than once                        if (!isset($vars[$placeholder])) {                            $args         = array();                            $preserveArgs = isset($this->_callback[$data['name']]['preserveArgs']) && $this->_callback[$data['name']]['preserveArgs'];                            foreach ($data['args'] as $arg) {                                $args[] = (empty($varKeys) || $preserveArgs)? $arg: str_replace($varKeys, $varValues, $arg);                            }                            if (isset($this->_callback[$data['name']]['data'])) {                                $res = call_user_func_array($this->_callback[$data['name']]['data'], $args);                            } else {                                $res = isset($args[0])? $args[0]: '';                            }                            $outer = str_replace($placeholder, $res, $outer);                            // save the result to variable cache, it can be requested somewhere else                            $vars[$placeholder] = $res;                        }                    }                }                // substitute variables only on non-recursive call, thus all                // variables from all inner blocks get substituted                if (!$flagRecursion && !empty($varKeys)) {                    $outer = str_replace($varKeys, $varValues, $outer);                }                $this->_parsedBlocks[$block] .= $outer;                if (isset($this->_touchedBlocks[$block])) {                    unset($this->_touchedBlocks[$block]);                }            }        }        return $empty;    }   /**    * Sets a variable value.    *     * The function can be used either like setVariable("varname", "value")    * or with one array $variables["varname"] = "value" given setVariable($variables)    *     * @access public    * @param  mixed     variable name or array ('varname'=>'value')    * @param  string    variable value if $variable is not an array    */    function setVariable($variable, $value = '')    {        if (is_array($variable)) {            $this->_variables = array_merge($this->_variables, $variable);        } else {            $this->_variables[$variable] = $value;        }    }   /**    * Sets a global variable value.    *     * @access public    * @param  mixed     variable name or array ('varname'=>'value')    * @param  string    variable value if $variable is not an array    * @see    setVariable()    */    function setGlobalVariable($variable, $value = '')    {        if (is_array($variable)) {            $this->_globalVariables = array_merge($this->_globalVariables, $variable);        } else {            $this->_globalVariables[$variable] = $value;        }    }   /**    * Sets the name of the current block: the block where variables are added    *    * @param    string      block name    * @return   mixed       SIGMA_OK on success, error object on failure    * @throws   PEAR_Error    * @access   public    */    function setCurrentBlock($block = '__global__')    {        if (!isset($this->_blocks[$block])) {            return $this->raiseError($this->errorMessage(SIGMA_BLOCK_NOT_FOUND, $block), SIGMA_BLOCK_NOT_FOUND);        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?