32 require_once dirname(__FILE__).
'/../class.ilBMFBase.php';
128 $this->urlparts = @parse_url(
$url);
142 function send($msg, $options = null)
148 if (isset($options[
'timeout'])) {
149 $this->timeout = (int)$options[
'timeout'];
152 if (strcasecmp($this->urlparts[
'scheme'],
'HTTP') == 0) {
154 } elseif (strcasecmp($this->urlparts[
'scheme'],
'HTTPS') == 0) {
171 $this->headers[
'Authorization'] =
'Basic ' . base64_encode($username .
':' . $password);
183 $this->cookies[$name] = $value;
193 foreach ($this->cookies as $name=>$value) {
195 urlencode($name) .
'=' . urlencode($value);
208 if (!is_array($this->urlparts) ) {
212 if (!isset($this->urlparts[
'host'])) {
216 if (!isset($this->urlparts[
'port'])) {
217 if (strcasecmp($this->urlparts[
'scheme'],
'HTTP') == 0) {
218 $this->urlparts[
'port'] = 80;
219 } elseif (strcasecmp($this->urlparts[
'scheme'],
'HTTPS') == 0) {
220 $this->urlparts[
'port'] = 443;
224 if (isset($this->urlparts[
'user'])) {
226 urldecode($this->urlparts[
'pass']));
228 if (!isset($this->urlparts[
'path']) || !$this->urlparts[
'path']) {
229 $this->urlparts[
'path'] =
'/';
244 $h = stristr(
$headers,
'Content-Type');
245 preg_match_all(
'/^Content-Type:\s*(.*)$/im', $h, $ct, PREG_SET_ORDER);
250 $this->result_content_type = str_replace(
"\r",
'', $ct[1]);
252 if (preg_match(
'/(.*?)(?:;\s?charset=)(.*)/i',
253 $this->result_content_type,
255 $this->result_content_type = $m[1];
257 $enc = strtoupper(str_replace(
'"',
'', $m[2]));
258 if (in_array($enc, $this->_encodings)) {
259 $this->result_encoding = $enc;
265 if (!$this->result_content_type) {
266 $this->result_content_type =
'text/xml';
278 $this->result_headers = array();
281 if (strpos($value,
':') ===
false) {
282 $this->result_headers[0] = $value;
285 list($name, $value) = split(
':', $value);
286 $headername = strtolower($name);
287 $headervalue = trim($value);
288 $this->result_headers[$headername] = $headervalue;
290 if ($headername ==
'set-cookie') {
292 $cookie = array(
'expires' => null,
293 'domain' => $this->urlparts[
'host'],
297 if (!strpos($headervalue,
';')) {
299 list($cookie[
'name'], $cookie[
'value']) = array_map(
'trim', explode(
'=', $headervalue));
300 $cookie[
'name'] = urldecode($cookie[
'name']);
301 $cookie[
'value'] = urldecode($cookie[
'value']);
305 $elements = explode(
';', $headervalue);
306 list($cookie[
'name'], $cookie[
'value']) = array_map(
'trim', explode(
'=', $elements[0]));
307 $cookie[
'name'] = urldecode($cookie[
'name']);
308 $cookie[
'value'] = urldecode($cookie[
'value']);
310 for ($i = 1; $i < count($elements);$i++) {
311 list($elName, $elValue) = array_map(
'trim', explode(
'=', $elements[$i]));
312 if (
'secure' == $elName) {
313 $cookie[
'secure'] =
true;
314 } elseif (
'expires' == $elName) {
315 $cookie[
'expires'] = str_replace(
'"',
'', $elValue);
316 } elseif (
'path' == $elName OR
'domain' == $elName) {
317 $cookie[$elName] = urldecode($elValue);
319 $cookie[$elName] = $elValue;
323 $this->result_cookies[] = $cookie;
336 if (preg_match(
"/^(.*?)\r?\n\r?\n(.*)/s",
337 $this->incoming_payload,
339 $this->response = $match[2];
344 list($protocol, $code, $msg) = sscanf($this->result_headers[0],
346 unset($this->result_headers[0]);
350 $this->incoming_payload = $match[2];
369 $this->
_raiseSoapFault(
"HTTP Response $code Proxy Authentication Required");
381 if ($code >= 400 && $code < 500) {
382 $this->
_raiseSoapFault(
"HTTP Response $code Not Found, Server message: $msg");
389 if ($this->result_content_type ==
'application/dime') {
395 $this->result_content_type = $this->headers[
'content-type'];
396 } elseif (stristr($this->result_content_type,
'multipart/related')) {
402 } elseif ($this->result_content_type !=
'text/xml') {
407 return strlen($this->response) > 0;
423 $this->headers = array();
425 $action = isset($options[
'soapaction']) ? $options[
'soapaction'] :
'';
426 $fullpath = $this->urlparts[
'path'];
427 if (isset($this->urlparts[
'query'])) {
428 $fullpath .=
'?' . $this->urlparts[
'query'];
430 if (isset($this->urlparts[
'fragment'])) {
431 $fullpath .=
'#' . $this->urlparts[
'fragment'];
434 if (isset($options[
'proxy_host'])) {
435 $fullpath =
'http://' . $this->urlparts[
'host'] .
':' .
436 $this->urlparts[
'port'] . $fullpath;
439 if (isset($options[
'proxy_user'])) {
440 $this->headers[
'Proxy-Authorization'] =
'Basic ' .
441 base64_encode($options[
'proxy_user'] .
':' .
442 $options[
'proxy_pass']);
445 if (isset($options[
'user'])) {
450 $this->headers[
'Host'] = $this->urlparts[
'host'];
451 $this->headers[
'Content-Type'] =
"text/xml; charset=$this->encoding";
452 $this->headers[
'Content-Length'] = strlen($msg);
453 $this->headers[
'SOAPAction'] =
'"' . $action .
'"';
454 if (isset($options[
'headers'])) {
455 $this->headers = array_merge($this->headers, $options[
'headers']);
458 $this->cookies = array();
459 if (!isset($options[
'nocookies']) || !$options[
'nocookies']) {
461 if (isset($this->result_cookies)) {
462 foreach ($this->result_cookies as $cookie) {
463 if ($cookie[
'domain'] == $this->urlparts[
'host'])
464 $this->cookies[$cookie[
'name']] = $cookie[
'value'];
469 if (isset($options[
'cookies'])) {
470 foreach ($options[
'cookies'] as $cookie) {
471 if ($cookie[
'domain'] == $this->urlparts[
'host'])
472 $this->cookies[$cookie[
'name']] = $cookie[
'value'];
475 if (count($this->cookies)) {
479 foreach ($this->headers as $k => $v) {
482 $this->outgoing_payload =
"POST $fullpath HTTP/1.0\r\n" .
$headers .
485 return $this->outgoing_payload;
498 $this->incoming_payload =
'';
500 $host = $this->urlparts[
'host'];
501 $port = $this->urlparts[
'port'];
502 if (isset($options[
'proxy_host'])) {
503 $host = $options[
'proxy_host'];
504 $port = isset($options[
'proxy_port']) ? $options[
'proxy_port'] : 8080;
507 if ($this->timeout > 0) {
508 $fp = @fsockopen($host, $port, $this->errno, $this->errmsg, $this->timeout);
510 $fp = @fsockopen($host, $port, $this->errno, $this->errmsg);
515 if ($this->timeout > 0) {
517 @socket_set_timeout($fp, $this->timeout);
519 if (!fputs($fp, $this->outgoing_payload, strlen($this->outgoing_payload))) {
526 $data = fread($fp, 4096);
527 $_tmp_status = socket_get_status($fp);
528 if ($_tmp_status[
'timed_out']) {
531 $this->incoming_payload .=
$data;
533 }
while (!$_tmp_status[
'eof']);
540 return $this->response;
556 if (!extension_loaded(
'curl')) {
565 if (isset($options[
'proxy_host'])) {
567 $host = $options[
'proxy_host'];
568 $port = isset($options[
'proxy_port']) ? $options[
'proxy_port'] : 8080;
569 curl_setopt($ch, CURLOPT_PROXY, $host .
":" . $port);
572 if (isset($options[
'proxy_user'])) {
574 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $options[
'proxy_user'] .
':' . $options[
'proxy_pass']);
577 if (isset($options[
'user'])) {
578 curl_setopt($ch, CURLOPT_USERPWD, $options[
'user'] .
':' . $options[
'pass']);
581 if (!isset($options[
'soapaction'])) {
582 $options[
'soapaction'] =
'';
584 curl_setopt($ch, CURLOPT_HTTPHEADER , array(
'Content-Type: text/xml;charset=' . $this->encoding,
'SOAPAction: "'.$options[
'soapaction'].
'"'));
585 curl_setopt($ch, CURLOPT_USERAGENT , $this->_userAgent);
587 if ($this->timeout) {
588 curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
591 curl_setopt($ch, CURLOPT_POSTFIELDS, $msg);
592 curl_setopt($ch, CURLOPT_URL, $this->url);
593 curl_setopt($ch, CURLOPT_POST, 1);
595 # curl_setopt($ch, CURLOPT_FAILONERROR, 0);
596 curl_setopt($ch, CURLOPT_FAILONERROR, 1);
597 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
598 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
599 curl_setopt($ch, CURLOPT_HEADER, 1);
601 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
603 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
605 curl_setopt($ch, CURLOPT_VERBOSE, 1);
606 if (defined(
'CURLOPT_HTTP_VERSION')) {
607 curl_setopt($ch, CURLOPT_HTTP_VERSION, 1);
610 if (isset($options[
'curl'])) {
611 foreach ($options[
'curl'] as $key => $val) {
612 curl_setopt($ch, $key, $val);
619 $this->outgoing_payload = $msg;
621 $this->incoming_payload = curl_exec($ch);
622 if (!$this->incoming_payload) {
623 $m =
'curl_exec error ' . curl_errno($ch) .
' ' . curl_error($ch);
633 return $this->response;