diff options
| author | Horus3 | 2014-10-28 23:47:28 +0100 |
|---|---|---|
| committer | Horus3 | 2014-10-28 23:47:28 +0100 |
| commit | 44870defdcddbfceede437950fd9e3342f84ebf0 (patch) | |
| tree | f4de13cd94be784ef02975df95d5f0edb50aac90 /cdn | |
| parent | 25610c0ccb4c7c99fe0d6d82d6738dbcc40d05e3 (diff) | |
| download | jungegemeinde-44870defdcddbfceede437950fd9e3342f84ebf0.tar.gz | |
Added experimental CDN support.
Diffstat (limited to 'cdn')
| -rw-r--r-- | cdn/originpull.php | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/cdn/originpull.php b/cdn/originpull.php new file mode 100644 index 0000000..00f9a03 --- /dev/null +++ b/cdn/originpull.php @@ -0,0 +1,82 @@ +<?php +exit; // REMOVE THIS LINE IN PRODUCTION ENVIRONMENT!!! + +if ( ! isset($_GET['url']) || $_GET['url'] == "" ){ + header($_SERVER['SERVER_PROTOCOL'] . " 404 Not Found"); + header('X-Cache: Miss'); + exit; +} +header('X-Cache: Hit'); + +define('ABSP', dirname(__FILE__) . '/'); + +# directory on origin website +define('URI', '/'); + +# origin website +define('ORIGIN', 'https://jungegemeinde.iamfabulous.de' . URI); + +# local directory +define('LOCAL', '/'); + +$basepath = array('js', 'static', 'img', 'static/img'); +$path_parts = pathinfo($_GET['url']); + +function getMime($file){ + $finfo = new finfo(FILEINFO_MIME_TYPE); + $mime = $finfo->file($file); + + # hack to get correct mime type for .css and .js + $ext = pathinfo($file,PATHINFO_EXTENSION); + switch($ext){ + case 'css': + $mime = "text/css"; + break; + case'js': + $mime = "text/javascript"; + break; + default: + break; + } + + return $mime; +} + +foreach($basepath as $dir){ + + if ( URI . $dir . "/" == $path_parts['dirname'] . '/' ){ + + if ( !is_dir(ABSP . $dir) ){ + mkdir(ABSP . $dir, 0744, true); + } + + $request = str_replace(URI . $dir . "/", "",$_GET['url'] ); + $scan = array_diff( scandir($dir), array('.', '..') ); + + if ( in_array($request, $scan) ){ + header("Content-type: " . getMime($dir . '/' . $request)); + header("X-Accel-Redirect: " . LOCAL . $dir . '/' . $request); + exit; + } else { + $ch = curl_init(ORIGIN . $dir . "/" . $request); + curl_setopt($ch, CURLOPT_NOBODY, true); + curl_setopt($ch, CURLOPT_USERAGENT, 'Origin Pull/v0.1'); + curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ( $httpcode == 200 || $httpcode == 302 ) { + file_put_contents($dir . '/' . $request, file_get_contents(ORIGIN . $dir . "/" . $request) ); + header("Content-type: " . getMime($dir . '/' . $request)); + header("X-Accel-Redirect: " . LOCAL . $dir . '/' . $request); + exit; + } else { + continue; + } + } + } + +} + +header($_SERVER['SERVER_PROTOCOL'] . " 404 Not Found"); +header('X-Cache: Miss'); |
