public function setHeaders($headers) { $this->headers = $headers; if (array_key_exists('Cookie', $this->headers) && is_array($this->headers['Cookie'])) { $this->cookie = array(); } else { if (array_key_exists("Cookie", $this->headers)) { $this->_cookies = WebSocketFunctions::cookie_parse($this->headers['Cookie']); } else { $this->_cookies = array(); } } $this->isAdmin = array_key_exists('Admin-Key', $this->headers) && $this->headers['Admin-Key'] == $this->getServerInstance()->getAdminKey(); // Incorrect admin-key if ($this->isAdmin == false && array_key_exists('Admin-Key', $this->headers)) { throw new WebSocketNotAuthorizedException($this); } }
private function acceptSocket() { try { $client = stream_socket_accept($this->master); if ($client === false) { WebSocketFunctions::say('socket_accept() failed'); } $this->sockets->attach(new WebSocketSocket($this, $client)); $this->debug("Socket accepted"); } catch (Exception $e) { $this->say($e); } }
public function onConnect(IWebSocketConnection $user) { if ($user->getAdminKey() == self::$ADMIN_KEY) { $this->say("[ECHO] Admin user connected"); return; } $h = $user->getHeaders(); $c = WebSocketFunctions::cookie_parse($h["Cookie"]); $client = new HttpClient($this->host); $client->cookies = $c; $client->get("/{$this->path}/?get_action=ws_authenticate&key=" . self::$ADMIN_KEY); $registry = $client->getContent(); //$this->say("[ECHO] Registry loaded".$registry); $xml = new DOMDocument(); $xml->loadXML($registry); $xPath = new DOMXPath($xml); $err = $xPath->query("//message[@type='ERROR']"); if ($err->length) { $this->say($err->item(0)->firstChild->nodeValue); $user->disconnect(); } else { $userRepositories = array(); $repos = $xPath->query("/tree/user/repositories/repo"); foreach ($repos as $repo) { $repoId = $repo->attributes->getNamedItem("id")->nodeValue; $userRepositories[] = $repoId; } $user->ajxpRepositories = $userRepositories; $user->ajxpId = $xPath->query("/tree/user/@id")->item(0)->nodeValue; if ($xPath->query("/tree/user/@groupPath")->length) { $groupPath = $xPath->query("/tree/user/@groupPath")->item(0)->nodeValue; if (!empty($groupPath)) { $user->ajxpGroupPath = $groupPath; } } } $this->say("[ECHO] User '" . $user->ajxpId . "' connected with " . count($user->ajxpRepositories) . " registered repositories "); }
public function sendHandshakeResponse() { // Last 8 bytes of the client's handshake are used for key calculation later $l8b = substr($this->_clientHandshake, -8); // Check for 2-key based handshake (Hixie protocol draft) $key1 = isset($this->_headers['Sec-Websocket-Key1']) ? $this->_headers['Sec-Websocket-Key1'] : null; $key2 = isset($this->_headers['Sec-Websocket-Key2']) ? $this->_headers['Sec-Websocket-Key2'] : null; // Origin checking (TODO) $origin = isset($this->_headers['Origin']) ? $this->_headers['Origin'] : null; $host = $this->_headers['Host']; $location = $this->_headers['GET']; // Build response $response = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" . "Upgrade: WebSocket\r\n" . "Connection: Upgrade\r\n"; // Build HIXIE response $response .= "Sec-WebSocket-Origin: {$origin}\r\n" . "Sec-WebSocket-Location: ws://{$host}{$location}\r\n"; $response .= "\r\n" . WebSocketFunctions::calcHixieResponse($key1, $key2, $l8b); $this->_socket->write($response); echo "HIXIE Response SENT!"; }
/** * @return WebSocketFrame */ public function readFrame() { $buffer = WebSocketFunctions::readWholeBuffer($this->socket); $this->_frames = array_merge($this->_frames, $this->_connection->readFrame($buffer)); return array_shift($this->_frames); }
public function mayWrite() { if (strlen($this->_writeBuffer) > 4096) { $buff = substr($this->_writeBuffer, 0, 4096); $this->_writeBuffer = strlen($buff) > 0 ? substr($this->_writeBuffer, 4096) : ''; } else { $buff = $this->_writeBuffer; $this->_writeBuffer = ''; } if (WebSocketFunctions::writeWholeBuffer($this->_socket, $buff) == false) { $this->close(); } if (strlen($this->_writeBuffer) == 0 && $this->isClosing()) { $this->close(); } }