2020-11-19 23:10:06 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'php/dbutils.php';
|
|
|
|
require_once 'php/config.php';
|
2020-11-19 23:12:39 +01:00
|
|
|
defined('DB') || define ( 'DB','mysql' );
|
2020-11-19 23:10:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Sync {
|
|
|
|
public static function insertSystemData($pdo,$systemdata) {
|
|
|
|
try {
|
2020-11-19 23:12:39 +01:00
|
|
|
DbUtils::log("insertSystemData started");
|
2020-11-19 23:10:06 +01:00
|
|
|
if (!isset($systemdata["guestcode"])) {
|
|
|
|
return array("status" => "ERROR","msg" => "No Access code transmitted!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$transmittedGuestcode = $systemdata["guestcode"];
|
|
|
|
if ($transmittedGuestcode != CODE) {
|
|
|
|
return array("status" => "ERROR","msg" => "Wrong Access code!");
|
|
|
|
}
|
2020-11-19 23:12:39 +01:00
|
|
|
|
2020-11-19 23:10:06 +01:00
|
|
|
self::insertIntoSystemTable($pdo, $systemdata, 'timezone');
|
|
|
|
self::insertIntoSystemTable($pdo, $systemdata, 'dailycode');
|
|
|
|
self::insertIntoSystemTable($pdo, $systemdata, 'resttables');
|
|
|
|
self::insertIntoSystemTable($pdo, $systemdata, 'types');
|
|
|
|
self::insertIntoSystemTable($pdo, $systemdata, 'products');
|
|
|
|
self::insertIntoSystemTable($pdo, $systemdata, 'currency');
|
|
|
|
self::insertIntoSystemTable($pdo, $systemdata, 'decpoint');
|
|
|
|
|
2020-11-19 23:12:39 +01:00
|
|
|
self::insertIntoSystemTable($pdo, $systemdata, 'askdaycode');
|
|
|
|
self::insertIntoSystemTable($pdo, $systemdata, 'asktablecode');
|
|
|
|
self::insertIntoSystemTable($pdo, $systemdata, 'guesttimeout');
|
2020-11-19 23:10:21 +01:00
|
|
|
|
2020-11-19 23:10:06 +01:00
|
|
|
if (isset($systemdata["timezone"])) {
|
|
|
|
self::updateOsAccessStatus($pdo, $systemdata["timezone"]);
|
|
|
|
}
|
2020-11-19 23:12:39 +01:00
|
|
|
$logo = '';
|
|
|
|
if (isset($systemdata["logo"]) && ($systemdata["logo"] != '')) {
|
|
|
|
$logo = $systemdata["logo"];
|
2020-11-19 23:12:37 +01:00
|
|
|
}
|
2020-11-19 23:12:39 +01:00
|
|
|
self::insertOrUpdateOrDeleteImage($pdo, 'logo', $logo, DbUtils::$TYPE_LOGO, null);
|
2020-11-19 23:12:37 +01:00
|
|
|
|
|
|
|
if (isset($systemdata["prodimages"])) {
|
2020-11-19 23:12:39 +01:00
|
|
|
DbUtils::log("prodimages are transmitted -> update now");
|
|
|
|
self::insertOrUpdateProdImages($pdo, $systemdata["prodimages"]);
|
2020-11-19 23:12:37 +01:00
|
|
|
}
|
2020-11-19 23:10:06 +01:00
|
|
|
return array("status" => "OK");
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
return array("status" => "ERROR","msg" => $ex->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 23:12:39 +01:00
|
|
|
|
|
|
|
private static function deleteImage_not_used($pdo,$imageName,$productid) {
|
|
|
|
if (DB == "mysql") {
|
|
|
|
if (is_null($productid)) {
|
|
|
|
$sql = "DELETE FROM %images% WHERE imagename=?";
|
|
|
|
DbUtils::execSql($pdo, $sql, array($imageName));
|
|
|
|
} else {
|
|
|
|
$sql = "DELETE FROM %images% WHERE productid=?";
|
|
|
|
DbUtils::execSql($pdo, $sql, array($productid));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2020-11-19 23:10:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 23:12:39 +01:00
|
|
|
private static function insertOrUpdateOrDeleteImage($pdo,$imageName,$content,$contenttype,$productid) {
|
|
|
|
$contentMd5 = null;
|
|
|
|
if ($content == '-') {
|
|
|
|
$content = '';
|
|
|
|
}
|
|
|
|
if (!is_null($content)) {
|
|
|
|
$contentMd5 = md5($content);
|
|
|
|
}
|
|
|
|
if (DB == "mysql") {
|
|
|
|
|
|
|
|
if (is_null($productid)) {
|
|
|
|
$sql = "SELECT id,content FROM %images% WHERE imagename=?";
|
|
|
|
$result = DbUtils::fetchSqlAll($pdo, $sql, array($imageName));
|
|
|
|
} else {
|
|
|
|
$sql = "SELECT id,content FROM %images% WHERE productid=?";
|
|
|
|
$result = DbUtils::fetchSqlAll($pdo, $sql, array($productid));
|
|
|
|
}
|
|
|
|
|
|
|
|
$delete = false;
|
|
|
|
if (!is_null($productid) && ($content == '')) {
|
|
|
|
$delete = true;
|
|
|
|
} else if (is_null($productid) && ($content == '')) {
|
|
|
|
$delete = true;
|
|
|
|
}
|
|
|
|
if ($delete) {
|
|
|
|
if (count($result) > 0) {
|
|
|
|
$id = $result[0]["id"];
|
|
|
|
$sql = "DELETE FROM %images% WHERE id=?";
|
|
|
|
DbUtils::execSql($pdo, $sql, array($id));
|
|
|
|
if (!is_null($productid)) {
|
|
|
|
DbUtils::log("Product image with id $productid deleted from mysql");
|
|
|
|
} else {
|
|
|
|
DbUtils::log("Product image with name $imageName deleted from mysql");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-19 23:10:21 +01:00
|
|
|
if (count($result) == 0) {
|
2020-11-19 23:12:39 +01:00
|
|
|
$sql = "INSERT INTO %images% (imagename,content,contenttype,productid) VALUES(?,?,?,?)";
|
|
|
|
DbUtils::execSql($pdo, $sql, array($imageName,$content,$contenttype,$productid));
|
|
|
|
DbUtils::log("Image with name '" . $imageName . "' and product id=" . (is_null($productid) ? "null":$productid) . " inserted into mysql");
|
2020-11-19 23:10:21 +01:00
|
|
|
} else {
|
2020-11-19 23:12:39 +01:00
|
|
|
$origContentMd5 = md5($result[0]["content"]);
|
|
|
|
if ($contentMd5 != $origContentMd5) {
|
|
|
|
$rowId = $result[0]["id"];
|
|
|
|
$sql = "UPDATE %images% SET imagename=?,content=?,contenttype=?,productid=? WHERE id=?";
|
|
|
|
DbUtils::execSql($pdo, $sql, array($imageName,$content,$contenttype,$productid,$rowId));
|
|
|
|
DbUtils::log("Image updated with imagename='$imageName' and productid=" . (is_null($productid) ? "null":$productid) . " in mysql");
|
|
|
|
}
|
2020-11-19 23:10:21 +01:00
|
|
|
}
|
2020-11-19 23:12:39 +01:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$filename = "db/" . IMAGES_FILE;
|
|
|
|
$images = array();
|
|
|
|
if (file_exists($filename)) {
|
|
|
|
$imagesContent = file_get_contents($filename);
|
|
|
|
$images = json_decode($imagesContent,true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$imgesWereChanged = false;
|
|
|
|
$foundImageAtKey = null;
|
|
|
|
|
|
|
|
$keys = array_keys($images);
|
|
|
|
foreach($keys as $aKey) {
|
|
|
|
$img = $images[$aKey];
|
|
|
|
if (is_null($productid)) {
|
|
|
|
if ($img["imagename"] == $imageName) {
|
|
|
|
$foundImageAtKey = $aKey;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($img["productid"] == $productid) {
|
|
|
|
$foundImageAtKey = $aKey;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$delete = false;
|
|
|
|
if ($imageName == 'logo') {
|
|
|
|
DbUtils::log("Logo content: " . $content);
|
|
|
|
}
|
|
|
|
if (!is_null($productid) && ($content == '')) {
|
|
|
|
$delete = true;
|
|
|
|
} else if (is_null($productid) && ($content == '')) {
|
|
|
|
$delete = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($delete) {
|
|
|
|
if (!is_null($foundImageAtKey)) {
|
|
|
|
unset ($images[$foundImageAtKey]);
|
|
|
|
file_put_contents($filename, json_encode($images));
|
|
|
|
if (!is_null($productid)) {
|
|
|
|
DbUtils::log("Product image with id $productid deleted from file");
|
|
|
|
} else {
|
|
|
|
DbUtils::log("Product image with name $imageName deleted from file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$img = array("imagename" => $imageName,"content" => $content, "contenttype" => $contenttype, "productid" => $productid);
|
|
|
|
if (is_null($foundImageAtKey)) {
|
|
|
|
$images[] = $img;
|
|
|
|
$imgesWereChanged = true;
|
|
|
|
DbUtils::log("Image with name '" . $imageName . "' and product id=" . (is_null($productid) ? "null":$productid) . " inserted into file db");
|
|
|
|
} else {
|
|
|
|
$origContentMd5 = md5($images[$foundImageAtKey]["content"]);
|
|
|
|
if ($contentMd5 != $origContentMd5) {
|
|
|
|
$images[$foundImageAtKey] = $img;
|
|
|
|
$imgesWereChanged = true;
|
|
|
|
DbUtils::log("Image updated with imagename='$imageName' and productid=" . (is_null($productid) ? "null":$productid) . " in file db");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($imgesWereChanged) {
|
|
|
|
file_put_contents($filename, json_encode($images));
|
|
|
|
}
|
|
|
|
} catch(Exception $ex) { }
|
2020-11-19 23:10:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 23:12:39 +01:00
|
|
|
private static function insertOrUpdateProdImages($pdo,$imagedata) {
|
2020-11-19 23:12:37 +01:00
|
|
|
foreach ($imagedata as $img) {
|
2020-11-19 23:12:39 +01:00
|
|
|
$productid = $img['prodid'];
|
2020-11-19 23:12:37 +01:00
|
|
|
$imgl = $img['imagedata'];
|
2020-11-19 23:12:39 +01:00
|
|
|
self::insertOrUpdateOrDeleteImage($pdo, '', $imgl, DbUtils::$TYPE_PRODIMG, $productid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function insertIntoSystemTable($pdo,$data,$item) {
|
|
|
|
if (isset($data[$item])) {
|
|
|
|
$value = $data[$item];
|
|
|
|
$valuemd5 = md5($value);
|
|
|
|
if (DB == "mysql") {
|
|
|
|
$sql = "SELECT value FROM %ossystem% WHERE item=?";
|
|
|
|
$origvalSql = DbUtils::fetchSqlAll($pdo, $sql, array($item));
|
|
|
|
if (count($origvalSql) == 0) {
|
|
|
|
$sql = "INSERT INTO %ossystem% (item,value) VALUES(?,?)";
|
|
|
|
DbUtils::execSql($pdo, $sql, array($item,$value));
|
|
|
|
} else {
|
|
|
|
// to avoid unnecessary write operations check if update is needed
|
|
|
|
$origvalmd5 = md5($origvalSql[0]["value"]);
|
|
|
|
if ($valuemd5 != $origvalmd5) {
|
|
|
|
$sql = "UPDATE %ossystem% SET value=? WHERE item=?";
|
|
|
|
DbUtils::execSql($pdo, $sql, array($value,$item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$filename = "db/" . OSSYSTEM_FILE;
|
|
|
|
try {
|
|
|
|
if (file_exists($filename)) {
|
|
|
|
$allOssystemvalues = file_get_contents($filename);
|
|
|
|
$dataItems = json_decode($allOssystemvalues, true);
|
|
|
|
if (isset($dataItems[$item])) {
|
|
|
|
$origvalmd5 = md5($dataItems[$item]);
|
|
|
|
if ($valuemd5 != $origvalmd5) {
|
|
|
|
$dataItems[$item] = $value;
|
|
|
|
file_put_contents($filename, json_encode($dataItems));
|
|
|
|
DbUtils::log("In $filename update item $item with value $value");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dataItems[$item] = $value;
|
|
|
|
file_put_contents($filename, json_encode($dataItems));
|
|
|
|
DbUtils::log("In $filename insert item $item with value $value");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dataItems = [];
|
|
|
|
$dataItems[$item] = $value;
|
|
|
|
file_put_contents($filename, json_encode($dataItems));
|
|
|
|
DbUtils::log("Create $filename and insert item $item with value $value");
|
|
|
|
}
|
|
|
|
} catch (Exception $ex) { }
|
|
|
|
}
|
2020-11-19 23:12:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 23:10:06 +01:00
|
|
|
private static function updateOsAccessStatus($pdo,$timezone) {
|
|
|
|
date_default_timezone_set($timezone);
|
|
|
|
$date = new DateTime();
|
|
|
|
$currentTimeStamp = $date->getTimestamp();
|
|
|
|
|
2020-11-19 23:12:39 +01:00
|
|
|
if (DB == "mysql") {
|
|
|
|
$sql = "SELECT id FROM %gueststatus% WHERE item=?";
|
|
|
|
$result = DbUtils::fetchSqlAll($pdo, $sql, array('lastosaccess'));
|
|
|
|
if (count($result) == 0) {
|
|
|
|
$sql = "INSERT INTO %gueststatus% (date,item) VALUES(?,?)";
|
|
|
|
} else {
|
|
|
|
$sql = "UPDATE %gueststatus% SET date=? WHERE item=?";
|
|
|
|
}
|
|
|
|
DbUtils::execSql($pdo, $sql, array($currentTimeStamp,'lastosaccess'));
|
2020-11-19 23:10:06 +01:00
|
|
|
} else {
|
2020-11-19 23:12:39 +01:00
|
|
|
$filename = "db/" . STATUS_FILE;
|
|
|
|
try {
|
|
|
|
if (file_exists($filename)) {
|
|
|
|
$lastaccess = file_get_contents($filename);
|
|
|
|
if ($currentTimeStamp != $lastaccess) {
|
|
|
|
file_put_contents($filename, $currentTimeStamp);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file_put_contents($filename, $currentTimeStamp);
|
|
|
|
}
|
|
|
|
} catch (Exception $ex) { }
|
2020-11-19 23:10:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fetchQueueData($pdo) {
|
2020-11-19 23:12:39 +01:00
|
|
|
if (DB == "mysql") {
|
|
|
|
$pdo->beginTransaction();
|
|
|
|
$sql = "SELECT id,date,tableid,prodid,tablecode,dailycode FROM %queue%";
|
|
|
|
$result = DbUtils::fetchSqlAll($pdo, $sql, null);
|
|
|
|
DbUtils::log("Transmit " . count($result) . " items to core system from mysql queue table");
|
|
|
|
$sql = "DELETE FROM %queue%";
|
|
|
|
DbUtils::execSql($pdo, $sql, null);
|
|
|
|
$pdo->commit();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
} else {
|
|
|
|
$filename = "db/" . QUEUE_FILE;
|
|
|
|
if (file_exists($filename)) {
|
|
|
|
$queueItemsInFile = file_get_contents($filename);
|
|
|
|
$queueItems = json_decode($queueItemsInFile,true);
|
|
|
|
DbUtils::log("Transmit " . count($queueItems) . " items to core system from file queue");
|
|
|
|
|
|
|
|
$emptyContent = json_encode(array());
|
|
|
|
file_put_contents($filename, $emptyContent);
|
|
|
|
|
|
|
|
return $queueItems;
|
|
|
|
} else {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
2020-11-19 23:10:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_POST["data"])) {
|
2020-11-19 23:12:39 +01:00
|
|
|
$pdo = null;
|
|
|
|
if (DB == "mysql") {
|
|
|
|
$pdo = DbUtils::openDbAndReturnPdoStatic();
|
|
|
|
}
|
2020-11-19 23:10:06 +01:00
|
|
|
|
|
|
|
$data = $_POST["data"];
|
|
|
|
|
|
|
|
$utfdata = base64_decode($data);
|
|
|
|
$objFormat = json_decode($utfdata, true);
|
|
|
|
|
|
|
|
$ret = Sync::insertSystemData($pdo,$objFormat);
|
|
|
|
|
|
|
|
if ($ret["status"] != "OK") {
|
|
|
|
echo json_encode($ret);
|
|
|
|
} else {
|
|
|
|
$queuedData = Sync::fetchQueueData($pdo);
|
|
|
|
$ret = array("status" => "OK","msg" => $queuedData);
|
|
|
|
echo json_encode($ret);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
echo "No data transmitted!";
|
|
|
|
}
|