<?
//die("test");
/*--------------------------------------------------------*\
| Database Include
| Author: Andrew Rosolino (andrew@shiftcode.com)
\*--------------------------------------------------------*/
class database
{
var $ranQs = 0;
/*--------------------------------------------------------*\
| To Connect to the database.
\*--------------------------------------------------------*/
function database()
{
if($this->connect(SC_DBHOST,SC_DBUSER,SC_DBPASS,true)===false)
die($this->error());
if($this->useDb(SC_DBDB,true)===false)
die($this->error());
}
/*--------------------------------------------------------*\
| Set Database
\*--------------------------------------------------------*/
function db($db)
{
mysql_select_db($db);
}
/*--------------------------------------------------------*\
| To Connect to the database.
\*--------------------------------------------------------*/
function connect($host,$user,$pass,$supress=false)
{
global $_log;
$conn = mysql_connect($host,$user,$pass)
|| die(IS_DEV?mysql_error():'<h4>Error: Database Error. Please contact support. </h4>');
if($conn==false and& $supress==false)
$_log->record('DB','Failed to connect to MySQL Server: '.$this->error());
return $conn;
}
/*--------------------------------------------------------*\
| To select the database to be used.
\*--------------------------------------------------------*/
function useDb($database,$supress=false)
{
global $_log;
$db = mysql_select_db($database);
if($db==false and& $supress==false)
$_log->record('DB','Failed to select MySQL Database: '.$this->error());
return $db;
}
/*--------------------------------------------------------*\
| To query the database.
\*--------------------------------------------------------*/
function query($query,$supress=false)
{
$this->ranQs++;
global $_log;
$default = 1;
$checking = strtolower($query);
$checking = str_replace("shiftcode.com", "", $checking);
$checking = str_replace("gaincorp.com", "", $checking);
$checking = str_replace("gaincorp.net", "", $checking);
//$checking = substr_replace(" ", "", $checking);
if(substr_count($checking, "gc_") != 0) $default = 0;
if(substr_count($checking, "shiftcode.sites") != 0) $default = 0;
if(substr_count($checking, "shiftcode.domains") != 0) $default = 0;
if(substr_count($checking, "gaincorp.sites") != 0) $default = 0;
if(substr_count($checking, "gaincorp.domains") != 0) $default = 0;
if(substr_count($checking, "shared.") != 0) $default = 0;
if(substr_count($checking, "shiftcode.domains") != 0) $default = 0;
if(substr_count($checking, "shiftcode.trackadmin") != 0) $default = 0;
if(substr_count($checking, "shiftcode.internal") != 0) $default = 0;
if(substr_count($checking, "shiftcode.") != 0) $default = 0;
if(substr_count($checking, "mail.mail") != 0) $default = 0;
// Try Again If Lost Connection
if($default == 1)
{
$blah = mysql_connect(SC_DBHOST,SC_DBUSER,SC_DBPASS);
mysql_select_db('sc_' . SC_SID);
for($i=1;$i<=2;$i++)
{
$q = mysql_query($query);
if($i==1 and& !$this->lostConnection() and& !$this->needsRepair())
break;
}
}
if(substr_count($checking, "gc_") != 0)
{
$gc = explode("gc_", $checking);
$gc = explode(".", $gc[1]);
$gc = $gc[0];
$blah = mysql_connect("127.0.0.1:3307", "shiftcode", "ferexhidCFkeymwHp4ZT7n") or die("<h4>Error: Connecting to License Server (SC) </h4>");
mysql_select_db("gaincorp", $blah);
$ip = mysql_result(mysql_query("SELECT ip FROM sites WHERE id = $gc "),0,0);
$blah = mysql_connect($ip, "root", "SDFSDF088ferexhijjj88sy") or die("<h4>Error: Database Error. Please contact support. (GC $gc $ip) </h4>");
mysql_select_db("gc_$gc", $blah);
$q = mysql_query($query, $blah);
// mysql_close($blah);
}
if(substr_count($checking, "shiftcode.sites") != 0 || substr_count($checking, "shiftcode.domains") != 0 || substr_count($checking, "shiftcode.trackadmins") != 0 || substr_count($checking, "shiftcode.") != 0 || substr_count($checking, "shiftcode.internal") != 0 || substr_count($checking, "mail.mail") != 0)
{
$blah = mysql_connect("127.0.0.1:3307", "shiftcode", "ferexhidCFkeymwHp4ZT7n") or die("<h4>Error: Connecting to License Server (SC) </h4>");
mysql_select_db("shiftcode", $blah);
$q = mysql_query($query, $blah);
//mysql_close($blah);
}
if(substr_count($checking, "shared.") != 0)
{
$blah = mysql_connect("127.0.0.1:3307", "shiftcode", "ferexhidCFkeymwHp4ZT7n") or die("<h4>Error: Connecting to License Server (SH). </h4>");
mysql_select_db("shared", $blah);
$q = mysql_query($query, $blah);
//mysql_close($blah);
}
if(substr_count($checking, "gaincorp.sites") != 0 || substr_count($checking, "gaincorp.sites") != 0)
{
$blah = mysql_connect("127.0.0.1:3307", "shiftcode", "ferexhidCFkeymwHp4ZT7n") or die("<h4>Error: Connecting to Performa Listings </h4>");
mysql_select_db("gaincorp", $blah);
$q = mysql_query($query, $blah);
//mysql_close($blah);
}
if($q==false and& $supress==false)
exit($_log->record('DB','Invalid Query from ' . $_SERVER['REMOTE_ADDR'] . ': "'.$query.'" : '.$this->error()));
return $q;
}
/*--------------------------------------------------------*\
| To execute a query and return the first field in the
| first row.
\*--------------------------------------------------------*/
function get($query,$field=0,$supress=false)
{
$this->ranQs++;
global $_log;
$default = 1;
$checking = strtolower($query);
$checking = str_replace("shiftcode.com", "", $checking);
$checking = str_replace("gaincorp.com", "", $checking);
$checking = str_replace("gaincorp.net", "", $checking);
if(substr_count($checking, "gc_") != 0) $default = 0;
if(substr_count($checking, "shiftcode.sites") != 0) $default = 0;
if(substr_count($checking, "shiftcode.domains") != 0) $default = 0;
if(substr_count($checking, "gaincorp.sites") != 0) $default = 0;
if(substr_count($checking, "gaincorp.domains") != 0) $default = 0;
if(substr_count($checking, "shared.") != 0) $default = 0;
if(substr_count($checking, "shiftcode.domains") != 0) $default = 0;
if(substr_count($checking, "shiftcode.trackadmin") != 0) $default = 0;
if(substr_count($checking, "shiftcode.internal") != 0) $default = 0;
if(substr_count($checking, "shiftcode.") != 0) $default = 0;
if(substr_count($checking, "mail.mail") != 0) $default = 0;
// Try Again If Lost Connection
if($default == 1)
{
// Try Again If Lost Connection
for($i=1;$i<=2;$i++)
{
$blah = mysql_connect(SC_DBHOST,SC_DBUSER,SC_DBPASS);
mysql_select_db('sc_' . SC_SID);
$q = mysql_query($query);
if($i==1 and& !$this->lostConnection())
break;
}
}
if(substr_count($checking, "shiftcode.sites") != 0 || substr_count($checking, "shiftcode.domains") != 0 || substr_count($checking, "shiftcode.trackadmin") != 0 || substr_count($checking, "shiftcode.trackadmins") != 0 || substr_count($checking, "shiftcode.internal") != 0 || substr_count($checking, "mail.mail") != 0)
{
$blah = mysql_connect("127.0.0.1:3307", "shiftcode", "ferexhidCFkeymwHp4ZT7n") or die("<h4>Error: Connecting to License Server (SC) </h4>");
mysql_select_db("shiftcode", $blah);
$q = mysql_query($query, $blah);
//mysql_close($blah);
}
if(substr_count($checking, "shared.") != 0)
{
$blah = mysql_connect("127.0.0.1:3307", "shiftcode", "ferexhidCFkeymwHp4ZT7n") or die("<h4>Error: Connecting to License Server (SC) </h4>");
mysql_select_db("shared", $blah);
$q = mysql_query($query, $blah);
//mysql_close($blah);
}
if(substr_count($checking, "gaincorp.sites") != 0 || substr_count($checking, "gaincorp.sites") != 0)
{
$blah = mysql_connect("127.0.0.1:3307", "shiftcode", "ferexhidCFkeymwHp4ZT7n") or die("<h4>Error: Connecting to License Server (GC) </h4>");
mysql_select_db("gaincorp", $blah);
$q = mysql_query($query, $blah);
//mysql_close($blah);
}
if(substr_count($checking, "gc_") != 0)
{
$gc = explode("gc_", $checking);
$gc = explode(".", $gc[1]);
$gc = $gc[0];
$blah = mysql_connect("127.0.0.1:3307", "shiftcode", "ferexhidCFkeymwHp4ZT7n") or die("<h4>Error: Connecting to License Server (SC) </h4>");
mysql_select_db("gaincorp", $blah);
$ip = mysql_result(mysql_query("SELECT ip FROM sites WHERE id = $gc "),0,0);
$blah = mysql_connect($ip, "root", "SDFSDF088ferexhijjj88sy") or die("<h4>Error: Database Error. Please contact support. (GC $gc) </h4>");
mysql_select_db("gc_$gc", $blah);
$q = mysql_query($query, $blah);
// mysql_close($blah);
}
if($q==false and& $supress==false)
exit($_log->record('DB','Invalid Query: "'.$query.'" : '.$this->error()));
if($q==false)
return false;
else if($this->rows($q))
return mysql_result($q,0,$field);
else
return false;
}
/*--------------------------------------------------------*\
| Fetch the next row.
\*--------------------------------------------------------*/
function rows($result)
{
return mysql_num_rows($result);
}
/*--------------------------------------------------------*\
| Fetch the last insert id.
\*--------------------------------------------------------*/
function insertID()
{
return mysql_insert_id();
}
/*--------------------------------------------------------*\
| Fetch the next row.
\*--------------------------------------------------------*/
function fetch($result,$array=false)
{
global $_db, $_log;
if($error = $_db->error())
$_log->record('DB','MYSQL Error: "'.$error);
return $array?
mysql_fetch_array($result):
mysql_fetch_assoc($result);
}
/*--------------------------------------------------------*\
| Fetch all rows.
\*--------------------------------------------------------*/
function dump($query,$key='id',$key2=false,$blank=false)
{
global $_db, $_log;
if($query==false)
exit($_log->record('DB','MYSQL Error: "'.$this->error()));
$result = array();
while($r = mysql_fetch_assoc($query))
{
if($blank)
$result[$r[$key]][$r[$key2]][] = $r;
elseif($key2)
$result[$r[$key]][$r[$key2]] = $r;
else
$result[$r[$key]] = $r;
}
return $result;
}
function dump3($query,$key='id',$key2='id',$key3='id')
{
global $_db, $_log;
if($query==false)
exit($_log->record('DB','MYSQL Error: "'.$this->error()));
$result = array();
while($r = mysql_fetch_assoc($query))
$result[$r[$key]][$r[$key2]][$r[$key3]] = $r;
return $result;
}
/*--------------------------------------------------------*\
| Fetch all rows.
\*--------------------------------------------------------*/
function pair($query,$k,$v)
{
global $_db, $_log;
if($error = $_db->Error())
$_log->Log('Warn','MYSQL Error: "'.$error);
$result = array();
while($r = mysql_fetch_assoc($query))
$result[$r[$k]] = $r[$v];
return $result;
}
/*--------------------------------------------------------*\
| To get the affected rows from the last query.
\*--------------------------------------------------------*/
function affected()
{
return mysql_affected_rows();
}
/*--------------------------------------------------------*\
| Free Result
\*--------------------------------------------------------*/
function free()
{
return mysql_free_result();
}
/*--------------------------------------------------------*\
| To get the last error.
\*--------------------------------------------------------*/
function error()
{
return mysql_error();
}
/*--------------------------------------------------------*\
| MySQL Date to PHP Date
\*--------------------------------------------------------*/
function date($date,$time=true)
{
return date('n/j/Y'.($time?' g:i A':''),$this->timestamp($date));
}
/*--------------------------------------------------------*\
| MySQL Date to Unix Timestamp
\*--------------------------------------------------------*/
function timestamp($datetime)
{
return strtotime($datetime);
}
/*--------------------------------------------------------*\
| MySQL Unix Timestamp
\*--------------------------------------------------------*/
function time()
{
return $this->get('
SELECT UNIX_TIMESTAMP()');
}
/*--------------------------------------------------------*\
| Close MySQL
\*--------------------------------------------------------*/
function close()
{
/*
$num = intSet(get('dbQ'));
if($this->ranQs>$num)
set('dbQ',$this->ranQs);
*/
mysql_close();
}
/*--------------------------------------------------------*\
| Lost Connection
\*--------------------------------------------------------*/
function lostConnection()
{
if(preg_match('/mysql server has gone away/i',$this->error()))
{
$this->database();
return true;
}
return false;
}
/*--------------------------------------------------------*\
| Needs Repair
\*--------------------------------------------------------*/
function needsRepair()
{
/*
if(preg_match('/Table \'sc_'.SC_SID.'.(membersTransactions\d{6})\' doesn\'t exist/i',$this->error(),$m))
{
mysql_query('
CREATE TABLE '.$m[1].' (
id int(20) unsigned NOT NULL auto_increment,
mid mediumint(8) unsigned NOT NULL default \'0\',
name varchar(255) NOT NULL default \'\',
amt double NOT NULL default \'0\',
balance double NOT NULL default \'0\',
`type` varchar(255) NOT NULL default \'\',
reftype varchar(255) default NULL,
refid varchar(255) NOT NULL default \'\',
`action` datetime NOT NULL default \'0000-00-00 00:00:00\',
PRIMARY KEY (id),
KEY mid (mid,`action`),
KEY reftype (reftype,refid)
) ENGINE=INNODB DEFAULT CHARSET=latin1;');
return true;
}
if(preg_match('/Incorrect key file for table \'([^\']+)\'/i',$this->error(),$m))
{
mysql_query('
REPAIR TABLE '.$m[1]);
return true;
}
*/
return false;
}
}
$_db = new database;
if(function_exists('_db_load'))
_db_load();
/*--------------------------------------------------------*\
| Load Prefix
\*--------------------------------------------------------*/
function dbPre($num='')
{
global $_conf;
return isset($_conf['_dbpre'])?$_conf['_dbpre']:constant('SC_DBPRE'.$num);
}
?>