2020-11-28 08:19:59 +00:00
|
|
|
<?php
|
|
|
|
/***********************************************************************
|
|
|
|
* Script (c) Kubwa (https://kubwa.de)
|
|
|
|
*
|
|
|
|
* This script was release under BSD license.
|
|
|
|
* You are free to use, share or change this code as you wish. This
|
|
|
|
* header must be kept intact.
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
class MySql
|
|
|
|
{
|
|
|
|
private $Connection;
|
|
|
|
|
|
|
|
public function __construct($User, $Pass, $Database = null, $Server = "127.0.0.1")
|
|
|
|
{
|
|
|
|
$this->Connection = mysqli_connect($Server, $User, $Pass);
|
|
|
|
if ($Database != null)
|
2020-11-28 10:52:19 +00:00
|
|
|
{mysqli_select_db($this->Connection, $Database);}
|
2020-11-28 08:19:59 +00:00
|
|
|
$this->query("CREATE TABLE IF NOT EXISTS _datastore (id VARCHAR(64) PRIMARY KEY, data TEXT)");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function id($Len = 36)
|
|
|
|
{
|
|
|
|
$Chars = str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
|
|
|
|
$Res = "";
|
|
|
|
for ($i = 0; $i < $Len; ++$i) {$Res = $Res.substr($Chars, rand(0, strlen($Chars) - 1), 1);}
|
|
|
|
return $Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function close()
|
2020-11-28 10:52:19 +00:00
|
|
|
{mysqli_close($this->Connection);}
|
2020-11-28 08:19:59 +00:00
|
|
|
|
|
|
|
public function ping()
|
|
|
|
{return mysqli_ping($this->Connection);}
|
|
|
|
|
|
|
|
public function query($Query, $Values = array())
|
|
|
|
{
|
|
|
|
$Whitespaces = array(" ", "\t", "\n", "\r", "\0", "\x0B");
|
|
|
|
$ReplaceData = array(array(), array());
|
|
|
|
$StartOffset = 1;
|
|
|
|
if (stripos($Query, "{0}") !== false) {$StartOffset = 0;}
|
|
|
|
foreach($Values as $Key => $Value)
|
|
|
|
{
|
|
|
|
if (is_numeric($Key)) {$Key = $Key + $StartOffset;}
|
|
|
|
$InQueryPos = stripos($Query, "{".$Key."}");
|
|
|
|
if ($InQueryPos !== false)
|
|
|
|
{
|
|
|
|
$LeadingChr = "";
|
|
|
|
for ($u = $InQueryPos - 1; $u > -1; $u = $u - 1)
|
|
|
|
{
|
|
|
|
if (!in_array(substr($Query, $u, 1), $Whitespaces))
|
|
|
|
{
|
|
|
|
if (substr($Query, $u, 1) != "'") {$LeadingChr = "'";}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$FollowingChr = "";
|
|
|
|
for ($u = $InQueryPos + strlen("{".$Key."}"); $u < strlen($Query); $u = $u + 1)
|
|
|
|
{
|
|
|
|
if (!in_array(substr($Query, $u, 1), $Whitespaces))
|
|
|
|
{
|
|
|
|
if (substr($Query, $u, 1) != "'") {$FollowingChr = "'";}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($InQueryPos + strlen("{".$Key."}") >= strlen($Query)) {$FollowingChr = "'";}
|
|
|
|
$ReplaceData[0][] = "{".$Key."}";
|
2020-11-28 10:52:19 +00:00
|
|
|
$ReplaceData[1][] = $LeadingChr.mysqli_real_escape_string($this->Connection, $Value).$FollowingChr;
|
2020-11-28 08:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$Query = str_ireplace($ReplaceData[0], $ReplaceData[1], $Query);
|
2020-12-07 17:00:06 +00:00
|
|
|
$MysqlRes = new MySqlResult(mysqli_query($this->Connection, $Query), $Query, $this->Connection);
|
2020-11-28 08:19:59 +00:00
|
|
|
return $MysqlRes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setting($Name, $Value = null)
|
|
|
|
{
|
|
|
|
$Name = strtoupper($Name);
|
|
|
|
if (!isset($Value))
|
|
|
|
{
|
|
|
|
$Res = $this->query("SELECT * FROM _datastore WHERE id = '{0}' LIMIT 1", array($Name));
|
|
|
|
if ($Res->error() == "")
|
|
|
|
{
|
|
|
|
$DataRes = $Res->fetch();
|
|
|
|
if (isset($DataRes)) {return $DataRes[data];}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{$this->query("REPLACE INTO _datastore (id, data)VALUES('{0}', '{1}')", array($Name, $Value));}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MySqlResult
|
|
|
|
{
|
|
|
|
private $MySqlRes;
|
|
|
|
private $Error;
|
|
|
|
private $QueryString;
|
|
|
|
|
2020-12-07 17:00:06 +00:00
|
|
|
public function __construct($QueryRes, $QueryStr, $Connection)
|
2020-11-28 08:19:59 +00:00
|
|
|
{
|
|
|
|
$this->MySqlRes = $QueryRes;
|
2020-12-07 17:00:06 +00:00
|
|
|
$this->Error = mysqli_error($Connection);
|
2020-11-28 08:19:59 +00:00
|
|
|
$this->QueryString = $QueryStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetch()
|
|
|
|
{return mysqli_fetch_array($this->MySqlRes);}
|
|
|
|
|
|
|
|
public function error()
|
|
|
|
{return $this->Error;}
|
|
|
|
|
|
|
|
public function querystring()
|
|
|
|
{return $this->QueryString;}
|
|
|
|
|
|
|
|
public function numrows()
|
|
|
|
{return mysqli_num_rows($this->MySqlRes);}
|
|
|
|
}
|
|
|
|
?>
|