5.5 strict warnings, change background color
[php-js-websockets.git] / server.functions.php
blob:a/server.functions.php -> blob:b/server.functions.php
<?php <?php
   
function sendMesageToYou ($socket, $msg, $handshake=false) { function sendMesageToYou ($socket, $msg, $handshake=false) {
$msg = $msg; $msg = $msg;
echo "[".date('Y-m-d H:i:s')."] MESSAGE to ".intval($socket)." ".$msg.")\n"; echo "[".date('Y-m-d H:i:s')."] MESSAGE to ".intval($socket)." ".$msg.")\n";
if ( $handshake == false ) { if ( $handshake == false ) {
@socket_write ($socket, encode($msg)); @socket_write ($socket, encode($msg));
} else { } else {
@socket_write ($socket, $msg, strlen($msg)); @socket_write ($socket, $msg, strlen($msg));
} }
} }
   
function sendMessageToAll ($allclients, $session, $msg) { function sendMessageToAll ($allclients, $socket, $session, $msg) {
global $_sockets; global $_sockets;
foreach ($allclients as $client) { foreach ($allclients as $client) {
if ( $_sockets[$client]['session'] == $session ) { if ( $_sockets[intval ($client)]['session'] == $session && intval ($client) != intval ($socket) ) {
echo "[".date('Y-m-d H:i:s')."] MESSAGE to ".intval($client)." ".$msg.")\n"; echo "[".date('Y-m-d H:i:s')."] MESSAGE to ".intval($client)." ".$msg.")\n";
@socket_write ($client, encode ($msg)); @socket_write ($client, encode ($msg));
} }
} }
} }
   
function send_Msg($allclients,$socket,$id,$msg) { function send_Msg($allclients,$socket,$id,$msg) {
global $_sockets; global $_sockets;
   
if (!empty($_sockets[intval($socket)])) { if (!empty($_sockets[intval($socket)])) {
$nicks = array(); //amig fut a parancs ebben vannak a nickek. $nicks = array(); //amig fut a parancs ebben vannak a nickek.
   
foreach ($_sockets as $_socket){ foreach ($_sockets as $_socket){
foreach ($_socket as $key=>$val){ foreach ($_socket as $key=>$val){
// this check's the onliners // this check's the onliners
if (empty($nicks[$val])) $nicks[$val]=1; if (empty($nicks[$val])) $nicks[$val]=1;
else $nicks[$val]=$nicks[$val]+1; // we shouldn't have duplicated nicks, but what if... else $nicks[$val]=$nicks[$val]+1; // we shouldn't have duplicated nicks, but what if...
} }
} }
   
foreach($allclients as $client) { foreach($allclients as $client) {
if (!empty($_sockets[$client]['nick']) && ($_sockets[$client]['nick'] == $id)) { if (!empty($_sockets[$client]['nick']) && ($_sockets[$client]['nick'] == $id)) {
$_client = $client; $_client = $client;
$out = "<msg aid=\"".$_sockets[$socket]['nick']."\" time=\"".date("H:i:s")."\" msg=\"".$msg."\" from=\"".$_sockets[$client]['nick']."\" />"; $out = "<msg aid=\"".$_sockets[$socket]['nick']."\" time=\"".date("H:i:s")."\" msg=\"".$msg."\" from=\"".$_sockets[$client]['nick']."\" />";
} }
elseif(empty($nicks[$id])) { elseif(empty($nicks[$id])) {
//backto the sender //backto the sender
$_client = $socket; $_client = $socket;
$out = "<error value=\"User is already left.\"/>"; $out = "<error value=\"User is already left.\"/>";
} }
} }
} else { } else {
//backto the sender //backto the sender
$_client = $socket; $_client = $socket;
$out = "<error value=\"Not identified.\"/>"; $out = "<error value=\"Not identified.\"/>";
} }
if (!empty($out)) { if (!empty($out)) {
socket_write($socket, $out.chr(0)); //send to back ourself. we have to handle it in flash socket_write($socket, $out.chr(0)); //send to back ourself. we have to handle it in flash
socket_write($_client, $out.chr(0)); //send to the recipient socket_write($_client, $out.chr(0)); //send to the recipient
} }
} }
   
   
function dohandshake($socket, $buffer) { function dohandshake($socket, $buffer) {
// Determine which version of the WebSocket protocol the client is using // Determine which version of the WebSocket protocol the client is using
if(preg_match("/Sec-WebSocket-Version: (.*)\r\n/ ", $buffer, $match)) if(preg_match("/Sec-WebSocket-Version: (.*)\r\n/ ", $buffer, $match))
$version = $match[1]; $version = $match[1];
else else
return false; return false;
   
if($version >= 8){ if($version >= 8){
// Extract header variables // Extract header variables
if(preg_match("/GET (.*) HTTP/" ,$buffer,$match)){ $r=$match[1]; } if(preg_match("/GET (.*) HTTP/" ,$buffer,$match)){ $r=$match[1]; }
if(preg_match("/Host: (.*)\r\n/" ,$buffer,$match)){ $h=$match[1]; } if(preg_match("/Host: (.*)\r\n/" ,$buffer,$match)){ $h=$match[1]; }
if(preg_match("/Sec-WebSocket-Origin: (.*)\r\n/",$buffer,$match)){ $o=$match[1]; } if(preg_match("/Sec-WebSocket-Origin: (.*)\r\n/",$buffer,$match)){ $o=$match[1]; }
if(preg_match("/Sec-WebSocket-Key: (.*)\r\n/",$buffer,$match)){ $k = $match[1]; } if(preg_match("/Sec-WebSocket-Key: (.*)\r\n/",$buffer,$match)){ $k = $match[1]; }
   
// Generate our Socket-Accept key based on the IETF specifications // Generate our Socket-Accept key based on the IETF specifications
$accept_key = $k . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; $accept_key = $k . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
$accept_key = sha1($accept_key, true); $accept_key = sha1($accept_key, true);
$accept_key = base64_encode($accept_key); $accept_key = base64_encode($accept_key);
   
$upgrade = "HTTP/1.1 101 Switching Protocols\r\n" . $upgrade = "HTTP/1.1 101 Switching Protocols\r\n" .
"Upgrade: websocket\r\n" . "Upgrade: websocket\r\n" .
"Connection: Upgrade\r\n" . "Connection: Upgrade\r\n" .
"Sec-WebSocket-Accept: $accept_key\r\n\r\n"; "Sec-WebSocket-Accept: $accept_key\r\n\r\n";
   
echo "[".date('Y-m-d H:i:s')."] Sending handshake)\n"; echo "[".date('Y-m-d H:i:s')."] Sending handshake)\n";
sendMesageToYou ($socket, $upgrade, true); sendMesageToYou ($socket, $upgrade, true);
return true; return true;
} }
else { else {
return false; return false;
} }
} }
   
function unmask($payload) { function unmask($payload) {
$length = ord($payload[1]) & 127; $length = ord($payload[1]) & 127;
   
if($length == 126) { if($length == 126) {
$masks = substr($payload, 4, 4); $masks = substr($payload, 4, 4);
$data = substr($payload, 8); $data = substr($payload, 8);
} }
elseif($length == 127) { elseif($length == 127) {
$masks = substr($payload, 10, 4); $masks = substr($payload, 10, 4);
$data = substr($payload, 14); $data = substr($payload, 14);
} }
else { else {
$masks = substr($payload, 2, 4); $masks = substr($payload, 2, 4);
$data = substr($payload, 6); $data = substr($payload, 6);
} }
   
$text = ''; $text = '';
for ($i = 0; $i < strlen($data); ++$i) { for ($i = 0; $i < strlen($data); ++$i) {
$text .= $data[$i] ^ $masks[$i%4]; $text .= $data[$i] ^ $masks[$i%4];
} }
return $text; return $text;
} }
   
function encode($text) { function encode($text) {
// 0x1 text frame (FIN + opcode) // 0x1 text frame (FIN + opcode)
$b1 = 0x80 | (0x1 & 0x0f); $b1 = 0x80 | (0x1 & 0x0f);
$length = strlen($text); $length = strlen($text);
   
if($length <= 125) $header = pack('CC', $b1, $length); elseif($length > 125 && $length < 65536) $header = pack('CCS', $b1, 126, $length); elseif($length >= 65536) if($length <= 125) $header = pack('CC', $b1, $length); elseif($length > 125 && $length < 65536) $header = pack('CCS', $b1, 126, $length); elseif($length >= 65536)
$header = pack('CCN', $b1, 127, $length); $header = pack('CCN', $b1, 127, $length);
   
return $header.$text; return $header.$text;
} }
   
?> ?>
comments