Code:
<?php
/*
(C) AEwebworks Software Development Ltd., 2002-2004
IMPORTANT: This is a commercial software product and any kind of using it must agree
to the AEwebworks Software Development Ltd. license agreement. It can be found at
http://www.aewebworks.com/license.htm
This notice may not be removed from the source code.
Changes are all in this file unless otherwise noted, search for date of mod,
each mod is commented with first line of change log.
CHANGE LOG:
02.25.05 MOD R2 Fix for SQL Max User connections error
For Shared Server, we may not always get a connection to mysql esp if someone
has errors in their code. This was added to help fix the problem and generate a
nice user message should max connections be reached.
04.15.05 MOD R2 Error Reporting Fucntion With $DEBUG
For Turning Error reporting on and off during sql queries to display or trap
errors for display in web browser.
Other Files Affected
Open: headder.inc.php
Find: $BUG_REPORT_EMAIL
Below Add: $DEBUG = false;
Change $DEBUG to false when running on live server or when you don't
want path and errors to be displayed
================================================================================
db.inc.php Class for data base access
================================================================================
*/
class CMySQL
{
var $host, $sock, $port, $user, $passwd, $db, $connected, $link;
function connect()
{
if ( $this->connected )
return;
if (strlen($this->port)) $this->port = ":".$this->port;
if (strlen($this->sock)) $this->sock = ":".$this->sock;
//-- 02.25.05 MOD R2 Fix for SQL Max User connections error
// $this->link = mysql_pconnect( $this->host . $this->port . $this->sock, $this->user, $this->passwd );
$attempts = 75; // Set the number of Trys to connect to MySQL
$delay = 50000; // Delay in microseconds
for ( $i = 0; $i < $attempts; $i++)
{
if ( !$this->link = @mysql_pconnect( $this->host . $this->port . $this->sock, $this->user, $this->passwd ) )
{
usleep($delay); // Not available on windows before PHP 5.0
}
else
{
break;
}
}
if ( !$this->link )
{
echo "<table align=center border=\"1\" cellpadding=\"2\" cellspacing=\"2\" style=\"border-color:#9C21A5\"><tr><td bgcolor=\"#9C21A5\">";
echo "<font color=white face=\"Arial, Helvetica, sans-serif\"><b>YOUR Community Server NAME HERE is Temporaraily Busy!</b></font>";
echo "</td></tr>";
echo "<tr><td>";
echo "Our YOUR SITE NAME Community is currently in HIGH DEMAND!<br>USE your browers refresh or back button in just few seconds.</b><br>Please, Please, Please try again. We appreciate your patience and we very, very, very SORRY for the inconvenience.<br><br>We are experiencing some growing pains and we know this hurts you, so an Error report has been sent.";
echo "<br>Error Code ( inc 30 )";
echo "</td></tr></table>";
exit;
// EXIT to prevent any other code from running
}
//-- [ END ] 02.25.05 MOD R2 Fix for SQL Max User connections error
if ( $this->link ) $this->connected = TRUE;
else $this->connected = FALSE;
}
function select_db()
{
return mysql_select_db( $this->db );
}
}
$MySQL = new CMySQL;
$MySQL->host = $db['host'];
$MySQL->sock = $db['sock'];
$MySQL->port = $db['port'];
$MySQL->user = $db['user'];
$MySQL->passwd = $db['passwd'];
$MySQL->db = $db['db'];
$MySQL->connected = FALSE;
$MySQL->connect();
$MySQL->select_db();
function db_connect()
{
global $MySQL;
global $BUG_REPORT_EMAIL;
if ( !$MySQL->select_db() )
{
echo PrintErr( "Database activation failed: db_connect" );
mail( $BUG_REPORT_EMAIL, "Error", "Error in $_SERVER[PHP_SELF]: " . mysql_error() );
//-- 04.15.05 MOD R2 Error Reporting Fucntion With $DEBUG
$msg = "Error in $_SERVER[PHP_SELF]: " . mysql_error();
db_report_error( $msg);
//-- [END 04.15.05 MOD R2]
exit;
}
return true;
}
function db_list_tables( $error_checking = 1 )
{
global $BUG_REPORT_EMAIL;
global $MySQL;
$res = mysql_list_tables($MySQL->db);
if ( $error_checking && !$res )
{
echo PrintErr( "Database access error: db_list_tables" );
mail( $BUG_REPORT_EMAIL, "Error", "Error in $_SERVER[PHP_SELF]: " . mysql_error() . "\n" );
//-- 04.15.05 MOD R2 Error Reporting Fucntion With $DEBUG
$msg = "Error in $_SERVER[PHP_SELF]: " . mysql_error();
db_report_error( $msg);
//-- [END 04.15.05 MOD R2]
exit;
}
return $res;
}
function db_get_encoding ( $error_checking = 1 )
{
global $BUG_REPORT_EMAIL;
global $MySQL;
$res = mysql_client_encoding($MySQL->link);
if ( $error_checking && !$res )
{
echo PrintErr( "Database access error: db_get_encoding" );
mail( $BUG_REPORT_EMAIL, "Error", "Error in $_SERVER[PHP_SELF]: " . mysql_error() );
//-- 04.15.05 MOD R2 Error Reporting Fucntion With $DEBUG
$msg = "Error in $_SERVER[PHP_SELF]: " . mysql_error();
db_report_error( $msg);
//-- [END 04.15.05 MOD R2]
exit;
}
return $res;
}
function db_res( $query, $error_checking = 1 )
{
global $BUG_REPORT_EMAIL;
global $MySQL;
$res = mysql_query( $query, $MySQL->link );
if ( $error_checking && !$res )
{
echo PrintErr( "Database access error: db_res" );
mail( $BUG_REPORT_EMAIL, "Error", "Error in $_SERVER[PHP_SELF]: " . mysql_error() . "\nQuery: '$query'" );
//-- 04.15.05 MOD R2 Error Reporting Fucntion With $DEBUG
$msg = "Error in $_SERVER[PHP_SELF]: " . mysql_error() . "\nQuery: '$query'";
db_report_error( $msg);
//-- [END 04.15.05 MOD R2]
exit;
}
return $res;
}
function db_arr( $query, $error_checking = 1 )
{
global $BUG_REPORT_EMAIL;
global $MySQL;
$res = mysql_query( $query, $MySQL->link );
if ( $error_checking && !$res )
{
echo PrintErr( "Database access error: db_arr" );
mail( $BUG_REPORT_EMAIL, "Error", "Error in $_SERVER[PHP_SELF]: " . mysql_error() . "\nQuery: '$query'" );
//-- 04.15.05 MOD R2 Error Reporting Fucntion With $DEBUG
$msg = "Error in $_SERVER[PHP_SELF]: " . mysql_error() . "\nQuery: '$query'";
db_report_error( $msg);
//-- [END 04.15.05 MOD R2]
exit;
}
$arr = mysql_fetch_array( $res );
return $arr;
}
function fill_array( $res )
{
global $MySQL;
if ( !$res )
return false;
$i = 0;
$arr = array();
while( $r = mysql_fetch_array( $res ) )
$arr[$i++] = $r;
return $arr;
}
function getParam( $param_name )
{
if ( !$line = db_arr( "SELECT VALUE FROM GlParams WHERE Name = '$param_name'" ) )
return false;
return $line[VALUE];
}
function getParamDesc( $param_name )
{
if ( !$line = db_arr( "SELECT `desc` FROM GlParams WHERE Name = '$param_name'" ) )
return false;
return $line[desc];
}
function setParam( $param_name, $param_val )
{
$param_val = str_replace_mysql ($param_val);
if ( !$res = db_res( "UPDATE GlParams SET VALUE = '$param_val' WHERE Name = '$param_name'" ) )
return false;
return true;
}
function str_replace_mysql($str)
{
$option = $str;
$option_tmp = "";
$len = strlen($option);
for ($i = 0 ; $i<$len ; ++$i )
{
if ( $i == 0 )
{
if ( $option[$i] == "'" ) $option_tmp .= "\\";;
}
else
{
if ( $option[$i] == "'" && $option[$i-1] != "\\")
{
$option_tmp .= "\\";
}
}
$option_tmp .= $option[$i];
//$option = str_replace("'","\\'",$option);
}
return $option_tmp;
}
function PrintErr($out)
{
$ret = "<table cellspacing=2 cellpadding=2 align=center style=\"border: 1px solid red\"><tr><td bgcolor=red>";
$ret .= "<font color=white><b>Error</b></font>";
$ret .= "</td></tr>";
$ret .= "<tr><td>";
$ret .= $out;
$ret .= "</td></tr></table>";
return $ret;
}
//-- 04.15.05 MOD R2 Error Reporting Fucntion With $DEBUG
function db_report_error($msg){
global $DEBUG;
global $BUG_REPORT_EMAIL;
if ( $DEBUG )
{
trigger_error($msg,E_USER_ERROR);
}
else
{
mail( $BUG_REPORT_EMAIL, "Error", $msg);
}
exit;
}
//-- [END 04.15.05 MOD R2]
if ( !db_connect() )
exit();
?>
Bookmarks