1 | <?php |
||
19 | final class CookiePlugin implements Plugin |
||
20 | { |
||
21 | /** |
||
22 | * Cookie storage. |
||
23 | * |
||
24 | * @var CookieJar |
||
25 | */ |
||
26 | private $cookieJar; |
||
27 | |||
28 | /** |
||
29 | * @param CookieJar $cookieJar |
||
30 | */ |
||
31 | 12 | public function __construct(CookieJar $cookieJar) |
|
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | 10 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
40 | { |
||
41 | 10 | foreach ($this->cookieJar->getCookies() as $cookie) { |
|
42 | 8 | if ($cookie->isExpired()) { |
|
43 | 1 | continue; |
|
44 | } |
||
45 | |||
46 | 7 | if (!$cookie->matchDomain($request->getUri()->getHost())) { |
|
47 | 2 | continue; |
|
48 | } |
||
49 | |||
50 | 5 | if (!$cookie->matchPath($request->getUri()->getPath())) { |
|
51 | 1 | continue; |
|
52 | } |
||
53 | |||
54 | 4 | if ($cookie->isSecure() && ('https' !== $request->getUri()->getScheme())) { |
|
55 | 1 | continue; |
|
56 | } |
||
57 | |||
58 | 3 | $request = $request->withAddedHeader('Cookie', sprintf('%s=%s', $cookie->getName(), $cookie->getValue())); |
|
59 | } |
||
60 | |||
61 | 10 | return $next($request)->then(function (ResponseInterface $response) use ($request) { |
|
62 | 2 | if ($response->hasHeader('Set-Cookie')) { |
|
63 | 2 | $setCookies = $response->getHeader('Set-Cookie'); |
|
64 | |||
65 | 2 | foreach ($setCookies as $setCookie) { |
|
66 | 2 | $cookie = $this->createCookie($request, $setCookie); |
|
67 | |||
68 | // Cookie invalid do not use it |
||
69 | 1 | if (null === $cookie) { |
|
70 | continue; |
||
71 | } |
||
72 | |||
73 | // Restrict setting cookie from another domain |
||
74 | 1 | if (!preg_match("/\.{$cookie->getDomain()}$/", '.'.$request->getUri()->getHost())) { |
|
75 | continue; |
||
76 | } |
||
77 | |||
78 | 1 | $this->cookieJar->addCookie($cookie); |
|
79 | } |
||
80 | } |
||
81 | |||
82 | 1 | return $response; |
|
83 | 10 | }); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Creates a cookie from a string. |
||
88 | * |
||
89 | * @param RequestInterface $request |
||
90 | * @param $setCookie |
||
91 | * |
||
92 | * @return Cookie|null |
||
93 | * |
||
94 | * @throws TransferException |
||
95 | */ |
||
96 | 2 | private function createCookie(RequestInterface $request, $setCookie) |
|
97 | { |
||
98 | 2 | $parts = array_map('trim', explode(';', $setCookie)); |
|
99 | |||
100 | 2 | if (empty($parts) || !strpos($parts[0], '=')) { |
|
101 | return; |
||
102 | } |
||
103 | |||
104 | 2 | list($name, $cookieValue) = $this->createValueKey(array_shift($parts)); |
|
105 | |||
106 | 2 | $maxAge = null; |
|
107 | 2 | $expires = null; |
|
108 | 2 | $domain = $request->getUri()->getHost(); |
|
109 | 2 | $path = $request->getUri()->getPath(); |
|
110 | 2 | $secure = false; |
|
111 | 2 | $httpOnly = false; |
|
112 | |||
113 | // Add the cookie pieces into the parsed data array |
||
114 | 2 | foreach ($parts as $part) { |
|
115 | 2 | list($key, $value) = $this->createValueKey($part); |
|
116 | |||
117 | 2 | switch (strtolower($key)) { |
|
118 | case 'expires': |
||
119 | try { |
||
120 | 2 | $expires = CookieUtil::parseDate($value); |
|
|
|||
121 | 1 | } catch (UnexpectedValueException $e) { |
|
122 | 1 | throw new TransferException( |
|
123 | 1 | sprintf( |
|
124 | 1 | 'Cookie header `%s` expires value `%s` could not be converted to date', |
|
125 | 1 | $name, |
|
126 | 1 | $value |
|
127 | ), |
||
128 | 1 | null, |
|
129 | 1 | $e |
|
130 | ); |
||
131 | } |
||
132 | |||
133 | 1 | break; |
|
134 | |||
135 | case 'max-age': |
||
136 | 1 | $maxAge = (int) $value; |
|
137 | |||
138 | 1 | break; |
|
139 | |||
140 | case 'domain': |
||
141 | 1 | $domain = $value; |
|
142 | |||
143 | 1 | break; |
|
144 | |||
145 | case 'path': |
||
146 | 1 | $path = $value; |
|
147 | |||
148 | 1 | break; |
|
149 | |||
150 | case 'secure': |
||
151 | 1 | $secure = true; |
|
152 | |||
153 | 1 | break; |
|
154 | |||
155 | case 'httponly': |
||
156 | 1 | $httpOnly = true; |
|
157 | |||
158 | 1 | break; |
|
159 | } |
||
160 | } |
||
161 | |||
162 | 1 | return new Cookie($name, $cookieValue, $maxAge, $domain, $path, $secure, $httpOnly, $expires); |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Separates key/value pair from cookie. |
||
167 | * |
||
168 | * @param $part |
||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | 2 | private function createValueKey($part) |
|
180 | } |
||
181 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.