+ // fprintf(STDERR, "CHUNK: [%s]\n", $cont);
+ return $this->frame('@BEGIN@'.$cont.'@END@'); // , 'text', TRUE);
+ }
+
+ protected function frame($message, $messageType='text', $messageContinues=false) {
+ switch ($messageType) {
+ case 'continuous':
+ $b1 = 0;
+ break;
+ case 'text':
+ $b1 = ($this->sendingContinuous) ? 0 : 1;
+ break;
+ case 'binary':
+ $b1 = ($this->sendingContinuous) ? 0 : 2;
+ break;
+ case 'close':
+ $b1 = 8;
+ break;
+ case 'ping':
+ $b1 = 9;
+ break;
+ case 'pong':
+ $b1 = 10;
+ break;
+ }
+ if ($messageContinues) {
+ $this->sendingContinuous = true;
+ } else {
+ $b1 += 128;
+ $this->sendingContinuous = false;
+ }
+
+ $length = mb_strlen($message, "ASCII");
+ $lengthField = "";
+ if ($length < 126) {
+ $b2 = $length;
+ } elseif ($length <= 65536) {
+ $b2 = 126;
+ $hexLength = dechex($length);
+ //$this->stdout("Hex Length: $hexLength");
+ if (mb_strlen($hexLength, "ASCII")%2 == 1) {
+ $hexLength = '0' . $hexLength;
+ }
+ $n = mb_strlen($hexLength, "ASCII") - 2;
+
+ for ($i = $n; $i >= 0; $i=$i-2) {
+ $lengthField = chr(hexdec(substr($hexLength, $i, 2))) . $lengthField;
+ }
+ while (mb_strlen($lengthField, "ASCII") < 2) {
+ $lengthField = chr(0) . $lengthField;
+ }
+ } else {
+ $b2 = 127;
+ $hexLength = dechex($length);
+ if (mb_strlen($hexLength, "ASCII")%2 == 1) {
+ $hexLength = '0' . $hexLength;
+ }
+ $n = mb_strlen($hexLength, "ASCII") - 2;
+
+ for ($i = $n; $i >= 0; $i=$i-2) {
+ $lengthField = chr(hexdec(substr($hexLength, $i, 2))) . $lengthField;
+ }
+ while (mb_strlen($lengthField, "ASCII") < 8) {
+ $lengthField = chr(0) . $lengthField;
+ }
+ }
+
+ return chr($b1) . chr($b2) . $lengthField . $message;
+ }
+
+ protected function deframe($message) {
+ //echo $this->strtohex($message);
+ $headers = $this->extractHeaders($message);
+ $pongReply = false;
+ $willClose = false;
+ switch($headers['opcode']) {
+ case 0:
+ case 1:
+ case 2:
+ break;
+ case 8:
+ // todo: close the connection
+ $this->hasSentClose = true;
+ return "";
+ case 9:
+ $pongReply = true;
+ case 10:
+ break;
+ default:
+ //$this->disconnect($user); // todo: fail connection
+ $willClose = true;
+ break;
+ }
+
+ if ($this->handlingPartialPacket) {
+ $message = $this->partialBuffer . $message;
+ $this->handlingPartialPacket = false;
+ return $this->deframe($message);
+ }
+
+ if ($this->checkRSVBits($headers,$this)) {
+ return false;
+ }
+
+ if ($willClose) {
+ // todo: fail the connection
+ return false;
+ }
+
+ $payload = $this->partialMessage . $this->extractPayload($message,$headers);
+
+ if ($pongReply) {
+ $reply = $this->frame($payload,$this,'pong');
+ // TODO FIXME ALL socket_write management
+ socket_write($user->socket,$reply,mb_strlen($reply, "ASCII"));
+ return false;
+ }
+ if (extension_loaded('mbstring')) {
+ if ($headers['length'] > mb_strlen($payload, "ASCII")) {
+ $this->handlingPartialPacket = true;
+ $this->partialBuffer = $message;
+ return false;
+ }
+ } else {
+ if ($headers['length'] > mb_strlen($payload, "ASCII")) {
+ $this->handlingPartialPacket = true;
+ $this->partialBuffer = $message;
+ return false;
+ }
+ }
+
+ $payload = $this->applyMask($headers,$payload);
+
+ if ($headers['fin']) {
+ $this->partialMessage = "";
+ return $payload;
+ }
+ $this->partialMessage = $payload;
+ return false;
+ }
+
+
+ protected function checkHost($hostName) {
+ return true; // Override and return false if the host is not one that you would expect.
+ // Ex: You only want to accept hosts from the my-domain.com domain,
+ // but you receive a host from malicious-site.com instead.
+ }
+
+ protected function checkOrigin($origin) {
+ return true; // Override and return false if the origin is not one that you would expect.
+ }
+
+ protected function checkWebsocProtocol($protocol) {
+ return true; // Override and return false if a protocol is not found that you would expect.
+ }
+
+ protected function checkWebsocExtensions($extensions) {
+ return true; // Override and return false if an extension is not found that you would expect.
+ }
+
+ protected function processProtocol($protocol) {
+ return ""; // return either "Sec-WebSocket-Protocol: SelectedProtocolFromClientList\r\n" or return an empty string.
+ // The carriage return/newline combo must appear at the end of a non-empty string, and must not
+ // appear at the beginning of the string nor in an otherwise empty string, or it will be considered part of
+ // the response body, which will trigger an error in the client as it will not be formatted correctly.
+ }
+
+ protected function processExtensions($extensions) {
+ return ""; // return either "Sec-WebSocket-Extensions: SelectedExtensions\r\n" or return an empty string.
+ }
+
+ function init($enc, $headers, &$headers_out, $init_string, $base, $step)
+ {
+ if (0) { // TODO: what is ?
+ if (isset($headers['get'])) {
+ $this->requestedResource = $headers['get'];
+ } else {
+ // todo: fail the connection
+ $headers_out['HTTP-Response'] = "405 Method Not Allowed";
+ }
+ }
+
+ if (!isset($headers['Host']) || !$this->checkHost($headers['Host'])) {
+ $headers_out['HTTP-Response'] = "400 Bad Request";
+ }
+ if (!isset($headers['Upgrade']) || strtolower($headers['Upgrade']) != 'websocket') {
+ $headers_out['HTTP-Response'] = "400 Bad Request";
+ }
+ if (!isset($headers['Connection']) || strpos(strtolower($headers['Connection']), 'upgrade') === FALSE) {
+ $headers_out['HTTP-Response'] = "400 Bad Request";
+ }
+ if (!isset($headers['Sec-Websocket-Key'])) {
+ $headers_out['HTTP-Response'] = "400 Bad Request";
+ } else {
+ }
+
+ if (!isset($headers['Sec-Websocket-Version']) || strtolower($headers['Sec-Websocket-Version']) != 13) {
+ $headers_out['HTTP-Response'] = "426 Upgrade Required";
+ $headers_out['Sec-WebSocketVersion'] = "13";
+ }
+ if ( ($this->headerOriginRequired && !isset($headers['Origin']) )
+ || ($this->headerOriginRequired && !$this->checkOrigin($headers['Origin'])) ) {
+ $headers_out['HTTP-Response'] = "403 Forbidden";
+ }
+ if ( ($this->headerSecWebSocketProtocolRequired && !isset($headers['Sec-Websocket-Protocol']))
+ || ($this->headerSecWebSocketProtocolRequired &&
+ !$this->checkWebsocProtocol($headers['Sec-Websocket-Protocol']))) {
+ $headers_out['HTTP-Response'] = "400 Bad Request";
+ }
+ if ( ($this->headerSecWebSocketExtensionsRequired && !isset($headers['Sec-Websocket-Extensions']))
+ || ($this->headerSecWebSocketExtensionsRequired &&
+ !$this->checkWebsocExtensions($headers['Sec-Websocket-Extensions'])) ) {
+ $headers_out['HTTP-Response'] = "400 Bad Request";
+ }
+
+ if (isset($headers_out['HTTP-Response'])) {
+ // TODO: check return management
+ return (FALSE);
+ }
+
+ // TODO: verify both variables
+ // here there is a change of the socket status from start to handshaked
+ // th headers are saved too but without any further access so we skip it
+
+
+
+ $inno = 'x3JJHMbDL1EzLkh9GBhXDw==';
+ $outo = sha1($inno . $this->magicGUID);
+ $rawToken = "";
+ for ($i = 0; $i < 20; $i++) {
+ $rawToken .= chr(hexdec(substr($outo,$i*2, 2)));
+ }
+
+ $outo = base64_encode($rawToken);
+
+ $webSocketKeyHash = sha1($headers['Sec-Websocket-Key'] . $this->magicGUID);
+ $rawToken = "";
+ for ($i = 0; $i < 20; $i++) {
+ $rawToken .= chr(hexdec(substr($webSocketKeyHash,$i*2, 2)));
+ }
+ $handshakeToken = base64_encode($rawToken);
+ $subProtocol = (isset($headers['Sec-Websocket-Protocol'])) ?
+ $this->processProtocol($headers['Sec-Websocket-Protocol']) : "";
+ $extensions = (isset($headers['Sec-Websocket-Extensions'])) ?
+ $this->processExtensions($headers['Sec-Websocket-Extensions']) : "";
+
+ $headers_out['HTTP-Response'] = "101 Switching Protocols";
+ $headers_out['Upgrade'] = 'websocket';
+ $headers_out['Connection'] = 'Upgrade';
+ $headers_out['Sec-WebSocket-Accept'] = "$handshakeToken$subProtocol$extensions";
+
+ return ("");
+ }
+
+ static function close()
+ {
+ return(chr(0x88).chr(0x02).chr(0xe8).chr(0x03));