|
1 | 1 | <?php
|
| 2 | + |
2 | 3 | namespace PHPCrawl\CookieCache;
|
3 | 4 |
|
| 5 | +use PHPCrawl\PHPCrawlerCookieDescriptor; |
| 6 | +use PHPCrawl\Utils\PHPCrawlerUtils; |
| 7 | + |
4 | 8 | /**
|
5 | 9 | * Class for storing/caching cookies in memory.
|
6 | 10 | *
|
|
9 | 13 | */
|
10 | 14 | class PHPCrawlerMemoryCookieCache extends PHPCrawlerCookieCacheBase
|
11 | 15 | {
|
12 |
| - protected $cookies = []; |
13 |
| - |
14 |
| - /** |
15 |
| - * Adds a cookie to the cookie-cache. |
16 |
| - * |
17 |
| - * @param PHPCrawlerCookieDescriptor $Cookie The cookie to add. |
18 |
| - */ |
19 |
| - public function addCookie(PHPCrawlerCookieDescriptor $Cookie) |
20 |
| - { |
21 |
| - $source_domain = $Cookie->source_domain; |
22 |
| - $cookie_domain = $Cookie->domain; |
23 |
| - $cookie_path = $Cookie->path; |
24 |
| - $cookie_name = $Cookie->name; |
25 |
| - |
26 |
| - $cookie_hash = md5($cookie_domain. '_' .$cookie_path. '_' .$cookie_name); |
27 |
| - |
28 |
| - $this->cookies[$source_domain][$cookie_hash] = $Cookie; |
29 |
| - } |
30 |
| - |
31 |
| - /** |
32 |
| - * Adds a bunch of cookies to the cookie-cache. |
33 |
| - * |
34 |
| - * @param array $cookies Numeric array conatinin the cookies to add as PHPCrawlerCookieDescriptor-objects |
35 |
| - */ |
36 |
| - public function addCookies($cookies) |
37 |
| - { |
38 |
| - for ($x=0, $xMax = count($cookies); $x< $xMax; $x++) |
| 16 | + protected $cookies = []; |
| 17 | + |
| 18 | + /** |
| 19 | + * Adds a cookie to the cookie-cache. |
| 20 | + * |
| 21 | + * @param PHPCrawlerCookieDescriptor $Cookie The cookie to add. |
| 22 | + */ |
| 23 | + public function addCookie(PHPCrawlerCookieDescriptor $Cookie) |
39 | 24 | {
|
40 |
| - $this->addCookie($cookies[$x]); |
| 25 | + $source_domain = $Cookie->source_domain; |
| 26 | + $cookie_domain = $Cookie->domain; |
| 27 | + $cookie_path = $Cookie->path; |
| 28 | + $cookie_name = $Cookie->name; |
| 29 | + |
| 30 | + $cookie_hash = md5($cookie_domain . '_' . $cookie_path . '_' . $cookie_name); |
| 31 | + |
| 32 | + $this->cookies[$source_domain][$cookie_hash] = $Cookie; |
41 | 33 | }
|
42 |
| - } |
43 |
| - |
44 |
| - /** |
45 |
| - * Returns all cookies from the cache that are adressed to the given URL |
46 |
| - * |
47 |
| - * @param string $target_url The target-URL |
48 |
| - * @return array Numeric array conatining all matching cookies as PHPCrawlerCookieDescriptor-objects |
49 |
| - */ |
50 |
| - public function getCookiesForUrl($target_url) |
51 |
| - { |
52 |
| - $url_parts = PHPCrawlerUtils::splitURL($target_url); |
53 |
| - |
54 |
| - $target_domain = $url_parts['domain']; // e.g. acme.com |
55 |
| - |
56 |
| - $return_cookies = []; |
57 |
| - |
58 |
| - // Iterate over all cookies of this domain |
59 |
| - @reset($this->cookies[$target_domain]); |
60 |
| - while (list($hash) = @each($this->cookies[$target_domain])) |
| 34 | + |
| 35 | + /** |
| 36 | + * Adds a bunch of cookies to the cookie-cache. |
| 37 | + * |
| 38 | + * @param array $cookies Numeric array conatinin the cookies to add as PHPCrawlerCookieDescriptor-objects |
| 39 | + */ |
| 40 | + public function addCookies($cookies) |
61 | 41 | {
|
62 |
| - $Cookie = $this->cookies[$target_domain][$hash]; |
63 |
| - |
64 |
| - // Does the cookie-domain match? |
65 |
| - // Tail-matching, see http://curl.haxx.se/rfc/cookie_spec.html: |
66 |
| - // A domain attribute of "acme.com" would match host names "anvil.acme.com" as well as "shipping.crate.acme.com" |
67 |
| - // Seems like ".acme.com" should also match "anvil.acme.com", so just remove the dot |
68 |
| - |
69 |
| - $Cookie->domain = preg_replace('#^.#', '', $Cookie->domain); |
70 |
| - |
71 |
| - if ($Cookie->domain == $url_parts['host'] || preg_match('#' .preg_quote($Cookie->domain). '$#', $url_parts['host'])) |
72 |
| - { |
73 |
| - // Does the path match? |
74 |
| - if (preg_match('#^' .preg_quote($Cookie->path). '#', $url_parts['path'])) |
75 |
| - { |
76 |
| - $return_cookies[$Cookie->name] = $Cookie; // Use cookie-name as index to avoid double-cookies |
| 42 | + for ($x = 0, $xMax = count($cookies); $x < $xMax; $x++) { |
| 43 | + $this->addCookie($cookies[$x]); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns all cookies from the cache that are adressed to the given URL |
| 49 | + * |
| 50 | + * @param string $target_url The target-URL |
| 51 | + * @return array Numeric array conatining all matching cookies as PHPCrawlerCookieDescriptor-objects |
| 52 | + */ |
| 53 | + public function getCookiesForUrl($target_url) |
| 54 | + { |
| 55 | + $url_parts = PHPCrawlerUtils::splitURL($target_url); |
| 56 | + |
| 57 | + $target_domain = $url_parts['domain']; // e.g. acme.com |
| 58 | + |
| 59 | + $return_cookies = []; |
| 60 | + |
| 61 | + // Iterate over all cookies of this domain |
| 62 | + if (isset($this->cookies[$target_domain])) { |
| 63 | + foreach ($this->cookies[$target_domain] as $hash => $hash_value) { |
| 64 | + $Cookie = $this->cookies[$target_domain][$hash]; |
| 65 | + |
| 66 | + // Does the cookie-domain match? |
| 67 | + // Tail-matching, see http://curl.haxx.se/rfc/cookie_spec.html: |
| 68 | + // A domain attribute of "acme.com" would match host names "anvil.acme.com" as well as "shipping.crate.acme.com" |
| 69 | + // Seems like ".acme.com" should also match "anvil.acme.com", so just remove the dot |
| 70 | + |
| 71 | + $Cookie->domain = preg_replace('#^.#', '', $Cookie->domain); |
| 72 | + |
| 73 | + if ($Cookie->domain == $url_parts['host'] || preg_match('#' . preg_quote($Cookie->domain) . '$#', $url_parts['host'])) { |
| 74 | + // Does the path match? |
| 75 | + if (preg_match('#^' . preg_quote($Cookie->path) . '#', $url_parts['path'])) { |
| 76 | + $return_cookies[$Cookie->name] = $Cookie; // Use cookie-name as index to avoid double-cookies |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
77 | 81 | }
|
78 |
| - } |
| 82 | + |
| 83 | + // Convert to numeric array |
| 84 | + $return_cookies = array_values($return_cookies); |
| 85 | + |
| 86 | + return $return_cookies; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Cleans up the cache after is it not needed anymore. |
| 91 | + */ |
| 92 | + public function cleanup() |
| 93 | + { |
| 94 | + $this->cookies = []; |
79 | 95 | }
|
80 |
| - |
81 |
| - // Convert to numeric array |
82 |
| - $return_cookies = array_values($return_cookies); |
83 |
| - |
84 |
| - return $return_cookies; |
85 |
| - } |
86 |
| - |
87 |
| - /** |
88 |
| - * Cleans up the cache after is it not needed anymore. |
89 |
| - */ |
90 |
| - public function cleanup() |
91 |
| - { |
92 |
| - $this->cookies = []; |
93 |
| - } |
94 | 96 | }
|
0 commit comments