Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. class DBHandler{
  3. private $mysql;
  4. private function __construct() {}
  5. public static function Connect($hostname, $username, $password, $database){
  6. $handler = new DBHandler();
  7. $handler->mysql = mysqli_connect($hostname, $username, $password);
  8. if(!$handler->mysql) die("MySql error: " . mysql_error());
  9. mysqli_select_db($handler->mysql, $database);
  10. return $handler;
  11. }
  12. public static function Wrap($mysql) {
  13. $handler = new DBHandler();
  14. $handler->mysql = $mysql;
  15. return $handler;
  16. }
  17. public function sanitize($text){
  18. return mysqli_real_escape_string($this->mysql, $text);
  19. }
  20. public function query($query){
  21. try {
  22. return mysqli_query($this->mysql, $query);
  23. } catch (Throwable $err) {
  24. return false;
  25. }
  26. }
  27. public function fetchRow($query){
  28. $result = $this->query($query);
  29. if($result === false || $result === true) die(mysqli_error($this->mysql));//return $result;
  30. return mysqli_fetch_assoc($result);
  31. }
  32. public function fetchAssoc($query){
  33. $result = $this->query($query);
  34. if($result === false || $result === true) die(mysqli_error($this->mysql));//return $result;
  35. $data = array();
  36. while($row = mysqli_fetch_assoc($result)){
  37. $data[] = $row;
  38. }
  39. return $data;
  40. }
  41. public function getError() {
  42. return mysqli_error($this->mysql);
  43. }
  44. public function lastInsertId(){
  45. return mysqli_insert_id($this->mysql);
  46. }
  47. public function getAffectedRows() {
  48. return mysqli_affected_rows($this->mysql);
  49. }
  50. public function verifyInteger($input, $minimum = 1){
  51. /*
  52. * Pretty hacky solution.
  53. *
  54. * First checks if the integer cast of the input is equal to itself.
  55. * This filters out decimals, alternate bases, and exponents.
  56. * Then checks if the input is numeric, which filters out other strings that slip by the first check.
  57. * This guarantees that it's in a numeric format, which combined with the first filter, should guarantee that it is an integer
  58. */
  59. return (((int) $input == $input) && is_numeric($input) && (($minimum === false) || (int) $input >= $minimum));
  60. }
  61. // Always returns a key of length 40. TODO: Add arbitrary length
  62. public function randomKey(){
  63. //Cryptographically Secure Key
  64. if(function_exists('openssl_random_pseudo_bytes')) return base64_encode(openssl_random_pseudo_bytes(30));
  65. //Fallback (Not cryptographically secure)
  66. $str = "";
  67. for($i=0; $i<30; $i++){
  68. $str .= chr(mt_rand(0,255));
  69. }
  70. return base64_encode($str);
  71. }
  72. }