Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 214 lines (199 sloc) 9.114 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
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
20
4738060a » jakubvrana
2008-05-23 Use numeric language IDs to save bytes
21 function lang_ids($match) {
22 global $lang_ids;
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
23 $lang_id = &$lang_ids[stripslashes($match[1])];
24 if (!isset($lang_id)) {
25 $lang_id = count($lang_ids) - 1;
26 }
27 return ($_COOKIE["adminer_lang"] ? $match[0] : "lang($lang_id$match[2]");
4738060a » jakubvrana
2008-05-23 Use numeric language IDs to save bytes
28 }
29
667bfec4 » jakubvrana
2007-07-02 Decomposition
30 function put_file($match) {
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
31 global $project;
b95f24ea » jakubvrana
2009-07-03 Editor: User friendly data editor
32 if (basename($match[2]) == '$LANG.inc.php') {
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
33 return $match[0]; // processed later
438ca058 » jakubvrana
2007-07-26 Separate languages
34 }
b95f24ea » jakubvrana
2009-07-03 Editor: User friendly data editor
35 $return = file_get_contents(dirname(__FILE__) . "/$project/$match[2]");
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
36 if (basename($match[2]) != "lang.inc.php" || !$_COOKIE["adminer_lang"]) {
64ba9242 » jakubvrana
2009-06-21 Comments
37 $tokens = token_get_all($return); // to find out the last token
6bb1401d » jakubvrana
2009-06-01 Remove ?><?php
38 return "?>\n$return" . (in_array($tokens[count($tokens) - 1][0], array(T_CLOSE_TAG, T_INLINE_HTML), true) ? "<?php" : "");
39 } elseif (preg_match('~\\s*(\\$pos = .*)~', $return, $match2)) {
64ba9242 » jakubvrana
2009-06-21 Comments
40 // single language lang() is used for plural
b8ef9fb9 » jakubvrana
2009-06-21 Use prefix for cookies
41 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
42 } else {
43 echo "lang() not found\n";
667bfec4 » jakubvrana
2007-07-02 Decomposition
44 }
45 }
46
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
47 function put_file_lang($match) {
48 global $lang_ids, $project;
49 if ($_COOKIE["adminer_lang"]) {
50 return "";
51 }
52 $return = "";
53 foreach (glob(dirname(__FILE__) . "/$project/lang/*.inc.php") as $filename) {
54 include $filename; // assign $translations
55 $translation_ids = array_flip($lang_ids); // default translation
56 foreach ($translations as $key => $val) {
57 if (isset($val)) {
58 $translation_ids[$lang_ids[$key]] = $val;
59 }
60 }
61 $return .= "\tcase \"" . basename($filename, '.inc.php') . '": $translations = array(';
62 foreach ($translation_ids as $val) {
63 $return .= (is_array($val) ? "array('" . implode("', '", array_map('add_apo_slashes', $val)) . "')" : "'" . add_apo_slashes($val) . "'") . ", ";
64 }
65 $return = substr($return, 0, -2) . "); break;\n";
66 }
67 return "switch (\$LANG) {\n$return}\n";
68 }
69
16f42102 » jakubvrana
2009-04-15 Function minification
70 function short_identifier($number, $chars) {
71 $return = '';
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
72 while ($number >= 0) {
73 $return .= $chars{$number % strlen($chars)};
74 $number = floor($number / strlen($chars)) - 1;
75 }
76 return $return;
77 }
78
5ef93226 » jakubvrana
2009-05-08 Separate PDO
79 // based on Dgx's PHP shrinker
794192d9 » jakubvrana
2007-09-04 php_shrink()
80 function php_shrink($input) {
815eb26e » jakubvrana
2009-05-08 Use one token for all
81 $special_variables = array_flip(array('$this', '$GLOBALS', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_SERVER'));
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
82 static $short_variables = array();
64ba9242 » jakubvrana
2009-06-21 Comments
83 static $short_functions = array();
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
84 $shortening = true;
fbebb087 » jakubvrana
2009-06-03 Use separate connection for exploring indexes
85 $special_functions = array_flip(array('Min_DB', 'Min_Result', '__construct'));
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
86 $defined_functions = array();
16f42102 » jakubvrana
2009-04-15 Function minification
87 $tokens = token_get_all($input);
88
89 foreach ($tokens as $i => $token) {
90 if ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
91 $short_variables[$token[1]]++;
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
92 } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && !isset($special_functions[$token[1]])) {
16f42102 » jakubvrana
2009-04-15 Function minification
93 $short_functions[$token[1]]++;
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
94 if ($tokens[$i-2][0] === T_FUNCTION) {
95 $defined_functions[$token[1]] = true;
96 }
16f42102 » jakubvrana
2009-04-15 Function minification
97 }
98 }
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
99
16f42102 » jakubvrana
2009-04-15 Function minification
100 arsort($short_variables);
101 foreach (array_keys($short_variables) as $number => $key) {
102 $short_variables[$key] = short_identifier($number, implode("", range('a', 'z')) . '_' . implode("", range('A', 'Z'))); // could use also numbers and \x7f-\xff
103 }
104 arsort($short_functions);
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
105 $number = 0;
106 foreach ($short_functions as $key => $val) {
107 if (isset($defined_functions[$key])) {
3aaeff0d » jakubvrana
2009-04-17 Prevent name conflicts in function minification
108 do {
64ba9242 » jakubvrana
2009-06-21 Comments
109 $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
110 $number++;
64ba9242 » jakubvrana
2009-06-21 Comments
111 } while (isset($short_functions[$short_functions[$key]])); // don't overwrite existing functions
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
112 }
16f42102 » jakubvrana
2009-04-15 Function minification
113 }
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
114
794192d9 » jakubvrana
2007-09-04 php_shrink()
115 $set = array_flip(preg_split('//', '!"#$&\'()*+,-./:;<=>?@[\]^`{|}'));
116 $space = '';
117 $output = '';
ff85f125 » jakubvrana
2009-06-01 Join echos
118 $in_echo = false;
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
119 $doc_comment = false; // include only first /**
ff85f125 » jakubvrana
2009-06-01 Join echos
120 for (reset($tokens); list($i, $token) = each($tokens); ) {
794192d9 » jakubvrana
2007-09-04 php_shrink()
121 if (!is_array($token)) {
122 $token = array(0, $token);
123 }
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
124 if ($token[0] == T_COMMENT || $token[0] == T_WHITESPACE || ($token[0] == T_DOC_COMMENT && $doc_comment)) {
794192d9 » jakubvrana
2007-09-04 php_shrink()
125 $space = "\n";
126 } else {
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
127 if ($token[0] == T_DOC_COMMENT) {
128 $doc_comment = true;
129 }
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
130 if ($token[0] == T_VAR) {
131 $shortening = false;
132 } elseif (!$shortening) {
133 if ($token[1] == ';') {
134 $shortening = true;
135 }
ff85f125 » jakubvrana
2009-06-01 Join echos
136 } elseif ($token[0] == T_ECHO) {
137 $in_echo = true;
138 } elseif ($token[1] == ';' && $in_echo) {
139 $in_echo = false;
140 if ($tokens[$i+1][0] === T_WHITESPACE && $tokens[$i+2][0] === T_ECHO) {
64ba9242 » jakubvrana
2009-06-21 Comments
141 // join two consecutive echos
ff85f125 » jakubvrana
2009-06-01 Join echos
142 next($tokens);
143 next($tokens);
144 $token[1] = '.'; //! join ''.'' and "".""
145 }
16f42102 » jakubvrana
2009-04-15 Function minification
146 } elseif ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
147 $token[1] = '$' . $short_variables[$token[1]];
c27a8774 » jakubvrana
2009-04-16 Minify defined functions only
148 } elseif ($token[0] === T_STRING && $tokens[$i+1] === '(' && isset($defined_functions[$token[1]])
64ba9242 » jakubvrana
2009-06-21 Comments
149 && $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
150 ) {
151 $token[1] = $short_functions[$token[1]];
64ba9242 » jakubvrana
2009-06-21 Comments
152 } elseif ($token[0] == T_CONSTANT_ENCAPSED_STRING
153 && (($tokens[$i-1] === '(' && in_array($tokens[$i-2][1], array('array_map', 'set_exception_handler'), true)) || $token[1] == "'normalize_enum'")
154 && isset($defined_functions[substr($token[1], 1, -1)])
155 ) { // minify callback functions too
16f42102 » jakubvrana
2009-04-15 Function minification
156 $token[1] = "'" . $short_functions[substr($token[1], 1, -1)] . "'";
a06f4c15 » jakubvrana
2009-04-15 Size reduction by minification of variables
157 }
794192d9 » jakubvrana
2007-09-04 php_shrink()
158 if (isset($set[substr($output, -1)]) || isset($set[$token[1]{0}])) {
159 $space = '';
160 }
161 $output .= $space . $token[1];
162 $space = '';
163 }
164 }
165 return $output;
166 }
167
9ee9cf08 » jakubvrana
2009-06-26 Remove code generation
168 function minify_css($file) {
169 return preg_replace('~\\s*([:;{},])\\s*~', '\\1', $file);
170 }
171
172 function compile_file($match) {
173 return call_user_func($match[2], file_get_contents(dirname(__FILE__) . "/adminer/$match[1]"));
174 }
175
31b0572b » jakubvrana
2009-07-03 Don't report E_DEPRECATED
176 error_reporting(4343); // errors and warnings
b95f24ea » jakubvrana
2009-07-03 Editor: User friendly data editor
177 $project = "adminer";
0f850a65 » jakubvrana
2009-07-03 Allow compilation of all languages Editor
178 if (file_exists($_SERVER["argv"][1] . "/index.php")) {
179 $project = $_SERVER["argv"][1];
180 array_shift($_SERVER["argv"]);
181 }
182 if (isset($_SERVER["argv"][1])) {
b8ef9fb9 » jakubvrana
2009-06-21 Use prefix for cookies
183 $_COOKIE["adminer_lang"] = $_SERVER["argv"][1]; // Adminer functions read language from cookie
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
184 include dirname(__FILE__) . "/adminer/include/lang.inc.php";
b95f24ea » jakubvrana
2009-07-03 Editor: User friendly data editor
185 if (isset($_SERVER["argv"][2]) || !isset($langs[$_COOKIE["adminer_lang"]])) {
0f850a65 » jakubvrana
2009-07-03 Allow compilation of all languages Editor
186 echo "Usage: php compile.php [adminer] [lang]\nPurpose: Compile adminer[-lang].php from adminer/index.php.\n";
7418af0c » jakubvrana
2007-07-05 Czech translation
187 exit(1);
188 }
16cdb674 » jakubvrana
2009-07-03 Separate Editor translations
189 include dirname(__FILE__) . "/$project/lang/$_COOKIE[adminer_lang].inc.php";
7418af0c » jakubvrana
2007-07-05 Czech translation
190 }
bba76d8d » jakubvrana
2007-08-04 Flatten $translations
191
b95f24ea » jakubvrana
2009-07-03 Editor: User friendly data editor
192 $filename = $project . ($_COOKIE["adminer_lang"] ? "-$_COOKIE[adminer_lang]" : "") . ".php";
193 $file = file_get_contents(dirname(__FILE__) . "/$project/index.php");
91156d1a » jakubvrana
2009-06-24 Coverage uses the same cookie so must live in the same directory
194 $file = preg_replace('(' . str_replace(' ', '\\s*', preg_quote(' if (isset($_GET["coverage"])) { include "./coverage.inc.php"; }')) . ')', '', $file);
6bb1401d » jakubvrana
2009-06-01 Remove ?><?php
195 $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file);
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
196 $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file); // bootstrap.inc.php
bb1ae0ba » jakubvrana
2008-10-02 Code coverage
197 $file = preg_replace("~if \\(isset\\(\\\$_SESSION\\[\"coverage.*\n}\n| && !isset\\(\\\$_SESSION\\[\"coverage\"\\]\\)~sU", '', $file);
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
198 $file = preg_replace_callback("~lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])~s", 'lang_ids', $file);
199 $file = preg_replace_callback('~\\b(include|require) "([^"]*\\$LANG.inc.php)";~', 'put_file_lang', $file);
b8ef9fb9 » jakubvrana
2009-06-21 Use prefix for cookies
200 if ($_COOKIE["adminer_lang"]) {
64ba9242 » jakubvrana
2009-06-21 Comments
201 // single language version
a9907f18 » jakubvrana
2007-07-10 Greedy expressions
202 $file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file);
7418af0c » jakubvrana
2007-07-05 Czech translation
203 $file = str_replace("<?php switch_lang(); ?>\n", "", $file);
b8ef9fb9 » jakubvrana
2009-06-21 Use prefix for cookies
204 $file = str_replace('<?php echo $LANG; ?>', $_COOKIE["adminer_lang"], $file);
0970a583 » jakubvrana
2007-07-05 Prepare for single language version
205 }
9ee9cf08 » jakubvrana
2009-06-26 Remove code generation
206 $file = preg_replace_callback("~compile_file\\('([^']+)', '([^']+)'\\)~", 'compile_file', $file); // integrate static files
b95f24ea » jakubvrana
2009-07-03 Editor: User friendly data editor
207 $replace = 'htmlspecialchars(preg_replace("~\\\\\\\\?.*~", "", $_SERVER["REQUEST_URI"])) . "?file=\\1&amp;version=' . $VERSION;
45f5cdd3 » jakubvrana
2009-07-03 Separate editing JavaScript
208 $file = preg_replace('~(?:\\.\\./adminer/|\\./)(default\\.css|functions\\.js|editing\\.js|favicon\\.ico)~', '<?php echo ' . $replace . '"; ?>', $file);
b95f24ea » jakubvrana
2009-07-03 Editor: User friendly data editor
209 $file = preg_replace('~\\.\\./adminer/((plus|cross|up|down|arrow)\\.gif)~', '" . ' . $replace, $file);
e1bc7af8 » jakubvrana
2009-06-13 Change directory structure
210 $file = str_replace("../externals/jush/", "http://jush.sourceforge.net/", $file);
35bca85a » jakubvrana
2009-06-06 Fix XSS (thanks to Synopsi)
211 $file = preg_replace("~<\\?php\\s*\\?>\n?|\\?>\n?<\\?php~", '', $file);
794192d9 » jakubvrana
2007-09-04 php_shrink()
212 $file = php_shrink($file);
64ba9242 » jakubvrana
2009-06-21 Comments
213 fwrite(fopen($filename, "w"), $file); // file_put_contents() since PHP 5
7418af0c » jakubvrana
2007-07-05 Czech translation
214 echo "$filename created.\n";
Something went wrong with that request. Please try again.