"Fossies" - the Fresh Open Source Software Archive

Member "libs/PHPCrawler/PHPCrawlerUserSendDataCache.class.php" (8 Jan 2013, 3838 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  * Cache for storing user-data to send with requests, like cookies, post-data
    4  * and basic-authentications.
    5  *
    6  * @package phpcrawl
    7  * @internal
    8  */
    9 class PHPCrawlerUserSendDataCache
   10 {
   11   /**
   12    * Array containing basic-authentications to send.
   13    *
   14    * @var array
   15    */
   16   protected $basic_authentications = array();
   17   
   18   /**
   19    * Array containing post-data to send.
   20    *
   21    * @var array
   22    */
   23   protected $post_data = array();
   24   
   25   /**
   26    * Adds post-data together with an URL-regex to the list of post-data to send with requests.
   27    *
   28    * @param string $url_regex       Regular expression defining the URL(s) the post-data should be send to.
   29    * @param array  $post_data_array Post-data-array, the keys are the post-data-keys, the values the post-values.
   30    *                                (like array("key1" => "value1", "key2" => "value2")
   31    */
   32   public function addPostData($url_regex, $post_data_array)
   33   {
   34     // Check regex
   35     $regex_okay = PHPCrawlerUtils::checkRegexPattern($url_regex);
   36     
   37     if ($regex_okay == true)
   38     {
   39       @reset($post_data_array);
   40       while (list($key, $value) = @each($post_data_array))
   41       {  
   42         // Add data to post_data-array
   43         $tmp = array();
   44         $tmp["url_regex"] = $url_regex;
   45         $tmp["key"] = $key;
   46         $tmp["value"] = $value;
   47       
   48         $this->post_data[] = $tmp;
   49       }
   50       
   51       return true;
   52     }
   53     else return false;
   54   }
   55   
   56   /**
   57    * Returns the post-data (key and value) that should be send to the given URL.
   58    *
   59    * @param string $url The URL.
   60    * @return array Array containing the post_keys as keys and the values as values.
   61    *               (like array("key1" => "value1", "key2" => "value2")
   62    */
   63   public function getPostDataForUrl($url)
   64   {
   65     $post_data_array = array();
   66     
   67     $cnt = count($this->post_data);
   68     for ($x=0; $x<$cnt; $x++)
   69     {
   70       if (preg_match($this->post_data[$x]["url_regex"], $url))
   71       {
   72         $post_data_array[$this->post_data[$x]["key"]] = $this->post_data[$x]["value"];
   73       }
   74     }
   75     
   76     return $post_data_array;
   77   }
   78   
   79   /**
   80    * Adds a basic-authentication (username and password) to the list of authentications that will be send
   81    * with requests.
   82    *
   83    * @param string $url_regex Regular expression defining the URL(s) the authentication should be send to.
   84    * @param string $username  The username
   85    * @param string $password  The password
   86    *
   87    * @return bool
   88    */
   89   public function addBasicAuthentication($url_regex, $username, $password)
   90   {
   91     // Check regex
   92     $regex_okay = PHPCrawlerUtils::checkRegexPattern($url_regex);
   93     
   94     if ($regex_okay == true)
   95     {
   96       // Add authentication to basic_authentications-array
   97       $tmp = array();
   98       $tmp["url_regex"] = $url_regex;
   99       $tmp["username"] = $username;
  100       $tmp["password"] = $password;
  101       
  102       $this->basic_authentications[] = $tmp;
  103       return true;
  104     }
  105     else return false;
  106   }
  107   
  108   /**
  109    * Returns the basic-authentication (username and password) that should be send to the given URL.
  110    *
  111    * @param string $url The URL.
  112    * @return array Array containing the keys "username" and "password".
  113    *               Returns NULL if no authentication was found in cache for the given URL.
  114    */
  115   public function getBasicAuthenticationForUrl($url)
  116   {
  117     for ($x=0; $x<count($this->basic_authentications); $x++)
  118     {
  119       if (preg_match($this->basic_authentications[$x]["url_regex"], $url))
  120       {
  121         $tmp = array();
  122         $tmp["username"] = $this->basic_authentications[$x]["username"];
  123         $tmp["password"] = $this->basic_authentications[$x]["password"];
  124         
  125         return $tmp;
  126       }
  127     }
  128     
  129     return null;
  130   }
  131 }
  132 ?>