edituser.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. $auth["enabled"] = 0;
  23. $auth["blocked"] = 0;
  24. } else {
  25. $new["enabled"] = $_POST["f_enabled"] * 1;
  26. $new["blocked"] = $_POST["f_blocked"] * 1;
  27. $new["day_quota"] = trim($_POST["f_perday"]) * 1;
  28. $new["month_quota"] = trim($_POST["f_permonth"]) * 1;
  29. }
  30. $changes = get_diff_rec($db_link,"User_list","id='$id'", $new, 0);
  31. if (!empty($changes)) { LOG_WARNING($db_link,"Изменён пользователь id: $id login: ".$new["login"].". \r\nПрименено: $changes"); }
  32. update_record($db_link, "User_list", "id='$id'", $new);
  33. run_sql($db_link, "UPDATE User_auth SET ou_id=".$new["ou_id"]." WHERE user_id=".$id);
  34. run_sql($db_link, "UPDATE devices SET device_name='".$new["login"]."' WHERE user_id=".$id);
  35. header("Location: " . $_SERVER["REQUEST_URI"]);
  36. exit;
  37. }
  38. if (isset($_POST["addMacRule"])) {
  39. unset($new);
  40. $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");
  41. if (!empty($first_auth) and !empty($first_auth['mac'])) {
  42. $new['user_id']=$id;
  43. $new['type']=2;
  44. $new['rule']=$first_auth['mac'];
  45. insert_record($db_link,"auth_rules",$new);
  46. LOG_INFO($db_link,"Создано правило атоназначения юзеру id: ".$id." login: ".$user_info["login"]." для мака ".$first_auth['mac']);
  47. }
  48. header("Location: " . $_SERVER["REQUEST_URI"]);
  49. exit;
  50. }
  51. if (isset($_POST["delMacRule"])) {
  52. run_sql($db_link,"DELETE FROM auth_rules WHERE user_id=".$id." AND type=2");
  53. LOG_INFO($db_link,"Удалены все правила атоназначения юзеру id: $id login: ".$user_info["login"]." по маку");
  54. header("Location: " . $_SERVER["REQUEST_URI"]);
  55. exit;
  56. }
  57. if (isset($_POST["addIPRule"])) {
  58. unset($new);
  59. $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");
  60. if (!empty($first_auth) and !empty($first_auth['ip'])) {
  61. $new['user_id']=$id;
  62. $new['type']=1;
  63. $new['rule']=$first_auth['ip'];
  64. insert_record($db_link,"auth_rules",$new);
  65. LOG_INFO($db_link,"Создано правило атоназначения юзеру id: ".$id." login: ".$user_info["login"]." для IP ".$first_auth['IP']);
  66. }
  67. header("Location: " . $_SERVER["REQUEST_URI"]);
  68. exit;
  69. }
  70. if (isset($_POST["delIPRule"])) {
  71. run_sql($db_link,"DELETE FROM auth_rules WHERE user_id=".$id." AND type=1");
  72. LOG_INFO($db_link,"Удалены все правила атоназначения юзеру id: $id login: ".$user_info["login"]." по ip");
  73. header("Location: " . $_SERVER["REQUEST_URI"]);
  74. exit;
  75. }
  76. if (isset($_POST["showDevice"])) {
  77. $device = get_record_sql($db_link,"SELECT * FROM devices WHERE user_id=".$id);
  78. $auth = get_record_sql($db_link,"SELECT * FROM User_auth WHERE user_id=".$id);
  79. if (empty($device) and !empty($auth)) {
  80. $new['user_id']=$id;
  81. $new['device_name'] = $user_info['login'];
  82. $new['device_type'] = 5;
  83. $new['ip']=$auth['ip'];
  84. $new['community'] = get_const('snmp_default_community');
  85. $new['snmp_version'] = get_const('snmp_default_version');
  86. $new_id=insert_record($db_link, "devices", $new);
  87. unset($_POST);
  88. if (!empty($new_id)) {
  89. LOG_INFO($db_link, "Created device with id: $new_id for auth_id: $id");
  90. header("Location: /admin/devices/editdevice.php?id={$new_id}");
  91. exit;
  92. } else {
  93. header("Location: ".$_SERVER["REQUEST_URI"]);
  94. exit;
  95. }
  96. }
  97. header("Location: /admin/devices/editdevice.php?id=".$device['id']);
  98. exit;
  99. }
  100. if (isset($_POST["addauth"])) {
  101. $fip = substr(trim($_POST["newip"]), 0, 18);
  102. if (isset($_POST["newmac"])) { $fmac = mac_dotted(substr(trim($_POST["newmac"]), 0, 17)); }
  103. if ($fip) {
  104. if (checkValidIp($fip)) {
  105. $ip_aton = ip2long($fip);
  106. //search mac
  107. $mac_exists=find_mac_in_subnet($db_link,$fip,$fmac);
  108. if (isset($mac_exists) and $mac_exists['count']>=1 and !in_array($id,$mac_exists['users_id'])) {
  109. $dup_sql = "SELECT * FROM User_list WHERE id=".$mac_exists['users_id']['0'];
  110. $dup_info = get_record_sql($db_link, $dup_sql);
  111. $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'];
  112. $_SESSION[$page_url]['msg'] = $msg_error;
  113. LOG_ERROR($db_link, $msg_error);
  114. header("Location: " . $_SERVER["REQUEST_URI"]);
  115. exit;
  116. }
  117. //disable dhcp for secondary ip
  118. $f_dhcp = 1;
  119. if (in_array($id,$mac_exists['users_id'])) { $f_dhcp = 0; }
  120. //search ip
  121. $dup_ip_record = get_record_sql($db_link, "SELECT * FROM User_auth WHERE `ip_int`=$ip_aton AND user_id<>".$id." AND deleted=0");
  122. if (!empty($dup_ip_record)) {
  123. $dup_info = get_record_sql($db_link, "SELECT * FROM User_list WHERE id=".$dup_ip_record['user_id']);
  124. $msg_error = "$fip already exists. 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. $fid = new_auth($db_link, $fip, $fmac, $id);
  131. if (!empty($fid)) {
  132. $new['dhcp']=$f_dhcp;
  133. update_record($db_link,"User_auth","id=".$fid,$new);
  134. apply_auth_rule($db_link,$fid,$id);
  135. LOG_WARNING($db_link,"Создан новый адрес доступа для login: ".$user_info["login"].": ip => $fip, mac => $fmac",$fid);
  136. header("Location: /admin/users/editauth.php?id=".$fid);
  137. exit;
  138. }
  139. header("Location: " . $_SERVER["REQUEST_URI"]);
  140. exit;
  141. } else {
  142. $msg_error = "$msg_ip_error xxx.xxx.xxx.xxx";
  143. $_SESSION[$page_url]['msg'] = $msg_error;
  144. }
  145. }
  146. header("Location: " . $_SERVER["REQUEST_URI"]);
  147. exit;
  148. }
  149. if (isset($_POST["removeauth"])) {
  150. $auth_id = $_POST["f_auth_id"];
  151. foreach ($auth_id as $key => $val) {
  152. if ($val) {
  153. run_sql($db_link, 'DELETE FROM connections WHERE auth_id='.$val);
  154. run_sql($db_link, 'DELETE FROM User_auth_alias WHERE auth_id='.$val);
  155. $auth["deleted"] = 1;
  156. $changes = get_diff_rec($db_link,"User_auth","id='$val'", '', 0);
  157. if (!empty($changes)) { LOG_WARNING($db_link,"Удалён адрес доступа для login: ".$user_info["login"].": \r\n $changes",$val); }
  158. update_record($db_link, "User_auth", "id=" . $val, $auth);
  159. }
  160. }
  161. header("Location: " . $_SERVER["REQUEST_URI"]);
  162. exit;
  163. }
  164. if (isset($_POST["new_user"])) {
  165. $auth_id = $_POST["f_auth_id"];
  166. $save_traf = get_option($db_link, 23) * 1;
  167. foreach ($auth_id as $key => $val) {
  168. if ($val) {
  169. $auth_info = get_record_sql($db_link,"SELECT ip, mac, comments, dns_name, dhcp_hostname FROM User_auth WHERE id=$val");
  170. $ou_id = $user_info["ou_id"];
  171. $login = NULL;
  172. if (!empty($auth_info["dns_name"])) { $login = $auth_info["dns_name"]; }
  173. if (empty($login) and !empty($auth_info["comments"])) { $login = transliterate($auth_info["comments"]); }
  174. if (empty($login) and !empty($auth_info["dhcp_hostname"])) { $login = $auth_info["dhcp_hostname"]; }
  175. if (empty($login) and !empty($auth_info["mac"])) { $login = $auth_info["mac"]; }
  176. if (empty($login)) { $login = $auth_info["ip"]; }
  177. $new_user = get_record_sql($db_link,"SELECT * FROM User_list WHERE LCase(login)=LCase('$login') and deleted=0");
  178. if (!empty($new_user)) {
  179. // move auth
  180. $auth["user_id"] = $new_user["id"];
  181. $auth["ou_id"] = $new_user["ou_id"];
  182. $auth["save_traf"] = $save_traf;
  183. update_record($db_link, "User_auth", "id='" . $val . "'", $auth);
  184. apply_auth_rule($db_link,$val,$l_id);
  185. LOG_WARNING($db_link,"Адрес доступа id: $val перемещён к другому юзеру user_id: ".$new_user["id"], $val);
  186. } else {
  187. $new["login"] = $login;
  188. $new["ou_id"] = $ou_id;
  189. $l_id=insert_record($db_link, "User_list", $new);
  190. $auth["user_id"] = $l_id;
  191. $auth["save_traf"] = $save_traf;
  192. update_record($db_link, "User_auth", "id='" . $val . "'", $auth);
  193. LOG_WARNING($db_link,"Создан новый пользователь из адреса доступа: login => $login. Адрес доступа auth_id: $val перемещён к созданному пользователю.", $val);
  194. }
  195. }
  196. }
  197. header("Location: " . $_SERVER["REQUEST_URI"]);
  198. exit;
  199. }
  200. unset($_POST);
  201. require_once ($_SERVER["DOCUMENT_ROOT"]."/inc/header.php");
  202. ?>
  203. <div id="cont">
  204. <?php
  205. if (!empty($_SESSION[$page_url]['msg'])) {
  206. print '<div id="msg">'.$_SESSION[$page_url]['msg'].'</div>';
  207. unset($_SESSION[$page_url]['msg']);
  208. }
  209. ?>
  210. <form name="def" action="edituser.php?id=<?php echo $id; ?>" method="post">
  211. <input type="hidden" name="id" value=<?php echo $id; ?>>
  212. <table class="data">
  213. <tr>
  214. <td colspan=2><?php print WEB_cell_login; ?></td>
  215. <td colspan=2><?php print WEB_cell_fio; ?></td>
  216. <td colspan=2><?php print WEB_cell_ou; ?></td>
  217. </tr>
  218. <tr>
  219. <td colspan=2><input type="text" name="f_login" value="<?php print $user_info["login"]; ?>" size=25></td>
  220. <td colspan=2><input type="text" name="f_fio" value="<?php print $user_info["fio"]; ?>" size=25></td>
  221. <td colspan=2><?php print_ou_set($db_link, 'f_ou', $user_info["ou_id"]); ?></td>
  222. </tr>
  223. <tr>
  224. <td colspan=2><?php print WEB_cell_perday; ?></td>
  225. <td colspan=2><?php print WEB_cell_permonth; ?></td>
  226. <td><?php print WEB_cell_blocked; ?></td>
  227. <td><?php print WEB_cell_enabled; ?></td>
  228. </tr>
  229. <tr>
  230. <td colspan=2><input type="text" name="f_perday" value="<?php echo $user_info["day_quota"]; ?>" size=5></td>
  231. <td colspan=2><input type="text" name="f_permonth" value="<?php echo $user_info["month_quota"]; ?>" size=5></td>
  232. <td ><?php print_qa_select('f_blocked', $user_info["blocked"]); ?></td>
  233. <td ><?php print_qa_select('f_enabled', $user_info["enabled"]); ?></td>
  234. </tr>
  235. <tr><td class=data colspan=6><?php echo WEB_user_rules_for_autoassign; ?>:</td></tr>
  236. <tr>
  237. <td colspan=2><?php print WEB_cell_filter; ?></td>
  238. <td colspan=2><?php print WEB_cell_shaper; ?></td>
  239. <td colspan=2></td>
  240. </tr>
  241. <tr>
  242. <td colspan=2><?php print_group_select($db_link, 'f_filter', $user_info["filter_group_id"]); ?></td>
  243. <td colspan=2><?php print_queue_select($db_link, 'f_queue', $user_info["queue_id"]); ?></td>
  244. <td colspan=2></td>
  245. </tr>
  246. <tr>
  247. <?php
  248. print "<td>"; print_url(WEB_user_rule_list,"/admin/users/edit_rules.php?id=$id"); print "</td>";
  249. $rule_count = get_count_records($db_link,"auth_rules","user_id=".$id);
  250. print "<td > Count: ".$rule_count."</td>";
  251. $first_auth = get_record_sql($db_link,"SELECT id FROM User_auth WHERE user_id=".$id." AND deleted=0 ORDER BY id");
  252. if (!empty($first_auth)) {
  253. //mac
  254. $mac_rule_count = get_count_records($db_link,"auth_rules","user_id=".$id." AND type=2");
  255. if (!empty($mac_rule_count)) {
  256. print "<td><input type=\"submit\" name=\"delMacRule\" value=".WEB_btn_mac_del." ></td>";
  257. } else {
  258. print "<td><input type=\"submit\" name=\"addMacRule\" value=".WEB_btn_mac_add." ></td>";
  259. }
  260. //ip
  261. $ip_rule_count = get_count_records($db_link,"auth_rules","user_id=".$id." AND type=1");
  262. if (!empty($ip_rule_count)) {
  263. print "<td><input type=\"submit\" name=\"delIPRule\" value=".WEB_btn_ip_del." ></td>";
  264. } else {
  265. print "<td><input type=\"submit\" name=\"addIPRule\" value=".WEB_btn_ip_add." ></td>";
  266. }
  267. } else { print "<td colspan=2></td>"; }
  268. ?>
  269. <td colspan=2 align=right><?php print WEB_cell_created.":";print $user_info["timestamp"]; ?></td>
  270. </tr>
  271. <tr>
  272. <?php print "<td colspan=2>"; print_url(WEB_report_by_day,"/admin/reports/userday.php?id=$id"); ?></td>
  273. <td></td>
  274. <td><input type="submit" name="showDevice" value=<?php print WEB_btn_device; ?>></td>
  275. <td></td>
  276. <td align=right><input type="submit" name="edituser" value=<?php print WEB_btn_save; ?>></td>
  277. </tr>
  278. </table>
  279. <?php
  280. if ($msg_error) { print "<div id='msg'><b>$msg_error</b></div><br>\n"; }
  281. $sort_table = 'User_auth';
  282. $sort_url = "<a href=edituser.php?id=" . $id;
  283. ?>
  284. <br><b><?php echo WEB_user_ip_list; ?></b><br>
  285. <table class="data">
  286. <tr>
  287. <td class="data"><?php echo WEB_user_add_ip; ?>:&nbsp<input type=text name=newip value=""></td>
  288. <td class="data"><?php echo WEB_user_add_mac; ?>:&nbsp<input type=text name=newmac value=""></td>
  289. <td class="data"><input type="submit" name="addauth" value="<?php echo WEB_btn_add; ?>"></td>
  290. <td class="data" align=right><input type="submit" name="new_user" value="<?php echo WEB_btn_transfom; ?>"></td>
  291. </tr>
  292. </table>
  293. <table class="data">
  294. <tr>
  295. <td class="data"><input type="checkbox" onClick="checkAll(this.checked);"></td>
  296. <td class="data"><?php print $sort_url . "&sort=ip_int&order=$new_order>" . WEB_cell_ip . "</a>"; ?></td>
  297. <td class="data"><?php print $sort_url . "&sort=mac&order=$new_order>" . WEB_cell_mac . "</a>"; ?></td>
  298. <td class="data"><?php print WEB_cell_comment; ?></td>
  299. <td class="data"><?php print $sort_url . "&sort=dns_name&order=$new_order>" . WEB_cell_dns_name . "</a>"; ?></td>
  300. <td class="data"><?php print WEB_cell_enabled; ?></td>
  301. <td class="data"><?php print WEB_cell_dhcp; ?></td>
  302. <td class="data"><?php print WEB_cell_filter; ?></td>
  303. <td class="data"><?php print WEB_cell_shaper; ?></td>
  304. <td class="data"><?php print WEB_cell_perday."/<br>".WEB_cell_permonth.", Mb"; ?></td>
  305. <td class="data"><?php print WEB_cell_connection; ?></td>
  306. <td class="data"><?php print $sort_url . "&sort=timestamp&order=$new_order>".WEB_cell_created."</a>"; ?></td>
  307. <td class="data">Last DHCP/ARP Event</td>
  308. <td class="data"><?php print $sort_url . "&sort=last_found&order=$new_order>".WEB_cell_last_found."</a>"; ?></td>
  309. <td class="data"><?php print "<input type=\"submit\" onclick=\"return confirm('".WEB_msg_apply_selected."?')\" name=\"removeauth\" value=".WEB_btn_remove.">"; ?></td>
  310. </tr>
  311. <?php
  312. $flist=get_records($db_link,'User_auth',"user_id=".$id." and deleted=0 ORDER BY $sort_table.$sort_field $order");
  313. if (!empty($flist)) {
  314. foreach ( $flist as $row ) {
  315. if ($row["dhcp_time"] == '0000-00-00 00:00:00') {
  316. $dhcp_str = '';
  317. } else {
  318. $dhcp_str = FormatDateStr('Y.m.d H:m',$row["dhcp_time"]) . " (" . $row["dhcp_action"] . ")";
  319. }
  320. if ($row["last_found"] == '0000-00-00 00:00:00') { $row["last_found"] = ''; }
  321. print "<tr align=center>\n";
  322. print "<td class=\"data\" style='padding:0'><input type=checkbox name=f_auth_id[] value=".$row["id"]." ></td>\n";
  323. print "<td class=\"data\" align=left><a href=editauth.php?id=".$row["id"].">" . $row["ip"] . "</a></td>\n";
  324. print "<td class=\"data\" >" . expand_mac($db_link,$row["mac"]) . "</td>\n";
  325. if (isset($row["dhcp_hostname"]) and strlen($row["dhcp_hostname"]) > 0) {
  326. print "<td class=\"data\" >".$row["comments"]." [" . $row["dhcp_hostname"] . "]</td>\n";
  327. } else {
  328. print "<td class=\"data\" >".$row["comments"]."</td>\n";
  329. }
  330. print "<td class=\"data\" >".$row["dns_name"]."</td>\n";
  331. $ip_status = 1;
  332. if ($row["blocked"] or !$row["enabled"]) { $ip_status = 0; }
  333. print "<td class=\"data\" >" . get_qa($ip_status). "</td>\n";
  334. print "<td class=\"data\" >" . get_qa($row["dhcp"]). "</td>\n";
  335. print "<td class=\"data\" >" . get_group($db_link, $row["filter_group_id"]) . "</td>\n";
  336. print "<td class=\"data\" >" . get_queue($db_link, $row["queue_id"]) . "</td>\n";
  337. print "<td class=\"data\" >".$row["day_quota"]."/".$row["month_quota"]."</td>\n";
  338. print "<td class=\"data\" >" . get_connection($db_link, $row["id"]) . "</td>\n";
  339. print "<td class=\"data\" >" . FormatDateStr('Y.m.d',$row["timestamp"]) . "</td>\n";
  340. print "<td class=\"data\" >" . $dhcp_str . "</td>\n";
  341. print "<td class=\"data\" >" . FormatDateStr('Y.m.d H:i',$row["last_found"]) . "</td>\n";
  342. print "<td class=\"data\" ></td></tr>";
  343. }
  344. }
  345. ?>
  346. </table>
  347. </form>
  348. <?php
  349. require_once ($_SERVER["DOCUMENT_ROOT"]."/inc/footer.php");
  350. ?>