oxopenidhttpfetcher.php

Go to the documentation of this file.
00001 <?php
00002 
00003 require_once "Auth/Yadis/HTTPFetcher.php";
00004 require_once "Auth/OpenID.php";
00005 
00010 class oxOpenIdHTTPFetcher extends Auth_Yadis_ParanoidHTTPFetcher
00011 {
00020     public function get($url, $extra_headers = null)
00021     {
00022         if (!$this->canFetchURL($url)) {
00023             return null;
00024         }
00025 
00026         $stop = time() + $this->timeout;
00027         $off = $this->timeout;
00028 
00029         $redir = true;
00030 
00031         while ($redir && ($off > 0)) {
00032             $this->reset();
00033 
00034             $c = curl_init();
00035 
00036             if ($c === false) {
00037                 Auth_OpenID::log(
00038                     "curl_init returned false; could not " .
00039                     "initialize for URL '%s'", $url);
00040                 return null;
00041             }
00042 
00043             if (defined('CURLOPT_NOSIGNAL')) {
00044                 curl_setopt($c, CURLOPT_NOSIGNAL, true);
00045             }
00046 
00047             if (!$this->allowedURL($url)) {
00048                 Auth_OpenID::log("Fetching URL not allowed: %s",
00049                                  $url);
00050                 return null;
00051             }
00052 
00053             curl_setopt($c, CURLOPT_WRITEFUNCTION,
00054                         array(&$this, "_writeData"));
00055             curl_setopt($c, CURLOPT_HEADERFUNCTION,
00056                         array(&$this, "_writeHeader"));
00057 
00058             if ($extra_headers) {
00059                 curl_setopt($c, CURLOPT_HTTPHEADER, $extra_headers);
00060             }
00061 
00062             $cv = curl_version();
00063             if ( is_array( $cv ) ) {
00064                 $curl_user_agent = 'curl/'.$cv['version'];
00065             } else {
00066                 $curl_user_agent = $cv;
00067             }
00068 
00069             curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
00070             curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
00071             curl_setopt($c, CURLOPT_USERAGENT,
00072                         Auth_OpenID_USER_AGENT.' '.$curl_user_agent);
00073             curl_setopt($c, CURLOPT_TIMEOUT, $off);
00074             curl_setopt($c, CURLOPT_URL, $url);
00075 
00076             curl_exec($c);
00077 
00078             $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
00079             $body = $this->data;
00080             $headers = $this->headers;
00081 
00082             if (!$code) {
00083                 Auth_OpenID::log("Got no response code when fetching %s", $url);
00084                 Auth_OpenID::log("CURL error (%s): %s",
00085                                  curl_errno($c), curl_error($c));
00086                 return null;
00087             }
00088 
00089             if (in_array($code, array(301, 302, 303, 307))) {
00090                 $url = $this->_findRedirect($headers);
00091                 $redir = true;
00092             } else {
00093                 $redir = false;
00094                 curl_close($c);
00095 
00096                 $new_headers = array();
00097 
00098                 foreach ($headers as $header) {
00099                     if (strpos($header, ': ')) {
00100                         list($name, $value) = explode(': ', $header, 2);
00101                         $new_headers[$name] = $value;
00102                     }
00103                 }
00104 
00105                 Auth_OpenID::log(
00106                     "Successfully fetched '%s': GET response code %s",
00107                     $url, $code);
00108 
00109                 return new Auth_Yadis_HTTPResponse($url, $code,
00110                                                     $new_headers, $body);
00111             }
00112 
00113             $off = $stop - time();
00114         }
00115 
00116         return null;
00117     }
00118 
00128     public function post($url, $body, $extra_headers = null)
00129     {
00130         if (!$this->canFetchURL($url)) {
00131             return null;
00132         }
00133 
00134         $this->reset();
00135 
00136         $c = curl_init();
00137 
00138         if (defined('CURLOPT_NOSIGNAL')) {
00139             curl_setopt($c, CURLOPT_NOSIGNAL, true);
00140         }
00141 
00142         curl_setopt ($c, CURLOPT_SSL_VERIFYPEER, false);
00143         curl_setopt ($c, CURLOPT_SSL_VERIFYHOST, false);
00144         curl_setopt($c, CURLOPT_POST, true);
00145         curl_setopt($c, CURLOPT_POSTFIELDS, $body);
00146         curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
00147         curl_setopt($c, CURLOPT_URL, $url);
00148         curl_setopt($c, CURLOPT_WRITEFUNCTION,
00149                     array(&$this, "_writeData"));
00150 
00151         curl_exec($c);
00152 
00153         $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
00154 
00155         if (!$code) {
00156             Auth_OpenID::log("Got no response code when fetching %s", $url);
00157             Auth_OpenID::log("CURL error (%s): %s",
00158                              curl_errno($c), curl_error($c));
00159             return null;
00160         }
00161 
00162         $body = $this->data;
00163 
00164         curl_close($c);
00165 
00166         $new_headers = $extra_headers;
00167 
00168         foreach ($this->headers as $header) {
00169             if (strpos($header, ': ')) {
00170                 list($name, $value) = explode(': ', $header, 2);
00171                 $new_headers[$name] = $value;
00172             }
00173 
00174         }
00175 
00176         Auth_OpenID::log("Successfully fetched '%s': POST response code %s",
00177                          $url, $code);
00178 
00179         return new Auth_Yadis_HTTPResponse($url, $code, $new_headers, $body);
00180     }
00181 
00187     protected function _isSSL()
00188     {
00189         $v = curl_version();
00190         if ( is_array( $v ) ) {
00191             return in_array('https', $v['protocols']);
00192         }
00193         return false;
00194     }
00195 }