1
0

api.php 3.5 KB

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