edituser.php 20 KB

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