<?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)
			 {mysqli_select_db($Database, $this->Connection);}
			$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()
		 {mysql_close($this->Connection);}

		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."}";
					$ReplaceData[1][] = $LeadingChr.mysqli_real_escape_string($Value, $this->Connection).$FollowingChr;
				}
			}
			$Query = str_ireplace($ReplaceData[0], $ReplaceData[1], $Query);
			$MysqlRes = new MySqlResult(mysqli_query($Query, $this->Connection), $Query);
			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;

		public function __construct($QueryRes, $QueryStr)
		{
			$this->MySqlRes = $QueryRes;
			$this->Error = mysqli_error($this->Connection);
			$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);}
	}
?>