Plugin.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\CalDAV\WebcalCaching;
  27. use OCA\DAV\CalDAV\CalendarHome;
  28. use OCP\IRequest;
  29. use Sabre\DAV\Exception\NotFound;
  30. use Sabre\DAV\Server;
  31. use Sabre\DAV\ServerPlugin;
  32. use Sabre\HTTP\RequestInterface;
  33. use Sabre\HTTP\ResponseInterface;
  34. class Plugin extends ServerPlugin {
  35. /**
  36. * list of regular expressions for calendar user agents,
  37. * that do not support subscriptions on their own
  38. *
  39. * @var string[]
  40. */
  41. public const ENABLE_FOR_CLIENTS = [];
  42. /**
  43. * @var bool
  44. */
  45. private $enabled=false;
  46. /**
  47. * @var Server
  48. */
  49. private $server;
  50. /**
  51. * Plugin constructor.
  52. *
  53. * @param IRequest $request
  54. */
  55. public function __construct(IRequest $request) {
  56. if ($request->isUserAgent(self::ENABLE_FOR_CLIENTS)) {
  57. $this->enabled = true;
  58. }
  59. $magicHeader = $request->getHeader('X-NC-CalDAV-Webcal-Caching');
  60. if ($magicHeader === 'On') {
  61. $this->enabled = true;
  62. }
  63. }
  64. /**
  65. * This initializes the plugin.
  66. *
  67. * This function is called by Sabre\DAV\Server, after
  68. * addPlugin is called.
  69. *
  70. * This method should set up the required event subscriptions.
  71. *
  72. * @param Server $server
  73. */
  74. public function initialize(Server $server) {
  75. $this->server = $server;
  76. $server->on('beforeMethod:*', [$this, 'beforeMethod']);
  77. }
  78. /**
  79. * @param RequestInterface $request
  80. * @param ResponseInterface $response
  81. */
  82. public function beforeMethod(RequestInterface $request, ResponseInterface $response) {
  83. if (!$this->enabled) {
  84. return;
  85. }
  86. $path = $request->getPath();
  87. $pathParts = explode('/', ltrim($path, '/'));
  88. if (\count($pathParts) < 2) {
  89. return;
  90. }
  91. // $calendarHomePath will look like: calendars/username
  92. $calendarHomePath = $pathParts[0] . '/' . $pathParts[1];
  93. try {
  94. $calendarHome = $this->server->tree->getNodeForPath($calendarHomePath);
  95. if (!($calendarHome instanceof CalendarHome)) {
  96. //how did we end up here?
  97. return;
  98. }
  99. $calendarHome->enableCachedSubscriptionsForThisRequest();
  100. } catch (NotFound $ex) {
  101. return;
  102. }
  103. }
  104. /**
  105. * @return bool
  106. */
  107. public function isCachingEnabledForThisRequest():bool {
  108. return $this->enabled;
  109. }
  110. /**
  111. * This method should return a list of server-features.
  112. *
  113. * This is for example 'versioning' and is added to the DAV: header
  114. * in an OPTIONS response.
  115. *
  116. * @return string[]
  117. */
  118. public function getFeatures():array {
  119. return ['nc-calendar-webcal-cache'];
  120. }
  121. /**
  122. * Returns a plugin name.
  123. *
  124. * Using this name other plugins will be able to access other plugins
  125. * using Sabre\DAV\Server::getPlugin
  126. *
  127. * @return string
  128. */
  129. public function getPluginName():string {
  130. return 'nc-calendar-webcal-cache';
  131. }
  132. }