1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| function send($client,$data)
{
$this->say($data);
$header = " ";
$header[0] = chr(0x81);
$header_length = 1;
//Payload length: 7 bits, 7+16 bits, or 7+64 bits
$dataLength = strlen(trim($data));
//The length of the payload data, in bytes: if 0-125, that is the payload length.
if($dataLength <= 125)
{
$header[1] = chr($dataLength);
$header_length = 2;
}
elseif ($dataLength <= 65535)
{
// If 126, the following 2 bytes interpreted as a 16
//bit unsigned integer are the payload length.
$header[1] = chr(126);
$header[2] = chr($dataLength >> 8);
$header[3] = chr($dataLength & 0xFF);
$header_length = 4;
}
else
{
// If 127, the following 8 bytes interpreted as a 64-bit unsigned integer (the
// most significant bit MUST be 0) are the payload length.
$header[1] = chr(127);
$header[2] = chr(($dataLength & 0xFF00000000000000) >> 56);
$header[3] = chr(($dataLength & 0xFF000000000000) >> 48);
$header[4] = chr(($dataLength & 0xFF0000000000) >> 40);
$header[5] = chr(($dataLength & 0xFF00000000) >> 32);
$header[6] = chr(($dataLength & 0xFF000000) >> 24);
$header[7] = chr(($dataLength & 0xFF0000) >> 16);
$header[8] = chr(($dataLength & 0xFF00 ) >> 8);
$header[9] = chr( $dataLength & 0xFF );
$header_length = 10;
}
$result = socket_write($client, $header . $data, strlen($data) + $header_length);
if (!$result)
{
$this->disconnect($client);
$client = false;
}
$this->say("len(".strlen($data).")");
}
//*******************************************
function unwrap($data="")
{
$bytes = $data;
$dataLength = '';
$mask = '';
$coded_data = '';
$decodedData = '';
$secondByte = sprintf('%08b', ord($bytes[1]));
$masked = ($secondByte[0] == '1') ? true : false;
$dataLength = ($masked === true) ? ord($bytes[1]) & 127 : ord($bytes[1]);
$this->say("unwrap dataLength = ".$dataLength);
if($masked === true)
{
if($dataLength === 126)
{
$mask = substr($bytes, 4, 4);
$coded_data = substr($bytes, 8);
}
elseif($dataLength === 127)
{
$mask = substr($bytes, 10, 4);
$coded_data = substr($bytes, 14);
}
else
{
$mask = substr($bytes, 2, 4);
$coded_data = substr($bytes, 6);
}
for($i = 0; $i < strlen($coded_data); $i++)
{
$decodedData .= $coded_data[$i] ^ $mask[$i % 4];
}
}
else
{
if($dataLength === 126)
{
$decodedData = substr($bytes, 4);
}
elseif($dataLength === 127)
{
$decodedData = substr($bytes, 10);
}
else
{
$decodedData = substr($bytes, 2);
}
}
return $decodedData;
} |
Partager