edituser.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. require_once ($_SERVER["DOCUMENT_ROOT"]."/inc/auth.php");
  3. require_once ($_SERVER["DOCUMENT_ROOT"]."/inc/languages/" . $language . ".php");
  4. require_once ($_SERVER["DOCUMENT_ROOT"]."/inc/idfilter.php");
  5. global $default_user_ou_id;
  6. global $default_hotspot_ou_id;
  7. $default_sort='ip_int';
  8. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/sortfilter.php");
  9. $msg_error = "";
  10. $sSQL = "SELECT * FROM User_list WHERE id=$id";
  11. $user_info = get_record_sql($db_link, $sSQL);
  12. if (isset($_POST["edituser"])) {
  13. unset($new);
  14. $new["ou_id"] = $_POST["f_ou"] * 1;
  15. $new["filter_group_id"] = $_POST["f_filter"]*1;
  16. $new["queue_id"] = $_POST["f_queue"]*1;
  17. $new["login"] = trim($_POST["f_login"]);
  18. $new["fio"] = trim($_POST["f_fio"]);
  19. if ($default_user_ou_id == $new["ou_id"] or $default_hotspot_ou_id == $new["ou_id"]) {
  20. $new["enabled"] = 0;
  21. $new["blocked"] = 0;
  22. $new["day_quota"] = 0;
  23. $new["month_quota"] = 0;
  24. $auth["enabled"] = 0;
  25. $auth["blocked"] = 0;
  26. } else {
  27. $new["enabled"] = $_POST["f_enabled"] * 1;
  28. $new["blocked"] = $_POST["f_blocked"] * 1;
  29. $new["day_quota"] = trim($_POST["f_perday"]) * 1;
  30. $new["month_quota"] = trim($_POST["f_permonth"]) * 1;
  31. }
  32. $changes = get_diff_rec($db_link,"User_list","id='$id'", $new, 0);
  33. if (!empty($changes)) { LOG_WARNING($db_link,"Изменён пользователь id: $id login: ".$new["login"].". \r\nПрименено: $changes"); }
  34. update_record($db_link, "User_list", "id='$id'", $new);
  35. run_sql($db_link, "UPDATE User_auth SET ou_id=".$new["ou_id"]." WHERE user_id=".$id);
  36. run_sql($db_link, "UPDATE devices SET device_name='".$new["login"]."' WHERE user_id=".$id);
  37. header("Location: " . $_SERVER["REQUEST_URI"]);
  38. exit;
  39. }
  40. if (isset($_POST["addMacRule"])) {
  41. unset($new);
  42. $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");
  43. if (!empty($first_auth) and !empty($first_auth['mac'])) {
  44. $new['user_id']=$id;
  45. $new['type']=2;
  46. $new['rule']=$first_auth['mac'];
  47. insert_record($db_link,"auth_rules",$new);
  48. LOG_INFO($db_link,"Создано правило атоназначения юзеру id: ".$id." login: ".$user_info["login"]." для мака ".$first_auth['mac']);
  49. }
  50. header("Location: " . $_SERVER["REQUEST_URI"]);
  51. exit;
  52. }
  53. if (isset($_POST["delMacRule"])) {
  54. run_sql($db_link,"DELETE FROM auth_rules WHERE user_id=".$id." AND type=2");
  55. LOG_INFO($db_link,"Удалены все правила атоназначения юзеру id: $id login: ".$user_info["login"]." по маку");
  56. header("Location: " . $_SERVER["REQUEST_URI"]);
  57. exit;
  58. }
  59. if (isset($_POST["addIPRule"])) {
  60. unset($new);
  61. $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");
  62. if (!empty($first_auth) and !empty($first_auth['ip'])) {
  63. $new['user_id']=$id;
  64. $new['type']=1;
  65. $new['rule']=$first_auth['ip'];
  66. insert_record($db_link,"auth_rules",$new);
  67. LOG_INFO($db_link,"Создано правило атоназначения юзеру id: ".$id." login: ".$user_info["login"]." для IP ".$first_auth['IP']);
  68. }
  69. header("Location: " . $_SERVER["REQUEST_URI"]);
  70. exit;
  71. }
  72. if (isset($_POST["delIPRule"])) {
  73. run_sql($db_link,"DELETE FROM auth_rules WHERE user_id=".$id." AND type=1");
  74. LOG_INFO($db_link,"Удалены все правила атоназначения юзеру id: $id login: ".$user_info["login"]." по ip");
  75. header("Location: " . $_SERVER["REQUEST_URI"]);
  76. exit;
  77. }
  78. if (isset($_POST["showDevice"])) {
  79. $device = get_record_sql($db_link,"SELECT * FROM devices WHERE user_id=".$id);
  80. $auth = get_record_sql($db_link,"SELECT * FROM User_auth WHERE user_id=".$id);
  81. if (empty($device) and !empty($auth)) {
  82. global $snmp_default_version;
  83. global $snmp_default_community;
  84. $new['user_id']=$id;
  85. $new['device_name'] = $user_info['login'];
  86. $new['device_type'] = 5;
  87. $new['ip']=$auth['ip'];
  88. $new['community'] = $snmp_default_community;
  89. $new['snmp_version'] = $snmp_default_version;
  90. $new_id=insert_record($db_link, "devices", $new);
  91. unset($_POST);
  92. if (!empty($new_id)) {
  93. LOG_INFO($db_link, "Created device with id: $new_id for auth_id: $id");
  94. header("Location: /admin/devices/editdevice.php?id={$new_id}");
  95. exit;
  96. } else {
  97. header("Location: ".$_SERVER["REQUEST_URI"]);
  98. exit;
  99. }
  100. }
  101. header("Location: /admin/devices/editdevice.php?id=".$device['id']);
  102. exit;
  103. }
  104. if (isset($_POST["addauth"])) {
  105. $fip = substr(trim($_POST["newip"]), 0, 18);
  106. if (isset($_POST["newmac"])) { $fmac = mac_dotted(substr(trim($_POST["newmac"]), 0, 17)); }
  107. if ($fip) {
  108. if (checkValidIp($fip)) {
  109. $ip_aton = ip2long($fip);
  110. //search mac
  111. $mac_exists=find_mac_in_subnet($db_link,$fip,$fmac);
  112. if (isset($mac_exists) and $mac_exists['count']>=1 and !in_array($id,$mac_exists['users_id'])) {
  113. $dup_sql = "SELECT * FROM User_list WHERE id=".$mac_exists['users_id']['0'];
  114. $dup_info = get_record_sql($db_link, $dup_sql);
  115. $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'];
  116. $_SESSION[$page_url]['msg'] = $msg_error;
  117. LOG_ERROR($db_link, $msg_error);
  118. header("Location: " . $_SERVER["REQUEST_URI"]);
  119. exit;
  120. }
  121. //disable dhcp for secondary ip
  122. $f_dhcp = 1;
  123. if (in_array($id,$mac_exists['users_id'])) { $f_dhcp = 0; }
  124. //search ip
  125. $dup_ip_record = get_record_sql($db_link, "SELECT * FROM User_auth WHERE `ip_int`=$ip_aton AND user_id<>".$id." AND deleted=0");
  126. if (!empty($dup_ip_record)) {
  127. $dup_info = get_record_sql($db_link, "SELECT * FROM User_list WHERE id=".$dup_ip_record['user_id']);
  128. $msg_error = "$fip already exists. 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. $fid = new_auth($db_link, $fip, $fmac, $id);
  135. if (!empty($fid)) {
  136. $new['dhcp']=$f_dhcp;
  137. update_record($db_link,"User_auth","id=".$fid,$new);
  138. apply_auth_rule($db_link,$fid,$id);
  139. LOG_WARNING($db_link,"Создан новый адрес доступа для login: ".$user_info["login"].": ip => $fip, mac => $fmac",$fid);
  140. header("Location: /admin/users/editauth.php?id=".$fid);
  141. exit;
  142. }
  143. header("Location: " . $_SERVER["REQUEST_URI"]);
  144. exit;
  145. } else {
  146. $msg_error = "$msg_ip_error xxx.xxx.xxx.xxx";
  147. $_SESSION[$page_url]['msg'] = $msg_error;
  148. }
  149. }
  150. header("Location: " . $_SERVER["REQUEST_URI"]);
  151. exit;
  152. }
  153. if (isset($_POST["removeauth"])) {
  154. $auth_id = $_POST["f_auth_id"];
  155. foreach ($auth_id as $key => $val) {
  156. if ($val) {
  157. run_sql($db_link, 'DELETE FROM connections WHERE auth_id='.$val);
  158. run_sql($db_link, 'DELETE FROM User_auth_alias WHERE auth_id='.$val);
  159. $auth["deleted"] = 1;
  160. $changes = get_diff_rec($db_link,"User_auth","id='$val'", '', 0);
  161. if (!empty($changes)) { LOG_WARNING($db_link,"Удалён адрес доступа для login: ".$user_info["login"].": \r\n $changes",$val); }
  162. update_record($db_link, "User_auth", "id=" . $val, $auth);
  163. }
  164. }
  165. header("Location: " . $_SERVER["REQUEST_URI"]);
  166. exit;
  167. }
  168. unset($_POST);
  169. require_once ($_SERVER["DOCUMENT_ROOT"]."/inc/header.php");
  170. ?>
  171. <div id="cont">
  172. <?php
  173. if (!empty($_SESSION[$page_url]['msg'])) {
  174. print '<div id="msg">'.$_SESSION[$page_url]['msg'].'</div>';
  175. unset($_SESSION[$page_url]['msg']);
  176. }
  177. ?>
  178. <form name="def" action="edituser.php?id=<?php echo $id; ?>" method="post">
  179. <input type="hidden" name="id" value=<?php echo $id; ?>>
  180. <table class="data">
  181. <tr>
  182. <td colspan=2><?php print $cell_login; ?></td>
  183. <td colspan=2><?php print $cell_fio; ?></td>
  184. <td colspan=2><?php print $cell_ou; ?></td>
  185. </tr>
  186. <tr>
  187. <td colspan=2><input type="text" name="f_login" value="<?php print $user_info["login"]; ?>" size=25></td>
  188. <td colspan=2><input type="text" name="f_fio" value="<?php print $user_info["fio"]; ?>" size=25></td>
  189. <td colspan=2><?php print_ou_set($db_link, 'f_ou', $user_info["ou_id"]); ?></td>
  190. </tr>
  191. <tr>
  192. <td colspan=2><?php print $cell_perday; ?></td>
  193. <td colspan=2><?php print $cell_permonth; ?></td>
  194. <td><?php print $cell_blocked; ?></td>
  195. <td><?php print $cell_enabled; ?></td>
  196. </tr>
  197. <tr>
  198. <td colspan=2><input type="text" name="f_perday" value="<?php echo $user_info["day_quota"]; ?>" size=5></td>
  199. <td colspan=2><input type="text" name="f_permonth" value="<?php echo $user_info["month_quota"]; ?>" size=5></td>
  200. <td ><?php print_qa_select('f_blocked', $user_info["blocked"]); ?></td>
  201. <td ><?php print_qa_select('f_enabled', $user_info["enabled"]); ?></td>
  202. </tr>
  203. <tr><td class=data colspan=6>Параметры для автоназначенных адресов:</td></tr>
  204. <tr>
  205. <td colspan=2><?php print $cell_filter; ?></td>
  206. <td colspan=2><?php print $cell_shaper; ?></td>
  207. <td colspan=2></td>
  208. </tr>
  209. <tr>
  210. <td colspan=2><?php print_group_select($db_link, 'f_filter', $user_info["filter_group_id"]); ?></td>
  211. <td colspan=2><?php print_queue_select($db_link, 'f_queue', $user_info["queue_id"]); ?></td>
  212. <td colspan=2></td>
  213. </tr>
  214. <tr>
  215. <?php
  216. print "<td>"; print_url("Список правил","/admin/users/edit_rules.php?id=$id"); print "</td>";
  217. $rule_count = get_count_records($db_link,"auth_rules","user_id=".$id);
  218. print "<td > Count: ".$rule_count."</td>";
  219. $first_auth = get_record_sql($db_link,"SELECT id FROM User_auth WHERE user_id=".$id." AND deleted=0 ORDER BY id");
  220. if (!empty($first_auth)) {
  221. //mac
  222. $mac_rule_count = get_count_records($db_link,"auth_rules","user_id=".$id." AND type=2");
  223. if (!empty($mac_rule_count)) {
  224. print "<td><input type=\"submit\" name=\"delMacRule\" value=".$btn_mac_del." ></td>";
  225. } else {
  226. print "<td><input type=\"submit\" name=\"addMacRule\" value=".$btn_mac_add." ></td>";
  227. }
  228. //ip
  229. $ip_rule_count = get_count_records($db_link,"auth_rules","user_id=".$id." AND type=1");
  230. if (!empty($ip_rule_count)) {
  231. print "<td><input type=\"submit\" name=\"delIPRule\" value=".$btn_ip_del." ></td>";
  232. } else {
  233. print "<td><input type=\"submit\" name=\"addIPRule\" value=".$btn_ip_add." ></td>";
  234. }
  235. } else { print "<td colspan=2></td>"; }
  236. ?>
  237. <td colspan=2 align=right>Created: <?php print $user_info["timestamp"]; ?></td>
  238. </tr>
  239. <tr>
  240. <?php print "<td colspan=2>"; print_url("Трафик за день","/admin/reports/userday.php?id=$id"); ?></td>
  241. <td></td>
  242. <td><input type="submit" name="showDevice" value=<?php print $btn_device; ?>></td>
  243. <td></td>
  244. <td align=right><input type="submit" name="edituser" value=<?php print $btn_save; ?>></td>
  245. </tr>
  246. </table>
  247. <?php
  248. if ($msg_error) { print "<div id='msg'><b>$msg_error</b></div><br>\n"; }
  249. $sort_table = 'User_auth';
  250. $sort_url = "<a href=edituser.php?id=" . $id;
  251. if ($id == $default_user_id or $id == $hotspot_user_id) { $default_sort = 'last_found DESC'; }
  252. ?>
  253. <br><b>Список адресов доступа</b><br>
  254. <table class="data">
  255. <tr>
  256. <td class="data">Новый адрес доступа IP:&nbsp<input type=text name=newip value=""></td>
  257. <td class="data">Mac (необязательно):&nbsp<input type=text name=newmac value=""></td>
  258. <td class="data"><input type="submit" name="addauth" value="Добавить"></td>
  259. </tr>
  260. </table>
  261. <table class="data">
  262. <tr>
  263. <td class="data"><input type="checkbox" onClick="checkAll(this.checked);"></td>
  264. <td class="data"><?php print $sort_url . "&sort=ip_int&order=$new_order>" . $cell_ip . "</a>"; ?></td>
  265. <td class="data"><?php print $sort_url . "&sort=mac&order=$new_order>" . $cell_mac . "</a>"; ?></td>
  266. <td class="data"><?php print $cell_comment; ?></td>
  267. <td class="data"><?php print $sort_url . "&sort=dns_name&order=$new_order>" . $cell_dns_name . "</a>"; ?></td>
  268. <td class="data"><?php print $cell_enabled; ?></td>
  269. <td class="data"><?php print $cell_dhcp; ?></td>
  270. <td class="data"><?php print $cell_filter; ?></td>
  271. <td class="data"><?php print $cell_shaper; ?></td>
  272. <td class="data"><?php print $cell_perday."/<br>".$cell_permonth.", Mb"; ?></td>
  273. <td class="data"><?php print $cell_connection; ?></td>
  274. <td class="data"><?php print $sort_url . "&sort=timestamp&order=$new_order>Created</a>"; ?></td>
  275. <td class="data">Last DHCP/ARP Event</td>
  276. <td class="data"><?php print $sort_url . "&sort=last_found&order=$new_order>Last found</a>"; ?></td>
  277. <td class="data"><?php print "<input type=\"submit\" onclick=\"return confirm('Применить для выделенных?')\" name=\"removeauth\" value=".$btn_remove.">"; ?></td>
  278. </tr>
  279. <?php
  280. $flist=get_records($db_link,'User_auth',"user_id=".$id." and deleted=0 ORDER BY $sort_table.$sort_field $order");
  281. if (!empty($flist)) {
  282. foreach ( $flist as $row ) {
  283. if ($row["dhcp_time"] == '0000-00-00 00:00:00') {
  284. $dhcp_str = '';
  285. } else {
  286. $dhcp_str = FormatDateStr('Y.m.d H:m',$row["dhcp_time"]) . " (" . $row["dhcp_action"] . ")";
  287. }
  288. if ($row["last_found"] == '0000-00-00 00:00:00') { $row["last_found"] = ''; }
  289. print "<tr align=center>\n";
  290. print "<td class=\"data\" style='padding:0'><input type=checkbox name=f_auth_id[] value=".$row["id"]." ></td>\n";
  291. print "<td class=\"data\" align=left><a href=editauth.php?id=".$row["id"].">" . $row["ip"] . "</a></td>\n";
  292. print "<td class=\"data\" >" . expand_mac($db_link,$row["mac"]) . "</td>\n";
  293. if (isset($row["dhcp_hostname"]) and strlen($row["dhcp_hostname"]) > 0) {
  294. print "<td class=\"data\" >".$row["comments"]." [" . $row["dhcp_hostname"] . "]</td>\n";
  295. } else {
  296. print "<td class=\"data\" >".$row["comments"]."</td>\n";
  297. }
  298. print "<td class=\"data\" >".$row["dns_name"]."</td>\n";
  299. $ip_status = 1;
  300. if ($row["blocked"] or !$row["enabled"]) { $ip_status = 0; }
  301. print "<td class=\"data\" >" . get_qa($ip_status). "</td>\n";
  302. print "<td class=\"data\" >" . get_qa($row["dhcp"]). "</td>\n";
  303. print "<td class=\"data\" >" . get_group($db_link, $row["filter_group_id"]) . "</td>\n";
  304. print "<td class=\"data\" >" . get_queue($db_link, $row["queue_id"]) . "</td>\n";
  305. print "<td class=\"data\" >".$row["day_quota"]."/".$row["month_quota"]."</td>\n";
  306. print "<td class=\"data\" >" . get_connection($db_link, $row["id"]) . "</td>\n";
  307. print "<td class=\"data\" >" . FormatDateStr('Y.m.d',$row["timestamp"]) . "</td>\n";
  308. print "<td class=\"data\" >" . $dhcp_str . "</td>\n";
  309. print "<td class=\"data\" >" . FormatDateStr('Y.m.d H:i',$row["last_found"]) . "</td>\n";
  310. print "<td class=\"data\" ></td></tr>";
  311. }
  312. }
  313. ?>
  314. </table>
  315. </form>
  316. <?php
  317. require_once ($_SERVER["DOCUMENT_ROOT"]."/inc/footer.php");
  318. ?>