Created
December 15, 2017 12:55
-
-
Save biboletin/074f2b643656aa0adfde33bfa9a9009a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DataBase extends MySQLi{ | |
/** | |
* [$host description] адрес на сървъра | |
* @var string | |
*/ | |
private $host = "localhost"; | |
/** | |
* [$port description] порт на сървъра | |
* @var integer | |
*/ | |
private $port = 3306; | |
/** | |
* [$user description] MySQL потребител | |
* @var string | |
*/ | |
private $user = "root"; | |
/** | |
* [$pass description] MySQL Парола | |
* @var string | |
*/ | |
private $pass = ""; | |
/** | |
* [$database description] Име на База данни | |
* @var string | |
*/ | |
private $database = "kameno_08_08_2017"; | |
/** | |
* [$mysqli description] инстанция към MySQLi | |
* @var [object] | |
*/ | |
public $mysqli; | |
/** | |
* [$charset description] енкондинг на данните | |
* | |
* @var [string] | |
*/ | |
public $charset = "utf8"; | |
/** | |
* [$forbidden_db description] Бази данни към, които няма да се прави връзка | |
* @var array | |
*/ | |
private $forbidden_db = array("mysql", "performance_schema", "phpmyadmin"); | |
/** | |
* [$forbidden_queries description] SQL заявки, които няма да се изпълняват | |
* @var array | |
*/ | |
private $forbidden_queries = array("DROP", "ALTER"); | |
public function __construct(){ | |
$this->mysqli = new \MySQLi($this->host, $this->user, $this->pass, $this->database); | |
if($this->mysqli->connect_errno){ | |
echo "Грешка: " . $this->mysqli->connect_error . " <br>"; | |
exit(); | |
} | |
// mysqli_set_charset($this->mysqli, $this->charset); | |
$this->mysqli->set_charset($this->charset); | |
// echo "<br>Connection open!<br>"; | |
} | |
/** | |
* връща името на БД | |
* @return string | |
*/ | |
public function getDataBase(){ | |
return $this->database; | |
} | |
/** | |
* Променя името на БД | |
* @param [string] | |
*/ | |
public function setDataBase($db){ | |
if(!in_array($db, $this->forbidden_db)){ | |
return $this->database = $db; | |
} | |
} | |
/** | |
* @param [string] | |
* @return [boolean or array] | |
*/ | |
public function sql($sql){ | |
// file_put_contents("logs/queries.txt", "QUERY[" . date("H:i:s") . "]: " . $sql . "\r\n", FILE_APPEND); | |
$action = strtolower(explode(" ", trim($sql))[0]); | |
if(($action == "insert") || ($action == "update")){ | |
$query = $this->mysqli->query($sql) ? true : false; | |
return $query; | |
}else{ | |
if($action == "alter"){ | |
$tableName = strtolower(explode(" ", trim($sql))[2]); // името на таблицата, която ще се обработва | |
if(!in_array($tableName, array_map("strtolower", $this->forbidden_db))){ | |
if($query = $this->mysqli->query($sql)){ | |
$result = true; | |
}else{ | |
$result = false; | |
} | |
}else{ | |
$result = false; | |
} | |
} | |
if($action == "select"){ | |
if($query = $this->mysqli->query($sql)){ | |
if($query->num_rows > 0){ | |
while($row = $query->fetch_assoc()){ | |
$result[] = $row; | |
} | |
}else{ | |
$result = false; | |
} | |
}else{ | |
$result = false; | |
} | |
} | |
if($action == "drop"){ | |
$result = false; | |
} | |
return $result; | |
} | |
} | |
/** | |
* валидиране на подадена стойност преди да бъде вкарана в БД | |
* | |
* @param [string] $value | |
* @return string | |
*/ | |
public function validate($value){ | |
if(!empty($value)){ | |
$str = trim($value); | |
$str = htmlentities($str, ENT_QUOTES, "UTF-8"); | |
$str = $this->mysqli->escape_string($str); | |
}else{ | |
$str = ""; | |
} | |
return $str; | |
} | |
/** | |
* Затваряне на SQL връзката | |
*/ | |
public function __destruct(){ | |
$this->mysqli->free_result; | |
$this->mysqli->close; | |
// echo "<br>Connection closed!<br>"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment