PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/plugins/core.mq/vendor/phpws/websocket.functions.php

https://github.com/sheafferusa/pydio-core
PHP | 216 lines | 139 code | 38 blank | 39 comment | 17 complexity | 36d03123f314226f46e15b28f9c7722b MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-3.0, GPL-3.0, BSD-3-Clause, LGPL-2.1, LGPL-2.0, AGPL-3.0, Apache-2.0
  1. <?php
  2. // mamta
  3. class HixieKey {
  4. public $number;
  5. public $key;
  6. public function __construct($number, $key) {
  7. $this->number = $number;
  8. $this->key = $key;
  9. }
  10. }
  11. class WebSocketProtocolVersions {
  12. const HIXIE_76 = 0;
  13. const HYBI_8 = 8;
  14. const HYBI_9 = 8;
  15. const HYBI_10 = 8;
  16. const HYBI_11 = 8;
  17. const HYBI_12 = 8;
  18. const LATEST = self::HYBI_12;
  19. private function __construct() {
  20. }
  21. }
  22. class WebSocketFunctions {
  23. /**
  24. * Parse a HTTP HEADER 'Cookie:' value into a key-value pair array
  25. *
  26. * @param string $line Value of the COOKIE header
  27. * @return array Key-value pair array
  28. */
  29. public static function cookie_parse($line) {
  30. $cookies = array();
  31. $csplit = explode(';', $line);
  32. $cdata = array();
  33. foreach ($csplit as $data) {
  34. $cinfo = explode('=', $data);
  35. $key = trim($cinfo[0]);
  36. $val = urldecode($cinfo[1]);
  37. $cookies[$key] = $val;
  38. }
  39. return $cookies;
  40. }
  41. public static function writeWholeBuffer($fp, $string) {
  42. for ($written = 0; $written < strlen($string); $written += $fwrite) {
  43. $fwrite = fwrite($fp, substr($string, $written));
  44. if ($fwrite === false) {
  45. return $written;
  46. }
  47. }
  48. return $written;
  49. }
  50. public static function readWholeBuffer($resource) {
  51. $buffer = '';
  52. $buffsize = 8192;
  53. $metadata['unread_bytes'] = 0;
  54. do {
  55. if (feof($resource)) {
  56. return false;
  57. }
  58. $result = fread($resource, $buffsize);
  59. if ($result === false) {
  60. return false;
  61. }
  62. $buffer .= $result;
  63. $metadata = stream_get_meta_data($resource);
  64. $buffsize = min($buffsize, $metadata['unread_bytes']);
  65. } while ($metadata['unread_bytes'] > 0);
  66. return $buffer;
  67. }
  68. /**
  69. * Parse HTTP request into an array
  70. *
  71. * @param string $header HTTP request as a string
  72. * @return array Headers as a key-value pair array
  73. */
  74. public static function parseHeaders($header) {
  75. $retVal = array();
  76. $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
  77. foreach ($fields as $field) {
  78. if (preg_match('/([^:]+): (.+)/m', $field, $match)) {
  79. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($matches){return strtoupper($matches[0]);}, strtolower(trim($match[1])));
  80. if (isset($retVal[$match[1]])) {
  81. $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
  82. } else {
  83. $retVal[$match[1]] = trim($match[2]);
  84. }
  85. }
  86. }
  87. if (preg_match("/GET (.*) HTTP/", $header, $match)) {
  88. $retVal['GET'] = $match[1];
  89. }
  90. return $retVal;
  91. }
  92. public static function calcHybiResponse($challenge) {
  93. return base64_encode(sha1($challenge . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true));
  94. }
  95. /**
  96. * Calculate the #76 draft key based on the 2 challenges from the client and the last 8 bytes of the request
  97. *
  98. * @param string $key1 Sec-WebSocket-Key1
  99. * @param string $key2 Sec-Websocket-Key2
  100. * @param string $l8b Last 8 bytes of the client's opening handshake
  101. */
  102. public static function calcHixieResponse($key1, $key2, $l8b) {
  103. // Get the numbers from the opening handshake
  104. $numbers1 = preg_replace("/[^0-9]/", "", $key1);
  105. $numbers2 = preg_replace("/[^0-9]/", "", $key2);
  106. //Count spaces
  107. $spaces1 = substr_count($key1, " ");
  108. $spaces2 = substr_count($key2, " ");
  109. if ($spaces1 == 0 || $spaces2 == 0) {
  110. throw new WebSocketInvalidKeyException($key1, $key2, $l8b);
  111. return null;
  112. }
  113. // Key is the number divided by the amount of spaces expressed as a big-endian 32 bit integer
  114. $key1_sec = pack("N", $numbers1 / $spaces1);
  115. $key2_sec = pack("N", $numbers2 / $spaces2);
  116. // The response is the md5-hash of the 2 keys and the last 8 bytes of the opening handshake, expressed as a binary string
  117. return md5($key1_sec . $key2_sec . $l8b, 1);
  118. }
  119. public static function randHybiKey() {
  120. return base64_encode(
  121. chr(rand(0, 255)) . chr(rand(0, 255)) . chr(rand(0, 255)) . chr(rand(0, 255))
  122. . chr(rand(0, 255)) . chr(rand(0, 255)) . chr(rand(0, 255)) . chr(rand(0, 255))
  123. . chr(rand(0, 255)) . chr(rand(0, 255)) . chr(rand(0, 255)) . chr(rand(0, 255))
  124. . chr(rand(0, 255)) . chr(rand(0, 255)) . chr(rand(0, 255)) . chr(rand(0, 255))
  125. );
  126. }
  127. /**
  128. * Output a line to stdout
  129. *
  130. * @param string $msg Message to output to the STDOUT
  131. */
  132. public static function say($msg = "") {
  133. echo date("Y-m-d H:i:s") . " | " . $msg . "\n";
  134. }
  135. // mamta
  136. public static function genKey3() {
  137. return "" . chr(rand(0, 255)) . chr(rand(0, 255)) . chr(rand(0, 255)) . chr(rand(0, 255))
  138. . chr(rand(0, 255)) . chr(rand(0, 255)) . chr(rand(0, 255)) . chr(rand(0, 255));
  139. }
  140. public static function randHixieKey() {
  141. $_MAX_INTEGER = (1 << 32) - 1;
  142. #$_AVAILABLE_KEY_CHARS = range(0x21, 0x2f + 1) + range(0x3a, 0x7e + 1);
  143. #$_MAX_CHAR_BYTE = (1<<8) -1;
  144. # $spaces_n = 2;
  145. $spaces_n = rand(1, 12); // random.randint(1, 12)
  146. $max_n = $_MAX_INTEGER / $spaces_n;
  147. # $number_n = 123456789;
  148. $number_n = rand(0, $max_n); // random.randint(0, max_n)
  149. $product_n = $number_n * $spaces_n;
  150. $key_n = "" . $product_n;
  151. # $range = 3; //
  152. $range = rand(1, 12);
  153. for ($i = 0; $i < $range; $i++) {
  154. #i in range(random.randint(1, 12)):
  155. if (rand(0, 1) > 0) {
  156. $c = chr(rand(0x21, 0x2f + 1)); #random.choice(_AVAILABLE_KEY_CHARS)
  157. } else {
  158. $c = chr(rand(0x3a, 0x7e + 1)); #random.choice(_AVAILABLE_KEY_CHARS)
  159. }
  160. # $c = chr(65);
  161. $len = strlen($key_n);
  162. # $pos = 2;
  163. $pos = rand(0, $len);
  164. $key_n1 = substr($key_n, 0, $pos);
  165. $key_n2 = substr($key_n, $pos);
  166. $key_n = $key_n1 . $c . $key_n2;
  167. }
  168. for ($i = 0; $i < $spaces_n; $i++) {
  169. $len = strlen($key_n);
  170. # $pos = 2;
  171. $pos = rand(1, $len - 1);
  172. $key_n1 = substr($key_n, 0, $pos);
  173. $key_n2 = substr($key_n, $pos);
  174. $key_n = $key_n1 . " " . $key_n2;
  175. }
  176. return new HixieKey($number_n, $key_n);
  177. }
  178. }