diff options
author | DM42.Net (Matt Dent) <dentm42@dm42.net> | 2019-07-14 23:56:11 -0400 |
---|---|---|
committer | DM42.Net (Matt Dent) <dentm42@dm42.net> | 2019-07-14 23:56:11 -0400 |
commit | 68733a2bc013e9773f17dfc969c3ebc2055fad87 (patch) | |
tree | 44ba8643227e342e63170f9d7cf47e071ac97638 /boot.php | |
parent | b2d3a11de8ed9a7eb86159520705dd04bb821dcc (diff) | |
download | volse-hubzilla-68733a2bc013e9773f17dfc969c3ebc2055fad87.tar.gz volse-hubzilla-68733a2bc013e9773f17dfc969c3ebc2055fad87.tar.bz2 volse-hubzilla-68733a2bc013e9773f17dfc969c3ebc2055fad87.zip |
Serve static files directly if not caught by web server
Diffstat (limited to 'boot.php')
-rwxr-xr-x | boot.php | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -896,6 +896,51 @@ class App { if(x($_GET,'q')) self::$cmd = escape_tags(trim($_GET['q'],'/\\')); + // Serve raw files from the file system in certain cases. + $filext = pathinfo(self::$cmd, PATHINFO_EXTENSION); + + $serve_rawfiles=[ + 'jpg'=>'image/jpeg', + 'jpeg'=>'image/jpeg', + 'gif'=>'image/gif', + 'png'=>'image/png', + 'ico'=>'image/vnd.microsoft.icon', + 'css'=>'text/css', + 'js'=>'text/javascript', + 'htm'=>'text/html', + 'html'=>'text/html', + 'map'=>'application/octet-stream', + 'ttf'=>'font/ttf', + 'woff'=>'font/woff', + 'woff2'=>'font/woff2', + 'svg'=>'image/svg+xml']; + + if (array_key_exists($filext, $serve_rawfiles) && file_exists(self::$cmd)) { + $staticfilecwd = getcwd(); + $staticfilerealpath = realpath(self::$cmd); + if(strpos($staticfilerealpath,$staticfilecwd) !== 0) { + header("HTTP/1.1 404 Not Found", true, 404); + killme(); + } + + $staticfileetag = '"'.md5($staticfilerealpath.filemtime(self::$cmd)).'"'; + header("ETag: ".$staticfileetag); + header("Cache-control: max-age=2592000"); + if(isset($_SERVER['HTTP_IF_NONE_MATCH'])) { + // If HTTP_IF_NONE_MATCH is same as the generated ETag => content is the same as browser cache + // So send a 304 Not Modified response header and exit + if($_SERVER['HTTP_IF_NONE_MATCH'] == $staticfileetag) { + header('HTTP/1.1 304 Not Modified', true, 304); + killme(); + } + } + header("Content-type: ".$serve_rawfiles[$filext]); + $handle = fopen(self::$cmd, "rb"); + fpassthru($handle); + fclose($handle); + killme(); + } + // unix style "homedir" if((substr(self::$cmd, 0, 1) === '~') || (substr(self::$cmd, 0, 1) === '@')) |