index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. $default_displayed = 500;
  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/oufilter.php");
  7. $default_sort='login';
  8. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/sortfilter.php");
  9. $msg_error = "";
  10. if (getPOST("create") !== null) {
  11. $login = trim(getPOST("newlogin", null, ''));
  12. if ($login !== '') {
  13. // Проверка существования логина
  14. $lcount = get_count_records($db_link, "user_list", "LOWER(login) = LOWER(?)", [$login]);
  15. if ($lcount > 0) {
  16. $msg_error = WEB_cell_login . " " . $login . " " . $msg_exists . "!";
  17. } else {
  18. $new = ['login' => $login];
  19. // Определение OU
  20. if ($rou > 0) {
  21. $new['ou_id'] = $rou;
  22. } else {
  23. $rou = 3;
  24. $ou_exists = get_record_sql($db_link, "SELECT id FROM ou WHERE id = ?", [$rou]);
  25. if (empty($ou_exists)) {
  26. $new['ou_id'] = $default_user_ou_id; // по умолчанию
  27. } else {
  28. $new['ou_id'] = $rou;
  29. }
  30. }
  31. // Наследование настроек от OU
  32. $ou_info = get_record_sql($db_link, "SELECT * FROM ou WHERE id = ?", [$new['ou_id']]);
  33. if (!empty($ou_info)) {
  34. $new['enabled'] = isset($ou_info['enabled']) ? (int)$ou_info['enabled'] : 0;
  35. $new['queue_id'] = isset($ou_info['queue_id']) ? (int)$ou_info['queue_id'] : 0;
  36. $new['filter_group_id'] = isset($ou_info['filter_group_id']) ? (int)$ou_info['filter_group_id'] : 0;
  37. } else {
  38. // Если OU не найден — значения по умолчанию
  39. $new['enabled'] = 0;
  40. $new['queue_id'] = 0;
  41. $new['filter_group_id'] = 0;
  42. }
  43. $lid = insert_record($db_link, "user_list", $new);
  44. if (!empty($lid)) {
  45. header("Location: edituser.php?id=$lid");
  46. exit;
  47. }
  48. }
  49. }
  50. header("Location: " . $_SERVER["REQUEST_URI"]);
  51. exit;
  52. }
  53. ?>
  54. <div id="cont">
  55. <?php
  56. if ($msg_error) {
  57. print "<div id='msg'><b>$msg_error</b></div><br>\n";
  58. }
  59. ?>
  60. <form id="filter" name="filter" action="index.php" method="post">
  61. <div>
  62. <b><?php print WEB_cell_ou; ?> - </b>
  63. <?php print_ou_select($db_link, 'ou', $rou); print WEB_rows_at_page."&nbsp"; print_row_at_pages('rows',$displayed); ?>
  64. <input id="btn_filter" name="btn_filter" type="submit" value="<?php echo WEB_btn_show; ?>">
  65. </div>
  66. <br>
  67. <div>
  68. <?php echo WEB_new_user."&nbsp"; ?>
  69. <input type=text name=newlogin value="Unknown">
  70. <input type="submit" name="create" value="<?php echo WEB_btn_add; ?>">
  71. </div>
  72. </form>
  73. <br>
  74. <a class="mainButton" href="#modal"><?php print WEB_btn_apply_selected; ?></a>
  75. <div class="remodal" data-remodal-options="closeOnConfirm: true" data-remodal-id="modal" role="dialog" aria-labelledby="modal1Title" aria-describedby="modal1Desc">
  76. <div class="remodalBorder">
  77. <button data-remodal-action="close" class="remodal-close" aria-label="Close"></button>
  78. <form id="formUserApply">
  79. <h2 id="modal1Title"><?php print WEB_selection_title; ?></h2>
  80. <input type="hidden" name="ApplyForAll" value="MassChange">
  81. <table class="data" align=center>
  82. <tr><td><input type=checkbox class="putField" name="e_enabled" value='1'></td><td align=left><?php print WEB_cell_enabled."</td><td align=right>";print_qa_select('a_enabled', 0); ?></td></tr>
  83. <tr><td><input type=checkbox class="putField" name="e_group_id" value='1'></td><td align=left><?php print WEB_cell_filter."</td><td align=right>";print_filter_group_select($db_link, 'a_group_id', 0); ?></td></tr>
  84. <tr><td><input type=checkbox class="putField" name="e_queue_id" value='1'></td><td align=left><?php print WEB_cell_shaper."</td><td align=right>";print_queue_select($db_link, 'a_queue_id', 0); ?></td></tr>
  85. <tr><td><input type=checkbox class="putField" name="e_day_q" value='1'></td><td align=left><?php print WEB_cell_perday."</td><td align=right>"; ?><input type="text" name="a_day_q" value="0" size=5></td></tr>
  86. <tr><td><input type=checkbox class="putField" name="e_month_q" value='1'></td><td align=left><?php print WEB_cell_permonth."</td><td align=right>"; ?><input type="text" name="a_month_q" value="0" size=5></td></tr>
  87. <tr><td><input type=checkbox class="putField" name="e_new_ou" value='1'></td><td align=left><?php print WEB_cell_ou."</td><td align=right>";print_ou_select($db_link, 'a_new_ou', $rou); ?></td></tr>
  88. <tr><td><input type=checkbox class="putField" name="e_bind_mac" value='1'></td><td align=left><?php print WEB_user_bind_mac."</td><td align=right>";print_qa_select('a_bind_mac', 1);?></td></tr>
  89. <tr><td><input type=checkbox class="putField" name="e_bind_ip" value='1'></td><td align=left><?php print WEB_user_bind_ip."</td><td align=right>";print_qa_select('a_bind_ip', 1);?></td></tr>
  90. <tr><td><input type=checkbox class="putField" name="e_create_netdev" value='1'></td><td align=left><?php print WEB_user_create_netdev."</td><td align=right>";print_qa_select('a_create_netdev', 1);?></td></tr>
  91. <tr><td><input type=checkbox class="putField" name="e_permanent" value='1'></td><td align=left><?php print WEB_user_permanent."</td><td align=right>";print_qa_select('a_permanent', 0);?></td></tr>
  92. </table>
  93. <input type="submit" name="submit" class="btn" value="<?php echo WEB_btn_apply; ?>">
  94. </form>
  95. </div>
  96. </div>
  97. <a class="delButton" href="#modalDel"><?php print WEB_btn_delete; ?></a>
  98. <div class="remodal" data-remodal-options="closeOnConfirm: true" data-remodal-id="modalDel" role="dialog" aria-labelledby="modal1Title" aria-describedby="modal1Desc">
  99. <div class="remodalBorder">
  100. <button data-remodal-action="close" class="remodal-close" aria-label="Close"></button>
  101. <form id="formUserDel">
  102. <h2 id="modal1Title"><?php print WEB_msg_delete_selected; ?></h2>
  103. <input type="hidden" name="RemoveUser" value="MassChange">
  104. <?php print_qa_select('f_deleted', 0);?><br><br>
  105. <input type="submit" name="submit" class="btn" value="<?php echo WEB_btn_apply; ?>">
  106. </form>
  107. </div>
  108. </div>
  109. <?php
  110. $sort_url = "<a href=/admin/users/index.php?";
  111. // === 1. Базовые условия ===
  112. $params = [];
  113. $conditions = ["U.deleted = 0", "U.ou_id = O.id"];
  114. if ($rou != 0) {
  115. $conditions[] = "U.ou_id = ?";
  116. $params[] = (int)$rou;
  117. }
  118. $whereClause = implode(' AND ', $conditions);
  119. // === 2. Безопасная сортировка (БЕЛЫЙ СПИСОК!) ===
  120. $allowed_sort_fields = ['id', 'login', 'description', 'ou_name', 'enabled', 'day_quota', 'month_quota', 'blocked', 'permanent'];
  121. $allowed_order = ['ASC', 'DESC'];
  122. $sort_field = in_array($sort_field, $allowed_sort_fields, true) ? $sort_field : 'id';
  123. $order = in_array(strtoupper($order), $allowed_order, true) ? strtoupper($order) : 'ASC';
  124. // === 3. Подсчёт записей ===
  125. $countSQL = "SELECT COUNT(*) FROM user_list U JOIN ou O ON U.ou_id = O.id WHERE $whereClause";
  126. $count_records = (int)get_single_field($db_link, $countSQL, $params);
  127. // === 4. Пагинация ===
  128. $total = ceil($count_records / $displayed);
  129. $page = max(1, min($page, $total));
  130. $start = ($page - 1) * $displayed;
  131. print_navigation($page_url, $page, $displayed, $count_records, $total);
  132. // === 5. Запрос данных ===
  133. $limit = (int)$displayed;
  134. $offset = (int)$start;
  135. $dataParams = array_merge($params, [$limit, $offset]);
  136. $sSQL = "
  137. SELECT
  138. U.id, U.login, U.description, O.ou_name, U.enabled,
  139. U.day_quota, U.month_quota, U.blocked, U.permanent
  140. FROM user_list U
  141. JOIN ou O ON U.ou_id = O.id
  142. WHERE $whereClause
  143. ORDER BY U.$sort_field $order
  144. LIMIT ? OFFSET ?
  145. ";
  146. $users = get_records_sql($db_link, $sSQL, $dataParams);
  147. ?>
  148. <form id="def" name="def" >
  149. <table class="data">
  150. <tr align="center">
  151. <td><input type="checkbox" onClick="checkAll(this.checked);"></td>
  152. <td><b><?php print $sort_url . "sort=id&order=$new_order>id</a>"; ?></b></td>
  153. <td><b><?php print $sort_url . "sort=login&order=$new_order>" . WEB_cell_login . "</a>"; ?></b></td>
  154. <td><b><?php print $sort_url . "sort=description&order=$new_order>" . WEB_cell_description . "</a>"; ?></b></td>
  155. <td><b><?php print WEB_cell_rule; ?></b></td>
  156. <td><b><?php print WEB_cell_ou; ?></b></td>
  157. <td><b><?php print WEB_cell_enabled; ?></b></td>
  158. <td><b><?php print WEB_cell_perday; ?></b></td>
  159. <td><b><?php print WEB_cell_permonth; ?></b></td>
  160. <td><b><?php print WEB_cell_report; ?></b></td>
  161. </tr>
  162. <?php
  163. foreach ($users as $row) {
  164. $auth_customs = get_count_records($db_link,"user_auth","user_id=? AND deleted=0 AND enabled <>?", [ $row['id'],$row['enabled'] ] );
  165. $cl = "data";
  166. if (! $row['enabled']) {
  167. $cl = "off";
  168. }
  169. if ($row['blocked']) {
  170. $cl = "error";
  171. }
  172. if ($auth_customs > 0) {
  173. $cl = "custom";
  174. }
  175. if (! get_auth_count($db_link, $row['id'])) {
  176. $cl = 'nb';
  177. }
  178. print "<tr align=center>\n";
  179. print "<td class=\"$cl\" style='padding:0'><input type=checkbox name=fid[] value=".$row['id']."></td>\n";
  180. $cl_id = $cl;
  181. if (!empty($row['permanent']) and $row['permanent'] == 1) { $cl_id = 'warn'; }
  182. print "<td class=\"$cl_id\">".$row['id']."</td>\n";
  183. if (empty($row['login'])) { $row['login']=$row['id']; }
  184. print "<td class=\"$cl\" align=left><a href=edituser.php?id=".$row['id'].">" . $row['login'] . "</a></td>\n";
  185. print "<td class=\"$cl\">".$row['description']."</td>\n";
  186. $rules_count = get_count_records($db_link,"auth_rules","user_id=?", [$row['id']]);
  187. print "<td class=\"$cl\">".$rules_count."</td>\n";
  188. print "<td class=\"$cl\">".$row['ou_name']."</td>\n";
  189. print "<td class=\"$cl\">".get_qa($row['enabled']) . "</td>\n";
  190. print "<td class=\"$cl\">".$row['day_quota']."</td>\n";
  191. print "<td class=\"$cl\">".$row['month_quota']."</td>\n";
  192. print "<td class=\"$cl\" align=center colspan=2><a href=../reports/userday.php?id=".$row['id'].">Просмотр</a></td>\n";
  193. }
  194. ?>
  195. </table>
  196. <?php
  197. print_navigation($page_url,$page,$displayed,$count_records,$total);
  198. ?>
  199. </form>
  200. <table class="data">
  201. <tr>
  202. <td colspan = 3><?php echo WEB_color_description; ?></td>
  203. </tr>
  204. <tr>
  205. <td class="nb"><?php echo WEB_color_user_empty; ?></td>
  206. <td class="off"><?php echo WEB_color_user_disabled; ?></td>
  207. <td class="error"><?php echo WEB_color_user_blocked; ?></td>
  208. <td class="custom"><?php echo WEB_color_user_custom; ?></td>
  209. <td class="warn"><?php echo WEB_color_user_permanent; ?></td>
  210. </table>
  211. <script src="/js/remodal/remodal.min.js"></script>
  212. <script src="/js/remodal-user.js"></script>
  213. <script>
  214. document.getElementById('ou').addEventListener('change', function(event) {
  215. const buttonApply = document.getElementById('btn_filter');
  216. buttonApply.click();
  217. });
  218. document.getElementById('rows').addEventListener('change', function(event) {
  219. const buttonApply = document.getElementById('btn_filter');
  220. buttonApply.click();
  221. });
  222. </script>
  223. <?php
  224. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/footer.php");
  225. ?>