ImageExportPlugin.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * @author Thomas Müller <thomas.mueller@tmit.eu>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\DAV\CardDAV;
  22. use OCP\ILogger;
  23. use Sabre\CardDAV\Card;
  24. use Sabre\DAV\Server;
  25. use Sabre\DAV\ServerPlugin;
  26. use Sabre\HTTP\RequestInterface;
  27. use Sabre\HTTP\ResponseInterface;
  28. use Sabre\VObject\Parameter;
  29. use Sabre\VObject\Property\Binary;
  30. use Sabre\VObject\Reader;
  31. class ImageExportPlugin extends ServerPlugin {
  32. /** @var Server */
  33. protected $server;
  34. /** @var ILogger */
  35. private $logger;
  36. public function __construct(ILogger $logger) {
  37. $this->logger = $logger;
  38. }
  39. /**
  40. * Initializes the plugin and registers event handlers
  41. *
  42. * @param Server $server
  43. * @return void
  44. */
  45. function initialize(Server $server) {
  46. $this->server = $server;
  47. $this->server->on('method:GET', [$this, 'httpGet'], 90);
  48. }
  49. /**
  50. * Intercepts GET requests on addressbook urls ending with ?photo.
  51. *
  52. * @param RequestInterface $request
  53. * @param ResponseInterface $response
  54. * @return bool|void
  55. */
  56. function httpGet(RequestInterface $request, ResponseInterface $response) {
  57. $queryParams = $request->getQueryParameters();
  58. // TODO: in addition to photo we should also add logo some point in time
  59. if (!array_key_exists('photo', $queryParams)) {
  60. return true;
  61. }
  62. $path = $request->getPath();
  63. $node = $this->server->tree->getNodeForPath($path);
  64. if (!($node instanceof Card)) {
  65. return true;
  66. }
  67. $this->server->transactionType = 'carddav-image-export';
  68. // Checking ACL, if available.
  69. if ($aclPlugin = $this->server->getPlugin('acl')) {
  70. /** @var \Sabre\DAVACL\Plugin $aclPlugin */
  71. $aclPlugin->checkPrivileges($path, '{DAV:}read');
  72. }
  73. if ($result = $this->getPhoto($node)) {
  74. $response->setHeader('Content-Type', $result['Content-Type']);
  75. $response->setStatus(200);
  76. $response->setBody($result['body']);
  77. // Returning false to break the event chain
  78. return false;
  79. }
  80. return true;
  81. }
  82. function getPhoto(Card $node) {
  83. // TODO: this is kind of expensive - load carddav data from database and parse it
  84. // we might want to build up a cache one day
  85. try {
  86. $vObject = $this->readCard($node->get());
  87. if (!$vObject->PHOTO) {
  88. return false;
  89. }
  90. $photo = $vObject->PHOTO;
  91. $type = $this->getType($photo);
  92. $valType = $photo->getValueType();
  93. $val = ($valType === 'URI' ? $photo->getRawMimeDirValue() : $photo->getValue());
  94. return [
  95. 'Content-Type' => $type,
  96. 'body' => $val
  97. ];
  98. } catch(\Exception $ex) {
  99. $this->logger->logException($ex);
  100. }
  101. return false;
  102. }
  103. private function readCard($cardData) {
  104. return Reader::read($cardData);
  105. }
  106. /**
  107. * @param Binary $photo
  108. * @return Parameter
  109. */
  110. private function getType($photo) {
  111. $params = $photo->parameters();
  112. if (isset($params['TYPE']) || isset($params['MEDIATYPE'])) {
  113. /** @var Parameter $typeParam */
  114. $typeParam = isset($params['TYPE']) ? $params['TYPE'] : $params['MEDIATYPE'];
  115. $type = $typeParam->getValue();
  116. if (strpos($type, 'image/') === 0) {
  117. return $type;
  118. } else {
  119. return 'image/' . strtolower($type);
  120. }
  121. }
  122. return '';
  123. }
  124. }