ordersprinter/webapp/php/updater.php

255 lines
7.2 KiB
PHP
Raw Normal View History

2020-11-19 23:02:33 +01:00
<?php
class Updater {
function handleCommand($command) {
if (!self::isUserAlreadyLoggedInAndAdmin()) {
echo json_encode(array("status" => "ERROR", "msg" => "Not authorized"));
} else {
$pdo = DbUtils::openDbAndReturnPdoStatic();
if ($command == 'getAvailableVersion') {
echo json_encode(self::getAvailableVersion($pdo));
} else if ($command == 'updatecheck') {
echo json_encode(self::updatecheck($pdo));
} else if ($command == 'replace') {
echo json_encode(self::replace($pdo,$_GET["fileindex"],$_GET["totalLines"]));
}
else {
echo "Kommando nicht unterstuetzt.";
}
}
}
private static function isUserAlreadyLoggedInAndAdmin() {
if(session_id() == '') {
session_start();
}
if (!isset($_SESSION['angemeldet']) || !$_SESSION['angemeldet']) {
return false;
} else {
return ($_SESSION['is_admin']);
}
}
private static function getFile($url,$file,$asArray = false,$timeout = 200) {
$ctx = stream_context_create(array('http' =>
array(
'timeout' => $timeout, // seconds
)
));
2020-11-19 23:02:42 +01:00
$url = $url . "/downloader.php?file=" . $file;
try {
$infoFile = file_get_contents($url, false, $ctx);
$test = substr($infoFile, 1,50);
if ($infoFile != FALSE) {
if ($asArray) {
$retArr = array();
$lines = explode("\n", $infoFile);
foreach($lines as $aLine) {
$l = trim($aLine);
if ($l != '') {
$retArr[] = $l;
}
}
return array("status" => "OK","msg" => $retArr);
} else {
return array("status" => "OK","msg" => $infoFile);
}
} else {
return array("status" => "ERROR","msg" => "File to replace not found ($url)");
}
} catch (Exception $ex) {
return array("status" => "ERROR","msg" => $ex->getMessage());
}
}
private static function getInfoFile($url,$file,$asArray = false,$timeout = 200) {
$ctx = stream_context_create(array('http' =>
array(
'timeout' => $timeout, // seconds
)
));
2020-11-19 23:02:33 +01:00
$url = $url . "/" . $file;
try {
$infoFile = @file_get_contents($url, false, $ctx);
if ($infoFile != FALSE) {
if ($asArray) {
$retArr = array();
$lines = explode("\n", $infoFile);
if (count($lines) < 1) {
return array("status" => "ERROR","msg" => "Info file not valid");
}
$versionMatch = '/^[0-9]*\.[0-9]*\.[0-9]*/';
$ret = preg_match($versionMatch, $lines[0]);
if ($ret == 0) {
return array("status" => "ERROR","msg" => "Info file has no version info.");
}
foreach($lines as $aLine) {
$l = trim($aLine);
if ($l != '') {
$retArr[] = $l;
}
}
return array("status" => "OK","msg" => $retArr);
} else {
return array("status" => "OK","msg" => $infoFile);
}
} else {
2020-11-19 23:02:42 +01:00
return array("status" => "ERROR","msg" => "File to replace not found ($url)");
2020-11-19 23:02:33 +01:00
}
} catch (Exception $ex) {
return array("status" => "ERROR","msg" => $ex->getMessage());
}
}
private static function getAvailableVersion($pdo) {
$url = CommonUtils::getConfigValue($pdo, "updateurl", '');
2020-11-19 23:02:42 +01:00
$infoFile = self::getInfoFile($url,'updateinfo.txt',true,3);
2020-11-19 23:02:33 +01:00
if ($infoFile["status"] != "OK") {
return array("status" => "ERROR","msg" => "could not get info file: " . $infoFile["msg"],"url" => $url);
}
$infoFileLines = $infoFile["msg"];
if (count($infoFileLines) > 1) {
2020-11-19 23:03:26 +01:00
$checkIfNewerVersion = self::isV2Newer('1.3.16',trim($infoFileLines[0]));
2020-11-19 23:02:49 +01:00
return array("status" => "OK","msg" => $infoFileLines[0],"url" => $url,"neweravailable" => ($checkIfNewerVersion ? 1 : 0));
2020-11-19 23:02:33 +01:00
} else {
return array("status" => "ERROR","msg" => "Info file not valid","url" => $url);
}
}
2020-11-19 23:02:49 +01:00
private static function isV2Newer($v1,$v2) {
if (is_null($v1) || is_null($v2)) {
return false;
}
$v1key = self::genVerKey($v1);
$v2key = self::genVerKey($v2);
if (is_null($v1key) || is_null($v2key)) {
return false;
}
if ($v1key < $v2key) {
return true;
} else {
return false;
}
}
private static function genVerKey($v) {
2020-11-19 23:02:57 +01:00
$vparts = explode('.',$v);
2020-11-19 23:02:49 +01:00
$len = count($vparts);
$key = 0;
try {
for ($i=0;$i<$len;$i++) {
$key += intval($vparts[$i]) * pow(1000,2-$i);
}
return $key;
} catch (Exception $e) {
return null;
}
}
2020-11-19 23:02:33 +01:00
private static function doCheck($lineArr) {
2020-11-19 23:02:42 +01:00
if (!is_writable("..")) {
return array("status" => "ERROR","msg" => "Root directory not writable. But this is necessary to create and delete install directory.");
}
if (!file_exists("../install")) {
if (!mkdir('../install', 0777)) {
return array("status" => "ERROR","msg" => "Install directory could not be created.");
}
} else {
if (!is_writable("../install")) {
return array("status" => "ERROR","msg" => "Cannot write into install directory.");
}
}
2020-11-19 23:02:33 +01:00
if (count($lineArr) < 2) {
return array("status" => "OK","msg" => '');
}
for ($i=1;$i<count($lineArr);$i++) {
if (trim($lineArr[$i]) == '') {
continue;
}
$aLine = "../" . $lineArr[$i];
$basename = basename($aLine);
$dirname = dirname($aLine);
$isDirExists = file_exists($dirname);
$isFileExists = file_exists($aLine);
$isDirWritable = is_writable($dirname);
$isFileWritable = is_writable($aLine);
if ($isDirExists) {
if ($isFileExists) {
if (!$isFileWritable) {
return array("status" => "ERROR","msg" => $aLine . " cannot be overwritten");
}
} else {
// file does not exist, but can it be created?
if (!$isDirWritable) {
return array("status" => "ERROR","msg" => $basename . " cannot be written into $dirname");
}
}
} else {
return array("status" => "ERROR","msg" => $dirname . " does not exist");
}
}
return array("status" => "OK","msg" => "");
}
public static function updatecheck($pdo) {
$url = CommonUtils::getConfigValue($pdo, "updateurl", '');
2020-11-19 23:02:42 +01:00
$infoFile = self::getInfoFile($url,'updateinfo.txt',true,3);
2020-11-19 23:02:33 +01:00
if ($infoFile["status"] != "OK") {
return array("status" => "ERROR","msg" => "could not get info file: " . $infoFile["msg"]);
}
$infoFileLines = $infoFile["msg"];
$check = self::doCheck($infoFileLines);
if ($check["status"] != "OK") {
$ret = array("status" => "ERROR","msg" => "Check returned: " . $check["msg"]);
} else {
$ret = array("status" => "OK","msg" => $infoFileLines);
}
return $ret;
}
public static function replace($pdo,$fileindex,$totallines) {
$url = CommonUtils::getConfigValue($pdo, "updateurl", '');
2020-11-19 23:02:42 +01:00
$infoFile = self::getInfoFile($url,'updateinfo.txt',true,3);
2020-11-19 23:02:33 +01:00
if ($infoFile["status"] != "OK") {
2020-11-19 23:02:42 +01:00
return array("status" => "ERROR","msg" => "could not get file: " . $infoFile["msg"]);
2020-11-19 23:02:33 +01:00
}
$lineArr = $infoFile["msg"];
try {
$fileToRead = $lineArr[intval($fileindex) + 1];
if (trim($fileToRead) != '') {
$targetFile = "../" . $fileToRead;
$fileContent = self::getFile($url, $fileToRead);
2020-11-19 23:02:42 +01:00
if ($fileContent["status"] == "OK") {
file_put_contents($targetFile, $fileContent["msg"]);
} else {
return array("status" => "ERROR","msg" => "$targetFile cannot be fetched from update server.");
}
2020-11-19 23:02:33 +01:00
}
} catch (Exception $ex) {
return array("status" => "ERROR","msg" => $ex->getMessage());
}
return array("status" => "OK","msg" => array("index" => $fileindex,"file" => $fileToRead,"totalLines" => $totallines));
}
}