00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 require_once dirname(__FILE__).'/../class.ilBMFBase.php';
00023 require_once 'Net/DIME.php';
00032 class ilBMFTransport_HTTP extends ilBMFBase
00033 {
00034
00040 var $headers = array();
00041
00046 var $timeout = 4;
00047
00053 var $urlparts = NULL;
00054
00060 var $url = '';
00061
00067 var $incoming_payload = '';
00068
00074 var $_userAgent = SOAP_LIBRARY_NAME;
00075
00076 var $encoding = SOAP_DEFAULT_ENCODING;
00077
00084 var $result_encoding = 'UTF-8';
00085
00086 var $result_content_type;
00094 function ilBMFTransport_HTTP($URL, $encoding=SOAP_DEFAULT_ENCODING)
00095 {
00096 parent::ilBMFBase('HTTP');
00097 $this->urlparts = @parse_url($URL);
00098 $this->url = $URL;
00099 $this->encoding = $encoding;
00100 }
00101
00112 function &send(&$msg, $options = NULL)
00113 {
00114 if (!$this->_validateUrl()) {
00115 return $this->fault;
00116 }
00117
00118 if (isset($options['timeout']))
00119 $this->timeout = (int)$options['timeout'];
00120
00121 if (strcasecmp($this->urlparts['scheme'], 'HTTP') == 0) {
00122 return $this->_sendHTTP($msg, $options);
00123 } else if (strcasecmp($this->urlparts['scheme'], 'HTTPS') == 0) {
00124 return $this->_sendHTTPS($msg, $options);
00125 }
00126
00127 return $this->_raiseSoapFault('Invalid url scheme '.$this->url);
00128 }
00129
00140 function setCredentials($username, $password)
00141 {
00142 $this->headers['Authorization'] = 'Basic ' . base64_encode($username . ':' . $password);
00143 }
00144
00145
00146
00153 function _validateUrl()
00154 {
00155 if ( ! is_array($this->urlparts) ) {
00156 $this->_raiseSoapFault("Unable to parse URL $url");
00157 return FALSE;
00158 }
00159 if (!isset($this->urlparts['host'])) {
00160 $this->_raiseSoapFault("No host in URL $url");
00161 return FALSE;
00162 }
00163 if (!isset($this->urlparts['port'])) {
00164
00165 if (strcasecmp($this->urlparts['scheme'], 'HTTP') == 0)
00166 $this->urlparts['port'] = 80;
00167 else if (strcasecmp($this->urlparts['scheme'], 'HTTPS') == 0)
00168 $this->urlparts['port'] = 443;
00169
00170 }
00171 if (isset($this->urlparts['user'])) {
00172 $this->setCredentials($this->urlparts['user'], $this->urlparts['pass']);
00173 }
00174 if (!isset($this->urlparts['path']) || !$this->urlparts['path'])
00175 $this->urlparts['path'] = '/';
00176 return TRUE;
00177 }
00178
00179 function _parseEncoding($headers)
00180 {
00181 $h = stristr($headers,'Content-Type');
00182 preg_match('/^Content-Type:\s*(.*)$/im',$h,$ct);
00183 $this->result_content_type = str_replace("\r","",$ct[1]);
00184 if (preg_match('/(.*?)(?:;\s?charset=)(.*)/i',$this->result_content_type,$m)) {
00185
00186 $this->result_content_type = $m[1];
00187 if (count($m) > 2) {
00188 $enc = strtoupper(str_replace('"',"",$m[2]));
00189 if (in_array($enc, $this->_encodings)) {
00190 $this->result_encoding = $enc;
00191 }
00192 }
00193 }
00194
00195 if (!$this->result_content_type) $this->result_content_type = 'text/xml';
00196 }
00197
00204 function _parseResponse()
00205 {
00206 if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $this->incoming_payload, $match)) {
00207 #$this->response = preg_replace("/[\r|\n]/", '', $match[2]);
00208 $this->response = $match[2];
00209
00210 if (preg_match("/^HTTP\/1\.. (\d+).*/s",$match[1],$status) &&
00211 $status[1] >= 400 && $status[1] < 500) {
00212 $this->_raiseSoapFault("HTTP Response $status[1] Not Found");
00213 return FALSE;
00214 }
00215 $this->_parseEncoding($match[1]);
00216 if ($this->result_content_type == 'application/dime') {
00217
00218 $this->_decodeDIMEMessage($this->response,$this->headers,$this->attachments);
00219 $this->result_content_type = $this->headers['content-type'];
00220 } else if (stristr($this->result_content_type,'multipart/related')) {
00221 $this->response = $this->incoming_payload;
00222 $this->_decodeMimeMessage($this->response,$this->headers,$this->attachments);
00223 } else if ($this->result_content_type != 'text/xml') {
00224 $this->_raiseSoapFault($this->response);
00225 return FALSE;
00226 }
00227
00228 return strlen($this->response) > 0;
00229 }
00230 $this->_raiseSoapFault('Invalid HTTP Response');
00231 return FALSE;
00232 }
00233
00240 function &_getRequest(&$msg, $options)
00241 {
00242 $action = isset($options['soapaction'])?$options['soapaction']:'';
00243 $fullpath = $this->urlparts['path'].
00244 (isset($this->urlparts['query'])?'?'.$this->urlparts['query']:'').
00245 (isset($this->urlparts['fragment'])?'#'.$this->urlparts['fragment']:'');
00246 if (isset($options['proxy_user'])) {
00247 $this->headers['Proxy-Authorization'] = 'Basic ' . base64_encode($options['proxy_user'].":".$options['proxy_pass']);
00248 }
00249 $this->headers['User-Agent'] = $this->_userAgent;
00250 $this->headers['Host'] = $this->urlparts['host'];
00251 $this->headers['Content-Type'] = "text/xml; charset=$this->encoding";
00252 $this->headers['Content-Length'] = strlen($msg);
00253 $this->headers['SOAPAction'] = "\"$action\"";
00254 if (isset($options['headers'])) {
00255 $this->headers = array_merge($this->headers, $options['headers']);
00256 }
00257 $headers = '';
00258 foreach ($this->headers as $k => $v) {
00259 $headers .= "$k: $v\r\n";
00260 }
00261 $this->outgoing_payload =
00262 "POST $fullpath HTTP/1.0\r\n".
00263 $headers."\r\n".
00264 $msg;
00265 return $this->outgoing_payload;
00266 }
00267
00277 function &_sendHTTP(&$msg, $options)
00278 {
00279 $this->_getRequest($msg, $options);
00280 $host = $this->urlparts['host'];
00281 $port = $this->urlparts['port'];
00282 if (isset($options['proxy_host'])) {
00283 $host = $options['proxy_host'];
00284 $port = isset($options['proxy_port'])?$options['proxy_port']:8080;
00285 }
00286
00287 if ($this->timeout > 0) {
00288 $fp = fsockopen($host, $port, $this->errno, $this->errmsg, $this->timeout);
00289 } else {
00290 $fp = fsockopen($host, $port, $this->errno, $this->errmsg);
00291 }
00292 if (!$fp) {
00293 return $this->_raiseSoapFault("Connect Error to $host:$port");
00294 }
00295 if ($this->timeout > 0) {
00296
00297
00298 @socket_set_timeout($fp, $this->timeout);
00299 }
00300 if (!fputs($fp, $this->outgoing_payload, strlen($this->outgoing_payload))) {
00301 return $this->_raiseSoapFault("Error POSTing Data to $host");
00302 }
00303
00304
00305
00306 while ($data = fread($fp, 32768)) {
00307 $this->incoming_payload .= $data;
00308 }
00309
00310 fclose($fp);
00311
00312 if (!$this->_parseResponse()) {
00313 return $this->fault;
00314 }
00315 return $this->response;
00316 }
00317
00327 function &_sendHTTPS(&$msg, $options)
00328 {
00329
00330
00331
00332 if (!extension_loaded('curl')) {
00333 return $this->_raiseSoapFault('CURL Extension is required for HTTPS');
00334 }
00335
00336 $this->_getRequest($msg, $options);
00337
00338 $ch = curl_init();
00339
00340
00341 if (isset($options['proxy_host'])) {
00342
00343 $host = $options['proxy_host'];
00344 $port = isset($options['proxy_port'])?$options['proxy_port']:8080;
00345 curl_setopt($ch, CURLOPT_PROXY, $host.":".$port);
00346 }
00347 if (isset($options['proxy_user'])) {
00348
00349 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $options['proxy_user'].':'.$options['proxy_pass']);
00350 }
00351
00352 if ($this->timeout) {
00353
00354 }
00355 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->outgoing_payload);
00356 curl_setopt($ch, CURLOPT_URL, $this->url);
00357 curl_setopt($ch, CURLOPT_FAILONERROR, 1);
00358 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
00359 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
00360 curl_setopt($ch, CURLOPT_VERBOSE, 0);
00361 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
00362 curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
00363
00364 if (isset($options['curl'])) {
00365 reset($options['curl']);
00366 while (list($key, $val) = each ($options['curl'])) {
00367 curl_setopt($ch, $key, $val);
00368 }
00369 }
00370
00371
00372 $this->response = curl_exec($ch);
00373 echo curl_error ( $ch);
00374 curl_close($ch);
00375
00376 return $this->response;
00377 }
00378 }
00379 ?>