EnablePlugin.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @copyright 2017, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\CalDAV\BirthdayCalendar;
  26. use OCA\DAV\CalDAV\BirthdayService;
  27. use OCA\DAV\CalDAV\CalendarHome;
  28. use OCP\IConfig;
  29. use Sabre\DAV\Server;
  30. use Sabre\DAV\ServerPlugin;
  31. use Sabre\HTTP\RequestInterface;
  32. use Sabre\HTTP\ResponseInterface;
  33. /**
  34. * Class EnablePlugin
  35. * allows users to re-enable the birthday calendar via CalDAV
  36. *
  37. * @package OCA\DAV\CalDAV\BirthdayCalendar
  38. */
  39. class EnablePlugin extends ServerPlugin {
  40. public const NS_Nextcloud = 'http://nextcloud.com/ns';
  41. /**
  42. * @var IConfig
  43. */
  44. protected $config;
  45. /**
  46. * @var BirthdayService
  47. */
  48. protected $birthdayService;
  49. /**
  50. * @var Server
  51. */
  52. protected $server;
  53. /**
  54. * PublishPlugin constructor.
  55. *
  56. * @param IConfig $config
  57. * @param BirthdayService $birthdayService
  58. */
  59. public function __construct(IConfig $config, BirthdayService $birthdayService) {
  60. $this->config = $config;
  61. $this->birthdayService = $birthdayService;
  62. }
  63. /**
  64. * This method should return a list of server-features.
  65. *
  66. * This is for example 'versioning' and is added to the DAV: header
  67. * in an OPTIONS response.
  68. *
  69. * @return string[]
  70. */
  71. public function getFeatures() {
  72. return ['nc-enable-birthday-calendar'];
  73. }
  74. /**
  75. * Returns a plugin name.
  76. *
  77. * Using this name other plugins will be able to access other plugins
  78. * using Sabre\DAV\Server::getPlugin
  79. *
  80. * @return string
  81. */
  82. public function getPluginName() {
  83. return 'nc-enable-birthday-calendar';
  84. }
  85. /**
  86. * This initializes the plugin.
  87. *
  88. * This function is called by Sabre\DAV\Server, after
  89. * addPlugin is called.
  90. *
  91. * This method should set up the required event subscriptions.
  92. *
  93. * @param Server $server
  94. */
  95. public function initialize(Server $server) {
  96. $this->server = $server;
  97. $this->server->on('method:POST', [$this, 'httpPost']);
  98. }
  99. /**
  100. * We intercept this to handle POST requests on calendar homes.
  101. *
  102. * @param RequestInterface $request
  103. * @param ResponseInterface $response
  104. *
  105. * @return bool|void
  106. */
  107. public function httpPost(RequestInterface $request, ResponseInterface $response) {
  108. $node = $this->server->tree->getNodeForPath($this->server->getRequestUri());
  109. if (!($node instanceof CalendarHome)) {
  110. return;
  111. }
  112. $requestBody = $request->getBodyAsString();
  113. $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
  114. if ($documentType !== '{'.self::NS_Nextcloud.'}enable-birthday-calendar') {
  115. return;
  116. }
  117. $principalUri = $node->getOwner();
  118. $userId = substr($principalUri, 17);
  119. $this->config->setUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes');
  120. $this->birthdayService->syncUser($userId);
  121. $this->server->httpResponse->setStatus(204);
  122. return false;
  123. }
  124. }