taglib.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 619 行 · 第 1/2 页
PHP
619 行
*/ function includeFile( $input ) {//print "<br>includeFile<br>";//print_r($this->getOptions()); $openDel = preg_quote($this->getOption('delimiter',0)); $closeDel = preg_quote($this->getOption('delimiter',1)); $_openDel = $this->getOption('delimiter',0); $_closeDel = $this->getOption('delimiter',1);// FIXXXME discover all the functions that are used in the current file// BUT only if it is no template, because then we assume it is a macro file!!! or smthg like this// so only those functions are pasted inside the code!!! //"// OTHER IDEA// may be just compile the files that shall be included and replace the {%include by a php-include // this var contains all the files this method has already included // we count them to prevent recursive inclusion of files! $justIncluded = array(); // $parseAgain will be true if another template was included // if the last one is done $parseAgain will be set to false and the while loop quits $parseAgain = true; while ($parseAgain) { // if there are no more {%include%} tags in the file content, then we dont have to // loop again. This is to handle included files that have includes inside. if (!preg_match_all( '/'.$openDel.'%\s*include\s+(.+)\s*%'.$closeDel.'/U',$input,$includes)) { $parseAgain = false; } else { //print_r($includes); if (sizeof($includes[1])) { foreach ($includes[1] as $index=>$aInclude) { // get the relative path to templateDir or absolute if given // FIXXME unix specific!!!! if ($aInclude[0] != '/') { // add trailing slash if missing $_aInclude = '/'.$aInclude; } $fileToInclude = $this->options['templateDir'].$_aInclude; if (in_array($fileToInclude,$justIncluded)) {//FIXXXME i need a nicer error handling way// somehow i would need to make the Xipe-Error method from the Main.php to be called statically!!!print "<b>Xipe-Error</b><br>The file <b>{$_aInclude}</b> is included recursively, this is not possible!<br>"; break(2); } $justIncluded[] = $fileToInclude; // do only include a file that really exists, otherwise the tag also stays there, so the programmer removes it // do also search for the file in the include path, but as the second option only! if ($fileContent = @file_get_contents($fileToInclude)) { $contentFile = $fileToInclude; } else { if ($fileContent = @file_get_contents($aInclude,true)) { $contentFile = $aInclude; } } if ($fileContent) { $pathInfo = pathinfo($contentFile); if ($this->getOption('macroExtension')==$pathInfo['extension']) { // do only include the files content if we didnt include it yet // just like 'include_once' only that it does it by default :-) // this only works if we are only using one instance of the filter, which is not the case // since every file might have different options, i.e. delimiters, so i changed // it to make a new instance for every file, which means this has almost no effect if (!in_array($contentFile,$this->_includedFiles)) { //print "including: $contentFile<br>"; $this->_includedFiles[] = $contentFile; // put an if around the entire macro file, so it wont even be parsed // if it is already once in the code, this takes care of not multiple // times defining functions (macros in this case) // it also works if you compile multiple files with different instances of this filter // since php checks the variable $___HTML_Template_Xipe_TagLib_includedFile given here // use isset to prevent E_ALL-warning $testVar = "\$___HTML_Template_Xipe_TagLib_includedFile['$fileToInclude']"; $fileContent = "$_openDel if(!isset($testVar) || !$testVar)\\\{ $_closeDel". $fileContent. $_openDel." \$___HTML_Template_Xipe_TagLib_includedFile['$fileToInclude']=true;\\\}".$_closeDel; } else { //print "already included: $contentFile<br>"; $fileContent = ''; } } // replace the string from $includes[0] with the file $input = preg_replace( '/'.preg_quote($includes[0][$index],'/').'/' , $fileContent , $input ); } } } } // end of if-any include-tags found } // end of while return $input; } /** * parses {%block xxx%} tags * DEPRECATED, use macro instead!!!! * * @version 01/12/18 * @author Wolfram Kriesing <wolfram@kriesing.de> * @param string $input the original template code * @return string the modified template */ function block( $input ) { // do somehow add the block-end tag first, use autoBraces, but needs modification first // for now you need to write the {%/block%} end tag $regExpToFindBlocks = '/{%\s*block\s+(.+)\s*%}.*{%\/\s*block\s*%}/Us'; // retreive the block names only, since a block might contain another block // by not getting the block content here we can also put blocks in blocks... preg_match_all( $regExpToFindBlocks , $input , $blocks ); if( sizeof($blocks[0])) { foreach( $blocks[1] as $index=>$aBlockName ) { // we trim the block name here, so we only get the real block name // and we dont have to add this 'no spaces' in the regExp $realBlockName = trim($aBlockName); // !!! // get the block content now, because it might containes another copy-tag !!! // which was replaced by the according block, write it in $blockContent $blockRegExp = '/{%\s*block\s+'.$aBlockName.'\s*%}(.*){%\/\s*block\s*%}/Us'; preg_match( $blockRegExp , $input , $blockContent ); // and replace the block definitions with nothing $input = preg_replace( $blockRegExp , '' , $input ); $this->blocks[$realBlockName] = $blockContent[1]; // we need to get the number of spaces before each '{%copy' to maintain indention preg_match_all( '/\n(\s*){%\s*copy\s+block\s+'.$realBlockName.'.*%}/' , $input , $copyTags ); // now we need to go thru every '{%copy' tag that has to be replaced and get its indention // to keep it in front, this adds the indention that is given in the block too !!! if(sizeof($copyTags[0])) foreach( $copyTags[0] as $cpIndex=>$aCopyTag ) { $indentedBlockContent = preg_replace( '/\n/' , "\n".$copyTags[1][$cpIndex] , $blockContent[1] ); $input = preg_replace( '/'.$copyTags[0][$cpIndex].'/' , $indentedBlockContent , $input ); } } } // go thru all blocks to replace copy-tags that are still left // in the first foreach we had only replaced copy tags which use blocks that // are defined in the same file if( isset($this->blocks) && sizeof($this->blocks) ) foreach( $this->blocks as $realBlockName=>$blockContent ) { // we need to get the number of spaces before each '{%copy' to maintain indention preg_match_all( '/\n(\s*){%\s*copy\s+block\s+'.$realBlockName.'.*%}/' , $input , $copyTags ); // now we need to go thru every '{%copy' tag that has to be replaced and get its indention // to keep it in front, this adds the indention that is given in the block too !!! if(sizeof($copyTags[0])) foreach( $copyTags[0] as $cpIndex=>$aCopyTag ) { $indentedBlockContent = preg_replace( '/\n/' , "\n".$copyTags[1][$cpIndex] , $blockContent ); $input = preg_replace( '/'.$copyTags[0][$cpIndex].'/' , $indentedBlockContent , $input ); } } // we have replaced all that was to replace, remove {%copy-tags // that were not replaced by anything $input = preg_replace( '/\n(\s*){%\s*copy\s+block\s+.*%}/' , '' , $input ); return $input; /* tested with {% block x %}<br> hi i am your first block even a line break i contain {%/block %} {%block this_block%} this is this block INSERTED<br> {%/block %} 1.<br> {%copy block this_block here %} <br><br> 2.<br> {% copy block x here %} <br><br> 3.<br> {% copy block this_block%} <br><br> 4.<br> {%copy block this_block %} <br><br> */ } /** * applies htmlentites to all the '{%$xxxx%}' strings, so the * printout will always be valid html * * @version 02/05/13 * @author Wolfram Kriesing <wolfram@kriesing.de> * @param string $input the original template code * @return string the modified input */ function applyHtmlEntites($input) { $input = preg_replace( '/'.preg_quote($this->options['delimiter'][0]). '%\$(.*)%'.preg_quote($this->options['delimiter'][1]). '/U' , '<?=htmlentities($$1)?>' , $input ); return $input; } /** * * * @version 02/06/21 * @author Wolfram Kriesing <wolfram@kriesing.de> * @param string $input the original template code * @return string the modified input" */ function macro( $input ) { $openDel = preg_quote($this->getOption('delimiter',0)); $closeDel = preg_quote($this->getOption('delimiter',1)); $_openDel = $this->getOption('delimiter',0); $_closeDel = $this->getOption('delimiter',1); // replace 'macro' with 'function' $regExp = '/'.$openDel.'%\s*(macro|function)\s+(.*)%'.$closeDel.'/Usi'; // if autoBraces is off, we need to put the opening-brace here :-) if ($this->getOption('autoBraces')) { $input = preg_replace( $regExp , $_openDel.'function $2'.$_closeDel , $input ); } else { $input = preg_replace( $regExp , $_openDel.'function $2 \\{ '.$_closeDel , $input ); } // replace {%macroName()%} with {macroName()} $regExp = '/'.$openDel.'\s*function\s+(.*)\(.*\)\s*(\\\{)?\s*'.$closeDel.'/Usi'; preg_match_all( $regExp , $input , $macroCalls ); // merge the macros found now with the macros already found // do this because we might have some macros which are not defined in the current file // but we assume, that all the files that are being processed by the same instance of this filter // are merged to one big php-file, so the macro will be defined and available! $this->_macros = array_unique(array_merge($this->_macros,$macroCalls[1])); if (sizeof($this->_macros)) { foreach ($this->_macros as $aMacroCall) { $regExp = '/'.$openDel.'%\s*'.trim($aMacroCall).'\s*(\(.*\))%'.$closeDel.'/Ui'; $input = preg_replace( $regExp , $_openDel.$aMacroCall.'$1'.$_closeDel , $input ); } } // if someone has autoBraces on and uses this.... well that would be a user-mistake, or a missing doc :-) $regExp = '/[^\\\]'.$openDel.'%\s*endmacro\s*%'.$closeDel.'/Ui'; $input = preg_replace( $regExp , $_openDel.' \\} '.$_closeDel , $input ); return $input; } function loop( $input ) { $input = $this->_replaceName( $input , 'while' ); $input = $this->_replaceName( $input , 'for' ); return $this->_replaceName( $input , 'foreach' ); } function condition( $input ) { return $this->_replaceName( $input , 'if' ); } function _replaceName( $input , $name ) { $openBrace = '{'; if( $this->getOption('delimiter',0) == '{' ) $openBrace = '\{'; $input = preg_replace( '/'.preg_quote($this->getOption('delimiter',0)). '%\s*'.$name.'\s*\((.*)\)\s*%'.preg_quote($this->getOption('delimiter',1)). '/Ui' , "<?php $name($1) $openBrace ?>" , $input ); return $input; } function end( $input ) { $closeBrace = '}'; if( $this->getOption('delimiter',0) == '{' ) $closeBrace = '\}'; $input = preg_replace( '/'.preg_quote($this->getOption('delimiter',0)). '%\s*end\s*%'.preg_quote($this->getOption('delimiter',1)). '/Umi' , "<?php $closeBrace ?>" , $input ); return $input; }}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?