summaryrefslogtreecommitdiffstats
path: root/modules-available/dozmod/api.inc.php
diff options
context:
space:
mode:
authorSimon Rettberg2017-11-03 13:25:42 +0100
committerSimon Rettberg2017-11-03 13:25:42 +0100
commit9ada6980a6ec29b9cec9495a77e9bae8ac4ab236 (patch)
treec5c8048f125660668afca5471c34e1a939d79c84 /modules-available/dozmod/api.inc.php
parent[baseconfig_bwlp] Add SLX_NET_SEARCH (diff)
downloadslx-admin-9ada6980a6ec29b9cec9495a77e9bae8ac4ab236.tar.gz
slx-admin-9ada6980a6ec29b9cec9495a77e9bae8ac4ab236.tar.xz
slx-admin-9ada6980a6ec29b9cec9495a77e9bae8ac4ab236.zip
[dozmod] Refactor dmsd proxing, better error handling, less c & p
Diffstat (limited to 'modules-available/dozmod/api.inc.php')
-rw-r--r--modules-available/dozmod/api.inc.php107
1 files changed, 26 insertions, 81 deletions
diff --git a/modules-available/dozmod/api.inc.php b/modules-available/dozmod/api.inc.php
index b7f6c4e7..de0883a6 100644
--- a/modules-available/dozmod/api.inc.php
+++ b/modules-available/dozmod/api.inc.php
@@ -45,7 +45,8 @@ function cache_has($key)
if ($mtime === false) {
return false; // cache miss
}
- if (time() - $mtime > CONFIG_DOZMOD_EXPIRE) {
+ $now = time();
+ if ($now < $mtime || $now - $mtime > CONFIG_DOZMOD_EXPIRE) {
return false;
} else {
return true;
@@ -94,7 +95,9 @@ function xmlToLectureIds($responseXML)
$uuids = [];
foreach ($xml->eintrag as $e) {
- $uuids[] = strval($e->uuid['param'][0]);
+ if (isset($e->uuid) && isset($e->uuid['param']) && isset($e->uuid['param'][0])) {
+ $uuids[] = strval($e->uuid['param'][0]);
+ }
}
return $uuids;
}
@@ -146,14 +149,14 @@ function getListForLocations($locationIds, $raw)
$key = 'lectures_' . cache_hash($locationIds);
$examMode = Request::get('exams', 'normal-mode', 'string') !== 'normal-mode';
$clientServerMismatch = false;
- if (Module::isAvailable('exams')) {
+ if ($raw && Module::isAvailable('exams')) {
// If we have the exam mode module, we can enforce a server side check and make sure it agrees with the client
$serverExamMode = Exams::isInExamMode($locationIds);
$clientServerMismatch = ($serverExamMode !== $examMode);
$examMode = $serverExamMode;
}
// Only enforce exam mode validity check if the client requests the raw xml data
- if ($raw && $clientServerMismatch) {
+ if ($clientServerMismatch) {
sendExamModeMismatch(); // does not return
}
// Proceed normally from here on
@@ -175,7 +178,7 @@ function getListForLocations($locationIds, $raw)
$url .= '&exams';
}
$value = Download::asString($url, 60, $code);
- if ($value === false)
+ if ($value === false || $code < 200 || $code > 299)
return false;
cache_put($rawKey, $value);
$list = xmlToLectureIds($value);
@@ -203,79 +206,45 @@ function _getVmData($lecture_uuid, $subResource = false)
$url .= '/' . $subResource;
}
$response = Download::asString($url, 60, $code);
+ if ($code < 200 || $code > 299)
+ return (int)$code;
return $response;
}
-/** Caching wrapper around _getVMX() **/
-function outputVMX($lecture_uuid)
-{
- $key = 'vmx_' . $lecture_uuid;
- if (cache_has($key)) {
- cache_get_passthru($key);
- } else {
- $value = _getVmData($lecture_uuid);
- if ($value === false)
- return false;
- cache_put($key, $value);
- die($value);
- }
-}
-
-function outputNetrules($lecture_uuid)
-{
- $key = 'netrules_' . $lecture_uuid;
- if (cache_has($key)) {
- cache_get_passthru($key);
- } else {
- $value = _getVmData($lecture_uuid, 'netrules');
- if ($value === false)
- return false;
- cache_put($key, $value);
- die($value);
- }
-}
-function outputNetshares($lecture_uuid)
-{
- $key = 'netshares_' . $lecture_uuid;
- if (cache_has($key)) {
- cache_get_passthru($key);
- } else {
- $value = _getVmData($lecture_uuid, 'netshares');
- if ($value === false)
- return false;
- cache_put($key, $value);
- die($value);
- }
-}
-function outputRunscript($lecture_uuid)
+/** Caching wrapper around _getVmData() **/
+function outputResource($lecture_uuid, $resource)
{
- $key = 'runscript_' . $lecture_uuid;
+ $key = $resource . '_' . $lecture_uuid;
if (cache_has($key)) {
cache_get_passthru($key);
} else {
- $value = _getVmData($lecture_uuid, 'runscript');
+ $value = _getVmData($lecture_uuid, $resource);
if ($value === false)
return false;
+ if (is_int($value)) {
+ http_response_code($value);
+ exit;
+ }
cache_put($key, $value);
die($value);
}
+ return false;
}
function fatalDozmodUnreachable()
{
Header('HTTP/1.1 504 Gateway Timeout');
- die('Resource not available');
+ die('DMSD currently not available');
}
-function readLectureParam()
+function readLectureParam($locationIds)
{
- global $location_ids;
$lecture = Request::get('lecture', false, 'string');
if ($lecture === false) {
Header('HTTP/1.1 400 Bad Request');
die('Missing lecture UUID');
}
- $lectures = getLectureUuidsForLocations($location_ids);
+ $lectures = getLectureUuidsForLocations($locationIds);
if ($lectures === false) {
fatalDozmodUnreachable();
}
@@ -310,38 +279,14 @@ if (substr($ip, 0, 7) === '::ffff:') {
$location_ids = Location::getFromIp($ip);
$location_ids = Location::getLocationRootChain($location_ids);
-if ($resource === 'vmx') {
- $lecture = readLectureParam();
- outputVMX($lecture);
- // outputVMX does not return on success
- fatalDozmodUnreachable();
-}
-
-if ($resource === 'netrules') {
- $lecture = readLectureParam();
- outputNetrules($lecture);
- // no return on success
- fatalDozmodUnreachable();
-}
-
-if ($resource === 'netshares') {
- $lecture = readLectureParam();
- outputNetshares($lecture);
- // no return on success
- fatalDozmodUnreachable();
-}
-
-if ($resource === 'runscript') {
- $lecture = readLectureParam();
- outputRunscript($lecture);
- // no return on success
- fatalDozmodUnreachable();
-}
-
if ($resource === 'list') {
outputLectureXmlForLocation($location_ids);
// Won't return on success...
fatalDozmodUnreachable();
+} else {
+ $lecture = readLectureParam($location_ids);
+ outputResource($lecture, $resource);
+ fatalDozmodUnreachable();
}
Header('HTTP/1.1 400 Bad Request');