2009-07-03 06:26:01 +00:00
< ? php
2009-07-11 19:45:57 +00:00
function adminer_name () {
return call_adminer ( 'name' , lang ( 'Editor' ));
}
function adminer_credentials () {
return call_adminer ( 'credentials' , array ()); // default INI settings
}
function adminer_database () {
2009-07-16 17:45:07 +00:00
$dbs = get_databases ();
2009-07-11 19:45:57 +00:00
return call_adminer ( 'database' , ( count ( $dbs ) == 1 ? $dbs [ 0 ] : ( count ( $dbs ) == 2 && information_schema ( $dbs [ 0 ]) ? $dbs [ 1 ] : 'test' )));
}
2009-07-21 12:19:25 +00:00
function adminer_login_form ( $username ) {
if ( call_adminer ( 'login_form' , true , $username )) {
?>
2009-07-21 15:25:05 +00:00
< table cellspacing = " 0 " >
< tr >< th >< ? php echo lang ( 'Username' ); ?> <td><input type="hidden" name="server" value="" /><input name="username" value="<?php echo htmlspecialchars($username); ?>">
< tr >< th >< ? php echo lang ( 'Password' ); ?> <td><input type="password" name="password">
</ table >
2009-07-21 12:19:25 +00:00
< ? php
}
}
function adminer_login ( $login , $password ) {
return call_adminer ( 'login' , true , $login , $password );
}
2009-07-11 19:45:57 +00:00
function adminer_table_name ( $row ) {
2009-07-16 09:07:04 +00:00
table_comment ( $row );
2009-07-11 19:45:57 +00:00
return call_adminer ( 'table_name' , htmlspecialchars ( strlen ( $row [ " Comment " ]) ? $row [ " Comment " ] : $row [ " Name " ]), $row );
}
function adminer_field_name ( $fields , $key ) {
return call_adminer ( 'field_name' , htmlspecialchars ( strlen ( $fields [ $key ][ " comment " ]) ? $fields [ $key ][ " comment " ] : $key ), $fields , $key );
}
function adminer_select_links ( $table_status ) {
return call_adminer ( 'select_links' , " " , $table_status );
}
2009-07-22 11:04:03 +00:00
function adminer_backward_keys ( $table ) {
global $dbh ;
$return = array ();
2009-07-22 11:36:27 +00:00
$result = $dbh -> query ( " SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_COLUMN_NAME
FROM information_schema . KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = " . $dbh->quote (adminer_database()) . "
AND REFERENCED_TABLE_SCHEMA = " . $dbh->quote (adminer_database()) . "
AND REFERENCED_TABLE_NAME = " . $dbh->quote ( $table ) . "
ORDER BY ORDINAL_POSITION " );
2009-07-22 11:04:03 +00:00
if ( $result ) {
while ( $row = $result -> fetch_assoc ()) {
$return [ $row [ " TABLE_NAME " ]][ $row [ " CONSTRAINT_NAME " ]][ $row [ " COLUMN_NAME " ]] = $row [ " REFERENCED_COLUMN_NAME " ];
}
$result -> free ();
}
return call_adminer ( 'backward_keys' , $return , $table );
}
2009-07-11 19:45:57 +00:00
function adminer_select_query ( $query ) {
2009-07-11 19:52:48 +00:00
return call_adminer ( 'select_query' , " <!-- " . str_replace ( " -- " , " --><!-- " , $query ) . " --> \n " , $query );
2009-07-11 19:45:57 +00:00
}
2009-07-23 14:42:38 +00:00
function adminer_row_description ( $table ) {
$return = " " ;
// first varchar column
foreach ( fields ( $table ) as $field ) {
if ( $field [ " type " ] == " varchar " ) {
$return = idf_escape ( $field [ " field " ]);
break ;
}
}
return call_adminer ( 'row_description' , $return , $table );
}
2009-07-20 15:34:05 +00:00
function adminer_row_descriptions ( $rows , $foreign_keys ) {
global $dbh ;
$return = $rows ;
foreach ( $rows [ 0 ] as $key => $val ) {
foreach (( array ) $foreign_keys [ $key ] as $foreign_key ) {
if ( count ( $foreign_key [ " source " ]) == 1 ) {
$id = idf_escape ( $foreign_key [ " target " ][ 0 ]);
2009-07-23 14:42:38 +00:00
$name = adminer_row_description ( $foreign_key [ " table " ]);
if ( strlen ( $name )) {
// find all used ids
$ids = array ();
foreach ( $rows as $row ) {
$ids [ $row [ $key ]] = $dbh -> quote ( $row [ $key ]);
2009-07-20 15:34:05 +00:00
}
2009-07-23 14:42:38 +00:00
// uses constant number of queries to get the descriptions, join would be complex, multiple queries would be slow
$descriptions = array ();
$result = $dbh -> query ( " SELECT $id , $name FROM " . idf_escape ( $foreign_key [ " table " ]) . " WHERE $id IN ( " . implode ( " , " , $ids ) . " ) " );
while ( $row = $result -> fetch_row ()) {
$descriptions [ $row [ 0 ]] = $row [ 1 ];
}
$result -> free ();
// use the descriptions
foreach ( $rows as $n => $row ) {
$return [ $n ][ $key ] = $descriptions [ $row [ $key ]];
}
break ;
2009-07-20 15:34:05 +00:00
}
}
}
}
return call_adminer ( 'row_descriptions' , $return , $rows , $foreign_keys );
}
2009-07-14 10:06:35 +00:00
function adminer_select_val ( $val , $link ) {
2009-07-22 14:00:03 +00:00
return call_adminer ( 'select_val' , ( $link ?
" <a href= \" $link\ " > $val </ a > " :
2009-07-23 12:50:46 +00:00
( $val == " <i>NULL</i> " ? " " : preg_replace ( '~^<code>(.*)</code>$~' , '\\1' , $val ))
2009-07-22 14:00:03 +00:00
), $val , $link );
2009-07-14 10:06:35 +00:00
}
2009-07-11 19:45:57 +00:00
function adminer_message_query ( $query ) {
2009-07-23 14:42:38 +00:00
return call_adminer ( 'message_query' , " <!-- \n " . str_replace ( " -- " , " --><!-- " , $query ) . " \n --> " , $query );
2009-07-11 19:45:57 +00:00
}
2009-07-23 11:31:08 +00:00
function adminer_edit_functions ( $field ) {
2009-07-23 14:42:38 +00:00
return call_adminer ( 'edit_functions' , ( isset ( $_GET [ " select " ]) ? array ( " orig " => lang ( 'original' )) : array ()) + array ( " " ), $field );
}
function adminer_edit_input ( $table , $field ) {
global $dbh ;
$return = null ;
$foreign_keys = column_foreign_keys ( $table );
foreach (( array ) $foreign_keys [ $field [ " field " ]] as $foreign_key ) {
if ( count ( $foreign_key [ " source " ]) == 1 ) {
$id = idf_escape ( $foreign_key [ " target " ][ 0 ]);
$name = adminer_row_description ( $foreign_key [ " table " ]);
if ( strlen ( $name ) && $dbh -> result ( $dbh -> query ( " SELECT COUNT(*) FROM " . idf_escape ( $foreign_key [ " table " ]))) <= 1000 ) { // optionlist with more than 1000 options would be too big
if ( $field [ " null " ]) {
$return [ " " ] = " " ;
}
$result = $dbh -> query ( " SELECT $id , $name FROM " . idf_escape ( $foreign_key [ " table " ]) . " ORDER BY 2 " );
while ( $row = $result -> fetch_row ()) {
$return [ $row [ 0 ]] = $row [ 1 ];
}
$result -> free ();
break ;
}
}
}
return call_adminer ( 'edit_input' , $return , $table , $field );
2009-07-23 11:31:08 +00:00
}
2009-07-11 19:45:57 +00:00
function adminer_navigation ( $missing ) {
global $SELF ;
if ( call_adminer ( 'navigation' , true , $missing ) && $missing != " auth " ) {
?>
2009-07-03 06:26:01 +00:00
< form action = " " method = " post " >
< p >
2009-07-11 20:30:40 +00:00
< input type = " hidden " name = " token " value = " <?php echo $_SESSION["tokens"] [ $_GET["server"] ]; ?> " >
< input type = " submit " name = " logout " value = " <?php echo lang('Logout'); ?> " >
2009-07-14 11:09:39 +00:00
</ p >
2009-07-03 06:26:01 +00:00
</ form >
< ? php
2009-07-11 19:45:57 +00:00
if ( $missing != " db " ) {
$table_status = table_status ();
if ( ! $table_status ) {
2009-07-11 20:30:40 +00:00
echo " <p class='message'> " . lang ( 'No tables.' ) . " \n " ;
2009-07-11 19:45:57 +00:00
} else {
echo " <p> \n " ;
foreach ( $table_status as $row ) {
2009-07-21 13:59:02 +00:00
$name = adminer_table_name ( $row );
if ( isset ( $row [ " Engine " ]) && strlen ( $name )) { // ignore views and tables without name
echo '<a href="' . htmlspecialchars ( $SELF ) . 'select=' . urlencode ( $row [ " Name " ]) . " \" > $name </a><br> \n " ;
2009-07-03 06:26:01 +00:00
}
}
}
}
}
}