1
0

Plugin.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * /Evolution/ - Gnome Calendar/Evolution
  22. * /KIO/ - KDE PIM/Akonadi
  23. * @var string[]
  24. */
  25. public const ENABLE_FOR_CLIENTS = [
  26. '/^MSFT-WIN-3/',
  27. '/Evolution/',
  28. '/KIO/'
  29. ];
  30. /**
  31. * @var bool
  32. */
  33. private $enabled = false;
  34. /**
  35. * @var Server
  36. */
  37. private $server;
  38. /**
  39. * Plugin constructor.
  40. *
  41. * @param IRequest $request
  42. */
  43. public function __construct(IRequest $request) {
  44. if ($request->isUserAgent(self::ENABLE_FOR_CLIENTS)) {
  45. $this->enabled = true;
  46. }
  47. $magicHeader = $request->getHeader('X-NC-CalDAV-Webcal-Caching');
  48. if ($magicHeader === 'On') {
  49. $this->enabled = true;
  50. }
  51. $isExportRequest = $request->getMethod() === 'GET' && array_key_exists('export', $request->getParams());
  52. if ($isExportRequest) {
  53. $this->enabled = true;
  54. }
  55. }
  56. /**
  57. * This initializes the plugin.
  58. *
  59. * This function is called by Sabre\DAV\Server, after
  60. * addPlugin is called.
  61. *
  62. * This method should set up the required event subscriptions.
  63. *
  64. * @param Server $server
  65. */
  66. public function initialize(Server $server) {
  67. $this->server = $server;
  68. $server->on('beforeMethod:*', [$this, 'beforeMethod'], 15);
  69. }
  70. /**
  71. * @param RequestInterface $request
  72. * @param ResponseInterface $response
  73. */
  74. public function beforeMethod(RequestInterface $request, ResponseInterface $response) {
  75. if (!$this->enabled) {
  76. return;
  77. }
  78. $path = $request->getPath();
  79. if (!str_starts_with($path, 'calendars/')) {
  80. return;
  81. }
  82. $pathParts = explode('/', ltrim($path, '/'));
  83. if (\count($pathParts) < 2) {
  84. return;
  85. }
  86. try {
  87. $calendarRoot = $this->server->tree->getNodeForPath($pathParts[0]);
  88. if ($calendarRoot instanceof CalendarRoot) {
  89. $calendarRoot->enableReturnCachedSubscriptions($pathParts[1]);
  90. }
  91. } catch (NotFound $ex) {
  92. return;
  93. }
  94. }
  95. /**
  96. * @return bool
  97. */
  98. public function isCachingEnabledForThisRequest():bool {
  99. return $this->enabled;
  100. }
  101. /**
  102. * This method should return a list of server-features.
  103. *
  104. * This is for example 'versioning' and is added to the DAV: header
  105. * in an OPTIONS response.
  106. *
  107. * @return string[]
  108. */
  109. public function getFeatures():array {
  110. return ['nc-calendar-webcal-cache'];
  111. }
  112. /**
  113. * Returns a plugin name.
  114. *
  115. * Using this name other plugins will be able to access other plugins
  116. * using Sabre\DAV\Server::getPlugin
  117. *
  118. * @return string
  119. */
  120. public function getPluginName():string {
  121. return 'nc-calendar-webcal-cache';
  122. }
  123. }