3c4b00512982a5ec3548c7f07a02f92d5b633083
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)     {
Hanno Böck Spaces between string conca...

Hanno Böck authored 6 months ago

23)         $dsn = "mysql:host=" . config('db_host', true);
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

24)         if (config('db_port', true)) {
Hanno Böck Spaces between string conca...

Hanno Böck authored 6 months ago

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)) {
Hanno Böck Spaces between string conca...

Hanno Böck authored 6 months ago

28)             $dsn = "mysql:unix_socket=" . config('db_socket', true);
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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;
Hanno Böck Fix not operator (!) spaces

Hanno Böck authored 6 months ago

102)     if (!isset($_db)) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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) {
Hanno Böck Spaces between string conca...

Hanno Böck authored 6 months ago

112)                 die("MySQL-Fehler: " . $e->getMessage());
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

113)             } else {
Bernd Wurst merge passkeys feature

Bernd Wurst authored 4 months ago

114)                 // log errors
115)                 $f = fopen("../dberror.log", "a");
116)                 fwrite($f, date('Y-m-d H:i:s') . ' ' . $_SERVER['PHP_SELF'] . ' DB exception: ' . $e->getMessage() . "\n");
117)                 fclose($f);
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

118)                 die("Fehler bei der Datenbankverbindung!");
119)             }
120)         }
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

121)     }
122) }
123) 
124) 
Hanno optional parameter to not w...

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

127)     global $_db;
128)     __ensure_connected();
129)     $backtrace = debug_backtrace();
Hanno Böck Spaces between string conca...

Hanno Böck authored 6 months ago

130)     DEBUG($backtrace[0]['file'] . ':' . $backtrace[0]['line'] . ': ' . htmlspecialchars($stmt));
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

131)     if ($params) {
132)         DEBUG($params);
133)     }
134)     try {
Bernd Wurst PHP 8.0 compatibility

Bernd Wurst authored 3 years ago

135)         $result = $_db->myquery($stmt, $params, $allowempty);
Hanno Böck Spaces between string conca...

Hanno Böck authored 6 months ago

136)         DEBUG('=> ' . $result->rowCount() . ' rows');
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

137)     } catch (PDOException $e) {
138)         global $debugmode;
139)         if ($debugmode) {
Hanno Böck Spaces between string conca...

Hanno Böck authored 6 months ago

140)             system_failure("MySQL-Fehler: " . $e->getMessage() . "\nQuery:\n" . $stmt . "\nParameters:\n" . print_r($params, true));
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

141)         } else {
Bernd Wurst merge passkeys feature

Bernd Wurst authored 4 months ago

142)             // log errors
143)             $f = fopen("../dberror.log", "a");
144)             fwrite($f, date('Y-m-d H:i:s') . ' ' . $_SERVER['PHP_SELF'] . ' DB exception: ' . $e->getMessage() . "\n");
145)             fclose($f);
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

146)             system_failure("Datenbankfehler");
147)         }
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

149)     return $result;