opensearch.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. header("Content-Type: application/xml");
  3. include "data/config.php";
  4. $host = strtolower(explode(":", $_SERVER["HTTP_HOST"], 2)[0]);
  5. // detect if 4get should serve the HTTPS endpoint
  6. if(
  7. preg_match(
  8. '/\.(onion|i2p)$/',
  9. strtolower($host),
  10. $match
  11. )
  12. ){
  13. $ssl = false;
  14. $src = $match[1];
  15. }elseif(
  16. is_string(
  17. filter_var(
  18. $host,
  19. FILTER_VALIDATE_IP
  20. )
  21. ) ||
  22. $host == "localhost"
  23. ){
  24. $ssl = false;
  25. $src = "localhost";
  26. }else{
  27. $ssl = true;
  28. }
  29. $domain = ($ssl ? "https" : "http") . "://" . $_SERVER["HTTP_HOST"];
  30. echo
  31. '<?xml version="1.0" encoding="UTF-8"?>' .
  32. '<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">' .
  33. '<ShortName>' . htmlspecialchars(config::SERVER_NAME) . ($ssl === false ? " ({$src})" : "") . '</ShortName>' .
  34. '<Description>' . htmlspecialchars(config::SERVER_SHORT_DESCRIPTION) . '</Description>' .
  35. '<InputEncoding>UTF-8</InputEncoding>' .
  36. '<Image width="16" height="16" type="image/x-icon">' . htmlspecialchars($domain) . '/favicon.ico</Image>' .
  37. '<Url type="text/html" method="GET" template="' . htmlspecialchars($domain) . '/web?s={searchTerms}"/>';
  38. if(
  39. isset($_GET["ac"]) &&
  40. is_string($_GET["ac"]) &&
  41. $_GET["ac"] != "disabled"
  42. ){
  43. echo '<Url type="application/x-suggestions+json" template="' . $domain . '/api/v1/ac?s={searchTerms}&amp;scraper=' . htmlspecialchars($_GET["ac"]) . '"/>';
  44. }
  45. echo '</OpenSearchDescription>';