Contacts: Move response caching to OC_Response

This commit is contained in:
Bart Visscher 2012-02-12 17:20:30 +01:00
parent a0bb6079c5
commit 0917bdecdd
4 changed files with 68 additions and 25 deletions

View File

@ -152,4 +152,12 @@ class OC_Contacts_App {
);
}
}
public static function setLastModifiedHeader() {
$rev = $contact->getAsString('REV');
if ($rev) {
$rev = DateTime::createFromFormat(DateTime::W3C, $rev);
OC_Response::setLastModifiedHeader($rev);
}
}
}

View File

@ -14,9 +14,6 @@ OC_Util::checkLoggedIn();
OC_Util::checkAppEnabled('contacts');
$id = $_GET['id'];
if(isset($GET['refresh'])) {
header("Cache-Control: no-cache, no-store, must-revalidate");
}
$contact = OC_Contacts_App::getContactVCard($id);
$image = new OC_Image();
@ -24,16 +21,18 @@ $image = new OC_Image();
if( is_null($contact)) {
OC_Log::write('contacts','photo.php. The VCard for ID '.$id.' is not RFC compatible',OC_Log::ERROR);
} else {
OC_Contacts_App::setLastModifiedHeader($contact);
// Photo :-)
if($image->loadFromBase64($contact->getAsString('PHOTO'))) {
// OK
header('ETag: '.md5($contact->getAsString('PHOTO')));
OC_Response::setETagHeader(md5($contact->getAsString('PHOTO')));
}
else
// Logo :-/
if($image->loadFromBase64($contact->getAsString('LOGO'))) {
// OK
header('ETag: '.md5($contact->getAsString('LOGO')));
OC_Response::setETagHeader(md5($contact->getAsString('LOGO')));
}
if ($image->valid()) {
$max_size = 200;

View File

@ -58,27 +58,9 @@ $thumbnail_size = 23;
// Find the photo from VCard.
$image = new OC_Image();
$photo = $contact->getAsString('PHOTO');
$etag = md5($photo);
$rev_string = $contact->getAsString('REV');
if ($rev_string) {
$rev = DateTime::createFromFormat(DateTime::W3C, $rev_string);
$last_modified_time = $rev->format(DateTime::RFC2822);
} else {
$last_modified_time = null;
}
header('Cache-Control: cache');
header('Pragma: cache');
if ($rev_string) {
header('Last-Modified: '.$last_modified_time);
}
header('ETag: '.$etag);
if (@trim($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time ||
@trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
header('HTTP/1.1 304 Not Modified');
exit;
}
OC_Response::setETagHeader(md5($photo));
OC_Contacts_App::setLastModifiedHeader($contact);
if($image->loadFromBase64($photo)) {
if($image->centerCrop()) {

54
lib/response.php Normal file
View File

@ -0,0 +1,54 @@
<?php
/**
* Copyright (c) 2011 Bart Visscher bartv@thisnet.nl
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
class OC_Response {
const STATUS_NOT_MODIFIED = 304;
static public function enableCaching() {
header('Cache-Control: cache');
header('Pragma: cache');
}
static public function setStatus($status) {
switch($status) {
case self::STATUS_NOT_MODIFIED:
$status = $status . ' Not Modified';
break;
}
header($_SERVER["SERVER_PROTOCOL"].' '.$status);
}
static public function setETagHeader($etag) {
if (empty($etag)) {
return;
}
self::enableCaching();
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
self::setStatus(self::STATUS_NOT_MODIFIED);
exit;
}
header('ETag: '.$etag);
}
static public function setLastModifiedHeader($lastModified) {
if (empty($lastModified)) {
return;
}
if ($lastModified instanceof DateTime) {
$lastModified = $lastModified->format(DateTime::RFC2822);
}
self::enableCaching();
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
trim($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) {
self::setStatus(self::STATUS_NOT_MODIFIED);
exit;
}
header('Last-Modified: '.$lastModified);
}
}