summaryrefslogtreecommitdiffstats
path: root/ldap-site-mngmt/webinterface/class.FastTemplate.php
blob: d048ee5c706019db199b2f0dd65d247094b2065e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
<?
################################################
#### Angepasst an PHP4 von                  ####
#### Timothy Burk - Webdesign & PC-Service  ####
####   info@tburk.com                       ####
################################################
/*
        CVS Revision. 1.1.0
*/

/*
  eine weitere kleine Anpassung von Thomas Nunninger betrifft das append in der
  parse-Funktion. Bei error_reporting(E_ALL) erschien sonst beim ersten Anhängen
  an eine noch nicht vorhandene Variable eine Fehlermeldung.
*/

class FastTemplate {

        var $FILELIST = array();        //        Holds the array of filehandles
                                        //        FILELIST[HANDLE] == "fileName"

        var $DYNAMIC = array();        //        Holds the array of dynamic
                                       //        blocks, and the fileHandles they
                                       //        live in.

        var $PARSEVARS = array();      //        Holds the array of Variable
                                       //        handles.
                                       //        PARSEVARS[HANDLE] == "value"

        var $LOADED = array();        //        We only want to load a template
                                      //        once - when it's used.
                                      //        LOADED[FILEHANDLE] == 1 if loaded
                                      //        undefined if not loaded yet.

        var $HANDLE = array();        //        Holds the handle names assigned
                                                                        //        by a call to parse()

        var $ROOT = "";                        //        Holds path-to-templates

        var $WIN32 = false;                //        Set to true if this is a WIN32 server

        var $ERROR = "";                        //        Holds the last error message

        var $LAST = "";                        //        Holds the HANDLE to the last
                                                                        //        template parsed by parse()

        var $STRICT = true;                //        Strict template checking.
                                                                        //        Unresolved vars in templates will
                                                                        //        generate a warning when found.

//        ************************************************************

        function FastTemplate ($pathToTemplates = "")
        {
                global $php_errormsg;

                if(!empty($pathToTemplates))
                {
                        $this->set_root($pathToTemplates);
                }

        }        // end (new) FastTemplate ()


//        ************************************************************
//        All templates will be loaded from this "root" directory
//        Can be changed in mid-process by re-calling with a new
//        value.

        function set_root ($root)
        {
                $trailer = substr($root,-1);

                if(!$this->WIN32)
                {
                        if( (ord($trailer)) != 47 )
                        {
                                $root = "$root".chr(47);
                        }

                        if(is_dir($root))
                        {
                                $this->ROOT = $root;
                        }
                        else
                        {
                                $this->ROOT = "";
                                $this->error("Specified ROOT dir [$root] is not a directory");
                        }
                }
                else
                {
                        // WIN32 box - no testing
                        if( (ord($trailer)) != 92 )
                        {
                                $root = "$root".chr(92);
                        }
                        $this->ROOT = $root;
                }

        }        // End set_root()


//  **************************************************************
//  Calculates current microtime
//        I throw this into all my classes for benchmarking purposes
//        It's not used by anything in this class and can be removed
//        if you don't need it.


        function utime ()
        {
                $time = explode( " ", microtime());
                $usec = (double)$time[0];
                $sec = (double)$time[1];
                return $sec + $usec;
    }

//  **************************************************************
//        Strict template checking, if true sends warnings to STDOUT when
//        parsing a template with undefined variable references
//        Used for tracking down bugs-n-such. Use no_strict() to disable.

        function strict ()
        {
                $this->STRICT = true;
        }

//        ************************************************************
//        Silently discards (removes) undefined variable references
//        found in templates

        function no_strict ()
        {
                $this->STRICT = false;
        }

//        ************************************************************
//        A quick check of the template file before reading it.
//        This is -not- a reliable check, mostly due to inconsistencies
//        in the way PHP determines if a file is readable.

        function is_safe ($filename)
        {
                if(!file_exists($filename))
                {
                        $this->error("[$filename] does not exist",0);
                        return false;
                }
                return true;
        }

//        ************************************************************
//        Grabs a template from the root dir and
//        reads it into a (potentially REALLY) big string

        function get_template ($template)
        {
                if(empty($this->ROOT))
                {
                        $this->error("Cannot open template. Root not valid.",1);
                        return false;
                }

                $filename        =        "$this->ROOT"."$template";

                $contents = implode("",(@file($filename)));
                if( (!$contents) or (empty($contents)) )
                {
                        $this->error("get_template() failure: [$filename] $php_errormsg",1);
                }

                return $contents;

        } // end get_template

//        ************************************************************
//        Prints the warnings for unresolved variable references
//        in template files. Used if STRICT is true

        function show_unknowns ($Line)
        {
                $unknown = array();
                if (ereg("({[A-Z0-9_]+})",$Line,$unknown))
                {
                        $UnkVar = $unknown[1];
                        if(!(empty($UnkVar)))
                        {
                                @error_log("[FastTemplate] Warning: no value found for variable: $UnkVar ",0);
                        }
                }
        }        // end show_unknowns()

//        ************************************************************
//        This routine get's called by parse() and does the actual
//        {VAR} to VALUE conversion within the template.

        function parse_template ($template, $tpl_array)
        {
                while ( list ($key,$val) = each ($tpl_array) )
                {
                        if (!(empty($key)))
                        {
                                if(gettype($val) != "string")
                                {
                                        settype($val,"string");
                                }

                                $template = ereg_replace("\{$key}","$val","$template");
                                //$template = str_replace("\{$key}","$val","$template");
                        }
                }

                if(!$this->STRICT)
                {
                        // Silently remove anything not already found

                        $template = ereg_replace("\{([A-Z0-9_]+)}","",$template);
                }
                else
                {
                        // Warn about unresolved template variables
                        if (ereg("(\{[A-Z0-9_]+})",$template))
                        {
                                $unknown = split("\n",$template);
                                while (list ($Element,$Line) = each($unknown) )
                                {
                                        $UnkVar = $Line;
                                        if(!(empty($UnkVar)))
                                        {
                                                $this->show_unknowns($UnkVar);
                                        }
                                }
                        }
                }
                return $template;

        }        // end parse_template();

//        ************************************************************
//        The meat of the whole class. The magic happens here.

        function parse ( $ReturnVar, $FileTags )
        {
                $append = false;
                $this->LAST = $ReturnVar;
                $this->HANDLE[$ReturnVar] = 1;

                if (gettype($FileTags) == "array")
                {
                        unset($this->$ReturnVar);        // Clear any previous data

                        while ( list ( $key , $val ) = each ( $FileTags ) )
                        {
                                if ( (!isset($this->$val)) || (empty($this->$val)) )
                                {
                                        $this->LOADED["$val"] = 1;
                                        if(isset($this->DYNAMIC["$val"]))
                                        {
                                                $this->parse_dynamic($val,$ReturnVar);
                                        }
                                        else
                                        {
                                                $fileName = $this->FILELIST["$val"];
                                                $this->$val = $this->get_template($fileName);
                                        }
                                }

                                //        Array context implies overwrite

                                $this->$ReturnVar = $this->parse_template($this->$val,$this->PARSEVARS);

                                //        For recursive calls.

                                $this->assign( array( $ReturnVar => $this->$ReturnVar ) );

                        }
                }        // end if FileTags is array()
                else
                {
                        // FileTags is not an array

                        $val = $FileTags;

                        if( (substr($val,0,1)) == '.' )
                        {
                                // Append this template to a previous ReturnVar

                                $append = true;
                                $val = substr($val,1);
                        }

                        if ( (!isset($this->$val)) || (empty($this->$val)) )
                        {
                                        $this->LOADED["$val"] = 1;
                                        if(isset($this->DYNAMIC["$val"]))
                                        {
                                                $this->parse_dynamic($val,$ReturnVar);
                                        }
                                        else
                                        {
                                                $fileName = $this->FILELIST["$val"];
                                                $this->$val = $this->get_template($fileName);
                                        }
                        }

                        if($append)
                        {
                                // Thomas> auch hier append korrigiert
                                if (isset($this->$ReturnVar)) {
                                    $this->$ReturnVar .= $this->parse_template($this->$val,$this->PARSEVARS);
                                } else {
                                    $this->$ReturnVar = $this->parse_template($this->$val,$this->PARSEVARS);
                                }
                        }
                        else
                        {
                                $this->$ReturnVar = $this->parse_template($this->$val,$this->PARSEVARS);
                        }

                        //        For recursive calls.

                        $this->assign(array( $ReturnVar => $this->$ReturnVar) );

                }
                return;
        }        //        End parse()


//        ************************************************************

        function FastPrint ( $template = "" )
        {
                if(empty($template))
                {
                        $template = $this->LAST;
                }

                if( (!(isset($this->$template))) || (empty($this->$template)) )
                {
                        $this->error("Nothing parsed, nothing printed",0);
                        return;
                }
                else
                {
                        print $this->$template;
                }
                return;
        }

//        ************************************************************

        function fetch ( $template = "" )
        {
                if(empty($template))
                {
                        $template = $this->LAST;
                }
                if( (!(isset($this->$template))) || (empty($this->$template)) )
                {
                        $this->error("Nothing parsed, nothing printed",0);
                        return "";
                }

                return($this->$template);
        }


//        ************************************************************

        function define_dynamic ($Macro, $ParentName)
        {
                //        A dynamic block lives inside another template file.
                //        It will be stripped from the template when parsed
                //        and replaced with the {$Tag}.

                $this->DYNAMIC["$Macro"] = $ParentName;
                return true;
        }

//        ************************************************************

        function parse_dynamic ($Macro,$MacroName)
        {
                // The file must already be in memory.

                $ParentTag = $this->DYNAMIC["$Macro"];
                // Thomas> in Folgezeile !isset($this->$ParentTag eingefügt
                if( (!isset($this->$ParentTag) || !$this->$ParentTag) or (empty($this->$ParentTag)) )
                {
                        $fileName = $this->FILELIST[$ParentTag];
                        $this->$ParentTag = $this->get_template($fileName);
                        $this->LOADED[$ParentTag] = 1;
                }
                if($this->$ParentTag)
                {
                        $template = $this->$ParentTag;
                        $DataArray = split("\n",$template);
                        $newMacro = "";
                        $newParent = "";
                        $outside = true;
                        $start = false;
                        $end = false;
                        while ( list ($lineNum,$lineData) = each ($DataArray) )
                        {
                                $lineTest = trim($lineData);
                                if("<!-- BEGIN DYNAMIC BLOCK: $Macro -->" == "$lineTest" )
                                {
                                        $start = true;
                                        $end = false;
                                        $outside = false;
                                }
                                if("<!-- END DYNAMIC BLOCK: $Macro -->" == "$lineTest" )
                                {
                                        $start = false;
                                        $end = true;
                                        $outside = true;
                                }
                                if( (!$outside) and (!$start) and (!$end) )
                                {
                                        $newMacro .= "$lineData\n"; // Restore linebreaks
                                }
                                if( ($outside) and (!$start) and (!$end) )
                                {
                                        $newParent .= "$lineData\n"; // Restore linebreaks
                                }
                                if($end)
                                {
                                        $newParent .= "\{$MacroName}\n";
                                }
                                // Next line please
                                if($end) { $end = false; }
                                if($start) { $start = false; }
                        }        // end While

                        $this->$Macro = $newMacro;
                        $this->$ParentTag = $newParent;
                        return true;

                }        // $ParentTag NOT loaded - MAJOR oopsie
                else
                {
                        @error_log("ParentTag: [$ParentTag] not loaded!",0);
                        $this->error("ParentTag: [$ParentTag] not loaded!",0);
                }
                return false;
        }

//        ************************************************************
//        Strips a DYNAMIC BLOCK from a template.

        function clear_dynamic ($Macro="")
        {
                if(empty($Macro)) { return false; }

                // The file must already be in memory.

                $ParentTag = $this->DYNAMIC["$Macro"];

                if( (!$this->$ParentTag) or (empty($this->$ParentTag)) )
                {
                        $fileName = $this->FILELIST[$ParentTag];
                        $this->$ParentTag = $this->get_template($fileName);
                        $this->LOADED[$ParentTag] = 1;
                }

                if($this->$ParentTag)
                {
                        $template = $this->$ParentTag;
                        $DataArray = split("\n",$template);
                        $newParent = "";
                        $outside = true;
                        $start = false;
                        $end = false;
                        while ( list ($lineNum,$lineData) = each ($DataArray) )
                        {
                                $lineTest = trim($lineData);
                                if("<!-- BEGIN DYNAMIC BLOCK: $Macro -->" == "$lineTest" )
                                {
                                        $start = true;
                                        $end = false;
                                        $outside = false;
                                }
                                if("<!-- END DYNAMIC BLOCK: $Macro -->" == "$lineTest" )
                                {
                                        $start = false;
                                        $end = true;
                                        $outside = true;
                                }
                                if( ($outside) and (!$start) and (!$end) )
                                {
                                        $newParent .= "$lineData\n"; // Restore linebreaks
                                }
                                // Next line please
                                if($end) { $end = false; }
                                if($start) { $start = false; }
                        }        // end While

                        $this->$ParentTag = $newParent;
                        return true;

                }        // $ParentTag NOT loaded - MAJOR oopsie
                else
                {
                        @error_log("ParentTag: [$ParentTag] not loaded!",0);
                        $this->error("ParentTag: [$ParentTag] not loaded!",0);
                }
                return false;
        }


//        ************************************************************

        function define ($fileList)
        {
                while ( list ($FileTag,$FileName) = each ($fileList) )
                {
                        $this->FILELIST["$FileTag"] = $FileName;
                }
                return true;
        }

//        ************************************************************

        function clear_parse ( $ReturnVar = "")
        {
                $this->clear($ReturnVar);
        }

//        ************************************************************

        function clear ( $ReturnVar = "" )
        {
                // Clears out hash created by call to parse()

                if(!empty($ReturnVar))
                {
                        if( (gettype($ReturnVar)) != "array")
                        {
                                unset($this->$ReturnVar);
                                return;
                        }
                        else
                        {
                                while ( list ($key,$val) = each ($ReturnVar) )
                                {
                                        unset($this->$val);
                                }
                                return;
                        }
                }

                // Empty - clear all of them

                while ( list ( $key,$val) = each ($this->HANDLE) )
                {
                        $KEY = $key;
                        unset($this->$KEY);
                }
                return;

        }        //        end clear()

//        ************************************************************

        function clear_all ()
        {
                $this->clear();
                $this->clear_assign();
                $this->clear_define();
                $this->clear_tpl();

                return;

        }        //        end clear_all

//        ************************************************************

        function clear_tpl ($fileHandle = "")
        {
                if(empty($this->LOADED))
                {
                        // Nothing loaded, nothing to clear

                        return true;
                }
                if(empty($fileHandle))
                {
                        // Clear ALL fileHandles

                        while ( list ($key, $val) = each ($this->LOADED) )
                        {
                                unset($this->$key);
                        }
                        unset($this->LOADED);

                        return true;
                }
                else
                {
                        if( (gettype($fileHandle)) != "array")
                        {
                                if( (isset($this->$fileHandle)) || (!empty($this->$fileHandle)) )
                                {
                                        unset($this->LOADED[$fileHandle]);
                                        unset($this->$fileHandle);
                                        return true;
                                }
                        }
                        else
                        {
                                while ( list ($Key, $Val) = each ($fileHandle) )
                                {
                                        unset($this->LOADED[$Key]);
                                        unset($this->$Key);
                                }
                                return true;
                        }
                }

                return false;

        }        // end clear_tpl

//        ************************************************************

        function clear_define ( $FileTag = "" )
        {
                if(empty($FileTag))
                {
                        unset($this->FILELIST);
                        return;
                }

                if( (gettype($Files)) != "array")
                {
                        unset($this->FILELIST[$FileTag]);
                        return;
                }
                else
                {
                        while ( list ( $Tag, $Val) = each ($FileTag) )
                        {
                                unset($this->FILELIST[$Tag]);
                        }
                        return;
                }
        }

//        ************************************************************
//        Aliased function - used for compatibility with CGI::FastTemplate
 /*       function clear_parse ()
        {
                $this->clear_assign();
        }     */

//        ************************************************************
//        Clears all variables set by assign()

        function clear_assign ()
        {
                if(!(empty($this->PARSEVARS)))
                {
                        while(list($Ref,$Val) = each ($this->PARSEVARS) )
                        {
                                unset($this->PARSEVARS["$Ref"]);
                        }
                }
        }

//        ************************************************************

        function clear_href ($href)
        {
                if(!empty($href))
                {
                        if( (gettype($href)) != "array")
                        {
                                unset($this->PARSEVARS[$href]);
                                return;
                        }
                        else
                        {
                                while (list ($Ref,$val) = each ($href) )
                                {
                                        unset($this->PARSEVARS[$Ref]);
                                }
                                return;
                        }
                }
                else
                {
                        // Empty - clear them all

                        $this->clear_assign();
                }
                return;
        }

//        ************************************************************

        function assign ($tpl_array, $trailer="")
        {
                if(gettype($tpl_array) == "array")
                {
                        while ( list ($key,$val) = each ($tpl_array) )
                        {
                                if (!(empty($key)))
                                {
                                        //        Empty values are allowed
                                        //        Empty Keys are NOT

                                        $this->PARSEVARS["$key"] = $val;
                                }
                        }
                }
                else
                {
                        // Empty values are allowed in non-array context now.
                        if (!empty($tpl_array))
                        {
                                $this->PARSEVARS["$tpl_array"] = $trailer;
                        }
                }
        }

//        ************************************************************
//        Return the value of an assigned variable.
//        Christian Brandel cbrandel@gmx.de

        function get_assigned($tpl_name = "")
        {
                if(empty($tpl_name)) { return false; }
                if(isset($this->PARSEVARS["$tpl_name"]))
                {
                        return ($this->PARSEVARS["$tpl_name"]);
                }
                else
                {
                        return false;
        }
        }

//        ************************************************************

        function error ($errorMsg, $die = 0)
        {
                $this->ERROR = $errorMsg;
                echo "ERROR: $this->ERROR <BR> \n";
                if ($die == 1)
                {
                        exit;
                }

                return;

        } // end error()


//        ************************************************************



//        ************************************************************

} // End class.FastTemplate.php3

?>