/var/www/hkosl.com/littleark/webadmin/libraies/illuminate/cookie/Illuminate/Cookie/Guard.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php namespace Illuminate\Cookie;

use 
Illuminate\Encryption\Encrypter;
use 
Illuminate\Encryption\DecryptException;
use 
Symfony\Component\HttpFoundation\Cookie;
use 
Symfony\Component\HttpFoundation\Request;
use 
Symfony\Component\HttpFoundation\Response;
use 
Symfony\Component\HttpKernel\HttpKernelInterface;

class 
Guard implements HttpKernelInterface {

    
/**
     * The wrapped kernel implementation.
     *
     * @var \Symfony\Component\HttpKernel\HttpKernelInterface
     */
    
protected $app;

    
/**
     * The encrypter instance.
     *
     * @var \Illuminate\Encryption\Encrypter
     */
    
protected $encrypter;

    
/**
     * Create a new CookieGuard instance.
     *
     * @param  \Symfony\Component\HttpKernel\HttpKernelInterface  $app
     * @param  \Illuminate\Encryption\Encrypter  $encrypter
     * @return void
     */
    
public function __construct(HttpKernelInterface $appEncrypter $encrypter)
    {
        
$this->app $app;
        
$this->encrypter $encrypter;
    }

    
/**
     * Handle the given request and get the response.
     *
     * @implements HttpKernelInterface::handle
     *
     * @param  \Symfony\Component\HttpFoundation\Request  $request
     * @param  int   $type
     * @param  bool  $catch
     * @return \Symfony\Component\HttpFoundation\Response
     */
    
public function handle(Request $request$type HttpKernelInterface::MASTER_REQUEST$catch true)
    {
        return 
$this->encrypt($this->app->handle($this->decrypt($request), $type$catch));
    }

    
/**
     * 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 => $c)
        {
            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 = array();

        foreach (
$cookie as $key => $value)
        {
            
$decrypted[$key] = $this->encrypter->decrypt($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 $key => $c)
        {
            
$encrypted $this->encrypter->encrypt($c->getValue());

            
$response->headers->setCookie($this->duplicate($c$encrypted));
        }

        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 $c$value)
    {
        return new 
Cookie(
            
$c->getName(), $value$c->getExpiresTime(), $c->getPath(),
            
$c->getDomain(), $c->isSecure(), $c->isHttpOnly()
        );
    }

}