r4093 Finalise the work on issue #369 by means of new function HTTPDateToUnixTime(),
[racktables] / render_image.php
index c1735e6..37147ab 100644 (file)
@@ -6,7 +6,7 @@ if ( // 'progressbar's never change, force cache hit before loading init.php
        && $_REQUEST['img'] == 'progressbar'
 )
 {
        && $_REQUEST['img'] == 'progressbar'
 )
 {
-       $client_time = strtotime ($_SERVER['HTTP_IF_MODIFIED_SINCE']);
+       $client_time = HTTPDateToUnixTime ($_SERVER['HTTP_IF_MODIFIED_SINCE']);
        if ($client_time !== FALSE && $client_time !== -1) // readable
        {
                $server_time = time();
        if ($client_time !== FALSE && $client_time !== -1) // readable
        {
                $server_time = time();
@@ -66,6 +66,58 @@ catch (Exception $e)
 }
 
 //------------------------------------------------------------------------
 }
 
 //------------------------------------------------------------------------
+function HTTPDateToUnixTime ($string)
+{
+       //Written per RFC 2616 3.3.1 - Full Date
+       //http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
+       $month_number = array
+       (
+               'Jan' => 1,
+               'Feb' => 2,
+               'Mar' => 3,
+               'Apr' => 4,
+               'May' => 5,
+               'Jun' => 6,
+               'Jul' => 7,
+               'Aug' => 8,
+               'Sep' => 9,
+               'Oct' => 10,
+               'Nov' => 11,
+               'Dec' => 12,
+       );
+
+       $formats = array();
+       $formats['rfc1123'] = '/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat), (\d{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{4}) (\d{2}):(\d{2}):(\d{2}) GMT$/';
+       $formats['rfc850'] = '/^(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday), (\d{2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{2}) (\d{2}):(\d{2}):(\d{2}) GMT$/';
+       $formats['asctime'] = '/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{2}|\d{1}) (\d{2}):(\d{2}):(\d{2}) (\d{4})$/';
+
+       $matches = array();
+       if (preg_match ($formats['rfc1123'], $string, $matches)) {
+               $hours = $matches[5];
+               $minutes = $matches[6];
+               $seconds = $matches[7];
+               $month = $month_number[$matches[3]];
+               $day = $matches[2];
+               $year = $matches[4];
+       } elseif (preg_match ($formats['rfc850'], $string, $matches)) {
+               $hours = $matches[5];
+               $minutes = $matches[6];
+               $seconds = $matches[7];
+               $month = $month_number[substr($matches[3],0,3)];
+               $day = $matches[2];
+               $year = $matches[4];
+       } elseif (preg_match ($formats['asctime'], $string, $matches)) {
+               $hours = $matches[4];
+               $minutes = $matches[5];
+               $seconds = $matches[6];
+               $month = $month_number[$matches[2]];
+               $day = $matches[3];
+               $year = $matches[7];
+       } else
+               return false;
+       return gmmktime ($hours, $minutes, $seconds, $month, $day, $year);
+}
+
 function renderError ()
 {
        // A hardcoded value is worth of saving lots of code here.
 function renderError ()
 {
        // A hardcoded value is worth of saving lots of code here.