├── .htaccess ├── README.md ├── do_not_upload └── misc.code.txt └── index.php /.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine on 2 | RewriteBase / 3 | RewriteCond %{REQUEST_URI} !^/index.php 4 | RewriteRule .* index.php [L] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | php-cdn 2 | ======= 3 | 4 | 5 | 6 | `dynamic` `file` `caching` `pseudo` `cdn` 7 | 8 | 9 | * cdn root path : http://cdn.com/ 10 | * cdn example url : http://cdn.com/path/to/resource.css?d=12345 11 | * maps the uri : /path/to/resource.css?d=12345 12 | * to the origin : http://yoursite.com/path/to/resource.css?d=12345 13 | * caches file to : ./cache/[base64-encoded-uri].css 14 | * returns local cached copy or issues 304 not modified -------------------------------------------------------------------------------- /do_not_upload/misc.code.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * some client code for using php socket/streams instead of curl 3 | * - note: it has *not* been tested in any way whatsoever, i had 4 | * - planned on using it eventually, but didn't get around to it 5 | * 6 | // issue an http *HEAD* request 7 | // to verify that the image exists 8 | stream_context_set_default(array('http' => array('method' => 'HEAD'))); 9 | 10 | // we have located the remote file 11 | if (($headers = @get_headers($f_origin . $_SERVER['REQUEST_URI'])) !== false && 12 | strpos($headers[0], '200') !== false 13 | ) { 14 | $fp = fopen($f_path, 'a+b'); 15 | if(flock($fp, LOCK_EX | LOCK_NB)) { 16 | // empty *possible* contents 17 | ftruncate($fp, 0); 18 | rewind($fp); 19 | 20 | // issue an http *GET* request 21 | stream_context_set_default(array('http' => array('method' => 'GET'))); 22 | 23 | // make sure that we retrieved the 24 | // intended file instead of an error message 25 | if (($stream = @fopen($f_origin . $_SERVER['REQUEST_URI'], 'rb')) !== false && 26 | ($data = @stream_get_contents($stream)) !== false 27 | ) { 28 | // write to file 29 | fwrite($fp, $data); 30 | } 31 | 32 | // 1) flush the output to the file 33 | // 2) and release the secondary lock 34 | // 3) close the opened php socket/stream 35 | fflush($fp); 36 | flock($fp, LOCK_UN); 37 | @fclose($stream); 38 | } 39 | 40 | // close the file 41 | fclose($fp); 42 | 43 | // issue *302* for *this* request 44 | header('Location: ' . $f_origin . $_SERVER['REQUEST_URI']); 45 | } else { 46 | // the file doesn't exist, issue *404* 47 | header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found'); 48 | header('Cache-Control: private'); 49 | } 50 | */ -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | $f_origin . $_SERVER['REQUEST_URI'], 78 | CURLOPT_TIMEOUT => 15, 79 | CURLOPT_CONNECTTIMEOUT => 15, 80 | CURLOPT_FAILONERROR => 1, 81 | CURLOPT_RETURNTRANSFER => 1, 82 | CURLOPT_BINARYTRANSFER => 1, 83 | CURLOPT_HEADER => 0, 84 | CURLOPT_NOBODY => 1, 85 | // CURLOPT_FOLLOWLOCATION => 1, 86 | )); 87 | 88 | // we have located the remote file 89 | if (curl_exec($ch) !== false) { 90 | $fp = fopen($f_path, 'a+b'); 91 | if(flock($fp, LOCK_EX | LOCK_NB)) { 92 | // empty *possible* contents 93 | ftruncate($fp, 0); 94 | rewind($fp); 95 | 96 | // http *GET* request 97 | // and write directly to the file 98 | $ch2 = curl_init(); 99 | curl_setopt_array($ch2, array( 100 | CURLOPT_URL => $f_origin . $_SERVER['REQUEST_URI'], 101 | CURLOPT_TIMEOUT => 15, 102 | CURLOPT_CONNECTTIMEOUT => 15, 103 | CURLOPT_FAILONERROR => 1, 104 | CURLOPT_RETURNTRANSFER => 1, 105 | CURLOPT_BINARYTRANSFER => 1, 106 | CURLOPT_HEADER => 0, 107 | CURLOPT_FILE => $fp 108 | // CURLOPT_FOLLOWLOCATION => 1, 109 | )); 110 | 111 | // did the transfer complete? 112 | if (curl_exec($ch2) === false) { 113 | // something went wrong, null 114 | // the file just in case >.> 115 | ftruncate($fp, 0); 116 | } 117 | 118 | // 1) flush output to the file 119 | // 2) release the file lock 120 | // 3) release the curl socket 121 | fflush($fp); 122 | flock($fp, LOCK_UN); 123 | curl_close($ch2); 124 | } 125 | 126 | // close the file 127 | fclose($fp); 128 | 129 | // issue *302* for *this* request 130 | header('Location: ' . $f_origin . $_SERVER['REQUEST_URI']); 131 | } else { 132 | // the file doesn't exist, issue *404* 133 | header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found'); 134 | header('Cache-Control: private'); 135 | } 136 | 137 | // finished 138 | curl_close($ch); 139 | } 140 | 141 | ?> --------------------------------------------------------------------------------