Source for file Token.php
Documentation is available at Token.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 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: Alan Knowles <alan@akbkhome> |
// +----------------------------------------------------------------------+
// $Id: Token.php,v 1.36 2004/04/24 02:05:48 alan_k Exp $
// This is the master Token file for The New Token driver Engine.
// All the Token output, and building routines are in here.
// Note overriden methods are not documented unless they differ majorly from
$GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['base'] = 0;
$GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['state'] = 0;
$GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['statevars'] = array ();
$GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['activeForm'] = '';
$GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['tokens'] = array ();
$GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['gettextStrings'] = array ();
$GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['activeFormId'] = 0;
$GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['flexyIgnore'] = false;
* Base Class for all Tokens.
* @abstract Provides the static Create Method, and default toString() methods
class HTML_Template_Flexy_Token {
* the token type (Depreciated when we have classes for all tokens
* the default value (normally a string)
* the line the token is from
* Standard factory method.. - with object vars.
* ?? rename me to factory?
* @param string Token type
* @param mixed Initialization settings for token
* @param int line that the token is defined.
* @return object Created Object
function factory ($token,$value,$line) {
// try not to reload the same class to often
static $loaded = array ();
$c = 'HTML_Template_Flexy_Token_'. $token;
if (!class_exists ($c) && !isset ($loaded[$token])) {
// make sure parse errors are picked up - now @ here..
if (file_exists (dirname (__FILE__ )."/Token/{ $token}.php" )) {
require_once 'HTML/Template/Flexy/Token/'. $token. '.php';
$t = new HTML_Template_Flexy_Token;
if ($t->setValue ($value) === false ) {
// kick back error conditions..
* Standard Value iterpretor
* @param mixed value recieved from factory method
function setValue ($value) {
* compile to String (vistor method) replaces toString
function compile (&$compiler) {
return $compiler->toString ($this);
* compile children (visitor approach).
function compileChildren ( &$compiler) {
if ($this->ignoreChildren ) {
//echo "output $this->id";
//new Gtk_VarDump($this);
foreach ($this->children as $child) {
$add = $child->compile ($compiler);
if (is_a($add,'PEAR_Error')) {
/* ======================================================= */
/* Token Managmenet = parse and store all the tokens in
* an associative array and tree.
* Run a Tokenizer and Store its results
* It should build a DOM Tree of the HTML
* @param object Tokenizer to run.. - Theoretically other Tokenizers could be done for email,rtf etc.
* @return base token (really a dummy token, which contains the tree)
function buildTokens ($tokenizer)
global $_HTML_TEMPLATE_FLEXY_TOKEN;
// first record is a filler - to stick all the children on !
$_HTML_TEMPLATE_FLEXY_TOKEN['base'] = 0;
$_HTML_TEMPLATE_FLEXY_TOKEN['statevars'] = array ();
$_HTML_TEMPLATE_FLEXY_TOKEN['state'] = 0;
$_HTML_TEMPLATE_FLEXY_TOKEN['flexyIgnore'] = false;
if (@$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['flexyIgnore']) {
$_HTML_TEMPLATE_FLEXY_TOKEN['flexyIgnore'] = true;
$_HTML_TEMPLATE_FLEXY_TOKEN['activeFormId'] = 0;
$_HTML_TEMPLATE_FLEXY_TOKEN['activeForm'] = '';
$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'] = array (new HTML_Template_Flexy_Token );
$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][0 ]->id =0;
$_HTML_TEMPLATE_FLEXY_TOKEN['gettextStrings'] = array ();
// initialize state - this trys to make sure that
// you dont do to many elses etc.
//echo "RUNNING TOKENIZER";
// step one just tokenize it.
while ($t = $tokenizer->yylex ()) {
$err = "<PRE>" . $tokenizer->error;
return PEAR ::raiseError ('HTML_Template_Flexy::Syntax error in ".
"Template line:'. $tokenizer->yyline .
$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i] = $tokenizer->value;
$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->id = $i;
// this whole children thing needs rethinking
// - I think the body of the page should be wrapped: ..
// ?php if (!$this->bodyOnly) { .. <HTML> .... <BODY....> ?php } ?
if (isset ($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes ['FLEXYSTART'])) {
$_HTML_TEMPLATE_FLEXY_TOKEN['base'] = $i;
unset ($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes ['FLEXYSTART']);
if (isset ($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes ['FLEXYSTARTCHILDREN'])) {
$_HTML_TEMPLATE_FLEXY_TOKEN['base'] = $i;
$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->startChildren = true;
unset ($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes ['FLEXYSTARTCHILDREN']);
if (isset ($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes ['FLEXY:START'])) {
$_HTML_TEMPLATE_FLEXY_TOKEN['base'] = $i;
unset ($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes ['FLEXY:START']);
if (isset ($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes ['FLEXY:STARTCHILDREN'])) {
$_HTML_TEMPLATE_FLEXY_TOKEN['base'] = $i;
$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->startChildren = true;
unset ($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->ucAttributes ['FLEXY:STARTCHILDREN']);
//print_r($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]);
$res = &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'];
// DEBUG DUMPTING : foreach($res as $k) { $k->dump(); }
// merge variables into strings. so printf && gettext work.
for($i=1; $i< $total; $i++ ) {
if (!$res[$i]->isWord ()) {
$i = $res[$i]->forwardSearch ($total);
// connect open and close tags.
// this is done by having a stack for each of the tag types..
// then removing it when it finds the closing one
// <a href=""><img src=""></a>
// ends up with a stack for <a>'s and a stack for <img>'s
for($i=1; $i< $total; $i++ ) {
//echo "Checking TAG $i\n";
if ($res[$i]->tag {0 } == '/') { // it's a close tag..
//echo "GOT END TAG: {$res[$i]->tag}\n";
if (!isset ($stack[$tag]['pos'])) {
$npos = $stack[$tag]['pos'];
if (!isset ($stack[$tag][$npos])) {
//echo "CLOSING {$stack[$tag][$npos]}:{$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$stack[$tag][$npos]]->tag} WITH {$i}:{$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->tag}<BR>";
$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$stack[$tag][$npos]]->close = &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i];
// take it off the stack so no one else uses it!!!
unset ($stack[$tag][$npos]);
if ($stack[$tag]['pos'] < 0 ) {
// too many closes - just ignore it..
if (!isset ($stack[$tag])) {
$npos = $stack[$tag]['pos'] = 0;
$npos = ++ $stack[$tag]['pos'];
$stack[$tag][$npos] = $i;
// create a dummy close for the end
$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i] = new HTML_Template_Flexy_Token;
$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->id = $total;
$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][0 ]->close = &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$total];
// now is it possible to connect children...
// now we need to GLOBALIZE!! -
$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'] = $res;
HTML_Template_Flexy_Token ::buildChildren (0 );
//new Gtk_VarDump($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][0]);
return $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$_HTML_TEMPLATE_FLEXY_TOKEN['base']];
* Matching closing tag for a Token
* @var object|none optional closing tag
* array of children to each object.
* Build the child array for each element.
* @param int id of node to add children to.
function buildChildren ($id)
global $_HTML_TEMPLATE_FLEXY_TOKEN;
$base = &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$id];
$base->children = array ();
for ($i= $start; $i< $end; $i++ ) {
//echo "{$base->id}:{$base->tag} ADDING {$i}{$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->tag}<BR>";
//if ($base->id == 1176) {
// echo "<PRE>";print_r($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]);
$base->children [] = &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i];
if (isset ($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->close )) {
// if the close id is greater than my id - ignore it! -
if ($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->close ->id > $end) {
HTML_Template_Flexy_Token ::buildChildren ($i);
$i = $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->close ->id;
* Flag to ignore children - Used to block output for select/text area etc.
* may not be required as I moved the Tag parsing into the toString ph
* @var boolean ingore children
var $ignoreChildren = false;
/* ======================================================== */
/* variable STATE management
* raw variables are assumed to be $this->, unless defined by foreach..
* it also monitors syntax - eg. end without an if/foreach etc.
* tell the generator you are entering a block
global $_HTML_TEMPLATE_FLEXY_TOKEN;
$_HTML_TEMPLATE_FLEXY_TOKEN['state']++;
$s = $_HTML_TEMPLATE_FLEXY_TOKEN['state'];
$_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s] = array (); // initialize statevars
* tell the generator you are entering a block
* @return boolean parse error - out of bounds
global $_HTML_TEMPLATE_FLEXY_TOKEN;
$s = $_HTML_TEMPLATE_FLEXY_TOKEN['state'];
$_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s] = array (); // initialize statevars
$_HTML_TEMPLATE_FLEXY_TOKEN['state']--;
* get the real variable name formated x.y.z => $this->x->y->z
* if a variable is in the stack it return $x->y->z
* @return string PHP variable
// strip the quotes if it's only numbers..
$ret = $this->findVar ($parts[0 ]);
if (is_a($ret,'PEAR_Error')) {
* do the stack lookup on the variable
* t relates to the object being parsed.
* @return string PHP variable
function findVar ($string)
global $_HTML_TEMPLATE_FLEXY_TOKEN;
if (!$string || $string == 't') {
// accept global access on some string
if (@$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['globals'] &&
preg_match('/^(_POST|_GET|_REQUEST|_SESSION|_COOKIE|GLOBALS)\[/',$string)) {
if (!@$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['privates'] &&
return PEAR ::raiseError ('HTML_Template_Flexy::Attempt to access private variable:'.
" on line {$this->line } of {$GLOBALS['_HTML_TEMPLATE_FLEXY']['filename']}".
", Use options[privates] to allow this."
if ($p = strpos($string,'[')) {
$lookup = substr($string,0 ,$p);
for ($s = $_HTML_TEMPLATE_FLEXY_TOKEN['state']; $s > 0; $s-- ) {
if (in_array($lookup , $_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s])) {
* add a variable to the stack.
* @param string PHP variable
function pushVar ($string)
global $_HTML_TEMPLATE_FLEXY_TOKEN;
$s = $_HTML_TEMPLATE_FLEXY_TOKEN['state'];
$_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s][] = $string;
echo "{ $this->token }/" . (isset ($this->tag ) ? " <{$this->tag }>" : '') . " : {$this->value }\n";
Documentation generated on Mon, 11 Mar 2019 10:15:39 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|