Created
May 25, 2016 22:05
-
-
Save nfreear/742cf6839c871467df0f020b349ef15e to your computer and use it in GitHub Desktop.
A simple library to serve static files & documents via PHP (APPLAuD)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Nfreear\HttpFileDispatcher; | |
//namespace IET_OU\Frontend\Classes; | |
/** | |
* A simple library to serve static files & documents via PHP. | |
* | |
* @copyright © Nick Freear, 25 May 2016. | |
* | |
* @link http://localhost/applaud/file?f=APPLAuD%20Principles.pdf Example 1 | |
* @link http://localhost/applaud/file?f=Fig1.jpg Example 2 | |
*/ | |
use Response; | |
class FileDispatcher | |
{ | |
const URI_REGEX = '@\/file\?f=(?P<file>\w[\w\%]+\w\.(?P<ext>\w{2,}))$@'; | |
const ALLOW_EXT_REGEX = '@^(pdf|docx?|png|jpg)$@i'; | |
const DISP_INLINE_REGEX = '@^(pdf|png|jpg)$@i'; | |
const FILE_PATH = '%s/../../../../path/to/files/'; | |
public static function run() | |
{ | |
$request_uri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL); | |
//$file_param = filter_input(INPUT_GET, 'file', FILTER_SANITIZE_URL); | |
if (preg_match(self::URI_REGEX, $request_uri, $matches)) { | |
$file = urldecode($matches[ 'file' ]); | |
$file_ext = $matches[ 'ext' ]; | |
$file_path = sprintf(self::FILE_PATH, __DIR__) . $file; | |
if (! preg_match(self::ALLOW_EXT_REGEX, $file_ext)) { | |
FileDispatcher::debug([ 400, 'Bad file extension', $file, $ext ]); | |
return Response::make('Bad Request', 403); | |
} | |
if (! file_exists($file_path)) { | |
//throw new Exception('404 Not Found: ' . $file); | |
FileDispatcher::debug([ 404, 'File not found', $file ]); | |
// https://octobercms.com/forum/post/returning-404-from-a-component | |
return Response::make('File not found', 404); | |
} | |
self::dispatch($file, $file_path, $file_ext); | |
} | |
} | |
protected static function dispatch($filename, $file_path, $file_ext) | |
{ | |
$info = (object) [ | |
'filename' => $filename, | |
'filesize' => filesize($file_path), | |
'mimetype' => self::mimetype($file_path), | |
'disposition' => preg_match(self::DISP_INLINE_REGEX, $file_ext) ? 'inline' : 'attachment', | |
]; | |
$contents = file_get_contents($file_path); | |
FileDispatcher::debug([ 'OK', $info ]); | |
header('Content-Type: ' . $info->mimetype); | |
header('Content-Disposition: ' .sprintf('%s; filename=%s', $info->disposition, $filename)); | |
echo $contents; | |
exit; | |
} | |
/** | |
* @link http://stackoverflow.com/questions/23287341/how-to-get-mime-type-of-a-file-in-php-5-5 | |
*/ | |
protected static function mimetype($file_path) | |
{ | |
$finfo = finfo_open(FILEINFO_MIME_TYPE); | |
$mimetype = finfo_file($finfo, $file_path); | |
finfo_close($finfo); | |
return $mimetype; | |
} | |
public static function debug($obj) | |
{ | |
static $count = 0; | |
header(sprintf('X-Frontend-%02d: %s', $count, json_encode($obj))); | |
$count++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment