"Fossies" - the Fresh Open Source Software Archive

Member "libs/PHPCrawler/CookieCache/PHPCrawlerSQLiteCookieCache.class.php" (8 Jan 2013, 6154 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 a SQLite-db-file.
    4  *
    5  * @package phpcrawl
    6  * @internal
    7  */
    8 class PHPCrawlerSQLiteCookieCache extends PHPCrawlerCookieCacheBase
    9 {
   10   protected $PDO;
   11   
   12   protected $sqlite_db_file;
   13   
   14   public function __construct($sqlite_db_file, $create_tables = false)
   15   {
   16     $this->sqlite_db_file = $sqlite_db_file;
   17     $this->openConnection($create_tables);
   18   }
   19    
   20   /**
   21    * Adds a cookie to the cookie-cache.
   22    *
   23    * @param PHPCrawlerCookieDescriptor $Cookie The cookie to add.
   24    */
   25   public function addCookie(PHPCrawlerCookieDescriptor $Cookie)
   26   {
   27     $source_domain = $Cookie->source_domain;
   28     $cookie_domain = $Cookie->domain;
   29     $cookie_path = $Cookie->path;
   30     $cookie_name = $Cookie->name;
   31     
   32     $cookie_hash = md5($cookie_domain."_".$cookie_path."_".$cookie_name);
   33     
   34     $this->PDO->exec("INSERT OR REPLACE INTO cookies (cookie_hash, source_domain, source_url, name, value, domain, path, expires, expire_timestamp, cookie_send_time)
   35                       VALUES (".$this->PDO->quote($cookie_hash).",
   36                               ".$this->PDO->quote($Cookie->source_domain).",
   37                               ".$this->PDO->quote($Cookie->source_url).",
   38                               ".$this->PDO->quote($Cookie->name).",
   39                               ".$this->PDO->quote($Cookie->value).",
   40                               ".$this->PDO->quote($Cookie->domain).",
   41                               ".$this->PDO->quote($Cookie->path).",
   42                               ".$this->PDO->quote($Cookie->expires).",
   43                               ".$this->PDO->quote($Cookie->expire_timestamp).",
   44                               ".$this->PDO->quote($Cookie->cookie_send_time).")");
   45       
   46   }
   47   
   48   /**
   49    * Adds a bunch of cookies to the cookie-cache.
   50    *
   51    * @param array $cookies  Numeric array conatining the cookies to add as PHPCrawlerCookieDescriptor-objects
   52    */
   53   public function addCookies($cookies)
   54   {
   55     PHPCrawlerBenchmark::start("adding_cookies_to_cache");
   56     
   57     $this->PDO->exec("BEGIN EXCLUSIVE TRANSACTION");
   58     
   59     for ($x=0; $x<count($cookies); $x++)
   60     {
   61       $this->addCookie($cookies[$x]);
   62     }
   63     
   64     $this->PDO->exec("COMMIT");
   65     
   66     PHPCrawlerBenchmark::stop("adding_cookies_to_cache");
   67   }
   68   
   69   /**
   70    * Returns all cookies from the cache that are adressed to the given URL
   71    *
   72    * @param string $target_url The target-URL
   73    * @return array  Numeric array conatining all matching cookies as PHPCrawlerCookieDescriptor-objects
   74    */
   75   public function getCookiesForUrl($target_url)
   76   {
   77     PHPCrawlerBenchmark::start("getting_cookies_from_cache");
   78     
   79     $url_parts = PHPCrawlerUtils::splitURL($target_url);
   80     
   81     $return_cookies = array();
   82 
   83     $Result = $this->PDO->query("SELECT * FROM cookies WHERE source_domain = '".$url_parts["domain"]."';");
   84     $rows = $Result->fetchAll(PDO::FETCH_ASSOC);
   85     $Result->closeCursor();
   86     
   87     $cnt = count($rows);
   88     for ($x=0; $x<$cnt; $x++)
   89     {
   90       // Does the cookie-domain match?
   91       // Tail-matching, see http://curl.haxx.se/rfc/cookie_spec.html:
   92       // A domain attribute of "acme.com" would match host names "anvil.acme.com" as well as "shipping.crate.acme.com"
   93       if ($rows[$x]["domain"] == $url_parts["host"] || preg_match("#".preg_quote($rows[$x]["domain"])."$#", $url_parts["host"]))
   94       {
   95         // Does the path match?
   96         if (preg_match("#^".preg_quote($rows[$x]["path"])."#", $url_parts["path"]))
   97         {
   98           $Cookie = new PHPCrawlerCookieDescriptor($rows[$x]["source_url"], $rows[$x]["name"], $rows[$x]["value"], $rows[$x]["expires"], $rows[$x]["path"], $rows[$x]["domain"]);
   99           $return_cookies[$Cookie->name] = $Cookie; // Use cookie-name as index to avoid double-cookies
  100         }
  101       }
  102     }
  103     
  104     // Convert to numeric array
  105     $return_cookies = array_values($return_cookies);
  106     
  107     PHPCrawlerBenchmark::stop("getting_cookies_from_cache");
  108     
  109     return $return_cookies;
  110   }
  111   
  112    /**
  113    * Creates the sqlite-db-file and opens connection to it.
  114    *
  115    * @param bool $create_tables Defines whether all necessary tables should be created
  116    */
  117   protected function openConnection($create_tables = false)
  118   {
  119     //PHPCrawlerBenchmark::start("Connecting to SQLite-cache-db");
  120     
  121     // Open sqlite-file
  122     try
  123     {
  124       $this->PDO = new PDO("sqlite:".$this->sqlite_db_file);
  125     }
  126     catch (Exception $e)
  127     {
  128       throw new Exception("Error creating SQLite-cache-file, ".$e->getMessage().", try installing sqlite3-extension for PHP.");
  129     }
  130     
  131     $this->PDO->exec("PRAGMA journal_mode = OFF");
  132     
  133     $this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  134     $this->PDO->setAttribute(PDO::ATTR_TIMEOUT, 100);
  135     
  136     if ($create_tables == true)
  137     {
  138       // Create url-table (if not exists)
  139       $this->PDO->exec("CREATE TABLE IF NOT EXISTS cookies (id integer PRIMARY KEY AUTOINCREMENT,
  140                                                             cookie_hash TEXT UNIQUE,
  141                                                             source_domain TEXT,
  142                                                             source_url TEXT,
  143                                                             name TEXT,
  144                                                             value TEXT,
  145                                                             domain TEXT,
  146                                                             path TEXT,
  147                                                             expires TEXT,
  148                                                             expire_timestamp INTEGER,
  149                                                             cookie_send_time INTEGER);");
  150       
  151       // Create indexes (seems that indexes make the whole thingy slower)
  152       $this->PDO->exec("CREATE INDEX IF NOT EXISTS cookie_hash ON cookies (cookie_hash);");
  153       
  154       $this->PDO->exec("ANALYZE;");
  155     }
  156     
  157     //PHPCrawlerBenchmark::stop("Connecting to SQLite-cache-db");
  158   }
  159 }
  160 ?>