1
0

api.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. $f_subnet='';
  9. //MODE
  10. if (!empty($_GET['get'])) { $action = 'get_'.$_GET['get']; }
  11. if (!empty($_GET['send'])) { $action = 'send_'.$_GET['send']; }
  12. if (!empty($_POST['get'])) { $action = 'get_'.$_POST['get']; }
  13. if (!empty($_POST['send'])) { $action = 'send_'.$_POST['send']; }
  14. //GET
  15. if (!empty($_GET['ip'])) { $ip = $_GET['ip']; }
  16. if (!empty($_GET['mac'])) { $mac = mac_dotted(trim($_GET['mac'])); }
  17. if (!empty($_GET['rec_id'])) { $rec_id = $_GET['id']; }
  18. if (!empty($_GET['subnet'])) { $f_subnet = $_GET['subnet']; }
  19. //POST
  20. if (!empty($_POST['ip'])) { $ip = $_POST['ip']; }
  21. if (!empty($_POST['mac'])) { $mac = mac_dotted($_POST['mac']); }
  22. if (!empty($_POST['rec_id'])) { $rec_id = $_POST['id']; }
  23. if (!empty($_POST['subnet'])) { $f_subnet = $_POST['subnet']; }
  24. if (!empty($action)) {
  25. if (!empty($ip) and checkValidIp($ip)) { $ip_aton=ip2long($ip); }
  26. //return user auth record
  27. if ($action ==='get_user_auth') {
  28. $result=[];
  29. $sql='';
  30. LOG_VERBOSE($db_link,"API: Get User Auth record with ip: $ip mac: $mac id: $rec_id");
  31. if (!empty($mac) and !empty($ip_aton)) {
  32. $sql="SELECT * FROM User_auth WHERE `ip_int`=".$ip_aton." AND `mac`='".$mac."' AND deleted=0";
  33. } else {
  34. if (!empty($ip_aton)) { $sql = "SELECT * FROM User_auth WHERE `ip_int`=".$ip_aton." AND deleted=0"; }
  35. if (!empty($mac)) { $sql="SELECT * FROM User_auth WHERE `mac`='".$mac."' AND deleted=0"; }
  36. }
  37. if (!empty($rec_id)) { $sql="SELECT * FROM User_auth WHERE id=".$rec_id; }
  38. if (!empty($sql)) {
  39. $result=get_record_sql($db_link,$sql);
  40. if (!empty($result)) {
  41. LOG_VERBOSE($db_link,"API: Record found.");
  42. try {
  43. $json = json_encode($result, JSON_THROW_ON_ERROR);
  44. header('Content-Type: application/json');
  45. echo $json;
  46. }
  47. catch (JsonException $exception) {
  48. LOG_ERROR($db_link,"API: Error decoding JSON. Error: ".$exception->getMessage());
  49. exit($exception->getMessage());
  50. }
  51. } else {
  52. LOG_VERBOSE($db_link,"API: Not found.");
  53. }
  54. } else {
  55. LOG_VERBOSE($db_link,"API: not enough parameters");
  56. }
  57. }
  58. //return user auth record
  59. if ($action ==='get_dhcp_all') {
  60. $result=[];
  61. LOG_VERBOSE($db_link,"API: Get all dhcp records");
  62. $sql = "SELECT ua.id, ua.ip, ua.ip_int, ua.mac, ua.comments, ua.dns_name, ua.dhcp_option_set, ua.dhcp_acl, ua.ou_id, SUBSTRING_INDEX(s.subnet, '/', 1) AS subnet_base
  63. FROM User_auth ua JOIN subnets s ON ua.ip_int BETWEEN s.ip_int_start AND s.ip_int_stop
  64. WHERE ua.dhcp = 1 AND ua.deleted = 0 AND s.dhcp = 1 ORDER BY ua.ip_int";
  65. $result = get_records_sql($db_link, $sql);
  66. if (!empty($result)) {
  67. LOG_VERBOSE($db_link, "API: " . count($result) . " records found.");
  68. try {
  69. header('Content-Type: application/json');
  70. echo json_encode($result, JSON_THROW_ON_ERROR);
  71. } catch (JsonException $exception) {
  72. LOG_ERROR($db_link, "API: JSON encoding error: " . $exception->getMessage());
  73. exit("JSON error");
  74. }
  75. } else {
  76. LOG_VERBOSE($db_link, "API: No records found.");
  77. header('Content-Type: application/json');
  78. echo json_encode([]);
  79. }
  80. }
  81. //return user auth record
  82. if ($action ==='get_dhcp_subnet' and !empty($f_subnet)) {
  83. $result=[];
  84. $f_subnet = trim($f_subnet, "'");
  85. LOG_VERBOSE($db_link,"API: Get dhcp records for subnet ".$f_subnet);
  86. $sql = "SELECT ua.id, ua.ip, ua.ip_int, ua.mac, ua.comments, ua.dns_name, ua.dhcp_option_set, ua.dhcp_acl, ua.ou_id, SUBSTRING_INDEX(s.subnet, '/', 1) AS subnet_base
  87. FROM User_auth ua JOIN subnets s ON ua.ip_int BETWEEN s.ip_int_start AND s.ip_int_stop
  88. WHERE ua.dhcp = 1 AND ua.deleted = 0 AND s.dhcp = 1 AND SUBSTRING_INDEX(s.subnet, '/', 1) = '".$f_subnet."' ORDER BY ua.ip_int";
  89. $result = get_records_sql($db_link, $sql);
  90. if (!empty($result)) {
  91. LOG_VERBOSE($db_link, "API: " . count($result) . " records found.");
  92. try {
  93. header('Content-Type: application/json');
  94. echo json_encode($result, JSON_THROW_ON_ERROR);
  95. } catch (JsonException $exception) {
  96. LOG_ERROR($db_link, "API: JSON encoding error: " . $exception->getMessage());
  97. exit("JSON error");
  98. }
  99. } else {
  100. LOG_VERBOSE($db_link, "API: No records found.");
  101. header('Content-Type: application/json');
  102. echo json_encode([]);
  103. }
  104. }
  105. //add dhcp log record
  106. if ($action ==='send_dhcp') {
  107. if (!empty($ip) and !empty($mac)) {
  108. $dhcp_hostname = '';
  109. if (!empty($_GET["hostname"])) { $dhcp_hostname = trim($_GET["hostname"]); }
  110. if (!empty($_POST["hostname"])) { $dhcp_hostname = trim($_POST["hostname"]); }
  111. $faction = $_GET["action"] * 1;
  112. $dhcp_action = 'add';
  113. if ($faction == 1) { $dhcp_action = 'add'; }
  114. if ($faction == 0) { $dhcp_action = 'del'; }
  115. LOG_VERBOSE($db_link, "API: external dhcp request for $ip [$mac] $dhcp_action");
  116. if (checkValidIp($ip) and is_our_network($db_link, $ip)) {
  117. $new['action']=$dhcp_action;
  118. $new['mac']=$mac;
  119. $new['ip']=$ip;
  120. $new['dhcp_hostname']=$dhcp_hostname;
  121. insert_record($db_link,"dhcp_queue",$new);
  122. } else { LOG_ERROR($db_link, "$ip - wrong network!"); }
  123. }
  124. }
  125. } else {
  126. LOG_WARNING($db_link,"API: Unknown request");
  127. }
  128. unset($_GET);
  129. unset($_POST);
  130. logout($db_link,TRUE);
  131. ?>