181 lines
4.7 KiB
PHP
181 lines
4.7 KiB
PHP
<?php
|
|
|
|
if (!isset($db_host) || strlen($db_host) == 0) return;
|
|
if (!isset($db_uname) || strlen($db_uname) == 0) return;
|
|
if (!isset($db_pass) || strlen($db_pass) == 0) return;
|
|
if (!isset($db_dbname) || strlen($db_dbname) == 0) return;
|
|
|
|
|
|
// Connect to the database, and return any errors
|
|
$_DB = mysql_connect($db_host, $db_uname, $db_pass)
|
|
or die("Cannot connect to database host: " . mysql_error());
|
|
mysql_select_db($db_dbname)
|
|
or die("The \"$db_dbname\" database does not exist: " . mysql_error());
|
|
mysql_query("SET AUTOCOMMIT=1");
|
|
|
|
// Define error-handling sql-query function
|
|
if (!function_exists('db_query')) {
|
|
function db_query($query) {
|
|
$r = mysql_query($query);
|
|
if (is_resource($r)) {
|
|
// mysql_query("COMMIT");
|
|
return($r);
|
|
}
|
|
if (!$r) {
|
|
$SQLlog = fopen("sql_error.log","a");
|
|
$date = date("Y-m-d H:i:s",time());
|
|
$file = $_SERVER["SCRIPT_FILENAME"];
|
|
|
|
$msg = "";
|
|
$msg .= "$date: $query\n";
|
|
$msg .= "$date: " . mysql_error() . "\n";
|
|
$msg .= "$date: $file\n";
|
|
|
|
if (isset($GLOBALS['action'])) $msg .= "$date: action - " . $GLOBALS['action'] . "\n";
|
|
if (isset($GLOBALS['naction'])) $msg .= "$date: naction - " . $GLOBALS['naction'] . "\n";
|
|
|
|
if (isset($_SERVER['HTTP_HOST']) && strlen($_SERVER['HTTP_HOST'])) {
|
|
$msg .= "$date: Host - " . $_SERVER['HTTP_HOST'] . "\n";
|
|
$msg .= "\n";
|
|
fwrite($SQLlog,$msg);
|
|
|
|
if (function_exists('getLanguageText')) $error_message = getLanguageText('ERRORMSG-DATABASE-ERROR') . " - $date";
|
|
else $error_message = "DB Error - $date";
|
|
header("Location: /?error_message=".urlencode($error_message));
|
|
exit;
|
|
}
|
|
else {
|
|
$msg .= "\n";
|
|
fwrite($SQLlog,$msg);
|
|
echo("DB ERROR - $date\n");
|
|
}
|
|
exit;
|
|
}
|
|
return(true);
|
|
}
|
|
}
|
|
|
|
// sql query function to return a single row
|
|
if (!function_exists('db_getVal')) {
|
|
function db_getVal($query) {
|
|
$r = db_query($query);
|
|
if ($r && is_resource($r) && mysql_num_rows($r) == 1) {
|
|
$data = mysql_fetch_array($r,MYSQL_ASSOC);
|
|
if (count($data) == 1) $data = implode($data);
|
|
}
|
|
else {
|
|
$data = '';
|
|
}
|
|
return $data;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('db_fetch_assoc')) {
|
|
function db_fetch_assoc($results) {
|
|
return(mysql_fetch_array($results,MYSQL_ASSOC));
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_fetch_array')) {
|
|
function db_fetch_array($results) {
|
|
return(mysql_fetch_array($results));
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_num_rows')) {
|
|
function db_num_rows($results) {
|
|
return(mysql_num_rows($results));
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_escape_string')) {
|
|
function db_escape_string($string) {
|
|
return(mysql_real_escape_string($string));
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_affected_rows')) {
|
|
function db_affected_rows() {
|
|
return(mysql_affected_rows());
|
|
}
|
|
}
|
|
|
|
if (!function_exists('db_insert_id')) {
|
|
function db_insert_id() {
|
|
return(mysql_insert_id());
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_error')) {
|
|
function db_error() {
|
|
return(mysql_error());
|
|
}
|
|
}
|
|
|
|
|
|
// check if table exists
|
|
if (!function_exists('db_tableExists')) {
|
|
function db_tableExists($table) {
|
|
$r = db_query("SHOW TABLES LIKE '$table'");
|
|
if (db_num_rows($r) == 1) return(true);
|
|
else return(false);
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_close')) {
|
|
function db_close() {
|
|
global $_DB;
|
|
return(mysql_close($_DB));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Define query logging function
|
|
if (!function_exists('db_query_log')) {
|
|
function db_query_log($query,$id) {
|
|
$GLOBALS['userID'] = $_SESSION['UserInfo']['userid'];
|
|
$GLOBALS['userNUM'] = $_SESSION['UserInfo']['record_num'];
|
|
$GLOBALS['userIP'] = $_SESSION['UserInfo']['user_ip'];
|
|
$GLOBALS['activityTIME'] = time();
|
|
$GLOBALS['activityDATE'] = date("Y-m-d H:i:s",$activity_time);
|
|
$GLOBALS['queryID'] = $id;
|
|
|
|
extract($GLOBALS);
|
|
|
|
$q = "";
|
|
$table = new Table("query_log");
|
|
$q = $table->BuildInsert();
|
|
db_query($q);
|
|
$r = mysql_insert_id();
|
|
|
|
db_query("UPDATE query_log SET q = '" . mysql_real_escape_string($q) . "' WHERE record_num = $r");
|
|
}
|
|
}
|
|
|
|
// sql function to get/set config values
|
|
if (!function_exists('db_getConfig')) {
|
|
function db_getConfig($config) {
|
|
return db_getVal("SELECT value FROM configs WHERE name='$config' LIMIT 1");
|
|
}
|
|
}
|
|
|
|
if (!function_exists('db_setConfig')) {
|
|
function db_setConfig($config, $value) {
|
|
$config = mysql_escape_string($config);
|
|
$value = mysql_escape_string($value);
|
|
db_query("REPLACE INTO configs SET name='{$config}', value='{$value}'");
|
|
}
|
|
}
|
|
|
|
// Set collation
|
|
db_query("SET NAMES 'utf8'");
|
|
|
|
?>
|