sigma.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,759 行 · 第 1/5 页
PHP
1,759 行
* This should NOT be used to add logic (except some presentation one) to * the template. If you use a lot of such callbacks and implement business * logic through them, then you're reinventing the wheel. Consider using * XML/XSLT, native PHP or some other template engine. * * <?php * function h_one($arg) { * return '<h1>' . $arg . '</h1>'; * } * ... * $tpl = new HTML_Template_Sigma( ... ); * ... * $tpl->setCallbackFunction('h1', 'h_one'); * ?> * * template: * func_h1('H1 Headline'); * * @param string Function name in the template * @param mixed A callback: anything that can be passed to call_user_func_array() * @param bool If true, then no variable substitution in arguments will take place before function call * @return mixed SIGMA_OK on success, error object on failure * @throws PEAR_Error * @access public */ function setCallbackFunction($tplFunction, $callback, $preserveArgs = false) { if (!is_callable($callback)) { return $this->raiseError($this->errorMessage(SIGMA_INVALID_CALLBACK), SIGMA_INVALID_CALLBACK); } $this->_callback[$tplFunction] = array( 'data' => $callback, 'preserveArgs' => $preserveArgs ); return SIGMA_OK; } // end func setCallbackFunction /** * Returns a list of blocks within a template. * * If $recursive is false, it returns just a 'flat' array of $parent's * direct subblocks. If $recursive is true, it builds a tree of template * blocks using $parent as root. Tree structure is compatible with * PEAR::Tree's Memory_Array driver. * * @param string parent block name * @param bool whether to return a tree of child blocks (true) or a 'flat' array (false) * @access public * @return array a list of child blocks * @throws PEAR_Error */ function getBlockList($parent = '__global__', $recursive = false) { if (!isset($this->_blocks[$parent])) { return $this->raiseError($this->errorMessage(SIGMA_BLOCK_NOT_FOUND, $parent), SIGMA_BLOCK_NOT_FOUND); } if (!$recursive) { return isset($this->_children[$parent])? array_keys($this->_children[$parent]): array(); } else { $ret = array('name' => $parent); if (!empty($this->_children[$parent])) { $ret['children'] = array(); foreach (array_keys($this->_children[$parent]) as $child) { $ret['children'][] = $this->getBlockList($child, true); } } return $ret; } } /** * Returns a list of placeholders within a block. * * Only 'normal' placeholders are returned, not auto-created ones. * * @param string block name * @access public * @return array a list of placeholders * @throws PEAR_Error */ function getPlaceholderList($block = '__global__') { if (!isset($this->_blocks[$block])) { return $this->raiseError($this->errorMessage(SIGMA_BLOCK_NOT_FOUND, $block), SIGMA_BLOCK_NOT_FOUND); } $ret = array(); foreach ($this->_blockVariables[$block] as $var => $v) { if ('__' != substr($var, 0, 2) || '__' != substr($var, -2)) { $ret[] = $var; } } return $ret; } /** * Clears the variables * * Global variables are not affected. The method is useful when you add * a lot of variables via setVariable() and are not sure whether all of * them appear in the block you parse(). If you clear the variables after * parse(), you don't risk them suddenly showing up in other blocks. * * @access public * @see setVariable() */ function clearVariables() { $this->_variables = array(); } //------------------------------------------------------------ // // Private methods follow // //------------------------------------------------------------ /** * Reads the file and returns its content * * @param string filename * @return string file content (or error object) * @access private */ function _getFile($filename) { if (!($fh = @fopen($filename, 'r'))) { return $this->raiseError($this->errorMessage(SIGMA_TPL_NOT_FOUND, $filename), SIGMA_TPL_NOT_FOUND); } $content = fread($fh, max(1, filesize($filename))); fclose($fh); return $content; } /** * Recursively builds a list of all variables within a block. * * Also calls _buildFunctionlist() for each block it visits * * @param string block name * @see _buildFunctionlist() * @access private */ function _buildBlockVariables($block = '__global__') { $this->_blockVariables[$block] = array(); $this->_functions[$block] = array(); preg_match_all($this->variablesRegExp, $this->_blocks[$block], $regs, PREG_SET_ORDER); foreach ($regs as $match) { $this->_blockVariables[$block][$match[1]] = true; if (!empty($match[3])) { $funcData = array( 'name' => $match[3], 'args' => array($this->openingDelimiter . $match[1] . $this->closingDelimiter) ); $funcId = substr(md5(serialize($funcData)), 0, 10); // update block info $this->_blocks[$block] = str_replace($match[0], $this->openingDelimiter . '__function_' . $funcId . '__' . $this->closingDelimiter, $this->_blocks[$block]); $this->_blockVariables[$block]['__function_' . $funcId . '__'] = true; $this->_functions[$block][$funcId] = $funcData; } } if (SIGMA_OK != ($res = $this->_buildFunctionlist($block))) { return $res; } if (isset($this->_children[$block]) && is_array($this->_children[$block])) { foreach ($this->_children[$block] as $child => $v) { if (SIGMA_OK != ($res = $this->_buildBlockVariables($child))) { return $res; } } } return SIGMA_OK; } /** * Recusively builds a list of all blocks within the template. * * @param string template to be scanned * @see $_blocks * @throws PEAR_Error * @return mixed array of block names on success or error object on failure * @access private */ function _buildBlocks($string) { $blocks = array(); if (preg_match_all($this->blockRegExp, $string, $regs, PREG_SET_ORDER)) { foreach ($regs as $k => $match) { $blockname = $match[1]; $blockcontent = $match[2]; if (isset($this->_blocks[$blockname]) || isset($blocks[$blockname])) { return $this->raiseError($this->errorMessage(SIGMA_BLOCK_DUPLICATE, $blockname), SIGMA_BLOCK_DUPLICATE); } $this->_blocks[$blockname] = $blockcontent; $blocks[$blockname] = true; $inner = $this->_buildBlocks($blockcontent); if (PEAR::isError($inner)) { return $inner; } foreach ($inner as $name => $v) { $pattern = sprintf('@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm', $name, $name); $replacement = $this->openingDelimiter.'__'.$name.'__'.$this->closingDelimiter; $this->_blocks[$blockname] = preg_replace($pattern, $replacement, $this->_blocks[$blockname]); $this->_children[$blockname][$name] = true; } } } return $blocks; } /** * Resets the object's properties, used before processing a new template * * @access private * @param boolean remove unknown/unused variables? * @param boolean remove empty blocks? * @see setTemplate(), loadTemplateFile() * @access private */ function _resetTemplate($removeUnknownVariables = true, $removeEmptyBlocks = true) { $this->removeUnknownVariables = $removeUnknownVariables; $this->removeEmptyBlocks = $removeEmptyBlocks; $this->currentBlock = '__global__'; $this->_variables = array(); $this->_blocks = array(); $this->_children = array(); $this->_parsedBlocks = array(); $this->_touchedBlocks = array(); $this->_functions = array(); $this->flagGlobalParsed = false; } // _resetTemplate /** * Checks whether we have a "prepared" template cached. * * If we do not do caching, always returns false * * @access private * @param string source filename * @return bool yes/no * @see loadTemplatefile(), addBlockfile(), replaceBlockfile() */ function _isCached($filename) { if (null === $this->_cacheRoot) { return false; } $cachedName = $this->_cachedName($filename); $sourceName = $this->_sourceName($filename); // if $sourceName does not exist, error will be thrown later $sourceTime = @filemtime($sourceName); if ((false !== $sourceTime) && @file_exists($cachedName) && (filemtime($cachedName) > $sourceTime)) { return true; } else { return false; } } // _isCached /** * Loads a "prepared" template file * * @access private * @param string filename * @param string block name * @param string variable placeholder to replace by a block * @return mixed SIGMA_OK on success, error object on failure * @see loadTemplatefile(), addBlockfile(), replaceBlockfile() */ function _getCached($filename, $block = '__global__', $placeholder = '') { // the same checks are done in addBlock() if (!empty($placeholder)) { if (isset($this->_blocks[$block])) { return $this->raiseError($this->errorMessage(SIGMA_BLOCK_EXISTS, $block), SIGMA_BLOCK_EXISTS); } $parents = $this->_findParentBlocks($placeholder); if (0 == count($parents)) { return $this->raiseError($this->errorMessage(SIGMA_PLACEHOLDER_NOT_FOUND, $placeholder), SIGMA_PLACEHOLDER_NOT_FOUND); } elseif (count($parents) > 1) { return $this->raiseError($this->errorMessage(SIGMA_PLACEHOLDER_DUPLICATE, $placeholder), SIGMA_PLACEHOLDER_DUPLICATE); } } $content = $this->_getFile($this->_cachedName($filename)); if (PEAR::isError($content)) { return $content; } $cache = unserialize($content); if ('__global__' != $block) { $this->_blocks[$block] = $cache['blocks']['__global__']; $this->_blockVariables[$block] = $cache['variables']['__global__']; $this->_children[$block] = $cache['children']['__global__']; $this->_functions[$block] = $cache['functions']['__global__']; unset($cache['blocks']['__global__'], $cache['variables']['__global__'], $cache['children']['__global__'], $cache['functions']['__global__']); } $this->_blocks = array_merge($this->_blocks, $cache['blocks']); $this->_blockVariables = array_merge($this->_blockVariables, $cache['variables']); $this->_children = array_merge($this->_children, $cache['children']); $this->_functions = array_merge($this->_functions, $cache['functions']); // the same thing gets done in addBlockfile() if (!empty($placeholder)) { $this->_replacePlaceholder($parents[0], $placeholder, $block); } // pull the triggers, if any if (isset($cache['triggers'])) { return $this->_pullTriggers($cache['triggers']); } return SIGMA_OK; } // _getCached /** * Returns a full name of a "prepared" template file * * @access private * @param string source filename, relative to root directory * @return string filename */ function _cachedName($filename) { if ('/' == $filename{0} && '/' == substr($this->_cacheRoot, -1)) { $filename = substr($filename, 1); } $filename = str_replace('/', '__', $filename); return $this->_cacheRoot. $filename. '.it'; } // _cachedName /** * Returns a full name of a "source" template file * * @param string source filename, relative to root directory * @access private * @return string */ function _sourceName($filename) { if ('/' == $filename{0} && '/' == substr($this->fileRoot, -1)) { $filename = substr($filename, 1); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?