Skip to content
Newer
Older
100644 202 lines (188 sloc) 8.33 KB
667bfec Decomposition
jakubvrana authored Jul 2, 2007
1 <?php
e1bc7af Change directory structure
jakubvrana authored Jun 13, 2009
2 include dirname(__FILE__) . "/adminer/include/version.inc.php";
b8b4548 Utilize JSMin
jakubvrana authored May 29, 2009
3 include dirname(__FILE__) . "/externals/jsmin-php/jsmin.php";
7f3b4f2 Version checker
jakubvrana authored May 29, 2009
4
8943678 sprintf() in lang()
jakubvrana authored Jul 9, 2007
5 function add_apo_slashes($s) {
6 return addcslashes($s, "\\'");
7 }
8
0970a58 Prepare for single language version
jakubvrana authored Jul 5, 2007
9 function remove_lang($match) {
bba76d8 Flatten $translations
jakubvrana authored Aug 4, 2007
10 global $translations;
438ca05 Separate languages
jakubvrana authored Jul 26, 2007
11 $idf = strtr($match[2], array("\\'" => "'", "\\\\" => "\\"));
bba76d8 Flatten $translations
jakubvrana authored Aug 4, 2007
12 $s = ($translations[$idf] ? $translations[$idf] : $idf);
64ba924 Comments
jakubvrana authored Jun 21, 2009
13 if ($match[3] == ",") { // lang() has parameters
8943678 sprintf() in lang()
jakubvrana authored Jul 9, 2007
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 Jul 5, 2007
17 }
18
64ba924 Comments
jakubvrana authored Jun 21, 2009
19 $lang_ids = array(); // global variable simplifies usage in a callback function
4738060 Use numeric language IDs to save bytes
jakubvrana authored May 23, 2008
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 Jul 2, 2007
25 function put_file($match) {
4738060 Use numeric language IDs to save bytes
jakubvrana authored May 23, 2008
26 global $lang_ids;
6bb1401 Remove ?><?php
jakubvrana authored Jun 1, 2009
27 if ($match[2] == './lang/$LANG.inc.php') {
b8ef9fb Use prefix for cookies
jakubvrana authored Jun 21, 2009
28 if ($_COOKIE["adminer_lang"]) {
bba76d8 Flatten $translations
jakubvrana authored Aug 4, 2007
29 return "";
8943678 sprintf() in lang()
jakubvrana authored Jul 9, 2007
30 }
c645807 PHP 4 compatibility
jakubvrana authored Aug 7, 2007
31 $return = "";
e1bc7af Change directory structure
jakubvrana authored Jun 13, 2009
32 foreach (glob(dirname(__FILE__) . "/adminer/lang/*.inc.php") as $filename) {
64ba924 Comments
jakubvrana authored Jun 21, 2009
33 // assign translation numbers
4738060 Use numeric language IDs to save bytes
jakubvrana authored May 23, 2008
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 May 24, 2008
40 }
e1bc7af Change directory structure
jakubvrana authored Jun 13, 2009
41 foreach (glob(dirname(__FILE__) . "/adminer/lang/*.inc.php") as $filename) {
64ba924 Comments
jakubvrana authored Jun 21, 2009
42 include $filename; // reassign $translations
4738060 Use numeric language IDs to save bytes
jakubvrana authored May 23, 2008
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 May 26, 2008
51 $return = substr($return, 0, -2) . "); break;\n";
bba76d8 Flatten $translations
jakubvrana authored Aug 4, 2007
52 }
c645807 PHP 4 compatibility
jakubvrana authored Aug 7, 2007
53 return "switch (\$LANG) {\n$return}\n";
438ca05 Separate languages
jakubvrana authored Jul 26, 2007
54 }
e1bc7af Change directory structure
jakubvrana authored Jun 13, 2009
55 $return = file_get_contents(dirname(__FILE__) . "/adminer/$match[2]");
b8ef9fb Use prefix for cookies
jakubvrana authored Jun 21, 2009
56 if ($match[2] != "./include/lang.inc.php" || !$_COOKIE["adminer_lang"]) {
64ba924 Comments
jakubvrana authored Jun 21, 2009
57 $tokens = token_get_all($return); // to find out the last token
6bb1401 Remove ?><?php
jakubvrana authored Jun 1, 2009
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 Jun 21, 2009
60 // single language lang() is used for plural
b8ef9fb Use prefix for cookies
jakubvrana authored Jun 21, 2009
61 return "function lang(\$translation, \$number) {\n\t" . str_replace('$LANG', "'$_COOKIE[adminer_lang]'", $match2[1]) . "\n\treturn sprintf(\$translation[\$pos], \$number);\n}\n";
6bb1401 Remove ?><?php
jakubvrana authored Jun 1, 2009
62 } else {
63 echo "lang() not found\n";
667bfec Decomposition
jakubvrana authored Jul 2, 2007
64 }
65 }
66
16f4210 Function minification
jakubvrana authored Apr 15, 2009
67 function short_identifier($number, $chars) {
68 $return = '';
a06f4c1 Size reduction by minification of variables
jakubvrana authored Apr 15, 2009
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 May 8, 2009
76 // based on Dgx's PHP shrinker
794192d php_shrink()
jakubvrana authored Sep 4, 2007
77 function php_shrink($input) {
815eb26 Use one token for all
jakubvrana authored May 8, 2009
78 $special_variables = array_flip(array('$this', '$GLOBALS', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_SERVER'));
a06f4c1 Size reduction by minification of variables
jakubvrana authored Apr 15, 2009
79 static $short_variables = array();
64ba924 Comments
jakubvrana authored Jun 21, 2009
80 static $short_functions = array();
a06f4c1 Size reduction by minification of variables
jakubvrana authored Apr 15, 2009
81 $shortening = true;
fbebb08 Use separate connection for exploring indexes
jakubvrana authored Jun 3, 2009
82 $special_functions = array_flip(array('Min_DB', 'Min_Result', '__construct'));
c27a877 Minify defined functions only
jakubvrana authored Apr 16, 2009
83 $defined_functions = array();
16f4210 Function minification
jakubvrana authored Apr 15, 2009
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 Apr 16, 2009
89 } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && !isset($special_functions[$token[1]])) {
16f4210 Function minification
jakubvrana authored Apr 15, 2009
90 $short_functions[$token[1]]++;
c27a877 Minify defined functions only
jakubvrana authored Apr 16, 2009
91 if ($tokens[$i-2][0] === T_FUNCTION) {
92 $defined_functions[$token[1]] = true;
93 }
16f4210 Function minification
jakubvrana authored Apr 15, 2009
94 }
95 }
c27a877 Minify defined functions only
jakubvrana authored Apr 16, 2009
96
16f4210 Function minification
jakubvrana authored Apr 15, 2009
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 Apr 16, 2009
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 Apr 17, 2009
105 do {
64ba924 Comments
jakubvrana authored Jun 21, 2009
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 Apr 17, 2009
107 $number++;
64ba924 Comments
jakubvrana authored Jun 21, 2009
108 } while (isset($short_functions[$short_functions[$key]])); // don't overwrite existing functions
c27a877 Minify defined functions only
jakubvrana authored Apr 16, 2009
109 }
16f4210 Function minification
jakubvrana authored Apr 15, 2009
110 }
a06f4c1 Size reduction by minification of variables
jakubvrana authored Apr 15, 2009
111
794192d php_shrink()
jakubvrana authored Sep 4, 2007
112 $set = array_flip(preg_split('//', '!"#$&\'()*+,-./:;<=>?@[\]^`{|}'));
113 $space = '';
114 $output = '';
ff85f12 Join echos
jakubvrana authored Jun 1, 2009
115 $in_echo = false;
116 for (reset($tokens); list($i, $token) = each($tokens); ) {
794192d php_shrink()
jakubvrana authored Sep 4, 2007
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 Apr 15, 2009
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 Jun 1, 2009
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 Jun 21, 2009
134 // join two consecutive echos
ff85f12 Join echos
jakubvrana authored Jun 1, 2009
135 next($tokens);
136 next($tokens);
137 $token[1] = '.'; //! join ''.'' and "".""
138 }
16f4210 Function minification
jakubvrana authored Apr 15, 2009
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 Apr 16, 2009
141 } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && isset($defined_functions[$token[1]])
64ba924 Comments
jakubvrana authored Jun 21, 2009
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 Apr 15, 2009
143 ) {
144 $token[1] = $short_functions[$token[1]];
64ba924 Comments
jakubvrana authored Jun 21, 2009
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 Apr 15, 2009
149 $token[1] = "'" . $short_functions[substr($token[1], 1, -1)] . "'";
a06f4c1 Size reduction by minification of variables
jakubvrana authored Apr 15, 2009
150 }
794192d php_shrink()
jakubvrana authored Sep 4, 2007
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
9ee9cf0 Remove code generation
jakubvrana authored Jun 26, 2009
161 function minify_css($file) {
162 return preg_replace('~\\s*([:;{},])\\s*~', '\\1', $file);
163 }
164
165 function compile_file($match) {
166 return call_user_func($match[2], file_get_contents(dirname(__FILE__) . "/adminer/$match[1]"));
167 }
168
0970a58 Prepare for single language version
jakubvrana authored Jul 5, 2007
169 error_reporting(E_ALL & ~E_NOTICE);
7418af0 Czech translation
jakubvrana authored Jul 5, 2007
170 if ($_SERVER["argc"] > 1) {
b8ef9fb Use prefix for cookies
jakubvrana authored Jun 21, 2009
171 $_COOKIE["adminer_lang"] = $_SERVER["argv"][1]; // Adminer functions read language from cookie
e1bc7af Change directory structure
jakubvrana authored Jun 13, 2009
172 include dirname(__FILE__) . "/adminer/include/lang.inc.php";
b8ef9fb Use prefix for cookies
jakubvrana authored Jun 21, 2009
173 if ($_SERVER["argc"] != 2 || !isset($langs[$_COOKIE["adminer_lang"]])) {
e1bc7af Change directory structure
jakubvrana authored Jun 13, 2009
174 echo "Usage: php compile.php [lang]\nPurpose: Compile adminer[-lang].php from index.php.\n";
7418af0 Czech translation
jakubvrana authored Jul 5, 2007
175 exit(1);
176 }
b8ef9fb Use prefix for cookies
jakubvrana authored Jun 21, 2009
177 include dirname(__FILE__) . "/adminer/lang/$_COOKIE[adminer_lang].inc.php";
7418af0 Czech translation
jakubvrana authored Jul 5, 2007
178 }
bba76d8 Flatten $translations
jakubvrana authored Aug 4, 2007
179
b8ef9fb Use prefix for cookies
jakubvrana authored Jun 21, 2009
180 $filename = "adminer" . ($_COOKIE["adminer_lang"] ? "-$_COOKIE[adminer_lang]" : "") . ".php";
e1bc7af Change directory structure
jakubvrana authored Jun 13, 2009
181 $file = file_get_contents(dirname(__FILE__) . "/adminer/index.php");
91156d1 Coverage uses the same cookie so must live in the same directory
jakubvrana authored Jun 24, 2009
182 $file = preg_replace('(' . str_replace(' ', '\\s*', preg_quote(' if (isset($_GET["coverage"])) { include "./coverage.inc.php"; }')) . ')', '', $file);
6bb1401 Remove ?><?php
jakubvrana authored Jun 1, 2009
183 $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file);
bb1ae0b Code coverage
jakubvrana authored Oct 2, 2008
184 $file = preg_replace("~if \\(isset\\(\\\$_SESSION\\[\"coverage.*\n}\n| && !isset\\(\\\$_SESSION\\[\"coverage\"\\]\\)~sU", '', $file);
b8ef9fb Use prefix for cookies
jakubvrana authored Jun 21, 2009
185 if ($_COOKIE["adminer_lang"]) {
64ba924 Comments
jakubvrana authored Jun 21, 2009
186 // single language version
a9907f1 Greedy expressions
jakubvrana authored Jul 10, 2007
187 $file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file);
7418af0 Czech translation
jakubvrana authored Jul 5, 2007
188 $file = str_replace("<?php switch_lang(); ?>\n", "", $file);
b8ef9fb Use prefix for cookies
jakubvrana authored Jun 21, 2009
189 $file = str_replace('<?php echo $LANG; ?>', $_COOKIE["adminer_lang"], $file);
4738060 Use numeric language IDs to save bytes
jakubvrana authored May 23, 2008
190 } else {
191 $file = preg_replace_callback("~lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])~s", 'lang_ids', $file);
0970a58 Prepare for single language version
jakubvrana authored Jul 5, 2007
192 }
9ee9cf0 Remove code generation
jakubvrana authored Jun 26, 2009
193 $file = preg_replace_callback("~compile_file\\('([^']+)', '([^']+)'\\)~", 'compile_file', $file); // integrate static files
35bca85 Fix XSS (thanks to Synopsi)
jakubvrana authored Jun 6, 2009
194 $replace = 'htmlspecialchars(preg_replace("~\\\\\\\\?.*~", "", $_SERVER["REQUEST_URI"])) . "?file=\\0&amp;version=' . $VERSION;
9ee9cf0 Remove code generation
jakubvrana authored Jun 26, 2009
195 $file = preg_replace('~(?<!== ")(default\\.css|functions\\.js|favicon\\.ico)~', '<?php echo ' . $replace . '"; ?>', $file);
196 $file = preg_replace('~(?<!case ")(plus|cross|up|down|arrow)\\.gif~', '" . ' . $replace, $file);
e1bc7af Change directory structure
jakubvrana authored Jun 13, 2009
197 $file = str_replace("../externals/jush/", "http://jush.sourceforge.net/", $file);
35bca85 Fix XSS (thanks to Synopsi)
jakubvrana authored Jun 6, 2009
198 $file = preg_replace("~<\\?php\\s*\\?>\n?|\\?>\n?<\\?php~", '', $file);
794192d php_shrink()
jakubvrana authored Sep 4, 2007
199 $file = php_shrink($file);
64ba924 Comments
jakubvrana authored Jun 21, 2009
200 fwrite(fopen($filename, "w"), $file); // file_put_contents() since PHP 5
7418af0 Czech translation
jakubvrana authored Jul 5, 2007
201 echo "$filename created.\n";
Something went wrong with that request. Please try again.