HttpFunctions.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @defgroup HTTP HTTP
  4. * @file
  5. * @ingroup HTTP
  6. */
  7. /**
  8. * Various HTTP related functions
  9. * @ingroup HTTP
  10. */
  11. class Http {
  12. /**
  13. * Simple wrapper for Http::request( 'GET' )
  14. * @see Http::request()
  15. */
  16. public static function get( $url, $timeout = 'default', $opts = array() ) {
  17. return Http::request( "GET", $url, $timeout, $opts );
  18. }
  19. /**
  20. * Simple wrapper for Http::request( 'POST' )
  21. * @see Http::request()
  22. */
  23. public static function post( $url, $timeout = 'default', $opts = array() ) {
  24. return Http::request( "POST", $url, $timeout, $opts );
  25. }
  26. /**
  27. * Get the contents of a file by HTTP
  28. * @param $method string HTTP method. Usually GET/POST
  29. * @param $url string Full URL to act on
  30. * @param $timeout int Seconds to timeout. 'default' falls to $wgHTTPTimeout
  31. * @param $curlOptions array Optional array of extra params to pass
  32. * to curl_setopt()
  33. */
  34. public static function request( $method, $url, $timeout = 'default', $curlOptions = array() ) {
  35. global $wgHTTPTimeout, $wgHTTPProxy, $wgTitle;
  36. // Go ahead and set the timeout if not otherwise specified
  37. if ( $timeout == 'default' ) {
  38. $timeout = $wgHTTPTimeout;
  39. }
  40. wfDebug( __METHOD__ . ": $method $url\n" );
  41. # Use curl if available
  42. if ( function_exists( 'curl_init' ) ) {
  43. $c = curl_init( $url );
  44. if ( self::isLocalURL( $url ) ) {
  45. curl_setopt( $c, CURLOPT_PROXY, 'localhost:80' );
  46. } else if ($wgHTTPProxy) {
  47. curl_setopt($c, CURLOPT_PROXY, $wgHTTPProxy);
  48. }
  49. curl_setopt( $c, CURLOPT_TIMEOUT, $timeout );
  50. curl_setopt( $c, CURLOPT_USERAGENT, self :: userAgent() );
  51. if ( $method == 'POST' ) {
  52. curl_setopt( $c, CURLOPT_POST, true );
  53. curl_setopt( $c, CURLOPT_POSTFIELDS, '' );
  54. }
  55. else
  56. curl_setopt( $c, CURLOPT_CUSTOMREQUEST, $method );
  57. # Set the referer to $wgTitle, even in command-line mode
  58. # This is useful for interwiki transclusion, where the foreign
  59. # server wants to know what the referring page is.
  60. # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
  61. # referring page.
  62. if ( is_object( $wgTitle ) ) {
  63. curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() );
  64. }
  65. if ( is_array( $curlOptions ) ) {
  66. foreach( $curlOptions as $option => $value ) {
  67. curl_setopt( $c, $option, $value );
  68. }
  69. }
  70. ob_start();
  71. curl_exec( $c );
  72. $text = ob_get_contents();
  73. ob_end_clean();
  74. # Don't return the text of error messages, return false on error
  75. $retcode = curl_getinfo( $c, CURLINFO_HTTP_CODE );
  76. if ( $retcode != 200 ) {
  77. wfDebug( __METHOD__ . ": HTTP return code $retcode\n" );
  78. $text = false;
  79. }
  80. # Don't return truncated output
  81. $errno = curl_errno( $c );
  82. if ( $errno != CURLE_OK ) {
  83. $errstr = curl_error( $c );
  84. wfDebug( __METHOD__ . ": CURL error code $errno: $errstr\n" );
  85. $text = false;
  86. }
  87. curl_close( $c );
  88. } else {
  89. # Otherwise use file_get_contents...
  90. # This doesn't have local fetch capabilities...
  91. $headers = array( "User-Agent: " . self :: userAgent() );
  92. if( strcasecmp( $method, 'post' ) == 0 ) {
  93. // Required for HTTP 1.0 POSTs
  94. $headers[] = "Content-Length: 0";
  95. }
  96. $opts = array(
  97. 'http' => array(
  98. 'method' => $method,
  99. 'header' => implode( "\r\n", $headers ),
  100. 'timeout' => $timeout ) );
  101. $ctx = stream_context_create($opts);
  102. $text = file_get_contents( $url, false, $ctx );
  103. }
  104. return $text;
  105. }
  106. /**
  107. * Check if the URL can be served by localhost
  108. * @param $url string Full url to check
  109. * @return bool
  110. */
  111. public static function isLocalURL( $url ) {
  112. global $wgCommandLineMode, $wgConf;
  113. if ( $wgCommandLineMode ) {
  114. return false;
  115. }
  116. // Extract host part
  117. $matches = array();
  118. if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
  119. $host = $matches[1];
  120. // Split up dotwise
  121. $domainParts = explode( '.', $host );
  122. // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
  123. $domainParts = array_reverse( $domainParts );
  124. for ( $i = 0; $i < count( $domainParts ); $i++ ) {
  125. $domainPart = $domainParts[$i];
  126. if ( $i == 0 ) {
  127. $domain = $domainPart;
  128. } else {
  129. $domain = $domainPart . '.' . $domain;
  130. }
  131. if ( $wgConf->isLocalVHost( $domain ) ) {
  132. return true;
  133. }
  134. }
  135. }
  136. return false;
  137. }
  138. /**
  139. * Return a standard user-agent we can use for external requests.
  140. */
  141. public static function userAgent() {
  142. global $wgVersion;
  143. return "MediaWiki/$wgVersion";
  144. }
  145. }