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 curl_setopt($c, CURLOPT_RANGE,
00076 "0-".(1024 * Auth_OpenID_FETCHER_MAX_RESPONSE_KB));
00077
00078 curl_exec($c);
00079
00080 $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
00081 $body = $this->data;
00082 $headers = $this->headers;
00083
00084 if (!$code) {
00085 Auth_OpenID::log("Got no response code when fetching %s", $url);
00086 Auth_OpenID::log("CURL error (%s): %s",
00087 curl_errno($c), curl_error($c));
00088 return null;
00089 }
00090
00091 if (in_array($code, array(301, 302, 303, 307))) {
00092 $url = $this->_findRedirect($headers);
00093 $redir = true;
00094 } else {
00095 $redir = false;
00096 curl_close($c);
00097
00098 $new_headers = array();
00099
00100 foreach ($headers as $header) {
00101 if (strpos($header, ': ')) {
00102 list($name, $value) = explode(': ', $header, 2);
00103 $new_headers[$name] = $value;
00104 }
00105 }
00106
00107 Auth_OpenID::log(
00108 "Successfully fetched '%s': GET response code %s",
00109 $url, $code);
00110
00111 return new Auth_Yadis_HTTPResponse($url, $code,
00112 $new_headers, $body);
00113 }
00114
00115 $off = $stop - time();
00116 }
00117
00118 return null;
00119 }
00120
00130 public function post($url, $body, $extra_headers = null)
00131 {
00132 if (!$this->canFetchURL($url)) {
00133 return null;
00134 }
00135
00136 $this->reset();
00137
00138 $c = curl_init();
00139
00140 if (defined('CURLOPT_NOSIGNAL')) {
00141 curl_setopt($c, CURLOPT_NOSIGNAL, true);
00142 }
00143
00144 curl_setopt ($c, CURLOPT_SSL_VERIFYPEER, false);
00145 curl_setopt ($c, CURLOPT_SSL_VERIFYHOST, false);
00146 curl_setopt($c, CURLOPT_POST, true);
00147 curl_setopt($c, CURLOPT_POSTFIELDS, $body);
00148 curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
00149 curl_setopt($c, CURLOPT_URL, $url);
00150 curl_setopt($c, CURLOPT_WRITEFUNCTION,
00151 array(&$this, "_writeData"));
00152
00153 curl_exec($c);
00154
00155 $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
00156
00157 if (!$code) {
00158 Auth_OpenID::log("Got no response code when fetching %s", $url);
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,
00180 $new_headers, $body);
00181 }
00182
00188 protected function _isSSL()
00189 {
00190 $v = curl_version();
00191 if ( is_array( $v ) ) {
00192 return in_array('https', $v['protocols']);
00193 }
00194 return false;
00195 }
00196 }