1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <fabien@symfony.com>
7: *
8: * For the full copyright and license information, please view the LICENSE
9: * file that was distributed with this source code.
10: */
11:
12: namespace Symfony\Component\HttpFoundation;
13:
14: /**
15: * Represents a cookie
16: *
17: * @author Johannes M. Schmitt <schmittjoh@gmail.com>
18: *
19: * @api
20: */
21: class Cookie
22: {
23: protected $name;
24: protected $value;
25: protected $domain;
26: protected $expire;
27: protected $path;
28: protected $secure;
29: protected $httpOnly;
30:
31: /**
32: * Constructor.
33: *
34: * @param string $name The name of the cookie
35: * @param string $value The value of the cookie
36: * @param integer|string|\DateTime $expire The time the cookie expires
37: * @param string $path The path on the server in which the cookie will be available on
38: * @param string $domain The domain that the cookie is available to
39: * @param Boolean $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client
40: * @param Boolean $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
41: *
42: * @throws \InvalidArgumentException
43: *
44: * @api
45: */
46: public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true)
47: {
48: // from PHP source code
49: if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
50: throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
51: }
52:
53: if (empty($name)) {
54: throw new \InvalidArgumentException('The cookie name cannot be empty.');
55: }
56:
57: // convert expiration time to a Unix timestamp
58: if ($expire instanceof \DateTime) {
59: $expire = $expire->format('U');
60: } elseif (!is_numeric($expire)) {
61: $expire = strtotime($expire);
62:
63: if (false === $expire || -1 === $expire) {
64: throw new \InvalidArgumentException('The cookie expiration time is not valid.');
65: }
66: }
67:
68: $this->name = $name;
69: $this->value = $value;
70: $this->domain = $domain;
71: $this->expire = $expire;
72: $this->path = empty($path) ? '/' : $path;
73: $this->secure = (Boolean) $secure;
74: $this->httpOnly = (Boolean) $httpOnly;
75: }
76:
77: /**
78: * Returns the cookie as a string.
79: *
80: * @return string The cookie
81: */
82: public function __toString()
83: {
84: $str = urlencode($this->getName()).'=';
85:
86: if ('' === (string) $this->getValue()) {
87: $str .= 'deleted; expires='.gmdate("D, d-M-Y H:i:s T", time() - 31536001);
88: } else {
89: $str .= urlencode($this->getValue());
90:
91: if ($this->getExpiresTime() !== 0) {
92: $str .= '; expires='.gmdate("D, d-M-Y H:i:s T", $this->getExpiresTime());
93: }
94: }
95:
96: if ('/' !== $this->path) {
97: $str .= '; path='.$this->path;
98: }
99:
100: if (null !== $this->getDomain()) {
101: $str .= '; domain='.$this->getDomain();
102: }
103:
104: if (true === $this->isSecure()) {
105: $str .= '; secure';
106: }
107:
108: if (true === $this->isHttpOnly()) {
109: $str .= '; httponly';
110: }
111:
112: return $str;
113: }
114:
115: /**
116: * Gets the name of the cookie.
117: *
118: * @return string
119: *
120: * @api
121: */
122: public function getName()
123: {
124: return $this->name;
125: }
126:
127: /**
128: * Gets the value of the cookie.
129: *
130: * @return string
131: *
132: * @api
133: */
134: public function getValue()
135: {
136: return $this->value;
137: }
138:
139: /**
140: * Gets the domain that the cookie is available to.
141: *
142: * @return string
143: *
144: * @api
145: */
146: public function getDomain()
147: {
148: return $this->domain;
149: }
150:
151: /**
152: * Gets the time the cookie expires.
153: *
154: * @return integer
155: *
156: * @api
157: */
158: public function getExpiresTime()
159: {
160: return $this->expire;
161: }
162:
163: /**
164: * Gets the path on the server in which the cookie will be available on.
165: *
166: * @return string
167: *
168: * @api
169: */
170: public function getPath()
171: {
172: return $this->path;
173: }
174:
175: /**
176: * Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
177: *
178: * @return Boolean
179: *
180: * @api
181: */
182: public function isSecure()
183: {
184: return $this->secure;
185: }
186:
187: /**
188: * Checks whether the cookie will be made accessible only through the HTTP protocol.
189: *
190: * @return Boolean
191: *
192: * @api
193: */
194: public function isHttpOnly()
195: {
196: return $this->httpOnly;
197: }
198:
199: /**
200: * Whether this cookie is about to be cleared
201: *
202: * @return Boolean
203: *
204: * @api
205: */
206: public function isCleared()
207: {
208: return $this->expire < time();
209: }
210: }
211: