Вход Регистрация
Файл: vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php
Строк: 232
<?php

namespace IlluminateCookieMiddleware;

use 
Closure;
use 
IlluminateContractsEncryptionDecryptException;
use 
IlluminateContractsEncryptionEncrypter as EncrypterContract;
use 
IlluminateCookieCookieValuePrefix;
use 
SymfonyComponentHttpFoundationCookie;
use 
SymfonyComponentHttpFoundationRequest;
use 
SymfonyComponentHttpFoundationResponse;

class 
EncryptCookies
{
    
/**
     * The encrypter instance.
     *
     * @var IlluminateContractsEncryptionEncrypter
     */
    
protected $encrypter;

    
/**
     * The names of the cookies that should not be encrypted.
     *
     * @var array
     */
    
protected $except = [];

    
/**
     * Indicates if cookies should be serialized.
     *
     * @var bool
     */
    
protected static $serialize false;

    
/**
     * Create a new CookieGuard instance.
     *
     * @param  IlluminateContractsEncryptionEncrypter  $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  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return SymfonyComponentHttpFoundationResponse
     */
    
public function handle($requestClosure $next)
    {
        return 
$this->encrypt($next($this->decrypt($request)));
    }

    
/**
     * Decrypt the cookies on the request.
     *
     * @param  SymfonyComponentHttpFoundationRequest  $request
     * @return SymfonyComponentHttpFoundationRequest
     */
    
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 $e) {
                
$request->cookies->set($keynull);
            }
        }

        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->getKey());
    }

    
/**
     * 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  SymfonyComponentHttpFoundationResponse  $response
     * @return SymfonyComponentHttpFoundationResponse
     */
    
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  SymfonyComponentHttpFoundationCookie  $cookie
     * @param  mixed  $value
     * @return SymfonyComponentHttpFoundationCookie
     */
    
protected function duplicate(Cookie $cookie$value)
    {
        return new 
Cookie(
            
$cookie->getName(), $value$cookie->getExpiresTime(),
            
$cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(),
            
$cookie->isHttpOnly(), $cookie->isRaw(), $cookie->getSameSite()
        );
    }

    
/**
     * Determine whether encryption has been disabled for the given cookie.
     *
     * @param  string  $name
     * @return bool
     */
    
public function isDisabled($name)
    {
        return 
in_array($name$this->except);
    }

    
/**
     * Determine if the cookie contents should be serialized.
     *
     * @param  string  $name
     * @return bool
     */
    
public static function serialized($name)
    {
        return static::
$serialize;
    }
}
Онлайн: 0
Реклама