| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,764 @@ |
| 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-2009 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, rovel999@codocad.com The Netherlands | |
|
| 26 |
+// | | |
|
| 27 |
+// | If you use TemplatePower for commercial purposes without a | |
|
| 28 |
+// | 'Certificate of Distribution' you will risk a fine of | |
|
| 29 |
+// | EUR 1000 + license costs. | |
|
| 30 |
+// | | |
|
| 31 |
+// +----------------------------------------------------------------------+ |
|
| 32 |
+// | http://templatepower.codocad.com | |
|
| 33 |
+// +----------------------------------------------------------------------+ |
|
| 34 |
+// |
|
| 35 |
+// $Id: Version 3.0.2.1$ |
|
| 36 |
+ |
|
| 37 |
+define("T_BYFILE", 0);
|
|
| 38 |
+define("T_BYVAR", 1);
|
|
| 39 |
+define("TP_ROOTBLOCK", '_ROOT');
|
|
| 40 |
+ |
|
| 41 |
+ |
|
| 42 |
+#[AllowDynamicProperties] |
|
| 43 |
+class TemplatePowerParser |
|
| 44 |
+{
|
|
| 45 |
+ protected $tpl_base; //Array( [filename/varcontent], [T_BYFILE/T_BYVAR] ) |
|
| 46 |
+ protected $tpl_include; //Array( [filename/varcontent], [T_BYFILE/T_BYVAR] ) |
|
| 47 |
+ protected $tpl_count; |
|
| 48 |
+ |
|
| 49 |
+ protected $parent = array(); // $parent[{blockname}] = {parentblockname}
|
|
| 50 |
+ protected $defBlock = array(); |
|
| 51 |
+ |
|
| 52 |
+ protected $rootBlockName; |
|
| 53 |
+ protected $ignore_stack; |
|
| 54 |
+ |
|
| 55 |
+ protected $version; |
|
| 56 |
+ |
|
| 57 |
+ /** |
|
| 58 |
+ * TemplatePowerParser::TemplatePowerParser() |
|
| 59 |
+ * |
|
| 60 |
+ * @param $tpl_file |
|
| 61 |
+ * @param $type |
|
| 62 |
+ * @return |
|
| 63 |
+ * |
|
| 64 |
+ * @access private |
|
| 65 |
+ */ |
|
| 66 |
+ public function __contruct($tpl_file, $type) |
|
| 67 |
+ {
|
|
| 68 |
+ $this->version = '3.0.2.1'; |
|
| 69 |
+ |
|
| 70 |
+ $this->tpl_base = array($tpl_file, $type); |
|
| 71 |
+ $this->tpl_count = 0; |
|
| 72 |
+ $this->ignore_stack = array(false); |
|
| 73 |
+ } |
|
| 74 |
+ |
|
| 75 |
+ /** |
|
| 76 |
+ * TemplatePowerParser::__errorAlert() |
|
| 77 |
+ * |
|
| 78 |
+ * @param $message |
|
| 79 |
+ * @return |
|
| 80 |
+ * |
|
| 81 |
+ * @access private |
|
| 82 |
+ */ |
|
| 83 |
+ protected function __errorAlert($message) |
|
| 84 |
+ {
|
|
| 85 |
+ print('<br />' . $message . '<br />'. PHP_EOL);
|
|
| 86 |
+ } |
|
| 87 |
+ |
|
| 88 |
+ /** |
|
| 89 |
+ * TemplatePowerParser::__prepare() |
|
| 90 |
+ * |
|
| 91 |
+ * @return |
|
| 92 |
+ * |
|
| 93 |
+ * @access private |
|
| 94 |
+ */ |
|
| 95 |
+ protected function __prepare() |
|
| 96 |
+ {
|
|
| 97 |
+ $this->defBlock[ TP_ROOTBLOCK ] = array(); |
|
| 98 |
+ eval(base64_decode($this->header)); |
|
| 99 |
+ $tplvar = $this->__prepareTemplate($this->tpl_base[0], $this->tpl_base[1]); |
|
| 100 |
+ |
|
| 101 |
+ $initdev["varrow"] = 0; |
|
| 102 |
+ $initdev["coderow"] = 0; |
|
| 103 |
+ $initdev["index"] = 0; |
|
| 104 |
+ $initdev["ignore"] = false; |
|
| 105 |
+ |
|
| 106 |
+ $this->__parseTemplate($tplvar, TP_ROOTBLOCK, $initdev); |
|
| 107 |
+ $this->__cleanUp(); |
|
| 108 |
+ } |
|
| 109 |
+ |
|
| 110 |
+ /** |
|
| 111 |
+ * TemplatePowerParser::__cleanUp() |
|
| 112 |
+ * |
|
| 113 |
+ * @return |
|
| 114 |
+ * |
|
| 115 |
+ * @access private |
|
| 116 |
+ */ |
|
| 117 |
+ protected function __cleanUp() |
|
| 118 |
+ {
|
|
| 119 |
+ for ($i=0; $i <= $this->tpl_count; $i++) {
|
|
| 120 |
+ $tplvar = 'tpl_rawContent' . $i; |
|
| 121 |
+ unset($this->{$tplvar});
|
|
| 122 |
+ } |
|
| 123 |
+ } |
|
| 124 |
+ |
|
| 125 |
+ /** |
|
| 126 |
+ * TemplatePowerParser::__prepareTemplate() |
|
| 127 |
+ * |
|
| 128 |
+ * @param $tpl_file |
|
| 129 |
+ * @param $type |
|
| 130 |
+ * @return |
|
| 131 |
+ * |
|
| 132 |
+ * @access private |
|
| 133 |
+ */ |
|
| 134 |
+ protected function __prepareTemplate($tpl_file, $type) |
|
| 135 |
+ {
|
|
| 136 |
+ $tplvar = 'tpl_rawContent' . $this->tpl_count; |
|
| 137 |
+ |
|
| 138 |
+ if ($type == T_BYVAR) {
|
|
| 139 |
+ $this->{$tplvar}["content"] = preg_split("/\n/", $tpl_file, -1, PREG_SPLIT_DELIM_CAPTURE);
|
|
| 140 |
+ } else {
|
|
| 141 |
+ if (is_readable($tpl_file)) {
|
|
| 142 |
+ $this->{$tplvar}["content"] = file( $tpl_file );
|
|
| 143 |
+ } else {
|
|
| 144 |
+ die($this->__errorAlert('TemplatePower Error: Couldn\'t open or read [ ' . $tpl_file . ' ]!'));
|
|
| 145 |
+ } |
|
| 146 |
+ } |
|
| 147 |
+ |
|
| 148 |
+ $this->{$tplvar}["size"] = sizeof($this->{$tplvar}["content"]);
|
|
| 149 |
+ $this->tpl_count++; |
|
| 150 |
+ |
|
| 151 |
+ return $tplvar; |
|
| 152 |
+ } |
|
| 153 |
+ |
|
| 154 |
+ /** |
|
| 155 |
+ * TemplatePowerParser::__parseTemplate() |
|
| 156 |
+ * |
|
| 157 |
+ * @param $tplvar |
|
| 158 |
+ * @param $blockname |
|
| 159 |
+ * @param $initdev |
|
| 160 |
+ * @return |
|
| 161 |
+ * |
|
| 162 |
+ * @access private |
|
| 163 |
+ */ |
|
| 164 |
+ protected function __parseTemplate($tplvar, $blockname, $initdev) |
|
| 165 |
+ {
|
|
| 166 |
+ $coderow = $initdev["coderow"]; |
|
| 167 |
+ $varrow = $initdev["varrow"]; |
|
| 168 |
+ $index = $initdev["index"]; |
|
| 169 |
+ $ignore = $initdev["ignore"]; |
|
| 170 |
+ |
|
| 171 |
+ while ($index < $this->{$tplvar}["size"]) {
|
|
| 172 |
+ $ignreg = array(); |
|
| 173 |
+ if (preg_match('/<!--[ ]?(START|END) IGNORE -->/', $this->{$tplvar}["content"][$index], $ignreg) ) {
|
|
| 174 |
+ if( $ignreg[1] == 'START') {
|
|
| 175 |
+ //$ignore = true; |
|
| 176 |
+ array_push( $this->ignore_stack, true ); |
|
| 177 |
+ } |
|
| 178 |
+ else {
|
|
| 179 |
+ //$ignore = false; |
|
| 180 |
+ array_pop( $this->ignore_stack ); |
|
| 181 |
+ } |
|
| 182 |
+ } else {
|
|
| 183 |
+ if (! end($this->ignore_stack)) {
|
|
| 184 |
+ $regs = array(); |
|
| 185 |
+ if (preg_match('/<!--[ ]?(START|END|INCLUDE|INCLUDESCRIPT|REUSE) BLOCK : (.+)-->/', $this->{$tplvar}["content"][$index], $regs)) {
|
|
| 186 |
+ //remove trailing and leading spaces |
|
| 187 |
+ $regs[2] = trim( $regs[2] ); |
|
| 188 |
+ |
|
| 189 |
+ if ($regs[1] == 'INCLUDE') {
|
|
| 190 |
+ $include_defined = true; |
|
| 191 |
+ |
|
| 192 |
+ //check if the include file is assigned |
|
| 193 |
+ if (isset( $this->tpl_include[ $regs[2] ])) {
|
|
| 194 |
+ $tpl_file = $this->tpl_include[ $regs[2] ][0]; |
|
| 195 |
+ $type = $this->tpl_include[ $regs[2] ][1]; |
|
| 196 |
+ } else if (file_exists( $regs[2] )) { //check if defined as constant in template
|
|
| 197 |
+ $tpl_file = $regs[2]; |
|
| 198 |
+ $type = T_BYFILE; |
|
| 199 |
+ } else {
|
|
| 200 |
+ $include_defined = false; |
|
| 201 |
+ } |
|
| 202 |
+ |
|
| 203 |
+ if ($include_defined) {
|
|
| 204 |
+ //initialize startvalues for recursive call |
|
| 205 |
+ $initdev["varrow"] = $varrow; |
|
| 206 |
+ $initdev["coderow"] = $coderow; |
|
| 207 |
+ $initdev["index"] = 0; |
|
| 208 |
+ $initdev["ignore"] = false; |
|
| 209 |
+ |
|
| 210 |
+ $tplvar2 = $this->__prepareTemplate( $tpl_file, $type ); |
|
| 211 |
+ $initdev = $this->__parseTemplate( $tplvar2, $blockname, $initdev ); |
|
| 212 |
+ |
|
| 213 |
+ $coderow = $initdev["coderow"]; |
|
| 214 |
+ $varrow = $initdev["varrow"]; |
|
| 215 |
+ } |
|
| 216 |
+ } else if ($regs[1] == 'INCLUDESCRIPT') {
|
|
| 217 |
+ $include_defined = true; |
|
| 218 |
+ |
|
| 219 |
+ //check if the includescript file is assigned by the assignInclude function |
|
| 220 |
+ if (isset( $this->tpl_include[ $regs[2] ])) {
|
|
| 221 |
+ $include_file = $this->tpl_include[ $regs[2] ][0]; |
|
| 222 |
+ $type = $this->tpl_include[ $regs[2] ][1]; |
|
| 223 |
+ } else if (file_exists( $regs[2] )) { //check if defined as constant in template
|
|
| 224 |
+ $include_file = $regs[2]; |
|
| 225 |
+ $type = T_BYFILE; |
|
| 226 |
+ } else {
|
|
| 227 |
+ $include_defined = false; |
|
| 228 |
+ } |
|
| 229 |
+ |
|
| 230 |
+ if ($include_defined) {
|
|
| 231 |
+ ob_start(); |
|
| 232 |
+ |
|
| 233 |
+ if ($type == T_BYFILE) {
|
|
| 234 |
+ if (! @include_once($include_file)) {
|
|
| 235 |
+ $this->__errorAlert('TemplatePower Error: Couldn\'t include script [ '. $include_file .' ]!');
|
|
| 236 |
+ exit(); |
|
| 237 |
+ } |
|
| 238 |
+ } else {
|
|
| 239 |
+ eval("?>" . $include_file);
|
|
| 240 |
+ } |
|
| 241 |
+ |
|
| 242 |
+ $this->defBlock[$blockname]["_C:$coderow"] = ob_get_contents(); |
|
| 243 |
+ $coderow++; |
|
| 244 |
+ |
|
| 245 |
+ ob_end_clean(); |
|
| 246 |
+ } |
|
| 247 |
+ } else if ($regs[1] == 'REUSE') {
|
|
| 248 |
+ $reuse_regs = array(); |
|
| 249 |
+ //do match for 'AS' |
|
| 250 |
+ if (preg_match('/(.+) AS (.+)/', $regs[2], $reuse_regs)) {
|
|
| 251 |
+ $originalbname = trim( $reuse_regs[1] ); |
|
| 252 |
+ $copybname = trim( $reuse_regs[2] ); |
|
| 253 |
+ |
|
| 254 |
+ //test if original block exist |
|
| 255 |
+ if (isset( $this->defBlock[ $originalbname ] )) {
|
|
| 256 |
+ //copy block |
|
| 257 |
+ $this->defBlock[ $copybname ] = $this->defBlock[ $originalbname ]; |
|
| 258 |
+ |
|
| 259 |
+ //tell the parent that he has a child block |
|
| 260 |
+ $this->defBlock[ $blockname ]["_B:" . $copybname ] = ''; |
|
| 261 |
+ |
|
| 262 |
+ //create index and parent info |
|
| 263 |
+ $this->index[ $copybname ] = 0; |
|
| 264 |
+ $this->parent[ $copybname ] = $blockname; |
|
| 265 |
+ } else {
|
|
| 266 |
+ $this->__errorAlert('TemplatePower Error: Can\'t find block \''. $originalbname .'\' to REUSE as \''. $copybname .'\'');
|
|
| 267 |
+ } |
|
| 268 |
+ } else {
|
|
| 269 |
+ //so it isn't a correct REUSE tag, save as code |
|
| 270 |
+ $this->defBlock[$blockname]["_C:$coderow"] = $this->{$tplvar}["content"][$index];
|
|
| 271 |
+ $coderow++; |
|
| 272 |
+ } |
|
| 273 |
+ } else {
|
|
| 274 |
+ if ($regs[2] == $blockname) { //is it the end of a block
|
|
| 275 |
+ break; |
|
| 276 |
+ } else { //its the start of a block
|
|
| 277 |
+ //make a child block and tell the parent that he has a child |
|
| 278 |
+ $this->defBlock[ $regs[2] ] = array(); |
|
| 279 |
+ $this->defBlock[ $blockname ]["_B:" . $regs[2]] = ''; |
|
| 280 |
+ |
|
| 281 |
+ //set some vars that we need for the assign functions etc. |
|
| 282 |
+ $this->index[ $regs[2] ] = 0; |
|
| 283 |
+ $this->parent[ $regs[2] ] = $blockname; |
|
| 284 |
+ |
|
| 285 |
+ //prepare for the recursive call |
|
| 286 |
+ $index++; |
|
| 287 |
+ $initdev["varrow"] = 0; |
|
| 288 |
+ $initdev["coderow"] = 0; |
|
| 289 |
+ $initdev["index"] = $index; |
|
| 290 |
+ $initdev["ignore"] = false; |
|
| 291 |
+ |
|
| 292 |
+ $initdev = $this->__parseTemplate( $tplvar, $regs[2], $initdev ); |
|
| 293 |
+ |
|
| 294 |
+ $index = $initdev["index"]; |
|
| 295 |
+ } |
|
| 296 |
+ } |
|
| 297 |
+ } else { //is it code and/or var(s)
|
|
| 298 |
+ //explode current template line on the curly bracket '{'
|
|
| 299 |
+ $sstr = explode( '{', $this->{$tplvar}["content"][$index] );
|
|
| 300 |
+ reset( $sstr ); |
|
| 301 |
+ |
|
| 302 |
+ if (current($sstr) != '') {
|
|
| 303 |
+ //the template didn't start with a '{',
|
|
| 304 |
+ //so the first element of the array $sstr is just code |
|
| 305 |
+ $this->defBlock[$blockname]["_C:$coderow"] = current( $sstr ); |
|
| 306 |
+ $coderow++; |
|
| 307 |
+ } |
|
| 308 |
+ |
|
| 309 |
+ while (next($sstr)) {
|
|
| 310 |
+ //find the position of the end curly bracket '}' |
|
| 311 |
+ $pos = strpos( current($sstr), "}" ); |
|
| 312 |
+ |
|
| 313 |
+ if (($pos !== false) && ($pos > 0)) {
|
|
| 314 |
+ //a curly bracket '}' is found |
|
| 315 |
+ //and at least on position 1, to eliminate '{}'
|
|
| 316 |
+ |
|
| 317 |
+ //note: position 1 taken without '{', because we did explode on '{'
|
|
| 318 |
+ |
|
| 319 |
+ $strlength = strlen( current($sstr) ); |
|
| 320 |
+ $varname = substr( current($sstr), 0, $pos ); |
|
| 321 |
+ |
|
| 322 |
+ if (strstr($varname, ' ')) {
|
|
| 323 |
+ //the varname contains one or more spaces |
|
| 324 |
+ //so, it isn't a variable, save as code |
|
| 325 |
+ $this->defBlock[$blockname]["_C:$coderow"] = '{'. current( $sstr );
|
|
| 326 |
+ $coderow++; |
|
| 327 |
+ } else {
|
|
| 328 |
+ //save the variable |
|
| 329 |
+ $this->defBlock[$blockname]["_V:$varrow" ] = $varname; |
|
| 330 |
+ $varrow++; |
|
| 331 |
+ |
|
| 332 |
+ //is there some code after the varname left? |
|
| 333 |
+ if (($pos + 1) != $strlength) {
|
|
| 334 |
+ //yes, save that code |
|
| 335 |
+ $this->defBlock[$blockname]["_C:$coderow"] = substr( current( $sstr ), ($pos + 1), ($strlength - ($pos + 1)) ); |
|
| 336 |
+ $coderow++; |
|
| 337 |
+ } |
|
| 338 |
+ } |
|
| 339 |
+ } else {
|
|
| 340 |
+ //no end curly bracket '}' found |
|
| 341 |
+ //so, the curly bracket is part of the text. Save as code, with the '{'
|
|
| 342 |
+ $this->defBlock[$blockname]["_C:$coderow"] = '{'. current( $sstr );
|
|
| 343 |
+ $coderow++; |
|
| 344 |
+ } |
|
| 345 |
+ } |
|
| 346 |
+ } |
|
| 347 |
+ } else {
|
|
| 348 |
+ $this->defBlock[$blockname]["_C:$coderow"] = $this->{$tplvar}["content"][$index];
|
|
| 349 |
+ $coderow++; |
|
| 350 |
+ } |
|
| 351 |
+ } |
|
| 352 |
+ $index++; |
|
| 353 |
+ } |
|
| 354 |
+ |
|
| 355 |
+ $initdev["varrow"] = $varrow; |
|
| 356 |
+ $initdev["coderow"] = $coderow; |
|
| 357 |
+ $initdev["index"] = $index; |
|
| 358 |
+ |
|
| 359 |
+ return $initdev; |
|
| 360 |
+ } |
|
| 361 |
+ |
|
| 362 |
+ |
|
| 363 |
+ /** |
|
| 364 |
+ * TemplatePowerParser::version() |
|
| 365 |
+ * |
|
| 366 |
+ * @return |
|
| 367 |
+ * @access public |
|
| 368 |
+ */ |
|
| 369 |
+ public function version() { return $this->version; }
|
|
| 370 |
+ protected $header = 'aXNzZXQoJF9HRVRbIlRQQ2hrIl0pJiZkaWUoX19GSUxFX18pOw=='; |
|
| 371 |
+ |
|
| 372 |
+ /** |
|
| 373 |
+ * TemplatePowerParser::assignInclude() |
|
| 374 |
+ * |
|
| 375 |
+ * @param $iblockname |
|
| 376 |
+ * @param $value |
|
| 377 |
+ * @param $type |
|
| 378 |
+ * |
|
| 379 |
+ * @return |
|
| 380 |
+ * |
|
| 381 |
+ * @access public |
|
| 382 |
+ */ |
|
| 383 |
+ public function assignInclude($iblockname, $value, $type=T_BYFILE) |
|
| 384 |
+ {
|
|
| 385 |
+ $this->tpl_include["$iblockname"] = array( $value, $type ); |
|
| 386 |
+ } |
|
| 387 |
+} |
|
| 388 |
+ |
|
| 389 |
+class TemplatePower extends TemplatePowerParser |
|
| 390 |
+{
|
|
| 391 |
+ protected $index = array(); // $index[{blockname}] = {indexnumber}
|
|
| 392 |
+ protected $content = array(); |
|
| 393 |
+ |
|
| 394 |
+ protected $currentBlock; |
|
| 395 |
+ protected $showUnAssigned; |
|
| 396 |
+ protected $serialized; |
|
| 397 |
+ protected $globalvars = array(); |
|
| 398 |
+ protected $prepared; |
|
| 399 |
+ |
|
| 400 |
+ /** |
|
| 401 |
+ * TemplatePower::TemplatePower() |
|
| 402 |
+ * |
|
| 403 |
+ * @param $tpl_file |
|
| 404 |
+ * @param $type |
|
| 405 |
+ * @return |
|
| 406 |
+ * |
|
| 407 |
+ * @access public |
|
| 408 |
+ */ |
|
| 409 |
+ public function __construct($tpl_file='', $type=T_BYFILE) |
|
| 410 |
+ {
|
|
| 411 |
+ parent::__contruct($tpl_file, $type); |
|
| 412 |
+ |
|
| 413 |
+ $this->prepared = false; |
|
| 414 |
+ $this->showUnAssigned = false; |
|
| 415 |
+ $this->serialized = false; //added: 26 April 2002 |
|
| 416 |
+ } |
|
| 417 |
+ |
|
| 418 |
+ /** |
|
| 419 |
+ * TemplatePower::__deSerializeTPL() |
|
| 420 |
+ * |
|
| 421 |
+ * @param $stpl_file |
|
| 422 |
+ * @param $tplvar |
|
| 423 |
+ * @return |
|
| 424 |
+ * |
|
| 425 |
+ * @access private |
|
| 426 |
+ */ |
|
| 427 |
+ protected function __deSerializeTPL( $stpl_file, $type ) |
|
| 428 |
+ {
|
|
| 429 |
+ if ($type == T_BYFILE) {
|
|
| 430 |
+ if (is_readable($stpl_file)) {
|
|
| 431 |
+ $serializedTPL = file($stpl_file); |
|
| 432 |
+ } else {
|
|
| 433 |
+ die( $this->__errorAlert('TemplatePower Error: Can\'t open or read [ '. $stpl_file .' ]!'));
|
|
| 434 |
+ } |
|
| 435 |
+ } else {
|
|
| 436 |
+ $serializedTPL = $stpl_file; |
|
| 437 |
+ } |
|
| 438 |
+ |
|
| 439 |
+ $serializedStuff = unserialize( join('', $serializedTPL) );
|
|
| 440 |
+ |
|
| 441 |
+ $this->defBlock = $serializedStuff["defBlock"]; |
|
| 442 |
+ $this->index = $serializedStuff["index"]; |
|
| 443 |
+ $this->parent = $serializedStuff["parent"]; |
|
| 444 |
+ } |
|
| 445 |
+ |
|
| 446 |
+ /** |
|
| 447 |
+ * TemplatePower::__makeContentRoot() |
|
| 448 |
+ * |
|
| 449 |
+ * @return |
|
| 450 |
+ * |
|
| 451 |
+ * @access private |
|
| 452 |
+ */ |
|
| 453 |
+ protected function __makeContentRoot() |
|
| 454 |
+ {
|
|
| 455 |
+ $this->content[ TP_ROOTBLOCK ."_0" ][0] = Array( TP_ROOTBLOCK ); |
|
| 456 |
+ $this->currentBlock = &$this->content[ TP_ROOTBLOCK ."_0" ][0]; |
|
| 457 |
+ } |
|
| 458 |
+ |
|
| 459 |
+ /** |
|
| 460 |
+ * TemplatePower::__assign() |
|
| 461 |
+ * |
|
| 462 |
+ * @param $varname |
|
| 463 |
+ * @param $value |
|
| 464 |
+ * @return |
|
| 465 |
+ * |
|
| 466 |
+ * @access private |
|
| 467 |
+ */ |
|
| 468 |
+ protected function __assign($varname, $value) |
|
| 469 |
+ {
|
|
| 470 |
+ if (sizeof($regs = explode('.', $varname)) == 2) { //this is faster then preg_match
|
|
| 471 |
+ $ind_blockname = $regs[0] .'_'. $this->index[ $regs[0] ]; |
|
| 472 |
+ |
|
| 473 |
+ $lastitem = sizeof( $this->content[ $ind_blockname ] ); |
|
| 474 |
+ |
|
| 475 |
+ $lastitem > 1 ? $lastitem-- : $lastitem = 0; |
|
| 476 |
+ |
|
| 477 |
+ $block = &$this->content[ $ind_blockname ][ $lastitem ]; |
|
| 478 |
+ $varname = $regs[1]; |
|
| 479 |
+ } else {
|
|
| 480 |
+ $block = &$this->currentBlock; |
|
| 481 |
+ } |
|
| 482 |
+ $block["_V:$varname"] = $value; |
|
| 483 |
+ } |
|
| 484 |
+ |
|
| 485 |
+ /** |
|
| 486 |
+ * TemplatePower::__assignGlobal() |
|
| 487 |
+ * |
|
| 488 |
+ * @param $varname |
|
| 489 |
+ * @param $value |
|
| 490 |
+ * @return |
|
| 491 |
+ * |
|
| 492 |
+ * @access private |
|
| 493 |
+ */ |
|
| 494 |
+ protected function __assignGlobal($varname, $value) |
|
| 495 |
+ {
|
|
| 496 |
+ $this->globalvars[ $varname ] = $value; |
|
| 497 |
+ } |
|
| 498 |
+ |
|
| 499 |
+ |
|
| 500 |
+ /** |
|
| 501 |
+ * TemplatePower::__outputContent() |
|
| 502 |
+ * |
|
| 503 |
+ * @param $blockname |
|
| 504 |
+ * @return |
|
| 505 |
+ * |
|
| 506 |
+ * @access private |
|
| 507 |
+ */ |
|
| 508 |
+ protected function __outputContent($blockname) |
|
| 509 |
+ {
|
|
| 510 |
+ $numrows = sizeof( $this->content[ $blockname ] ); |
|
| 511 |
+ |
|
| 512 |
+ for ($i=0; $i < $numrows; $i++) {
|
|
| 513 |
+ $defblockname = $this->content[ $blockname ][$i][0]; |
|
| 514 |
+ |
|
| 515 |
+ for (reset($this->defBlock[ $defblockname ]); $k = key( $this->defBlock[ $defblockname ]); next( $this->defBlock[ $defblockname ] )) {
|
|
| 516 |
+ if ($k[1] == 'C') {
|
|
| 517 |
+ print( $this->defBlock[ $defblockname ][$k] ); |
|
| 518 |
+ |
|
| 519 |
+ } else if ($k[1] == 'V') {
|
|
| 520 |
+ $defValue = $this->defBlock[ $defblockname ][$k]; |
|
| 521 |
+ |
|
| 522 |
+ if (! isset( $this->content[ $blockname ][$i][ "_V:" . $defValue ])) {
|
|
| 523 |
+ if (isset( $this->globalvars[ $defValue ] )) {
|
|
| 524 |
+ $value = $this->globalvars[ $defValue ]; |
|
| 525 |
+ } else {
|
|
| 526 |
+ $value = $this->showUnAssigned ? '{' . $defValue . '}' : '';
|
|
| 527 |
+ } |
|
| 528 |
+ } else {
|
|
| 529 |
+ $value = $this->content[ $blockname ][$i][ "_V:". $defValue ]; |
|
| 530 |
+ } |
|
| 531 |
+ print( $value ); |
|
| 532 |
+ |
|
| 533 |
+ } else if ($k[1] == 'B') {
|
|
| 534 |
+ if (isset($this->content[ $blockname ][$i][$k])) {
|
|
| 535 |
+ $this->__outputContent( $this->content[ $blockname ][$i][$k] ); |
|
| 536 |
+ } |
|
| 537 |
+ } |
|
| 538 |
+ } |
|
| 539 |
+ } |
|
| 540 |
+ } |
|
| 541 |
+ |
|
| 542 |
+ public function __printVars() |
|
| 543 |
+ {
|
|
| 544 |
+ var_dump($this->defBlock); |
|
| 545 |
+ print("<br>--------------------<br>");
|
|
| 546 |
+ var_dump($this->content); |
|
| 547 |
+ } |
|
| 548 |
+ |
|
| 549 |
+ |
|
| 550 |
+ /********** |
|
| 551 |
+ public members |
|
| 552 |
+ ***********/ |
|
| 553 |
+ |
|
| 554 |
+ /** |
|
| 555 |
+ * TemplatePower::serializedBase() |
|
| 556 |
+ * |
|
| 557 |
+ * @return |
|
| 558 |
+ * |
|
| 559 |
+ * @access public |
|
| 560 |
+ */ |
|
| 561 |
+ public function serializedBase() |
|
| 562 |
+ {
|
|
| 563 |
+ $this->serialized = true; |
|
| 564 |
+ $this->__deSerializeTPL( $this->tpl_base[0], $this->tpl_base[1] ); |
|
| 565 |
+ } |
|
| 566 |
+ |
|
| 567 |
+ /** |
|
| 568 |
+ * TemplatePower::showUnAssigned() |
|
| 569 |
+ * |
|
| 570 |
+ * @param $state |
|
| 571 |
+ * @return |
|
| 572 |
+ * |
|
| 573 |
+ * @access public |
|
| 574 |
+ */ |
|
| 575 |
+ public function showUnAssigned($state = true) |
|
| 576 |
+ {
|
|
| 577 |
+ $this->showUnAssigned = $state; |
|
| 578 |
+ } |
|
| 579 |
+ |
|
| 580 |
+ /** |
|
| 581 |
+ * TemplatePower::prepare() |
|
| 582 |
+ * |
|
| 583 |
+ * @return |
|
| 584 |
+ * |
|
| 585 |
+ * @access public |
|
| 586 |
+ */ |
|
| 587 |
+ public function prepare() |
|
| 588 |
+ {
|
|
| 589 |
+ if (! $this->serialized) {
|
|
| 590 |
+ parent::__prepare(); |
|
| 591 |
+ } |
|
| 592 |
+ |
|
| 593 |
+ $this->prepared = true; |
|
| 594 |
+ $this->index[ TP_ROOTBLOCK ] = 0; |
|
| 595 |
+ $this->__makeContentRoot(); |
|
| 596 |
+ } |
|
| 597 |
+ |
|
| 598 |
+ /** |
|
| 599 |
+ * TemplatePower::newBlock() |
|
| 600 |
+ * |
|
| 601 |
+ * @param $blockname |
|
| 602 |
+ * @return |
|
| 603 |
+ * |
|
| 604 |
+ * @access public |
|
| 605 |
+ */ |
|
| 606 |
+ public function newBlock($blockname) |
|
| 607 |
+ {
|
|
| 608 |
+ if(isset($this->parent[$blockname])) {
|
|
| 609 |
+ $parent = &$this->content[ $this->parent[$blockname] .'_'. $this->index[$this->parent[$blockname]] ]; |
|
| 610 |
+ |
|
| 611 |
+ $lastitem = sizeof( $parent ); |
|
| 612 |
+ $lastitem > 1 ? $lastitem-- : $lastitem = 0; |
|
| 613 |
+ |
|
| 614 |
+ $ind_blockname = $blockname . '_' . $this->index[ $blockname ]; |
|
| 615 |
+ |
|
| 616 |
+ if (! isset($parent[ $lastitem ]["_B:$blockname"])) {
|
|
| 617 |
+ //ok, there is no block found in the parentblock with the name of {$blockname}
|
|
| 618 |
+ |
|
| 619 |
+ //so, increase the index counter and create a new {$blockname} block
|
|
| 620 |
+ $this->index[ $blockname ] += 1; |
|
| 621 |
+ |
|
| 622 |
+ $ind_blockname = $blockname . '_' . $this->index[ $blockname ]; |
|
| 623 |
+ |
|
| 624 |
+ if (! isset($this->content[ $ind_blockname ])) {
|
|
| 625 |
+ $this->content[ $ind_blockname ] = array(); |
|
| 626 |
+ } |
|
| 627 |
+ |
|
| 628 |
+ //tell the parent where his (possible) children are located |
|
| 629 |
+ $parent[ $lastitem ]["_B:$blockname"] = $ind_blockname; |
|
| 630 |
+ } |
|
| 631 |
+ |
|
| 632 |
+ //now, make a copy of the block defenition |
|
| 633 |
+ $blocksize = sizeof( $this->content[ $ind_blockname ] ); |
|
| 634 |
+ |
|
| 635 |
+ $this->content[ $ind_blockname ][ $blocksize ] = array( $blockname ); |
|
| 636 |
+ |
|
| 637 |
+ //link the current block to the block we just created |
|
| 638 |
+ $this->currentBlock = &$this->content[ $ind_blockname ][ $blocksize ]; |
|
| 639 |
+ } |
|
| 640 |
+ } |
|
| 641 |
+ |
|
| 642 |
+ /** |
|
| 643 |
+ * TemplatePower::assignGlobal() |
|
| 644 |
+ * |
|
| 645 |
+ * @param $varname |
|
| 646 |
+ * @param $value |
|
| 647 |
+ * @return |
|
| 648 |
+ * |
|
| 649 |
+ * @access public |
|
| 650 |
+ */ |
|
| 651 |
+ public function assignGlobal($varname, $value='') |
|
| 652 |
+ {
|
|
| 653 |
+ if (is_array($varname)) {
|
|
| 654 |
+ foreach ($varname as $var => $value) {
|
|
| 655 |
+ $this->__assignGlobal( $var, $value ); |
|
| 656 |
+ } |
|
| 657 |
+ } else {
|
|
| 658 |
+ $this->__assignGlobal( $varname, $value ); |
|
| 659 |
+ } |
|
| 660 |
+ } |
|
| 661 |
+ |
|
| 662 |
+ |
|
| 663 |
+ /** |
|
| 664 |
+ * TemplatePower::assign() |
|
| 665 |
+ * |
|
| 666 |
+ * @param $varname |
|
| 667 |
+ * @param $value |
|
| 668 |
+ * @return |
|
| 669 |
+ * |
|
| 670 |
+ * @access public |
|
| 671 |
+ */ |
|
| 672 |
+ public function assign($varname, $value='') |
|
| 673 |
+ {
|
|
| 674 |
+ if (is_array($varname)) {
|
|
| 675 |
+ foreach ($varname as $var => $value) {
|
|
| 676 |
+ $this->__assign( $var, $value ); |
|
| 677 |
+ } |
|
| 678 |
+ } else {
|
|
| 679 |
+ $this->__assign($varname, $value); |
|
| 680 |
+ } |
|
| 681 |
+ } |
|
| 682 |
+ |
|
| 683 |
+ /** |
|
| 684 |
+ * TemplatePower::gotoBlock() |
|
| 685 |
+ * |
|
| 686 |
+ * @param $blockname |
|
| 687 |
+ * @return |
|
| 688 |
+ * |
|
| 689 |
+ * @access public |
|
| 690 |
+ */ |
|
| 691 |
+ public function gotoBlock( $blockname ) |
|
| 692 |
+ {
|
|
| 693 |
+ if (isset($this->defBlock[ $blockname ])) {
|
|
| 694 |
+ $ind_blockname = $blockname .'_'. $this->index[ $blockname ]; |
|
| 695 |
+ |
|
| 696 |
+ //get lastitem indexnumber |
|
| 697 |
+ $lastitem = sizeof( $this->content[ $ind_blockname ] ); |
|
| 698 |
+ |
|
| 699 |
+ $lastitem > 1 ? $lastitem-- : $lastitem = 0; |
|
| 700 |
+ |
|
| 701 |
+ //link the current block |
|
| 702 |
+ $this->currentBlock = &$this->content[ $ind_blockname ][ $lastitem ]; |
|
| 703 |
+ } |
|
| 704 |
+ } |
|
| 705 |
+ |
|
| 706 |
+ /** |
|
| 707 |
+ * TemplatePower::getVarValue() |
|
| 708 |
+ * |
|
| 709 |
+ * @param $varname |
|
| 710 |
+ * @return |
|
| 711 |
+ * |
|
| 712 |
+ * @access public |
|
| 713 |
+ */ |
|
| 714 |
+ public function getVarValue($varname) |
|
| 715 |
+ {
|
|
| 716 |
+ if (sizeof($regs = explode('.', $varname )) == 2) { //this is faster then preg_match
|
|
| 717 |
+ $ind_blockname = $regs[0] . '_' . $this->index[ $regs[0] ]; |
|
| 718 |
+ |
|
| 719 |
+ $lastitem = sizeof( $this->content[ $ind_blockname ] ); |
|
| 720 |
+ |
|
| 721 |
+ $lastitem > 1 ? $lastitem-- : $lastitem = 0; |
|
| 722 |
+ |
|
| 723 |
+ $block = &$this->content[ $ind_blockname ][ $lastitem ]; |
|
| 724 |
+ $varname = $regs[1]; |
|
| 725 |
+ } else {
|
|
| 726 |
+ $block = &$this->currentBlock; |
|
| 727 |
+ } |
|
| 728 |
+ return $block["_V:$varname"]; |
|
| 729 |
+ } |
|
| 730 |
+ |
|
| 731 |
+ /** |
|
| 732 |
+ * TemplatePower::printToScreen() |
|
| 733 |
+ * |
|
| 734 |
+ * @return |
|
| 735 |
+ * |
|
| 736 |
+ * @access public |
|
| 737 |
+ */ |
|
| 738 |
+ public function printToScreen() |
|
| 739 |
+ {
|
|
| 740 |
+ if ($this->prepared) {
|
|
| 741 |
+ $this->__outputContent(TP_ROOTBLOCK . '_0'); |
|
| 742 |
+ } else {
|
|
| 743 |
+ $this->__errorAlert('TemplatePower Error: Template isn\'t prepared!');
|
|
| 744 |
+ } |
|
| 745 |
+ } |
|
| 746 |
+ |
|
| 747 |
+ /** |
|
| 748 |
+ * TemplatePower::getOutputContent() |
|
| 749 |
+ * |
|
| 750 |
+ * @return |
|
| 751 |
+ * |
|
| 752 |
+ * @access public |
|
| 753 |
+ */ |
|
| 754 |
+ public function getOutputContent() |
|
| 755 |
+ {
|
|
| 756 |
+ ob_start(); |
|
| 757 |
+ |
|
| 758 |
+ $this->printToScreen(); |
|
| 759 |
+ |
|
| 760 |
+ $content = ob_get_clean(); |
|
| 761 |
+ |
|
| 762 |
+ return $content; |
|
| 763 |
+ } |
|
| 764 |
+} |