Archived
1
0
This repository has been archived on 2026-01-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
arema-jobs/aremaproxy.php
T
2022-10-18 16:32:20 +02:00

77 lines
1.8 KiB
PHP
Executable File

<?php
// Proxy for requests to Arema because of CORS limitations
const AREMA_URL = 'http://arema01as.tv.asinfra.net:9090';
const AREMA_USERNAME = 'dmae';
const AREMA_PASSWORD = 'admira';
const CACHE_DIR = 'cache';
const CACHE_SECONDS = 2;
$ctx = stream_context_create([
'http' => [
'method' => 'GET',
'header' => ['Accept: application/json'],
],
]);
// default parameters for all requests
$params = [
'userid' => AREMA_USERNAME,
'password' => AREMA_PASSWORD,
'encrypted' => 'false',
];
// request-specific handling
switch ($_GET['fetch']) {
default:
case 'joblist':
$url = AREMA_URL . '/rest/jobs';
$params['limit'] = 50;
$params['order_by'] = 'job_id_desc';
$params['history'] = 'false';
break;
case 'jobdetails':
$url = AREMA_URL . '/rest/jobs/' . intval($_GET['jobid']);
break;
case 'jobprogress':
$url = AREMA_URL . '/rest/jobs/' . intval($_GET['jobid']) . '/progress';
break;
}
$params_encoded = [];
foreach ($params as $k => $v) {
$params_encoded[] = $k . '=' . rawurlencode($v);
}
$params_str = implode('&', $params_encoded);
$url .= '?' . $params_str;
$cache_token = sha1($url);
// Clean cache
$cached_files = glob(CACHE_DIR . '/*');
foreach ($cached_files as $f) {
$mtime = filemtime($f);
if ((time() - $mtime) > CACHE_SECONDS) {
// cached file outdated --> delete
unlink($f);
}
}
$cache_file = CACHE_DIR . '/' . $cache_token;
if (file_exists($cache_file)) {
// Get cached data
$data = file_get_contents($cache_file);
} else {
// Query Arema
$data = file_get_contents($url, false, $ctx);
// Cache answer
file_put_contents($cache_file, $data);
}
// Output JSON
header('Content-Type: application/json');
echo $data;