ordersprinter/webapp/php/utilities/tse.php

214 lines
5.9 KiB
PHP

<?php
require_once (__DIR__. '/../dbutils.php');
class Tse {
private static $rights = array(
"tsecmd" => array("loggedin" => 1, "isadmin" => 0, "rights" => null)
);
public static function handleCommand($command) {
if (!CommonUtils::checkRights($command, self::$rights)) {
return false;
}
$pdo = DbUtils::openDbAndReturnPdoStatic();
switch ($command) {
case 'tsecmd':
self::tsecmd($pdo,null);
break;
default:
echo json_encode(array("status" => "ERROR", "msg" => "Command not supported"));
break;
}
}
private static function getPostArgOrDbData($pdo,$dbconfigitem,$postarg) {
$value = CommonUtils::getConfigValue($pdo, $dbconfigitem, '');
if (isset($_POST[$postarg])) {
$value = $_POST[$postarg];
}
return $value;
}
private static function csvToArray($csvStr) {
$values = array();
try {
$parts = explode(",", $csvStr);
foreach($parts as $p) {
$values[] = intval($p);
}
} catch (Exception $ex) {
}
return $values;
}
private static function getTseParams($pdo) {
$pin = self::getPostArgOrDbData($pdo, 'tsepin', 'pin');
$pinBytes = self::csvToArray($pin);
$puk = self::getPostArgOrDbData($pdo, 'tsepuk', 'puk');
$pukBytes = self::csvToArray($puk);
$clientid = CommonUtils::getConfigValue($pdo, 'sn', '');
return array(
"url" => self::getPostArgOrDbData($pdo, 'tseurl', 'url'),
"pass" => self::getPostArgOrDbData($pdo, 'tsepass', 'pass'),
"clientid" => $clientid,
"pin" => $pinBytes,
"puk" => $pukBytes
);
}
private static function tsecmd($pdo,$request) {
$calledInternally = true;
if (is_null($request)) {
if (!isset($_POST['request'])) {
echo json_encode(array("status" => "ERROR","msg" => "No TSE request transmitted"));
return;
}
$request = $_POST['request'];
$calledInternally = false;
}
$tseparams = self::getTseParams($pdo);
$transferdata = array(
"pass" => $tseparams['pass'],
"pin" => $tseparams['pin'],
"puk" => $tseparams['puk'],
"clientid" => $tseparams['clientid'],
"cmd" => $request
);
$data = json_encode($transferdata);
$transferdataBase64 = base64_encode($data);
//$transferdataBase64 = CommonUtils::base64_encode_url($data);
$tseanswer = self::sendToTSEConnector($tseparams['url'] . "/admin", $transferdataBase64,560);
if ($calledInternally) {
return $tseanswer;
} else {
echo json_encode($tseanswer);
}
}
private static function sendToTSEConnector($tseurl, $data,$timeout) {
$query = http_build_query(array("data" => $data));
$opts = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($query) . "\r\n" .
"User-Agent:MyAgent/1.0\r\n",
'method' => 'POST',
'content' => $query,
'timeout' => $timeout
)
);
$context = stream_context_create($opts);
$ret = file_get_contents($tseurl, false, $context);
if (!$ret) {
return array("status" => "ERROR","msg" => "Communication with TSEConnector not successful!");
}
return array("status" => "OK","msg" => $ret);
}
private static function sendValueToTseForSigning($pdo,$valueToSign,$cmd) {
$useTse = CommonUtils::getConfigValue($pdo, 'usetse', 0);
if ($useTse == DbUtils::$NO_TSE) {
return array("status" => "OK","usetse" => DbUtils::$NO_TSE);
} else if ($useTse == DbUtils::$TSE_KNOWN_ERROR) {
return array("status" => "OK","usetse" => DbUtils::$TSE_KNOWN_ERROR);
}
$tseurl = trim(CommonUtils::getConfigValue($pdo, 'tseurl', ''));
if ($tseurl == "") {
return array("status" => "OK","usetse" => DbUtils::$TSE_MISCONFIG);
}
$tseparams = self::getTseParams($pdo);
$transferdata = array(
"pass" => $tseparams['pass'],
"pin" => $tseparams['pin'],
"clientid" => $tseparams['clientid'],
"cmd" => $cmd,
"value" => $valueToSign
);
$data = json_encode($transferdata);
$transferdataBase64 = base64_encode($data);
$tseanswer = self::sendToTSEConnector($tseurl . "/sign", $transferdataBase64,560);
if ($tseanswer["status"] == "OK") {
$tseanswer["usetse"] = DbUtils::$TSE_OK;
if (isset($tseanswer["msg"])) {
$msg = $tseanswer["msg"];
$jsonMsg = json_decode($msg,true);
if (isset($jsonMsg["status"])) {
$stat = $jsonMsg["status"];
if ($stat != "OK") {
$tseanswer["usetse"] = DbUtils::$TSE_RUNTIME_ERROR;
$tseanswer["status"] = "ERROR";
}
}
} else {
$tseanswer["usetse"] = DbUtils::$TSE_OK;
}
} else {
$tseanswer["usetse"] = DbUtils::$TSE_RUNTIME_ERROR;
}
return $tseanswer;
}
public static function sendNormalBillToTSE($pdo,$billValueToSign) {
return self::sendValueToTseForSigning($pdo, $billValueToSign, "signnormalbill");
}
public static function sendOrdersToTSE($pdo,$prodEntriesToSign) {
return self::sendValueToTseForSigning($pdo, $prodEntriesToSign, "signorders");
}
public static function sendFreeContentToTSE($pdo,$freeContent) {
return self::sendValueToTseForSigning($pdo, $freeContent, "signfreecontent");
}
public static function checkTseServerAccesible($pdo) {
$useTse = CommonUtils::getConfigValue($pdo, 'usetse', 0);
$tseurl = CommonUtils::getConfigValue($pdo, 'tseurl', "");
if (($useTse == 0) || ($tseurl == "")) {
return 1;
} else {
$tseanswer = self::sendToTSEConnector($tseurl . "/info?cmd=ping", "", 200);
if ($tseanswer["status"] == "OK") {
$answer = $tseanswer["msg"];
if ($answer == "WAITING") {
self::tsecmd($pdo,"selftest");
return 1;
} else {
return 1;
}
}
}
return 0;
}
private static function getClientIP()
{
$ipaddress = 'UNKNOWN';
$keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
foreach ($keys as $k) {
if (isset($_SERVER[$k]) && !empty($_SERVER[$k]) && filter_var($_SERVER[$k], FILTER_VALIDATE_IP)) {
$ipaddress = $_SERVER[$k];
break;
}
}
return $ipaddress;
}
}