From fdbcba8fc19d35aa9116ca4d132908bcf23dd64a Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 17 Jun 2024 16:08:55 +0200 Subject: [dozmod] Add support for CoW API --- modules-available/dozmod/api.inc.php | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/modules-available/dozmod/api.inc.php b/modules-available/dozmod/api.inc.php index b5030cc5..34b5136d 100644 --- a/modules-available/dozmod/api.inc.php +++ b/modules-available/dozmod/api.inc.php @@ -156,6 +156,7 @@ function getListForLocations(array $locationIds, bool $raw) to be in "exam-mode" and only offer him exams (no lectures) */ $key = 'lectures_' . cache_hash($locationIds); $examMode = Request::get('exams', 'normal-mode', 'string') !== 'normal-mode'; + $cow = Request::get('cow-user', null, 'string'); $clientServerMismatch = false; if (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 @@ -176,16 +177,21 @@ function getListForLocations(array $locationIds, bool $raw) $rawKey = $key . '_raw'; if ($raw) { Header('Content-Type: text/xml; charset=utf-8'); - if (cache_has($rawKey)) { + if ($cow === null && cache_has($rawKey)) { cache_get_passthru($rawKey); } - } elseif (cache_has($key)) { + } elseif ($cow === null && cache_has($key)) { return unserialize(cache_get($key)); } // Not in cache $url = LIST_URL . "?locations=" . implode('%20', $locationIds); if ($examMode) { $url .= '&exams'; + } else { + // Only allow CoW in non-exam environment + if ($cow !== null) { + $url .= '&cow-user=' . urlencode($cow); + } } $t = microtime(true); $value = Download::asString($url, 60, $code); @@ -195,9 +201,11 @@ function getListForLocations(array $locationIds, bool $raw) } if ($value === false || $code < 200 || $code > 299) return false; - cache_put($rawKey, $value); $list = xmlToLectureIds($value); - cache_put($key, serialize($list)); + if ($cow === null) { + cache_put($rawKey, $value); + cache_put($key, serialize($list)); + } if ($raw) { die($value); } @@ -214,12 +222,16 @@ function outputLectureXmlForLocation(array $locationIds) return getListForLocations($locationIds, true); } -function _getVmData(string $lecture_uuid, string $subResource = null) +function _getVmData(string $lecture_uuid, string $subResource = null, string $cowUser = null) { $url = VMX_URL . '/' . $lecture_uuid; if ($subResource !== null) { $url .= '/' . $subResource; } + if ($cowUser !== null) { + $url .= '?cow-user=' . urlencode($cowUser); + $url .= '&cow-type=' . urlencode(Request::get('cow-type', '', 'string')); + } $t = microtime(true); $response = Download::asString($url, 60, $code); $t = microtime(true) - $t; @@ -236,15 +248,17 @@ function _getVmData(string $lecture_uuid, string $subResource = null) /** Caching wrapper around _getVmData() **/ function outputResource(string $lecture_uuid, string $resource): void { + $key = $resource . '_' . $lecture_uuid; if ($resource === 'metadata') { // HACK: config.tgz is compressed, don't use gzip output handler @ob_end_clean(); Header('Content-Type: application/gzip'); + $cow = Request::get('cow-user', null, 'string'); } else { Header('Content-Type: text/plain; charset=utf-8'); + $cow = null; } - $key = $resource . '_' . $lecture_uuid; - if (cache_has($key)) { + if ($cow === null && cache_has($key)) { if ($resource === 'metadata' || $resource === 'vmx') { // HACK HACK HACK: Update launch counter as it was cached, // otherwise dmsd would take care of increasing it... @@ -253,14 +267,16 @@ function outputResource(string $lecture_uuid, string $resource): void } cache_get_passthru($key); } else { - $value = _getVmData($lecture_uuid, $resource); + $value = _getVmData($lecture_uuid, $resource, $cow); if ($value === false) return; if (is_int($value)) { http_response_code($value); exit; } - cache_put($key, $value); + if ($cow === null) { + cache_put($key, $value); + } die($value); } } -- cgit v1.2.3-55-g7522