Skip to content

Instantly share code, notes, and snippets.

@SignpostMarv
Created August 12, 2013 16:05
Show Gist options
  • Select an option

  • Save SignpostMarv/6212316 to your computer and use it in GitHub Desktop.

Select an option

Save SignpostMarv/6212316 to your computer and use it in GitHub Desktop.
Serve OAR files with prettier screen presence than a blank browser screen!
<?php
/**
Copyright (c) 2013 SignpostMarv
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# .htaccess rules:
RewriteEngine On
RewriteBase /oars/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.oar -f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule . pretty-oar-download.php
AddType application/octet-stream .oar
*/
define('OAR', basename($_SERVER['REQUEST_URI']) . '.oar');
define('HTML', basename($_SERVER['REQUEST_URI']) . '.html');
define('TESTGZIP', true);
if(!file_exists(OAR) || !file_exists(HTML)){
header('HTTP/1.1 404 Not Found');
}else{
define('ETAG', sha1_file(HTML));
header('Content-Type: text/html');
header('Refresh:0;url=' . OAR);
header('Last-Modified: ' . filemtime(HTML));
header('ETag: ' . ETAG);
if(isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == ETAG){
header('HTTP/1.1 304 Not Modified');
exit;
}
if(TESTGZIP && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false){
header('Content-Encoding: gzip');
$buff = gzencode(file_get_contents(HTML));
header('Content-Length: ' . strlen($buff));
die($buff);
}else{
header('Content-Length: ' . filesize(HTML));
readfile(HTML);exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment