Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 216 lines (204 sloc) 9.501 kb
667bfec Decomposition
jakubvrana authored
1 <?php
e1bc7af Change directory structure
jakubvrana authored
2 include dirname(__FILE__) . "/adminer/include/version.inc.php";
b8b4548 Utilize JSMin
jakubvrana authored
3 include dirname(__FILE__) . "/externals/jsmin-php/jsmin.php";
7f3b4f2 Version checker
jakubvrana authored
4
8943678 sprintf() in lang()
jakubvrana authored
5 function add_apo_slashes($s) {
6 return addcslashes($s, "\\'");
7 }
8
0970a58 Prepare for single language version
jakubvrana authored
9 function remove_lang($match) {
bba76d8 Flatten $translations
jakubvrana authored
10 global $translations;
438ca05 Separate languages
jakubvrana authored
11 $idf = strtr($match[2], array("\\'" => "'", "\\\\" => "\\"));
bba76d8 Flatten $translations
jakubvrana authored
12 $s = ($translations[$idf] ? $translations[$idf] : $idf);
64ba924 Comments
jakubvrana authored
13 if ($match[3] == ",") { // lang() has parameters
8943678 sprintf() in lang()
jakubvrana authored
14 return "$match[1]" . (is_array($s) ? "lang(array('" . implode("', '", array_map('add_apo_slashes', $s)) . "')," : "sprintf('" . add_apo_slashes($s) . "',");
15 }
16 return ($match[1] && $match[4] ? $s : "$match[1]'" . add_apo_slashes($s) . "'$match[4]");
0970a58 Prepare for single language version
jakubvrana authored
17 }
18
64ba924 Comments
jakubvrana authored
19 $lang_ids = array(); // global variable simplifies usage in a callback function
4738060 Use numeric language IDs to save bytes
jakubvrana authored
20 function lang_ids($match) {
21 global $lang_ids;
22 return 'lang(' . $lang_ids[stripslashes($match[1])] . $match[2];
23 }
24
667bfec Decomposition
jakubvrana authored
25 function put_file($match) {
4738060 Use numeric language IDs to save bytes
jakubvrana authored
26 global $lang_ids;
6bb1401 Remove ?><?php
jakubvrana authored
27 if ($match[2] == './lang/$LANG.inc.php') {
bba76d8 Flatten $translations
jakubvrana authored
28 if ($_COOKIE["lang"]) {
29 return "";
8943678 sprintf() in lang()
jakubvrana authored
30 }
c645807 PHP 4 compatibility
jakubvrana authored
31 $return = "";
e1bc7af Change directory structure
jakubvrana authored
32 foreach (glob(dirname(__FILE__) . "/adminer/lang/*.inc.php") as $filename) {
64ba924 Comments
jakubvrana authored
33 // assign translation numbers
4738060 Use numeric language IDs to save bytes
jakubvrana authored
34 include $filename;
35 foreach ($translations as $key => $val) {
36 if (!isset($lang_ids[$key])) {
37 $lang_ids[$key] = count($lang_ids);
38 }
39 }
932a719 Fill $lang_ids first
jakubvrana authored
40 }
e1bc7af Change directory structure
jakubvrana authored
41 foreach (glob(dirname(__FILE__) . "/adminer/lang/*.inc.php") as $filename) {
64ba924 Comments
jakubvrana authored
42 include $filename; // reassign $translations
4738060 Use numeric language IDs to save bytes
jakubvrana authored
43 $translation_ids = array_flip($lang_ids);
44 foreach ($translations as $key => $val) {
45 $translation_ids[$lang_ids[$key]] = $val;
46 }
47 $return .= 'case "' . basename($filename, '.inc.php') . '": $translations = array(';
48 foreach ($translation_ids as $val) {
49 $return .= (is_array($val) ? "array('" . implode("', '", array_map('add_apo_slashes', $val)) . "')" : "'" . add_apo_slashes($val) . "'") . ", ";
50 }
eb7e599 Remove extra comma
jakubvrana authored
51 $return = substr($return, 0, -2) . "); break;\n";
bba76d8 Flatten $translations
jakubvrana authored
52 }
c645807 PHP 4 compatibility
jakubvrana authored
53 return "switch (\$LANG) {\n$return}\n";
438ca05 Separate languages
jakubvrana authored
54 }
e1bc7af Change directory structure
jakubvrana authored
55 $return = file_get_contents(dirname(__FILE__) . "/adminer/$match[2]");
6bb1401 Remove ?><?php
jakubvrana authored
56 if ($match[2] != "./include/lang.inc.php" || !$_COOKIE["lang"]) {
64ba924 Comments
jakubvrana authored
57 $tokens = token_get_all($return); // to find out the last token
6bb1401 Remove ?><?php
jakubvrana authored
58 return "?>\n$return" . (in_array($tokens[count($tokens) - 1][0], array(T_CLOSE_TAG, T_INLINE_HTML), true) ? "<?php" : "");
59 } elseif (preg_match('~\\s*(\\$pos = .*)~', $return, $match2)) {
64ba924 Comments
jakubvrana authored
60 // single language lang() is used for plural
6bb1401 Remove ?><?php
jakubvrana authored
61 return "function lang(\$translation, \$number) {\n\t" . str_replace('$LANG', "'$_COOKIE[lang]'", $match2[1]) . "\n\treturn sprintf(\$translation[\$pos], \$number);\n}\n";
62 } else {
63 echo "lang() not found\n";
667bfec Decomposition
jakubvrana authored
64 }
65 }
66
16f4210 Function minification
jakubvrana authored
67 function short_identifier($number, $chars) {
68 $return = '';
a06f4c1 Size reduction by minification of variables
jakubvrana authored
69 while ($number >= 0) {
70 $return .= $chars{$number % strlen($chars)};
71 $number = floor($number / strlen($chars)) - 1;
72 }
73 return $return;
74 }
75
5ef9322 Separate PDO
jakubvrana authored
76 // based on Dgx's PHP shrinker
794192d php_shrink()
jakubvrana authored
77 function php_shrink($input) {
815eb26 Use one token for all
jakubvrana authored
78 $special_variables = array_flip(array('$this', '$GLOBALS', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_SERVER'));
a06f4c1 Size reduction by minification of variables
jakubvrana authored
79 static $short_variables = array();
64ba924 Comments
jakubvrana authored
80 static $short_functions = array();
a06f4c1 Size reduction by minification of variables
jakubvrana authored
81 $shortening = true;
fbebb08 Use separate connection for exploring indexes
jakubvrana authored
82 $special_functions = array_flip(array('Min_DB', 'Min_Result', '__construct'));
c27a877 Minify defined functions only
jakubvrana authored
83 $defined_functions = array();
16f4210 Function minification
jakubvrana authored
84 $tokens = token_get_all($input);
85
86 foreach ($tokens as $i => $token) {
87 if ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
88 $short_variables[$token[1]]++;
c27a877 Minify defined functions only
jakubvrana authored
89 } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && !isset($special_functions[$token[1]])) {
16f4210 Function minification
jakubvrana authored
90 $short_functions[$token[1]]++;
c27a877 Minify defined functions only
jakubvrana authored
91 if ($tokens[$i-2][0] === T_FUNCTION) {
92 $defined_functions[$token[1]] = true;
93 }
16f4210 Function minification
jakubvrana authored
94 }
95 }
c27a877 Minify defined functions only
jakubvrana authored
96
16f4210 Function minification
jakubvrana authored
97 arsort($short_variables);
98 foreach (array_keys($short_variables) as $number => $key) {
99 $short_variables[$key] = short_identifier($number, implode("", range('a', 'z')) . '_' . implode("", range('A', 'Z'))); // could use also numbers and \x7f-\xff
100 }
101 arsort($short_functions);
c27a877 Minify defined functions only
jakubvrana authored
102 $number = 0;
103 foreach ($short_functions as $key => $val) {
104 if (isset($defined_functions[$key])) {
3aaeff0 Prevent name conflicts in function minification
jakubvrana authored
105 do {
64ba924 Comments
jakubvrana authored
106 $short_functions[$key] = short_identifier($number, implode("", range('a', 'z'))); // _ not used to not collide with gettext()
3aaeff0 Prevent name conflicts in function minification
jakubvrana authored
107 $number++;
64ba924 Comments
jakubvrana authored
108 } while (isset($short_functions[$short_functions[$key]])); // don't overwrite existing functions
c27a877 Minify defined functions only
jakubvrana authored
109 }
16f4210 Function minification
jakubvrana authored
110 }
a06f4c1 Size reduction by minification of variables
jakubvrana authored
111
794192d php_shrink()
jakubvrana authored
112 $set = array_flip(preg_split('//', '!"#$&\'()*+,-./:;<=>?@[\]^`{|}'));
113 $space = '';
114 $output = '';
ff85f12 Join echos
jakubvrana authored
115 $in_echo = false;
116 for (reset($tokens); list($i, $token) = each($tokens); ) {
794192d php_shrink()
jakubvrana authored
117 if (!is_array($token)) {
118 $token = array(0, $token);
119 }
120 if ($token[0] == T_COMMENT || $token[0] == T_WHITESPACE) {
121 $space = "\n";
122 } else {
a06f4c1 Size reduction by minification of variables
jakubvrana authored
123 if ($token[0] == T_VAR) {
124 $shortening = false;
125 } elseif (!$shortening) {
126 if ($token[1] == ';') {
127 $shortening = true;
128 }
ff85f12 Join echos
jakubvrana authored
129 } elseif ($token[0] == T_ECHO) {
130 $in_echo = true;
131 } elseif ($token[1] == ';' && $in_echo) {
132 $in_echo = false;
133 if ($tokens[$i+1][0] === T_WHITESPACE && $tokens[$i+2][0] === T_ECHO) {
64ba924 Comments
jakubvrana authored
134 // join two consecutive echos
ff85f12 Join echos
jakubvrana authored
135 next($tokens);
136 next($tokens);
137 $token[1] = '.'; //! join ''.'' and "".""
138 }
16f4210 Function minification
jakubvrana authored
139 } elseif ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
140 $token[1] = '$' . $short_variables[$token[1]];
c27a877 Minify defined functions only
jakubvrana authored
141 } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && isset($defined_functions[$token[1]])
64ba924 Comments
jakubvrana authored
142 && $tokens[$i-1][0] !== T_DOUBLE_COLON && $tokens[$i-2][0] !== T_NEW && $tokens[$i-2][1] !== '_result' // don't substitute parent methods - used to link PHP methods only
16f4210 Function minification
jakubvrana authored
143 ) {
144 $token[1] = $short_functions[$token[1]];
64ba924 Comments
jakubvrana authored
145 } elseif ($token[0] == T_CONSTANT_ENCAPSED_STRING
146 && (($tokens[$i-1] === '(' && in_array($tokens[$i-2][1], array('array_map', 'set_exception_handler'), true)) || $token[1] == "'normalize_enum'")
147 && isset($defined_functions[substr($token[1], 1, -1)])
148 ) { // minify callback functions too
16f4210 Function minification
jakubvrana authored
149 $token[1] = "'" . $short_functions[substr($token[1], 1, -1)] . "'";
a06f4c1 Size reduction by minification of variables
jakubvrana authored
150 }
794192d php_shrink()
jakubvrana authored
151 if (isset($set[substr($output, -1)]) || isset($set[$token[1]{0}])) {
152 $space = '';
153 }
154 $output .= $space . $token[1];
155 $space = '';
156 }
157 }
158 return $output;
159 }
160
0970a58 Prepare for single language version
jakubvrana authored
161 error_reporting(E_ALL & ~E_NOTICE);
7418af0 Czech translation
jakubvrana authored
162 if ($_SERVER["argc"] > 1) {
64ba924 Comments
jakubvrana authored
163 $_COOKIE["lang"] = $_SERVER["argv"][1]; // Adminer functions read language from cookie
e1bc7af Change directory structure
jakubvrana authored
164 include dirname(__FILE__) . "/adminer/include/lang.inc.php";
bba76d8 Flatten $translations
jakubvrana authored
165 if ($_SERVER["argc"] != 2 || !isset($langs[$_COOKIE["lang"]])) {
e1bc7af Change directory structure
jakubvrana authored
166 echo "Usage: php compile.php [lang]\nPurpose: Compile adminer[-lang].php from index.php.\n";
7418af0 Czech translation
jakubvrana authored
167 exit(1);
168 }
e1bc7af Change directory structure
jakubvrana authored
169 include dirname(__FILE__) . "/adminer/lang/$_COOKIE[lang].inc.php";
7418af0 Czech translation
jakubvrana authored
170 }
bba76d8 Flatten $translations
jakubvrana authored
171
1fe2f33 Rename to Adminer
jakubvrana authored
172 $filename = "adminer" . ($_COOKIE["lang"] ? "-$_COOKIE[lang]" : "") . ".php";
e1bc7af Change directory structure
jakubvrana authored
173 $file = file_get_contents(dirname(__FILE__) . "/adminer/index.php");
6bb1401 Remove ?><?php
jakubvrana authored
174 $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file);
bb1ae0b Code coverage
jakubvrana authored
175 $file = preg_replace("~if \\(isset\\(\\\$_SESSION\\[\"coverage.*\n}\n| && !isset\\(\\\$_SESSION\\[\"coverage\"\\]\\)~sU", '', $file);
222fc78 Save lang to cookie
jakubvrana authored
176 if ($_COOKIE["lang"]) {
64ba924 Comments
jakubvrana authored
177 // single language version
a9907f1 Greedy expressions
jakubvrana authored
178 $file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file);
7418af0 Czech translation
jakubvrana authored
179 $file = str_replace("<?php switch_lang(); ?>\n", "", $file);
4eb07d7 Globalize $translations
jakubvrana authored
180 $file = str_replace('<?php echo $LANG; ?>', $_COOKIE["lang"], $file);
4738060 Use numeric language IDs to save bytes
jakubvrana authored
181 } else {
182 $file = preg_replace_callback("~lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])~s", 'lang_ids', $file);
0970a58 Prepare for single language version
jakubvrana authored
183 }
35bca85 Fix XSS (thanks to Synopsi)
jakubvrana authored
184 $replace = 'htmlspecialchars(preg_replace("~\\\\\\\\?.*~", "", $_SERVER["REQUEST_URI"])) . "?file=\\0&amp;version=' . $VERSION;
a76c555 Remove odd() from create
jakubvrana authored
185 $file = preg_replace('~default\\.css|functions\\.js|favicon\\.ico~', '<?php echo ' . $replace . '"; ?>', $file);
186 $file = preg_replace('~(plus|cross|up|down|arrow)\\.gif~', '" . ' . $replace, $file);
4619f78 External stylesheet
jakubvrana authored
187 $file = str_replace('error_reporting(E_ALL & ~E_NOTICE);', 'error_reporting(E_ALL & ~E_NOTICE);
188 if (isset($_GET["file"])) {
7f3b4f2 Version checker
jakubvrana authored
189 header("Expires: " . gmdate("D, d M Y H:i:s", time() + 365*24*60*60) . " GMT");
f3a9e46 Static files never expires
jakubvrana authored
190 if ($_GET["file"] == "favicon.ico") {
191 header("Content-Type: image/x-icon");
e1bc7af Change directory structure
jakubvrana authored
192 echo base64_decode("' . base64_encode(file_get_contents(dirname(__FILE__) . "/adminer/favicon.ico")) . '");
f3a9e46 Static files never expires
jakubvrana authored
193 } elseif ($_GET["file"] == "default.css") {
194 header("Content-Type: text/css");
e1bc7af Change directory structure
jakubvrana authored
195 ?>' . preg_replace('~\\s*([:;{},])\\s*~', '\\1', file_get_contents(dirname(__FILE__) . "/adminer/default.css")) . '<?php
540e217 Separate JavaScript
jakubvrana authored
196 } elseif ($_GET["file"] == "functions.js") {
197 header("Content-Type: text/javascript");
e1bc7af Change directory structure
jakubvrana authored
198 ?>' . JSMin::minify(file_get_contents(dirname(__FILE__) . "/adminer/functions.js")) . '<?php
4619f78 External stylesheet
jakubvrana authored
199 } else {
f3a9e46 Static files never expires
jakubvrana authored
200 header("Content-Type: image/gif");
201 switch ($_GET["file"]) {
e1bc7af Change directory structure
jakubvrana authored
202 case "arrow.gif": echo base64_decode("' . base64_encode(file_get_contents(dirname(__FILE__) . "/adminer/arrow.gif")) . '"); break;
203 case "up.gif": echo base64_decode("' . base64_encode(file_get_contents(dirname(__FILE__) . "/adminer/up.gif")) . '"); break;
204 case "down.gif": echo base64_decode("' . base64_encode(file_get_contents(dirname(__FILE__) . "/adminer/down.gif")) . '"); break;
205 case "plus.gif": echo base64_decode("' . base64_encode(file_get_contents(dirname(__FILE__) . "/adminer/plus.gif")) . '"); break;
206 case "cross.gif": echo base64_decode("' . base64_encode(file_get_contents(dirname(__FILE__) . "/adminer/cross.gif")) . '"); break;
4619f78 External stylesheet
jakubvrana authored
207 }
208 }
209 exit;
64ba924 Comments
jakubvrana authored
210 }', $file); // integrate static files
e1bc7af Change directory structure
jakubvrana authored
211 $file = str_replace("../externals/jush/", "http://jush.sourceforge.net/", $file);
35bca85 Fix XSS (thanks to Synopsi)
jakubvrana authored
212 $file = preg_replace("~<\\?php\\s*\\?>\n?|\\?>\n?<\\?php~", '', $file);
794192d php_shrink()
jakubvrana authored
213 $file = php_shrink($file);
64ba924 Comments
jakubvrana authored
214 fwrite(fopen($filename, "w"), $file); // file_put_contents() since PHP 5
7418af0 Czech translation
jakubvrana authored
215 echo "$filename created.\n";
Something went wrong with that request. Please try again.