00001 <?php 00002 00003 00004 00005 00015 class soap_transport_http extends nusoap_base { 00016 00017 var $url = ''; 00018 var $uri = ''; 00019 var $digest_uri = ''; 00020 var $scheme = ''; 00021 var $host = ''; 00022 var $port = ''; 00023 var $path = ''; 00024 var $request_method = 'POST'; 00025 var $protocol_version = '1.0'; 00026 var $encoding = ''; 00027 var $outgoing_headers = array(); 00028 var $incoming_headers = array(); 00029 var $incoming_cookies = array(); 00030 var $outgoing_payload = ''; 00031 var $incoming_payload = ''; 00032 var $response_status_line; // HTTP response status line 00033 var $useSOAPAction = true; 00034 var $persistentConnection = false; 00035 var $ch = false; // cURL handle 00036 var $ch_options = array(); // cURL custom options 00037 var $use_curl = false; // force cURL use 00038 var $proxy = null; // proxy information (associative array) 00039 var $username = ''; 00040 var $password = ''; 00041 var $authtype = ''; 00042 var $digestRequest = array(); 00043 var $certRequest = array(); // keys must be cainfofile (optional), sslcertfile, sslkeyfile, passphrase, certpassword (optional), verifypeer (optional), verifyhost (optional) 00044 // cainfofile: certificate authority file, e.g. '$pathToPemFiles/rootca.pem' 00045 // sslcertfile: SSL certificate file, e.g. '$pathToPemFiles/mycert.pem' 00046 // sslkeyfile: SSL key file, e.g. '$pathToPemFiles/mykey.pem' 00047 // passphrase: SSL key password/passphrase 00048 // certpassword: SSL certificate password 00049 // verifypeer: default is 1 00050 // verifyhost: default is 1 00051 00060 function soap_transport_http($url, $curl_options = NULL, $use_curl = false){ 00061 parent::nusoap_base(); 00062 $this->debug("ctor url=$url use_curl=$use_curl curl_options:"); 00063 $this->appendDebug($this->varDump($curl_options)); 00064 $this->setURL($url); 00065 if (is_array($curl_options)) { 00066 $this->ch_options = $curl_options; 00067 } 00068 $this->use_curl = $use_curl; 00069 ereg('\$Revisio' . 'n: ([^ ]+)', $this->revision, $rev); 00070 $this->setHeader('User-Agent', $this->title.'/'.$this->version.' ('.$rev[1].')'); 00071 } 00072 00080 function setCurlOption($option, $value) { 00081 $this->debug("setCurlOption option=$option, value="); 00082 $this->appendDebug($this->varDump($value)); 00083 curl_setopt($this->ch, $option, $value); 00084 } 00085 00093 function setHeader($name, $value) { 00094 $this->outgoing_headers[$name] = $value; 00095 $this->debug("set header $name: $value"); 00096 } 00097 00104 function unsetHeader($name) { 00105 if (isset($this->outgoing_headers[$name])) { 00106 $this->debug("unset header $name"); 00107 unset($this->outgoing_headers[$name]); 00108 } 00109 } 00110 00117 function setURL($url) { 00118 $this->url = $url; 00119 00120 $u = parse_url($url); 00121 foreach($u as $k => $v){ 00122 $this->debug("parsed URL $k = $v"); 00123 $this->$k = $v; 00124 } 00125 00126 // add any GET params to path 00127 if(isset($u['query']) && $u['query'] != ''){ 00128 $this->path .= '?' . $u['query']; 00129 } 00130 00131 // set default port 00132 if(!isset($u['port'])){ 00133 if($u['scheme'] == 'https'){ 00134 $this->port = 443; 00135 } else { 00136 $this->port = 80; 00137 } 00138 } 00139 00140 $this->uri = $this->path; 00141 $this->digest_uri = $this->uri; 00142 00143 // build headers 00144 if (!isset($u['port'])) { 00145 $this->setHeader('Host', $this->host); 00146 } else { 00147 $this->setHeader('Host', $this->host.':'.$this->port); 00148 } 00149 00150 if (isset($u['user']) && $u['user'] != '') { 00151 $this->setCredentials(urldecode($u['user']), isset($u['pass']) ? urldecode($u['pass']) : ''); 00152 } 00153 } 00154 00161 function io_method() { 00162 if ($this->use_curl || ($this->scheme == 'https') || ($this->scheme == 'http' && $this->authtype == 'ntlm') || ($this->scheme == 'http' && is_array($this->proxy) && $this->proxy['authtype'] == 'ntlm')) 00163 return 'curl'; 00164 if (($this->scheme == 'http' || $this->scheme == 'ssl') && $this->authtype != 'ntlm' && (!is_array($this->proxy) || $this->proxy['authtype'] != 'ntlm')) 00165 return 'socket'; 00166 return 'unknown'; 00167 } 00168 00177 function connect($connection_timeout=0,$response_timeout=30){ 00178 // For PHP 4.3 with OpenSSL, change https scheme to ssl, then treat like 00179 // "regular" socket. 00180 // TODO: disabled for now because OpenSSL must be *compiled* in (not just 00181 // loaded), and until PHP5 stream_get_wrappers is not available. 00182 // if ($this->scheme == 'https') { 00183 // if (version_compare(phpversion(), '4.3.0') >= 0) { 00184 // if (extension_loaded('openssl')) { 00185 // $this->scheme = 'ssl'; 00186 // $this->debug('Using SSL over OpenSSL'); 00187 // } 00188 // } 00189 // } 00190 $this->debug("connect connection_timeout $connection_timeout, response_timeout $response_timeout, scheme $this->scheme, host $this->host, port $this->port"); 00191 if ($this->io_method() == 'socket') { 00192 if (!is_array($this->proxy)) { 00193 $host = $this->host; 00194 $port = $this->port; 00195 } else { 00196 $host = $this->proxy['host']; 00197 $port = $this->proxy['port']; 00198 } 00199 00200 // use persistent connection 00201 if($this->persistentConnection && isset($this->fp) && is_resource($this->fp)){ 00202 if (!feof($this->fp)) { 00203 $this->debug('Re-use persistent connection'); 00204 return true; 00205 } 00206 fclose($this->fp); 00207 $this->debug('Closed persistent connection at EOF'); 00208 } 00209 00210 // munge host if using OpenSSL 00211 if ($this->scheme == 'ssl') { 00212 $host = 'ssl://' . $host; 00213 } 00214 $this->debug('calling fsockopen with host ' . $host . ' connection_timeout ' . $connection_timeout); 00215 00216 // open socket 00217 if($connection_timeout > 0){ 00218 $this->fp = @fsockopen( $host, $this->port, $this->errno, $this->error_str, $connection_timeout); 00219 } else { 00220 $this->fp = @fsockopen( $host, $this->port, $this->errno, $this->error_str); 00221 } 00222 00223 // test pointer 00224 if(!$this->fp) { 00225 $msg = 'Couldn\'t open socket connection to server ' . $this->url; 00226 if ($this->errno) { 00227 $msg .= ', Error ('.$this->errno.'): '.$this->error_str; 00228 } else { 00229 $msg .= ' prior to connect(). This is often a problem looking up the host name.'; 00230 } 00231 $this->debug($msg); 00232 $this->setError($msg); 00233 return false; 00234 } 00235 00236 // set response timeout 00237 $this->debug('set response timeout to ' . $response_timeout); 00238 socket_set_timeout( $this->fp, $response_timeout); 00239 00240 $this->debug('socket connected'); 00241 return true; 00242 } else if ($this->io_method() == 'curl') { 00243 if (!extension_loaded('curl')) { 00244 // $this->setError('cURL Extension, or OpenSSL extension w/ PHP version >= 4.3 is required for HTTPS'); 00245 $this->setError('The PHP cURL Extension is required for HTTPS or NLTM. You will need to re-build or update your PHP to included cURL.'); 00246 return false; 00247 } 00248 // Avoid warnings when PHP does not have these options 00249 if (defined('CURLOPT_CONNECTIONTIMEOUT')) 00250 $CURLOPT_CONNECTIONTIMEOUT = CURLOPT_CONNECTIONTIMEOUT; 00251 else 00252 $CURLOPT_CONNECTIONTIMEOUT = 78; 00253 if (defined('CURLOPT_HTTPAUTH')) 00254 $CURLOPT_HTTPAUTH = CURLOPT_HTTPAUTH; 00255 else 00256 $CURLOPT_HTTPAUTH = 107; 00257 if (defined('CURLOPT_PROXYAUTH')) 00258 $CURLOPT_PROXYAUTH = CURLOPT_PROXYAUTH; 00259 else 00260 $CURLOPT_PROXYAUTH = 111; 00261 if (defined('CURLAUTH_BASIC')) 00262 $CURLAUTH_BASIC = CURLAUTH_BASIC; 00263 else 00264 $CURLAUTH_BASIC = 1; 00265 if (defined('CURLAUTH_DIGEST')) 00266 $CURLAUTH_DIGEST = CURLAUTH_DIGEST; 00267 else 00268 $CURLAUTH_DIGEST = 2; 00269 if (defined('CURLAUTH_NTLM')) 00270 $CURLAUTH_NTLM = CURLAUTH_NTLM; 00271 else 00272 $CURLAUTH_NTLM = 8; 00273 00274 $this->debug('connect using cURL'); 00275 // init CURL 00276 $this->ch = curl_init(); 00277 // set url 00278 $hostURL = ($this->port != '') ? "$this->scheme://$this->host:$this->port" : "$this->scheme://$this->host"; 00279 // add path 00280 $hostURL .= $this->path; 00281 $this->setCurlOption(CURLOPT_URL, $hostURL); 00282 // follow location headers (re-directs) 00283 if (ini_get('safe_mode') || ini_get('open_basedir')) { 00284 $this->debug('safe_mode or open_basedir set, so do not set CURLOPT_FOLLOWLOCATION'); 00285 $this->debug('safe_mode = '); 00286 $this->appendDebug($this->varDump(ini_get('safe_mode'))); 00287 $this->debug('open_basedir = '); 00288 $this->appendDebug($this->varDump(ini_get('open_basedir'))); 00289 } else { 00290 $this->setCurlOption(CURLOPT_FOLLOWLOCATION, 1); 00291 } 00292 // ask for headers in the response output 00293 $this->setCurlOption(CURLOPT_HEADER, 1); 00294 // ask for the response output as the return value 00295 $this->setCurlOption(CURLOPT_RETURNTRANSFER, 1); 00296 // encode 00297 // We manage this ourselves through headers and encoding 00298 // if(function_exists('gzuncompress')){ 00299 // $this->setCurlOption(CURLOPT_ENCODING, 'deflate'); 00300 // } 00301 // persistent connection 00302 if ($this->persistentConnection) { 00303 // I believe the following comment is now bogus, having applied to 00304 // the code when it used CURLOPT_CUSTOMREQUEST to send the request. 00305 // The way we send data, we cannot use persistent connections, since 00306 // there will be some "junk" at the end of our request. 00307 //$this->setCurlOption(CURL_HTTP_VERSION_1_1, true); 00308 $this->persistentConnection = false; 00309 $this->setHeader('Connection', 'close'); 00310 } 00311 // set timeouts 00312 if ($connection_timeout != 0) { 00313 $this->setCurlOption($CURLOPT_CONNECTIONTIMEOUT, $connection_timeout); 00314 } 00315 if ($response_timeout != 0) { 00316 $this->setCurlOption(CURLOPT_TIMEOUT, $response_timeout); 00317 } 00318 00319 if ($this->scheme == 'https') { 00320 $this->debug('set cURL SSL verify options'); 00321 // recent versions of cURL turn on peer/host checking by default, 00322 // while PHP binaries are not compiled with a default location for the 00323 // CA cert bundle, so disable peer/host checking. 00324 //$this->setCurlOption(CURLOPT_CAINFO, 'f:\php-4.3.2-win32\extensions\curl-ca-bundle.crt'); 00325 $this->setCurlOption(CURLOPT_SSL_VERIFYPEER, 0); 00326 $this->setCurlOption(CURLOPT_SSL_VERIFYHOST, 0); 00327 00328 // support client certificates (thanks Tobias Boes, Doug Anarino, Eryan Ariobowo) 00329 if ($this->authtype == 'certificate') { 00330 $this->debug('set cURL certificate options'); 00331 if (isset($this->certRequest['cainfofile'])) { 00332 $this->setCurlOption(CURLOPT_CAINFO, $this->certRequest['cainfofile']); 00333 } 00334 if (isset($this->certRequest['verifypeer'])) { 00335 $this->setCurlOption(CURLOPT_SSL_VERIFYPEER, $this->certRequest['verifypeer']); 00336 } else { 00337 $this->setCurlOption(CURLOPT_SSL_VERIFYPEER, 1); 00338 } 00339 if (isset($this->certRequest['verifyhost'])) { 00340 $this->setCurlOption(CURLOPT_SSL_VERIFYHOST, $this->certRequest['verifyhost']); 00341 } else { 00342 $this->setCurlOption(CURLOPT_SSL_VERIFYHOST, 1); 00343 } 00344 if (isset($this->certRequest['sslcertfile'])) { 00345 $this->setCurlOption(CURLOPT_SSLCERT, $this->certRequest['sslcertfile']); 00346 } 00347 if (isset($this->certRequest['sslkeyfile'])) { 00348 $this->setCurlOption(CURLOPT_SSLKEY, $this->certRequest['sslkeyfile']); 00349 } 00350 if (isset($this->certRequest['passphrase'])) { 00351 $this->setCurlOption(CURLOPT_SSLKEYPASSWD, $this->certRequest['passphrase']); 00352 } 00353 if (isset($this->certRequest['certpassword'])) { 00354 $this->setCurlOption(CURLOPT_SSLCERTPASSWD, $this->certRequest['certpassword']); 00355 } 00356 } 00357 } 00358 if ($this->authtype && ($this->authtype != 'certificate')) { 00359 if ($this->username) { 00360 $this->debug('set cURL username/password'); 00361 $this->setCurlOption(CURLOPT_USERPWD, "$this->username:$this->password"); 00362 } 00363 if ($this->authtype == 'basic') { 00364 $this->debug('set cURL for Basic authentication'); 00365 $this->setCurlOption($CURLOPT_HTTPAUTH, $CURLAUTH_BASIC); 00366 } 00367 if ($this->authtype == 'digest') { 00368 $this->debug('set cURL for digest authentication'); 00369 $this->setCurlOption($CURLOPT_HTTPAUTH, $CURLAUTH_DIGEST); 00370 } 00371 if ($this->authtype == 'ntlm') { 00372 $this->debug('set cURL for NTLM authentication'); 00373 $this->setCurlOption($CURLOPT_HTTPAUTH, $CURLAUTH_NTLM); 00374 } 00375 } 00376 if (is_array($this->proxy)) { 00377 $this->debug('set cURL proxy options'); 00378 if ($this->proxy['port'] != '') { 00379 $this->setCurlOption(CURLOPT_PROXY, $this->proxy['host'].':'.$this->proxy['port']); 00380 } else { 00381 $this->setCurlOption(CURLOPT_PROXY, $this->proxy['host']); 00382 } 00383 if ($this->proxy['username'] || $this->proxy['password']) { 00384 $this->debug('set cURL proxy authentication options'); 00385 $this->setCurlOption(CURLOPT_PROXYUSERPWD, $this->proxy['username'].':'.$this->proxy['password']); 00386 if ($this->proxy['authtype'] == 'basic') { 00387 $this->setCurlOption($CURLOPT_PROXYAUTH, $CURLAUTH_BASIC); 00388 } 00389 if ($this->proxy['authtype'] == 'ntlm') { 00390 $this->setCurlOption($CURLOPT_PROXYAUTH, $CURLAUTH_NTLM); 00391 } 00392 } 00393 } 00394 $this->debug('cURL connection set up'); 00395 return true; 00396 } else { 00397 $this->setError('Unknown scheme ' . $this->scheme); 00398 $this->debug('Unknown scheme ' . $this->scheme); 00399 return false; 00400 } 00401 } 00402 00413 function send($data, $timeout=0, $response_timeout=30, $cookies=NULL) { 00414 00415 $this->debug('entered send() with data of length: '.strlen($data)); 00416 00417 $this->tryagain = true; 00418 $tries = 0; 00419 while ($this->tryagain) { 00420 $this->tryagain = false; 00421 if ($tries++ < 2) { 00422 // make connnection 00423 if (!$this->connect($timeout, $response_timeout)){ 00424 return false; 00425 } 00426 00427 // send request 00428 if (!$this->sendRequest($data, $cookies)){ 00429 return false; 00430 } 00431 00432 // get response 00433 $respdata = $this->getResponse(); 00434 } else { 00435 $this->setError("Too many tries to get an OK response ($this->response_status_line)"); 00436 } 00437 } 00438 $this->debug('end of send()'); 00439 return $respdata; 00440 } 00441 00442 00454 function sendHTTPS($data, $timeout=0, $response_timeout=30, $cookies) { 00455 return $this->send($data, $timeout, $response_timeout, $cookies); 00456 } 00457 00468 function setCredentials($username, $password, $authtype = 'basic', $digestRequest = array(), $certRequest = array()) { 00469 $this->debug("setCredentials username=$username authtype=$authtype digestRequest="); 00470 $this->appendDebug($this->varDump($digestRequest)); 00471 $this->debug("certRequest="); 00472 $this->appendDebug($this->varDump($certRequest)); 00473 // cf. RFC 2617 00474 if ($authtype == 'basic') { 00475 $this->setHeader('Authorization', 'Basic '.base64_encode(str_replace(':','',$username).':'.$password)); 00476 } elseif ($authtype == 'digest') { 00477 if (isset($digestRequest['nonce'])) { 00478 $digestRequest['nc'] = isset($digestRequest['nc']) ? $digestRequest['nc']++ : 1; 00479 00480 // calculate the Digest hashes (calculate code based on digest implementation found at: http://www.rassoc.com/gregr/weblog/stories/2002/07/09/webServicesSecurityHttpDigestAuthenticationWithoutActiveDirectory.html) 00481 00482 // A1 = unq(username-value) ":" unq(realm-value) ":" passwd 00483 $A1 = $username. ':' . (isset($digestRequest['realm']) ? $digestRequest['realm'] : '') . ':' . $password; 00484 00485 // H(A1) = MD5(A1) 00486 $HA1 = md5($A1); 00487 00488 // A2 = Method ":" digest-uri-value 00489 $A2 = $this->request_method . ':' . $this->digest_uri; 00490 00491 // H(A2) 00492 $HA2 = md5($A2); 00493 00494 // KD(secret, data) = H(concat(secret, ":", data)) 00495 // if qop == auth: 00496 // request-digest = <"> < KD ( H(A1), unq(nonce-value) 00497 // ":" nc-value 00498 // ":" unq(cnonce-value) 00499 // ":" unq(qop-value) 00500 // ":" H(A2) 00501 // ) <"> 00502 // if qop is missing, 00503 // request-digest = <"> < KD ( H(A1), unq(nonce-value) ":" H(A2) ) > <"> 00504 00505 $unhashedDigest = ''; 00506 $nonce = isset($digestRequest['nonce']) ? $digestRequest['nonce'] : ''; 00507 $cnonce = $nonce; 00508 if ($digestRequest['qop'] != '') { 00509 $unhashedDigest = $HA1 . ':' . $nonce . ':' . sprintf("%08d", $digestRequest['nc']) . ':' . $cnonce . ':' . $digestRequest['qop'] . ':' . $HA2; 00510 } else { 00511 $unhashedDigest = $HA1 . ':' . $nonce . ':' . $HA2; 00512 } 00513 00514 $hashedDigest = md5($unhashedDigest); 00515 00516 $opaque = ''; 00517 if (isset($digestRequest['opaque'])) { 00518 $opaque = ', opaque="' . $digestRequest['opaque'] . '"'; 00519 } 00520 00521 $this->setHeader('Authorization', 'Digest username="' . $username . '", realm="' . $digestRequest['realm'] . '", nonce="' . $nonce . '", uri="' . $this->digest_uri . $opaque . '", cnonce="' . $cnonce . '", nc=' . sprintf("%08x", $digestRequest['nc']) . ', qop="' . $digestRequest['qop'] . '", response="' . $hashedDigest . '"'); 00522 } 00523 } elseif ($authtype == 'certificate') { 00524 $this->certRequest = $certRequest; 00525 $this->debug('Authorization header not set for certificate'); 00526 } elseif ($authtype == 'ntlm') { 00527 // do nothing 00528 $this->debug('Authorization header not set for ntlm'); 00529 } 00530 $this->username = $username; 00531 $this->password = $password; 00532 $this->authtype = $authtype; 00533 $this->digestRequest = $digestRequest; 00534 } 00535 00542 function setSOAPAction($soapaction) { 00543 $this->setHeader('SOAPAction', '"' . $soapaction . '"'); 00544 } 00545 00552 function setEncoding($enc='gzip, deflate') { 00553 if (function_exists('gzdeflate')) { 00554 $this->protocol_version = '1.1'; 00555 $this->setHeader('Accept-Encoding', $enc); 00556 if (!isset($this->outgoing_headers['Connection'])) { 00557 $this->setHeader('Connection', 'close'); 00558 $this->persistentConnection = false; 00559 } 00560 set_magic_quotes_runtime(0); 00561 // deprecated 00562 $this->encoding = $enc; 00563 } 00564 } 00565 00576 function setProxy($proxyhost, $proxyport, $proxyusername = '', $proxypassword = '', $proxyauthtype = 'basic') { 00577 if ($proxyhost) { 00578 $this->proxy = array( 00579 'host' => $proxyhost, 00580 'port' => $proxyport, 00581 'username' => $proxyusername, 00582 'password' => $proxypassword, 00583 'authtype' => $proxyauthtype 00584 ); 00585 if ($proxyusername != '' && $proxypassword != '' && $proxyauthtype = 'basic') { 00586 $this->setHeader('Proxy-Authorization', ' Basic '.base64_encode($proxyusername.':'.$proxypassword)); 00587 } 00588 } else { 00589 $this->debug('remove proxy'); 00590 $proxy = null; 00591 unsetHeader('Proxy-Authorization'); 00592 } 00593 } 00594 00595 00604 function isSkippableCurlHeader(&$data) { 00605 $skipHeaders = array( 'HTTP/1.1 100', 00606 'HTTP/1.0 301', 00607 'HTTP/1.1 301', 00608 'HTTP/1.0 302', 00609 'HTTP/1.1 302', 00610 'HTTP/1.0 401', 00611 'HTTP/1.1 401', 00612 'HTTP/1.0 200 Connection established'); 00613 foreach ($skipHeaders as $hd) { 00614 $prefix = substr($data, 0, strlen($hd)); 00615 if ($prefix == $hd) return true; 00616 } 00617 00618 return false; 00619 } 00620 00631 function decodeChunked($buffer, $lb){ 00632 // length := 0 00633 $length = 0; 00634 $new = ''; 00635 00636 // read chunk-size, chunk-extension (if any) and CRLF 00637 // get the position of the linebreak 00638 $chunkend = strpos($buffer, $lb); 00639 if ($chunkend == FALSE) { 00640 $this->debug('no linebreak found in decodeChunked'); 00641 return $new; 00642 } 00643 $temp = substr($buffer,0,$chunkend); 00644 $chunk_size = hexdec( trim($temp) ); 00645 $chunkstart = $chunkend + strlen($lb); 00646 // while (chunk-size > 0) { 00647 while ($chunk_size > 0) { 00648 $this->debug("chunkstart: $chunkstart chunk_size: $chunk_size"); 00649 $chunkend = strpos( $buffer, $lb, $chunkstart + $chunk_size); 00650 00651 // Just in case we got a broken connection 00652 if ($chunkend == FALSE) { 00653 $chunk = substr($buffer,$chunkstart); 00654 // append chunk-data to entity-body 00655 $new .= $chunk; 00656 $length += strlen($chunk); 00657 break; 00658 } 00659 00660 // read chunk-data and CRLF 00661 $chunk = substr($buffer,$chunkstart,$chunkend-$chunkstart); 00662 // append chunk-data to entity-body 00663 $new .= $chunk; 00664 // length := length + chunk-size 00665 $length += strlen($chunk); 00666 // read chunk-size and CRLF 00667 $chunkstart = $chunkend + strlen($lb); 00668 00669 $chunkend = strpos($buffer, $lb, $chunkstart) + strlen($lb); 00670 if ($chunkend == FALSE) { 00671 break; //Just in case we got a broken connection 00672 } 00673 $temp = substr($buffer,$chunkstart,$chunkend-$chunkstart); 00674 $chunk_size = hexdec( trim($temp) ); 00675 $chunkstart = $chunkend; 00676 } 00677 return $new; 00678 } 00679 00688 function buildPayload($data, $cookie_str = '') { 00689 // Note: for cURL connections, $this->outgoing_payload is ignored, 00690 // as is the Content-Length header, but these are still created as 00691 // debugging guides. 00692 00693 // add content-length header 00694 if ($this->request_method != 'GET') { 00695 $this->setHeader('Content-Length', strlen($data)); 00696 } 00697 00698 // start building outgoing payload: 00699 if ($this->proxy) { 00700 $uri = $this->url; 00701 } else { 00702 $uri = $this->uri; 00703 } 00704 $req = "$this->request_method $uri HTTP/$this->protocol_version"; 00705 $this->debug("HTTP request: $req"); 00706 $this->outgoing_payload = "$req\r\n"; 00707 00708 // loop thru headers, serializing 00709 foreach($this->outgoing_headers as $k => $v){ 00710 $hdr = $k.': '.$v; 00711 $this->debug("HTTP header: $hdr"); 00712 $this->outgoing_payload .= "$hdr\r\n"; 00713 } 00714 00715 // add any cookies 00716 if ($cookie_str != '') { 00717 $hdr = 'Cookie: '.$cookie_str; 00718 $this->debug("HTTP header: $hdr"); 00719 $this->outgoing_payload .= "$hdr\r\n"; 00720 } 00721 00722 // header/body separator 00723 $this->outgoing_payload .= "\r\n"; 00724 00725 // add data 00726 $this->outgoing_payload .= $data; 00727 } 00728 00737 function sendRequest($data, $cookies = NULL) { 00738 // build cookie string 00739 $cookie_str = $this->getCookiesForRequest($cookies, (($this->scheme == 'ssl') || ($this->scheme == 'https'))); 00740 00741 // build payload 00742 $this->buildPayload($data, $cookie_str); 00743 00744 if ($this->io_method() == 'socket') { 00745 // send payload 00746 if(!fputs($this->fp, $this->outgoing_payload, strlen($this->outgoing_payload))) { 00747 $this->setError('couldn\'t write message data to socket'); 00748 $this->debug('couldn\'t write message data to socket'); 00749 return false; 00750 } 00751 $this->debug('wrote data to socket, length = ' . strlen($this->outgoing_payload)); 00752 return true; 00753 } else if ($this->io_method() == 'curl') { 00754 // set payload 00755 // cURL does say this should only be the verb, and in fact it 00756 // turns out that the URI and HTTP version are appended to this, which 00757 // some servers refuse to work with (so we no longer use this method!) 00758 //$this->setCurlOption(CURLOPT_CUSTOMREQUEST, $this->outgoing_payload); 00759 $curl_headers = array(); 00760 foreach($this->outgoing_headers as $k => $v){ 00761 if ($k == 'Connection' || $k == 'Content-Length' || $k == 'Host' || $k == 'Authorization' || $k == 'Proxy-Authorization') { 00762 $this->debug("Skip cURL header $k: $v"); 00763 } else { 00764 $curl_headers[] = "$k: $v"; 00765 } 00766 } 00767 if ($cookie_str != '') { 00768 $curl_headers[] = 'Cookie: ' . $cookie_str; 00769 } 00770 $this->setCurlOption(CURLOPT_HTTPHEADER, $curl_headers); 00771 $this->debug('set cURL HTTP headers'); 00772 if ($this->request_method == "POST") { 00773 $this->setCurlOption(CURLOPT_POST, 1); 00774 $this->setCurlOption(CURLOPT_POSTFIELDS, $data); 00775 $this->debug('set cURL POST data'); 00776 } else { 00777 } 00778 // insert custom user-set cURL options 00779 foreach ($this->ch_options as $key => $val) { 00780 $this->setCurlOption($key, $val); 00781 } 00782 00783 $this->debug('set cURL payload'); 00784 return true; 00785 } 00786 } 00787 00794 function getResponse(){ 00795 $this->incoming_payload = ''; 00796 00797 if ($this->io_method() == 'socket') { 00798 // loop until headers have been retrieved 00799 $data = ''; 00800 while (!isset($lb)){ 00801 00802 // We might EOF during header read. 00803 if(feof($this->fp)) { 00804 $this->incoming_payload = $data; 00805 $this->debug('found no headers before EOF after length ' . strlen($data)); 00806 $this->debug("received before EOF:\n" . $data); 00807 $this->setError('server failed to send headers'); 00808 return false; 00809 } 00810 00811 $tmp = fgets($this->fp, 256); 00812 $tmplen = strlen($tmp); 00813 $this->debug("read line of $tmplen bytes: " . trim($tmp)); 00814 00815 if ($tmplen == 0) { 00816 $this->incoming_payload = $data; 00817 $this->debug('socket read of headers timed out after length ' . strlen($data)); 00818 $this->debug("read before timeout: " . $data); 00819 $this->setError('socket read of headers timed out'); 00820 return false; 00821 } 00822 00823 $data .= $tmp; 00824 $pos = strpos($data,"\r\n\r\n"); 00825 if($pos > 1){ 00826 $lb = "\r\n"; 00827 } else { 00828 $pos = strpos($data,"\n\n"); 00829 if($pos > 1){ 00830 $lb = "\n"; 00831 } 00832 } 00833 // remove 100 headers 00834 if (isset($lb) && ereg('^HTTP/1.1 100',$data)) { 00835 unset($lb); 00836 $data = ''; 00837 }// 00838 } 00839 // store header data 00840 $this->incoming_payload .= $data; 00841 $this->debug('found end of headers after length ' . strlen($data)); 00842 // process headers 00843 $header_data = trim(substr($data,0,$pos)); 00844 $header_array = explode($lb,$header_data); 00845 $this->incoming_headers = array(); 00846 $this->incoming_cookies = array(); 00847 foreach($header_array as $header_line){ 00848 $arr = explode(':',$header_line, 2); 00849 if(count($arr) > 1){ 00850 $header_name = strtolower(trim($arr[0])); 00851 $this->incoming_headers[$header_name] = trim($arr[1]); 00852 if ($header_name == 'set-cookie') { 00853 // TODO: allow multiple cookies from parseCookie 00854 $cookie = $this->parseCookie(trim($arr[1])); 00855 if ($cookie) { 00856 $this->incoming_cookies[] = $cookie; 00857 $this->debug('found cookie: ' . $cookie['name'] . ' = ' . $cookie['value']); 00858 } else { 00859 $this->debug('did not find cookie in ' . trim($arr[1])); 00860 } 00861 } 00862 } else if (isset($header_name)) { 00863 // append continuation line to previous header 00864 $this->incoming_headers[$header_name] .= $lb . ' ' . $header_line; 00865 } 00866 } 00867 00868 // loop until msg has been received 00869 if (isset($this->incoming_headers['transfer-encoding']) && strtolower($this->incoming_headers['transfer-encoding']) == 'chunked') { 00870 $content_length = 2147483647; // ignore any content-length header 00871 $chunked = true; 00872 $this->debug("want to read chunked content"); 00873 } elseif (isset($this->incoming_headers['content-length'])) { 00874 $content_length = $this->incoming_headers['content-length']; 00875 $chunked = false; 00876 $this->debug("want to read content of length $content_length"); 00877 } else { 00878 $content_length = 2147483647; 00879 $chunked = false; 00880 $this->debug("want to read content to EOF"); 00881 } 00882 $data = ''; 00883 do { 00884 if ($chunked) { 00885 $tmp = fgets($this->fp, 256); 00886 $tmplen = strlen($tmp); 00887 $this->debug("read chunk line of $tmplen bytes"); 00888 if ($tmplen == 0) { 00889 $this->incoming_payload = $data; 00890 $this->debug('socket read of chunk length timed out after length ' . strlen($data)); 00891 $this->debug("read before timeout:\n" . $data); 00892 $this->setError('socket read of chunk length timed out'); 00893 return false; 00894 } 00895 $content_length = hexdec(trim($tmp)); 00896 $this->debug("chunk length $content_length"); 00897 } 00898 $strlen = 0; 00899 while (($strlen < $content_length) && (!feof($this->fp))) { 00900 $readlen = min(8192, $content_length - $strlen); 00901 $tmp = fread($this->fp, $readlen); 00902 $tmplen = strlen($tmp); 00903 $this->debug("read buffer of $tmplen bytes"); 00904 if (($tmplen == 0) && (!feof($this->fp))) { 00905 $this->incoming_payload = $data; 00906 $this->debug('socket read of body timed out after length ' . strlen($data)); 00907 $this->debug("read before timeout:\n" . $data); 00908 $this->setError('socket read of body timed out'); 00909 return false; 00910 } 00911 $strlen += $tmplen; 00912 $data .= $tmp; 00913 } 00914 if ($chunked && ($content_length > 0)) { 00915 $tmp = fgets($this->fp, 256); 00916 $tmplen = strlen($tmp); 00917 $this->debug("read chunk terminator of $tmplen bytes"); 00918 if ($tmplen == 0) { 00919 $this->incoming_payload = $data; 00920 $this->debug('socket read of chunk terminator timed out after length ' . strlen($data)); 00921 $this->debug("read before timeout:\n" . $data); 00922 $this->setError('socket read of chunk terminator timed out'); 00923 return false; 00924 } 00925 } 00926 } while ($chunked && ($content_length > 0) && (!feof($this->fp))); 00927 if (feof($this->fp)) { 00928 $this->debug('read to EOF'); 00929 } 00930 $this->debug('read body of length ' . strlen($data)); 00931 $this->incoming_payload .= $data; 00932 $this->debug('received a total of '.strlen($this->incoming_payload).' bytes of data from server'); 00933 00934 // close filepointer 00935 if( 00936 (isset($this->incoming_headers['connection']) && strtolower($this->incoming_headers['connection']) == 'close') || 00937 (! $this->persistentConnection) || feof($this->fp)){ 00938 fclose($this->fp); 00939 $this->fp = false; 00940 $this->debug('closed socket'); 00941 } 00942 00943 // connection was closed unexpectedly 00944 if($this->incoming_payload == ''){ 00945 $this->setError('no response from server'); 00946 return false; 00947 } 00948 00949 // decode transfer-encoding 00950 // if(isset($this->incoming_headers['transfer-encoding']) && strtolower($this->incoming_headers['transfer-encoding']) == 'chunked'){ 00951 // if(!$data = $this->decodeChunked($data, $lb)){ 00952 // $this->setError('Decoding of chunked data failed'); 00953 // return false; 00954 // } 00955 //print "<pre>\nde-chunked:\n---------------\n$data\n\n---------------\n</pre>"; 00956 // set decoded payload 00957 // $this->incoming_payload = $header_data.$lb.$lb.$data; 00958 // } 00959 00960 } else if ($this->io_method() == 'curl') { 00961 // send and receive 00962 $this->debug('send and receive with cURL'); 00963 $this->incoming_payload = curl_exec($this->ch); 00964 $data = $this->incoming_payload; 00965 00966 $cErr = curl_error($this->ch); 00967 if ($cErr != '') { 00968 $err = 'cURL ERROR: '.curl_errno($this->ch).': '.$cErr.'<br>'; 00969 // TODO: there is a PHP bug that can cause this to SEGV for CURLINFO_CONTENT_TYPE 00970 foreach(curl_getinfo($this->ch) as $k => $v){ 00971 $err .= "$k: $v<br>"; 00972 } 00973 $this->debug($err); 00974 $this->setError($err); 00975 curl_close($this->ch); 00976 return false; 00977 } else { 00978 //echo '<pre>'; 00979 //var_dump(curl_getinfo($this->ch)); 00980 //echo '</pre>'; 00981 } 00982 // close curl 00983 $this->debug('No cURL error, closing cURL'); 00984 curl_close($this->ch); 00985 00986 // try removing skippable headers 00987 $savedata = $data; 00988 while ($this->isSkippableCurlHeader($data)) { 00989 $this->debug("Found HTTP header to skip"); 00990 if ($pos = strpos($data,"\r\n\r\n")) { 00991 $data = ltrim(substr($data,$pos)); 00992 } elseif($pos = strpos($data,"\n\n") ) { 00993 $data = ltrim(substr($data,$pos)); 00994 } 00995 } 00996 00997 if ($data == '') { 00998 // have nothing left; just remove 100 header(s) 00999 $data = $savedata; 01000 while (ereg('^HTTP/1.1 100',$data)) { 01001 if ($pos = strpos($data,"\r\n\r\n")) { 01002 $data = ltrim(substr($data,$pos)); 01003 } elseif($pos = strpos($data,"\n\n") ) { 01004 $data = ltrim(substr($data,$pos)); 01005 } 01006 } 01007 } 01008 01009 // separate content from HTTP headers 01010 if ($pos = strpos($data,"\r\n\r\n")) { 01011 $lb = "\r\n"; 01012 } elseif( $pos = strpos($data,"\n\n")) { 01013 $lb = "\n"; 01014 } else { 01015 $this->debug('no proper separation of headers and document'); 01016 $this->setError('no proper separation of headers and document'); 01017 return false; 01018 } 01019 $header_data = trim(substr($data,0,$pos)); 01020 $header_array = explode($lb,$header_data); 01021 $data = ltrim(substr($data,$pos)); 01022 $this->debug('found proper separation of headers and document'); 01023 $this->debug('cleaned data, stringlen: '.strlen($data)); 01024 // clean headers 01025 foreach ($header_array as $header_line) { 01026 $arr = explode(':',$header_line,2); 01027 if(count($arr) > 1){ 01028 $header_name = strtolower(trim($arr[0])); 01029 $this->incoming_headers[$header_name] = trim($arr[1]); 01030 if ($header_name == 'set-cookie') { 01031 // TODO: allow multiple cookies from parseCookie 01032 $cookie = $this->parseCookie(trim($arr[1])); 01033 if ($cookie) { 01034 $this->incoming_cookies[] = $cookie; 01035 $this->debug('found cookie: ' . $cookie['name'] . ' = ' . $cookie['value']); 01036 } else { 01037 $this->debug('did not find cookie in ' . trim($arr[1])); 01038 } 01039 } 01040 } else if (isset($header_name)) { 01041 // append continuation line to previous header 01042 $this->incoming_headers[$header_name] .= $lb . ' ' . $header_line; 01043 } 01044 } 01045 } 01046 01047 $this->response_status_line = $header_array[0]; 01048 $arr = explode(' ', $this->response_status_line, 3); 01049 $http_version = $arr[0]; 01050 $http_status = intval($arr[1]); 01051 $http_reason = count($arr) > 2 ? $arr[2] : ''; 01052 01053 // see if we need to resend the request with http digest authentication 01054 if (isset($this->incoming_headers['location']) && ($http_status == 301 || $http_status == 302)) { 01055 $this->debug("Got $http_status $http_reason with Location: " . $this->incoming_headers['location']); 01056 $this->setURL($this->incoming_headers['location']); 01057 $this->tryagain = true; 01058 return false; 01059 } 01060 01061 // see if we need to resend the request with http digest authentication 01062 if (isset($this->incoming_headers['www-authenticate']) && $http_status == 401) { 01063 $this->debug("Got 401 $http_reason with WWW-Authenticate: " . $this->incoming_headers['www-authenticate']); 01064 if (strstr($this->incoming_headers['www-authenticate'], "Digest ")) { 01065 $this->debug('Server wants digest authentication'); 01066 // remove "Digest " from our elements 01067 $digestString = str_replace('Digest ', '', $this->incoming_headers['www-authenticate']); 01068 01069 // parse elements into array 01070 $digestElements = explode(',', $digestString); 01071 foreach ($digestElements as $val) { 01072 $tempElement = explode('=', trim($val), 2); 01073 $digestRequest[$tempElement[0]] = str_replace("\"", '', $tempElement[1]); 01074 } 01075 01076 // should have (at least) qop, realm, nonce 01077 if (isset($digestRequest['nonce'])) { 01078 $this->setCredentials($this->username, $this->password, 'digest', $digestRequest); 01079 $this->tryagain = true; 01080 return false; 01081 } 01082 } 01083 $this->debug('HTTP authentication failed'); 01084 $this->setError('HTTP authentication failed'); 01085 return false; 01086 } 01087 01088 if ( 01089 ($http_status >= 300 && $http_status <= 307) || 01090 ($http_status >= 400 && $http_status <= 417) || 01091 ($http_status >= 501 && $http_status <= 505) 01092 ) { 01093 $this->setError("Unsupported HTTP response status $http_status $http_reason (soapclient->response has contents of the response)"); 01094 return false; 01095 } 01096 01097 // decode content-encoding 01098 if(isset($this->incoming_headers['content-encoding']) && $this->incoming_headers['content-encoding'] != ''){ 01099 if(strtolower($this->incoming_headers['content-encoding']) == 'deflate' || strtolower($this->incoming_headers['content-encoding']) == 'gzip'){ 01100 // if decoding works, use it. else assume data wasn't gzencoded 01101 if(function_exists('gzinflate')){ 01102 //$timer->setMarker('starting decoding of gzip/deflated content'); 01103 // IIS 5 requires gzinflate instead of gzuncompress (similar to IE 5 and gzdeflate v. gzcompress) 01104 // this means there are no Zlib headers, although there should be 01105 $this->debug('The gzinflate function exists'); 01106 $datalen = strlen($data); 01107 if ($this->incoming_headers['content-encoding'] == 'deflate') { 01108 if ($degzdata = @gzinflate($data)) { 01109 $data = $degzdata; 01110 $this->debug('The payload has been inflated to ' . strlen($data) . ' bytes'); 01111 if (strlen($data) < $datalen) { 01112 // test for the case that the payload has been compressed twice 01113 $this->debug('The inflated payload is smaller than the gzipped one; try again'); 01114 if ($degzdata = @gzinflate($data)) { 01115 $data = $degzdata; 01116 $this->debug('The payload has been inflated again to ' . strlen($data) . ' bytes'); 01117 } 01118 } 01119 } else { 01120 $this->debug('Error using gzinflate to inflate the payload'); 01121 $this->setError('Error using gzinflate to inflate the payload'); 01122 } 01123 } elseif ($this->incoming_headers['content-encoding'] == 'gzip') { 01124 if ($degzdata = @gzinflate(substr($data, 10))) { // do our best 01125 $data = $degzdata; 01126 $this->debug('The payload has been un-gzipped to ' . strlen($data) . ' bytes'); 01127 if (strlen($data) < $datalen) { 01128 // test for the case that the payload has been compressed twice 01129 $this->debug('The un-gzipped payload is smaller than the gzipped one; try again'); 01130 if ($degzdata = @gzinflate(substr($data, 10))) { 01131 $data = $degzdata; 01132 $this->debug('The payload has been un-gzipped again to ' . strlen($data) . ' bytes'); 01133 } 01134 } 01135 } else { 01136 $this->debug('Error using gzinflate to un-gzip the payload'); 01137 $this->setError('Error using gzinflate to un-gzip the payload'); 01138 } 01139 } 01140 //$timer->setMarker('finished decoding of gzip/deflated content'); 01141 //print "<xmp>\nde-inflated:\n---------------\n$data\n-------------\n</xmp>"; 01142 // set decoded payload 01143 $this->incoming_payload = $header_data.$lb.$lb.$data; 01144 } else { 01145 $this->debug('The server sent compressed data. Your php install must have the Zlib extension compiled in to support this.'); 01146 $this->setError('The server sent compressed data. Your php install must have the Zlib extension compiled in to support this.'); 01147 } 01148 } else { 01149 $this->debug('Unsupported Content-Encoding ' . $this->incoming_headers['content-encoding']); 01150 $this->setError('Unsupported Content-Encoding ' . $this->incoming_headers['content-encoding']); 01151 } 01152 } else { 01153 $this->debug('No Content-Encoding header'); 01154 } 01155 01156 if(strlen($data) == 0){ 01157 $this->debug('no data after headers!'); 01158 $this->setError('no data present after HTTP headers'); 01159 return false; 01160 } 01161 01162 return $data; 01163 } 01164 01172 function setContentType($type, $charset = false) { 01173 $this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : '')); 01174 } 01175 01182 function usePersistentConnection(){ 01183 if (isset($this->outgoing_headers['Accept-Encoding'])) { 01184 return false; 01185 } 01186 $this->protocol_version = '1.1'; 01187 $this->persistentConnection = true; 01188 $this->setHeader('Connection', 'Keep-Alive'); 01189 return true; 01190 } 01191 01199 /* 01200 * TODO: allow a Set-Cookie string to be parsed into multiple cookies 01201 */ 01202 function parseCookie($cookie_str) { 01203 $cookie_str = str_replace('; ', ';', $cookie_str) . ';'; 01204 $data = split(';', $cookie_str); 01205 $value_str = $data[0]; 01206 01207 $cookie_param = 'domain='; 01208 $start = strpos($cookie_str, $cookie_param); 01209 if ($start > 0) { 01210 $domain = substr($cookie_str, $start + strlen($cookie_param)); 01211 $domain = substr($domain, 0, strpos($domain, ';')); 01212 } else { 01213 $domain = ''; 01214 } 01215 01216 $cookie_param = 'expires='; 01217 $start = strpos($cookie_str, $cookie_param); 01218 if ($start > 0) { 01219 $expires = substr($cookie_str, $start + strlen($cookie_param)); 01220 $expires = substr($expires, 0, strpos($expires, ';')); 01221 } else { 01222 $expires = ''; 01223 } 01224 01225 $cookie_param = 'path='; 01226 $start = strpos($cookie_str, $cookie_param); 01227 if ( $start > 0 ) { 01228 $path = substr($cookie_str, $start + strlen($cookie_param)); 01229 $path = substr($path, 0, strpos($path, ';')); 01230 } else { 01231 $path = '/'; 01232 } 01233 01234 $cookie_param = ';secure;'; 01235 if (strpos($cookie_str, $cookie_param) !== FALSE) { 01236 $secure = true; 01237 } else { 01238 $secure = false; 01239 } 01240 01241 $sep_pos = strpos($value_str, '='); 01242 01243 if ($sep_pos) { 01244 $name = substr($value_str, 0, $sep_pos); 01245 $value = substr($value_str, $sep_pos + 1); 01246 $cookie= array( 'name' => $name, 01247 'value' => $value, 01248 'domain' => $domain, 01249 'path' => $path, 01250 'expires' => $expires, 01251 'secure' => $secure 01252 ); 01253 return $cookie; 01254 } 01255 return false; 01256 } 01257 01266 function getCookiesForRequest($cookies, $secure=false) { 01267 $cookie_str = ''; 01268 if ((! is_null($cookies)) && (is_array($cookies))) { 01269 foreach ($cookies as $cookie) { 01270 if (! is_array($cookie)) { 01271 continue; 01272 } 01273 $this->debug("check cookie for validity: ".$cookie['name'].'='.$cookie['value']); 01274 if ((isset($cookie['expires'])) && (! empty($cookie['expires']))) { 01275 if (strtotime($cookie['expires']) <= time()) { 01276 $this->debug('cookie has expired'); 01277 continue; 01278 } 01279 } 01280 if ((isset($cookie['domain'])) && (! empty($cookie['domain']))) { 01281 $domain = preg_quote($cookie['domain']); 01282 if (! preg_match("'.*$domain$'i", $this->host)) { 01283 $this->debug('cookie has different domain'); 01284 continue; 01285 } 01286 } 01287 if ((isset($cookie['path'])) && (! empty($cookie['path']))) { 01288 $path = preg_quote($cookie['path']); 01289 if (! preg_match("'^$path.*'i", $this->path)) { 01290 $this->debug('cookie is for a different path'); 01291 continue; 01292 } 01293 } 01294 if ((! $secure) && (isset($cookie['secure'])) && ($cookie['secure'])) { 01295 $this->debug('cookie is secure, transport is not'); 01296 continue; 01297 } 01298 $cookie_str .= $cookie['name'] . '=' . $cookie['value'] . '; '; 01299 $this->debug('add cookie to Cookie-String: ' . $cookie['name'] . '=' . $cookie['value']); 01300 } 01301 } 01302 return $cookie_str; 01303 } 01304 } 01305 01306 01307 ?>
Copyright © 2003 - 2009 MyOOS [Shopsystem]. All rights reserved. MyOOS [Shopsystem] is Free Software released under the GNU/GPL License. Webmaster: info@r23.de (Impressum) |
|