Index: trunk/phase3/thumb.config.sample |
— | — | @@ -37,3 +37,6 @@ |
38 | 38 | |
39 | 39 | # File path to a php file the gives a 404 error message |
40 | 40 | $thgThumb404File = "404.php"; |
| 41 | + |
| 42 | +# Custom functions for overriding aspects of thumb handling |
| 43 | +$thgThumbCallbacks = array(); |
Index: trunk/phase3/thumb-handler.php |
— | — | @@ -12,7 +12,7 @@ |
13 | 13 | require( $configPath ); |
14 | 14 | |
15 | 15 | function wfHandleThumb404() { |
16 | | - global $thgThumb404File; |
| 16 | + global $thgThumbCallbacks, $thgThumb404File; |
17 | 17 | |
18 | 18 | # lighttpd puts the original request in REQUEST_URI, while |
19 | 19 | # sjs sets that to the 404 handler, and puts the original |
— | — | @@ -24,9 +24,12 @@ |
25 | 25 | $uri = $_SERVER['REQUEST_URI']; |
26 | 26 | } |
27 | 27 | |
28 | | - # Extract thumb.php params from the URI. |
29 | | - if ( function_exists( 'wfCustomExtractThumbParams' ) ) { |
30 | | - $params = wfCustomExtractThumbParams( $uri ); // overridden by configuration |
| 28 | + # Extract thumb.php params from the URI... |
| 29 | + if ( isset( $thgThumbCallbacks['extractParams'] ) |
| 30 | + && is_callable( $thgThumbCallbacks['extractParams'] ) ) |
| 31 | + { |
| 32 | + # Overridden by configuration |
| 33 | + $params = call_user_func_array( $thgThumbCallbacks['extractParams'], array( $uri ) ); |
31 | 34 | } else { |
32 | 35 | $params = wfExtractThumbParams( $uri ); // basic wiki URL param extracting |
33 | 36 | } |
— | — | @@ -40,7 +43,7 @@ |
41 | 44 | if ( preg_match( '/[\x80-\xff]/', $uri ) ) { |
42 | 45 | header( 'HTTP/1.0 400 Bad request' ); |
43 | 46 | header( 'Content-Type: text/html' ); |
44 | | - echo "<html><head><title>Bad request</title></head><body>" . |
| 47 | + print "<html><head><title>Bad request</title></head><body>" . |
45 | 48 | "The URI contained bytes with the high bit set, this is not allowed." . |
46 | 49 | "</body></html>"; |
47 | 50 | return; |
— | — | @@ -48,12 +51,21 @@ |
49 | 52 | header( 'HTTP/1.0 404 Not found' ); |
50 | 53 | header( 'Content-Type: text/html' ); |
51 | 54 | header( 'X-Debug: filename contains a space' ); // useful for debugging |
52 | | - echo "<html><head><title>Not found</title></head><body>" . |
| 55 | + print "<html><head><title>Not found</title></head><body>" . |
53 | 56 | "The URL contained spaces, we don't have any thumbnail files with spaces." . |
54 | 57 | "</body></html>"; |
55 | 58 | return; |
56 | 59 | } |
57 | 60 | |
| 61 | + # Check any backend caches for the thumbnail... |
| 62 | + if ( isset( $thgThumbCallbacks['checkCache'] ) |
| 63 | + && is_callable( $thgThumbCallbacks['checkCache'] ) ) |
| 64 | + { |
| 65 | + if ( call_user_func_array( $thgThumbCallbacks['checkCache'], array( $uri, $params ) ) ) { |
| 66 | + return; // file streamed from backend thumb cache |
| 67 | + } |
| 68 | + } |
| 69 | + |
58 | 70 | wfStreamThumbViaCurl( $params, $uri ); |
59 | 71 | } |
60 | 72 | |
— | — | @@ -69,11 +81,11 @@ |
70 | 82 | |
71 | 83 | $thumbRegex = '!^(?:' . preg_quote( $thgThumbServer ) . ')?/' . |
72 | 84 | preg_quote( $thgThumbFragment ) . '(/archive|/temp|)/' . |
73 | | - $thgThumbHashFragment . '([^/]*)/' . '(page(\d*)-)*(\d*)px-([^/]*)$!'; |
| 85 | + $thgThumbHashFragment . '([^/]*)/(page(\d*)-)*(\d*)px-[^/]*$!'; |
74 | 86 | |
75 | 87 | # Is this a thumbnail? |
76 | 88 | if ( preg_match( $thumbRegex, $uri, $matches ) ) { |
77 | | - list( $all, $archOrTemp, $filename, $pagefull, $pagenum, $size, $fn2 ) = $matches; |
| 89 | + list( $all, $archOrTemp, $filename, $pagefull, $pagenum, $size ) = $matches; |
78 | 90 | $params = array( 'f' => $filename, 'width' => $size ); |
79 | 91 | if ( $pagenum ) { |
80 | 92 | $params['page'] = $pagenum; |
— | — | @@ -98,13 +110,13 @@ |
99 | 111 | * @return void |
100 | 112 | */ |
101 | 113 | function wfStreamThumbViaCurl( array $params, $uri ) { |
102 | | - global $thgThumbScriptPath, $thgThumbCurlProxy, $thgThumbCurlTimeout; |
| 114 | + global $thgThumbCallbacks, $thgThumbScriptPath, $thgThumbCurlProxy, $thgThumbCurlTimeout; |
103 | 115 | |
104 | 116 | if ( !function_exists( 'curl_init' ) ) { |
105 | 117 | header( 'HTTP/1.0 404 Not found' ); |
106 | 118 | header( 'Content-Type: text/html' ); |
107 | 119 | header( 'X-Debug: cURL is not enabled' ); // useful for debugging |
108 | | - echo "<html><head><title>Not found</title></head><body>" . |
| 120 | + print "<html><head><title>Not found</title></head><body>" . |
109 | 121 | "cURL is not enabled for PHP on this wiki. Unable to send request thumb.php." . |
110 | 122 | "</body></html>"; |
111 | 123 | return; |
— | — | @@ -119,24 +131,22 @@ |
120 | 132 | } else { |
121 | 133 | $reqURL .= '&'; |
122 | 134 | } |
123 | | - // Note: value is already urlencoded |
124 | | - $reqURL .= "$name=$value"; |
| 135 | + $reqURL .= "$name=$value"; // Note: value is already urlencoded |
125 | 136 | } |
126 | 137 | |
127 | | - $ch = curl_init( $reqURL ); |
128 | | - if ( $thgThumbCurlProxy ) { |
129 | | - curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlProxy ); |
| 138 | + # Set relevant HTTP headers... |
| 139 | + $headers = array(); |
| 140 | + $headers[] = "X-Original-URI: " . str_replace( "\n", '', $uri ); |
| 141 | + if ( isset( $thgThumbCallbacks['curlHeaders'] ) |
| 142 | + && is_callable( $thgThumbCallbacks['curlHeaders'] ) ) |
| 143 | + { |
| 144 | + # Add on any custom headers (like XFF) |
| 145 | + call_user_func_array( $thgThumbCallbacks['curlHeaders'], array( &$headers ) ); |
130 | 146 | } |
131 | 147 | |
132 | | - $headers = array(); // HTTP headers |
133 | | - # Set certain headers... |
134 | | - $headers[] = "X-Original-URI: " . str_replace( "\n", '', $uri ); |
135 | | - if ( function_exists( 'wfCustomThumbRequestHeaders' ) ) { |
136 | | - wfCustomThumbRequestHeaders( $headers ); // add on any custom headers (like XFF) |
137 | | - } |
138 | 148 | # Pass through some other headers... |
139 | | - $passthrough = array( 'If-Modified-Since', 'Referer', 'User-Agent' ); |
140 | | - foreach ( $passthrough as $headerName ) { |
| 149 | + $passThrough = array( 'If-Modified-Since', 'Referer', 'User-Agent' ); |
| 150 | + foreach ( $passThrough as $headerName ) { |
141 | 151 | $serverVarName = 'HTTP_' . str_replace( '-', '_', strtoupper( $headerName ) ); |
142 | 152 | if ( !empty( $_SERVER[$serverVarName] ) ) { |
143 | 153 | $headers[] = $headerName . ': ' . |
— | — | @@ -144,6 +154,11 @@ |
145 | 155 | } |
146 | 156 | } |
147 | 157 | |
| 158 | + $ch = curl_init( $reqURL ); |
| 159 | + if ( $thgThumbCurlProxy ) { |
| 160 | + curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlProxy ); |
| 161 | + } |
| 162 | + |
148 | 163 | curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); |
149 | 164 | curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); |
150 | 165 | curl_setopt( $ch, CURLOPT_TIMEOUT, $thgThumbCurlTimeout ); |
— | — | @@ -177,6 +192,13 @@ |
178 | 193 | # Error message, suppress cache |
179 | 194 | header( 'HTTP/1.1 500 Internal server error' ); |
180 | 195 | header( 'Cache-Control: no-cache' ); |
| 196 | + } else { |
| 197 | + # OK thumbnail; save to any backend caches... |
| 198 | + if ( isset( $thgThumbCallbacks['fillCache'] ) |
| 199 | + && is_callable( $thgThumbCallbacks['fillCache'] ) ) |
| 200 | + { |
| 201 | + call_user_func_array( $thgThumbCallbacks['fillCache'], array( $uri, $text ) ); |
| 202 | + } |
181 | 203 | } |
182 | 204 | |
183 | 205 | if ( !$contentType ) { |