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

Documentation generated on Mon, 11 Mar 2019 14:05:26 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.