$filename, 'width' => $size ); if ( $pagenum ) { $params['page'] = $pagenum; } if ( $archOrTemp == '/archive' ) { $params['archived'] = 1; } elseif ( $archOrTemp == '/temp' ) { $params['temp'] = 1; } } else { $params = null; // not a valid thumbnail URL } return $params; } /** * cURL to thumb.php and stream back the resulting file or give an error message. * * @param $params Array Parameters to thumb.php * @param $uri String Thumbnail request URI * @return void */ function wfStreamThumbViaCurl( array $params, $uri ) { global $thgThumbCallbacks, $thgThumbCurlConfig; # Check any backend caches for the thumbnail... if ( isset( $thgThumbCallbacks['checkCache'] ) && is_callable( $thgThumbCallbacks['checkCache'] ) ) { if ( call_user_func_array( $thgThumbCallbacks['checkCache'], array( $uri, $params ) ) ) { return; // file streamed from backend thumb cache } } 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. * * @param $ch cURL handle * @return string */ function wfCurlErrorText( $ch ) { $error = htmlspecialchars( curl_error( $ch ) ); return << Thumbnail error Error retrieving thumbnail from scaling server: $error EOT; } /** * Get error message HTML for when the cURL response is empty. * * @param $ch cURL handle * @return string */ function wfCurlEmptyText( $ch ) { return << Thumbnail error Error retrieving thumbnail from scaling server: empty response EOT; } /** * Print out a generic 404 error message. * * @return void */ function wfDisplay404Error() { header( 'HTTP/1.1 404 Not Found' ); header( 'Content-Type: text/html;charset=utf-8' ); $prot = isset( $_SERVER['HTTPS'] ) ? "https://" : "http://"; $serv = strlen( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']; $loc = $_SERVER["REQUEST_URI"]; $encUrl = htmlspecialchars( $prot . $serv . $loc ); // Looks like a typical apache2 error $standard_404 = << 404 Not Found

Not Found

The requested URL $encUrl was not found on this server.

ENDTEXT; print $standard_404; }