.
[corvix.git] / var / deb-package / cluster_raw_old / opt / cluster / lib / www / ganglia / class.TemplatePower.inc.php
blob424d5b4b663d7442d7ef20995a19e10c01fa3f09
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
4 // | TemplatePower: |
5 // | offers you the ability to separate your PHP code and your HTML |
6 // +----------------------------------------------------------------------+
7 // | |
8 // | Copyright (C) 2001,2002 R.P.J. Velzeboer, The Netherlands |
9 // | |
10 // | This program is free software; you can redistribute it and/or |
11 // | modify it under the terms of the GNU General Public License |
12 // | as published by the Free Software Foundation; either version 2 |
13 // | of the License, or (at your option) any later version. |
14 // | |
15 // | This program is distributed in the hope that it will be useful, |
16 // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 // | GNU General Public License for more details. |
19 // | |
20 // | You should have received a copy of the GNU General Public License |
21 // | along with this program; if not, write to the Free Software |
22 // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
23 // | 02111-1307, USA. |
24 // | |
25 // | Author: R.P.J. Velzeboer, rovel@codocad.nl The Netherlands |
26 // | |
27 // +----------------------------------------------------------------------+
28 // | http://templatepower.codocad.com |
29 // +----------------------------------------------------------------------+
31 // $Id: class.TemplatePower.inc.php 459 2004-12-13 19:01:46Z sacerdoti $
33 define("T_BYFILE", 0);
34 define("T_BYVAR", 1);
36 define("TP_ROOTBLOCK", '_ROOT');
38 class TemplatePowerParser
40 var $tpl_base; //Array( [filename/varcontent], [T_BYFILE/T_BYVAR] )
41 var $tpl_include; //Array( [filename/varcontent], [T_BYFILE/T_BYVAR] )
42 var $tpl_count;
44 var $parent = Array(); // $parent[{blockname}] = {parentblockname}
45 var $defBlock = Array();
47 var $rootBlockName;
48 var $ignore_stack;
50 var $version;
52 /**
53 * TemplatePowerParser::TemplatePowerParser()
55 * @param $tpl_file
56 * @param $type
57 * @return
59 * @access private
61 function TemplatePowerParser( $tpl_file, $type )
63 $this->version = '3.0.1';
65 $this->tpl_base = Array( $tpl_file, $type );
66 $this->tpl_count = 0;
67 $this->ignore_stack = Array( false );
70 /**
71 * TemplatePowerParser::__errorAlert()
73 * @param $message
74 * @return
76 * @access private
78 function __errorAlert( $message )
80 print( '<br>'. $message .'<br>\r\n');
83 /**
84 * TemplatePowerParser::__prepare()
86 * @return
88 * @access private
90 function __prepare()
92 $this->defBlock[ TP_ROOTBLOCK ] = Array();
93 $tplvar = $this->__prepareTemplate( $this->tpl_base[0], $this->tpl_base[1] );
95 $initdev["varrow"] = 0;
96 $initdev["coderow"] = 0;
97 $initdev["index"] = 0;
98 $initdev["ignore"] = false;
100 $this->__parseTemplate( $tplvar, TP_ROOTBLOCK, $initdev );
101 $this->__cleanUp();
105 * TemplatePowerParser::__cleanUp()
107 * @return
109 * @access private
111 function __cleanUp()
113 for( $i=0; $i <= $this->tpl_count; $i++ )
115 $tplvar = 'tpl_rawContent'. $i;
116 unset( $this->{$tplvar} );
121 * TemplatePowerParser::__prepareTemplate()
123 * @param $tpl_file
124 * @param $type
125 * @return
127 * @access private
129 function __prepareTemplate( $tpl_file, $type )
131 $tplvar = 'tpl_rawContent'. $this->tpl_count;
133 if( $type == T_BYVAR )
135 $this->{$tplvar}["content"] = preg_split('/\n/', $tpl_file, -1, PREG_SPLIT_DELIM_CAPTURE);
137 else
139 $this->{$tplvar}["content"] = @file( $tpl_file ) or
140 die( $this->__errorAlert('TemplatePower Error: Couldn\'t open [ '. $tpl_file .' ]!'));
143 $this->{$tplvar}["size"] = sizeof( $this->{$tplvar}["content"] );
145 $this->tpl_count++;
147 return $tplvar;
151 * TemplatePowerParser::__parseTemplate()
153 * @param $tplvar
154 * @param $blockname
155 * @param $initdev
156 * @return
158 * @access private
160 function __parseTemplate( $tplvar, $blockname, $initdev )
162 $coderow = $initdev["coderow"];
163 $varrow = $initdev["varrow"];
164 $index = $initdev["index"];
165 $ignore = $initdev["ignore"];
167 while( $index < $this->{$tplvar}["size"] )
169 if ( preg_match('/<!--[ ]?(START|END) IGNORE -->/', $this->{$tplvar}["content"][$index], $ignreg) )
171 if( $ignreg[1] == 'START')
173 //$ignore = true;
174 array_push( $this->ignore_stack, true );
176 else
178 //$ignore = false;
179 array_pop( $this->ignore_stack );
182 else
184 if( !end( $this->ignore_stack ) )
186 if( preg_match('/<!--[ ]?(START|END|INCLUDE|INCLUDESCRIPT|REUSE) BLOCK : (.+)-->/', $this->{$tplvar}["content"][$index], $regs))
188 //remove trailing and leading spaces
189 $regs[2] = trim( $regs[2] );
191 if( $regs[1] == 'INCLUDE')
193 $include_defined = true;
195 //check if the include file is assigned
196 if( isset( $this->tpl_include[ $regs[2] ]) )
198 $tpl_file = $this->tpl_include[ $regs[2] ][0];
199 $type = $this->tpl_include[ $regs[2] ][1];
201 else
202 if (file_exists( $regs[2] )) //check if defined as constant in template
204 $tpl_file = $regs[2];
205 $type = T_BYFILE;
207 else
209 $include_defined = false;
212 if( $include_defined )
214 //initialize startvalues for recursive call
215 $initdev["varrow"] = $varrow;
216 $initdev["coderow"] = $coderow;
217 $initdev["index"] = 0;
218 $initdev["ignore"] = false;
220 $tplvar2 = $this->__prepareTemplate( $tpl_file, $type );
221 $initdev = $this->__parseTemplate( $tplvar2, $blockname, $initdev );
223 $coderow = $initdev["coderow"];
224 $varrow = $initdev["varrow"];
227 else
228 if( $regs[1] == 'INCLUDESCRIPT' )
230 $include_defined = true;
232 //check if the includescript file is assigned by the assignInclude function
233 if( isset( $this->tpl_include[ $regs[2] ]) )
235 $include_file = $this->tpl_include[ $regs[2] ][0];
236 $type = $this->tpl_include[ $regs[2] ][1];
238 else
239 if (file_exists( $regs[2] )) //check if defined as constant in template
241 $include_file = $regs[2];
242 $type = T_BYFILE;
244 else
246 $include_defined = false;
249 if( $include_defined )
251 ob_start();
253 if( $type == T_BYFILE )
255 if( !@include_once( $include_file ) )
257 $this->__errorAlert( 'TemplatePower Error: Couldn\'t include script [ '. $include_file .' ]!' );
258 exit();
261 else
263 eval( "?>" . $include_file );
266 $this->defBlock[$blockname]["_C:$coderow"] = ob_get_contents();
267 $coderow++;
269 ob_end_clean();
272 else
273 if( $regs[1] == 'REUSE' )
275 //do match for 'AS'
276 if (preg_match('/(.+) AS (.+)/', $regs[2], $reuse_regs))
278 $originalbname = trim( $reuse_regs[1] );
279 $copybname = trim( $reuse_regs[2] );
281 //test if original block exist
282 if (isset( $this->defBlock[ $originalbname ] ))
284 //copy block
285 $this->defBlock[ $copybname ] = $this->defBlock[ $originalbname ];
287 //tell the parent that he has a child block
288 $this->defBlock[ $blockname ]["_B:". $copybname ] = '';
290 //create index and parent info
291 $this->index[ $copybname ] = 0;
292 $this->parent[ $copybname ] = $blockname;
294 else
296 $this->__errorAlert('TemplatePower Error: Can\'t find block \''. $originalbname .'\' to REUSE as \''. $copybname .'\'');
299 else
301 //so it isn't a correct REUSE tag, save as code
302 $this->defBlock[$blockname]["_C:$coderow"] = $this->{$tplvar}["content"][$index];
303 $coderow++;
306 else
308 if( $regs[2] == $blockname ) //is it the end of a block
310 break;
312 else //its the start of a block
314 //make a child block and tell the parent that he has a child
315 $this->defBlock[ $regs[2] ] = Array();
316 $this->defBlock[ $blockname ]["_B:". $regs[2]] = '';
318 //set some vars that we need for the assign functions etc.
319 $this->index[ $regs[2] ] = 0;
320 $this->parent[ $regs[2] ] = $blockname;
322 //prepare for the recursive call
323 $index++;
324 $initdev["varrow"] = 0;
325 $initdev["coderow"] = 0;
326 $initdev["index"] = $index;
327 $initdev["ignore"] = false;
329 $initdev = $this->__parseTemplate( $tplvar, $regs[2], $initdev );
331 $index = $initdev["index"];
335 else //is it code and/or var(s)
337 //explode current template line on the curly bracket '{'
338 $sstr = explode( '{', $this->{$tplvar}["content"][$index] );
340 reset( $sstr );
342 if (current($sstr) != '')
344 //the template didn't start with a '{',
345 //so the first element of the array $sstr is just code
346 $this->defBlock[$blockname]["_C:$coderow"] = current( $sstr );
347 $coderow++;
350 while (next($sstr))
352 //find the position of the end curly bracket '}'
353 $pos = strpos( current($sstr), "}" );
355 if ( ($pos !== false) && ($pos > 0) )
357 //a curly bracket '}' is found
358 //and at least on position 1, to eliminate '{}'
360 //note: position 1 taken without '{', because we did explode on '{'
362 $strlength = strlen( current($sstr) );
363 $varname = substr( current($sstr), 0, $pos );
365 if (strstr( $varname, ' ' ))
367 //the varname contains one or more spaces
368 //so, it isn't a variable, save as code
369 $this->defBlock[$blockname]["_C:$coderow"] = '{'. current( $sstr );
370 $coderow++;
372 else
374 //save the variable
375 $this->defBlock[$blockname]["_V:$varrow" ] = $varname;
376 $varrow++;
378 //is there some code after the varname left?
379 if( ($pos + 1) != $strlength )
381 //yes, save that code
382 $this->defBlock[$blockname]["_C:$coderow"] = substr( current( $sstr ), ($pos + 1), ($strlength - ($pos + 1)) );
383 $coderow++;
387 else
389 //no end curly bracket '}' found
390 //so, the curly bracket is part of the text. Save as code, with the '{'
391 $this->defBlock[$blockname]["_C:$coderow"] = '{'. current( $sstr );
392 $coderow++;
397 else
399 $this->defBlock[$blockname]["_C:$coderow"] = $this->{$tplvar}["content"][$index];
400 $coderow++;
404 $index++;
407 $initdev["varrow"] = $varrow;
408 $initdev["coderow"] = $coderow;
409 $initdev["index"] = $index;
411 return $initdev;
416 * TemplatePowerParser::version()
418 * @return
419 * @access public
421 function version()
423 return $this->version;
427 * TemplatePowerParser::assignInclude()
429 * @param $iblockname
430 * @param $value
431 * @param $type
433 * @return
435 * @access public
437 function assignInclude( $iblockname, $value, $type=T_BYFILE )
439 $this->tpl_include["$iblockname"] = Array( $value, $type );
443 class TemplatePower extends TemplatePowerParser
445 var $index = Array(); // $index[{blockname}] = {indexnumber}
446 var $content = Array();
448 var $currentBlock;
449 var $showUnAssigned;
450 var $serialized;
451 var $globalvars = Array();
452 var $prepared;
455 * TemplatePower::TemplatePower()
457 * @param $tpl_file
458 * @param $type
459 * @return
461 * @access public
463 function TemplatePower( $tpl_file='', $type= T_BYFILE )
465 TemplatePowerParser::TemplatePowerParser( $tpl_file, $type );
467 $this->prepared = false;
468 $this->showUnAssigned = false;
469 $this->serialized = false; //added: 26 April 2002
473 * TemplatePower::__deSerializeTPL()
475 * @param $stpl_file
476 * @param $tplvar
477 * @return
479 * @access private
481 function __deSerializeTPL( $stpl_file, $type )
483 if( $type == T_BYFILE )
485 $serializedTPL = @file( $stpl_file ) or
486 die( $this->__errorAlert('TemplatePower Error: Can\'t open [ '. $stpl_file .' ]!'));
488 else
490 $serializedTPL = $stpl_file;
493 $serializedStuff = unserialize( join ('', $serializedTPL) );
495 $this->defBlock = $serializedStuff["defBlock"];
496 $this->index = $serializedStuff["index"];
497 $this->parent = $serializedStuff["parent"];
501 * TemplatePower::__makeContentRoot()
503 * @return
505 * @access private
507 function __makeContentRoot()
509 $this->content[ TP_ROOTBLOCK ."_0" ][0] = Array( TP_ROOTBLOCK );
510 $this->currentBlock = &$this->content[ TP_ROOTBLOCK ."_0" ][0];
514 * TemplatePower::__assign()
516 * @param $varname
517 * @param $value
518 * @return
520 * @access private
522 function __assign( $varname, $value)
524 if( sizeof( $regs = explode('.', $varname ) ) == 2 ) //this is faster then preg_match
526 $ind_blockname = $regs[0] .'_'. $this->index[ $regs[0] ];
528 $lastitem = sizeof( $this->content[ $ind_blockname ] );
530 $lastitem > 1 ? $lastitem-- : $lastitem = 0;
532 $block = &$this->content[ $ind_blockname ][ $lastitem ];
533 $varname = $regs[1];
535 else
537 $block = &$this->currentBlock;
540 $block["_V:$varname"] = $value;
545 * TemplatePower::__assignGlobal()
547 * @param $varname
548 * @param $value
549 * @return
551 * @access private
553 function __assignGlobal( $varname, $value )
555 $this->globalvars[ $varname ] = $value;
560 * TemplatePower::__outputContent()
562 * @param $blockname
563 * @return
565 * @access private
567 function __outputContent( $blockname )
569 $numrows = sizeof( $this->content[ $blockname ] );
571 for( $i=0; $i < $numrows; $i++)
573 $defblockname = $this->content[ $blockname ][$i][0];
575 for( reset( $this->defBlock[ $defblockname ]); $k = key( $this->defBlock[ $defblockname ]); next( $this->defBlock[ $defblockname ] ) )
577 if ($k[1] == 'C')
579 print( $this->defBlock[ $defblockname ][$k] );
581 else
582 if ($k[1] == 'V')
584 $defValue = $this->defBlock[ $defblockname ][$k];
586 if( !isset( $this->content[ $blockname ][$i][ "_V:". $defValue ] ) )
588 if( isset( $this->globalvars[ $defValue ] ) )
590 $value = $this->globalvars[ $defValue ];
592 else
594 if( $this->showUnAssigned )
596 //$value = '{'. $this->defBlock[ $defblockname ][$k] .'}';
597 $value = '{'. $defValue .'}';
599 else
601 $value = '';
605 else
607 $value = $this->content[ $blockname ][$i][ "_V:". $defValue ];
610 print( $value );
613 else
614 if ($k[1] == 'B')
616 if( isset( $this->content[ $blockname ][$i][$k] ) )
618 $this->__outputContent( $this->content[ $blockname ][$i][$k] );
625 function __printVars()
627 var_dump($this->defBlock);
628 print("<br>--------------------<br>");
629 var_dump($this->content);
633 /**********
634 public members
635 ***********/
638 * TemplatePower::serializedBase()
640 * @return
642 * @access public
644 function serializedBase()
646 $this->serialized = true;
647 $this->__deSerializeTPL( $this->tpl_base[0], $this->tpl_base[1] );
651 * TemplatePower::showUnAssigned()
653 * @param $state
654 * @return
656 * @access public
658 function showUnAssigned( $state = true )
660 $this->showUnAssigned = $state;
664 * TemplatePower::prepare()
666 * @return
668 * @access public
670 function prepare()
672 if (!$this->serialized)
674 TemplatePowerParser::__prepare();
677 $this->prepared = true;
679 $this->index[ TP_ROOTBLOCK ] = 0;
680 $this->__makeContentRoot();
684 * TemplatePower::newBlock()
686 * @param $blockname
687 * @return
689 * @access public
691 function newBlock( $blockname )
693 $parent = &$this->content[ $this->parent[$blockname] .'_'. $this->index[$this->parent[$blockname]] ];
695 $lastitem = sizeof( $parent );
696 $lastitem > 1 ? $lastitem-- : $lastitem = 0;
698 $ind_blockname = $blockname .'_'. $this->index[ $blockname ];
700 if ( !isset( $parent[ $lastitem ]["_B:$blockname"] ))
702 //ok, there is no block found in the parentblock with the name of {$blockname}
704 //so, increase the index counter and create a new {$blockname} block
705 $this->index[ $blockname ] += 1;
707 $ind_blockname = $blockname .'_'. $this->index[ $blockname ];
709 if (!isset( $this->content[ $ind_blockname ] ) )
711 $this->content[ $ind_blockname ] = Array();
714 //tell the parent where his (possible) children are located
715 $parent[ $lastitem ]["_B:$blockname"] = $ind_blockname;
718 //now, make a copy of the block defenition
719 $blocksize = sizeof( $this->content[ $ind_blockname ] );
721 $this->content[ $ind_blockname ][ $blocksize ] = Array( $blockname );
723 //link the current block to the block we just created
724 $this->currentBlock = &$this->content[ $ind_blockname ][ $blocksize ];
728 * TemplatePower::assignGlobal()
730 * @param $varname
731 * @param $value
732 * @return
734 * @access public
736 function assignGlobal( $varname, $value )
738 if (is_array( $varname ))
740 foreach($varname as $var => $value)
742 $this->__assignGlobal( $var, $value );
745 else
747 $this->__assignGlobal( $varname, $value );
753 * TemplatePower::assign()
755 * @param $varname
756 * @param $value
757 * @return
759 * @access public
761 function assign( $varname, $value='' )
763 if (is_array( $varname ))
765 foreach($varname as $var => $value)
767 $this->__assign( $var, $value );
770 else
772 $this->__assign( $varname, $value );
777 * TemplatePower::gotoBlock()
779 * @param $blockname
780 * @return
782 * @access public
784 function gotoBlock( $blockname )
786 if ( isset( $this->defBlock[ $blockname ] ) )
788 $ind_blockname = $blockname .'_'. $this->index[ $blockname ];
790 //get lastitem indexnumber
791 $lastitem = sizeof( $this->content[ $ind_blockname ] );
793 $lastitem > 1 ? $lastitem-- : $lastitem = 0;
795 //link the current block
796 $this->currentBlock = &$this->content[ $ind_blockname ][ $lastitem ];
801 * TemplatePower::getVarValue()
803 * @param $varname
804 * @return
806 * @access public
808 function getVarValue( $varname )
810 if( sizeof( $regs = explode('.', $varname ) ) == 2 ) //this is faster then preg_match
812 $ind_blockname = $regs[0] .'_'. $this->index[ $regs[0] ];
814 $lastitem = sizeof( $this->content[ $ind_blockname ] );
816 $lastitem > 1 ? $lastitem-- : $lastitem = 0;
818 $block = &$this->content[ $ind_blockname ][ $lastitem ];
819 $varname = $regs[1];
821 else
823 $block = &$this->currentBlock;
826 return $block["_V:$varname"];
830 * TemplatePower::printToScreen()
832 * @return
834 * @access public
836 function printToScreen()
838 if ($this->prepared)
840 $this->__outputContent( TP_ROOTBLOCK .'_0' );
842 else
844 $this->__errorAlert('TemplatePower Error: Template isn\'t prepared!');
849 * TemplatePower::getOutputContent()
851 * @return
853 * @access public
855 function getOutputContent()
857 ob_start();
859 $this->printToScreen();
861 $content = ob_get_contents();
863 ob_end_clean();
865 return $content;