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': $jsonAnswer = self::tsecmd($pdo,null); echo json_encode($jsonAnswer); 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 ); if (($request == "setup") || ($request == "factory_reset")) { $hist = new HistFiller(); $hist->updateConfigInHist($pdo, 'tsepin', implode(',',$tseparams['pin'])); $hist->updateConfigInHist($pdo, 'tsepuk', implode(',',$tseparams['puk'])); } $data = json_encode($transferdata); $transferdataBase64 = base64_encode($data); return self::sendToTSEConnector($tseparams['url'] . "/admin", $transferdataBase64,560); } 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); try { $ret = @file_get_contents($tseurl, false, $context); } catch (Exception $ex) { return array("status" => "ERROR","msg" => "No connection with TSEConnector"); } if ($ret === false) { return array("status" => "ERROR","msg" => "No connection with TSEConnector"); } return json_decode($ret, true); } 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; } 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 array("status" => "OK"); } else { $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"; } if ($tseanswer["status"] == "OK") { $tseanswer["usetse"] = DbUtils::$TSE_OK; } else { $tseanswer["usetse"] = DbUtils::$TSE_RUNTIME_ERROR; } return $tseanswer; } } 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; } }