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"); fpassthru($fp); } /* END: Cache ---------------------------------------------------- */ /* this script requires 2 (3 with implicit client ip) parameters * * ressource = 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 */ $request = [ 'ressource' => filter_var(strtolower(trim($_REQUEST['ressource'])), FILTER_SANITIZE_STRING), 'lecture' => filter_var(strtolower(trim($_REQUEST['lecture'])), FILTER_SANITIZE_STRING), 'ip' => $ip ]; /* lookup location id(s) */ $location_ids = Location::getFromIP($request['ip']); /* lookup lecture uuids */ $lectures = getLecturesForLocations(array($location_ids)); /* validate request -------------------------------------------- */ /* check ressources */ if (!in_array($request['ressource'], $availableRessources)) { Util::traceError("unknown ressource: {$request['ressource']}"); } /* 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']}"); } if ($request['ressource'] === 'vmx') { echo getVMX($request['lecture']); } else if ($request['ressource'] === 'test') { echo "Here's your special test data!"; } else { echo "I don't know how to give you that ressource"; }