editauth.php 15 KB

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