it.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 793 行 · 第 1/2 页
PHP
793 行
* @see parseCurrentBlock() * @throws IT_Error */ function parse($block = "__global__", $flag_recursion = false) { if (!isset($this->blocklist[$block])) { return new IT_Error("The block '$block' was not found in the template.", __FILE__, __LINE__); return false; } if ("__global__" == $block) $this->flagGlobalParsed = true; $regs = array(); $values = array(); if ($this->clearCacheOnParse) { foreach ($this->variableCache as $name => $value) { $regs[] = "@" . $this->openingDelimiter . $name . $this->closingDelimiter . "@"; $values[] = $value; } $this->variableCache = array(); } else { foreach ($this->blockvariables[$block] as $allowedvar => $v) { if (isset($this->variableCache[$allowedvar])) { $regs[] = "@".$this->openingDelimiter . $allowedvar . $this->closingDelimiter . "@"; $values[] = $this->variableCache[$allowedvar]; unset($this->variableCache[$allowedvar]); } } } $outer = (0 == count($regs)) ? $this->blocklist[$block] : preg_replace($regs, $values, $this->blocklist[$block]); $empty = (0 == count($values)) ? true : false; if (isset($this->blockinner[$block])) { foreach ($this->blockinner[$block] as $k => $innerblock) { $this->parse($innerblock, true); if ("" != $this->blockdata[$innerblock]) $empty = false; $placeholder = $this->openingDelimiter . "__" . $innerblock . "__" . $this->closingDelimiter; $outer = str_replace($placeholder, $this->blockdata[$innerblock], $outer); $this->blockdata[$innerblock] = ""; } } if ($this->removeUnknownVariables) $outer = preg_replace($this->removeVariablesRegExp, "", $outer); if ($empty) { if (!$this->removeEmptyBlocks) { $this->blockdata[$block ].= $outer; } else { if (isset($this->touchedBlocks[$block])) { $this->blockdata[$block] .= $outer; unset($this->touchedBlocks[$block]); } } } else { $this->blockdata[$block] .= $outer; } return $empty; } // end func parse /** * Parses the current block * @see parse(), setCurrentBlock(), $currentBlock * @access public */ function parseCurrentBlock() { return $this->parse($this->currentBlock); } // end func parseCurrentBlock /** * Sets a variable value. * * The function can be used eighter like setVariable( "varname", "value") * or with one array $variables["varname"] = "value" given setVariable($variables) * quite like phplib templates set_var(). * * @param mixed string with the variable name or an array %variables["varname"] = "value" * @param string value of the variable or empty if $variable is an array. * @param string prefix for variable names * @access public */ function setVariable($variable, $value = "") { if (is_array($variable)) { $this->variableCache = array_merge($this->variableCache, $variable); } else { $this->variableCache[$variable] = $value; } } // end func setVariable /** * Sets the name of the current block that is the block where variables are added. * * @param string name of the block * @return boolean false on failure, otherwise true * @throws IT_Error * @access public */ function setCurrentBlock($block = "__global__") { if (!isset($this->blocklist[$block])) return new IT_Error("Can't find the block '$block' in the template.", __FILE__, __LINE__); $this->currentBlock = $block; return true; } // end func setCurrentBlock /** * Preserves an empty block even if removeEmptyBlocks is true. * * @param string name of the block * @return boolean false on false, otherwise true * @throws IT_Error * @access public * @see $removeEmptyBlocks */ function touchBlock($block) { if (!isset($this->blocklist[$block])) return new IT_Error("Can't find the block '$block' in the template.", __FILE__, __LINE__); $this->touchedBlocks[$block] = true; return true; } // end func touchBlock /** * Clears all datafields of the object and rebuild the internal blocklist * * LoadTemplatefile() and setTemplate() automatically call this function * when a new template is given. Don't use this function * unless you know what you're doing. * * @access public * @see free() */ function init() { $this->free(); $this->findBlocks($this->template); // we don't need it any more $this->template = ''; $this->buildBlockvariablelist(); } // end func init /** * Clears all datafields of the object. * * Don't use this function unless you know what you're doing. * * @access public * @see init() */ function free() { $this->err = array(); $this->currentBlock = "__global__"; $this->variableCache = array(); $this->blocklookup = array(); $this->touchedBlocks = array(); $this->flagBlocktrouble = false; $this->flagGlobalParsed = false; } // end func free /** * Sets the template. * * You can eighter load a template file from disk with LoadTemplatefile() or set the * template manually using this function. * * @param string template content * @param boolean remove unknown/unused variables? * @param boolean remove empty blocks? * @see LoadTemplatefile(), $template * @access public */ function setTemplate($template, $removeUnknownVariables = true, $removeEmptyBlocks = true) { $this->removeUnknownVariables = $removeUnknownVariables; $this->removeEmptyBlocks = $removeEmptyBlocks; if ("" == $template && $this->flagCacheTemplatefile) { $this->variableCache = array(); $this->blockdata = array(); $this->touchedBlocks = array(); $this->currentBlock = "__global__"; } else { $this->template = '<!-- BEGIN __global__ -->' . $template . '<!-- END __global__ -->'; $this->init(); } if ($this->flagBlocktrouble) return false; return true; } // end func setTemplate /** * Reads a template file from the disk. * * @param string name of the template file * @param bool how to handle unknown variables. * @param bool how to handle empty blocks. * @access public * @return boolean false on failure, otherwise true * @see $template, setTemplate(), $removeUnknownVariables, $removeEmptyBlocks */ function loadTemplatefile($filename, $removeUnknownVariables = true, $removeEmptyBlocks = true) { $template = ""; if (!$this->flagCacheTemplatefile || $this->lastTemplatefile != $filename) $template = $this->getfile($filename); $this->lastTemplatefile = $filename; return $this->setTemplate($template, $removeUnknownVariables, $removeEmptyBlocks); } // end func LoadTemplatefile /** * Sets the file root. The file root gets prefixed to all filenames passed to the object. * * Make sure that you override this function when using the class * on windows. * * @param string * @see IntegratedTemplate() * @access public */ function setRoot($root) { if ("" != $root && "/" != substr($root, -1)) $root .= "/"; $this->fileRoot = $root; } // end func setRoot /** * Build a list of all variables within of a block */ function buildBlockvariablelist() { foreach ($this->blocklist as $name => $content) { preg_match_all( $this->variablesRegExp, $content, $regs ); if (0 != count($regs[1])) { foreach ($regs[1] as $k => $var) $this->blockvariables[$name][$var] = true; } else { $this->blockvariables[$name] = array(); } } } // end func buildBlockvariablelist /** * Returns a list of all */ function getGlobalvariables() { $regs = array(); $values = array(); foreach ($this->blockvariables["__global__"] as $allowedvar => $v) { if (isset($this->variableCache[$allowedvar])) { $regs[] = "@" . $this->openingDelimiter . $allowedvar . $this->closingDelimiter."@"; $values[] = $this->variableCache[$allowedvar]; unset($this->variableCache[$allowedvar]); } } return array($regs, $values); } // end func getGlobalvariables /** * Recusively builds a list of all blocks within the template. * * @param string string that gets scanned * @see $blocklist */ function findBlocks($string) { $blocklist = 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->blocklist[$blockname])) { new IT_Error("The name of a block must be unique within a template. Found '$blockname' twice. Unpredictable results may appear.", __FILE__, __LINE__); $this->flagBlocktrouble = true; } $this->blocklist[$blockname] = $blockcontent; $this->blockdata[$blockname] = ""; $blocklist[] = $blockname; $inner = $this->findBlocks($blockcontent); foreach ($inner as $k => $name) { $pattern = sprintf('@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm', $name, $name ); $this->blocklist[$blockname] = preg_replace( $pattern, $this->openingDelimiter . "__" . $name . "__" . $this->closingDelimiter, $this->blocklist[$blockname] ); $this->blockinner[$blockname][] = $name; $this->blockparents[$name] = $blockname; } } } return $blocklist; } // end func findBlocks /** * Reads a file from disk and returns its content. * @param string Filename * @return string Filecontent */ function getFile($filename) { if ("/" == $filename{0} && "/" == substr($this->fileRoot, -1)) $filename = substr($filename, 1); $filename = $this->fileRoot . $filename; if (!($fh = @fopen($filename, "r"))) { new IT_Error("Can't read '$filename'.", __FILE__, __LINE__); return ""; } $content = fread($fh, filesize($filename)); fclose($fh); return preg_replace("#<!-- INCLUDE (.*) -->#ime", "\$this->getFile('\\1')", $content); } // end func getFile } // end class IntegratedTemplate?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?