"Fossies" - the Fresh Open Source Software Archive

Member "libs/PHPCrawler/CookieCache/PHPCrawlerMemoryCookieCache.class.php" (8 Jan 2013, 2502 Bytes) of package /linux/www/SitemapCreator.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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