|
TYPO3 API
SVNRelease
|
00001 <?php 00002 00003 /** 00004 * This module contains the plain non-curl HTTP fetcher 00005 * implementation. 00006 * 00007 * PHP versions 4 and 5 00008 * 00009 * LICENSE: See the COPYING file included in this distribution. 00010 * 00011 * @package OpenID 00012 * @author JanRain, Inc. <openid@janrain.com> 00013 * @copyright 2005-2008 Janrain, Inc. 00014 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache 00015 */ 00016 00017 /** 00018 * Interface import 00019 */ 00020 require_once "Auth/Yadis/HTTPFetcher.php"; 00021 00022 /** 00023 * This class implements a plain, hand-built socket-based fetcher 00024 * which will be used in the event that CURL is unavailable. 00025 * 00026 * @package OpenID 00027 */ 00028 class Auth_Yadis_PlainHTTPFetcher extends Auth_Yadis_HTTPFetcher { 00029 /** 00030 * Does this fetcher support SSL URLs? 00031 */ 00032 function supportsSSL() 00033 { 00034 return function_exists('openssl_open'); 00035 } 00036 00037 function get($url, $extra_headers = null) 00038 { 00039 if (!$this->canFetchURL($url)) { 00040 return null; 00041 } 00042 00043 $redir = true; 00044 00045 $stop = time() + $this->timeout; 00046 $off = $this->timeout; 00047 00048 while ($redir && ($off > 0)) { 00049 00050 $parts = parse_url($url); 00051 00052 $specify_port = true; 00053 00054 // Set a default port. 00055 if (!array_key_exists('port', $parts)) { 00056 $specify_port = false; 00057 if ($parts['scheme'] == 'http') { 00058 $parts['port'] = 80; 00059 } elseif ($parts['scheme'] == 'https') { 00060 $parts['port'] = 443; 00061 } else { 00062 return null; 00063 } 00064 } 00065 00066 if (!array_key_exists('path', $parts)) { 00067 $parts['path'] = '/'; 00068 } 00069 00070 $host = $parts['host']; 00071 00072 if ($parts['scheme'] == 'https') { 00073 $host = 'ssl://' . $host; 00074 } 00075 00076 $user_agent = Auth_OpenID_USER_AGENT; 00077 00078 $headers = array( 00079 "GET ".$parts['path']. 00080 (array_key_exists('query', $parts) ? 00081 "?".$parts['query'] : ""). 00082 " HTTP/1.0", 00083 "User-Agent: $user_agent", 00084 "Host: ".$parts['host']. 00085 ($specify_port ? ":".$parts['port'] : ""), 00086 "Range: 0-". 00087 (1024*Auth_OpenID_FETCHER_MAX_RESPONSE_KB), 00088 "Port: ".$parts['port']); 00089 00090 $errno = 0; 00091 $errstr = ''; 00092 00093 if ($extra_headers) { 00094 foreach ($extra_headers as $h) { 00095 $headers[] = $h; 00096 } 00097 } 00098 00099 @$sock = fsockopen($host, $parts['port'], $errno, $errstr, 00100 $this->timeout); 00101 if ($sock === false) { 00102 return false; 00103 } 00104 00105 stream_set_timeout($sock, $this->timeout); 00106 00107 fputs($sock, implode("\r\n", $headers) . "\r\n\r\n"); 00108 00109 $data = ""; 00110 $kilobytes = 0; 00111 while (!feof($sock) && 00112 $kilobytes < Auth_OpenID_FETCHER_MAX_RESPONSE_KB ) { 00113 $data .= fgets($sock, 1024); 00114 $kilobytes += 1; 00115 } 00116 00117 fclose($sock); 00118 00119 // Split response into header and body sections 00120 list($headers, $body) = explode("\r\n\r\n", $data, 2); 00121 $headers = explode("\r\n", $headers); 00122 00123 $http_code = explode(" ", $headers[0]); 00124 $code = $http_code[1]; 00125 00126 if (in_array($code, array('301', '302'))) { 00127 $url = $this->_findRedirect($headers); 00128 $redir = true; 00129 } else { 00130 $redir = false; 00131 } 00132 00133 $off = $stop - time(); 00134 } 00135 00136 $new_headers = array(); 00137 00138 foreach ($headers as $header) { 00139 if (preg_match("/:/", $header)) { 00140 $parts = explode(": ", $header, 2); 00141 00142 if (count($parts) == 2) { 00143 list($name, $value) = $parts; 00144 $new_headers[$name] = $value; 00145 } 00146 } 00147 00148 } 00149 00150 return new Auth_Yadis_HTTPResponse($url, $code, $new_headers, $body); 00151 } 00152 00153 function post($url, $body, $extra_headers = null) 00154 { 00155 if (!$this->canFetchURL($url)) { 00156 return null; 00157 } 00158 00159 $parts = parse_url($url); 00160 00161 $headers = array(); 00162 00163 $post_path = $parts['path']; 00164 if (isset($parts['query'])) { 00165 $post_path .= '?' . $parts['query']; 00166 } 00167 00168 $headers[] = "POST ".$post_path." HTTP/1.0"; 00169 $headers[] = "Host: " . $parts['host']; 00170 $headers[] = "Content-type: application/x-www-form-urlencoded"; 00171 $headers[] = "Content-length: " . strval(strlen($body)); 00172 00173 if ($extra_headers && 00174 is_array($extra_headers)) { 00175 $headers = array_merge($headers, $extra_headers); 00176 } 00177 00178 // Join all headers together. 00179 $all_headers = implode("\r\n", $headers); 00180 00181 // Add headers, two newlines, and request body. 00182 $request = $all_headers . "\r\n\r\n" . $body; 00183 00184 // Set a default port. 00185 if (!array_key_exists('port', $parts)) { 00186 if ($parts['scheme'] == 'http') { 00187 $parts['port'] = 80; 00188 } elseif ($parts['scheme'] == 'https') { 00189 $parts['port'] = 443; 00190 } else { 00191 return null; 00192 } 00193 } 00194 00195 if ($parts['scheme'] == 'https') { 00196 $parts['host'] = sprintf("ssl://%s", $parts['host']); 00197 } 00198 00199 // Connect to the remote server. 00200 $errno = 0; 00201 $errstr = ''; 00202 00203 $sock = fsockopen($parts['host'], $parts['port'], $errno, $errstr, 00204 $this->timeout); 00205 00206 if ($sock === false) { 00207 return null; 00208 } 00209 00210 stream_set_timeout($sock, $this->timeout); 00211 00212 // Write the POST request. 00213 fputs($sock, $request); 00214 00215 // Get the response from the server. 00216 $response = ""; 00217 while (!feof($sock)) { 00218 if ($data = fgets($sock, 128)) { 00219 $response .= $data; 00220 } else { 00221 break; 00222 } 00223 } 00224 00225 // Split the request into headers and body. 00226 list($headers, $response_body) = explode("\r\n\r\n", $response, 2); 00227 00228 $headers = explode("\r\n", $headers); 00229 00230 // Expect the first line of the headers data to be something 00231 // like HTTP/1.1 200 OK. Split the line on spaces and take 00232 // the second token, which should be the return code. 00233 $http_code = explode(" ", $headers[0]); 00234 $code = $http_code[1]; 00235 00236 $new_headers = array(); 00237 00238 foreach ($headers as $header) { 00239 if (preg_match("/:/", $header)) { 00240 list($name, $value) = explode(": ", $header, 2); 00241 $new_headers[$name] = $value; 00242 } 00243 00244 } 00245 00246 return new Auth_Yadis_HTTPResponse($url, $code, 00247 $new_headers, $response_body); 00248 } 00249 } 00250 00251 ?>
1.8.0