Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 208 lines (197 sloc) 8.918 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);
89436789 » jakubvrana
2007-07-09 sprintf() in lang()
13 if ($match[3] == ",") {
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
4738060a » jakubvrana
2008-05-23 Use numeric language IDs to save bytes
19 $lang_ids = array();
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') {
bba76d8d » jakubvrana
2007-08-04 Flatten $translations
28 if ($_COOKIE["lang"]) {
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) {
4738060a » jakubvrana
2008-05-23 Use numeric language IDs to save bytes
33 include $filename;
34 foreach ($translations as $key => $val) {
35 if (!isset($lang_ids[$key])) {
36 $lang_ids[$key] = count($lang_ids);
37 }
38 }
932a7192 » jakubvrana
2008-05-24 Fill $lang_ids first
39 }
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
40 foreach (glob(dirname(__FILE__) . "/adminer/lang/*.inc.php") as $filename) {
932a7192 » jakubvrana
2008-05-24 Fill $lang_ids first
41 include $filename;
4738060a » jakubvrana
2008-05-23 Use numeric language IDs to save bytes
42 $translation_ids = array_flip($lang_ids);
43 foreach ($translations as $key => $val) {
44 $translation_ids[$lang_ids[$key]] = $val;
45 }
46 $return .= 'case "' . basename($filename, '.inc.php') . '": $translations = array(';
47 foreach ($translation_ids as $val) {
48 $return .= (is_array($val) ? "array('" . implode("', '", array_map('add_apo_slashes', $val)) . "')" : "'" . add_apo_slashes($val) . "'") . ", ";
49 }
eb7e599e » jakubvrana
2008-05-26 Remove extra comma
50 $return = substr($return, 0, -2) . "); break;\n";
bba76d8d » jakubvrana
2007-08-04 Flatten $translations
51 }
c6458077 » jakubvrana
2007-08-07 PHP 4 compatibility
52 return "switch (\$LANG) {\n$return}\n";
438ca058 » jakubvrana
2007-07-26 Separate languages
53 }
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
54 $return = file_get_contents(dirname(__FILE__) . "/adminer/$match[2]");
6bb1401d » jakubvrana
2009-06-01 Remove ?><?php
55 if ($match[2] != "./include/lang.inc.php" || !$_COOKIE["lang"]) {
56 $tokens = token_get_all($return);
57 return "?>\n$return" . (in_array($tokens[count($tokens) - 1][0], array(T_CLOSE_TAG, T_INLINE_HTML), true) ? "<?php" : "");
58 } elseif (preg_match('~\\s*(\\$pos = .*)~', $return, $match2)) {
59 return "function lang(\$translation, \$number) {\n\t" . str_replace('$LANG', "'$_COOKIE[lang]'", $match2[1]) . "\n\treturn sprintf(\$translation[\$pos], \$number);\n}\n";
60 } else {
61 echo "lang() not found\n";
667bfec4 » jakubvrana
2007-07-02 Decomposition
62 }
63 }
64
16f42102 » jakubvrana
2009-04-15 Function minification
65 function short_identifier($number, $chars) {
66 $return = '';
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
67 while ($number >= 0) {
68 $return .= $chars{$number % strlen($chars)};
69 $number = floor($number / strlen($chars)) - 1;
70 }
71 return $return;
72 }
73
5ef93226 » jakubvrana
2009-05-08 Separate PDO
74 // based on Dgx's PHP shrinker
794192d9 » jakubvrana
2007-09-04 php_shrink()
75 function php_shrink($input) {
815eb26e » jakubvrana
2009-05-08 Use one token for all
76 $special_variables = array_flip(array('$this', '$GLOBALS', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_SERVER'));
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
77 static $short_variables = array();
78 $shortening = true;
fbebb087 » jakubvrana
2009-06-03 Use separate connection for exploring indexes
79 $special_functions = array_flip(array('Min_DB', 'Min_Result', '__construct'));
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
80 $defined_functions = array();
16f42102 » jakubvrana
2009-04-15 Function minification
81 static $short_functions = array();
82 $tokens = token_get_all($input);
83
84 foreach ($tokens as $i => $token) {
85 if ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
86 $short_variables[$token[1]]++;
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
87 } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && !isset($special_functions[$token[1]])) {
16f42102 » jakubvrana
2009-04-15 Function minification
88 $short_functions[$token[1]]++;
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
89 if ($tokens[$i-2][0] === T_FUNCTION) {
90 $defined_functions[$token[1]] = true;
91 }
16f42102 » jakubvrana
2009-04-15 Function minification
92 }
93 }
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
94
16f42102 » jakubvrana
2009-04-15 Function minification
95 arsort($short_variables);
96 foreach (array_keys($short_variables) as $number => $key) {
97 $short_variables[$key] = short_identifier($number, implode("", range('a', 'z')) . '_' . implode("", range('A', 'Z'))); // could use also numbers and \x7f-\xff
98 }
99 arsort($short_functions);
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
100 $number = 0;
101 foreach ($short_functions as $key => $val) {
102 if (isset($defined_functions[$key])) {
3aaeff0d » jakubvrana
2009-04-17 Prevent name conflicts in function minification
103 do {
f80a20ff » jakubvrana
2009-05-03 Don't use _ as function name - Gettext compatibility (bug #2786028)
104 $short_functions[$key] = short_identifier($number, implode("", range('a', 'z')));
3aaeff0d » jakubvrana
2009-04-17 Prevent name conflicts in function minification
105 $number++;
106 } while (isset($short_functions[$short_functions[$key]]));
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
107 }
16f42102 » jakubvrana
2009-04-15 Function minification
108 }
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
109
794192d9 » jakubvrana
2007-09-04 php_shrink()
110 $set = array_flip(preg_split('//', '!"#$&\'()*+,-./:;<=>?@[\]^`{|}'));
111 $space = '';
112 $output = '';
ff85f125 » jakubvrana
2009-06-01 Join echos
113 $in_echo = false;
114 for (reset($tokens); list($i, $token) = each($tokens); ) {
794192d9 » jakubvrana
2007-09-04 php_shrink()
115 if (!is_array($token)) {
116 $token = array(0, $token);
117 }
118 if ($token[0] == T_COMMENT || $token[0] == T_WHITESPACE) {
119 $space = "\n";
120 } else {
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
121 if ($token[0] == T_VAR) {
122 $shortening = false;
123 } elseif (!$shortening) {
124 if ($token[1] == ';') {
125 $shortening = true;
126 }
ff85f125 » jakubvrana
2009-06-01 Join echos
127 } elseif ($token[0] == T_ECHO) {
128 $in_echo = true;
129 } elseif ($token[1] == ';' && $in_echo) {
130 $in_echo = false;
131 if ($tokens[$i+1][0] === T_WHITESPACE && $tokens[$i+2][0] === T_ECHO) {
132 next($tokens);
133 next($tokens);
134 $token[1] = '.'; //! join ''.'' and "".""
135 }
16f42102 » jakubvrana
2009-04-15 Function minification
136 } elseif ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
137 $token[1] = '$' . $short_variables[$token[1]];
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
138 } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && isset($defined_functions[$token[1]])
c97325b0 » jakubvrana
2009-04-16 $_result is not needed for minification
139 && $tokens[$i-1][0] !== T_DOUBLE_COLON && $tokens[$i-2][0] !== T_NEW && $tokens[$i-2][1] !== '_result'
16f42102 » jakubvrana
2009-04-15 Function minification
140 ) {
141 $token[1] = $short_functions[$token[1]];
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
142 } elseif ($token[0] == T_CONSTANT_ENCAPSED_STRING && (($tokens[$i-1] === '(' && in_array($tokens[$i-2][1], array('array_map', 'set_exception_handler'), true)) || $token[1] == "'normalize_enum'") && isset($defined_functions[substr($token[1], 1, -1)])) {
16f42102 » jakubvrana
2009-04-15 Function minification
143 $token[1] = "'" . $short_functions[substr($token[1], 1, -1)] . "'";
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
144 }
794192d9 » jakubvrana
2007-09-04 php_shrink()
145 if (isset($set[substr($output, -1)]) || isset($set[$token[1]{0}])) {
146 $space = '';
147 }
148 $output .= $space . $token[1];
149 $space = '';
150 }
151 }
152 return $output;
153 }
154
0970a583 » jakubvrana
2007-07-05 Prepare for single language version
155 error_reporting(E_ALL & ~E_NOTICE);
7418af0c » jakubvrana
2007-07-05 Czech translation
156 if ($_SERVER["argc"] > 1) {
222fc786 » jakubvrana
2007-07-13 Save lang to cookie
157 $_COOKIE["lang"] = $_SERVER["argv"][1];
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
158 include dirname(__FILE__) . "/adminer/include/lang.inc.php";
bba76d8d » jakubvrana
2007-08-04 Flatten $translations
159 if ($_SERVER["argc"] != 2 || !isset($langs[$_COOKIE["lang"]])) {
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
160 echo "Usage: php compile.php [lang]\nPurpose: Compile adminer[-lang].php from index.php.\n";
7418af0c » jakubvrana
2007-07-05 Czech translation
161 exit(1);
162 }
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
163 include dirname(__FILE__) . "/adminer/lang/$_COOKIE[lang].inc.php";
7418af0c » jakubvrana
2007-07-05 Czech translation
164 }
bba76d8d » jakubvrana
2007-08-04 Flatten $translations
165
1fe2f33e » jakubvrana
2009-06-08 Rename to Adminer
166 $filename = "adminer" . ($_COOKIE["lang"] ? "-$_COOKIE[lang]" : "") . ".php";
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
167 $file = file_get_contents(dirname(__FILE__) . "/adminer/index.php");
6bb1401d » jakubvrana
2009-06-01 Remove ?><?php
168 $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file);
bb1ae0ba » jakubvrana
2008-10-02 Code coverage
169 $file = preg_replace("~if \\(isset\\(\\\$_SESSION\\[\"coverage.*\n}\n| && !isset\\(\\\$_SESSION\\[\"coverage\"\\]\\)~sU", '', $file);
222fc786 » jakubvrana
2007-07-13 Save lang to cookie
170 if ($_COOKIE["lang"]) {
a9907f18 » jakubvrana
2007-07-10 Greedy expressions
171 $file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file);
7418af0c » jakubvrana
2007-07-05 Czech translation
172 $file = str_replace("<?php switch_lang(); ?>\n", "", $file);
4eb07d70 » jakubvrana
2007-07-17 Globalize $translations
173 $file = str_replace('<?php echo $LANG; ?>', $_COOKIE["lang"], $file);
4738060a » jakubvrana
2008-05-23 Use numeric language IDs to save bytes
174 } else {
175 $file = preg_replace_callback("~lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])~s", 'lang_ids', $file);
0970a583 » jakubvrana
2007-07-05 Prepare for single language version
176 }
35bca85a » jakubvrana
2009-06-06 Fix XSS (thanks to Synopsi)
177 $replace = 'htmlspecialchars(preg_replace("~\\\\\\\\?.*~", "", $_SERVER["REQUEST_URI"])) . "?file=\\0&amp;version=' . $VERSION;
a76c555b » jakubvrana
2009-06-11 Remove odd() from create
178 $file = preg_replace('~default\\.css|functions\\.js|favicon\\.ico~', '<?php echo ' . $replace . '"; ?>', $file);
179 $file = preg_replace('~(plus|cross|up|down|arrow)\\.gif~', '" . ' . $replace, $file);
4619f78a » jakubvrana
2008-01-21 External stylesheet
180 $file = str_replace('error_reporting(E_ALL & ~E_NOTICE);', 'error_reporting(E_ALL & ~E_NOTICE);
181 if (isset($_GET["file"])) {
7f3b4f2b » jakubvrana
2009-05-29 Version checker
182 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
183 if ($_GET["file"] == "favicon.ico") {
184 header("Content-Type: image/x-icon");
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
185 echo base64_decode("' . base64_encode(file_get_contents(dirname(__FILE__) . "/adminer/favicon.ico")) . '");
f3a9e46b » jakubvrana
2008-05-23 Static files never expires
186 } elseif ($_GET["file"] == "default.css") {
187 header("Content-Type: text/css");
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
188 ?>' . preg_replace('~\\s*([:;{},])\\s*~', '\\1', file_get_contents(dirname(__FILE__) . "/adminer/default.css")) . '<?php
540e2174 » jakubvrana
2009-05-29 Separate JavaScript
189 } elseif ($_GET["file"] == "functions.js") {
190 header("Content-Type: text/javascript");
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
191 ?>' . JSMin::minify(file_get_contents(dirname(__FILE__) . "/adminer/functions.js")) . '<?php
4619f78a » jakubvrana
2008-01-21 External stylesheet
192 } else {
f3a9e46b » jakubvrana
2008-05-23 Static files never expires
193 header("Content-Type: image/gif");
194 switch ($_GET["file"]) {
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
195 case "arrow.gif": echo base64_decode("' . base64_encode(file_get_contents(dirname(__FILE__) . "/adminer/arrow.gif")) . '"); break;
196 case "up.gif": echo base64_decode("' . base64_encode(file_get_contents(dirname(__FILE__) . "/adminer/up.gif")) . '"); break;
197 case "down.gif": echo base64_decode("' . base64_encode(file_get_contents(dirname(__FILE__) . "/adminer/down.gif")) . '"); break;
198 case "plus.gif": echo base64_decode("' . base64_encode(file_get_contents(dirname(__FILE__) . "/adminer/plus.gif")) . '"); break;
199 case "cross.gif": echo base64_decode("' . base64_encode(file_get_contents(dirname(__FILE__) . "/adminer/cross.gif")) . '"); break;
4619f78a » jakubvrana
2008-01-21 External stylesheet
200 }
201 }
202 exit;
203 }', $file);
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
204 $file = str_replace("../externals/jush/", "http://jush.sourceforge.net/", $file);
35bca85a » jakubvrana
2009-06-06 Fix XSS (thanks to Synopsi)
205 $file = preg_replace("~<\\?php\\s*\\?>\n?|\\?>\n?<\\?php~", '', $file);
794192d9 » jakubvrana
2007-09-04 php_shrink()
206 $file = php_shrink($file);
c6458077 » jakubvrana
2007-08-07 PHP 4 compatibility
207 fwrite(fopen($filename, "w"), $file);
7418af0c » jakubvrana
2007-07-05 Czech translation
208 echo "$filename created.\n";
Something went wrong with that request. Please try again.