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.45 2004/12/17 06:29:02 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.         
  288.         // merge variables into strings. so printf && gettext work.
  289.         
  290.         for($i=1;$i<$total;$i++{
  291.             if (!isset($res[$i])) {
  292.                 continue;
  293.             }
  294.             if (strtolower(get_class($res[$i])) == 'html_template_flexy_token_text'{
  295.                 if (!$res[$i]->isWord()) {
  296.                     continue;
  297.                 }
  298.                 
  299.                 $res[$i]->backSearch($res);
  300.                 $i $res[$i]->forwardSearch($res);
  301.             }
  302.         }
  303.         
  304.         
  305.         
  306.         // connect open  and close tags.
  307.         
  308.         // this is done by having a stack for each of the tag types..
  309.         // then removing it when it finds the closing one
  310.         // eg.
  311.         //  <a href=""><img src=""></a>
  312.         //  ends up with a stack for <a>'s and a stack for <img>'s
  313.         //
  314.         //
  315.         //
  316.         
  317.         
  318.         //echo '<PRE>' . htmlspecialchars(print_R($res,true));//exit;
  319.        
  320.         for($i=1;$i<$total;$i++{
  321.             //echo "Checking TAG $i\n";
  322.             if (empty($res[$i]->tag)) {
  323.                 continue;
  324.             }
  325.             if ($res[$i]->tag{0== '/'// it's a close tag..
  326.                 //echo "GOT END TAG: {$res[$i]->tag}\n";
  327.                 $tag strtoupper(substr($res[$i]->tag,1));
  328.                 if (!count($stack)) {
  329.                     continue;
  330.                 }
  331.                 $ssc count($stack- 1;
  332.                 /* go up the stack trying to find a match */
  333.                 for($s $ssc$s > -1; $s--{
  334.                     if (!isset($stack[$s])) {
  335.                         echo "MISSED STACK? $s<BR><PRE>";print_r($stack);exit;
  336.                     }
  337.                     if (!isset($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$stack[$s]])) {
  338.                         echo "STACKED BAD OFFSET : {$stack[$s]}<BR><PRE>";print_r($stack);exit;
  339.                     }
  340.                     $tok =  &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$stack[$s]];
  341.                     if (strtoupper($tok->tag== $tag{
  342.                         // got the matching closer..
  343.                         // echo "MATCH: {$i}:{$tok->tag}({$tok->line}) to  {$stack[$s]}:$tag<BR>";
  344.                         $tok->close = &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i];
  345.                         
  346.                         array_splice($stack,$s);
  347.                         //print_R($stack);
  348.                          
  349.                         break;
  350.                     }
  351.                 }
  352.                 continue;
  353.             }
  354.             $stack[$i;
  355.             // tag with no closer (or unmatched in stack..)
  356.         
  357.       
  358.                 
  359.         // create a dummy close for the end
  360.         $i $total;
  361.         $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i= new HTML_Template_Flexy_Token;
  362.         $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->id = $total;
  363.         $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][0]->close = &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$total];
  364.         
  365.         // now is it possible to connect children...
  366.         // now we need to GLOBALIZE!! - 
  367.         
  368.         
  369.         $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'$res;
  370.         
  371.         HTML_Template_Flexy_Token::buildChildren(0);
  372.         //new Gtk_VarDump($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][0]);
  373.         //echo '<PRE>';print_R($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$_HTML_TEMPLATE_FLEXY_TOKEN['base']]  ); 
  374.         return $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$_HTML_TEMPLATE_FLEXY_TOKEN['base']];
  375.     }
  376.     /**
  377.     * Matching closing tag for a Token
  378.     *
  379.     * @var object|none optional closing tag
  380.     * @access public
  381.     */
  382.     
  383.   
  384.     var $close;
  385.            
  386.     /**
  387.     * array of children to each object.
  388.     *
  389.     * @var array 
  390.     * @access public|private
  391.     */
  392.     
  393.   
  394.     var $children = array();
  395.     
  396.     /**
  397.     * Build the child array for each element.
  398.     * RECURSIVE FUNCTION!!!!
  399.     * @param   int  id of node to add children to.
  400.     *
  401.     * @access   public
  402.     * @static
  403.     */
  404.     function buildChildren($id
  405.     {
  406.         global $_HTML_TEMPLATE_FLEXY_TOKEN;
  407.         
  408.         $base &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$id];
  409.         $base->children = array();
  410.         $start $base->id +1;
  411.         $end $base->close->id;
  412.         
  413.         for ($i=$start$i<$end$i++{
  414.             //echo "{$base->id}:{$base->tag} ADDING {$i}{$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->tag}<BR>";
  415.             //if ($base->id == 1176) {
  416.             //    echo "<PRE>";print_r($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]);
  417.             // }
  418.          
  419.             $base->children[&$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i];
  420.             
  421.             if (is_object($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->close)) {
  422.             
  423.                 // if the close id is greater than my id - ignore it! - 
  424.                 if ($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->close->id > $end{
  425.                     continue;
  426.                 }
  427.                 HTML_Template_Flexy_Token::buildChildren($i);
  428.                 $i $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->close->id;
  429.             }
  430.         }
  431.     }
  432.             
  433.             
  434.             
  435.     /**
  436.     * Flag to ignore children - Used to block output for select/text area etc.
  437.     * may not be required as I moved the Tag parsing into the toString ph
  438.     *
  439.     * @var boolean ingore children
  440.     * @access public
  441.     */
  442.     
  443.   
  444.     var $ignoreChildren = false;
  445.     
  446.     
  447.     
  448.      
  449.     /* ======================================================== */
  450.     /* variable STATE management 
  451.     *
  452.     * raw variables are assumed to be $this->, unless defined by foreach..
  453.     * it also monitors syntax - eg. end without an if/foreach etc.
  454.     */
  455.  
  456.     
  457.     /**
  458.     * tell the generator you are entering a block
  459.     *
  460.     * @access   public
  461.     */
  462.     function pushState(
  463.     {
  464.         global $_HTML_TEMPLATE_FLEXY_TOKEN;
  465.         
  466.         $_HTML_TEMPLATE_FLEXY_TOKEN['state']++;
  467.         $s $_HTML_TEMPLATE_FLEXY_TOKEN['state'];
  468.         
  469.         $_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s= array()// initialize statevars
  470.     }
  471.     /**
  472.     * tell the generator you are entering a block
  473.     *
  474.     * @return  boolean  parse error - out of bounds
  475.     * @access   public
  476.     */
  477.     function pullState(
  478.     {
  479.         global $_HTML_TEMPLATE_FLEXY_TOKEN;
  480.         
  481.         $s $_HTML_TEMPLATE_FLEXY_TOKEN['state'];
  482.         $_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s= array()// initialize statevars
  483.         $_HTML_TEMPLATE_FLEXY_TOKEN['state']--;
  484.         if ($s<0{
  485.             return false;
  486.         }
  487.         return true;
  488.     }
  489.      /**
  490.     * get the real variable name formated x.y.z => $this->x->y->z
  491.     * if  a variable is in the stack it return $x->y->z
  492.     *
  493.     * @return  string PHP variable
  494.     * @access   public
  495.     */
  496.     
  497.     function toVar($s{
  498.         // wrap [] with quotes.
  499.         $s str_replace('[',"['",$s);
  500.         $s str_replace('%5b',"['",$s);
  501.         $s str_replace('%5B',"['",$s);
  502.         $s str_replace(']',"']",$s);
  503.         $s str_replace('%5d',"']",$s);
  504.         $s str_replace('%5D',"']",$s);
  505.         // strip the quotes if it's only numbers..
  506.         $s preg_replace("/'([-]?[0-9]+)'/""\\1",$s);
  507.         
  508.         $parts explode(".",$s);
  509.         
  510.         
  511.         $ret =  $this->findVar($parts[0]);
  512.         if (is_a($ret,'PEAR_Error')) {
  513.             return $ret;
  514.         }
  515.         
  516.         array_shift($parts);
  517.         
  518.         if (!count($parts)) {
  519.             return $ret;
  520.         }
  521.         foreach($parts as $p{
  522.             $ret .= "->{$p}";
  523.         }
  524.         return $ret;
  525.     }
  526.     /**
  527.     * do the stack lookup on the variable
  528.     * this relates to flexy
  529.     * t relates to the object being parsed.
  530.     *
  531.     * @return  string PHP variable
  532.     * @access   public
  533.     */
  534.     
  535.     function findVar($string
  536.     {
  537.         global $_HTML_TEMPLATE_FLEXY_TOKEN;
  538.     
  539.         if (!$string || $string == 't'{
  540.             return '$t';
  541.         }
  542.         if ($string == 'this'{
  543.             return '$this';
  544.         }
  545.         // accept global access on some string
  546.         if (@$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['globals'&&
  547.             preg_match('/^(_POST|_GET|_REQUEST|_SESSION|_COOKIE|GLOBALS)\[/',$string)) {
  548.             return '$'.$string;
  549.         
  550.         if (!@$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['privates'&&
  551.                 ($string{0== '_')) {
  552.                 return HTML_Template_Flexy::raiseError('HTML_Template_Flexy::Attempt to access private variable:'.
  553.                     " on line {$this->line} of {$GLOBALS['_HTML_TEMPLATE_FLEXY']['filename']}".
  554.                     ", Use options[privates] to allow this."
  555.                    HTML_TEMPLATE_FLEXY_ERROR_SYNTAX ,HTML_TEMPLATE_FLEXY_ERROR_RETURN);
  556.         }
  557.         
  558.         $lookup $string;
  559.         if ($p strpos($string,'[')) {
  560.             $lookup substr($string,0,$p);
  561.         }
  562.         
  563.         
  564.         for ($s $_HTML_TEMPLATE_FLEXY_TOKEN['state']$s > 0; $s--{
  565.             if (in_array($lookup $_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s])) {
  566.                 return '$'.$string;
  567.             }
  568.         }
  569.         return '$t->'.$string;
  570.     }
  571.     /**
  572.     * add a variable to the stack.
  573.     *
  574.     * @param  string PHP variable
  575.     * @access   public
  576.     */
  577.     
  578.     function pushVar($string
  579.     {
  580.         global $_HTML_TEMPLATE_FLEXY_TOKEN;
  581.         $s $_HTML_TEMPLATE_FLEXY_TOKEN['state'];
  582.         $_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s][$string;
  583.     }
  584.     
  585.      /**
  586.     * dump to text ATM
  587.     *
  588.     * 
  589.     * @access   public
  590.     */
  591.     
  592.     function dump({
  593.         echo "{$this->token}/" . (isset($this->tag? "<{$this->tag}>" : ''. ": {$this->value}\n";
  594.     }
  595.     
  596.     
  597.     
  598.     
  599. }

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