HttpUtils.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * GET an HTTP URL to retrieve its content
  4. *
  5. * @param string $url URL to get (http://...)
  6. * @param int $timeout network timeout (in seconds)
  7. * @param int $maxBytes maximum downloaded bytes (default: 4 MiB)
  8. *
  9. * @return array HTTP response headers, downloaded content
  10. *
  11. * Output format:
  12. * [0] = associative array containing HTTP response headers
  13. * [1] = URL content (downloaded data)
  14. *
  15. * Example:
  16. * list($headers, $data) = get_http_response('http://sebauvage.net/');
  17. * if (strpos($headers[0], '200 OK') !== false) {
  18. * echo 'Data type: '.htmlspecialchars($headers['Content-Type']);
  19. * } else {
  20. * echo 'There was an error: '.htmlspecialchars($headers[0]);
  21. * }
  22. *
  23. * @see http://php.net/manual/en/function.file-get-contents.php
  24. * @see http://php.net/manual/en/function.stream-context-create.php
  25. * @see http://php.net/manual/en/function.get-headers.php
  26. */
  27. function get_http_response($url, $timeout = 30, $maxBytes = 4194304)
  28. {
  29. $urlObj = new Url($url);
  30. if (! filter_var($url, FILTER_VALIDATE_URL) || ! $urlObj->isHttp()) {
  31. return array(array(0 => 'Invalid HTTP Url'), false);
  32. }
  33. $options = array(
  34. 'http' => array(
  35. 'method' => 'GET',
  36. 'timeout' => $timeout,
  37. 'user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:23.0)'
  38. .' Gecko/20100101 Firefox/23.0',
  39. 'request_fulluri' => true,
  40. )
  41. );
  42. $context = stream_context_create($options);
  43. stream_context_set_default($options);
  44. list($headers, $finalUrl) = get_redirected_headers($urlObj->cleanup());
  45. if (! $headers || strpos($headers[0], '200 OK') === false) {
  46. return array($headers, false);
  47. }
  48. try {
  49. // TODO: catch Exception in calling code (thumbnailer)
  50. $content = file_get_contents($finalUrl, false, $context, -1, $maxBytes);
  51. } catch (Exception $exc) {
  52. return array(array(0 => 'HTTP Error'), $exc->getMessage());
  53. }
  54. return array($headers, $content);
  55. }
  56. /**
  57. * Retrieve HTTP headers, following n redirections (temporary and permanent).
  58. *
  59. * @param string $url initial URL to reach.
  60. * @param int $redirectionLimit max redirection follow..
  61. *
  62. * @return array
  63. */
  64. function get_redirected_headers($url, $redirectionLimit = 3)
  65. {
  66. $headers = get_headers($url, 1);
  67. // Headers found, redirection found, and limit not reached.
  68. if ($redirectionLimit-- > 0
  69. && !empty($headers)
  70. && (strpos($headers[0], '301') !== false || strpos($headers[0], '302') !== false)
  71. && !empty($headers['Location'])) {
  72. $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location'];
  73. if ($redirection != $url) {
  74. return get_redirected_headers($redirection, $redirectionLimit);
  75. }
  76. }
  77. return array($headers, $url);
  78. }
  79. /**
  80. * Returns the server's base URL: scheme://domain.tld[:port]
  81. *
  82. * @param array $server the $_SERVER array
  83. *
  84. * @return string the server's base URL
  85. *
  86. * @see http://www.ietf.org/rfc/rfc7239.txt
  87. * @see http://www.ietf.org/rfc/rfc6648.txt
  88. * @see http://stackoverflow.com/a/3561399
  89. * @see http://stackoverflow.com/q/452375
  90. */
  91. function server_url($server)
  92. {
  93. $scheme = 'http';
  94. $port = '';
  95. // Shaarli is served behind a proxy
  96. if (isset($server['HTTP_X_FORWARDED_PROTO'])) {
  97. // Keep forwarded scheme
  98. $scheme = $server['HTTP_X_FORWARDED_PROTO'];
  99. if (isset($server['HTTP_X_FORWARDED_PORT'])) {
  100. // Keep forwarded port
  101. $port = ':'.$server['HTTP_X_FORWARDED_PORT'];
  102. }
  103. return $scheme.'://'.$server['SERVER_NAME'].$port;
  104. }
  105. // SSL detection
  106. if ((! empty($server['HTTPS']) && strtolower($server['HTTPS']) == 'on')
  107. || (isset($server['SERVER_PORT']) && $server['SERVER_PORT'] == '443')) {
  108. $scheme = 'https';
  109. }
  110. // Do not append standard port values
  111. if (($scheme == 'http' && $server['SERVER_PORT'] != '80')
  112. || ($scheme == 'https' && $server['SERVER_PORT'] != '443')) {
  113. $port = ':'.$server['SERVER_PORT'];
  114. }
  115. return $scheme.'://'.$server['SERVER_NAME'].$port;
  116. }
  117. /**
  118. * Returns the absolute URL of the current script, without the query
  119. *
  120. * If the resource is "index.php", then it is removed (for better-looking URLs)
  121. *
  122. * @param array $server the $_SERVER array
  123. *
  124. * @return string the absolute URL of the current script, without the query
  125. */
  126. function index_url($server)
  127. {
  128. $scriptname = $server['SCRIPT_NAME'];
  129. if (endswith($scriptname, 'index.php')) {
  130. $scriptname = substr($scriptname, 0, -9);
  131. }
  132. return server_url($server) . $scriptname;
  133. }
  134. /**
  135. * Returns the absolute URL of the current script, with the query
  136. *
  137. * If the resource is "index.php", then it is removed (for better-looking URLs)
  138. *
  139. * @param array $server the $_SERVER array
  140. *
  141. * @return string the absolute URL of the current script, with the query
  142. */
  143. function page_url($server)
  144. {
  145. if (! empty($server['QUERY_STRING'])) {
  146. return index_url($server).'?'.$server['QUERY_STRING'];
  147. }
  148. return index_url($server);
  149. }