summaryrefslogtreecommitdiffstats
path: root/modules-available/dozmod/api.inc.php
diff options
context:
space:
mode:
authorSimon Rettberg2016-06-09 14:20:44 +0200
committerSimon Rettberg2016-06-09 14:20:44 +0200
commitbdd5860435e7bfd7f9b656917e73d558aa8d7ffa (patch)
treecf86e2c338cebf3db47c75f8bbb8ebe538382d40 /modules-available/dozmod/api.inc.php
parentMerge branch 'modularization' of dnbd3:openslx-ng/slx-admin into modularization (diff)
downloadslx-admin-bdd5860435e7bfd7f9b656917e73d558aa8d7ffa.tar.gz
slx-admin-bdd5860435e7bfd7f9b656917e73d558aa8d7ffa.tar.xz
slx-admin-bdd5860435e7bfd7f9b656917e73d558aa8d7ffa.zip
[dozmod/api] Formatting
Diffstat (limited to 'modules-available/dozmod/api.inc.php')
-rw-r--r--modules-available/dozmod/api.inc.php158
1 files changed, 85 insertions, 73 deletions
diff --git a/modules-available/dozmod/api.inc.php b/modules-available/dozmod/api.inc.php
index bc2410aa..a3e30dfa 100644
--- a/modules-available/dozmod/api.inc.php
+++ b/modules-available/dozmod/api.inc.php
@@ -1,4 +1,4 @@
-<?php
+<?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
* gets cached into a file cache.
@@ -18,49 +18,56 @@ $availableRessources = ['vmx', 'test', 'netrules'];
/* BEGIN: A simple caching mechanism ---------------------------- */
-function cache_hash($obj) {
- return md5(serialize($obj));
+function cache_hash($obj)
+{
+ return md5(serialize($obj));
}
-function cache_key_to_filename($key) {
- return "/tmp/bwlp-slxadmin-cache-$key"; // TODO: hash
+function cache_key_to_filename($key)
+{
+ return "/tmp/bwlp-slxadmin-cache-$key"; // TODO: hash
}
-function cache_put($key, $value) {
- $filename = cache_key_to_filename($key);
- file_put_contents($filename, $value);
+function cache_put($key, $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);
-
- if (!$mtime) {
- return false; // cache miss
- }
- if ( time() - $mtime > CONFIG_DOZMOD_EXPIRE) {
- return false;
- } else {
- return true;
- }
+function cache_has($key)
+{
+ $filename = cache_key_to_filename($key);
+ $mtime = filemtime($filename);
+
+ if (!$mtime) {
+ 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);
+function cache_get($key)
+{
+ $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);
- } else {
- Util::traceError("cannot open file");
- }
+function cache_get_passthru($key)
+{
+ $filename = cache_key_to_filename($key);
+ $fp = fopen($filename, "r");
+ if ($fp) {
+ fpassthru($fp);
+ } else {
+ Util::traceError("cannot open file");
+ }
}
+
/* END: Cache ---------------------------------------------------- */
@@ -76,46 +83,51 @@ function println($str) { echo "$str\n"; }
/* return an array of lecutre uuids.
* Parameter: an array with location Ids
* */
-function _getLecturesForLocations($locationIds) {
- $ids = implode('%20', $locationIds);
- $url = LIST_URL . "?locations=$ids";
- $responseXML = Download::asString($url, 60, $code);
- $xml = new SimpleXMLElement($responseXML);
-
- $uuids = [];
- foreach ($xml->eintrag as $e) {
- $uuids[] = strval($e->uuid['param'][0]);
- }
- return $uuids;
-}
+function _getLecturesForLocations($locationIds)
+{
+ $ids = implode('%20', $locationIds);
+ $url = LIST_URL . "?locations=$ids";
+ $responseXML = Download::asString($url, 60, $code);
+ $xml = new SimpleXMLElement($responseXML);
+
+ $uuids = [];
+ foreach ($xml->eintrag as $e) {
+ $uuids[] = strval($e->uuid['param'][0]);
+ }
+ return $uuids;
+}
+
/** Caching wrapper around _getLecturesForLocations() */
-function getLecturesForLocations($locationIds) {
- $key = 'lectures_' . cache_hash($locationIds);
- if (cache_has($key)) {
- return unserialize(cache_get($key));
- } else {
- $value = _getLecturesForLocations($locationIds);
- cache_put($key, serialize($value));
- return $value;
- }
+function getLecturesForLocations($locationIds)
+{
+ $key = 'lectures_' . cache_hash($locationIds);
+ if (cache_has($key)) {
+ return unserialize(cache_get($key));
+ } else {
+ $value = _getLecturesForLocations($locationIds);
+ cache_put($key, serialize($value));
+ return $value;
+ }
}
-function _getVMX($lecture_uuid) {
- $url = VMX_URL . '/' . $lecture_uuid;
- $response = Download::asString($url, 60, $code);
- return $response;
+function _getVMX($lecture_uuid)
+{
+ $url = VMX_URL . '/' . $lecture_uuid;
+ $response = Download::asString($url, 60, $code);
+ return $response;
}
/** Caching wrapper around _getVMX() **/
-function getVMX($lecture_uuid) {
- $key = 'vmx_' . $lecture_uuid;
- if (cache_has($key)) {
- cache_get_passthru($key);
- } else {
- $value = _getVMX($lecture_uuid);
- cache_put($key, $value);
- return $value;
- }
+function getVMX($lecture_uuid)
+{
+ $key = 'vmx_' . $lecture_uuid;
+ if (cache_has($key)) {
+ cache_get_passthru($key);
+ } else {
+ $value = _getVMX($lecture_uuid);
+ cache_put($key, $value);
+ return $value;
+ }
}
@@ -126,9 +138,9 @@ if (substr($ip, 0, 7) === '::ffff:') {
}
/* request data, don't trust */
-$request = [ 'resource' => filter_var(strtolower(trim($_REQUEST['resource'])), FILTER_SANITIZE_STRING),
- 'lecture' => filter_var(strtolower(trim($_REQUEST['lecture'])), FILTER_SANITIZE_STRING),
- 'ip' => $ip ];
+$request = ['resource' => filter_var(strtolower(trim($_REQUEST['resource'])), FILTER_SANITIZE_STRING),
+ 'lecture' => filter_var(strtolower(trim($_REQUEST['lecture'])), FILTER_SANITIZE_STRING),
+ 'ip' => $ip];
/* lookup location id(s) */
@@ -140,18 +152,18 @@ $lectures = getLecturesForLocations(array($location_ids));
/* validate request -------------------------------------------- */
/* check resources */
if (!in_array($request['resource'], $availableRessources)) {
- Util::traceError("unknown resource: {$request['resource']}");
+ Util::traceError("unknown resource: {$request['resource']}");
}
/* check that the user requests a lecture that he is allowed to have */
if (!in_array($request['lecture'], $lectures)) {
- Util::traceError("client is not allowed to access this lecture: ${request['lecture']}");
+ Util::traceError("client is not allowed to access this lecture: ${request['lecture']}");
}
if ($request['resource'] === 'vmx') {
- echo getVMX($request['lecture']);
+ echo getVMX($request['lecture']);
} else if ($request['resource'] === 'test') {
- echo "Here's your special test data!";
+ echo "Here's your special test data!";
} else {
- echo "I don't know how to give you that resource";
+ echo "I don't know how to give you that resource";
}