index-full.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. $default_displayed=100;
  3. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/auth.php");
  4. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/languages/" . HTML_LANG . ".php");
  5. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/header.php");
  6. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/datetimefilter.php");
  7. $default_sort='tin';
  8. $default_order='DESC';
  9. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/oufilter.php");
  10. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/sortfilter.php");
  11. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/gatefilter.php");
  12. $gateway_list = get_gateways($db_link);
  13. print_reports_submenu($page_url);
  14. ?>
  15. <div id="cont">
  16. <form action="index-full.php" method="post">
  17. <?php echo WEB_cell_ou; ?>:&nbsp<?php print_ou_select($db_link,'ou',$rou); ?>
  18. <?php print_date_fields($date1,$date2,$date_shift); ?>
  19. <?php echo WEB_cell_gateway; ?>:&nbsp<?php print_gateway_select($db_link, 'gateway', $rgateway); ?>
  20. <?php print WEB_rows_at_page."&nbsp"; print_row_at_pages('rows',$displayed); ?>
  21. <input id='btn_filter' name='btn_filter' type="submit" value="<?php echo WEB_btn_show; ?>">
  22. </form>
  23. <?php
  24. // === 1. Выбор таблицы статистики ===
  25. $traffic_stat_table = ($days_shift >= ($config["traffic_ipstat_history"] ?? 30))
  26. ? 'user_stats'
  27. : 'user_stats_full';
  28. // === 2. Безопасная сортировка ===
  29. $allowed_sort_fields = ['tin', 'tout', 'pin', 'pout', 'id', 'router_id'];
  30. $allowed_orders = ['ASC', 'DESC'];
  31. $sort_field = in_array($sort_field, $allowed_sort_fields, true) ? $sort_field : 'tin';
  32. $order = in_array(strtoupper($order), $allowed_orders, true) ? strtoupper($order) : 'DESC';
  33. $sort_sql = " ORDER BY $sort_field $order";
  34. // === 3. Базовые параметры ===
  35. $sql_params = [$date1, $date2];
  36. // === 4. Формируем запрос ===
  37. $trafSQL = "
  38. SELECT
  39. user_auth.id,
  40. {$traffic_stat_table}.router_id,
  41. SUM(byte_in) AS tin,
  42. SUM(byte_out) AS tout,
  43. MAX(ROUND(pkt_in / step)) AS pin,
  44. MAX(ROUND(pkt_out / step)) AS pout
  45. FROM {$traffic_stat_table}
  46. JOIN user_auth ON {$traffic_stat_table}.auth_id = user_auth.id
  47. JOIN user_list ON user_list.id = user_auth.user_id
  48. WHERE {$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 (корректный для текущего SELECT) ===
  61. $trafSQL .= " GROUP BY user_auth.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. Добавляем сортировку, LIMIT, OFFSET ===
  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\" cellspacing=\"1\" cellpadding=\"4\">\n";
  78. print "<tr align=\"center\">\n";
  79. print "<td ><b><a href=index-full.php?sort=login&order=$new_order>".WEB_cell_login."</a></b></td>\n";
  80. print "<td ><b><a href=index-full.php?sort=ip&order=$new_order>".WEB_cell_ip."</a></b></td>\n";
  81. print "<td ><b>".WEB_cell_gateway."</b></td>\n";
  82. print "<td ><b><a href=index-full.php?sort=tin&order=$new_order>".WEB_title_input."</a></b></td>\n";
  83. print "<td ><b><a href=index-full.php?sort=tout&order=$new_order>".WEB_title_output."<a></b></td>\n";
  84. print "<td ><b><a href=index-full.php?sort=pin&order=$new_order>".WEB_title_maxpktin."</a></b></td>\n";
  85. print "<td ><b><a href=index-full.php?sort=pout&order=$new_order>".WEB_title_maxpktout."<a></b></td>\n";
  86. print "</tr>\n";
  87. $total_in = 0;
  88. $total_out = 0;
  89. foreach ($traf as $row) {
  90. if ($row['tin'] + $row['tout'] == 0) { continue; }
  91. $total_in += $row['tin'];
  92. $total_out += $row['tout'];
  93. $s_router = !empty($gateway_list[$row['router_id']]) ? $gateway_list[$row['router_id']] : '';
  94. $cl = $row['tout'] > 2 * $row['tin'] ? "nb" : "data";
  95. $a_SQL='SELECT ip,U.login FROM user_auth, user_list as U where user_auth.user_id=U.id and user_auth.id=?';
  96. $auth_record = get_record_sql($db_link,$a_SQL,[$row['id']]);
  97. print "<tr align=center class=\"tr1\" onmouseover=\"className='tr2'\" onmouseout=\"className='tr1'\">\n";
  98. print "<td align=left class=\"$cl\">" . $auth_record['login'] . "</td>\n";
  99. print "<td align=left class=\"$cl\"><a href=authday.php?id=" . $row['id'] . "&date_start=$date1&date_stop=$date2>" . $auth_record['ip'] . "</a></td>\n";
  100. print "<td align=left class=\"$cl\">$s_router</td>\n";
  101. print "<td class=\"$cl\">" . fbytes($row['tin']) . "</td>\n";
  102. print "<td class=\"$cl\">" . fbytes($row['tout']) . "</td>\n";
  103. print "<td class=\"$cl\">" . fpkts($row['pin']) . "</td>\n";
  104. print "<td class=\"$cl\">" . fpkts($row['pout']) . "</td>\n";
  105. print "</tr>\n";
  106. }
  107. print "<tr align=center class=\"tr1\" onmouseover=\"className='tr2'\" onmouseout=\"className='tr1'\">\n";
  108. print "<td class=\"data\" colspan=2><b>".WEB_title_itog."</b></td>\n";
  109. print "<td class=\"data\"><b></b></td>\n";
  110. print "<td class=\"data\"><b>" . fbytes($total_in) . "</b></td>\n";
  111. print "<td class=\"data\"><b>" . fbytes($total_out) . "</b></td>\n";
  112. print "<td class=\"data\"><b></b></td>\n";
  113. print "<td class=\"data\"><b></b></td>\n";
  114. print "</tr>\n";
  115. ?>
  116. </table>
  117. <?php
  118. print_navigation($page_url,$page,$displayed,$count_records,$total);
  119. ?>
  120. <script>
  121. document.getElementById('ou').addEventListener('change', function(event) {
  122. const buttonApply = document.getElementById('btn_filter');
  123. buttonApply.click();
  124. });
  125. document.getElementById('rows').addEventListener('change', function(event) {
  126. const buttonApply = document.getElementById('btn_filter');
  127. buttonApply.click();
  128. });
  129. document.getElementById('gateway').addEventListener('change', function(event) {
  130. const buttonApply = document.getElementById('btn_filter');
  131. buttonApply.click();
  132. });
  133. </script>
  134. <?php
  135. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/footer.php");
  136. ?>