summaryrefslogtreecommitdiff
path: root/src/ste/STECore.php
blob: a23261606bcc80725d84f90410a151cdca7ad187 (plain)
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
<?php

namespace kch42\ste;

use Exception;

/**
 * The Core of STE
 */
class STECore
{
    /** @var callable[] */
    private $tags;

    /** @var StorageAccess */
    private $storage_access;

    /** @var string */
    private $cur_tpl_dir;

    /** @var Scope */
    public $scope;

    /**
     * @var array
     * Associative array of blocks (see the language definition).
     */
    public $blocks;

    /**
     * @var array
     * The order of the blocks (an array)
     */
    public $blockorder;

    /**
     * @var bool
     * If true (default) a {@see RuntimeError} exception will result in no
     * output from the tag, if false a error message will be written to output.
     */
    public $mute_runtime_errors = true;

    /**
     * @var bool
     * If true, STE will throw a {@see FatalRuntimeError} if a tag was called
     * that was not registered, otherwise (default) a regular
     * {@see RuntimeError} will be thrown and automatically handled by STE
     * (see {@see STECore::$mute_runtime_errors}).
     */
    public $fatal_error_on_missing_tag = false;

    /**
     * @var array
     * Variables in the top scope of the template.
     */
    public $vars;

    /**
     * @param StorageAccess $storage_access
     */
    public function __construct(StorageAccess $storage_access)
    {
        $this->storage_access = $storage_access;
        $this->cur_tpl_dir = "/";
        STEStandardLibrary::_register_lib($this);
        $this->blockorder = [];
        $this->blocks = [];

        $this->vars = [];
        $this->scope = new Scope();
        $this->scope->vars =& $this->vars;
    }

    /**
     * Register a custom tag.
     *
     * Parameters:
     * @param string $name The name of the tag.
     * @param callable $callback A callable function
     *                           Must take three parameters:
     *
     *                           The {@see STECore} instance,
     *                           an associative array of parameters,
     *                           and a function representing the tags content
     *                           (This expects the {@see STECore} instance as
     *                           its only parameter and returns its text result,
     *                           i.e to get the text, you need to call this
     *                           function with the {@see STECore} instance as a
     *                           parameter).
     *
     * @throws Exception If the tag could not be registered (if $callback is not callable or if $name is empty)
     */
    public function register_tag(string $name, callable $callback): void
    {
        if (!is_callable($callback)) {
            throw new Exception("Can not register tag \"$name\", not callable.");
        }
        if (empty($name)) {
            throw new Exception("Can not register tag, empty name.");
        }
        $this->tags[$name] = $callback;
    }

    /**
     * Calling a custom tag (builtin ones can not be called)
     *
     * @param string $name The Tag's name
     * @param array $params Associative array of parameters
     * @param callable $sub A callable function (expecting an {@see STECore} instance as it's parameter) that represents the tag's content.
     *
     * @throws FatalRuntimeError see {@see STECore::$fatal_error_on_missing_tag}.
     *
     * @return string The output of the tag or, if a {@see RuntimeError} was thrown, the appropriate result
     *                (see {@see STECore::$mute_runtime_errors}).
     */
    public function call_tag(string $name, array $params, callable $sub): string
    {
        try {
            if (!isset($this->tags[$name])) {
                if ($this->fatal_error_on_missing_tag) {
                    throw new FatalRuntimeError("Can not call tag \"$name\": Does not exist.");
                } else {
                    throw new RuntimeError("Can not call tag \"$name\": Does not exist.");
                }
            }
            return call_user_func($this->tags[$name], $this, $params, $sub);
        } catch (RuntimeError $e) {
            if (!$this->mute_runtime_errors) {
                return "RuntimeError occurred on tag '$name': " . $e->getMessage();
            }
        }

        return "";
    }

    /**
     * {@see Calc::calc()}
     *
     * @param string $expression
     * @return float|int
     * @throws RuntimeError
     */
    public function calc(string $expression)
    {
        return Calc::calc($expression);
    }

    /**
     * Executes a template and returns the result.
     *
     * The huge difference to {@see STECore::load()} is that this function will also output all blocks.
     *
     * @param string $tpl The name of the template to execute.
     *
     * @throws CantLoadTemplate If the template could not be loaded.
     * @throws CantSaveTemplate If the template could not be saved (after compilation).
     * @throws ParseCompileError If the template could not be parsed or compiled.
     * @throws FatalRuntimeError If a tag threw it or if a tag was not found and <$fatal_error_on_missing_tag> is true.
     *
     * Might also throw different exceptions, if a external tag threw it
     * (but they should use {@see RuntimeError} or {@see FatalRuntimeError} to make it possible for STE to handle them correctly).
     *
     * @return string The output of the template.
     */
    public function exectemplate(string $tpl): string
    {
        $output = "";
        $lastblock = $this->load($tpl);

        foreach ($this->blockorder as $blockname) {
            $output .= $this->blocks[$blockname];
        }

        return $output . $lastblock;
    }

    /**
     * Get a reference to a template variable using a variable name.
     * This can be used,if your custom tag takes a variable name as a parameter.
     *
     * @param string $name The variables name.
     * @param bool $create_if_not_exist Should the variable be created, if it does not exist? Otherwise NULL will be returned, if the variable does not exist.
     *
     * @throws RuntimeError If the variable name can not be parsed (e.g. unbalanced brackets).
     * @return mixed A Reference to the variable.
     */
    public function &get_var_reference(string $name, bool $create_if_not_exist)
    {
        $ref = &$this->scope->get_var_reference($name, $create_if_not_exist);
        return $ref;
    }

    /**
     * Set a template variable by its name.
     * This can be used,if your custom tag takes a variable name as a parameter.
     *
     * @param string $name The variables name.
     * @param mixed $val The new value.
     *
     * @throws RuntimeError If the variable name can not be parsed (e.g. unbalanced brackets).
     */
    public function set_var_by_name(string $name, $val): void
    {
        $this->scope->set_var_by_name($name, $val);
    }

    /**
     * Like {@see STECore::set_var_by_name}, but only sets the variable in the global scope
     * ({@see STECore::set_var_by_name} will overwrite the variable in the parent scope, if it's defined there) .
     *
     * @param string $name The variables name.
     * @param mixed $val The new value.
     *
     * @throws RuntimeError If the variable name can not be parsed (e.g. unbalanced brackets).
     */
    public function set_local_var(string $name, $val): void
    {
        $this->scope->set_local_var($name, $val);
    }

    /**
     * Get a template variable by its name.
     * This can be used,if your custom tag takes a variable name as a parameter.
     *
     * @param string $name The variables name.
     *
     * @return mixed The variables value
     * @throws RuntimeError If the variable name can not be parsed (e.g. unbalanced brackets).
     */
    public function get_var_by_name(string $name)
    {
        return $this->scope->get_var_by_name($name);
    }

    /**
     * Load a template and return its result (blocks not included, use {@see STECore::exectemplate} for this).
     *
     * @param string $tpl The name of the template to be loaded.
     * @param bool $quiet If true, do not output anything and do not modify the blocks. This can be useful to load custom tags that are programmed in the STE Template Language. Default: false.
     *
     * @throws CantLoadTemplate If the template could not be loaded.
     * @throws CantSaveTemplate If the template could not be saved.
     * @throws ParseCompileError If the template could not be parsed or compiled.
     * @throws FatalRuntimeError If a tag threw it or if a tag was not found and {@see STECore::$fatal_error_on_missing_tag} is true.
     *
     * Might also throw different exceptions, if a external tag threw it
     * (but they should use {@see RuntimeError} or {@see FatalRuntimeError} to make it possible for STE to handle them correctly).
     *
     * @return string|null The result of the template (if $quiet == false).
     */
    public function load(string $tpl, bool $quiet = false): ?string
    {
        $tpldir_b4 = $this->cur_tpl_dir;

        /* Resolve ".", ".." and protect from possible LFI */
        $tpl = str_replace("\\", "/", $tpl);
        if ($tpl[0] != "/") {
            $tpl = $this->cur_tpl_dir . "/" . $tpl;
        }
        $pathex = array_filter(explode("/", $tpl), function ($s) {
            return $s != "." && !empty($s);
        });
        $pathex = array_merge($pathex);
        while (($i = array_search("..", $pathex)) !== false) {
            if ($i == 0) {
                $pathex = array_slice($pathex, 1);
            } else {
                $pathex = array_merge(array_slice($pathex, 0, $i), array_slice($pathex, $i + 2));
            }
        }
        $tpl = implode("/", $pathex);
        $this->cur_tpl_dir = dirname($tpl);

        if ($quiet) {
            $blocks_back     = $this->blocks;
            $blockorder_back = $this->blockorder;
        }

        $mode = StorageAccess::MODE_TRANSCOMPILED;
        $content = $this->storage_access->load($tpl, $mode);
        if ($mode == StorageAccess::MODE_SOURCE) {
            try {
                $ast     = Parser::parse($content, $tpl);
                $transc  = Transcompiler::transcompile($ast);
            } catch (ParseCompileError $e) {
                $e->rewrite($content);
                throw $e;
            }
            $this->storage_access->save($tpl, $transc, StorageAccess::MODE_TRANSCOMPILED);
            eval("\$content = $transc;");
        }

        $output = $content($this);

        $this->cur_tpl_dir = $tpldir_b4;

        if ($quiet) {
            $this->blocks     = $blocks_back;
            $this->blockorder = $blockorder_back;

            return null;
        } else {
            return $output;
        }
    }

    /**
     * Test, if a text represents false (an empty / only whitespace text) or true (everything else).
     *
     * @param mixed $txt The text to test.
     *
     * @return bool
     */
    public function evalbool($txt): bool
    {
        return trim(@(string)$txt) != "";
    }

    /**
     * Internal function for implementing tag content and custom tags.
     *
     * @param callable $fx
     * @return \Closure
     */
    public function make_closure(callable $fx): \Closure
    {
        $bound_scope = $this->scope;
        return function () use ($bound_scope, $fx) {
            $args = func_get_args();
            $ste = $args[0];

            $prev = $ste->scope;
            $scope = $bound_scope->new_subscope();
            $ste->scope = $scope;

            try {
                $result = call_user_func_array($fx, $args);
                $ste->scope = $prev;
                return $result;
            } catch (Exception $e) {
                $ste->scope = $prev;
                throw $e;
            }
        };
    }
}