2007-07-02 05:51:26 +00:00
< ? php
2008-01-08 14:02:55 +00:00
$table_status = table_status ( $_GET [ " select " ]);
$indexes = indexes ( $_GET [ " select " ]);
2007-07-06 13:03:13 +00:00
$fields = fields ( $_GET [ " select " ]);
2009-06-21 23:20:32 +00:00
$rights = array (); // privilege => 0
$columns = array (); // selectable columns
2007-08-24 16:00:10 +00:00
unset ( $text_length );
2007-07-06 13:03:13 +00:00
foreach ( $fields as $key => $field ) {
2009-07-27 11:25:37 +00:00
$name = $adminer -> fieldName ( $field );
2009-07-21 13:59:02 +00:00
if ( isset ( $field [ " privileges " ][ " select " ]) && strlen ( $name )) {
2009-07-23 14:42:38 +00:00
$columns [ $key ] = html_entity_decode ( strip_tags ( $name ));
2009-07-13 16:03:51 +00:00
if ( ereg ( 'text|blob' , $field [ " type " ])) {
2009-07-27 14:40:55 +00:00
$text_length = $adminer -> selectLengthProcess ();
2009-07-13 23:28:26 +00:00
}
2007-07-06 13:03:13 +00:00
}
2007-07-09 06:12:22 +00:00
$rights += $field [ " privileges " ];
}
2009-07-24 06:33:30 +00:00
function apply_sql_function ( $function , $column ) {
return ( $function
2009-07-27 09:37:17 +00:00
? ( $function == " distinct " ? " COUNT(DISTINCT " : strtoupper ( " $function ( " )) . " $column ) "
2009-07-24 06:33:30 +00:00
: $column
);
}
2009-07-27 14:40:55 +00:00
list ( $select , $group ) = $adminer -> selectColumnsProcess ( $columns , $indexes );
$where = $adminer -> selectSearchProcess ( $indexes , $fields );
$order = $adminer -> selectOrderProcess ( $columns , $select , $indexes );
$limit = $adminer -> selectLimitProcess ();
2009-06-22 00:13:11 +00:00
$from = ( $select ? implode ( " , " , $select ) : " * " ) . " FROM " . idf_escape ( $_GET [ " select " ]) . ( $where ? " WHERE " . implode ( " AND " , $where ) : " " );
$group_by = ( $group && count ( $group ) < count ( $select ) ? " GROUP BY " . implode ( " , " , $group ) : " " ) . ( $order ? " ORDER BY " . implode ( " , " , $order ) : " " );
2008-01-08 14:02:55 +00:00
if ( $_POST && ! $error ) {
2009-07-16 14:07:14 +00:00
$where_check = " ( " . implode ( " ) OR ( " , array_map ( 'where_check' , ( array ) $_POST [ " check " ])) . " ) " ;
2009-07-27 14:40:55 +00:00
$primary = null ; // empty array means that all primary fields are selected
foreach ( $indexes as $index ) {
if ( $index [ " type " ] == " PRIMARY " ) {
$primary = ( $select ? array_flip ( $index [ " columns " ]) : array ());
}
}
foreach ( $select as $key => $val ) {
$val = $_GET [ " columns " ][ $key ];
if ( ! $val [ " fun " ]) {
unset ( $primary [ $val [ " col " ]]);
}
}
2008-10-01 11:39:13 +00:00
if ( $_POST [ " export " ]) {
2008-08-18 15:18:16 +00:00
dump_headers ( $_GET [ " select " ]);
2008-08-18 22:40:45 +00:00
dump_table ( $_GET [ " select " ], " " );
2009-07-13 23:28:26 +00:00
if ( ! is_array ( $_POST [ " check " ]) || $primary === array ()) {
dump_data ( $_GET [ " select " ], " INSERT " , " SELECT $from " . ( is_array ( $_POST [ " check " ]) ? ( $where ? " AND " : " WHERE " ) . " ( $where_check ) " : " " ) . $group_by );
} else {
2009-06-15 10:01:55 +00:00
$union = array ();
2009-04-01 10:57:34 +00:00
foreach ( $_POST [ " check " ] as $val ) {
2009-07-13 23:28:26 +00:00
// where is not unique so OR can't be used
2009-07-11 19:45:57 +00:00
$union [] = " (SELECT $from " . ( $where ? " AND " : " WHERE " ) . where_check ( $val ) . $group_by . " LIMIT 1) " ;
2008-01-08 14:02:55 +00:00
}
2009-06-15 10:01:55 +00:00
dump_data ( $_GET [ " select " ], " INSERT " , implode ( " UNION ALL " , $union ));
2008-01-08 14:02:55 +00:00
}
2008-10-01 11:39:13 +00:00
exit ;
}
2009-07-27 11:25:37 +00:00
if ( ! $adminer -> selectExtraProcess ( $where )) {
2009-07-24 10:52:24 +00:00
if ( ! $_POST [ " import " ]) { // edit
$result = true ;
$affected = 0 ;
$command = ( $_POST [ " delete " ] ? ( $_POST [ " all " ] && ! $where ? " TRUNCATE " : " DELETE FROM " ) : ( $_POST [ " clone " ] ? " INSERT INTO " : " UPDATE " )) . idf_escape ( $_GET [ " select " ]);
if ( ! $_POST [ " delete " ]) {
$set = array ();
foreach ( $columns as $name => $val ) { //! should check also for edit or insert privileges
$val = process_input ( $name , $fields [ $name ]);
if ( $_POST [ " clone " ]) {
$set [] = ( $val !== false ? $val : idf_escape ( $name ));
} elseif ( $val !== false ) {
$set [] = idf_escape ( $name ) . " = $val " ;
}
2008-12-30 15:19:35 +00:00
}
2009-07-24 10:52:24 +00:00
$command .= ( $_POST [ " clone " ] ? " \n SELECT " . implode ( " , " , $set ) . " \n FROM " . idf_escape ( $_GET [ " select " ]) : " SET \n " . implode ( " , \n " , $set ));
2008-12-30 15:19:35 +00:00
}
2009-07-24 10:52:24 +00:00
if ( $_POST [ " delete " ] || $set ) {
if ( $_POST [ " all " ] || ( $primary === array () && $_POST [ " check " ])) {
$result = queries ( $command . ( $_POST [ " all " ] ? ( $where ? " \n WHERE " . implode ( " AND " , $where ) : " " ) : " \n WHERE $where_check " ));
$affected = $dbh -> affected_rows ;
} else {
foreach (( array ) $_POST [ " check " ] as $val ) {
// where is not unique so OR can't be used
$result = queries ( $command . " \n WHERE " . where_check ( $val ) . " \n LIMIT 1 " );
if ( ! $result ) {
break ;
}
$affected += $dbh -> affected_rows ;
2009-06-30 16:30:28 +00:00
}
2008-12-30 15:19:35 +00:00
}
2008-10-01 11:39:13 +00:00
}
2009-07-24 10:52:24 +00:00
query_redirect ( queries (), remove_from_uri ( " page " ), lang ( '%d item(s) have been affected.' , $affected ), $result , false , ! $result );
//! display edit page in case of an error
} elseif ( is_string ( $file = get_file ( " csv_file " ))) {
$file = preg_replace ( " ~^ \xEF \xBB \xBF ~ " , '' , $file ); //! character set
$cols = " " ;
$rows = array (); //! packet size
preg_match_all ( '~("[^"]*"|[^"\\n]+)+~' , $file , $matches );
foreach ( $matches [ 0 ] as $key => $val ) {
$row = array ();
preg_match_all ( '~(("[^"]*")+|[^,]*),~' , " $val , " , $matches2 );
if ( ! $key && ! array_diff ( $matches2 [ 1 ], array_keys ( $fields ))) { //! doesn't work with column names containing ",\n
// first row corresponds to column names - use it for table structure
$cols = " ( " . implode ( " , " , array_map ( 'idf_escape' , $matches2 [ 1 ])) . " ) " ;
} else {
foreach ( $matches2 [ 1 ] as $col ) {
$row [] = ( ! strlen ( $col ) ? " NULL " : $dbh -> quote ( str_replace ( '""' , '"' , preg_replace ( '~^"|"$~' , '' , $col ))));
}
$rows [] = " \n ( " . implode ( " , " , $row ) . " ) " ;
2009-04-16 13:18:22 +00:00
}
}
2009-07-24 10:52:24 +00:00
$result = queries ( " INSERT INTO " . idf_escape ( $_GET [ " select " ]) . " $cols VALUES " . implode ( " , " , $rows ));
query_redirect ( queries (), remove_from_uri ( " page " ), lang ( '%d row(s) has been imported.' , $dbh -> affected_rows ), $result , false , ! $result );
} else {
$error = upload_error ( $file );
2009-04-16 13:18:22 +00:00
}
2008-08-16 21:15:46 +00:00
}
2008-01-08 14:02:55 +00:00
}
2009-07-11 19:45:57 +00:00
2009-07-27 11:25:37 +00:00
page_header ( lang ( 'Select' ) . " : " . $adminer -> tableName ( $table_status ), $error );
2008-01-08 14:02:55 +00:00
2009-01-23 15:50:42 +00:00
echo " <p> " ;
2007-07-09 06:12:22 +00:00
if ( isset ( $rights [ " insert " ])) {
2007-07-15 10:56:40 +00:00
//! pass search values forth and back
2009-01-23 15:50:42 +00:00
echo '<a href="' . htmlspecialchars ( $SELF ) . 'edit=' . urlencode ( $_GET [ 'select' ]) . '">' . lang ( 'New item' ) . '</a> ' ;
2007-07-02 05:51:26 +00:00
}
2009-07-27 11:25:37 +00:00
echo $adminer -> selectLinks ( $table_status );
2007-07-02 05:51:26 +00:00
2007-07-06 13:03:13 +00:00
if ( ! $columns ) {
2009-07-11 20:30:40 +00:00
echo " <p class='error'> " . lang ( 'Unable to select the table' ) . ( $fields ? " " : " : " . htmlspecialchars ( $dbh -> error )) . " . \n " ;
2007-07-06 13:03:13 +00:00
} else {
2008-08-07 10:57:28 +00:00
echo " <form action='' id='form'> \n " ;
2009-07-27 14:40:55 +00:00
echo " <div style='display: none;'> " ;
echo ( strlen ( $_GET [ " server " ]) ? '<input type="hidden" name="server" value="' . htmlspecialchars ( $_GET [ " server " ]) . '">' : " " );
2009-07-11 20:30:40 +00:00
echo '<input type="hidden" name="db" value="' . htmlspecialchars ( $_GET [ " db " ]) . '">' ;
echo '<input type="hidden" name="select" value="' . htmlspecialchars ( $_GET [ " select " ]) . '">' ;
2009-07-27 14:40:55 +00:00
echo " </div> \n " ;
$adminer -> selectColumnsPrint ( $select , $columns );
$adminer -> selectSearchPrint ( $where , $columns , $indexes );
$adminer -> selectOrderPrint ( $order , $columns , $indexes );
$adminer -> selectLimitPrint ( $limit );
$adminer -> selectLengthPrint ( $text_length );
$adminer -> selectActionPrint ( $text_length );
2007-07-06 13:03:13 +00:00
echo " </form> \n " ;
2009-07-24 06:43:56 +00:00
$query = " SELECT " . ( intval ( $limit ) && count ( $group ) < count ( $select ) ? " SQL_CALC_FOUND_ROWS " : " " ) . $from . $group_by . ( strlen ( $limit ) ? " LIMIT " . intval ( $limit ) . ( intval ( $_GET [ " page " ]) ? " OFFSET " . ( $limit * $_GET [ " page " ]) : " " ) : " " );
2009-07-27 11:25:37 +00:00
echo $adminer -> selectQuery ( $query );
2008-09-01 12:22:26 +00:00
2009-05-08 05:23:03 +00:00
$result = $dbh -> query ( $query );
2007-09-03 11:12:38 +00:00
if ( ! $result ) {
2009-07-11 20:30:40 +00:00
echo " <p class='error'> " . htmlspecialchars ( $dbh -> error ) . " \n " ;
2007-07-06 13:03:13 +00:00
} else {
2009-07-11 22:44:22 +00:00
$email_fields = array ();
2008-12-30 15:19:35 +00:00
echo " <form action='' method='post' enctype='multipart/form-data'> \n " ;
2007-09-03 11:12:38 +00:00
if ( ! $result -> num_rows ) {
2009-07-11 20:30:40 +00:00
echo " <p class='message'> " . lang ( 'No rows.' ) . " \n " ;
2007-09-03 11:12:38 +00:00
} else {
2009-07-14 10:06:35 +00:00
$rows = array ();
while ( $row = $result -> fetch_assoc ()) {
$rows [] = $row ;
}
$result -> free ();
2009-07-16 14:20:05 +00:00
// use count($rows) without LIMIT, COUNT(*) without grouping, FOUND_ROWS otherwise (slowest)
$found_rows = ( intval ( $limit ) && count ( $group ) < count ( $select )
? $dbh -> result ( $dbh -> query ( " SELECT FOUND_ROWS() " )) // space to allow mysql.trace_mode
: count ( $rows )
);
2009-07-14 10:06:35 +00:00
2009-07-23 14:42:38 +00:00
$foreign_keys = column_foreign_keys ( $_GET [ " select " ]);
2009-07-27 11:25:37 +00:00
$descriptions = $adminer -> rowDescriptions ( $rows , $foreign_keys );
2007-09-03 11:12:38 +00:00
2009-07-27 11:25:37 +00:00
$backward_keys = $adminer -> backwardKeys ( $_GET [ " select " ]);
2009-07-21 21:10:19 +00:00
$table_names = array_keys ( $backward_keys );
if ( $table_names ) {
2009-07-27 11:25:37 +00:00
$table_names = array_combine ( $table_names , array_map ( array ( $adminer , 'tableName' ), array_map ( 'table_status' , $table_names )));
2009-07-21 21:10:19 +00:00
}
2009-07-15 14:56:27 +00:00
2009-05-01 10:41:33 +00:00
echo " <table cellspacing='0' class='nowrap'> \n " ;
2009-07-14 10:06:35 +00:00
echo " <thead><tr><td><input type='checkbox' id='all-page' onclick='form_check(this, /check/);'> " ;
2009-07-23 15:49:59 +00:00
$names = array ();
reset ( $select );
2009-07-14 10:06:35 +00:00
foreach ( $rows [ 0 ] as $key => $val ) {
2009-07-23 15:49:59 +00:00
$val = $_GET [ " columns " ][ key ( $select )];
2009-07-24 06:33:30 +00:00
$field = $fields [ $select ? $val [ " col " ] : $key ];
2009-07-27 11:25:37 +00:00
$name = ( $field ? $adminer -> fieldName ( $field ) : " * " );
2009-07-21 13:59:02 +00:00
if ( strlen ( $name )) {
2009-07-23 15:49:59 +00:00
$names [ $key ] = $name ;
2009-07-24 06:33:30 +00:00
echo '<th><a href="' . htmlspecialchars ( remove_from_uri ( '(order|desc)[^=]*' ) . '&order%5B0%5D=' . urlencode ( $key ) . ( $_GET [ " order " ] == array ( $key ) && ! $_GET [ " desc " ][ 0 ] ? '&desc%5B0%5D=1' : '' )) . '">' . apply_sql_function ( $val [ " fun " ], $name ) . " </a> " ;
2009-07-21 13:59:02 +00:00
}
2009-07-23 15:49:59 +00:00
next ( $select );
2009-07-14 10:06:35 +00:00
}
2009-07-15 14:56:27 +00:00
echo ( $backward_keys ? " <th> " . lang ( 'Relations' ) : " " ) . " </thead> \n " ;
2009-07-20 15:34:05 +00:00
foreach ( $descriptions as $n => $row ) {
2009-07-23 17:13:55 +00:00
$unique_idf = implode ( '&' , unique_idf ( $rows [ $n ], $indexes )); //! don't use aggregation functions
2009-07-11 20:30:40 +00:00
echo '<tr' . odd () . '><td><input type="checkbox" name="check[]" value="' . $unique_idf . '" onclick="this.form[\'all\'].checked = false; form_uncheck(\'all-page\');">' . ( count ( $select ) != count ( $group ) || information_schema ( $_GET [ " db " ]) ? '' : ' <a href="' . htmlspecialchars ( $SELF ) . 'edit=' . urlencode ( $_GET [ 'select' ]) . '&' . $unique_idf . '">' . lang ( 'edit' ) . '</a>' );
2007-09-03 11:12:38 +00:00
foreach ( $row as $key => $val ) {
2009-07-24 09:52:18 +00:00
if ( isset ( $names [ $key ])) {
2009-07-22 13:58:54 +00:00
if ( strlen ( $val ) && ( ! isset ( $email_fields [ $key ]) || strlen ( $email_fields [ $key ]))) {
2009-07-23 15:49:59 +00:00
$email_fields [ $key ] = ( is_email ( $val ) ? $names [ $key ] : " " ); //! filled e-mails may be contained on other pages
2009-07-21 13:59:02 +00:00
}
2009-07-21 16:22:42 +00:00
$link = " " ;
2009-07-21 13:59:02 +00:00
if ( ! isset ( $val )) {
$val = " <i>NULL</i> " ;
2007-09-03 11:12:38 +00:00
} else {
2009-07-24 10:14:36 +00:00
if ( ereg ( 'blob|binary' , $fields [ $key ][ " type " ]) && strlen ( $val )) {
$link = htmlspecialchars ( $SELF . 'download=' . urlencode ( $_GET [ " select " ]) . '&field=' . urlencode ( $key ) . '&' ) . $unique_idf ;
}
2009-07-21 13:59:02 +00:00
if ( ! strlen ( trim ( $val , " \t " ))) {
$val = " " ;
2009-07-24 10:14:36 +00:00
} elseif ( strlen ( $text_length ) && ereg ( 'blob|text' , $fields [ $key ][ " type " ]) && is_utf8 ( $val )) {
2009-07-23 17:28:29 +00:00
$val = nl2br ( shorten_utf8 ( $val , max ( 0 , intval ( $text_length )))); // usage of LEFT() would reduce traffic but complicate query
2009-07-21 13:59:02 +00:00
} else {
$val = nl2br ( htmlspecialchars ( $val ));
2007-09-03 11:12:38 +00:00
}
2009-07-21 13:59:02 +00:00
// link related items
foreach (( array ) $foreign_keys [ $key ] as $foreign_key ) {
if ( count ( $foreign_keys [ $key ]) == 1 || count ( $foreign_key [ " source " ]) == 1 ) {
foreach ( $foreign_key [ " source " ] as $i => $source ) {
$link .= where_link ( $i , $foreign_key [ " target " ][ $i ], $rows [ $n ][ $source ]);
}
$link = htmlspecialchars (( strlen ( $foreign_key [ " db " ]) ? preg_replace ( '~([?&]db=)[^&]+~' , '\\1' . urlencode ( $foreign_key [ " db " ]), $SELF ) : $SELF ) . 'select=' . urlencode ( $foreign_key [ " table " ])) . $link ; // InnoDB supports non-UNIQUE keys
break ;
2007-09-03 11:12:38 +00:00
}
2007-07-06 13:03:13 +00:00
}
2007-07-02 15:52:15 +00:00
}
2009-07-22 13:58:54 +00:00
if ( ! $link && is_email ( $val )) {
$link = " mailto: $val " ;
}
2009-07-27 11:25:37 +00:00
$val = $adminer -> selectVal ( $val , $link , $fields [ $key ]);
2009-07-21 13:59:02 +00:00
echo " <td> $val " ;
2007-07-02 05:51:26 +00:00
}
}
2009-07-15 14:56:27 +00:00
if ( $backward_keys ) {
echo " <td> " ;
foreach ( $backward_keys as $table => $keys ) {
foreach ( $keys as $columns ) {
echo ' <a href="' . htmlspecialchars ( $SELF ) . 'select=' . urlencode ( $table );
$i = 0 ;
foreach ( $columns as $column => $val ) {
2009-07-20 15:34:05 +00:00
echo where_link ( $i , $column , $rows [ $n ][ $val ]);
2009-07-15 14:56:27 +00:00
$i ++ ;
}
2009-07-21 21:10:19 +00:00
echo " \" > $table_names[$table] </a> " ;
2009-07-15 14:56:27 +00:00
}
}
}
2009-07-14 10:06:35 +00:00
echo " \n " ;
2007-07-25 16:31:47 +00:00
}
2007-09-03 11:12:38 +00:00
echo " </table> \n " ;
2008-09-01 10:25:29 +00:00
2009-07-16 14:20:05 +00:00
if ( intval ( $limit ) && count ( $group ) >= count ( $select )) {
// slow with big tables
ob_flush ();
flush ();
$found_rows = $dbh -> result ( $dbh -> query ( " SELECT COUNT(*) FROM " . idf_escape ( $_GET [ " select " ]) . ( $where ? " WHERE " . implode ( " AND " , $where ) : " " )));
}
2008-09-01 10:25:29 +00:00
echo " <p> " ;
if ( intval ( $limit ) && $found_rows > $limit ) {
2009-06-21 23:20:32 +00:00
// display first, previous 3, next 3 and last page
2007-09-03 11:12:38 +00:00
$max_page = floor (( $found_rows - 1 ) / $limit );
2009-07-15 11:31:17 +00:00
echo lang ( 'Page' ) . " : " . pagination ( 0 ) . ( $_GET [ " page " ] > 3 ? " ... " : " " );
2007-09-03 11:12:38 +00:00
for ( $i = max ( 1 , $_GET [ " page " ] - 2 ); $i < min ( $max_page , $_GET [ " page " ] + 3 ); $i ++ ) {
2009-07-15 11:31:17 +00:00
echo pagination ( $i );
2007-09-03 11:12:38 +00:00
}
2009-07-15 11:31:17 +00:00
echo ( $_GET [ " page " ] + 3 < $max_page ? " ... " : " " ) . pagination ( $max_page );
2007-07-25 16:31:47 +00:00
}
2009-07-11 20:30:40 +00:00
echo " ( " . lang ( '%d row(s)' , $found_rows ) . ') <label><input type="checkbox" name="all" value="1">' . lang ( 'whole result' ) . " </label> \n " ;
2008-09-01 12:22:26 +00:00
2009-07-13 22:18:53 +00:00
echo ( information_schema ( $_GET [ " db " ]) ? " " : " <fieldset><legend> " . lang ( 'Edit' ) . " </legend><div><input type='submit' name='edit' value=' " . lang ( 'Edit' ) . " '> <input type='submit' name='clone' value=' " . lang ( 'Clone' ) . " '> <input type='submit' name='delete' value=' " . lang ( 'Delete' ) . " ' $confirm ></div></fieldset> \n " );
2009-07-11 20:30:40 +00:00
echo " <fieldset><legend> " . lang ( 'Export' ) . " </legend><div> $dump_output $dump_format <input type='submit' name='export' value=' " . lang ( 'Export' ) . " '></div></fieldset> \n " ;
2007-07-02 05:51:26 +00:00
}
2009-07-11 20:30:40 +00:00
echo " <fieldset><legend> " . lang ( 'CSV Import' ) . " </legend><div><input type='hidden' name='token' value=' $token '><input type='file' name='csv_file'> <input type='submit' name='import' value=' " . lang ( 'Import' ) . " '></div></fieldset> \n " ;
2009-07-11 19:45:57 +00:00
2009-07-27 14:40:55 +00:00
$adminer -> selectExtraPrint ( array_filter ( $email_fields , 'strlen' ));
2009-07-11 19:45:57 +00:00
2008-12-30 15:19:35 +00:00
echo " </form> \n " ;
2007-07-02 05:51:26 +00:00
}
}