api.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/auth.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. header('Content-Type: application/json');
  41. echo $json;
  42. }
  43. catch (JsonException $exception) {
  44. LOG_ERROR($db_link,"API: Error decoding JSON. Error: ".$exception->getMessage());
  45. exit($exception->getMessage());
  46. }
  47. } else {
  48. LOG_VERBOSE($db_link,"API: Not found.");
  49. }
  50. } else {
  51. LOG_VERBOSE($db_link,"API: not enough parameters");
  52. }
  53. }
  54. //return user auth record
  55. if ($action ==='get_dhcp_all') {
  56. $result=[];
  57. LOG_VERBOSE($db_link,"API: Get all dhcp records");
  58. $sql = "SELECT id, ip, ip_int, mac, comments, dns_name, dhcp_option_set, dhcp_acl, ou_id
  59. FROM User_auth
  60. WHERE dhcp=1 AND deleted=0
  61. ORDER BY ip_int";
  62. $result = get_records_sql($db_link, $sql);
  63. if (!empty($result)) {
  64. LOG_VERBOSE($db_link, "API: " . count($result) . " records found.");
  65. try {
  66. header('Content-Type: application/json');
  67. echo json_encode($result, JSON_THROW_ON_ERROR);
  68. } catch (JsonException $exception) {
  69. LOG_ERROR($db_link, "API: JSON encoding error: " . $exception->getMessage());
  70. exit("JSON error");
  71. }
  72. } else {
  73. LOG_VERBOSE($db_link, "API: No records found.");
  74. header('Content-Type: application/json');
  75. echo json_encode([]);
  76. }
  77. }
  78. //add dhcp log record
  79. if ($action ==='send_dhcp') {
  80. if (!empty($ip) and !empty($mac)) {
  81. $dhcp_hostname = '';
  82. if (!empty($_GET["hostname"])) { $dhcp_hostname = trim($_GET["hostname"]); }
  83. if (!empty($_POST["hostname"])) { $dhcp_hostname = trim($_POST["hostname"]); }
  84. $faction = $_GET["action"] * 1;
  85. $dhcp_action = 'add';
  86. if ($faction == 1) { $dhcp_action = 'add'; }
  87. if ($faction == 0) { $dhcp_action = 'del'; }
  88. LOG_VERBOSE($db_link, "API: external dhcp request for $ip [$mac] $dhcp_action");
  89. if (checkValidIp($ip) and is_our_network($db_link, $ip)) {
  90. $new['action']=$dhcp_action;
  91. $new['mac']=$mac;
  92. $new['ip']=$ip;
  93. $new['dhcp_hostname']=$dhcp_hostname;
  94. insert_record($db_link,"dhcp_queue",$new);
  95. } else { LOG_ERROR($db_link, "$ip - wrong network!"); }
  96. }
  97. }
  98. } else {
  99. LOG_WARNING($db_link,"API: Unknown request");
  100. }
  101. unset($_GET);
  102. unset($_POST);
  103. logout($db_link,TRUE);
  104. ?>