PageRenderTime 23ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/backwpup/sdk/Aws/Guzzle/Plugin/Cookie/Cookie.php

https://bitbucket.org/cesarmedrano/cesarmedrano
PHP | 525 lines | 218 code | 55 blank | 252 comment | 21 complexity | 3ee694c25c767126d058eddb0ccf25e6 MD5 | raw file
  1. <?php
  2. namespace Guzzle\Plugin\Cookie;
  3. use Guzzle\Common\ToArrayInterface;
  4. /**
  5. * Set-Cookie object
  6. */
  7. class Cookie implements ToArrayInterface
  8. {
  9. /**
  10. * @var array Cookie data
  11. */
  12. protected $data;
  13. /**
  14. * @var string ASCII codes not valid for for use in a cookie name
  15. *
  16. * Cookie names are defined as 'token', according to RFC 2616, Section 2.2
  17. * A valid token may contain any CHAR except CTLs (ASCII 0 - 31 or 127)
  18. * or any of the following separators
  19. */
  20. protected static $invalidCharString;
  21. /**
  22. * Gets an array of invalid cookie characters
  23. *
  24. * @return array
  25. */
  26. protected static function getInvalidCharacters()
  27. {
  28. if (!self::$invalidCharString) {
  29. self::$invalidCharString = implode('', array_map('chr', array_merge(
  30. range(0, 32),
  31. array(34, 40, 41, 44, 47),
  32. array(58, 59, 60, 61, 62, 63, 64, 91, 92, 93, 123, 125, 127)
  33. )));
  34. }
  35. return self::$invalidCharString;
  36. }
  37. /**
  38. * @param array $data Array of cookie data provided by a Cookie parser
  39. */
  40. public function __construct(array $data = array())
  41. {
  42. static $defaults = array(
  43. 'name' => '',
  44. 'value' => '',
  45. 'domain' => '',
  46. 'path' => '/',
  47. 'expires' => null,
  48. 'max_age' => 0,
  49. 'comment' => null,
  50. 'comment_url' => null,
  51. 'port' => array(),
  52. 'version' => null,
  53. 'secure' => false,
  54. 'discard' => false,
  55. 'http_only' => false
  56. );
  57. $this->data = array_merge($defaults, $data);
  58. // Extract the expires value and turn it into a UNIX timestamp if needed
  59. if (!$this->getExpires() && $this->getMaxAge()) {
  60. // Calculate the expires date
  61. $this->setExpires(time() + (int) $this->getMaxAge());
  62. } elseif ($this->getExpires() && !is_numeric($this->getExpires())) {
  63. $this->setExpires(strtotime($this->getExpires()));
  64. }
  65. }
  66. /**
  67. * Get the cookie as an array
  68. *
  69. * @return array
  70. */
  71. public function toArray()
  72. {
  73. return $this->data;
  74. }
  75. /**
  76. * Get the cookie name
  77. *
  78. * @return string
  79. */
  80. public function getName()
  81. {
  82. return $this->data['name'];
  83. }
  84. /**
  85. * Set the cookie name
  86. *
  87. * @param string $name Cookie name
  88. *
  89. * @return Cookie
  90. */
  91. public function setName($name)
  92. {
  93. return $this->setData('name', $name);
  94. }
  95. /**
  96. * Get the cookie value
  97. *
  98. * @return string
  99. */
  100. public function getValue()
  101. {
  102. return $this->data['value'];
  103. }
  104. /**
  105. * Set the cookie value
  106. *
  107. * @param string $value Cookie value
  108. *
  109. * @return Cookie
  110. */
  111. public function setValue($value)
  112. {
  113. return $this->setData('value', $value);
  114. }
  115. /**
  116. * Get the domain
  117. *
  118. * @return string|null
  119. */
  120. public function getDomain()
  121. {
  122. return $this->data['domain'];
  123. }
  124. /**
  125. * Set the domain of the cookie
  126. *
  127. * @param string $domain
  128. *
  129. * @return Cookie
  130. */
  131. public function setDomain($domain)
  132. {
  133. return $this->setData('domain', $domain);
  134. }
  135. /**
  136. * Get the path
  137. *
  138. * @return string
  139. */
  140. public function getPath()
  141. {
  142. return $this->data['path'];
  143. }
  144. /**
  145. * Set the path of the cookie
  146. *
  147. * @param string $path Path of the cookie
  148. *
  149. * @return Cookie
  150. */
  151. public function setPath($path)
  152. {
  153. return $this->setData('path', $path);
  154. }
  155. /**
  156. * Maximum lifetime of the cookie in seconds
  157. *
  158. * @return int|null
  159. */
  160. public function getMaxAge()
  161. {
  162. return $this->data['max_age'];
  163. }
  164. /**
  165. * Set the max-age of the cookie
  166. *
  167. * @param int $maxAge Max age of the cookie in seconds
  168. *
  169. * @return Cookie
  170. */
  171. public function setMaxAge($maxAge)
  172. {
  173. return $this->setData('max_age', $maxAge);
  174. }
  175. /**
  176. * The UNIX timestamp when the cookie expires
  177. *
  178. * @return mixed
  179. */
  180. public function getExpires()
  181. {
  182. return $this->data['expires'];
  183. }
  184. /**
  185. * Set the unix timestamp for which the cookie will expire
  186. *
  187. * @param int $timestamp Unix timestamp
  188. *
  189. * @return Cookie
  190. */
  191. public function setExpires($timestamp)
  192. {
  193. return $this->setData('expires', $timestamp);
  194. }
  195. /**
  196. * Version of the cookie specification. RFC 2965 is 1
  197. *
  198. * @return mixed
  199. */
  200. public function getVersion()
  201. {
  202. return $this->data['version'];
  203. }
  204. /**
  205. * Set the cookie version
  206. *
  207. * @param string|int $version Version to set
  208. *
  209. * @return Cookie
  210. */
  211. public function setVersion($version)
  212. {
  213. return $this->setData('version', $version);
  214. }
  215. /**
  216. * Get whether or not this is a secure cookie
  217. *
  218. * @return null|bool
  219. */
  220. public function getSecure()
  221. {
  222. return $this->data['secure'];
  223. }
  224. /**
  225. * Set whether or not the cookie is secure
  226. *
  227. * @param bool $secure Set to true or false if secure
  228. *
  229. * @return Cookie
  230. */
  231. public function setSecure($secure)
  232. {
  233. return $this->setData('secure', (bool) $secure);
  234. }
  235. /**
  236. * Get whether or not this is a session cookie
  237. *
  238. * @return null|bool
  239. */
  240. public function getDiscard()
  241. {
  242. return $this->data['discard'];
  243. }
  244. /**
  245. * Set whether or not this is a session cookie
  246. *
  247. * @param bool $discard Set to true or false if this is a session cookie
  248. *
  249. * @return Cookie
  250. */
  251. public function setDiscard($discard)
  252. {
  253. return $this->setData('discard', $discard);
  254. }
  255. /**
  256. * Get the comment
  257. *
  258. * @return string|null
  259. */
  260. public function getComment()
  261. {
  262. return $this->data['comment'];
  263. }
  264. /**
  265. * Set the comment of the cookie
  266. *
  267. * @param string $comment Cookie comment
  268. *
  269. * @return Cookie
  270. */
  271. public function setComment($comment)
  272. {
  273. return $this->setData('comment', $comment);
  274. }
  275. /**
  276. * Get the comment URL of the cookie
  277. *
  278. * @return string|null
  279. */
  280. public function getCommentUrl()
  281. {
  282. return $this->data['comment_url'];
  283. }
  284. /**
  285. * Set the comment URL of the cookie
  286. *
  287. * @param string $commentUrl Cookie comment URL for more information
  288. *
  289. * @return Cookie
  290. */
  291. public function setCommentUrl($commentUrl)
  292. {
  293. return $this->setData('comment_url', $commentUrl);
  294. }
  295. /**
  296. * Get an array of acceptable ports this cookie can be used with
  297. *
  298. * @return array
  299. */
  300. public function getPorts()
  301. {
  302. return $this->data['port'];
  303. }
  304. /**
  305. * Set a list of acceptable ports this cookie can be used with
  306. *
  307. * @param array $ports Array of acceptable ports
  308. *
  309. * @return Cookie
  310. */
  311. public function setPorts(array $ports)
  312. {
  313. return $this->setData('port', $ports);
  314. }
  315. /**
  316. * Get whether or not this is an HTTP only cookie
  317. *
  318. * @return bool
  319. */
  320. public function getHttpOnly()
  321. {
  322. return $this->data['http_only'];
  323. }
  324. /**
  325. * Set whether or not this is an HTTP only cookie
  326. *
  327. * @param bool $httpOnly Set to true or false if this is HTTP only
  328. *
  329. * @return Cookie
  330. */
  331. public function setHttpOnly($httpOnly)
  332. {
  333. return $this->setData('http_only', $httpOnly);
  334. }
  335. /**
  336. * Get an array of extra cookie data
  337. *
  338. * @return array
  339. */
  340. public function getAttributes()
  341. {
  342. return $this->data['data'];
  343. }
  344. /**
  345. * Get a specific data point from the extra cookie data
  346. *
  347. * @param string $name Name of the data point to retrieve
  348. *
  349. * @return null|string
  350. */
  351. public function getAttribute($name)
  352. {
  353. return array_key_exists($name, $this->data['data']) ? $this->data['data'][$name] : null;
  354. }
  355. /**
  356. * Set a cookie data attribute
  357. *
  358. * @param string $name Name of the attribute to set
  359. * @param string $value Value to set
  360. *
  361. * @return Cookie
  362. */
  363. public function setAttribute($name, $value)
  364. {
  365. $this->data['data'][$name] = $value;
  366. return $this;
  367. }
  368. /**
  369. * Check if the cookie matches a path value
  370. *
  371. * @param string $path Path to check against
  372. *
  373. * @return bool
  374. */
  375. public function matchesPath($path)
  376. {
  377. return !$this->getPath() || 0 === stripos($path, $this->getPath());
  378. }
  379. /**
  380. * Check if the cookie matches a domain value
  381. *
  382. * @param string $domain Domain to check against
  383. *
  384. * @return bool
  385. */
  386. public function matchesDomain($domain)
  387. {
  388. $cookieDomain = $this->getDomain();
  389. // Domain not set or exact match.
  390. if (!$cookieDomain || !strcasecmp($domain, $cookieDomain)) {
  391. return true;
  392. }
  393. // . prefix match.
  394. if (strpos($cookieDomain, '.') === 0) {
  395. $realDomain = substr($cookieDomain, 1);
  396. // Root domains don't match except for .local.
  397. if (!substr_count($realDomain, '.') && strcasecmp($realDomain, 'local')) {
  398. return false;
  399. }
  400. if (substr($domain, -strlen($realDomain)) === $realDomain) {
  401. // Match exact or 1 deep subdomain.
  402. return !strcasecmp($domain, $realDomain) ||
  403. substr_count(substr($domain, 0, -strlen($realDomain)), '.') === 1;
  404. }
  405. }
  406. return false;
  407. }
  408. /**
  409. * Check if the cookie is compatible with a specific port
  410. *
  411. * @param int $port Port to check
  412. *
  413. * @return bool
  414. */
  415. public function matchesPort($port)
  416. {
  417. return count($this->getPorts()) == 0 || in_array($port, $this->getPorts());
  418. }
  419. /**
  420. * Check if the cookie is expired
  421. *
  422. * @return bool
  423. */
  424. public function isExpired()
  425. {
  426. return $this->getExpires() && time() > $this->getExpires();
  427. }
  428. /**
  429. * Check if the cookie is valid according to RFC 6265
  430. *
  431. * @return bool|string Returns true if valid or an error message if invalid
  432. */
  433. public function validate()
  434. {
  435. // Names must not be empty, but can be 0
  436. $name = $this->getName();
  437. if (empty($name) && !is_numeric($name)) {
  438. return 'The cookie name must not be empty';
  439. }
  440. // Check if any of the invalid characters are present in the cookie name
  441. if (strpbrk($name, self::getInvalidCharacters()) !== false) {
  442. return 'The cookie name must not contain invalid characters: ' . $name;
  443. }
  444. // Value must not be empty, but can be 0
  445. $value = $this->getValue();
  446. if (empty($value) && !is_numeric($value)) {
  447. return 'The cookie value must not be empty';
  448. }
  449. // Domains must not be empty, but can be 0
  450. // A "0" is not a valid internet domain, but may be used as server name in a private network
  451. $domain = $this->getDomain();
  452. if (empty($domain) && !is_numeric($domain)) {
  453. return 'The cookie domain must not be empty';
  454. }
  455. return true;
  456. }
  457. /**
  458. * Set a value and return the cookie object
  459. *
  460. * @param string $key Key to set
  461. * @param string $value Value to set
  462. *
  463. * @return Cookie
  464. */
  465. private function setData($key, $value)
  466. {
  467. $this->data[$key] = $value;
  468. return $this;
  469. }
  470. }