index.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/auth.php");
  3. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/languages/" . HTML_LANG . ".php");
  4. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/header.php");
  5. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/datetimefilter.php");
  6. $default_sort='tin';
  7. $default_order='DESC';
  8. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/oufilter.php");
  9. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/sortfilter.php");
  10. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/gatefilter.php");
  11. $gateway_list = get_gateways($db_link);
  12. print_reports_submenu($page_url);
  13. ?>
  14. <div id="cont">
  15. <form action="index.php" method="post">
  16. <?php echo WEB_cell_ou; ?>:&nbsp<?php print_ou_select($db_link,'ou',$rou); ?>
  17. <?php print_date_fields($date1,$date2,$date_shift); ?>
  18. <?php echo WEB_cell_gateway; ?>:&nbsp<?php print_gateway_select($db_link, 'gateway', $rgateway); ?>
  19. <?php print WEB_rows_at_page."&nbsp"; print_row_at_pages('rows',$displayed); ?>
  20. <input type="submit" value="<?php echo WEB_btn_show; ?>">
  21. </form>
  22. <?php
  23. // === 1. Выбор таблицы ===
  24. $traffic_stat_table = ($days_shift >= ($config["traffic_ipstat_history"] ?? 30))
  25. ? 'user_stats'
  26. : 'user_stats_full';
  27. // === 2. Безопасная сортировка ===
  28. $allowed_sort = ['tin', 'tout', 'pin', 'pout', 'user_id', 'router_id'];
  29. $allowed_order = ['ASC', 'DESC'];
  30. $sort_field = in_array($sort_field, $allowed_sort, true) ? $sort_field : 'tin';
  31. $order = in_array(strtoupper($order), $allowed_order, true) ? strtoupper($order) : 'DESC';
  32. $sort_sql = " ORDER BY $sort_field $order";
  33. // === 3. Базовые параметры ===
  34. $sql_params = [$date1, $date2];
  35. // === 4. Формируем запрос ===
  36. $trafSQL = "
  37. SELECT
  38. user_auth.user_id,
  39. {$traffic_stat_table}.router_id,
  40. SUM(byte_in) AS tin,
  41. SUM(byte_out) AS tout,
  42. MAX(ROUND(pkt_in / step)) AS pin,
  43. MAX(ROUND(pkt_out / step)) AS pout
  44. FROM {$traffic_stat_table}, user_auth, user_list
  45. WHERE
  46. user_list.id = user_auth.user_id
  47. AND {$traffic_stat_table}.auth_id = user_auth.id
  48. AND {$traffic_stat_table}.ts >= ?
  49. AND {$traffic_stat_table}.ts < ?
  50. ";
  51. // === 5. Дополнительные условия ===
  52. if ($rou !== 0) {
  53. $trafSQL .= " AND user_list.ou_id = ?";
  54. $sql_params[] = (int)$rou;
  55. }
  56. if ($rgateway > 0) {
  57. $trafSQL .= " AND {$traffic_stat_table}.router_id = ?";
  58. $sql_params[] = (int)$rgateway;
  59. }
  60. // === 6. GROUP BY ===
  61. $trafSQL .= " GROUP BY user_auth.user_id, {$traffic_stat_table}.router_id";
  62. // === 7. Подсчёт записей ===
  63. $countSQL = "SELECT COUNT(*) FROM ($trafSQL) AS subquery";
  64. $count_records = (int)get_single_field($db_link, $countSQL, $sql_params);
  65. // === 8. Пагинация ===
  66. $total = ceil($count_records / $displayed);
  67. $page = max(1, min($page, $total));
  68. $start = ($page - 1) * $displayed;
  69. print_navigation($page_url, $page, $displayed, $count_records, $total);
  70. // === 9. Добавляем сортировку + пагинацию ===
  71. $trafSQL .= $sort_sql . " LIMIT ? OFFSET ?";
  72. $sql_params[] = (int)$displayed;
  73. $sql_params[] = (int)$start;
  74. // === 10. Выполняем запрос ===
  75. $traf = get_records_sql($db_link, $trafSQL, $sql_params);
  76. print "<br><br>\n";
  77. print "<table class=\"data\">\n";
  78. print "<tr class=\"info\">\n";
  79. print "<td ><b><a href=index.php?sort=login&order=$new_order>".WEB_cell_login."</a></b></td>\n";
  80. print "<td ><b>".WEB_cell_gateway."</b></td>\n";
  81. print "<td ><b><a href=index.php?sort=tin&order=$new_order>".WEB_title_input."</a></b></td>\n";
  82. print "<td ><b><a href=index.php?sort=tout&order=$new_order>".WEB_title_output."<a></b></td>\n";
  83. print "<td ><b><a href=index.php?sort=pin&order=$new_order>".WEB_title_maxpktin."</a></b></td>\n";
  84. print "<td ><b><a href=index.php?sort=pout&order=$new_order>".WEB_title_maxpktout."<a></b></td>\n";
  85. print "</tr>\n";
  86. $total_in = 0;
  87. $total_out = 0;
  88. foreach ($traf as $row) {
  89. if ($row['tin'] + $row['tout'] == 0) { continue; }
  90. $total_in += $row['tin'];
  91. $total_out += $row['tout'];
  92. $s_router = !empty($gateway_list[$row['router_id']]) ? $gateway_list[$row['router_id']] : '';
  93. $cl = $row['tout'] > 2 * $row['tin'] ? "nb" : "data";
  94. $u_SQL='SELECT * FROM user_list WHERE id=?';
  95. $user_record = get_record_sql($db_link,$u_SQL,[$row['user_id']]);
  96. print "<tr align=center class=\"tr1\" onmouseover=\"className='tr2'\" onmouseout=\"className='tr1'\">\n";
  97. print "<td align=left class=\"$cl\"><a href=userday.php?id=" . $row['user_id'] . "&date_start=$date1&date_stop=$date2>" . $user_record['login'] . "</a></td>\n";
  98. print "<td align=left class=\"$cl\">$s_router</td>\n";
  99. print "<td class=\"$cl\">" . fbytes($row['tin']) . "</td>\n";
  100. print "<td class=\"$cl\">" . fbytes($row['tout']) . "</td>\n";
  101. print "<td class=\"$cl\">" . fpkts($row['pin']) . "</td>\n";
  102. print "<td class=\"$cl\">" . fpkts($row['pout']) . "</td>\n";
  103. print "</tr>\n";
  104. }
  105. print "<tr align=center class=\"tr1\" onmouseover=\"className='tr2'\" onmouseout=\"className='tr1'\">\n";
  106. print "<td class=\"data\" colspan=2><b>".WEB_title_itog."</b></td>\n";
  107. print "<td class=\"data\"><b>" . fbytes($total_in) . "</b></td>\n";
  108. print "<td class=\"data\"><b>" . fbytes($total_out) . "</b></td>\n";
  109. print "<td class=\"data\"><b></b></td>\n";
  110. print "<td class=\"data\"><b></b></td>\n";
  111. print "</tr>\n";
  112. ?>
  113. </table>
  114. <?php
  115. print_navigation($page_url,$page,$displayed,$count_records,$total);
  116. ?>
  117. <script>
  118. document.getElementById('ou').addEventListener('change', function(event) {
  119. const buttonApply = document.getElementById('btn_filter');
  120. buttonApply.click();
  121. });
  122. document.getElementById('rows').addEventListener('change', function(event) {
  123. const buttonApply = document.getElementById('btn_filter');
  124. buttonApply.click();
  125. });
  126. document.getElementById('gateway').addEventListener('change', function(event) {
  127. const buttonApply = document.getElementById('btn_filter');
  128. buttonApply.click();
  129. });
  130. </script>
  131. <?php
  132. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/footer.php");
  133. ?>