WebOrbiton
v3.0.0.2

FlatlyPage

47 lines · 1.3 KB
  1. <?php
  2. header('Content-Type: application/json; charset=utf-8');
  3. if (isset($_SERVER['HTTP_ORIGIN'])) {
  4. $originHost = parse_url($_SERVER['HTTP_ORIGIN'], PHP_URL_HOST);
  5. $serverHost = $_SERVER['HTTP_HOST'];
  6. $serverParts = explode('.', $serverHost);
  7. $baseDomain = implode('.', array_slice($serverParts, -2));
  8. if (
  9. $originHost === $baseDomain ||
  10. str_ends_with($originHost, '.' . $baseDomain)
  11. ) {
  12. header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
  13. header('Access-Control-Allow-Credentials: true');
  14. header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
  15. header('Access-Control-Allow-Headers: Content-Type, Authorization');
  16. }
  17. }
  18. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  19. http_response_code(200);
  20. exit;
  21. }
  22. $xmlPath = __DIR__ . '/../data/translate-api.xml';
  23. if (!file_exists($xmlPath)) {
  24. http_response_code(404);
  25. die(json_encode(['error' => 'Config not found']));
  26. }
  27. $xml = simplexml_load_file($xmlPath);
  28. $apis = [];
  29. foreach ($xml->apis->api as $api) {
  30. $id = (string)$api->id;
  31. $apis[$id] = [
  32. 'name' => (string)$api->name,
  33. 'free' => (string)$api->free === 'true',
  34. 'rateLimit' => (string)$api->rateLimit ?? ''
  35. ];
  36. }
  37. echo json_encode($apis);