HasPhotoPlugin.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CardDAV;
  8. use Sabre\CardDAV\Card;
  9. use Sabre\DAV\INode;
  10. use Sabre\DAV\PropFind;
  11. use Sabre\DAV\Server;
  12. use Sabre\DAV\ServerPlugin;
  13. use Sabre\VObject\Component\VCard;
  14. use Sabre\VObject\Reader;
  15. class HasPhotoPlugin extends ServerPlugin {
  16. /** @var Server */
  17. protected $server;
  18. /**
  19. * Initializes the plugin and registers event handlers
  20. *
  21. * @param Server $server
  22. * @return void
  23. */
  24. public function initialize(Server $server) {
  25. $server->on('propFind', [$this, 'propFind']);
  26. }
  27. /**
  28. * Adds all CardDAV-specific properties
  29. *
  30. * @param PropFind $propFind
  31. * @param INode $node
  32. * @return void
  33. */
  34. public function propFind(PropFind $propFind, INode $node) {
  35. $ns = '{http://nextcloud.com/ns}';
  36. if ($node instanceof Card) {
  37. $propFind->handle($ns . 'has-photo', function () use ($node) {
  38. $vcard = Reader::read($node->get());
  39. return $vcard instanceof VCard
  40. && $vcard->PHOTO
  41. // Either the PHOTO is a url (doesn't start with data:) or the mimetype has to start with image/
  42. && (!str_starts_with($vcard->PHOTO->getValue(), 'data:')
  43. || str_starts_with($vcard->PHOTO->getValue(), 'data:image/'))
  44. ;
  45. });
  46. }
  47. }
  48. /**
  49. * Returns a plugin name.
  50. *
  51. * Using this name other plugins will be able to access other plugins
  52. * using \Sabre\DAV\Server::getPlugin
  53. *
  54. * @return string
  55. */
  56. public function getPluginName() {
  57. return 'vcard-has-photo';
  58. }
  59. /**
  60. * Returns a bunch of meta-data about the plugin.
  61. *
  62. * Providing this information is optional, and is mainly displayed by the
  63. * Browser plugin.
  64. *
  65. * The description key in the returned array may contain html and will not
  66. * be sanitized.
  67. *
  68. * @return array
  69. */
  70. public function getPluginInfo() {
  71. return [
  72. 'name' => $this->getPluginName(),
  73. 'description' => 'Return a boolean stating if the vcard have a photo property set or not.'
  74. ];
  75. }
  76. }