summaryrefslogtreecommitdiffstats
path: root/modules-available/dozmod/api.inc.php
diff options
context:
space:
mode:
authorSimon Rettberg2016-07-06 11:57:47 +0200
committerSimon Rettberg2016-07-06 11:57:47 +0200
commitd9b3f3d6c9957d5f164ec6ce5341414841711297 (patch)
treea06c6afccda835b57e704c6d469addfae4bec28a /modules-available/dozmod/api.inc.php
parent[sysconfig] Refactor ldapauth and adauth classes; show mainpage warning; impr... (diff)
downloadslx-admin-d9b3f3d6c9957d5f164ec6ce5341414841711297.tar.gz
slx-admin-d9b3f3d6c9957d5f164ec6ce5341414841711297.tar.xz
slx-admin-d9b3f3d6c9957d5f164ec6ce5341414841711297.zip
Fix formatting
Diffstat (limited to 'modules-available/dozmod/api.inc.php')
-rw-r--r--modules-available/dozmod/api.inc.php219
1 files changed, 109 insertions, 110 deletions
diff --git a/modules-available/dozmod/api.inc.php b/modules-available/dozmod/api.inc.php
index e5214a55..1adcc429 100644
--- a/modules-available/dozmod/api.inc.php
+++ b/modules-available/dozmod/api.inc.php
@@ -1,4 +1,3 @@
-
<?php
/* small API server that acts as a proxy to the dozmod server.
* To reduce the number of requests and connections to dozmod-server, results
@@ -12,7 +11,7 @@
if (!Module::isAvailable('locations')) {
- die('require locations module');
+ die('require locations module');
}
@@ -24,52 +23,52 @@ $availableRessources = ['list', 'vmx', 'test', 'netrules'];
function cache_hash($obj)
{
- return md5(serialize($obj));
+ return md5(serialize($obj));
}
function cache_key_to_filename($key)
{
- return "/tmp/bwlp-slxadmin-cache-$key"; // TODO: hash
+ return "/tmp/bwlp-slxadmin-cache-$key"; // TODO: hash
}
function cache_put($key, $value)
{
- $filename = cache_key_to_filename($key);
- file_put_contents($filename, $value);
+ $filename = cache_key_to_filename($key);
+ file_put_contents($filename, $value);
}
function cache_has($key)
{
- $filename = cache_key_to_filename($key);
- $mtime = @filemtime($filename);
+ $filename = cache_key_to_filename($key);
+ $mtime = @filemtime($filename);
- if ($mtime === false) {
- return false; // cache miss
- }
- if (time() - $mtime > CONFIG_DOZMOD_EXPIRE) {
- return false;
- } else {
- return true;
- }
+ if ($mtime === false) {
+ return false; // cache miss
+ }
+ if (time() - $mtime > CONFIG_DOZMOD_EXPIRE) {
+ return false;
+ } else {
+ return true;
+ }
}
function cache_get($key)
{
- $filename = cache_key_to_filename($key);
- return file_get_contents($filename);
+ $filename = cache_key_to_filename($key);
+ return file_get_contents($filename);
}
/* good for large binary files */
function cache_get_passthru($key)
{
- $filename = cache_key_to_filename($key);
- $fp = fopen($filename, "r");
- if ($fp) {
- fpassthru($fp);
- exit;
- }
- error_log('Cannot passthrough cache file ' . $filename);
+ $filename = cache_key_to_filename($key);
+ $fp = fopen($filename, "r");
+ if ($fp) {
+ fpassthru($fp);
+ exit;
+ }
+ error_log('Cannot passthrough cache file ' . $filename);
}
/* END: Cache ---------------------------------------------------- */
@@ -89,9 +88,9 @@ function cache_get_passthru($key)
*/
function _getLecturesForLocations($locationIds, $examMode)
{
- $ids = implode('%20', $locationIds);
- $url = LIST_URL . "?locations=$ids" . ($examMode ? '&exams' : '');
- return Download::asString($url, 60, $code);
+ $ids = implode('%20', $locationIds);
+ $url = LIST_URL . "?locations=$ids" . ($examMode ? '&exams' : '');
+ return Download::asString($url, 60, $code);
}
/**
@@ -102,126 +101,126 @@ function _getLecturesForLocations($locationIds, $examMode)
*/
function xmlToLectureIds($responseXML)
{
- $xml = new SimpleXMLElement($responseXML);
- if (!isset($xml->eintrag))
- return [];
+ $xml = new SimpleXMLElement($responseXML);
+ if (!isset($xml->eintrag))
+ return [];
- $uuids = [];
- foreach ($xml->eintrag as $e) {
- $uuids[] = strval($e->uuid['param'][0]);
- }
- return $uuids;
+ $uuids = [];
+ foreach ($xml->eintrag as $e) {
+ $uuids[] = strval($e->uuid['param'][0]);
+ }
+ return $uuids;
}
/** Caching wrapper around _getLecturesForLocations() */
function getListForLocations($locationIds, $raw)
{
- /* if in any of the locations there is an exam active, consider the client
- to be in "exam-mode" and only offer him exams (no lectures) */
- $key = 'lectures_' . cache_hash($locationIds);
- $examMode = false;
- if (Module::isAvailable('exams')) {
- $examMode = Exams::isInExamMode($locationIds);
- if ($examMode) {
- $key .= '_exams';
- }
- }
- $rawKey = $key . '_raw';
- if ($raw) {
- Header('Content-Type: text/xml; charset=utf-8');
- if (cache_has($rawKey)) {
- cache_get_passthru($rawKey);
- }
- } elseif (cache_has($key)) {
- return unserialize(cache_get($key));
- }
- // Not in cache
- $value = _getLecturesForLocations($locationIds, $examMode);
- if ($value === false)
- return false;
- cache_put($rawKey, $value);
- $list = xmlToLectureIds($value);
- cache_put($key, serialize($list));
- if ($raw) {
- die($value);
- }
- return $list;
+ /* if in any of the locations there is an exam active, consider the client
+ to be in "exam-mode" and only offer him exams (no lectures) */
+ $key = 'lectures_' . cache_hash($locationIds);
+ $examMode = false;
+ if (Module::isAvailable('exams')) {
+ $examMode = Exams::isInExamMode($locationIds);
+ if ($examMode) {
+ $key .= '_exams';
+ }
+ }
+ $rawKey = $key . '_raw';
+ if ($raw) {
+ Header('Content-Type: text/xml; charset=utf-8');
+ if (cache_has($rawKey)) {
+ cache_get_passthru($rawKey);
+ }
+ } elseif (cache_has($key)) {
+ return unserialize(cache_get($key));
+ }
+ // Not in cache
+ $value = _getLecturesForLocations($locationIds, $examMode);
+ if ($value === false)
+ return false;
+ cache_put($rawKey, $value);
+ $list = xmlToLectureIds($value);
+ cache_put($key, serialize($list));
+ if ($raw) {
+ die($value);
+ }
+ return $list;
}
function getLectureUuidsForLocations($locationIds)
{
- return getListForLocations($locationIds, false);
+ return getListForLocations($locationIds, false);
}
function outputLectureXmlForLocation($locationIds)
{
- return getListForLocations($locationIds, true);
+ return getListForLocations($locationIds, true);
}
function _getVMX($lecture_uuid)
{
- $url = VMX_URL . '/' . $lecture_uuid;
- $response = Download::asString($url, 60, $code);
- return $response;
+ $url = VMX_URL . '/' . $lecture_uuid;
+ $response = Download::asString($url, 60, $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 = _getVMX($lecture_uuid);
- if ($value === false)
- return false;
- cache_put($key, $value);
- die($value);
- }
+ $key = 'vmx_' . $lecture_uuid;
+ if (cache_has($key)) {
+ cache_get_passthru($key);
+ } else {
+ $value = _getVMX($lecture_uuid);
+ if ($value === false)
+ return false;
+ cache_put($key, $value);
+ die($value);
+ }
}
function fatalDozmodUnreachable()
{
- Header('HTTP/1.1 504 Gateway Timeout');
- die('Resource not available');
+ Header('HTTP/1.1 504 Gateway Timeout');
+ die('Resource not available');
}
function readLectureParam()
{
- 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);
- if ($lectures === false) {
- fatalDozmodUnreachable();
- }
- /* check that the user requests a lecture that he is allowed to have */
- if (!in_array($lecture, $lectures)) {
- Header('HTTP/1.1 403 Forbidden');
- die("You don't have permission to access this lecture");
- }
- return $lecture;
+ 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);
+ if ($lectures === false) {
+ fatalDozmodUnreachable();
+ }
+ /* check that the user requests a lecture that he is allowed to have */
+ if (!in_array($lecture, $lectures)) {
+ Header('HTTP/1.1 403 Forbidden');
+ die("You don't have permission to access this lecture");
+ }
+ return $lecture;
}
// -----------------------------------------------------------------------------//
/* request data, don't trust */
-$resource = Request::get('resource', false, 'string');
+$resource = Request::get('resource', false, 'string');
if ($resource === false) {
- Util::traceError("you have to specify the 'resource' parameter");
+ Util::traceError("you have to specify the 'resource' parameter");
}
if (!in_array($resource, $availableRessources)) {
- Util::traceError("unknown resource: $resource");
+ Util::traceError("unknown resource: $resource");
}
$ip = $_SERVER['REMOTE_ADDR'];
if (substr($ip, 0, 7) === '::ffff:') {
- $ip = substr($ip, 7);
+ $ip = substr($ip, 7);
}
@@ -230,16 +229,16 @@ $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();
+ $lecture = readLectureParam();
+ outputVMX($lecture);
+ // outputVMX does not return on success
+ fatalDozmodUnreachable();
}
if ($resource === 'list') {
- outputLectureXmlForLocation($location_ids);
- // Won't return on success...
- fatalDozmodUnreachable();
+ outputLectureXmlForLocation($location_ids);
+ // Won't return on success...
+ fatalDozmodUnreachable();
}
Header('HTTP/1.1 400 Bad Request');