00001 <?php 00002 00019 require_once "Auth/Yadis/HTTPFetcher.php"; 00020 00021 require_once "Auth/OpenID.php"; 00022 00029 class Auth_Yadis_ParanoidHTTPFetcher extends Auth_Yadis_HTTPFetcher { 00030 function Auth_Yadis_ParanoidHTTPFetcher() 00031 { 00032 $this->reset(); 00033 } 00034 00035 function reset() 00036 { 00037 $this->headers = array(); 00038 $this->data = ""; 00039 } 00040 00044 function _writeHeader($ch, $header) 00045 { 00046 array_push($this->headers, rtrim($header)); 00047 return strlen($header); 00048 } 00049 00053 function _writeData($ch, $data) 00054 { 00055 if (strlen($this->data) > 1024*Auth_OpenID_FETCHER_MAX_RESPONSE_KB) { 00056 return 0; 00057 } else { 00058 $this->data .= $data; 00059 return strlen($data); 00060 } 00061 } 00062 00066 function supportsSSL() 00067 { 00068 $v = curl_version(); 00069 if(is_array($v)) { 00070 return in_array('https', $v['protocols']); 00071 } elseif (is_string($v)) { 00072 return preg_match('/OpenSSL/i', $v); 00073 } else { 00074 return 0; 00075 } 00076 } 00077 00078 function get($url, $extra_headers = null) 00079 { 00080 if (!$this->canFetchURL($url)) { 00081 return null; 00082 } 00083 00084 $stop = time() + $this->timeout; 00085 $off = $this->timeout; 00086 00087 $redir = true; 00088 00089 while ($redir && ($off > 0)) { 00090 $this->reset(); 00091 00092 $c = curl_init(); 00093 00094 if ($c === false) { 00095 Auth_OpenID::log( 00096 "curl_init returned false; could not " . 00097 "initialize for URL '%s'", $url); 00098 return null; 00099 } 00100 00101 if (defined('CURLOPT_NOSIGNAL')) { 00102 curl_setopt($c, CURLOPT_NOSIGNAL, true); 00103 } 00104 00105 if (!$this->allowedURL($url)) { 00106 Auth_OpenID::log("Fetching URL not allowed: %s", 00107 $url); 00108 return null; 00109 } 00110 00111 curl_setopt($c, CURLOPT_WRITEFUNCTION, 00112 array(&$this, "_writeData")); 00113 curl_setopt($c, CURLOPT_HEADERFUNCTION, 00114 array(&$this, "_writeHeader")); 00115 00116 if ($extra_headers) { 00117 curl_setopt($c, CURLOPT_HTTPHEADER, $extra_headers); 00118 } 00119 00120 $cv = curl_version(); 00121 if(is_array($cv)) { 00122 $curl_user_agent = 'curl/'.$cv['version']; 00123 } else { 00124 $curl_user_agent = $cv; 00125 } 00126 curl_setopt($c, CURLOPT_USERAGENT, 00127 Auth_OpenID_USER_AGENT.' '.$curl_user_agent); 00128 curl_setopt($c, CURLOPT_TIMEOUT, $off); 00129 curl_setopt($c, CURLOPT_URL, $url); 00130 curl_setopt($c, CURLOPT_RANGE, 00131 "0-".(1024 * Auth_OpenID_FETCHER_MAX_RESPONSE_KB)); 00132 00133 curl_exec($c); 00134 00135 $code = curl_getinfo($c, CURLINFO_HTTP_CODE); 00136 $body = $this->data; 00137 $headers = $this->headers; 00138 00139 if (!$code) { 00140 Auth_OpenID::log("Got no response code when fetching %s", $url); 00141 Auth_OpenID::log("CURL error (%s): %s", 00142 curl_errno($c), curl_error($c)); 00143 return null; 00144 } 00145 00146 if (in_array($code, array(301, 302, 303, 307))) { 00147 $url = $this->_findRedirect($headers); 00148 $redir = true; 00149 } else { 00150 $redir = false; 00151 curl_close($c); 00152 00153 $new_headers = array(); 00154 00155 foreach ($headers as $header) { 00156 if (strpos($header, ': ')) { 00157 list($name, $value) = explode(': ', $header, 2); 00158 $new_headers[$name] = $value; 00159 } 00160 } 00161 00162 Auth_OpenID::log( 00163 "Successfully fetched '%s': GET response code %s", 00164 $url, $code); 00165 00166 return new Auth_Yadis_HTTPResponse($url, $code, 00167 $new_headers, $body); 00168 } 00169 00170 $off = $stop - time(); 00171 } 00172 00173 return null; 00174 } 00175 00176 function post($url, $body, $extra_headers = null) 00177 { 00178 if (!$this->canFetchURL($url)) { 00179 return null; 00180 } 00181 00182 $this->reset(); 00183 00184 $c = curl_init(); 00185 00186 if (defined('CURLOPT_NOSIGNAL')) { 00187 curl_setopt($c, CURLOPT_NOSIGNAL, true); 00188 } 00189 00190 curl_setopt($c, CURLOPT_POST, true); 00191 curl_setopt($c, CURLOPT_POSTFIELDS, $body); 00192 curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout); 00193 curl_setopt($c, CURLOPT_URL, $url); 00194 curl_setopt($c, CURLOPT_WRITEFUNCTION, 00195 array(&$this, "_writeData")); 00196 00197 curl_exec($c); 00198 00199 $code = curl_getinfo($c, CURLINFO_HTTP_CODE); 00200 00201 if (!$code) { 00202 Auth_OpenID::log("Got no response code when fetching %s", $url); 00203 return null; 00204 } 00205 00206 $body = $this->data; 00207 00208 curl_close($c); 00209 00210 $new_headers = $extra_headers; 00211 00212 foreach ($this->headers as $header) { 00213 if (strpos($header, ': ')) { 00214 list($name, $value) = explode(': ', $header, 2); 00215 $new_headers[$name] = $value; 00216 } 00217 00218 } 00219 00220 Auth_OpenID::log("Successfully fetched '%s': POST response code %s", 00221 $url, $code); 00222 00223 return new Auth_Yadis_HTTPResponse($url, $code, 00224 $new_headers, $body); 00225 } 00226 } 00227 00228 ?>