- * @param $params Array Parameters to thumb.php
- * @param $uri String Thumbnail request URI
- * @return void
- */
-function wfStreamThumbViaCurl( array $params, $uri ) {
- global $thgThumbCallbacks, $thgThumbCurlConfig;
-
- if ( !extension_loaded( 'curl' ) ) {
- die( "cURL is not enabled for PHP on this wiki.\n" ); // sanity
- }
-
- # Build up the request URL to use with CURL...
- $reqURL = $thgThumbCurlConfig['url'] . '?';
- $first = true;
- foreach ( $params as $name => $value ) {
- if ( $first ) {
- $first = false;
- } else {
- $reqURL .= '&';
- }
- $reqURL .= "$name=$value"; // Note: value is already urlencoded
- }
-
- # Set relevant HTTP headers...
- $headers = array();
- $headers[] = "X-Original-URI: " . str_replace( "\n", '', $uri );
- if ( isset( $thgThumbCallbacks['curlHeaders'] )
- && is_callable( $thgThumbCallbacks['curlHeaders'] ) )
- {
- # Add on any custom headers (like XFF)
- call_user_func_array( $thgThumbCallbacks['curlHeaders'], array( &$headers ) );
- }
-
- # Pass through some other headers...
- $passThrough = array( 'If-Modified-Since', 'Referer', 'User-Agent' );
- foreach ( $passThrough as $headerName ) {
- $serverVarName = 'HTTP_' . str_replace( '-', '_', strtoupper( $headerName ) );
- if ( !empty( $_SERVER[$serverVarName] ) ) {
- $headers[] = $headerName . ': ' .
- str_replace( "\n", '', $_SERVER[$serverVarName] );
- }
- }
-
- $ch = curl_init( $reqURL );
- if ( $thgThumbCurlConfig['proxy'] ) {
- curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlConfig['proxy'] );
- }
-
- curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
- curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
- curl_setopt( $ch, CURLOPT_TIMEOUT, $thgThumbCurlConfig['timeout'] );
-
- # Actually make the request
- $text = curl_exec( $ch );
-
- # Send it on to the client...
- $errno = curl_errno( $ch );
- $contentType = curl_getinfo( $ch, CURLINFO_CONTENT_TYPE );
- $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
- if ( $errno ) {
- header( 'HTTP/1.1 500 Internal server error' );
- header( 'Cache-Control: no-cache' );
- $contentType = 'text/html';
- $text = wfCurlErrorText( $ch );
- } elseif ( $httpCode == 304 ) { // OK
- header( 'HTTP/1.1 304 Not modified' );
- $contentType = '';
- $text = '';
- } elseif ( strval( $text ) == '' ) {
- header( 'HTTP/1.1 500 Internal server error' );
- header( 'Cache-Control: no-cache' );
- $contentType = 'text/html';
- $text = wfCurlEmptyText( $ch );
- } elseif ( $httpCode == 404 ) {
- header( 'HTTP/1.1 404 Not found' );
- header( 'Cache-Control: s-maxage=300, must-revalidate, max-age=0' );
- } elseif ( $httpCode != 200 || substr( $contentType, 0, 9 ) == 'text/html' ) {
- # Error message, suppress cache
- header( 'HTTP/1.1 500 Internal server error' );
- header( 'Cache-Control: no-cache' );
- } else {
- # OK thumbnail; save to any backend caches...
- if ( isset( $thgThumbCallbacks['fillCache'] )
- && is_callable( $thgThumbCallbacks['fillCache'] ) )
- {
- call_user_func_array( $thgThumbCallbacks['fillCache'], array( $uri, $text ) );
- }
- }
-
- if ( !$contentType ) {
- header( 'Content-Type:' );
- } else {
- header( "Content-Type: $contentType" );
- }
-
- print $text; // thumb data or error text
-
- curl_close( $ch );
-}
-
-/**
- * Get error message HTML for when the cURL response is an error.