Initial commit

This commit is contained in:
2025-08-04 16:33:07 +03:30
commit f798e8e35c
9595 changed files with 1208683 additions and 0 deletions

View File

@@ -0,0 +1,242 @@
<?php
namespace Illuminate\Cookie;
use Illuminate\Contracts\Cookie\QueueingFactory as JarContract;
use Illuminate\Support\Arr;
use Illuminate\Support\InteractsWithTime;
use Illuminate\Support\Traits\Macroable;
use Symfony\Component\HttpFoundation\Cookie;
class CookieJar implements JarContract
{
use InteractsWithTime, Macroable;
/**
* The default path (if specified).
*
* @var string
*/
protected $path = '/';
/**
* The default domain (if specified).
*
* @var string|null
*/
protected $domain;
/**
* The default secure setting (defaults to null).
*
* @var bool|null
*/
protected $secure;
/**
* The default SameSite option (defaults to lax).
*
* @var string
*/
protected $sameSite = 'lax';
/**
* All of the cookies queued for sending.
*
* @var \Symfony\Component\HttpFoundation\Cookie[]
*/
protected $queued = [];
/**
* Create a new cookie instance.
*
* @param string $name
* @param string $value
* @param int $minutes
* @param string|null $path
* @param string|null $domain
* @param bool|null $secure
* @param bool $httpOnly
* @param bool $raw
* @param string|null $sameSite
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public function make($name, $value, $minutes = 0, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null)
{
[$path, $domain, $secure, $sameSite] = $this->getPathAndDomain($path, $domain, $secure, $sameSite);
$time = ($minutes == 0) ? 0 : $this->availableAt($minutes * 60);
return new Cookie($name, $value, $time, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
}
/**
* Create a cookie that lasts "forever" (400 days).
*
* @param string $name
* @param string $value
* @param string|null $path
* @param string|null $domain
* @param bool|null $secure
* @param bool $httpOnly
* @param bool $raw
* @param string|null $sameSite
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public function forever($name, $value, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null)
{
return $this->make($name, $value, 576000, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
}
/**
* Expire the given cookie.
*
* @param string $name
* @param string|null $path
* @param string|null $domain
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public function forget($name, $path = null, $domain = null)
{
return $this->make($name, null, -2628000, $path, $domain);
}
/**
* Determine if a cookie has been queued.
*
* @param string $key
* @param string|null $path
* @return bool
*/
public function hasQueued($key, $path = null)
{
return ! is_null($this->queued($key, null, $path));
}
/**
* Get a queued cookie instance.
*
* @param string $key
* @param mixed $default
* @param string|null $path
* @return \Symfony\Component\HttpFoundation\Cookie|null
*/
public function queued($key, $default = null, $path = null)
{
$queued = Arr::get($this->queued, $key, $default);
if ($path === null) {
return Arr::last($queued, null, $default);
}
return Arr::get($queued, $path, $default);
}
/**
* Queue a cookie to send with the next response.
*
* @param mixed ...$parameters
* @return void
*/
public function queue(...$parameters)
{
if (isset($parameters[0]) && $parameters[0] instanceof Cookie) {
$cookie = $parameters[0];
} else {
$cookie = $this->make(...array_values($parameters));
}
if (! isset($this->queued[$cookie->getName()])) {
$this->queued[$cookie->getName()] = [];
}
$this->queued[$cookie->getName()][$cookie->getPath()] = $cookie;
}
/**
* Queue a cookie to expire with the next response.
*
* @param string $name
* @param string|null $path
* @param string|null $domain
* @return void
*/
public function expire($name, $path = null, $domain = null)
{
$this->queue($this->forget($name, $path, $domain));
}
/**
* Remove a cookie from the queue.
*
* @param string $name
* @param string|null $path
* @return void
*/
public function unqueue($name, $path = null)
{
if ($path === null) {
unset($this->queued[$name]);
return;
}
unset($this->queued[$name][$path]);
if (empty($this->queued[$name])) {
unset($this->queued[$name]);
}
}
/**
* Get the path and domain, or the default values.
*
* @param string $path
* @param string|null $domain
* @param bool|null $secure
* @param string|null $sameSite
* @return array
*/
protected function getPathAndDomain($path, $domain, $secure = null, $sameSite = null)
{
return [$path ?: $this->path, $domain ?: $this->domain, is_bool($secure) ? $secure : $this->secure, $sameSite ?: $this->sameSite];
}
/**
* Set the default path and domain for the jar.
*
* @param string $path
* @param string|null $domain
* @param bool|null $secure
* @param string|null $sameSite
* @return $this
*/
public function setDefaultPathAndDomain($path, $domain, $secure = false, $sameSite = null)
{
[$this->path, $this->domain, $this->secure, $this->sameSite] = [$path, $domain, $secure, $sameSite];
return $this;
}
/**
* Get the cookies which have been queued for the next request.
*
* @return \Symfony\Component\HttpFoundation\Cookie[]
*/
public function getQueuedCookies()
{
return Arr::flatten($this->queued);
}
/**
* Flush the cookies which have been queued for the next request.
*
* @return $this
*/
public function flushQueuedCookies()
{
$this->queued = [];
return $this;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Illuminate\Cookie;
use Illuminate\Support\ServiceProvider;
class CookieServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('cookie', function ($app) {
$config = $app->make('config')->get('session');
return (new CookieJar)->setDefaultPathAndDomain(
$config['path'], $config['domain'], $config['secure'], $config['same_site'] ?? null
);
});
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace Illuminate\Cookie;
class CookieValuePrefix
{
/**
* Create a new cookie value prefix for the given cookie name.
*
* @param string $cookieName
* @param string $key
* @return string
*/
public static function create($cookieName, $key)
{
return hash_hmac('sha1', $cookieName.'v2', $key).'|';
}
/**
* Remove the cookie value prefix.
*
* @param string $cookieValue
* @return string
*/
public static function remove($cookieValue)
{
return substr($cookieValue, 41);
}
/**
* Validate a cookie value contains a valid prefix. If it does, return the cookie value with the prefix removed. Otherwise, return null.
*
* @param string $cookieName
* @param string $cookieValue
* @param array $keys
* @return string|null
*/
public static function validate($cookieName, $cookieValue, array $keys)
{
foreach ($keys as $key) {
$hasValidPrefix = str_starts_with($cookieValue, static::create($cookieName, $key));
if ($hasValidPrefix) {
return static::remove($cookieValue);
}
}
}
}

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Taylor Otwell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,45 @@
<?php
namespace Illuminate\Cookie\Middleware;
use Closure;
use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar;
class AddQueuedCookiesToResponse
{
/**
* The cookie jar instance.
*
* @var \Illuminate\Contracts\Cookie\QueueingFactory
*/
protected $cookies;
/**
* Create a new CookieQueue instance.
*
* @param \Illuminate\Contracts\Cookie\QueueingFactory $cookies
* @return void
*/
public function __construct(CookieJar $cookies)
{
$this->cookies = $cookies;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
foreach ($this->cookies->getQueuedCookies() as $cookie) {
$response->headers->setCookie($cookie);
}
return $response;
}
}

View File

@@ -0,0 +1,255 @@
<?php
namespace Illuminate\Cookie\Middleware;
use Closure;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
use Illuminate\Cookie\CookieValuePrefix;
use Illuminate\Support\Arr;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class EncryptCookies
{
/**
* The encrypter instance.
*
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
protected $encrypter;
/**
* The names of the cookies that should not be encrypted.
*
* @var array<int, string>
*/
protected $except = [];
/**
* The globally ignored cookies that should not be encrypted.
*
* @var array
*/
protected static $neverEncrypt = [];
/**
* Indicates if cookies should be serialized.
*
* @var bool
*/
protected static $serialize = false;
/**
* Create a new CookieGuard instance.
*
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
* @return void
*/
public function __construct(EncrypterContract $encrypter)
{
$this->encrypter = $encrypter;
}
/**
* Disable encryption for the given cookie name(s).
*
* @param string|array $name
* @return void
*/
public function disableFor($name)
{
$this->except = array_merge($this->except, (array) $name);
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handle($request, Closure $next)
{
return $this->encrypt($next($this->decrypt($request)));
}
/**
* Decrypt the cookies on the request.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Request
*/
protected function decrypt(Request $request)
{
foreach ($request->cookies as $key => $cookie) {
if ($this->isDisabled($key)) {
continue;
}
try {
$value = $this->decryptCookie($key, $cookie);
$request->cookies->set($key, $this->validateValue($key, $value));
} catch (DecryptException) {
$request->cookies->set($key, null);
}
}
return $request;
}
/**
* Validate and remove the cookie value prefix from the value.
*
* @param string $key
* @param string $value
* @return string|array|null
*/
protected function validateValue(string $key, $value)
{
return is_array($value)
? $this->validateArray($key, $value)
: CookieValuePrefix::validate($key, $value, $this->encrypter->getAllKeys());
}
/**
* Validate and remove the cookie value prefix from all values of an array.
*
* @param string $key
* @param array $value
* @return array
*/
protected function validateArray(string $key, array $value)
{
$validated = [];
foreach ($value as $index => $subValue) {
$validated[$index] = $this->validateValue("{$key}[{$index}]", $subValue);
}
return $validated;
}
/**
* Decrypt the given cookie and return the value.
*
* @param string $name
* @param string|array $cookie
* @return string|array
*/
protected function decryptCookie($name, $cookie)
{
return is_array($cookie)
? $this->decryptArray($cookie)
: $this->encrypter->decrypt($cookie, static::serialized($name));
}
/**
* Decrypt an array based cookie.
*
* @param array $cookie
* @return array
*/
protected function decryptArray(array $cookie)
{
$decrypted = [];
foreach ($cookie as $key => $value) {
if (is_string($value)) {
$decrypted[$key] = $this->encrypter->decrypt($value, static::serialized($key));
}
if (is_array($value)) {
$decrypted[$key] = $this->decryptArray($value);
}
}
return $decrypted;
}
/**
* Encrypt the cookies on an outgoing response.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function encrypt(Response $response)
{
foreach ($response->headers->getCookies() as $cookie) {
if ($this->isDisabled($cookie->getName())) {
continue;
}
$response->headers->setCookie($this->duplicate(
$cookie,
$this->encrypter->encrypt(
CookieValuePrefix::create($cookie->getName(), $this->encrypter->getKey()).$cookie->getValue(),
static::serialized($cookie->getName())
)
));
}
return $response;
}
/**
* Duplicate a cookie with a new value.
*
* @param \Symfony\Component\HttpFoundation\Cookie $cookie
* @param mixed $value
* @return \Symfony\Component\HttpFoundation\Cookie
*/
protected function duplicate(Cookie $cookie, $value)
{
return $cookie->withValue($value);
}
/**
* Determine whether encryption has been disabled for the given cookie.
*
* @param string $name
* @return bool
*/
public function isDisabled($name)
{
return in_array($name, array_merge($this->except, static::$neverEncrypt));
}
/**
* Indicate that the given cookies should never be encrypted.
*
* @param array|string $cookies
* @return void
*/
public static function except($cookies)
{
static::$neverEncrypt = array_values(array_unique(
array_merge(static::$neverEncrypt, Arr::wrap($cookies))
));
}
/**
* Determine if the cookie contents should be serialized.
*
* @param string $name
* @return bool
*/
public static function serialized($name)
{
return static::$serialize;
}
/**
* Flush the middleware's global state.
*
* @return void
*/
public static function flushState()
{
static::$neverEncrypt = [];
static::$serialize = false;
}
}

View File

@@ -0,0 +1,40 @@
{
"name": "illuminate/cookie",
"description": "The Illuminate Cookie package.",
"license": "MIT",
"homepage": "https://laravel.com",
"support": {
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"require": {
"php": "^8.2",
"ext-hash": "*",
"illuminate/collections": "^11.0",
"illuminate/contracts": "^11.0",
"illuminate/macroable": "^11.0",
"illuminate/support": "^11.0",
"symfony/http-foundation": "^7.0.3",
"symfony/http-kernel": "^7.0.3"
},
"autoload": {
"psr-4": {
"Illuminate\\Cookie\\": ""
}
},
"extra": {
"branch-alias": {
"dev-master": "11.x-dev"
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev"
}