Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

db.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. class MysqlDb {
  3. private $mysql;
  4. private function __construct() {}
  5. public static function Connect($hostname, $username, $password, $database){
  6. $handler = new MysqlDb();
  7. mysqli_report(MYSQLI_REPORT_OFF);
  8. $handler->mysql = mysqli_connect($hostname, $username, $password);
  9. if(!$handler->mysql) return false;
  10. mysqli_select_db($handler->mysql, $database);
  11. if (mysqli_error($handler->mysql))
  12. return false;
  13. return $handler;
  14. }
  15. public function sanitize($text){
  16. return mysqli_real_escape_string($this->mysql, $text);
  17. }
  18. public function query($query){
  19. try {
  20. return mysqli_query($this->mysql, $query);
  21. } catch (Throwable $err) {
  22. return false;
  23. }
  24. }
  25. public function insert($table, $fields, $values_assoc, $ignore = false) {
  26. $field_list = implode(",", $fields);
  27. $values_arr = [];
  28. foreach ($values_assoc as $value) {
  29. $value_arr = [];
  30. foreach ($value as $v) {
  31. if ($this->verifyInteger($v))
  32. $value_arr[] = $v;
  33. else if ($v === "NULL")
  34. $value_arr[] = "NULL";
  35. else
  36. $value_arr[] = "\"{$this->sanitize($v)}\"";
  37. }
  38. $values_arr[] = "(" . implode(',', $value_arr) . ")";
  39. }
  40. $values_list = implode(',', $values_arr);
  41. $ignore_str = '';
  42. if ($ignore)
  43. $ignore_str = 'IGNORE';
  44. return $this->query("INSERT $ignore_str INTO $table ($field_list) VALUES $values_list");
  45. }
  46. public function exec_multi($query){
  47. try {
  48. $res = mysqli_multi_query($this->mysql, $query);
  49. do {
  50. if ($err = $this->getError())
  51. return false;
  52. } while (mysqli_next_result($this->mysqli) || $this->getError());
  53. return true;
  54. } catch (Throwable $err) {
  55. return false;
  56. }
  57. }
  58. public function fetchRow($query){
  59. $result = $this->query($query);
  60. if($result === false || $result === true) die(mysqli_error($this->mysql));//return $result;
  61. return mysqli_fetch_assoc($result);
  62. }
  63. public function fetchAssoc($query){
  64. $result = $this->query($query);
  65. if($result === false || $result === true) die(mysqli_error($this->mysql));//return $result;
  66. $data = array();
  67. while($row = mysqli_fetch_assoc($result)){
  68. $data[] = $row;
  69. }
  70. return $data;
  71. }
  72. public function getError() {
  73. return mysqli_error($this->mysql);
  74. }
  75. //public function lastInsertId(){
  76. // return mysqli_insert_id($this->mysql);
  77. //}
  78. //public function getAffectedRows() {
  79. // return mysqli_affected_rows($this->mysql);
  80. //}
  81. public function verifyInteger($input, $minimum = 1){
  82. /*
  83. * Pretty hacky solution.
  84. *
  85. * First checks if the integer cast of the input is equal to itself.
  86. * This filters out decimals, alternate bases, and exponents.
  87. * Then checks if the input is numeric, which filters out other strings that slip by the first check.
  88. * This guarantees that it's in a numeric format, which combined with the first filter, should guarantee that it is an integer
  89. */
  90. return (((int) $input == $input) && is_numeric($input) && (($minimum === false) || (int) $input >= $minimum));
  91. }
  92. // Always returns a key of length 40. TODO: Add arbitrary length
  93. public function randomKey(){
  94. //Cryptographically Secure Key
  95. if(function_exists('openssl_random_pseudo_bytes')) return base64_encode(openssl_random_pseudo_bytes(30));
  96. //Fallback (Not cryptographically secure)
  97. $str = "";
  98. for($i=0; $i<30; $i++){
  99. $str .= chr(mt_rand(0,255));
  100. }
  101. return base64_encode($str);
  102. }
  103. }
  104. class SqliteDb {
  105. private $sqlite;
  106. private function __construct() {}
  107. public static function Connect(){
  108. $handler = new SqliteDb();
  109. $handler->sqlite = new Sqlite3(BASE . "/inc/config/sqlite3.db");
  110. $handler->sqlite->exec('PRAGMA foreign_keys = ON');
  111. return $handler;
  112. }
  113. public function sanitize($text){
  114. return SQLite3::escapeString($text);
  115. }
  116. public function query($query){
  117. try {
  118. return $this->sqlite->query($query);
  119. } catch (Throwable $err) {
  120. return false;
  121. }
  122. }
  123. public function insert($table, $fields, $values_assoc, $ignore = false) {
  124. $field_list = implode(",", $fields);
  125. $values_arr = [];
  126. foreach ($values_assoc as $value) {
  127. $value_arr = [];
  128. foreach ($value as $v) {
  129. if ($this->verifyInteger($v))
  130. $value_arr[] = $v;
  131. else if ($v === "NULL")
  132. $value_arr[] = "NULL";
  133. else
  134. $value_arr[] = "\"{$this->sanitize($v)}\"";
  135. }
  136. $values_arr[] = "(" . implode(',', $value_arr) . ")";
  137. }
  138. $values_list = implode(',', $values_arr);
  139. $ignore_str = '';
  140. if ($ignore)
  141. $ignore_str = 'OR IGNORE';
  142. return $this->query("INSERT $ignore_str INTO $table ($field_list) VALUES $values_list");
  143. }
  144. public function exec_multi($query){
  145. try {
  146. return $this->sqlite->exec($query);
  147. } catch (Throwable $err) {
  148. return false;
  149. }
  150. }
  151. public function fetchRow($query){
  152. $result = $this->query($query);
  153. if ($result === false || $result === true)
  154. return $result;
  155. return $result->fetchArray();
  156. }
  157. public function fetchAssoc($query){
  158. $result = $this->query($query);
  159. if($result === false || $result === true) die($this->getError());//return $result;
  160. $data = array();
  161. while($row = $result->fetchArray()){
  162. $data[] = $row;
  163. }
  164. return $data;
  165. }
  166. public function getError() {
  167. return $this->sqlite->lastErrorCode() === 0 ? "" : $this->sqlite->lastErrorMsg();
  168. }
  169. //public function lastInsertId(){
  170. // return mysqli_insert_id($this->mysql);
  171. //}
  172. //public function getAffectedRows() {
  173. // return mysqli_affected_rows($this->mysql);
  174. //}
  175. public function verifyInteger($input, $minimum = 1){
  176. /*
  177. * Pretty hacky solution.
  178. *
  179. * First checks if the integer cast of the input is equal to itself.
  180. * This filters out decimals, alternate bases, and exponents.
  181. * Then checks if the input is numeric, which filters out other strings that slip by the first check.
  182. * This guarantees that it's in a numeric format, which combined with the first filter, should guarantee that it is an integer
  183. */
  184. return (((int) $input == $input) && is_numeric($input) && (($minimum === false) || (int) $input >= $minimum));
  185. }
  186. // Always returns a key of length 40. TODO: Add arbitrary length
  187. public function randomKey(){
  188. //Cryptographically Secure Key
  189. if(function_exists('openssl_random_pseudo_bytes')) return base64_encode(openssl_random_pseudo_bytes(30));
  190. //Fallback (Not cryptographically secure)
  191. $str = "";
  192. for($i=0; $i<30; $i++){
  193. $str .= chr(mt_rand(0,255));
  194. }
  195. return base64_encode($str);
  196. }
  197. }