Campsite /campsite/src/include/phorum/include/templates.php

Language PHP Lines 381
MD5 Hash 1464a075e513531562ad20e266ac055b
Repository https://github.com/joechrysler/Campsite.git View Raw File View Project SPDX
  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
<?php

////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//   Copyright (C) 2006  Phorum Development Team                              //
//   http://www.phorum.org                                                    //
//                                                                            //
//   This program is free software. You can redistribute it and/or modify     //
//   it under the terms of either the current Phorum License (viewable at     //
//   phorum.org) or the Phorum License that was distributed with this file    //
//                                                                            //
//   This program is distributed in the hope that it will be useful,          //
//   but WITHOUT ANY WARRANTY, without even the implied warranty of           //
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     //
//                                                                            //
//   You should have received a copy of the Phorum License                    //
//   along with this program.                                                 //
////////////////////////////////////////////////////////////////////////////////

if(!defined("PHORUM")) return;

// For keeping track of include dependancies, which
// are used to let templates automatically rebuild
// in case an included subtemplate has been changed.
$include_level = 0;
$include_deps  = array();

function phorum_import_template($tplfile, $outfile)
{
    global $include_level, $include_deps;
    $include_level++;

    // Remember that we used this template.
    $include_deps[$tplfile] = $outfile;

    // In case we're handling 0 byte large files, we set $page
    // directly. Running fread($fp, 0) gives a PHP warning.
    if (filesize($tplfile)) {
        $fp=fopen($tplfile, "r");
        $page=fread($fp, filesize($tplfile));
        fclose($fp);
    } else {
        $page = '';
    }

    preg_match_all("/\{[\!\/A-Za-z].+?\}/s", $page, $matches);

    settype($oldloopvar, "string");
    settype($loopvar, "string");
    settype($olddatavar, "string");
    settype($datavar, "string");
    $loopvars = array();

    foreach($matches[0] as $match){
        unset($parts);

        $string=substr($match, 1, -1);

        $string = trim($string);

        // pre-parse pointer variables
        if(strstr($string, "->")){
            $string=str_replace("->", "']['", $string);
        }

        $parts=explode(" ", $string);

        switch(strtolower($parts[0])){

            // Comment
            case "!":

            $repl="<?php // ".implode(" ", $parts)." ?>";
            break;


            case "include":

            $repl = file_get_contents(phorum_get_template($parts[1],1));
            break;

            case "include_once":

            $repl="<?php include_once phorum_get_template('$parts[1]'); ?>";
            break;

            case "include_var": // include a file given by a variable

            $repl="<?php include_once phorum_get_template( \$PHORUM[\"DATA\"]['$parts[1]']); ?>";
            break;

            // A define is used to create vars for the engine to use.
            case "define":

            $repl="<?php \$PHORUM[\"TMP\"]['$parts[1]']='";
            array_shift($parts);
            array_shift($parts);
            foreach($parts as $part){
                $repl.=str_replace("'", "\\'", $part)." ";
            }
            $repl=trim($repl)."'; ?>";
            break;


            // A var is used to create vars for the template.
            case "var":

            $repl="<?php \$PHORUM[\"DATA\"]['$parts[1]']='";
            array_shift($parts);
            array_shift($parts);
            foreach($parts as $part){
                $repl.=str_replace("'", "\\'", $part)." ";
            }
            $repl=trim($repl)."'; ?>";
            break;

            // Run a Phorum hook. The first parameter is the name of the
            // hook. Other parameters will be passed on as arguments for
            // the hook function. On argument will be passed directly to
            // the hook. Multiple arguments will be passed in an array.
            case "hook":

            // Setup hook arguments.
            $hookargs = array();
            for($i = 2; !empty($parts[$i]); $i++) {
                // For supporting the following construct, where the
                // loopvar is passed to the hook in full:
                // {LOOP SOMELIST}
                //   {HOOK some_hook SOMELIST}
                // {/LOOP SOMELIST}
                if (isset($loopvars[$parts[$i]])) {
                    $hookargs[] = "\$PHORUM['TMP']['".addslashes($parts[$i])."']";
                } else {
                    $index = phorum_determine_index($loopvars, $parts[$i]);
                    $hookargs[] = "\$PHORUM['$index']['".addslashes($parts[$i])."']";
                }
            }

            // Build the replacement string.
            $repl = "<?php if(isset(\$PHORUM['hooks']['".addslashes($parts[1])."'])) phorum_hook('".addslashes($parts[1])."'";
            if (count($hookargs) == 1) {
                $repl .= "," . $hookargs[0];
            } elseif (count($hookargs) > 1) {
                $repl .= ",array(" . implode(",", $hookargs) . ")";
            }
            $repl .= ");?>";
            break;

            // starts a loop
            case "loop":

            $loopvars[$parts[1]]=true;
            $index=phorum_determine_index($loopvars, $parts[1]);
            $repl="<?php \$phorum_loopstack[] = isset(\$PHORUM['TMP']['$parts[1]']) ? \$PHORUM['TMP']['$parts[1]']:NULL; if(isset(\$PHORUM['$index']['$parts[1]']) && is_array(\$PHORUM['$index']['$parts[1]'])) foreach(\$PHORUM['$index']['$parts[1]'] as \$PHORUM['TMP']['$parts[1]']){ ?>";
            break;


            // ends a loop
            case "/loop":

            if (!isset($parts[1])) print "<h3>Template warning: Missing argument for /loop statement in file '" . htmlspecialchars($tplfile) . "'</h3>";
            $repl="<?php } if(isset(\$PHORUM['TMP']) && isset(\$PHORUM['TMP']['$parts[1]'])) unset(\$PHORUM['TMP']['$parts[1]']); \$phorum_loopstackitem=array_pop(\$phorum_loopstack); if (isset(\$phorum_loopstackitem)) \$PHORUM['TMP']['$parts[1]'] = \$phorum_loopstackitem;?>";
            unset($loopvars[$parts[1]]);
            break;


            // if and elseif are the same accept how the line starts
            case "if":
            case "elseif":

            // determine if or elseif
            $prefix = (strtolower($parts[0])=="if") ? "if" : "} elseif";

            // are we wanting == or !=
            if(strtolower($parts[1])=="not"){
                $operator="!=";
                $parts[1]=$parts[2];
                if(isset($parts[3])){
                    $parts[2]=$parts[3];
                    unset($parts[3]);
                } else {
                    unset($parts[2]);
                }
            } else {
                $operator="==";
            }

            $index=phorum_determine_index($loopvars, $parts[1]);

            // if there is no part 2, check that the value is set and not empty
            if(!isset($parts[2])){
                if($operator=="=="){
                    $repl="<?php $prefix(isset(\$PHORUM['$index']['$parts[1]']) && !empty(\$PHORUM['$index']['$parts[1]'])){ ?>";
                } else {
                    $repl="<?php $prefix(!isset(\$PHORUM['$index']['$parts[1]']) || empty(\$PHORUM['$index']['$parts[1]'])){ ?>";
                }

                // if it is numeric, a constant or a string, simply set it as is
            } elseif(is_numeric($parts[2]) || defined($parts[2]) || preg_match('!"[^"]*"!', $parts[2])) {
                $repl="<?php $prefix(isset(\$PHORUM['$index']['$parts[1]']) && \$PHORUM['$index']['$parts[1]']$operator$parts[2]){ ?>";

                // we must have a template var
            } else {

                $index_part2=phorum_determine_index($loopvars, $parts[2]);

                // this is a really complicated IF we are building.

                $repl="<?php $prefix(isset(\$PHORUM['$index']['$parts[1]']) && isset(\$PHORUM['$index_part2']['$parts[2]']) && \$PHORUM['$index']['$parts[1]']$operator\$PHORUM['$index_part2']['$parts[2]']) { ?>";

            }

            // reset $prefix
            $prefix="";
            break;


            // create an else
            case "else":

            $repl="<?php } else { ?>";
            break;


            // close an if
            case "/if":

            $repl="<?php } ?>";
            break;

            case "assign":
            if(defined($parts[2]) || is_numeric($parts[2])){
                $repl="<?php \$PHORUM[\"DATA\"]['$parts[1]']=$parts[2]; ?>";
            } else {
                $index=phorum_determine_index($loopvars, $parts[2]);

                $repl="<?php \$PHORUM[\"DATA\"]['$parts[1]']=\$PHORUM['$index']['$parts[2]']; ?>";
            }
            break;


            // this is just for echoing vars from DATA or TMP if it is a loopvar
            default:

            if(defined($parts[0])){
                $repl="<?php echo $parts[0]; ?>";
            } else {

                $index=phorum_determine_index($loopvars, $parts[0]);

                $repl="<?php echo \$PHORUM['$index']['$parts[0]']; ?>";
            }
        }

        $page=str_replace($match, $repl, $page);
    }

    $include_level--;

    // Did we finish processing our top level template? Then write out
    // the compiled template to the cache.
    //
    // For storing the compiled template, we use two files. The first one
    // has some code for checking if one of the dependant files has been
    // updated and for rebuilding the template if this is the case.
    // This one loads the second file, which is the template itself.
    //
    // This two-stage loading is needed to make sure that syntax
    // errors in a template file won't break the depancy checking process.
    // If both were in the same file, the complete file would not be run
    // at all and the user would have to clean out the template cache to
    // reload the template once it was fixed. This way user intervention
    // is never needed.
    if ($include_level == 0)
    {
        // Find the template name for the top level template.
        $pathparts = preg_split('[\\/]', $outfile);
        $fileparts = explode('-', preg_replace('/^.*\//', '', $pathparts[count($pathparts)-1]));
        $this_template = addslashes($fileparts[2]);

        // Determine first and second stage cache filenames.
        $stage1_file = $outfile;
        $fileparts[3] = "toplevel_stage2";
        unset($pathparts[count($pathparts)-1]);
        $stage2_file = implode('/', $pathparts) . '/' . implode('-', $fileparts);

        // Create code for automatic rebuilding of rendered templates
        // in case of changes. This is done by checking if one of the
        // templates in the dependancy list has been updated. If this
        // is the case, all dependant rendered subtemplates are deleted.
        // After that phorum_get_template() is called on the top level
        // template to rebuild all needed templates.

        $check_deps =
            "<?php\n" .
            '$mymtime = @filemtime("' . addslashes($stage1_file) . '");' . "\n" .
            "\$update_count = 0;\n" .
            "\$need_update = (\n";
        foreach ($include_deps as $tpl => $out) {
            $qtpl = addslashes($tpl);
            $check_deps .= "    @filemtime(\"$qtpl\") > \$mymtime ||\n";
        }
        $check_deps = substr($check_deps, 0, -4); // strip trailing " ||\n"
        $check_deps .=
        "\n" .
        ");\n" .
        "if (\$need_update) {\n";
        foreach ($include_deps as $tpl => $out) {
            $qout = addslashes($out);
            $check_deps .= "    @unlink(\"$qout\");\n";
        }
        $check_deps .=
        "    \$tplfile = phorum_get_template(\"$this_template\");\n" .
        "}\n" .
        "include(\"" . addslashes($stage2_file) . "\");\n" .
        "?>\n";

        // Reset dependancy list for the next phorum_import_template() call.
        $include_deps = array();

        // Write out data to the cache.
        phorum_write_templatefile($stage1_file, $check_deps);
        phorum_write_templatefile($stage2_file, $page, true);
    }
    else
    {
        // Write out subtemplate to the cache.
        phorum_write_templatefile($outfile, $page);
    }


}

function phorum_write_templatefile($filename, $content, $is_toplevel = false)
{
    if($fp=fopen($filename, "w")) {
        fputs($fp, "<?php if(!defined(\"PHORUM\")) return; ?>\n");
        if ($is_toplevel) {
            fputs($fp, "<?php \$phorum_loopstack = array() ?>\n");
        }
        fputs($fp, $content);
        if (! fclose($fp)) {
            die("Error on closing $filename. Is your disk full?");
        }
        // Some very unusual thing might happen. On Windows2000 we have seen
        // that the webserver can write a message to the cache directory,
        // but that it cannot read it afterwards. Probably due to 
        // specific NTFS file permission settings. So here we have to make
        // sure that we can open the file that we just wrote.
        $checkfp = fopen($filename, "r");
        if (! $checkfp) {
            die("Failed to write a usable compiled template to $filename. " .
                "The file was was created successfully, but it could not " .
                "be read by the webserver afterwards. This is probably " .
                "caused by the file permissions on your cache directory.");
        }
        fclose($checkfp);
    } else {
        die("Failed to write a compiled template to $filename. This is " .
            "probably caused by the file permissions on your cache " .
            "directory.");
    }
}

function phorum_determine_index($loopvars, $varname)
{
    if(isset($loopvars) && count($loopvars)){
        while(strstr($varname, "]")){
            $varname=substr($varname, 0, strrpos($varname, "]")-1);
            if(isset($loopvars[$varname])){
                return "TMP";
                break;
            }
        }
    }

    return "DATA";
}

?>
Back to Top