2009-07-03 04:40:48 +00:00
< ? php
2011-05-24 15:16:13 +00:00
// any method change in this file should be transferred to editor/include/adminer.inc.php and plugins/plugin.php
2009-07-27 11:25:37 +00:00
class Adminer {
2010-04-21 12:01:32 +00:00
/** @var array operators used in select, null for all operators */
var $operators ;
2013-07-24 20:00:37 +00:00
2009-07-27 11:25:37 +00:00
/** Name in title and navigation
2011-04-03 15:27:34 +00:00
* @ return string HTML code
2009-07-27 11:25:37 +00:00
*/
function name () {
2023-07-10 14:29:10 +00:00
return " <a href='https://www.adminerevo.org/' " . target_blank () . " id='h1'>AdminerEvo</a> " ;
2009-07-27 11:25:37 +00:00
}
2013-07-24 20:00:37 +00:00
2009-07-27 11:25:37 +00:00
/** Connection parameters
* @ return array ( $server , $username , $password )
*/
function credentials () {
2013-08-11 02:06:21 +00:00
return array ( SERVER , $_GET [ " username " ], get_password ());
2009-07-27 11:25:37 +00:00
}
2013-07-24 20:00:37 +00:00
2018-02-07 11:13:58 +00:00
/** Get SSL connection options
* @ return array array ( " key " => filename , " cert " => filename , " ca " => filename ) or null
*/
function connectSsl () {
}
2009-12-18 17:39:48 +00:00
/** Get key used for permanent login
2013-06-11 09:02:17 +00:00
* @ param bool
* @ return string cryptic string which gets combined with password or false in case of an error
2009-12-18 17:39:48 +00:00
*/
2013-06-11 09:02:17 +00:00
function permanentLogin ( $create = false ) {
return password_file ( $create );
2009-12-18 17:39:48 +00:00
}
2013-07-24 20:00:37 +00:00
2014-03-22 05:47:34 +00:00
/** Return key used to group brute force attacks ; behind a reverse proxy , you want to return the last part of X - Forwarded - For
* @ return string
*/
function bruteForceKey () {
return $_SERVER [ " REMOTE_ADDR " ];
}
2023-05-21 13:03:36 +00:00
2018-02-08 13:27:44 +00:00
/** Get server name displayed in breadcrumbs
* @ param string
* @ return string HTML code or null
*/
function serverName ( $server ) {
return h ( $server );
}
2014-03-22 05:47:34 +00:00
2009-07-27 11:25:37 +00:00
/** Identifier of selected database
* @ return string
*/
function database () {
2009-08-29 13:54:25 +00:00
// should be used everywhere instead of DB
return DB ;
2009-07-27 11:25:37 +00:00
}
2013-07-24 20:00:37 +00:00
2012-02-24 06:54:48 +00:00
/** Get cached list of databases
* @ param bool
* @ return array
*/
function databases ( $flush = true ) {
return get_databases ( $flush );
}
2013-07-24 20:00:37 +00:00
2013-10-25 05:16:24 +00:00
/** Get list of schemas
* @ return array
*/
function schemas () {
return schemas ();
}
2012-08-20 00:12:19 +00:00
/** Specify limit for waiting on some slow queries like DB list
* @ return float number of seconds
*/
function queryTimeout () {
2018-03-09 17:06:52 +00:00
return 2 ;
2012-08-20 00:12:19 +00:00
}
2013-07-24 20:00:37 +00:00
2010-10-17 20:13:32 +00:00
/** Headers to send before HTML output
2018-01-09 12:48:51 +00:00
* @ return null
2010-10-17 20:13:32 +00:00
*/
function headers () {
}
2013-07-24 20:00:37 +00:00
2018-01-09 17:53:17 +00:00
/** Get Content Security Policy headers
2018-01-17 10:05:59 +00:00
* @ return array of arrays with directive name in key , allowed sources in value
2018-01-09 17:53:17 +00:00
*/
function csp () {
return csp ();
}
2011-03-23 10:57:35 +00:00
/** Print HTML code inside < head >
2018-01-23 11:15:38 +00:00
* @ return bool true to link favicon . ico and adminer . css if exists
2011-03-23 10:57:35 +00:00
*/
function head () {
2014-01-30 16:27:56 +00:00
?>
2014-01-10 18:05:57 +00:00
< link rel = " stylesheet " type = " text/css " href = " ../externals/jush/jush.css " >
< ? php
2011-03-23 10:57:35 +00:00
return true ;
}
2013-07-24 20:00:37 +00:00
2018-01-23 11:15:38 +00:00
/** Get URLs of the CSS files
* @ return array of strings
*/
function css () {
$return = array ();
$filename = " adminer.css " ;
if ( file_exists ( $filename )) {
2019-04-01 22:42:55 +00:00
$return [] = " $filename ?v= " . crc32 ( file_get_contents ( $filename ));
2018-01-23 11:15:38 +00:00
}
return $return ;
}
2009-07-27 11:25:37 +00:00
/** Print login form
* @ return null
*/
2010-04-21 12:01:32 +00:00
function loginForm () {
2010-10-13 15:53:59 +00:00
global $drivers ;
2018-07-27 13:22:24 +00:00
echo " <table cellspacing='0' class='layout'> \n " ;
2019-01-16 13:57:21 +00:00
echo $this -> loginFormField ( 'driver' , '<tr><th>' . lang ( 'System' ) . '<td>' , html_select ( " auth[driver] " , $drivers , DRIVER , " loginDriver(this); " ) . " \n " );
2018-02-22 11:38:22 +00:00
echo $this -> loginFormField ( 'server' , '<tr><th>' . lang ( 'Server' ) . '<td>' , '<input name="auth[server]" value="' . h ( SERVER ) . '" title="hostname[:port]" placeholder="localhost" autocapitalize="off">' . " \n " );
2019-01-16 13:57:21 +00:00
echo $this -> loginFormField ( 'username' , '<tr><th>' . lang ( 'Username' ) . '<td>' , '<input name="auth[username]" id="username" value="' . h ( $_GET [ " username " ]) . '" autocomplete="username" autocapitalize="off">' . script ( " focus(qs('#username')); qs('#username').form['auth[driver]'].onchange(); " ));
2018-12-12 15:13:14 +00:00
echo $this -> loginFormField ( 'password' , '<tr><th>' . lang ( 'Password' ) . '<td>' , '<input type="password" name="auth[password]" autocomplete="current-password">' . " \n " );
2018-02-22 11:38:22 +00:00
echo $this -> loginFormField ( 'db' , '<tr><th>' . lang ( 'Database' ) . '<td>' , '<input name="auth[db]" value="' . h ( $_GET [ " db " ]) . '" autocapitalize="off">' . " \n " );
2018-02-22 10:36:11 +00:00
echo " </table> \n " ;
2009-12-18 17:49:21 +00:00
echo " <p><input type='submit' value=' " . lang ( 'Login' ) . " '> \n " ;
2012-05-14 07:08:32 +00:00
echo checkbox ( " auth[permanent] " , 1 , $_COOKIE [ " adminer_permanent " ], lang ( 'Permanent login' )) . " \n " ;
2009-07-21 12:19:25 +00:00
}
2023-05-21 13:03:36 +00:00
2018-02-22 10:36:11 +00:00
/** Get login form field
* @ param string
* @ param string HTML
2018-02-22 11:38:22 +00:00
* @ param string HTML
2018-02-22 10:36:11 +00:00
* @ return string
*/
2018-02-22 11:38:22 +00:00
function loginFormField ( $name , $heading , $value ) {
return $heading . $value ;
2018-02-22 10:36:11 +00:00
}
2013-07-24 20:00:37 +00:00
2009-07-27 11:25:37 +00:00
/** Authorize the user
* @ param string
* @ param string
2016-06-01 16:57:17 +00:00
* @ return mixed true for success , string for error message , false for unknown error
2009-07-27 11:25:37 +00:00
*/
function login ( $login , $password ) {
2018-05-07 10:21:31 +00:00
if ( $password == " " ) {
return lang ( 'Adminer does not support accessing a database without a password, <a href="https://www.adminer.org/en/password/"%s>more information</a>.' , target_blank ());
2016-06-01 16:57:17 +00:00
}
2009-07-27 11:25:37 +00:00
return true ;
2009-07-24 10:14:36 +00:00
}
2013-07-24 20:00:37 +00:00
2009-07-27 11:25:37 +00:00
/** Table caption used in navigation and headings
* @ param array result of SHOW TABLE STATUS
2011-03-24 01:19:21 +00:00
* @ return string HTML code , " " to ignore table
2009-07-27 11:25:37 +00:00
*/
function tableName ( $tableStatus ) {
2009-07-28 16:20:50 +00:00
return h ( $tableStatus [ " Name " ]);
2009-07-27 11:25:37 +00:00
}
2013-07-24 20:00:37 +00:00
2009-07-27 11:25:37 +00:00
/** Field caption used in select and edit
* @ param array single field returned from fields ()
2009-07-27 16:51:40 +00:00
* @ param int order of column in select
2011-03-24 01:19:21 +00:00
* @ return string HTML code , " " to ignore field
2009-07-27 11:25:37 +00:00
*/
2009-07-27 16:51:40 +00:00
function fieldName ( $field , $order = 0 ) {
2009-07-28 16:20:50 +00:00
return '<span title="' . h ( $field [ " full_type " ]) . '">' . h ( $field [ " field " ]) . '</span>' ;
2009-07-27 11:25:37 +00:00
}
2013-07-24 20:00:37 +00:00
2009-09-20 07:31:46 +00:00
/** Print links after select heading
2009-07-27 11:25:37 +00:00
* @ param array result of SHOW TABLE STATUS
2009-11-24 09:44:28 +00:00
* @ param string new item options , NULL for no new item
2009-09-20 07:31:46 +00:00
* @ return null
2009-07-27 11:25:37 +00:00
*/
2009-09-20 07:31:46 +00:00
function selectLinks ( $tableStatus , $set = " " ) {
2018-02-08 10:21:33 +00:00
global $jush , $driver ;
2013-07-09 06:51:50 +00:00
echo '<p class="links">' ;
2013-07-06 17:31:21 +00:00
$links = array ( " select " => lang ( 'Select data' ));
2014-01-09 08:06:59 +00:00
if ( support ( " table " ) || support ( " indexes " )) {
2013-07-06 17:31:21 +00:00
$links [ " table " ] = lang ( 'Show structure' );
2014-01-09 08:06:59 +00:00
}
if ( support ( " table " )) {
2013-07-06 17:31:21 +00:00
if ( is_view ( $tableStatus )) {
$links [ " view " ] = lang ( 'Alter view' );
} else {
$links [ " create " ] = lang ( 'Alter table' );
}
2009-09-20 07:31:46 +00:00
}
2012-05-14 06:54:07 +00:00
if ( $set !== null ) {
2009-11-24 09:44:28 +00:00
$links [ " edit " ] = lang ( 'New item' );
}
2018-02-08 10:21:33 +00:00
$name = $tableStatus [ " Name " ];
2009-11-24 09:44:28 +00:00
foreach ( $links as $key => $val ) {
2023-07-11 22:13:45 +00:00
if ( $key !== array_key_first ( $links )) {
2023-07-14 20:19:15 +00:00
echo " | " ;
2023-07-11 22:13:45 +00:00
}
2018-02-08 10:21:33 +00:00
echo " <a href=' " . h ( ME ) . " $key = " . urlencode ( $name ) . ( $key == " edit " ? $set : " " ) . " ' " . bold ( isset ( $_GET [ $key ])) . " > $val </a> " ;
2009-09-20 07:31:46 +00:00
}
2018-02-08 10:21:33 +00:00
echo doc_link ( array ( $jush => $driver -> tableHelp ( $name )), " ? " );
2009-09-20 07:31:46 +00:00
echo " \n " ;
2009-07-27 11:25:37 +00:00
}
2013-07-24 20:00:37 +00:00
2010-10-29 11:58:08 +00:00
/** Get foreign keys for table
* @ param string
* @ return array same format as foreign_keys ()
*/
function foreignKeys ( $table ) {
return foreign_keys ( $table );
}
2013-07-24 20:00:37 +00:00
2009-07-27 11:25:37 +00:00
/** Find backward keys for table
* @ param string
2009-09-23 14:28:06 +00:00
* @ param string
* @ return array $return [ $target_table ][ " keys " ][ $key_name ][ $target_column ] = $source_column ; $return [ $target_table ][ " name " ] = $this -> tableName ( $target_table );
2009-07-27 11:25:37 +00:00
*/
2009-09-23 14:28:06 +00:00
function backwardKeys ( $table , $tableName ) {
2009-07-27 11:25:37 +00:00
return array ();
}
2013-07-24 20:00:37 +00:00
2009-09-23 14:28:06 +00:00
/** Print backward keys for row
* @ param array result of $this -> backwardKeys ()
* @ param array
* @ return null
*/
function backwardKeysPrint ( $backwardKeys , $row ) {
}
2013-07-24 20:00:37 +00:00
2009-07-27 11:25:37 +00:00
/** Query printed in select before execution
* @ param string query to be executed
2018-02-01 10:54:39 +00:00
* @ param float start time of the query
2018-02-01 12:12:05 +00:00
* @ param bool
2009-07-27 11:25:37 +00:00
* @ return string
*/
2018-02-01 12:12:05 +00:00
function selectQuery ( $query , $start , $failed = false ) {
2018-02-01 07:55:09 +00:00
global $jush , $driver ;
2018-02-01 11:27:22 +00:00
$return = " </p> \n " ; // required for IE9 inline edit
2018-02-01 12:12:05 +00:00
if ( ! $failed && ( $warnings = $driver -> warnings ())) {
2018-02-01 07:55:09 +00:00
$id = " warnings " ;
2018-02-01 11:27:22 +00:00
$return = " , <a href='# $id '> " . lang ( 'Warnings' ) . " </a> " . script ( " qsl('a').onclick = partial(toggle, ' $id '); " , " " )
. " $return <div id=' $id ' class='hidden'> \n $warnings </div> \n "
2018-02-01 07:55:09 +00:00
;
}
2023-05-28 19:33:24 +00:00
return " <p class='links'><code class='jush- $jush copy-to-clipboard'> " . h ( str_replace ( " \n " , " " , $query )) . " </code> <span class='time'>( " . format_time ( $start ) . " )</span> "
2018-02-01 11:27:22 +00:00
. ( support ( " sql " ) ? " <a href=' " . h ( ME ) . " sql= " . urlencode ( $query ) . " '> " . lang ( 'Edit' ) . " </a> " : " " )
2023-07-14 20:19:15 +00:00
. " | <a href='#' class='copy-to-clipboard'> " . lang ( 'Copy to clipboard' ) . " </a> "
2018-02-01 11:27:22 +00:00
. $return
;
2009-07-27 11:25:37 +00:00
}
2013-07-24 20:00:37 +00:00
2015-03-11 16:52:11 +00:00
/** Query printed in SQL command before execution
* @ param string query to be executed
* @ return string escaped query to be printed
*/
function sqlCommandQuery ( $query )
{
return shorten_utf8 ( trim ( $query ), 1000 );
}
2009-07-27 11:25:37 +00:00
/** Description of a row in a table
* @ param string
* @ return string SQL expression , empty string for no description
*/
function rowDescription ( $table ) {
return " " ;
}
2013-07-24 20:00:37 +00:00
2009-07-27 11:25:37 +00:00
/** Get descriptions of selected data
* @ param array all data to print
* @ param array
* @ return array
*/
function rowDescriptions ( $rows , $foreignKeys ) {
return $rows ;
}
2013-07-24 20:00:37 +00:00
2013-01-11 01:59:43 +00:00
/** Get a link to use in select table
* @ param string raw value of the field
* @ param array single field returned from fields ()
* @ return string or null to create the default link
*/
function selectLink ( $val , $field ) {
}
2013-07-24 20:00:37 +00:00
2009-07-27 11:25:37 +00:00
/** Value printed in select table
2009-07-27 16:32:56 +00:00
* @ param string HTML - escaped value to print
2009-07-27 11:25:37 +00:00
* @ param string link to foreign key
* @ param array single field returned from fields ()
2014-01-11 05:32:07 +00:00
* @ param array original value before applying editVal () and escaping
2009-07-27 11:25:37 +00:00
* @ return string
*/
2014-01-11 05:32:07 +00:00
function selectVal ( $val , $link , $field , $original ) {
2018-01-24 09:53:25 +00:00
$return = ( $val === null ? " <i>NULL</i> " : ( preg_match ( " ~char|binary|boolean~ " , $field [ " type " ]) && ! preg_match ( " ~var~ " , $field [ " type " ]) ? " <code> $val </code> " : $val ));
2013-07-24 23:26:41 +00:00
if ( preg_match ( '~blob|bytea|raw|file~' , $field [ " type " ]) && ! is_utf8 ( $val )) {
2017-03-10 07:19:58 +00:00
$return = " <i> " . lang ( '%d byte(s)' , strlen ( $original )) . " </i> " ;
2009-07-23 11:31:08 +00:00
}
2017-03-10 07:56:58 +00:00
if ( preg_match ( '~json~' , $field [ " type " ])) {
$return = " <code class='jush-js'> $return </code> " ;
}
2018-01-30 18:12:49 +00:00
return ( $link ? " <a href=' " . h ( $link ) . " ' " . ( is_url ( $link ) ? target_blank () : " " ) . " > $return </a> " : $return );
2009-07-27 11:25:37 +00:00
}
2013-07-24 20:00:37 +00:00
2009-07-27 16:32:56 +00:00
/** Value conversion used in select and edit
* @ param string
* @ param array single field returned from fields ()
2009-07-28 09:27:00 +00:00
* @ return string
2009-07-27 16:32:56 +00:00
*/
function editVal ( $val , $field ) {
2012-09-09 05:56:34 +00:00
return $val ;
2009-07-27 16:32:56 +00:00
}
2013-07-24 20:00:37 +00:00
2015-12-05 14:21:37 +00:00
/** Print table structure in tabular format
* @ param array data about individual fields
* @ return null
*/
function tableStructurePrint ( $fields ) {
2018-10-27 19:20:56 +00:00
echo " <div class='scrollable'> \n " ;
2018-01-22 20:03:48 +00:00
echo " <table cellspacing='0' class='nowrap'> \n " ;
2015-12-05 14:21:37 +00:00
echo " <thead><tr><th> " . lang ( 'Column' ) . " <td> " . lang ( 'Type' ) . ( support ( " comment " ) ? " <td> " . lang ( 'Comment' ) : " " ) . " </thead> \n " ;
foreach ( $fields as $field ) {
echo " <tr " . odd () . " ><th> " . h ( $field [ " field " ]);
echo " <td><span title=' " . h ( $field [ " collation " ]) . " '> " . h ( $field [ " full_type " ]) . " </span> " ;
echo ( $field [ " null " ] ? " <i>NULL</i> " : " " );
echo ( $field [ " auto_increment " ] ? " <i> " . lang ( 'Auto Increment' ) . " </i> " : " " );
echo ( isset ( $field [ " default " ]) ? " <span title=' " . lang ( 'Default value' ) . " '>[<b> " . h ( $field [ " default " ]) . " </b>]</span> " : " " );
2018-02-20 15:02:25 +00:00
echo ( support ( " comment " ) ? " <td> " . h ( $field [ " comment " ]) : " " );
2015-12-05 14:21:37 +00:00
echo " \n " ;
}
echo " </table> \n " ;
2018-10-25 10:31:28 +00:00
echo " </div> \n " ;
2015-12-05 14:21:37 +00:00
}
/** Print list of indexes on table in tabular format
* @ param array data about all indexes on a table
* @ return null
*/
function tableIndexesPrint ( $indexes ) {
echo " <table cellspacing='0'> \n " ;
foreach ( $indexes as $name => $index ) {
ksort ( $index [ " columns " ]); // enforce correct columns order
$print = array ();
foreach ( $index [ " columns " ] as $key => $val ) {
$print [] = " <i> " . h ( $val ) . " </i> "
. ( $index [ " lengths " ][ $key ] ? " ( " . $index [ " lengths " ][ $key ] . " ) " : " " )
. ( $index [ " descs " ][ $key ] ? " DESC " : " " )
;
}
echo " <tr title=' " . h ( $name ) . " '><th> $index[type] <td> " . implode ( " , " , $print ) . " \n " ;
}
echo " </table> \n " ;
}
2009-07-27 14:40:55 +00:00
/** Print columns box in select
2011-05-24 15:03:48 +00:00
* @ param array result of selectColumnsProcess ()[ 0 ]
2009-07-27 14:40:55 +00:00
* @ param array selectable columns
* @ return null
*/
function selectColumnsPrint ( $select , $columns ) {
2010-04-21 12:01:32 +00:00
global $functions , $grouping ;
2009-09-11 19:26:27 +00:00
print_fieldset ( " select " , lang ( 'Select' ), $select );
2009-07-27 14:40:55 +00:00
$i = 0 ;
2013-07-19 17:35:31 +00:00
$select [ " " ] = array ();
2009-07-27 14:40:55 +00:00
foreach ( $select as $key => $val ) {
$val = $_GET [ " columns " ][ $key ];
2018-01-12 16:52:45 +00:00
$column = select_input (
" name='columns[ $i ][col]' " ,
$columns ,
$val [ " col " ],
2018-01-22 12:51:47 +00:00
( $key !== " " ? " selectFieldChange " : " selectAddRow " )
2018-01-12 16:52:45 +00:00
);
2018-01-12 17:11:00 +00:00
echo " <div> " . ( $functions || $grouping ? " <select name='columns[ $i ][fun]'> "
. optionlist ( array ( - 1 => " " ) + array_filter ( array ( lang ( 'Functions' ) => $functions , lang ( 'Aggregation' ) => $grouping )), $val [ " fun " ]) . " </select> "
. on_help ( " getTarget(event).value && getTarget(event).value.replace(/ | \$ /, '(') + ')' " , 1 )
2018-01-14 09:18:16 +00:00
. script ( " qsl('select').onchange = function () { helpClose(); " . ( $key !== " " ? " " : " qsl('select, input', this.parentNode).onchange(); " ) . " }; " , " " )
2013-07-19 21:10:02 +00:00
. " ( $column ) " : $column ) . " </div> \n " ;
2009-07-27 14:40:55 +00:00
$i ++ ;
}
echo " </div></fieldset> \n " ;
}
2013-07-24 20:00:37 +00:00
2009-07-27 14:40:55 +00:00
/** Print search box in select
* @ param array result of selectSearchProcess ()
* @ param array selectable columns
* @ param array
* @ return null
*/
function selectSearchPrint ( $where , $columns , $indexes ) {
2009-09-11 19:26:27 +00:00
print_fieldset ( " search " , lang ( 'Search' ), $where );
2009-07-27 14:40:55 +00:00
foreach ( $indexes as $i => $index ) {
if ( $index [ " type " ] == " FULLTEXT " ) {
2018-01-21 00:25:33 +00:00
echo " <div>(<i> " . implode ( " </i>, <i> " , array_map ( 'h' , $index [ " columns " ])) . " </i>) AGAINST " ;
2018-01-12 12:08:21 +00:00
echo " <input type='search' name='fulltext[ $i ]' value=' " . h ( $_GET [ " fulltext " ][ $i ]) . " '> " ;
2018-01-12 21:18:54 +00:00
echo script ( " qsl('input').oninput = selectFieldChange; " , " " );
2009-09-25 12:03:30 +00:00
echo checkbox ( " boolean[ $i ] " , 1 , isset ( $_GET [ " boolean " ][ $i ]), " BOOL " );
2018-01-21 00:25:33 +00:00
echo " </div> \n " ;
2009-07-27 14:40:55 +00:00
}
}
2018-01-12 16:52:45 +00:00
$change_next = " this.parentNode.firstChild.onchange(); " ;
2018-02-06 11:58:06 +00:00
foreach ( array_merge (( array ) $_GET [ " where " ], array ( array ())) as $i => $val ) {
2012-05-17 06:54:56 +00:00
if ( ! $val || ( " $val[col] $val[val] " != " " && in_array ( $val [ " op " ], $this -> operators ))) {
2018-01-12 16:52:45 +00:00
echo " <div> " . select_input (
" name='where[ $i ][col]' " ,
$columns ,
$val [ " col " ],
( $val ? " selectFieldChange " : " selectAddRow " ),
" ( " . lang ( 'anywhere' ) . " ) "
);
2012-05-17 06:54:56 +00:00
echo html_select ( " where[ $i ][op] " , $this -> operators , $val [ " op " ], $change_next );
2018-01-12 16:52:45 +00:00
echo " <input type='search' name='where[ $i ][val]' value=' " . h ( $val [ " val " ]) . " '> " ;
2018-01-12 21:18:54 +00:00
echo script ( " mixin(qsl('input'), { oninput: function () { $change_next }, onkeydown: selectSearchKeydown, onsearch: selectSearchSearch}); " , " " );
2018-01-12 16:52:45 +00:00
echo " </div> \n " ;
2009-07-27 14:40:55 +00:00
}
}
echo " </div></fieldset> \n " ;
}
2013-07-24 20:00:37 +00:00
2009-07-27 14:40:55 +00:00
/** Print order box in select
* @ param array result of selectOrderProcess ()
* @ param array selectable columns
* @ param array
* @ return null
*/
function selectOrderPrint ( $order , $columns , $indexes ) {
2013-04-03 17:10:15 +00:00
print_fieldset ( " sort " , lang ( 'Sort' ), $order );
2009-07-27 14:40:55 +00:00
$i = 0 ;
foreach (( array ) $_GET [ " order " ] as $key => $val ) {
2013-07-06 17:31:21 +00:00
if ( $val != " " ) {
2018-01-12 12:08:21 +00:00
echo " <div> " . select_input ( " name='order[ $i ]' " , $columns , $val , " selectFieldChange " );
2009-09-25 12:03:30 +00:00
echo checkbox ( " desc[ $i ] " , 1 , isset ( $_GET [ " desc " ][ $key ]), lang ( 'descending' )) . " </div> \n " ;
2009-07-27 14:40:55 +00:00
$i ++ ;
}
}
2018-01-12 16:52:45 +00:00
echo " <div> " . select_input ( " name='order[ $i ]' " , $columns , " " , " selectAddRow " );
2013-06-03 15:56:18 +00:00
echo checkbox ( " desc[ $i ] " , 1 , false , lang ( 'descending' )) . " </div> \n " ;
2009-07-27 14:40:55 +00:00
echo " </div></fieldset> \n " ;
}
2013-07-24 20:00:37 +00:00
2009-07-27 14:40:55 +00:00
/** Print limit box in select
* @ param string result of selectLimitProcess ()
* @ return null
*/
function selectLimitPrint ( $limit ) {
echo " <fieldset><legend> " . lang ( 'Limit' ) . " </legend><div> " ; // <div> for easy styling
2018-01-12 12:08:21 +00:00
echo " <input type='number' name='limit' class='size' value=' " . h ( $limit ) . " '> " ;
2018-01-12 21:18:54 +00:00
echo script ( " qsl('input').oninput = selectFieldChange; " , " " );
2009-07-27 14:40:55 +00:00
echo " </div></fieldset> \n " ;
}
2013-07-24 20:00:37 +00:00
2009-07-27 14:40:55 +00:00
/** Print text length box in select
* @ param string result of selectLengthProcess ()
* @ return null
*/
function selectLengthPrint ( $text_length ) {
2012-05-14 06:54:07 +00:00
if ( $text_length !== null ) {
2009-07-27 14:40:55 +00:00
echo " <fieldset><legend> " . lang ( 'Text length' ) . " </legend><div> " ;
2012-12-05 21:11:36 +00:00
echo " <input type='number' name='text_length' class='size' value=' " . h ( $text_length ) . " '> " ;
2009-07-27 14:40:55 +00:00
echo " </div></fieldset> \n " ;
}
}
2013-07-24 20:00:37 +00:00
2009-07-27 14:40:55 +00:00
/** Print action box in select
2012-05-17 06:54:56 +00:00
* @ param array
2009-07-27 14:40:55 +00:00
* @ return null
*/
2012-05-17 06:54:56 +00:00
function selectActionPrint ( $indexes ) {
2009-07-27 14:40:55 +00:00
echo " <fieldset><legend> " . lang ( 'Action' ) . " </legend><div> " ;
echo " <input type='submit' value=' " . lang ( 'Select' ) . " '> " ;
2012-05-17 06:54:56 +00:00
echo " <span id='noindex' title=' " . lang ( 'Full table scan' ) . " '></span> " ;
2018-01-13 21:17:00 +00:00
echo " <script " . nonce () . " > \n " ;
2012-05-17 06:54:56 +00:00
echo " var indexColumns = " ;
$columns = array ();
foreach ( $indexes as $index ) {
2016-08-30 11:23:14 +00:00
$current_key = reset ( $index [ " columns " ]);
if ( $index [ " type " ] != " FULLTEXT " && $current_key ) {
$columns [ $current_key ] = 1 ;
2012-05-17 06:54:56 +00:00
}
}
$columns [ " " ] = 1 ;
foreach ( $columns as $key => $val ) {
json_row ( $key );
}
echo " ; \n " ;
2018-01-12 11:50:09 +00:00
echo " selectFieldChange.call(qs('#form')['select']); \n " ;
2012-05-17 06:54:56 +00:00
echo " </script> \n " ;
2009-07-27 14:40:55 +00:00
echo " </div></fieldset> \n " ;
}
2023-05-21 13:03:36 +00:00
2011-05-24 15:16:13 +00:00
/** Print command box in select
* @ return bool whether to print default commands
*/
function selectCommandPrint () {
return ! information_schema ( DB );
}
2013-07-24 20:00:37 +00:00
2011-05-24 15:16:13 +00:00
/** Print import box in select
* @ return bool whether to print default import
*/
function selectImportPrint () {
2012-08-29 17:08:18 +00:00
return ! information_schema ( DB );
2011-05-24 15:16:13 +00:00
}
2013-07-24 20:00:37 +00:00
2009-07-28 11:18:08 +00:00
/** Print extra text in the end of a select form
* @ param array fields holding e - mails
2009-09-09 20:47:42 +00:00
* @ param array selectable columns
2009-07-28 11:18:08 +00:00
* @ return null
*/
2009-09-09 20:47:42 +00:00
function selectEmailPrint ( $emailFields , $columns ) {
2009-07-28 11:18:08 +00:00
}
2013-07-24 20:00:37 +00:00
2009-07-27 14:40:55 +00:00
/** Process columns box in select
* @ param array selectable columns
2009-09-09 20:47:42 +00:00
* @ param array
2009-07-27 14:40:55 +00:00
* @ return array ( array ( select_expressions ), array ( group_expressions ))
*/
function selectColumnsProcess ( $columns , $indexes ) {
2010-04-21 12:01:32 +00:00
global $functions , $grouping ;
2009-07-27 14:40:55 +00:00
$select = array (); // select expressions, empty for *
$group = array (); // expressions without aggregation - will be used for GROUP BY if an aggregation function is used
foreach (( array ) $_GET [ " columns " ] as $key => $val ) {
2013-07-06 17:31:21 +00:00
if ( $val [ " fun " ] == " count " || ( $val [ " col " ] != " " && ( ! $val [ " fun " ] || in_array ( $val [ " fun " ], $functions ) || in_array ( $val [ " fun " ], $grouping )))) {
$select [ $key ] = apply_sql_function ( $val [ " fun " ], ( $val [ " col " ] != " " ? idf_escape ( $val [ " col " ]) : " * " ));
2010-04-21 12:01:32 +00:00
if ( ! in_array ( $val [ " fun " ], $grouping )) {
2009-07-27 14:40:55 +00:00
$group [] = $select [ $key ];
}
}
}
return array ( $select , $group );
}
2013-07-24 20:00:37 +00:00
2009-07-27 14:40:55 +00:00
/** Process search box in select
* @ param array
* @ param array
* @ return array expressions to join by AND
*/
2009-07-28 14:21:05 +00:00
function selectSearchProcess ( $fields , $indexes ) {
2018-02-06 12:19:12 +00:00
global $connection , $driver ;
2009-07-27 14:40:55 +00:00
$return = array ();
foreach ( $indexes as $i => $index ) {
2010-01-09 23:33:41 +00:00
if ( $index [ " type " ] == " FULLTEXT " && $_GET [ " fulltext " ][ $i ] != " " ) {
2010-10-13 16:04:40 +00:00
$return [] = " MATCH ( " . implode ( " , " , array_map ( 'idf_escape' , $index [ " columns " ])) . " ) AGAINST ( " . q ( $_GET [ " fulltext " ][ $i ]) . ( isset ( $_GET [ " boolean " ][ $i ]) ? " IN BOOLEAN MODE " : " " ) . " ) " ;
2009-07-27 14:40:55 +00:00
}
}
2018-02-06 11:56:18 +00:00
foreach (( array ) $_GET [ " where " ] as $key => $val ) {
2010-01-09 23:33:41 +00:00
if ( " $val[col] $val[val] " != " " && in_array ( $val [ " op " ], $this -> operators )) {
2018-02-01 14:25:38 +00:00
$prefix = " " ;
2010-04-21 12:01:32 +00:00
$cond = " $val[op] " ;
2013-07-24 23:26:41 +00:00
if ( preg_match ( '~IN$~' , $val [ " op " ])) {
2010-04-21 12:01:32 +00:00
$in = process_length ( $val [ " val " ]);
2013-08-06 22:48:45 +00:00
$cond .= " " . ( $in != " " ? $in : " (NULL) " );
2013-04-26 19:04:28 +00:00
} elseif ( $val [ " op " ] == " SQL " ) {
$cond = " $val[val] " ; // SQL injection
2010-04-21 12:01:32 +00:00
} elseif ( $val [ " op " ] == " LIKE %% " ) {
$cond = " LIKE " . $this -> processInput ( $fields [ $val [ " col " ]], " % $val[val] % " );
2015-01-19 08:38:10 +00:00
} elseif ( $val [ " op " ] == " ILIKE %% " ) {
$cond = " ILIKE " . $this -> processInput ( $fields [ $val [ " col " ]], " % $val[val] % " );
2018-02-01 14:25:38 +00:00
} elseif ( $val [ " op " ] == " FIND_IN_SET " ) {
$prefix = " $val[op] ( " . q ( $val [ " val " ]) . " , " ;
$cond = " ) " ;
2013-07-24 23:26:41 +00:00
} elseif ( ! preg_match ( '~NULL$~' , $val [ " op " ])) {
2010-04-21 12:01:32 +00:00
$cond .= " " . $this -> processInput ( $fields [ $val [ " col " ]], $val [ " val " ]);
}
2010-01-09 23:33:41 +00:00
if ( $val [ " col " ] != " " ) {
2018-02-06 12:39:44 +00:00
$return [] = $prefix . $driver -> convertSearch ( idf_escape ( $val [ " col " ]), $val , $fields [ $val [ " col " ]]) . $cond ;
2009-07-27 14:40:55 +00:00
} else {
2009-07-28 13:43:38 +00:00
// find anywhere
$cols = array ();
foreach ( $fields as $name => $field ) {
2018-03-13 13:09:53 +00:00
if (( preg_match ( '~^[-\d.' . ( preg_match ( '~IN$~' , $val [ " op " ]) ? ',' : '' ) . ']+$~' , $val [ " val " ]) || ! preg_match ( '~' . number_type () . '|bit~' , $field [ " type " ]))
2018-02-06 12:19:12 +00:00
&& ( ! preg_match ( " ~[ \x80 - \xFF ]~ " , $val [ " val " ]) || preg_match ( '~char|text|enum|set~' , $field [ " type " ]))
2021-02-08 09:53:20 +00:00
&& ( ! preg_match ( '~date|timestamp~' , $field [ " type " ]) || preg_match ( '~^\d+-\d+-\d+~' , $val [ " val " ]))
2012-09-09 18:03:02 +00:00
) {
2018-02-06 12:39:44 +00:00
$cols [] = $prefix . $driver -> convertSearch ( idf_escape ( $name ), $val , $field ) . $cond ;
2009-07-27 14:40:55 +00:00
}
}
2018-02-06 11:43:10 +00:00
$return [] = ( $cols ? " ( " . implode ( " OR " , $cols ) . " ) " : " 1 = 0 " );
2009-07-27 14:40:55 +00:00
}
}
}
return $return ;
}
2013-07-24 20:00:37 +00:00
2009-07-27 14:40:55 +00:00
/** Process order box in select
* @ param array
* @ param array
* @ return array expressions to join by comma
*/
2009-07-28 14:21:05 +00:00
function selectOrderProcess ( $fields , $indexes ) {
2009-07-27 14:40:55 +00:00
$return = array ();
foreach (( array ) $_GET [ " order " ] as $key => $val ) {
2013-07-06 17:31:21 +00:00
if ( $val != " " ) {
2018-02-20 15:27:40 +00:00
$return [] = ( preg_match ( '~^((COUNT\(DISTINCT |[A-Z0-9_]+\()(`(?:[^`]|``)+`|"(?:[^"]|"")+")\)|COUNT\(\*\))$~' , $val ) ? $val : idf_escape ( $val )) //! MS SQL uses []
2013-07-06 17:31:21 +00:00
. ( isset ( $_GET [ " desc " ][ $key ]) ? " DESC " : " " )
;
2009-07-27 14:40:55 +00:00
}
}
return $return ;
}
2013-07-24 20:00:37 +00:00
2009-07-27 14:40:55 +00:00
/** Process limit box in select
* @ return string expression to use in LIMIT , will be escaped
*/
function selectLimitProcess () {
2013-04-15 21:45:49 +00:00
return ( isset ( $_GET [ " limit " ]) ? $_GET [ " limit " ] : " 50 " );
2009-07-27 14:40:55 +00:00
}
2013-07-24 20:00:37 +00:00
2009-07-27 14:40:55 +00:00
/** Process length box in select
* @ return string number of characters to shorten texts , will be escaped
*/
function selectLengthProcess () {
return ( isset ( $_GET [ " text_length " ]) ? $_GET [ " text_length " ] : " 100 " );
}
2013-07-24 20:00:37 +00:00
2009-07-27 11:25:37 +00:00
/** Process extras in select form
* @ param array AND conditions
2009-09-09 20:19:27 +00:00
* @ param array
2009-07-27 11:25:37 +00:00
* @ return bool true if processed , false to process other parts of form
*/
2009-09-09 20:19:27 +00:00
function selectEmailProcess ( $where , $foreignKeys ) {
2009-07-27 11:25:37 +00:00
return false ;
}
2013-07-24 20:00:37 +00:00
2012-08-24 05:17:44 +00:00
/** Build SQL query used in select
* @ param array result of selectColumnsProcess ()[ 0 ]
* @ param array result of selectSearchProcess ()
* @ param array result of selectColumnsProcess ()[ 1 ]
* @ param array result of selectOrderProcess ()
* @ param int result of selectLimitProcess ()
* @ param int index of page starting at zero
* @ return string empty string to use default query
*/
function selectQueryBuild ( $select , $where , $group , $order , $limit , $page ) {
return " " ;
}
2013-07-24 20:00:37 +00:00
2009-07-27 11:25:37 +00:00
/** Query printed after execution in the message
* @ param string executed query
2014-03-07 17:33:37 +00:00
* @ param string elapsed time
2018-02-01 12:12:05 +00:00
* @ param bool
2009-07-27 11:25:37 +00:00
* @ return string
*/
2018-02-01 12:12:05 +00:00
function messageQuery ( $query , $time , $failed = false ) {
2018-02-01 08:21:50 +00:00
global $jush , $driver ;
2009-11-03 10:55:57 +00:00
restart_session ();
2010-10-15 08:58:08 +00:00
$history = & get_session ( " queries " );
2018-01-16 15:22:24 +00:00
if ( ! $history [ $_GET [ " db " ]]) {
$history [ $_GET [ " db " ]] = array ();
}
2012-03-10 10:28:13 +00:00
if ( strlen ( $query ) > 1e6 ) {
2018-12-18 13:45:37 +00:00
$query = preg_replace ( '~[\x80-\xFF]+$~' , '' , substr ( $query , 0 , 1e6 )) . " \n … " ; // [\x80-\xFF] - valid UTF-8, \n - can end by one-line comment
2011-03-22 14:12:38 +00:00
}
2014-03-07 17:33:37 +00:00
$history [ $_GET [ " db " ]][] = array ( $query , time (), $time ); // not DB - $_GET["db"] is changed in database.inc.php //! respect $_GET["ns"]
2018-02-01 12:08:28 +00:00
$sql_id = " sql- " . count ( $history [ $_GET [ " db " ]]);
$return = " <a href='# $sql_id ' class='toggle'> " . lang ( 'SQL command' ) . " </a> \n " ;
2018-02-01 12:12:05 +00:00
if ( ! $failed && ( $warnings = $driver -> warnings ())) {
2018-02-01 08:21:50 +00:00
$id = " warnings- " . count ( $history [ $_GET [ " db " ]]);
2018-02-01 11:27:22 +00:00
$return = " <a href='# $id ' class='toggle'> " . lang ( 'Warnings' ) . " </a>, $return <div id=' $id ' class='hidden'> \n $warnings </div> \n " ;
2018-02-01 08:21:50 +00:00
}
2018-02-01 11:27:22 +00:00
return " <span class='time'> " . @ date ( " H:i:s " ) . " </span> " // @ - time zone may be not set
2023-05-28 19:33:24 +00:00
. " $return <div id=' $sql_id ' class='hidden'><pre><code class='jush- $jush copy-to-clipboard'> " . shorten_utf8 ( $query , 1000 ) . " </code></pre> "
2014-03-07 17:33:37 +00:00
. ( $time ? " <span class='time'>( $time )</span> " : '' )
2023-07-14 20:19:15 +00:00
. ( support ( " sql " ) ? '<p class="links"><a href="' . h ( str_replace ( " db= " . urlencode ( DB ), " db= " . urlencode ( $_GET [ " db " ]), ME ) . 'sql=&history=' . ( count ( $history [ $_GET [ " db " ]]) - 1 )) . '">' . lang ( 'Edit' ) . '</a> | <a href="#" class="copy-to-clipboard">' . lang ( 'Copy to clipboard' ) . '</a>' : '' )
2013-08-02 22:11:00 +00:00
. '</div>'
;
2009-07-27 11:25:37 +00:00
}
2013-07-24 20:00:37 +00:00
2021-02-08 08:23:57 +00:00
/** Print before edit form
* @ param string
* @ param array
* @ param mixed
* @ param bool
* @ return null
*/
function editRowPrint ( $table , $fields , $row , $update ) {
}
2009-07-27 11:25:37 +00:00
/** Functions displayed in edit form
* @ param array single field from fields ()
* @ return array
*/
function editFunctions ( $field ) {
2010-04-21 12:01:32 +00:00
global $edit_functions ;
2010-04-22 00:33:46 +00:00
$return = ( $field [ " null " ] ? " NULL/ " : " " );
2021-02-08 16:22:53 +00:00
$update = isset ( $_GET [ " select " ]) || where ( $_GET );
2010-04-21 12:01:32 +00:00
foreach ( $edit_functions as $key => $functions ) {
2021-02-08 16:22:53 +00:00
if ( ! $key || ( ! isset ( $_GET [ " call " ]) && $update )) { // relative functions
2010-04-21 12:01:32 +00:00
foreach ( $functions as $pattern => $val ) {
2013-07-24 23:26:41 +00:00
if ( ! $pattern || preg_match ( " ~ $pattern ~ " , $field [ " type " ])) {
2010-04-21 12:01:32 +00:00
$return .= " / $val " ;
}
}
2021-02-08 16:41:59 +00:00
}
if ( $key && ! preg_match ( '~set|blob|bytea|raw|file|bool~' , $field [ " type " ])) {
$return .= " /SQL " ;
2009-08-28 14:09:53 +00:00
}
2009-07-23 11:31:08 +00:00
}
2021-02-08 16:22:53 +00:00
if ( $field [ " auto_increment " ] && ! $update ) {
2014-01-04 22:30:42 +00:00
$return = lang ( 'Auto Increment' );
}
2010-04-21 12:01:32 +00:00
return explode ( " / " , $return );
2009-07-23 11:31:08 +00:00
}
2013-07-24 20:00:37 +00:00
2009-07-27 11:25:37 +00:00
/** Get options to display edit field
* @ param string table name
* @ param array single field from fields ()
2009-07-28 10:09:05 +00:00
* @ param string attributes to use inside the tag
* @ param string
* @ return string custom input field or empty string for default
2009-07-27 11:25:37 +00:00
*/
2009-07-28 10:09:05 +00:00
function editInput ( $table , $field , $attrs , $value ) {
2009-11-12 15:25:21 +00:00
if ( $field [ " type " ] == " enum " ) {
2010-10-22 22:28:48 +00:00
return ( isset ( $_GET [ " select " ]) ? " <label><input type='radio' $attrs value='-1' checked><i> " . lang ( 'original' ) . " </i></label> " : " " )
2012-05-14 06:54:07 +00:00
. ( $field [ " null " ] ? " <label><input type='radio' $attrs value='' " . ( $value !== null || isset ( $_GET [ " select " ]) ? " " : " checked " ) . " ><i>NULL</i></label> " : " " )
2011-01-21 16:36:56 +00:00
. enum_input ( " radio " , $attrs , $field , $value , 0 ) // 0 - empty
2009-11-12 15:25:21 +00:00
;
}
return " " ;
2009-07-23 11:31:08 +00:00
}
2013-07-24 20:00:37 +00:00
2017-10-01 15:45:17 +00:00
/** Get hint for edit field
* @ param string table name
* @ param array single field from fields ()
* @ param string
* @ return string
*/
function editHint ( $table , $field , $value ) {
return " " ;
}
2009-07-27 11:25:37 +00:00
/** Process sent input
* @ param array single field from fields ()
2009-07-27 16:32:56 +00:00
* @ param string
* @ param string
2009-07-27 11:25:37 +00:00
* @ return string expression to use in a query
*/
2009-07-27 16:32:56 +00:00
function processInput ( $field , $value , $function = " " ) {
2013-04-26 19:20:47 +00:00
if ( $function == " SQL " ) {
2011-03-15 16:34:25 +00:00
return $value ; // SQL injection
}
2009-07-27 16:32:56 +00:00
$name = $field [ " field " ];
2013-04-27 06:21:09 +00:00
$return = q ( $value );
2013-07-24 23:26:41 +00:00
if ( preg_match ( '~^(now|getdate|uuid)$~' , $function )) {
2009-07-27 11:25:37 +00:00
$return = " $function () " ;
2013-07-24 23:26:41 +00:00
} elseif ( preg_match ( '~^current_(date|timestamp)$~' , $function )) {
2010-05-17 22:49:37 +00:00
$return = $function ;
2018-02-20 15:27:40 +00:00
} elseif ( preg_match ( '~^([+-]|\|\|)$~' , $function )) {
2009-07-27 11:25:37 +00:00
$return = idf_escape ( $name ) . " $function $return " ;
2013-07-24 23:26:41 +00:00
} elseif ( preg_match ( '~^[+-] interval$~' , $function )) {
2018-01-11 15:21:33 +00:00
$return = idf_escape ( $name ) . " $function " . ( preg_match ( " ~^( \\ d+|'[0-9.: -]') [A-Z_]+ \$ ~i " , $value ) ? $value : $return );
2013-07-24 23:26:41 +00:00
} elseif ( preg_match ( '~^(addtime|subtime|concat)$~' , $function )) {
2009-07-27 11:25:37 +00:00
$return = " $function ( " . idf_escape ( $name ) . " , $return ) " ;
2013-07-24 23:26:41 +00:00
} elseif ( preg_match ( '~^(md5|sha1|password|encrypt)$~' , $function )) {
2009-07-27 11:25:37 +00:00
$return = " $function ( $return ) " ;
}
2012-09-09 05:56:34 +00:00
return unconvert_field ( $field , $return );
2009-07-23 16:22:50 +00:00
}
2013-07-24 20:00:37 +00:00
2009-10-02 12:21:55 +00:00
/** Returns export output options
2010-10-29 15:11:00 +00:00
* @ return array
2009-10-02 12:21:55 +00:00
*/
2010-10-29 15:11:00 +00:00
function dumpOutput () {
2009-10-02 12:21:55 +00:00
$return = array ( 'text' => lang ( 'open' ), 'file' => lang ( 'save' ));
if ( function_exists ( 'gzencode' )) {
$return [ 'gz' ] = 'gzip' ;
}
2010-10-29 15:11:00 +00:00
return $return ;
2009-10-02 12:21:55 +00:00
}
2013-07-24 20:00:37 +00:00
2009-10-02 12:21:55 +00:00
/** Returns export format options
2011-12-22 06:08:18 +00:00
* @ return array empty to disable export
2009-10-02 12:21:55 +00:00
*/
2010-10-29 15:11:00 +00:00
function dumpFormat () {
2010-10-29 16:02:20 +00:00
return array ( 'sql' => 'SQL' , 'csv' => 'CSV,' , 'csv;' => 'CSV;' , 'tsv' => 'TSV' );
2009-10-02 12:21:55 +00:00
}
2013-07-24 20:00:37 +00:00
2013-04-04 01:49:05 +00:00
/** Export database structure
* @ param string
* @ return null prints data
*/
function dumpDatabase ( $db ) {
}
2013-07-24 20:00:37 +00:00
2010-10-29 15:03:02 +00:00
/** Export table structure
* @ param string
* @ param string
2013-04-29 22:42:39 +00:00
* @ param int 0 table , 1 view , 2 temporary view table
2010-10-29 15:03:02 +00:00
* @ return null prints data
*/
2013-04-29 22:42:39 +00:00
function dumpTable ( $table , $style , $is_view = 0 ) {
2010-10-29 15:03:02 +00:00
if ( $_POST [ " format " ] != " sql " ) {
echo " \xef \xbb \xbf " ; // UTF-8 byte order mark
if ( $style ) {
dump_csv ( array_keys ( fields ( $table )));
}
2015-03-06 17:36:20 +00:00
} else {
2013-04-29 22:42:39 +00:00
if ( $is_view == 2 ) {
$fields = array ();
foreach ( fields ( $table ) as $name => $field ) {
$fields [] = idf_escape ( $name ) . " $field[full_type] " ;
}
$create = " CREATE TABLE " . table ( $table ) . " ( " . implode ( " , " , $fields ) . " ) " ;
} else {
2018-01-22 13:33:21 +00:00
$create = create_sql ( $table , $_POST [ " auto_increment " ], $style );
2013-04-29 22:42:39 +00:00
}
2015-03-06 17:36:20 +00:00
set_utf8mb4 ( $create );
if ( $style && $create ) {
2013-04-29 22:42:39 +00:00
if ( $style == " DROP+CREATE " || $is_view == 1 ) {
echo " DROP " . ( $is_view == 2 ? " VIEW " : " TABLE " ) . " IF EXISTS " . table ( $table ) . " ; \n " ;
2010-10-29 15:03:02 +00:00
}
2013-04-29 22:42:39 +00:00
if ( $is_view == 1 ) {
2012-08-09 16:39:12 +00:00
$create = remove_definer ( $create );
2010-10-29 15:03:02 +00:00
}
2013-04-04 01:49:05 +00:00
echo " $create ; \n \n " ;
2010-10-29 15:03:02 +00:00
}
}
}
2013-07-24 20:00:37 +00:00
2010-10-29 15:03:02 +00:00
/** Export table data
* @ param string
* @ param string
2010-10-29 15:11:00 +00:00
* @ param string
2010-10-29 15:03:02 +00:00
* @ return null prints data
*/
2010-10-29 15:11:00 +00:00
function dumpData ( $table , $style , $query ) {
2010-10-29 15:03:02 +00:00
global $connection , $jush ;
$max_packet = ( $jush == " sqlite " ? 0 : 1048576 ); // default, minimum is 1024
if ( $style ) {
2011-02-15 16:22:50 +00:00
if ( $_POST [ " format " ] == " sql " ) {
2013-04-27 05:11:58 +00:00
if ( $style == " TRUNCATE+INSERT " ) {
echo truncate_sql ( $table ) . " ; \n " ;
}
2011-02-15 16:22:50 +00:00
$fields = fields ( $table );
}
2010-10-29 15:11:00 +00:00
$result = $connection -> query ( $query , 1 ); // 1 - MYSQLI_USE_RESULT //! enum and set as numbers
2010-10-29 15:03:02 +00:00
if ( $result ) {
$insert = " " ;
$buffer = " " ;
2012-08-09 15:57:45 +00:00
$keys = array ();
2012-08-12 05:59:20 +00:00
$suffix = " " ;
2013-04-27 05:57:44 +00:00
$fetch_function = ( $table != '' ? 'fetch_assoc' : 'fetch_row' );
while ( $row = $result -> $fetch_function ()) {
2012-08-09 15:57:45 +00:00
if ( ! $keys ) {
2012-08-12 05:59:20 +00:00
$values = array ();
2012-08-09 15:57:45 +00:00
foreach ( $row as $val ) {
$field = $result -> fetch_field ();
$keys [] = $field -> name ;
2012-08-12 05:59:20 +00:00
$key = idf_escape ( $field -> name );
$values [] = " $key = VALUES( $key ) " ;
2012-08-09 15:57:45 +00:00
}
2012-08-12 05:59:20 +00:00
$suffix = ( $style == " INSERT+UPDATE " ? " \n ON DUPLICATE KEY UPDATE " . implode ( " , " , $values ) : " " ) . " ; \n " ;
2012-08-09 15:57:45 +00:00
}
2010-10-29 15:03:02 +00:00
if ( $_POST [ " format " ] != " sql " ) {
2011-02-15 16:22:50 +00:00
if ( $style == " table " ) {
2012-08-09 15:57:45 +00:00
dump_csv ( $keys );
2011-02-15 16:22:50 +00:00
$style = " INSERT " ;
}
2010-10-29 15:03:02 +00:00
dump_csv ( $row );
} else {
if ( ! $insert ) {
2012-08-09 15:57:45 +00:00
$insert = " INSERT INTO " . table ( $table ) . " ( " . implode ( " , " , array_map ( 'idf_escape' , $keys )) . " ) VALUES " ;
2010-10-29 15:03:02 +00:00
}
foreach ( $row as $key => $val ) {
2013-04-27 05:57:44 +00:00
$field = $fields [ $key ];
2013-04-03 17:24:13 +00:00
$row [ $key ] = ( $val !== null
2019-08-20 07:47:01 +00:00
? unconvert_field ( $field , preg_match ( number_type (), $field [ " type " ]) && ! preg_match ( '~\[~' , $field [ " full_type " ]) && is_numeric ( $val ) ? $val : q (( $val === false ? 0 : $val )))
2013-04-03 17:24:13 +00:00
: " NULL "
);
2010-10-29 15:03:02 +00:00
}
2012-08-12 05:59:20 +00:00
$s = ( $max_packet ? " \n " : " " ) . " ( " . implode ( " , \t " , $row ) . " ) " ;
if ( ! $buffer ) {
$buffer = $insert . $s ;
} elseif ( strlen ( $buffer ) + 4 + strlen ( $s ) + strlen ( $suffix ) < $max_packet ) { // 4 - length specification
$buffer .= " , $s " ;
2010-10-29 15:03:02 +00:00
} else {
2012-08-12 05:59:20 +00:00
echo $buffer . $suffix ;
$buffer = $insert . $s ;
2010-10-29 15:03:02 +00:00
}
}
}
2012-08-12 05:59:20 +00:00
if ( $buffer ) {
echo $buffer . $suffix ;
2010-10-29 15:03:02 +00:00
}
2010-10-29 15:29:35 +00:00
} elseif ( $_POST [ " format " ] == " sql " ) {
echo " -- " . str_replace ( " \n " , " " , $connection -> error ) . " \n " ;
2010-10-29 15:03:02 +00:00
}
}
}
2013-07-24 20:00:37 +00:00
2012-06-29 21:41:47 +00:00
/** Set export filename
* @ param string
* @ return string filename without extension
*/
function dumpFilename ( $identifier ) {
return friendly_url ( $identifier != " " ? $identifier : ( SERVER != " " ? SERVER : " localhost " ));
}
2013-07-24 20:00:37 +00:00
2010-10-29 15:03:02 +00:00
/** Send headers for export
* @ param string
* @ param bool
* @ return string extension
*/
function dumpHeaders ( $identifier , $multi_table = false ) {
$output = $_POST [ " output " ];
2013-07-24 23:26:41 +00:00
$ext = ( preg_match ( '~sql~' , $_POST [ " format " ]) ? " sql " : ( $multi_table ? " tar " : " csv " )); // multiple CSV packed to TAR
2010-10-29 15:03:02 +00:00
header ( " Content-Type: " .
( $output == " gz " ? " application/x-gzip " :
( $ext == " tar " ? " application/x-tar " :
( $ext == " sql " || $output != " file " ? " text/plain " : " text/csv " ) . " ; charset=utf-8 "
2013-04-29 21:00:35 +00:00
)));
2011-02-17 10:43:21 +00:00
if ( $output == " gz " ) {
2014-02-24 02:02:43 +00:00
ob_start ( 'ob_gzencode' , 1e6 );
2010-10-29 15:03:02 +00:00
}
return $ext ;
2009-10-02 12:21:55 +00:00
}
2013-07-24 20:00:37 +00:00
2017-09-27 16:09:14 +00:00
/** Set the path of the file for webserver load
* @ return string path of the sql dump file
*/
function importServerPath () {
return " adminer.sql " ;
}
2011-01-06 08:30:07 +00:00
/** Print homepage
* @ return bool whether to print default homepage
*/
function homepage () {
2013-07-06 17:31:21 +00:00
echo '<p class="links">' . ( $_GET [ " ns " ] == " " && support ( " database " ) ? '<a href="' . h ( ME ) . 'database=">' . lang ( 'Alter database' ) . " </a> \n " : " " );
2011-06-03 11:49:47 +00:00
echo ( support ( " scheme " ) ? " <a href=' " . h ( ME ) . " scheme='> " . ( $_GET [ " ns " ] != " " ? lang ( 'Alter schema' ) : lang ( 'Create schema' )) . " </a> \n " : " " );
2011-07-12 13:45:55 +00:00
echo ( $_GET [ " ns " ] !== " " ? '<a href="' . h ( ME ) . 'schema=">' . lang ( 'Database schema' ) . " </a> \n " : " " );
2011-06-03 11:50:20 +00:00
echo ( support ( " privileges " ) ? " <a href=' " . h ( ME ) . " privileges='> " . lang ( 'Privileges' ) . " </a> \n " : " " );
2011-01-06 08:30:07 +00:00
return true ;
}
2013-07-24 20:00:37 +00:00
2009-07-27 11:25:37 +00:00
/** Prints navigation after Adminer title
2010-10-18 00:15:58 +00:00
* @ param string can be " auth " if there is no database connection , " db " if there is no database selected , " ns " with invalid schema
2009-07-27 11:25:37 +00:00
* @ return null
*/
function navigation ( $missing ) {
2014-01-30 17:06:58 +00:00
global $VERSION , $jush , $drivers , $connection ;
2009-09-19 20:16:15 +00:00
?>
< h1 >
2011-04-03 15:27:34 +00:00
< ? php echo $this -> name (); ?> <span class="version"><?php echo $VERSION; ?></span>
2018-01-23 11:53:22 +00:00
< a href = " https://www.adminer.org/#download " < ? php echo target_blank (); ?> id="version"><?php echo (version_compare($VERSION, $_COOKIE["adminer_version"]) < 0 ? h($_COOKIE["adminer_version"]) : ""); ?></a>
2009-09-19 20:16:15 +00:00
</ h1 >
< ? php
2010-05-06 12:21:22 +00:00
if ( $missing == " auth " ) {
2019-07-19 11:52:24 +00:00
$output = " " ;
2013-07-05 15:28:37 +00:00
foreach (( array ) $_SESSION [ " pwds " ] as $vendor => $servers ) {
2010-05-06 12:21:22 +00:00
foreach ( $servers as $server => $usernames ) {
foreach ( $usernames as $username => $password ) {
2012-05-14 06:54:07 +00:00
if ( $password !== null ) {
2013-07-05 15:28:37 +00:00
$dbs = $_SESSION [ " db " ][ $vendor ][ $server ][ $username ];
2012-09-09 04:29:16 +00:00
foreach (( $dbs ? array_keys ( $dbs ) : array ( " " )) as $db ) {
2019-07-19 11:52:24 +00:00
$output .= " <li><a href=' " . h ( auth_url ( $vendor , $server , $username , $db )) . " '>( $drivers[$vendor] ) " . h ( $username . ( $server != " " ? " @ " . $this -> serverName ( $server ) : " " ) . ( $db != " " ? " - $db " : " " )) . " </a> \n " ;
2012-09-09 04:29:16 +00:00
}
2010-05-06 12:21:22 +00:00
}
}
}
}
2019-07-19 11:52:24 +00:00
if ( $output ) {
echo " <ul id='logins'> \n $output </ul> \n " . script ( " mixin(qs('#logins'), { onmouseover: menuOver, onmouseout: menuOut}); " );
}
2010-05-06 12:21:22 +00:00
} else {
2021-02-12 08:53:20 +00:00
$tables = array ();
2014-01-30 17:06:58 +00:00
if ( $_GET [ " ns " ] !== " " && ! $missing && DB != " " ) {
$connection -> select_db ( DB );
$tables = table_status ( '' , true );
}
2018-01-13 15:25:11 +00:00
echo script_src ( " ../externals/jush/modules/jush.js " );
echo script_src ( " ../externals/jush/modules/jush-textarea.js " );
echo script_src ( " ../externals/jush/modules/jush-txt.js " );
echo script_src ( " ../externals/jush/modules/jush-js.js " );
2017-03-10 07:56:58 +00:00
if ( support ( " sql " )) {
2018-01-13 15:25:11 +00:00
echo script_src ( " ../externals/jush/modules/jush- $jush .js " );
2017-03-10 07:56:58 +00:00
?>
2018-01-13 21:17:00 +00:00
< script < ? php echo nonce (); ?> >
2014-01-30 17:06:58 +00:00
< ? php
if ( $tables ) {
$links = array ();
foreach ( $tables as $table => $type ) {
$links [] = preg_quote ( $table , '/' );
}
echo " var jushLinks = { $jush : [ ' " . js_escape ( ME ) . ( support ( " table " ) ? " table= " : " select= " ) . " \$ &', / \\ b( " . implode ( " | " , $links ) . " ) \\ b/g ] }; \n " ;
foreach ( array ( " bac " , " bra " , " sqlite_quo " , " mssql_bra " ) as $val ) {
echo " jushLinks. $val = jushLinks. $jush ; \n " ;
}
}
2018-02-08 22:47:16 +00:00
$server_info = $connection -> server_info ;
2014-01-30 17:06:58 +00:00
?>
2018-02-20 15:27:40 +00:00
bodyLoad ( '<?php echo (is_object($connection) ? preg_replace(' ~^ ( \d\ . ? \d ) .*~ s ', ' \1 ', $server_info) : ""); ?>' < ? php echo ( preg_match ( '~MariaDB~' , $server_info ) ? " , true " : " " ); ?> );
2014-01-30 17:06:58 +00:00
</ script >
2014-01-30 16:27:56 +00:00
< ? php
}
2013-07-09 07:21:48 +00:00
$this -> databasesPrint ( $missing );
2010-10-19 15:29:27 +00:00
if ( DB == " " || ! $missing ) {
2023-07-14 20:19:15 +00:00
echo " <p class='links'> " . ( support ( " sql " ) ? " <a href=' " . h ( ME ) . " sql=' " . bold ( isset ( $_GET [ " sql " ]) && ! isset ( $_GET [ " import " ])) . " > " . lang ( 'SQL command' ) . " </a> \n | <a href=' " . h ( ME ) . " import=' " . bold ( isset ( $_GET [ " import " ])) . " > " . lang ( 'Import' ) . " </a> \n " : " " ) . " " ;
2010-10-19 15:29:27 +00:00
if ( support ( " dump " )) {
2023-07-14 20:19:15 +00:00
echo " | <a href=' " . h ( ME ) . " dump= " . urlencode ( isset ( $_GET [ " table " ]) ? $_GET [ " table " ] : $_GET [ " select " ]) . " ' id='dump' " . bold ( isset ( $_GET [ " dump " ])) . " > " . lang ( 'Export' ) . " </a> \n " ;
2010-10-19 15:29:27 +00:00
}
}
2012-08-08 15:41:52 +00:00
if ( $_GET [ " ns " ] !== " " && ! $missing && DB != " " ) {
2023-07-14 20:19:15 +00:00
echo '<br><a href="' . h ( ME ) . 'create="' . bold ( $_GET [ " create " ] === " " ) . " > " . lang ( 'Create table' ) . " </a> \n " ;
2012-07-29 23:05:37 +00:00
if ( ! $tables ) {
echo " <p class='message'> " . lang ( 'No tables.' ) . " \n " ;
} else {
$this -> tablesPrint ( $tables );
}
}
2009-09-19 20:16:15 +00:00
}
}
2013-07-24 20:00:37 +00:00
2012-08-21 05:03:39 +00:00
/** Prints databases list in menu
* @ param string
* @ return null
*/
function databasesPrint ( $missing ) {
2013-10-25 05:16:24 +00:00
global $adminer , $connection ;
2012-08-21 05:03:39 +00:00
$databases = $this -> databases ();
2021-02-07 13:28:16 +00:00
if ( DB && $databases && ! in_array ( DB , $databases )) {
2018-03-01 11:09:10 +00:00
array_unshift ( $databases , DB );
}
2012-08-21 05:03:39 +00:00
?>
< form action = " " >
< p id = " dbs " >
2013-04-04 17:35:15 +00:00
< ? php
2013-06-25 16:58:08 +00:00
hidden_fields_get ();
2018-02-01 21:22:29 +00:00
$db_events = script ( " mixin(qsl('select'), { onmousedown: dbMouseDown, onchange: dbChange}); " );
2018-02-01 21:20:56 +00:00
echo " <span title=' " . lang ( 'database' ) . " '> " . lang ( 'DB' ) . " </span>: " . ( $databases
2018-01-12 12:18:44 +00:00
? " <select name='db'> " . optionlist ( array ( " " => " " ) + $databases , DB ) . " </select> $db_events "
2018-02-01 21:22:29 +00:00
: " <input name='db' value=' " . h ( DB ) . " ' autocapitalize='off'> \n "
2013-06-25 16:58:08 +00:00
);
echo " <input type='submit' value=' " . lang ( 'Use' ) . " ' " . ( $databases ? " class='hidden' " : " " ) . " > \n " ;
2021-02-12 15:08:40 +00:00
if ( support ( " scheme " )) {
if ( $missing != " db " && DB != " " && $connection -> select_db ( DB )) {
2018-01-12 12:18:44 +00:00
echo " <br> " . lang ( 'Schema' ) . " : <select name='ns'> " . optionlist ( array ( " " => " " ) + $adminer -> schemas (), $_GET [ " ns " ]) . " </select> $db_events " ;
2012-08-21 05:03:39 +00:00
if ( $_GET [ " ns " ] != " " ) {
set_schema ( $_GET [ " ns " ]);
}
}
}
2018-03-09 17:18:45 +00:00
foreach ( array ( " import " , " sql " , " schema " , " dump " , " privileges " ) as $val ) {
if ( isset ( $_GET [ $val ])) {
echo " <input type='hidden' name=' $val ' value=''> " ;
break ;
}
}
2012-08-21 05:03:39 +00:00
echo " </p></form> \n " ;
}
2013-07-24 20:00:37 +00:00
2009-09-19 20:16:15 +00:00
/** Prints table list in menu
2013-04-27 03:04:57 +00:00
* @ param array result of table_status ( '' , true )
2009-09-19 20:16:15 +00:00
* @ return null
*/
2009-09-23 15:01:17 +00:00
function tablesPrint ( $tables ) {
2018-01-12 14:27:44 +00:00
echo " <ul id='tables'> " . script ( " mixin(qs('#tables'), { onmouseover: menuOver, onmouseout: menuOut}); " );
2013-04-27 03:04:57 +00:00
foreach ( $tables as $table => $status ) {
2013-07-06 17:31:21 +00:00
$name = $this -> tableName ( $status );
2018-02-08 09:35:54 +00:00
if ( $name != " " ) {
2021-01-26 23:30:44 +00:00
echo '<li><a href="' . h ( ME ) . 'select=' . urlencode ( $table ) . '"'
. bold ( $_GET [ " select " ] == $table || $_GET [ " edit " ] == $table , " select " )
. " title=' " . lang ( 'Select data' ) . " '> " . lang ( 'select' ) . " </a> "
;
2018-02-08 09:35:54 +00:00
echo ( support ( " table " ) || support ( " indexes " )
? '<a href="' . h ( ME ) . 'table=' . urlencode ( $table ) . '"'
. bold ( in_array ( $table , array ( $_GET [ " table " ], $_GET [ " create " ], $_GET [ " indexes " ], $_GET [ " foreign " ], $_GET [ " trigger " ])), ( is_view ( $status ) ? " view " : " structure " ))
. " title=' " . lang ( 'Show structure' ) . " '> $name </a> "
: " <span> $name </span> "
) . " \n " ;
}
2009-07-03 06:26:01 +00:00
}
2014-12-07 23:01:52 +00:00
echo " </ul> \n " ;
2009-07-03 06:26:01 +00:00
}
2013-07-24 20:00:37 +00:00
2009-07-03 04:40:48 +00:00
}