71e87c960a844568bbe0c2a7dd012d8b9247b037
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 12 years ago

1) <?php
Hanno Böck Add newlines before comment...

Hanno Böck authored 1 year ago

2) 
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 12 years ago

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

Hanno Böck authored 3 years ago

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

Bernd Wurst authored 12 years ago

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

Hanno Böck authored 3 years ago

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

Bernd Wurst authored 12 years ago

11) 
12) 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.
13) */
14) 
15) require_once('inc/base.php');
16) require_once('inc/error.php');
17) require_once('inc/debug.php');
18) 
19) 
Hanno Fix coding style with php-c...

Hanno authored 7 years ago

20) class DB extends PDO
21) {
22)     public function __construct()
23)     {
Hanno Böck Spaces between string conca...

Hanno Böck authored 2 years ago

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

Hanno authored 7 years ago

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

Hanno Böck authored 2 years ago

26)             $dsn .= ';port=' . config('db_port', true);
Bernd Wurst Weitere Umstellungen auf pr...

Bernd Wurst authored 12 years ago

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

Hanno authored 7 years ago

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

Hanno Böck authored 2 years ago

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

Hanno authored 7 years ago

30)         }
31)         $username = config('db_user', true);
32)         $password = config('db_pass', true);
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 4 years ago

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

Hanno authored 7 years ago

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

Bernd Wurst authored 5 years ago

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

Hanno authored 7 years ago

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

Hanno authored 7 years ago

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

Hanno authored 7 years ago

44)                 foreach (array_values($params) as $p) {
45)                     if ($p === '') {
46)                         DEBUG("Potential bug, empty string found in database parameters");
47)                         warning("Potential bug, empty string found in database parameters");
48)                     }
49)                 }
50)             }
51)             $response = parent::prepare($stmt);
52)             $response->execute($params);
53)             return $response;
54)         } else {
Hanno Böck Neuer @PER codingstyle mit...

Hanno Böck authored 8 months ago

55)             if (strtoupper(substr($stmt, 0, 6)) == "INSERT"
56)           || strtoupper(substr($stmt, 0, 7)) == "REPLACE"
57)           || strpos(strtoupper($stmt), "WHERE") > 0) { // Das steht nie am Anfang
Hanno Fix coding style with php-c...

Hanno authored 7 years ago

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

Bernd Wurst authored 12 years ago

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

Bernd Wurst authored 12 years ago

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

Hanno authored 7 years ago

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

Bernd Wurst authored 12 years ago

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

Hanno authored 7 years ago

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

Bernd Wurst authored 12 years ago

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

Hanno authored 7 years ago

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

Bernd Wurst authored 12 years ago

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

Hanno authored 7 years ago

98)     /*
99)       Dieses Kontrukt ist vermultich noch schlimmer als ein normales singleton
100)       aber es hilft uns in unserem prozeduralen Kontext
101)     */
102)     global $_db;
Hanno Böck Fix not operator (!) spaces

Hanno Böck authored 2 years ago

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

Hanno authored 7 years ago

104)         try {
105)             DEBUG("Neue Datenbankverbindung!");
106)             $_db = new DB();
107)             $_db->query("SET NAMES utf8mb4");
108)             $_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
109)             $_db->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
110)         } catch (PDOException $e) {
111)             global $debugmode;
112)             if ($debugmode) {
Hanno Böck Spaces between string conca...

Hanno Böck authored 2 years ago

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

Hanno authored 7 years ago

114)             } else {
Bernd Wurst merge passkeys feature

Bernd Wurst authored 2 years ago

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

Hanno authored 7 years ago

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

Bernd Wurst authored 12 years ago

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

Hanno authored 7 years ago

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

Bernd Wurst authored 12 years ago

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

Hanno authored 7 years ago

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

Hanno Böck authored 2 years ago

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

Hanno authored 7 years ago

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

Bernd Wurst authored 5 years ago

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

Hanno Böck authored 2 years ago

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

Hanno authored 7 years ago

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

Hanno Böck authored 2 years ago

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

Hanno authored 7 years ago

142)         } else {
Bernd Wurst merge passkeys feature

Bernd Wurst authored 2 years ago

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

Hanno authored 7 years ago

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

Bernd Wurst authored 12 years ago

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

Hanno authored 7 years ago

150)     return $result;