[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * This module contains the plain non-curl HTTP fetcher
   5   * implementation.
   6   *
   7   * PHP versions 4 and 5
   8   *
   9   * LICENSE: See the COPYING file included in this distribution.
  10   *
  11   * @package OpenID
  12   * @author JanRain, Inc. <openid@janrain.com>
  13   * @copyright 2005-2008 Janrain, Inc.
  14   * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  15   */
  16  
  17  // Do not allow direct access
  18  defined( '_JEXEC' ) or die( 'Restricted access' );
  19  
  20  /**
  21   * Interface import
  22   */
  23  require_once "Auth/Yadis/HTTPFetcher.php";
  24  
  25  /**
  26   * This class implements a plain, hand-built socket-based fetcher
  27   * which will be used in the event that CURL is unavailable.
  28   *
  29   * @package OpenID
  30   */
  31  class Auth_Yadis_PlainHTTPFetcher extends Auth_Yadis_HTTPFetcher {
  32      /**
  33       * Does this fetcher support SSL URLs?
  34       */
  35      function supportsSSL()
  36      {
  37          return function_exists('openssl_open');
  38      }
  39  
  40      function get($url, $extra_headers = null)
  41      {
  42          if (!$this->canFetchURL($url)) {
  43              return null;
  44          }
  45  
  46          $redir = true;
  47  
  48          $stop = time() + $this->timeout;
  49          $off = $this->timeout;
  50  
  51          while ($redir && ($off > 0)) {
  52  
  53              $parts = parse_url($url);
  54  
  55              $specify_port = true;
  56  
  57              // Set a default port.
  58              if (!array_key_exists('port', $parts)) {
  59                  $specify_port = false;
  60                  if ($parts['scheme'] == 'http') {
  61                      $parts['port'] = 80;
  62                  } elseif ($parts['scheme'] == 'https') {
  63                      $parts['port'] = 443;
  64                  } else {
  65                      return null;
  66                  }
  67              }
  68  
  69              if (!array_key_exists('path', $parts)) {
  70                  $parts['path'] = '/';
  71              }
  72  
  73              $host = $parts['host'];
  74  
  75              if ($parts['scheme'] == 'https') {
  76                  $host = 'ssl://' . $host;
  77              }
  78  
  79              $user_agent = Auth_OpenID_USER_AGENT;
  80  
  81              $headers = array(
  82                               "GET ".$parts['path'].
  83                               (array_key_exists('query', $parts) ?
  84                                "?".$parts['query'] : "").
  85                                   " HTTP/1.0",
  86                               "User-Agent: $user_agent",
  87                               "Host: ".$parts['host'].
  88                                  ($specify_port ? ":".$parts['port'] : ""),
  89                               "Range: 0-".
  90                                  (1024*Auth_OpenID_FETCHER_MAX_RESPONSE_KB),
  91                               "Port: ".$parts['port']);
  92  
  93              $errno = 0;
  94              $errstr = '';
  95  
  96              if ($extra_headers) {
  97                  foreach ($extra_headers as $h) {
  98                      $headers[] = $h;
  99                  }
 100              }
 101  
 102              @$sock = fsockopen($host, $parts['port'], $errno, $errstr,
 103                                 $this->timeout);
 104              if ($sock === false) {
 105                  return false;
 106              }
 107  
 108              stream_set_timeout($sock, $this->timeout);
 109  
 110              fputs($sock, implode("\r\n", $headers) . "\r\n\r\n");
 111  
 112              $data = "";
 113              $kilobytes = 0;
 114              while (!feof($sock) &&
 115                     $kilobytes < Auth_OpenID_FETCHER_MAX_RESPONSE_KB ) {
 116                  $data .= fgets($sock, 1024);
 117                  $kilobytes += 1;
 118              }
 119  
 120              fclose($sock);
 121  
 122              // Split response into header and body sections
 123              list($headers, $body) = explode("\r\n\r\n", $data, 2);
 124              $headers = explode("\r\n", $headers);
 125  
 126              $http_code = explode(" ", $headers[0]);
 127              $code = $http_code[1];
 128  
 129              if (in_array($code, array('301', '302'))) {
 130                  $url = $this->_findRedirect($headers);
 131                  $redir = true;
 132              } else {
 133                  $redir = false;
 134              }
 135  
 136              $off = $stop - time();
 137          }
 138  
 139          $new_headers = array();
 140  
 141          foreach ($headers as $header) {
 142              if (preg_match("/:/", $header)) {
 143                  $parts = explode(": ", $header, 2);
 144  
 145                  if (count($parts) == 2) {
 146                      list($name, $value) = $parts;
 147                      $new_headers[$name] = $value;
 148                  }
 149              }
 150  
 151          }
 152  
 153          return new Auth_Yadis_HTTPResponse($url, $code, $new_headers, $body);
 154      }
 155  
 156      function post($url, $body, $extra_headers = null)
 157      {
 158          if (!$this->canFetchURL($url)) {
 159              return null;
 160          }
 161  
 162          $parts = parse_url($url);
 163  
 164          $headers = array();
 165  
 166          $post_path = $parts['path'];
 167          if (isset($parts['query'])) {
 168              $post_path .= '?' . $parts['query'];
 169          }
 170  
 171          $headers[] = "POST ".$post_path." HTTP/1.0";
 172          $headers[] = "Host: " . $parts['host'];
 173          $headers[] = "Content-type: application/x-www-form-urlencoded";
 174          $headers[] = "Content-length: " . strval(strlen($body));
 175  
 176          if ($extra_headers &&
 177              is_array($extra_headers)) {
 178              $headers = array_merge($headers, $extra_headers);
 179          }
 180  
 181          // Join all headers together.
 182          $all_headers = implode("\r\n", $headers);
 183  
 184          // Add headers, two newlines, and request body.
 185          $request = $all_headers . "\r\n\r\n" . $body;
 186  
 187          // Set a default port.
 188          if (!array_key_exists('port', $parts)) {
 189              if ($parts['scheme'] == 'http') {
 190                  $parts['port'] = 80;
 191              } elseif ($parts['scheme'] == 'https') {
 192                  $parts['port'] = 443;
 193              } else {
 194                  return null;
 195              }
 196          }
 197  
 198          if ($parts['scheme'] == 'https') {
 199              $parts['host'] = sprintf("ssl://%s", $parts['host']);
 200          }
 201  
 202          // Connect to the remote server.
 203          $errno = 0;
 204          $errstr = '';
 205  
 206          $sock = fsockopen($parts['host'], $parts['port'], $errno, $errstr,
 207                            $this->timeout);
 208  
 209          if ($sock === false) {
 210              return null;
 211          }
 212  
 213          stream_set_timeout($sock, $this->timeout);
 214  
 215          // Write the POST request.
 216          fputs($sock, $request);
 217  
 218          // Get the response from the server.
 219          $response = "";
 220          while (!feof($sock)) {
 221              if ($data = fgets($sock, 128)) {
 222                  $response .= $data;
 223              } else {
 224                  break;
 225              }
 226          }
 227  
 228          // Split the request into headers and body.
 229          list($headers, $response_body) = explode("\r\n\r\n", $response, 2);
 230  
 231          $headers = explode("\r\n", $headers);
 232  
 233          // Expect the first line of the headers data to be something
 234          // like HTTP/1.1 200 OK.  Split the line on spaces and take
 235          // the second token, which should be the return code.
 236          $http_code = explode(" ", $headers[0]);
 237          $code = $http_code[1];
 238  
 239          $new_headers = array();
 240  
 241          foreach ($headers as $header) {
 242              if (preg_match("/:/", $header)) {
 243                  list($name, $value) = explode(": ", $header, 2);
 244                  $new_headers[$name] = $value;
 245              }
 246  
 247          }
 248  
 249          return new Auth_Yadis_HTTPResponse($url, $code,
 250                                             $new_headers, $response_body);
 251      }
 252  }
 253  
 254  ?>


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