user_apply.php 9.2 KB

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