ordersprinter/gastsystem/php/dbutils.php

172 lines
4.5 KiB
PHP
Raw Normal View History

2020-11-19 23:10:06 +01:00
<?php
// Datenbank-Verbindungsparameter
require_once ('config.php');
2020-11-19 23:12:39 +01:00
defined('DB') || define ( 'DB','mysql' );
defined('LOG') || define ('LOG', false);
defined('MAX_LOG_CHARS') || define ('MAX_LOG_CHARS', 10000);
2020-11-19 23:10:06 +01:00
class DbUtils {
2020-11-19 23:12:37 +01:00
public static $TYPE_LOGO = 1;
public static $TYPE_PRODIMG = 2;
2020-11-19 23:12:39 +01:00
private static $cachedTimezone = "Europe/Berlin";
2020-11-19 23:10:06 +01:00
public static function openDbAndReturnPdoStatic () {
$dsn = 'mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DB;
$user = MYSQL_USER;
$password = MYSQL_PASSWORD;
$pdo = null;
try {
$pdo = new PDO($dsn, $user, $password);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
return $pdo;
}
public static function substTableAlias($sqlString) {
$out = str_replace("%ossystem%",TAB_PREFIX . 'ossystem',$sqlString);
$out = str_replace("%gueststatus%", TAB_PREFIX . 'gueststatus',$out);
$out = str_replace("%queue%", TAB_PREFIX . 'queue',$out);
2020-11-19 23:12:37 +01:00
$out = str_replace("%images%", TAB_PREFIX . 'images',$out);
2020-11-19 23:10:06 +01:00
return $out;
}
public static function getRowSqlObject($pdo,$sql,$params) {
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
if (is_null($params)) {
$stmt->execute();
} else {
$stmt->execute($params);
}
return ($stmt->fetchObject());
}
public static function fetchSqlAll($pdo,$sql,$params) {
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
if (is_null($params)) {
$stmt->execute();
} else {
$stmt->execute($params);
}
return ($stmt->fetchAll(PDO::FETCH_ASSOC));
}
public static function execSql($pdo,$sql,$params) {
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
if (is_null($params)) {
$stmt->execute();
} else {
$stmt->execute($params);
}
}
2020-11-19 23:10:21 +01:00
2020-11-19 23:12:39 +01:00
private static function getOsValueFromDbFile($item,$default) {
$filename = dirname(__FILE__) . '/../db/' . OSSYSTEM_FILE;
try {
if (file_exists($filename)) {
$allData = file_get_contents($filename);
$dataItems = json_decode($allData,true);
if (isset($dataItems[$item])) {
return $dataItems[$item];
} else {
return $default;
}
} else {
return $default;
}
} catch (Exception $ex) {
2020-11-19 23:10:21 +01:00
return $default;
2020-11-19 23:12:39 +01:00
}
}
public static function getConfigItem($pdo,$item,$default) {
$val = $default;
if (DB == "mysql") {
$sql = "select value from %ossystem% where item=?";
$result = DbUtils::fetchSqlAll($pdo, $sql, array($item));
if (count($result) > 0) {
$val = $result[0]["value"];
}
2020-11-19 23:10:21 +01:00
} else {
2020-11-19 23:12:39 +01:00
$val = self::getOsValueFromDbFile($item, $default);
}
if ($item == "timezone") {
self::$cachedTimezone = $val;
2020-11-19 23:10:21 +01:00
}
2020-11-19 23:12:39 +01:00
return $val;
}
public static function getImageData($pdo,$imageName,$productid) {
if (DB == "mysql") {
if (is_null($productid)) {
$sql = "SELECT content FROM %images% WHERE imagename=?";
$result = DbUtils::fetchSqlAll($pdo, $sql, array($imageName));
} else {
$sql = "SELECT content FROM %images% WHERE productid=?";
$result = DbUtils::fetchSqlAll($pdo, $sql, array($productid));
}
if (count($result) > 0) {
return ($result[0]["content"]);
} else {
return null;
}
} else {
$filename = dirname(__FILE__) . '/../db/' . IMAGES_FILE;
try {
if (file_exists($filename)) {
$fileContent = file_get_contents($filename);
$images = json_decode($fileContent,true);
foreach($images as $img) {
if (is_null($productid)) {
if ($img["imagename"] == $imageName) {
return $img["content"];
}
} else {
if ($img["productid"] == $productid) {
return $img["content"];
}
}
}
}
} catch (Exception $ex) {
return null;
}
return null;
}
}
private static function logTruncated($filename, $max) {
try {
$offset = filesize($filename) - $max;
if ($offset > 0) {
$logsToKeep = file_get_contents($filename, NULL, NULL, $offset, $max);
file_put_contents($filename, $logsToKeep);
}
} catch (Exception $ex) {}
}
public static function log($str) {
if (LOG) {
try {
date_default_timezone_set(self::$cachedTimezone);
$logfile = dirname(__FILE__) . '/../db/log.txt';
self::logTruncated($logfile, MAX_LOG_CHARS);
$logtime = date('Y-m-d H:i:s');
file_put_contents($logfile, "$logtime: $str\r\n", FILE_APPEND);
} catch (Exception $ex) { }
}
}
public static function logVar($description,$v) {
ob_start();
print_r($v);
$result = ob_get_clean();
self::log($description . ": " . $result);
2020-11-19 23:10:21 +01:00
}
2020-11-19 23:10:06 +01:00
}