2007-07-02 05:51:26 +00:00
< ? php
2009-08-31 10:11:52 +00:00
/** Get database connection
* @ return Min_DB
*/
2009-09-22 10:51:40 +00:00
function connection () {
// can be used in customization, $connection is minified
global $connection ;
return $connection ;
2009-07-27 11:25:37 +00:00
}
2009-08-31 10:11:52 +00:00
/** Unescape database identifier
* @ param string text inside ``
* @ return string
*/
2007-07-02 05:51:26 +00:00
function idf_unescape ( $idf ) {
2010-04-21 22:22:23 +00:00
$last = substr ( $idf , - 1 );
return str_replace ( $last . $last , $last , substr ( $idf , 1 , - 1 ));
2007-07-02 05:51:26 +00:00
}
2009-10-16 12:26:16 +00:00
/** Escape string to use inside ''
* @ param string
* @ return string
*/
function escape_string ( $val ) {
global $connection ;
return substr ( $connection -> quote ( $val ), 1 , - 1 );
}
2009-08-31 10:11:52 +00:00
/** Escape or unescape string to use inside form []
* @ param string
* @ param bool
* @ return string
*/
2007-07-02 05:51:26 +00:00
function bracket_escape ( $idf , $back = false ) {
2009-06-21 23:20:32 +00:00
// escape brackets inside name="x[]"
2007-07-09 14:45:11 +00:00
static $trans = array ( ':' => ':1' , ']' => ':2' , '[' => ':3' );
2007-07-02 05:51:26 +00:00
return strtr ( $idf , ( $back ? array_flip ( $trans ) : $trans ));
}
2009-08-31 10:11:52 +00:00
/** Escape for HTML
* @ param string
* @ return string
*/
2009-07-28 16:20:50 +00:00
function h ( $string ) {
return htmlspecialchars ( $string , ENT_QUOTES );
}
2009-08-31 10:11:52 +00:00
/** Escape for TD
* @ param string
* @ return string
*/
2009-08-25 10:26:47 +00:00
function nbsp ( $string ) {
2010-01-09 23:33:41 +00:00
return ( trim ( $string ) != " " ? h ( $string ) : " " );
2009-08-25 10:26:47 +00:00
}
2010-04-21 12:01:32 +00:00
/** Convert \n to < br >
* @ param string
* @ return string
*/
function nl_br ( $string ) {
return str_replace ( " \n " , " <br> " , $string ); // nl2br() uses XHTML before PHP 5.3
}
2009-09-25 12:03:30 +00:00
/** Generate HTML checkbox
* @ param string
* @ param string
* @ param bool
* @ param string
* @ param string
* @ return string
*/
function checkbox ( $name , $value , $checked , $label = " " , $onclick = " " ) {
2009-09-26 16:52:32 +00:00
static $id = 0 ;
$id ++ ;
2010-04-21 12:01:32 +00:00
$return = " <input type='checkbox' " . ( $name ? " name=' $name ' value=' " . h ( $value ) . " ' " : " " ) . ( $checked ? " checked " : " " ) . ( $onclick ? " onclick= \" $onclick\ " " : " " ) . " id = 'checkbox-$id' > " ;
2010-01-09 23:33:41 +00:00
return ( $label != " " ? " <label for='checkbox- $id '> $return " . h ( $label ) . " </label> " : $return );
2009-09-25 12:03:30 +00:00
}
2009-08-31 10:11:52 +00:00
/** Generate list of HTML options
* @ param array array of strings or arrays ( creates optgroup )
* @ param mixed
* @ param bool always use array keys for value = " " , otherwise only string keys are used
* @ return string
*/
2009-07-23 14:42:38 +00:00
function optionlist ( $options , $selected = null , $use_keys = false ) {
2007-07-02 05:51:26 +00:00
$return = " " ;
foreach ( $options as $k => $v ) {
if ( is_array ( $v )) {
2009-07-28 16:20:50 +00:00
$return .= '<optgroup label="' . h ( $k ) . '">' ;
2007-07-02 05:51:26 +00:00
}
2009-07-02 22:37:10 +00:00
foreach (( is_array ( $v ) ? $v : array ( $k => $v )) as $key => $val ) {
2009-07-28 16:28:47 +00:00
$return .= '<option' . ( $use_keys || is_string ( $key ) ? ' value="' . h ( $key ) . '"' : '' ) . (( $use_keys || is_string ( $key ) ? ( string ) $key : $val ) === $selected ? ' selected' : '' ) . '>' . h ( $val );
2007-07-02 05:51:26 +00:00
}
if ( is_array ( $v )) {
$return .= '</optgroup>' ;
}
}
return $return ;
}
2009-11-09 22:22:28 +00:00
/** Generate HTML radio list
* @ param string
* @ param array
* @ param string
* @ param string true for no onchange , false for radio
* @ return string
*/
function html_select ( $name , $options , $value = " " , $onchange = true ) {
if ( $onchange ) {
return " <select name=' " . h ( $name ) . " ' " . ( is_string ( $onchange ) ? " onchange= \" $onchange\ " " : " " ) . " > " . optionlist( $options , $value ) . " </ select > " ;
}
$return = " " ;
foreach ( $options as $key => $val ) {
$return .= " <label><input type='radio' name=' " . h ( $name ) . " ' value=' " . h ( $key ) . " ' " . ( $key == $value ? " checked " : " " ) . " > " . h ( $val ) . " </label> " ;
}
return $return ;
}
2010-04-26 16:22:24 +00:00
/** Print SQL < textarea > tag
* @ param string
* @ param int
* @ param int
* @ param string
* @ return null
*/
function textarea ( $name , $value , $rows = 10 , $cols = 80 ) {
echo " <textarea name=' $name ' rows=' $rows ' cols=' $cols ' style='width: 98%;' spellcheck='false'> " . h ( $value ) . " </textarea> " ; // spellcheck - not valid before HTML5
}
// other functions
2010-04-21 12:01:32 +00:00
/** Get INI boolean value
* @ param string
* @ return bool
*/
function ini_bool ( $ini ) {
$val = ini_get ( $ini );
return ( eregi ( '^(on|true|yes)$' , $val ) || ( int ) $val ); // boolean values set by php_value are strings
}
2009-08-31 10:11:52 +00:00
/** Get list of values from database
* @ param string
* @ param mixed
* @ return array
*/
2009-05-21 11:05:22 +00:00
function get_vals ( $query , $column = 0 ) {
2009-09-22 10:51:40 +00:00
global $connection ;
2007-07-11 22:04:08 +00:00
$return = array ();
2009-09-22 10:51:40 +00:00
$result = $connection -> query ( $query );
2010-04-21 23:06:32 +00:00
if ( is_object ( $result )) {
2008-09-30 12:00:18 +00:00
while ( $row = $result -> fetch_row ()) {
2009-05-21 11:05:22 +00:00
$return [] = $row [ $column ];
2008-09-30 12:00:18 +00:00
}
2007-07-11 22:04:08 +00:00
}
return $return ;
}
2010-04-21 12:01:32 +00:00
/** Get keys from first column and values from second
* @ param string
* @ param Min_DB
* @ return array
*/
function get_key_vals ( $query , $connection2 = null ) {
global $connection ;
if ( ! is_object ( $connection2 )) {
$connection2 = $connection ;
}
$return = array ();
$result = $connection2 -> query ( $query );
while ( $row = $result -> fetch_row ()) {
$return [ $row [ 0 ]] = $row [ 1 ];
}
return $return ;
}
2009-08-31 10:11:52 +00:00
/** Find unique identifier of a row
* @ param array
* @ param array result of indexes ()
2010-04-02 12:13:56 +00:00
* @ return array
2009-08-31 10:11:52 +00:00
*/
2010-04-02 12:13:56 +00:00
function unique_array ( $row , $indexes ) {
2007-07-05 04:39:15 +00:00
foreach ( $indexes as $index ) {
2009-10-07 15:55:47 +00:00
if ( ereg ( " PRIMARY|UNIQUE " , $index [ " type " ])) {
2007-07-05 04:39:15 +00:00
$return = array ();
foreach ( $index [ " columns " ] as $key ) {
2009-06-21 23:20:32 +00:00
if ( ! isset ( $row [ $key ])) { // NULL is ambiguous
2007-07-05 04:39:15 +00:00
continue 2 ;
2007-07-02 05:51:26 +00:00
}
2010-04-02 12:13:56 +00:00
$return [ $key ] = $row [ $key ];
2007-07-02 05:51:26 +00:00
}
2007-07-05 04:39:15 +00:00
return $return ;
2007-07-02 05:51:26 +00:00
}
}
$return = array ();
foreach ( $row as $key => $val ) {
2009-08-19 17:10:06 +00:00
if ( ! preg_match ( '~^(COUNT\\((\\*|(DISTINCT )?`(?:[^`]|``)+`)\\)|(AVG|GROUP_CONCAT|MAX|MIN|SUM)\\(`(?:[^`]|``)+`\\))$~' , $key )) { //! columns looking like functions
2010-04-02 12:13:56 +00:00
$return [ $key ] = $val ;
2009-07-28 12:56:38 +00:00
}
2007-07-09 06:12:22 +00:00
}
return $return ;
}
2009-08-31 10:11:52 +00:00
/** Create SQL condition from parsed query string
* @ param array parsed query string
* @ return string
*/
2008-08-08 11:56:37 +00:00
function where ( $where ) {
2007-07-09 06:12:22 +00:00
$return = array ();
2010-04-23 09:03:27 +00:00
foreach (( array ) $where [ " where " ] as $key => $val ) {
2010-04-26 16:22:58 +00:00
$return [] = idf_escape ( bracket_escape ( $key , 1 )) // 1 - back
. ( ereg ( '\\.' , $val ) ? " LIKE " . exact_value ( addcslashes ( $val , " %_ " )) : " = " . exact_value ( $val )) // LIKE because of floats, but slow with ints
; //! enum and set
2010-04-23 09:03:27 +00:00
}
foreach (( array ) $where [ " null " ] as $key ) {
$return [] = idf_escape ( $key ) . " IS NULL " ;
2007-07-02 05:51:26 +00:00
}
2009-07-11 19:45:57 +00:00
return implode ( " AND " , $return );
2007-07-02 05:51:26 +00:00
}
2009-08-31 10:11:52 +00:00
/** Create SQL condition from query string
* @ param string
* @ return string
*/
2009-06-07 23:53:51 +00:00
function where_check ( $val ) {
parse_str ( $val , $check );
return where ( $check );
}
2009-08-31 10:11:52 +00:00
/** Create query string where condition from value
* @ param int condition order
* @ param string column identifier
* @ param string
* @ return string
2010-04-15 14:43:44 +00:00
* @ return string
2009-08-31 10:11:52 +00:00
*/
2010-04-15 14:43:44 +00:00
function where_link ( $i , $column , $value , $operator = " = " ) {
return " &where%5B $i %5D%5Bcol%5D= " . urlencode ( $column ) . " &where%5B $i %5D%5Bop%5D= " . urlencode ( $operator ) . " &where%5B $i %5D%5Bval%5D= " . urlencode ( $value );
2009-07-15 14:56:27 +00:00
}
2009-09-09 20:49:30 +00:00
/** Set cookie valid for 1 month
* @ param string
* @ param string
* @ return bool
*/
function cookie ( $name , $value ) {
2010-04-21 12:01:32 +00:00
$params = array (
$name ,
( ereg ( " \n " , $value ) ? " " : $value ), // HTTP Response Splitting protection in PHP < 5.1.2
time () + 2592000 , // 2592000 - 30 days
preg_replace ( '~\\?.*~' , '' , $_SERVER [ " REQUEST_URI " ]),
" " ,
$_SERVER [ " HTTPS " ] && strcasecmp ( $_SERVER [ " HTTPS " ], " off " )
);
2009-12-18 17:59:58 +00:00
if ( version_compare ( PHP_VERSION , '5.2.0' ) >= 0 ) {
$params [] = true ; // HttpOnly
}
return call_user_func_array ( 'setcookie' , $params );
2009-09-09 20:49:30 +00:00
}
2009-11-02 22:09:23 +00:00
/** Restart stopped session
* @ return null
*/
function restart_session () {
2010-04-21 12:01:32 +00:00
if ( ! ini_bool ( " session.use_cookies " )) {
2009-11-02 22:09:23 +00:00
session_start ();
}
}
2010-04-21 12:01:32 +00:00
/** Get session variable for current server
* @ param string
* @ return mixed
*/
function & get_session ( $key ) {
return $_SESSION [ $key ][ DRIVER ][ SERVER ][ $_GET [ " username " ]];
}
/** Set session variable for current server
* @ param string
* @ param mixed
* @ return mixed
*/
function set_session ( $key , $val ) {
$_SESSION [ $key ][ DRIVER ][ SERVER ][ $_GET [ " username " ]] = $val ; // used also in auth.inc.php
}
2010-05-06 12:21:22 +00:00
/** Get authenticated URL
* @ param string
* @ param string
* @ param string
* @ return string
*/
function auth_url ( $driver , $server , $username ) {
global $drivers ;
preg_match ( '~([^?]*)\\??(.*)~' , remove_from_uri ( implode ( " | " , array_keys ( $drivers )) . " |username| " . session_name ()), $match );
return " $match[1] ? "
. ( SID ? SID . " & " : " " )
. ( $driver != " server " || $server != " " ? urlencode ( $driver ) . " = " . urlencode ( $server ) . " & " : " " )
. " username= " . urlencode ( $username )
. ( $match [ 2 ] ? " & $match[2] " : " " )
;
}
2009-08-31 10:11:52 +00:00
/** Send Location header and exit
2010-04-15 14:59:40 +00:00
* @ param string null to only set a message
2009-08-31 10:11:52 +00:00
* @ param string
* @ return null
*/
2007-07-09 06:12:22 +00:00
function redirect ( $location , $message = null ) {
if ( isset ( $message )) {
2009-11-03 10:55:57 +00:00
restart_session ();
2008-04-18 12:01:53 +00:00
$_SESSION [ " messages " ][] = $message ;
2007-07-09 06:12:22 +00:00
}
2010-04-15 14:59:40 +00:00
if ( isset ( $location )) {
header ( " Location: " . ( $location != " " ? $location : " . " ));
exit ;
}
2007-07-09 06:12:22 +00:00
}
2009-08-31 10:11:52 +00:00
/** Execute query and redirect if successful
* @ param string
* @ param string
* @ param string
* @ param bool
* @ param bool
* @ param bool
* @ return bool
*/
2008-09-03 13:55:43 +00:00
function query_redirect ( $query , $location , $message , $redirect = true , $execute = true , $failed = false ) {
2009-09-22 10:51:40 +00:00
global $connection , $error , $adminer ;
2009-11-02 22:09:23 +00:00
if ( $execute ) {
$failed = ! $connection -> query ( $query );
}
2009-06-04 14:00:23 +00:00
$sql = " " ;
if ( $query ) {
2009-07-27 11:25:37 +00:00
$sql = $adminer -> messageQuery ( $query );
2009-06-04 14:00:23 +00:00
}
2008-09-03 13:55:43 +00:00
if ( $failed ) {
2009-12-17 15:31:24 +00:00
$error = error () . $sql ;
2008-09-02 12:39:04 +00:00
return false ;
2008-09-02 08:42:14 +00:00
}
2008-09-02 12:39:04 +00:00
if ( $redirect ) {
redirect ( $location , $message . $sql );
}
return true ;
2008-09-02 08:42:14 +00:00
}
2009-08-31 10:11:52 +00:00
/** Execute and remember query
* @ param string null to return remembered queries
* @ return Min_Result
*/
2008-09-03 13:55:43 +00:00
function queries ( $query = null ) {
2009-09-22 10:51:40 +00:00
global $connection ;
2008-09-03 13:55:43 +00:00
static $queries = array ();
if ( ! isset ( $query )) {
2009-06-21 23:20:32 +00:00
// return executed queries without parameter
2008-09-25 14:27:19 +00:00
return implode ( " ; \n " , $queries );
2008-09-03 13:55:43 +00:00
}
$queries [] = $query ;
2009-09-22 10:51:40 +00:00
return $connection -> query ( $query );
2008-09-03 13:55:43 +00:00
}
2009-10-13 20:01:52 +00:00
/** Redirect by remembered queries
* @ param string
* @ param string
* @ param bool
*/
function queries_redirect ( $location , $message , $redirect ) {
return query_redirect ( queries (), $location , $message , $redirect , false , ! $redirect );
}
2009-08-31 10:11:52 +00:00
/** Remove parameter from query string
* @ param string
* @ return string
*/
2007-07-27 11:43:44 +00:00
function remove_from_uri ( $param = " " ) {
2009-12-08 22:57:39 +00:00
return substr ( preg_replace ( " ~(?<=[?&])( $param " . ( SID ? " " : " | " . session_name ()) . " )=[^&]*&~ " , '' , " $_SERVER[REQUEST_URI] & " ), 0 , - 1 );
2007-07-27 11:43:44 +00:00
}
2009-08-31 10:11:52 +00:00
/** Generate page number for pagination
* @ param int
* @ return string
*/
2010-04-22 12:37:56 +00:00
function pagination ( $page , $current ) {
return " " . ( $page == $current ? $page + 1 : '<a href="' . h ( remove_from_uri ( " page " ) . ( $page ? " &page= $page " : " " )) . '">' . ( $page + 1 ) . " </a> " );
2008-08-07 08:02:54 +00:00
}
2009-11-21 09:08:52 +00:00
/** Get file contents from $_FILES
2009-08-31 10:11:52 +00:00
* @ param string
* @ param bool
2009-11-21 09:08:52 +00:00
* @ return mixed int for error , string otherwise
2009-08-31 10:11:52 +00:00
*/
2009-08-28 11:49:57 +00:00
function get_file ( $key , $decompress = false ) {
$file = $_FILES [ $key ];
if ( ! $file || $file [ " error " ]) {
return $file [ " error " ];
2007-07-09 06:12:22 +00:00
}
2009-08-29 13:50:28 +00:00
return file_get_contents ( $decompress && ereg ( '\\.gz$' , $file [ " name " ]) ? " compress.zlib:// $file[tmp_name] "
: ( $decompress && ereg ( '\\.bz2$' , $file [ " name " ]) ? " compress.bzip2:// $file[tmp_name] "
: $file [ " tmp_name " ]
)); //! may not be reachable because of open_basedir
2007-07-09 06:12:22 +00:00
}
2009-08-31 10:11:52 +00:00
/** Determine upload error
* @ param int
* @ return string
*/
2009-07-16 13:46:49 +00:00
function upload_error ( $error ) {
$max_size = ( $error == UPLOAD_ERR_INI_SIZE ? ini_get ( " upload_max_filesize " ) : null ); // post_max_size is checked in index.php
2009-08-25 15:58:04 +00:00
return ( $error ? lang ( 'Unable to upload a file.' ) . ( $max_size ? " " . lang ( 'Maximum allowed file size is %sB.' , $max_size ) : " " ) : lang ( 'File does not exist.' ));
2009-07-16 13:46:49 +00:00
}
2009-08-31 10:11:52 +00:00
/** Generate class for odd rows
* @ param string return this for odd rows , empty to reset counter
* @ return string
*/
function odd ( $return = ' class="odd"' ) {
2009-04-30 15:21:31 +00:00
static $i = 0 ;
2009-08-31 10:11:52 +00:00
if ( ! $return ) { // reset counter
2009-05-18 10:27:49 +00:00
$i = - 1 ;
2009-04-30 15:21:31 +00:00
}
2009-08-31 10:11:52 +00:00
return ( $i ++ % 2 ? $return : '' );
2009-04-30 15:21:31 +00:00
}
2009-08-31 10:11:52 +00:00
/** Check whether the string is in UTF - 8
* @ param string
* @ return bool
*/
2009-06-11 05:05:35 +00:00
function is_utf8 ( $val ) {
2009-06-21 23:20:32 +00:00
// don't print control chars except \t\r\n
2009-06-11 05:05:35 +00:00
return ( preg_match ( '~~u' , $val ) && ! preg_match ( '~[\\0-\\x8\\xB\\xC\\xE-\\x1F]~' , $val ));
}
2009-08-31 10:11:52 +00:00
/** Shorten UTF - 8 string
* @ param string
* @ param int
* @ param string
* @ return string escaped string with appended ...
*/
2009-06-15 09:07:05 +00:00
function shorten_utf8 ( $string , $length = 80 , $suffix = " " ) {
2009-09-24 10:07:06 +00:00
if ( ! preg_match ( " (^([ \t \r \n - \ x { FFFF}] { 0, $length })( $ )?)u " , $string , $match )) { // ~s causes trash in $match[2] under some PHP versions, (.|\n) is slow
preg_match ( " (^([ \t \r \n -~] { 0, $length })( $ )?) " , $string , $match );
2009-08-25 14:17:58 +00:00
}
2009-09-24 10:07:06 +00:00
return h ( $match [ 1 ]) . $suffix . ( isset ( $match [ 2 ]) ? " " : " <em>...</em> " );
2007-08-24 16:00:10 +00:00
}
2008-10-03 12:40:20 +00:00
2009-08-31 10:11:52 +00:00
/** Generate friendly URL
* @ param string
* @ return string
*/
2009-06-11 05:18:40 +00:00
function friendly_url ( $val ) {
2009-06-21 23:20:32 +00:00
// used for blobs and export
2009-06-11 05:18:40 +00:00
return preg_replace ( '~[^a-z0-9_]~i' , '-' , $val );
}
2009-08-31 10:11:52 +00:00
/** Print hidden fields
* @ param array
* @ param array
* @ return null
*/
2008-10-09 13:23:54 +00:00
function hidden_fields ( $process , $ignore = array ()) {
2008-10-03 13:15:58 +00:00
while ( list ( $key , $val ) = each ( $process )) {
if ( is_array ( $val )) {
foreach ( $val as $k => $v ) {
$process [ $key . " [ $k ] " ] = $v ;
}
} elseif ( ! in_array ( $key , $ignore )) {
2009-07-28 16:20:50 +00:00
echo '<input type="hidden" name="' . h ( $key ) . '" value="' . h ( $val ) . '">' ;
2008-10-03 13:15:58 +00:00
}
}
}
2009-07-03 09:16:50 +00:00
2010-04-21 12:01:32 +00:00
/** Print hidden fields for GET forms
* @ return null
*/
function hidden_fields_get () {
echo ( SID && ! $_COOKIE ? '<input type="hidden" name="' . session_name () . '" value="' . h ( session_id ()) . '">' : '' );
echo ( SERVER !== null ? '<input type="hidden" name="' . DRIVER . '" value="' . h ( SERVER ) . '">' : " " );
echo '<input type="hidden" name="username" value="' . h ( $_GET [ " username " ]) . '">' ;
}
2009-08-31 10:11:52 +00:00
/** Find out foreign keys for each column
* @ param string
* @ return array array ( $col => array ())
*/
2009-07-23 14:42:38 +00:00
function column_foreign_keys ( $table ) {
$return = array ();
foreach ( foreign_keys ( $table ) as $foreign_key ) {
foreach ( $foreign_key [ " source " ] as $val ) {
$return [ $val ][] = $foreign_key ;
}
}
return $return ;
}
2010-04-21 12:01:32 +00:00
/** Print enum input field
* @ param string " radio " | " checkbox "
* @ param string
* @ param array
* @ param mixed int | string | array
* @ return null
*/
function enum_input ( $type , $name , $field , $value ) {
preg_match_all ( " ~'((?:[^']|'')*)'~ " , $field [ " length " ], $matches );
foreach ( $matches [ 1 ] as $i => $val ) {
$val = stripcslashes ( str_replace ( " '' " , " ' " , $val ));
$checked = ( is_int ( $value ) ? $value == $i + 1 : ( is_array ( $value ) ? in_array ( $i + 1 , $value ) : $value === $val ));
echo " <label><input type=' $type ' name=' $name ' value=' " . ( $i + 1 ) . " ' " . ( $checked ? ' checked' : '' ) . '>' . h ( $val ) . '</label>' ;
}
}
2009-08-31 10:11:52 +00:00
/** Print edit input field
* @ param array one field from fields ()
* @ param mixed
* @ param string
* @ return null
*/
2009-07-23 14:42:38 +00:00
function input ( $field , $value , $function ) {
2010-05-07 13:44:22 +00:00
global $types , $adminer , $jush ;
2009-07-28 16:20:50 +00:00
$name = h ( bracket_escape ( $field [ " field " ]));
2009-07-03 09:16:50 +00:00
echo " <td class='function'> " ;
2009-10-09 16:19:40 +00:00
$functions = ( isset ( $_GET [ " select " ]) ? array ( " orig " => lang ( 'original' )) : array ()) + $adminer -> editFunctions ( $field );
2009-07-03 09:16:50 +00:00
if ( $field [ " type " ] == " enum " ) {
2009-11-12 15:25:21 +00:00
echo nbsp ( $functions [ " " ]) . " <td> " . ( $functions [ " orig " ] ? " <label><input type='radio' name='fields[ $name ]' value='-1' checked><em> $functions[orig] </em></label> " : " " );
echo $adminer -> editInput ( $_GET [ " edit " ], $field , " name='fields[ $name ]' " , $value );
2010-04-21 12:01:32 +00:00
enum_input ( " radio " , " fields[ $name ] " , $field , $value );
2009-07-03 09:16:50 +00:00
} else {
2009-09-08 08:13:39 +00:00
$first = 0 ;
foreach ( $functions as $key => $val ) {
if ( $key === " " || ! $val ) {
break ;
}
$first ++ ;
}
2009-07-28 16:20:50 +00:00
$onchange = ( $first ? " onchange= \" var f = this.form['function[ " . addcslashes ( $name , " \r \n ' \\ " ) . " ]']; if ( $first > f.selectedIndex) f.selectedIndex = $first ; \" " : " " );
2009-11-09 14:47:47 +00:00
$attrs = " name='fields[ $name ]' $onchange " ;
2009-10-02 13:21:34 +00:00
echo ( count ( $functions ) > 1 ? html_select ( " function[ $name ] " , $functions , ! isset ( $function ) || in_array ( $function , $functions ) ? $function : " " ) : nbsp ( reset ( $functions ))) . '<td>' ;
2009-11-09 14:47:47 +00:00
$input = $adminer -> editInput ( $_GET [ " edit " ], $field , $attrs , $value ); // usage in call is without a table
2010-01-09 23:33:41 +00:00
if ( $input != " " ) {
2009-07-28 10:09:05 +00:00
echo $input ;
2009-07-23 14:42:38 +00:00
} elseif ( $field [ " type " ] == " set " ) { //! 64 bits
2009-08-19 17:10:06 +00:00
preg_match_all ( " ~'((?:[^']|'')*)'~ " , $field [ " length " ], $matches );
2009-07-03 09:16:50 +00:00
foreach ( $matches [ 1 ] as $i => $val ) {
$val = stripcslashes ( str_replace ( " '' " , " ' " , $val ));
$checked = ( is_int ( $value ) ? ( $value >> $i ) & 1 : in_array ( $val , explode ( " , " , $value ), true ));
2009-08-21 16:54:10 +00:00
echo " <label><input type='checkbox' name='fields[ $name ][ $i ]' value=' " . ( 1 << $i ) . " ' " . ( $checked ? ' checked' : '' ) . " $onchange > " . h ( $val ) . '</label>' ;
2009-07-03 09:16:50 +00:00
}
2010-04-21 12:01:32 +00:00
} elseif ( ereg ( 'binary|blob|bytea' , $field [ " type " ]) && ini_bool ( " file_uploads " )) {
2009-11-09 14:47:47 +00:00
echo " <input type='file' name='fields- $name ' $onchange > " ;
2009-11-09 14:34:13 +00:00
} elseif ( ereg ( 'text|blob' , $field [ " type " ])) {
2010-05-07 13:44:22 +00:00
echo " <textarea " . ( $jush != " sqlite " || ereg ( " \n " , $value ) ? " cols='50' rows='12' " : " cols='30' rows='1' style='height: 1.2em;' " ) . " $attrs > " . h ( $value ) . '</textarea>' ; // 1.2em - line-height
2009-07-03 09:16:50 +00:00
} else {
// int(3) is only a display hint
$maxlength = ( ! ereg ( 'int' , $field [ " type " ]) && preg_match ( '~^([0-9]+)(,([0-9]+))?$~' , $field [ " length " ], $match ) ? ( $match [ 1 ] + ( $match [ 3 ] ? 1 : 0 ) + ( $match [ 2 ] && ! $field [ " unsigned " ] ? 1 : 0 )) : ( $types [ $field [ " type " ]] ? $types [ $field [ " type " ]] + ( $field [ " unsigned " ] ? 0 : 1 ) : 0 ));
2009-11-09 14:47:47 +00:00
echo " <input value=' " . h ( $value ) . " ' " . ( $maxlength ? " maxlength=' $maxlength ' " : " " ) . ( ereg ( 'char' , $field [ " type " ]) && $field [ " length " ] > 20 ? " size='40' " : " " ) . " $attrs > " ;
2009-07-03 09:16:50 +00:00
}
}
}
2009-08-31 10:11:52 +00:00
/** Process edit input field
* @ param one field from fields ()
* @ return string
*/
2009-07-27 16:32:56 +00:00
function process_input ( $field ) {
2009-09-22 10:51:40 +00:00
global $connection , $adminer ;
2009-07-27 16:32:56 +00:00
$idf = bracket_escape ( $field [ " field " ]);
2009-07-03 09:16:50 +00:00
$function = $_POST [ " function " ][ $idf ];
$value = $_POST [ " fields " ][ $idf ];
2010-04-21 12:01:32 +00:00
if ( $field [ " type " ] == " enum " ) {
if ( $value == - 1 ) {
return false ;
}
if ( $value == " " ) {
return " NULL " ;
}
return intval ( $value );
}
if ( $field [ " auto_increment " ] && $value == " " ) {
return null ;
}
if ( $function == " orig " ) {
2009-07-03 09:16:50 +00:00
return false ;
2010-04-21 12:01:32 +00:00
}
if ( $function == " NULL " ) {
2009-07-03 09:16:50 +00:00
return " NULL " ;
2010-04-21 12:01:32 +00:00
}
if ( $field [ " type " ] == " set " ) {
2009-08-21 16:54:10 +00:00
return array_sum (( array ) $value );
2010-04-21 12:01:32 +00:00
}
if ( ereg ( 'binary|blob|bytea' , $field [ " type " ]) && ini_bool ( " file_uploads " )) {
2009-11-09 14:47:47 +00:00
$file = get_file ( " fields- $idf " );
2009-07-03 09:16:50 +00:00
if ( ! is_string ( $file )) {
return false ; //! report errors
}
2009-11-09 14:30:57 +00:00
return $connection -> quote ( $file );
2009-07-03 09:16:50 +00:00
}
2010-04-21 12:01:32 +00:00
return $adminer -> processInput ( $field , $value , $function );
2009-07-03 09:16:50 +00:00
}
2010-02-16 14:35:32 +00:00
/** Print results of search in all tables
* @ uses $_GET [ " where " ][ 0 ]
* @ uses $_POST [ " tables " ]
* @ return null
*/
function search_tables () {
global $adminer , $connection ;
$found = false ;
foreach ( table_status () as $table => $table_status ) {
$name = $adminer -> tableName ( $table_status );
if ( isset ( $table_status [ " Engine " ]) && $name != " " && ( ! $_POST [ " tables " ] || in_array ( $table , $_POST [ " tables " ]))) {
2010-04-21 12:01:32 +00:00
$result = $connection -> query ( " SELECT " . limit ( " 1 FROM " . idf_escape ( $table ) . " WHERE " . implode ( " AND " , $adminer -> selectSearchProcess ( fields ( $table ), array ())), 1 ));
2010-02-16 14:35:32 +00:00
if ( $result -> num_rows ) {
if ( ! $found ) {
echo " <ul> \n " ;
$found = true ;
}
echo " <li><a href=' " . h ( ME . " select= " . urlencode ( $table ) . " &where[0][op]= " . urlencode ( $_GET [ " where " ][ 0 ][ " op " ]) . " &where[0][val]= " . urlencode ( $_GET [ " where " ][ 0 ][ " val " ])) . " '> " . h ( $name ) . " </a> \n " ;
}
}
}
echo ( $found ? " </ul> " : " <p class='message'> " . lang ( 'No tables.' )) . " \n " ;
}
2009-08-31 10:11:52 +00:00
/** Print CSV row
* @ param array
* @ return null
*/
2009-07-03 09:16:50 +00:00
function dump_csv ( $row ) {
foreach ( $row as $key => $val ) {
2010-04-21 12:01:32 +00:00
if ( preg_match ( " ~[ \" \n ,;]~ " , $val ) || $val === " " ) {
2009-07-03 09:16:50 +00:00
$row [ $key ] = '"' . str_replace ( '"' , '""' , $val ) . '"' ;
}
}
2010-04-22 23:03:54 +00:00
echo implode (( $_POST [ " format " ] == " csv " ? " , " : " ; " ), $row ) . " \n " ;
2009-07-03 09:16:50 +00:00
}
2009-07-11 19:45:57 +00:00
2009-08-31 10:11:52 +00:00
/** Apply SQL function
* @ param string
* @ param string escaped column identifier
* @ return string
*/
2009-08-18 09:19:54 +00:00
function apply_sql_function ( $function , $column ) {
2010-04-21 12:01:32 +00:00
return ( $function ? ( $function == " unixepoch " ? " DATETIME( $column , ' $function ') " : ( $function == " count distinct " ? " COUNT(DISTINCT " : strtoupper ( " $function ( " )) . " $column ) " ) : $column );
2009-08-18 09:19:54 +00:00
}
2010-05-06 13:45:34 +00:00
/** Read password from file adminer . key in temporary directory or create one
* @ return string or false if the file can not be created
*/
function password_file () {
$dir = ini_get ( " upload_tmp_dir " ); // session_save_path() may contain other storage path
if ( ! $dir ) {
if ( function_exists ( 'sys_get_temp_dir' )) {
$dir = sys_get_temp_dir ();
} else {
$filename = @ tempnam ( " " , " " ); // @ - temp directory can be disabled by open_basedir
if ( ! $filename ) {
return false ;
}
$dir = dirname ( $filename );
unlink ( $filename );
}
}
$filename = " $dir /adminer.key " ;
$return = @ file_get_contents ( $filename ); // @ - can not exist
if ( $return ) {
return $return ;
}
$fp = @ fopen ( $filename , " w " ); // @ - can have insufficient rights //! is not atomic
if ( $fp ) {
$return = md5 ( uniqid ( mt_rand (), true ));
fwrite ( $fp , $return );
fclose ( $fp );
}
return $return ;
}
2009-08-31 10:11:52 +00:00
/** Check whether the string is e - mail address
* @ param string
* @ return bool
*/
2009-07-11 19:45:57 +00:00
function is_email ( $email ) {
$atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]' ; // characters of local-name
$domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])' ; // one domain component
2010-04-21 22:21:47 +00:00
$pattern = " $atom +( \\ . $atom +)*@( $domain ? \\ .)+ $domain " ;
return preg_match ( " (^ $pattern (, \\ s* $pattern )* \$ )i " , $email );
2009-07-11 19:45:57 +00:00
}
2009-09-11 19:26:27 +00:00
2009-10-02 14:25:18 +00:00
/** Check whether the string is URL address
* @ param string
* @ return bool
*/
function is_url ( $string ) {
$domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])' ; // one domain component
return preg_match ( " ~^https?://( $domain ? \\ .)+ $domain (:[0-9]+)?(/.*)?( \\ ?.*)?(#.*)? \$ ~i " , $string ); //! restrict path, query and fragment characters
}
2009-09-11 19:26:27 +00:00
/** Print header for hidden fieldset ( close by </ div ></ fieldset > )
* @ param string
* @ param string
* @ param bool
* @ return null
*/
function print_fieldset ( $id , $legend , $visible = false ) {
echo " <fieldset><legend><a href='#fieldset- $id ' onclick= \" return !toggle('fieldset- $id '); \" > $legend </a></legend><div id='fieldset- $id ' " . ( $visible ? " " : " class='hidden' " ) . " > \n " ;
}
2009-11-26 12:14:39 +00:00
/** Enclose $string to < b > if $bold is true
* @ param string
* @ param bool
* @ return string
*/
function bold ( $string , $bold ) {
return ( $bold ? " <b> $string </b> " : $string );
}