' . htmlspecialchars(print_R($res,true));//exit;
//echo '';
for($i=1;$i<$total;$i++) {
if (empty($res[$i]->tag)) {
continue;
}
//echo "Checking TAG $i {$res[$i]->tag}\n";
if ($res[$i]->tag{0} == '/') { // it's a close tag..
//echo "GOT END TAG: {$res[$i]->tag}\n";
$tag = strtoupper(substr($res[$i]->tag,1));
if (!count($stack)) {
continue;
}
$ssc = count($stack) - 1;
/* go up the stack trying to find a match */
for($s = $ssc; $s > -1; $s--) {
if (!isset($stack[$s])) {
echo "MISSED STACK? $s
";print_r($stack);exit;
}
if (!isset($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$stack[$s]])) {
echo "STACKED BAD OFFSET : {$stack[$s]}
";print_r($stack);exit;
}
$tok = &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$stack[$s]];
if (strtoupper($tok->tag) == $tag) {
// got the matching closer..
// echo "MATCH: {$i}:{$tok->tag}({$tok->line}) to {$stack[$s]}:$tag
";
$tok->close = &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i];
array_splice($stack,$s);
//print_R($stack);
break;
}
}
continue;
}
$stack[] = $i;
// tag with no closer (or unmatched in stack..)
}
// create a dummy close for the end
$i = $total;
$_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]);
//echo '';print_R($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$_HTML_TEMPLATE_FLEXY_TOKEN['base']] );
return $_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$_HTML_TEMPLATE_FLEXY_TOKEN['base']];
}
/**
* Matching closing tag for a Token
*
* @var object|none optional closing tag
* @access public
*/
var $close;
/**
* array of children to each object.
*
* @var array
* @access public|private
*/
var $children = array();
/**
* Build the child array for each element.
* RECURSIVE FUNCTION!!!!
* @param int id of node to add children to.
*
* @access public
* @static
*/
static function buildChildren($id)
{
global $_HTML_TEMPLATE_FLEXY_TOKEN;
$base = &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$id];
$base->children = array();
$start = $base->id +1;
$end = $base->close->id;
for ($i=$start; $i<$end; $i++) {
//echo "{$base->id}:{$base->tag} ADDING {$i}{$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->tag}
";
//if ($base->id == 1176) {
// echo "";print_r($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]);
// }
$base->children[] = &$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i];
if (isset($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]) &&
is_object($_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) {
continue;
}
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
* @access public
*/
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
*
* @access public
*/
function pushState()
{
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
* @access public
*/
function pullState()
{
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']--;
if ($s<0) {
return false;
}
return true;
}
/**
* 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
* @access public
*/
function toVar($s) {
// wrap [] with quotes.
$s = str_replace('[',"['",$s);
$s = str_replace('%5b',"['",$s);
$s = str_replace('%5B',"['",$s);
$s = str_replace(']',"']",$s);
$s = str_replace('%5d',"']",$s);
$s = str_replace('%5D',"']",$s);
// strip the quotes if it's only numbers..
$s = preg_replace("/'([-]?[0-9]+)'/", "\\1",$s);
$parts = explode(".",$s);
$ret = $this->findVar($parts[0]);
if (is_object($ret) && is_a($ret,'PEAR_Error')) {
return $ret;
}
array_shift($parts);
if (!count($parts)) {
return $ret;
}
foreach($parts as $p) {
$ret .= "->{$p}";
}
return $ret;
}
/**
* do the stack lookup on the variable
* this relates to flexy
* t relates to the object being parsed.
*
* @return string PHP variable
* @access public
*/
function findVar($string)
{
global $_HTML_TEMPLATE_FLEXY_TOKEN;
if (!$string || $string == 't') {
return '$t';
}
if ($string == 'this') {
return '$this';
}
// accept global access on some string
if (@$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['globals'] &&
preg_match('/^(_POST|_GET|_REQUEST|_SESSION|_COOKIE|GLOBALS)\[/',$string)) {
return '$'.$string;
}
if (!@$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['privates'] &&
($string{0} == '_')) {
return HTML_Template_Flexy::staticRaiseError('HTML_Template_Flexy::Attempt to access private variable:'.
" on line {$this->line} of {$GLOBALS['_HTML_TEMPLATE_FLEXY']['filename']}".
", Use options[privates] to allow this."
, HTML_TEMPLATE_FLEXY_ERROR_SYNTAX ,HTML_TEMPLATE_FLEXY_ERROR_RETURN);
}
$lookup = $string;
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])) {
return '$'.$string;
}
}
return '$t->'.$string;
}
/**
* add a variable to the stack.
*
* @param string PHP variable
* @access public
*/
function pushVar($string)
{
if (empty($string)) {
return;
}
global $_HTML_TEMPLATE_FLEXY_TOKEN;
$s = $_HTML_TEMPLATE_FLEXY_TOKEN['state'];
$_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s][] = $string;
}
/**
* get the scoped variables as an array of strings so that it can
* be passed to child templates...
*
*
* @access public
*/
function scopeVarsToArrayString()
{
global $_HTML_TEMPLATE_FLEXY_TOKEN;
$ar = array();
for ($s = $_HTML_TEMPLATE_FLEXY_TOKEN['state']; $s > 0; $s--) {
$ar = array_merge($ar, $_HTML_TEMPLATE_FLEXY_TOKEN['statevars'][$s]);
}
if (empty($ar)) {
return 'array()';
}
return "array('". implode("','", $ar). "')";
}
/**
* dump to text ATM
*
*
* @access public
*/
function dump() {
echo "{$this->token}/" . (isset($this->tag) ? "<{$this->tag}>" : '') . ": {$this->value}\n";
}
}