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