2020-11-19 23:14:48 +01:00
|
|
|
<?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':
|
2020-11-19 23:15:07 +01:00
|
|
|
$jsonAnswer = self::tsecmd($pdo,null);
|
|
|
|
echo json_encode($jsonAnswer);
|
2020-11-19 23:14:48 +01:00
|
|
|
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) {
|
|
|
|
if (is_null($request)) {
|
|
|
|
if (!isset($_POST['request'])) {
|
|
|
|
echo json_encode(array("status" => "ERROR","msg" => "No TSE request transmitted"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$request = $_POST['request'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$tseparams = self::getTseParams($pdo);
|
|
|
|
|
|
|
|
$transferdata = array(
|
|
|
|
"pass" => $tseparams['pass'],
|
|
|
|
"pin" => $tseparams['pin'],
|
|
|
|
"puk" => $tseparams['puk'],
|
|
|
|
"clientid" => $tseparams['clientid'],
|
|
|
|
"cmd" => $request
|
|
|
|
);
|
|
|
|
|
2020-11-19 23:24:04 +01:00
|
|
|
if (($request == "setup") || ($request == "factory_reset")) {
|
|
|
|
$hist = new HistFiller();
|
|
|
|
$hist->updateConfigInHist($pdo, 'tsepin', implode(',',$tseparams['pin']));
|
|
|
|
$hist->updateConfigInHist($pdo, 'tsepuk', implode(',',$tseparams['puk']));
|
|
|
|
}
|
|
|
|
|
2020-11-19 23:14:48 +01:00
|
|
|
$data = json_encode($transferdata);
|
|
|
|
$transferdataBase64 = base64_encode($data);
|
|
|
|
|
2020-11-19 23:15:07 +01:00
|
|
|
return self::sendToTSEConnector($tseparams['url'] . "/admin", $transferdataBase64,560);
|
2020-11-19 23:14:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-11-19 23:15:07 +01:00
|
|
|
try {
|
|
|
|
$ret = @file_get_contents($tseurl, false, $context);
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
return array("status" => "ERROR","msg" => "No connection with TSEConnector");
|
|
|
|
}
|
2020-11-19 23:14:48 +01:00
|
|
|
|
2020-11-19 23:15:07 +01:00
|
|
|
if ($ret === false) {
|
|
|
|
return array("status" => "ERROR","msg" => "No connection with TSEConnector");
|
2020-11-19 23:14:48 +01:00
|
|
|
}
|
2020-11-19 23:15:07 +01:00
|
|
|
return json_decode($ret, true);
|
2020-11-19 23:14:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
$tseanswer["usetse"] = DbUtils::$TSE_RUNTIME_ERROR;
|
|
|
|
}
|
|
|
|
return $tseanswer;
|
|
|
|
}
|
2020-11-19 23:15:07 +01:00
|
|
|
|
2020-11-19 23:14:48 +01:00
|
|
|
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 == "")) {
|
2020-11-19 23:15:07 +01:00
|
|
|
return array("status" => "OK");
|
2020-11-19 23:14:48 +01:00
|
|
|
} else {
|
2020-11-19 23:15:07 +01:00
|
|
|
$tseparams = self::getTseParams($pdo);
|
|
|
|
|
|
|
|
$transferdata = array(
|
|
|
|
"pass" => $tseparams['pass'],
|
|
|
|
"pin" => $tseparams['pin'],
|
|
|
|
"clientid" => $tseparams['clientid'],
|
|
|
|
"cmd" => "check"
|
|
|
|
);
|
|
|
|
|
|
|
|
$data = json_encode($transferdata);
|
|
|
|
$transferdataBase64 = base64_encode($data);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$tseanswer = self::sendToTSEConnector($tseurl . "/admin", $transferdataBase64,560);
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$tseanswer["usetse"] = DbUtils::$TSE_RUNTIME_ERROR;
|
|
|
|
$tseanswer["status"] = "ERROR";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-19 23:14:48 +01:00
|
|
|
if ($tseanswer["status"] == "OK") {
|
2020-11-19 23:15:07 +01:00
|
|
|
$tseanswer["usetse"] = DbUtils::$TSE_OK;
|
|
|
|
} else {
|
|
|
|
$tseanswer["usetse"] = DbUtils::$TSE_RUNTIME_ERROR;
|
2020-11-19 23:14:48 +01:00
|
|
|
}
|
2020-11-19 23:15:07 +01:00
|
|
|
return $tseanswer;
|
2020-11-19 23:14:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|