Plugin.php 3.6 KB

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