admin.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Statistics\Modules;
  3. function admin()
  4. {
  5. $act = isset($_GET['act'])? $_GET['act'] : 'home';
  6. $err_msg = $notice_msg = $suc_msg = $ip_list_str = '';
  7. $action = 'save_server_list';
  8. switch($act)
  9. {
  10. case 'detect_server':
  11. // 创建udp socket
  12. $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  13. socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);
  14. $buffer = json_encode(array('cmd'=>'REPORT_IP'))."\n";
  15. // 广播
  16. socket_sendto($socket, $buffer, strlen($buffer), 0, '255.255.255.255', \Statistics\Config\Config::$ProviderPort);
  17. // 超时相关
  18. $time_start = microtime(true);
  19. $global_timeout = 1;
  20. $ip_list = array();
  21. $recv_timeout = array('sec'=>0,'usec'=>8000);
  22. socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$recv_timeout);
  23. // 循环读数据
  24. while(microtime(true) - $time_start < $global_timeout)
  25. {
  26. $buf = $host = $port = '';
  27. if(@socket_recvfrom($socket, $buf, 65535, 0, $host, $port))
  28. {
  29. $ip_list[$host] = $host;
  30. }
  31. }
  32. // 过滤掉已经保存的ip
  33. $count = 0;
  34. foreach($ip_list as $ip)
  35. {
  36. if(!isset(\Statistics\Lib\Cache::$ServerIpList[$ip]))
  37. {
  38. $ip_list_str .= $ip."\r\n";
  39. $count ++;
  40. }
  41. }
  42. $action = 'add_to_server_list';
  43. $notice_msg = "探测到{$count}个新数据源";
  44. break;
  45. case 'add_to_server_list':
  46. if(empty($_POST['ip_list']))
  47. {
  48. $err_msg = "保存的ip列表为空";
  49. break;
  50. }
  51. $ip_list = explode("\n", $_POST['ip_list']);
  52. if($ip_list)
  53. {
  54. foreach($ip_list as $ip)
  55. {
  56. $ip = trim($ip);
  57. if(false !== ip2long($ip))
  58. {
  59. \Statistics\Lib\Cache::$ServerIpList[$ip] = $ip;
  60. }
  61. }
  62. }
  63. $suc_msg = "添加成功";
  64. foreach(\Statistics\Lib\Cache::$ServerIpList as $ip)
  65. {
  66. $ip_list_str .= $ip."\r\n";
  67. }
  68. saveServerIpListToCache();
  69. break;
  70. case 'save_server_list':
  71. if(empty($_POST['ip_list']))
  72. {
  73. $err_msg = "保存的ip列表为空";
  74. break;
  75. }
  76. \Statistics\Lib\Cache::$ServerIpList = array();
  77. $ip_list = explode("\n", $_POST['ip_list']);
  78. if($ip_list)
  79. {
  80. foreach($ip_list as $ip)
  81. {
  82. $ip = trim($ip);
  83. if(false !== ip2long($ip))
  84. {
  85. \Statistics\Lib\Cache::$ServerIpList[$ip] = $ip;
  86. }
  87. }
  88. }
  89. $suc_msg = "保存成功";
  90. foreach(\Statistics\Lib\Cache::$ServerIpList as $ip)
  91. {
  92. $ip_list_str .= $ip."\r\n";
  93. }
  94. saveServerIpListToCache();
  95. break;
  96. default:
  97. foreach(\Statistics\Lib\Cache::$ServerIpList as $ip)
  98. {
  99. $ip_list_str .= $ip."\r\n";
  100. }
  101. }
  102. include ST_ROOT . '/Views/header.tpl.php';
  103. include ST_ROOT . '/Views/admin.tpl.php';
  104. include ST_ROOT . '/Views/footer.tpl.php';
  105. }
  106. function saveServerIpListToCache()
  107. {
  108. foreach(glob(ST_ROOT . '/Config/Cache/*.iplist.cache.php') as $php_file)
  109. {
  110. unlink($php_file);
  111. }
  112. file_put_contents(ST_ROOT . '/Config/Cache/'.time().'.iplist.cache.php', "<?php\n\\Statistics\\Lib\\Cache::\$ServerIpList=".var_export(\Statistics\Lib\Cache::$ServerIpList,true).';');
  113. }