FU r100535:
authorAaron Schulz <aaron@users.mediawiki.org>
Fri, 28 Oct 2011 05:12:12 +0000 (05:12 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Fri, 28 Oct 2011 05:12:12 +0000 (05:12 +0000)
* Thumb handler can now also work without cURL
* Combined related config vars into array config vars
* Folded $thgThumb404File into $thgThumbCallbacks
* Avoided some global pollution

thumb.config.sample
thumb.php
thumb_handler.php

index 927302a..97cb922 100644 (file)
@@ -14,29 +14,31 @@ if ( !defined( 'THUMB_HANDLER' ) ) {
  * 1) Copy this file to thumb.config.php and modify the settings.
  * 2) The webserver must be setup to have thumb-handler.php as a 404 handler.
  *    This can be done in apache by editing .htaccess in the /thumb directory by adding:
- *       ErrorDocument 404 /path/to/thumb-handler.php
+ *       ErrorDocument 404 /path/to/thumb_handler.php
  */
 
-# URL name of the server (e.g. "upload.wikipedia.org").
-$thgThumbServer = "http://localhost";
-# URL fragment after the server name to the thumb directory
-$thgThumbFragment = "MW_trunk/images/thumb";
-# URL regex fragment correspond to the directory hashing of thumbnails.
-# This must correspond to $wgLocalFileRepo['hashLevels'].
-$thgThumbHashFragment = '[0-9a-f]/[0-9a-f][0-9a-f]/'; // 2-level directory hashing
+$thgThumbUrlMatch = array(
+       # URL name of the server (e.g. "upload.wikipedia.org").
+       'server'        => 'http://localhost',
+       # URL fragment to the thumb/ directory
+       'dirFragment'   => 'MW_trunk/images/thumb',
+       # URL regex fragment correspond to the directory hashing of thumbnails.
+       # This must correspond to $wgLocalFileRepo['hashLevels'].
+       'hashFragment'  => '[0-9a-f]/[0-9a-f][0-9a-f]/' // 2-level directory hashing
+);
 
-# The URL to thumb.php, accessible from the web server.
-$thgThumbScriptPath = "http://localhost/MW_trunk/thumb.php";
-
-# Timeout to use for cURL request to thumb.php.
-# Leave it long enough to generate a ulimit timeout in ordinary
-# cases, but short enough to avoid a local PHP timeout.
-$thgThumbCurlTimeout = 53;
-# Optional proxy server to use to access thumb.php
-$thgThumbCurlProxy = null; // proxy to thumb.php
-
-# File path to a php file the gives a 404 error message
-$thgThumb404File = null;
+$thgThumbCurlConfig = array(
+       # Optionally cURL to thumb.php instead of using it directly
+       'enabled' => false,
+       # The URL to thumb.php, accessible from the web server.
+       'url'     => 'http://localhost/MW_trunk/thumb.php',
+       # Optional proxy server to use to access thumb.php
+       'proxy'   => null,
+       # Timeout to use for cURL request to thumb.php.
+       # Leave it long enough to generate a ulimit timeout in ordinary
+       # cases, but short enough to avoid a local PHP timeout.
+       'timeout' => 53
+);
 
 # Custom functions for overriding aspects of thumb handling
 $thgThumbCallbacks = array();
index 15a8666..0bb0b60 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -26,10 +26,12 @@ function wfThumbMain() {
        $headers = array();
 
        // Get input parameters
-       if ( get_magic_quotes_gpc() ) {
-               $params = array_map( 'stripslashes', $_REQUEST );
+       if ( defined( 'THUMB_HANDLER' ) ) {
+               $params = $_REQUEST; // called from thumb_handler.php
        } else {
-               $params = $_REQUEST;
+               $params = get_magic_quotes_gpc()
+                       ? array_map( 'stripslashes', $_REQUEST )
+                       : $_REQUEST;
        }
 
        $fileName = isset( $params['f'] ) ? $params['f'] : '';
index 6d18bab..0f7d1af 100644 (file)
@@ -3,20 +3,19 @@
 # Valid web server entry point
 define( 'THUMB_HANDLER', true );
 
-# Load thumb-handler configuration. We don't want to use
-# WebStart.php or the like as it would kill performance.
-$configPath = dirname( __FILE__ ) . "/thumb.config.php";
-if ( !file_exists( $configPath ) ) {
-       die( "Thumb-handler.php is not enabled for this wiki.\n" );
-} elseif ( !extension_loaded( 'curl' ) ) {
-       die( "cURL is not enabled for PHP on this wiki.\n" ); // sanity
+# Load thumb-handler configuration. Avoids WebStart.php for performance.
+if ( !file_exists( dirname( __FILE__ ) . "/thumb.config.php" ) ) {
+       die( "thumb_handler.php is not enabled for this wiki.\n" );
 }
-require( $configPath );
+require( dirname( __FILE__ ) . "/thumb.config.php" );
 
-wfHandleThumb404Main();
+# Execute thumb.php if not handled via cURL
+if ( wfHandleThumb404Main() === 'wfThumbMain' ) {
+       require( dirname( __FILE__ ) . '/thumb.php' );
+}
 
 function wfHandleThumb404Main() {
-       global $thgThumbCallbacks, $thgThumb404File;
+       global $thgThumbCallbacks, $thgThumbCurlConfig;
 
        # lighttpd puts the original request in REQUEST_URI, while
        # sjs sets that to the 404 handler, and puts the original
@@ -40,8 +39,10 @@ function wfHandleThumb404Main() {
        # Show 404 error if this is not a valid thumb request...
        if ( !is_array( $params ) ) {
                header( 'X-Debug: no regex match' ); // useful for debugging
-               if ( $thgThumb404File ) { // overridden by configuration?
-                       require( $thgThumb404File );
+               if ( isset( $thgThumbCallbacks['error404'] )
+                       && is_callable( $thgThumbCallbacks['error404'] ) ) // overridden by configuration?
+               {
+                       call_user_func( $thgThumbCallbacks['error404'] );
                } else {
                        wfDisplay404Error(); // standard 404 message
                }
@@ -57,7 +58,14 @@ function wfHandleThumb404Main() {
                }
        }
 
-       wfStreamThumbViaCurl( $params, $uri );
+       # Obtain and stream the thumbnail or setup for wfThumbMain() call...
+       if ( $thgThumbCurlConfig['enabled'] ) {
+               wfStreamThumbViaCurl( $params, $uri );
+               return true; // done
+       } else {
+               $_REQUEST = $params; // pass params to thumb.php
+               return 'wfThumbMain';
+       }
 }
 
 /**
@@ -68,11 +76,11 @@ function wfHandleThumb404Main() {
  * @return Array|null associative params array or null
  */
 function wfExtractThumbParams( $uri ) {
-       global $thgThumbServer, $thgThumbFragment, $thgThumbHashFragment;
+       global $thgThumbUrlMatch;
 
-       $thumbRegex = '!^(?:' . preg_quote( $thgThumbServer ) . ')?/' .
-               preg_quote( $thgThumbFragment ) . '(/archive|/temp|)/' .
-               $thgThumbHashFragment . '([^/]*)/(page(\d*)-)*(\d*)px-[^/]*$!';
+       $thumbRegex = '!^(?:' . preg_quote( $thgThumbUrlMatch['server'] ) . ')?/' .
+               preg_quote( $thgThumbUrlMatch['dirFragment'] ) . '(/archive|/temp|)/' .
+               $thgThumbUrlMatch['hashFragment'] . '([^/]*)/(page(\d*)-)*(\d*)px-[^/]*$!';
 
        if ( preg_match( $thumbRegex, $uri, $matches ) ) {
                list( $all, $archOrTemp, $filename, $pagefull, $pagenum, $size ) = $matches;
@@ -100,10 +108,14 @@ function wfExtractThumbParams( $uri ) {
  * @return void
  */
 function wfStreamThumbViaCurl( array $params, $uri ) {
-       global $thgThumbCallbacks, $thgThumbScriptPath, $thgThumbCurlProxy, $thgThumbCurlTimeout;
+       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 = "{$thgThumbScriptPath}?";
+       $reqURL = $thgThumbCurlConfig['url'] . '?';
        $first = true;
        foreach ( $params as $name => $value ) {
                if ( $first ) {
@@ -135,13 +147,13 @@ function wfStreamThumbViaCurl( array $params, $uri ) {
        }
 
        $ch = curl_init( $reqURL );
-       if ( $thgThumbCurlProxy ) {
-               curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlProxy );
+       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, $thgThumbCurlTimeout );
+       curl_setopt( $ch, CURLOPT_TIMEOUT, $thgThumbCurlConfig['timeout'] );
 
        # Actually make the request
        $text = curl_exec( $ch );