photo.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Thomas Tanghus <thomas@tanghus.net>
  4. * Copyright (c) 2011, 2012 Bart Visscher <bartv@thisnet.nl>
  5. * Copyright (c) 2011 Jakob Sack mail@jakobsack.de
  6. * This file is licensed under the Affero General Public License version 3 or
  7. * later.
  8. * See the COPYING-README file.
  9. */
  10. // Init owncloud
  11. OCP\User::checkLoggedIn();
  12. OCP\App::checkAppEnabled('contacts');
  13. function getStandardImage() {
  14. //OCP\Response::setExpiresHeader('P10D');
  15. OCP\Response::enableCaching();
  16. OCP\Response::redirect(OCP\Util::imagePath('contacts', 'person_large.png'));
  17. }
  18. $id = isset($_GET['id']) ? $_GET['id'] : null;
  19. $etag = null;
  20. $caching = null;
  21. if(is_null($id)) {
  22. getStandardImage();
  23. }
  24. if(!extension_loaded('gd') || !function_exists('gd_info')) {
  25. OCP\Util::writeLog('contacts',
  26. 'photo.php. GD module not installed', OCP\Util::DEBUG);
  27. getStandardImage();
  28. }
  29. $contact = OC_Contacts_App::getContactVCard($id);
  30. $image = new OC_Image();
  31. if (!$image) {
  32. getStandardImage();
  33. }
  34. // invalid vcard
  35. if (is_null($contact)) {
  36. OCP\Util::writeLog('contacts',
  37. 'photo.php. The VCard for ID ' . $id . ' is not RFC compatible',
  38. OCP\Util::ERROR);
  39. } else {
  40. // Photo :-)
  41. if ($image->loadFromBase64($contact->getAsString('PHOTO'))) {
  42. // OK
  43. $etag = md5($contact->getAsString('PHOTO'));
  44. }
  45. else
  46. // Logo :-/
  47. if ($image->loadFromBase64($contact->getAsString('LOGO'))) {
  48. // OK
  49. $etag = md5($contact->getAsString('LOGO'));
  50. }
  51. if ($image->valid()) {
  52. $modified = OC_Contacts_App::lastModified($contact);
  53. // Force refresh if modified within the last minute.
  54. if(!is_null($modified)) {
  55. $caching = (time() - $modified->format('U') > 60) ? null : 0;
  56. }
  57. OCP\Response::enableCaching($caching);
  58. if(!is_null($modified)) {
  59. OCP\Response::setLastModifiedHeader($modified);
  60. }
  61. if($etag) {
  62. OCP\Response::setETagHeader($etag);
  63. }
  64. $max_size = 200;
  65. if ($image->width() > $max_size || $image->height() > $max_size) {
  66. $image->resize($max_size);
  67. }
  68. }
  69. }
  70. if (!$image->valid()) {
  71. // Not found :-(
  72. getStandardImage();
  73. }
  74. header('Content-Type: '.$image->mimeType());
  75. $image->show();