From 0917bdecddd74a48ee2b21f18e184c579d156b62 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Sun, 12 Feb 2012 17:20:30 +0100 Subject: [PATCH] Contacts: Move response caching to OC_Response --- apps/contacts/lib/app.php | 8 ++++++ apps/contacts/photo.php | 9 +++---- apps/contacts/thumbnail.php | 22 ++------------- lib/response.php | 54 +++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 25 deletions(-) create mode 100644 lib/response.php diff --git a/apps/contacts/lib/app.php b/apps/contacts/lib/app.php index d8d1d3ac04..b804983efa 100644 --- a/apps/contacts/lib/app.php +++ b/apps/contacts/lib/app.php @@ -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); + } + } } diff --git a/apps/contacts/photo.php b/apps/contacts/photo.php index 0b7769ebe0..314bce7cec 100644 --- a/apps/contacts/photo.php +++ b/apps/contacts/photo.php @@ -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; diff --git a/apps/contacts/thumbnail.php b/apps/contacts/thumbnail.php index 4f65afb154..d7043b75f0 100644 --- a/apps/contacts/thumbnail.php +++ b/apps/contacts/thumbnail.php @@ -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()) { diff --git a/lib/response.php b/lib/response.php new file mode 100644 index 0000000000..c254ddd10e --- /dev/null +++ b/lib/response.php @@ -0,0 +1,54 @@ +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); + } +}