[ Index ] |
|
Code source de Zen Cart E-Commerce Shopping Cart 1.3.7.1 |
1 <?php 2 /** 3 * httpClient Class. 4 * 5 * @package classes 6 * @copyright Copyright 2003-2005 Zen Cart Development Team 7 * @copyright Portions Copyright 2001 Leo West <west_leo@yahoo-REMOVE-.com> Net_HTTP_Client v0.6 8 * @copyright Portions Copyright 2003 osCommerce 9 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0 10 * @version $Id: http_client.php 3047 2006-02-16 21:53:56Z wilt $ 11 */ 12 if (!defined('IS_ADMIN_FLAG')) { 13 die('Illegal Access'); 14 } 15 /** 16 * httpClient Class. 17 * This class is used mainly by payment modules to simulate a browser session 18 * when communicating back to another server to collect information 19 * 20 * @package classes 21 */ 22 class httpClient extends base { 23 var $url; // array containing server URL, similar to parseurl() returned array 24 var $reply; // response code 25 var $replyString; // full response 26 var $protocolVersion = '1.1'; 27 var $requestHeaders, $requestBody; 28 var $socket = false; 29 // proxy stuff 30 var $useProxy = false; 31 var $proxyHost, $proxyPort; 32 33 /** 34 * httpClient constructor 35 * Note: when host and port are defined, the connection is immediate 36 **/ 37 function httpClient($host = '', $port = '') { 38 if (zen_not_null($host)) { 39 $this->connect($host, $port); 40 } 41 } 42 43 /** 44 * turn on proxy support 45 * @param proxyHost proxy host address eg "proxy.mycorp.com" 46 * @param proxyPort proxy port usually 80 or 8080 47 **/ 48 function setProxy($proxyHost, $proxyPort) { 49 $this->useProxy = true; 50 $this->proxyHost = $proxyHost; 51 $this->proxyPort = $proxyPort; 52 } 53 54 /** 55 * setProtocolVersion 56 * define the HTTP protocol version to use 57 * @param version string the version number with one decimal: "0.9", "1.0", "1.1" 58 * when using 1.1, you MUST set the mandatory headers "Host" 59 * @return boolean false if the version number is bad, true if ok 60 **/ 61 function setProtocolVersion($version) { 62 if ( ($version > 0) && ($version <= 1.1) ) { 63 $this->protocolVersion = $version; 64 return true; 65 } else { 66 return false; 67 } 68 } 69 70 /** 71 * set a username and password to access a protected resource 72 * Only "Basic" authentication scheme is supported yet 73 * @param username string - identifier 74 * @param password string - clear password 75 **/ 76 function setCredentials($username, $password) { 77 $this->addHeader('Authorization', 'Basic ' . base64_encode($username . ':' . $password)); 78 } 79 80 /** 81 * define a set of HTTP headers to be sent to the server 82 * header names are lowercased to avoid duplicated headers 83 * @param headers hash array containing the headers as headerName => headerValue pairs 84 **/ 85 function setHeaders($headers) { 86 if (is_array($headers)) { 87 reset($headers); 88 while (list($name, $value) = each($headers)) { 89 $this->requestHeaders[$name] = $value; 90 } 91 } 92 } 93 94 /** 95 * addHeader 96 * set a unique request header 97 * @param headerName the header name 98 * @param headerValue the header value, ( unencoded) 99 **/ 100 function addHeader($headerName, $headerValue) { 101 $this->requestHeaders[$headerName] = $headerValue; 102 } 103 104 /** 105 * removeHeader 106 * unset a request header 107 * @param headerName the header name 108 **/ 109 function removeHeader($headerName) { 110 unset($this->requestHeaders[$headerName]); 111 } 112 113 /** 114 * Connect 115 * open the connection to the server 116 * @param host string server address (or IP) 117 * @param port string server listening port - defaults to 80 118 * @return boolean false is connection failed, true otherwise 119 **/ 120 function Connect($host, $port = '') { 121 $this->url['scheme'] = 'http'; 122 $this->url['host'] = $host; 123 if (zen_not_null($port)) $this->url['port'] = $port; 124 125 return true; 126 } 127 128 /** 129 * Disconnect 130 * close the connection to the server 131 **/ 132 function Disconnect() { 133 if ($this->socket) fclose($this->socket); 134 } 135 136 /** 137 * head 138 * issue a HEAD request 139 * @param uri string URI of the document 140 * @return string response status code (200 if ok) 141 **/ 142 function Head($uri) { 143 $this->responseHeaders = $this->responseBody = ''; 144 145 $uri = $this->makeUri($uri); 146 147 if ($this->sendCommand('HEAD ' . $uri . ' HTTP/' . $this->protocolVersion)) { 148 $this->processReply(); 149 } 150 151 return $this->reply; 152 } 153 154 /** 155 * get 156 * issue a GET http request 157 * @param uri URI (path on server) or full URL of the document 158 * @return string response status code (200 if ok) 159 **/ 160 function Get($url) { 161 $this->responseHeaders = $this->responseBody = ''; 162 163 $uri = $this->makeUri($url); 164 165 if ($this->sendCommand('GET ' . $uri . ' HTTP/' . $this->protocolVersion)) { 166 $this->processReply(); 167 } 168 169 return $this->reply; 170 } 171 172 /** 173 * Post 174 * issue a POST http request 175 * @param uri string URI of the document 176 * @param query_params array parameters to send in the form "parameter name" => value 177 * @return string response status code (200 if ok) 178 **/ 179 // * $params = array( "login" => "tiger", "password" => "secret" ); 180 // * $http->post( "/login.php", $params ); 181 function Post($uri, $query_params = '') { 182 $uri = $this->makeUri($uri); 183 184 if (is_array($query_params)) { 185 $postArray = array(); 186 reset($query_params); 187 while (list($k, $v) = each($query_params)) { 188 $postArray[] = urlencode($k) . '=' . urlencode($v); 189 } 190 191 $this->requestBody = implode('&', $postArray); 192 } 193 194 // set the content type for post parameters 195 $this->addHeader('Content-Type', 'application/x-www-form-urlencoded'); 196 197 if ($this->sendCommand('POST ' . $uri . ' HTTP/' . $this->protocolVersion)) { 198 $this->processReply(); 199 } 200 201 $this->removeHeader('Content-Type'); 202 $this->removeHeader('Content-Length'); 203 $this->requestBody = ''; 204 205 return $this->reply; 206 } 207 208 /** 209 * Put 210 * Send a PUT request 211 * PUT is the method to sending a file on the server. it is *not* widely supported 212 * @param uri the location of the file on the server. dont forget the heading "/" 213 * @param filecontent the content of the file. binary content accepted 214 * @return string response status code 201 (Created) if ok 215 * @see RFC2518 "HTTP Extensions for Distributed Authoring WEBDAV" 216 **/ 217 function Put($uri, $filecontent) { 218 $uri = $this->makeUri($uri); 219 $this->requestBody = $filecontent; 220 221 if ($this->sendCommand('PUT ' . $uri . ' HTTP/' . $this->protocolVersion)) { 222 $this->processReply(); 223 } 224 225 return $this->reply; 226 } 227 228 /** 229 * getHeaders 230 * return the response headers 231 * to be called after a Get() or Head() call 232 * @return array headers received from server in the form headername => value 233 **/ 234 function getHeaders() { 235 return $this->responseHeaders; 236 } 237 238 /** 239 * getHeader 240 * return the response header "headername" 241 * @param headername the name of the header 242 * @return header value or NULL if no such header is defined 243 **/ 244 function getHeader($headername) { 245 return $this->responseHeaders[$headername]; 246 } 247 248 /** 249 * getBody 250 * return the response body 251 * invoke it after a Get() call for instance, to retrieve the response 252 * @return string body content 253 **/ 254 function getBody() { 255 return $this->responseBody; 256 } 257 258 /** 259 * getStatus return the server response's status code 260 * @return string a status code 261 * code are divided in classes (where x is a digit) 262 * - 20x : request processed OK 263 * - 30x : document moved 264 * - 40x : client error ( bad url, document not found, etc...) 265 * - 50x : server error 266 * @see RFC2616 "Hypertext Transfer Protocol -- HTTP/1.1" 267 **/ 268 function getStatus() { 269 return $this->reply; 270 } 271 272 /** 273 * getStatusMessage return the full response status, of the form "CODE Message" 274 * eg. "404 Document not found" 275 * @return string the message 276 **/ 277 function getStatusMessage() { 278 return $this->replyString; 279 } 280 281 282 /** 283 * send a request 284 * data sent are in order 285 * a) the command 286 * b) the request headers if they are defined 287 * c) the request body if defined 288 * @return string the server repsonse status code 289 **/ 290 function sendCommand($command) { 291 $this->responseHeaders = array(); 292 $this->responseBody = ''; 293 294 // connect if necessary 295 if ( ($this->socket == false) || (feof($this->socket)) ) { 296 if ($this->useProxy) { 297 $host = $this->proxyHost; 298 $port = $this->proxyPort; 299 } else { 300 $host = $this->url['host']; 301 $port = $this->url['port']; 302 } 303 304 if (!zen_not_null($port)) $port = 80; 305 306 if (!$this->socket = @fsockopen($host, $port, $this->reply, $this->replyString)) { 307 return false; 308 } 309 310 if (zen_not_null($this->requestBody)) { 311 $this->addHeader('Content-Length', strlen($this->requestBody)); 312 } 313 314 $this->request = $command; 315 $cmd = $command . "\r\n"; 316 if (is_array($this->requestHeaders)) { 317 reset($this->requestHeaders); 318 while (list($k, $v) = each($this->requestHeaders)) { 319 $cmd .= $k . ': ' . $v . "\r\n"; 320 } 321 } 322 323 if (zen_not_null($this->requestBody)) { 324 $cmd .= "\r\n" . $this->requestBody; 325 } 326 327 // unset body (in case of successive requests) 328 $this->requestBody = ''; 329 330 fputs($this->socket, $cmd . "\r\n"); 331 332 return true; 333 } 334 } 335 336 function processReply() { 337 $this->replyString = trim(fgets($this->socket, 1024)); 338 339 if (preg_match('|^HTTP/\S+ (\d+) |i', $this->replyString, $a )) { 340 $this->reply = $a[1]; 341 } else { 342 $this->reply = 'Bad Response'; 343 } 344 345 //get response headers and body 346 $this->responseHeaders = $this->processHeader(); 347 $this->responseBody = $this->processBody(); 348 349 return $this->reply; 350 } 351 352 /** 353 * processHeader() reads header lines from socket until the line equals $lastLine 354 * @return array of headers with header names as keys and header content as values 355 **/ 356 function processHeader($lastLine = "\r\n") { 357 $headers = array(); 358 $finished = false; 359 360 while ( (!$finished) && (!feof($this->socket)) ) { 361 $str = fgets($this->socket, 1024); 362 $finished = ($str == $lastLine); 363 if (!$finished) { 364 list($hdr, $value) = split(': ', $str, 2); 365 // nasty workaround broken multiple same headers (eg. Set-Cookie headers) @FIXME 366 if (isset($headers[$hdr])) { 367 $headers[$hdr] .= '; ' . trim($value); 368 } else { 369 $headers[$hdr] = trim($value); 370 } 371 } 372 } 373 374 return $headers; 375 } 376 377 /** 378 * processBody() reads the body from the socket 379 * the body is the "real" content of the reply 380 * @return string body content 381 **/ 382 function processBody() { 383 $data = ''; 384 $counter = 0; 385 386 do { 387 $status = socket_get_status($this->socket); 388 if ($status['eof'] == 1) { 389 break; 390 } 391 392 if ($status['unread_bytes'] > 0) { 393 $buffer = fread($this->socket, $status['unread_bytes']); 394 $counter = 0; 395 } else { 396 $buffer = fread($this->socket, 128); 397 $counter++; 398 usleep(2); 399 } 400 401 $data .= $buffer; 402 } while ( ($status['unread_bytes'] > 0) || ($counter++ < 10) ); 403 404 return $data; 405 } 406 407 /** 408 * Calculate and return the URI to be sent ( proxy purpose ) 409 * @param the local URI 410 * @return URI to be used in the HTTP request 411 **/ 412 function makeUri($uri) { 413 $a = parse_url($uri); 414 415 if ( (isset($a['scheme'])) && (isset($a['host'])) ) { 416 $this->url = $a; 417 } else { 418 unset($this->url['query']); 419 unset($this->url['fragment']); 420 $this->url = array_merge($this->url, $a); 421 } 422 423 if ($this->useProxy) { 424 $requesturi = 'http://' . $this->url['host'] . (empty($this->url['port']) ? '' : ':' . $this->url['port']) . $this->url['path'] . (empty($this->url['query']) ? '' : '?' . $this->url['query']); 425 } else { 426 $requesturi = $this->url['path'] . (empty($this->url['query']) ? '' : '?' . $this->url['query']); 427 } 428 429 return $requesturi; 430 } 431 } 432 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 16:45:43 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |