142 lines
3.7 KiB
PHP
142 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
use Illuminate\Foundation\Http\MaintenanceModeBypassCookie;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
class PreventRequestsDuringMaintenance
|
|
{
|
|
/**
|
|
* The application implementation.
|
|
*
|
|
* @var \Illuminate\Contracts\Foundation\Application
|
|
*/
|
|
protected $app;
|
|
|
|
/**
|
|
* The URIs that should be accessible while maintenance mode is enabled.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $except = ['maintenance', 'maintenance/bypass'];
|
|
|
|
/**
|
|
* Create a new middleware instance.
|
|
*
|
|
* @param \Illuminate\Contracts\Foundation\Application $app
|
|
* @return void
|
|
*/
|
|
public function __construct(Application $app)
|
|
{
|
|
$this->app = $app;
|
|
}
|
|
|
|
/**
|
|
* Handle an incoming request.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \Closure $next
|
|
* @return mixed
|
|
*
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
|
*/
|
|
public function handle($request, Closure $next)
|
|
{
|
|
if ($this->app->isDownForMaintenance()) {
|
|
$data = json_decode(file_get_contents($this->app->storagePath().'/framework/down'), true);
|
|
|
|
if ($this->hasValidBypassCookie($request, $data) ||
|
|
$this->inExceptArray($request)) {
|
|
return $next($request);
|
|
}
|
|
|
|
return response('{"errors":[{"code":503,"message":"ServiceUnavailable"}]}', 503)
|
|
->header('Cache-Control', 'private')
|
|
->header('Content-Type', 'application/json; charset=utf-8');
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
/**
|
|
* Determine if the incoming request has a maintenance mode bypass cookie.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param array $data
|
|
* @return bool
|
|
*/
|
|
protected function hasValidBypassCookie($request, array $data)
|
|
{
|
|
return isset($data['secret']) &&
|
|
$request->cookie('gt_constraint') &&
|
|
MaintenanceModeBypassCookie::isValid(
|
|
$request->cookie('gt_constraint'),
|
|
$data['secret']
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Determine if the request has a URI that should be accessible in maintenance mode.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return bool
|
|
*/
|
|
protected function inExceptArray($request)
|
|
{
|
|
foreach ($this->except as $except) {
|
|
if ($except !== '/') {
|
|
$except = trim($except, '/');
|
|
}
|
|
|
|
if ($request->fullUrlIs($except) || $request->is($except)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Redirect the user back to the root of the application with a maintenance mode bypass cookie.
|
|
*
|
|
* @param string $secret
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
protected function bypassResponse(string $secret)
|
|
{
|
|
return redirect('/')->withCookie(
|
|
MaintenanceModeBypassCookie::create($secret)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get the headers that should be sent with the response.
|
|
*
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
protected function getHeaders($data)
|
|
{
|
|
$headers = isset($data['retry']) ? ['Retry-After' => $data['retry']] : [];
|
|
|
|
if (isset($data['refresh'])) {
|
|
$headers['Refresh'] = $data['refresh'];
|
|
}
|
|
|
|
return $headers;
|
|
}
|
|
|
|
/**
|
|
* Get the URIs that should be accessible even when maintenance mode is enabled.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getExcludedPaths()
|
|
{
|
|
return $this->except;
|
|
}
|
|
}
|