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\CalendarHome;
  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. }
  66. /**
  67. * This initializes the plugin.
  68. *
  69. * This function is called by Sabre\DAV\Server, after
  70. * addPlugin is called.
  71. *
  72. * This method should set up the required event subscriptions.
  73. *
  74. * @param Server $server
  75. */
  76. public function initialize(Server $server) {
  77. $this->server = $server;
  78. $server->on('beforeMethod:*', [$this, 'beforeMethod']);
  79. }
  80. /**
  81. * @param RequestInterface $request
  82. * @param ResponseInterface $response
  83. */
  84. public function beforeMethod(RequestInterface $request, ResponseInterface $response) {
  85. if (!$this->enabled) {
  86. return;
  87. }
  88. $path = $request->getPath();
  89. $pathParts = explode('/', ltrim($path, '/'));
  90. if (\count($pathParts) < 2) {
  91. return;
  92. }
  93. // $calendarHomePath will look like: calendars/username
  94. $calendarHomePath = $pathParts[0] . '/' . $pathParts[1];
  95. try {
  96. $calendarHome = $this->server->tree->getNodeForPath($calendarHomePath);
  97. if (!($calendarHome instanceof CalendarHome)) {
  98. //how did we end up here?
  99. return;
  100. }
  101. $calendarHome->enableCachedSubscriptionsForThisRequest();
  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. }