edituser.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 (isset($_POST["edituser"])) {
  11. unset($new);
  12. $new["ou_id"] = $_POST["f_ou"] * 1;
  13. $new["filter_group_id"] = $_POST["f_filter"]*1;
  14. $new["queue_id"] = $_POST["f_queue"]*1;
  15. $new["login"] = trim($_POST["f_login"]);
  16. $new["fio"] = trim($_POST["f_fio"]);
  17. if (get_const('default_user_ou_id') == $new["ou_id"] or get_const('default_hotspot_ou_id') == $new["ou_id"]) {
  18. $new["enabled"] = 0;
  19. $new["blocked"] = 0;
  20. $new["day_quota"] = 0;
  21. $new["month_quota"] = 0;
  22. } else {
  23. $new["enabled"] = get_int($_POST["f_enabled"]);
  24. $new["blocked"] = get_int($_POST["f_blocked"]);
  25. $new["day_quota"] = get_int(trim($_POST["f_perday"]));
  26. $new["month_quota"] = get_int(trim($_POST["f_permonth"]));
  27. }
  28. $changes = get_diff_rec($db_link,"User_list","id='$id'", $new, 0);
  29. if (!empty($changes)) { LOG_WARNING($db_link,"Changed user id: $id login: ".$new["login"].". \r\nApply: $changes"); }
  30. update_record($db_link, "User_list", "id='$id'", $new);
  31. if (!$new["enabled"]) {
  32. run_sql($db_link, "UPDATE User_auth SET enabled=0, changed=1 WHERE user_id=".$id);
  33. }
  34. run_sql($db_link, "UPDATE User_auth SET ou_id=".$new["ou_id"]." WHERE user_id=".$id);
  35. run_sql($db_link, "UPDATE devices SET device_name='".$new["login"]."' WHERE user_id=".$id);
  36. header("Location: " . $_SERVER["REQUEST_URI"]);
  37. exit;
  38. }
  39. if (isset($_POST["addMacRule"])) {
  40. unset($new);
  41. $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");
  42. if (!empty($first_auth) and !empty($first_auth['mac'])) {
  43. $auth_rules_user = get_record_sql($db_link,"SELECT * FROM auth_rules WHERE user_id=".$id." AND type=2");
  44. $auth_rules_mac = get_record_sql($db_link,"SELECT * FROM auth_rules WHERE rule='".$first_auth['mac']."' AND type=2");
  45. if (empty($auth_rules_user) and empty($auth_rules_mac)) {
  46. $new['user_id']=$id;
  47. $new['type']=2;
  48. $new['rule']=$first_auth['mac'];
  49. insert_record($db_link,"auth_rules",$new);
  50. LOG_INFO($db_link,"Create auto rule at id: ".$id." login: ".$user_info["login"]." for mac ".$first_auth['mac']);
  51. }
  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. $auth_rules_user = get_record_sql($db_link,"SELECT * FROM auth_rules WHERE user_id=".$id." AND type=1");
  67. $auth_rules_ip = get_record_sql($db_link,"SELECT * FROM auth_rules WHERE rule='".$first_auth['ip']."' AND type=1");
  68. if (empty($auth_rules_user) and empty($auth_rules_ip)) {
  69. $new['user_id']=$id;
  70. $new['type']=1;
  71. $new['rule']=$first_auth['ip'];
  72. insert_record($db_link,"auth_rules",$new);
  73. LOG_INFO($db_link,"Create auto rule id: ".$id." login: ".$user_info["login"]." for ip ".$first_auth['ip']);
  74. }
  75. }
  76. header("Location: " . $_SERVER["REQUEST_URI"]);
  77. exit;
  78. }
  79. if (isset($_POST["delIPRule"])) {
  80. run_sql($db_link,"DELETE FROM auth_rules WHERE user_id=".$id." AND type=1");
  81. LOG_INFO($db_link,"Removed all auto rules for id: $id login: ".$user_info["login"]." by ip");
  82. header("Location: " . $_SERVER["REQUEST_URI"]);
  83. exit;
  84. }
  85. if (isset($_POST["showDevice"])) {
  86. $device = get_record_sql($db_link,"SELECT * FROM devices WHERE user_id=".$id);
  87. $auth = get_record_sql($db_link,"SELECT * FROM User_auth WHERE user_id=".$id);
  88. if (empty($device) and !empty($auth)) {
  89. $new['user_id']=$id;
  90. $new['device_name'] = $user_info['login'];
  91. $new['device_type'] = 5;
  92. $new['ip']=$auth['ip'];
  93. $new['community'] = get_const('snmp_default_community');
  94. $new['snmp_version'] = get_const('snmp_default_version');
  95. $new['login'] = get_option($db_link,28);
  96. $new['password'] = get_option($db_link,29);
  97. //default ssh
  98. $new['protocol'] = 0;
  99. $new['control_port'] = get_option($db_link,30);
  100. $new_id=insert_record($db_link, "devices", $new);
  101. unset($_POST);
  102. if (!empty($new_id)) {
  103. LOG_INFO($db_link, "Created device with id: $new_id for auth_id: $id");
  104. header("Location: /admin/devices/editdevice.php?id={$new_id}");
  105. exit;
  106. } else {
  107. header("Location: ".$_SERVER["REQUEST_URI"]);
  108. exit;
  109. }
  110. }
  111. header("Location: /admin/devices/editdevice.php?id=".$device['id']);
  112. exit;
  113. }
  114. if (isset($_POST["addauth"])) {
  115. $fip = substr(trim($_POST["newip"]), 0, 18);
  116. $fmac = NULL;
  117. if (isset($_POST["newmac"])) { $fmac = mac_dotted(substr(trim($_POST["newmac"]), 0, 17)); }
  118. if ($fip) {
  119. if (checkValidIp($fip)) {
  120. $ip_aton = ip2long($fip);
  121. $f_dhcp = 1;
  122. //search mac
  123. if (!empty($fmac)) {
  124. $mac_exists=find_mac_in_subnet($db_link,$fip,$fmac);
  125. if (isset($mac_exists) and $mac_exists['count']>=1 and !in_array($id,$mac_exists['users_id'])) {
  126. $dup_sql = "SELECT * FROM User_list WHERE id=".$mac_exists['users_id']['0'];
  127. $dup_info = get_record_sql($db_link, $dup_sql);
  128. $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'];
  129. $_SESSION[$page_url]['msg'] = $msg_error;
  130. LOG_ERROR($db_link, $msg_error);
  131. header("Location: " . $_SERVER["REQUEST_URI"]);
  132. exit;
  133. }
  134. //disable dhcp for secondary ip
  135. if (in_array($id,$mac_exists['users_id'])) { $f_dhcp = 0; }
  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["dhcp_action"]='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)) { LOG_WARNING($db_link,"Remove user ".$user_info["login"]." ip: \r\n ".$changes); }
  174. }
  175. }
  176. header("Location: " . $_SERVER["REQUEST_URI"]);
  177. exit;
  178. }
  179. if (isset($_POST["new_user"])) {
  180. $auth_id = $_POST["f_auth_id"];
  181. $save_traf = get_option($db_link, 23) * 1;
  182. foreach ($auth_id as $key => $val) {
  183. if ($val) {
  184. $auth_info = get_record_sql($db_link,"SELECT ip, mac, comments, dns_name, dhcp_hostname FROM User_auth WHERE id=$val");
  185. $ou_id = $user_info["ou_id"];
  186. $login = NULL;
  187. if (!empty($auth_info["dns_name"])) { $login = $auth_info["dns_name"]; }
  188. if (empty($login) and !empty($auth_info["comments"])) { $login = transliterate($auth_info["comments"]); }
  189. if (empty($login) and !empty($auth_info["dhcp_hostname"])) { $login = $auth_info["dhcp_hostname"]; }
  190. if (empty($login) and !empty($auth_info["mac"])) { $login = $auth_info["mac"]; }
  191. if (empty($login)) { $login = $auth_info["ip"]; }
  192. $new_user = get_record_sql($db_link,"SELECT * FROM User_list WHERE LCase(login)=LCase('$login') and deleted=0");
  193. if (!empty($new_user)) {
  194. // move auth
  195. $auth["user_id"] = $new_user["id"];
  196. $auth["ou_id"] = $new_user["ou_id"];
  197. $auth["save_traf"] = $save_traf;
  198. $auth=apply_auth_rule($db_link,$auth,$l_id);
  199. update_record($db_link, "User_auth", "id='" . $val . "'", $auth);
  200. LOG_WARNING($db_link,"ip from id: $val moved to another user user_id: ".$new_user["id"], $val);
  201. } else {
  202. $new["login"] = $login;
  203. $new["ou_id"] = $ou_id;
  204. $l_id=insert_record($db_link, "User_list", $new);
  205. $auth["user_id"] = $l_id;
  206. $auth["save_traf"] = $save_traf;
  207. update_record($db_link, "User_auth", "id='" . $val . "'", $auth);
  208. LOG_WARNING($db_link,"Create user from ip: login => $login. ip-record auth_id: $val moved to this user.", $val);
  209. }
  210. }
  211. }
  212. header("Location: " . $_SERVER["REQUEST_URI"]);
  213. exit;
  214. }
  215. unset($_POST);
  216. require_once ($_SERVER["DOCUMENT_ROOT"]."/inc/header.php");
  217. ?>
  218. <div id="cont">
  219. <?php
  220. if (!empty($_SESSION[$page_url]['msg'])) {
  221. print '<div id="msg">'.$_SESSION[$page_url]['msg'].'</div>';
  222. unset($_SESSION[$page_url]['msg']);
  223. }
  224. ?>
  225. <form name="def" action="edituser.php?id=<?php echo $id; ?>" method="post">
  226. <input type="hidden" name="id" value=<?php echo $id; ?>>
  227. <table class="data">
  228. <tr>
  229. <td colspan=2><?php print WEB_cell_login; ?></td>
  230. <td colspan=2><?php print WEB_cell_fio; ?></td>
  231. <td colspan=2><?php print WEB_cell_ou; ?></td>
  232. </tr>
  233. <tr>
  234. <td colspan=2><input type="text" name="f_login" value="<?php print $user_info["login"]; ?>" size=25></td>
  235. <td colspan=2><input type="text" name="f_fio" value="<?php print $user_info["fio"]; ?>" size=25></td>
  236. <td colspan=2><?php print_ou_set($db_link, 'f_ou', $user_info["ou_id"]); ?></td>
  237. </tr>
  238. <tr>
  239. <td colspan=2><?php print WEB_cell_perday; ?></td>
  240. <td colspan=2><?php print WEB_cell_permonth; ?></td>
  241. <td><?php print WEB_cell_blocked; ?></td>
  242. <td><?php print WEB_cell_enabled; ?></td>
  243. </tr>
  244. <tr>
  245. <td colspan=2><input type="text" name="f_perday" value="<?php echo $user_info["day_quota"]; ?>" size=5></td>
  246. <td colspan=2><input type="text" name="f_permonth" value="<?php echo $user_info["month_quota"]; ?>" size=5></td>
  247. <td ><?php print_qa_select('f_blocked', $user_info["blocked"]); ?></td>
  248. <td ><?php print_qa_select('f_enabled', $user_info["enabled"]); ?></td>
  249. </tr>
  250. <tr><td class=data colspan=6><?php echo WEB_user_rules_for_autoassign; ?>:</td></tr>
  251. <tr>
  252. <td colspan=2><?php print WEB_cell_filter; ?></td>
  253. <td colspan=2><?php print WEB_cell_shaper; ?></td>
  254. <td colspan=2></td>
  255. </tr>
  256. <tr>
  257. <td colspan=2><?php print_group_select($db_link, 'f_filter', $user_info["filter_group_id"]); ?></td>
  258. <td colspan=2><?php print_queue_select($db_link, 'f_queue', $user_info["queue_id"]); ?></td>
  259. <td colspan=2></td>
  260. </tr>
  261. <tr>
  262. <?php
  263. print "<td>"; print_url(WEB_user_rule_list,"/admin/users/edit_rules.php?id=$id"); print "</td>";
  264. $rule_count = get_count_records($db_link,"auth_rules","user_id=".$id);
  265. print "<td > Count: ".$rule_count."</td>";
  266. $first_auth = get_record_sql($db_link,"SELECT id FROM User_auth WHERE user_id=".$id." AND deleted=0 ORDER BY id");
  267. if (!empty($first_auth)) {
  268. //mac
  269. $mac_rule_count = get_count_records($db_link,"auth_rules","user_id=".$id." AND type=2");
  270. if (!empty($mac_rule_count)) {
  271. print "<td><input type=\"submit\" name=\"delMacRule\" value=".WEB_btn_mac_del." ></td>";
  272. } else {
  273. print "<td><input type=\"submit\" name=\"addMacRule\" value=".WEB_btn_mac_add." ></td>";
  274. }
  275. //ip
  276. $ip_rule_count = get_count_records($db_link,"auth_rules","user_id=".$id." AND type=1");
  277. if (!empty($ip_rule_count)) {
  278. print "<td><input type=\"submit\" name=\"delIPRule\" value=".WEB_btn_ip_del." ></td>";
  279. } else {
  280. print "<td><input type=\"submit\" name=\"addIPRule\" value=".WEB_btn_ip_add." ></td>";
  281. }
  282. } else { print "<td colspan=2></td>"; }
  283. ?>
  284. <td colspan=2 align=right><?php print WEB_cell_created.":";print $user_info["timestamp"]; ?></td>
  285. </tr>
  286. <tr>
  287. <?php print "<td colspan=2>"; print_url(WEB_report_by_day,"/admin/reports/userday.php?id=$id"); ?></td>
  288. <td></td>
  289. <td><input type="submit" name="showDevice" value=<?php print WEB_btn_device; ?>></td>
  290. <td></td>
  291. <td align=right><input type="submit" name="edituser" value=<?php print WEB_btn_save; ?>></td>
  292. </tr>
  293. </table>
  294. <?php
  295. if ($msg_error) { print "<div id='msg'><b>$msg_error</b></div><br>\n"; }
  296. $sort_table = 'User_auth';
  297. $sort_url = "<a href=edituser.php?id=" . $id;
  298. ?>
  299. <br><b><?php echo WEB_user_ip_list; ?></b><br>
  300. <table class="data">
  301. <tr>
  302. <td class="data"><?php echo WEB_user_add_ip; ?>:&nbsp<input type=text name=newip value=""></td>
  303. <td class="data"><?php echo WEB_user_add_mac; ?>:&nbsp<input type=text name=newmac value=""></td>
  304. <td class="data"><input type="submit" name="addauth" value="<?php echo WEB_btn_add; ?>"></td>
  305. <td class="data" align=right><input type="submit" name="new_user" value="<?php echo WEB_btn_transfom; ?>"></td>
  306. </tr>
  307. </table>
  308. <table class="data">
  309. <tr>
  310. <td class="data"><input type="checkbox" onClick="checkAll(this.checked);"></td>
  311. <td class="data"><?php print $sort_url . "&sort=ip_int&order=$new_order>" . WEB_cell_ip . "</a>"; ?></td>
  312. <td class="data"><?php print $sort_url . "&sort=mac&order=$new_order>" . WEB_cell_mac . "</a>"; ?></td>
  313. <td class="data"><?php print WEB_cell_comment; ?></td>
  314. <td class="data"><?php print $sort_url . "&sort=dns_name&order=$new_order>" . WEB_cell_dns_name . "</a>"; ?></td>
  315. <td class="data"><?php print WEB_cell_enabled; ?></td>
  316. <td class="data"><?php print WEB_cell_dhcp; ?></td>
  317. <td class="data"><?php print WEB_cell_filter; ?></td>
  318. <td class="data"><?php print WEB_cell_shaper; ?></td>
  319. <td class="data"><?php print WEB_cell_perday."/<br>".WEB_cell_permonth.", Mb"; ?></td>
  320. <td class="data"><?php print WEB_cell_connection; ?></td>
  321. <td class="data"><?php print $sort_url . "&sort=timestamp&order=$new_order>".WEB_cell_created."</a>"; ?></td>
  322. <td class="data">Last DHCP/ARP Event</td>
  323. <td class="data"><?php print $sort_url . "&sort=last_found&order=$new_order>".WEB_cell_last_found."</a>"; ?></td>
  324. <td class="data"><?php print "<input type=\"submit\" onclick=\"return confirm('".WEB_msg_apply_selected."?')\" name=\"removeauth\" value=".WEB_btn_remove.">"; ?></td>
  325. </tr>
  326. <?php
  327. $flist=get_records($db_link,'User_auth',"user_id=".$id." and deleted=0 ORDER BY $sort_table.$sort_field $order");
  328. if (!empty($flist)) {
  329. foreach ( $flist as $row ) {
  330. if ($row["dhcp_time"] == '0000-00-00 00:00:00') {
  331. $dhcp_str = '';
  332. } else {
  333. $dhcp_str = FormatDateStr('Y.m.d H:m',$row["dhcp_time"]) . " (" . $row["dhcp_action"] . ")";
  334. }
  335. if ($row["last_found"] == '0000-00-00 00:00:00') { $row["last_found"] = ''; }
  336. print "<tr align=center>\n";
  337. print "<td class=\"data\" style='padding:0'><input type=checkbox name=f_auth_id[] value=".$row["id"]." ></td>\n";
  338. print "<td class=\"data\" align=left><a href=editauth.php?id=".$row["id"].">" . $row["ip"] . "</a></td>\n";
  339. print "<td class=\"data\" >" . expand_mac($db_link,$row["mac"]) . "</td>\n";
  340. if (isset($row["dhcp_hostname"]) and strlen($row["dhcp_hostname"]) > 0) {
  341. print "<td class=\"data\" >".$row["comments"]." [" . $row["dhcp_hostname"] . "]</td>\n";
  342. } else {
  343. print "<td class=\"data\" >".$row["comments"]."</td>\n";
  344. }
  345. print "<td class=\"data\" >".$row["dns_name"]."</td>\n";
  346. $ip_status = 1;
  347. if ($row["blocked"] or !$row["enabled"]) { $ip_status = 0; }
  348. print "<td class=\"data\" >" . get_qa($ip_status). "</td>\n";
  349. print "<td class=\"data\" >" . get_qa($row["dhcp"]). "</td>\n";
  350. print "<td class=\"data\" >" . get_group($db_link, $row["filter_group_id"]) . "</td>\n";
  351. print "<td class=\"data\" >" . get_queue($db_link, $row["queue_id"]) . "</td>\n";
  352. print "<td class=\"data\" >".$row["day_quota"]."/".$row["month_quota"]."</td>\n";
  353. print "<td class=\"data\" >" . get_connection($db_link, $row["id"]) . "</td>\n";
  354. print "<td class=\"data\" >" . FormatDateStr('Y.m.d',$row["timestamp"]) . "</td>\n";
  355. print "<td class=\"data\" >" . $dhcp_str . "</td>\n";
  356. print "<td class=\"data\" >" . FormatDateStr('Y.m.d H:i',$row["last_found"]) . "</td>\n";
  357. print "<td class=\"data\" ></td></tr>";
  358. }
  359. }
  360. ?>
  361. </table>
  362. </form>
  363. <?php
  364. require_once ($_SERVER["DOCUMENT_ROOT"]."/inc/footer.php");
  365. ?>