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
}
2011-08-10 16:10:23 +00:00
/** Get Adminer object
* @ return Adminer
*/
function adminer () {
global $adminer ;
return $adminer ;
}
2018-02-07 11:49:53 +00:00
/** Get Adminer version
* @ return string
*/
function version () {
global $VERSION ;
return $VERSION ;
}
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 ) {
2010-10-13 16:04:40 +00:00
return substr ( q ( $val ), 1 , - 1 );
2009-10-16 12:26:16 +00:00
}
2014-11-25 02:17:43 +00:00
/** Remove non - digits from a string
* @ param string
* @ return string
*/
function number ( $val ) {
2018-01-22 12:51:47 +00:00
return preg_replace ( '~[^0-9]+~' , '' , $val );
2014-11-25 02:17:43 +00:00
}
2018-02-06 11:17:01 +00:00
/** Get regular expression to match numeric types
* @ return string
*/
function number_type () {
2018-02-06 13:52:12 +00:00
return '((?<!o)int(?!er)|numeric|real|float|double|decimal|money)' ; // not point, not interval
2018-02-06 11:17:01 +00:00
}
2010-07-26 12:47:26 +00:00
/** Disable magic_quotes_gpc
* @ param array e . g . ( & $_GET , & $_POST , & $_COOKIE )
2011-07-22 11:37:01 +00:00
* @ param bool whether to leave values as is
2010-07-26 12:47:26 +00:00
* @ return null modified in place
*/
2011-07-22 11:37:01 +00:00
function remove_slashes ( $process , $filter = false ) {
2010-07-26 12:47:26 +00:00
if ( get_magic_quotes_gpc ()) {
while ( list ( $key , $val ) = each ( $process )) {
foreach ( $val as $k => $v ) {
unset ( $process [ $key ][ $k ]);
if ( is_array ( $v )) {
$process [ $key ][ stripslashes ( $k )] = $v ;
$process [] = & $process [ $key ][ stripslashes ( $k )];
} else {
$process [ $key ][ stripslashes ( $k )] = ( $filter ? $v : stripslashes ( $v ));
}
}
}
}
}
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[]"
2017-01-23 21:30:05 +00:00
static $trans = array ( ':' => ':1' , ']' => ':2' , '[' => ':3' , '"' => ':4' );
2007-07-02 05:51:26 +00:00
return strtr ( $idf , ( $back ? array_flip ( $trans ) : $trans ));
}
2018-01-29 18:34:26 +00:00
/** Check if connection has at least the given version
* @ param string required version
* @ param string required MariaDB version
2018-01-29 20:08:38 +00:00
* @ param Min_DB defaults to $connection
2018-01-29 18:34:26 +00:00
* @ return bool
*/
2018-01-29 20:08:38 +00:00
function min_version ( $version , $maria_db = " " , $connection2 = null ) {
2018-01-29 18:34:26 +00:00
global $connection ;
2018-01-29 20:08:38 +00:00
if ( ! $connection2 ) {
$connection2 = $connection ;
}
$server_info = $connection2 -> server_info ;
2018-01-29 18:34:26 +00:00
if ( $maria_db && preg_match ( '~([\d.]+)-MariaDB~' , $server_info , $match )) {
$server_info = $match [ 1 ];
$version = $maria_db ;
}
return ( version_compare ( $server_info , $version ) >= 0 );
}
2014-06-26 11:22:35 +00:00
/** Get connection charset
* @ param Min_DB
* @ return string
*/
function charset ( $connection ) {
2018-01-30 22:53:04 +00:00
return ( min_version ( " 5.5.3 " , 0 , $connection ) ? " utf8mb4 " : " utf8 " ); // SHOW CHARSET would require an extra query
2014-06-26 11:22:35 +00:00
}
2018-01-12 14:27:44 +00:00
/** Return < script > element
* @ param string
* @ param string
* @ return string
*/
function script ( $source , $trailing = " \n " ) {
2018-01-13 21:17:00 +00:00
return " <script " . nonce () . " > $source </script> $trailing " ;
2018-01-12 14:27:44 +00:00
}
2018-01-13 15:25:11 +00:00
/** Return < script src > element
* @ param string
* @ return string
*/
function script_src ( $url ) {
2018-01-13 21:17:00 +00:00
return " <script src=' " . h ( $url ) . " ' " . nonce () . " ></script> \n " ;
}
/** Get a nonce = " " attribute with CSP nonce
* @ return string
*/
function nonce () {
return ' nonce="' . get_nonce () . '"' ;
2018-01-13 15:25:11 +00:00
}
2018-01-23 11:53:22 +00:00
/** Get a target = " _blank " attribute
* @ return string
*/
function target_blank () {
2018-01-30 18:12:49 +00:00
return ' target="_blank" rel="noreferrer noopener"' ;
2018-01-23 11:53:22 +00:00
}
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 ) {
2014-09-11 15:48:17 +00:00
return str_replace ( " \0 " , " � " , htmlspecialchars ( $string , ENT_QUOTES , 'utf-8' ));
2009-07-28 16:20:50 +00:00
}
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
2013-05-20 17:06:23 +00:00
* @ param string
2017-01-23 22:29:00 +00:00
* @ param string
2016-09-19 13:27:34 +00:00
* @ return string
2009-09-25 12:03:30 +00:00
*/
2016-09-19 13:27:34 +00:00
function checkbox ( $name , $value , $checked , $label = " " , $onclick = " " , $class = " " , $labelled_by = " " ) {
2013-05-20 17:06:23 +00:00
$return = " <input type='checkbox' name=' $name ' value=' " . h ( $value ) . " ' "
. ( $checked ? " checked " : " " )
2016-09-19 13:27:34 +00:00
. ( $labelled_by ? " aria-labelledby=' $labelled_by ' " : " " )
2013-06-03 15:56:18 +00:00
. " > "
2018-01-12 14:27:44 +00:00
. ( $onclick ? script ( " qsl('input').onclick = function () { $onclick }; " , " " ) : " " )
2013-05-20 17:06:23 +00:00
;
2013-06-03 15:56:18 +00:00
return ( $label != " " || $class ? " <label " . ( $class ? " class=' $class ' " : " " ) . " > $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 ) {
2011-03-21 08:53:41 +00:00
$opts = array ( $k => $v );
2007-07-02 05:51:26 +00:00
if ( is_array ( $v )) {
2009-07-28 16:20:50 +00:00
$return .= '<optgroup label="' . h ( $k ) . '">' ;
2011-03-21 08:53:41 +00:00
$opts = $v ;
2007-07-02 05:51:26 +00:00
}
2011-03-21 08:53:41 +00:00
foreach ( $opts 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
2017-01-23 22:29:00 +00:00
* @ param string
2009-11-09 22:22:28 +00:00
* @ return string
*/
2017-01-23 22:29:00 +00:00
function html_select ( $name , $options , $value = " " , $onchange = true , $labelled_by = " " ) {
2009-11-09 22:22:28 +00:00
if ( $onchange ) {
2017-01-23 22:29:00 +00:00
return " <select name=' " . h ( $name ) . " ' "
. ( $labelled_by ? " aria-labelledby=' $labelled_by ' " : " " )
2018-01-12 13:35:22 +00:00
. " > " . optionlist ( $options , $value ) . " </select> "
2018-01-12 14:27:44 +00:00
. ( is_string ( $onchange ) ? script ( " qsl('select').onchange = function () { $onchange }; " , " " ) : " " )
2018-01-12 13:35:22 +00:00
;
2009-11-09 22:22:28 +00:00
}
$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 ;
}
2013-07-06 17:31:21 +00:00
/** Generate HTML < select > or < input > if $options are empty
2018-01-11 14:14:05 +00:00
* @ param string
* @ param array
* @ param string
* @ param string
2018-01-12 11:42:30 +00:00
* @ param string
2018-01-11 14:14:05 +00:00
* @ return string
*/
2018-01-12 11:42:30 +00:00
function select_input ( $attrs , $options , $value = " " , $onchange = " " , $placeholder = " " ) {
$tag = ( $options ? " select " : " input " );
return " < $tag $attrs " . ( $options
? " ><option value=''> $placeholder " . optionlist ( $options , $value , true ) . " </select> "
: " size='10' value=' " . h ( $value ) . " ' placeholder=' $placeholder '> "
2018-01-12 21:18:54 +00:00
) . ( $onchange ? script ( " qsl(' $tag ').onchange = $onchange ; " , " " ) : " " ); //! use oninput for input
2013-07-06 17:31:21 +00:00
}
2010-10-18 21:57:39 +00:00
/** Get onclick confirmation
2018-01-12 13:43:55 +00:00
* @ param string
2018-01-24 17:13:05 +00:00
* @ param string
2010-10-18 21:57:39 +00:00
* @ return string
*/
2018-01-24 17:13:05 +00:00
function confirm ( $message = " " , $selector = " qsl('input') " ) {
2018-01-24 17:36:19 +00:00
return script ( " $selector .onclick = function () { return confirm(' " . ( $message ? js_escape ( $message ) : lang ( 'Are you sure?' )) . " '); }; " , " " );
2010-10-18 21:57:39 +00:00
}
2011-07-27 07:04:43 +00:00
/** Print header for hidden fieldset ( close by </ div ></ fieldset > )
* @ param string
* @ param string
* @ param bool
* @ return null
*/
2018-01-12 13:49:55 +00:00
function print_fieldset ( $id , $legend , $visible = false ) {
echo " <fieldset><legend> " ;
echo " <a href='#fieldset- $id '> $legend </a> " ;
2018-01-12 14:27:44 +00:00
echo script ( " qsl('a').onclick = partial(toggle, 'fieldset- $id '); " , " " );
2018-01-12 13:49:55 +00:00
echo " </legend> " ;
echo " <div id='fieldset- $id ' " . ( $visible ? " " : " class='hidden' " ) . " > \n " ;
2011-07-27 07:04:43 +00:00
}
/** Return class = 'active' if $bold is true
* @ param bool
2013-10-25 05:04:57 +00:00
* @ param string
2011-07-27 07:04:43 +00:00
* @ return string
*/
2013-10-25 05:04:57 +00:00
function bold ( $bold , $class = " " ) {
return ( $bold ? " class='active $class ' " : ( $class ? " class=' $class ' " : " " ));
2011-07-27 07:04:43 +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"' ) {
static $i = 0 ;
if ( ! $return ) { // reset counter
$i = - 1 ;
}
return ( $i ++ % 2 ? $return : '' );
}
2010-10-22 21:07:30 +00:00
/** Escape string for JavaScript apostrophes
* @ param string
* @ return string
*/
function js_escape ( $string ) {
return addcslashes ( $string , " \r \n ' \\ / " ); // slash for <script>
}
2011-07-27 07:04:43 +00:00
/** Print one row in JSON object
* @ param string or " " to close the object
* @ param string
* @ return null
*/
function json_row ( $key , $val = null ) {
static $first = true ;
if ( $first ) {
echo " { " ;
}
if ( $key != " " ) {
2016-07-07 12:34:59 +00:00
echo ( $first ? " " : " , " ) . " \n \t \" " . addcslashes ( $key , " \r \n \t \" \\ / " ) . '": ' . ( $val !== null ? '"' . addcslashes ( $val , " \r \n \" \\ / " ) . '"' : 'null' );
2011-07-27 07:04:43 +00:00
$first = false ;
} else {
echo " \n } \n " ;
$first = true ;
}
}
2010-04-21 12:01:32 +00:00
/** Get INI boolean value
* @ param string
* @ return bool
*/
function ini_bool ( $ini ) {
$val = ini_get ( $ini );
2013-07-24 23:26:41 +00:00
return ( preg_match ( '~^(on|true|yes)$~i' , $val ) || ( int ) $val ); // boolean values set by php_value are strings
2010-04-21 12:01:32 +00:00
}
2010-12-20 17:09:13 +00:00
/** Check if SID is neccessary
* @ return bool
*/
function sid () {
2010-12-29 17:15:42 +00:00
static $return ;
2012-05-14 06:54:07 +00:00
if ( $return === null ) { // restart_session() defines SID
2010-12-29 17:15:42 +00:00
$return = ( SID && ! ( $_COOKIE && ini_bool ( " session.use_cookies " ))); // $_COOKIE - don't pass SID with permanent login
}
return $return ;
2010-12-20 17:09:13 +00:00
}
2014-02-19 17:27:22 +00:00
/** Set password to session
* @ param string
* @ param string
* @ param string
* @ param string
* @ return null
*/
function set_password ( $vendor , $server , $username , $password ) {
$_SESSION [ " pwds " ][ $vendor ][ $server ][ $username ] = ( $_COOKIE [ " adminer_key " ] && is_string ( $password )
? array ( encrypt_string ( $password , $_COOKIE [ " adminer_key " ]))
: $password
);
}
/** Get password from session
* @ return string
*/
function get_password () {
$return = get_session ( " pwds " );
if ( is_array ( $return )) {
$return = ( $_COOKIE [ " adminer_key " ]
? decrypt_string ( $return [ 0 ], $_COOKIE [ " adminer_key " ])
: false
);
}
return $return ;
}
2014-09-13 17:19:51 +00:00
/** Shortcut for $connection -> quote ( $string )
2010-12-20 17:09:13 +00:00
* @ param string
* @ return string
*/
2010-10-13 16:04:40 +00:00
function q ( $string ) {
2014-09-13 17:19:51 +00:00
global $connection ;
return $connection -> quote ( $string );
2010-10-13 16:04:40 +00:00
}
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
2013-07-06 17:31:21 +00:00
* @ param float
2018-01-19 16:39:27 +00:00
* @ param bool
2010-04-21 12:01:32 +00:00
* @ return array
*/
2018-01-19 16:39:27 +00:00
function get_key_vals ( $query , $connection2 = null , $timeout = 0 , $set_keys = true ) {
2010-04-21 12:01:32 +00:00
global $connection ;
if ( ! is_object ( $connection2 )) {
$connection2 = $connection ;
}
$return = array ();
2013-07-06 17:31:21 +00:00
$connection2 -> timeout = $timeout ;
2010-04-21 12:01:32 +00:00
$result = $connection2 -> query ( $query );
2013-07-06 17:31:21 +00:00
$connection2 -> timeout = 0 ;
2011-01-31 14:49:07 +00:00
if ( is_object ( $result )) {
while ( $row = $result -> fetch_row ()) {
2018-01-19 16:39:27 +00:00
if ( $set_keys ) {
$return [ $row [ 0 ]] = $row [ 1 ];
} else {
$return [] = $row [ 0 ];
}
2011-01-31 14:49:07 +00:00
}
2010-04-21 12:01:32 +00:00
}
return $return ;
}
2010-10-13 15:53:59 +00:00
/** Get all rows of result
* @ param string
2012-01-06 08:56:30 +00:00
* @ param Min_DB
* @ param string
2018-01-31 16:28:12 +00:00
* @ return array of associative arrays
2010-10-13 15:53:59 +00:00
*/
2010-10-13 16:59:15 +00:00
function get_rows ( $query , $connection2 = null , $error = " <p class='error'> " ) {
2010-10-13 15:53:59 +00:00
global $connection ;
2012-01-06 08:56:30 +00:00
$conn = ( is_object ( $connection2 ) ? $connection2 : $connection );
2010-10-13 15:53:59 +00:00
$return = array ();
2012-01-06 08:56:30 +00:00
$result = $conn -> query ( $query );
2010-10-13 15:53:59 +00:00
if ( is_object ( $result )) { // can return true
while ( $row = $result -> fetch_assoc ()) {
$return [] = $row ;
}
2012-01-06 08:56:30 +00:00
} elseif ( ! $result && ! is_object ( $connection2 ) && $error && defined ( " PAGE_HEADER " )) {
2010-10-13 16:59:15 +00:00
echo $error . error () . " \n " ;
2010-10-13 15:53:59 +00:00
}
return $return ;
}
2009-08-31 10:11:52 +00:00
/** Find unique identifier of a row
* @ param array
* @ param array result of indexes ()
2013-05-11 20:03:39 +00:00
* @ return array or null if there is no unique identifier
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 ) {
2013-07-24 23:26:41 +00:00
if ( preg_match ( " ~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
}
}
2007-07-09 06:12:22 +00:00
}
2014-06-26 12:36:47 +00:00
/** Escape column key used in where ()
* @ param string
* @ return string
*/
function escape_key ( $key ) {
if ( preg_match ( '(^([\w(]+)(' . str_replace ( " _ " , " .* " , preg_quote ( idf_escape ( " _ " ))) . ')([ \w)]+)$)' , $key , $match )) { //! columns looking like functions
return $match [ 1 ] . idf_escape ( idf_unescape ( $match [ 2 ])) . $match [ 3 ]; //! SQL injection
}
return idf_escape ( $key );
}
2009-08-31 10:11:52 +00:00
/** Create SQL condition from parsed query string
* @ param array parsed query string
2012-12-12 05:25:56 +00:00
* @ param array
2009-08-31 10:11:52 +00:00
* @ return string
*/
2012-12-12 05:25:56 +00:00
function where ( $where , $fields = array ()) {
2014-06-26 11:22:35 +00:00
global $connection , $jush ;
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 ) {
2013-01-23 11:29:58 +00:00
$key = bracket_escape ( $key , 1 ); // 1 - back
2014-06-26 12:36:47 +00:00
$column = escape_key ( $key );
2013-06-05 02:40:17 +00:00
$return [] = $column
2017-02-20 14:23:22 +00:00
. ( $jush == " sql " && preg_match ( '~^[0-9]*\\.[0-9]*$~' , $val ) ? " LIKE " . q ( addcslashes ( $val , " %_ \\ " ))
: ( $jush == " mssql " ? " LIKE " . q ( preg_replace ( '~[_%[]~' , '[\0]' , $val ))
2013-06-05 02:40:17 +00:00
: " = " . unconvert_field ( $fields [ $key ], q ( $val ))
2017-02-20 14:23:22 +00:00
)) // LIKE because of floats but slow with ints, in MS SQL because of text
2010-04-26 16:22:58 +00:00
; //! enum and set
2014-02-24 02:49:58 +00:00
if ( $jush == " sql " && preg_match ( '~char|text~' , $fields [ $key ][ " type " ]) && preg_match ( " ~[^ -@]~ " , $val )) { // not just [a-z] to catch non-ASCII characters
2014-06-26 11:22:35 +00:00
$return [] = " $column = " . q ( $val ) . " COLLATE " . charset ( $connection ) . " _bin " ;
2013-06-05 02:40:17 +00:00
}
2010-04-23 09:03:27 +00:00
}
foreach (( array ) $where [ " null " ] as $key ) {
2014-06-26 12:36:47 +00:00
$return [] = escape_key ( $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
2012-12-12 05:25:56 +00:00
* @ param array
2009-08-31 10:11:52 +00:00
* @ return string
*/
2012-12-12 05:25:56 +00:00
function where_check ( $val , $fields = array ()) {
2009-06-07 23:53:51 +00:00
parse_str ( $val , $check );
2010-07-26 12:47:26 +00:00
remove_slashes ( array ( & $check ));
2012-12-12 05:25:56 +00:00
return where ( $check , $fields );
2009-06-07 23:53:51 +00:00
}
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
2012-09-16 13:43:55 +00:00
* @ param 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 = " = " ) {
2012-05-14 06:54:07 +00:00
return " &where%5B $i %5D%5Bcol%5D= " . urlencode ( $column ) . " &where%5B $i %5D%5Bop%5D= " . urlencode (( $value !== null ? $operator : " IS NULL " )) . " &where%5B $i %5D%5Bval%5D= " . urlencode ( $value );
2009-07-15 14:56:27 +00:00
}
2013-04-27 05:57:44 +00:00
/** Get select clause for convertible fields
* @ param array
* @ param array
* @ param array
* @ return string
*/
function convert_fields ( $columns , $fields , $select = array ()) {
$return = " " ;
foreach ( $columns as $key => $val ) {
if ( $select && ! in_array ( idf_escape ( $key ), $select )) {
continue ;
}
$as = convert_field ( $fields [ $key ]);
if ( $as ) {
$return .= " , $as AS " . idf_escape ( $key );
}
}
return $return ;
}
2013-08-11 16:26:18 +00:00
/** Set cookie valid on current path
2009-09-09 20:49:30 +00:00
* @ param string
* @ param string
2013-08-11 16:26:18 +00:00
* @ param int number of seconds , 0 for session cookie
2009-09-09 20:49:30 +00:00
* @ return bool
*/
2013-08-11 16:26:18 +00:00
function cookie ( $name , $value , $lifetime = 2592000 ) { // 2592000 - 30 days
2010-05-25 09:39:13 +00:00
global $HTTPS ;
2017-03-07 17:12:23 +00:00
return header ( " Set-Cookie: $name = " . urlencode ( $value )
. ( $lifetime ? " ; expires= " . gmdate ( " D, d M Y H:i:s " , time () + $lifetime ) . " GMT " : " " )
. " ; path= " . preg_replace ( '~\\?.*~' , '' , $_SERVER [ " REQUEST_URI " ])
. ( $HTTPS ? " ; secure " : " " )
2017-04-09 09:03:42 +00:00
. " ; HttpOnly; SameSite=lax " ,
false );
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 ();
}
}
2012-08-19 20:42:13 +00:00
/** Stop session if it would be possible to restart it later
* @ return null
*/
function stop_session () {
if ( ! ini_bool ( " session.use_cookies " )) {
session_write_close ();
}
}
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
2012-05-14 07:24:23 +00:00
* @ param string
2010-05-06 12:21:22 +00:00
* @ return string
*/
2013-07-05 15:28:37 +00:00
function auth_url ( $vendor , $server , $username , $db = null ) {
2010-05-06 12:21:22 +00:00
global $drivers ;
2012-05-14 07:24:23 +00:00
preg_match ( '~([^?]*)\\??(.*)~' , remove_from_uri ( implode ( " | " , array_keys ( $drivers )) . " |username| " . ( $db !== null ? " db| " : " " ) . session_name ()), $match );
2010-05-06 12:21:22 +00:00
return " $match[1] ? "
2010-12-20 17:09:13 +00:00
. ( sid () ? SID . " & " : " " )
2013-07-05 15:28:37 +00:00
. ( $vendor != " server " || $server != " " ? urlencode ( $vendor ) . " = " . urlencode ( $server ) . " & " : " " )
2010-05-06 12:21:22 +00:00
. " username= " . urlencode ( $username )
2012-05-14 07:24:23 +00:00
. ( $db != " " ? " &db= " . urlencode ( $db ) : " " )
2010-05-06 12:21:22 +00:00
. ( $match [ 2 ] ? " & $match[2] " : " " )
;
}
2010-10-18 11:57:22 +00:00
/** Find whether it is an AJAX request
* @ return bool
*/
function is_ajax () {
2011-01-25 15:13:31 +00:00
return ( $_SERVER [ " HTTP_X_REQUESTED_WITH " ] == " XMLHttpRequest " );
2010-10-18 11:57:22 +00:00
}
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 ) {
2012-05-14 06:54:07 +00:00
if ( $message !== null ) {
2009-11-03 10:55:57 +00:00
restart_session ();
2012-05-14 06:54:07 +00:00
$_SESSION [ " messages " ][ preg_replace ( '~^[^?]*~' , '' , ( $location !== null ? $location : $_SERVER [ " REQUEST_URI " ]))][] = $message ;
2007-07-09 06:12:22 +00:00
}
2012-05-14 06:54:07 +00:00
if ( $location !== null ) {
2010-11-12 16:09:30 +00:00
if ( $location == " " ) {
$location = " . " ;
}
2012-04-16 05:18:03 +00:00
header ( " Location: $location " );
2010-11-23 10:50:53 +00:00
exit ;
2010-04-15 14:59:40 +00:00
}
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
2014-04-18 16:10:17 +00:00
* @ param string
2009-08-31 10:11:52 +00:00
* @ return bool
*/
2014-03-07 17:33:37 +00:00
function query_redirect ( $query , $location , $message , $redirect = true , $execute = true , $failed = false , $time = " " ) {
2009-09-22 10:51:40 +00:00
global $connection , $error , $adminer ;
2009-11-02 22:09:23 +00:00
if ( $execute ) {
2013-08-09 00:18:39 +00:00
$start = microtime ( true );
2009-11-02 22:09:23 +00:00
$failed = ! $connection -> query ( $query );
2014-03-11 16:37:56 +00:00
$time = format_time ( $start );
2009-11-02 22:09:23 +00:00
}
2009-06-04 14:00:23 +00:00
$sql = " " ;
if ( $query ) {
2018-02-01 12:12:05 +00:00
$sql = $adminer -> messageQuery ( $query , $time , $failed );
2009-06-04 14:00:23 +00:00
}
2008-09-03 13:55:43 +00:00
if ( $failed ) {
2018-01-13 22:42:48 +00:00
$error = error () . $sql . script ( " messagesPrint(); " );
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
2014-03-07 17:33:37 +00:00
* @ param string or null to return remembered queries , end with ';' to use DELIMITER
2014-03-11 16:37:56 +00:00
* @ return Min_Result or array ( $queries , $time ) if $query = null
2009-08-31 10:11:52 +00:00
*/
2014-03-07 17:33:37 +00:00
function queries ( $query ) {
2009-09-22 10:51:40 +00:00
global $connection ;
2008-09-03 13:55:43 +00:00
static $queries = array ();
2014-03-07 17:33:37 +00:00
static $start ;
if ( ! $start ) {
$start = microtime ( true );
}
2012-05-14 06:54:07 +00:00
if ( $query === null ) {
2014-03-07 17:33:37 +00:00
// return executed queries
2014-03-11 16:37:56 +00:00
return array ( implode ( " \n " , $queries ), format_time ( $start ));
2008-09-03 13:55:43 +00:00
}
2014-03-07 17:33:37 +00:00
$queries [] = ( preg_match ( '~;$~' , $query ) ? " DELIMITER ;; \n $query ; \n DELIMITER " : $query ) . " ; " ;
return $connection -> query ( $query );
2008-09-03 13:55:43 +00:00
}
2010-05-17 16:18:32 +00:00
/** Apply command to all array items
* @ param string
* @ param array
* @ param callback
* @ return bool
*/
function apply_queries ( $query , $tables , $escape = 'table' ) {
foreach ( $tables as $table ) {
if ( ! queries ( " $query " . $escape ( $table ))) {
return false ;
}
}
return true ;
}
2009-10-13 20:01:52 +00:00
/** Redirect by remembered queries
* @ param string
* @ param string
* @ param bool
2010-10-15 13:51:40 +00:00
* @ return bool
2009-10-13 20:01:52 +00:00
*/
function queries_redirect ( $location , $message , $redirect ) {
2014-03-07 17:33:37 +00:00
list ( $queries , $time ) = queries ( null );
return query_redirect ( $queries , $location , $message , $redirect , false , ! $redirect , $time );
2009-10-13 20:01:52 +00:00
}
2014-03-11 16:37:56 +00:00
/** Format elapsed time
* @ param float output of microtime ( true )
2013-05-08 17:46:16 +00:00
* @ return string HTML code
*/
2014-03-11 16:37:56 +00:00
function format_time ( $start ) {
return lang ( '%.3f s' , max ( 0 , microtime ( true ) - $start ));
2013-05-08 17:46:16 +00:00
}
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
2012-09-16 13:43:55 +00:00
* @ param int
2009-08-31 10:11:52 +00:00
* @ return string
*/
2010-04-22 12:37:56 +00:00
function pagination ( $page , $current ) {
2013-07-06 17:31:21 +00:00
return " " . ( $page == $current
? $page + 1
: '<a href="' . h ( remove_from_uri ( " page " ) . ( $page ? " &page= $page " . ( $_GET [ " next " ] ? " &next= " . urlencode ( $_GET [ " next " ]) : " " ) : " " )) . '">' . ( $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 ];
2013-04-26 20:26:08 +00:00
if ( ! $file ) {
return null ;
2007-07-09 06:12:22 +00:00
}
2013-04-26 20:26:08 +00:00
foreach ( $file as $key => $val ) {
$file [ $key ] = ( array ) $val ;
}
2013-04-28 15:12:21 +00:00
$return = '' ;
2013-04-26 20:26:08 +00:00
foreach ( $file [ " error " ] as $key => $error ) {
if ( $error ) {
return $error ;
2011-03-07 13:27:03 +00:00
}
2013-04-26 20:26:08 +00:00
$name = $file [ " name " ][ $key ];
$tmp_name = $file [ " tmp_name " ][ $key ];
2013-07-24 23:26:41 +00:00
$content = file_get_contents ( $decompress && preg_match ( '~\\.gz$~' , $name )
2013-04-29 21:00:35 +00:00
? " compress.zlib:// $tmp_name "
2013-04-26 20:26:08 +00:00
: $tmp_name
2013-04-29 21:00:35 +00:00
); //! may not be reachable because of open_basedir
2013-04-26 20:26:08 +00:00
if ( $decompress ) {
$start = substr ( $content , 0 , 3 );
2013-07-24 23:26:41 +00:00
if ( function_exists ( " iconv " ) && preg_match ( " ~^ \xFE \xFF |^ \xFF \xFE ~ " , $start , $regs )) { // not ternary operator to save memory
2013-04-26 20:26:08 +00:00
$content = iconv ( " utf-16 " , " utf-8 " , $content );
} elseif ( $start == " \xEF \xBB \xBF " ) { // UTF-8 BOM
$content = substr ( $content , 3 );
}
2013-07-14 01:36:27 +00:00
$return .= $content . " \n \n " ;
2014-01-11 05:32:17 +00:00
} else {
$return .= $content ;
2013-04-26 20:26:08 +00:00
}
2011-03-07 13:27:03 +00:00
}
2013-04-26 20:26:08 +00:00
//! support SQL files not ending with semicolon
2013-04-28 15:12:21 +00:00
return $return ;
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 ) {
2011-08-24 12:16:11 +00:00
$max_size = ( $error == UPLOAD_ERR_INI_SIZE ? ini_get ( " upload_max_filesize " ) : 0 ); // 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
}
2011-07-27 07:04:43 +00:00
/** Create repeat pattern for preg
2010-10-18 23:40:49 +00:00
* @ param string
2011-07-27 07:04:43 +00:00
* @ param int
* @ return string
2010-10-18 23:40:49 +00:00
*/
2011-07-27 07:04:43 +00:00
function repeat_pattern ( $pattern , $length ) {
// fix for Compilation failed: number too big in {} quantifier
return str_repeat ( " $pattern { 0,65535} " , $length / 65535 ) . " $pattern { 0, " . ( $length % 65535 ) . " } " ; // can create {0,0} which is OK
2010-10-18 23:40:49 +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 = " " ) {
2017-02-20 15:22:30 +00:00
if ( ! preg_match ( " (^( " . repeat_pattern ( " [ \t \r \n - \ x { 10FFFF}] " , $length ) . " )( $ )?)u " , $string , $match )) { // ~s causes trash in $match[2] under some PHP versions, (.|\n) is slow
2011-01-10 16:23:47 +00:00
preg_match ( " (^( " . repeat_pattern ( " [ \t \r \n -~] " , $length ) . " )( $ )?) " , $string , $match );
2009-08-25 14:17:58 +00:00
}
2010-05-20 21:05:25 +00:00
return h ( $match [ 1 ]) . $suffix . ( isset ( $match [ 2 ]) ? " " : " <i>...</i> " );
2007-08-24 16:00:10 +00:00
}
2008-10-03 12:40:20 +00:00
2014-03-20 04:56:42 +00:00
/** Format decimal number
* @ param int
* @ return string
*/
function format_number ( $val ) {
2018-01-22 12:51:47 +00:00
return strtr ( number_format ( $val , 0 , " . " , lang ( ',' )), preg_split ( '~~u' , lang ( '0123456789' ), - 1 , PREG_SPLIT_NO_EMPTY ));
2014-03-20 04:56:42 +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
2018-02-08 15:49:56 +00:00
* @ return bool
2009-08-31 10:11:52 +00:00
*/
2008-10-09 13:23:54 +00:00
function hidden_fields ( $process , $ignore = array ()) {
2018-02-08 15:49:56 +00:00
$return = false ;
2008-10-03 13:15:58 +00:00
while ( list ( $key , $val ) = each ( $process )) {
2014-03-22 05:45:38 +00:00
if ( ! in_array ( $key , $ignore )) {
if ( is_array ( $val )) {
foreach ( $val as $k => $v ) {
$process [ $key . " [ $k ] " ] = $v ;
}
} else {
2018-02-08 15:49:56 +00:00
$return = true ;
2014-03-22 05:45:38 +00:00
echo '<input type="hidden" name="' . h ( $key ) . '" value="' . h ( $val ) . '">' ;
2008-10-03 13:15:58 +00:00
}
}
}
2018-02-08 15:49:56 +00:00
return $return ;
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 () {
2010-12-20 17:09:13 +00:00
echo ( sid () ? '<input type="hidden" name="' . session_name () . '" value="' . h ( session_id ()) . '">' : '' );
2010-04-21 12:01:32 +00:00
echo ( SERVER !== null ? '<input type="hidden" name="' . DRIVER . '" value="' . h ( SERVER ) . '">' : " " );
echo '<input type="hidden" name="username" value="' . h ( $_GET [ " username " ]) . '">' ;
}
2013-06-24 12:53:23 +00:00
/** Get status of a single table and fall back to name on error
* @ param string
* @ param bool
* @ return array
*/
function table_status1 ( $table , $fast = false ) {
$return = table_status ( $table , $fast );
return ( $return ? $return : array ( " Name " => $table ));
}
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 ) {
2010-10-29 11:58:08 +00:00
global $adminer ;
2009-07-23 14:42:38 +00:00
$return = array ();
2010-10-29 11:58:08 +00:00
foreach ( $adminer -> foreignKeys ( $table ) as $foreign_key ) {
2009-07-23 14:42:38 +00:00
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
2011-01-21 16:36:56 +00:00
* @ param string
2010-04-21 12:01:32 +00:00
* @ return null
*/
2011-01-21 16:36:56 +00:00
function enum_input ( $type , $attrs , $field , $value , $empty = null ) {
2011-01-18 13:06:23 +00:00
global $adminer ;
2010-04-21 12:01:32 +00:00
preg_match_all ( " ~'((?:[^']|'')*)'~ " , $field [ " length " ], $matches );
2012-05-14 06:54:07 +00:00
$return = ( $empty !== null ? " <label><input type=' $type ' $attrs value=' $empty ' " . (( is_array ( $value ) ? in_array ( $empty , $value ) : $value === 0 ) ? " checked " : " " ) . " ><i> " . lang ( 'empty' ) . " </i></label> " : " " );
2010-04-21 12:01:32 +00:00
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 ));
2011-01-18 13:06:23 +00:00
$return .= " <label><input type=' $type ' $attrs value=' " . ( $i + 1 ) . " ' " . ( $checked ? ' checked' : '' ) . '>' . h ( $adminer -> editVal ( $val , $field )) . '</label>' ;
2010-04-21 12:01:32 +00:00
}
2010-10-22 22:28:48 +00:00
return $return ;
2010-04-21 12:01:32 +00:00
}
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 ) {
2018-01-29 18:49:52 +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'> " ;
2013-08-09 22:49:34 +00:00
if ( is_array ( $value ) && ! $function ) {
$args = array ( $value );
if ( version_compare ( PHP_VERSION , 5.4 ) >= 0 ) {
$args [] = JSON_PRETTY_PRINT ;
}
$value = call_user_func_array ( 'json_encode' , $args ); //! requires PHP 5.2
$function = " json " ;
}
2010-11-28 15:35:34 +00:00
$reset = ( $jush == " mssql " && $field [ " auto_increment " ]);
if ( $reset && ! $_POST [ " save " ]) {
$function = null ;
}
$functions = ( isset ( $_GET [ " select " ]) || $reset ? array ( " orig " => lang ( 'original' )) : array ()) + $adminer -> editFunctions ( $field );
2010-09-09 11:03:10 +00:00
$attrs = " name='fields[ $name ]' " ;
2009-07-03 09:16:50 +00:00
if ( $field [ " type " ] == " enum " ) {
2010-10-22 22:28:48 +00:00
echo nbsp ( $functions [ " " ]) . " <td> " . $adminer -> editInput ( $_GET [ " edit " ], $field , $attrs , $value );
2009-07-03 09:16:50 +00:00
} else {
2014-01-08 17:07:58 +00:00
$has_function = ( in_array ( $function , $functions ) || isset ( $functions [ $function ]));
2013-07-19 17:35:31 +00:00
echo ( count ( $functions ) > 1
2018-01-12 17:11:00 +00:00
? " <select name='function[ $name ]'> " . optionlist ( $functions , $function === null || $has_function ? $function : " " ) . " </select> "
. on_help ( " getTarget(event).value.replace(/^SQL \$ /, '') " , 1 )
2018-01-12 14:27:44 +00:00
. script ( " qsl('select').onchange = functionChange; " , " " )
2013-07-19 17:35:31 +00:00
: 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 ;
2016-06-29 16:27:34 +00:00
} elseif ( preg_match ( '~bool~' , $field [ " type " ])) {
echo " <input type='hidden' $attrs value='0'> " .
2018-01-23 10:02:15 +00:00
" <input type='checkbox' " . ( preg_match ( '~^(1|t|true|y|yes|on)$~i' , $value ) ? " checked='checked' " : " " ) . " $attrs value='1'> " ;
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 ));
2018-01-12 22:27:14 +00:00
echo " <label><input type='checkbox' name='fields[ $name ][ $i ]' value=' " . ( 1 << $i ) . " ' " . ( $checked ? ' checked' : '' ) . " > " . h ( $adminer -> editVal ( $val , $field )) . '</label>' ;
2009-07-03 09:16:50 +00:00
}
2013-07-24 23:26:41 +00:00
} elseif ( preg_match ( '~blob|bytea|raw|file~' , $field [ " type " ]) && ini_bool ( " file_uploads " )) {
2018-01-12 22:27:14 +00:00
echo " <input type='file' name='fields- $name '> " ;
2013-07-24 23:26:41 +00:00
} elseif (( $text = preg_match ( '~text|lob~' , $field [ " type " ])) || preg_match ( " ~ \n ~ " , $value )) {
2012-08-12 03:21:03 +00:00
if ( $text && $jush != " sqlite " ) {
$attrs .= " cols='50' rows='12' " ;
} else {
$rows = min ( 12 , substr_count ( $value , " \n " ) + 1 );
$attrs .= " cols='30' rows=' $rows ' " . ( $rows == 1 ? " style='height: 1.2em;' " : " " ); // 1.2em - line-height
}
echo " <textarea $attrs > " . h ( $value ) . '</textarea>' ;
2015-09-14 22:34:01 +00:00
} elseif ( $function == " json " || preg_match ( '~^jsonb?$~' , $field [ " type " ])) {
2013-08-09 22:49:34 +00:00
echo " <textarea $attrs cols='50' rows='12' class='jush-js'> " . h ( $value ) . '</textarea>' ;
2009-07-03 09:16:50 +00:00
} else {
// int(3) is only a display hint
2013-07-24 23:26:41 +00:00
$maxlength = ( ! preg_match ( '~int~' , $field [ " type " ]) && preg_match ( '~^(\\d+)(,(\\d+))?$~' , $field [ " length " ], $match ) ? (( preg_match ( " ~binary~ " , $field [ " type " ]) ? 2 : 1 ) * $match [ 1 ] + ( $match [ 3 ] ? 1 : 0 ) + ( $match [ 2 ] && ! $field [ " unsigned " ] ? 1 : 0 )) : ( $types [ $field [ " type " ]] ? $types [ $field [ " type " ]] + ( $field [ " unsigned " ] ? 0 : 1 ) : 0 ));
2018-01-29 18:49:52 +00:00
if ( $jush == 'sql' && min_version ( 5.6 ) && preg_match ( '~time~' , $field [ " type " ])) {
2013-02-20 00:57:04 +00:00
$maxlength += 7 ; // microtime
}
2012-12-05 21:11:36 +00:00
// type='date' and type='time' display localized value which may be confusing, type='datetime' uses 'T' as date and time separator
2014-01-08 17:07:58 +00:00
echo " <input "
2018-02-06 11:17:01 +00:00
. (( ! $has_function || $function === " " ) && preg_match ( '~(?<!o)int(?!er)~' , $field [ " type " ]) && ! preg_match ( '~\[\]~' , $field [ " full_type " ]) ? " type='number' " : " " )
2016-09-19 15:21:41 +00:00
. " value=' " . h ( $value ) . " ' " . ( $maxlength ? " data-maxlength=' $maxlength ' " : " " )
2014-01-08 17:07:58 +00:00
. ( preg_match ( '~char|binary~' , $field [ " type " ]) && $maxlength > 20 ? " size='40' " : " " )
. " $attrs > "
;
2009-07-03 09:16:50 +00:00
}
2017-10-01 15:45:17 +00:00
echo $adminer -> editHint ( $_GET [ " edit " ], $field , $value );
2018-01-12 22:27:14 +00:00
// skip 'original'
$first = 0 ;
foreach ( $functions as $key => $val ) {
if ( $key === " " || ! $val ) {
break ;
}
$first ++ ;
}
if ( $first ) {
echo script ( " mixin(qsl('td'), { onchange: partial(skipOriginal, $first ), oninput: function () { this.onchange(); }}); " );
}
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 ()
2013-08-09 22:49:34 +00:00
* @ return string or false to leave the original value
2009-08-31 10:11:52 +00:00
*/
2009-07-27 16:32:56 +00:00
function process_input ( $field ) {
2018-02-06 14:42:14 +00:00
global $adminer , $driver ;
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 " ;
}
2010-10-22 21:36:56 +00:00
return + $value ;
2010-04-21 12:01:32 +00:00
}
if ( $field [ " auto_increment " ] && $value == " " ) {
return null ;
}
if ( $function == " orig " ) {
2011-06-08 10:48:39 +00:00
return ( $field [ " on_update " ] == " CURRENT_TIMESTAMP " ? idf_escape ( $field [ " field " ]) : false );
2010-04-21 12:01:32 +00:00
}
if ( $function == " NULL " ) {
2014-09-13 17:19:51 +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
}
2013-08-09 22:49:34 +00:00
if ( $function == " json " ) {
$function = " " ;
$value = json_decode ( $value , true );
if ( ! is_array ( $value )) {
return false ; //! report errors
}
return $value ;
}
2013-07-24 23:26:41 +00:00
if ( preg_match ( '~blob|bytea|raw|file~' , $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
}
2018-02-06 14:42:14 +00:00
return $driver -> quoteBinary ( $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
}
2014-01-11 23:44:03 +00:00
/** Compute fields () from $_POST edit data
* @ return array
*/
function fields_from_edit () {
2014-01-12 01:27:04 +00:00
global $driver ;
2014-01-11 23:44:03 +00:00
$return = array ();
foreach (( array ) $_POST [ " field_keys " ] as $key => $val ) {
if ( $val != " " ) {
$val = bracket_escape ( $val );
$_POST [ " function " ][ $val ] = $_POST [ " field_funs " ][ $key ];
$_POST [ " fields " ][ $val ] = $_POST [ " field_vals " ][ $key ];
}
}
foreach (( array ) $_POST [ " fields " ] as $key => $val ) {
$name = bracket_escape ( $key , 1 ); // 1 - back
2014-01-12 01:27:04 +00:00
$return [ $name ] = array (
" field " => $name ,
" privileges " => array ( " insert " => 1 , " update " => 1 ),
" null " => 1 ,
" auto_increment " => ( $key == $driver -> primary ),
);
2014-01-11 23:44:03 +00:00
}
return $return ;
}
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 ;
2010-10-07 14:17:34 +00:00
$_GET [ " where " ][ 0 ][ " val " ] = $_POST [ " query " ];
2018-02-06 14:54:19 +00:00
$sep = " <ul> \n " ;
2013-04-27 03:04:57 +00:00
foreach ( table_status ( '' , true ) as $table => $table_status ) {
2010-02-16 14:35:32 +00:00
$name = $adminer -> tableName ( $table_status );
if ( isset ( $table_status [ " Engine " ]) && $name != " " && ( ! $_POST [ " tables " ] || in_array ( $table , $_POST [ " tables " ]))) {
2010-05-14 13:51:54 +00:00
$result = $connection -> query ( " SELECT " . limit ( " 1 FROM " . table ( $table ), " WHERE " . implode ( " AND " , $adminer -> selectSearchProcess ( fields ( $table ), array ())), 1 ));
2012-09-09 18:03:02 +00:00
if ( ! $result || $result -> fetch_row ()) {
2018-02-06 11:40:28 +00:00
$print = " <a href=' " . h ( ME . " select= " . urlencode ( $table ) . " &where[0][op]= " . urlencode ( $_GET [ " where " ][ 0 ][ " op " ]) . " &where[0][val]= " . urlencode ( $_GET [ " where " ][ 0 ][ " val " ])) . " '> $name </a> " ;
2018-02-06 14:54:19 +00:00
echo " $sep <li> " . ( $result ? $print : " <p class='error'> $print : " . error ()) . " \n " ;
$sep = " " ;
2010-02-16 14:35:32 +00:00
}
}
}
2018-02-06 14:54:19 +00:00
echo ( $sep ? " <p class='message'> " . lang ( 'No tables.' ) : " </ul> " ) . " \n " ;
2010-02-16 14:35:32 +00:00
}
2011-02-17 10:43:21 +00:00
/** Send headers for export
* @ param string
* @ param bool
* @ return string extension
*/
function dump_headers ( $identifier , $multi_table = false ) {
global $adminer ;
$return = $adminer -> dumpHeaders ( $identifier , $multi_table );
2012-06-29 21:41:47 +00:00
$output = $_POST [ " output " ];
if ( $output != " text " ) {
2013-07-24 23:26:41 +00:00
header ( " Content-Disposition: attachment; filename= " . $adminer -> dumpFilename ( $identifier ) . " . $return " . ( $output != " file " && ! preg_match ( '~[^0-9a-z]~' , $output ) ? " . $output " : " " ));
2012-06-29 21:41:47 +00:00
}
2011-02-17 10:43:21 +00:00
session_write_close ();
2013-05-01 16:44:07 +00:00
ob_flush ();
flush ();
2011-02-17 10:43:21 +00:00
return $return ;
}
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-10-29 16:02:20 +00:00
if ( preg_match ( " ~[ \" \n ,; \t ]~ " , $val ) || $val === " " ) {
2009-07-03 09:16:50 +00:00
$row [ $key ] = '"' . str_replace ( '"' , '""' , $val ) . '"' ;
}
}
2010-12-01 08:39:47 +00:00
echo implode (( $_POST [ " format " ] == " csv " ? " , " : ( $_POST [ " format " ] == " tsv " ? " \t " : " ; " )), $row ) . " \r \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
}
2014-03-22 05:47:34 +00:00
/** Get path of the temporary directory
* @ return string
2010-05-06 13:45:34 +00:00
*/
2014-03-22 05:47:34 +00:00
function get_temp_dir () {
$return = ini_get ( " upload_tmp_dir " ); // session_save_path() may contain other storage path
if ( ! $return ) {
2010-05-06 13:45:34 +00:00
if ( function_exists ( 'sys_get_temp_dir' )) {
2014-03-22 05:47:34 +00:00
$return = sys_get_temp_dir ();
2010-05-06 13:45:34 +00:00
} else {
$filename = @ tempnam ( " " , " " ); // @ - temp directory can be disabled by open_basedir
if ( ! $filename ) {
return false ;
}
2014-03-22 05:47:34 +00:00
$return = dirname ( $filename );
2010-05-06 13:45:34 +00:00
unlink ( $filename );
}
}
2014-03-22 05:47:34 +00:00
return $return ;
}
2018-01-24 11:04:53 +00:00
/** Open and exclusively lock a file
* @ param string
* @ return resource or null for error
*/
function file_open_lock ( $filename ) {
$fp = @ fopen ( $filename , " r+ " ); // @ - may not exist
if ( ! $fp ) { // c+ is available since PHP 5.2.6
$fp = @ fopen ( $filename , " w " ); // @ - may not be writable
if ( ! $fp ) {
return ;
}
2018-01-24 11:11:26 +00:00
chmod ( $filename , 0660 );
2018-01-24 11:04:53 +00:00
}
flock ( $fp , LOCK_EX );
return $fp ;
}
/** Write and unlock a file
* @ param resource
* @ param string
*/
function file_write_unlock ( $fp , $data ) {
rewind ( $fp );
fwrite ( $fp , $data );
ftruncate ( $fp , strlen ( $data ));
flock ( $fp , LOCK_UN );
fclose ( $fp );
}
2014-03-22 05:47:34 +00:00
/** Read password from file adminer . key in temporary directory or create one
* @ param bool
* @ return string or false if the file can not be created
*/
function password_file ( $create ) {
$filename = get_temp_dir () . " /adminer.key " ;
$return = @ file_get_contents ( $filename ); // @ - may not exist
2013-06-11 09:02:17 +00:00
if ( $return || ! $create ) {
2010-05-06 13:45:34 +00:00
return $return ;
}
$fp = @ fopen ( $filename , " w " ); // @ - can have insufficient rights //! is not atomic
if ( $fp ) {
2014-06-27 15:17:33 +00:00
chmod ( $filename , 0660 );
2013-08-11 16:26:18 +00:00
$return = rand_string ();
2010-05-06 13:45:34 +00:00
fwrite ( $fp , $return );
fclose ( $fp );
}
return $return ;
}
2013-08-11 16:26:18 +00:00
/** Get a random string
* @ return string 32 hexadecimal characters
*/
function rand_string () {
return md5 ( uniqid ( mt_rand (), true ));
}
2013-08-06 21:55:56 +00:00
/** Format value to use in select
* @ param string
* @ param string
* @ param array
* @ param int
* @ return string HTML
*/
function select_value ( $val , $link , $field , $text_length ) {
2018-01-30 18:12:49 +00:00
global $adminer ;
2013-08-06 21:55:56 +00:00
if ( is_array ( $val )) {
$return = " " ;
foreach ( $val as $k => $v ) {
$return .= " <tr> "
. ( $val != array_values ( $val ) ? " <th> " . h ( $k ) : " " )
. " <td> " . select_value ( $v , $link , $field , $text_length )
;
}
return " <table cellspacing='0'> $return </table> " ;
}
if ( ! $link ) {
$link = $adminer -> selectLink ( $val , $field );
}
if ( $link === null ) {
if ( is_mail ( $val )) {
$link = " mailto: $val " ;
}
2018-01-30 18:12:49 +00:00
if ( is_url ( $val )) {
$link = $val ; // IE 11 and all modern browsers hide referrer
2013-08-06 21:55:56 +00:00
}
}
2014-01-11 05:32:07 +00:00
$return = $adminer -> editVal ( $val , $field );
if ( $return !== null ) {
if ( $return === " " ) { // === - may be int
$return = " " ;
2014-09-13 18:00:00 +00:00
} elseif ( ! is_utf8 ( $return )) {
$return = " \0 " ; // htmlspecialchars of binary data returns an empty string
} elseif ( $text_length != " " && is_shortable ( $field )) {
2014-01-11 05:32:07 +00:00
$return = shorten_utf8 ( $return , max ( 0 , + $text_length )); // usage of LEFT() would reduce traffic but complicate query - expected average speedup: .001 s VS .01 s on local network
2013-08-06 21:55:56 +00:00
} else {
2014-01-11 05:32:07 +00:00
$return = h ( $return );
2013-08-06 21:55:56 +00:00
}
}
2014-01-11 05:32:07 +00:00
return $adminer -> selectVal ( $return , $link , $field , $val );
2013-08-06 21:55:56 +00:00
}
2009-08-31 10:11:52 +00:00
/** Check whether the string is e - mail address
* @ param string
* @ return bool
*/
2010-10-16 13:47:51 +00:00
function is_mail ( $email ) {
2009-07-11 19:45:57 +00:00
$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 " ;
2013-08-06 21:55:56 +00:00
return is_string ( $email ) && 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
2018-01-30 18:12:49 +00:00
* @ return bool
2009-10-02 14:25:18 +00:00
*/
function is_url ( $string ) {
2010-05-25 09:39:13 +00:00
$domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])' ; // one domain component //! IDN
2018-01-30 18:12:49 +00:00
return preg_match ( " ~^(https?)://( $domain ? \\ .)+ $domain (: \\ d+)?(/.*)?( \\ ?.*)?(#.*)? \$ ~i " , $string ); //! restrict path, query and fragment characters
2009-10-02 14:25:18 +00:00
}
2012-08-20 00:12:19 +00:00
2012-09-22 14:34:51 +00:00
/** Check if field should be shortened
* @ param array
* @ return bool
*/
function is_shortable ( $field ) {
2018-02-09 12:48:33 +00:00
return preg_match ( '~char|text|json|lob|geometry|point|linestring|polygon|string|bytea~' , $field [ " type " ]);
2012-09-22 14:34:51 +00:00
}
2013-07-04 01:40:55 +00:00
/** Get query to compute number of found rows
* @ param string
* @ param array
* @ param bool
* @ param array
* @ return string
*/
function count_rows ( $table , $where , $is_group , $group ) {
global $jush ;
$query = " FROM " . table ( $table ) . ( $where ? " WHERE " . implode ( " AND " , $where ) : " " );
return ( $is_group && ( $jush == " sql " || count ( $group ) == 1 )
? " SELECT COUNT(DISTINCT " . implode ( " , " , $group ) . " ) $query "
2018-02-01 19:45:49 +00:00
: " SELECT COUNT(*) " . ( $is_group ? " FROM (SELECT 1 $query GROUP BY " . implode ( " , " , $group ) . " ) x " : $query )
2013-07-04 01:40:55 +00:00
);
}
2012-08-20 04:55:00 +00:00
/** Run query which can be killed by AJAX call after timing out
* @ param string
2013-07-04 00:28:53 +00:00
* @ return array of strings
2012-08-20 00:12:19 +00:00
*/
2012-08-20 04:55:00 +00:00
function slow_query ( $query ) {
2012-08-20 00:12:19 +00:00
global $adminer , $token ;
2012-08-20 06:25:23 +00:00
$db = $adminer -> database ();
2013-07-06 17:31:21 +00:00
$timeout = $adminer -> queryTimeout ();
2012-08-20 06:25:23 +00:00
if ( support ( " kill " ) && is_object ( $connection2 = connect ()) && ( $db == " " || $connection2 -> select_db ( $db ))) {
2017-01-16 16:03:02 +00:00
$kill = $connection2 -> result ( connection_id ()); // MySQL and MySQLi can use thread_id but it's not in PDO_MySQL
2012-08-20 04:55:00 +00:00
?>
2018-01-13 21:17:00 +00:00
< script < ? php echo nonce (); ?> >
2012-08-20 00:12:19 +00:00
var timeout = setTimeout ( function () {
ajax ( '<?php echo js_escape(ME); ?>script=kill' , function () {
2018-01-24 12:44:31 +00:00
}, 'kill=<?php echo $kill; ?>&token=<?php echo $token; ?>' );
2013-07-06 17:31:21 +00:00
}, < ? php echo 1000 * $timeout ; ?> );
2012-08-20 00:12:19 +00:00
</ script >
< ? php
2012-08-20 04:55:00 +00:00
} else {
$connection2 = null ;
2012-08-20 02:38:53 +00:00
}
2012-08-20 00:12:19 +00:00
ob_flush ();
flush ();
2018-01-19 16:39:27 +00:00
$return = @ get_key_vals ( $query , $connection2 , $timeout , false ); // @ - may be killed
2012-08-20 04:55:00 +00:00
if ( $connection2 ) {
2018-01-12 14:27:44 +00:00
echo script ( " clearTimeout(timeout); " );
2012-08-20 02:38:53 +00:00
ob_flush ();
flush ();
2012-08-20 00:12:19 +00:00
}
2018-01-19 16:39:27 +00:00
return $return ;
2012-08-20 00:12:19 +00:00
}
2012-09-07 05:20:44 +00:00
2013-10-25 02:10:50 +00:00
/** Generate BREACH resistant CSRF token
* @ return string
*/
function get_token () {
$rand = rand ( 1 , 1e6 );
return ( $rand ^ $_SESSION [ " token " ]) . " : $rand " ;
}
/** Verify if supplied CSRF token is valid
* @ return bool
*/
function verify_token () {
list ( $token , $rand ) = explode ( " : " , $_POST [ " token " ]);
return ( $rand ^ $_SESSION [ " token " ]) == $token ;
}
2012-09-07 05:20:44 +00:00
// used in compiled version
function lzw_decompress ( $binary ) {
// convert binary string to codes
$dictionary_count = 256 ;
$bits = 8 ; // ceil(log($dictionary_count, 2))
$codes = array ();
$rest = 0 ;
$rest_length = 0 ;
for ( $i = 0 ; $i < strlen ( $binary ); $i ++ ) {
$rest = ( $rest << 8 ) + ord ( $binary [ $i ]);
$rest_length += 8 ;
if ( $rest_length >= $bits ) {
$rest_length -= $bits ;
$codes [] = $rest >> $rest_length ;
$rest &= ( 1 << $rest_length ) - 1 ;
$dictionary_count ++ ;
if ( $dictionary_count >> $bits ) {
$bits ++ ;
}
}
}
// decompression
$dictionary = range ( " \0 " , " \xFF " );
$return = " " ;
foreach ( $codes as $i => $code ) {
$element = $dictionary [ $code ];
if ( ! isset ( $element )) {
$element = $word . $word [ 0 ];
}
$return .= $element ;
if ( $i ) {
$dictionary [] = $word . $element [ 0 ];
}
$word = $element ;
}
return $return ;
}
2013-12-19 20:55:53 +00:00
/** Return events to display help on mouse over
* @ param string JS expression
* @ param bool JS expression
* @ return string
*/
function on_help ( $command , $side = 0 ) {
2018-01-12 17:11:00 +00:00
return script ( " mixin(qsl('select, input'), { onmouseover: function (event) { helpMouseover.call(this, event, $command , $side ) }, onmouseout: helpMouseout}); " , " " );
2013-12-19 20:55:53 +00:00
}
2014-02-25 07:44:24 +00:00
/** Print edit data form
2014-02-25 07:59:12 +00:00
* @ param string
2014-02-25 07:44:24 +00:00
* @ param array
* @ param mixed
* @ param bool
* @ return null
*/
2014-02-25 07:59:12 +00:00
function edit_form ( $TABLE , $fields , $row , $update ) {
global $adminer , $jush , $token , $error ;
$table_name = $adminer -> tableName ( table_status1 ( $TABLE , true ));
page_header (
( $update ? lang ( 'Edit' ) : lang ( 'Insert' )),
$error ,
array ( " select " => array ( $TABLE , $table_name )),
$table_name
);
if ( $row === false ) {
echo " <p class='error'> " . lang ( 'No rows.' ) . " \n " ;
}
2014-02-25 07:44:24 +00:00
?>
< form action = " " method = " post " enctype = " multipart/form-data " id = " form " >
< ? php
if ( ! $fields ) {
echo " <p class='error'> " . lang ( 'You have no privileges to update this table.' ) . " \n " ;
} else {
2018-01-12 16:19:35 +00:00
echo " <table cellspacing='0'> " . script ( " qsl('table').onkeydown = editingKeydown; " );
2014-02-25 07:44:24 +00:00
foreach ( $fields as $name => $field ) {
echo " <tr><th> " . $adminer -> fieldName ( $field );
$default = $_GET [ " set " ][ bracket_escape ( $name )];
if ( $default === null ) {
$default = $field [ " default " ];
if ( $field [ " type " ] == " bit " && preg_match ( " ~^b'([01]*)' \$ ~ " , $default , $regs )) {
$default = $regs [ 1 ];
}
}
$value = ( $row !== null
? ( $row [ $name ] != " " && $jush == " sql " && preg_match ( " ~enum|set~ " , $field [ " type " ])
? ( is_array ( $row [ $name ]) ? array_sum ( $row [ $name ]) : + $row [ $name ])
: $row [ $name ]
)
: ( ! $update && $field [ " auto_increment " ]
? " "
: ( isset ( $_GET [ " select " ]) ? false : $default )
)
);
if ( ! $_POST [ " save " ] && is_string ( $value )) {
$value = $adminer -> editVal ( $value , $field );
}
$function = ( $_POST [ " save " ]
? ( string ) $_POST [ " function " ][ $name ]
: ( $update && $field [ " on_update " ] == " CURRENT_TIMESTAMP "
? " now "
: ( $value === false ? null : ( $value !== null ? '' : 'NULL' ))
)
);
if ( preg_match ( " ~time~ " , $field [ " type " ]) && $value == " CURRENT_TIMESTAMP " ) {
$value = " " ;
$function = " now " ;
}
input ( $field , $value , $function );
echo " \n " ;
}
if ( ! support ( " table " )) {
echo " <tr> "
2018-01-12 17:46:44 +00:00
. " <th><input name='field_keys[]'> "
. script ( " qsl('input').oninput = fieldChange; " )
2014-02-25 07:44:24 +00:00
. " <td class='function'> " . html_select ( " field_funs[] " , $adminer -> editFunctions ( array ( " null " => isset ( $_GET [ " select " ]))))
. " <td><input name='field_vals[]'> "
. " \n "
;
}
echo " </table> \n " ;
}
echo " <p> \n " ;
if ( $fields ) {
echo " <input type='submit' value=' " . lang ( 'Save' ) . " '> \n " ;
if ( ! isset ( $_GET [ " select " ])) {
echo " <input type='submit' name='insert' value=' " . ( $update
2018-01-12 16:52:45 +00:00
? lang ( 'Save and continue edit' )
2014-02-25 07:44:24 +00:00
: lang ( 'Save and insert next' )
) . " ' title='Ctrl+Shift+Enter'> \n " ;
2018-01-12 16:52:45 +00:00
echo ( $update ? script ( " qsl('input').onclick = function () { return !ajaxForm(this.form, ' " . lang ( 'Saving' ) . " ...', this); }; " ) : " " );
2014-02-25 07:44:24 +00:00
}
}
2018-01-12 13:43:55 +00:00
echo ( $update ? " <input type='submit' name='delete' value=' " . lang ( 'Delete' ) . " '> " . confirm () . " \n "
2018-01-12 14:27:44 +00:00
: ( $_POST || ! $fields ? " " : script ( " focus(qsa('td', qs('#form'))[1].firstChild); " ))
2014-02-25 07:44:24 +00:00
);
if ( isset ( $_GET [ " select " ])) {
hidden_fields ( array ( " check " => ( array ) $_POST [ " check " ], " clone " => $_POST [ " clone " ], " all " => $_POST [ " all " ]));
}
?>
< input type = " hidden " name = " referer " value = " <?php echo h(isset( $_POST["referer"] ) ? $_POST["referer"] : $_SERVER["HTTP_REFERER"] ); ?> " >
< input type = " hidden " name = " save " value = " 1 " >
< input type = " hidden " name = " token " value = " <?php echo $token ; ?> " >
</ form >
< ? php
}