HasPhotoPlugin.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\CardDAV;
  27. use Sabre\CardDAV\Card;
  28. use Sabre\DAV\INode;
  29. use Sabre\DAV\PropFind;
  30. use Sabre\DAV\Server;
  31. use Sabre\DAV\ServerPlugin;
  32. use Sabre\VObject\Component\VCard;
  33. use Sabre\VObject\Reader;
  34. class HasPhotoPlugin extends ServerPlugin {
  35. /** @var Server */
  36. protected $server;
  37. /**
  38. * Initializes the plugin and registers event handlers
  39. *
  40. * @param Server $server
  41. * @return void
  42. */
  43. public function initialize(Server $server) {
  44. $server->on('propFind', [$this, 'propFind']);
  45. }
  46. /**
  47. * Adds all CardDAV-specific properties
  48. *
  49. * @param PropFind $propFind
  50. * @param INode $node
  51. * @return void
  52. */
  53. public function propFind(PropFind $propFind, INode $node) {
  54. $ns = '{http://nextcloud.com/ns}';
  55. if ($node instanceof Card) {
  56. $propFind->handle($ns . 'has-photo', function () use ($node) {
  57. $vcard = Reader::read($node->get());
  58. return $vcard instanceof VCard
  59. && $vcard->PHOTO
  60. // Either the PHOTO is a url (doesn't start with data:) or the mimetype has to start with image/
  61. && (strpos($vcard->PHOTO->getValue(), 'data:') !== 0
  62. || strpos($vcard->PHOTO->getValue(), 'data:image/') === 0)
  63. ;
  64. });
  65. }
  66. }
  67. /**
  68. * Returns a plugin name.
  69. *
  70. * Using this name other plugins will be able to access other plugins
  71. * using \Sabre\DAV\Server::getPlugin
  72. *
  73. * @return string
  74. */
  75. public function getPluginName() {
  76. return 'vcard-has-photo';
  77. }
  78. /**
  79. * Returns a bunch of meta-data about the plugin.
  80. *
  81. * Providing this information is optional, and is mainly displayed by the
  82. * Browser plugin.
  83. *
  84. * The description key in the returned array may contain html and will not
  85. * be sanitized.
  86. *
  87. * @return array
  88. */
  89. public function getPluginInfo() {
  90. return [
  91. 'name' => $this->getPluginName(),
  92. 'description' => 'Return a boolean stating if the vcard have a photo property set or not.'
  93. ];
  94. }
  95. }