-
Notifications
You must be signed in to change notification settings - Fork 9
/
common.php
634 lines (500 loc) · 22.5 KB
/
common.php
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
<?php
# try to load the wordpress environment from passed in location known by the client
# otherwise, fall back to the mostly (but sometimes not) safe assumption that it is
# up 3 directories from this file.
if ( isset( $_POST["root"] ) && is_dir( $_POST["root"] ) ) {
require_once( $_POST["root"] . "/wp-load.php" );
} else {
require_once( dirname( __FILE__ ) . "/../../../wp-load.php" );
}
require_once( ABSPATH . "wp-admin/includes/admin.php" );
if ( !session_id() ) {
session_start();
}
if ( ob_get_length() > 0 ) {
ob_end_clean();
}
error_reporting( E_ALL ^ E_PARSE );
set_time_limit( 0 );
if ( !function_exists( "json_encode" ) ) {
function json_encode( $value ) {
require_once( "lib/FastJSON.class.php" );
return FastJSON::encode($value);
}
}
function console_error_handler( $errno, $errorstr ) {
error( $errorstr );
}
function error( $error ) {
exit( json_encode( array( "error" => $error ) ) );
}
function logit( $msg ) {
$file = "/tmp/console.log";
$fh = fopen($file,"a");
fwrite($fh,$msg);
fwrite($fh,"\n\n");
fclose($fh);
}
// saves newly defined variables to session.
// somebody please refactor this!
function save_variables( $existing, $current, $ignore ) {
$new_vars = array_diff( array_keys( $current ), array_keys( $existing ) );
$user_vars = array_diff( $new_vars, $ignore );
$save_vars = array();
foreach( $current as $key => $value ) {
if ( in_array( $key, $user_vars ) ) {
$save_vars[$key] = $value;
}
}
$export = var_export( $save_vars, true );
// special consideration for variables that are objects
// see: http://www.thoughtlabs.com/2008/02/02/phps-mystical-__set_state-method/
$export = preg_replace_callback( "/(\w+)::__set_state/Ums", "class_set_state_check", $export );
$_SESSION["console_vars"] = $export;
}
// classes to be restored need to implement __set_state() function.
// if they don't have it, we will convert to stdClass object.
function class_set_state_check($matches) {
if (method_exists($matches[1], "__set_state")) {
return $matches[0];
} else {
return "(object) ";
}
}
// this function was yoinked (and adjusted) from the 'php shell' project. See:
// http://jan.kneschke.de/projects/php-shell
// return int 0 if a executable statement is in the session buffer, non-zero otherwise
function parse( $code ) {
## remove empty lines
if (trim($code) == "") return 1;
$t = token_get_all("<?php ".$code." ?>");
// logit($code);
$need_semicolon = 1; /* do we need a semicolon to complete the statement ? */
$need_return = 1; /* can we prepend a return to the eval-string ? */
$open_comment = 0; /* a open multi-line comment */
$eval = ""; /* code to be eval()'ed later */
$braces = array(); /* to track if we need more closing braces */
$methods = array(); /* to track duplicate methods in a class declaration */
$ts = array(); /* tokens without whitespaces */
foreach ($t as $ndx => $token) {
if (is_array($token)) {
$ignore = 0;
switch($token[0]) {
case T_WHITESPACE:
case T_OPEN_TAG:
case T_CLOSE_TAG:
$ignore = 1;
break;
case T_FOREACH:
case T_DO:
case T_WHILE:
case T_FOR:
case T_IF:
case T_RETURN:
case T_CLASS:
case T_FUNCTION:
case T_INTERFACE:
case T_PRINT:
case T_ECHO:
case T_COMMENT:
case T_UNSET:
case T_INCLUDE:
case T_REQUIRE:
case T_INCLUDE_ONCE:
case T_REQUIRE_ONCE:
case T_TRY:
case T_SWITCH:
case T_DEFAULT:
case T_CASE:
case T_BREAK:
case T_DOC_COMMENT:
$need_return = 0;
break;
case T_EMPTY:
case T_ISSET:
case T_EVAL:
case T_EXIT:
case T_VARIABLE:
case T_STRING:
case T_NEW:
case T_EXTENDS:
case T_IMPLEMENTS:
case T_OBJECT_OPERATOR:
case T_DOUBLE_COLON:
case T_INSTANCEOF:
case T_CATCH:
case T_THROW:
case T_ELSE:
case T_AS:
case T_LNUMBER:
case T_DNUMBER:
case T_CONSTANT_ENCAPSED_STRING:
case T_ENCAPSED_AND_WHITESPACE:
case T_CHARACTER:
case T_ARRAY:
case T_DOUBLE_ARROW:
case T_CONST:
case T_PUBLIC:
case T_PROTECTED:
case T_PRIVATE:
case T_ABSTRACT:
case T_STATIC:
case T_VAR:
case T_INC:
case T_DEC:
case T_SL:
case T_SL_EQUAL:
case T_SR:
case T_SR_EQUAL:
case T_IS_EQUAL:
case T_IS_IDENTICAL:
case T_IS_GREATER_OR_EQUAL:
case T_IS_SMALLER_OR_EQUAL:
case T_BOOLEAN_OR:
case T_LOGICAL_OR:
case T_BOOLEAN_AND:
case T_LOGICAL_AND:
case T_LOGICAL_XOR:
case T_MINUS_EQUAL:
case T_PLUS_EQUAL:
case T_MUL_EQUAL:
case T_DIV_EQUAL:
case T_MOD_EQUAL:
case T_XOR_EQUAL:
case T_AND_EQUAL:
case T_OR_EQUAL:
case T_FUNC_C:
case T_CLASS_C:
case T_LINE:
case T_FILE:
case T_BOOL_CAST:
case T_INT_CAST:
case T_STRING_CAST:
/* just go on */
break;
default:
/* debug unknown tags*/
error_log(sprintf("unknown tag: %d (%s): %s".PHP_EOL, $token[0], token_name($token[0]), $token[1]));
break;
}
if (!$ignore) {
$eval .= $token[1]." ";
$ts[] = array("token" => $token[0], "value" => $token[1]);
}
} else {
$ts[] = array("token" => $token, "value" => "");
$last = count($ts) - 1;
switch ($token) {
case "(":
/* walk backwards through the tokens */
if ($last >= 4 &&
$ts[$last - 1]["token"] == T_STRING &&
$ts[$last - 2]["token"] == T_OBJECT_OPERATOR &&
$ts[$last - 3]["token"] == ")" ) {
/* func()->method()
*
* we can't know what func() is return, so we can't
* say if the method() exists or not
*
*/
} else if ($last >= 3 &&
$ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
$ts[0]["token"] != T_ABSTRACT && /* if we are not in a class definition */
$ts[1]["token"] != T_CLASS && /* if we are not in a class definition */
$ts[$last - 1]["token"] == T_STRING &&
$ts[$last - 2]["token"] == T_OBJECT_OPERATOR &&
$ts[$last - 3]["token"] == T_VARIABLE ) {
/* $object->method( */
/* catch (Exception $e) does not set $e in $GLOBALS[] */
$in_catch = 0;
foreach ($ts as $v) {
if ($v["token"] == T_CATCH) {
$in_catch = 1;
}
}
if (!$in_catch) {
/* $object has to exist and has to be a object */
$objname = $ts[$last - 3]["value"];
if (!isset($GLOBALS[ltrim($objname, "$")])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $objname));
}
$object = $GLOBALS[ltrim($objname, "$")];
if (!is_object($object)) {
throw new Exception(sprintf('Variable \'%s\' is not a class', $objname));
}
$method = $ts[$last - 1]["value"];
/* obj */
if (!method_exists($object, $method)) {
throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'",
$objname, get_class($object), $method));
}
}
} else if ($last >= 3 &&
$ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
$ts[$last - 1]["token"] == T_VARIABLE &&
$ts[$last - 2]["token"] == T_OBJECT_OPERATOR &&
$ts[$last - 3]["token"] == T_VARIABLE ) {
/* $object->$method( */
/* $object has to exist and has to be a object */
$objname = $ts[$last - 3]["value"];
if (!isset($GLOBALS[ltrim($objname, "$")])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $objname));
}
$object = $GLOBALS[ltrim($objname, "$")];
if (!is_object($object)) {
throw new Exception(sprintf('Variable \'%s\' is not a class', $objname));
}
$methodname = $ts[$last - 1]["value"];
if (!isset($GLOBALS[ltrim($methodname, "$")])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $methodname));
}
$method = $GLOBALS[ltrim($methodname, "$")];
/* obj */
if (!method_exists($object, $method)) {
throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'",
$objname, get_class($object), $method));
}
} else if ($last >= 6 &&
$ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
$ts[$last - 1]["token"] == T_STRING &&
$ts[$last - 2]["token"] == T_OBJECT_OPERATOR &&
$ts[$last - 3]["token"] == "]" &&
/* might be anything as index */
$ts[$last - 5]["token"] == "[" &&
$ts[$last - 6]["token"] == T_VARIABLE ) {
/* $object[...]->method( */
/* $object has to exist and has to be a object */
$objname = $ts[$last - 6]["value"];
if (!isset($GLOBALS[ltrim($objname, "$")])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $objname));
}
$array = $GLOBALS[ltrim($objname, "$")];
if (!is_array($array)) {
throw new Exception(sprintf('Variable \'%s\' is not a array', $objname));
}
$andx = $ts[$last - 4]["value"];
if (!isset($array[$andx])) {
throw new Exception(sprintf('%s[\'%s\'] is not set', $objname, $andx));
}
$object = $array[$andx];
if (!is_object($object)) {
throw new Exception(sprintf('Variable \'%s\' is not a class', $objname));
}
$method = $ts[$last - 1]["value"];
/* obj */
if (!method_exists($object, $method)) {
throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'",
$objname, get_class($object), $method));
}
} else if ($last >= 3 &&
$ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
$ts[$last - 1]["token"] == T_STRING &&
$ts[$last - 2]["token"] == T_DOUBLE_COLON &&
$ts[$last - 3]["token"] == T_STRING ) {
/* Class::method() */
/* $object has to exist and has to be a object */
$classname = $ts[$last - 3]["value"];
if (!class_exists($classname)) {
throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname));
}
$method = $ts[$last - 1]["value"];
if (!in_array($method, get_class_methods($classname))) {
throw new Exception(sprintf("Class '%s' doesn't have a method named '%s'",
$classname, $method));
}
} else if ($last >= 3 &&
$ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
$ts[$last - 1]["token"] == T_VARIABLE &&
$ts[$last - 2]["token"] == T_DOUBLE_COLON &&
$ts[$last - 3]["token"] == T_STRING ) {
/* $var::method() */
/* $object has to exist and has to be a object */
$classname = $ts[$last - 3]["value"];
if (!class_exists($classname)) {
throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname));
}
$methodname = $ts[$last - 1]["value"];
if (!isset($GLOBALS[ltrim($methodname, "$")])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $methodname));
}
$method = $GLOBALS[ltrim($methodname, "$")];
if (!in_array($method, get_class_methods($classname))) {
throw new Exception(sprintf("Class '%s' doesn't have a method named '%s'",
$classname, $method));
}
} else if ($last >= 2 &&
$ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
$ts[$last - 1]["token"] == T_STRING &&
$ts[$last - 2]["token"] == T_NEW ) {
/* new Class() */
/* don't care about this in a class ... { ... } */
$classname = $ts[$last - 1]["value"];
if (!class_exists($classname)) {
throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname));
}
$r = new ReflectionClass($classname);
if ($r->isAbstract()) {
throw new Exception(sprintf("Can't instantiate abstract Class '%s'", $classname));
}
if (!$r->isInstantiable()) {
throw new Exception(sprintf('Class \'%s\' can\'t be instantiated. Is the class abstract ?', $classname));
}
} else if ($last >= 2 &&
$ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
$ts[$last - 1]["token"] == T_STRING &&
$ts[$last - 2]["token"] == T_FUNCTION ) {
/* make sure we are not a in class definition */
/* function a() */
$func = $ts[$last - 1]["value"];
if (function_exists($func)) {
throw new Exception(sprintf('Function \'%s\' is already defined', $func));
}
} else if ($last >= 4 &&
$ts[0]["token"] == T_CLASS &&
$ts[1]["token"] == T_STRING &&
$ts[$last - 1]["token"] == T_STRING &&
$ts[$last - 2]["token"] == T_FUNCTION ) {
/* make sure we are not a in class definition */
/* class a { .. function a() ... } */
$func = $ts[$last - 1]["value"];
$classname = $ts[1]["value"];
if (isset($methods[$func])) {
throw new Exception(sprintf("Can't redeclare method '%s' in Class '%s'", $func, $classname));
}
$methods[$func] = 1;
} else if ($last >= 1 &&
$ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
$ts[0]["token"] != T_ABSTRACT && /* if we are not in a class definition */
$ts[1]["token"] != T_CLASS && /* if we are not in a class definition */
$ts[$last - 1]["token"] == T_STRING ) {
/* func() */
$funcname = $ts[$last - 1]["value"];
if (!function_exists($funcname)) {
throw new Exception(sprintf("Function %s() doesn't exist", $funcname));
}
} else if ($last >= 1 &&
$ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
$ts[$last - 1]["token"] == T_VARIABLE ) {
/* $object has to exist and has to be a object */
$funcname = $ts[$last - 1]["value"];
if (!isset($GLOBALS[ltrim($funcname, "$")])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $funcname));
}
$func = $GLOBALS[ltrim($funcname, "$")];
if (!function_exists($func)) {
throw new Exception(sprintf("Function %s() doesn't exist", $func));
}
}
array_push($braces, $token);
break;
case "{":
$need_return = 0;
if ($last >= 2 &&
$ts[$last - 1]["token"] == T_STRING &&
$ts[$last - 2]["token"] == T_CLASS ) {
/* class name { */
$classname = $ts[$last - 1]["value"];
if (class_exists($classname, false)) {
throw new Exception(sprintf("Class '%s' can't be redeclared", $classname));
}
} else if ($last >= 4 &&
$ts[$last - 1]["token"] == T_STRING &&
$ts[$last - 2]["token"] == T_EXTENDS &&
$ts[$last - 3]["token"] == T_STRING &&
$ts[$last - 4]["token"] == T_CLASS ) {
/* class classname extends classname { */
$classname = $ts[$last - 3]["value"];
$extendsname = $ts[$last - 1]["value"];
if (class_exists($classname, false)) {
throw new Exception(sprintf("Class '%s' can't be redeclared",
$classname));
}
if (!class_exists($extendsname, true)) {
throw new Exception(sprintf("Can't extend '%s' ... from not existing Class '%s'",
$classname, $extendsname));
}
} else if ($last >= 4 &&
$ts[$last - 1]["token"] == T_STRING &&
$ts[$last - 2]["token"] == T_IMPLEMENTS &&
$ts[$last - 3]["token"] == T_STRING &&
$ts[$last - 4]["token"] == T_CLASS ) {
/* class name implements interface { */
$classname = $ts[$last - 3]["value"];
$implements = $ts[$last - 1]["value"];
if (class_exists($classname, false)) {
throw new Exception(sprintf("Class '%s' can't be redeclared",
$classname));
}
if (!interface_exists($implements, false)) {
throw new Exception(sprintf("Can't implement not existing Interface '%s' for Class '%s'",
$implements, $classname));
}
}
array_push($braces, $token);
break;
case "}":
$need_return = 0;
case ")":
array_pop($braces);
break;
case "[":
if ($ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
$ts[0]["token"] != T_ABSTRACT && /* if we are not in a class definition */
$ts[1]["token"] != T_CLASS && /* if we are not in a class definition */
$ts[$last - 1]["token"] == T_VARIABLE) {
/* $a[] only works on array and string */
/* $object has to exist and has to be a object */
$objname = $ts[$last - 1]["value"];
if (!isset($GLOBALS[ltrim($objname, "$")])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $objname));
}
$obj = $GLOBALS[ltrim($objname, "$")];
if (is_object($obj)) {
throw new Exception(sprintf('Objects (%s) don\'t support array access operators', $objname));
}
}
break;
}
$eval .= $token;
}
}
$last = count($ts) - 1;
if ($last >= 2 &&
$ts[$last - 0]["token"] == T_STRING &&
$ts[$last - 1]["token"] == T_DOUBLE_COLON &&
$ts[$last - 2]["token"] == T_STRING ) {
/* Class::constant */
/* $object has to exist and has to be a object */
$classname = $ts[$last - 2]["value"];
if (!class_exists($classname)) {
throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname));
}
$constname = $ts[$last - 0]["value"];
$c = new ReflectionClass($classname);
if (!$c->hasConstant($constname)) {
throw new Exception(sprintf("Class '%s' doesn't have a constant named '%s'",
$classname, $constname));
}
} else if ($last == 0 &&
$ts[$last - 0]["token"] == T_VARIABLE ) {
/* $var */
$varname = $ts[$last - 0]["value"];
if (!isset($GLOBALS[ltrim($varname, "$")])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $varname));
}
}
$need_more = (count($braces) > 0) || $open_comment;
if ($need_more || ";" === $token) {
$need_semicolon = 0;
}
if ($need_return) {
$eval = "return ".$eval;
}
if ($need_more) {
$_SESSION["partial"] = $eval;
} else {
$_SESSION["code"] = $eval;
}
return $need_more;
}
?>