[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/openid/Auth/Yadis/ -> ParanoidHTTPFetcher.php (source)

   1  <?php
   2  
   3  /**
   4   * This module contains the CURL-based HTTP fetcher implementation.
   5   *
   6   * PHP versions 4 and 5
   7   *
   8   * LICENSE: See the COPYING file included in this distribution.
   9   *
  10   * @package OpenID
  11   * @author JanRain, Inc. <openid@janrain.com>
  12   * @copyright 2005-2008 Janrain, Inc.
  13   * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  14   */
  15  
  16  // Do not allow direct access
  17  defined( '_JEXEC' ) or die( 'Restricted access' );
  18  
  19  /**
  20   * Interface import
  21   */
  22  require_once "Auth/Yadis/HTTPFetcher.php";
  23  
  24  require_once "Auth/OpenID.php";
  25  
  26  /**
  27   * A paranoid {@link Auth_Yadis_HTTPFetcher} class which uses CURL
  28   * for fetching.
  29   *
  30   * @package OpenID
  31   */
  32  class Auth_Yadis_ParanoidHTTPFetcher extends Auth_Yadis_HTTPFetcher {
  33      function Auth_Yadis_ParanoidHTTPFetcher()
  34      {
  35          $this->reset();
  36      }
  37  
  38      function reset()
  39      {
  40          $this->headers = array();
  41          $this->data = "";
  42      }
  43  
  44      /**
  45       * @access private
  46       */
  47      function _writeHeader($ch, $header)
  48      {
  49          array_push($this->headers, rtrim($header));
  50          return strlen($header);
  51      }
  52  
  53      /**
  54       * @access private
  55       */
  56      function _writeData($ch, $data)
  57      {
  58          if (strlen($this->data) > 1024*Auth_OpenID_FETCHER_MAX_RESPONSE_KB) {
  59              return 0;
  60          } else {
  61              $this->data .= $data;
  62              return strlen($data);
  63          }
  64      }
  65  
  66      /**
  67       * Does this fetcher support SSL URLs?
  68       */
  69      function supportsSSL()
  70      {
  71          $v = curl_version();
  72          if(is_array($v)) {
  73              return in_array('https', $v['protocols']);
  74          } elseif (is_string($v)) {
  75              return preg_match('/OpenSSL/i', $v);
  76          } else {
  77              return 0;
  78          }
  79      }
  80  
  81      function get($url, $extra_headers = null)
  82      {
  83          if (!$this->canFetchURL($url)) {
  84              return null;
  85          }
  86  
  87          $stop = time() + $this->timeout;
  88          $off = $this->timeout;
  89  
  90          $redir = true;
  91  
  92          while ($redir && ($off > 0)) {
  93              $this->reset();
  94  
  95              $c = curl_init();
  96  
  97              if ($c === false) {
  98                  Auth_OpenID::log(
  99                      "curl_init returned false; could not " .
 100                      "initialize for URL '%s'", $url);
 101                  return null;
 102              }
 103  
 104              if (defined('CURLOPT_NOSIGNAL')) {
 105                  curl_setopt($c, CURLOPT_NOSIGNAL, true);
 106              }
 107  
 108              if (!$this->allowedURL($url)) {
 109                  Auth_OpenID::log("Fetching URL not allowed: %s",
 110                                   $url);
 111                  return null;
 112              }
 113  
 114              curl_setopt($c, CURLOPT_WRITEFUNCTION,
 115                          array(&$this, "_writeData"));
 116              curl_setopt($c, CURLOPT_HEADERFUNCTION,
 117                          array(&$this, "_writeHeader"));
 118  
 119              if ($extra_headers) {
 120                  curl_setopt($c, CURLOPT_HTTPHEADER, $extra_headers);
 121              }
 122  
 123              $cv = curl_version();
 124              if(is_array($cv)) {
 125                $curl_user_agent = 'curl/'.$cv['version'];
 126              } else {
 127                $curl_user_agent = $cv;
 128              }
 129              curl_setopt($c, CURLOPT_USERAGENT,
 130                          Auth_OpenID_USER_AGENT.' '.$curl_user_agent);
 131              curl_setopt($c, CURLOPT_TIMEOUT, $off);
 132              curl_setopt($c, CURLOPT_URL, $url);
 133              curl_setopt($c, CURLOPT_RANGE, 
 134                          "0-".(1024 * Auth_OpenID_FETCHER_MAX_RESPONSE_KB));
 135  
 136              curl_exec($c);
 137  
 138              $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
 139              $body = $this->data;
 140              $headers = $this->headers;
 141  
 142              if (!$code) {
 143                  Auth_OpenID::log("Got no response code when fetching %s", $url);
 144                  Auth_OpenID::log("CURL error (%s): %s",
 145                                   curl_errno($c), curl_error($c));
 146                  return null;
 147              }
 148  
 149              if (in_array($code, array(301, 302, 303, 307))) {
 150                  $url = $this->_findRedirect($headers);
 151                  $redir = true;
 152              } else {
 153                  $redir = false;
 154                  curl_close($c);
 155  
 156                  $new_headers = array();
 157  
 158                  foreach ($headers as $header) {
 159                      if (strpos($header, ': ')) {
 160                          list($name, $value) = explode(': ', $header, 2);
 161                          $new_headers[$name] = $value;
 162                      }
 163                  }
 164  
 165                  Auth_OpenID::log(
 166                      "Successfully fetched '%s': GET response code %s",
 167                      $url, $code);
 168  
 169                  return new Auth_Yadis_HTTPResponse($url, $code,
 170                                                      $new_headers, $body);
 171              }
 172  
 173              $off = $stop - time();
 174          }
 175  
 176          return null;
 177      }
 178  
 179      function post($url, $body, $extra_headers = null)
 180      {
 181          if (!$this->canFetchURL($url)) {
 182              return null;
 183          }
 184  
 185          $this->reset();
 186  
 187          $c = curl_init();
 188  
 189          if (defined('CURLOPT_NOSIGNAL')) {
 190              curl_setopt($c, CURLOPT_NOSIGNAL, true);
 191          }
 192  
 193          curl_setopt($c, CURLOPT_POST, true);
 194          curl_setopt($c, CURLOPT_POSTFIELDS, $body);
 195          curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
 196          curl_setopt($c, CURLOPT_URL, $url);
 197          curl_setopt($c, CURLOPT_WRITEFUNCTION,
 198                      array(&$this, "_writeData"));
 199  
 200          curl_exec($c);
 201  
 202          $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
 203  
 204          if (!$code) {
 205              Auth_OpenID::log("Got no response code when fetching %s", $url);
 206              return null;
 207          }
 208  
 209          $body = $this->data;
 210  
 211          curl_close($c);
 212  
 213          $new_headers = $extra_headers;
 214  
 215          foreach ($this->headers as $header) {
 216              if (strpos($header, ': ')) {
 217                  list($name, $value) = explode(': ', $header, 2);
 218                  $new_headers[$name] = $value;
 219              }
 220  
 221          }
 222  
 223          Auth_OpenID::log("Successfully fetched '%s': POST response code %s",
 224                           $url, $code);
 225  
 226          return new Auth_Yadis_HTTPResponse($url, $code,
 227                                             $new_headers, $body);
 228      }
 229  }
 230  
 231  ?>


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1