edituser.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. require_once($_SERVER["DOCUMENT_ROOT"] . "/inc/auth.php");
  3. require_once($_SERVER["DOCUMENT_ROOT"] . "/inc/languages/" . HTML_LANG . ".php");
  4. require_once($_SERVER["DOCUMENT_ROOT"] . "/inc/idfilter.php");
  5. $default_sort = 'ip_int';
  6. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/sortfilter.php");
  7. $msg_error = "";
  8. $sSQL = "SELECT * FROM User_list WHERE id=$id";
  9. $user_info = get_record_sql($db_link, $sSQL);
  10. if (empty($user_info)) {
  11. header("Location: /admin/");
  12. }
  13. if (isset($_POST["edituser"])) {
  14. unset($new);
  15. $new["ou_id"] = $_POST["f_ou"] * 1;
  16. $new["filter_group_id"] = $_POST["f_filter"] * 1;
  17. $new["queue_id"] = $_POST["f_queue"] * 1;
  18. $new["login"] = trim($_POST["f_login"]);
  19. $new["fio"] = trim($_POST["f_fio"]);
  20. if (get_const('default_user_ou_id') == $new["ou_id"] or get_const('default_hotspot_ou_id') == $new["ou_id"]) {
  21. $new["enabled"] = 0;
  22. $new["blocked"] = 0;
  23. $new["day_quota"] = 0;
  24. $new["month_quota"] = 0;
  25. $new["permanent"] = 0;
  26. } else {
  27. $new["enabled"] = get_int($_POST["f_enabled"]);
  28. $new["blocked"] = get_int($_POST["f_blocked"]);
  29. $new["day_quota"] = get_int(trim($_POST["f_perday"]));
  30. $new["month_quota"] = get_int(trim($_POST["f_permonth"]));
  31. $new["permanent"] = $_POST["f_permanent"] * 1;
  32. }
  33. $changes = get_diff_rec($db_link, "User_list", "id='$id'", $new, 0);
  34. if (!empty($changes)) {
  35. LOG_WARNING($db_link, "Changed user id: $id login: " . $new["login"] . ". \r\nApply: $changes");
  36. }
  37. update_record($db_link, "User_list", "id='$id'", $new);
  38. if (!$new["enabled"]) {
  39. run_sql($db_link, "UPDATE User_auth SET enabled=0, changed=1 WHERE user_id=" . $id);
  40. }
  41. if (!empty($new["fio"])) {
  42. run_sql($db_link, "UPDATE User_auth SET `comments`='" . mysqli_real_escape_string($db_link, $new["fio"]) . "' WHERE `user_id`=" . $id . " AND `deleted`=0 AND (`comments` IS NULL or `comments`='' or `comments`='" . $user_info["fio"] . "')");
  43. }
  44. run_sql($db_link, "UPDATE User_auth SET ou_id=" . $new["ou_id"] . " WHERE user_id=" . $id);
  45. run_sql($db_link, "UPDATE devices SET device_name='" . $new["login"] . "' WHERE user_id=" . $id);
  46. header("Location: " . $_SERVER["REQUEST_URI"]);
  47. exit;
  48. }
  49. if (isset($_POST["addMacRule"])) {
  50. unset($new);
  51. $first_auth = get_records_sql($db_link, "SELECT mac FROM User_auth WHERE user_id=" . $id . " AND deleted=0 AND LENGTH(mac)>0 ORDER BY id");
  52. foreach ($first_auth as $row) {
  53. if (!empty($row['mac'])) { add_auth_rule($db_link, $row['mac'], 2, $id); }
  54. }
  55. header("Location: " . $_SERVER["REQUEST_URI"]);
  56. exit;
  57. }
  58. if (isset($_POST["delMacRule"])) {
  59. run_sql($db_link, "DELETE FROM auth_rules WHERE user_id=" . $id . " AND type=2");
  60. LOG_INFO($db_link, "All autorules removed for id: $id login: " . $user_info["login"] . " by mac");
  61. header("Location: " . $_SERVER["REQUEST_URI"]);
  62. exit;
  63. }
  64. if (isset($_POST["addIPRule"])) {
  65. unset($new);
  66. $first_auth = get_records_sql($db_link, "SELECT ip FROM User_auth WHERE user_id=" . $id . " AND deleted=0 AND LENGTH(ip)>0 ORDER BY id");
  67. foreach ($first_auth as $row) {
  68. if (!empty($row['ip'])) { add_auth_rule($db_link, $row['ip'], 1, $id); }
  69. }
  70. header("Location: " . $_SERVER["REQUEST_URI"]);
  71. exit;
  72. }
  73. if (isset($_POST["delIPRule"])) {
  74. run_sql($db_link, "DELETE FROM auth_rules WHERE user_id=" . $id . " AND type=1");
  75. LOG_INFO($db_link, "Removed all auto rules for id: $id login: " . $user_info["login"] . " by ip");
  76. header("Location: " . $_SERVER["REQUEST_URI"]);
  77. exit;
  78. }
  79. if (isset($_POST["showDevice"])) {
  80. $device = get_record_sql($db_link, "SELECT * FROM devices WHERE user_id=" . $id);
  81. $auth = get_record_sql($db_link, "SELECT * FROM User_auth WHERE user_id=" . $id);
  82. if (empty($device) and !empty($auth)) {
  83. $new['user_id'] = $id;
  84. $new['device_name'] = $user_info['login'];
  85. $new['device_type'] = 5;
  86. $new['ip'] = $auth['ip'];
  87. $new['ip_int'] = $auth['ip_int'];
  88. $new['community'] = get_const('snmp_default_community');
  89. $new['snmp_version'] = get_const('snmp_default_version');
  90. $new['login'] = get_option($db_link, 28);
  91. $new['password'] = get_option($db_link, 29);
  92. //default ssh
  93. $new['protocol'] = 0;
  94. $new['control_port'] = get_option($db_link, 30);
  95. $new_id = insert_record($db_link, "devices", $new);
  96. unset($_POST);
  97. if (!empty($new_id)) {
  98. LOG_INFO($db_link, "Created device with id: $new_id for auth_id: $id");
  99. header("Location: /admin/devices/editdevice.php?id={$new_id}");
  100. exit;
  101. } else {
  102. header("Location: " . $_SERVER["REQUEST_URI"]);
  103. exit;
  104. }
  105. }
  106. header("Location: /admin/devices/editdevice.php?id=" . $device['id']);
  107. exit;
  108. }
  109. if (isset($_POST["addauth"])) {
  110. $fip = substr(trim($_POST["newip"]), 0, 18);
  111. $fmac = NULL;
  112. if (isset($_POST["newmac"])) {
  113. $fmac = mac_dotted(substr(trim($_POST["newmac"]), 0, 17));
  114. }
  115. if ($fip) {
  116. if (checkValidIp($fip)) {
  117. $ip_aton = ip2long($fip);
  118. $f_dhcp = 1;
  119. //search mac
  120. if (!empty($fmac) and !empty($fip)) {
  121. $mac_exists = find_mac_in_subnet($db_link, $fip, $fmac);
  122. if (!empty($mac_exists) and $mac_exists['count'] >= 1 and !in_array($id, $mac_exists['users_id'])) {
  123. $dup_sql = "SELECT * FROM User_list WHERE id=" . $mac_exists['users_id']['0'];
  124. $dup_info = get_record_sql($db_link, $dup_sql);
  125. $msg_error = "Mac already exists at another user in this subnet! Skip creating $fip [$fmac].<br>Old user id: " . $dup_info['id'] . " login: " . $dup_info['login'];
  126. $_SESSION[$page_url]['msg'] = $msg_error;
  127. LOG_ERROR($db_link, $msg_error);
  128. header("Location: " . $_SERVER["REQUEST_URI"]);
  129. exit;
  130. }
  131. //disable dhcp for secondary ip
  132. if (empty($mac_exists)) {
  133. $f_dhcp = 1;
  134. } else {
  135. if (in_array($id, $mac_exists['users_id'])) {
  136. $f_dhcp = 0;
  137. }
  138. }
  139. }
  140. //search ip
  141. $dup_ip_record = get_record_sql($db_link, "SELECT * FROM User_auth WHERE `ip_int`=$ip_aton AND user_id<>" . $id . " AND deleted=0");
  142. if (!empty($dup_ip_record)) {
  143. $dup_info = get_record_sql($db_link, "SELECT * FROM User_list WHERE id=" . $dup_ip_record['user_id']);
  144. $msg_error = "$fip already exists. Skip creating $fip [$fmac].<br>Old user id: " . $dup_info['id'] . " login: " . $dup_info['login'];
  145. $_SESSION[$page_url]['msg'] = $msg_error;
  146. LOG_ERROR($db_link, $msg_error);
  147. header("Location: " . $_SERVER["REQUEST_URI"]);
  148. exit;
  149. }
  150. $fid = new_auth($db_link, $fip, $fmac, $id);
  151. if (!empty($fid)) {
  152. $new['dhcp'] = $f_dhcp;
  153. $new['created_by'] = 'manual';
  154. update_record($db_link, "User_auth", "id=" . $fid, $new);
  155. LOG_WARNING($db_link, "Add ip for login: " . $user_info["login"] . ": ip => $fip, mac => $fmac", $fid);
  156. header("Location: /admin/users/editauth.php?id=" . $fid);
  157. exit;
  158. }
  159. header("Location: " . $_SERVER["REQUEST_URI"]);
  160. exit;
  161. } else {
  162. $msg_error = "$msg_ip_error xxx.xxx.xxx.xxx";
  163. $_SESSION[$page_url]['msg'] = $msg_error;
  164. }
  165. }
  166. header("Location: " . $_SERVER["REQUEST_URI"]);
  167. exit;
  168. }
  169. if (isset($_POST["removeauth"])) {
  170. $auth_id = $_POST["f_auth_id"];
  171. foreach ($auth_id as $key => $val) {
  172. if ($val) { delete_user_auth($db_link, $val); }
  173. }
  174. header("Location: " . $_SERVER["REQUEST_URI"]);
  175. exit;
  176. }
  177. if (isset($_POST["new_user"])) {
  178. $auth_id = $_POST["f_auth_id"];
  179. $save_traf = get_option($db_link, 23) * 1;
  180. foreach ($auth_id as $key => $val) {
  181. if ($val) {
  182. $auth_info = get_record_sql($db_link, "SELECT * FROM User_auth WHERE id=$val");
  183. $ou_id = $user_info["ou_id"];
  184. $login = NULL;
  185. if (!empty($auth_info["dns_name"])) {
  186. $login = $auth_info["dns_name"];
  187. }
  188. if (empty($login) and !empty($auth_info["comments"])) {
  189. $login = transliterate($auth_info["comments"]);
  190. }
  191. if (empty($login) and !empty($auth_info["dhcp_hostname"])) {
  192. $login = $auth_info["dhcp_hostname"];
  193. }
  194. if (empty($login) and !empty($auth_info["mac"])) {
  195. $login = $auth_info["mac"];
  196. }
  197. if (empty($login)) {
  198. $login = $auth_info["ip"];
  199. }
  200. $new_user = get_record_sql($db_link, "SELECT * FROM User_list WHERE LCase(login)=LCase('$login') and deleted=0");
  201. if (!empty($new_user)) {
  202. // move auth
  203. $auth["user_id"] = $new_user["id"];
  204. $auth["ou_id"] = $new_user["ou_id"];
  205. $auth["save_traf"] = $save_traf;
  206. $auth = apply_auth_rule($db_link, $auth, $l_id);
  207. update_record($db_link, "User_auth", "id='" . $val . "'", $auth);
  208. LOG_WARNING($db_link, "ip from id: $val moved to another user user_id: " . $new_user["id"], $val);
  209. } else {
  210. $new["login"] = $login;
  211. $new["ou_id"] = $ou_id;
  212. if (!empty($auth_info["comments"])) { $new["fio"] = $auth_info["comments"]; }
  213. if (!isset($new["fio"]) and !empty($auth_info["dns_name"])) { $new["fio"] = $auth_info["dns_name"]; }
  214. if (!isset($new["fio"]) and !empty($auth_info["dhcp_hostname"])) { $new["fio"] = $auth_info["dhcp_hostname"]; }
  215. $new["enabled"] = $auth_info["enabled"];
  216. $l_id = insert_record($db_link, "User_list", $new);
  217. $auth["user_id"] = $l_id;
  218. $auth["save_traf"] = $save_traf;
  219. update_record($db_link, "User_auth", "id='" . $val . "'", $auth);
  220. LOG_WARNING($db_link, "Create user from ip: login => $login. ip-record auth_id: $val moved to this user.", $val);
  221. }
  222. }
  223. }
  224. header("Location: " . $_SERVER["REQUEST_URI"]);
  225. exit;
  226. }
  227. unset($_POST);
  228. require_once($_SERVER["DOCUMENT_ROOT"] . "/inc/header.php");
  229. ?>
  230. <div id="cont">
  231. <?php
  232. if (!empty($_SESSION[$page_url]['msg'])) {
  233. print '<div id="msg">' . $_SESSION[$page_url]['msg'] . '</div>';
  234. unset($_SESSION[$page_url]['msg']);
  235. }
  236. ?>
  237. <form name="def" action="edituser.php?id=<?php echo $id; ?>" method="post">
  238. <input type="hidden" name="id" value=<?php echo $id; ?>>
  239. <table class="data">
  240. <tr>
  241. <td colspan=2><?php print WEB_cell_login; ?></td>
  242. <td colspan=2><?php print WEB_cell_fio; ?></td>
  243. <td colspan=2><?php print WEB_cell_ou; ?></td>
  244. <td ><?php print WEB_user_permanent; ?></td>
  245. </tr>
  246. <tr>
  247. <td colspan=2><input type="text" name="f_login" value="<?php print $user_info["login"]; ?>" size=25></td>
  248. <td colspan=2><input type="text" name="f_fio" value="<?php print $user_info["fio"]; ?>" size=25></td>
  249. <td colspan=2><?php print_ou_set($db_link, 'f_ou', $user_info["ou_id"]); ?></td>
  250. <td><?php print_qa_select('f_permanent', $user_info["permanent"]); ?></td>
  251. </tr>
  252. <tr>
  253. <td colspan=2><?php print WEB_cell_perday; ?></td>
  254. <td colspan=2><?php print WEB_cell_permonth; ?></td>
  255. <td><?php print WEB_cell_blocked; ?></td>
  256. <td></td>
  257. <td><?php print WEB_cell_enabled; ?></td>
  258. </tr>
  259. <tr>
  260. <td colspan=2><input type="text" name="f_perday" value="<?php echo $user_info["day_quota"]; ?>" size=5></td>
  261. <td colspan=2><input type="text" name="f_permonth" value="<?php echo $user_info["month_quota"]; ?>" size=5></td>
  262. <td><?php print_qa_select('f_blocked', $user_info["blocked"]); ?></td>
  263. <td></td>
  264. <td><?php print_qa_select('f_enabled', $user_info["enabled"]); ?></td>
  265. </tr>
  266. <tr>
  267. <td class=data colspan=7><?php echo WEB_user_rules_for_autoassign; ?>:</td>
  268. </tr>
  269. <tr>
  270. <td colspan=2><?php print WEB_cell_filter; ?></td>
  271. <td colspan=2><?php print WEB_cell_shaper; ?></td>
  272. <td colspan=3></td>
  273. </tr>
  274. <tr>
  275. <td colspan=2><?php print_group_select($db_link, 'f_filter', $user_info["filter_group_id"]); ?></td>
  276. <td colspan=2><?php print_queue_select($db_link, 'f_queue', $user_info["queue_id"]); ?></td>
  277. <td colspan=3></td>
  278. </tr>
  279. <tr>
  280. <?php
  281. print "<td>";
  282. print_url(WEB_user_rule_list, "/admin/users/edit_rules.php?id=$id");
  283. print "</td>";
  284. $rule_count = get_count_records($db_link, "auth_rules", "user_id=" . $id);
  285. print "<td > Count: " . $rule_count . "</td>";
  286. $first_auth = get_record_sql($db_link, "SELECT id FROM User_auth WHERE user_id=" . $id . " AND deleted=0 ORDER BY id");
  287. if (!empty($first_auth)) {
  288. //mac
  289. $mac_rule_count = get_count_records($db_link, "auth_rules", "user_id=" . $id . " AND type=2");
  290. if (!empty($mac_rule_count)) {
  291. print "<td><input type=\"submit\" name=\"delMacRule\" value=" . WEB_btn_mac_del . " ></td>";
  292. } else {
  293. print "<td><input type=\"submit\" name=\"addMacRule\" value=" . WEB_btn_mac_add . " ></td>";
  294. }
  295. //ip
  296. $ip_rule_count = get_count_records($db_link, "auth_rules", "user_id=" . $id . " AND type=1");
  297. if (!empty($ip_rule_count)) {
  298. print "<td><input type=\"submit\" name=\"delIPRule\" value=" . WEB_btn_ip_del . " ></td>";
  299. } else {
  300. print "<td><input type=\"submit\" name=\"addIPRule\" value=" . WEB_btn_ip_add . " ></td>";
  301. }
  302. } else {
  303. print "<td colspan=2></td>";
  304. }
  305. ?>
  306. <td colspan=3 align=right><?php print WEB_cell_created . ":&nbsp";
  307. print $user_info["timestamp"]; ?></td>
  308. </tr>
  309. <tr>
  310. <?php print "<td colspan=2>";
  311. print_url(WEB_report_by_day, "/admin/reports/userday.php?id=$id"); ?></td>
  312. <td></td>
  313. <td><input type="submit" name="showDevice" value=<?php print WEB_btn_device; ?>></td>
  314. <td colspan=2></td>
  315. <td align=right><input type="submit" name="edituser" value=<?php print WEB_btn_save; ?>></td>
  316. </tr>
  317. </table>
  318. <?php
  319. if ($msg_error) {
  320. print "<div id='msg'><b>$msg_error</b></div><br>\n";
  321. }
  322. $sort_table = 'User_auth';
  323. $sort_url = "<a href=edituser.php?id=" . $id;
  324. ?>
  325. <br><b><?php echo WEB_user_ip_list; ?></b><br>
  326. <table class="data">
  327. <tr>
  328. <td class="data"><?php echo WEB_user_add_ip; ?>:&nbsp<input type=text name=newip value=""></td>
  329. <td class="data"><?php echo WEB_user_add_mac; ?>:&nbsp<input type=text name=newmac value=""></td>
  330. <td class="data"><input type="submit" name="addauth" value="<?php echo WEB_btn_add; ?>"></td>
  331. <td class="data" align=right><input type="submit" name="new_user" value="<?php echo WEB_btn_transfom; ?>"></td>
  332. </tr>
  333. </table>
  334. <table class="data" width=120%>
  335. <tr align=center>
  336. <td class="data"><input type="checkbox" onClick="checkAll(this.checked);"></td>
  337. <td class="data"><?php print $sort_url . "&sort=ip_int&order=$new_order>" . WEB_cell_ip . "</a>"; ?></td>
  338. <td class="data"><?php print $sort_url . "&sort=mac&order=$new_order>" . WEB_cell_mac . "</a>"; ?></td>
  339. <td class="data"><?php print WEB_cell_comment; ?></td>
  340. <td class="data"><?php print $sort_url . "&sort=dns_name&order=$new_order>" . WEB_cell_dns_name . "</a>"; ?></td>
  341. <td class="data"><?php print WEB_cell_enabled; ?></td>
  342. <td class="data"><?php print WEB_cell_dhcp; ?></td>
  343. <td class="data"><?php print WEB_cell_filter; ?></td>
  344. <td class="data"><?php print WEB_cell_shaper; ?></td>
  345. <td class="data"><?php print WEB_cell_perday . "/<br>" . WEB_cell_permonth . ", Mb"; ?></td>
  346. <td class="data"><?php print WEB_cell_temporary; ?></td>
  347. <td class="data"><?php print "<input type=\"submit\" onclick=\"return confirm('" . WEB_msg_apply_selected . "?')\" name=\"removeauth\" value=" . WEB_btn_remove . ">"; ?></td>
  348. </tr>
  349. <?php
  350. $flist = get_records($db_link, 'User_auth', "user_id=" . $id . " and deleted=0 ORDER BY $sort_table.$sort_field $order");
  351. if (!empty($flist)) {
  352. foreach ($flist as $row) {
  353. $dhcp_str = '';
  354. if ($row["dhcp_time"] !== '0000-00-00 00:00:00') {
  355. if (!empty($row["dhcp_action"])) { $dhcp_str = FormatDateStr('Y.m.d H:m', $row["dhcp_time"]) . " (" . $row["dhcp_action"] . ")"; }
  356. }
  357. if ($row["last_found"] == '0000-00-00 00:00:00') {
  358. $row["last_found"] = '';
  359. }
  360. print "<tr align=center>";
  361. print "<td class=\"data\" style='padding:0'><input type=checkbox name=f_auth_id[] value=" . $row["id"] . " ></td>";
  362. print "<td class=\"data\" align=left><a href=editauth.php?id=" . $row["id"] . ">" . $row["ip"] . "</a>";
  363. if (!empty($row["arp_found"])) { print "<p class='timestamp'>".FormatDateStr('Y.m.d H:i', $row["arp_found"])."</p>"; }
  364. print "</td>";
  365. print "<td class=\"data\" >" . expand_mac($db_link, $row["mac"]);
  366. if (!empty($row["last_found"])) { print "<p class='timestamp'>".FormatDateStr('Y.m.d H:i', $row["last_found"])."</p>"; }
  367. print "</td>";
  368. if (isset($row["dhcp_hostname"]) and strlen($row["dhcp_hostname"]) > 0) {
  369. print "<td class=\"data\" >" . $row["comments"] . " [" . $row["dhcp_hostname"] . "]</td>";
  370. } else {
  371. print "<td class=\"data\" >" . $row["comments"] . "</td>";
  372. }
  373. print "<td class=\"data\" >" . $row["dns_name"] . "</td>";
  374. $ip_status = 1;
  375. if ($row["blocked"] or !$row["enabled"]) {
  376. $ip_status = 0;
  377. }
  378. print_td_qa($ip_status);
  379. $cl = "data_green";
  380. if (!$row["dhcp"]) { $cl = "data_red"; }
  381. print "<td class=\"$cl\" >" . get_qa($row["dhcp"]);
  382. if (!empty($row["dhcp_acl"])) { print "<p class='timestamp'>".$row["dhcp_acl"]. "</p>"; }
  383. if (!empty($dhcp_str)) { print "<p class='timestamp'>".$dhcp_str. "</p>"; }
  384. print "</td>";
  385. print "<td class=\"data\" >" . get_group($db_link, $row["filter_group_id"]) . "</td>";
  386. print "<td class=\"data\" >" . get_queue($db_link, $row["queue_id"]) . "</td>";
  387. print "<td class=\"data\" >" . $row["day_quota"] . "/" . $row["month_quota"] . "</td>";
  388. if ($row['dynamic']) { $cl = "data_red"; } else { $cl = "data_green"; }
  389. print "<td class=\"$cl\" >". get_qa($row['dynamic']);
  390. if ($row['dynamic'] and !empty($row["eof"])) { print "<p class='timestamp'>".FormatDateStr('Y.m.d H:i', $row["eof"])."</p>"; } else { print "&nbsp"; }
  391. print "</td>";
  392. print "<td class=\"data\" >";
  393. if (!empty($row["created_by"])) { print $row["created_by"]; } else { print "&nbsp"; }
  394. print "</td>";
  395. print "</tr>";
  396. }
  397. }
  398. ?>
  399. </table>
  400. </form>
  401. <?php
  402. require_once($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php");
  403. ?>