Example #1
0
 /**
  * Sends Last-Modified and checks If-Modified-Since for a match (if so, terminates and sends a 304 Not Modified status code). Uses the given timestamp as base for calculation. If it is an object or a query, the updated-at field of the object (or the newest item that matches the query) is used. You can call this method twice if you created a new cache file and don’t have any other timestamp. It will only output the headers once.
  * @param $mTimestamp The last-modified date to send. Can be one of the following:
  *                     • A UNIX timestamp as an integer
  *                     • A string to be parsed into a date using strtotime
  *                     • A DateTime object
  *                     • A Propel database object whose updated_at timestamp will be used
  *                     • A Propel query that will find the object most recently updated and use its updated_at timestamp
  */
 public static function sendLastModifiedAndCheckModifiedSince($mTimestamp)
 {
     if (self::$LAST_MODIFIED_SENT) {
         return;
     }
     if ($mTimestamp instanceof BaseObject) {
         $mTimestamp = $mTimestamp->getUpdatedAtTimestamp();
     }
     if ($mTimestamp instanceof ModelCriteria) {
         $mTimestamp = $mTimestamp->findMostRecentUpdate();
     }
     if (is_string($mTimestamp)) {
         $mTimestamp = strtotime($mTimestamp);
     }
     if ($mTimestamp === null) {
         return;
     }
     if ($mTimestamp instanceof DateTime) {
         $mTimestamp = clone $mTimestamp;
         $mTimestamp->setTimezone(new DateTimeZone('UTC'));
     } else {
         $mTimestamp = new DateTime("@{$mTimestamp}");
     }
     header("Last-Modified: " . $mTimestamp->format(self::DATE_RFC2616));
     self::$LAST_MODIFIED_SENT = true;
     if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
         $oSinceDate = DateTime::createFromFormat(self::DATE_RFC2616, $_SERVER['HTTP_IF_MODIFIED_SINCE'], new DateTimeZone('UTC'));
         if ($oSinceDate->getTimestamp() >= $mTimestamp->getTimestamp()) {
             self::sendHTTPStatusCode(304, 'Not Modified');
             header('Content-Length: 0');
             exit;
         }
     }
 }