[BUGFIX] Send sane HTTP response in showpic on error
authorChristian Kuhn <lolli@schwarzbu.ch>
Wed, 18 Jul 2012 09:37:29 +0000 (11:37 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Fri, 20 Jul 2012 10:20:36 +0000 (12:20 +0200)
Currently, if showpic.php requests fail, exceptions are thrown. Those are
usually converted to a HTTP response 500 (internal server error) by the
production exception handler. Crawlers react on 500 by trying to index
the resource again later.

This is changed to now set specific response codes. If the paramters are
incorrect or broken, an 410 (Gone) will be sent, informing users and
crawlers that the resource is not available under this URL. Crawlers
like googleBot will then stop requesting the resource.

In case all parameters are ok, but the image itself is not found, a 404
will be sent to the client. Crawlers will try to index the resource
again only if there are still links pointing to the resource.

Change-Id: I606937fa9953b88be5edf940201e0153223ae0e6
Fixes: #39052
Releases: 6.0, 4.7, 4.6, 4.5
Reviewed-on: http://review.typo3.org/12919
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
t3lib/utility/class.t3lib_utility_http.php
typo3/sysext/cms/tslib/showpic.php

index e66d5da..a6f7296 100644 (file)
@@ -92,9 +92,29 @@ class t3lib_utility_Http {
         * @param       string  An optional HTTP status header. Default is 'HTTP/1.1 303 See Other'
         */
        public static function redirect($url, $httpStatus = self::HTTP_STATUS_303) {
-               header($httpStatus);
+               self::setResponseCode($httpStatus);
                header('Location: ' . t3lib_div::locationHeaderUrl($url));
+               exit;
+       }
+
+       /**
+        * Set a specifc response code like 404.
+        *
+        * @param string $httpStatus One of the HTTP_STATUS_* class class constants, default to self::HTTP_STATUS_303
+        * @return void
+        */
+       public static function setResponseCode($httpStatus = self::HTTP_STATUS_303) {
+               header($httpStatus);
+       }
 
+       /**
+        * Set a specific response code and exit script execution.
+        *
+        * @param string $httpStatus One of the HTTP_STATUS_* class class constants, default to self::HTTP_STATUS_303
+        * @return void
+        */
+       public static function setResponseCodeAndExit($httpStatus = self::HTTP_STATUS_303) {
+               self::setResponseCode($httpStatus);
                exit;
        }
 }
index 653e57c..2ab6dca 100644 (file)
@@ -170,7 +170,7 @@ class SC_tslib_showpic {
                // ***********************
                        // If no file-param or parameters are given, we must exit
                if (!$this->file || !isset($parametersArray) || !is_array($parametersArray)) {
-                       throw new UnexpectedValueException('Parameter Error: No file or no parameters given.');
+                       t3lib_utility_Http::setResponseCodeAndExit(t3lib_utility_Http::HTTP_STATUS_410);
                }
 
                $this->parametersEncoded = implode('', $parametersArray);
@@ -184,7 +184,7 @@ class SC_tslib_showpic {
                );
 
                if ($md5_value !== $this->md5) {
-                       throw new UnexpectedValueException('Parameter Error: Wrong parameters sent.');
+                       t3lib_utility_Http::setResponseCodeAndExit(t3lib_utility_Http::HTTP_STATUS_410);
                }
 
                $parameters = unserialize(base64_decode($this->parametersEncoded));
@@ -199,10 +199,10 @@ class SC_tslib_showpic {
 
                $test_file=PATH_site.$this->file;
                if (!t3lib_div::validPathStr($test_file))       {
-                       throw new UnexpectedValueException('Parameter Error: No valid filepath');
+                       t3lib_utility_Http::setResponseCodeAndExit(t3lib_utility_Http::HTTP_STATUS_410);
                }
                if (!@is_file($test_file))      {
-                       throw new UnexpectedValueException('The given file was not found');
+                       t3lib_utility_Http::setResponseCodeAndExit(t3lib_utility_Http::HTTP_STATUS_404);
                }
        }
 
@@ -277,4 +277,4 @@ $SOBE = t3lib_div::makeInstance('SC_tslib_showpic');
 $SOBE->init();
 $SOBE->main();
 $SOBE->printContent();
-?>
+?>
\ No newline at end of file