1
0

api.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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['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['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. //api.php?login=<LOGIN>&api_key=<API_KEY>&get=user_auth&{mac=<MAC>|ip=<IP>}
  28. if ($action ==='get_user_auth') {
  29. $result=[];
  30. $sql='';
  31. LOG_VERBOSE($db_link,"API: Get User Auth record with ip: $ip mac: $mac id: $rec_id");
  32. if (!empty($mac) and !empty($ip_aton)) {
  33. $sql="SELECT * FROM User_auth WHERE `ip_int`=".$ip_aton." AND `mac`='".$mac."' AND deleted=0";
  34. } else {
  35. if (!empty($ip_aton)) { $sql = "SELECT * FROM User_auth WHERE `ip_int`=".$ip_aton." AND deleted=0"; }
  36. if (!empty($mac)) { $sql="SELECT * FROM User_auth WHERE `mac`='".$mac."' AND deleted=0"; }
  37. }
  38. if (!empty($rec_id)) { $sql="SELECT * FROM User_auth WHERE id=".$rec_id; }
  39. if (!empty($sql)) {
  40. $result=get_record_sql($db_link,$sql);
  41. if (!empty($result)) {
  42. LOG_VERBOSE($db_link,"API: Record found.");
  43. try {
  44. $json = json_encode($result, JSON_THROW_ON_ERROR);
  45. header('Content-Type: application/json');
  46. echo $json;
  47. }
  48. catch (JsonException $exception) {
  49. LOG_ERROR($db_link,"API: Error decoding JSON. Error: ".$exception->getMessage());
  50. exit($exception->getMessage());
  51. }
  52. } else {
  53. LOG_VERBOSE($db_link,"API: Not found.");
  54. }
  55. } else {
  56. LOG_VERBOSE($db_link,"API: not enough parameters");
  57. }
  58. }
  59. //return user records
  60. //api.php?login=<LOGIN>&api_key=<API_KEY>&get=user&id=<ID>
  61. if ($action ==='get_user') {
  62. $result=[];
  63. $sql='';
  64. LOG_VERBOSE($db_link,"API: Get User record with id: $rec_id");
  65. if (!empty($rec_id)) {
  66. $sql="SELECT * FROM User_list WHERE id=$rec_id";
  67. $result=get_record_sql($db_link,$sql);
  68. if (!empty($result)) {
  69. LOG_VERBOSE($db_link,"API: User record found.");
  70. $sql="SELECT * FROM User_auth WHERE deleted=0 AND user_id=".$rec_id;
  71. $result_auth=get_records_sql($db_link,$sql);
  72. try {
  73. if (!empty($result_auth)) { $result["auth"]=$result_auth; } else { $result["auth"]=''; }
  74. $json_user = json_encode($result, JSON_THROW_ON_ERROR);
  75. header('Content-Type: application/json');
  76. echo $json_user;
  77. }
  78. catch (JsonException $exception) {
  79. LOG_ERROR($db_link,"API: Error decoding JSON. Error: ".$exception->getMessage());
  80. exit($exception->getMessage());
  81. }
  82. } else {
  83. LOG_VERBOSE($db_link,"API: User not found.");
  84. }
  85. } else {
  86. LOG_VERBOSE($db_link,"API: not enough parameters");
  87. }
  88. }
  89. //return all records for dhcp server
  90. //api.php?login=<LOGIN>&api_key=<API_KEY>&get=dhcp_all
  91. if ($action ==='get_dhcp_all') {
  92. $result=[];
  93. LOG_VERBOSE($db_link,"API: Get all dhcp records");
  94. $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
  95. FROM User_auth ua JOIN subnets s ON ua.ip_int BETWEEN s.ip_int_start AND s.ip_int_stop
  96. WHERE ua.dhcp = 1 AND ua.deleted = 0 AND s.dhcp = 1 ORDER BY ua.ip_int";
  97. $result = get_records_sql($db_link, $sql);
  98. if (!empty($result)) {
  99. LOG_VERBOSE($db_link, "API: " . count($result) . " records found.");
  100. try {
  101. header('Content-Type: application/json');
  102. echo json_encode($result, JSON_THROW_ON_ERROR);
  103. } catch (JsonException $exception) {
  104. LOG_ERROR($db_link, "API: JSON encoding error: " . $exception->getMessage());
  105. exit("JSON error");
  106. }
  107. } else {
  108. LOG_VERBOSE($db_link, "API: No records found.");
  109. header('Content-Type: application/json');
  110. echo json_encode([]);
  111. }
  112. }
  113. //return all record in subnet for dhcp-server
  114. //api.php?login=<LOGIN>&api_key=<API_KEY>&get=dhcp_subnet&subnet=<SUBNET>
  115. if ($action ==='get_dhcp_subnet' and !empty($f_subnet)) {
  116. $result=[];
  117. $f_subnet = trim($f_subnet, "'");
  118. LOG_VERBOSE($db_link,"API: Get dhcp records for subnet ".$f_subnet);
  119. $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
  120. FROM User_auth ua JOIN subnets s ON ua.ip_int BETWEEN s.ip_int_start AND s.ip_int_stop
  121. 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";
  122. $result = get_records_sql($db_link, $sql);
  123. if (!empty($result)) {
  124. LOG_VERBOSE($db_link, "API: " . count($result) . " records found.");
  125. try {
  126. header('Content-Type: application/json');
  127. echo json_encode($result, JSON_THROW_ON_ERROR);
  128. } catch (JsonException $exception) {
  129. LOG_ERROR($db_link, "API: JSON encoding error: " . $exception->getMessage());
  130. exit("JSON error");
  131. }
  132. } else {
  133. LOG_VERBOSE($db_link, "API: No records found.");
  134. header('Content-Type: application/json');
  135. echo json_encode([]);
  136. }
  137. }
  138. //add dhcp log record
  139. //api.php?login=<LOGIN>&api_key=<API_KEY>&send=dhcp&mac=<MAC>&ip=<IP>&action=<0|1>[&hostname=<HOSTNAME>]
  140. if ($action ==='send_dhcp') {
  141. if (!empty($ip) and !empty($mac)) {
  142. $dhcp_hostname = '';
  143. if (!empty($_GET["hostname"])) { $dhcp_hostname = trim($_GET["hostname"]); }
  144. if (!empty($_POST["hostname"])) { $dhcp_hostname = trim($_POST["hostname"]); }
  145. $faction = $_GET["action"] * 1;
  146. $dhcp_action = 'add';
  147. if ($faction == 1) { $dhcp_action = 'add'; }
  148. if ($faction == 0) { $dhcp_action = 'del'; }
  149. LOG_VERBOSE($db_link, "API: external dhcp request for $ip [$mac] $dhcp_action");
  150. if (checkValidIp($ip) and is_our_network($db_link, $ip)) {
  151. $new['action']=$dhcp_action;
  152. $new['mac']=$mac;
  153. $new['ip']=$ip;
  154. $new['dhcp_hostname']=$dhcp_hostname;
  155. insert_record($db_link,"dhcp_queue",$new);
  156. } else { LOG_ERROR($db_link, "$ip - wrong network!"); }
  157. }
  158. }
  159. } else {
  160. LOG_WARNING($db_link,"API: Unknown request");
  161. }
  162. unset($_GET);
  163. unset($_POST);
  164. logout($db_link,TRUE);
  165. ?>