diff options
author | Bart Visscher <bartv@thisnet.nl> | 2012-02-12 17:20:30 +0100 |
---|---|---|
committer | Bart Visscher <bartv@thisnet.nl> | 2012-02-12 17:21:33 +0100 |
commit | 0917bdecddd74a48ee2b21f18e184c579d156b62 (patch) | |
tree | 440ef374bcecc71752ecd3fa725165c9a8b94989 | |
parent | a0bb6079c5169f7314b9c82983e7a019469c2a00 (diff) | |
download | nextcloud-server-0917bdecddd74a48ee2b21f18e184c579d156b62.tar.gz nextcloud-server-0917bdecddd74a48ee2b21f18e184c579d156b62.zip |
Contacts: Move response caching to OC_Response
-rw-r--r-- | apps/contacts/lib/app.php | 8 | ||||
-rw-r--r-- | apps/contacts/photo.php | 9 | ||||
-rw-r--r-- | apps/contacts/thumbnail.php | 22 | ||||
-rw-r--r-- | lib/response.php | 54 |
4 files changed, 68 insertions, 25 deletions
diff --git a/apps/contacts/lib/app.php b/apps/contacts/lib/app.php index d8d1d3ac047..b804983efa5 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 0b7769ebe07..314bce7cecc 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 4f65afb1540..d7043b75f04 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 00000000000..c254ddd10e7 --- /dev/null +++ b/lib/response.php @@ -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); + } +} |