1
0

EnablePlugin.php 3.5 KB

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