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