2007-07-02 05:51:26 +00:00
< ? php
2009-06-13 20:55:19 +00:00
include dirname ( __FILE__ ) . " /adminer/include/version.inc.php " ;
2009-05-29 16:57:32 +00:00
include dirname ( __FILE__ ) . " /externals/jsmin-php/jsmin.php " ;
2009-05-29 14:53:42 +00:00
2007-07-09 15:28:37 +00:00
function add_apo_slashes ( $s ) {
return addcslashes ( $s , " \\ ' " );
}
2007-07-05 07:34:42 +00:00
function remove_lang ( $match ) {
2007-08-04 19:36:01 +00:00
global $translations ;
2007-07-26 16:47:28 +00:00
$idf = strtr ( $match [ 2 ], array ( " \\ ' " => " ' " , " \\ \\ " => " \\ " ));
2007-08-04 19:36:01 +00:00
$s = ( $translations [ $idf ] ? $translations [ $idf ] : $idf );
2009-06-21 23:20:32 +00:00
if ( $match [ 3 ] == " , " ) { // lang() has parameters
2007-07-09 15:28:37 +00:00
return " $match[1] " . ( is_array ( $s ) ? " lang(array(' " . implode ( " ', ' " , array_map ( 'add_apo_slashes' , $s )) . " '), " : " sprintf(' " . add_apo_slashes ( $s ) . " ', " );
}
return ( $match [ 1 ] && $match [ 4 ] ? $s : " $match[1] ' " . add_apo_slashes ( $s ) . " ' $match[4] " );
2007-07-05 07:34:42 +00:00
}
2009-06-21 23:20:32 +00:00
$lang_ids = array (); // global variable simplifies usage in a callback function
2009-07-11 19:45:57 +00:00
2008-05-23 14:46:52 +00:00
function lang_ids ( $match ) {
global $lang_ids ;
2009-07-11 19:45:57 +00:00
$lang_id = & $lang_ids [ stripslashes ( $match [ 1 ])];
if ( ! isset ( $lang_id )) {
$lang_id = count ( $lang_ids ) - 1 ;
}
return ( $_COOKIE [ " adminer_lang " ] ? $match [ 0 ] : " lang( $lang_id $match[2] " );
2008-05-23 14:46:52 +00:00
}
2007-07-02 05:51:26 +00:00
function put_file ( $match ) {
2009-07-11 19:45:57 +00:00
global $project ;
2009-07-03 06:26:01 +00:00
if ( basename ( $match [ 2 ]) == '$LANG.inc.php' ) {
2009-07-11 19:45:57 +00:00
return $match [ 0 ]; // processed later
2007-07-26 16:47:28 +00:00
}
2009-07-03 06:26:01 +00:00
$return = file_get_contents ( dirname ( __FILE__ ) . " / $project / $match[2] " );
2009-07-11 19:45:57 +00:00
if ( basename ( $match [ 2 ]) != " lang.inc.php " || ! $_COOKIE [ " adminer_lang " ]) {
2009-06-21 23:20:32 +00:00
$tokens = token_get_all ( $return ); // to find out the last token
2009-06-01 16:20:06 +00:00
return " ?> \n $return " . ( in_array ( $tokens [ count ( $tokens ) - 1 ][ 0 ], array ( T_CLOSE_TAG , T_INLINE_HTML ), true ) ? " <?php " : " " );
} elseif ( preg_match ( '~\\s*(\\$pos = .*)~' , $return , $match2 )) {
2009-06-21 23:20:32 +00:00
// single language lang() is used for plural
2009-06-21 23:31:12 +00:00
return " function lang( \$ translation, \$ number) { \n \t " . str_replace ( '$LANG' , " ' $_COOKIE[adminer_lang] ' " , $match2 [ 1 ]) . " \n \t return sprintf( \$ translation[ \$ pos], \$ number); \n } \n " ;
2009-06-01 16:20:06 +00:00
} else {
echo " lang() not found \n " ;
2007-07-02 05:51:26 +00:00
}
}
2009-07-11 19:45:57 +00:00
function put_file_lang ( $match ) {
global $lang_ids , $project ;
if ( $_COOKIE [ " adminer_lang " ]) {
return " " ;
}
$return = " " ;
foreach ( glob ( dirname ( __FILE__ ) . " / $project /lang/*.inc.php " ) as $filename ) {
include $filename ; // assign $translations
$translation_ids = array_flip ( $lang_ids ); // default translation
foreach ( $translations as $key => $val ) {
if ( isset ( $val )) {
$translation_ids [ $lang_ids [ $key ]] = $val ;
}
}
$return .= " \t case \" " . basename ( $filename , '.inc.php' ) . '": $translations = array(' ;
foreach ( $translation_ids as $val ) {
$return .= ( is_array ( $val ) ? " array(' " . implode ( " ', ' " , array_map ( 'add_apo_slashes' , $val )) . " ') " : " ' " . add_apo_slashes ( $val ) . " ' " ) . " , " ;
}
$return = substr ( $return , 0 , - 2 ) . " ); break; \n " ;
}
return " switch ( \$ LANG) { \n $return } \n " ;
}
2009-04-15 14:31:05 +00:00
function short_identifier ( $number , $chars ) {
$return = '' ;
2009-04-15 11:23:31 +00:00
while ( $number >= 0 ) {
$return .= $chars { $number % strlen ( $chars )};
$number = floor ( $number / strlen ( $chars )) - 1 ;
}
return $return ;
}
2009-05-08 08:31:56 +00:00
// based on Dgx's PHP shrinker
2007-09-04 14:01:00 +00:00
function php_shrink ( $input ) {
2009-05-08 05:13:51 +00:00
$special_variables = array_flip ( array ( '$this' , '$GLOBALS' , '$_GET' , '$_POST' , '$_FILES' , '$_COOKIE' , '$_SESSION' , '$_SERVER' ));
2009-04-15 11:23:31 +00:00
static $short_variables = array ();
2009-06-21 23:20:32 +00:00
static $short_functions = array ();
2009-04-15 11:23:31 +00:00
$shortening = true ;
2009-06-03 18:34:57 +00:00
$special_functions = array_flip ( array ( 'Min_DB' , 'Min_Result' , '__construct' ));
2009-04-16 14:17:31 +00:00
$defined_functions = array ();
2009-04-15 14:31:05 +00:00
$tokens = token_get_all ( $input );
foreach ( $tokens as $i => $token ) {
if ( $token [ 0 ] === T_VARIABLE && ! isset ( $special_variables [ $token [ 1 ]])) {
$short_variables [ $token [ 1 ]] ++ ;
2009-04-16 14:17:31 +00:00
} elseif ( $token [ 0 ] === T_STRING && $tokens [ $i + 1 ] === '(' && ! isset ( $special_functions [ $token [ 1 ]])) {
2009-04-15 14:31:05 +00:00
$short_functions [ $token [ 1 ]] ++ ;
2009-04-16 14:17:31 +00:00
if ( $tokens [ $i - 2 ][ 0 ] === T_FUNCTION ) {
$defined_functions [ $token [ 1 ]] = true ;
}
2009-04-15 14:31:05 +00:00
}
}
2009-04-16 14:17:31 +00:00
2009-04-15 14:31:05 +00:00
arsort ( $short_variables );
foreach ( array_keys ( $short_variables ) as $number => $key ) {
$short_variables [ $key ] = short_identifier ( $number , implode ( " " , range ( 'a' , 'z' )) . '_' . implode ( " " , range ( 'A' , 'Z' ))); // could use also numbers and \x7f-\xff
}
arsort ( $short_functions );
2009-04-16 14:17:31 +00:00
$number = 0 ;
foreach ( $short_functions as $key => $val ) {
if ( isset ( $defined_functions [ $key ])) {
2009-04-17 13:25:35 +00:00
do {
2009-06-21 23:20:32 +00:00
$short_functions [ $key ] = short_identifier ( $number , implode ( " " , range ( 'a' , 'z' ))); // _ not used to not collide with gettext()
2009-04-17 13:25:35 +00:00
$number ++ ;
2009-06-21 23:20:32 +00:00
} while ( isset ( $short_functions [ $short_functions [ $key ]])); // don't overwrite existing functions
2009-04-16 14:17:31 +00:00
}
2009-04-15 14:31:05 +00:00
}
2009-04-15 11:23:31 +00:00
2007-09-04 14:01:00 +00:00
$set = array_flip ( preg_split ( '//' , '!"#$&\'()*+,-./:;<=>?@[\]^`{|}' ));
$space = '' ;
$output = '' ;
2009-06-01 21:31:05 +00:00
$in_echo = false ;
2009-07-11 19:45:57 +00:00
$doc_comment = false ; // include only first /**
2009-06-01 21:31:05 +00:00
for ( reset ( $tokens ); list ( $i , $token ) = each ( $tokens ); ) {
2007-09-04 14:01:00 +00:00
if ( ! is_array ( $token )) {
$token = array ( 0 , $token );
}
2009-07-11 19:45:57 +00:00
if ( $token [ 0 ] == T_COMMENT || $token [ 0 ] == T_WHITESPACE || ( $token [ 0 ] == T_DOC_COMMENT && $doc_comment )) {
2007-09-04 14:01:00 +00:00
$space = " \n " ;
} else {
2009-07-11 19:45:57 +00:00
if ( $token [ 0 ] == T_DOC_COMMENT ) {
$doc_comment = true ;
}
2009-04-15 11:23:31 +00:00
if ( $token [ 0 ] == T_VAR ) {
$shortening = false ;
} elseif ( ! $shortening ) {
if ( $token [ 1 ] == ';' ) {
$shortening = true ;
}
2009-06-01 21:31:05 +00:00
} elseif ( $token [ 0 ] == T_ECHO ) {
$in_echo = true ;
} elseif ( $token [ 1 ] == ';' && $in_echo ) {
$in_echo = false ;
if ( $tokens [ $i + 1 ][ 0 ] === T_WHITESPACE && $tokens [ $i + 2 ][ 0 ] === T_ECHO ) {
2009-06-21 23:20:32 +00:00
// join two consecutive echos
2009-06-01 21:31:05 +00:00
next ( $tokens );
next ( $tokens );
$token [ 1 ] = '.' ; //! join ''.'' and "".""
}
2009-04-15 14:31:05 +00:00
} elseif ( $token [ 0 ] === T_VARIABLE && ! isset ( $special_variables [ $token [ 1 ]])) {
$token [ 1 ] = '$' . $short_variables [ $token [ 1 ]];
2009-04-16 14:17:31 +00:00
} elseif ( $token [ 0 ] === T_STRING && $tokens [ $i + 1 ] === '(' && isset ( $defined_functions [ $token [ 1 ]])
2009-06-21 23:20:32 +00:00
&& $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
2009-04-15 14:31:05 +00:00
) {
$token [ 1 ] = $short_functions [ $token [ 1 ]];
2009-06-21 23:20:32 +00:00
} elseif ( $token [ 0 ] == T_CONSTANT_ENCAPSED_STRING
&& (( $tokens [ $i - 1 ] === '(' && in_array ( $tokens [ $i - 2 ][ 1 ], array ( 'array_map' , 'set_exception_handler' ), true )) || $token [ 1 ] == " 'normalize_enum' " )
&& isset ( $defined_functions [ substr ( $token [ 1 ], 1 , - 1 )])
) { // minify callback functions too
2009-04-15 14:31:05 +00:00
$token [ 1 ] = " ' " . $short_functions [ substr ( $token [ 1 ], 1 , - 1 )] . " ' " ;
2009-04-15 11:23:31 +00:00
}
2007-09-04 14:01:00 +00:00
if ( isset ( $set [ substr ( $output , - 1 )]) || isset ( $set [ $token [ 1 ]{ 0 }])) {
$space = '' ;
}
$output .= $space . $token [ 1 ];
$space = '' ;
}
}
return $output ;
}
2009-06-26 20:43:41 +00:00
function minify_css ( $file ) {
return preg_replace ( '~\\s*([:;{},])\\s*~' , '\\1' , $file );
}
function compile_file ( $match ) {
2009-07-13 21:40:43 +00:00
global $project ;
return call_user_func ( $match [ 2 ], file_get_contents ( dirname ( __FILE__ ) . " / $project / $match[1] " ));
2009-06-26 20:43:41 +00:00
}
2009-07-03 09:17:23 +00:00
error_reporting ( 4343 ); // errors and warnings
2009-07-03 06:26:01 +00:00
$project = " adminer " ;
2009-07-03 09:15:22 +00:00
if ( file_exists ( $_SERVER [ " argv " ][ 1 ] . " /index.php " )) {
$project = $_SERVER [ " argv " ][ 1 ];
array_shift ( $_SERVER [ " argv " ]);
}
if ( isset ( $_SERVER [ " argv " ][ 1 ])) {
2009-06-21 23:31:12 +00:00
$_COOKIE [ " adminer_lang " ] = $_SERVER [ " argv " ][ 1 ]; // Adminer functions read language from cookie
2009-06-13 20:55:19 +00:00
include dirname ( __FILE__ ) . " /adminer/include/lang.inc.php " ;
2009-07-03 06:26:01 +00:00
if ( isset ( $_SERVER [ " argv " ][ 2 ]) || ! isset ( $langs [ $_COOKIE [ " adminer_lang " ]])) {
2009-07-03 09:15:22 +00:00
echo " Usage: php compile.php [adminer] [lang] \n Purpose: Compile adminer[-lang].php from adminer/index.php. \n " ;
2007-07-05 10:46:44 +00:00
exit ( 1 );
}
2009-07-03 09:47:29 +00:00
include dirname ( __FILE__ ) . " / $project /lang/ $_COOKIE[adminer_lang] .inc.php " ;
2007-07-05 10:46:44 +00:00
}
2007-08-04 19:36:01 +00:00
2009-07-03 06:26:01 +00:00
$filename = $project . ( $_COOKIE [ " adminer_lang " ] ? " - $_COOKIE[adminer_lang] " : " " ) . " .php " ;
$file = file_get_contents ( dirname ( __FILE__ ) . " / $project /index.php " );
2009-06-24 10:02:12 +00:00
$file = preg_replace ( '(' . str_replace ( ' ' , '\\s*' , preg_quote ( ' if (isset($_GET["coverage"])) { include "./coverage.inc.php"; }' )) . ')' , '' , $file );
2009-06-01 16:20:06 +00:00
$file = preg_replace_callback ( '~\\b(include|require) "([^"]*)";~' , 'put_file' , $file );
2009-07-11 19:45:57 +00:00
$file = preg_replace_callback ( '~\\b(include|require) "([^"]*)";~' , 'put_file' , $file ); // bootstrap.inc.php
2008-10-02 16:42:55 +00:00
$file = preg_replace ( " ~if \\ (isset \\ ( \\ \$ _SESSION \\ [ \" coverage.* \n } \n | && !isset \\ ( \\ \$ _SESSION \\ [ \" coverage \" \\ ] \\ )~sU " , '' , $file );
2009-07-11 19:45:57 +00:00
$file = preg_replace_callback ( " ~lang \\ ('((?:[^ \\ \\ ']+| \\ \\ .)*)'([,)])~s " , 'lang_ids' , $file );
$file = preg_replace_callback ( '~\\b(include|require) "([^"]*\\$LANG.inc.php)";~' , 'put_file_lang' , $file );
2009-06-21 23:31:12 +00:00
if ( $_COOKIE [ " adminer_lang " ]) {
2009-06-21 23:20:32 +00:00
// single language version
2007-07-10 18:53:35 +00:00
$file = preg_replace_callback ( " ~(< \\ ?php \\ s*echo )?lang \\ ('((?:[^ \\ \\ ']+| \\ \\ .)*)'([,)])(; \\ s* \\ ?>)?~s " , 'remove_lang' , $file );
2007-07-05 10:46:44 +00:00
$file = str_replace ( " <?php switch_lang(); ?> \n " , " " , $file );
2009-06-21 23:31:12 +00:00
$file = str_replace ( '<?php echo $LANG; ?>' , $_COOKIE [ " adminer_lang " ], $file );
2007-07-05 07:34:42 +00:00
}
2009-07-13 21:40:43 +00:00
$file = str_replace ( '<script type="text/javascript" src="editing.js"></script>' . " \n " , " " , $file );
$file = preg_replace_callback ( " ~compile_file \\ ('([^']+)', '([^']+)' \\ );~ " , 'compile_file' , $file ); // integrate static files
2009-07-03 06:26:01 +00:00
$replace = 'htmlspecialchars(preg_replace("~\\\\\\\\?.*~", "", $_SERVER["REQUEST_URI"])) . "?file=\\1&version=' . $VERSION ;
2009-07-13 21:40:43 +00:00
$file = preg_replace ( '~(?:\\.\\./adminer/|\\./)(default\\.css|functions\\.js|favicon\\.ico)~' , '<?php echo ' . $replace . '"; ?>' , $file );
2009-07-03 06:26:01 +00:00
$file = preg_replace ( '~\\.\\./adminer/((plus|cross|up|down|arrow)\\.gif)~' , '" . ' . $replace , $file );
2009-06-13 20:55:19 +00:00
$file = str_replace ( " ../externals/jush/ " , " http://jush.sourceforge.net/ " , $file );
2009-06-06 04:33:17 +00:00
$file = preg_replace ( " ~< \\ ?php \\ s* \\ ?> \n ?| \\ ?> \n ?< \\ ?php~ " , '' , $file );
2007-09-04 14:01:00 +00:00
$file = php_shrink ( $file );
2009-06-21 23:20:32 +00:00
fwrite ( fopen ( $filename , " w " ), $file ); // file_put_contents() since PHP 5
2007-07-05 10:46:44 +00:00
echo " $filename created. \n " ;