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

namespace IlluminateCookieMiddleware;

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

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

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

    
/**
     * 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 $cookieName
     * @return void
     */
    
public function disableFor($cookieName)
    {
        
$this->except array_merge($this->except, (array) $cookieName);
    }

    
/**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     */
    
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 => $c) {
            if (
$this->isDisabled($key)) {
                continue;
            }

            try {
                
$request->cookies->set($key$this->decryptCookie($c));
            } catch (
DecryptException $e) {
                
$request->cookies->set($keynull);
            }
        }

        return 
$request;
    }

    
/**
     * Decrypt the given cookie and return the value.
     *
     * @param  string|array  $cookie
     * @return string|array
     */
    
protected function decryptCookie($cookie)
    {
        return 
is_array($cookie)
                        ? 
$this->decryptArray($cookie)
                        : 
$this->encrypter->decrypt($cookie);
    }

    
/**
     * 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);
            }
        }

        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($cookie->getValue())
            ));
        }

        return 
$response;
    }

    
/**
     * Duplicate a cookie with a new value.
     *
     * @param  SymfonyComponentHttpFoundationCookie  $c
     * @param  mixed  $value
     * @return SymfonyComponentHttpFoundationCookie
     */
    
protected function duplicate(Cookie $c$value)
    {
        return new 
Cookie(
            
$c->getName(), $value$c->getExpiresTime(), $c->getPath(),
            
$c->getDomain(), $c->isSecure(), $c->isHttpOnly()
        );
    }

    
/**
     * 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);
    }
}
Онлайн: 1
Реклама