Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 215 lines (204 sloc) 9.581 kb
667bfec4 » jakubvrana
2007-07-02 Decomposition
1 <?php
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
2 include dirname(__FILE__) . "/adminer/include/version.inc.php";
b8b45482 » jakubvrana
2009-05-29 Utilize JSMin
3 include dirname(__FILE__) . "/externals/jsmin-php/jsmin.php";
7f3b4f2b » jakubvrana
2009-05-29 Version checker
4
89436789 » jakubvrana
2007-07-09 sprintf() in lang()
5 function add_apo_slashes($s) {
6 return addcslashes($s, "\\'");
7 }
8
0970a583 » jakubvrana
2007-07-05 Prepare for single language version
9 function remove_lang($match) {
bba76d8d » jakubvrana
2007-08-04 Flatten $translations
10 global $translations;
438ca058 » jakubvrana
2007-07-26 Separate languages
11 $idf = strtr($match[2], array("\\'" => "'", "\\\\" => "\\"));
bba76d8d » jakubvrana
2007-08-04 Flatten $translations
12 $s = ($translations[$idf] ? $translations[$idf] : $idf);
64ba9242 » jakubvrana
2009-06-21 Comments
13 if ($match[3] == ",") { // lang() has parameters
89436789 » jakubvrana
2007-07-09 sprintf() in lang()
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]");
0970a583 » jakubvrana
2007-07-05 Prepare for single language version
17 }
18
64ba9242 » jakubvrana
2009-06-21 Comments
19 $lang_ids = array(); // global variable simplifies usage in a callback function
4738060a » jakubvrana
2008-05-23 Use numeric language IDs to save bytes
20 function lang_ids($match) {
21 global $lang_ids;
22 return 'lang(' . $lang_ids[stripslashes($match[1])] . $match[2];
23 }
24
667bfec4 » jakubvrana
2007-07-02 Decomposition
25 function put_file($match) {
4738060a » jakubvrana
2008-05-23 Use numeric language IDs to save bytes
26 global $lang_ids;
6bb1401d » jakubvrana
2009-06-01 Remove ?><?php
27 if ($match[2] == './lang/$LANG.inc.php') {
b8ef9fb9 » jakubvrana
2009-06-21 Use prefix for cookies
28 if ($_COOKIE["adminer_lang"]) {
bba76d8d » jakubvrana
2007-08-04 Flatten $translations
29 return "";
89436789 » jakubvrana
2007-07-09 sprintf() in lang()
30 }
c6458077 » jakubvrana
2007-08-07 PHP 4 compatibility
31 $return = "";
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
32 foreach (glob(dirname(__FILE__) . "/adminer/lang/*.inc.php") as $filename) {
64ba9242 » jakubvrana
2009-06-21 Comments
33 // assign translation numbers
4738060a » jakubvrana
2008-05-23 Use numeric language IDs to save bytes
34 include $filename;
35 foreach ($translations as $key => $val) {
36 if (!isset($lang_ids[$key])) {
37 $lang_ids[$key] = count($lang_ids);
38 }
39 }
932a7192 » jakubvrana
2008-05-24 Fill $lang_ids first
40 }
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
41 foreach (glob(dirname(__FILE__) . "/adminer/lang/*.inc.php") as $filename) {
64ba9242 » jakubvrana
2009-06-21 Comments
42 include $filename; // reassign $translations
4738060a » jakubvrana
2008-05-23 Use numeric language IDs to save bytes
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 }
eb7e599e » jakubvrana
2008-05-26 Remove extra comma
51 $return = substr($return, 0, -2) . "); break;\n";
bba76d8d » jakubvrana
2007-08-04 Flatten $translations
52 }
c6458077 » jakubvrana
2007-08-07 PHP 4 compatibility
53 return "switch (\$LANG) {\n$return}\n";
438ca058 » jakubvrana
2007-07-26 Separate languages
54 }
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
55 $return = file_get_contents(dirname(__FILE__) . "/adminer/$match[2]");
b8ef9fb9 » jakubvrana
2009-06-21 Use prefix for cookies
56 if ($match[2] != "./include/lang.inc.php" || !$_COOKIE["adminer_lang"]) {
64ba9242 » jakubvrana
2009-06-21 Comments
57 $tokens = token_get_all($return); // to find out the last token
6bb1401d » jakubvrana
2009-06-01 Remove ?><?php
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)) {
64ba9242 » jakubvrana
2009-06-21 Comments
60 // single language lang() is used for plural
b8ef9fb9 » jakubvrana
2009-06-21 Use prefix for cookies
61 return "function lang(\$translation, \$number) {\n\t" . str_replace('$LANG', "'$_COOKIE[adminer_lang]'", $match2[1]) . "\n\treturn sprintf(\$translation[\$pos], \$number);\n}\n";
6bb1401d » jakubvrana
2009-06-01 Remove ?><?php
62 } else {
63 echo "lang() not found\n";
667bfec4 » jakubvrana
2007-07-02 Decomposition
64 }
65 }
66
16f42102 » jakubvrana
2009-04-15 Function minification
67 function short_identifier($number, $chars) {
68 $return = '';
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
69 while ($number >= 0) {
70 $return .= $chars{$number % strlen($chars)};
71 $number = floor($number / strlen($chars)) - 1;
72 }
73 return $return;
74 }
75
5ef93226 » jakubvrana
2009-05-08 Separate PDO
76 // based on Dgx's PHP shrinker
794192d9 » jakubvrana
2007-09-04 php_shrink()
77 function php_shrink($input) {
815eb26e » jakubvrana
2009-05-08 Use one token for all
78 $special_variables = array_flip(array('$this', '$GLOBALS', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_SERVER'));
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
79 static $short_variables = array();
64ba9242 » jakubvrana
2009-06-21 Comments
80 static $short_functions = array();
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
81 $shortening = true;
fbebb087 » jakubvrana
2009-06-03 Use separate connection for exploring indexes
82 $special_functions = array_flip(array('Min_DB', 'Min_Result', '__construct'));
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
83 $defined_functions = array();
16f42102 » jakubvrana
2009-04-15 Function minification
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]]++;
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
89 } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && !isset($special_functions[$token[1]])) {
16f42102 » jakubvrana
2009-04-15 Function minification
90 $short_functions[$token[1]]++;
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
91 if ($tokens[$i-2][0] === T_FUNCTION) {
92 $defined_functions[$token[1]] = true;
93 }
16f42102 » jakubvrana
2009-04-15 Function minification
94 }
95 }
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
96
16f42102 » jakubvrana
2009-04-15 Function minification
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);
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
102 $number = 0;
103 foreach ($short_functions as $key => $val) {
104 if (isset($defined_functions[$key])) {
3aaeff0d » jakubvrana
2009-04-17 Prevent name conflicts in function minification
105 do {
64ba9242 » jakubvrana
2009-06-21 Comments
106 $short_functions[$key] = short_identifier($number, implode("", range('a', 'z'))); // _ not used to not collide with gettext()
3aaeff0d » jakubvrana
2009-04-17 Prevent name conflicts in function minification
107 $number++;
64ba9242 » jakubvrana
2009-06-21 Comments
108 } while (isset($short_functions[$short_functions[$key]])); // don't overwrite existing functions
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
109 }
16f42102 » jakubvrana
2009-04-15 Function minification
110 }
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
111
794192d9 » jakubvrana
2007-09-04 php_shrink()
112 $set = array_flip(preg_split('//', '!"#$&\'()*+,-./:;<=>?@[\]^`{|}'));
113 $space = '';
114 $output = '';
ff85f125 » jakubvrana
2009-06-01 Join echos
115 $in_echo = false;
116 for (reset($tokens); list($i, $token) = each($tokens); ) {
794192d9 » jakubvrana
2007-09-04 php_shrink()
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 {
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
123 if ($token[0] == T_VAR) {
124 $shortening = false;
125 } elseif (!$shortening) {
126 if ($token[1] == ';') {
127 $shortening = true;
128 }
ff85f125 » jakubvrana
2009-06-01 Join echos
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) {
64ba9242 » jakubvrana
2009-06-21 Comments
134 // join two consecutive echos
ff85f125 » jakubvrana
2009-06-01 Join echos
135 next($tokens);
136 next($tokens);
137 $token[1] = '.'; //! join ''.'' and "".""
138 }
16f42102 » jakubvrana
2009-04-15 Function minification
139 } elseif ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
140 $token[1] = '$' . $short_variables[$token[1]];
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
141 } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && isset($defined_functions[$token[1]])
64ba9242 » jakubvrana
2009-06-21 Comments
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
16f42102 » jakubvrana
2009-04-15 Function minification
143 ) {
144 $token[1] = $short_functions[$token[1]];
64ba9242 » jakubvrana
2009-06-21 Comments
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
16f42102 » jakubvrana
2009-04-15 Function minification
149 $token[1] = "'" . $short_functions[substr($token[1], 1, -1)] . "'";
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
150 }
794192d9 » jakubvrana
2007-09-04 php_shrink()
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
0970a583 » jakubvrana
2007-07-05 Prepare for single language version
161 error_reporting(E_ALL & ~E_NOTICE);
7418af0c » jakubvrana
2007-07-05 Czech translation
162 if ($_SERVER["argc"] > 1) {
b8ef9fb9 » jakubvrana
2009-06-21 Use prefix for cookies
163 $_COOKIE["adminer_lang"] = $_SERVER["argv"][1]; // Adminer functions read language from cookie
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
164 include dirname(__FILE__) . "/adminer/include/lang.inc.php";
b8ef9fb9 » jakubvrana
2009-06-21 Use prefix for cookies
165 if ($_SERVER["argc"] != 2 || !isset($langs[$_COOKIE["adminer_lang"]])) {
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
166 echo "Usage: php compile.php [lang]\nPurpose: Compile adminer[-lang].php from index.php.\n";
7418af0c » jakubvrana
2007-07-05 Czech translation
167 exit(1);
168 }
b8ef9fb9 » jakubvrana
2009-06-21 Use prefix for cookies
169 include dirname(__FILE__) . "/adminer/lang/$_COOKIE[adminer_lang].inc.php";
7418af0c » jakubvrana
2007-07-05 Czech translation
170 }
bba76d8d » jakubvrana
2007-08-04 Flatten $translations
171
b8ef9fb9 » jakubvrana
2009-06-21 Use prefix for cookies
172 $filename = "adminer" . ($_COOKIE["adminer_lang"] ? "-$_COOKIE[adminer_lang]" : "") . ".php";
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
173 $file = file_get_contents(dirname(__FILE__) . "/adminer/index.php");
6bb1401d » jakubvrana
2009-06-01 Remove ?><?php
174 $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file);
bb1ae0ba » jakubvrana
2008-10-02 Code coverage
175 $file = preg_replace("~if \\(isset\\(\\\$_SESSION\\[\"coverage.*\n}\n| && !isset\\(\\\$_SESSION\\[\"coverage\"\\]\\)~sU", '', $file);
b8ef9fb9 » jakubvrana
2009-06-21 Use prefix for cookies
176 if ($_COOKIE["adminer_lang"]) {
64ba9242 » jakubvrana
2009-06-21 Comments
177 // single language version
a9907f18 » jakubvrana
2007-07-10 Greedy expressions
178 $file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file);
7418af0c » jakubvrana
2007-07-05 Czech translation
179 $file = str_replace("<?php switch_lang(); ?>\n", "", $file);
b8ef9fb9 » jakubvrana
2009-06-21 Use prefix for cookies
180 $file = str_replace('<?php echo $LANG; ?>', $_COOKIE["adminer_lang"], $file);
4738060a » jakubvrana
2008-05-23 Use numeric language IDs to save bytes
181 } else {
182 $file = preg_replace_callback("~lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])~s", 'lang_ids', $file);
0970a583 » jakubvrana
2007-07-05 Prepare for single language version
183 }
35bca85a » jakubvrana
2009-06-06 Fix XSS (thanks to Synopsi)
184 $replace = 'htmlspecialchars(preg_replace("~\\\\\\\\?.*~", "", $_SERVER["REQUEST_URI"])) . "?file=\\0&amp;version=' . $VERSION;
a76c555b » jakubvrana
2009-06-11 Remove odd() from create
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);
4619f78a » jakubvrana
2008-01-21 External stylesheet
187 $file = str_replace('error_reporting(E_ALL & ~E_NOTICE);', 'error_reporting(E_ALL & ~E_NOTICE);
188 if (isset($_GET["file"])) {
7f3b4f2b » jakubvrana
2009-05-29 Version checker
189 header("Expires: " . gmdate("D, d M Y H:i:s", time() + 365*24*60*60) . " GMT");
f3a9e46b » jakubvrana
2008-05-23 Static files never expires
190 if ($_GET["file"] == "favicon.ico") {
191 header("Content-Type: image/x-icon");
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
192 echo base64_decode("' . base64_encode(file_get_contents(dirname(__FILE__) . "/adminer/favicon.ico")) . '");
f3a9e46b » jakubvrana
2008-05-23 Static files never expires
193 } elseif ($_GET["file"] == "default.css") {
194 header("Content-Type: text/css");
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
195 ?>' . preg_replace('~\\s*([:;{},])\\s*~', '\\1', file_get_contents(dirname(__FILE__) . "/adminer/default.css")) . '<?php
540e2174 » jakubvrana
2009-05-29 Separate JavaScript
196 } elseif ($_GET["file"] == "functions.js") {
197 header("Content-Type: text/javascript");
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
198 ?>' . JSMin::minify(file_get_contents(dirname(__FILE__) . "/adminer/functions.js")) . '<?php
4619f78a » jakubvrana
2008-01-21 External stylesheet
199 } else {
f3a9e46b » jakubvrana
2008-05-23 Static files never expires
200 header("Content-Type: image/gif");
201 switch ($_GET["file"]) {
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
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;
4619f78a » jakubvrana
2008-01-21 External stylesheet
207 }
208 }
209 exit;
64ba9242 » jakubvrana
2009-06-21 Comments
210 }', $file); // integrate static files
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
211 $file = str_replace("../externals/jush/", "http://jush.sourceforge.net/", $file);
35bca85a » jakubvrana
2009-06-06 Fix XSS (thanks to Synopsi)
212 $file = preg_replace("~<\\?php\\s*\\?>\n?|\\?>\n?<\\?php~", '', $file);
794192d9 » jakubvrana
2007-09-04 php_shrink()
213 $file = php_shrink($file);
64ba9242 » jakubvrana
2009-06-21 Comments
214 fwrite(fopen($filename, "w"), $file); // file_put_contents() since PHP 5
7418af0c » jakubvrana
2007-07-05 Czech translation
215 echo "$filename created.\n";
Something went wrong with that request. Please try again.