index.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. // FIXME: REPLACE \/ here
  3. define('MY_GNUSOCIAL', 'https://www.example.org/gnusocial/index.php');
  4. /**
  5. * This is a general solution for when you can't have your GNU social instance in the domain root and for when you want to
  6. * socialfy from another domain.
  7. */
  8. // From https://www.php.net/manual/en/function.getallheaders.php#84262 (joyview at gmail dot com)
  9. if (!function_exists('getallheaders')) {
  10. function getallheaders()
  11. {
  12. $headers = [];
  13. foreach ($_SERVER as $name => $value) {
  14. if (substr($name, 0, 5) == 'HTTP_') {
  15. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
  16. }
  17. }
  18. return $headers;
  19. }
  20. }
  21. $ch = curl_init();
  22. curl_setopt($ch, CURLOPT_HTTPHEADER, getallheaders());
  23. curl_setopt($ch, CURLOPT_URL, MY_GNUSOCIAL . str_replace('webfinger/', 'webfinger', $_SERVER['REQUEST_URI']));
  24. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  25. curl_setopt($ch, CURLOPT_HEADER, true);
  26. $response = curl_exec($ch);
  27. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  28. $body = substr($response, $header_size);
  29. // From https://stackoverflow.com/a/10590242 (c.hill)
  30. function get_headers_from_curl_response($response)
  31. {
  32. $headers = array();
  33. $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
  34. foreach (explode("\r\n", $header_text) as $i => $line) {
  35. if ($i === 0) {
  36. $headers['http_code'] = $line;
  37. } else {
  38. list($key, $value) = explode(': ', $line);
  39. $headers[$key] = $value;
  40. }
  41. }
  42. return $headers;
  43. }
  44. $headers = get_headers_from_curl_response($response);
  45. foreach ($headers as $name => $value) {
  46. header("{$name}: $value");
  47. }
  48. echo $body;
  49. curl_close($ch);