1
0

edituser.php 20 KB

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