-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup.php
More file actions
319 lines (311 loc) · 12.4 KB
/
setup.php
File metadata and controls
319 lines (311 loc) · 12.4 KB
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
<?
# define the IN function
if (!function_exists('in')) {
#
# in(needle, haystack) -> boolean
# - needle (mixed) - what to find in the haystack
# - haystack (mixed) - what to search in
#
# The `in(needle, haystack)` function will return true, if the first argument is a
# part of the second argument. The `haystack` can be numbers, arrays, strings and objects.
#
# Snow:
#
# # will echo "3 is in."
# if 3->in [1, 2, 3]
# echo "3 is in.\n"
#
# # will echo "e is in test".
# if "e"->in "test"
# echo "e is in test.\n"
#
# # will echo "3 is in 10."
# if 3->in 10
# echo "3 is in 10.\n"
#
function in($needle, $haystack) {
switch(gettype($haystack)) {
case 'string': return strpos($haystack, $needle) !== false;
case 'array': return in_array($needle, $haystack);
case 'integer': return $haystack - $needle >= 0;
case 'object': return isset($haystack->$needle);default:return null;
}
}
}
# define the REPLACE function
if (!function_exists('replace')) {
#
# replace(haystack, pattern, replacement) -> mixed
# - haystack (mixed) - where to replace things in
# - pattern (mixed) - what to search for
# - replacement (mixed) - what to replace it with
#
# will replace the `needle` with the `replacement` in the `haystack`. The `needle` can
# be either a regular expression, a normal string. If the `haystack` is an array, the
# `replace()` function will simply replace the `needle` with the `replacement`.
#
# Snow:
#
# # will output "TesT"
# echo "test"->replace 't', 'T'
#
# # will output "Test"
# echo "test"->replace /^[a-z]/, 'T'
#
# # will output "[1, 4, 3]"
# echo [1, 2, 3]->replace(2, 4)->json_encode()
#
function replace($haystack, $pattern, $rep) {
if (is_array($haystack)) {
$pos = array_search($pattern, $haystack, true);
if ($pos === false) return $haystack;
return array_replace($haystack, Array($pos => $rep));
}
$pp = '/^[^a-zA-Z0-9\\\\s].*' . str_replace('/', '\/', $pattern[0]) . '[imsxADSUXJu]*$/m';
if (preg_match($pp, $pattern))
return preg_replace($pattern, $rep, $haystack);
else
return str_replace($pattern, $rep, $haystack);
}
}
# the TEMPLATE function
if (!function_exists('template')) {
#
# template(file, data) -> mixed
# - file (string) - what to use as a template (can be the template name (without .tpl) or the actual template)
# - data (Array) - the data to be used in the template variables
#
# provides a basic templating system with a syntax resembling [Mustache](http://mustache.github.io). The
# first argument `tpl` can either be the template itself or the name of the template file. All template
# file names must end in `.tpl`. The file path provided should be without the ending `.tpl`.
#
# Template `nameList.tpl`:
#
# <h2>Names</h2>
# {{#names}}
# {{> user}}
# {{/names}}
#
# Template `user.tpl`:
#
# <strong>{{name}}</strong>
#
# Snow:
#
# echo template "nameList", ["names": [
# ["name": Carl"],
# [name: "Fred"],
# [name: "Dan"]
# ]]
#
# will output:
#
# <h2>Names</h2>
# <strong>Carl</strong>
# <strong>Fred</strong>
# <strong>Dan</strong>
#
function template($file, $data, $rootCore = '$data', $eval = true, $depth = 0) {
$code = str_replace('\'', '\\\'', (file_exists($file.'.tpl') ? file_get_contents($file . '.tpl') : $file));
# parse a single variable
$parseVar = function($var, $root = '$data') use ($rootCore) {
# if the variable is just a dot, then return the whole root variable
if (trim($var) == '.') return Array($root, $root);
# split the variable name by dot
$parts = explode('.', trim($var));
# if the first part is empty
if ($parts[0] == '') {
# we want the base root
$start = $obj = $rootCore;
} else {
# else just use the current root and add the first part
$start = $root . '[\'' . $parts[0] . '\']';
$obj = $root . "->" . $parts[0];
}
# finally add all other parts
for ($i = 1; $i < count($parts); $i++) {
$start .= '[\'' . $parts[$i]. '\']';
$obj .= "->" . $parts[$i];
}
# returns an array expression and an object expression (p.e. $data['test']['me'], $data->test->me)
return Array($start, $obj);
};
# parse all variables
$parseVars = function($code, $root = '$data') use ($parseVar) {
preg_match_all('/\{\{(?!#\/\^)((?:[^}]|\}[^}])+)\}\}/m', $code, $matches);
foreach ($matches[1] as $key => $match) {
list($var, $obj) = $parseVar($match, $root);
$parts = explode("->", $obj);
$pre = implode("->", array_slice($parts, 0, -1));
$post = array_pop($parts);
# check if the current variable is a function; if so: call it, else just use the variable's value
if ($var === $obj) {
$var = '(is_object('.$var.') && is_callable('.$var.') ? '.$var.'() : '.$var.')';
} else {
$var = '(isset('.$obj.') ? ' . $obj . ' : (isset('.$pre.') && is_callable(Array('.$pre.', \''.$post.'\')) ? ' . $obj . '() : (is_object('.$var.') && is_callable('.$var.') ? '.$var.'() : ' . $var . ')))';
}
if (trim($match) != '.') {
$var = 'function_exists(\''.$match.'\') ? '.$match.'() : ' . $var;
}
# replace the expression in the resulting code
$code = str_replace($matches[0][$key], '\' . (' . $var . ') . \'', $code);
}
return $code;
};
# parse a template include
$parseTpl = function($code, $root = '$data') use ($depth, $file) {
preg_match_all('/\{\{>((?:[^}]|\}[^\}])+)\}\}/m', $code, $matches);
foreach ($matches[1] as $key => $match) {
# recursively parse templates includes via {{>blalbll}}
$code = str_replace($matches[0][$key], template(dirname((file_exists($file.'.tpl') ? $file : __FILE__))."/".trim($match), null, $root, false, $depth + 1), $code);
}
# return the non-evaluated result
return $code;
};
# parse expressions
preg_match_all('/\{\{(#|\^)((?:[^}]|\}[^}])+)\}\}((?:[^{]|\{[^{]|\{\{[^#\^])*)\{\{\/\2\}\}/m', $code, $matches);
foreach ($matches[1] as $key => $match) {
# parse the respective expression's variable
list($var, $obj) = $parseVar($matches[2][$key]);
# if the current expression starts with a ^
$parts = explode("->", $obj);
$pre = implode("->", array_slice($parts, 0, -1));
$post = array_pop($parts);
if ($obj === $var) {
$replacement = '\'; $__l'.$depth.' = (is_object('.$var.') && is_callable('.$var.') ? '.$var.'() : isset('.$var.') ? '.$var.' : \'\');';
} else {
$replacement = '\'; $__l'.$depth.' = (isset('.$obj.') ? '.$obj.' : (isset('.$pre.') && is_callable(Array('.$pre.', \''.$post.'\')) ? '.$obj.'() : (!is_object('.$pre.') && isset('.$var.') ? '.$var.' : \'\')));';
}
if ($match == '^') {
# then we check if the variable is empty
$code = str_replace($matches[0][$key], $replacement . 'if (empty($__l'.$depth.')) { $__p .= \'' . $matches[3][$key] . '\'; } unset($__l'.$depth.');$__p .= \'', $code);
} else if ($match == '#') {
# else we treat it like a loop
$replacement .= 'if (!empty($__l'.$depth.')) { if (is_array($__l'.$depth.')) foreach ($__l'.$depth.' as $counter => $entry'.$depth.') { $__p .= \'';
$replacement .= $parseVars($parseTpl($matches[3][$key], '$entry'.$depth), '$entry'.$depth) . '\'; } else { $__p .= \'' . $matches[3][$key] . '\'; }} unset($__l'.$depth.'); $__p .= \'';
$code = str_replace($matches[0][$key], $replacement, $code);
}
}
$code = $parseVars($parseTpl($code, $rootCore), $rootCore);
# if the result should be evaluated
if ($eval) {
# do the evaluation
$code = '$__p = \'' . $code . '\';';
eval($code);
# and return the resulting string
return $__p;
}
return $code;
}
}
# the DEBUGGER functions
if (!function_exists("debugger")) {
$break = false;
#
# debugger() -> void
#
# This function stops the execution at the current point and renders the current program status (defined variables,
# the current line of code that is executed next, call stack and some controls). The controls can be used to
# resume execution, show the stack trace, do step-wise execution, and stop further execution of the program.
# It is designed to work even without Xdebug and other debugging extensions and works from within a web interface
# and the command line interface.
#
# Pre-condition for the debugger() function to work is, that the Snow code was compiled with the -d compiler
# switch (if using the `snow.php` script) or the third parameter of the SnowCompiler constructor was set to
# `true` (if using a custom script to compile/execute snow code).
#
function debugger() {
global $break;
$break = true;
}
# this is a "private" function for the debugger, which renders a breakpoint
function breakpoint($vars = null) {
global $break;
if (!$break) return;
set_time_limit(600);
# if we're not on the command line
if (PHP_SAPI !== 'cli') {
# we probably are on a web server => prepare the environment to support our implicit flush
if (function_exists("apache_setenv")) @apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
$id = str_replace(".", "-", microtime(true));
# have the background php file & log file be created
$log = '__debug_action_current.log';
$run = '__debug_action_snow.php';
file_put_contents("./$run", "<?php\nfile_put_contents('./$log', json_encode(\$_GET), LOCK_EX);echo base64_decode('R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==');?".">", LOCK_EX);
# and write out the controls for debugging
echo "<div class='__snow_debug' id='__snow_debug$id'><button onclick='new Image().src=\"$run?x=n&l=\"+(+new Date());'>step</button><button onclick='document.getElementById(\"__snow_stack$id\").style.display=\"block\";'>call stack</button><button onclick='new Image().src=\"$run?x=c&l=\"+(+new Date());'>resume</button><button onclick='new Image().src=\"$run?x=t&l=\"+(+new Date());'>stop</button><br/><pre>";
ob_start();
} else {
# we're on the command line => clear the screen so that we see what happens
echo chr(27) . "[2J" . chr(27) . "[;H";
}
$trace = debug_backtrace();
echo $trace[0]['file'].":".(empty($trace[1])?'{main}' : $trace[1]["function"]) . " - Line " . $trace[0]["line"]."\n";
if (file_exists($trace[0]['file'])) {
$file = array_slice(file($trace[0]['file']), max(0, $trace[0]['line'] - 1), 1);
echo preg_replace('/\\bbreakpoint\\(get_defined_vars\\(\\)\\);/m', '', implode("\n", $file));
}
print_r(@array_diff($vars, Array(Array())));
if (PHP_SAPI === 'cli') {
# on the command line, we show the controls after rendering all environment data
$msg = "\nDEBUGGER HALT.\n[RETURN] - step next\n[P]+[RETURN] - print call stack\n[SPACE]+[RETURN] - resume\n";
echo $msg;
# read in the user input in order for him to control the behavior
while ($c=fgets(STDIN, 1024)) {
if ($c == "\n") {
# just return, then next step
return;
} else if ($c == " \n") {
# space + return: resume execution
$break = false;
return;
} else if ($c == "p\n") {
# p + return: print call stack but don't do the next step yet
$e = new Exception();
echo implode("\n", array_slice(explode("\n", $e->getTraceAsString()), 1));
echo $msg;
}
}
} else {
# we're on a web server, so we just print out the stack trace directly
echo ob_get_clean()."</pre><pre id='__snow_stack$id' style='display:none;'>";
$e = new Exception();
echo implode("\n", array_slice(explode("\n", $e->getTraceAsString()), 1));
# clean up the HTML structure printed out
echo "</pre></div>" . str_repeat(" ", 1024);
# flush all prepared output to the client
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
flush();
ob_implicit_flush(1);
$die = false;
# when wait
while (1) {
sleep(.25);
# until we find something useful in the action log file
if (file_exists("$log")) {
$action = @json_decode(file_get_contents($log))->x;
@unlink($log);
# if it's a "c", then we should resume execution
if ($action === "c") $break = false;
# for a "t", we just terminate any further execution
if ($action === "t") { $die = true; }
# for any "n", "c" or "t", we stop waiting and proceed
if (in($action, "nct")) break;
}
}
# ok we need to clean up the debug script
@unlink($run);
# hide the last output
echo "<style type='text/css'>div#__snow_debug$id { display: none; }</style>" . str_repeat(" ", 1024);
# if necessary, terminate further execution
if ($die) die();
# else just flush it out to the client and proceed
flush();
ob_implicit_flush(1);
sleep(.01);
}
set_time_limit(30);
}
}