-
Notifications
You must be signed in to change notification settings - Fork 10
/
tinyphp.class.php
116 lines (103 loc) · 3.36 KB
/
tinyphp.class.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
<?php
class tinyPhp {
public $php_excludes = array(
'$GLOBALS',
'$_SERVER',
'$_GET',
'$_POST',
'$_FILES',
'$_REQUEST',
'$_SESSION',
'$_ENV',
'$_COOKIE',
'$php_errormsg',
'$HTTP_RAW_POST_DATA',
'$http_response_header',
'$argc',
'$argv'
);
function is_label($str) {
return preg_match('~[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+~',$str);
}
function get_tiny($snippet,
$replace_variables=FALSE,
$remove_whitespace=FALSE,
$remove_comments=FALSE,
$additional_excludes=array()) {
//create excluded variables array
$php_excludes = $this->php_excludes;
if(!empty($additional_excludes)) {
$excludes = array_merge($php_excludes,$additional_excludes);
$excludes = array_unique($excludes);
} else {
$excludes = $php_excludes;
}
//generate tokens from snippet
$tokens = token_get_all($snippet);
//get all variables in snippet, remove comments
$variables = array();
$new_source = '';
foreach ($tokens as $token) {
if(!is_array($token)) {
$new_source .= $token;
continue;
}
if($token[0]==T_VARIABLE && !in_array($token[1],$excludes)) {
$variables[] = $token[1];
}
if($remove_comments) {
if(in_array($token[0],array(T_COMMENT,T_DOC_COMMENT))) {
continue;
}
}
$new_source .= $token[1];
}
$variables = array_unique($variables);
//generate replace values for all variables
if(!empty($variables)) {
$replace_values = array();
$count = 0;
while($variable = current($variables)) {
$mod = $count%26;
$repeat_chars = ((int)floor($count/26))+1;
$replace_int = 97+$mod;
$replace_value = chr(36);
for($i=1;$i<=$repeat_chars;$i++) {
$replace_value .= chr($replace_int);
}
if(!in_array($replace_value,$variables) && !in_array($replace_value,$excludes)) {
$variable_replace[$variable] = $replace_value;
next($variables);
}
$count++;
}
}
//get all tokens from previous iteration
$tokens = token_get_all($new_source);
//replace all variables, remove whitespace
$new_source = '';
foreach ($tokens as $i => $token) {
if(!is_array($token)) {
$new_source .= $token;
continue;
}
if($replace_variables) {
if($token[0] == T_VARIABLE && !in_array($token[1],$excludes)) {
$key = $token[1];
$new_source .= $variable_replace[$key];
continue;
}
}
if ($token[0] == T_WHITESPACE && $remove_whitespace) {
if (isset($tokens[$i-1]) && isset($tokens[$i+1]) && is_array($tokens[$i-1]) && is_array($tokens[$i+1]) && $this->is_label($tokens[$i-1][1]) && $this->is_label($tokens[$i+1][1])) {
$new_source .= ' ';
}
} elseif($token[0]==T_CASE) {
$new_source .= $token[1].' ';
} else {
$new_source .= $token[1];
}
}
return htmlentities($new_source);
}
}