user_apply.php 8.4 KB

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