class/database.php
f1f231f5
 <?php
 /*
 This file belongs to the Webinterface of schokokeks.org Hosting
 
cf54502a
 Written 2008-2018 by schokokeks.org Hosting, namely
f1f231f5
   Bernd Wurst <bernd@schokokeks.org>
   Hanno Böck <hanno@schokokeks.org>
 
 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.
 
2626dd47
 You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see
f1f231f5
 http://creativecommons.org/publicdomain/zero/1.0/
 
 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.
 */
 
 require_once('inc/base.php');
 require_once('inc/error.php');
 require_once('inc/debug.php');
 
 
2626dd47
 class DB extends PDO
 {
     public function __construct()
     {
         $dsn = "mysql:host=".config('db_host', true);
         if (config('db_port', true)) {
             $dsn .= ';port='.config('db_port', true);
c0def067
         }
2626dd47
         if (config('db_socket', true)) {
             $dsn = "mysql:unix_socket=".config('db_socket', true);
         }
         $username = config('db_user', true);
         $password = config('db_pass', true);
         parent::__construct($dsn, $username, $password, array(PDO::ATTR_TIMEOUT => "30"));
     }
 
 
     /*
       Wenn Parameter übergeben werden, werden Queries immer als Prepared statements übertragen
     */
4f9eac39
     public function query($stmt, $params = null, $allowempty = false)
2626dd47
     {
         if (is_array($params)) {
4f9eac39
             if (config("enable_debug") && !$allowempty) {
2626dd47
                 foreach (array_values($params) as $p) {
                     if ($p === '') {
                         DEBUG("Potential bug, empty string found in database parameters");
                         warning("Potential bug, empty string found in database parameters");
                     }
                 }
             }
             $response = parent::prepare($stmt);
             $response->execute($params);
             return $response;
         } else {
             if (strtoupper(substr($stmt, 0, 6)) == "INSERT" ||
44996558
           strtoupper(substr($stmt, 0, 7)) == "REPLACE" ||
d88d35dc
           strpos(strtoupper($stmt), "WHERE") > 0) { // Das steht nie am Anfang
2626dd47
                 $backtrace = debug_backtrace();
                 $wherepart = substr(strtoupper($stmt), strpos(strtoupper($stmt), "WHERE"));
                 if ((strpos($wherepart, '"') > 0 || strpos($wherepart, "'") > 0) && config("enable_debug")) {
                     warning("Possibly unsafe SQL statement in {$backtrace[1]['file']} line {$backtrace[1]['line']}:\n$stmt");
                 }
             }
             return parent::query($stmt);
d88d35dc
         }
f1f231f5
     }
 }
 
 
2626dd47
 /* FIXME
f1f231f5
    Das ist etwas unelegant. Soll nur übergangsweise verwendet werden bis alles auf prepared statements umgestellt ist
 */
 function db_escape_string($string)
 {
2626dd47
     if (config("enable_debug")) {
         $backtrace = debug_backtrace();
         warning("call to db_escape_string() in {$backtrace[0]['file']} line {$backtrace[0]['line']}");
     }
     global $_db;
     __ensure_connected();
     $quoted = $_db->quote($string);
     // entferne die quotes, damit wird es drop-in-Kompatibel zu db_escape_string()
     $ret = substr($quoted, 1, -1);
     return $ret;
f1f231f5
 }
 
 
 function db_insert_id()
 {
2626dd47
     global $_db;
     __ensure_connected();
     return $_db->lastInsertId();
f1f231f5
 }
 
 
 function __ensure_connected()
 {
2626dd47
     /*
       Dieses Kontrukt ist vermultich noch schlimmer als ein normales singleton
       aber es hilft uns in unserem prozeduralen Kontext
     */
     global $_db;
     if (! isset($_db)) {
         try {
             DEBUG("Neue Datenbankverbindung!");
             $_db = new DB();
             $_db->query("SET NAMES utf8mb4");
             $_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
             $_db->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
         } catch (PDOException $e) {
             global $debugmode;
             if ($debugmode) {
                 die("MySQL-Fehler: ".$e->getMessage());
             } else {
                 die("Fehler bei der Datenbankverbindung!");
             }
         }
f1f231f5
     }
 }
 
 
4f9eac39
 function db_query($stmt, $params = null, $allowempty = false)
f1f231f5
 {
2626dd47
     global $_db;
     __ensure_connected();
     $backtrace = debug_backtrace();
     DEBUG($backtrace[0]['file'].':'.$backtrace[0]['line'].': '.htmlspecialchars($stmt));
     if ($params) {
         DEBUG($params);
     }
     try {
4f9eac39
         $result = $_db->query($stmt, $params, $allowempty);
2626dd47
         DEBUG('=> '.$result->rowCount().' rows');
     } catch (PDOException $e) {
         global $debugmode;
         if ($debugmode) {
             system_failure("MySQL-Fehler: ".$e->getMessage()."\nQuery:\n".$stmt."\nParameters:\n".print_r($params, true));
         } else {
             system_failure("Datenbankfehler");
         }
f1f231f5
     }
2626dd47
     return $result;
f1f231f5
 }