Index: trunk/phase3/thumb_handler.php |
— | — | @@ -3,20 +3,19 @@ |
4 | 4 | # Valid web server entry point |
5 | 5 | define( 'THUMB_HANDLER', true ); |
6 | 6 | |
7 | | -# Load thumb-handler configuration. We don't want to use |
8 | | -# WebStart.php or the like as it would kill performance. |
9 | | -$configPath = dirname( __FILE__ ) . "/thumb.config.php"; |
10 | | -if ( !file_exists( $configPath ) ) { |
11 | | - die( "Thumb-handler.php is not enabled for this wiki.\n" ); |
12 | | -} elseif ( !extension_loaded( 'curl' ) ) { |
13 | | - die( "cURL is not enabled for PHP on this wiki.\n" ); // sanity |
| 7 | +# Load thumb-handler configuration. Avoids WebStart.php for performance. |
| 8 | +if ( !file_exists( dirname( __FILE__ ) . "/thumb.config.php" ) ) { |
| 9 | + die( "thumb_handler.php is not enabled for this wiki.\n" ); |
14 | 10 | } |
15 | | -require( $configPath ); |
| 11 | +require( dirname( __FILE__ ) . "/thumb.config.php" ); |
16 | 12 | |
17 | | -wfHandleThumb404Main(); |
| 13 | +# Execute thumb.php if not handled via cURL |
| 14 | +if ( wfHandleThumb404Main() === 'wfThumbMain' ) { |
| 15 | + require( dirname( __FILE__ ) . '/thumb.php' ); |
| 16 | +} |
18 | 17 | |
19 | 18 | function wfHandleThumb404Main() { |
20 | | - global $thgThumbCallbacks, $thgThumb404File; |
| 19 | + global $thgThumbCallbacks, $thgThumbCurlConfig; |
21 | 20 | |
22 | 21 | # lighttpd puts the original request in REQUEST_URI, while |
23 | 22 | # sjs sets that to the 404 handler, and puts the original |
— | — | @@ -40,8 +39,10 @@ |
41 | 40 | # Show 404 error if this is not a valid thumb request... |
42 | 41 | if ( !is_array( $params ) ) { |
43 | 42 | header( 'X-Debug: no regex match' ); // useful for debugging |
44 | | - if ( $thgThumb404File ) { // overridden by configuration? |
45 | | - require( $thgThumb404File ); |
| 43 | + if ( isset( $thgThumbCallbacks['error404'] ) |
| 44 | + && is_callable( $thgThumbCallbacks['error404'] ) ) // overridden by configuration? |
| 45 | + { |
| 46 | + call_user_func( $thgThumbCallbacks['error404'] ); |
46 | 47 | } else { |
47 | 48 | wfDisplay404Error(); // standard 404 message |
48 | 49 | } |
— | — | @@ -57,7 +58,14 @@ |
58 | 59 | } |
59 | 60 | } |
60 | 61 | |
61 | | - wfStreamThumbViaCurl( $params, $uri ); |
| 62 | + # Obtain and stream the thumbnail or setup for wfThumbMain() call... |
| 63 | + if ( $thgThumbCurlConfig['enabled'] ) { |
| 64 | + wfStreamThumbViaCurl( $params, $uri ); |
| 65 | + return true; // done |
| 66 | + } else { |
| 67 | + $_REQUEST = $params; // pass params to thumb.php |
| 68 | + return 'wfThumbMain'; |
| 69 | + } |
62 | 70 | } |
63 | 71 | |
64 | 72 | /** |
— | — | @@ -68,11 +76,11 @@ |
69 | 77 | * @return Array|null associative params array or null |
70 | 78 | */ |
71 | 79 | function wfExtractThumbParams( $uri ) { |
72 | | - global $thgThumbServer, $thgThumbFragment, $thgThumbHashFragment; |
| 80 | + global $thgThumbUrlMatch; |
73 | 81 | |
74 | | - $thumbRegex = '!^(?:' . preg_quote( $thgThumbServer ) . ')?/' . |
75 | | - preg_quote( $thgThumbFragment ) . '(/archive|/temp|)/' . |
76 | | - $thgThumbHashFragment . '([^/]*)/(page(\d*)-)*(\d*)px-[^/]*$!'; |
| 82 | + $thumbRegex = '!^(?:' . preg_quote( $thgThumbUrlMatch['server'] ) . ')?/' . |
| 83 | + preg_quote( $thgThumbUrlMatch['dirFragment'] ) . '(/archive|/temp|)/' . |
| 84 | + $thgThumbUrlMatch['hashFragment'] . '([^/]*)/(page(\d*)-)*(\d*)px-[^/]*$!'; |
77 | 85 | |
78 | 86 | if ( preg_match( $thumbRegex, $uri, $matches ) ) { |
79 | 87 | list( $all, $archOrTemp, $filename, $pagefull, $pagenum, $size ) = $matches; |
— | — | @@ -100,10 +108,14 @@ |
101 | 109 | * @return void |
102 | 110 | */ |
103 | 111 | function wfStreamThumbViaCurl( array $params, $uri ) { |
104 | | - global $thgThumbCallbacks, $thgThumbScriptPath, $thgThumbCurlProxy, $thgThumbCurlTimeout; |
| 112 | + global $thgThumbCallbacks, $thgThumbCurlConfig; |
105 | 113 | |
| 114 | + if ( !extension_loaded( 'curl' ) ) { |
| 115 | + die( "cURL is not enabled for PHP on this wiki.\n" ); // sanity |
| 116 | + } |
| 117 | + |
106 | 118 | # Build up the request URL to use with CURL... |
107 | | - $reqURL = "{$thgThumbScriptPath}?"; |
| 119 | + $reqURL = $thgThumbCurlConfig['url'] . '?'; |
108 | 120 | $first = true; |
109 | 121 | foreach ( $params as $name => $value ) { |
110 | 122 | if ( $first ) { |
— | — | @@ -135,13 +147,13 @@ |
136 | 148 | } |
137 | 149 | |
138 | 150 | $ch = curl_init( $reqURL ); |
139 | | - if ( $thgThumbCurlProxy ) { |
140 | | - curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlProxy ); |
| 151 | + if ( $thgThumbCurlConfig['proxy'] ) { |
| 152 | + curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlConfig['proxy'] ); |
141 | 153 | } |
142 | 154 | |
143 | 155 | curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); |
144 | 156 | curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); |
145 | | - curl_setopt( $ch, CURLOPT_TIMEOUT, $thgThumbCurlTimeout ); |
| 157 | + curl_setopt( $ch, CURLOPT_TIMEOUT, $thgThumbCurlConfig['timeout'] ); |
146 | 158 | |
147 | 159 | # Actually make the request |
148 | 160 | $text = curl_exec( $ch ); |
Index: trunk/phase3/thumb.config.sample |
— | — | @@ -14,29 +14,31 @@ |
15 | 15 | * 1) Copy this file to thumb.config.php and modify the settings. |
16 | 16 | * 2) The webserver must be setup to have thumb-handler.php as a 404 handler. |
17 | 17 | * This can be done in apache by editing .htaccess in the /thumb directory by adding: |
18 | | - * ErrorDocument 404 /path/to/thumb-handler.php |
| 18 | + * ErrorDocument 404 /path/to/thumb_handler.php |
19 | 19 | */ |
20 | 20 | |
21 | | -# URL name of the server (e.g. "upload.wikipedia.org"). |
22 | | -$thgThumbServer = "http://localhost"; |
23 | | -# URL fragment after the server name to the thumb directory |
24 | | -$thgThumbFragment = "MW_trunk/images/thumb"; |
25 | | -# URL regex fragment correspond to the directory hashing of thumbnails. |
26 | | -# This must correspond to $wgLocalFileRepo['hashLevels']. |
27 | | -$thgThumbHashFragment = '[0-9a-f]/[0-9a-f][0-9a-f]/'; // 2-level directory hashing |
| 21 | +$thgThumbUrlMatch = array( |
| 22 | + # URL name of the server (e.g. "upload.wikipedia.org"). |
| 23 | + 'server' => 'http://localhost', |
| 24 | + # URL fragment to the thumb/ directory |
| 25 | + 'dirFragment' => 'MW_trunk/images/thumb', |
| 26 | + # URL regex fragment correspond to the directory hashing of thumbnails. |
| 27 | + # This must correspond to $wgLocalFileRepo['hashLevels']. |
| 28 | + 'hashFragment' => '[0-9a-f]/[0-9a-f][0-9a-f]/' // 2-level directory hashing |
| 29 | +); |
28 | 30 | |
29 | | -# The URL to thumb.php, accessible from the web server. |
30 | | -$thgThumbScriptPath = "http://localhost/MW_trunk/thumb.php"; |
| 31 | +$thgThumbCurlConfig = array( |
| 32 | + # Optionally cURL to thumb.php instead of using it directly |
| 33 | + 'enabled' => false, |
| 34 | + # The URL to thumb.php, accessible from the web server. |
| 35 | + 'url' => 'http://localhost/MW_trunk/thumb.php', |
| 36 | + # Optional proxy server to use to access thumb.php |
| 37 | + 'proxy' => null, |
| 38 | + # Timeout to use for cURL request to thumb.php. |
| 39 | + # Leave it long enough to generate a ulimit timeout in ordinary |
| 40 | + # cases, but short enough to avoid a local PHP timeout. |
| 41 | + 'timeout' => 53 |
| 42 | +); |
31 | 43 | |
32 | | -# Timeout to use for cURL request to thumb.php. |
33 | | -# Leave it long enough to generate a ulimit timeout in ordinary |
34 | | -# cases, but short enough to avoid a local PHP timeout. |
35 | | -$thgThumbCurlTimeout = 53; |
36 | | -# Optional proxy server to use to access thumb.php |
37 | | -$thgThumbCurlProxy = null; // proxy to thumb.php |
38 | | - |
39 | | -# File path to a php file the gives a 404 error message |
40 | | -$thgThumb404File = null; |
41 | | - |
42 | 44 | # Custom functions for overriding aspects of thumb handling |
43 | 45 | $thgThumbCallbacks = array(); |
Index: trunk/phase3/thumb.php |
— | — | @@ -26,10 +26,12 @@ |
27 | 27 | $headers = array(); |
28 | 28 | |
29 | 29 | // Get input parameters |
30 | | - if ( get_magic_quotes_gpc() ) { |
31 | | - $params = array_map( 'stripslashes', $_REQUEST ); |
| 30 | + if ( defined( 'THUMB_HANDLER' ) ) { |
| 31 | + $params = $_REQUEST; // called from thumb_handler.php |
32 | 32 | } else { |
33 | | - $params = $_REQUEST; |
| 33 | + $params = get_magic_quotes_gpc() |
| 34 | + ? array_map( 'stripslashes', $_REQUEST ) |
| 35 | + : $_REQUEST; |
34 | 36 | } |
35 | 37 | |
36 | 38 | $fileName = isset( $params['f'] ) ? $params['f'] : ''; |