1: <?php
2:
3: namespace Guzzle\Parser\Cookie;
4:
5: 6: 7:
8: class CookieParser implements CookieParserInterface
9: {
10: 11: 12:
13: protected static $cookieParts = array(
14: 'domain' => 'Domain',
15: 'path' => 'Path',
16: 'max_age' => 'Max-Age',
17: 'expires' => 'Expires',
18: 'version' => 'Version',
19: 'secure' => 'Secure',
20: 'port' => 'Port',
21: 'discard' => 'Discard',
22: 'comment' => 'Comment',
23: 'comment_url' => 'Comment-Url',
24: 'http_only' => 'HttpOnly'
25: );
26:
27: 28: 29:
30: public function parseCookie($cookie, $host = null, $path = null, $decode = false)
31: {
32:
33: $pieces = array_filter(array_map('trim', explode(';', $cookie)));
34:
35:
36: if (empty($pieces) || !strpos($pieces[0], '=')) {
37: return false;
38: }
39:
40:
41: $data = array_merge(array_fill_keys(array_keys(self::$cookieParts), null), array(
42: 'cookies' => array(),
43: 'data' => array(),
44: 'path' => $path ?: '/',
45: 'http_only' => false,
46: 'discard' => false,
47: 'domain' => $host
48: ));
49: $foundNonCookies = 0;
50:
51:
52: foreach ($pieces as $part) {
53:
54: $cookieParts = explode('=', $part, 2);
55: $key = trim($cookieParts[0]);
56:
57: if (count($cookieParts) == 1) {
58:
59: $value = true;
60: } else {
61:
62: $value = trim($cookieParts[1], " \n\r\t\0\x0B\"");
63: if ($decode) {
64: $value = urldecode($value);
65: }
66: }
67:
68:
69: if (!empty($data['cookies'])) {
70: foreach (self::$cookieParts as $mapValue => $search) {
71: if (!strcasecmp($search, $key)) {
72: $data[$mapValue] = $mapValue == 'port' ? array_map('trim', explode(',', $value)) : $value;
73: $foundNonCookies++;
74: continue 2;
75: }
76: }
77: }
78:
79:
80:
81: $data[$foundNonCookies ? 'data' : 'cookies'][$key] = $value;
82: }
83:
84:
85: if (!$data['expires'] && $data['max_age']) {
86: $data['expires'] = time() + (int) $data['max_age'];
87: }
88:
89: return $data;
90: }
91: }
92: