taglib.php

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

PHP
619
字号
<?php//// +----------------------------------------------------------------------+// | PHP Version 4                                                        |// +----------------------------------------------------------------------+// | Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 The PHP Group |// +----------------------------------------------------------------------+// | This source file is subject to version 2.02 of the PHP license,      |// | that is bundled with this package in the file LICENSE, and is        |// | available at through the world-wide-web at                           |// | http://www.php.net/license/2_02.txt.                                 |// | If you did not receive a copy of the PHP license and are unable to   |// | obtain it through the world-wide-web, please send a note to          |// | license@php.net so we can mail you a copy immediately.               |// +----------------------------------------------------------------------+// | Authors: Wolfram Kriesing <wolfram@kriesing.de>                      |// +----------------------------------------------------------------------+//  $Id: TagLib.php,v 1.22 2003/04/01 17:46:58 cain Exp $//require_once 'HTML/Template/Xipe/Options.php';/***   this file is intended to realize stuff like this*   - add custom tags, which are no PHP (but replaced by it)!!! therefore they go like this: {%...%} where { and } are the delimiters*       {%repeat $x times%}, {%repeat $x times, loopname=$loopCounter%}, replace by a simple for loop, if*       loopname is given use this as the loop varibale name**       {%copy block x here%} this replaces a defined block which is somewhere in the file*           {%block x%}*       {%include directory/file.tpl %}  this might define different blocks, which can be copied by using the above tag {%copy ...%}**       {%strip whitespaces%}*       {%strip%}**       {%trim $x after 20 characters and add '...'%}*       {%trim $x 20 '...'%}**       {%trim $x by words after 20 characters and add '...'%}*       {%trim $x by words 20 '...'%}**   @package    HTML_Template_Xipe*   @version    01/12/15*/class HTML_Template_Xipe_Filter_TagLib extends HTML_Template_Xipe_Options{// i need the method setOption, that's why i extend myPEAR_Common    /**    *   for passing values to the class, i.e. like the delimiters    *   @access private    *   @var    array   $options    the options for initializing the filter class    */    var $options = array(   'delimiter'     =>  array()      // first value of the array is the begin delimiter, second the end delimiter                            ,'templateDir'   =>  ''          // we need the template dir for the include directive                            ,'templateExtension'=>  ''                            ,'macroExtension'=>     ''                            ,'autoBraces'   =>      false                        );// remove the constructor one day, i feel that passing the delimiters to this class makes it all somehow unclean// but therefore we have to move addIfBeforeForeach too, since it depends on having the delimiters    /**    *   @var    array   all the files that get included    */    var $_includedFiles = array();    /**    *   @var    array   all the macros that are defined    */    var $_macros = array();    /**    *   actually i made a constructor only to pass the delimiters to this class    *   at a definite point    *    *   @version    01/12/15    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      array   $options    need to be given, use the options from your tempalte class    */    function HTML_Template_Xipe_Filter_TagLib($options=array())    {        if(sizeof($options))            foreach( $options as $key=>$aOption )                $this->setOption( $key , $aOption );    }    /**    *   apply all filters available in this class    *   thanks to hint from Alan Knowles    *    *   @version    02/05/22    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      string  the actual input string, to which the filters will be applied    *   @return     string  the resulting string    */    function allPrefilters( $input )    {        $input = $this->includeFile($input);        $input = $this->block($input);        // do block and include before other tags, so the other tags also work        // when they were used in a block !!!        $input = $this->macro($input);        // do trim words before trim!! so trim doesnt catch the tag first :-)        $input = $this->trimByWords($input);        $input = $this->trim($input);        $input = $this->repeat($input);        $input = $this->applyHtmlEntites($input);        $input = $this->loop($input);        $input = $this->condition($input);        $input = $this->end($input);        return $input;    }    /**    *   NOT IMPLEMENTED, AND I WONT    *   removes spaces and new lines    *   ACTUALLY i think this is unnecessary, simply use filters trimLines and optimizeHtml, this    *   does everything, at least it works perfect for me    *    *   @version    01/12/15    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      string  $input  the original template code    *   @return     string  the modified template    */    function strip( $input )    {        return $input;    }    /**    *   {%repeat $x times%}, {%repeat $x times using $loopCounter%}, replace by a simple for loop, if    *   a variable is given use this as the loop varibale name    *    *   @version    01/12/15    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      string  $input  the original template code    *   @return     string  the modified template    */    function repeat( $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);        $autoBraces = $this->getOption('autoBraces');                // if someone has autoBraces on and uses this.... well that would be a user-mistake, or a missing doc :-)        $regExp = '/[^\\\]'.$openDel.'%\s*endrepeat\s*%'.$closeDel.'/Ui';        $input = preg_replace( $regExp , $_openDel.($autoBraces?' \\} ':' endfor ').$_closeDel , $input );                        // find those repeats which dont have no variable that is given as the loop variable        // we need to do this, since the next regExp needs this variable name, because        // we can not use the $5 to check if it is given (down there in the second regExp)... bummer        $counterName = '$_someUniqueVariableName';  // generate something here        $input = preg_replace(  '/'.$openDel.                                '%\s*repeat\s+([^\s%]+)([^\$]*)%'.$closeDel.                                '/',                                //"PRE-REPEAT:<br>1='$1'<br>2='$2'<br>3='$3'<br>4='$4'<br>5='$5'<br>" , // for testing                                $_openDel.                                "%repeat $1 $counterName%".                                $_closeDel,                                $input);        $input = preg_replace(  '/\n(.*)'.          // save the indention in $1                                $openDel.                                '%\s*repeat\s+'.    // find begin delimiter repeat at least one space behind and variable spaces before                                '([^\s]+)'.         // find everything until the next space, which is the count variable $2                                '(([^\$%]*)?(\$[^\s]+)?)?'. // find the loop varibale name $5, a lot of stuff around it is optional (?)                                                    // the variable name has to start with a $ and spaces are excluded, so we trim it too                                '\s*%'.             // optional numbner of spaces before closing delimiter                                $closeDel.                                '/',                                "\n$1".$_openDel.                                "for($5=0;$5<$2;$5++)".($autoBraces?'':':').                                $_closeDel,                                //"REPEAT:<br>1='$1'<br>2='$2'<br>3='$3'<br>4='$4'<br>5='$5'<br>6='$6'<br>" , // for testing                                $input);  // replace unnecessary spaces, so the next regexp is shorter and easier                        return $input;        /* TESTS        { $xx->methodCall=7}        { $x=1}        { $x1=1}        { $x2=1}        { $x_y=1}        { $variableName_howEver_Long_it_mig111htBe=1}        { $x4=1}        <!--{%repeat $x->methodCall($easyVar)%} this works too, but i am too lazy to declare a class here-->        {%repeat $xx->methodCall%}            repeat 1        <br>        {%repeat sizeof($x)%}            repeat 2        <br>        {%repeat $x times%}            repeat 3        <br>        {%repeat $x1 times $y1%}            repeat 4        <br>        {%    repeat     $x2    times    $y2   %}            repeat 5        <br>        {%repeat $x_y times using $y%}            repeat 6        <br>        {%repeat $variableName_howEver_Long_it_mig111htBe times with $y3%}            repeat 7        <br>        {%repeat sizeof($x4) times $y4%}            repeat 8        <br>        */    }    /**    *   trims strings after X characters and adds a given string, if given    *   use as PRE filter    *   @todo   the length can not be a variable yet, do this someday    *    *   tested with    *   {$x1='What a long string'}<br>    *   {$x2='I am here '}<br>    *   {$that->fuck='He ho'}<br>    *   <br><br>    *   1. {%trim $x1 after 5 characters and add "JUST simple ..."%}<br>    *   2. {%trim $x2 3 "REPLACE with this"%}<br>    *   3. {%   trim    $that->fuck fucking off unitl it dies after no more than  200 characters ,kajdfa sdkjas dlkjas dfkjasdf lksjd fksjdf lksjdf lksjd flkj l reaplce with ""%}    *   <br>    *   4. {%trim $x2 3%}<br>    *   5. {%trim $x2  after 3 letters %}<br>    *   6. {%trim   $x2  3   %}<br>    *   7. {%trim $x2 to the length of 3%}<br>    *    *   @version    01/12/18    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      string  $input  the original template code    *   @param      string  this is an extra string which can be added behind trim, is used i.e. for "trim words"    *   @return     string  the modified template    */    function trim( $input , $extra='' )    {        $exp =  $this->options['delimiter'][0].                'echo ((strlen($1) > $2))?(substr($1,0,$2)."$4"):$1'.                $this->options['delimiter'][1];        if ($extra == 'by words') {            $exp =  $this->options['delimiter'][0].                    'echo ((strlen($1) > $2))?(substr($1,0,(($2)-(strlen(strrchr(substr($1,0,$2),\' \')))))."$4"):$1'.                    $this->options['delimiter'][1];            $extra = '\s+by\s+words';        }        return preg_replace(    '/'.preg_quote($this->options['delimiter'][0]).                                '%\s*trim\s+'.      // find at least one space behind and any number of spaces between % and trim                                '([^\s]+)'.         // find all until the next space, that will be our variable name $1                                $extra.                                '[^\d]+'.           // find anything until a decimal number comes, at least one character                                '(\d+)'.            // put the decimal number in $2                                '(\s+.*"(.*)")?'.   // this is saucy, we only need the most inner pair of (),                                                    // that will be our string we use to add at the end in case we trim it                                                    // all those other () are only for making each block optional (?), esp. for test 5 to work                                '\s*%'.preg_quote($this->options['delimiter'][1]).                                                    // allow any kind of spaces before the end delimiter                                '/i' ,              // search case insensitive                                $exp,                                //"TRIM:<br>1='$1'<br>2='$2'<br>3='$3'<br>4='$4'<br>5='$5'<br>" , // for testing                                $input );    }    /**    *   this trims strings but only after a space    *   NOTE: be sure to put this filter before "trim"    *    *   @version    02/05/30    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      string  $input  the original template code    *   @param      string  this is an extra string which can be added behind trim, is used i.e. for "trim words"    *   @return     string  the modified template    */    function trimByWords( $input )    {        return $this->trim( $input , 'by words' );    }    /**    *   {%include xxx.tagLib%}    *    *   @version    01/12/18    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      string  $input  the original template code    *   @return     string  the modified template

⌨️ 快捷键说明

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