d0416d23b70a701e7c12b884bd947d4c670cc50e
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) 
Bernd Wurst Copyright year update

Bernd Wurst authored 6 years ago

5) Written 2008-2018 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) 
9) To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
10) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

11) You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

12) http://creativecommons.org/publicdomain/zero/1.0/
13) 
14) 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.
15) */
16) 
17) require_once('inc/base.php');
18) require_once('inc/error.php');
19) require_once('inc/debug.php');
20) 
21) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

30)         if (config('db_socket', true)) {
31)             $dsn = "mysql:unix_socket=".config('db_socket', true);
32)         }
33)         $username = config('db_user', true);
34)         $password = config('db_pass', true);
35)         parent::__construct($dsn, $username, $password, array(PDO::ATTR_TIMEOUT => "30"));
36)     }
37) 
38) 
39)     /*
40)       Wenn Parameter übergeben werden, werden Queries immer als Prepared statements übertragen
41)     */
Hanno optional parameter to not w...

Hanno authored 5 years ago

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

Hanno authored 5 years ago

43)     {
44)         if (is_array($params)) {
Hanno optional parameter to not w...

Hanno authored 5 years ago

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

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

68)     }
69) }
70) 
71) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

126)     global $_db;
127)     __ensure_connected();
128)     $backtrace = debug_backtrace();
129)     DEBUG($backtrace[0]['file'].':'.$backtrace[0]['line'].': '.htmlspecialchars($stmt));
130)     if ($params) {
131)         DEBUG($params);
132)     }
133)     try {
Hanno optional parameter to not w...

Hanno authored 5 years ago

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

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

144)     return $result;