ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilBMFTransport_TCP.php
Go to the documentation of this file.
1 <?php
22 require_once dirname(__FILE__).'/../class.ilBMFBase.php';
23 
34 {
35 
36  var $headers = array();
37  var $urlparts = null;
38  var $url = '';
42  var $result_encoding = 'UTF-8';
44 
48  var $socket = null;
49 
58  {
60  $this->urlparts = @parse_url($url);
61  $this->url = $url;
62  $this->encoding = $encoding;
63  }
64 
65  function _socket_ping()
66  {
67  // XXX how do we restart after socket_shutdown?
68  //if (!$this->socket) {
69  // Create socket resource.
70  $this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
71  if ($this->socket < 0) {
72  return 0;
73  }
74 
75  // Connect.
76  $result = socket_connect($this->socket, $this->urlparts['host'],
77  $this->urlparts['port']);
78  if ($result < 0) {
79  return 0;
80  }
81  //}
82  return 1;
83  }
84 
94  function send($msg, $options = NULL)
95  {
96  $this->incoming_payload = '';
97  $this->outgoing_payload = $msg;
98  if (!$this->_validateUrl()) {
99  return $this->fault;
100  }
101 
102  // Check for TCP scheme.
103  if (strcasecmp($this->urlparts['scheme'], 'TCP') == 0) {
104  // Check connection.
105  if (!$this->_socket_ping()) {
106  return $this->_raiseSoapFault('Error connecting to ' . $this->url . '; reason: ' . socket_strerror(socket_last_error($this->socket)));
107  }
108 
109  // Write to the socket.
110  if (!@socket_write($this->socket, $this->outgoing_payload,
111  strlen($this->outgoing_payload))) {
112  return $this->_raiseSoapFault('Error sending data to ' . $this->url . '; reason: ' . socket_strerror(socket_last_error($this->socket)));
113  }
114 
115  // Shutdown writing.
116  if(!socket_shutdown($this->socket, 1)) {
117  return $this->_raiseSoapFault('Cannot change socket mode to read.');
118  }
119 
120  // Read everything we can.
121  while ($buf = @socket_read($this->socket, 1024, PHP_BINARY_READ)) {
122  $this->incoming_payload .= $buf;
123  }
124 
125  // Return payload or die.
126  if ($this->incoming_payload) {
128  }
129 
130  return $this->_raiseSoapFault('Error reveiving data from ' . $this->url);
131  }
132 
133  return $this->_raiseSoapFault('Invalid url scheme ' . $this->url);
134  }
135 
142  function _validateUrl()
143  {
144  if (!is_array($this->urlparts) ) {
145  $this->_raiseSoapFault("Unable to parse URL $url");
146  return false;
147  }
148  if (!isset($this->urlparts['host'])) {
149  $this->_raiseSoapFault("No host in URL $url");
150  return false;
151  }
152  if (!isset($this->urlparts['path']) || !$this->urlparts['path']) {
153  $this->urlparts['path'] = '/';
154  }
155 
156  return true;
157  }
158 
159 }