api.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/auth.php");
  3. // Определяем page_url для сессии (можно использовать константу или путь)
  4. $page_url = 'api';
  5. // Получаем параметры через безопасные функции
  6. $action_get = getParam('get', $page_url);
  7. $action_send = getParam('send', $page_url);
  8. $ip = getParam('ip', $page_url, '', FILTER_VALIDATE_IP, ['flags' => FILTER_FLAG_IPV4]);
  9. $mac_raw = getParam('mac', $page_url, '');
  10. $rec_id = getParam('id', $page_url, null, FILTER_VALIDATE_INT);
  11. $f_subnet = getParam('subnet', $page_url, '');
  12. // Обработка MAC-адреса
  13. $mac = !empty($mac_raw) ? mac_dotted(trim($mac_raw)) : '';
  14. // Определяем действие
  15. $action = '';
  16. if (!empty($action_get)) { $action = 'get_' . $action_get; }
  17. if (!empty($action_send)) { $action = 'send_' . $action_send; }
  18. // Дополнительные параметры для send_dhcp
  19. $dhcp_hostname = getParam('hostname', $page_url, '');
  20. $faction_raw = getParam('action', $page_url, 1, FILTER_VALIDATE_INT);
  21. if (!empty($action)) {
  22. // Преобразуем IP в BIGINT (если валиден)
  23. $ip_aton = null;
  24. if ($ip) {
  25. $ip_aton = sprintf('%u', ip2long($ip));
  26. }
  27. // === get_user_auth ===
  28. if ($action === 'get_user_auth') {
  29. LOG_VERBOSE($db_link, "API: Get User Auth record with ip: $ip mac: $mac id: $rec_id");
  30. $result = null;
  31. $sql = "";
  32. $params = [];
  33. if ($rec_id > 0) {
  34. $sql = "SELECT * FROM user_auth WHERE id = ?";
  35. $params = [$rec_id];
  36. } elseif ($ip_aton !== null && !empty($mac)) {
  37. $sql = "SELECT * FROM user_auth WHERE ip_int = ? AND mac = ? AND deleted = 0";
  38. $params = [$ip_aton, $mac];
  39. } elseif ($ip_aton !== null) {
  40. $sql = "SELECT * FROM user_auth WHERE ip_int = ? AND deleted = 0";
  41. $params = [$ip_aton];
  42. } elseif (!empty($mac)) {
  43. $sql = "SELECT * FROM user_auth WHERE mac = ? AND deleted = 0";
  44. $params = [$mac];
  45. }
  46. if ($sql) {
  47. $result = get_record_sql($db_link, $sql, $params);
  48. if ($result) {
  49. LOG_VERBOSE($db_link, "API: Record found.");
  50. header('Content-Type: application/json; charset=utf-8');
  51. echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
  52. } else {
  53. LOG_VERBOSE($db_link, "API: Not found.");
  54. http_response_code(404);
  55. echo json_encode(['error' => 'Not found']);
  56. }
  57. } else {
  58. LOG_VERBOSE($db_link, "API: not enough parameters");
  59. http_response_code(400);
  60. echo json_encode(['error' => 'Missing parameters']);
  61. }
  62. }
  63. // === get_user ===
  64. if ($action === 'get_user') {
  65. LOG_VERBOSE($db_link, "API: Get User record with id: $rec_id");
  66. if ($rec_id > 0) {
  67. $user = get_record_sql($db_link, "SELECT * FROM user_list WHERE id = ?", [$rec_id]);
  68. if ($user) {
  69. $auth_records = get_records_sql($db_link,
  70. "SELECT * FROM user_auth WHERE deleted = 0 AND user_id = ?",
  71. [$rec_id]
  72. );
  73. $user['auth'] = $auth_records ?: [];
  74. LOG_VERBOSE($db_link, "API: User record found.");
  75. header('Content-Type: application/json; charset=utf-8');
  76. echo json_encode($user, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
  77. } else {
  78. LOG_VERBOSE($db_link, "API: User not found.");
  79. http_response_code(404);
  80. echo json_encode(['error' => 'User not found']);
  81. }
  82. } else {
  83. LOG_VERBOSE($db_link, "API: not enough parameters");
  84. http_response_code(400);
  85. echo json_encode(['error' => 'Missing user ID']);
  86. }
  87. }
  88. // === get_dhcp_all ===
  89. if ($action === 'get_dhcp_all') {
  90. LOG_VERBOSE($db_link, "API: Get all dhcp records");
  91. $result = get_records_sql($db_link, "
  92. SELECT
  93. ua.id, ua.ip, ua.ip_int, ua.mac, ua.description,
  94. ua.dns_name, ua.dhcp_option_set, ua.dhcp_acl, ua.ou_id,
  95. SUBSTRING_INDEX(s.subnet, '/', 1) AS subnet_base
  96. FROM user_auth ua
  97. JOIN subnets s ON ua.ip_int BETWEEN s.ip_int_start AND s.ip_int_stop
  98. WHERE ua.dhcp = 1 AND ua.deleted = 0 AND s.dhcp = 1
  99. ORDER BY ua.ip_int
  100. ");
  101. LOG_VERBOSE($db_link, "API: " . count($result) . " records found.");
  102. header('Content-Type: application/json; charset=utf-8');
  103. echo json_encode($result ?: [], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
  104. }
  105. // === get_dhcp_subnet ===
  106. if ($action === 'get_dhcp_subnet' && !empty($f_subnet)) {
  107. // Валидация подсети как IPv4-адреса
  108. if (!filter_var($f_subnet, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  109. http_response_code(400);
  110. echo json_encode(['error' => 'Invalid subnet format']);
  111. exit;
  112. }
  113. LOG_VERBOSE($db_link, "API: Get dhcp records for subnet " . $f_subnet);
  114. $result = get_records_sql($db_link, "
  115. SELECT
  116. ua.id, ua.ip, ua.ip_int, ua.mac, ua.description,
  117. ua.dns_name, ua.dhcp_option_set, ua.dhcp_acl, ua.ou_id,
  118. SUBSTRING_INDEX(s.subnet, '/', 1) AS subnet_base
  119. FROM user_auth ua
  120. 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
  122. AND SUBSTRING_INDEX(s.subnet, '/', 1) = ?
  123. ORDER BY ua.ip_int
  124. ", [$f_subnet]);
  125. LOG_VERBOSE($db_link, "API: " . count($result) . " records found.");
  126. header('Content-Type: application/json; charset=utf-8');
  127. echo json_encode($result ?: [], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
  128. }
  129. // === send_dhcp ===
  130. if ($action === 'send_dhcp') {
  131. if ($ip && $mac) {
  132. $faction = $faction_raw !== null ? (int)$faction_raw : 1;
  133. $dhcp_action = ($faction === 0) ? 'del' : 'add';
  134. LOG_VERBOSE($db_link, "API: external dhcp request for $ip [$mac] $dhcp_action");
  135. if (is_our_network($db_link, $ip)) {
  136. insert_record($db_link, "dhcp_queue", [
  137. 'action' => $dhcp_action,
  138. 'mac' => $mac,
  139. 'ip' => $ip,
  140. 'dhcp_hostname' => $dhcp_hostname
  141. ]);
  142. http_response_code(201);
  143. echo json_encode(['status' => 'queued']);
  144. } else {
  145. LOG_ERROR($db_link, "$ip - wrong network!");
  146. http_response_code(400);
  147. echo json_encode(['error' => 'IP not in allowed network']);
  148. }
  149. } else {
  150. http_response_code(400);
  151. echo json_encode(['error' => 'Missing IP or MAC']);
  152. }
  153. }
  154. } else {
  155. LOG_WARNING($db_link, "API: Unknown request");
  156. http_response_code(400);
  157. echo json_encode(['error' => 'Unknown action']);
  158. }
  159. ob_end_flush();
  160. // Очистка сессии
  161. if (session_status() === PHP_SESSION_ACTIVE) {
  162. $_SESSION = [];
  163. session_destroy();
  164. }
  165. ?>