support_functions.inc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Functions to support the documentation and examples.
  6. *
  7. */
  8. /**
  9. * Computes base root, base path, and base url.
  10. *
  11. * This code is adapted from Drupal function conf_init, see:
  12. * http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/conf_init/6
  13. *
  14. */
  15. function htmltodocx_paths() {
  16. if (!isset($_SERVER['SERVER_PROTOCOL']) || ($_SERVER['SERVER_PROTOCOL'] != 'HTTP/1.0' && $_SERVER['SERVER_PROTOCOL'] != 'HTTP/1.1')) {
  17. $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
  18. }
  19. if (isset($_SERVER['HTTP_HOST'])) {
  20. // As HTTP_HOST is user input, ensure it only contains characters allowed
  21. // in hostnames. See RFC 952 (and RFC 2181).
  22. // $_SERVER['HTTP_HOST'] is lowercased here per specifications.
  23. $_SERVER['HTTP_HOST'] = strtolower($_SERVER['HTTP_HOST']);
  24. if (!htmltodocx_valid_http_host($_SERVER['HTTP_HOST'])) {
  25. // HTTP_HOST is invalid, e.g. if containing slashes it may be an attack.
  26. header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
  27. exit;
  28. }
  29. }
  30. else {
  31. // Some pre-HTTP/1.1 clients will not send a Host header. Ensure the key is
  32. // defined for E_ALL compliance.
  33. $_SERVER['HTTP_HOST'] = '';
  34. }
  35. // Create base URL
  36. $base_root = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
  37. $base_url = $base_root .= '://' . $_SERVER['HTTP_HOST'];
  38. // $_SERVER['SCRIPT_NAME'] can, in contrast to $_SERVER['PHP_SELF'], not
  39. // be modified by a visitor.
  40. if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) {
  41. $base_path = "/$dir";
  42. $base_url .= $base_path;
  43. $base_path .= '/';
  44. }
  45. else {
  46. $base_path = '/';
  47. }
  48. return array(
  49. 'base_path' => $base_path,
  50. 'base_url' => $base_url,
  51. 'base_root' => $base_root,
  52. );
  53. }
  54. /**
  55. * Check for valid http host.
  56. *
  57. * This code is adapted from function drupal_valid_http_host, see:
  58. * http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_valid_http_host/6
  59. *
  60. * @param mixed $host
  61. * @return int
  62. */
  63. function htmltodocx_valid_http_host($host) {
  64. return preg_match('/^\[?(?:[a-z0-9-:\]_]+\.?)+$/', $host);
  65. }