http://osalabs.com
Light standalone PHP script for quick and easy access MySQL databases.
http://phpminiadmin.sourceforge.net
Dual licensed: GPL v2 and MIT, see texts at http://opensource.org/licenses/
*/
$ACCESS_PWD=''; #!!!IMPORTANT!!! this is script access password, SET IT if you want to protect you DB from public access
#DEFAULT db connection settings
# --- WARNING! --- if you set defaults - it's recommended to set $ACCESS_PWD to protect your db!
$DBDEF=array(
'user'=>"",#required
'pwd'=>"", #required
'db'=>"", #optional, default DB
'host'=>"",#optional
'port'=>"",#optional
'chset'=>"utf8",#optional, default charset
);
file_exists($f=dirname(__FILE__) . '/phpminiconfig.php')&&require($f); // Read from config (easier to update)
if (function_exists('date_default_timezone_set')) date_default_timezone_set('UTC');#required by PHP 5.1+
//constants
$VERSION='1.9.160630';
$MAX_ROWS_PER_PAGE=50; #max number of rows in select per one page
$D="\r\n"; #default delimiter for export
$BOM=chr(239).chr(187).chr(191);
$SHOW_D="SHOW DATABASES";
$SHOW_T="SHOW TABLE STATUS";
$DB=array(); #working copy for DB settings
$self=$_SERVER['PHP_SELF'];
session_set_cookie_params(0, null, null, false, true);
session_start();
if (!isset($_SESSION['XSS'])) $_SESSION['XSS']=get_rand_str(16);
$xurl='XSS='.$_SESSION['XSS'];
ini_set('display_errors',0); #turn on to debug db or script issues
error_reporting(E_ALL ^ E_NOTICE);
//strip quotes if they set
if (get_magic_quotes_gpc()){
$_COOKIE=array_map('killmq',$_COOKIE);
$_REQUEST=array_map('killmq',$_REQUEST);
}
if ($_REQUEST['login']){
if ($_REQUEST['pwd']!=$ACCESS_PWD){
$err_msg="Invalid password. Try again";
}else{
$_SESSION['is_logged']=true;
loadcfg();
}
}
if ($_REQUEST['logoff']){
check_xss();
$_SESSION = array();
savecfg();
session_destroy();
$url=$self;
if (!$ACCESS_PWD) $url='/';
header("location: $url");
exit;
}
if (!$_SESSION['is_logged']){
if (!$ACCESS_PWD) {
$_SESSION['is_logged']=true;
loadcfg();
}else{
print_login();
exit;
}
}
if ($_REQUEST['savecfg']){
check_xss();
savecfg();
}
loadsess();
if ($_REQUEST['showcfg']){
print_cfg();
exit;
}
//get initial values
$SQLq=trim(base64_decode($_REQUEST['q']));
$page=$_REQUEST['p']+0;
if ($_REQUEST['refresh'] && $DB['db'] && preg_match('/^show/',$SQLq) ) $SQLq=$SHOW_T;
if (db_connect('nodie')){
$time_start=microtime_float();
if ($_REQUEST['phpinfo']){
ob_start();phpinfo();$sqldr='
'.ob_get_clean().'
';
}else{
if ($DB['db']){
if ($_REQUEST['shex']){
print_export();
}elseif ($_REQUEST['doex']){
check_xss();do_export();
}elseif ($_REQUEST['shim']){
print_import();
}elseif ($_REQUEST['doim']){
check_xss();do_import();
}elseif ($_REQUEST['dosht']){
check_xss();do_sht();
}elseif (!$_REQUEST['refresh'] || preg_match('/^select|show|explain|desc/i',$SQLq) ){
if ($SQLq)check_xss();
do_sql($SQLq);#perform non-select SQL only if not refresh (to avoid dangerous delete/drop)
}
}else{
if ( $_REQUEST['refresh'] ){
check_xss();do_sql($SHOW_D);
}elseif ($_REQUEST['crdb']){
check_xss();do_sql('CREATE DATABASE `'.$_REQUEST['new_db'].'`');do_sql($SHOW_D);
}elseif ( preg_match('/^(?:show\s+(?:databases|status|variables|process)|create\s+database|grant\s+)/i',$SQLq) ){
check_xss();do_sql($SQLq);
}else{
$err_msg="Select Database first";
if (!$SQLq) do_sql($SHOW_D);
}
}
}
$time_all=ceil((microtime_float()-$time_start)*10000)/10000;
print_screen();
}else{
print_cfg();
}
function do_sql($q){
global $dbh,$last_sth,$last_sql,$reccount,$out_message,$SQLq,$SHOW_T;
$SQLq=$q;
if (!do_multi_sql($q)){
$out_message="Error: ".mysqli_error($dbh);
}else{
if ($last_sth && $last_sql){
$SQLq=$last_sql;
if (preg_match("/^select|show|explain|desc/i",$last_sql)) {
if ($q!=$last_sql) $out_message="Results of the last select displayed:";
display_select($last_sth,$last_sql);
} else {
$reccount=mysqli_affected_rows($dbh);
$out_message="Done.";
if (preg_match("/^insert|replace/i",$last_sql)) $out_message.=" Last inserted id=".get_identity();
if (preg_match("/^drop|truncate/i",$last_sql)) do_sql($SHOW_T);
}
}
}
}
function display_select($sth,$q){
global $dbh,$DB,$sqldr,$reccount,$is_sht,$xurl;
$rc=array("o","e");
$dbn=$DB['db'];
$sqldr='';
$is_shd=(preg_match('/^show\s+databases/i',$q));
$is_sht=(preg_match('/^show\s+tables|^SHOW\s+TABLE\s+STATUS/',$q));
$is_show_crt=(preg_match('/^show\s+create\s+table/i',$q));
if ($sth===FALSE or $sth===TRUE) return;#check if $sth is not a mysql resource
$reccount=mysqli_num_rows($sth);
$fields_num=mysqli_field_count($dbh);
$w='';
if ($is_sht || $is_shd) {$w='wa';
$url='?'.$xurl."&db=$dbn";
$sqldr.="
";
}else{
for($i=0;$i<$fields_num;$i++){
$v=$row[$i];
if (is_null($v)) $v="NULL";
elseif (preg_match('/[\x00-\x09\x0B\x0C\x0E-\x1F]+/',$v)) { #all chars <32, except \n\r(0D0A)
$vl=strlen($v);$pf='';
if ($vl>16 && $fields_num>1){#show full dump if just one field
$v=substr($v, 0, 16);$pf='...';
}
$v='BINARY: '.chunk_split(strtoupper(bin2hex($v)),2,' ').$pf;
}else $v=hs($v);
if ($is_show_crt) $v="
$v
";
$sqldr.="
$v".(!strlen($v)?" ":'')."
";
}
}
$sqldr.="
\n";
}
$sqldr.="
\n".$abtn;
}
function print_header(){
global $err_msg,$VERSION,$DB,$dbh,$self,$is_sht,$xurl,$SHOW_T;
$dbn=$DB['db'];
?>
phpMiniAdmin