forked from jscad/OpenJSCAD.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.php
42 lines (33 loc) · 1.03 KB
/
remote.php
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
<?php
// error_reporting(E_ALL);
define(CACHE_PATH, 'cache');
$maxSize = 10000000; // [bytes], max size of file
$maxTime = 60; // [s], max download time
$cacheExpireSeconds = 60;
// echo "Fetching ".$_REQUEST['url'];
retrieveFile($_REQUEST['url']);
cleanUpExpired();
function retrieveFile($url) {
// only process valid requests
$valid = strncmp($url, "http://", 7) || strncmp($url, "https://", 8);
if (! $valid) return;
$contents = file_get_contents($url);
$fileName = basename($url); // FIXME a default extension needs to be added
$filePath = CACHE_PATH . "/".date("ymdHms")."-".$fileName;
file_put_contents("./".$filePath, $contents);
$data = array(
'filename' => $fileName,
'file' => $filePath,
'url' => $url
);
echo json_encode ( $data );
}
function cleanUpExpired() {
// Remove cached files that expired
$files = glob("".CACHE_PATH."/*");
foreach($files as $file)
{
if(time() - filemtime($file) > $cacheExpireSeconds)
@unlink($file);
}
}