X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=thumb_handler.php;h=e2b165b8a95b72812910b83fa4e47a77748a97ae;hb=021ed39d59b3a2edcaea70edb03778eb0bdc36d4;hp=073a87929e0550ebb76fac513f0d411f17092b2d;hpb=87382f33808f5ad4bf8fb6a4a07ef6e1a775e7cc;p=lhc%2Fweb%2Fwiklou.git diff --git a/thumb_handler.php b/thumb_handler.php index 073a87929e..e2b165b8a9 100644 --- a/thumb_handler.php +++ b/thumb_handler.php @@ -1,264 +1,29 @@ $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. + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * - * @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. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * @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. + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html * - * @return void + * @file + * @ingroup Media */ -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; +define( 'THUMB_HANDLER', true ); - print $standard_404; -} +// Execute thumb.php, having set THUMB_HANDLER so that +// it knows to extract params from a thumbnail file URL. +require __DIR__ . '/thumb.php';