You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HasPhotoPlugin.php 2.5KB

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