user_apply.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/auth.php");
  3. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/languages/" . HTML_LANG . ".php");
  4. if (!defined("CONFIG")) die("Not defined");
  5. $page_url = null;
  6. $all_ok = true;
  7. if (getPOST("ApplyForAll", $page_url)) {
  8. // === Безопасное получение и приведение параметров через getPOST ===
  9. $auth_id = getPOST("fid", $page_url, []);
  10. $a_enabled = (int)getPOST("a_enabled", $page_url, 0);
  11. $a_dhcp = (int)getPOST("a_dhcp", $page_url, 0);
  12. $a_queue = (int)getPOST("a_queue_id", $page_url, 0);
  13. $a_group = (int)getPOST("a_group_id", $page_url, 0);
  14. $a_traf = (int)getPOST("a_traf", $page_url, 0);
  15. $a_day = (int)getPOST("a_day_q", $page_url, 0);
  16. $a_month = (int)getPOST("a_month_q", $page_url, 0);
  17. $a_ou_id = (int)getPOST("a_new_ou", $page_url, 0);
  18. $a_permanent = (int)getPOST("a_permanent", $page_url, 0);
  19. $a_bind_mac = (int)getPOST("a_bind_mac", $page_url, 0);
  20. $a_bind_ip = (int)getPOST("a_bind_ip", $page_url, 0);
  21. $a_create_netdev = (int)getPOST("a_create_netdev", $page_url, 0);
  22. $a_dhcp_acl = trim(getPOST("a_dhcp_acl", $page_url, ''));
  23. $a_dhcp_option_set = trim(getPOST("a_dhcp_option_set", $page_url, ''));
  24. foreach ($auth_id as $user_id_raw) {
  25. $user_id = (int)$user_id_raw;
  26. if (!$user_id) continue;
  27. $auth_updates = [];
  28. $user_updates = [];
  29. if (getPOST("e_enabled", $page_url) !== null) {
  30. $auth_updates['enabled'] = $a_enabled;
  31. $user_updates['enabled'] = $a_enabled;
  32. }
  33. if (getPOST("e_group_id", $page_url) !== null) {
  34. $auth_updates['filter_group_id'] = $a_group;
  35. }
  36. if (getPOST("e_queue_id", $page_url) !== null) {
  37. $auth_updates['queue_id'] = $a_queue;
  38. }
  39. if (getPOST("e_dhcp", $page_url) !== null) {
  40. $auth_updates['dhcp'] = $a_dhcp;
  41. }
  42. if (getPOST("e_dhcp_acl", $page_url) !== null) {
  43. $auth_updates['dhcp_acl'] = $a_dhcp_acl;
  44. }
  45. if (getPOST("e_dhcp_option_set", $page_url) !== null) {
  46. $auth_updates['dhcp_option_set'] = $a_dhcp_option_set;
  47. }
  48. if (getPOST("e_traf", $page_url) !== null) {
  49. $auth_updates['save_traf'] = $a_traf;
  50. }
  51. if (getPOST("e_day_q", $page_url) !== null) {
  52. $user_updates['day_quota'] = $a_day;
  53. }
  54. if (getPOST("e_month_q", $page_url) !== null) {
  55. $user_updates['month_quota'] = $a_month;
  56. }
  57. if (getPOST("e_new_ou", $page_url) !== null) {
  58. $user_updates['ou_id'] = $a_ou_id;
  59. $auth_updates['ou_id'] = $a_ou_id;
  60. }
  61. if (getPOST("e_permanent", $page_url) !== null) {
  62. $user_updates['permanent'] = $a_permanent;
  63. }
  64. // === Обновление user_list ===
  65. if (!empty($user_updates)) {
  66. $login_record = get_record($db_link, "user_list", "id = ?", [$user_id]);
  67. if ($login_record) {
  68. $msg .= " For all ip user id: " . $user_id . " login: " . ($login_record['login'] ?? '') . " set: ";
  69. $msg .= get_diff_rec($db_link, "user_list", "id = ?", $user_updates, 1, [$user_id]);
  70. $ret = update_record($db_link, "user_list", "id = ?", $user_updates, [$user_id]);
  71. if (!$ret) $all_ok = false;
  72. }
  73. }
  74. // === Получаем все активные auth записи пользователя ===
  75. $auth_list = get_records_sql($db_link,
  76. "SELECT id, mac, ip FROM user_auth WHERE deleted = 0 AND user_id = ?",
  77. [$user_id]
  78. );
  79. $b_mac = '';
  80. $b_ip = '';
  81. // === Обновляем каждую auth запись ===
  82. if (!empty($auth_list)) {
  83. foreach ($auth_list as $row) {
  84. if (empty($row['id'])) continue;
  85. if (empty($b_mac) && !empty($row['mac'])) $b_mac = $row['mac'];
  86. if (empty($b_ip) && !empty($row['ip'])) $b_ip = $row['ip'];
  87. if (!empty($auth_updates)) {
  88. $ret = update_record($db_link, "user_auth", "id = ?", $auth_updates, [(int)$row['id']]);
  89. if (!$ret) $all_ok = false;
  90. }
  91. }
  92. }
  93. // === Правило привязки MAC ===
  94. if (getPOST("e_bind_mac", $page_url) !== null) {
  95. if ($a_bind_mac && $b_mac) {
  96. $user_rule = get_record_sql($db_link,
  97. "SELECT * FROM auth_rules WHERE user_id = ? AND rule_type = 2",
  98. [$user_id]
  99. );
  100. $mac_rule = get_record_sql($db_link,
  101. "SELECT * FROM auth_rules WHERE rule = ? AND rule_type = 2",
  102. [$b_mac]
  103. );
  104. if (!$user_rule && !$mac_rule) {
  105. insert_record($db_link, "auth_rules", [
  106. 'user_id' => $user_id,
  107. 'rule_type' => 2,
  108. 'rule' => $b_mac
  109. ]);
  110. } else {
  111. LOG_INFO($db_link, "Auto rule for user_id: $user_id and mac $b_mac already exists");
  112. }
  113. } else {
  114. delete_records($db_link, "auth_rules","user_id = ? AND rule_type = 2", [$user_id]);
  115. }
  116. }
  117. // === Правило привязки IP ===
  118. if (getPOST("e_bind_ip", $page_url) !== null) {
  119. if ($a_bind_ip && $b_ip) {
  120. $user_rule = get_record_sql($db_link,
  121. "SELECT * FROM auth_rules WHERE user_id = ? AND rule_type = 1",
  122. [$user_id]
  123. );
  124. $ip_rule = get_record_sql($db_link,
  125. "SELECT * FROM auth_rules WHERE rule = ? AND rule_type = 1",
  126. [$b_ip]
  127. );
  128. if (!$user_rule && !$ip_rule) {
  129. insert_record($db_link, "auth_rules", [
  130. 'user_id' => $user_id,
  131. 'rule_type' => 1,
  132. 'rule' => $b_ip
  133. ]);
  134. } else {
  135. LOG_INFO($db_link, "Auto rule for user_id: $user_id and ip $b_ip already exists");
  136. }
  137. } else {
  138. delete_records($db_link, "auth_rules","user_id = ? AND rule_type = 1", [$user_id]);
  139. }
  140. }
  141. // === Создание сетевого устройства ===
  142. if (getPOST("e_create_netdev", $page_url) !== null && $a_create_netdev && $b_ip) {
  143. $existing_device = get_record_sql($db_link,
  144. "SELECT * FROM devices WHERE user_id = ?",
  145. [$user_id]
  146. );
  147. if (!$existing_device) {
  148. $latest_auth = get_record_sql($db_link,
  149. "SELECT * FROM user_auth WHERE user_id = ? ORDER BY last_found DESC",
  150. [$user_id]
  151. );
  152. if ($latest_auth) {
  153. $new_device = [
  154. 'user_id' => $user_id,
  155. 'device_name' => $login_record['login'] ?? 'user_' . $user_id,
  156. 'device_type' => 5,
  157. 'ip' => $latest_auth['ip'],
  158. 'community' => get_const('snmp_default_community'),
  159. 'snmp_version' => get_const('snmp_default_version'),
  160. 'login' => get_option($db_link, 28),
  161. 'password' => get_option($db_link, 29),
  162. 'protocol' => 0,
  163. 'control_port' => get_option($db_link, 30)
  164. ];
  165. $new_id = insert_record($db_link, "devices", $new_device);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. $message = $all_ok ? "Success!" : "Fail!";
  172. print "<div style='padding:20px; font-size:18px; background:#e9f7ef; border:1px solid #2ecc71;'>$message</div>";
  173. ?>