Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 201 lines (184 sloc) 8.168 kb
667bfec Decomposition
jakubvrana authored
1 <?php
e1bc7af Change directory structure
jakubvrana authored
2 include dirname(__FILE__) . "/adminer/include/version.inc.php";
b8b4548 Utilize JSMin
jakubvrana authored
3 include dirname(__FILE__) . "/externals/jsmin-php/jsmin.php";
7f3b4f2 Version checker
jakubvrana authored
4
8943678 sprintf() in lang()
jakubvrana authored
5 function add_apo_slashes($s) {
6 return addcslashes($s, "\\'");
7 }
8
0970a58 Prepare for single language version
jakubvrana authored
9 function remove_lang($match) {
bba76d8 Flatten $translations
jakubvrana authored
10 global $translations;
438ca05 Separate languages
jakubvrana authored
11 $idf = strtr($match[2], array("\\'" => "'", "\\\\" => "\\"));
bba76d8 Flatten $translations
jakubvrana authored
12 $s = ($translations[$idf] ? $translations[$idf] : $idf);
64ba924 Comments
jakubvrana authored
13 if ($match[3] == ",") { // lang() has parameters
8943678 sprintf() in lang()
jakubvrana authored
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]");
0970a58 Prepare for single language version
jakubvrana authored
17 }
18
64ba924 Comments
jakubvrana authored
19 $lang_ids = array(); // global variable simplifies usage in a callback function
c16c57b E-mail sending
jakubvrana authored
20
4738060 Use numeric language IDs to save bytes
jakubvrana authored
21 function lang_ids($match) {
22 global $lang_ids;
c16c57b E-mail sending
jakubvrana authored
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]");
4738060 Use numeric language IDs to save bytes
jakubvrana authored
28 }
29
667bfec Decomposition
jakubvrana authored
30 function put_file($match) {
c16c57b E-mail sending
jakubvrana authored
31 global $project;
b95f24e Editor: User friendly data editor
jakubvrana authored
32 if (basename($match[2]) == '$LANG.inc.php') {
c16c57b E-mail sending
jakubvrana authored
33 return $match[0]; // processed later
438ca05 Separate languages
jakubvrana authored
34 }
b95f24e Editor: User friendly data editor
jakubvrana authored
35 $return = file_get_contents(dirname(__FILE__) . "/$project/$match[2]");
c16c57b E-mail sending
jakubvrana authored
36 if (basename($match[2]) != "lang.inc.php" || !$_COOKIE["adminer_lang"]) {
64ba924 Comments
jakubvrana authored
37 $tokens = token_get_all($return); // to find out the last token
6bb1401 Remove ?><?php
jakubvrana authored
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)) {
64ba924 Comments
jakubvrana authored
40 // single language lang() is used for plural
b8ef9fb Use prefix for cookies
jakubvrana authored
41 return "function lang(\$translation, \$number) {\n\t" . str_replace('$LANG', "'$_COOKIE[adminer_lang]'", $match2[1]) . "\n\treturn sprintf(\$translation[\$pos], \$number);\n}\n";
6bb1401 Remove ?><?php
jakubvrana authored
42 } else {
43 echo "lang() not found\n";
667bfec Decomposition
jakubvrana authored
44 }
45 }
46
c16c57b E-mail sending
jakubvrana authored
47 function put_file_lang($match) {
48 global $lang_ids, $project;
49 if ($_COOKIE["adminer_lang"]) {
50 return "";
51 }
52 $return = "";
ea4fd61 Join translations
jakubvrana authored
53 foreach (glob(dirname(__FILE__) . "/adminer/lang/*.inc.php") as $filename) {
c16c57b E-mail sending
jakubvrana authored
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
16f4210 Function minification
jakubvrana authored
70 function short_identifier($number, $chars) {
71 $return = '';
a06f4c1 Size reduction by minification of variables
jakubvrana authored
72 while ($number >= 0) {
73 $return .= $chars{$number % strlen($chars)};
74 $number = floor($number / strlen($chars)) - 1;
75 }
76 return $return;
77 }
78
004d7a9 Link
jakubvrana authored
79 // based on http://latrine.dgx.cz/jak-zredukovat-php-skripty
794192d php_shrink()
jakubvrana authored
80 function php_shrink($input) {
815eb26 Use one token for all
jakubvrana authored
81 $special_variables = array_flip(array('$this', '$GLOBALS', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_SERVER'));
a06f4c1 Size reduction by minification of variables
jakubvrana authored
82 static $short_variables = array();
83 $shortening = true;
16f4210 Function minification
jakubvrana authored
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]]++;
89 }
90 }
c27a877 Minify defined functions only
jakubvrana authored
91
16f4210 Function minification
jakubvrana authored
92 arsort($short_variables);
93 foreach (array_keys($short_variables) as $number => $key) {
94 $short_variables[$key] = short_identifier($number, implode("", range('a', 'z')) . '_' . implode("", range('A', 'Z'))); // could use also numbers and \x7f-\xff
95 }
a06f4c1 Size reduction by minification of variables
jakubvrana authored
96
794192d php_shrink()
jakubvrana authored
97 $set = array_flip(preg_split('//', '!"#$&\'()*+,-./:;<=>?@[\]^`{|}'));
98 $space = '';
99 $output = '';
ff85f12 Join echos
jakubvrana authored
100 $in_echo = false;
c16c57b E-mail sending
jakubvrana authored
101 $doc_comment = false; // include only first /**
ff85f12 Join echos
jakubvrana authored
102 for (reset($tokens); list($i, $token) = each($tokens); ) {
794192d php_shrink()
jakubvrana authored
103 if (!is_array($token)) {
104 $token = array(0, $token);
105 }
dbf9566 Better consecutive echo handling
jakubvrana authored
106 if ($tokens[$i+2][0] === T_CLOSE_TAG && $tokens[$i+3][0] === T_INLINE_HTML && $tokens[$i+4][0] === T_OPEN_TAG
107 && strlen(addcslashes($tokens[$i+3][1], "'\\")) < strlen($tokens[$i+3][1]) + 3
108 ) {
109 $tokens[$i+2] = array(T_ECHO, 'echo');
110 $tokens[$i+3] = array(T_CONSTANT_ENCAPSED_STRING, "'" . addcslashes($tokens[$i+3][1], "'\\") . "'");
111 $tokens[$i+4] = array(0, ';');
112 }
c16c57b E-mail sending
jakubvrana authored
113 if ($token[0] == T_COMMENT || $token[0] == T_WHITESPACE || ($token[0] == T_DOC_COMMENT && $doc_comment)) {
794192d php_shrink()
jakubvrana authored
114 $space = "\n";
115 } else {
c16c57b E-mail sending
jakubvrana authored
116 if ($token[0] == T_DOC_COMMENT) {
117 $doc_comment = true;
118 }
a06f4c1 Size reduction by minification of variables
jakubvrana authored
119 if ($token[0] == T_VAR) {
120 $shortening = false;
121 } elseif (!$shortening) {
122 if ($token[1] == ';') {
123 $shortening = true;
124 }
ff85f12 Join echos
jakubvrana authored
125 } elseif ($token[0] == T_ECHO) {
126 $in_echo = true;
127 } elseif ($token[1] == ';' && $in_echo) {
dbf9566 Better consecutive echo handling
jakubvrana authored
128 if (in_array($tokens[$i+1][0], array(T_WHITESPACE, T_COMMENT)) && $tokens[$i+2][0] === T_ECHO) {
ff85f12 Join echos
jakubvrana authored
129 next($tokens);
c030c16 Compile inline HTML to echo
jakubvrana authored
130 $i++;
131 }
132 if ($tokens[$i+1][0] === T_ECHO) {
133 // join two consecutive echos
ff85f12 Join echos
jakubvrana authored
134 next($tokens);
135 $token[1] = '.'; //! join ''.'' and "".""
dbf9566 Better consecutive echo handling
jakubvrana authored
136 } else {
137 $in_echo = false;
ff85f12 Join echos
jakubvrana authored
138 }
16f4210 Function minification
jakubvrana authored
139 } elseif ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
140 $token[1] = '$' . $short_variables[$token[1]];
a06f4c1 Size reduction by minification of variables
jakubvrana authored
141 }
794192d php_shrink()
jakubvrana authored
142 if (isset($set[substr($output, -1)]) || isset($set[$token[1]{0}])) {
143 $space = '';
144 }
145 $output .= $space . $token[1];
146 $space = '';
147 }
148 }
149 return $output;
150 }
151
9ee9cf0 Remove code generation
jakubvrana authored
152 function minify_css($file) {
153 return preg_replace('~\\s*([:;{},])\\s*~', '\\1', $file);
154 }
155
156 function compile_file($match) {
df03f4f Join editing.js in compilation
jakubvrana authored
157 global $project;
158 return call_user_func($match[2], file_get_contents(dirname(__FILE__) . "/$project/$match[1]"));
9ee9cf0 Remove code generation
jakubvrana authored
159 }
160
31b0572 Don't report E_DEPRECATED
jakubvrana authored
161 error_reporting(4343); // errors and warnings
b95f24e Editor: User friendly data editor
jakubvrana authored
162 $project = "adminer";
943a3e8 Find adminer/ in compile.php directory
jakubvrana authored
163 if (file_exists(dirname(__FILE__) . "/" . $_SERVER["argv"][1] . "/index.php")) {
0f850a6 Allow compilation of all languages Editor
jakubvrana authored
164 $project = $_SERVER["argv"][1];
165 array_shift($_SERVER["argv"]);
166 }
e933baa Example sets admin_lang cookie
jakubvrana authored
167 $_COOKIE["adminer_lang"] = $_SERVER["argv"][1]; // Adminer functions read language from cookie
0f850a6 Allow compilation of all languages Editor
jakubvrana authored
168 if (isset($_SERVER["argv"][1])) {
e1bc7af Change directory structure
jakubvrana authored
169 include dirname(__FILE__) . "/adminer/include/lang.inc.php";
b95f24e Editor: User friendly data editor
jakubvrana authored
170 if (isset($_SERVER["argv"][2]) || !isset($langs[$_COOKIE["adminer_lang"]])) {
0f850a6 Allow compilation of all languages Editor
jakubvrana authored
171 echo "Usage: php compile.php [adminer] [lang]\nPurpose: Compile adminer[-lang].php from adminer/index.php.\n";
7418af0 Czech translation
jakubvrana authored
172 exit(1);
173 }
ea4fd61 Join translations
jakubvrana authored
174 include dirname(__FILE__) . "/adminer/lang/$_COOKIE[adminer_lang].inc.php";
7418af0 Czech translation
jakubvrana authored
175 }
bba76d8 Flatten $translations
jakubvrana authored
176
b95f24e Editor: User friendly data editor
jakubvrana authored
177 $file = file_get_contents(dirname(__FILE__) . "/$project/index.php");
6bb1401 Remove ?><?php
jakubvrana authored
178 $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file);
70c8343 Save coverage to database
jakubvrana authored
179 $file = str_replace('include "../adminer/include/coverage.inc.php";', '', $file);
c16c57b E-mail sending
jakubvrana authored
180 $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file); // bootstrap.inc.php
181 $file = preg_replace_callback("~lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])~s", 'lang_ids', $file);
182 $file = preg_replace_callback('~\\b(include|require) "([^"]*\\$LANG.inc.php)";~', 'put_file_lang', $file);
b8ef9fb Use prefix for cookies
jakubvrana authored
183 if ($_COOKIE["adminer_lang"]) {
64ba924 Comments
jakubvrana authored
184 // single language version
a9907f1 Greedy expressions
jakubvrana authored
185 $file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file);
7418af0 Czech translation
jakubvrana authored
186 $file = str_replace("<?php switch_lang(); ?>\n", "", $file);
b8ef9fb Use prefix for cookies
jakubvrana authored
187 $file = str_replace('<?php echo $LANG; ?>', $_COOKIE["adminer_lang"], $file);
0970a58 Prepare for single language version
jakubvrana authored
188 }
df03f4f Join editing.js in compilation
jakubvrana authored
189 $file = str_replace('<script type="text/javascript" src="editing.js"></script>' . "\n", "", $file);
190 $file = preg_replace_callback("~compile_file\\('([^']+)', '([^']+)'\\);~", 'compile_file', $file); // integrate static files
689699a Shortcut for htmlspecialchars
jakubvrana authored
191 $replace = 'h(preg_replace("~\\\\\\\\?.*~", "", $_SERVER["REQUEST_URI"])) . "?file=\\1&amp;version=' . $VERSION;
e1abcda Treat tinyint(1) as boolean
jakubvrana authored
192 $file = preg_replace('~\\.\\./adminer/(default\\.css|functions\\.js|favicon\\.ico)~', '<?php echo ' . $replace . '"; ?>', $file);
b95f24e Editor: User friendly data editor
jakubvrana authored
193 $file = preg_replace('~\\.\\./adminer/((plus|cross|up|down|arrow)\\.gif)~', '" . ' . $replace, $file);
b3fc4c0 Download externals from HTTPS
jakubvrana authored
194 $file = str_replace("../externals/jush/", "https://jush.svn.sourceforge.net/svnroot/jush/trunk/", $file); // mixed-content warning if Adminer runs on HTTPS and external files on HTTP
35bca85 Fix XSS (thanks to Synopsi)
jakubvrana authored
195 $file = preg_replace("~<\\?php\\s*\\?>\n?|\\?>\n?<\\?php~", '', $file);
794192d php_shrink()
jakubvrana authored
196 $file = php_shrink($file);
c02bd44 Use $filename
jakubvrana authored
197
198 $filename = $project . ($_COOKIE["adminer_lang"] ? "-$_COOKIE[adminer_lang]" : "") . ".php";
199 fwrite(fopen($filename, "w"), $file); // file_put_contents() since PHP 5
7418af0 Czech translation
jakubvrana authored
200 echo "$filename created.\n";
Something went wrong with that request. Please try again.