Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 254 lines (235 sloc) 10.602 kb
667bfec4 » jakubvrana
2007-07-02 Decomposition
1 <?php
1bc77f88 » jakubvrana
2009-08-28 Report user errors
2 error_reporting(6135); // errors and warnings
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
3 include dirname(__FILE__) . "/adminer/include/version.inc.php";
b8b45482 » jakubvrana
2009-05-29 Utilize JSMin
4 include dirname(__FILE__) . "/externals/jsmin-php/jsmin.php";
7f3b4f2b » jakubvrana
2009-05-29 Version checker
5
c1f83825 »
2010-05-06 Internal JSMin if external is not found
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
89436789 » jakubvrana
2007-07-09 sprintf() in lang()
22 function add_apo_slashes($s) {
23 return addcslashes($s, "\\'");
24 }
25
0970a583 » jakubvrana
2007-07-05 Prepare for single language version
26 function remove_lang($match) {
bba76d8d » jakubvrana
2007-08-04 Flatten $translations
27 global $translations;
438ca058 » jakubvrana
2007-07-26 Separate languages
28 $idf = strtr($match[2], array("\\'" => "'", "\\\\" => "\\"));
bba76d8d » jakubvrana
2007-08-04 Flatten $translations
29 $s = ($translations[$idf] ? $translations[$idf] : $idf);
64ba9242 » jakubvrana
2009-06-21 Comments
30 if ($match[3] == ",") { // lang() has parameters
89436789 » jakubvrana
2007-07-09 sprintf() in lang()
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]");
0970a583 » jakubvrana
2007-07-05 Prepare for single language version
34 }
35
4738060a » jakubvrana
2008-05-23 Use numeric language IDs to save bytes
36 function lang_ids($match) {
37 global $lang_ids;
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
38 $lang_id = &$lang_ids[stripslashes($match[1])];
39 if (!isset($lang_id)) {
40 $lang_id = count($lang_ids) - 1;
41 }
0116b9d1 » jakubvrana
2009-09-10 Don't set cookie in compile scripts
42 return ($_SESSION["lang"] ? $match[0] : "lang($lang_id$match[2]");
4738060a » jakubvrana
2008-05-23 Use numeric language IDs to save bytes
43 }
44
667bfec4 » jakubvrana
2007-07-02 Decomposition
45 function put_file($match) {
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
46 global $project;
b95f24ea » jakubvrana
2009-07-03 Editor: User friendly data editor
47 if (basename($match[2]) == '$LANG.inc.php') {
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
48 return $match[0]; // processed later
438ca058 » jakubvrana
2007-07-26 Separate languages
49 }
b95f24ea » jakubvrana
2009-07-03 Editor: User friendly data editor
50 $return = file_get_contents(dirname(__FILE__) . "/$project/$match[2]");
0116b9d1 » jakubvrana
2009-09-10 Don't set cookie in compile scripts
51 if (basename($match[2]) != "lang.inc.php" || !$_SESSION["lang"]) {
64ba9242 » jakubvrana
2009-06-21 Comments
52 $tokens = token_get_all($return); // to find out the last token
6bb1401d » jakubvrana
2009-06-01 Remove ?><?php
53 return "?>\n$return" . (in_array($tokens[count($tokens) - 1][0], array(T_CLOSE_TAG, T_INLINE_HTML), true) ? "<?php" : "");
54 } elseif (preg_match('~\\s*(\\$pos = .*)~', $return, $match2)) {
64ba9242 » jakubvrana
2009-06-21 Comments
55 // single language lang() is used for plural
0116b9d1 » jakubvrana
2009-09-10 Don't set cookie in compile scripts
56 return "function lang(\$translation, \$number) {\n\t" . str_replace('$LANG', "'$_SESSION[lang]'", $match2[1]) . "\n\treturn sprintf(\$translation[\$pos], \$number);\n}\n";
6bb1401d » jakubvrana
2009-06-01 Remove ?><?php
57 } else {
58 echo "lang() not found\n";
667bfec4 » jakubvrana
2007-07-02 Decomposition
59 }
60 }
61
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
62 function put_file_lang($match) {
63 global $lang_ids, $project;
0116b9d1 » jakubvrana
2009-09-10 Don't set cookie in compile scripts
64 if ($_SESSION["lang"]) {
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
65 return "";
66 }
67 $return = "";
ea4fd610 » jakubvrana
2009-07-22 Join translations
68 foreach (glob(dirname(__FILE__) . "/adminer/lang/*.inc.php") as $filename) {
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
69 include $filename; // assign $translations
70 $translation_ids = array_flip($lang_ids); // default translation
71 foreach ($translations as $key => $val) {
72 if (isset($val)) {
73 $translation_ids[$lang_ids[$key]] = $val;
74 }
75 }
76 $return .= "\tcase \"" . basename($filename, '.inc.php') . '": $translations = array(';
77 foreach ($translation_ids as $val) {
78 $return .= (is_array($val) ? "array('" . implode("', '", array_map('add_apo_slashes', $val)) . "')" : "'" . add_apo_slashes($val) . "'") . ", ";
79 }
80 $return = substr($return, 0, -2) . "); break;\n";
81 }
82 return "switch (\$LANG) {\n$return}\n";
83 }
84
16f42102 » jakubvrana
2009-04-15 Function minification
85 function short_identifier($number, $chars) {
86 $return = '';
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
87 while ($number >= 0) {
88 $return .= $chars{$number % strlen($chars)};
89 $number = floor($number / strlen($chars)) - 1;
90 }
91 return $return;
92 }
93
004d7a96 » jakubvrana
2009-07-24 Link
94 // based on http://latrine.dgx.cz/jak-zredukovat-php-skripty
794192d9 » jakubvrana
2007-09-04 php_shrink()
95 function php_shrink($input) {
815eb26e » jakubvrana
2009-05-08 Use one token for all
96 $special_variables = array_flip(array('$this', '$GLOBALS', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_SERVER'));
12d2c6db » jakubvrana
2009-10-06 Build Adminer and Editor together
97 $short_variables = array();
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
98 $shortening = true;
16f42102 » jakubvrana
2009-04-15 Function minification
99 $tokens = token_get_all($input);
100
101 foreach ($tokens as $i => $token) {
102 if ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
103 $short_variables[$token[1]]++;
104 }
105 }
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
106
16f42102 » jakubvrana
2009-04-15 Function minification
107 arsort($short_variables);
108 foreach (array_keys($short_variables) as $number => $key) {
109 $short_variables[$key] = short_identifier($number, implode("", range('a', 'z')) . '_' . implode("", range('A', 'Z'))); // could use also numbers and \x7f-\xff
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;
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
116 $doc_comment = false; // include only first /**
ff85f125 » jakubvrana
2009-06-01 Join echos
117 for (reset($tokens); list($i, $token) = each($tokens); ) {
794192d9 » jakubvrana
2007-09-04 php_shrink()
118 if (!is_array($token)) {
119 $token = array(0, $token);
120 }
dbf95669 » jakubvrana
2009-08-26 Better consecutive echo handling
121 if ($tokens[$i+2][0] === T_CLOSE_TAG && $tokens[$i+3][0] === T_INLINE_HTML && $tokens[$i+4][0] === T_OPEN_TAG
fc3b652a » jakubvrana
2009-12-03 Whitespace
122 && strlen(addcslashes($tokens[$i+3][1], "'\\")) < strlen($tokens[$i+3][1]) + 3
dbf95669 » jakubvrana
2009-08-26 Better consecutive echo handling
123 ) {
124 $tokens[$i+2] = array(T_ECHO, 'echo');
125 $tokens[$i+3] = array(T_CONSTANT_ENCAPSED_STRING, "'" . addcslashes($tokens[$i+3][1], "'\\") . "'");
126 $tokens[$i+4] = array(0, ';');
127 }
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
128 if ($token[0] == T_COMMENT || $token[0] == T_WHITESPACE || ($token[0] == T_DOC_COMMENT && $doc_comment)) {
794192d9 » jakubvrana
2007-09-04 php_shrink()
129 $space = "\n";
130 } else {
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
131 if ($token[0] == T_DOC_COMMENT) {
132 $doc_comment = true;
133 }
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
134 if ($token[0] == T_VAR) {
135 $shortening = false;
136 } elseif (!$shortening) {
137 if ($token[1] == ';') {
138 $shortening = true;
139 }
ff85f125 » jakubvrana
2009-06-01 Join echos
140 } elseif ($token[0] == T_ECHO) {
141 $in_echo = true;
142 } elseif ($token[1] == ';' && $in_echo) {
e2bc1daa » jakubvrana
2009-08-26 No comments without whitespace
143 if ($tokens[$i+1][0] === T_WHITESPACE && $tokens[$i+2][0] === T_ECHO) {
ff85f125 » jakubvrana
2009-06-01 Join echos
144 next($tokens);
c030c163 » jakubvrana
2009-07-30 Compile inline HTML to echo
145 $i++;
146 }
147 if ($tokens[$i+1][0] === T_ECHO) {
148 // join two consecutive echos
ff85f125 » jakubvrana
2009-06-01 Join echos
149 next($tokens);
02046d69 » jakubvrana
2009-09-23 Windows support in e-mail attachments
150 $token[1] = ','; // '.' would conflict with "a".1+2 and would use more memory //! remove ',' and "," but not $var","
dbf95669 » jakubvrana
2009-08-26 Better consecutive echo handling
151 } else {
152 $in_echo = false;
ff85f125 » jakubvrana
2009-06-01 Join echos
153 }
16f42102 » jakubvrana
2009-04-15 Function minification
154 } elseif ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
155 $token[1] = '$' . $short_variables[$token[1]];
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
156 }
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
157 if (isset($set[substr($output, -1)]) || isset($set[$token[1][0]])) {
794192d9 » jakubvrana
2007-09-04 php_shrink()
158 $space = '';
159 }
160 $output .= $space . $token[1];
161 $space = '';
162 }
163 }
164 return $output;
165 }
166
9ee9cf08 » jakubvrana
2009-06-26 Remove code generation
167 function minify_css($file) {
168 return preg_replace('~\\s*([:;{},])\\s*~', '\\1', $file);
169 }
170
171 function compile_file($match) {
df03f4f0 » jakubvrana
2009-07-13 Join editing.js in compilation
172 global $project;
173 return call_user_func($match[2], file_get_contents(dirname(__FILE__) . "/$project/$match[1]"));
9ee9cf08 » jakubvrana
2009-06-26 Remove code generation
174 }
175
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
176 $DRIVER = "";
177 if (file_exists(dirname(__FILE__) . "/adminer/drivers/" . $_SERVER["argv"][1] . ".inc.php")) {
178 $DRIVER = $_SERVER["argv"][1];
179 array_shift($_SERVER["argv"]);
180 }
181
0116b9d1 » jakubvrana
2009-09-10 Don't set cookie in compile scripts
182 unset($_COOKIE["adminer_lang"]);
183 $_SESSION["lang"] = $_SERVER["argv"][1]; // Adminer functions read language from session
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
184 include dirname(__FILE__) . "/adminer/include/lang.inc.php";
0116b9d1 » jakubvrana
2009-09-10 Don't set cookie in compile scripts
185 if (isset($_SESSION["lang"])) {
186 if (isset($_SERVER["argv"][2]) || !isset($langs[$_SESSION["lang"]])) {
12d2c6db » jakubvrana
2009-10-06 Build Adminer and Editor together
187 echo "Usage: php compile.php [lang]\nPurpose: Compile adminer[-lang].php and editor[-lang].php.\n";
7418af0c » jakubvrana
2007-07-05 Czech translation
188 exit(1);
189 }
0116b9d1 » jakubvrana
2009-09-10 Don't set cookie in compile scripts
190 include dirname(__FILE__) . "/adminer/lang/$_SESSION[lang].inc.php";
7418af0c » jakubvrana
2007-07-05 Czech translation
191 }
bba76d8d » jakubvrana
2007-08-04 Flatten $translations
192
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
193 // check function definition in drivers
194 $filename = dirname(__FILE__) . "/adminer/drivers/mysql.inc.php";
195 preg_match_all('~\\bfunction ([^(]+)~', file_get_contents($filename), $matches); //! respect context (extension, class)
196 $functions = array_combine($matches[1], $matches[0]);
197 unset($functions["__destruct"], $functions["Min_DB"], $functions["Min_Result"]);
198 foreach (glob(dirname(__FILE__) . "/adminer/drivers/" . ($DRIVER ? $DRIVER : "*") . ".inc.php") as $filename) {
199 if ($filename != "mysql.inc.php") {
200 $file = file_get_contents($filename);
201 foreach ($functions as $val) {
202 if (!strpos($file, "$val(")) {
203 echo "Missing $val in $filename\n";
204 }
205 }
206 }
207 }
208
6420c584 » jakubvrana
2010-05-05 Schema support for PostgreSQL
209 $drivers = array();
12d2c6db » jakubvrana
2009-10-06 Build Adminer and Editor together
210 foreach (array("adminer", "editor") as $project) {
211 $lang_ids = array(); // global variable simplifies usage in a callback function
212 $file = file_get_contents(dirname(__FILE__) . "/$project/index.php");
6420c584 » jakubvrana
2010-05-05 Schema support for PostgreSQL
213 if ($DRIVER) {
214 $connection = (object) array("server_info" => 5.1); // MySQL support is version specific
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
215 $_GET[$DRIVER] = true; // to load the driver
216 include_once dirname(__FILE__) . "/adminer/drivers/$DRIVER.inc.php";
6420c584 » jakubvrana
2010-05-05 Schema support for PostgreSQL
217 foreach (array("view", "event", "privileges", "user", "processlist", "variables", "trigger", "scheme") as $feature) {
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
218 if (!support($feature)) {
219 $file = str_replace("} elseif (isset(\$_GET[\"$feature\"])) {\n\tinclude \"./$feature.inc.php\";\n", "", $file);
220 }
221 }
222 if (!support("routine")) {
223 $file = str_replace("} elseif (isset(\$_GET[\"procedure\"])) {\n\tinclude \"./procedure.inc.php\";\n", "", $file);
224 $file = str_replace("} elseif (isset(\$_GET[\"call\"])) {\n\tinclude \"./call.inc.php\";\n", "", $file);
225 $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);
226 }
227 }
12d2c6db » jakubvrana
2009-10-06 Build Adminer and Editor together
228 $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file);
229 $file = str_replace('include "../adminer/include/coverage.inc.php";', '', $file);
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
230 if ($DRIVER) {
231 $file = preg_replace('(include "../adminer/drivers/(?!' . preg_quote($DRIVER) . ').*\\s*)', '', $file);
232 }
12d2c6db » jakubvrana
2009-10-06 Build Adminer and Editor together
233 $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file); // bootstrap.inc.php
234 $file = preg_replace_callback("~lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])~s", 'lang_ids', $file);
235 $file = preg_replace_callback('~\\b(include|require) "([^"]*\\$LANG.inc.php)";~', 'put_file_lang', $file);
236 if ($_SESSION["lang"]) {
237 // single language version
238 $file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file);
239 $file = str_replace("<?php switch_lang(); ?>\n", "", $file);
240 $file = str_replace('<?php echo $LANG; ?>', $_SESSION["lang"], $file);
241 }
242 $file = str_replace('<script type="text/javascript" src="static/editing.js"></script>' . "\n", "", $file);
243 $file = preg_replace_callback("~compile_file\\('([^']+)', '([^']+)'\\);~", 'compile_file', $file); // integrate static files
244 $replace = 'h(preg_replace("~\\\\\\\\?.*~", "", $_SERVER["REQUEST_URI"])) . "?file=\\1&amp;version=' . $VERSION;
245 $file = preg_replace('~\\.\\./adminer/static/(default\\.css|functions\\.js|favicon\\.ico)~', '<?php echo ' . $replace . '"; ?>', $file);
246 $file = preg_replace('~\\.\\./adminer/static/([^\'"]*)~', '" . ' . $replace, $file);
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
247 $file = str_replace("../externals/jush/", "https://www.adminer.org/static/", $file);
12d2c6db » jakubvrana
2009-10-06 Build Adminer and Editor together
248 $file = preg_replace("~<\\?php\\s*\\?>\n?|\\?>\n?<\\?php~", '', $file);
249 $file = php_shrink($file);
c02bd441 » jakubvrana
2009-07-20 Use $filename
250
86bb5da4 » jakubvrana
2010-04-23 Use version without -dev
251 $filename = $project . (preg_match('~-dev$~', $VERSION) ? "" : "-$VERSION") . ($DRIVER ? "-$DRIVER" : "") . ($_SESSION["lang"] ? "-$_SESSION[lang]" : "") . ".php";
12d2c6db » jakubvrana
2009-10-06 Build Adminer and Editor together
252 fwrite(fopen($filename, "w"), $file); // file_put_contents() since PHP 5
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
253 echo "$filename created (" . strlen($file) . " B).\n";
12d2c6db » jakubvrana
2009-10-06 Build Adminer and Editor together
254 }
Something went wrong with that request. Please try again.