Skip to content

Commit

Permalink
Merge pull request #15 from ilia-emesa/mysqli
Browse files Browse the repository at this point in the history
use mysqli instead of mysql for php5.5 compatibility
  • Loading branch information
samcleaver committed Apr 21, 2016
2 parents 721d459 + ffabec4 commit d24a04f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 74 deletions.
12 changes: 6 additions & 6 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
foreach($phpgsb->usinglists as $value)
{
//Create ADD tables
mysql_query("CREATE TABLE IF NOT EXISTS `$value-a-hosts` (
mysqli_query($phpgsb->getDbLink(), "CREATE TABLE IF NOT EXISTS `$value-a-hosts` (
`ID` int(255) NOT NULL auto_increment,
`Hostkey` varchar(8) NOT NULL,
`Chunknum` int(255) NOT NULL,
Expand All @@ -23,12 +23,12 @@
PRIMARY KEY (`ID`),
KEY `Hostkey` (`Hostkey`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;");
mysql_query("CREATE TABLE IF NOT EXISTS `$value-a-index` (
mysqli_query($phpgsb->getDbLink(), "CREATE TABLE IF NOT EXISTS `$value-a-index` (
`ChunkNum` int(255) NOT NULL auto_increment,
`Chunklen` int(255) NOT NULL default '0',
PRIMARY KEY (`ChunkNum`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;");
mysql_query("CREATE TABLE IF NOT EXISTS `$value-a-prefixes` (
mysqli_query($phpgsb->getDbLink(), "CREATE TABLE IF NOT EXISTS `$value-a-prefixes` (
`ID` int(255) NOT NULL auto_increment,
`Hostkey` varchar(8) NOT NULL,
`Prefix` varchar(255) NOT NULL,
Expand All @@ -37,7 +37,7 @@
KEY `Hostkey` (`Hostkey`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;");
//Create SUB tables
mysql_query("CREATE TABLE IF NOT EXISTS `$value-s-hosts` (
mysqli_query($phpgsb->getDbLink(), "CREATE TABLE IF NOT EXISTS `$value-s-hosts` (
`ID` int(255) NOT NULL auto_increment,
`Hostkey` varchar(8) NOT NULL,
`Chunknum` int(255) NOT NULL,
Expand All @@ -46,12 +46,12 @@
PRIMARY KEY (`ID`),
KEY `Hostkey` (`Hostkey`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;");
mysql_query("CREATE TABLE IF NOT EXISTS `$value-s-index` (
mysqli_query($phpgsb->getDbLink(), "CREATE TABLE IF NOT EXISTS `$value-s-index` (
`ChunkNum` int(255) NOT NULL auto_increment,
`Chunklen` int(255) NOT NULL default '0',
PRIMARY KEY (`ChunkNum`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;");
mysql_query("CREATE TABLE IF NOT EXISTS `$value-s-prefixes` (
mysqli_query($phpgsb->getDbLink(), "CREATE TABLE IF NOT EXISTS `$value-s-prefixes` (
`ID` int(255) NOT NULL auto_increment,
`Hostkey` varchar(8) NOT NULL,
`AddChunkNum` varchar(8) NOT NULL,
Expand Down
142 changes: 74 additions & 68 deletions phpgsb.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ class phpGSB
var $transtarted= false;
var $transenabled=true;
var $pingfilepath=""; //This is the path used to store the ping/last update files. (Must inc. trailing slash)
private $dbLink; // mysql connection link
//GENERIC FUNCTIONS (USED BY BOTH LOOKUP AND UPDATER)
/*Automatically connect to database on calling class*/
function phpGSB($database=false,$username=false,$password=false,$host="localhost",$verbose=true)
function phpGSB($database=false,$username=false,$password=false,$host="localhost",$port=3306,$verbose=true)
{
if(!$verbose)
$this->silent();
$this->outputmsg("phpGSB Loaded");
if($database&&$username)
$this->dbConnect($database,$username,$password,$host);
$this->dbConnect($database,$username,$password,$host,$port);
}
function close()
{
mysql_close();
mysqli_close($this->dbLink);
$this->outputmsg("Closing phpGSB. (Peak Memory: ".(round(memory_get_peak_usage()/1048576,3))."MB)");
}
function silent()
Expand All @@ -55,25 +56,25 @@ function trans_begin()
{
$this->transtarted = true;
$this->outputmsg("Begin MySQL Transaction");
mysql_query("BEGIN");
mysqli_query($this->dbLink, "BEGIN");
}
}
function trans_commit()
{
if($this->transtarted&&mysql_ping()&&$this->transenabled)
if($this->transtarted&&mysqli_ping($this->dbLink)&&$this->transenabled)
{
$this->transtarted = false;
$this->outputmsg("Comitting Transaction");
mysql_query("COMMIT");
mysqli_query($this->dbLink, "COMMIT");
}
}
function trans_rollback()
{
if($this->transtarted&&mysql_ping()&&$this->transenabled)
if($this->transtarted&&mysqli_ping($this->dbLink)&&$this->transenabled)
{
$this->transtarted = false;
$this->outputmsg("Rolling Back Transaction");
mysql_query("ROLLBACK");
mysqli_query($this->dbLink, "ROLLBACK");
}
}
/*Function to output messages, used instead of echo,
Expand Down Expand Up @@ -106,19 +107,23 @@ function fatalerror($msg)
die();
}
/*Wrapper to connect to database. Simples.*/
function dbConnect($database,$username,$password,$host="localhost")
function dbConnect($database,$username,$password,$host="localhost",$port=3306)
{
$link = mysql_connect($host, $username, $password);
if (!$link) {
$this->fatalerror('Could not connect: ' . mysql_error());
$this->dbLink = mysqli_connect($host, $username, $password, '', $port);
if (!$this->dbLink) {
$this->fatalerror('Could not connect: ' . mysqli_error($this->dbLink));
}
$this->outputmsg('Connected successfully to database server');
$db_selected = mysql_select_db($database, $link);
$db_selected = mysqli_select_db($this->dbLink, $database);
if (!$db_selected) {
$this->fatalerror('Can\'t use $database : ' . mysql_error());
$this->fatalerror('Can\'t use $database : ' . mysqli_error($this->dbLink));
}
$this->outputmsg('Connected to database successfully');
}
public function getDbLink()
{
return $this->dbLink;
}
/*Simple logic function to calculate timeout
based on the number of previous errors*/
function calc($errors)
Expand Down Expand Up @@ -216,12 +221,12 @@ function resetDatabase()
mail($this->adminemail,'Reset Database Request Issued','For some crazy unknown reason GSB requested a database reset at '.time());
foreach($this->usinglists as $value)
{
mysql_query("TRUNCATE TABLE `$value-s-index`");
mysql_query("TRUNCATE TABLE `$value-s-hosts`");
mysql_query("TRUNCATE TABLE `$value-s-prefixes`");
mysql_query("TRUNCATE TABLE `$value-a-index`");
mysql_query("TRUNCATE TABLE `$value-a-hosts`");
mysql_query("TRUNCATE TABLE `$value-a-prefixes`");
mysqli_query($this->dbLink, "TRUNCATE TABLE `$value-s-index`");
mysqli_query($this->dbLink, "TRUNCATE TABLE `$value-s-hosts`");
mysqli_query($this->dbLink, "TRUNCATE TABLE `$value-s-prefixes`");
mysqli_query($this->dbLink, "TRUNCATE TABLE `$value-a-index`");
mysqli_query($this->dbLink, "TRUNCATE TABLE `$value-a-hosts`");
mysqli_query($this->dbLink, "TRUNCATE TABLE `$value-a-prefixes`");
}
}
/*Processes data recieved from a GSB data request into a managable array*/
Expand Down Expand Up @@ -413,34 +418,34 @@ function saveChunkPart($data,$type,$listname)
$listtype = 's';
//Insert index value
$indexinsert = implode(', ',$buildindex);
$indexins = mysql_query("INSERT INTO `$listname-$listtype-index` (`ChunkNum`,`Chunklen`) VALUES $indexinsert;");
$error = mysql_error();
$indexins = mysqli_query($this->dbLink, "INSERT INTO `$listname-$listtype-index` (`ChunkNum`,`Chunklen`) VALUES $indexinsert;");
$error = mysqli_error($this->dbLink);
if($indexins)
{
if(count($buildhost)>0)
{
//Insert hostkeys index
$hostinsert = implode(', ',$buildhost);
mysql_query("INSERT INTO `$listname-$listtype-hosts` (`Hostkey`,`Chunknum`,`Count`,`FullHash`) VALUES $hostinsert;");
$error = mysql_error();
mysqli_query($this->dbLink, "INSERT INTO `$listname-$listtype-hosts` (`Hostkey`,`Chunknum`,`Count`,`FullHash`) VALUES $hostinsert;");
$error = mysqli_error($this->dbLink);
if(!empty($error))
$this->outputmsg("INSERTED $listname $type HOST KEYS ".mysql_error());
$this->outputmsg("INSERTED $listname $type HOST KEYS ".mysqli_error($this->dbLink));
}
if(count($buildpairs)>0)
{
//Insert prefixes
$pairinsert = implode(', ',$buildpairs);
if($type=="ADD")
mysql_query("INSERT INTO `$listname-$listtype-prefixes` (`Hostkey`,`Prefix`,`FullHash`) VALUES $pairinsert;");
mysqli_query($this->dbLink, "INSERT INTO `$listname-$listtype-prefixes` (`Hostkey`,`Prefix`,`FullHash`) VALUES $pairinsert;");
elseif($type=="SUB")
mysql_query("INSERT INTO `$listname-$listtype-prefixes` (`Hostkey`,`AddChunkNum`,`Prefix`,`FullHash`) VALUES $pairinsert;");
$error = mysql_error();
mysqli_query($this->dbLink, "INSERT INTO `$listname-$listtype-prefixes` (`Hostkey`,`AddChunkNum`,`Prefix`,`FullHash`) VALUES $pairinsert;");
$error = mysqli_error($this->dbLink);
if(!empty($error))
$this->outputmsg("INSERTED $listname $type PREFIX HOST KEYS ".mysql_error());
$this->outputmsg("INSERTED $listname $type PREFIX HOST KEYS ".mysqli_error($this->dbLink));
}
}
elseif(!empty($error))
$this->outputmsg("COULD NOT SAVE $listname $type INDEXS ".mysql_error());
$this->outputmsg("COULD NOT SAVE $listname $type INDEXS ".mysqli_error($this->dbLink));
}
}
/*Get ranges of existing chunks from a requested list
Expand All @@ -449,11 +454,11 @@ function saveChunkPart($data,$type,$listname)
function getRanges($listname,$mode)
{
$checktable = $listname.'-'.$mode.'-index';
$results = mysql_query("SELECT ChunkNum FROM `$checktable` ORDER BY `ChunkNum` ASC");
$results = mysqli_query($this->dbLink, "SELECT ChunkNum FROM `$checktable` ORDER BY `ChunkNum` ASC");
$ranges = array();
$i = 0;
$start = 0;
while ($row = mysql_fetch_array($results, MYSQL_BOTH))
while ($row = mysqli_fetch_array($results, MYSQLI_BOTH))
{
$this->mainlist[$mode][$listname][$row['ChunkNum']] = true;
if($i==0)
Expand Down Expand Up @@ -518,27 +523,28 @@ function deleteRange($range,$mode,$listname)
else
$clause = "`ChunkNum` = '$range'";
//Delete from index
mysql_query("DELETE FROM `$buildtrunk-index` WHERE $clause");
mysqli_query($this->dbLink, "DELETE FROM `$buildtrunk-index` WHERE $clause");

//Select all host keys that match chunks (we'll delete them after but we need the hostkeys list!)
$result = mysql_query("SELECT `Hostkey` FROM `$buildtrunk-hosts` WHERE $clause");
$result = mysqli_query($this->dbLink, "SELECT `Hostkey` FROM `$buildtrunk-hosts` WHERE $clause");
$buildprefixdel = array();
if($result&&mysql_num_rows($result)>0)
if($result&&mysqli_num_rows($result)>0)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
if(!empty($row['Hostkey']))
$buildprefixdel[] = $row['Hostkey'];
}
if (count($buildprefixdel)) {
//Delete all matching hostkey prefixes
mysql_query(
mysqli_query(
$this->dbLink,
"DELETE FROM `$buildtrunk-prefixes` WHERE `Hostkey` in ('" . implode('\',\'', $buildprefixdel) . "')"
);
}

//Delete all matching hostkeys
mysql_query("DELETE FROM `$buildtrunk-hosts` WHERE $clause");
mysqli_query($this->dbLink, "DELETE FROM `$buildtrunk-hosts` WHERE $clause");
}
}
/*Main part of updater function, will call all other functions, merely requires
Expand Down Expand Up @@ -1125,15 +1131,15 @@ function addFullHash($prefix,$chunknum,$fullhash,$listname)
{
$buildtrunk = $listname."-a";
//First check hosts
$result = mysql_query("SELECT * FROM `$buildtrunk-hosts` WHERE `Hostkey` = '$prefix' AND `Chunknum` = '$chunknum'");
if($result&&mysql_num_rows($result)>0)
$result = mysqli_query($this->dbLink, "SELECT * FROM `$buildtrunk-hosts` WHERE `Hostkey` = '$prefix' AND `Chunknum` = '$chunknum'");
if($result&&mysqli_num_rows($result)>0)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
if(empty($row['FullHash']))
{
//We've got a live one! Insert the full hash for it
$addresult = mysql_query("UPDATE `$buildtrunk-hosts` SET `FullHash` = '$fullhash' WHERE `ID` = '{$row['ID']}';");
$addresult = mysqli_query($this->dbLink, "UPDATE `$buildtrunk-hosts` SET `FullHash` = '$fullhash' WHERE `ID` = '{$row['ID']}';");
if(!$addresult)
$this->fatalerror("Could not cache full-hash key. $prefix, $chunknum, $fullhash, $listname");
}
Expand All @@ -1142,17 +1148,17 @@ function addFullHash($prefix,$chunknum,$fullhash,$listname)
else
{
//If there are no rows it must be a prefix
$result = mysql_query("SELECT * FROM `$buildtrunk-prefixes` WHERE `Prefix` = '$prefix'");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
$result = mysqli_query($this->dbLink, "SELECT * FROM `$buildtrunk-prefixes` WHERE `Prefix` = '$prefix'");
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
if(empty($row['FullHash']))
{
$resulttwo = mysql_query("SELECT * FROM `$buildtrunk-hosts` WHERE `Hostkey` = '{$row['Hostkey']}' AND `Chunknum` = '$chunknum'");
while ($rowtwo = mysql_fetch_array($resulttwo, MYSQL_ASSOC))
$resulttwo = mysqli_query($this->dbLink, "SELECT * FROM `$buildtrunk-hosts` WHERE `Hostkey` = '{$row['Hostkey']}' AND `Chunknum` = '$chunknum'");
while ($rowtwo = mysqli_fetch_array($resulttwo, MYSQLI_ASSOC))
{
if(hexdec($rowtwo['Count'])>0)
{
$addresult = mysql_query("UPDATE `$buildtrunk-prefixes` SET `FullHash` = '$fullhash' WHERE `ID` = '{$row['ID']}';");
$addresult = mysqli_query($this->dbLink, "UPDATE `$buildtrunk-prefixes` SET `FullHash` = '$fullhash' WHERE `ID` = '{$row['ID']}';");
if(!$addresult)
$this->fatalerror("Could not cache full-hash key. $prefix, $chunknum, $fullhash, $listname");
}
Expand All @@ -1168,23 +1174,23 @@ function cacheCheck($prefix)
foreach($this->usinglists as $value)
{
$buildtrunk = $value."-a";
$result = mysql_query("SELECT * FROM `$buildtrunk-hosts` WHERE `Hostkey` = '$prefix' AND `FullHash` != ''");
if($result&&mysql_num_rows($result)>0)
$result = mysqli_query($this->dbLink, "SELECT * FROM `$buildtrunk-hosts` WHERE `Hostkey` = '$prefix' AND `FullHash` != ''");
if($result&&mysqli_num_rows($result)>0)
{
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
return array($row['FullHash'],$row['Chunknum']);
}
}
else
{
$result = mysql_query("SELECT * FROM `$buildtrunk-prefixes` WHERE `Prefix` = '$prefix' AND `FullHash` != ''");
if($result&&mysql_num_rows($result)>0)
$result = mysqli_query($this->dbLink, "SELECT * FROM `$buildtrunk-prefixes` WHERE `Prefix` = '$prefix' AND `FullHash` != ''");
if($result&&mysqli_num_rows($result)>0)
{
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$resulttwo = mysql_query("SELECT * FROM `$buildtrunk-hosts` WHERE `Hostkey` = '{$row['Hostkey']}'");
while ($rowtwo = mysql_fetch_array($resulttwo, MYSQL_ASSOC))
$resulttwo = mysqli_query($this->dbLink, "SELECT * FROM `$buildtrunk-hosts` WHERE `Hostkey` = '{$row['Hostkey']}'");
while ($rowtwo = mysqli_fetch_array($resulttwo, MYSQLI_ASSOC))
{
if(hexdec($rowtwo['Count'])>0)
{
Expand Down Expand Up @@ -1285,12 +1291,12 @@ function subCheck($listname,$prefixlist,$mode)
//Mode is prefix so the add part was a prefix, not a hostkey so we just check prefixes (saves a lookup)
foreach($prefixlist as $value)
{
$result = mysql_query("SELECT * FROM `$buildtrunk-prefixes` WHERE `Prefix` = '{$value[0]}'");
if($result&&mysql_num_rows($result)>0)
$result = mysqli_query($this->dbLink, "SELECT * FROM `$buildtrunk-prefixes` WHERE `Prefix` = '{$value[0]}'");
if($result&&mysqli_num_rows($result)>0)
{
//As interpreted from Developer Guide if theres a match in sub list it cancels out the add listing
//we'll double check its from the same chunk just to be pedantic
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
if(hexdec($row['AddChunkNum'])==$value[1])
return true;
Expand All @@ -1305,12 +1311,12 @@ function subCheck($listname,$prefixlist,$mode)
//Mode is hostkey
foreach($prefixlist as $value)
{
$result = mysql_query("SELECT * FROM `$buildtrunk-prefixes` WHERE `Hostkey` = '{$value[0]}'");
if($result&&mysql_num_rows($result)>0)
$result = mysqli_query($this->dbLink, "SELECT * FROM `$buildtrunk-prefixes` WHERE `Hostkey` = '{$value[0]}'");
if($result&&mysqli_num_rows($result)>0)
{
//As interpreted from Developer Guide if theres a match in sub list it cancels out the add listing
//we'll double check its from the same chunk just to be pedantic
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
if(hexdec($row['AddChunkNum'])==$value[1]&&empty($row['Prefix']))
return true;
Expand Down Expand Up @@ -1339,11 +1345,11 @@ function doLookup($url)
foreach($hostkeys as $keyinner=>$valueinner)
{
//Within each list loop over each hostkey
$result = mysql_query("SELECT * FROM `$buildtrunk-hosts` WHERE `Hostkey` = '{$valueinner['Prefix']}'");
if($result&&mysql_num_rows($result)>0)
$result = mysqli_query($this->dbLink, "SELECT * FROM `$buildtrunk-hosts` WHERE `Hostkey` = '{$valueinner['Prefix']}'");
if($result&&mysqli_num_rows($result)>0)
{
//For each hostkey match
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$nicecount = hexdec($row['Count']);
if($nicecount>0)
Expand All @@ -1361,13 +1367,13 @@ function doLookup($url)
$buildprequery = implode("OR",$buildprequery);
}
//Check if there are any matching prefixes
$resulttwo = mysql_query("SELECT * FROM `$buildtrunk-prefixes` WHERE ($buildprequery) AND `Hostkey` = '{$row['Hostkey']}'");
if($resulttwo&&mysql_num_rows($resulttwo)>0)
$resulttwo = mysqli_query($this->dbLink, "SELECT * FROM `$buildtrunk-prefixes` WHERE ($buildprequery) AND `Hostkey` = '{$row['Hostkey']}'");
if($resulttwo&&mysqli_num_rows($resulttwo)>0)
{
//We found prefix matches
$prematches = array();
$prelookup = array();
while ($rowtwo = mysql_fetch_array($resulttwo, MYSQL_ASSOC))
while ($rowtwo = mysqli_fetch_array($resulttwo, MYSQLI_ASSOC))
{
$prematches[] = array($rowtwo['Prefix'],$row['Chunknum']);
}
Expand Down Expand Up @@ -1400,5 +1406,5 @@ function doLookup($url)
}
return false;

}
}
}

0 comments on commit d24a04f

Please sign in to comment.