From fd37e68139b7d9f143e37ef15014a64f82cc651a Mon Sep 17 00:00:00 2001 From: Muhamed Didovic Date: Mon, 27 May 2019 19:05:33 +0200 Subject: [PATCH] use older version of csrf token class(L5.4) --- composer.json | 1 - src/VerifyCsrfToken.php | 94 ++- src/VerifyCsrfTokenNew.php | 191 ++++++ src/session.php | 199 +++++++ tests/VerifyCsrfTokenExceptStub.php | 2 - tests/helpers.php | 887 ---------------------------- 6 files changed, 427 insertions(+), 947 deletions(-) create mode 100644 src/VerifyCsrfTokenNew.php create mode 100644 src/session.php delete mode 100755 tests/helpers.php diff --git a/composer.json b/composer.json index d12968e..ede0e92 100755 --- a/composer.json +++ b/composer.json @@ -58,7 +58,6 @@ } }, "autoload-dev": { - "psr-4": { "MuhamedDidovic\\Tests\\": "tests", "Illuminate\\Foundation\\": "tests" diff --git a/src/VerifyCsrfToken.php b/src/VerifyCsrfToken.php index 51002ef..0b648e4 100644 --- a/src/VerifyCsrfToken.php +++ b/src/VerifyCsrfToken.php @@ -3,82 +3,77 @@ namespace MuhamedDidovic\Csrf; use Closure; +use Carbon\Carbon; +use Illuminate\Contracts\Container\Container; +use Illuminate\Foundation\Application; use Symfony\Component\HttpFoundation\Cookie; use Illuminate\Contracts\Encryption\Encrypter; -use Illuminate\Contracts\Foundation\Application; -use Illuminate\Cookie\Middleware\EncryptCookies; +use Illuminate\Session\TokenMismatchException; +use Symfony\Component\HttpFoundation\Response; class VerifyCsrfToken { - use InteractsWithTime; /** * The application instance. * - * @var \Illuminate\Contracts\Foundation\Application + * @var \Illuminate\Foundation\Application */ protected $app; + /** * The encrypter implementation. * * @var \Illuminate\Contracts\Encryption\Encrypter */ protected $encrypter; + /** * The URIs that should be excluded from CSRF verification. * * @var array */ protected $except = []; - /** - * Indicates whether the XSRF-TOKEN cookie should be set on the response. - * - * @var bool - */ - protected $addHttpCookie = true; /** * Create a new middleware instance. * - * @param \Illuminate\Contracts\Foundation\Application $app - * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter + * @param \Illuminate\Foundation\Application $app + * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter * @return void */ - public function __construct(Application $app, Encrypter $encrypter) + public function __construct(Container $app, Encrypter $encrypter) { - $this->app = $app; + $this->app = $app; $this->encrypter = $encrypter; } /** * Handle an incoming request. * - * @param \Illuminate\Http\Request $request - * @param \Closure $next + * @param \Illuminate\Http\Request $request + * @param \Closure $next * @return mixed * * @throws \Illuminate\Session\TokenMismatchException */ - public function handle($request, Closure $next) + public function handle($request, Closure $next = null) { if ( $this->isReading($request) || - $this->runningUnitTests() || + //$this->runningUnitTests() || $this->inExceptArray($request) || $this->tokensMatch($request) ) { - return tap($next($request), function ($response) use ($request) { - if ($this->shouldAddXsrfTokenCookie()) { - $this->addCookieToResponse($request, $response); - } - }); + return $this->addCookieToResponse($request, $next ? $next($request) : null); } - throw new TokenMismatchException('CSRF token mismatch.'); + + throw new TokenMismatchException; } /** * Determine if the HTTP request uses a ‘read’ verb. * - * @param \Illuminate\Http\Request $request + * @param \Illuminate\Http\Request $request * @return bool */ protected function isReading($request) @@ -99,7 +94,7 @@ protected function runningUnitTests() /** * Determine if the request has a URI that should pass through CSRF verification. * - * @param \Illuminate\Http\Request $request + * @param \Illuminate\Http\Request $request * @return bool */ protected function inExceptArray($request) @@ -108,6 +103,7 @@ protected function inExceptArray($request) if ($except !== '/') { $except = trim($except, '/'); } + if ($request->fullUrlIs($except) || $request->is($except)) { return true; } @@ -119,13 +115,13 @@ protected function inExceptArray($request) /** * Determine if the session and input CSRF tokens match. * - * @param \Illuminate\Http\Request $request + * @param \Illuminate\Http\Request $request * @return bool */ protected function tokensMatch($request) { $token = $this->getTokenFromRequest($request); - + //dd(11, $token); return is_string($request->session()->token()) && is_string($token) && hash_equals($request->session()->token(), $token); @@ -134,56 +130,40 @@ protected function tokensMatch($request) /** * Get the CSRF token from the request. * - * @param \Illuminate\Http\Request $request + * @param \Illuminate\Http\Request $request * @return string */ protected function getTokenFromRequest($request) { $token = $request->input('_token') ?: $request->header('X-CSRF-TOKEN'); - if (!$token && $header = $request->header('X-XSRF-TOKEN')) { - $token = $this->encrypter->decrypt($header, static::serialized()); + + if (! $token && $header = $request->header('X-XSRF-TOKEN')) { + $token = $this->encrypter->decrypt($header); } return $token; } - /** - * Determine if the cookie should be added to the response. - * - * @return bool - */ - public function shouldAddXsrfTokenCookie() - { - return $this->addHttpCookie; - } - /** * Add the CSRF token to the response cookies. * - * @param \Illuminate\Http\Request $request - * @param \Symfony\Component\HttpFoundation\Response $response + * @param \Illuminate\Http\Request $request + * @param \Symfony\Component\HttpFoundation\Response $response * @return \Symfony\Component\HttpFoundation\Response */ - protected function addCookieToResponse($request, $response) + protected function addCookieToResponse($request, $response = null) { - $config = config('session'); + dd($request->session()); + $config = require 'session.php';//config('session'); + + $response = $response ?: new Response; $response->headers->setCookie( new Cookie( - 'XSRF-TOKEN', $request->session()->token(), $this->availableAt(60 * $config['lifetime']), - $config['path'], $config['domain'], $config['secure'], false, false, $config['same_site'] ?? null + 'XSRF-TOKEN', $request->session()->token(), Carbon::now()->getTimestamp() + 60 * $config['lifetime'], + $config['path'], $config['domain'], $config['secure'], false ) ); return $response; } - - /** - * Determine if the cookie contents should be serialized. - * - * @return bool - */ - public static function serialized() - { - return EncryptCookies::serialized('XSRF-TOKEN'); - } } diff --git a/src/VerifyCsrfTokenNew.php b/src/VerifyCsrfTokenNew.php new file mode 100644 index 0000000..aac026d --- /dev/null +++ b/src/VerifyCsrfTokenNew.php @@ -0,0 +1,191 @@ +app = $app; + $this->encrypter = $encrypter; + } + + /** + * Handle an incoming request. + * + * @param \Illuminate\Http\Request $request + * @param \Closure $next + * @return mixed + * + * @throws \Illuminate\Session\TokenMismatchException + */ + public function handle($request, Closure $next) + { + if ( + $this->isReading($request) || + $this->runningUnitTests() || + $this->inExceptArray($request) || + $this->tokensMatch($request) + ) { + return tap($next($request), function ($response) use ($request) { + if ($this->shouldAddXsrfTokenCookie()) { + $this->addCookieToResponse($request, $response); + } + }); + } + throw new TokenMismatchException('CSRF token mismatch.'); + } + + /** + * Determine if the HTTP request uses a ‘read’ verb. + * + * @param \Illuminate\Http\Request $request + * @return bool + */ + protected function isReading($request) + { + return in_array($request->method(), ['HEAD', 'GET', 'OPTIONS']); + } + + /** + * Determine if the application is running unit tests. + * + * @return bool + */ + protected function runningUnitTests() + { + return $this->app->runningInConsole() && $this->app->runningUnitTests(); + } + + /** + * Determine if the request has a URI that should pass through CSRF verification. + * + * @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; + } + + /** + * Determine if the session and input CSRF tokens match. + * + * @param \Illuminate\Http\Request $request + * @return bool + */ + protected function tokensMatch($request) + { + $token = $this->getTokenFromRequest($request); + + return is_string($request->session()->token()) && + is_string($token) && + hash_equals($request->session()->token(), $token); + } + + /** + * Get the CSRF token from the request. + * + * @param \Illuminate\Http\Request $request + * @return string + */ + protected function getTokenFromRequest($request) + { + $token = $request->input('_token') ?: $request->header('X-CSRF-TOKEN'); + if (!$token && $header = $request->header('X-XSRF-TOKEN')) { + $token = $this->encrypter->decrypt($header, static::serialized()); + } + + return $token; + } + + /** + * Determine if the cookie should be added to the response. + * + * @return bool + */ + public function shouldAddXsrfTokenCookie() + { + return $this->addHttpCookie; + } + + /** + * Add the CSRF token to the response cookies. + * + * @param \Illuminate\Http\Request $request + * @param \Symfony\Component\HttpFoundation\Response $response + * @return \Symfony\Component\HttpFoundation\Response + */ + protected function addCookieToResponse($request, $response) + { + $config = config('session'); + dd($config); + $response->headers->setCookie( + new Cookie( + 'XSRF-TOKEN', $request->session()->token(), $this->availableAt(60 * $config['lifetime']), + $config['path'], $config['domain'], $config['secure'], false, false, $config['same_site'] ?? null + ) + ); + + return $response; + } + + /** + * Determine if the cookie contents should be serialized. + * + * @return bool + */ + public static function serialized() + { + return EncryptCookies::serialized('XSRF-TOKEN'); + } +} diff --git a/src/session.php b/src/session.php new file mode 100644 index 0000000..a53d3ef --- /dev/null +++ b/src/session.php @@ -0,0 +1,199 @@ + env('SESSION_DRIVER', 'file'), + + /* + |-------------------------------------------------------------------------- + | Session Lifetime + |-------------------------------------------------------------------------- + | + | Here you may specify the number of minutes that you wish the session + | to be allowed to remain idle before it expires. If you want them + | to immediately expire on the browser closing, set that option. + | + */ + + 'lifetime' => env('SESSION_LIFETIME', 120), + + 'expire_on_close' => false, + + /* + |-------------------------------------------------------------------------- + | Session Encryption + |-------------------------------------------------------------------------- + | + | This option allows you to easily specify that all of your session data + | should be encrypted before it is stored. All encryption will be run + | automatically by Laravel and you can use the Session like normal. + | + */ + + 'encrypt' => false, + + /* + |-------------------------------------------------------------------------- + | Session File Location + |-------------------------------------------------------------------------- + | + | When using the native session driver, we need a location where session + | files may be stored. A default has been set for you but a different + | location may be specified. This is only needed for file sessions. + | + */ + //todo: resolve this + 'files' => 'temp',//storage_path('framework/sessions'), + + /* + |-------------------------------------------------------------------------- + | Session Database Connection + |-------------------------------------------------------------------------- + | + | When using the "database" or "redis" session drivers, you may specify a + | connection that should be used to manage these sessions. This should + | correspond to a connection in your database configuration options. + | + */ + + 'connection' => env('SESSION_CONNECTION', null), + + /* + |-------------------------------------------------------------------------- + | Session Database Table + |-------------------------------------------------------------------------- + | + | When using the "database" session driver, you may specify the table we + | should use to manage the sessions. Of course, a sensible default is + | provided for you; however, you are free to change this as needed. + | + */ + + 'table' => 'sessions', + + /* + |-------------------------------------------------------------------------- + | Session Cache Store + |-------------------------------------------------------------------------- + | + | When using the "apc", "memcached", or "dynamodb" session drivers you may + | list a cache store that should be used for these sessions. This value + | must match with one of the application's configured cache "stores". + | + */ + + 'store' => env('SESSION_STORE', null), + + /* + |-------------------------------------------------------------------------- + | Session Sweeping Lottery + |-------------------------------------------------------------------------- + | + | Some session drivers must manually sweep their storage location to get + | rid of old sessions from storage. Here are the chances that it will + | happen on a given request. By default, the odds are 2 out of 100. + | + */ + + 'lottery' => [2, 100], + + /* + |-------------------------------------------------------------------------- + | Session Cookie Name + |-------------------------------------------------------------------------- + | + | Here you may change the name of the cookie used to identify a session + | instance by ID. The name specified here will get used every time a + | new session cookie is created by the framework for every driver. + | + */ + + 'cookie' => env( + 'SESSION_COOKIE', + Str::slug(env('APP_NAME', 'laravel'), '_').'_session' + ), + + /* + |-------------------------------------------------------------------------- + | Session Cookie Path + |-------------------------------------------------------------------------- + | + | The session cookie path determines the path for which the cookie will + | be regarded as available. Typically, this will be the root path of + | your application but you are free to change this when necessary. + | + */ + + 'path' => '/', + + /* + |-------------------------------------------------------------------------- + | Session Cookie Domain + |-------------------------------------------------------------------------- + | + | Here you may change the domain of the cookie used to identify a session + | in your application. This will determine which domains the cookie is + | available to in your application. A sensible default has been set. + | + */ + + 'domain' => env('SESSION_DOMAIN', null), + + /* + |-------------------------------------------------------------------------- + | HTTPS Only Cookies + |-------------------------------------------------------------------------- + | + | By setting this option to true, session cookies will only be sent back + | to the server if the browser has a HTTPS connection. This will keep + | the cookie from being sent to you if it can not be done securely. + | + */ + + 'secure' => env('SESSION_SECURE_COOKIE', false), + + /* + |-------------------------------------------------------------------------- + | HTTP Access Only + |-------------------------------------------------------------------------- + | + | Setting this value to true will prevent JavaScript from accessing the + | value of the cookie and the cookie will only be accessible through + | the HTTP protocol. You are free to modify this option if needed. + | + */ + + 'http_only' => true, + + /* + |-------------------------------------------------------------------------- + | Same-Site Cookies + |-------------------------------------------------------------------------- + | + | This option determines how your cookies behave when cross-site requests + | take place, and can be used to mitigate CSRF attacks. By default, we + | do not enable this as other CSRF protection services are in place. + | + | Supported: "lax", "strict" + | + */ + + 'same_site' => null, + +]; diff --git a/tests/VerifyCsrfTokenExceptStub.php b/tests/VerifyCsrfTokenExceptStub.php index 3366883..cf58bff 100644 --- a/tests/VerifyCsrfTokenExceptStub.php +++ b/tests/VerifyCsrfTokenExceptStub.php @@ -8,14 +8,12 @@ class VerifyCsrfTokenExceptStub extends VerifyCsrfToken { public function checkInExceptArray($request) { - //dd(22, $this->except); return $this->inExceptArray($request); } public function setExcept(array $except) { $this->except = $except; - //dd($this->except); return $this; } diff --git a/tests/helpers.php b/tests/helpers.php deleted file mode 100755 index 5313ffb..0000000 --- a/tests/helpers.php +++ /dev/null @@ -1,887 +0,0 @@ -abort($code, $message, $headers); - } -} - -if (! function_exists('abort_if')) { - /** - * Throw an HttpException with the given data if the given condition is true. - * - * @param bool $boolean - * @param int $code - * @param string $message - * @param array $headers - * @return void - * - * @throws \Symfony\Component\HttpKernel\Exception\HttpException - * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException - */ - function abort_if($boolean, $code, $message = '', array $headers = []) - { - if ($boolean) { - abort($code, $message, $headers); - } - } -} - -if (! function_exists('abort_unless')) { - /** - * Throw an HttpException with the given data unless the given condition is true. - * - * @param bool $boolean - * @param int $code - * @param string $message - * @param array $headers - * @return void - * - * @throws \Symfony\Component\HttpKernel\Exception\HttpException - * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException - */ - function abort_unless($boolean, $code, $message = '', array $headers = []) - { - if (! $boolean) { - abort($code, $message, $headers); - } - } -} - -if (! function_exists('action')) { - /** - * Generate the URL to a controller action. - * - * @param string $name - * @param array $parameters - * @param bool $absolute - * @return string - */ - function action($name, $parameters = [], $absolute = true) - { - return app('url')->action($name, $parameters, $absolute); - } -} - -if (! function_exists('app')) { - /** - * Get the available container instance. - * - * @param string $abstract - * @param array $parameters - * @return mixed|\Illuminate\Foundation\Application - */ - function app($abstract = null, array $parameters = []) - { - if (is_null($abstract)) { - return Container::getInstance(); - } - - return empty($parameters) - ? Container::getInstance()->make($abstract) - : Container::getInstance()->makeWith($abstract, $parameters); - } -} - -if (! function_exists('app_path')) { - /** - * Get the path to the application folder. - * - * @param string $path - * @return string - */ - function app_path($path = '') - { - return app('path').($path ? DIRECTORY_SEPARATOR.$path : $path); - } -} - -if (! function_exists('asset')) { - /** - * Generate an asset path for the application. - * - * @param string $path - * @param bool $secure - * @return string - */ - function asset($path, $secure = null) - { - return app('url')->asset($path, $secure); - } -} - -if (! function_exists('auth')) { - /** - * Get the available auth instance. - * - * @param string|null $guard - * @return \Illuminate\Contracts\Auth\Factory|\Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard - */ - function auth($guard = null) - { - if (is_null($guard)) { - return app(AuthFactory::class); - } else { - return app(AuthFactory::class)->guard($guard); - } - } -} - -if (! function_exists('back')) { - /** - * Create a new redirect response to the previous location. - * - * @param int $status - * @param array $headers - * @param mixed $fallback - * @return \Illuminate\Http\RedirectResponse - */ - function back($status = 302, $headers = [], $fallback = false) - { - return app('redirect')->back($status, $headers, $fallback); - } -} - -if (! function_exists('base_path')) { - /** - * Get the path to the base of the install. - * - * @param string $path - * @return string - */ - function base_path($path = '') - { - return app()->basePath().($path ? DIRECTORY_SEPARATOR.$path : $path); - } -} - -if (! function_exists('bcrypt')) { - /** - * Hash the given value. - * - * @param string $value - * @param array $options - * @return string - */ - function bcrypt($value, $options = []) - { - return app('hash')->make($value, $options); - } -} - -if (! function_exists('broadcast')) { - /** - * Begin broadcasting an event. - * - * @param mixed|null $event - * @return \Illuminate\Broadcasting\PendingBroadcast|void - */ - function broadcast($event = null) - { - return app(BroadcastFactory::class)->event($event); - } -} - -if (! function_exists('cache')) { - /** - * Get / set the specified cache value. - * - * If an array is passed, we'll assume you want to put to the cache. - * - * @param dynamic key|key,default|data,expiration|null - * @return mixed - * - * @throws \Exception - */ - function cache() - { - $arguments = func_get_args(); - - if (empty($arguments)) { - return app('cache'); - } - - if (is_string($arguments[0])) { - return app('cache')->get($arguments[0], isset($arguments[1]) ? $arguments[1] : null); - } - - if (! is_array($arguments[0])) { - throw new Exception( - 'When setting a value in the cache, you must pass an array of key / value pairs.' - ); - } - - if (! isset($arguments[1])) { - throw new Exception( - 'You must specify an expiration time when setting a value in the cache.' - ); - } - - return app('cache')->put(key($arguments[0]), reset($arguments[0]), $arguments[1]); - } -} - -if (! function_exists('config')) { - /** - * Get / set the specified configuration value. - * - * If an array is passed as the key, we will assume you want to set an array of values. - * - * @param array|string $key - * @param mixed $default - * @return mixed - */ - function config($key = null, $default = null) - { - if (is_null($key)) { - return app('config'); - } - - if (is_array($key)) { - return app('config')->set($key); - } - - return app('config')->get($key, $default); - } -} - -if (! function_exists('config_path')) { - /** - * Get the configuration path. - * - * @param string $path - * @return string - */ - function config_path($path = '') - { - return app()->make('path.config').($path ? DIRECTORY_SEPARATOR.$path : $path); - } -} - -if (! function_exists('cookie')) { - /** - * Create a new cookie instance. - * - * @param string $name - * @param string $value - * @param int $minutes - * @param string $path - * @param string $domain - * @param bool $secure - * @param bool $httpOnly - * @return \Symfony\Component\HttpFoundation\Cookie - */ - function cookie($name = null, $value = null, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true) - { - $cookie = app(CookieFactory::class); - - if (is_null($name)) { - return $cookie; - } - - return $cookie->make($name, $value, $minutes, $path, $domain, $secure, $httpOnly); - } -} - -if (! function_exists('csrf_field')) { - /** - * Generate a CSRF token form field. - * - * @return \Illuminate\Support\HtmlString - */ - function csrf_field() - { - return new HtmlString(''); - } -} - -if (! function_exists('csrf_token')) { - /** - * Get the CSRF token value. - * - * @return string - * - * @throws \RuntimeException - */ - function csrf_token() - { - $session = app('session'); - - if (isset($session)) { - return $session->token(); - } - - throw new RuntimeException('Application session store not set.'); - } -} - -if (! function_exists('database_path')) { - /** - * Get the database path. - * - * @param string $path - * @return string - */ - function database_path($path = '') - { - return app()->databasePath($path); - } -} - -if (! function_exists('decrypt')) { - /** - * Decrypt the given value. - * - * @param string $value - * @return string - */ - function decrypt($value) - { - return app('encrypter')->decrypt($value); - } -} - -if (! function_exists('dispatch')) { - /** - * Dispatch a job to its appropriate handler. - * - * @param mixed $job - * @return mixed - */ - function dispatch($job) - { - return app(Dispatcher::class)->dispatch($job); - } -} - -if (! function_exists('elixir')) { - /** - * Get the path to a versioned Elixir file. - * - * @param string $file - * @param string $buildDirectory - * @return string - * - * @throws \InvalidArgumentException - */ - function elixir($file, $buildDirectory = 'build') - { - static $manifest = []; - static $manifestPath; - - if (empty($manifest) || $manifestPath !== $buildDirectory) { - $path = public_path($buildDirectory.'/rev-manifest.json'); - - if (file_exists($path)) { - $manifest = json_decode(file_get_contents($path), true); - $manifestPath = $buildDirectory; - } - } - - $file = ltrim($file, '/'); - - if (isset($manifest[$file])) { - return '/'.trim($buildDirectory.'/'.$manifest[$file], '/'); - } - - $unversioned = public_path($file); - - if (file_exists($unversioned)) { - return '/'.trim($file, '/'); - } - - throw new InvalidArgumentException("File {$file} not defined in asset manifest."); - } -} - -if (! function_exists('encrypt')) { - /** - * Encrypt the given value. - * - * @param mixed $value - * @return string - */ - function encrypt($value) - { - return app('encrypter')->encrypt($value); - } -} - -if (! function_exists('event')) { - /** - * Dispatch an event and call the listeners. - * - * @param string|object $event - * @param mixed $payload - * @param bool $halt - * @return array|null - */ - function event(...$args) - { - return app('events')->dispatch(...$args); - } -} - -if (! function_exists('factory')) { - /** - * Create a model factory builder for a given class, name, and amount. - * - * @param dynamic class|class,name|class,amount|class,name,amount - * @return \Illuminate\Database\Eloquent\FactoryBuilder - */ - function factory() - { - $factory = app(EloquentFactory::class); - - $arguments = func_get_args(); - - if (isset($arguments[1]) && is_string($arguments[1])) { - return $factory->of($arguments[0], $arguments[1])->times(isset($arguments[2]) ? $arguments[2] : null); - } elseif (isset($arguments[1])) { - return $factory->of($arguments[0])->times($arguments[1]); - } else { - return $factory->of($arguments[0]); - } - } -} - -if (! function_exists('info')) { - /** - * Write some information to the log. - * - * @param string $message - * @param array $context - * @return void - */ - function info($message, $context = []) - { - return app('log')->info($message, $context); - } -} - -if (! function_exists('logger')) { - /** - * Log a debug message to the logs. - * - * @param string $message - * @param array $context - * @return \Illuminate\Contracts\Logging\Log|null - */ - function logger($message = null, array $context = []) - { - if (is_null($message)) { - return app('log'); - } - - return app('log')->debug($message, $context); - } -} - -if (! function_exists('method_field')) { - /** - * Generate a form field to spoof the HTTP verb used by forms. - * - * @param string $method - * @return \Illuminate\Support\HtmlString - */ - function method_field($method) - { - return new HtmlString(''); - } -} - -if (! function_exists('mix')) { - /** - * Get the path to a versioned Mix file. - * - * @param string $path - * @param string $manifestDirectory - * @return \Illuminate\Support\HtmlString - * - * @throws \Exception - */ - function mix($path, $manifestDirectory = '') - { - static $manifests = []; - - if (! starts_with($path, '/')) { - $path = "/{$path}"; - } - - if ($manifestDirectory && ! starts_with($manifestDirectory, '/')) { - $manifestDirectory = "/{$manifestDirectory}"; - } - - if (file_exists(public_path($manifestDirectory.'/hot'))) { - return new HtmlString("//localhost:8080{$path}"); - } - - $manifestPath = public_path($manifestDirectory.'/mix-manifest.json'); - - if (! isset($manifests[$manifestPath])) { - if (! file_exists($manifestPath)) { - throw new Exception('The Mix manifest does not exist.'); - } - - $manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true); - } - - $manifest = $manifests[$manifestPath]; - - if (! isset($manifest[$path])) { - throw new Exception( - "Unable to locate Mix file: {$path}. Please check your ". - 'webpack.mix.js output paths and try again.' - ); - } - - return new HtmlString($manifestDirectory.$manifest[$path]); - } -} - -if (! function_exists('old')) { - /** - * Retrieve an old input item. - * - * @param string $key - * @param mixed $default - * @return mixed - */ - function old($key = null, $default = null) - { - return app('request')->old($key, $default); - } -} - -if (! function_exists('policy')) { - /** - * Get a policy instance for a given class. - * - * @param object|string $class - * @return mixed - * - * @throws \InvalidArgumentException - */ - function policy($class) - { - return app(Gate::class)->getPolicyFor($class); - } -} - -if (! function_exists('public_path')) { - /** - * Get the path to the public folder. - * - * @param string $path - * @return string - */ - function public_path($path = '') - { - return app()->make('path.public').($path ? DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR) : $path); - } -} - -if (! function_exists('redirect')) { - /** - * Get an instance of the redirector. - * - * @param string|null $to - * @param int $status - * @param array $headers - * @param bool $secure - * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse - */ - function redirect($to = null, $status = 302, $headers = [], $secure = null) - { - if (is_null($to)) { - return app('redirect'); - } - - return app('redirect')->to($to, $status, $headers, $secure); - } -} - -if (! function_exists('request')) { - /** - * Get an instance of the current request or an input item from the request. - * - * @param array|string $key - * @param mixed $default - * @return \Illuminate\Http\Request|string|array - */ - function request($key = null, $default = null) - { - if (is_null($key)) { - return app('request'); - } - - if (is_array($key)) { - return app('request')->only($key); - } - - return data_get(app('request')->all(), $key, $default); - } -} - -if (! function_exists('resolve')) { - /** - * Resolve a service from the container. - * - * @param string $name - * @return mixed - */ - function resolve($name) - { - return app($name); - } -} - -if (! function_exists('resource_path')) { - /** - * Get the path to the resources folder. - * - * @param string $path - * @return string - */ - function resource_path($path = '') - { - return app()->resourcePath($path); - } -} - -if (! function_exists('response')) { - /** - * Return a new response from the application. - * - * @param string $content - * @param int $status - * @param array $headers - * @return \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Routing\ResponseFactory - */ - function response($content = '', $status = 200, array $headers = []) - { - $factory = app(ResponseFactory::class); - - if (func_num_args() === 0) { - return $factory; - } - - return $factory->make($content, $status, $headers); - } -} - -if (! function_exists('route')) { - /** - * Generate the URL to a named route. - * - * @param string $name - * @param array $parameters - * @param bool $absolute - * @return string - */ - function route($name, $parameters = [], $absolute = true) - { - return app('url')->route($name, $parameters, $absolute); - } -} - -if (! function_exists('secure_asset')) { - /** - * Generate an asset path for the application. - * - * @param string $path - * @return string - */ - function secure_asset($path) - { - return asset($path, true); - } -} - -if (! function_exists('secure_url')) { - /** - * Generate a HTTPS url for the application. - * - * @param string $path - * @param mixed $parameters - * @return string - */ - function secure_url($path, $parameters = []) - { - return url($path, $parameters, true); - } -} - -if (! function_exists('session')) { - /** - * Get / set the specified session value. - * - * If an array is passed as the key, we will assume you want to set an array of values. - * - * @param array|string $key - * @param mixed $default - * @return mixed - */ - function session($key = null, $default = null) - { - if (is_null($key)) { - return app('session'); - } - - if (is_array($key)) { - return app('session')->put($key); - } - - return app('session')->get($key, $default); - } -} - -if (! function_exists('storage_path')) { - /** - * Get the path to the storage folder. - * - * @param string $path - * @return string - */ - function storage_path($path = '') - { - return app('path.storage').($path ? DIRECTORY_SEPARATOR.$path : $path); - } -} - -if (! function_exists('trans')) { - /** - * Translate the given message. - * - * @param string $key - * @param array $replace - * @param string $locale - * @return \Illuminate\Contracts\Translation\Translator|string|array|null - */ - function trans($key = null, $replace = [], $locale = null) - { - if (is_null($key)) { - return app('translator'); - } - - return app('translator')->trans($key, $replace, $locale); - } -} - -if (! function_exists('trans_choice')) { - /** - * Translates the given message based on a count. - * - * @param string $key - * @param int|array|\Countable $number - * @param array $replace - * @param string $locale - * @return string - */ - function trans_choice($key, $number, array $replace = [], $locale = null) - { - return app('translator')->transChoice($key, $number, $replace, $locale); - } -} - -if (! function_exists('__')) { - /** - * Translate the given message. - * - * @param string $key - * @param array $replace - * @param string $locale - * @return \Illuminate\Contracts\Translation\Translator|string - */ - function __($key = null, $replace = [], $locale = null) - { - return app('translator')->getFromJson($key, $replace, $locale); - } -} - -if (! function_exists('url')) { - /** - * Generate a url for the application. - * - * @param string $path - * @param mixed $parameters - * @param bool $secure - * @return \Illuminate\Contracts\Routing\UrlGenerator|string - */ - function url($path = null, $parameters = [], $secure = null) - { - if (is_null($path)) { - return app(UrlGenerator::class); - } - - return app(UrlGenerator::class)->to($path, $parameters, $secure); - } -} - -if (! function_exists('validator')) { - /** - * Create a new Validator instance. - * - * @param array $data - * @param array $rules - * @param array $messages - * @param array $customAttributes - * @return \Illuminate\Contracts\Validation\Validator - */ - function validator(array $data = [], array $rules = [], array $messages = [], array $customAttributes = []) - { - $factory = app(ValidationFactory::class); - - if (func_num_args() === 0) { - return $factory; - } - - return $factory->make($data, $rules, $messages, $customAttributes); - } -} - -if (! function_exists('view')) { - /** - * Get the evaluated view contents for the given view. - * - * @param string $view - * @param array $data - * @param array $mergeData - * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory - */ - function view($view = null, $data = [], $mergeData = []) - { - $factory = app(ViewFactory::class); - - if (func_num_args() === 0) { - return $factory; - } - - return $factory->make($view, $data, $mergeData); - } -}