functions.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * 批量请求
  4. * @param array $request_buffer_array ['ip:port'=>req_buf, 'ip:port'=>req_buf, ...]
  5. * @return multitype:unknown string
  6. */
  7. function multiRequest($request_buffer_array)
  8. {
  9. \Statistics\Lib\Cache::$lastSuccessIpArray = array();
  10. $client_array = $sock_to_ip = $ip_list = array();
  11. foreach($request_buffer_array as $address => $buffer)
  12. {
  13. list($ip, $port) = explode(':', $address);
  14. $ip_list[$ip] = $ip;
  15. $client = stream_socket_client("tcp://$address", $errno, $errmsg, 1);
  16. if(!$client)
  17. {
  18. continue;
  19. }
  20. $client_array[$address] = $client;
  21. stream_set_timeout($client_array[$address], 0, 100000);
  22. fwrite($client_array[$address], $buffer);
  23. stream_set_blocking($client_array[$address], 0);
  24. $sock_to_address[(int)$client] = $address;
  25. }
  26. $read = $client_array;
  27. $write = $except = $read_buffer = array();
  28. $time_start = microtime(true);
  29. $timeout = 0.99;
  30. // 轮询处理数据
  31. while(count($read) > 0)
  32. {
  33. if(@stream_select($read, $write, $except, 0, 200000))
  34. {
  35. foreach($read as $socket)
  36. {
  37. $address = $sock_to_address[(int)$socket];
  38. $buf = fread($socket, 8192);
  39. if(!$buf)
  40. {
  41. if(feof($socket))
  42. {
  43. unset($client_array[$address]);
  44. }
  45. continue;
  46. }
  47. if(!isset($read_buffer[$address]))
  48. {
  49. $read_buffer[$address] = $buf;
  50. }
  51. else
  52. {
  53. $read_buffer[$address] .= $buf;
  54. }
  55. // 数据接收完毕
  56. if(($len = strlen($read_buffer[$address])) && $read_buffer[$address][$len-1] === "\n")
  57. {
  58. unset($client_array[$address]);
  59. }
  60. }
  61. }
  62. // 超时了
  63. if(microtime(true) - $time_start > $timeout)
  64. {
  65. break;
  66. }
  67. $read = $client_array;
  68. }
  69. foreach($read_buffer as $address => $buf)
  70. {
  71. list($ip, $port) = explode(':', $address);
  72. \Statistics\Lib\Cache::$lastSuccessIpArray[$ip] = $ip;
  73. }
  74. \Statistics\Lib\Cache::$lastFailedIpArray = array_diff($ip_list, \Statistics\Lib\Cache::$lastSuccessIpArray);
  75. ksort($read_buffer);
  76. return $read_buffer;
  77. }
  78. /**
  79. * 检查是否登录
  80. */
  81. function check_auth()
  82. {
  83. // 如果配置中管理员用户名密码为空则说明不用验证
  84. if(Statistics\Config\Config::$adminName == '' && Statistics\Config\Config::$adminPassword == '')
  85. {
  86. return true;
  87. }
  88. // 进入验证流程
  89. _session_start();
  90. if(!isset($_SESSION['admin']))
  91. {
  92. if(!isset($_POST['admin_name']) || !isset($_POST['admin_password']))
  93. {
  94. include ST_ROOT . '/Views/login.tpl.php';
  95. _exit();
  96. }
  97. else
  98. {
  99. $admin_name = $_POST['admin_name'];
  100. $admin_password = $_POST['admin_password'];
  101. if($admin_name != Statistics\Config\Config::$adminName || $admin_password != Statistics\Config\Config::$adminPassword)
  102. {
  103. $msg = "用户名或者密码不正确";
  104. include ST_ROOT . '/Views/login.tpl.php';
  105. _exit();
  106. }
  107. $_SESSION['admin'] = $admin_name;
  108. }
  109. }
  110. return true;
  111. }
  112. /**
  113. * 启动session,兼容fpm
  114. */
  115. function _session_start()
  116. {
  117. if(defined('WORKERMAN_ROOT_DIR'))
  118. {
  119. return \Man\Common\Protocols\Http\session_start();
  120. }
  121. return session_start();
  122. }
  123. /**
  124. * 退出
  125. * @param string $str
  126. */
  127. function _exit($str = '')
  128. {
  129. if(defined('WORKERMAN_ROOT_DIR'))
  130. {
  131. return \Man\Common\Protocols\Http\jump_exit($str);
  132. }
  133. return exit($str);
  134. }