Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
<?php
/*
DBKiss 1.11 (2011-05-29)
Author: Cezary Tomczak [cagret@gmail.com]
Web site: http://www.gosu.pl/dbkiss/
License: BSD revised (free for any use)
*/
ob_start('ob_gzhandler');
// Some of the features in the SQL editor require creating 'dbkiss_sql' directory,
// where history of queries are kept and other data. If the script has permission
// it will create that directory automatically, otherwise you need to create that
// directory manually and make it writable. You can also set it to empty '' string,
// but some of the features in the sql editor will not work (templates, pagination)
if (!defined('DBKISS_SQL_DIR')) {
define('DBKISS_SQL_DIR', 'dbkiss_sql');
}
/*
An example configuration script that will automatically connect to localhost database.
This is useful on localhost if you don't want to see the "Connect" screen.
mysql_local.php:
---------------------------------------------------------------------
define('COOKIE_PREFIX', str_replace('.php', '', basename(__FILE__)).'_');
define('DBKISS_SQL_DIR', 'dbkiss_mysql');
$cookie = array(
'db_driver' => 'mysql',
'db_server' => 'localhost',
'db_name' => 'test',
'db_user' => 'root',
'db_pass' => 'toor',
'db_charset' => 'latin2',
'page_charset' => 'iso-8859-2',
'remember' => 1
);
foreach ($cookie as $k => $v) {
if ('db_pass' == $k) { $v = base64_encode($v); }
$k = COOKIE_PREFIX.$k;
if (!isset($_COOKIE[$k])) {
$_COOKIE[$k] = $v;
}
}
require './dbkiss.php';
---------------------------------------------------------------------
*/
/*
Changelog:
1.11
* Links in data output are now clickable. Clicking them does not reveal the location of your dbkiss script to external sites.
1.10
* Support for views in Postgresql (mysql had it already).
* Views are now displayed in a seperate listing, to the right of the tables on main page.
* Secure redirection - no referer header sent - when clicking external links (ex. powered by), so that the location of the dbkiss script on your site is not revealed.
1.09
* CSV export in sql editor and table view (feature sponsored by Patrick McGovern)
1.08
* date.timezone E_STRICT error fixed
1.07
* mysql tables with dash in the name generated errors, now all tables in mysql driver are
enquoted with backtick.
1.06
* postgresql fix
1.05
* export of all structure and data does take into account the table name filter on the main page,
so you can filter the tables that you want to export.
1.04
* exporting all structure/data didn't work (ob_gzhandler flush bug)
* cookies are now set using httponly option
* text editor complained about bad cr/lf in exported sql files
(mysql create table uses \n, so insert queries need to be seperated by \n and not \r\n)
1.03
* re-created array_walk_recursive for php4 compatibility
* removed stripping slashes from displayed content
* added favicon (using base64_encode to store the icon in php code, so it is still one-file database browser)
1.02
* works with short_open_tag disabled
* code optimizations/fixes
* postgresql error fix for large tables
1.01
* fix for mysql 3.23, which doesnt understand "LIMIT x OFFSET z"
1.00
* bug fixes
* minor feature enhancements
* this release is stable and can be used in production environment
0.61
* upper casing keywords in submitted sql is disabled (it also modified quoted values)
* sql error when displaying table with 0 rows
* could not connect to database that had upper case characters
*/
// todo: php error handler which cancels buffer output and exits on error
// todo: XSS and CSRF protection.
// todo: connect screen: [x] create database (if not exists) [charset]
// todo: connect screen: database (optional, if none provided will select the first database the user has access to)
// todo: mysqli driver (check if mysql extension is loaded, if not try to use mysqli)
// todo: support for the enum field type when editing row
// todo: search whole database form should appear also on main page
// todo: improve detecting primary keys when editing row (querying information_schema , for mysql > 4)
// todo: when dbkiss_sql dir is missing, display a message in sql editor that some features won't work (templates, pagination) currently it displays a message to create that dir and EXIT, but should allow basic operations
// todo: "Insert" on table view page
// todo: edit table structure
error_reporting(-1);
ini_set('display_errors', true);
if (!ini_get('date.timezone')) {
ini_set('date.timezone', 'Europe/Warsaw');
}
if (isset($_GET['dbkiss_favicon'])) {
$favicon = 'AAABAAIAEBAAAAEACABoBQAAJgAAABAQAAABACAAaAQAAI4FAAAoAAAAEAAAACAAAAABAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP///wDQcRIAAGaZAL5mCwCZ//8Av24SAMVwEgCa//8AvmcLAKn//wAV0/8Awf//AErL5QDGcBIAvnESAHCpxgDf7PIA37aIAMNpDQDHcRIAZO7/AErl/wAdrNYAYMbZAI/1+QDouYkAO+D/AIT4/wDHcBIAjPr/AMJvEgDa//8AQIyzAMNvEgCfxdkA8v//AEzl/wB46fQAMLbZACms1gAAeaYAGou1AJfX6gAYo84AHrLbAN+zhgCXxtkAv/P5AI30+ADv9fkAFH2pABja/wDGaw4AwXASAAVwoQDjuIkAzXARADCmyQAAe64Ade35AMBxEgC+aQ0AAKnGACnw/wAngqwAxW8RABBwnwAAg6wAxW4QAL7w9wCG7PIAHKnSAMFsDwC/ZwwADnWkAASQwgAd1v8Aj7zSAMZvEQDv+fwABXSmABZ+qgAC6fIAAG+iAMhsDwAcz/kAvmsOAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAICAgICOTUTCQQECRMQEQACAgICVUpJEgEfBxRCJ1FOAgEBGgQ4AQEGAQEBDhZWAwICAgEEASIBBgEHFA4WTQMCAgECBAE2AQ8BDw89QDQDAgECAgQBVwEJAQQJPj9TKQIaAQEELgESBgEHHUU6N0QCAgICBA4iBgYfBx1PDUgDAAAAAAMcJQsLGxUeJg0XAwAAAAADHCULCxsVHiYNFwMAAAAAAzwtTDtUAwNLKiwDAAAAAAMoK0YMCggFRxgzAwAAAAADUCQgDAoIBQUFGQMAAAAAQzIkIAwKCAUFBRkDAAAAACNBLzAMCggFMRhSIwAAAAAAERAhAwMDAyEQEQAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPAAAADwAAAA8AAAAPAAAADwAAAA8AAAAPAAAAD4AQAAKAAAABAAAAAgAAAAAQAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMxmAO3MZgDtzGYA7cxmAO3MZgDtymYB78RmBvfCZgj6vmYK/r5mC/++Zgv/vmYK/sJmCPoAZpmPAGaZIAAAAADMZgDtzGYA7cxmAO3MZgDtxmYF9b9nDP/BbA//37aI///////CbxL/xXAS/8dxEv/FbxH/MLbZ/wV0pv8AZplwzGYA7f//////////57aF9r5mC//juIn///////////+/bhL/////////////////xnAS/0rl//8cz/n/AGaZ/8xmAO3MZgDtzGYA7f////++Zgv//////8NvEv//////v24S///////FcBL/x3ES/8ZwEv9K5f//Hdb//wBmmf/MZgDtzGYA7f/////MZgDtvmYL///////BcBL//////75xEv//////vnES/75xEv/AcRL/KfD//xja//8AZpn/zGYA7f/////MZgDtzGYA7b5mC///////vmsO//////++Zwv//////75mC/++Zwv/vmkN/wCpxv8C6fL/AHmm/8xmAO3ntoX2//////////++Zgv/37OG///////ftoj/v24S///////FcBL/x3AS/8VuEP8wpsn/BXCh/wCDrP/MZgDtzGYA7cxmAO3MZgDtvmYL/8ZwEv/DbxL/v24S/79uEv/CbxL/xXAS/8dwEv/GbxH/Ssvl/xyp0v8AZpn/AAAAAAAAAAAAAAAAAAAAAABmmf+E+P//TOX//xXT//8V0///O+D//2Tu//+M+v//eOn0/0rL5f8drNb/AGaZ/wAAAAAAAAAAAAAAAAAAAAAAZpn/hPj//0zl//8V0///FdP//zvg//9k7v//jPr//3jp9P9Ky+X/HazW/wBmmf8AAAAAAAAAAAAAAAAAAAAAAGaZ/3Xt+f8estv/BJDC/wB7rv8Ab6L/AGaZ/wBmmf8OdaT/Gou1/xijzv8AZpn/AAAAAAAAAAAAAAAAAAAAAABmmf8prNb/l9fq/77w9//B////qf///5r///+Z////huzy/2DG2f8Ufan/AGaZ/wAAAAAAAAAAAAAAAAAAAAAAZpn/7/n8//L////a////wf///6n///+a////mf///5n///+Z////j/X5/wBmmf8AAAAAAAAAAAAAAAAAAAAAAGaZ7+/1+f/y////2v///8H///+p////mv///5n///+Z////mf///4/1+f8AZpn/AAAAAAAAAAAAAAAAAAAAAABmmWAngqz/l8bZ/7/z+f/B////qf///5r///+Z////jfT4/2DG2f8Wfqr/AGaZYAAAAAAAAAAAAAAAAAAAAAAAAAAAAGaZIABmmY8AZpm/AGaZ/wBmmf8AZpn/AGaZ/wBmmb8AZpmPAGaZIAAAAAAAAQICAAA1EwAABAkAABEAAAACAgAASRIAAAcUAABRTvAAARrwAAEB8AABAfAAVgPwAAIB8AAiAfAABxT4AU0D';
header('Content-type: image/vnd.microsoft.icon');
echo base64_decode($favicon);
exit();
}
if (!function_exists('array_walk_recursive'))
{
function array_walk_recursive(&$array, $func)
{
foreach ($array as $k => $v) {
if (is_array($v)) {
array_walk_recursive($array[$k], $func);
} else {
$func($array[$k], $k);
}
}
}
}
function create_links($text)
{
// Protocols: http, https, ftp, irc, svn
// Parse emails also?
$text = preg_replace('#([a-z]+://[a-zA-Z0-9\.\,\;\:\[\]\{\}\-\_\+\=\!\@\#\%\&\(\)\/\?\`\~]+)#e', 'create_links_eval("\\1")', $text);
// Excaptions:
// 1) cut last char if link ends with ":" or ";" or "." or "," - cause in 99% cases that char doesnt belong to the link
// (check if previous char was "=" then let it stay cause that could be some variable in a query, some kind of separator)
// (should we add also "-" ? But it is a valid char in links and very common, many links might end with it when creating from some title of an article?)
// 2) brackets, the link could be inside one of 3 types of brackets:
// [http://...] , {http://...}
// and most common: (http://some.com/) OR http://some.com(some description of the link)
// In these cases regular expression will catch: "http://some.com/)" AND "http://some.com(some"
// So when we catch some kind of bracket in the link we will cut it unless there is also a closing bracket in the link:
// We will not cut brackets in this link: http://en.wikipedia.org/wiki/Common_(entertainer) - wikipedia often uses brackets.
return $text;
}
function create_links_eval($link)
{
$orig_link = $link;
$cutted = "";
if (in_array($link[strlen($link)-1], array(":", ";", ".", ","))) {
$link = substr($link, 0, -1);
$cutted = $orig_link[strlen($orig_link)-1];
}
if (($pos = strpos($link, "(")) !== false) {
if (strpos($link, ")") === false) {
$link = substr($link, 0, $pos);
$cutted = substr($orig_link, $pos);
}
} else if (($pos = strpos($link, ")")) !== false) {
if (strpos($link, "(") === false) {
$link = substr($link, 0, $pos);
$cutted = substr($orig_link, $pos);
}
} else if (($pos = strpos($link, "[")) !== false) {
if (strpos($link, "]") === false) {
$link = substr($link, 0, $pos);
$cutted = substr($orig_link, $pos);
}
} else if (($pos = strpos($link, "]")) !== false) {
if (strpos($link, "[") === false) {
$link = substr($link, 0, $pos);
$cutted = substr($orig_link, $pos);
}
} else if (($pos = strpos($link, "{")) !== false) {
if (strpos($link, "}") === false) {
$link = substr($link, 0, $pos);
$cutted = substr($orig_link, $pos);
}
} else if (($pos = strpos($link, "}")) !== false) {
if (strpos($link, "{") === false) {
$link = substr($link, 0, $pos);
$cutted = substr($orig_link, $pos);
}
}
return "<a title=\"$link\" style=\"color: #000; text-decoration: none; border-bottom: #000 1px dotted;\" href=\"javascript:;\" onclick=\"link_noreferer('$link')\">$link</a>$cutted";
}
function truncate_html($string, $length, $break_words = false, $end_str = '..')
{
// Does not break html tags whilte truncating, does not take into account chars inside tags: <b>a</b> = 1 char length.
// Break words is always TRUE - no breaking is not implemented.
// Limits: no handling of <script> tags.
$inside_tag = false;
$inside_amp = 0;
$finished = false; // finished but the loop is still running cause inside tag or amp.
$opened = 0;
$string_len = strlen($string);
$count = 0;
$ret = "";
for ($i = 0; $i < $string_len; $i++)
{
$char = $string[$i];
$nextchar = isset($string[$i+1]) ? $string[$i+1] : null;
if ('<' == $char && ('/' == $nextchar || ctype_alpha($nextchar))) {
if ('/' == $nextchar) {
$opened--;
} else {
$opened++;
}
$inside_tag = true;
}
if ('>' == $char) {
$inside_tag = false;
$ret .= $char;
continue;
}
if ($inside_tag) {
$ret .= $char;
continue;
}
if (!$finished)
{
if ('&' == $char) {
$inside_amp = 1;
$ret .= $char;
continue;
}
if (';' == $char && $inside_amp) {
$inside_amp = 0;
$count++;
$ret .= $char;
continue;
}
if ($inside_amp) {
$inside_amp++;
$ret .= $char;
if ('#' == $char || ctype_alnum($char)) {
if ($inside_amp > 7) {
$count += $inside_amp;
$inside_amp = 0;
}
} else {
$count += $inside_amp;
$inside_amp = 0;
}
continue;
}
}
$count++;
if (!$finished) {
$ret .= $char;
}
if ($count >= $length) {
if (!$inside_tag && !$inside_amp) {
if (!$finished) {
$ret .= $end_str;
$finished = true;
if (0 == $opened) {
break;
}
}
if (0 == $opened) {
break;
}
}
}
}
return $ret;
}
function table_filter($tables, $filter)
{
$filter = trim($filter);
if ($filter) {
foreach ($tables as $k => $table) {
if (!str_has_any($table, $filter, $ignore_case = true)) {
unset($tables[$k]);
}
}
}
return $tables;
}
function get($key, $type='string')
{
if (is_string($key)) {
$_GET[$key] = isset($_GET[$key]) ? $_GET[$key] : null;
if ('float' == $type) $_GET[$key] = str_replace(',','.',$_GET[$key]);
settype($_GET[$key], $type);
if ('string' == $type) $_GET[$key] = trim($_GET[$key]);
return $_GET[$key];
}
$vars = $key;
foreach ($vars as $key => $type) {
$_GET[$key] = isset($_GET[$key]) ? $_GET[$key] : null;
if ('float' == $type) $_GET[$key] = str_replace(',','.',$_GET[$key]);
settype($_GET[$key], $type);
if ('string' == $type) $_GET[$key] = trim($_GET[$key]);
$vars[$key] = $_GET[$key];
}
return $vars;
}
function post($key, $type='string')
{
if (is_string($key)) {
$_POST[$key] = isset($_POST[$key]) ? $_POST[$key] : null;
if ('float' == $type) $_POST[$key] = str_replace(',','.',$_POST[$key]);
settype($_POST[$key], $type);
if ('string' == $type) $_POST[$key] = trim($_POST[$key]);
return $_POST[$key];
}
$vars = $key;
foreach ($vars as $key => $type) {
$_POST[$key] = isset($_POST[$key]) ? $_POST[$key] : null;
if ('float' == $type) $_POST[$key] = str_replace(',','.',$_POST[$key]);
settype($_POST[$key], $type);
if ('string' == $type) $_POST[$key] = trim($_POST[$key]);
$vars[$key] = $_POST[$key];
}
return $vars;
}
$_ENV['IS_GET'] = ('GET' == $_SERVER['REQUEST_METHOD']);
$_ENV['IS_POST'] = ('POST' == $_SERVER['REQUEST_METHOD']);
function req_gpc_has($str)
{
/* finds if value exists in GPC data, used in filter_() functions, to check whether use html_tags_undo() on the data */
foreach ($_GET as $k => $v) {
if ($str == $v) {
return true;
}
}
foreach ($_POST as $k => $v) {
if ($str == $v) {
return true;
}
}
foreach ($_COOKIE as $k => $v) {
if ($str == $v) {
return true;
}
}
return false;
}
if (ini_get('magic_quotes_gpc')) {
ini_set('magic_quotes_runtime', 0);
array_walk_recursive($_GET, 'db_magic_quotes_gpc');
array_walk_recursive($_POST, 'db_magic_quotes_gpc');
array_walk_recursive($_COOKIE, 'db_magic_quotes_gpc');
}
function db_magic_quotes_gpc(&$val)
{
$val = stripslashes($val);
}
$sql_font = 'font-size: 12px; font-family: courier new;';
$sql_area = $sql_font.' width: 708px; height: 182px; border: #ccc 1px solid; background: #f9f9f9; padding: 3px;';
if (!isset($db_name_style)) {
$db_name_style = '';
}
if (!isset($db_name_h1)) {
$db_name_h1 = '';
}
global $db_link, $db_name;
if (!defined('COOKIE_PREFIX')) {
define('COOKIE_PREFIX', 'dbkiss_');
}
define('COOKIE_WEEK', 604800); // 3600*24*7
define('COOKIE_SESS', 0);
function cookie_get($key)
{
$key = COOKIE_PREFIX.$key;
if (isset($_COOKIE[$key])) return $_COOKIE[$key];
return null;
}
function cookie_set($key, $val, $time = COOKIE_SESS)
{
$key = COOKIE_PREFIX.$key;
$expire = $time ? time() + $time : 0;
if (version_compare(PHP_VERSION, '5.2.0', '>=')) {
setcookie($key, $val, $expire, '', '', false, true);
} else {
setcookie($key, $val, $expire);
}
$_COOKIE[$key] = $val;
}
function cookie_del($key)
{
$key = COOKIE_PREFIX.$key;
if (version_compare(PHP_VERSION, '5.2.0', '>=')) {
setcookie($key, '', time()-3600*24, '', '', false, true);
} else {
setcookie($key, '', time()-3600*24);
}
unset($_COOKIE[$key]);
}
conn_modify('db_name');
conn_modify('db_charset');
conn_modify('page_charset');
function conn_modify($key)
{
if (array_key_exists($key, $_GET)) {
cookie_set($key, $_GET[$key], cookie_get('remember') ? COOKIE_WEEK : COOKIE_SESS);
if (isset($_GET['from']) && $_GET['from']) {
header('Location: '.$_GET['from']);
} else {
header('Location: '.$_SERVER['PHP_SELF']);
}
exit;
}
}
$db_driver = cookie_get('db_driver');
$db_server = cookie_get('db_server');
$db_name = cookie_get('db_name');
$db_user = cookie_get('db_user');
$db_pass = base64_decode(cookie_get('db_pass'));
$db_charset = cookie_get('db_charset');
$page_charset = cookie_get('page_charset');
$charset1 = array('latin1', 'latin2', 'utf8', 'cp1250');
$charset2 = array('iso-8859-1', 'iso-8859-2', 'utf-8', 'windows-1250');
$charset1[] = $db_charset;
$charset2[] = $page_charset;
$charset1 = charset_assoc($charset1);
$charset2 = charset_assoc($charset2);
$driver_arr = array('mysql', 'pgsql');
$driver_arr = array_assoc($driver_arr);
function array_assoc($a)
{
$ret = array();
foreach ($a as $v) {
$ret[$v] = $v;
}
return $ret;
}
function charset_assoc($arr)
{
sort($arr);
$ret = array();
foreach ($arr as $v) {
if (!$v) { continue; }
$v = strtolower($v);
$ret[$v] = $v;
}
return $ret;
}
if (isset($_GET['disconnect']) && $_GET['disconnect'])
{
cookie_del('db_pass');
header('Location: '.$_SERVER['PHP_SELF']);
exit;
}
if (!$db_pass || (!$db_driver || !$db_server || !$db_name || !$db_user))
{
if ('POST' == $_SERVER['REQUEST_METHOD'])
{
$db_driver = post('db_driver');
$db_server = post('db_server');
$db_name = post('db_name');
$db_user = post('db_user');
$db_pass = post('db_pass');
$db_charset = post('db_charset');
$page_charset = post('page_charset');
if ($db_driver && $db_server && $db_name && $db_user)
{
$db_test = true;
db_connect($db_server, $db_name, $db_user, $db_pass);
if (is_resource($db_link))
{
$time = post('remember') ? COOKIE_WEEK : COOKIE_SESS;
cookie_set('db_driver', $db_driver, $time);
cookie_set('db_server', $db_server, $time);
cookie_set('db_name', $db_name, $time);
cookie_set('db_user', $db_user, $time);
cookie_set('db_pass', base64_encode($db_pass), $time);
cookie_set('db_charset', $db_charset, $time);
cookie_set('page_charset', $page_charset, $time);
cookie_set('remember', post('remember'), $time);
header('Location: '.$_SERVER['PHP_SELF']);
exit;
}
}
}
else
{
$_POST['db_driver'] = $db_driver;
$_POST['db_server'] = $db_server ? $db_server : 'localhost';
$_POST['db_name'] = $db_name;
$_POST['db_user'] = $db_user;
$_POST['db_charset'] = $db_charset;
$_POST['page_charset'] = $page_charset;
$_POST['db_driver'] = $db_driver;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Connect</title>
<link rel="shortcut icon" href="<?php echo $_SERVER['PHP_SELF']; ?>?dbkiss_favicon=1">
</head>
<body>
<?php layout(); ?>
<h1>Connect</h1>
<?php if (isset($db_test) && is_string($db_test)): ?>
<div style="background: #ffffd7; padding: 0.5em; border: #ccc 1px solid; margin-bottom: 1em;">
<span style="color: red; font-weight: bold;">Error:</span>&nbsp;
<?php echo $db_test;?>
</div>
<?php endif; ?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<table class="ls ls2" cellspacing="1">
<tr>
<th>Driver:</th>
<td><select name="db_driver"><?php echo options($driver_arr, post('db_driver'));?></select></td>
</tr>
<tr>
<th>Server:</th>
<td><input type="text" name="db_server" value="<?php echo post('db_server');?>"></td>
</tr>
<tr>
<th>Database:</th>
<td><input type="text" name="db_name" value="<?php echo post('db_name');?>"></td>
</tr>
<tr>
<th>User:</th>
<td><input type="text" name="db_user" value="<?php echo post('db_user');?>"></td>
</tr>
<tr>
<th>Password:</th>
<td><input type="password" name="db_pass" value=""></td>
</tr>
<tr>
<th>Db charset:</th>
<td><input type="text" name="db_charset" value="<?php echo post('db_charset');?>" size="10"> (optional)</td>
</tr>
<tr>
<th>Page charset:</th>
<td><input type="text" name="page_charset" value="<?php echo post('page_charset');?>" size="10"> (optional)</td>
</tr>
<tr>
<td colspan="2" class="none" style="padding: 0; background: none; padding-top: 0.3em;">
<table cellspacing="0" cellpadding="0"><tr><td>
<input type="checkbox" name="remember" id="remember" value="1" <?php echo checked(post('remember'));?>></td><td>
<label for="remember">remember me on this computer</label></td></tr></table>
</td>
</tr>
<tr>
<td class="none" colspan="2" style="padding-top: 0.4em;"><input type="submit" value="Connect"></td>
</tr>
</table>
</form>
<?php powered_by(); ?>
</body>
</html>
<?php
exit;
}
db_connect($db_server, $db_name, $db_user, $db_pass);
if ($db_charset && 'mysql' == $db_driver) {
db_exe("SET NAMES $db_charset");
}
if (isset($_GET['dump_all']) && 1 == $_GET['dump_all'])
{
dump_all($data = false);
}
if (isset($_GET['dump_all']) && 2 == $_GET['dump_all'])
{
dump_all($data = true);
}
if (isset($_GET['dump_table']) && $_GET['dump_table'])
{
dump_table($_GET['dump_table']);
}
if (isset($_GET['export']) && 'csv' == $_GET['export'])
{
export_csv(base64_decode($_GET['query']), $_GET['separator']);
}
if (isset($_POST['sqlfile']) && $_POST['sqlfile'])
{
$files = sql_files_assoc();
if (!isset($files[$_POST['sqlfile']])) {
exit('File not found. md5 = '.$_POST['sqlfile']);
}
$sqlfile = $files[$_POST['sqlfile']];
layout();
echo '<div>Importing: <b>'.$sqlfile.'</b> ('.size(filesize($sqlfile)).')</div>';
echo '<div>Database: <b>'.$db_name.'</b></div>';
flush();
import($sqlfile, post('ignore_errors'), post('transaction'), post('force_myisam'), post('query_start','int'));
exit;
}
if (isset($_POST['drop_table']) && $_POST['drop_table'])
{
$drop_table_enq = quote_table($_POST['drop_table']);
db_exe('DROP TABLE '.$drop_table_enq);
header('Location: '.$_SERVER['PHP_SELF']);
exit;
}
if (isset($_POST['drop_view']) && $_POST['drop_view'])
{
$drop_view_enq = quote_table($_POST['drop_view']);
db_exe('DROP VIEW '.$drop_view_enq);
header('Location: '.$_SERVER['PHP_SELF']);
exit;
}
function db_connect($db_server, $db_name, $db_user, $db_pass)
{
global $db_driver, $db_link, $db_test;
if (!extension_loaded($db_driver)) {
trigger_error($db_driver.' extension not loaded', E_USER_ERROR);
}
if ('mysql' == $db_driver)
{
$db_link = @mysql_connect($db_server, $db_user, $db_pass);
if (!is_resource($db_link)) {
if ($db_test) {
$db_test = 'mysql_connect() failed: '.db_error();
return;
} else {
cookie_del('db_pass');
cookie_del('db_name');
die('mysql_connect() failed: '.db_error());
}
}
if (!@mysql_select_db($db_name, $db_link)) {
$error = db_error();
db_close();
if ($db_test) {
$db_test = 'mysql_select_db() failed: '.$error;
return;
} else {
cookie_del('db_pass');
cookie_del('db_name');
die('mysql_select_db() failed: '.$error);
}
}
}
if ('pgsql' == $db_driver)
{
$conn = sprintf("host='%s' dbname='%s' user='%s' password='%s'", $db_server, $db_name, $db_user, $db_pass);
$db_link = @pg_connect($conn);
if (!is_resource($db_link)) {
if ($db_test) {
$db_test = 'pg_connect() failed: '.db_error();
return;
} else {
cookie_del('db_pass');
cookie_del('db_name');
die('pg_connect() failed: '.db_error());
}
}
}
register_shutdown_function('db_cleanup');
}
function db_cleanup()
{
db_close();
}
function db_close()
{
global $db_driver, $db_link;
if (is_resource($db_link)) {
if ('mysql' == $db_driver) {
mysql_close($db_link);
}
if ('pgsql' == $db_driver) {
pg_close($db_link);
}
}
}
function db_query($query, $dat = false)
{
global $db_driver, $db_link;
$query = db_bind($query, $dat);
if (!db_is_safe($query)) {
return false;
}
if ('mysql' == $db_driver)
{
$rs = mysql_query($query, $db_link);
return $rs;
}
if ('pgsql' == $db_driver)
{
$rs = pg_query($db_link, $query);
return $rs;
}
}
function db_is_safe($q, $ret = false)
{
// currently only checks UPDATE's/DELETE's if WHERE condition is not missing
$upd = 'update';
$del = 'delete';
$q = ltrim($q);
if (strtolower(substr($q, 0, strlen($upd))) == $upd
|| strtolower(substr($q, 0, strlen($del))) == $del) {
if (!preg_match('#\swhere\s#i', $q)) {
if ($ret) {
return false;
} else {
trigger_error(sprintf('db_is_safe() failed. Detected UPDATE/DELETE without WHERE condition. Query: %s.', $q), E_USER_ERROR);
return false;
}
}
}
return true;
}
function db_exe($query, $dat = false)
{
$rs = db_query($query, $dat);
db_free($rs);
}
function db_one($query, $dat = false)
{
$row = db_row_num($query, $dat);
if ($row) {
return $row[0];
} else {
return false;
}
}
function db_row($query, $dat = false)
{
global $db_driver, $db_link;
if ('mysql' == $db_driver)
{
if (is_resource($query)) {
$rs = $query;
return mysql_fetch_assoc($rs);
} else {
$query = db_limit($query, 0, 1);
$rs = db_query($query, $dat);
$row = mysql_fetch_assoc($rs);
db_free($rs);
if ($row) {
return $row;
}
}
return false;
}
if ('pgsql' == $db_driver)
{
if (is_resource($query) || is_object($query)) {
$rs = $query;
return pg_fetch_assoc($rs);
} else {
$query = db_limit($query, 0, 1);
$rs = db_query($query, $dat);
$row = pg_fetch_assoc($rs);
db_free($rs);
if ($row) {
return $row;
}
}
return false;
}
}
function db_row_num($query, $dat = false)
{
global $db_driver, $db_link;
if ('mysql' == $db_driver)
{
if (is_resource($query)) {
$rs = $query;
return mysql_fetch_row($rs);
} else {
$rs = db_query($query, $dat);
if (!$rs) {
/*
echo '<pre>';
print_r($rs);
echo "\r\n";
print_r($query);
echo "\r\n";
print_r($dat);
exit;
*/
}
$row = mysql_fetch_row($rs);
db_free($rs);
if ($row) {
return $row;
}
return false;
}
}
if ('pgsql' == $db_driver)
{
if (is_resource($query) || is_object($query)) {
$rs = $query;
return pg_fetch_row($rs);
} else {
$rs = db_query($query, $dat);
$row = pg_fetch_row($rs);
db_free($rs);
if ($row) {
return $row;
}
return false;
}
}
}
function db_list($query)
{
global $db_driver, $db_link;
$rs = db_query($query);
$ret = array();
if ('mysql' == $db_driver) {
while ($row = mysql_fetch_assoc($rs)) {
$ret[] = $row;
}
}
if ('pgsql' == $db_driver) {
while ($row = pg_fetch_assoc($rs)) {
$ret[] = $row;
}
}
db_free($rs);
return $ret;
}
function db_assoc($query)
{
global $db_driver, $db_link;
$rs = db_query($query);
$rows = array();
$num = db_row_num($rs);
if (!is_array($num)) {
return array();
}
if (!array_key_exists(0, $num)) {
return array();
}
if (1 == count($num)) {
$rows[] = $num[0];
while ($num = db_row_num($rs)) {
$rows[] = $num[0];
}
return $rows;
}
if ('mysql' == $db_driver)
{
mysql_data_seek($rs, 0);
}
if ('pgsql' == $db_driver)
{
pg_result_seek($rs, 0);
}
$row = db_row($rs);
if (!is_array($row)) {
return array();
}
if (count($num) < 2) {
trigger_error(sprintf('db_assoc() failed. Two fields required. Query: %s.', $query), E_USER_ERROR);
}
if (count($num) > 2 && count($row) <= 2) {
trigger_error(sprintf('db_assoc() failed. If specified more than two fields, then each of them must have a unique name. Query: %s.', $query), E_USER_ERROR);
}
foreach ($row as $k => $v) {
$first_key = $k;
break;
}
if (count($row) > 2) {
$rows[$row[$first_key]] = $row;
while ($row = db_row($rs)) {
$rows[$row[$first_key]] = $row;
}
} else {
$rows[$num[0]] = $num[1];
while ($num = db_row_num($rs)) {
$rows[$num[0]] = $num[1];
}
}
db_free($rs);
return $rows;
}
function db_limit($query, $offset, $limit)
{
global $db_driver;
$offset = (int) $offset;
$limit = (int) $limit;
$query = trim($query);
if (str_ends_with($query, ';')) {
$query = str_cut_end($query, ';');
}
$query = preg_replace('#^([\s\S]+)LIMIT\s+\d+\s+OFFSET\s+\d+\s*$#i', '$1', $query);
$query = preg_replace('#^([\s\S]+)LIMIT\s+\d+\s*,\s*\d+\s*$#i', '$1', $query);
if ('mysql' == $db_driver) {
// mysql 3.23 doesn't understand "LIMIT x OFFSET z"
return $query." LIMIT $offset, $limit";
} else {
return $query." LIMIT $limit OFFSET $offset";
}
}
function db_escape($value)
{
global $db_driver, $db_link;
if ('mysql' == $db_driver) {
return mysql_real_escape_string($value, $db_link);
}
if ('pgsql' == $db_driver) {
return pg_escape_string($value);
}
}
function db_quote($s)
{
switch (true) {
case is_null($s): return 'NULL';
case is_int($s): return $s;
case is_float($s): return $s;
case is_bool($s): return (int) $s;
case is_string($s): return "'" . db_escape($s) . "'";
case is_object($s): return $s->getValue();
default:
trigger_error(sprintf("db_quote() failed. Invalid data type: '%s'.", gettype($s)), E_USER_ERROR);
return false;
}
}
function db_strlen_cmp($a, $b)
{
if (strlen($a) == strlen($b)) {
return 0;
}
return strlen($a) > strlen($b) ? -1 : 1;
}
function db_bind($q, $dat)
{
if (false === $dat) {
return $q;
}
if (!is_array($dat)) {
//return trigger_error('db_bind() failed. Second argument expects to be an array.', E_USER_ERROR);
$dat = array($dat);
}
$qBase = $q;
// special case: LIKE '%asd%', need to ignore that
$q_search = array("'%", "%'");
$q_replace = array("'\$", "\$'");
$q = str_replace($q_search, $q_replace, $q);
preg_match_all('#%\w+#', $q, $match);
if ($match) {
$match = $match[0];
}
if (!$match || !count($match)) {
return trigger_error('db_bind() failed. No binding keys found in the query.', E_USER_ERROR);
}
$keys = $match;
usort($keys, 'db_strlen_cmp');
$num = array();
foreach ($keys as $key)
{
$key2 = str_replace('%', '', $key);
if (is_numeric($key2)) $num[$key] = true;
if (!array_key_exists($key2, $dat)) {
return trigger_error(sprintf('db_bind() failed. No data found for key: %s. Query: %s.', $key, $qBase), E_USER_ERROR);
}
$q = str_replace($key, db_quote($dat[$key2]), $q);
}
if (count($num)) {
if (count($dat) != count($num)) {
return trigger_error('db_bind() failed. When using numeric data binding you need to use all data passed to the query. You also cannot mix numeric and name binding.', E_USER_ERROR);
}
}
$q = str_replace($q_replace, $q_search, $q);
return $q;
}
function db_free($rs)
{
global $db_driver;
if (db_is_result($rs)) {
if ('mysql' == $db_driver) return mysql_free_result($rs);
if ('pgsql' == $db_driver) return pg_free_result($rs);
}
}
function db_is_result($rs)
{
global $db_driver;
if ('mysql' == $db_driver) return is_resource($rs);
if ('pgsql' == $db_driver) return is_object($rs) || is_resource($rs);
}
function db_error()
{
global $db_driver, $db_link;
if ('mysql' == $db_driver) {
if (is_resource($db_link)) {
if (mysql_error($db_link)) {
return mysql_error($db_link). ' ('. mysql_errno($db_link).')';
} else {
return false;
}
} else {
if (mysql_error()) {
return mysql_error(). ' ('. mysql_errno().')';
} else {
return false;
}
}
}
if ('pgsql' == $db_driver) {
if (is_resource($db_link)) {
return pg_last_error($db_link);
}
}
}
function db_begin()
{
global $db_driver;
if ('mysql' == $db_driver) {
db_exe('SET AUTOCOMMIT=0');
db_exe('BEGIN');
}
if ('pgsql' == $db_driver) {
db_exe('BEGIN');
}
}
function db_end()
{
global $db_driver;
if ('mysql' == $db_driver) {
db_exe('COMMIT');
db_exe('SET AUTOCOMMIT=1');
}
if ('pgsql' == $db_driver) {
db_exe('COMMIT');
}
}
function db_rollback()
{
global $db_driver;
if ('mysql' == $db_driver) {
db_exe('ROLLBACK');
db_exe('SET AUTOCOMMIT=1');
}
if ('pgsql' == $db_driver) {
db_exe('ROLLBACK');
}
}
function db_in_array($arr)
{
$in = '';
foreach ($arr as $v) {
if ($in) $in .= ',';
$in .= db_quote($v);
}
return $in;
}
function db_where($where_array, $field_prefix = null, $omit_where = false)
{
$field_prefix = str_replace('.', '', $field_prefix);
$where = '';
if (count($where_array)) {
foreach ($where_array as $wh_k => $wh)
{
if (is_numeric($wh_k)) {
if ($wh) {
if ($field_prefix && !preg_match('#^\s*\w+\.#i', $wh) && !preg_match('#^\s*\w+\s*\(#i', $wh)) {
$wh = $field_prefix.'.'.trim($wh);
}
if ($where) $where .= ' AND ';
$where .= $wh;
}
} else {
if ($wh_k) {
if ($field_prefix && !preg_match('#^\s*\w+\.#i', $wh_k) && !preg_match('#^\s*\w+\s*\(#i', $wh)) {
$wh_k = $field_prefix.'.'.$wh_k;
}
$wh = db_cond($wh_k, $wh);
if ($where) $where .= ' AND ';
$where .= $wh;
}
}
}
if ($where) {
if (!$omit_where) {
$where = ' WHERE '.$where;
}
}
}
return $where;
}
function db_insert($tbl, $dat)
{
global $db_driver;
if (!count($dat)) {
trigger_error('db_insert() failed. Data is empty.', E_USER_ERROR);
return false;
}
$cols = '';
$vals = '';
$first = true;
foreach ($dat as $k => $v) {
if ($first) {
$cols .= $k;
$vals .= db_quote($v);
$first = false;
} else {
$cols .= ',' . $k;
$vals .= ',' . db_quote($v);
}
}
if ('mysql' == $db_driver) {
$tbl = "`$tbl`";
}
$q = "INSERT INTO $tbl ($cols) VALUES ($vals)";
db_exe($q);
}
// $wh = WHERE condition, might be (string) or (array)
function db_update($tbl, $dat, $wh)
{
global $db_driver;
if (!count($dat)) {
trigger_error('db_update() failed. Data is empty.', E_USER_ERROR);
return false;
}
$set = '';
$first = true;
foreach ($dat as $k => $v) {
if ($first) {
$set .= $k . '=' . db_quote($v);
$first = false;
} else {
$set .= ',' . $k . '=' . db_quote($v);
}
}
if (is_array($wh)) {
$wh = db_where($wh, null, $omit_where = true);
}
if ('mysql' == $db_driver) {
$tbl = "`$tbl`";
}
$q = "UPDATE $tbl SET $set WHERE $wh";
return db_exe($q);
}
function db_insert_id($table = null, $pk = null)
{
global $db_driver, $db_link;
if ('mysql' == $db_driver) {
return mysql_insert_id($_db['conn_id']);
}
if ('pgsql' == $db_driver) {
if (!$table || !$pk) {
trigger_error('db_insert_id(): table & pk required', E_USER_ERROR);
}
$seq_id = $table.'_'.$pk.'_seq';
return db_seq_id($seq_id);
}
}
function db_seq_id($seqName)
{
return db_one('SELECT currval(%seqName)', array('seqName'=>$seqName));
}
function db_cond($k, $v)
{
if (is_null($v)) return sprintf('%s IS NULL', $k);
else return sprintf('%s = %s', $k, db_quote($v));
}
function list_dbs()
{
global $db_driver, $db_link;
if ('mysql' == $db_driver)
{
$result = mysql_query('SHOW DATABASES', $db_link);
$ret = array();
while ($row = mysql_fetch_row($result)) {
$ret[$row[0]] = $row[0];
}
return $ret;
}
if ('pgsql' == $db_driver)
{
return db_assoc('SELECT datname, datname FROM pg_database');
}
}
function views_supported()
{
static $ret;
if (isset($ret)) {
return $ret;
}
global $db_driver, $db_link;
if ('mysql' == $db_driver) {
$version = mysql_get_server_info($db_link);
if (strpos($version, "-") !== false) {
$version = substr($version, 0, strpos($version, "-"));
}
if (version_compare($version, "5.0.2", ">=")) {
// Views are available in 5.0.0 but we need SHOW FULL TABLES
// and the FULL syntax was added in 5.0.2, FULL allows us to
// to distinct between tables & views in the returned list by
// by providing an additional column.
$ret = true;
return true;
} else {
$ret = false;
return false;
}
}
if ('pgsql' == $db_driver) {
$ret = true;
return true;
}
}
function list_tables($views_mode=false)
{
global $db_driver, $db_link, $db_name;
if ($views_mode && !views_supported()) {
return array();
}
static $cache_tables;
static $cache_views;
if ($views_mode) {
if (isset($cache_views)) {
return $cache_views;
}
} else {
if (isset($cache_tables)) {
return $cache_tables;
}
}
static $all_tables; // tables and views
if ('mysql' == $db_driver)
{
if (!isset($all_tables)) {
$all_tables = db_assoc("SHOW FULL TABLES");
// assoc: table name => table type (BASE TABLE or VIEW)
}
// This chunk of code is the same as in pgsql driver.
if ($views_mode) {
$views = array();
foreach ($all_tables as $view => $type) {
if ($type != 'VIEW') { continue; }
$views[] = $view;
}
$cache_views = $views;
return $views;
} else {
$tables = array();
foreach ($all_tables as $table => $type) {
if ($type != 'BASE TABLE') { continue; }
$tables[] = $table;
}
$cache_tables = $tables;
return $tables;
}
}
if ('pgsql' == $db_driver)
{
if (!isset($all_tables)) {
$query = "SELECT table_name, table_type ";
$query .= "FROM information_schema.tables ";
$query .= "WHERE table_schema = 'public' ";
$query .= "AND (table_type = 'BASE TABLE' OR table_type = 'VIEW') ";
$query .= "ORDER BY table_name ";
$all_tables = db_assoc($query);
}
// This chunk of code is the same as in mysql driver.
if ($views_mode) {
$views = array();
foreach ($all_tables as $view => $type) {
if ($type != 'VIEW') { continue; }
$views[] = $view;
}
$cache_views = $views;
return $views;
} else {
$tables = array();
foreach ($all_tables as $table => $type) {
if ($type != 'BASE TABLE') { continue; }
$tables[] = $table;
}
$cache_tables = $tables;
return $tables;
}
}
}
function quote_table($table)
{
global $db_driver;
if ('mysql' == $db_driver) {
return "`$table`";
} else {
return $table;
}
}
function table_structure($table)
{
global $db_driver;
if ('mysql' == $db_driver)
{
$query = "SHOW CREATE TABLE `$table`";
$row = db_row_num($query);
echo $row[1].';';
echo "\n\n";
}
if ('pgsql' == $db_driver)
{
return '';
}
}
function table_data($table)
{
global $db_driver;
set_time_limit(0);
if ('mysql' == $db_driver) {
$query = "SELECT * FROM `$table`";
} else {
$query = "SELECT * FROM $table";
}
$result = db_query($query);
$count = 0;
while ($row = db_row($result))
{
if ('mysql' == $db_driver) {
echo 'INSERT INTO `'.$table.'` VALUES (';
}
if ('pgsql' == $db_driver) {
echo 'INSERT INTO '.$table.' VALUES (';
}
$x = 0;
foreach($row as $key => $value)
{
if ($x == 1) { echo ', '; }
else { $x = 1; }
if (is_numeric($value)) { echo "'".$value."'"; }
elseif (is_null($value)) { echo 'NULL'; }
else { echo '\''. escape($value) .'\''; }
}
echo ");\n";
$count++;
if ($count % 100 == 0) { flush(); }
}
db_free($result);
if ($count) {
echo "\n";
}
}
function table_status()
{
// Size is not supported for Views, only for Tables.
global $db_driver, $db_link, $db_name;
if ('mysql' == $db_driver)
{
$status = array();
$status['total_size'] = 0;
$result = mysql_query("SHOW TABLE STATUS FROM `$db_name`", $db_link);
while ($row = mysql_fetch_assoc($result)) {
if (!is_numeric($row['Data_length'])) {
// Data_length for Views is NULL.
continue;
}
$status['total_size'] += $row['Data_length']; // + Index_length
$status[$row['Name']]['size'] = $row['Data_length'];
$status[$row['Name']]['count'] = $row['Rows'];
}
return $status;
}
if ('pgsql' == $db_driver)
{
$status = array();
$status['total_size'] = 0;
$tables = list_tables(); // only tables, not views
if (!count($tables)) {
return $status;
}
$tables_in = db_in_array($tables);
$rels = db_list("SELECT relname, reltuples, (relpages::decimal + 1) * 8 * 2 * 1024 AS relsize FROM pg_class WHERE relname IN ($tables_in)");
foreach ($rels as $rel) {
$status['total_size'] += $rel['relsize'];
$status[$rel['relname']]['size'] = $rel['relsize'];
$status[$rel['relname']]['count'] = $rel['reltuples'];
}
return $status;
}
}
function table_columns($table)
{
global $db_driver;
static $cache = array();
if (isset($cache[$table])) {
return $cache[$table];
}
if ('mysql' == $db_driver) {
$row = db_row("SELECT * FROM `$table`");
} else {
$row = db_row("SELECT * FROM $table");
}
if (!$row) {
$cache[$table] = array();
return array();
}
foreach ($row as $k => $v) {
$row[$k] = $k;
}
$cache[$table] = $row;
return $row;
}
function table_types($table)
{
global $db_driver;
if ('mysql' == $db_driver)
{
$rows = db_list("SHOW COLUMNS FROM `$table`");
$types = array();
foreach ($rows as $row) {
$type = $row['Type'];
$types[$row['Field']] = $type;
}
return $types;
}
if ('pgsql' == $db_driver)
{
return db_assoc("SELECT column_name, udt_name FROM information_schema.columns WHERE table_name ='$table' ORDER BY ordinal_position");
}
}
function table_types2($table)
{
global $db_driver;
if ('mysql' == $db_driver)
{
$types = array();
$rows = @db_list("SHOW COLUMNS FROM `$table`");
if (!($rows && count($rows))) {
return false;
}
foreach ($rows as $row) {
$type = $row['Type'];
preg_match('#^[a-z]+#', $type, $match);
$type = $match[0];
$types[$row['Field']] = $type;
}
}
if ('pgsql' == $db_driver)
{
$types = db_assoc("SELECT column_name, udt_name FROM information_schema.columns WHERE table_name ='$table' ORDER BY ordinal_position");
if (!count($types)) {
return false;
}
foreach ($types as $col => $type) {
// "_" also in regexp - error when retrieving column info from "pg_class",
// udt_name might be "_aclitem" / "_text".
preg_match('#^[a-z_]+#', $type, $match);
$type = $match[0];
$types[$col] = $type;
}
}
foreach ($types as $col => $type) {
if ('varchar' == $type) { $type = 'char'; }
if ('integer' == $type) { $type = 'int'; }
if ('timestamp' == $type) { $type = 'time'; }
$types[$col] = $type;
}
return $types;
}
function table_types_group($types)
{
foreach ($types as $k => $type) {
preg_match('#^\w+#', $type, $match);
$type = $match[0];
$types[$k] = $type;
}
$types = array_unique($types);
$types = array_values($types);
$types2 = array();
foreach ($types as $type) {
$types2[$type] = $type;
}
return $types2;
}
function table_pk($table)
{
$cols = table_columns($table);
if (!$cols) return null;
foreach ($cols as $col) {
return $col;
}
}
function escape($text)
{
$text = addslashes($text);
$search = array("\r", "\n", "\t");
$replace = array('\r', '\n', '\t');
return str_replace($search, $replace, $text);
}
function ob_cleanup()
{
while (ob_get_level()) {
ob_end_clean();
}
if (headers_sent()) {
return;
}
if (function_exists('headers_list')) {
foreach (headers_list() as $header) {
if (preg_match('/Content-Encoding:/i', $header)) {
header('Content-encoding: none');
break;
}
}
} else {
header('Content-encoding: none');
}
}
function query_color($query)
{
$color = 'red';
$words = array('SELECT', 'UPDATE', 'DELETE', 'FROM', 'LIMIT', 'OFFSET', 'AND', 'LEFT JOIN', 'WHERE', 'SET',
'ORDER BY', 'GROUP BY', 'GROUP', 'DISTINCT', 'COUNT', 'COUNT\(\*\)', 'IS', 'NULL', 'IS NULL', 'AS', 'ON', 'INSERT INTO', 'VALUES', 'BEGIN', 'COMMIT', 'CASE', 'WHEN', 'THEN', 'END', 'ELSE', 'IN', 'NOT', 'LIKE', 'ILIKE', 'ASC', 'DESC', 'LOWER', 'UPPER');
$words = implode('|', $words);
$query = preg_replace("#^({$words})(\s)#i", '<font color="'.$color.'">$1</font>$2', $query);
$query = preg_replace("#(\s)({$words})$#i", '$1<font color="'.$color.'">$2</font>', $query);
// replace twice, some words when preceding other are not replaced
$query = preg_replace("#([\s\(\),])({$words})([\s\(\),])#i", '$1<font color="'.$color.'">$2</font>$3', $query);
$query = preg_replace("#([\s\(\),])({$words})([\s\(\),])#i", '$1<font color="'.$color.'">$2</font>$3', $query);
$query = preg_replace("#^($words)$#i", '<font color="'.$color.'">$1</font>', $query);
preg_match_all('#<font[^>]+>('.$words.')</font>#i', $query, $matches);
foreach ($matches[0] as $k => $font) {
$font2 = str_replace($matches[1][$k], strtoupper($matches[1][$k]), $font);
$query = str_replace($font, $font2, $query);
}
return $query;
}
function query_upper($sql)
{
return $sql;
// todo: don't upper quoted ' and ' values
$queries = preg_split("#;(\s*--[ \t\S]*)?(\r\n|\n|\r)#U", $sql);
foreach ($queries as $k => $query) {
$strip = query_strip($query);
$color = query_color($strip);
$sql = str_replace($strip, $color, $sql);
}
$sql = preg_replace('#<font color="\w+">([^>]+)</font>#iU', '$1', $sql);
return $sql;
}
function html_spaces($string)
{
$inside_tag = false;
for ($i = 0; $i < strlen($string); $i++)
{
$c = $string{$i};
if ('<' == $c) {
$inside_tag = true;
}
if ('>' == $c) {
$inside_tag = false;
}
if (' ' == $c && !$inside_tag) {
$string = substr($string, 0, $i).'&nbsp;'.substr($string, $i+1);
$i += strlen('&nbsp;')-1;
}
}
return $string;
}
function query_cut($query)
{
// removes sub-queries and string values from query
$brace_start = '(';
$brace_end = ')';
$quote = "'";
$inside_brace = false;
$inside_quote = false;
$depth = 0;
$ret = '';
$query = str_replace('\\\\', '', $query);
for ($i = 0; $i < strlen($query); $i++)
{
$prev_char = isset($query{$i-1}) ? $query{$i-1} : null;
$char = $query{$i};
if ($char == $brace_start) {
if (!$inside_quote) {
$depth++;
}
}
if ($char == $brace_end) {
if (!$inside_quote) {
$depth--;
if ($depth == 0) {
$ret .= '(...)';
}
continue;
}
}
if ($char == $quote) {
if ($inside_quote) {
if ($prev_char != '\\') {
$inside_quote = false;
if (!$depth) {
$ret .= "'...'";
}
continue;
}
} else {
$inside_quote = true;
}
}
if (!$depth && !$inside_quote) {
$ret .= $char;
}
}
return $ret;
}
function table_from_query($query)
{
if (preg_match('#\sFROM\s+["`]?(\w+)["`]?#i', $query, $match)) {
$cut = query_cut($query);
if (preg_match('#\sFROM\s+["`]?(\w+)["`]?#i', $cut, $match2)) {
$table = $match2[1];
} else {
$table = $match[1];
}
} else if (preg_match('#UPDATE\s+"?(\w+)"?#i', $query, $match)) {
$table = $match[1];
} else if (preg_match('#INSERT\s+INTO\s+"?(\w+)"?#', $query, $match)) {
$table = $match[1];
} else {
$table = false;
}
return $table;
}
function is_select($query)
{
return preg_match('#^\s*SELECT\s+#i', $query);
}
function query_strip($query)
{
// strip comments and ';' from the end of query
$query = trim($query);
if (str_ends_with($query, ';')) {
$query = str_cut_end($query, ';');
}
$lines = preg_split("#(\r\n|\n|\r)#", $query);
foreach ($lines as $k => $line) {
$line = trim($line);
if (!$line || str_starts_with($line, '--')) {
unset($lines[$k]);
}
}
$query = implode("\r\n", $lines);
return $query;
}
function dump_table($table)
{
ob_cleanup();
define('DEBUG_CONSOLE_HIDE', 1);
set_time_limit(0);
global $db_name;
header("Cache-control: private");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename='.$db_name.'_'.$table.'.sql');
table_structure($table);
table_data($table);
exit;
}
function dump_all($data = false)
{
global $db_name;
ob_cleanup();
define('DEBUG_CONSOLE_HIDE', 1);
set_time_limit(0);
$tables = list_tables();
$table_filter = get('table_filter');
$tables = table_filter($tables, $table_filter);
header("Cache-control: private");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename='.date('Ymd').'_'.$db_name.'.sql');
foreach ($tables as $key => $table)
{
table_structure($table);
if ($data) {
table_data($table);
}
flush();
}
exit;
}
function export_csv($query, $separator)
{
ob_cleanup();
set_time_limit(0);
if (!is_select($query)) {
trigger_error('export_csv() failed: not a SELECT query: '.$query, E_USER_ERROR);
}
$table = table_from_query($query);
if (!$table) {
$table = 'unknown';
}
header("Cache-control: private");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename='.$table.'_'.date('Ymd').'.csv');
$rs = db_query($query);
$first = true;
while ($row = db_row($rs)) {
if ($first) {
echo csv_row(array_keys($row), $separator);
$first = false;
}
echo csv_row($row, $separator);
flush();
}
exit();
}
function csv_row($row, $separator)
{
foreach ($row as $key => $val) {
$enquote = false;
if (false !== strpos($val, $separator)) {
$enquote = true;
}
if (false !== strpos($val, "\"")) {
$enquote = true;
$val = str_replace("\"", "\"\"", $val);
}
if (false !== strpos($val, "\r") || false !== strpos($val, "\n")) {
$enquote = true;
$val = preg_replace('#(\r\n|\r|\n)#', "\n", $val); // excel needs \n instead of \r\n
}
if ($enquote) {
$row[$key] = "\"".$val."\"";
}
}
$out = implode($separator, $row);
$out .= "\r\n";
return $out;
}
function import($file, $ignore_errors = false, $transaction = false, $force_myisam = false, $query_start = false)
{
global $db_driver, $db_link, $db_charset;
if ($ignore_errors && $transaction) {
echo '<div>You cannot select both: ignoring errors and transaction</div>';
exit;
}
$count_errors = 0;
set_time_limit(0);
$fp = fopen($file, 'r');
if (!$fp) { exit('fopen('.$file.') failed'); }
flock($fp, 1);
$text = trim(fread($fp, filesize($file)));
flock($fp, 3);
fclose($fp);
if ($db_charset == 'latin2') {
$text = charset_fix($text);
}
if ($force_myisam) {
$text = preg_replace('#TYPE\s*=\s*InnoDB#i', 'TYPE=MyISAM', $text);
}
$text = preg_split("#;(\r\n|\n|\r)#", $text);
$x = 0;
echo '<div>Ignoring errors: <b>'.($ignore_errors?'Yes':'No').'</b></div>';
echo '<div>Transaction: <b>'.($transaction?'Yes':'No').'</b></div>';
echo '<div>Force MyIsam: <b>'.($force_myisam?'Yes':'No').'</b></div>';
echo '<div>Query start: <b>#'.$query_start.'</b></div>';
echo '<div>Queries found: <b>'.count($text).'</b></div>';
echo '<div>Executing ...</div>';
flush();
if ($transaction) {
echo '<div>BEGIN;</div>';
db_begin();
}
$time = time_start();
$query_start = (int) $query_start;
if (!$query_start) {
$query_start = 1;
}
$query_no = 0;
foreach($text as $key => $value)
{
$x++;
$query_no++;
if ($query_start > $query_no) {
continue;
}
if ('mysql' == $db_driver)
{
$result = @mysql_query($value.';', $db_link);
}
if ('pgsql' == $db_driver)
{
$result = @pg_query($db_link, $value.';');
}
if(!$result) {
$x--;
if (!$count_errors) {
echo '<table class="ls" cellspacing="1"><tr><th width="25%">Error</th><th>Query</th></tr>';
}
$count_errors++;
echo '<tr><td>#'.$query_no.' '.db_error() .')'.'</td><td>'.nl2br(html_once($value)).'</td></tr>';
flush();
if (!$ignore_errors) {
echo '</table>';
echo '<div><span style="color: red;"><b>Import failed.</b></span></div>';
echo '<div>Queries executed: <b>'.($x-$query_start+1).'</b>.</div>';
if ($transaction) {
echo '<div>ROLLBACK;</div>';
db_rollback();
}
echo '<br><div><a href="'.$_SERVER['PHP_SELF'].'?import=1">&lt;&lt; go back</a></div>';
exit;
}
}
}
if ($count_errors) {
echo '</table>';
}
if ($transaction) {
echo '<div>COMMIT;</div>';
db_end();
}
echo '<div><span style="color: green;"><b>Import finished.</b></span></div>';
echo '<div>Queries executed: <b>'.($x-$query_start+1).'</b>.</div>';
echo '<div>Time: <b>'.time_end($time).'</b> sec</div>';
echo '<br><div><a href="'.$_SERVER['PHP_SELF'].'?import=1">&lt;&lt; go back</a></div>';
}
function layout()
{
global $sql_area;
?>
<style>
body,table,input,select,textarea { font-family: tahoma; font-size: 11px; }
body { margin: 1em; padding: 0; margin-top: 0.5em; }
h1, h2 { font-family: arial; margin: 1em 0; }
h1 { font-size: 150%; margin: 0.7em 0; }
h2 { font-size: 125%; }
.ls th { background: #ccc; }
.ls th th { background-color: none; }
.ls td { background: #f5f5f5; }
.ls td td { background-color: none; }
.ls th, .ls td { padding: 0.1em 0.5em; }
.ls th th, .ls td td { padding: 0; }
.ls2 th { text-align: left; vertical-align: top; line-height: 1.7em; background: #e0e0e0; font-weight: normal; }
.ls2 th th { line-height: normal; background-color: none; }
p { margin: 0.8em 0; }
form { margin: 0; }
form th { text-align: left; }
a, a:visited { text-decoration: none; }
a:hover { text-decoration: underline; }
a, a.blue { color: blue; }
a:visited { color: purple; }
a.blue:visited { color: blue; }
form .none td, form .none th { background: none; padding: 0 0.25em; }
label { padding-left: 2px; padding-right: 4px; }
.checkbox { padding-left: 0; margin-left: 0; margin-top: 1px; }
.none, .ls .none { background: none; padding-top: 0.4em; }
.button { cursor: pointer; }
.button_click { background: #e0e0e0; }
.error { background: #ffffd7; padding: 0.5em; border: #ccc 1px solid; margin-bottom: 1em; margin-top: 1em; }
.msg { background: #eee; padding: 0.5em; border: #ccc 1px solid; margin-bottom: 1em; margin-top: 1em; }
.sql_area { <?php echo $sql_area;?> }
div.query { background: #eee; padding: 0.35em; border: #ccc 1px solid; margin-bottom: 1em; margin-top: 1em; }
</style>
<script>
function mark_col(td)
{
}
function popup(url, width, height, more)
{
if (!width) width = 750;
if (!height) height = 500;
var x = (screen.width/2-width/2);
var y = (screen.height/2-height/2);
window.open(url, "", "scrollbars=yes,resizable=yes,width="+width+",height="+height+",screenX="+(x)+",screenY="+y+",left="+x+",top="+y+(more ? ","+more : ""));
}
function is_ie()
{
return navigator.appVersion.indexOf("MSIE") != -1;
}
function event_add(el, event, func)
{
if (is_ie()) {
if (el.attachEvent) {
el.attachEvent("on"+event, func);
}
} else {
if (el.addEventListener) {
el.addEventListener(event, func, false);
} else if (el.attachEvent) {
el.attachEvent("on"+event, func);
} else {
var oldfunc = el["on"+event];
el["on"+event] = function() { oldfunc(); func(); }
}
}
}
function event_target(event)
{
var el;
if (window.event) el = window.event.srcElement;
else if (event) el = event.target;
if (el.nodeType == 3) el = el.parentNode;
return el;
}
function button_init()
{
// dependency: event_add(), event_target()
event_add(window, "load", function() {
for (var i = 0; i < document.forms.length; i++) {
event_add(document.forms[i], "submit", function(event) {
var form = event_target(event);
if (form.tagName != 'FORM') form = this;
for (var k = 0; k < form.elements.length; k++) {
if ("button" == form.elements[k].type || "submit" == form.elements[k].type) {
button_click(form.elements[k], true);
}
}
});
var form = document.forms[i];
for (var j = 0; j < form.elements.length; j++) {
if ("button" == form.elements[j].type || "submit" == form.elements[j].type) {
event_add(form.elements[j], "click", button_click);
}
}
}
var inputs = document.getElementsByTagName('INPUT');
for (var i = 0; i < inputs.length; i++) {
if (('button' == inputs[i].type || 'submit' == inputs[i].type) && !inputs[i].form) {
event_add(inputs[i], 'click', button_click);
}
}
});
}
function button_click(but, calledFromOnSubmit)
{
but = but.nodeName ? but : event_target(but);
if ('button' == this.type || 'submit' == this.type) {
but = this;
}
if (but.getAttribute('button_click') == 1 || but.form && but.form.getAttribute("button_click") == 1) {
return;
}
if (button_click_sess_done(but)) {
return;
}
if ("button" == but.type) {
if (but.getAttribute("wait")) {
button_wait(but);
but.setAttribute("button_click", 1);
if (but.form) {
but.form.setAttribute("button_click", 1); // only when WAIT = other buttons in the form Choose From Pop etc.
}
}
} else if ("submit" == but.type) {
if (but.getAttribute("wait")) {
button_wait(but);
but.setAttribute("button_click", 1);
}
if (but.form) {
but.form.setAttribute("button_click", 1);
}
if (calledFromOnSubmit) {
if (but.getAttribute("block")) {
button_disable(but);
}
} else {
if (!but.form.getAttribute('button_disable_onsubmit'))
{
event_add(but.form, "submit", function(event) {
var form = event_target(event);
if (form.tagName != 'FORM') form = this;
if (!button_disable_sess_done(form)) {
for (var i = 0; i < form.elements.length; i++) {
if (form.elements[i].getAttribute("block")) {
button_disable(form.elements[i]);
}
}
}
});
but.form.setAttribute('button_disable_onsubmit', 1);
}
}
} else {
//return alert("button_click() failed, unknown button type");
}
}
function button_click_sess_done(but)
{
if (but.getAttribute('button_click_sess_done') == 1 || but.form && but.form.getAttribute('button_click_sess_done') == 1) {
if (but.getAttribute('button_click_sess_done') == 1) {
but.setAttribute('button_click_sess_done', 0);
}
if (but.form && but.form.getAttribute('button_click_sess_done') == 1) {
but.form.setAttribute('button_click_sess_done', 0);
}
return true;
}
return false;
}
function button_disable_sess_done(but)
{
if (but.getAttribute('button_disable_sess_done') == 1 || but.form && but.form.getAttribute('button_disable_sess_done') == 1) {
if (but.getAttribute('button_disable_sess_done') == 1) {
but.setAttribute('button_disable_sess_done', 0);
}
if (but.form && but.form.getAttribute('button_disable_sess_done') == 1) {
but.form.setAttribute('button_disable_sess_done', 0);
}
return true;
}
return false;
}
function button_disable(button)
{
button.disabled = true;
if (button.name)
{
var form = button.form;
var input = document.createElement('input');
input.setAttribute('type', 'hidden');
input.setAttribute('name', button.name);
input.setAttribute('value', button.value);
form.appendChild(input);
}
}
function button_wait(but)
{
//but.value += " ..";
but.className = but.className + ' button_click';
}
function button_clear(but)
{
if (but.tagName == 'FORM') {
var form = but;
for (var i = 0; i < form.elements.length; i++) {
button_clear(form.elements[i]);
}
form.setAttribute('button_click', 0);
form.setAttribute('button_click_sess_done', 1);
form.setAttribute('button_disable_sess_done', 1);
} else {
if (but.type == 'submit' || but.type == 'button')
{
if (but.getAttribute('button_click') == 1) {
//but.value = but.value.replace(/[ ]?\.{2,}$/, '');
but.className = but.className.replace('button_click', '');
but.setAttribute('button_click', 0);
but.setAttribute('button_click_sess_done', 1);
but.setAttribute('button_disable_sess_done', 1);
}
if (but.form && but.form.getAttribute('button_click') == 1) {
but.form.setAttribute('button_click', 0);
but.form.setAttribute('button_click_sess_done', 1);
but.form.setAttribute('button_disable_sess_done', 1);
}
}
}
}
button_init();
</script>
<?php
}
function conn_info()
{
global $db_driver, $db_server, $db_name, $db_user, $db_charset, $page_charset, $charset1, $charset2;
$dbs = list_dbs();
$db_name = $db_name;
?>
<p>
Driver: <b><?php echo $db_driver;?></b>
&nbsp;-&nbsp;
Server: <b><?php echo $db_server;?></b>
&nbsp;-&nbsp;
User: <b><?php echo $db_user;?></b>
&nbsp;-&nbsp;
<a class=blue href="<?php echo $_SERVER['PHP_SELF'];?>?execute_sql=1">Execute SQL</a>
( open in <a class=blue href="javascript:void(0)" onclick="popup('<?php echo $_SERVER['PHP_SELF'];?>?execute_sql=1&popup=1')">Popup</a> )
&nbsp;-&nbsp;
Database: <select name="db_name" onchange="location='<?php echo $_SERVER['PHP_SELF'];?>?db_name='+this.value"><?php echo options($dbs, $db_name);?></select>
&nbsp;-&nbsp;
Db charset: <select name="db_charset" onchange="location='<?php echo $_SERVER['PHP_SELF'];?>?db_charset='+this.value+'&from=<?php echo urlencode($_SERVER['REQUEST_URI']);?>'">
<option value=""></option><?php echo options($charset1, $db_charset);?></select>
&nbsp;-&nbsp;
Page charset: <select name="page_charset" onchange="location='<?php echo $_SERVER['PHP_SELF'];?>?page_charset='+this.value+'&from=<?php echo urlencode($_SERVER['REQUEST_URI']);?>'">
<option value=""></option><?php echo options($charset2, $page_charset);?></select>
&nbsp;-&nbsp;
<a class=blue href="<?php echo $_SERVER['PHP_SELF'];?>?disconnect=1">Disconnect</a>
</p>
<?php
}
function size($bytes)
{
return number_format(ceil($bytes / 1024),0,'',',').' KB';
}
function html($s)
{
$html = array(
'&' => '&amp;',
'<' => '&lt;',
'>' => '&gt;',
'"' => '&quot;',
'\'' => '&#039;'
);
$s = preg_replace('/&#(\d+)/', '@@@@@#$1', $s);
$s = str_replace(array_keys($html), array_values($html), $s);
$s = preg_replace('/@@@@@#(\d+)/', '&#$1', $s);
return trim($s);
}
function html_undo($s)
{
$html = array(
'&' => '&amp;',
'<' => '&lt;',
'>' => '&gt;',
'"' => '&quot;',
'\'' => '&#039;'
);
return str_replace(array_values($html), array_keys($html), $s);
}
function html_once($s)
{
$s = str_replace(array('&lt;','&gt;','&amp;lt;','&amp;gt;'),array('<','>','&lt;','&gt;'),$s);
return str_replace(array('&lt;','&gt;','<','>'),array('&amp;lt;','&amp;gt;','&lt;','&gt;'),$s);
}
function html_tags($s)
{
// succession of str_replace array is important! double escape bug..
return str_replace(array('&lt;','&gt;','<','>'), array('&amp;lt;','&amp;gt;','&lt;','&gt;'), $s);
}
function html_tags_undo($s)
{
return str_replace(array('&lt;','&gt;','&amp;lt;', '&amp;gt;'), array('<','>','&lt;','&gt;'), $s);
}
function html_allow_tags($s, $allow)
{
$s = html_once(trim($s));
preg_match_all('#<([a-z]+)>#i', $allow, $match);
foreach ($match[1] as $tag) {
$s = preg_replace('#&lt;'.$tag.'\s+style\s*=\s*&quot;([^"<>]+)&quot;\s*&gt;#i', '<'.$tag.' style="$1">', $s);
$s = str_replace('&lt;'.$tag.'&gt;', '<'.$tag.'>', $s);
$s = str_replace('&lt;/'.$tag.'&gt;', '</'.$tag.'>', $s);
}
return $s;
}
function str_truncate($string, $length, $etc = ' ..', $break_words = true)
{
if ($length == 0) {
return '';
}
if (strlen($string) > $length + strlen($etc)) {
if (!$break_words) {
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
}
return substr($string, 0, $length) . $etc;
}
return $string;
}
function str_bind($s, $dat = array(), $strict = false, $recur = 0)
{
if (!is_array($dat)) {
return trigger_error('str_bind() failed. Second argument expects to be an array.', E_USER_ERROR);
}
if ($strict) {
foreach ($dat as $k => $v) {
if (strpos($s, "%$k%") === false) {
return trigger_error(sprintf('str_bind() failed. Strict mode On. Key not found = %s. String = %s. Data = %s.', $k, $s, print_r($dat, 1)), E_USER_ERROR);
}
$s = str_replace("%$k%", $v, $s);
}
if (preg_match('#%\w+%#', $s, $match)) {
return trigger_error(sprintf('str_bind() failed. Unassigned data for = %s. String = %s.', $match[0], $sBase), E_USER_ERROR);
}
return $s;
}
$sBase = $s;
preg_match_all('#%\w+%#', $s, $match);
$keys = $match[0];
$num = array();
foreach ($keys as $key)
{
$key2 = str_replace('%', '', $key);
if (is_numeric($key2)) $num[$key] = true;
/* ignore!
if (!array_key_exists($key2, $dat)) {
return trigger_error(sprintf('str_bind() failed. No data found for key: %s. String: %s.', $key, $sBase), E_USER_ERROR);
}
*/
$val = $dat[$key2];
/* insecure!
if (preg_match('#%\w+%#', $val) && $recur < 5) {
$val = str_bind($val, $dat, $strict, ++$recur);
}
*/
$s = str_replace($key, $val, $s);
}
if (count($num)) {
if (count($dat) != count($num)) {
return trigger_error('str_bind() failed. When using numeric data binding you need to use all data passed to the string. You also cannot mix numeric and name binding.', E_USER_ERROR);
}
}
if (preg_match('#%\w+%#', $s, $match)) {
/* ignore! return trigger_error(sprintf('str_bind() failed. Unassigned data for = %s. String = %s. Data = %s.', $match[0], htmlspecialchars(print_r($sBase, true)), print_r($dat, true)), E_USER_ERROR);*/
}
return $s;
}
function dir_read($dir, $ignore_ext = array(), $allow_ext = array(), $sort = null)
{
if (is_null($ignore_ext)) $ignore_ext = array();
if (is_null($allow_ext)) $allow_ext = array();
foreach ($allow_ext as $k => $ext) {
$allow_ext[$k] = str_replace('.', '', $ext);
}
$ret = array();
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false) {
if ($file != '.' && $file != '..') {
$ignore = false;
foreach ($ignore_ext as $ext) {
if (file_ext_has($file, $ext)) {
$ignore = true;
}
}
if (is_array($allow_ext) && count($allow_ext) && !in_array(file_ext($file), $allow_ext)) {
$ignore = true;
}
if (!$ignore) {
$ret[] = array(
'file' => $dir.'/'.$file,
'time' => filemtime($dir.'/'.$file)
);
}
}
}
closedir($handle);
}
if ('date_desc' == $sort) {
$ret = array_sort_desc($ret, 'time');
}
return array_col($ret, 'file');
}
function array_col($arr, $col)
{
$ret = array();
foreach ($arr as $k => $row) {
$ret[] = $row[$col];
}
return $ret;
}
function array_sort($arr, $col_key)
{
if (is_array($col_key)) {
foreach ($arr as $k => $v) {
$arr[$k]['__array_sort'] = '';
foreach ($col_key as $col) {
$arr[$k]['__array_sort'] .= $arr[$k][$col].'_';
}
}
$col_key = '__array_sort';
}
uasort($arr, create_function('$a,$b', 'if (is_null($a["'.$col_key.'"]) && !is_null($b["'.$col_key.'"])) return 1; if (!is_null($a["'.$col_key.'"]) && is_null($b["'.$col_key.'"])) return -1; return strnatcasecmp($a["'.$col_key.'"], $b["'.$col_key.'"]);'));
if ('__array_sort' == $col_key) {
foreach ($arr as $k => $v) {
unset($arr[$k]['__array_sort']);
}
}
return $arr;
}
function array_sort_desc($arr, $col_key)
{
if (is_array($col_key)) {
foreach ($arr as $k => $v) {
$arr[$k]['__array_sort'] = '';
foreach ($col_key as $col) {
$arr[$k]['__array_sort'] .= $arr[$k][$col].'_';
}
}
$col_key = '__array_sort';
}
uasort($arr, create_function('$a,$b', 'return strnatcasecmp($b["'.$col_key.'"], $a["'.$col_key.'"]);'));
if ('__array_sort' == $col_key) {
foreach ($arr as $k => $v) {
unset($arr[$k]['__array_sort']);
}
}
return $arr;
}
function options($options, $selected = null, $ignore_type = false)
{
$ret = '';
foreach ($options as $k => $v) {
//str_replace('"', '\"', $k)
$ret .= '<option value="'.$k.'"';
if ((is_array($selected) && in_array($k, $selected)) || (!is_array($selected) && $k == $selected && $selected !== '' && $selected !== null)) {
if ($ignore_type) {
$ret .= ' selected="selected"';
} else {
if (!(is_numeric($k) xor is_numeric($selected))) {
$ret .= ' selected="selected"';
}
}
}
$ret .= '>'.$v.' </option>';
}
return $ret;
}
function sql_files()
{
$files = dir_read('.', null, array('.sql'));
$files2 = array();
foreach ($files as $file) {
$files2[md5($file)] = $file.sprintf(' (%s)', size(filesize($file)));
}
return $files2;
}
function sql_files_assoc()
{
$files = dir_read('.', null, array('.sql'));
$files2 = array();
foreach ($files as $file) {
$files2[md5($file)] = $file;
}
return $files2;
}
function file_ext($name)
{
$ext = null;
if (($pos = strrpos($name, '.')) !== false) {
$len = strlen($name) - ($pos+1);
$ext = substr($name, -$len);
if (!preg_match('#^[a-z0-9]+$#i', $ext)) {
return null;
}
}
return $ext;
}
function checked($bool)
{
if ($bool) return 'checked="checked"';
}
function radio_assoc($checked, $assoc, $input_name, $link = false)
{
$ret = '<table cellspacing="0" cellpadding="0"><tr>';
foreach ($assoc as $id => $name)
{
$params = array(
'id' => $id,
'name' => $name,
'checked' => checked($checked == $id),
'input_name' => $input_name
);
if ($link) {
if (is_array($link)) {
$params['link'] = $link[$id];
} else {
$params['link'] = sprintf($link, $id, $name);
}
$ret .= str_bind('<td><input class="checkbox" type="radio" name="%input_name%" id="%input_name%_%id%" value="%id%" %checked%></td><td>%link%&nbsp;</td>', $params);
} else {
$ret .= str_bind('<td><input class="checkbox" type="radio" name="%input_name%" id="%input_name%_%id%" value="%id%" %checked%></td><td><label for="%input_name%_%id%">%name%</label>&nbsp;</td>', $params);
}
}
$ret .= '</tr></table>';
return $ret;
}
function self($cut_query = false)
{
$uri = $_SERVER['REQUEST_URI'];
if ($cut_query) {
$before = str_before($uri, '?');
if ($before) {
return $before;
}
}
return $uri;
}
function url($script, $params = array())
{
$query = '';
/* remove from script url, actual params if exist */
foreach ($params as $k => $v) {
$exp = sprintf('#(\?|&)%s=[^&]*#i', $k);
if (preg_match($exp, $script)) {
$script = preg_replace($exp, '', $script);
}
}
/* repair url like 'script.php&id=12&asd=133' */
$exp = '#\?\w+=[^&]*#i';
$exp2 = '#&(\w+=[^&]*)#i';
if (!preg_match($exp, $script) && preg_match($exp2, $script)) {
$script = preg_replace($exp2, '?$1', $script, 1);
}
foreach ($params as $k => $v) {
if (!strlen($v)) continue;
if ($query) { $query .= '&'; }
else {
if (strpos($script, '?') === false) {
$query .= '?';
} else {
$query .= '&';
}
}
if ('%s' != $v) {
$v = urlencode($v);
}
$v = preg_replace('#%25(\w+)%25#i', '%$1%', $v); // %id_news% etc. used in listing
$query .= sprintf('%s=%s', $k, $v);
}
return $script.$query;
}
function url_offset($offset, $params = array())
{
$url = $_SERVER['REQUEST_URI'];
if (preg_match('#&offset=\d+#', $url)) {
$url = preg_replace('#&offset=\d+#', '&offset='.$offset, $url);
} else {
$url .= '&offset='.$offset;
}
return $url;
}
function str_wrap($s, $width, $break = ' ', $omit_tags = false)
{
//$restart = array(' ', "\t", "\r", "\n");
$restart = array();
$cnt = 0;
$ret = '';
$open_tag = false;
$inside_link = false;
for ($i=0; $i<strlen($s); $i++)
{
$char = $s[$i];
$nextchar = isset($s[$i+1]) ? $s[$i+1] : null;
$nextchar2 = isset($s[$i+2]) ? $s[$i+2] : null;
if ($omit_tags)
{
if ($char == '<') {
$open_tag = true;
if ('a' == $nextchar) {
$inside_link = true;
} else if ('/' == $nextchar && 'a' == $nextchar2) {
$inside_link = false;
}
}
if ($char == '>') {
$open_tag = false;
}
if ($open_tag) {
$ret .= $char;
continue;
}
}
if (in_array($char, $restart)) {
$cnt = 0;
} else {
$cnt++;
}
$ret .= $char;
if ($cnt > $width) {
if (!$inside_link) {
// Inside link, do not break it.
$ret .= $break;
$cnt = 0;
}
}
}
return $ret;
}
function time_micro()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function time_start()
{
return time_micro();
}
function time_end($start)
{
$end = time_micro();
$end = round($end - $start, 3);
$end = pad_zeros($end, 3);
return $end;
}
function str_has($str, $needle, $ignore_case = false)
{
if (is_array($needle)) {
foreach ($needle as $n) {
if (!str_has($str, $n, $ignore_case)) {
return false;
}
}
return true;
}
if ($ignore_case) {
$str = str_lower($str);
$needle = str_lower($needle);
}
return strpos($str, $needle) !== false;
}
function str_has_any($str, $arr_needle, $ignore_case = false)
{
if (is_string($arr_needle)) {
$arr_needle = preg_replace('#\s+#', ' ', $arr_needle);
$arr_needle = explode(' ', $arr_needle);
}
foreach ($arr_needle as $needle) {
if (str_has($str, $needle, $ignore_case)) {
return true;
}
}
return false;
}
function str_before($str, $needle)
{
$pos = strpos($str, $needle);
if ($pos !== false) {
$before = substr($str, 0, $pos);
return strlen($before) ? $before : false;
} else {
return false;
}
}
function pad_zeros($number, $zeros)
{
if (str_has($number, '.')) {
preg_match('#\.(\d+)$#', $number, $match);
$number .= str_repeat('0', $zeros-strlen($match[1]));
return $number;
} else {
return $number.'.'.str_repeat('0', $zeros);
}
}
function charset_fix_invalid($s)
{
$fix = '€â“„¢ž˜™”Ã';
$s = str_replace(str_array($fix), '', $s);
return $s;
}
function charset_is_invalid($s)
{
$fix = '€â“„¢ž˜™”Ã';
$fix = str_array($fix);
foreach ($fix as $char) {
if (str_has($s, $char)) {
return true;
}
}
return false;
}
function charset_fix($string)
{
// UTF-8 && WIN-1250 => ISO-8859-2
// todo: is checking required? redundant computing?
if (charset_win_is($string)) {
$string = charset_win_fix($string);
}
if (charset_utf_is($string)) {
$string = charset_utf_fix($string);
}
return $string;
}
function charset_win_is($string)
{
$win = '¹¥æÆêʳ£ñÑóӜŒŸ¿¯';