HTML_Template_Flexy
[ class tree: HTML_Template_Flexy ] [ index: HTML_Template_Flexy ] [ all elements ]

Source for file Token.php

Documentation is available at Token.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors:  Alan Knowles <alan@akbkhome>                               |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Token.php,v 1.35 2004/04/21 08:39:29 alan_k Exp $
  20. //
  21. //  This is the master Token file for The New Token driver Engine.
  22. //  All the Token output, and building routines are in here.
  23. //
  24. //  Note overriden methods are not documented unless they differ majorly from
  25. //  The parent.
  26. //
  27. $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['base'= 0; 
  28. $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['state'= 0;
  29. $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['statevars'= array();
  30. $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['activeForm''';
  31. $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['tokens'= array();
  32. $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['gettextStrings'= array();
  33. $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['activeFormId'= 0;
  34. $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['flexyIgnore'= false;
  35. /**
  36. * Base Class for all Tokens.
  37. *
  38. @abstract Provides the static Create Method, and default toString() methods
  39. *
  40. */
  41.  
  42. class HTML_Template_Flexy_Token {
  43.     
  44.     /**
  45.     * the token type (Depreciated when we have classes for all tokens
  46.     *
  47.     * @var string 
  48.     * @access public
  49.     */
  50.     var $token;
  51.     /**
  52.     * the default value (normally a string)
  53.     *
  54.     * @var string 
  55.     * @access public
  56.     */
  57.     var $value;
  58.     /**
  59.     * the line the token is from
  60.     *
  61.     * @var int 
  62.     * @access public
  63.     */
  64.     
  65.     
  66.     var $line;
  67.  
  68.     /**
  69.     * factory a Token
  70.     *
  71.     * Standard factory method.. - with object vars.
  72.     * ?? rename me to factory?
  73.     * @param   string      Token type
  74.     * @param   mixed       Initialization settings for token
  75.     * @param   int   line that the token is defined.
  76.     * 
  77.     *
  78.     * @return   object    Created Object
  79.     * @access   public
  80.     */
  81.   
  82.     function factory($token,$value,$line{
  83.         // try not to reload the same class to often
  84.         static $loaded = array();
  85.         
  86.         
  87.         $c 'HTML_Template_Flexy_Token_'.$token;
  88.         
  89.         if (!class_exists($c&& !isset($loaded[$token])) {
  90.             // make sure parse errors are picked up - now @ here..
  91.             if (file_exists(dirname(__FILE__)."/Token/{$token}.php")) {
  92.                 require_once 'HTML/Template/Flexy/Token/'.$token.'.php';
  93.             }
  94.             $loaded[$token= true;
  95.         
  96.             
  97.         $t = new HTML_Template_Flexy_Token;
  98.         
  99.         if (class_exists($c)) {
  100.             $t = new $c;
  101.         }
  102.         $t->token = $token;
  103.         
  104.         if ($t->setValue($value=== false{
  105.             // kick back error conditions..
  106.             return false;
  107.         }
  108.         $t->line = $line;
  109.         
  110.         return $t;
  111.     }
  112.     
  113.     /**
  114.     * Standard Value iterpretor
  115.     *
  116.     * @param   mixed    value recieved from factory method
  117.  
  118.     * @return   none 
  119.     * @access   public
  120.     */
  121.   
  122.     function setValue($value{
  123.         $this->value = $value;
  124.     }
  125.  
  126.     
  127.     /**
  128.     * compile to String (vistor method) replaces toString
  129.     *
  130.     * @return   string   HTML
  131.     * @access   public
  132.     */
  133.     
  134.     function compile(&$compiler{
  135.         return $compiler->toString($this);
  136.     }
  137.     
  138.      
  139.     /**
  140.     * compile children (visitor approach).
  141.     *
  142.     * @return   string   HTML
  143.     * @access   public
  144.     */
  145.     function compileChildren&$compiler{
  146.          
  147.         if (!$this->children{
  148.             return '';
  149.         }
  150.         if ($this->ignoreChildren{
  151.             return;
  152.         }
  153.         $ret '';
  154.         //echo "output $this->id";
  155.         //new Gtk_VarDump($this);
  156.         foreach ($this->children as $child{
  157.             if (!$child{
  158.                 continue;
  159.             }
  160.             $add $child->compile($compiler);
  161.             if (is_a($add,'PEAR_Error')) {
  162.                 return $add;
  163.             }
  164.             $ret .= $add;
  165.         }
  166.         return $ret;
  167.     }
  168.     
  169.     
  170.  
  171.     
  172.     /* ======================================================= */
  173.     /* Token Managmenet = parse and store all the tokens in 
  174.      * an associative array and tree.
  175.      */
  176.    
  177.     /**
  178.     * Run a Tokenizer and Store its results
  179.     * It should build a DOM Tree of the HTML
  180.     * 
  181.     * @param   object    Tokenizer to run.. - Theoretically other Tokenizers could be done for email,rtf etc.
  182.     *
  183.     * @access   public
  184.     * @return   base token (really a dummy token, which contains the tree)
  185.     * @static
  186.     */
  187.   
  188.     function buildTokens($tokenizer
  189.     {
  190.     
  191.         global $_HTML_TEMPLATE_FLEXY_TOKEN;
  192.         
  193.         // first record is a filler - to stick all the children on !
  194.         // reset my globals..
  195.         $_HTML_TEMPLATE_FLEXY_TOKEN['base'= 0;
  196.         
  197.         $_HTML_TEMPLATE_FLEXY_TOKEN['statevars'= array();
  198.         $_HTML_TEMPLATE_FLEXY_TOKEN['state'= 0;
  199.         
  200.         $_HTML_TEMPLATE_FLEXY_TOKEN['flexyIgnore'= false;
  201.         if (@$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['flexyIgnore']{
  202.             $_HTML_TEMPLATE_FLEXY_TOKEN['flexyIgnore'= true;
  203.         }
  204.         $_HTML_TEMPLATE_FLEXY_TOKEN['activeFormId'= 0;
  205.         $_HTML_TEMPLATE_FLEXY_TOKEN['activeForm''';
  206.         
  207.         $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'= array(new HTML_Template_Flexy_Token);
  208.         $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][0]->id =0;
  209.         $_HTML_TEMPLATE_FLEXY_TOKEN['gettextStrings'= array();
  210.         $i=1;
  211.         
  212.         
  213.         // initialize state - this trys to make sure that
  214.         // you dont do to many elses etc.
  215.        
  216.         //echo "RUNNING TOKENIZER";
  217.         // step one just tokenize it.
  218.         while ($t $tokenizer->yylex()) {  
  219.             
  220.             if ($t == HTML_TEMPLATE_FLEXY_TOKEN_ERROR{
  221.                 //echo "ERROR";
  222.                 
  223.                 //print_r($tokenizer);
  224.                 $err "<PRE>" $tokenizer->error;
  225.                     htmlspecialchars(substr($tokenizer->yy_buffer,0,$tokenizer->yy_buffer_end)) 
  226.                     "<font color='red'>"htmlspecialchars(substr($tokenizer->yy_buffer,$tokenizer->yy_buffer_end,100)) 
  227.                     ".......</font></PRE>";
  228.                     
  229.                 return PEAR::raiseError('HTML_Template_Flexy::Syntax error in ".
  230.                     "Template line:'$tokenizer->yyline .
  231.                     $err
  232.                    HTML_TEMPLATE_FLEXY_ERROR_SYNTAX ,PEAR_ERROR_RETURN);
  233.             }
  234.             if ($t == HTML_TEMPLATE_FLEXY_TOKEN_NONE{
  235.                 continue;
  236.             }
  237.            
  238.             $i++;
  239.             $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i$tokenizer->value;
  240.             $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->id = $i;
  241.             
  242.             // this whole children thing needs rethinking 
  243.             // - I think the body of the page should be wrapped: ..
  244.             //  ?php if (!$this->bodyOnly) { .. <HTML> .... <BODY....>  ?php } ?
  245.             // 
  246.             
  247.             if (isset($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes['FLEXYSTART'])) {
  248.                 $_HTML_TEMPLATE_FLEXY_TOKEN['base'$i;
  249.                 unset($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes['FLEXYSTART']);
  250.             }
  251.             
  252.             if (isset($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes['FLEXYSTARTCHILDREN'])) {
  253.                 $_HTML_TEMPLATE_FLEXY_TOKEN['base'$i;
  254.                 $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->startChildren = true;
  255.                 unset($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes['FLEXYSTARTCHILDREN']);
  256.             }
  257.             
  258.             if (isset($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes['FLEXY:START'])) {
  259.                 $_HTML_TEMPLATE_FLEXY_TOKEN['base'$i;
  260.                 unset($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes['FLEXY:START']);
  261.             }
  262.             
  263.             if (isset($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes['FLEXY:STARTCHILDREN'])) {
  264.                 $_HTML_TEMPLATE_FLEXY_TOKEN['base'$i;
  265.                 $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->startChildren = true;
  266.                 unset($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes['FLEXY:STARTCHILDREN']);
  267.             }
  268.             
  269.             
  270.             //print_r($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]);
  271.              
  272.         }
  273.         //echo "BUILT TOKENS";
  274.         
  275.         $res &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'];
  276.         
  277.         $stack = array();
  278.         $total $i +1;
  279.         
  280.         
  281.         // merge variables into strings. so printf && gettext work.
  282.         
  283.         for($i=1;$i<$total;$i++{
  284.             if (!isset($res[$i])) {
  285.                 continue;
  286.             }
  287.             if (strtolower(get_class($res[$i])) == 'html_template_flexy_token_text'{
  288.                 if (!$res[$i]->isWord()) {
  289.                     continue;
  290.                 }
  291.                 $res[$i]->backSearch();
  292.                 $i $res[$i]->forwardSearch($total);
  293.             }
  294.         }
  295.          
  296.         
  297.         
  298.         // connect open  and close tags.
  299.         
  300.         // this is done by having a stack for each of the tag types..
  301.         // then removing it when it finds the closing one
  302.         // eg.
  303.         //  <a href=""><img src=""></a>
  304.         //  ends up with a stack for <a>'s and a stack for <img>'s
  305.         //
  306.         //
  307.         //
  308.         
  309.        
  310.         for($i=1;$i<$total;$i++{
  311.             //echo "Checking TAG $i\n";
  312.             if (!@$res[$i]->tag{
  313.                 continue;
  314.             }
  315.             if ($res[$i]->tag{0== '/'// it's a close tag..
  316.                 //echo "GOT END TAG: {$res[$i]->tag}\n";
  317.                 $tag strtoupper(substr($res[$i]->tag,1));
  318.                 if (!isset($stack[$tag]['pos'])) {
  319.                     continue; // unmatched
  320.                 }
  321.                 
  322.                 $npos $stack[$tag]['pos'];
  323.                 if (!isset($stack[$tag][$npos])) {
  324.                     // stack is empty!!!
  325.                     continue;
  326.                 }
  327.                 //echo "CLOSING {$stack[$tag][$npos]}:{$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$stack[$tag][$npos]]->tag} WITH {$i}:{$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->tag}<BR>";
  328.                 $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$stack[$tag][$npos]]->close = &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i];
  329.                 $stack[$tag]['pos']--;
  330.                 // take it off the stack so no one else uses it!!!
  331.                 unset($stack[$tag][$npos]);
  332.                 if ($stack[$tag]['pos'< 0{
  333.                     // too many closes - just ignore it..
  334.                     $stack[$tag]['pos'= 0;
  335.                 }
  336.                 continue;
  337.             }
  338.             // new entry on stack..
  339.             $tag strtoupper($res[$i]->tag);
  340.             
  341.             if (!isset($stack[$tag])) {
  342.                 $npos $stack[$tag]['pos'= 0;
  343.             else {
  344.                 $npos = ++$stack[$tag]['pos'];
  345.             }
  346.             
  347.             $stack[$tag][$npos$i;
  348.         }
  349.                 
  350.         // create a dummy close for the end
  351.         $i $total;
  352.         $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i= new HTML_Template_Flexy_Token;
  353.         $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->id = $total;
  354.         $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][0]->close = &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$total];
  355.         
  356.         // now is it possible to connect children...
  357.         // now we need to GLOBALIZE!! - 
  358.         
  359.         
  360.         $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'$res;
  361.         
  362.         HTML_Template_Flexy_Token::buildChildren(0);
  363.         //new Gtk_VarDump($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][0]);
  364.        
  365.         return $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$_HTML_TEMPLATE_FLEXY_TOKEN['base']];
  366.     }
  367.     /**
  368.     * Matching closing tag for a Token
  369.     *
  370.     * @var object|none optional closing tag
  371.     * @access public
  372.     */
  373.     
  374.   
  375.     var $close;
  376.            
  377.     /**
  378.     * array of children to each object.
  379.     *
  380.     * @var array 
  381.     * @access public|private
  382.     */
  383.     
  384.   
  385.     var $children = array();
  386.     
  387.     /**
  388.     * Build the child array for each element.
  389.     * RECURSIVE FUNCTION!!!!
  390.     * @param   int  id of node to add children to.
  391.     *
  392.     * @access   public
  393.     * @static
  394.     */
  395.     function buildChildren($id
  396.     {
  397.         global $_HTML_TEMPLATE_FLEXY_TOKEN;
  398.         
  399.         $base &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$id];
  400.         $base->children = array();
  401.         $start $base->id +1;
  402.         $end $base->close->id;
  403.         
  404.         for ($i=$start$i<$end$i++{
  405.             //echo "{$base->id}:{$base->tag} ADDING {$i}{$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->tag}<BR>";
  406.             //if ($base->id == 1176) {
  407.             //    echo "<PRE>";print_r($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]);
  408.             // }
  409.             $base->children[&$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i];
  410.             if (isset($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->close)) {
  411.             
  412.                 // if the close id is greater than my id - ignore it! - 
  413.                 if ($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->close->id > $end{
  414.                     continue;
  415.                 }
  416.                 HTML_Template_Flexy_Token::buildChildren($i);
  417.                 $i $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->close->id;
  418.             }
  419.         }
  420.     }
  421.             
  422.             
  423.             
  424.     /**
  425.     * Flag to ignore children - Used to block output for select/text area etc.
  426.     * may not be required as I moved the Tag parsing into the toString ph
  427.     *
  428.     * @var boolean ingore children
  429.     * @access public
  430.     */
  431.     
  432.   
  433.     var $ignoreChildren = false;
  434.     
  435.     
  436.     
  437.      
  438.     /* ======================================================== */
  439.     /* variable STATE management 
  440.     *
  441.     * raw variables are assumed to be $this->, unless defined by foreach..
  442.     * it also monitors syntax - eg. end without an if/foreach etc.
  443.     */
  444.  
  445.     
  446.     /**
  447.     * tell the generator you are entering a block
  448.     *
  449.     * @access   public
  450.     */
  451.     function pushState(
  452.     {
  453.         global $_HTML_TEMPLATE_FLEXY_TOKEN;
  454.         
  455.         $_HTML_TEMPLATE_FLEXY_TOKEN['state']++;
  456.         $s $_HTML_TEMPLATE_FLEXY_TOKEN['state'];
  457.         
  458.         $_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s= array()// initialize statevars
  459.     }
  460.     /**
  461.     * tell the generator you are entering a block
  462.     *
  463.     * @return  boolean  parse error - out of bounds
  464.     * @access   public
  465.     */
  466.     function pullState(
  467.     {
  468.         global $_HTML_TEMPLATE_FLEXY_TOKEN;
  469.         
  470.         $s $_HTML_TEMPLATE_FLEXY_TOKEN['state'];
  471.         $_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s= array()// initialize statevars
  472.         $_HTML_TEMPLATE_FLEXY_TOKEN['state']--;
  473.         if ($s<0{
  474.             return false;
  475.         }
  476.         return true;
  477.     }
  478.      /**
  479.     * get the real variable name formated x.y.z => $this->x->y->z
  480.     * if  a variable is in the stack it return $x->y->z
  481.     *
  482.     * @return  string PHP variable
  483.     * @access   public
  484.     */
  485.     
  486.     function toVar($s{
  487.         // wrap [] with quotes.
  488.         $s str_replace('[',"['",$s);
  489.         $s str_replace('%5b',"['",$s);
  490.         $s str_replace('%5B',"['",$s);
  491.         $s str_replace(']',"']",$s);
  492.         $s str_replace('%5d',"']",$s);
  493.         $s str_replace('%5D',"']",$s);
  494.         // strip the quotes if it's only numbers..
  495.         $s preg_replace("/'([0-9]+)'/""\\1",$s);
  496.         
  497.         $parts explode(".",$s);
  498.         
  499.         
  500.         $ret =  $this->findVar($parts[0]);
  501.         if (is_a($ret,'PEAR_Error')) {
  502.             return $ret;
  503.         }
  504.         
  505.         array_shift($parts);
  506.         
  507.         if (!count($parts)) {
  508.             return $ret;
  509.         }
  510.         foreach($parts as $p{
  511.             $ret .= "->{$p}";
  512.         }
  513.         return $ret;
  514.     }
  515.     /**
  516.     * do the stack lookup on the variable
  517.     * this relates to flexy
  518.     * t relates to the object being parsed.
  519.     *
  520.     * @return  string PHP variable
  521.     * @access   public
  522.     */
  523.     
  524.     function findVar($string
  525.     {
  526.         global $_HTML_TEMPLATE_FLEXY_TOKEN;
  527.     
  528.         if (!$string || $string == 't'{
  529.             return '$t';
  530.         }
  531.         if ($string == 'this'{
  532.             return '$this';
  533.         }
  534.         // accept global access on some string
  535.         if (@$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['globals'&&
  536.             preg_match('/^(_POST|_GET|_REQUEST|_SESSION|_COOKIE|GLOBALS)\[/',$string)) {
  537.             return '$'.$string;
  538.         
  539.         if (!@$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['privates'&&
  540.                 ($string{0== '_')) {
  541.                 return PEAR::raiseError('HTML_Template_Flexy::Attempt to access private variable:'.
  542.                     " on line {$this->line} of {$GLOBALS['_HTML_TEMPLATE_FLEXY']['filename']}".
  543.                     ", Use options[privates] to allow this."
  544.                    HTML_TEMPLATE_FLEXY_ERROR_SYNTAX ,PEAR_ERROR_RETURN);
  545.         }
  546.         
  547.         $lookup $string;
  548.         if ($p strpos($string,'[')) {
  549.             $lookup substr($string,0,$p);
  550.         }
  551.         
  552.         
  553.         for ($s $_HTML_TEMPLATE_FLEXY_TOKEN['state']$s > 0; $s--{
  554.             if (in_array($lookup $_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s])) {
  555.                 return '$'.$string;
  556.             }
  557.         }
  558.         return '$t->'.$string;
  559.     }
  560.     /**
  561.     * add a variable to the stack.
  562.     *
  563.     * @param  string PHP variable
  564.     * @access   public
  565.     */
  566.     
  567.     function pushVar($string
  568.     {
  569.         global $_HTML_TEMPLATE_FLEXY_TOKEN;
  570.         $s $_HTML_TEMPLATE_FLEXY_TOKEN['state'];
  571.         $_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s][$string;
  572.     }
  573.     
  574.      
  575. }

Documentation generated on Mon, 11 Mar 2019 10:15:16 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.