summaryrefslogtreecommitdiffstats
path: root/modules-available/dozmod/api.inc.php
blob: 6ff52c14dd9c1825718199a2810556cd8de72bde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?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.
*
* Required Configuration:
* CONFIG_DOZMOD_EXPIRE: Expiration time in seconds for the cache
* CONFIG_DOZMOD: URL to the dozmod server
*
**/

require 'modules/locations/inc/location.inc.php';


define('LIST_URL', CONFIG_DOZMOD . '/vmchooser/list');
define('VMX_URL', CONFIG_DOZMOD . '/vmchooser/lecture');
$availableRessources = ['vmx', 'test', 'netrules'];

/* BEGIN: A simple caching mechanism ---------------------------- */

function cache_hash($obj)
{
	return md5(serialize($obj));
}

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_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);
}

/* 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");
	}
}

/* END: Cache ---------------------------------------------------- */


/* this script requires 2 (3 with implicit client ip) parameters
*
* resource     = vmx,...
* lecture_uuid = client can choose
**/


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;
}

/** 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 _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;
	}
}


// -----------------------------------------------------------------------------//
$ip = $_SERVER['REMOTE_ADDR'];
if (substr($ip, 0, 7) === '::ffff:') {
	$ip = substr($ip, 7);
}

/* request data, don't trust */
$resource   = Request::get('resource', false, 'string');
$lecture    = Request::get('lecture', false, 'string');

if ($resource === false) {
	Util::traceError("you have to specify the 'resource' parameter");
}
if ($lecture === false) {
	Util::traceError("you have to specify the 'lecture' parameter");
}

/* lookup location id(s) */
$location_ids = Location::getFromIp($ip);
$location_ids = Location::getLocationRootChain($location_ids);

/* lookup lecture uuids */
$lectures = getLecturesForLocations($location_ids);

/* validate request -------------------------------------------- */
/* check resources */
if (!in_array($resource, $availableRessources)) {
	Util::traceError("unknown resource: $resource");
}

/* check that the user requests a lecture that he is allowed to have */
if (!in_array($lecture, $lectures)) {
	Util::traceError("client is not allowed to access this lecture: $lecture");
}

if ($resource === 'vmx') {
	echo getVMX($lecture);
} else if ($resource === 'test') {
	echo "Here's your special test data!";
} else {
	echo "I don't know how to give you that resource";
}