user_apply.php 8.3 KB

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