Skip to content

Instantly share code, notes, and snippets.

@BSN4
Last active July 21, 2017 11:43
Show Gist options
  • Save BSN4/d11feca66fe96d1262bc195209ded8e0 to your computer and use it in GitHub Desktop.
Save BSN4/d11feca66fe96d1262bc195209ded8e0 to your computer and use it in GitHub Desktop.
fixing CROS bug
<?php
abstract class ValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return bool
*/
abstract public function serves($sitePath, $siteName, $uri);
/**
* Determine if the incoming request is for a static file.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string|false
*/
abstract public function isStaticFile($sitePath, $siteName, $uri);
/**
* Get the fully resolved path to the application's front controller.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string
*/
abstract public function frontControllerPath($sitePath, $siteName, $uri);
/**
* Find a driver that can serve the incoming request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return ValetDriver|null
*/
public static function assign($sitePath, $siteName, $uri)
{
$drivers = [];
if ($customSiteDriver = static::customSiteDriver($sitePath)) {
$drivers[] = $customSiteDriver;
}
$drivers = array_merge($drivers, static::driversIn(VALET_HOME_PATH.'/Drivers'));
$drivers[] = 'LaravelValetDriver';
$drivers[] = 'WordPressValetDriver';
$drivers[] = 'BedrockValetDriver';
$drivers[] = 'SymfonyValetDriver';
$drivers[] = 'CraftValetDriver';
$drivers[] = 'StatamicValetDriver';
$drivers[] = 'StatamicV1ValetDriver';
$drivers[] = 'CakeValetDriver';
$drivers[] = 'SculpinValetDriver';
$drivers[] = 'JigsawValetDriver';
$drivers[] = 'KirbyValetDriver';
$drivers[] = 'ContaoValetDriver';
$drivers[] = 'KatanaValetDriver';
$drivers[] = 'JoomlaValetDriver';
$drivers[] = 'DrupalValetDriver';
$drivers[] = 'Concrete5ValetDriver';
$drivers[] = 'BasicValetDriver';
foreach ($drivers as $driver) {
$driver = new $driver;
if ($driver->serves($sitePath, $siteName, $driver->mutateUri($uri))) {
return $driver;
}
}
}
/**
* Get the custom driver class from the site path, if one exists.
*
* @param string $sitePath
* @return string
*/
public static function customSiteDriver($sitePath)
{
if (! file_exists($sitePath.'/LocalValetDriver.php')) {
return;
}
require_once $sitePath.'/LocalValetDriver.php';
return 'LocalValetDriver';
}
/**
* Get all of the driver classes in a given path.
*
* @param string $path
* @return array
*/
public static function driversIn($path)
{
if (! is_dir($path)) {
return [];
}
$drivers = [];
foreach (scandir($path) as $file) {
if ($file !== 'ValetDriver.php' && strpos($file, 'ValetDriver') !== false) {
require_once $path.'/'.$file;
$drivers[] = basename($file, '.php');
}
}
return $drivers;
}
/**
* Mutate the incoming URI.
*
* @param string $uri
* @return string
*/
public function mutateUri($uri)
{
return $uri;
}
public function detectFileMimeType($filename='')
{
$mime_types = array(
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
@$ext = strtolower(array_pop(explode('.',$filename)));
if (array_key_exists($ext, $mime_types)) {
return $mime_types[$ext];
}
elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
else {
return 'application/octet-stream';
}
}
/**
* Serve the static file at the given path.
*
* @param string $staticFilePath
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return void
*/
public function serveStaticFile($staticFilePath, $sitePath, $siteName, $uri)
{
/**
* Back story...
*
* PHP docs *claim* you can set default_mimetype = "" to disable the default
* Content-Type header. This works in PHP 7+, but in PHP 5.* it sends an
* *empty* Content-Type header, which is significantly different than
* sending *no* Content-Type header.
*
* However, if you explicitly set a Content-Type header, then explicitly
* remove that Content-Type header, PHP seems to not re-add the default.
*
* I have a hard time believing this is by design and not coincidence.
*
* Burn. it. all.
*/
header('Access-Control-Allow-Origin: *');
header('Content-type: ' . $this->detectFileMimeType($staticFilePath));
echo readfile($staticFilePath);
//header('Content-Type: text/html');
//header_remove('Content-Type');
//header('X-Accel-Redirect: /' . VALET_STATIC_PREFIX . $staticFilePath);
}
/**
* Determine if the path is a file and not a directory.
*
* @param string $path
* @return bool
*/
protected function isActualFile($path)
{
return ! is_dir($path) && file_exists($path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment