ugh, missed this global var
[lhc/web/wiklou.git] / thumb.php
index 15574b8..60a7ffb 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -46,12 +46,18 @@ function wfThumbHandleRequest() {
  * @return void
  */
 function wfThumbHandle404() {
-       # lighttpd puts the original request in REQUEST_URI, while
-       # sjs sets that to the 404 handler, and puts the original
-       # request in REDIRECT_URL.
+       # lighttpd puts the original request in REQUEST_URI, while sjs sets
+       # that to the 404 handler, and puts the original request in REDIRECT_URL.
        if ( isset( $_SERVER['REDIRECT_URL'] ) ) {
-               # The URL is un-encoded, so put it back how it was.
+               # The URL is un-encoded, so put it back how it was
                $uri = str_replace( "%2F", "/", urlencode( $_SERVER['REDIRECT_URL'] ) );
+               # Just get the URI path (REDIRECT_URL is either a full URL or a path)
+               if ( $uri[0] !== '/' ) {
+                       $bits = wfParseUrl( $uri );
+                       if ( $bits && isset( $bits['path'] ) ) {
+                               $uri = $bits['path'];
+                       }
+               }
        } else {
                $uri = $_SERVER['REQUEST_URI'];
        }
@@ -167,6 +173,17 @@ function wfStreamThumb( array $params ) {
        try {
                $thumbName = $img->thumbName( $params );
                if ( strlen( $thumbName ) ) { // valid params?
+                       // For 404 handled thumbnails, we only use the the base name of the URI
+                       // for the thumb params and the parent directory for the source file name.
+                       // Check that the zone relative path matches up so squid caches won't pick
+                       // up thumbs that would not be purged on source file deletion (bug 34231).
+                       if ( isset( $params['rel404'] ) // thumbnail was handled via 404
+                               && urldecode( $params['rel404'] ) !== $img->getThumbRel( $thumbName ) ) 
+                       {
+                               wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
+                               wfProfileOut( __METHOD__ );
+                               return;
+                       }
                        $thumbPath = $img->getThumbPath( $thumbName );
                        if ( $img->getRepo()->fileExists( $thumbPath ) ) {
                                $img->getRepo()->streamFile( $thumbPath, $headers );
@@ -215,34 +232,45 @@ function wfStreamThumb( array $params ) {
  * Extract the required params for thumb.php from the thumbnail request URI.
  * At least 'width' and 'f' should be set if the result is an array.
  *
- * @param $uri String Thumbnail request URI
+ * @param $uri String Thumbnail request URI path
  * @return Array|null associative params array or null
  */
 function wfExtractThumbParams( $uri ) {
        $repo = RepoGroup::singleton()->getLocalRepo();
 
+       $zoneURI = $repo->getZoneUrl( 'thumb' );
+       if ( substr( $zoneURI, 0, 1 ) !== '/' ) {
+               $bits = wfParseUrl( $zoneURI );
+               if ( $bits && isset( $bits['path'] ) ) {
+                       $zoneURI = $bits['path'];
+               } else {
+                       return null;
+               }
+       }
+       $zoneUrlRegex = preg_quote( $zoneURI );
+
        $hashDirRegex = $subdirRegex = '';
        for ( $i = 0; $i < $repo->getHashLevels(); $i++ ) {
                $subdirRegex .= '[0-9a-f]';
                $hashDirRegex .= "$subdirRegex/";
        }
-       $zoneUrlRegex = preg_quote( $repo->getZoneUrl( 'thumb' ) );
 
-       $thumbUrlRegex = "!^$zoneUrlRegex(/archive|/temp|)/$hashDirRegex([^/]*)/([^/]*)$!";
+       $thumbUrlRegex = "!^$zoneUrlRegex/((archive/|temp/)?$hashDirRegex([^/]*)/([^/]*))$!";
 
        // Check if this is a valid looking thumbnail request...
        if ( preg_match( $thumbUrlRegex, $uri, $matches ) ) {
-               list( /* all */, $archOrTemp, $filename, $thumbname ) = $matches;
+               list( /* all */, $rel, $archOrTemp, $filename, $thumbname ) = $matches;
+               $filename = urldecode( $filename );
+               $thumbname = urldecode( $thumbname );
 
-               $params = array( 'f' => $filename );
-               if ( $archOrTemp == '/archive' ) {
+               $params = array( 'f' => $filename, 'rel404' => $rel );
+               if ( $archOrTemp == 'archive/' ) {
                        $params['archived'] = 1;
-               } elseif ( $archOrTemp == '/temp' ) {
+               } elseif ( $archOrTemp == 'temp/' ) {
                        $params['temp'] = 1;
                }
 
                // Check if the parameters can be extracted from the thumbnail name...
-               // @TODO: remove 'page' stuff and make ProofreadPage handle it via hook.
                if ( preg_match( '!^(page(\d*)-)*(\d*)px-[^/]*$!', $thumbname, $matches ) ) {
                        list( /* all */, $pagefull, $pagenum, $size ) = $matches;
                        $params['width'] = $size;