ordersprinter/webapp/php/commonutils.php

422 lines
12 KiB
PHP
Raw Normal View History

2020-11-19 22:47:44 +01:00
<?php
require_once ('dbutils.php');
2020-11-19 23:10:26 +01:00
defined('T_ORDER') || define ('T_ORDER', 0);
defined('T_BILL') || define ('T_BILL', 1);
defined('T_REMOVE') || define ('T_REMOVE', 2);
defined('T_BILLSTORNO') || define ('T_BILLSTORNO', 3);
defined('T_BILLSTORNOREMOVE') || define ('T_BILLSTORNOREMOVE', 4);
defined('T_FROM_TABLE') || define ('T_FROM_TABLE', 5);
defined('T_TO_TABLE') || define ('T_TO_TABLE', 6);
2020-11-19 22:47:44 +01:00
class CommonUtils {
var $dbutils;
2020-11-19 23:00:05 +01:00
private static $plugins = null;
2020-11-19 22:47:44 +01:00
2020-11-19 23:00:05 +01:00
function __construct() {
$this->dbutils = new DbUtils();
// $this->products = new Products(); --> endless loop!
2020-11-19 22:47:44 +01:00
// $this->lastSettingOfDisplayMode = "all";
2020-11-19 23:00:05 +01:00
//error_reporting(E_ALL);
}
public static function setPluginConfig($plugins) {
self::$plugins = $plugins;
2020-11-19 22:47:44 +01:00
}
function verifyLastBillId($pdo,$nextIdToUse) {
if ($nextIdToUse == 1) {
return true;
}
2020-11-19 23:00:05 +01:00
if (is_null($pdo)) {
$pdo = $this->dbutils->openDbAndReturnPdo();
2020-11-19 22:47:44 +01:00
}
$nextIdToUse = intval($nextIdToUse);
2020-11-19 23:00:05 +01:00
$sql = "SELECT value,signature FROM %work% WHERE item=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array("lastbillid"));
$row =$stmt->fetchObject();
2020-11-19 22:47:44 +01:00
$lastBillid = intval($row->value);
2020-11-19 23:00:05 +01:00
$lastBillInc = $lastBillid+1;
2020-11-19 22:47:44 +01:00
$signature = base64_decode($row->signature);
if ($lastBillInc != $nextIdToUse) {
return false;
} else {
// test for correct signature
2020-11-19 23:00:05 +01:00
$pubkeyid = $this->getCert($pdo);
$ok = openssl_verify("B($lastBillid)", $signature, $pubkeyid);
openssl_free_key($pubkeyid);
if ($ok == 0) {
return false;
2020-11-19 22:47:44 +01:00
} else {
2020-11-19 23:00:05 +01:00
// is id already used in bill table?
2020-11-19 22:47:44 +01:00
$sql = "SELECT id FROM %bill% WHERE id=?";
2020-11-19 23:00:05 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
2020-11-19 22:47:44 +01:00
$stmt->execute(array($nextIdToUse));
if ($stmt->rowCount() > 0) {
return false;
} else {
// is there a gap or does the previous id exist?
$sql = "SELECT id FROM %bill% WHERE id=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($nextIdToUse - 1));
if ($stmt->rowCount() != 1) {
return false;
} else {
return true;
}
2020-11-19 23:00:05 +01:00
}
2020-11-19 22:47:44 +01:00
}
}
}
function getPrivkey($pdo) {
$privkey = $this->getKeyFromWorkTable($pdo, "privkey");
return(openssl_get_privatekey($privkey)); // private key
}
function getCert($pdo) {
$pubKey = $this->getKeyFromWorkTable($pdo, "cert");
return(openssl_get_publickey($pubKey));
}
function getKeyFromWorkTable($pdo,$key) {
$sql = "SELECT signature FROM %work% WHERE item=?";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($key));
$row =$stmt->fetchObject();
return($row->signature);
}
function setLastBillIdInWorkTable($pdo,$lastBillId) {
2020-11-19 23:00:05 +01:00
if (is_null($pdo)) {
$pdo = $this->dbutils->openDbAndReturnPdo();
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:05 +01:00
$pkeyid = $this->getPrivkey($pdo);
openssl_sign("B($lastBillId)", $signature, $pkeyid);
openssl_free_key($pkeyid);
$sql = "UPDATE %work% SET value=?, signature=? WHERE item=?";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
2020-11-19 23:00:05 +01:00
$signature = base64_encode($signature);
2020-11-19 22:47:44 +01:00
$stmt->execute(array($lastBillId,$signature,"lastbillid"));
}
function verifyBill($pdo,$id) {
if (is_null($pdo)) {
$pdo = $this->dbutils->openDbAndReturnPdo();
}
2020-11-19 23:10:09 +01:00
$sql = "SELECT billdate,brutto,ROUND(netto,2) as netto,userid,IF(tax is not null, tax, '0.00') as tax,signature,status FROM %bill% WHERE id=?";
2020-11-19 23:00:05 +01:00
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute(array($id));
$row = $stmt->fetchObject();
2020-11-19 22:47:44 +01:00
$billdate = $row->billdate;
$brutto = $row->brutto;
$netto = $row->netto;
$tax = $row->tax;
$userid = $row->userid;
$signature = $row->signature;
2020-11-19 23:03:29 +01:00
$status = $row->status;
return($this->verifyBillByValues($pdo,$billdate, $brutto, $netto, $tax, $userid, $signature, $status));
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:03:29 +01:00
function verifyBillByValues($pdo,$billdate,$brutto,$netto,$tax,$userid,$signature,$status) {
if (($status == "c") && ($brutto == 0.00)) {
// workaround - the signature for cash inserts of vaue 0.00 are sometimes incorrect
return true;
}
2020-11-19 23:00:05 +01:00
if (is_null($signature)) {
return false;
2020-11-19 22:47:44 +01:00
}
if (is_null($pdo)) {
$pdo = $this->dbutils->openDbAndReturnPdo();
}
2020-11-19 23:00:05 +01:00
$brutto = number_format($brutto, 2, ".", '');
$netto = number_format($netto, 2, ".", '');
2020-11-19 22:47:44 +01:00
$tax = number_format($tax, 2, ".", '');
2020-11-19 23:00:05 +01:00
2020-11-19 22:47:44 +01:00
$data = "D($billdate)B($brutto)N($netto)T($tax)U($userid)";
2020-11-19 23:00:05 +01:00
$pubkeyid = $this->getCert($pdo);
$ok = openssl_verify($data, $signature, $pubkeyid);
2020-11-19 22:47:44 +01:00
openssl_free_key($pubkeyid);
2020-11-19 23:00:05 +01:00
if ($ok == 0) {
return false;
} else {
return true;
2020-11-19 22:47:44 +01:00
}
}
function calcSignatureForBill($pdo,$theTime,$brutto,$netto,$tax,$userid) {
// now calculate the signature for the bill entry
$bruttostr = number_format($brutto, 2, ".", '');
$nettostr = number_format($netto, 2, ".", '');
if (is_null($tax)) {
$taxstr = "0.00";
2020-11-19 23:00:05 +01:00
} else {
2020-11-19 22:47:44 +01:00
$taxstr = number_format($tax, 2, ".", '');
2020-11-19 23:00:05 +01:00
}
$data = "D($theTime)B($bruttostr)N($nettostr)T($taxstr)U($userid)";
$pkeyid = $this->getPrivkey($pdo);
openssl_sign($data, $signature, $pkeyid);
2020-11-19 22:47:44 +01:00
openssl_free_key($pkeyid);
return $signature;
}
function createGridTableWithSqrtSizeOfButtons ($inputArray) {
2020-11-19 23:00:05 +01:00
// create a table that is optimal (sqrt-like size)
2020-11-19 22:47:44 +01:00
$numberOfIcons = count($inputArray);
if ($numberOfIcons == 0) {
// no items to display
return;
2020-11-19 23:00:05 +01:00
}
$numberOfCols = ceil(sqrt($numberOfIcons));
$porcentageWidth = floor(100/$numberOfCols);
echo '<table class=gridtable>';
2020-11-19 22:47:44 +01:00
$colcounter = 0;
for ($index=0;$index<$numberOfIcons;$index++) {
2020-11-19 23:00:05 +01:00
if ($colcounter == 0) {
echo "<tr><td>";
2020-11-19 22:47:44 +01:00
}
$anEntry = $inputArray[$index];
$textOfButton = $anEntry["textOfButton"]; #
$onClickMethod = $anEntry["onClickMethod"]; // With parameters!
$button = '<input type="button" value="' . $textOfButton . '"';
$button = $button . ' onclick="' . $onClickMethod . '"';
$button = $button . ' style="height: 50px; width:' . $porcentageWidth . '%; font-size:20px; background-color:#b3b3c9" />';
echo $button;
$colcounter++;
if ($colcounter == $numberOfCols) {
$colcounter = 0;
echo "</tr>";
}
2020-11-19 23:00:05 +01:00
}
echo "</tr>";
2020-11-19 22:47:44 +01:00
echo "</table>";
}
function createGridTableWithSqrtSizeOfStyleButtons($inputArray) {
$this->createGridTableWithSqrtSizeOfStyleButtonsAndHeader($inputArray,'','dummy');
}
2020-11-19 22:54:51 +01:00
function getTableNameFromId($pdo,$tableid) {
2020-11-19 22:47:44 +01:00
if (is_null($tableid) || ($tableid == 0)) {
return "-"; // togo
2020-11-19 23:00:05 +01:00
}
2020-11-19 22:54:51 +01:00
$sql = "SELECT tableno FROM %resttables% WHERE id=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($tableid));
2020-11-19 23:00:05 +01:00
$row = $stmt->fetchObject();
return $row->tableno;
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:54:51 +01:00
function getCurrentPriceLevel($pdo) {
2020-11-19 22:47:44 +01:00
$sql = "SELECT setting FROM %config% WHERE name='pricelevel'";
2020-11-19 22:54:51 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$row = $stmt->fetchObject();
$pricelevelid = $row->setting;
2020-11-19 22:47:44 +01:00
2020-11-19 22:54:51 +01:00
$sql = "SELECT id,name FROM %pricelevel% WHERE id=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($pricelevelid));
$row = $stmt->fetchObject();
$pricelevelname = $row->name;
2020-11-19 22:47:44 +01:00
return (array("id" => $pricelevelid, "name" => $pricelevelname));
}
2020-11-19 23:00:05 +01:00
function createGridTableWithSqrtSizeOfStyleButtonsAndHeader ($inputArray,$headline,$headercolor) {
// create a table that is optimal (sqrt-like size)
2020-11-19 22:47:44 +01:00
$numberOfIcons = count($inputArray);
2020-11-19 23:00:05 +01:00
if ($numberOfIcons == 0) {
// no items to display
return;
}
$numberOfCols = ceil(sqrt($numberOfIcons));
$porcentageWidth = floor(100.0/$numberOfCols);
2020-11-19 22:47:44 +01:00
echo '<table class=gridtable>';
// Headline
if ($headline <> '') {
echo '<tr><th style="background-color:#' . $headercolor . '">' . $headline . '</th>';
}
2020-11-19 23:00:05 +01:00
$colcounter = 0;
for ($index=0;$index<$numberOfIcons;$index++) {
if ($colcounter == 0) {
echo "<tr><td>";
}
$anEntry = $inputArray[$index];
$textOfButton = $anEntry["textOfButton"]; #
2020-11-19 22:47:44 +01:00
$onClickMethod = $anEntry["onClickMethod"]; // With parameters!
2020-11-19 23:00:05 +01:00
$style = $anEntry["style"];
2020-11-19 22:47:44 +01:00
$button = '<input type="button" value="' . $textOfButton . '"';
2020-11-19 23:00:05 +01:00
$button = $button . ' onclick="' . $onClickMethod . '"';
$button = $button . ' style="' . $style . '; width:' . $porcentageWidth . '%;" />';
echo $button;
$colcounter++;
if ($colcounter == $numberOfCols) {
$colcounter = 0;
echo "</tr>";
}
}
echo "</tr>";
echo "</table>";
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:05 +01:00
function getCurrency() {
$pdo = $this->dbutils->openDbAndReturnPdo();
2020-11-19 22:47:44 +01:00
2020-11-19 23:00:05 +01:00
$sql = "SELECT setting from %config% where name='currency'";
$stmt = $pdo->prepare($this->dbutils->resolveTablenamesInSqlString($sql));
$stmt->execute();
$row =$stmt->fetchObject();
if ($row != null) {
return $row->setting;
2020-11-19 22:47:44 +01:00
} else {
return "Euro";
2020-11-19 23:00:05 +01:00
}
}
2020-11-19 23:11:33 +01:00
public static function getRowSqlObject($pdo,$sql,$params = null) {
2020-11-19 23:00:55 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
if (is_null($params)) {
$stmt->execute();
} else {
$stmt->execute($params);
}
return ($stmt->fetchObject());
}
2020-11-19 23:11:33 +01:00
public static function fetchSqlAll($pdo,$sql,$params = null) {
2020-11-19 23:00:58 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
if (is_null($params)) {
$stmt->execute();
} else {
$stmt->execute($params);
}
2020-11-19 23:10:06 +01:00
return ($stmt->fetchAll(PDO::FETCH_ASSOC));
2020-11-19 23:00:58 +01:00
}
2020-11-19 23:00:55 +01:00
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:03:23 +01:00
public static function getConfigValueStmt($pdo,$stmt,$item,$default) {
$stmt->execute(array($item));
$row =$stmt->fetchObject();
if ($row->countid == 0) {
return $default;
} else {
return self::getExistingConfigValue($pdo, $item);
}
}
2020-11-19 23:00:05 +01:00
public static function getConfigValue($pdo,$item,$default) {
$sql = "SELECT count(id) as countid FROM %config% WHERE name=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($item));
$row =$stmt->fetchObject();
if ($row->countid == 0) {
return $default;
} else {
2020-11-19 23:00:35 +01:00
return self::getExistingConfigValue($pdo, $item);
2020-11-19 23:00:05 +01:00
}
}
2020-11-19 23:00:31 +01:00
public static function getExistingConfigValue($pdo,$item) {
$sql = "SELECT setting FROM %config% WHERE name=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($item));
$row = $stmt->fetchObject();
return $row->setting;
}
2020-11-19 23:00:05 +01:00
public static function callPlugin($pdo,$fct,$condition) {
try {
if (!is_null(self::$plugins)) {
if (array_key_exists($fct,self::$plugins)) {
$plugin = self::$plugins->$fct;
if (($plugin->execution) === $condition) {
$cls = $plugin->PluginClass;
$fct=$plugin->PluginFct;
$call = "Plugin\\$cls::$fct";
call_user_func($call,$pdo);
return true;
}
}
}
} catch(Exception $e) { }
return false;
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:00:18 +01:00
2020-11-19 23:03:29 +01:00
public static function log($pdo, $component, $message) {
$dblog = self::getConfigValue($pdo, "dblog", 1);
if ($dblog == 1) {
date_default_timezone_set(DbUtils::getTimeZone());
$currentTime = date('Y-m-d H:i:s');
$sql = "INSERT INTO %log% (date,component,message) VALUES(?,?,?)";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array($currentTime, $component, $message));
}
2020-11-19 23:00:18 +01:00
}
2020-11-19 23:03:29 +01:00
2020-11-19 23:00:18 +01:00
public static function getLog($pdo) {
$sql = "SELECT date,component,message FROM %log%";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$result = $stmt->fetchAll();
$txt = "";
foreach ($result as $aLogLine) {
$txt .= $aLogLine["date"] . ";" . $aLogLine["component"] . ";" . $aLogLine["message"] . "\n";
}
return $txt;
}
public static function getLastLog($pdo) {
$sql = "SELECT date,component,message FROM %log% WHERE DATE_SUB(NOW(),INTERVAL 2 HOUR) <= date";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
$result = $stmt->fetchAll();
$txt = "";
foreach ($result as $aLogLine) {
$txt .= $aLogLine["date"] . ";" . $aLogLine["component"] . ";" . $aLogLine["message"] . "\n";
}
return $txt;
}
public static function keepOnlyLastLog($pdo) {
$sql = "DELETE FROM %log% WHERE DATE_SUB(NOW(),INTERVAL 2 HOUR) > date";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute();
}
2020-11-19 23:03:43 +01:00
public static function strEndsWith($haystack, $needle)
{
return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}
2020-11-19 23:10:26 +01:00
public static function startsWith($aText, $needle)
{
return $needle === "" || strpos($aText, $needle) === 0;
}
2020-11-19 23:03:29 +01:00
}