| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- class MysqlDb {
- private $mysql;
-
- private function __construct() {}
-
- public static function Connect($hostname, $username, $password, $database){
- $handler = new MysqlDb();
-
- mysqli_report(MYSQLI_REPORT_OFF);
- $handler->mysql = mysqli_connect($hostname, $username, $password);
- if(!$handler->mysql) return false;
-
- mysqli_select_db($handler->mysql, $database);
- if (mysqli_error($handler->mysql))
- return false;
-
- return $handler;
- }
-
- public function sanitize($text){
- return mysqli_real_escape_string($this->mysql, $text);
- }
-
- public function query($query){
- try {
- return mysqli_query($this->mysql, $query);
- } catch (Throwable $err) {
- return false;
- }
- }
-
- public function insert($table, $fields, $values_assoc, $ignore = false) {
- $field_list = implode(",", $fields);
- $values_arr = [];
- foreach ($values_assoc as $value) {
- $value_arr = [];
- foreach ($value as $v) {
- if ($this->verifyInteger($v))
- $value_arr[] = $v;
- else if ($v === "NULL")
- $value_arr[] = "NULL";
- else
- $value_arr[] = "\"{$this->sanitize($v)}\"";
- }
-
- $values_arr[] = "(" . implode(',', $value_arr) . ")";
- }
-
- $values_list = implode(',', $values_arr);
-
- $ignore_str = '';
- if ($ignore)
- $ignore_str = 'IGNORE';
-
- return $this->query("INSERT $ignore_str INTO $table ($field_list) VALUES $values_list");
- }
-
- public function exec_multi($query){
- try {
- $res = mysqli_multi_query($this->mysql, $query);
- do {
- if ($err = $this->getError())
- return false;
- } while (mysqli_next_result($this->mysqli) || $this->getError());
- return true;
- } catch (Throwable $err) {
- return false;
- }
- }
-
- public function fetchRow($query){
- $result = $this->query($query);
- if($result === false || $result === true) die(mysqli_error($this->mysql));//return $result;
-
- return mysqli_fetch_assoc($result);
- }
-
- public function fetchAssoc($query){
- $result = $this->query($query);
- if($result === false || $result === true) die(mysqli_error($this->mysql));//return $result;
-
- $data = array();
- while($row = mysqli_fetch_assoc($result)){
- $data[] = $row;
- }
-
- return $data;
- }
-
- public function getError() {
- return mysqli_error($this->mysql);
- }
-
- //public function lastInsertId(){
- // return mysqli_insert_id($this->mysql);
- //}
-
- //public function getAffectedRows() {
- // return mysqli_affected_rows($this->mysql);
- //}
-
- public function verifyInteger($input, $minimum = 1){
- /*
- * Pretty hacky solution.
- *
- * First checks if the integer cast of the input is equal to itself.
- * This filters out decimals, alternate bases, and exponents.
- * Then checks if the input is numeric, which filters out other strings that slip by the first check.
- * This guarantees that it's in a numeric format, which combined with the first filter, should guarantee that it is an integer
- */
- return (((int) $input == $input) && is_numeric($input) && (($minimum === false) || (int) $input >= $minimum));
- }
-
- // Always returns a key of length 40. TODO: Add arbitrary length
- public function randomKey(){
- //Cryptographically Secure Key
- if(function_exists('openssl_random_pseudo_bytes')) return base64_encode(openssl_random_pseudo_bytes(30));
-
-
- //Fallback (Not cryptographically secure)
- $str = "";
- for($i=0; $i<30; $i++){
- $str .= chr(mt_rand(0,255));
- }
-
- return base64_encode($str);
- }
- }
-
- class SqliteDb {
- private $sqlite;
-
- private function __construct() {}
-
- public static function Connect(){
- $handler = new SqliteDb();
- $handler->sqlite = new Sqlite3(BASE . "/inc/config/sqlite3.db");
- $handler->sqlite->exec('PRAGMA foreign_keys = ON');
-
- return $handler;
- }
-
- public function sanitize($text){
- return SQLite3::escapeString($text);
- }
-
- public function query($query){
- try {
- return $this->sqlite->query($query);
- } catch (Throwable $err) {
- return false;
- }
- }
-
- public function insert($table, $fields, $values_assoc, $ignore = false) {
- $field_list = implode(",", $fields);
- $values_arr = [];
- foreach ($values_assoc as $value) {
- $value_arr = [];
- foreach ($value as $v) {
- if ($this->verifyInteger($v))
- $value_arr[] = $v;
- else if ($v === "NULL")
- $value_arr[] = "NULL";
- else
- $value_arr[] = "\"{$this->sanitize($v)}\"";
- }
-
- $values_arr[] = "(" . implode(',', $value_arr) . ")";
- }
-
- $values_list = implode(',', $values_arr);
-
- $ignore_str = '';
- if ($ignore)
- $ignore_str = 'OR IGNORE';
-
- return $this->query("INSERT $ignore_str INTO $table ($field_list) VALUES $values_list");
- }
-
- public function exec_multi($query){
- try {
- return $this->sqlite->exec($query);
- } catch (Throwable $err) {
- return false;
- }
- }
-
- public function fetchRow($query){
- $result = $this->query($query);
- if ($result === false || $result === true)
- return $result;
-
- return $result->fetchArray();
- }
-
- public function fetchAssoc($query){
- $result = $this->query($query);
- if($result === false || $result === true) die($this->getError());//return $result;
-
- $data = array();
- while($row = $result->fetchArray()){
- $data[] = $row;
- }
-
- return $data;
- }
-
- public function getError() {
- return $this->sqlite->lastErrorCode() === 0 ? "" : $this->sqlite->lastErrorMsg();
- }
-
- //public function lastInsertId(){
- // return mysqli_insert_id($this->mysql);
- //}
-
- //public function getAffectedRows() {
- // return mysqli_affected_rows($this->mysql);
- //}
-
- public function verifyInteger($input, $minimum = 1){
- /*
- * Pretty hacky solution.
- *
- * First checks if the integer cast of the input is equal to itself.
- * This filters out decimals, alternate bases, and exponents.
- * Then checks if the input is numeric, which filters out other strings that slip by the first check.
- * This guarantees that it's in a numeric format, which combined with the first filter, should guarantee that it is an integer
- */
- return (((int) $input == $input) && is_numeric($input) && (($minimum === false) || (int) $input >= $minimum));
- }
-
- // Always returns a key of length 40. TODO: Add arbitrary length
- public function randomKey(){
- //Cryptographically Secure Key
- if(function_exists('openssl_random_pseudo_bytes')) return base64_encode(openssl_random_pseudo_bytes(30));
-
-
- //Fallback (Not cryptographically secure)
- $str = "";
- for($i=0; $i<30; $i++){
- $str .= chr(mt_rand(0,255));
- }
-
- return base64_encode($str);
- }
- }
|