editauth.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. $msg_error = "";
  6. $old_auth_info = get_record_sql($db_link, "SELECT * FROM User_auth WHERE id=".$id);
  7. $parent_id = $old_auth_info['user_id'];
  8. $user_info = get_record_sql($db_link,"SELECT * FROM User_list WHERE id=".$parent_id);
  9. $parent_ou_id = $user_info['ou_id'];
  10. if (isset($_POST["editauth"]) and !$old_auth_info['deleted']) {
  11. $ip = trim($_POST["f_ip"]);
  12. if (checkValidIp($ip)) {
  13. $ip_aton = ip2long($ip);
  14. $mac=mac_dotted($_POST["f_mac"]);
  15. //search mac
  16. $mac_exists=find_mac_in_subnet($db_link,$ip,$mac);
  17. if (isset($mac_exists) and $mac_exists['count']>=1 and !in_array($parent_id,$mac_exists['users_id'])) {
  18. $dup_sql = "SELECT * FROM User_list WHERE id=".$mac_exists['users_id']['0'];
  19. $dup_info = get_record_sql($db_link, $dup_sql);
  20. $msg_error="Mac already exists at another user in this subnet! Skip creating $ip [$mac].<br>Old user id: ".$dup_info['id']." login: ".$dup_info['login'];
  21. $_SESSION[$page_url]['msg'] = $msg_error;
  22. LOG_ERROR($db_link, $msg_error);
  23. header("Location: " . $_SERVER["REQUEST_URI"]);
  24. exit;
  25. }
  26. //disable dhcp for secondary ip
  27. $f_dhcp = $_POST["f_dhcp"] * 1;
  28. if (in_array($parent_id,$mac_exists['users_id'])) {
  29. if ($parent_id != $mac_exists['users_id'][0]) { $f_dhcp = 0; }
  30. }
  31. //search ip
  32. $dup_ip_record = get_record_sql($db_link, "SELECT * FROM User_auth WHERE `ip_int`=$ip_aton AND id<>$id AND deleted=0");
  33. if (!empty($dup_ip_record)) {
  34. $dup_info = get_record_sql($db_link, "SELECT * FROM User_list WHERE id=".$dup_ip_record['user_id']);
  35. $msg_error = "$ip already exists. Skip creating $ip [$mac].<br>Old user id: ".$dup_info['id']." login: ".$dup_info['login'];
  36. $_SESSION[$page_url]['msg'] = $msg_error;
  37. LOG_ERROR($db_link, $msg_error);
  38. header("Location: " . $_SERVER["REQUEST_URI"]);
  39. exit;
  40. }
  41. $new['ip'] = $ip;
  42. $new['ou_id'] = $parent_ou_id;
  43. $new['ip_int'] = $ip_aton;
  44. $new['mac'] = mac_dotted($_POST["f_mac"]);
  45. $new['comments'] = $_POST["f_comments"];
  46. $new['firmware'] = $_POST["f_firmware"];
  47. $new['WikiName'] = $_POST["f_wiki"];
  48. $f_dnsname=trim($_POST["f_dns_name"]);
  49. // if (!empty($f_dnsname) and checkValidHostname($f_dnsname) and checkUniqHostname($db_link,$id,$f_dnsname)) { $new['dns_name'] = $f_dnsname; }
  50. if (!empty($f_dnsname) and checkValidHostname($f_dnsname)) { $new['dns_name'] = $f_dnsname; }
  51. if (empty($f_dnsname)) { $new['dns_name'] = ''; }
  52. $new['save_traf'] = $_POST["f_save_traf"] * 1;
  53. $new['dhcp_acl'] = trim($_POST["f_acl"]);
  54. if (get_const('default_user_ou_id') == $parent_ou_id or get_const('default_hotspot_ou_id') == $parent_ou_id) {
  55. $new['nagios_handler'] = '';
  56. $new['enabled'] = 0;
  57. $new['link_check'] = 0;
  58. $new['nagios'] = 0;
  59. $new['blocked'] = 0;
  60. $new['day_quota'] = 0;
  61. $new['month_quota'] = 0;
  62. $new['queue_id'] = 0;
  63. $new['filter_group_id'] = 0;
  64. } else {
  65. $new['nagios_handler'] = $_POST["f_handler"];
  66. $new['enabled'] = $_POST["f_enabled"] * 1;
  67. $new['link_check'] = $_POST["f_link"] * 1;
  68. $new['nagios'] = $_POST["f_nagios"] * 1;
  69. $new['dhcp'] = $f_dhcp;
  70. $new['blocked'] = $_POST["f_blocked"] * 1;
  71. $new['day_quota'] = $_POST["f_day_q"] * 1;
  72. $new['month_quota'] = $_POST["f_month_q"] * 1;
  73. $new['queue_id'] = $_POST["f_queue_id"] * 1;
  74. $new['filter_group_id'] = $_POST["f_group_id"] * 1;
  75. }
  76. if ($new['nagios'] ==0) { $new['nagios_status']='UP'; }
  77. $changes = get_diff_rec($db_link,"User_auth","id='$id'", $new, 0);
  78. if (!empty($changes)) { LOG_WARNING($db_link,"Изменена запись для адреса $ip! Список изменений: $changes",$id); }
  79. if (is_auth_bind_changed($db_link,$id,$ip,$mac)) {
  80. $new_id = copy_auth($db_link,$id,$new);
  81. header("Location: /admin/users/editauth.php?id=".$new_id,TRUE, 302);
  82. exit;
  83. } else {
  84. update_record($db_link, "User_auth", "id='$id'", $new);
  85. }
  86. } else {
  87. $msg_error = "$msg_ip_error xxx.xxx.xxx.xxx";
  88. $_SESSION[$page_url]['msg'] = $msg_error;
  89. }
  90. header("Location: " . $_SERVER["REQUEST_URI"]);
  91. exit;
  92. }
  93. if (isset($_POST["moveauth"]) and !$old_auth_info['deleted']) {
  94. $new_parent_id = $_POST["f_new_parent"]*1;
  95. $changes=apply_auth_rule($db_link,$id,$new_parent_id);
  96. LOG_WARNING($db_link,"Адрес доступа перемещён к другому пользователю! Применено: $changes",$id);
  97. header("Location: " . $_SERVER["REQUEST_URI"]);
  98. exit;
  99. }
  100. if (isset($_POST["recovery"]) and $old_auth_info['deleted']) {
  101. $ip = trim($_POST["f_ip"]);
  102. if (checkValidIp($ip)) {
  103. $ip_aton = ip2long($ip);
  104. $mac=mac_dotted($_POST["f_mac"]);
  105. //search mac
  106. $mac_exists=find_mac_in_subnet($db_link,$ip,$mac);
  107. if (isset($mac_exists) and $mac_exists['count']>=1 and !in_array($parent_id,$mac_exists['users_id'])) {
  108. $dup_sql = "SELECT * FROM User_list WHERE id=".$mac_exists['users_id']['0'];
  109. $dup_info = get_record_sql($db_link, $dup_sql);
  110. $msg_error="Mac already exists at another user in this subnet! Skip creating $ip [$mac].<br>Old user id: ".$dup_info['id']." login: ".$dup_info['login'];
  111. $_SESSION[$page_url]['msg'] = $msg_error;
  112. LOG_ERROR($db_link, $msg_error);
  113. header("Location: " . $_SERVER["REQUEST_URI"]);
  114. exit;
  115. }
  116. //disable dhcp for secondary ip
  117. $f_dhcp = $_POST["f_dhcp"] * 1;
  118. if (in_array($parent_id,$mac_exists['users_id'])) {
  119. if ($parent_id != $mac_exists['users_id'][0]) { $f_dhcp = 0; }
  120. }
  121. //search ip
  122. $dup_ip_record = get_record_sql($db_link, "SELECT * FROM User_auth WHERE `ip_int`=$ip_aton AND id<>$id AND deleted=0");
  123. if (!empty($dup_ip_record)) {
  124. $dup_info = get_record_sql($db_link, "SELECT * FROM User_list WHERE id=".$dup_ip_record['user_id']);
  125. $msg_error = "$ip already exists. Skip creating $ip [$mac].<br>Old user id: ".$dup_info['id']." login: ".$dup_info['login'];
  126. $_SESSION[$page_url]['msg'] = $msg_error;
  127. LOG_ERROR($db_link, $msg_error);
  128. header("Location: " . $_SERVER["REQUEST_URI"]);
  129. exit;
  130. }
  131. $new['deleted'] = 0;
  132. if (!empty($_POST["f_nagios"])) { $a_nagios=$_POST["f_nagios"] * 1; } else { $a_nagios = 0; }
  133. if (!empty($_POST["f_link"])) { $a_link=$_POST["f_link"] * 1; } else { $a_link = 0; }
  134. $new_parent = get_record_sql($db_link,"User_list","id=".$parent_id);
  135. if (!empty($new_parent)) {
  136. $new['user_id'] = $parent_id;
  137. $new['ou_id'] = $new_parent['ou_id'];
  138. } else {
  139. $new_user_info = get_new_user_id($db_link, $ip, $mac, NULL);
  140. if ($new_user_info['user_id']) { $new_user_id = $new_user_info['user_id']; }
  141. if (empty($new_user_id)) { $new_user_id = new_user($db_link,$new_user_info); }
  142. $new['user_id'] = $new_user_id;
  143. }
  144. if (get_const('default_user_ou_id') == $parent_ou_id or get_const('default_hotspot_ou_id') == $parent_ou_id) {
  145. $new['nagios_handler'] = '';
  146. $new['enabled'] = 0;
  147. $new['link_check'] = 0;
  148. $new['nagios'] = 0;
  149. $new['blocked'] = 0;
  150. $new['day_quota'] = 0;
  151. $new['month_quota'] = 0;
  152. $new['queue_id'] = 0;
  153. $new['filter_group_id'] = 0;
  154. } else {
  155. $new['nagios_handler'] = $_POST["f_handler"];
  156. $new['enabled'] = $_POST["f_enabled"] * 1;
  157. $new['link_check'] = $a_link;
  158. $new['nagios'] = $a_nagios;
  159. $new['dhcp'] = $_POST["f_dhcp"] * 1;
  160. $new['blocked'] = $_POST["f_blocked"] * 1;
  161. $new['day_quota'] = $_POST["f_day_q"] * 1;
  162. $new['month_quota'] = $_POST["f_month_q"] * 1;
  163. $new['queue_id'] = $_POST["f_queue_id"] * 1;
  164. $new['filter_group_id'] = $_POST["f_group_id"] * 1;
  165. }
  166. $changes = get_diff_rec($db_link,"User_auth","id='$id'", $new, 0);
  167. if (!empty($changes)) { LOG_WARNING($db_link,"Восстановлен адрес доступа! Применено: $changes",$id); }
  168. update_record($db_link, "User_auth", "id='$id'", $new);
  169. apply_auth_rule($db_link,$id,$new['user_id']);
  170. } else {
  171. $msg_error = "$msg_ip_error xxx.xxx.xxx.xxx/xx";
  172. $_SESSION[$page_url]['msg'] = $msg_error;
  173. }
  174. header("Location: " . $_SERVER["REQUEST_URI"]);
  175. exit;
  176. }
  177. unset($_POST);
  178. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/header.php");
  179. $sSQL = "SELECT * FROM User_auth WHERE id=$id";
  180. $auth_info = get_record_sql($db_link, $sSQL);
  181. $device = get_record_sql($db_link,"SELECT * FROM devices WHERE user_id=".$auth_info['user_id']);
  182. $parent_name = get_login($db_link, $auth_info['user_id']);
  183. if ($auth_info['dhcp_time'] == '0000-00-00 00:00:00') { $dhcp_str = ''; } else { $dhcp_str = $auth_info['dhcp_time'] . " (" . $auth_info['dhcp_action'] . ")"; }
  184. if ($auth_info['last_found'] == '0000-00-00 00:00:00') { $auth_info['last_found'] = ''; }
  185. ?>
  186. <div id="cont">
  187. <?php
  188. if (!empty($_SESSION[$page_url]['msg'])) {
  189. print '<div id="msg">'.$_SESSION[$page_url]['msg'].'</div>';
  190. unset($_SESSION[$page_url]['msg']);
  191. }
  192. print "<b> Адрес доступа пользователя <a href=/admin/users/edituser.php?id=".$auth_info['user_id'].">".$parent_name."</a> </b>";
  193. ?>
  194. <form name="def" action="editauth.php?id=<?php echo $id; ?>" method="post">
  195. <input type="hidden" name="id" value=<?php echo $id; ?>>
  196. <table class="data">
  197. <tr>
  198. <td width=200><?php print $cell_dns_name." &nbsp | &nbsp "; print_url("Альясы","/admin/users/edit_alias.php?id=$id"); ?></td>
  199. <td width=200><?php print $cell_comment; ?></td>
  200. <td width=70><?php print $cell_enabled; ?></td>
  201. <td><?php print $cell_traf; ?></td>
  202. <td></td>
  203. </tr>
  204. <tr>
  205. <td><input type="text" name="f_dns_name" value="<?php echo $auth_info['dns_name']; ?>" pattern="^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$"></td>
  206. <td><input type="text" name="f_comments" value="<?php echo $auth_info['comments']; ?>"></td>
  207. <td><?php print_qa_select('f_enabled', $auth_info['enabled']); ?></td>
  208. <td><?php print_qa_select('f_save_traf', $auth_info['save_traf']); ?></td>
  209. <td></td>
  210. </tr>
  211. <tr>
  212. <td><?php print $cell_ip; ?></td>
  213. <td><?php print $cell_mac; ?></td>
  214. <td><?php print $cell_dhcp; ?></td>
  215. <td><?php print $cell_acl; ?></td>
  216. <td></td>
  217. <tr>
  218. <td><input type="text" name="f_ip" value="<?php echo $auth_info['ip']; ?>" pattern="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"></td>
  219. <td><input type="text" name="f_mac" value="<?php echo $auth_info['mac']; ?>" pattern="^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}|([0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4})|[0-9A-Fa-f]{12}$"></td>
  220. <td><?php print_qa_select('f_dhcp', $auth_info['dhcp']); ?></td>
  221. <td colspan=2><input type="text" name="f_acl" value="<?php echo $auth_info['dhcp_acl']; ?>"></td>
  222. </tr>
  223. <tr>
  224. <td><?php print $cell_filter; ?></td>
  225. <td><?php print $cell_shaper; ?></td>
  226. <td><?php print $cell_blocked; ?></td>
  227. <td><?php print $cell_perday; ?></td>
  228. <td><?php print $cell_permonth; ?></td>
  229. </tr>
  230. <tr>
  231. <td><?php print_group_select($db_link, 'f_group_id', $auth_info['filter_group_id']); ?> </td>
  232. <td><?php print_queue_select($db_link, 'f_queue_id', $auth_info['queue_id']); ?> </td>
  233. <td><?php print_qa_select('f_blocked', $auth_info['blocked']); ?></td>
  234. <td><input type="text" name="f_day_q" value="<?php echo $auth_info['day_quota']; ?>" size=5></td>
  235. <td><input type="text" name="f_month_q" value="<?php echo $auth_info['month_quota']; ?>" size=5></td>
  236. </tr>
  237. <tr>
  238. <td><?php print $cell_nagios_handler; ?></td>
  239. <td width=200>
  240. <?php
  241. if (!empty($auth_info['WikiName'])) {
  242. $wiki_url = rtrim(get_option($db_link, 60),'/');
  243. if (preg_match('/127.0.0.1/', $wiki_url)) { print $cell_wikiname; } else {
  244. $wiki_web = rtrim(get_option($db_link, 63),'/');
  245. $wiki_web = ltrim($wiki_web,'/');
  246. $wiki_link = $wiki_url.'/'.$wiki_web.'/'.$auth_info['WikiName'];
  247. print_url($cell_wikiname,$wiki_link);
  248. }
  249. } else {
  250. print $cell_wikiname;
  251. }
  252. $dev_id = get_device_by_auth($db_link,$auth_info['user_id']);
  253. if (isset($dev_id)) {
  254. print "&nbsp|&nbsp";
  255. print_url('Device','/admin/devices/editdevice.php?id='.$dev_id);
  256. }
  257. ?>
  258. </td>
  259. <td><?php if (empty($device) or (!empty($device) and $device['device_type']>2)) { print $cell_nagios; } ?></td>
  260. <td><?php if (empty($device) or (!empty($device) and $device['device_type']>2)) { print $cell_link; }?></td>
  261. <tr>
  262. <td><input type="text" name="f_handler" value="<?php echo $auth_info['nagios_handler']; ?>"></td>
  263. <td><input type="text" name="f_wiki" value="<?php echo $auth_info['WikiName']; ?>"></td>
  264. <td><?php if (empty($device) or (!empty($device) and $device['device_type']>2)) { print_qa_select('f_nagios', $auth_info['nagios']); } ?></td>
  265. <td><?php if (empty($device) or (!empty($device) and $device['device_type']>2)) { print_qa_select('f_link', $auth_info['link_check']); } ?></td>
  266. <td></td>
  267. </tr>
  268. <tr>
  269. <td colspan=2><input type="submit" name="moveauth" value=<?php print $btn_move; ?>><?php print_login_select($db_link, 'f_new_parent', $auth_info['user_id']); ?></td>
  270. <td><a href=/admin/logs/authlog.php?auth_id=<?php print $id; ?>>Лог</a></td>
  271. <?php
  272. if ($auth_info['deleted']) {
  273. print "<td >Deleted: " . $auth_info['changed_time']."</td>";
  274. print "<td align=right><input type=\"submit\" name=\"recovery\" value=\"Восстановить\"></td>";
  275. } else {
  276. print "<td ></td>";
  277. print "<td align=right><input type=\"submit\" name=\"editauth\" value=\"$btn_save\"></td>";
  278. }
  279. ?>
  280. </tr>
  281. </table>
  282. <table class="data">
  283. <tr><td class="data" colspan=5>Status:</td></tr>
  284. <tr>
  285. <td colspan=2><?php print "Dhcp hostname: " . $auth_info['dhcp_hostname']; ?></td><td width=100>&nbsp</td><td align=right><?php print "Dhcp event: " . $dhcp_str; ?></td>
  286. </tr>
  287. <tr>
  288. <td><?php print "Created: "; ?></td><td><?php print $auth_info['timestamp']; ?></td>
  289. <td align=right colspan=2><?php print_url("Трафик за день","/admin/reports/authday.php?id=$id"); ?></td>
  290. </tr>
  291. <tr>
  292. <td><?php print "Last found: "; ?></td><td><?php print $auth_info['last_found']."<br>"; ?></td>
  293. <td align=right><?php print "Connected: "; ?></td><td align=right><?php print get_connection($db_link, $id)."<br>"; ?></td>
  294. </tr>
  295. </table>
  296. <?php
  297. if ($msg_error) {
  298. print "<div id='msg'><b>$msg_error</b></div><br>\n";
  299. }
  300. ?>
  301. </form>
  302. <br>
  303. <?php require_once ($_SERVER['DOCUMENT_ROOT']."/inc/footer.php"); ?>