Skip to content
Newer
Older
100644 280 lines (259 sloc) 11.2 KB
667bfec Decomposition
jakubvrana authored
1 <?php
1bc77f8 Report user errors
jakubvrana authored
2 error_reporting(6135); // errors and warnings
e1bc7af Change directory structure
jakubvrana authored
3 include dirname(__FILE__) . "/adminer/include/version.inc.php";
b8b4548 Utilize JSMin
jakubvrana authored
4 include dirname(__FILE__) . "/externals/jsmin-php/jsmin.php";
7f3b4f2 Version checker
jakubvrana authored
5
c1f8382 @vrana Internal JSMin if external is not found
authored
6 if (!class_exists("JSMin")) {
7 /** Simple JS minifier without full support for regex literals
8 * @link http://pastebin.com/2Jc2swSr
9 */
10 class JSMin {
11 /*private static*/ function callback($match) {
12 $s = trim($match[0]);
13 return ($s === "" ? "\n" : ($s[0] === "/" && ($s[1] === "*" || $s[1] === "/") ? "" : $s));
14 }
15
16 /*static*/ function minify($input) {
17 return preg_replace_callback('~//[^\n]*|/\*.*?\*/|/(?!\s)(?:\\\\.|[^/\\\\])*/|\'(?:\\\\.|[^\'\\\\])*\'|"(?:\\\\.|[^"\\\\])*"|\s*[^0-9a-z_$\'"/\s]\s*|\s+~si', array('JSMin', 'callback'), $input);
18 }
19 }
20 }
21
8943678 sprintf() in lang()
jakubvrana authored
22 function add_apo_slashes($s) {
23 return addcslashes($s, "\\'");
24 }
25
0970a58 Prepare for single language version
jakubvrana authored
26 function remove_lang($match) {
bba76d8 Flatten $translations
jakubvrana authored
27 global $translations;
438ca05 Separate languages
jakubvrana authored
28 $idf = strtr($match[2], array("\\'" => "'", "\\\\" => "\\"));
bba76d8 Flatten $translations
jakubvrana authored
29 $s = ($translations[$idf] ? $translations[$idf] : $idf);
64ba924 Comments
jakubvrana authored
30 if ($match[3] == ",") { // lang() has parameters
8943678 sprintf() in lang()
jakubvrana authored
31 return "$match[1]" . (is_array($s) ? "lang(array('" . implode("', '", array_map('add_apo_slashes', $s)) . "')," : "sprintf('" . add_apo_slashes($s) . "',");
32 }
33 return ($match[1] && $match[4] ? $s : "$match[1]'" . add_apo_slashes($s) . "'$match[4]");
0970a58 Prepare for single language version
jakubvrana authored
34 }
35
4738060 Use numeric language IDs to save bytes
jakubvrana authored
36 function lang_ids($match) {
37 global $lang_ids;
c16c57b E-mail sending
jakubvrana authored
38 $lang_id = &$lang_ids[stripslashes($match[1])];
39 if (!isset($lang_id)) {
40 $lang_id = count($lang_ids) - 1;
41 }
0116b9d Don't set cookie in compile scripts
jakubvrana authored
42 return ($_SESSION["lang"] ? $match[0] : "lang($lang_id$match[2]");
4738060 Use numeric language IDs to save bytes
jakubvrana authored
43 }
44
667bfec Decomposition
jakubvrana authored
45 function put_file($match) {
c16c57b E-mail sending
jakubvrana authored
46 global $project;
b95f24e Editor: User friendly data editor
jakubvrana authored
47 if (basename($match[2]) == '$LANG.inc.php') {
c16c57b E-mail sending
jakubvrana authored
48 return $match[0]; // processed later
438ca05 Separate languages
jakubvrana authored
49 }
b95f24e Editor: User friendly data editor
jakubvrana authored
50 $return = file_get_contents(dirname(__FILE__) . "/$project/$match[2]");
0116b9d Don't set cookie in compile scripts
jakubvrana authored
51 if (basename($match[2]) != "lang.inc.php" || !$_SESSION["lang"]) {
64ba924 Comments
jakubvrana authored
52 $tokens = token_get_all($return); // to find out the last token
6bb1401 Remove ?><?php
jakubvrana authored
53 return "?>\n$return" . (in_array($tokens[count($tokens) - 1][0], array(T_CLOSE_TAG, T_INLINE_HTML), true) ? "<?php" : "");
3fa68ce @vrana Slovenian translation
authored
54 } elseif (preg_match('~\\s*(\\$pos = .*;)~sU', $return, $match2)) {
64ba924 Comments
jakubvrana authored
55 // single language lang() is used for plural
e37c427 @vrana Function get_lang useful for customization
authored
56 return "function get_lang() {
57 return '$_SESSION[lang]';
58 }
59
60 function lang(\$translation, \$number) {
61 " . str_replace('$LANG', "'$_SESSION[lang]'", $match2[1]) . '
62 return sprintf($translation[$pos], $number);
63 }
64 ';
6bb1401 Remove ?><?php
jakubvrana authored
65 } else {
66 echo "lang() not found\n";
667bfec Decomposition
jakubvrana authored
67 }
68 }
69
c16c57b E-mail sending
jakubvrana authored
70 function put_file_lang($match) {
b4b5977 @vrana Include only active translations
authored
71 global $lang_ids, $project, $langs;
0116b9d Don't set cookie in compile scripts
jakubvrana authored
72 if ($_SESSION["lang"]) {
c16c57b E-mail sending
jakubvrana authored
73 return "";
74 }
75 $return = "";
b4b5977 @vrana Include only active translations
authored
76 foreach ($langs as $lang => $val) {
77 include dirname(__FILE__) . "/adminer/lang/$lang.inc.php"; // assign $translations
c16c57b E-mail sending
jakubvrana authored
78 $translation_ids = array_flip($lang_ids); // default translation
79 foreach ($translations as $key => $val) {
80 if (isset($val)) {
81 $translation_ids[$lang_ids[$key]] = $val;
82 }
83 }
b4b5977 @vrana Include only active translations
authored
84 $return .= "\tcase \"$lang\": \$translations = array(";
c16c57b E-mail sending
jakubvrana authored
85 foreach ($translation_ids as $val) {
86 $return .= (is_array($val) ? "array('" . implode("', '", array_map('add_apo_slashes', $val)) . "')" : "'" . add_apo_slashes($val) . "'") . ", ";
87 }
88 $return = substr($return, 0, -2) . "); break;\n";
89 }
90 return "switch (\$LANG) {\n$return}\n";
91 }
92
16f4210 Function minification
jakubvrana authored
93 function short_identifier($number, $chars) {
94 $return = '';
a06f4c1 Size reduction by minification of variables
jakubvrana authored
95 while ($number >= 0) {
96 $return .= $chars{$number % strlen($chars)};
97 $number = floor($number / strlen($chars)) - 1;
98 }
99 return $return;
100 }
101
004d7a9 Link
jakubvrana authored
102 // based on http://latrine.dgx.cz/jak-zredukovat-php-skripty
794192d php_shrink()
jakubvrana authored
103 function php_shrink($input) {
cbdf09d @vrana Show version in comment (thanks to David Grudl)
authored
104 global $VERSION;
815eb26 Use one token for all
jakubvrana authored
105 $special_variables = array_flip(array('$this', '$GLOBALS', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_SERVER'));
12d2c6d Build Adminer and Editor together
jakubvrana authored
106 $short_variables = array();
a06f4c1 Size reduction by minification of variables
jakubvrana authored
107 $shortening = true;
16f4210 Function minification
jakubvrana authored
108 $tokens = token_get_all($input);
109
110 foreach ($tokens as $i => $token) {
111 if ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
112 $short_variables[$token[1]]++;
113 }
114 }
c27a877 Minify defined functions only
jakubvrana authored
115
16f4210 Function minification
jakubvrana authored
116 arsort($short_variables);
117 foreach (array_keys($short_variables) as $number => $key) {
7f3ccd3 @vrana Utilize single parameter implode
authored
118 $short_variables[$key] = short_identifier($number, implode(range('a', 'z')) . '_' . implode(range('A', 'Z'))); // could use also numbers and \x7f-\xff
16f4210 Function minification
jakubvrana authored
119 }
a06f4c1 Size reduction by minification of variables
jakubvrana authored
120
794192d php_shrink()
jakubvrana authored
121 $set = array_flip(preg_split('//', '!"#$&\'()*+,-./:;<=>?@[\]^`{|}'));
122 $space = '';
123 $output = '';
ff85f12 Join echos
jakubvrana authored
124 $in_echo = false;
c16c57b E-mail sending
jakubvrana authored
125 $doc_comment = false; // include only first /**
ff85f12 Join echos
jakubvrana authored
126 for (reset($tokens); list($i, $token) = each($tokens); ) {
794192d php_shrink()
jakubvrana authored
127 if (!is_array($token)) {
128 $token = array(0, $token);
129 }
dbf9566 Better consecutive echo handling
jakubvrana authored
130 if ($tokens[$i+2][0] === T_CLOSE_TAG && $tokens[$i+3][0] === T_INLINE_HTML && $tokens[$i+4][0] === T_OPEN_TAG
fc3b652 Whitespace
jakubvrana authored
131 && strlen(addcslashes($tokens[$i+3][1], "'\\")) < strlen($tokens[$i+3][1]) + 3
dbf9566 Better consecutive echo handling
jakubvrana authored
132 ) {
133 $tokens[$i+2] = array(T_ECHO, 'echo');
134 $tokens[$i+3] = array(T_CONSTANT_ENCAPSED_STRING, "'" . addcslashes($tokens[$i+3][1], "'\\") . "'");
135 $tokens[$i+4] = array(0, ';');
136 }
c16c57b E-mail sending
jakubvrana authored
137 if ($token[0] == T_COMMENT || $token[0] == T_WHITESPACE || ($token[0] == T_DOC_COMMENT && $doc_comment)) {
794192d php_shrink()
jakubvrana authored
138 $space = "\n";
139 } else {
c16c57b E-mail sending
jakubvrana authored
140 if ($token[0] == T_DOC_COMMENT) {
141 $doc_comment = true;
cbdf09d @vrana Show version in comment (thanks to David Grudl)
authored
142 $token[1] = substr_replace($token[1], "* @version $VERSION\n", -2, 0);
c16c57b E-mail sending
jakubvrana authored
143 }
a06f4c1 Size reduction by minification of variables
jakubvrana authored
144 if ($token[0] == T_VAR) {
145 $shortening = false;
146 } elseif (!$shortening) {
147 if ($token[1] == ';') {
148 $shortening = true;
149 }
ff85f12 Join echos
jakubvrana authored
150 } elseif ($token[0] == T_ECHO) {
151 $in_echo = true;
152 } elseif ($token[1] == ';' && $in_echo) {
e2bc1da No comments without whitespace
jakubvrana authored
153 if ($tokens[$i+1][0] === T_WHITESPACE && $tokens[$i+2][0] === T_ECHO) {
ff85f12 Join echos
jakubvrana authored
154 next($tokens);
c030c16 Compile inline HTML to echo
jakubvrana authored
155 $i++;
156 }
157 if ($tokens[$i+1][0] === T_ECHO) {
158 // join two consecutive echos
ff85f12 Join echos
jakubvrana authored
159 next($tokens);
02046d6 Windows support in e-mail attachments
jakubvrana authored
160 $token[1] = ','; // '.' would conflict with "a".1+2 and would use more memory //! remove ',' and "," but not $var","
dbf9566 Better consecutive echo handling
jakubvrana authored
161 } else {
162 $in_echo = false;
ff85f12 Join echos
jakubvrana authored
163 }
16f4210 Function minification
jakubvrana authored
164 } elseif ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
165 $token[1] = '$' . $short_variables[$token[1]];
a06f4c1 Size reduction by minification of variables
jakubvrana authored
166 }
3f5b683 Reintegrate sqlite branch
jakubvrana authored
167 if (isset($set[substr($output, -1)]) || isset($set[$token[1][0]])) {
794192d php_shrink()
jakubvrana authored
168 $space = '';
169 }
170 $output .= $space . $token[1];
171 $space = '';
172 }
173 }
174 return $output;
175 }
176
9ee9cf0 Remove code generation
jakubvrana authored
177 function minify_css($file) {
1be289e @vrana Optimize compiling files
authored
178 return preg_replace('~\\s*([:;{},])\\s*~', '\\1', preg_replace('~/\\*.*\\*/~sU', '', $file));
9ee9cf0 Remove code generation
jakubvrana authored
179 }
180
181 function compile_file($match) {
df03f4f Join editing.js in compilation
jakubvrana authored
182 global $project;
183 return call_user_func($match[2], file_get_contents(dirname(__FILE__) . "/$project/$match[1]"));
9ee9cf0 Remove code generation
jakubvrana authored
184 }
185
e672694 @vrana Rename $driver to $jush
authored
186 $driver = "";
3f5b683 Reintegrate sqlite branch
jakubvrana authored
187 if (file_exists(dirname(__FILE__) . "/adminer/drivers/" . $_SERVER["argv"][1] . ".inc.php")) {
e672694 @vrana Rename $driver to $jush
authored
188 $driver = $_SERVER["argv"][1];
3f5b683 Reintegrate sqlite branch
jakubvrana authored
189 array_shift($_SERVER["argv"]);
190 }
191
0116b9d Don't set cookie in compile scripts
jakubvrana authored
192 unset($_COOKIE["adminer_lang"]);
193 $_SESSION["lang"] = $_SERVER["argv"][1]; // Adminer functions read language from session
3f5b683 Reintegrate sqlite branch
jakubvrana authored
194 include dirname(__FILE__) . "/adminer/include/lang.inc.php";
0116b9d Don't set cookie in compile scripts
jakubvrana authored
195 if (isset($_SESSION["lang"])) {
196 if (isset($_SERVER["argv"][2]) || !isset($langs[$_SESSION["lang"]])) {
12d2c6d Build Adminer and Editor together
jakubvrana authored
197 echo "Usage: php compile.php [lang]\nPurpose: Compile adminer[-lang].php and editor[-lang].php.\n";
7418af0 Czech translation
jakubvrana authored
198 exit(1);
199 }
0116b9d Don't set cookie in compile scripts
jakubvrana authored
200 include dirname(__FILE__) . "/adminer/lang/$_SESSION[lang].inc.php";
7418af0 Czech translation
jakubvrana authored
201 }
bba76d8 Flatten $translations
jakubvrana authored
202
3f5b683 Reintegrate sqlite branch
jakubvrana authored
203 // check function definition in drivers
204 $filename = dirname(__FILE__) . "/adminer/drivers/mysql.inc.php";
205 preg_match_all('~\\bfunction ([^(]+)~', file_get_contents($filename), $matches); //! respect context (extension, class)
206 $functions = array_combine($matches[1], $matches[0]);
207 unset($functions["__destruct"], $functions["Min_DB"], $functions["Min_Result"]);
e672694 @vrana Rename $driver to $jush
authored
208 foreach (glob(dirname(__FILE__) . "/adminer/drivers/" . ($driver ? $driver : "*") . ".inc.php") as $filename) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
209 if ($filename != "mysql.inc.php") {
210 $file = file_get_contents($filename);
211 foreach ($functions as $val) {
212 if (!strpos($file, "$val(")) {
213 echo "Missing $val in $filename\n";
214 }
215 }
216 }
217 }
218
e672694 @vrana Rename $driver to $jush
authored
219 include dirname(__FILE__) . "/adminer/include/pdo.inc.php";
955e6b4 @vrana PostgreSQL supports only functions
authored
220 $features = array("call" => "routine", "dump", "event", "privileges", "procedure" => "routine", "processlist", "routine", "scheme", "sequence", "status", "trigger", "type", "user" => "privileges", "variables", "view");
12d2c6d Build Adminer and Editor together
jakubvrana authored
221 foreach (array("adminer", "editor") as $project) {
222 $lang_ids = array(); // global variable simplifies usage in a callback function
223 $file = file_get_contents(dirname(__FILE__) . "/$project/index.php");
e672694 @vrana Rename $driver to $jush
authored
224 if ($driver) {
6420c58 Schema support for PostgreSQL
jakubvrana authored
225 $connection = (object) array("server_info" => 5.1); // MySQL support is version specific
e672694 @vrana Rename $driver to $jush
authored
226 $_GET[$driver] = true; // to load the driver
227 include_once dirname(__FILE__) . "/adminer/drivers/$driver.inc.php";
955e6b4 @vrana PostgreSQL supports only functions
authored
228 foreach ($features as $key => $feature) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
229 if (!support($feature)) {
955e6b4 @vrana PostgreSQL supports only functions
authored
230 if (!is_int($key)) {
231 $feature = $key;
232 }
3f5b683 Reintegrate sqlite branch
jakubvrana authored
233 $file = str_replace("} elseif (isset(\$_GET[\"$feature\"])) {\n\tinclude \"./$feature.inc.php\";\n", "", $file);
234 }
235 }
236 if (!support("routine")) {
237 $file = str_replace("if (isset(\$_GET[\"callf\"])) {\n\t\$_GET[\"call\"] = \$_GET[\"callf\"];\n}\nif (isset(\$_GET[\"function\"])) {\n\t\$_GET[\"procedure\"] = \$_GET[\"function\"];\n}\n", "", $file);
238 }
239 }
12d2c6d Build Adminer and Editor together
jakubvrana authored
240 $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file);
241 $file = str_replace('include "../adminer/include/coverage.inc.php";', '', $file);
e672694 @vrana Rename $driver to $jush
authored
242 if ($driver) {
243 $file = preg_replace('(include "../adminer/drivers/(?!' . preg_quote($driver) . ').*\\s*)', '', $file);
3f5b683 Reintegrate sqlite branch
jakubvrana authored
244 }
12d2c6d Build Adminer and Editor together
jakubvrana authored
245 $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file); // bootstrap.inc.php
e678e46 @vrana Remove unsupported features in single driver version
authored
246 if ($driver) {
247 foreach ($features as $feature) {
248 if (!support($feature)) {
249 $file = preg_replace("((\t*)" . preg_quote('if (support("' . $feature . '")') . ".*\n\\1\\})sU", '', $file);
250 }
251 }
4b9d451 @vrana SQLite version supports two systems
authored
252 if (count($drivers) == 1) {
30cfa61 @vrana Fix MySQL only version
authored
253 $file = str_replace('<?php echo html_select("driver", $drivers, DRIVER); ?>', "<input type='hidden' name='driver' value='" . ($driver == "mysql" ? "server" : $driver) . "'>" . reset($drivers), $file);
4b9d451 @vrana SQLite version supports two systems
authored
254 }
e678e46 @vrana Remove unsupported features in single driver version
authored
255 }
12d2c6d Build Adminer and Editor together
jakubvrana authored
256 $file = preg_replace_callback("~lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])~s", 'lang_ids', $file);
257 $file = preg_replace_callback('~\\b(include|require) "([^"]*\\$LANG.inc.php)";~', 'put_file_lang', $file);
53804a2 @vrana Immunity against CRLF
authored
258 $file = str_replace("\r", "", $file);
12d2c6d Build Adminer and Editor together
jakubvrana authored
259 if ($_SESSION["lang"]) {
260 // single language version
261 $file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file);
262 $file = str_replace("<?php switch_lang(); ?>\n", "", $file);
263 $file = str_replace('<?php echo $LANG; ?>', $_SESSION["lang"], $file);
264 }
265 $file = str_replace('<script type="text/javascript" src="static/editing.js"></script>' . "\n", "", $file);
266 $file = preg_replace_callback("~compile_file\\('([^']+)', '([^']+)'\\);~", 'compile_file', $file); // integrate static files
1be289e @vrana Optimize compiling files
authored
267 $replace = 'h(preg_replace("~\\\\\\\\?.*~", "", ME)) . "?file=\\1&amp;version=' . $VERSION;
d55ba18 @vrana Customizable favicon
authored
268 $file = preg_replace("~'\\.\\./adminer/static/(loader\\.gif)~", "location.pathname+'?file=\\1&amp;version=$VERSION", $file);
7eb85a7 @vrana Use AJAX in select
authored
269 $file = preg_replace('~\\.\\./adminer/static/(loader\\.gif)~', "'+location.pathname+'?file=\\1&amp;version=$VERSION", $file);
12d2c6d Build Adminer and Editor together
jakubvrana authored
270 $file = preg_replace('~\\.\\./adminer/static/(default\\.css|functions\\.js|favicon\\.ico)~', '<?php echo ' . $replace . '"; ?>', $file);
271 $file = preg_replace('~\\.\\./adminer/static/([^\'"]*)~', '" . ' . $replace, $file);
e4deeb6 @vrana Utilize location.protocol
authored
272 $file = str_replace("'../externals/jush/'", "location.protocol + '//www.adminer.org/static/'", $file);
12d2c6d Build Adminer and Editor together
jakubvrana authored
273 $file = preg_replace("~<\\?php\\s*\\?>\n?|\\?>\n?<\\?php~", '', $file);
274 $file = php_shrink($file);
c02bd44 Use $filename
jakubvrana authored
275
e672694 @vrana Rename $driver to $jush
authored
276 $filename = $project . (preg_match('~-dev$~', $VERSION) ? "" : "-$VERSION") . ($driver ? "-$driver" : "") . ($_SESSION["lang"] ? "-$_SESSION[lang]" : "") . ".php";
12d2c6d Build Adminer and Editor together
jakubvrana authored
277 fwrite(fopen($filename, "w"), $file); // file_put_contents() since PHP 5
3f5b683 Reintegrate sqlite branch
jakubvrana authored
278 echo "$filename created (" . strlen($file) . " B).\n";
12d2c6d Build Adminer and Editor together
jakubvrana authored
279 }
Something went wrong with that request. Please try again.