api.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/qauth.php");
  3. $action='';
  4. $ip='';
  5. $mac='';
  6. $rec_id='';
  7. //GET
  8. if (!empty($_GET['get'])) { $action = 'get_'.$_GET['get']; }
  9. if (!empty($_GET['send'])) { $action = 'send_'.$_GET['send']; }
  10. if (!empty($_GET['ip'])) { $ip = $_GET['ip']; }
  11. if (!empty($_GET['mac'])) { $mac = mac_dotted(trim($_GET['mac'])); }
  12. if (!empty($_GET['rec_id'])) { $rec_id = $_GET['id']; }
  13. //POST
  14. if (!empty($_POST['get'])) { $action = 'get_'.$_POST['get']; }
  15. if (!empty($_POST['send'])) { $action = 'send_'.$_POST['send']; }
  16. if (!empty($_POST['ip'])) { $ip = $_POST['ip']; }
  17. if (!empty($_POST['mac'])) { $mac = mac_dotted($_POST['mac']); }
  18. if (!empty($_POST['rec_id'])) { $rec_id = $_POST['id']; }
  19. if (!empty($action)) {
  20. //return user auth record
  21. if ($action ==='get_user_auth') {
  22. $result=[];
  23. $sql='';
  24. if (!empty($ip) and checkValidIp($ip)) {
  25. $ip_aton=ip2long($ip);
  26. $sql = "SELECT * FROM User_auth WHERE `ip_int`=".$ip_aton." AND deleted=0";
  27. }
  28. if (!empty($mac)) { $sql="SELECT * FROM User_auth WHERE `mac`='".$mac."' AND deleted=0"; }
  29. if (!empty($rec_id)) { $sql="SELECT * FROM User_auth WHERE id=".$rec_id; }
  30. if (!empty($sql)) {
  31. $result=get_record_sql($db_link,$sql);
  32. if (!empty($result)) {
  33. try {
  34. $json = json_encode($result, JSON_THROW_ON_ERROR);
  35. echo $json;
  36. }
  37. catch (JsonException $exception) {
  38. exit($exception->getMessage());
  39. }
  40. }
  41. }
  42. }
  43. //add dhcp log record
  44. if ($action ==='send_dhcp') {
  45. if (!empty($ip) and !empty($mac)) {
  46. $dhcp_hostname = '';
  47. if (!empty($_GET["hostname"])) { $dhcp_hostname = trim($_GET["hostname"]); }
  48. if (!empty($_POST["hostname"])) { $dhcp_hostname = trim($_POST["hostname"]); }
  49. $faction = $_GET["action"] * 1;
  50. $dhcp_action = 'add';
  51. if ($faction == 1) { $dhcp_action = 'add'; }
  52. if ($faction == 0) { $dhcp_action = 'del'; }
  53. LOG_VERBOSE($db_link, "external dhcp request for $ip [$mac] $dhcp_action");
  54. if (checkValidIp($ip) and is_our_network($db_link, $ip)) {
  55. $run_cmd = "/opt/Eye/scripts/dnsmasq-hook.sh '".$dhcp_action."' '".$mac."' '".$ip."' '".$dhcp_hostname."'";
  56. $result = shell_exec("/usr/bin/sudo ".escapeshellcmd($run_cmd)." >/dev/null 2>/dev/null &");
  57. LOG_INFO($db_link, "Run command: $run_cmd ");
  58. } else { LOG_ERROR($db_link, "$ip - wrong network!"); }
  59. }
  60. }
  61. }
  62. unset($_GET);
  63. unset($_POST);
  64. ?>