dcc202fb249a446ac15c7cf413b9f1b4a3f31b58
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

1) <?php
2) /*
3) This file belongs to the Webinterface of schokokeks.org Hosting
4) 
Hanno Böck Change license from CC0 to...

Hanno Böck authored 1 year ago

5) Written by schokokeks.org Hosting, namely
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

6)   Bernd Wurst <bernd@schokokeks.org>
7)   Hanno Böck <hanno@schokokeks.org>
8) 
Hanno Böck Change license from CC0 to...

Hanno Böck authored 1 year ago

9) This code is published under a 0BSD license.
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

10) 
11) Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
12) */
13) 
14) require_once('inc/base.php');
15) require_once('inc/error.php');
16) require_once('inc/debug.php');
17) 
18) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

19) class DB extends PDO
20) {
21)     public function __construct()
22)     {
23)         $dsn = "mysql:host=".config('db_host', true);
24)         if (config('db_port', true)) {
25)             $dsn .= ';port='.config('db_port', true);
Bernd Wurst Weitere Umstellungen auf pr...

Bernd Wurst authored 10 years ago

26)         }
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

27)         if (config('db_socket', true)) {
28)             $dsn = "mysql:unix_socket=".config('db_socket', true);
29)         }
30)         $username = config('db_user', true);
31)         $password = config('db_pass', true);
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 2 years ago

32)         parent::__construct($dsn, $username, $password, [PDO::ATTR_TIMEOUT => "30"]);
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

33)     }
34) 
35) 
36)     /*
37)       Wenn Parameter übergeben werden, werden Queries immer als Prepared statements übertragen
38)     */
Bernd Wurst PHP 8.0 compatibility

Bernd Wurst authored 3 years ago

39)     public function myquery($stmt, $params = null, $allowempty = false)
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

40)     {
41)         if (is_array($params)) {
Hanno optional parameter to not w...

Hanno authored 5 years ago

42)             if (config("enable_debug") && !$allowempty) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

43)                 foreach (array_values($params) as $p) {
44)                     if ($p === '') {
45)                         DEBUG("Potential bug, empty string found in database parameters");
46)                         warning("Potential bug, empty string found in database parameters");
47)                     }
48)                 }
49)             }
50)             $response = parent::prepare($stmt);
51)             $response->execute($params);
52)             return $response;
53)         } else {
54)             if (strtoupper(substr($stmt, 0, 6)) == "INSERT" ||
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

55)           strtoupper(substr($stmt, 0, 7)) == "REPLACE" ||
Bernd Wurst Warnung im dev-branch bzgl....

Bernd Wurst authored 10 years ago

56)           strpos(strtoupper($stmt), "WHERE") > 0) { // Das steht nie am Anfang
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

57)                 $backtrace = debug_backtrace();
58)                 $wherepart = substr(strtoupper($stmt), strpos(strtoupper($stmt), "WHERE"));
59)                 if ((strpos($wherepart, '"') > 0 || strpos($wherepart, "'") > 0) && config("enable_debug")) {
60)                     warning("Possibly unsafe SQL statement in {$backtrace[1]['file']} line {$backtrace[1]['line']}:\n$stmt");
61)                 }
62)             }
63)             return parent::query($stmt);
Bernd Wurst Warnung im dev-branch bzgl....

Bernd Wurst authored 10 years ago

64)         }
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

65)     }
66) }
67) 
68) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

69) /* FIXME
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

70)    Das ist etwas unelegant. Soll nur übergangsweise verwendet werden bis alles auf prepared statements umgestellt ist
71) */
72) function db_escape_string($string)
73) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

74)     if (config("enable_debug")) {
75)         $backtrace = debug_backtrace();
76)         warning("call to db_escape_string() in {$backtrace[0]['file']} line {$backtrace[0]['line']}");
77)     }
78)     global $_db;
79)     __ensure_connected();
80)     $quoted = $_db->quote($string);
81)     // entferne die quotes, damit wird es drop-in-Kompatibel zu db_escape_string()
82)     $ret = substr($quoted, 1, -1);
83)     return $ret;
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

84) }
85) 
86) 
87) function db_insert_id()
88) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

89)     global $_db;
90)     __ensure_connected();
91)     return $_db->lastInsertId();
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

92) }
93) 
94) 
95) function __ensure_connected()
96) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

97)     /*
98)       Dieses Kontrukt ist vermultich noch schlimmer als ein normales singleton
99)       aber es hilft uns in unserem prozeduralen Kontext
100)     */
101)     global $_db;
102)     if (! isset($_db)) {
103)         try {
104)             DEBUG("Neue Datenbankverbindung!");
105)             $_db = new DB();
106)             $_db->query("SET NAMES utf8mb4");
107)             $_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
108)             $_db->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
109)         } catch (PDOException $e) {
110)             global $debugmode;
111)             if ($debugmode) {
112)                 die("MySQL-Fehler: ".$e->getMessage());
113)             } else {
114)                 die("Fehler bei der Datenbankverbindung!");
115)             }
116)         }
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

117)     }
118) }
119) 
120) 
Hanno optional parameter to not w...

Hanno authored 5 years ago

121) function db_query($stmt, $params = null, $allowempty = false)
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

122) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

123)     global $_db;
124)     __ensure_connected();
125)     $backtrace = debug_backtrace();
126)     DEBUG($backtrace[0]['file'].':'.$backtrace[0]['line'].': '.htmlspecialchars($stmt));
127)     if ($params) {
128)         DEBUG($params);
129)     }
130)     try {
Bernd Wurst PHP 8.0 compatibility

Bernd Wurst authored 3 years ago

131)         $result = $_db->myquery($stmt, $params, $allowempty);
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

132)         DEBUG('=> '.$result->rowCount().' rows');
133)     } catch (PDOException $e) {
134)         global $debugmode;
135)         if ($debugmode) {
136)             system_failure("MySQL-Fehler: ".$e->getMessage()."\nQuery:\n".$stmt."\nParameters:\n".print_r($params, true));
137)         } else {
138)             system_failure("Datenbankfehler");
139)         }
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

140)     }
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

141)     return $result;