PageRenderTime 32ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/include/nusoap/class.soap_transport_http.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 1359 lines | 934 code | 89 blank | 336 comment | 244 complexity | fc8d8de37eeea8b79c9963bd8ef1357b MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. /*
  3. Modification information for LGPL compliance
  4. r57813 - 2010-08-19 10:34:44 -0700 (Thu, 19 Aug 2010) - kjing - Author: John Mertic <jmertic@sugarcrm.com>
  5. Bug 39085 - When loading the opposite search panel via ajax on the ListViews, call the index action instead of the ListView action to avoid touching pre-MVC code by accident.
  6. r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
  7. r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
  8. r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
  9. r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
  10. r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
  11. r51509 - 2009-10-14 07:47:28 -0700 (Wed, 14 Oct 2009) - jmertic - More fallout fixes from the PHP 5.3 ereg to preg changes.
  12. r51491 - 2009-10-13 13:09:52 -0700 (Tue, 13 Oct 2009) - jmertic - Fixed lint errors found in this file.
  13. r51443 - 2009-10-12 13:34:36 -0700 (Mon, 12 Oct 2009) - jmertic - Bug 33332 - Made application PHP 5.3 compliant with E_DEPRECATED warnings on by:
  14. - Changing all ereg function to either preg or simple string based ones
  15. - No more references to magic quotes.
  16. - Change all the session_unregister() functions to just unset() the correct session variable instead.
  17. r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
  18. r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
  19. r13782 - 2006-06-06 10:58:55 -0700 (Tue, 06 Jun 2006) - majed - changes entry point code
  20. r11115 - 2006-01-17 14:54:45 -0800 (Tue, 17 Jan 2006) - majed - add entry point validation
  21. r8846 - 2005-10-31 11:01:12 -0800 (Mon, 31 Oct 2005) - majed - new version of nusoap
  22. r7452 - 2005-08-17 11:32:34 -0700 (Wed, 17 Aug 2005) - majed - changes soap to nusoap
  23. r5462 - 2005-05-25 13:50:11 -0700 (Wed, 25 May 2005) - majed - upgraded nusoap to .6.9
  24. r573 - 2004-09-04 13:03:32 -0700 (Sat, 04 Sep 2004) - sugarclint - undoing copyrights added in inadvertantly. --clint
  25. r546 - 2004-09-03 11:49:38 -0700 (Fri, 03 Sep 2004) - sugarmsi - removed echo count
  26. r354 - 2004-08-02 23:00:37 -0700 (Mon, 02 Aug 2004) - sugarjacob - Adding Soap
  27. */
  28. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  29. /**
  30. * transport class for sending/receiving data via HTTP and HTTPS
  31. * NOTE: PHP must be compiled with the CURL extension for HTTPS support
  32. *
  33. * @author Dietrich Ayala <dietrich@ganx4.com>
  34. * @author Scott Nichol <snichol@users.sourceforge.net>
  35. * @access public
  36. */
  37. class soap_transport_http extends nusoap_base {
  38. var $url = '';
  39. var $uri = '';
  40. var $digest_uri = '';
  41. var $scheme = '';
  42. var $host = '';
  43. var $port = '';
  44. var $path = '';
  45. var $request_method = 'POST';
  46. var $protocol_version = '1.0';
  47. var $encoding = '';
  48. var $outgoing_headers = array();
  49. var $incoming_headers = array();
  50. var $incoming_cookies = array();
  51. var $outgoing_payload = '';
  52. var $incoming_payload = '';
  53. var $response_status_line; // HTTP response status line
  54. var $useSOAPAction = true;
  55. var $persistentConnection = false;
  56. var $ch = false; // cURL handle
  57. var $ch_options = array(); // cURL custom options
  58. var $use_curl = false; // force cURL use
  59. var $proxy = null; // proxy information (associative array)
  60. var $username = '';
  61. var $password = '';
  62. var $authtype = '';
  63. var $digestRequest = array();
  64. var $certRequest = array(); // keys must be cainfofile (optional), sslcertfile, sslkeyfile, passphrase, certpassword (optional), verifypeer (optional), verifyhost (optional)
  65. // cainfofile: certificate authority file, e.g. '$pathToPemFiles/rootca.pem'
  66. // sslcertfile: SSL certificate file, e.g. '$pathToPemFiles/mycert.pem'
  67. // sslkeyfile: SSL key file, e.g. '$pathToPemFiles/mykey.pem'
  68. // passphrase: SSL key password/passphrase
  69. // certpassword: SSL certificate password
  70. // verifypeer: default is 1
  71. // verifyhost: default is 1
  72. /**
  73. * constructor
  74. *
  75. * @param string $url The URL to which to connect
  76. * @param array $curl_options User-specified cURL options
  77. * @param boolean $use_curl Whether to try to force cURL use
  78. * @access public
  79. */
  80. function soap_transport_http($url, $curl_options = NULL, $use_curl = false){
  81. parent::nusoap_base();
  82. $this->debug("ctor url=$url use_curl=$use_curl curl_options:");
  83. $this->appendDebug($this->varDump($curl_options));
  84. $this->setURL($url);
  85. if (is_array($curl_options)) {
  86. $this->ch_options = $curl_options;
  87. }
  88. $this->use_curl = $use_curl;
  89. preg_match('/\$Revisio' . 'n: ([^ ]+)/', $this->revision, $rev);
  90. $this->setHeader('User-Agent', $this->title.'/'.$this->version.' ('.$rev[1].')');
  91. }
  92. /**
  93. * sets a cURL option
  94. *
  95. * @param mixed $option The cURL option (always integer?)
  96. * @param mixed $value The cURL option value
  97. * @access private
  98. */
  99. function setCurlOption($option, $value) {
  100. $this->debug("setCurlOption option=$option, value=");
  101. $this->appendDebug($this->varDump($value));
  102. curl_setopt($this->ch, $option, $value);
  103. }
  104. /**
  105. * sets an HTTP header
  106. *
  107. * @param string $name The name of the header
  108. * @param string $value The value of the header
  109. * @access private
  110. */
  111. function setHeader($name, $value) {
  112. $this->outgoing_headers[$name] = $value;
  113. $this->debug("set header $name: $value");
  114. }
  115. /**
  116. * unsets an HTTP header
  117. *
  118. * @param string $name The name of the header
  119. * @access private
  120. */
  121. function unsetHeader($name) {
  122. if (isset($this->outgoing_headers[$name])) {
  123. $this->debug("unset header $name");
  124. unset($this->outgoing_headers[$name]);
  125. }
  126. }
  127. /**
  128. * sets the URL to which to connect
  129. *
  130. * @param string $url The URL to which to connect
  131. * @access private
  132. */
  133. function setURL($url) {
  134. $this->url = $url;
  135. $u = parse_url($url);
  136. foreach($u as $k => $v){
  137. $this->debug("parsed URL $k = $v");
  138. $this->$k = $v;
  139. }
  140. // add any GET params to path
  141. if(isset($u['query']) && $u['query'] != ''){
  142. $this->path .= '?' . $u['query'];
  143. }
  144. // set default port
  145. if(!isset($u['port'])){
  146. if($u['scheme'] == 'https'){
  147. $this->port = 443;
  148. } else {
  149. $this->port = 80;
  150. }
  151. }
  152. $this->uri = $this->path;
  153. $this->digest_uri = $this->uri;
  154. // build headers
  155. if (!isset($u['port'])) {
  156. $this->setHeader('Host', $this->host);
  157. } else {
  158. $this->setHeader('Host', $this->host.':'.$this->port);
  159. }
  160. if (isset($u['user']) && $u['user'] != '') {
  161. $this->setCredentials(urldecode($u['user']), isset($u['pass']) ? urldecode($u['pass']) : '');
  162. }
  163. }
  164. /**
  165. * gets the I/O method to use
  166. *
  167. * @return string I/O method to use (socket|curl|unknown)
  168. * @access private
  169. */
  170. function io_method() {
  171. if ($this->use_curl || ($this->scheme == 'https') || ($this->scheme == 'http' && $this->authtype == 'ntlm') || ($this->scheme == 'http' && is_array($this->proxy) && $this->proxy['authtype'] == 'ntlm'))
  172. return 'curl';
  173. if (($this->scheme == 'http' || $this->scheme == 'ssl') && $this->authtype != 'ntlm' && (!is_array($this->proxy) || $this->proxy['authtype'] != 'ntlm'))
  174. return 'socket';
  175. return 'unknown';
  176. }
  177. /**
  178. * establish an HTTP connection
  179. *
  180. * @param integer $timeout set connection timeout in seconds
  181. * @param integer $response_timeout set response timeout in seconds
  182. * @return boolean true if connected, false if not
  183. * @access private
  184. */
  185. function connect($connection_timeout=0,$response_timeout=30){
  186. // For PHP 4.3 with OpenSSL, change https scheme to ssl, then treat like
  187. // "regular" socket.
  188. // TODO: disabled for now because OpenSSL must be *compiled* in (not just
  189. // loaded), and until PHP5 stream_get_wrappers is not available.
  190. // if ($this->scheme == 'https') {
  191. // if (version_compare(phpversion(), '4.3.0') >= 0) {
  192. // if (extension_loaded('openssl')) {
  193. // $this->scheme = 'ssl';
  194. // $this->debug('Using SSL over OpenSSL');
  195. // }
  196. // }
  197. // }
  198. $this->debug("connect connection_timeout $connection_timeout, response_timeout $response_timeout, scheme $this->scheme, host $this->host, port $this->port");
  199. if ($this->io_method() == 'socket') {
  200. if (!is_array($this->proxy)) {
  201. $host = $this->host;
  202. $port = $this->port;
  203. } else {
  204. $host = $this->proxy['host'];
  205. $port = $this->proxy['port'];
  206. }
  207. // use persistent connection
  208. if($this->persistentConnection && isset($this->fp) && is_resource($this->fp)){
  209. if (!feof($this->fp)) {
  210. $this->debug('Re-use persistent connection');
  211. return true;
  212. }
  213. fclose($this->fp);
  214. $this->debug('Closed persistent connection at EOF');
  215. }
  216. // munge host if using OpenSSL
  217. if ($this->scheme == 'ssl') {
  218. $host = 'ssl://' . $host;
  219. }
  220. $this->debug('calling fsockopen with host ' . $host . ' connection_timeout ' . $connection_timeout);
  221. // open socket
  222. if($connection_timeout > 0){
  223. $this->fp = @fsockopen( $host, $this->port, $this->errno, $this->error_str, $connection_timeout);
  224. } else {
  225. $this->fp = @fsockopen( $host, $this->port, $this->errno, $this->error_str);
  226. }
  227. // test pointer
  228. if(!$this->fp) {
  229. $msg = 'Couldn\'t open socket connection to server ' . $this->url;
  230. if ($this->errno) {
  231. $msg .= ', Error ('.$this->errno.'): '.$this->error_str;
  232. } else {
  233. $msg .= ' prior to connect(). This is often a problem looking up the host name.';
  234. }
  235. $this->debug($msg);
  236. $this->setError($msg);
  237. return false;
  238. }
  239. // set response timeout
  240. $this->debug('set response timeout to ' . $response_timeout);
  241. socket_set_timeout( $this->fp, $response_timeout);
  242. $this->debug('socket connected');
  243. return true;
  244. } else if ($this->io_method() == 'curl') {
  245. if (!extension_loaded('curl')) {
  246. // $this->setError('cURL Extension, or OpenSSL extension w/ PHP version >= 4.3 is required for HTTPS');
  247. $this->setError('The PHP cURL Extension is required for HTTPS or NLTM. You will need to re-build or update your PHP to include cURL or change php.ini to load the PHP cURL extension.');
  248. return false;
  249. }
  250. // Avoid warnings when PHP does not have these options
  251. if (defined('CURLOPT_CONNECTIONTIMEOUT'))
  252. $CURLOPT_CONNECTIONTIMEOUT = CURLOPT_CONNECTIONTIMEOUT;
  253. else
  254. $CURLOPT_CONNECTIONTIMEOUT = 78;
  255. if (defined('CURLOPT_HTTPAUTH'))
  256. $CURLOPT_HTTPAUTH = CURLOPT_HTTPAUTH;
  257. else
  258. $CURLOPT_HTTPAUTH = 107;
  259. if (defined('CURLOPT_PROXYAUTH'))
  260. $CURLOPT_PROXYAUTH = CURLOPT_PROXYAUTH;
  261. else
  262. $CURLOPT_PROXYAUTH = 111;
  263. if (defined('CURLAUTH_BASIC'))
  264. $CURLAUTH_BASIC = CURLAUTH_BASIC;
  265. else
  266. $CURLAUTH_BASIC = 1;
  267. if (defined('CURLAUTH_DIGEST'))
  268. $CURLAUTH_DIGEST = CURLAUTH_DIGEST;
  269. else
  270. $CURLAUTH_DIGEST = 2;
  271. if (defined('CURLAUTH_NTLM'))
  272. $CURLAUTH_NTLM = CURLAUTH_NTLM;
  273. else
  274. $CURLAUTH_NTLM = 8;
  275. $this->debug('connect using cURL');
  276. // init CURL
  277. $this->ch = curl_init();
  278. // set url
  279. $hostURL = ($this->port != '') ? "$this->scheme://$this->host:$this->port" : "$this->scheme://$this->host";
  280. // add path
  281. $hostURL .= $this->path;
  282. $this->setCurlOption(CURLOPT_URL, $hostURL);
  283. // follow location headers (re-directs)
  284. if (ini_get('safe_mode') || ini_get('open_basedir')) {
  285. $this->debug('safe_mode or open_basedir set, so do not set CURLOPT_FOLLOWLOCATION');
  286. $this->debug('safe_mode = ');
  287. $this->appendDebug($this->varDump(ini_get('safe_mode')));
  288. $this->debug('open_basedir = ');
  289. $this->appendDebug($this->varDump(ini_get('open_basedir')));
  290. } else {
  291. $this->setCurlOption(CURLOPT_FOLLOWLOCATION, 1);
  292. }
  293. // ask for headers in the response output
  294. $this->setCurlOption(CURLOPT_HEADER, 1);
  295. // ask for the response output as the return value
  296. $this->setCurlOption(CURLOPT_RETURNTRANSFER, 1);
  297. // encode
  298. // We manage this ourselves through headers and encoding
  299. // if(function_exists('gzuncompress')){
  300. // $this->setCurlOption(CURLOPT_ENCODING, 'deflate');
  301. // }
  302. // persistent connection
  303. if ($this->persistentConnection) {
  304. // I believe the following comment is now bogus, having applied to
  305. // the code when it used CURLOPT_CUSTOMREQUEST to send the request.
  306. // The way we send data, we cannot use persistent connections, since
  307. // there will be some "junk" at the end of our request.
  308. //$this->setCurlOption(CURL_HTTP_VERSION_1_1, true);
  309. $this->persistentConnection = false;
  310. $this->setHeader('Connection', 'close');
  311. }
  312. // set timeouts
  313. if ($connection_timeout != 0) {
  314. $this->setCurlOption($CURLOPT_CONNECTIONTIMEOUT, $connection_timeout);
  315. }
  316. if ($response_timeout != 0) {
  317. $this->setCurlOption(CURLOPT_TIMEOUT, $response_timeout);
  318. }
  319. if ($this->scheme == 'https') {
  320. $this->debug('set cURL SSL verify options');
  321. // recent versions of cURL turn on peer/host checking by default,
  322. // while PHP binaries are not compiled with a default location for the
  323. // CA cert bundle, so disable peer/host checking.
  324. //$this->setCurlOption(CURLOPT_CAINFO, 'f:\php-4.3.2-win32\extensions\curl-ca-bundle.crt');
  325. $this->setCurlOption(CURLOPT_SSL_VERIFYPEER, 0);
  326. $this->setCurlOption(CURLOPT_SSL_VERIFYHOST, 0);
  327. // support client certificates (thanks Tobias Boes, Doug Anarino, Eryan Ariobowo)
  328. if ($this->authtype == 'certificate') {
  329. $this->debug('set cURL certificate options');
  330. if (isset($this->certRequest['cainfofile'])) {
  331. $this->setCurlOption(CURLOPT_CAINFO, $this->certRequest['cainfofile']);
  332. }
  333. if (isset($this->certRequest['verifypeer'])) {
  334. $this->setCurlOption(CURLOPT_SSL_VERIFYPEER, $this->certRequest['verifypeer']);
  335. } else {
  336. $this->setCurlOption(CURLOPT_SSL_VERIFYPEER, 1);
  337. }
  338. if (isset($this->certRequest['verifyhost'])) {
  339. $this->setCurlOption(CURLOPT_SSL_VERIFYHOST, $this->certRequest['verifyhost']);
  340. } else {
  341. $this->setCurlOption(CURLOPT_SSL_VERIFYHOST, 1);
  342. }
  343. if (isset($this->certRequest['sslcertfile'])) {
  344. $this->setCurlOption(CURLOPT_SSLCERT, $this->certRequest['sslcertfile']);
  345. }
  346. if (isset($this->certRequest['sslkeyfile'])) {
  347. $this->setCurlOption(CURLOPT_SSLKEY, $this->certRequest['sslkeyfile']);
  348. }
  349. if (isset($this->certRequest['passphrase'])) {
  350. $this->setCurlOption(CURLOPT_SSLKEYPASSWD, $this->certRequest['passphrase']);
  351. }
  352. if (isset($this->certRequest['certpassword'])) {
  353. $this->setCurlOption(CURLOPT_SSLCERTPASSWD, $this->certRequest['certpassword']);
  354. }
  355. }
  356. }
  357. if ($this->authtype && ($this->authtype != 'certificate')) {
  358. if ($this->username) {
  359. $this->debug('set cURL username/password');
  360. $this->setCurlOption(CURLOPT_USERPWD, "$this->username:$this->password");
  361. }
  362. if ($this->authtype == 'basic') {
  363. $this->debug('set cURL for Basic authentication');
  364. $this->setCurlOption($CURLOPT_HTTPAUTH, $CURLAUTH_BASIC);
  365. }
  366. if ($this->authtype == 'digest') {
  367. $this->debug('set cURL for digest authentication');
  368. $this->setCurlOption($CURLOPT_HTTPAUTH, $CURLAUTH_DIGEST);
  369. }
  370. if ($this->authtype == 'ntlm') {
  371. $this->debug('set cURL for NTLM authentication');
  372. $this->setCurlOption($CURLOPT_HTTPAUTH, $CURLAUTH_NTLM);
  373. }
  374. }
  375. if (is_array($this->proxy)) {
  376. $this->debug('set cURL proxy options');
  377. if ($this->proxy['port'] != '') {
  378. $this->setCurlOption(CURLOPT_PROXY, $this->proxy['host'].':'.$this->proxy['port']);
  379. } else {
  380. $this->setCurlOption(CURLOPT_PROXY, $this->proxy['host']);
  381. }
  382. if ($this->proxy['username'] || $this->proxy['password']) {
  383. $this->debug('set cURL proxy authentication options');
  384. $this->setCurlOption(CURLOPT_PROXYUSERPWD, $this->proxy['username'].':'.$this->proxy['password']);
  385. if ($this->proxy['authtype'] == 'basic') {
  386. $this->setCurlOption($CURLOPT_PROXYAUTH, $CURLAUTH_BASIC);
  387. }
  388. if ($this->proxy['authtype'] == 'ntlm') {
  389. $this->setCurlOption($CURLOPT_PROXYAUTH, $CURLAUTH_NTLM);
  390. }
  391. }
  392. }
  393. $this->debug('cURL connection set up');
  394. return true;
  395. } else {
  396. $this->setError('Unknown scheme ' . $this->scheme);
  397. $this->debug('Unknown scheme ' . $this->scheme);
  398. return false;
  399. }
  400. }
  401. /**
  402. * sends the SOAP request and gets the SOAP response via HTTP[S]
  403. *
  404. * @param string $data message data
  405. * @param integer $timeout set connection timeout in seconds
  406. * @param integer $response_timeout set response timeout in seconds
  407. * @param array $cookies cookies to send
  408. * @return string data
  409. * @access public
  410. */
  411. function send($data, $timeout=0, $response_timeout=30, $cookies=NULL) {
  412. $this->debug('entered send() with data of length: '.strlen($data));
  413. $this->tryagain = true;
  414. $tries = 0;
  415. while ($this->tryagain) {
  416. $this->tryagain = false;
  417. if ($tries++ < 2) {
  418. // make connnection
  419. if (!$this->connect($timeout, $response_timeout)){
  420. return false;
  421. }
  422. // send request
  423. if (!$this->sendRequest($data, $cookies)){
  424. return false;
  425. }
  426. // get response
  427. $respdata = $this->getResponse();
  428. } else {
  429. $this->setError("Too many tries to get an OK response ($this->response_status_line)");
  430. }
  431. }
  432. $this->debug('end of send()');
  433. return $respdata;
  434. }
  435. /**
  436. * sends the SOAP request and gets the SOAP response via HTTPS using CURL
  437. *
  438. * @param string $data message data
  439. * @param integer $timeout set connection timeout in seconds
  440. * @param integer $response_timeout set response timeout in seconds
  441. * @param array $cookies cookies to send
  442. * @return string data
  443. * @access public
  444. * @deprecated
  445. */
  446. function sendHTTPS($data, $timeout=0, $response_timeout=30, $cookies) {
  447. return $this->send($data, $timeout, $response_timeout, $cookies);
  448. }
  449. /**
  450. * if authenticating, set user credentials here
  451. *
  452. * @param string $username
  453. * @param string $password
  454. * @param string $authtype (basic|digest|certificate|ntlm)
  455. * @param array $digestRequest (keys must be nonce, nc, realm, qop)
  456. * @param array $certRequest (keys must be cainfofile (optional), sslcertfile, sslkeyfile, passphrase, certpassword (optional), verifypeer (optional), verifyhost (optional): see corresponding options in cURL docs)
  457. * @access public
  458. */
  459. function setCredentials($username, $password, $authtype = 'basic', $digestRequest = array(), $certRequest = array()) {
  460. $this->debug("setCredentials username=$username authtype=$authtype digestRequest=");
  461. $this->appendDebug($this->varDump($digestRequest));
  462. $this->debug("certRequest=");
  463. $this->appendDebug($this->varDump($certRequest));
  464. // cf. RFC 2617
  465. if ($authtype == 'basic') {
  466. $this->setHeader('Authorization', 'Basic '.base64_encode(str_replace(':','',$username).':'.$password));
  467. } elseif ($authtype == 'digest') {
  468. if (isset($digestRequest['nonce'])) {
  469. $digestRequest['nc'] = isset($digestRequest['nc']) ? $digestRequest['nc']++ : 1;
  470. // calculate the Digest hashes (calculate code based on digest implementation found at: http://www.rassoc.com/gregr/weblog/stories/2002/07/09/webServicesSecurityHttpDigestAuthenticationWithoutActiveDirectory.html)
  471. // A1 = unq(username-value) ":" unq(realm-value) ":" passwd
  472. $A1 = $username. ':' . (isset($digestRequest['realm']) ? $digestRequest['realm'] : '') . ':' . $password;
  473. // H(A1) = MD5(A1)
  474. $HA1 = md5($A1);
  475. // A2 = Method ":" digest-uri-value
  476. $A2 = $this->request_method . ':' . $this->digest_uri;
  477. // H(A2)
  478. $HA2 = md5($A2);
  479. // KD(secret, data) = H(concat(secret, ":", data))
  480. // if qop == auth:
  481. // request-digest = <"> < KD ( H(A1), unq(nonce-value)
  482. // ":" nc-value
  483. // ":" unq(cnonce-value)
  484. // ":" unq(qop-value)
  485. // ":" H(A2)
  486. // ) <">
  487. // if qop is missing,
  488. // request-digest = <"> < KD ( H(A1), unq(nonce-value) ":" H(A2) ) > <">
  489. $unhashedDigest = '';
  490. $nonce = isset($digestRequest['nonce']) ? $digestRequest['nonce'] : '';
  491. $cnonce = $nonce;
  492. if ($digestRequest['qop'] != '') {
  493. $unhashedDigest = $HA1 . ':' . $nonce . ':' . sprintf("%08d", $digestRequest['nc']) . ':' . $cnonce . ':' . $digestRequest['qop'] . ':' . $HA2;
  494. } else {
  495. $unhashedDigest = $HA1 . ':' . $nonce . ':' . $HA2;
  496. }
  497. $hashedDigest = md5($unhashedDigest);
  498. $opaque = '';
  499. if (isset($digestRequest['opaque'])) {
  500. $opaque = ', opaque="' . $digestRequest['opaque'] . '"';
  501. }
  502. $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 . '"');
  503. }
  504. } elseif ($authtype == 'certificate') {
  505. $this->certRequest = $certRequest;
  506. $this->debug('Authorization header not set for certificate');
  507. } elseif ($authtype == 'ntlm') {
  508. // do nothing
  509. $this->debug('Authorization header not set for ntlm');
  510. }
  511. $this->username = $username;
  512. $this->password = $password;
  513. $this->authtype = $authtype;
  514. $this->digestRequest = $digestRequest;
  515. }
  516. /**
  517. * set the soapaction value
  518. *
  519. * @param string $soapaction
  520. * @access public
  521. */
  522. function setSOAPAction($soapaction) {
  523. $this->setHeader('SOAPAction', '"' . $soapaction . '"');
  524. }
  525. /**
  526. * use http encoding
  527. *
  528. * @param string $enc encoding style. supported values: gzip, deflate, or both
  529. * @access public
  530. */
  531. function setEncoding($enc='gzip, deflate') {
  532. if (function_exists('gzdeflate')) {
  533. $this->protocol_version = '1.1';
  534. $this->setHeader('Accept-Encoding', $enc);
  535. if (!isset($this->outgoing_headers['Connection'])) {
  536. $this->setHeader('Connection', 'close');
  537. $this->persistentConnection = false;
  538. }
  539. // deprecated as of PHP 5.3.0
  540. //set_magic_quotes_runtime(0);
  541. $this->encoding = $enc;
  542. }
  543. }
  544. /**
  545. * set proxy info here
  546. *
  547. * @param string $proxyhost use an empty string to remove proxy
  548. * @param string $proxyport
  549. * @param string $proxyusername
  550. * @param string $proxypassword
  551. * @param string $proxyauthtype (basic|ntlm)
  552. * @access public
  553. */
  554. function setProxy($proxyhost, $proxyport, $proxyusername = '', $proxypassword = '', $proxyauthtype = 'basic') {
  555. if ($proxyhost) {
  556. $this->proxy = array(
  557. 'host' => $proxyhost,
  558. 'port' => $proxyport,
  559. 'username' => $proxyusername,
  560. 'password' => $proxypassword,
  561. 'authtype' => $proxyauthtype
  562. );
  563. if ($proxyusername != '' && $proxypassword != '' && $proxyauthtype = 'basic') {
  564. $this->setHeader('Proxy-Authorization', ' Basic '.base64_encode($proxyusername.':'.$proxypassword));
  565. }
  566. } else {
  567. $this->debug('remove proxy');
  568. $proxy = null;
  569. unsetHeader('Proxy-Authorization');
  570. }
  571. }
  572. /**
  573. * Test if the given string starts with a header that is to be skipped.
  574. * Skippable headers result from chunked transfer and proxy requests.
  575. *
  576. * @param string $data The string to check.
  577. * @returns boolean Whether a skippable header was found.
  578. * @access private
  579. */
  580. function isSkippableCurlHeader(&$data) {
  581. $skipHeaders = array( 'HTTP/1.1 100',
  582. 'HTTP/1.0 301',
  583. 'HTTP/1.1 301',
  584. 'HTTP/1.0 302',
  585. 'HTTP/1.1 302',
  586. 'HTTP/1.0 401',
  587. 'HTTP/1.1 401',
  588. 'HTTP/1.0 200 Connection established');
  589. foreach ($skipHeaders as $hd) {
  590. $prefix = substr($data, 0, strlen($hd));
  591. if ($prefix == $hd) return true;
  592. }
  593. return false;
  594. }
  595. /**
  596. * decode a string that is encoded w/ "chunked' transfer encoding
  597. * as defined in RFC2068 19.4.6
  598. *
  599. * @param string $buffer
  600. * @param string $lb
  601. * @returns string
  602. * @access public
  603. * @deprecated
  604. */
  605. function decodeChunked($buffer, $lb){
  606. // length := 0
  607. $length = 0;
  608. $new = '';
  609. // read chunk-size, chunk-extension (if any) and CRLF
  610. // get the position of the linebreak
  611. $chunkend = strpos($buffer, $lb);
  612. if ($chunkend == FALSE) {
  613. $this->debug('no linebreak found in decodeChunked');
  614. return $new;
  615. }
  616. $temp = substr($buffer,0,$chunkend);
  617. $chunk_size = hexdec( trim($temp) );
  618. $chunkstart = $chunkend + strlen($lb);
  619. // while (chunk-size > 0) {
  620. while ($chunk_size > 0) {
  621. $this->debug("chunkstart: $chunkstart chunk_size: $chunk_size");
  622. $chunkend = strpos( $buffer, $lb, $chunkstart + $chunk_size);
  623. // Just in case we got a broken connection
  624. if ($chunkend == FALSE) {
  625. $chunk = substr($buffer,$chunkstart);
  626. // append chunk-data to entity-body
  627. $new .= $chunk;
  628. $length += strlen($chunk);
  629. break;
  630. }
  631. // read chunk-data and CRLF
  632. $chunk = substr($buffer,$chunkstart,$chunkend-$chunkstart);
  633. // append chunk-data to entity-body
  634. $new .= $chunk;
  635. // length := length + chunk-size
  636. $length += strlen($chunk);
  637. // read chunk-size and CRLF
  638. $chunkstart = $chunkend + strlen($lb);
  639. $chunkend = strpos($buffer, $lb, $chunkstart) + strlen($lb);
  640. if ($chunkend == FALSE) {
  641. break; //Just in case we got a broken connection
  642. }
  643. $temp = substr($buffer,$chunkstart,$chunkend-$chunkstart);
  644. $chunk_size = hexdec( trim($temp) );
  645. $chunkstart = $chunkend;
  646. }
  647. return $new;
  648. }
  649. /**
  650. * Writes the payload, including HTTP headers, to $this->outgoing_payload.
  651. *
  652. * @param string $data HTTP body
  653. * @param string $cookie_str data for HTTP Cookie header
  654. * @return void
  655. * @access private
  656. */
  657. function buildPayload($data, $cookie_str = '') {
  658. // Note: for cURL connections, $this->outgoing_payload is ignored,
  659. // as is the Content-Length header, but these are still created as
  660. // debugging guides.
  661. // add content-length header
  662. if ($this->request_method != 'GET') {
  663. $this->setHeader('Content-Length', strlen($data));
  664. }
  665. // start building outgoing payload:
  666. if ($this->proxy) {
  667. $uri = $this->url;
  668. } else {
  669. $uri = $this->uri;
  670. }
  671. $req = "$this->request_method $uri HTTP/$this->protocol_version";
  672. $this->debug("HTTP request: $req");
  673. $this->outgoing_payload = "$req\r\n";
  674. // loop thru headers, serializing
  675. foreach($this->outgoing_headers as $k => $v){
  676. $hdr = $k.': '.$v;
  677. $this->debug("HTTP header: $hdr");
  678. $this->outgoing_payload .= "$hdr\r\n";
  679. }
  680. // add any cookies
  681. if ($cookie_str != '') {
  682. $hdr = 'Cookie: '.$cookie_str;
  683. $this->debug("HTTP header: $hdr");
  684. $this->outgoing_payload .= "$hdr\r\n";
  685. }
  686. // header/body separator
  687. $this->outgoing_payload .= "\r\n";
  688. // add data
  689. $this->outgoing_payload .= $data;
  690. }
  691. /**
  692. * sends the SOAP request via HTTP[S]
  693. *
  694. * @param string $data message data
  695. * @param array $cookies cookies to send
  696. * @return boolean true if OK, false if problem
  697. * @access private
  698. */
  699. function sendRequest($data, $cookies = NULL) {
  700. // build cookie string
  701. $cookie_str = $this->getCookiesForRequest($cookies, (($this->scheme == 'ssl') || ($this->scheme == 'https')));
  702. // build payload
  703. $this->buildPayload($data, $cookie_str);
  704. if ($this->io_method() == 'socket') {
  705. // send payload
  706. if(!fputs($this->fp, $this->outgoing_payload, strlen($this->outgoing_payload))) {
  707. $this->setError('couldn\'t write message data to socket');
  708. $this->debug('couldn\'t write message data to socket');
  709. return false;
  710. }
  711. $this->debug('wrote data to socket, length = ' . strlen($this->outgoing_payload));
  712. return true;
  713. } else if ($this->io_method() == 'curl') {
  714. // set payload
  715. // cURL does say this should only be the verb, and in fact it
  716. // turns out that the URI and HTTP version are appended to this, which
  717. // some servers refuse to work with (so we no longer use this method!)
  718. //$this->setCurlOption(CURLOPT_CUSTOMREQUEST, $this->outgoing_payload);
  719. $curl_headers = array();
  720. foreach($this->outgoing_headers as $k => $v){
  721. if ($k == 'Connection' || $k == 'Content-Length' || $k == 'Host' || $k == 'Authorization' || $k == 'Proxy-Authorization') {
  722. $this->debug("Skip cURL header $k: $v");
  723. } else {
  724. $curl_headers[] = "$k: $v";
  725. }
  726. }
  727. if ($cookie_str != '') {
  728. $curl_headers[] = 'Cookie: ' . $cookie_str;
  729. }
  730. $this->setCurlOption(CURLOPT_HTTPHEADER, $curl_headers);
  731. $this->debug('set cURL HTTP headers');
  732. if ($this->request_method == "POST") {
  733. $this->setCurlOption(CURLOPT_POST, 1);
  734. $this->setCurlOption(CURLOPT_POSTFIELDS, $data);
  735. $this->debug('set cURL POST data');
  736. } else {
  737. }
  738. // insert custom user-set cURL options
  739. foreach ($this->ch_options as $key => $val) {
  740. $this->setCurlOption($key, $val);
  741. }
  742. $this->debug('set cURL payload');
  743. return true;
  744. }
  745. }
  746. /**
  747. * gets the SOAP response via HTTP[S]
  748. *
  749. * @return string the response (also sets member variables like incoming_payload)
  750. * @access private
  751. */
  752. function getResponse(){
  753. $this->incoming_payload = '';
  754. if ($this->io_method() == 'socket') {
  755. // loop until headers have been retrieved
  756. $data = '';
  757. while (!isset($lb)){
  758. // We might EOF during header read.
  759. if(feof($this->fp)) {
  760. $this->incoming_payload = $data;
  761. $this->debug('found no headers before EOF after length ' . strlen($data));
  762. $this->debug("received before EOF:\n" . $data);
  763. $this->setError('server failed to send headers');
  764. return false;
  765. }
  766. $tmp = fgets($this->fp, 256);
  767. $tmplen = strlen($tmp);
  768. $this->debug("read line of $tmplen bytes: " . trim($tmp));
  769. if ($tmplen == 0) {
  770. $this->incoming_payload = $data;
  771. $this->debug('socket read of headers timed out after length ' . strlen($data));
  772. $this->debug("read before timeout: " . $data);
  773. $this->setError('socket read of headers timed out');
  774. return false;
  775. }
  776. $data .= $tmp;
  777. $pos = strpos($data,"\r\n\r\n");
  778. if($pos > 1){
  779. $lb = "\r\n";
  780. } else {
  781. $pos = strpos($data,"\n\n");
  782. if($pos > 1){
  783. $lb = "\n";
  784. }
  785. }
  786. // remove 100 headers
  787. if (isset($lb) && preg_match('/^HTTP\/1.1 100/',$data)) {
  788. unset($lb);
  789. $data = '';
  790. }//
  791. }
  792. // store header data
  793. $this->incoming_payload .= $data;
  794. $this->debug('found end of headers after length ' . strlen($data));
  795. // process headers
  796. $header_data = trim(substr($data,0,$pos));
  797. $header_array = explode($lb,$header_data);
  798. $this->incoming_headers = array();
  799. $this->incoming_cookies = array();
  800. foreach($header_array as $header_line){
  801. $arr = explode(':',$header_line, 2);
  802. if(count($arr) > 1){
  803. $header_name = strtolower(trim($arr[0]));
  804. $this->incoming_headers[$header_name] = trim($arr[1]);
  805. if ($header_name == 'set-cookie') {
  806. // TODO: allow multiple cookies from parseCookie
  807. $cookie = $this->parseCookie(trim($arr[1]));
  808. if ($cookie) {
  809. $this->incoming_cookies[] = $cookie;
  810. $this->debug('found cookie: ' . $cookie['name'] . ' = ' . $cookie['value']);
  811. } else {
  812. $this->debug('did not find cookie in ' . trim($arr[1]));
  813. }
  814. }
  815. } else if (isset($header_name)) {
  816. // append continuation line to previous header
  817. $this->incoming_headers[$header_name] .= $lb . ' ' . $header_line;
  818. }
  819. }
  820. // loop until msg has been received
  821. if (isset($this->incoming_headers['transfer-encoding']) && strtolower($this->incoming_headers['transfer-encoding']) == 'chunked') {
  822. $content_length = 2147483647; // ignore any content-length header
  823. $chunked = true;
  824. $this->debug("want to read chunked content");
  825. } elseif (isset($this->incoming_headers['content-length'])) {
  826. $content_length = $this->incoming_headers['content-length'];
  827. $chunked = false;
  828. $this->debug("want to read content of length $content_length");
  829. } else {
  830. $content_length = 2147483647;
  831. $chunked = false;
  832. $this->debug("want to read content to EOF");
  833. }
  834. $data = '';
  835. do {
  836. if ($chunked) {
  837. $tmp = fgets($this->fp, 256);
  838. $tmplen = strlen($tmp);
  839. $this->debug("read chunk line of $tmplen bytes");
  840. if ($tmplen == 0) {
  841. $this->incoming_payload = $data;
  842. $this->debug('socket read of chunk length timed out after length ' . strlen($data));
  843. $this->debug("read before timeout:\n" . $data);
  844. $this->setError('socket read of chunk length timed out');
  845. return false;
  846. }
  847. $content_length = hexdec(trim($tmp));
  848. $this->debug("chunk length $content_length");
  849. }
  850. $strlen = 0;
  851. while (($strlen < $content_length) && (!feof($this->fp))) {
  852. $readlen = min(8192, $content_length - $strlen);
  853. $tmp = fread($this->fp, $readlen);
  854. $tmplen = strlen($tmp);
  855. $this->debug("read buffer of $tmplen bytes");
  856. if (($tmplen == 0) && (!feof($this->fp))) {
  857. $this->incoming_payload = $data;
  858. $this->debug('socket read of body timed out after length ' . strlen($data));
  859. $this->debug("read before timeout:\n" . $data);
  860. $this->setError('socket read of body timed out');
  861. return false;
  862. }
  863. $strlen += $tmplen;
  864. $data .= $tmp;
  865. }
  866. if ($chunked && ($content_length > 0)) {
  867. $tmp = fgets($this->fp, 256);
  868. $tmplen = strlen($tmp);
  869. $this->debug("read chunk terminator of $tmplen bytes");
  870. if ($tmplen == 0) {
  871. $this->incoming_payload = $data;
  872. $this->debug('socket read of chunk terminator timed out after length ' . strlen($data));
  873. $this->debug("read before timeout:\n" . $data);
  874. $this->setError('socket read of chunk terminator timed out');
  875. return false;
  876. }
  877. }
  878. } while ($chunked && ($content_length > 0) && (!feof($this->fp)));
  879. if (feof($this->fp)) {
  880. $this->debug('read to EOF');
  881. }
  882. $this->debug('read body of length ' . strlen($data));
  883. $this->incoming_payload .= $data;
  884. $this->debug('received a total of '.strlen($this->incoming_payload).' bytes of data from server');
  885. // close filepointer
  886. if(
  887. (isset($this->incoming_headers['connection']) && strtolower($this->incoming_headers['connection']) == 'close') ||
  888. (! $this->persistentConnection) || feof($this->fp)){
  889. fclose($this->fp);
  890. $this->fp = false;
  891. $this->debug('closed socket');
  892. }
  893. // connection was closed unexpectedly
  894. if($this->incoming_payload == ''){
  895. $this->setError('no response from server');
  896. return false;
  897. }
  898. // decode transfer-encoding
  899. // if(isset($this->incoming_headers['transfer-encoding']) && strtolower($this->incoming_headers['transfer-encoding']) == 'chunked'){
  900. // if(!$data = $this->decodeChunked($data, $lb)){
  901. // $this->setError('Decoding of chunked data failed');
  902. // return false;
  903. // }
  904. //print "<pre>\nde-chunked:\n---------------\n$data\n\n---------------\n</pre>";
  905. // set decoded payload
  906. // $this->incoming_payload = $header_data.$lb.$lb.$data;
  907. // }
  908. } else if ($this->io_method() == 'curl') {
  909. // send and receive
  910. $this->debug('send and receive with cURL');
  911. $this->incoming_payload = curl_exec($this->ch);
  912. $data = $this->incoming_payload;
  913. $cErr = curl_error($this->ch);
  914. if ($cErr != '') {
  915. $err = 'cURL ERROR: '.curl_errno($this->ch).': '.$cErr.'<br>';
  916. // TODO: there is a PHP bug that can cause this to SEGV for CURLINFO_CONTENT_TYPE
  917. foreach(curl_getinfo($this->ch) as $k => $v){
  918. $err .= "$k: $v<br>";
  919. }
  920. $this->debug($err);
  921. $this->setError($err);
  922. curl_close($this->ch);
  923. return false;
  924. } else {
  925. //echo '<pre>';
  926. //var_dump(curl_getinfo($this->ch));
  927. //echo '</pre>';
  928. }
  929. // close curl
  930. $this->debug('No cURL error, closing cURL');
  931. curl_close($this->ch);
  932. // try removing skippable headers
  933. $savedata = $data;
  934. while ($this->isSkippableCurlHeader($data)) {
  935. $this->debug("Found HTTP header to skip");
  936. if ($pos = strpos($data,"\r\n\r\n")) {
  937. $data = ltrim(substr($data,$pos));
  938. } elseif($pos = strpos($data,"\n\n") ) {
  939. $data = ltrim(substr($data,$pos));
  940. }
  941. }
  942. if ($data == '') {
  943. // have nothing left; just remove 100 header(s)
  944. $data = $savedata;
  945. while (preg_match('/^HTTP\/1.1 100/',$data)) {
  946. if ($pos = strpos($data,"\r\n\r\n")) {
  947. $data = ltrim(substr($data,$pos));
  948. } elseif($pos = strpos($data,"\n\n") ) {
  949. $data = ltrim(substr($data,$pos));
  950. }
  951. }
  952. }
  953. // separate content from HTTP headers
  954. if ($pos = strpos($data,"\r\n\r\n")) {
  955. $lb = "\r\n";
  956. } elseif( $pos = strpos($data,"\n\n")) {
  957. $lb = "\n";
  958. } else {
  959. $this->debug('no proper separation of headers and document');
  960. $this->setError('no proper separation of headers and document');
  961. return false;
  962. }
  963. $header_data = trim(substr($data,0,$pos));
  964. $header_array = explode($lb,$header_data);
  965. $data = ltrim(substr($data,$pos));
  966. $this->debug('found proper separation of headers and document');
  967. $this->debug('cleaned data, stringlen: '.strlen($data));
  968. // clean headers
  969. foreach ($header_array as $header_line) {
  970. $arr = explode(':',$header_line,2);
  971. if(count($arr) > 1){
  972. $header_name = strtolower(trim($arr[0]));
  973. $this->incoming_headers[$header_name] = trim($arr[1]);
  974. if ($header_name == 'set-cookie') {
  975. // TODO: allow multiple cookies from parseCookie
  976. $cookie = $this->parseCookie(trim($arr[1]));
  977. if ($cookie) {
  978. $this->incoming_cookies[] = $cookie;
  979. $this->debug('found cookie: ' . $cookie['name'] . ' = ' . $cookie['value']);
  980. } else {
  981. $this->debug('did not find cookie in ' . trim($arr[1]));
  982. }
  983. }
  984. } else if (isset($header_name)) {
  985. // append continuation line to previous header
  986. $this->incoming_headers[$header_name] .= $lb . ' ' . $header_line;
  987. }
  988. }
  989. }
  990. $this->response_status_line = $header_array[0];
  991. $arr = explode(' ', $this->response_status_line, 3);
  992. $http_version = $arr[0];
  993. $http_status = intval($arr[1]);
  994. $http_reason = count($arr) > 2 ? $arr[2] : '';
  995. // see if we need to resend the request with http digest authentication
  996. if (isset($this->incoming_headers['location']) && ($http_status == 301 || $http_status == 302)) {
  997. $this->debug("Got $http_status $http_reason with Location: " . $this->incoming_headers['location']);
  998. $this->setURL($this->incoming_headers['location']);
  999. $this->tryagain = true;
  1000. return false;
  1001. }
  1002. // see if we need to resend the request with http digest authentication
  1003. if (isset($this->incoming_headers['www-authenticate']) && $http_status == 401) {
  1004. $this->debug("Got 401 $http_reason with WWW-Authenticate: " . $this->incoming_headers['www-authenticate']);
  1005. if (strstr($this->incoming_headers['www-authenticate'], "Digest ")) {
  1006. $this->debug('Server wants digest authentication');
  1007. // remove "Digest " from our elements
  1008. $digestString = str_replace('Digest ', '', $this->incoming_headers['www-authenticate']);
  1009. // parse elements into array
  1010. $digestElements = explode(',', $digestString);
  1011. foreach ($digestElements as $val) {
  1012. $tempElement = explode('=', trim($val), 2);
  1013. $digestRequest[$tempElement[0]] = str_replace("\"", '', $tempElement[1]);
  1014. }
  1015. // should have (at least) qop, realm, nonce
  1016. if (isset($digestRequest['nonce'])) {
  1017. $this->setCredentials($this->username, $this->password, 'digest', $digestRequest);
  1018. $this->tryagain = true;
  1019. return false;
  1020. }
  1021. }
  1022. $this->debug('HTTP authentication failed');
  1023. $this->setError('HTTP authentication failed');
  1024. return false;
  1025. }
  1026. if (
  1027. ($http_status >= 300 && $http_status <= 307) ||
  1028. ($http_status >= 400 && $http_status <= 417) ||
  1029. ($http_status >= 501 && $http_status <= 505)
  1030. ) {
  1031. $this->setError("Unsupported HTTP response status $http_status $http_reason (soapclient->response has contents of the response)");
  1032. return false;
  1033. }
  1034. // decode content-encoding
  1035. if(isset($this->incoming_headers['content-encoding']) && $this->incoming_headers['content-encoding'] != ''){
  1036. if(strtolower($this->incoming_headers['content-encoding']) == 'deflate' || strtolower($this->incoming_headers['content-encoding']) == 'gzip'){
  1037. // if decoding works, use it. else assume data wasn't gzencoded
  1038. if(function_exists('gzinflate')){
  1039. //$timer->setMarker('starting decoding of gzip/deflated content');
  1040. // IIS 5 requires gzinflate instead of gzuncompress (similar to IE 5 and gzdeflate v. gzcompress)
  1041. // this means there are no Zlib headers, although there should be
  1042. $this->debug('The gzinflate function exists');
  1043. $datalen = strlen($data);
  1044. if ($this->incoming_headers['content-encoding'] == 'deflate') {
  1045. if ($degzdata = @gzinflate($data)) {
  1046. $data = $degzdata;
  1047. $this->debug('The payload has been inflated to ' . strlen($data) . ' bytes');
  1048. if (strlen($data) < $datalen) {
  1049. // test for the case that the payload has been compressed twice
  1050. $this->debug('The inflated payload is smaller than the gzipped one; try again');
  1051. if ($degzdata = @gzinflate($data)) {
  1052. $data = $degzdata;
  1053. $this->debug('The payload has been inflated again to ' . strlen($data) . ' bytes');
  1054. }
  1055. }
  1056. } else {
  1057. $this->debug('Error using gzinflate to inflate the payload');
  1058. $this->setError('Error using gzinflate to inflate the payload');
  1059. }
  1060. } elseif ($this->incoming_headers['content-encoding'] == 'gzip') {
  1061. if ($degzdata = @gzinflate(substr($data, 10))) { // do our best
  1062. $data = $degzdata;
  1063. $this->debug('The payload has been un-gzipped to ' . strlen($data) . ' bytes');
  1064. if (strlen($data) < $datalen) {
  1065. // test for the case that the payload has been compressed twice
  1066. $this->debug('The un-gzipped payload is smaller than the gzipped one; try again');
  1067. if ($degzdata = @gzinflate(substr($data, 10))) {
  1068. $data = $degzdata;
  1069. $this->debug('The payload has been un-gzipped again to ' . strlen($data) . ' bytes');
  1070. }
  1071. }
  1072. } else {
  1073. $this->debug('Error using gzinflate to un-gzip the payload');
  1074. $this->setError('Error using gzinflate to un-gzip the payload');
  1075. }
  1076. }
  1077. //$timer->setMarker('finished decoding of gzip/deflated content');
  1078. //print "<xmp>\nde-inflated:\n---------------\n$data\n-------------\n</xmp>";
  1079. // set decoded payload
  1080. $this->incoming_payload = $header_data.$lb.$lb.$data;
  1081. } else {
  1082. $this->debug('The server sent compressed data. Your php install must have the Zlib extension compiled in to support this.');
  1083. $this->setError('The server sent compressed data. Your php install must have the Zlib extension compiled in to support this.');
  1084. }
  1085. } else {
  1086. $this->debug('Unsupported Content-Encoding ' . $this->incoming_headers['content-encoding']);
  1087. $this->setError('Unsupported Content-Encoding ' . $this->incoming_headers['content-encoding']);
  1088. }
  1089. } else {
  1090. $this->debug('No Content-Encoding header');
  1091. }
  1092. if(strlen($data) == 0){
  1093. $this->debug('no data after headers!');
  1094. $this->setError('no data present after HTTP headers');
  1095. return false;
  1096. }
  1097. return $data;
  1098. }
  1099. /**
  1100. * sets the content-type for the SOAP message to be sent
  1101. *
  1102. * @param string $type the content type, MIME style
  1103. * @param mixed $charset character set used for encoding (or false)
  1104. * @access public
  1105. */
  1106. function setContentType($type, $charset = false) {
  1107. $this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
  1108. }
  1109. /**
  1110. * specifies that an HTTP persistent connection should be used
  1111. *
  1112. * @return boolean whether the request was honored by this method.
  1113. * @access public
  1114. */
  1115. function usePersistentConnection(){
  1116. if (isset($this->outgoing_headers['Accept-Encoding'])) {
  1117. return false;
  1118. }
  1119. $this->protocol_version = '1.1';
  1120. $this->persistentConnection = true;
  1121. $this->setHeader('Connection', 'Keep-Alive');
  1122. return true;
  1123. }
  1124. /**
  1125. * parse an incoming Cookie into it's parts
  1126. *
  1127. * @param string $cookie_str content of cookie
  1128. * @return array with data of that cookie
  1129. * @access private
  1130. */
  1131. /*
  1132. * TODO: allow a Set-Cookie string to be parsed into multiple cookies
  1133. */
  1134. function parseCookie($cookie_str) {
  1135. $cookie_str = str_replace('; ', ';', $cookie_str) . ';';
  1136. $data = preg_split('/;/', $cookie_str);
  1137. $value_str = $data[0];
  1138. $cookie_param = 'domain=';
  1139. $start = strpos($cookie_str, $cookie_param);
  1140. if ($start > 0) {
  1141. $domain = substr($cookie_str, $start + strlen($cookie_param));
  1142. $domain = substr($domain, 0, strpos($domain, ';'));
  1143. } else {
  1144. $domain = '';
  1145. }
  1146. $cookie_param = 'expires=';
  1147. $start = strpos($cookie_str, $cookie_param);
  1148. if ($start > 0) {
  1149. $expires = substr($cookie_str, $start + strlen($cookie_param));
  1150. $expires = substr($expires, 0, strpos($expires, ';'));
  1151. } else {
  1152. $expires = '';
  1153. }
  1154. $cookie_param = 'path=';
  1155. $start = strpos($cookie_str, $cookie_param);
  1156. if ( $start > 0 ) {
  1157. $path = substr($cookie_str, $start + strlen($cookie_param));
  1158. $path = substr($path, 0, strpos($path, ';'));
  1159. } else {
  1160. $path = '/';
  1161. }
  1162. $cookie_param = ';secure;';
  1163. if (strpos($cookie_str, $cookie_param) !== FALSE) {
  1164. $secure = true;
  1165. } else {
  1166. $secure = false;
  1167. }
  1168. $sep_pos = strpos($value_str, '=');
  1169. if ($sep_pos) {
  1170. $name = substr($value_str, 0, $sep_pos);
  1171. $value = substr($value_str, $sep_pos + 1);
  1172. $cookie= array( 'name' => $name,
  1173. 'value' => $value,
  1174. 'domain' => $domain,
  1175. 'path' => $path,
  1176. 'expires' => $expires,
  1177. 'secure' => $secure
  1178. );
  1179. return $cookie;
  1180. }
  1181. return false;
  1182. }
  1183. /**
  1184. * sort out cookies for the current request
  1185. *
  1186. * @param array $cookies array with all cookies
  1187. * @param boolean $secure is the send-content secure or not?
  1188. * @return string for Cookie-HTTP-Header
  1189. * @access private
  1190. */
  1191. function getCookiesForRequest($cookies, $secure=false) {
  1192. $cookie_str = '';
  1193. if ((! is_null($cookies)) && (is_array($cookies))) {
  1194. foreach ($cookies as $cookie) {
  1195. if (! is_array($cookie)) {
  1196. continue;
  1197. }
  1198. $this->debug("check cookie for validity: ".$cookie['name'].'='.$cookie['value']);
  1199. if ((isset($cookie['expires'])) && (! empty($cookie['expires']))) {
  1200. if (strtotime($cookie['expires']) <= time()) {
  1201. $this->debug('cookie has expired');
  1202. continue;
  1203. }
  1204. }
  1205. if ((isset($cookie['domain'])) && (! empty($cookie['domain']))) {
  1206. $domain = preg_quote($cookie['domain']);
  1207. if (! preg_match("'.*$domain$'i", $this->host)) {
  1208. $this->debug('cookie has different domain');
  1209. continue;
  1210. }
  1211. }
  1212. if ((isset($cookie['path'])) && (! empty($cookie['path']))) {
  1213. $path = preg_quote($cookie['path']);
  1214. if (! preg_match("'^$path.*'i", $this->path)) {
  1215. $this->debug('cookie is for a different path');
  1216. continue;
  1217. }
  1218. }
  1219. if ((! $secure) && (isset($cookie['secure'])) && ($cookie['secure'])) {
  1220. $this->debug('cookie is secure, transport is not');
  1221. continue;
  1222. }
  1223. $cookie_str .= $cookie['name'] . '=' . $cookie['value'] . '; ';
  1224. $this->debug('add cookie to Cookie-String: ' . $cookie['name'] . '=' . $cookie['value']);
  1225. }
  1226. }
  1227. return $cookie_str;
  1228. }
  1229. }
  1230. ?>