"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 ) )); $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 { return array("status" => "ERROR","msg" => "Info file not found"); } } catch (Exception $ex) { return array("status" => "ERROR","msg" => $ex->getMessage()); } } private static function getAvailableVersion($pdo) { $url = CommonUtils::getConfigValue($pdo, "updateurl", ''); $infoFile = self::getFile($url,'updateinfo.txt',true,3); 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) { return array("status" => "OK","msg" => $infoFileLines[0],"url" => $url); } else { return array("status" => "ERROR","msg" => "Info file not valid","url" => $url); } } private static function doCheck($lineArr) { if (count($lineArr) < 2) { return array("status" => "OK","msg" => ''); } for ($i=1;$i "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", ''); $infoFile = self::getFile($url,'updateinfo.txt',true,3); 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", ''); $infoFile = self::getFile($url,'updateinfo.txt',true,3); if ($infoFile["status"] != "OK") { return array("status" => "ERROR","msg" => "could not get info file: " . $infoFile["msg"]); } $lineArr = $infoFile["msg"]; try { $fileToRead = $lineArr[intval($fileindex) + 1]; if (trim($fileToRead) != '') { $targetFile = "../" . $fileToRead; $fileContent = self::getFile($url, $fileToRead); file_put_contents($targetFile, $fileContent["msg"]); } } catch (Exception $ex) { return array("status" => "ERROR","msg" => $ex->getMessage()); } return array("status" => "OK","msg" => array("index" => $fileindex,"file" => $fileToRead,"totalLines" => $totallines)); } }