PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/compile.php

https://github.com/whiteneon/adminer
PHP | 410 lines | 407 code | 2 blank | 1 comment | 0 complexity | a1c4201770ae79e44f85dfda4f659a9f MD5 | raw file
  1. #!/usr/bin/env php
  2. <?php
  3. error_reporting(6135); // errors and warnings
  4. include dirname(__FILE__) . "/adminer/include/version.inc.php";
  5. include dirname(__FILE__) . "/externals/JsShrink/jsShrink.php";
  6. function add_apo_slashes($s) {
  7. return addcslashes($s, "\\'");
  8. }
  9. function add_quo_slashes($s) {
  10. $return = $s;
  11. $return = addcslashes($return, "\n\r\$\"\\");
  12. $return = preg_replace('~\0(?![0-7])~', '\\\\0', $return);
  13. $return = addcslashes($return, "\0");
  14. return $return;
  15. }
  16. function remove_lang($match) {
  17. global $translations;
  18. $idf = strtr($match[2], array("\\'" => "'", "\\\\" => "\\"));
  19. $s = ($translations[$idf] ? $translations[$idf] : $idf);
  20. if ($match[3] == ",") { // lang() has parameters
  21. return "$match[1]" . (is_array($s) ? "lang(array('" . implode("', '", array_map('add_apo_slashes', $s)) . "')," : "sprintf('" . add_apo_slashes($s) . "',");
  22. }
  23. return ($match[1] && $match[4] ? $s : "$match[1]'" . add_apo_slashes($s) . "'$match[4]");
  24. }
  25. function lang_ids($match) {
  26. global $lang_ids;
  27. $lang_id = &$lang_ids[stripslashes($match[1])];
  28. if ($lang_id === null) {
  29. $lang_id = count($lang_ids) - 1;
  30. }
  31. return ($_SESSION["lang"] ? $match[0] : "lang($lang_id$match[2]");
  32. }
  33. function put_file($match) {
  34. global $project;
  35. if (basename($match[2]) == '$LANG.inc.php') {
  36. return $match[0]; // processed later
  37. }
  38. $return = file_get_contents(dirname(__FILE__) . "/$project/$match[2]");
  39. if (basename($match[2]) != "lang.inc.php" || !$_SESSION["lang"]) {
  40. if (basename($match[2]) == "lang.inc.php") {
  41. $return = str_replace('function lang($idf, $number = null) {', 'function lang($idf, $number = null) {
  42. if (is_string($idf)) { // compiled version uses numbers, string comes from a plugin
  43. // English translation is closest to the original identifiers //! pluralized translations are not found
  44. $pos = array_search($idf, get_translations("en")); //! this should be cached
  45. if ($pos !== false) {
  46. $idf = $pos;
  47. }
  48. }', $return, $count);
  49. if (!$count) {
  50. echo "lang() not found\n";
  51. }
  52. }
  53. $tokens = token_get_all($return); // to find out the last token
  54. return "?>\n$return" . (in_array($tokens[count($tokens) - 1][0], array(T_CLOSE_TAG, T_INLINE_HTML), true) ? "<?php" : "");
  55. } elseif (preg_match('~\\s*(\\$pos = (.+\n).+;)~sU', $return, $match2)) {
  56. // single language lang() is used for plural
  57. return "function get_lang() {
  58. return '$_SESSION[lang]';
  59. }
  60. function lang(\$translation, \$number = null) {
  61. if (is_array(\$translation)) {
  62. \$pos = $match2[2]\t\t\t: " . (preg_match("~\\\$LANG == '$_SESSION[lang]'.* \\? (.+)\n~U", $match2[1], $match3) ? $match3[1] : "1") . '
  63. );
  64. $translation = $translation[$pos];
  65. }
  66. $translation = str_replace("%d", "%s", $translation);
  67. $number = format_number($number);
  68. return sprintf($translation, $number);
  69. }
  70. ';
  71. } else {
  72. echo "lang() \$pos not found\n";
  73. }
  74. }
  75. function lzw_compress($string) {
  76. // compression
  77. $dictionary = array_flip(range("\0", "\xFF"));
  78. $word = "";
  79. $codes = array();
  80. for ($i=0; $i <= strlen($string); $i++) {
  81. $x = $string[$i];
  82. if (strlen($x) && isset($dictionary[$word . $x])) {
  83. $word .= $x;
  84. } elseif ($i) {
  85. $codes[] = $dictionary[$word];
  86. $dictionary[$word . $x] = count($dictionary);
  87. $word = $x;
  88. }
  89. }
  90. // convert codes to binary string
  91. $dictionary_count = 256;
  92. $bits = 8; // ceil(log($dictionary_count, 2))
  93. $return = "";
  94. $rest = 0;
  95. $rest_length = 0;
  96. foreach ($codes as $code) {
  97. $rest = ($rest << $bits) + $code;
  98. $rest_length += $bits;
  99. $dictionary_count++;
  100. if ($dictionary_count >> $bits) {
  101. $bits++;
  102. }
  103. while ($rest_length > 7) {
  104. $rest_length -= 8;
  105. $return .= chr($rest >> $rest_length);
  106. $rest &= (1 << $rest_length) - 1;
  107. }
  108. }
  109. return $return . ($rest_length ? chr($rest << (8 - $rest_length)) : "");
  110. }
  111. function put_file_lang($match) {
  112. global $lang_ids, $project, $langs;
  113. if ($_SESSION["lang"]) {
  114. return "";
  115. }
  116. $return = "";
  117. foreach ($langs as $lang => $val) {
  118. include dirname(__FILE__) . "/adminer/lang/$lang.inc.php"; // assign $translations
  119. $translation_ids = array_flip($lang_ids); // default translation
  120. foreach ($translations as $key => $val) {
  121. if ($val !== null) {
  122. $translation_ids[$lang_ids[$key]] = implode("\t", (array) $val);
  123. }
  124. }
  125. $return .= '
  126. case "' . $lang . '": $compressed = "' . add_quo_slashes(lzw_compress(implode("\n", $translation_ids))) . '"; break;';
  127. }
  128. $translations_version = crc32($return);
  129. return '$translations = &$_SESSION["translations"];
  130. if ($_SESSION["translations_version"] != ' . $translations_version . ') {
  131. $translations = array();
  132. $_SESSION["translations_version"] = ' . $translations_version . ';
  133. }
  134. function get_translations($lang) {
  135. switch ($lang) {' . $return . '
  136. }
  137. $translations = array();
  138. foreach (explode("\n", lzw_decompress($compressed)) as $val) {
  139. $translations[] = (strpos($val, "\t") ? explode("\t", $val) : $val);
  140. }
  141. return $translations;
  142. }
  143. if (!$translations) {
  144. $translations = get_translations($LANG);
  145. }
  146. ';
  147. }
  148. function short_identifier($number, $chars) {
  149. $return = '';
  150. while ($number >= 0) {
  151. $return .= $chars[$number % strlen($chars)];
  152. $number = floor($number / strlen($chars)) - 1;
  153. }
  154. return $return;
  155. }
  156. // based on http://latrine.dgx.cz/jak-zredukovat-php-skripty
  157. function php_shrink($input) {
  158. global $VERSION;
  159. $special_variables = array_flip(array('$this', '$GLOBALS', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_SERVER', '$http_response_header', '$php_errormsg'));
  160. $short_variables = array();
  161. $shortening = true;
  162. $tokens = token_get_all($input);
  163. // remove unnecessary { }
  164. //! change also `while () { if () {;} }` to `while () if () ;` but be careful about `if () { if () { } } else { }
  165. $shorten = 0;
  166. $opening = -1;
  167. foreach ($tokens as $i => $token) {
  168. if (in_array($token[0], array(T_IF, T_ELSE, T_ELSEIF, T_WHILE, T_DO, T_FOR, T_FOREACH), true)) {
  169. $shorten = ($token[0] == T_FOR ? 4 : 2);
  170. $opening = -1;
  171. } elseif (in_array($token[0], array(T_SWITCH, T_FUNCTION, T_CLASS, T_CLOSE_TAG), true)) {
  172. $shorten = 0;
  173. } elseif ($token === ';') {
  174. $shorten--;
  175. } elseif ($token === '{') {
  176. if ($opening < 0) {
  177. $opening = $i;
  178. } elseif ($shorten > 1) {
  179. $shorten = 0;
  180. }
  181. } elseif ($token === '}' && $opening >= 0 && $shorten == 1) {
  182. unset($tokens[$opening]);
  183. unset($tokens[$i]);
  184. $shorten = 0;
  185. $opening = -1;
  186. }
  187. }
  188. $tokens = array_values($tokens);
  189. foreach ($tokens as $i => $token) {
  190. if ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
  191. $short_variables[$token[1]]++;
  192. }
  193. }
  194. arsort($short_variables);
  195. $chars = implode(range('a', 'z')) . '_' . implode(range('A', 'Z'));
  196. // preserve variable names between versions if possible
  197. $short_variables2 = array_splice($short_variables, strlen($chars));
  198. ksort($short_variables);
  199. ksort($short_variables2);
  200. $short_variables += $short_variables2;
  201. foreach (array_keys($short_variables) as $number => $key) {
  202. $short_variables[$key] = short_identifier($number, $chars); // could use also numbers and \x7f-\xff
  203. }
  204. $set = array_flip(preg_split('//', '!"#$%&\'()*+,-./:;<=>?@[\]^`{|}'));
  205. $space = '';
  206. $output = '';
  207. $in_echo = false;
  208. $doc_comment = false; // include only first /**
  209. for (reset($tokens); list($i, $token) = each($tokens); ) {
  210. if (!is_array($token)) {
  211. $token = array(0, $token);
  212. }
  213. if ($tokens[$i+2][0] === T_CLOSE_TAG && $tokens[$i+3][0] === T_INLINE_HTML && $tokens[$i+4][0] === T_OPEN_TAG
  214. && strlen(add_apo_slashes($tokens[$i+3][1])) < strlen($tokens[$i+3][1]) + 3
  215. ) {
  216. $tokens[$i+2] = array(T_ECHO, 'echo');
  217. $tokens[$i+3] = array(T_CONSTANT_ENCAPSED_STRING, "'" . add_apo_slashes($tokens[$i+3][1]) . "'");
  218. $tokens[$i+4] = array(0, ';');
  219. }
  220. if ($token[0] == T_COMMENT || $token[0] == T_WHITESPACE || ($token[0] == T_DOC_COMMENT && $doc_comment)) {
  221. $space = "\n";
  222. } else {
  223. if ($token[0] == T_DOC_COMMENT) {
  224. $doc_comment = true;
  225. $token[1] = substr_replace($token[1], "* @version $VERSION\n", -2, 0);
  226. }
  227. if ($token[0] == T_VAR) {
  228. $shortening = false;
  229. } elseif (!$shortening) {
  230. if ($token[1] == ';') {
  231. $shortening = true;
  232. }
  233. } elseif ($token[0] == T_ECHO) {
  234. $in_echo = true;
  235. } elseif ($token[1] == ';' && $in_echo) {
  236. if ($tokens[$i+1][0] === T_WHITESPACE && $tokens[$i+2][0] === T_ECHO) {
  237. next($tokens);
  238. $i++;
  239. }
  240. if ($tokens[$i+1][0] === T_ECHO) {
  241. // join two consecutive echos
  242. next($tokens);
  243. $token[1] = ','; // '.' would conflict with "a".1+2 and would use more memory //! remove ',' and "," but not $var","
  244. } else {
  245. $in_echo = false;
  246. }
  247. } elseif ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) {
  248. $token[1] = '$' . $short_variables[$token[1]];
  249. }
  250. if (isset($set[substr($output, -1)]) || isset($set[$token[1][0]])) {
  251. $space = '';
  252. }
  253. $output .= $space . $token[1];
  254. $space = '';
  255. }
  256. }
  257. return $output;
  258. }
  259. function minify_css($file) {
  260. return lzw_compress(preg_replace('~\\s*([:;{},])\\s*~', '\\1', preg_replace('~/\\*.*\\*/~sU', '', $file)));
  261. }
  262. function minify_js($file) {
  263. if (function_exists('jsShrink')) {
  264. $file = jsShrink($file);
  265. }
  266. return lzw_compress($file);
  267. }
  268. function compile_file($match) {
  269. global $project;
  270. $file = "";
  271. list(, $filenames, $callback) = $match;
  272. if ($filenames != "") {
  273. foreach (explode(";", $filenames) as $filename) {
  274. $file .= file_get_contents(dirname(__FILE__) . "/$project/$filename");
  275. }
  276. }
  277. if ($callback) {
  278. $file = call_user_func($callback, $file);
  279. }
  280. return '"' . add_quo_slashes($file) . '"';
  281. }
  282. $project = "adminer";
  283. if ($_SERVER["argv"][1] == "editor") {
  284. $project = "editor";
  285. array_shift($_SERVER["argv"]);
  286. }
  287. $driver = "";
  288. if (file_exists(dirname(__FILE__) . "/adminer/drivers/" . $_SERVER["argv"][1] . ".inc.php")) {
  289. $driver = $_SERVER["argv"][1];
  290. array_shift($_SERVER["argv"]);
  291. }
  292. unset($_COOKIE["adminer_lang"]);
  293. $_SESSION["lang"] = $_SERVER["argv"][1]; // Adminer functions read language from session
  294. include dirname(__FILE__) . "/adminer/include/lang.inc.php";
  295. if (isset($langs[$_SESSION["lang"]])) {
  296. include dirname(__FILE__) . "/adminer/lang/$_SESSION[lang].inc.php";
  297. array_shift($_SERVER["argv"]);
  298. }
  299. if ($_SERVER["argv"][1]) {
  300. echo "Usage: php compile.php [editor] [driver] [lang]\n";
  301. echo "Purpose: Compile adminer[-driver][-lang].php or editor[-driver][-lang].php.\n";
  302. exit(1);
  303. }
  304. // check function definition in drivers
  305. $filename = dirname(__FILE__) . "/adminer/drivers/mysql.inc.php";
  306. preg_match_all('~\\bfunction ([^(]+)~', file_get_contents($filename), $matches); //! respect context (extension, class)
  307. $functions = array_combine($matches[1], $matches[0]);
  308. unset($functions["__destruct"], $functions["Min_DB"], $functions["Min_Result"], $functions["Min_Driver"]);
  309. foreach (glob(dirname(__FILE__) . "/adminer/drivers/" . ($driver ? $driver : "*") . ".inc.php") as $filename) {
  310. if ($filename != "mysql.inc.php") {
  311. $file = file_get_contents($filename);
  312. foreach ($functions as $val) {
  313. if (!strpos($file, "$val(")) {
  314. fprintf(STDERR, "Missing $val in $filename\n");
  315. }
  316. }
  317. }
  318. }
  319. include dirname(__FILE__) . "/adminer/include/pdo.inc.php";
  320. include dirname(__FILE__) . "/adminer/include/driver.inc.php";
  321. $features = array("call" => "routine", "dump", "event", "privileges", "procedure" => "routine", "processlist", "routine", "scheme", "sequence", "status", "trigger", "type", "user" => "privileges", "variables", "view");
  322. $lang_ids = array(); // global variable simplifies usage in a callback function
  323. $file = file_get_contents(dirname(__FILE__) . "/$project/index.php");
  324. if ($driver) {
  325. $connection = (object) array("server_info" => 5.1); // MySQL support is version specific
  326. $_GET[$driver] = true; // to load the driver
  327. include_once dirname(__FILE__) . "/adminer/drivers/$driver.inc.php";
  328. foreach ($features as $key => $feature) {
  329. if (!support($feature)) {
  330. if (!is_int($key)) {
  331. $feature = $key;
  332. }
  333. $file = str_replace("} elseif (isset(\$_GET[\"$feature\"])) {\n\tinclude \"./$feature.inc.php\";\n", "", $file);
  334. }
  335. }
  336. if (!support("routine")) {
  337. $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);
  338. }
  339. }
  340. $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file);
  341. $file = str_replace('include "../adminer/include/coverage.inc.php";', '', $file);
  342. if ($driver) {
  343. $file = preg_replace('(include "../adminer/drivers/(?!' . preg_quote($driver) . '\.).*\\s*)', '', $file);
  344. }
  345. $file = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'put_file', $file); // bootstrap.inc.php
  346. if ($driver) {
  347. foreach ($features as $feature) {
  348. if (!support($feature)) {
  349. $file = preg_replace("((\t*)" . preg_quote('if (support("' . $feature . '")') . ".*\n\\1\\})sU", '', $file);
  350. }
  351. }
  352. if (count($drivers) == 1) {
  353. $file = str_replace('<?php echo html_select("driver", $drivers, DRIVER); ?>', "<input type='hidden' name='driver' value='" . ($driver == "mysql" ? "server" : $driver) . "'>" . reset($drivers), $file);
  354. }
  355. $file = preg_replace('(;../externals/jush/modules/jush-(?!textarea\.|txt\.|' . preg_quote($driver == "mysql" ? "sql" : $driver) . '\.)[^.]+.js)', '', $file);
  356. }
  357. if ($project == "editor") {
  358. $file = preg_replace('~;../externals/jush/jush.css~', '', $file);
  359. $file = preg_replace('~;?../externals/jush/modules/jush[^.]*.js~', '', $file);
  360. }
  361. $file = preg_replace_callback("~lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])~s", 'lang_ids', $file);
  362. $file = preg_replace_callback('~\\b(include|require) "([^"]*\\$LANG.inc.php)";~', 'put_file_lang', $file);
  363. $file = str_replace("\r", "", $file);
  364. if ($_SESSION["lang"]) {
  365. // single language version
  366. $file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file);
  367. $file = str_replace("<?php switch_lang(); ?>\n", "", $file);
  368. $file = str_replace('<?php echo $LANG; ?>', $_SESSION["lang"], $file);
  369. }
  370. $file = str_replace('<script type="text/javascript" src="static/editing.js"></script>' . "\n", "", $file);
  371. $file = str_replace('<script type="text/javascript" src="../externals/jush/modules/jush-textarea.js"></script>' . "\n", "", $file);
  372. $file = str_replace('<script type="text/javascript" src="../externals/jush/modules/jush-txt.js"></script>' . "\n", "", $file);
  373. $file = str_replace('<script type="text/javascript" src="../externals/jush/modules/jush-<?php echo $jush; ?>.js"></script>' . "\n", "", $file);
  374. $file = str_replace('<link rel="stylesheet" type="text/css" href="../externals/jush/jush.css">' . "\n", "", $file);
  375. $file = preg_replace_callback("~compile_file\\('([^']+)'(?:, '([^']*)')?\\)~", 'compile_file', $file); // integrate static files
  376. $replace = 'h(preg_replace("~\\\\\\\\?.*~", "", ME)) . "?file=\\1&amp;version=' . $VERSION . ($driver ? '&amp;driver=' . $driver : '');
  377. $file = preg_replace('~\\.\\./adminer/static/(default\\.css|functions\\.js|favicon\\.ico)~', '<?php echo ' . $replace . '"; ?>', $file);
  378. $file = preg_replace('~\\.\\./adminer/static/([^\'"]*)~', '" . ' . $replace, $file);
  379. $file = preg_replace('~\\.\\./externals/jush/modules/(jush\\.js)~', '<?php echo ' . $replace . '"; ?>', $file);
  380. $file = preg_replace("~<\\?php\\s*\\?>\n?|\\?>\n?<\\?php~", '', $file);
  381. $file = php_shrink($file);
  382. $filename = $project . (preg_match('~-dev$~', $VERSION) ? "" : "-$VERSION") . ($driver ? "-$driver" : "") . ($_SESSION["lang"] ? "-$_SESSION[lang]" : "") . ".php";
  383. file_put_contents($filename, $file);
  384. echo "$filename created (" . strlen($file) . " B).\n";