index.php 5.9 KB

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