1
0

Plugin.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV\WebcalCaching;
  8. use OCA\DAV\CalDAV\CalendarRoot;
  9. use OCP\IRequest;
  10. use Sabre\DAV\Exception\NotFound;
  11. use Sabre\DAV\Server;
  12. use Sabre\DAV\ServerPlugin;
  13. use Sabre\HTTP\RequestInterface;
  14. use Sabre\HTTP\ResponseInterface;
  15. class Plugin extends ServerPlugin {
  16. /**
  17. * list of regular expressions for calendar user agents,
  18. * that do not support subscriptions on their own
  19. *
  20. * /^MSFT-WIN-3/ - Windows 10 Calendar
  21. * @var string[]
  22. */
  23. public const ENABLE_FOR_CLIENTS = [
  24. "/^MSFT-WIN-3/"
  25. ];
  26. /**
  27. * @var bool
  28. */
  29. private $enabled = false;
  30. /**
  31. * @var Server
  32. */
  33. private $server;
  34. /**
  35. * Plugin constructor.
  36. *
  37. * @param IRequest $request
  38. */
  39. public function __construct(IRequest $request) {
  40. if ($request->isUserAgent(self::ENABLE_FOR_CLIENTS)) {
  41. $this->enabled = true;
  42. }
  43. $magicHeader = $request->getHeader('X-NC-CalDAV-Webcal-Caching');
  44. if ($magicHeader === 'On') {
  45. $this->enabled = true;
  46. }
  47. $isExportRequest = $request->getMethod() === 'GET' && array_key_exists('export', $request->getParams());
  48. if ($isExportRequest) {
  49. $this->enabled = true;
  50. }
  51. }
  52. /**
  53. * This initializes the plugin.
  54. *
  55. * This function is called by Sabre\DAV\Server, after
  56. * addPlugin is called.
  57. *
  58. * This method should set up the required event subscriptions.
  59. *
  60. * @param Server $server
  61. */
  62. public function initialize(Server $server) {
  63. $this->server = $server;
  64. $server->on('beforeMethod:*', [$this, 'beforeMethod'], 15);
  65. }
  66. /**
  67. * @param RequestInterface $request
  68. * @param ResponseInterface $response
  69. */
  70. public function beforeMethod(RequestInterface $request, ResponseInterface $response) {
  71. if (!$this->enabled) {
  72. return;
  73. }
  74. $path = $request->getPath();
  75. if (!str_starts_with($path, 'calendars/')) {
  76. return;
  77. }
  78. $pathParts = explode('/', ltrim($path, '/'));
  79. if (\count($pathParts) < 2) {
  80. return;
  81. }
  82. try {
  83. $calendarRoot = $this->server->tree->getNodeForPath($pathParts[0]);
  84. if ($calendarRoot instanceof CalendarRoot) {
  85. $calendarRoot->enableReturnCachedSubscriptions($pathParts[1]);
  86. }
  87. } catch (NotFound $ex) {
  88. return;
  89. }
  90. }
  91. /**
  92. * @return bool
  93. */
  94. public function isCachingEnabledForThisRequest():bool {
  95. return $this->enabled;
  96. }
  97. /**
  98. * This method should return a list of server-features.
  99. *
  100. * This is for example 'versioning' and is added to the DAV: header
  101. * in an OPTIONS response.
  102. *
  103. * @return string[]
  104. */
  105. public function getFeatures():array {
  106. return ['nc-calendar-webcal-cache'];
  107. }
  108. /**
  109. * Returns a plugin name.
  110. *
  111. * Using this name other plugins will be able to access other plugins
  112. * using Sabre\DAV\Server::getPlugin
  113. *
  114. * @return string
  115. */
  116. public function getPluginName():string {
  117. return 'nc-calendar-webcal-cache';
  118. }
  119. }