From 44870defdcddbfceede437950fd9e3342f84ebf0 Mon Sep 17 00:00:00 2001 From: Horus3 Date: Tue, 28 Oct 2014 23:47:28 +0100 Subject: Added experimental CDN support. --- cdn/originpull.php | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 cdn/originpull.php (limited to 'cdn') 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 @@ +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'); -- cgit v1.2.3