You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Plugin.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018 Georg Ehrke <oc.list@georgehrke.com>
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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. * @var string[]
  39. */
  40. const ENABLE_FOR_CLIENTS = [];
  41. /**
  42. * @var bool
  43. */
  44. private $enabled=false;
  45. /**
  46. * @var Server
  47. */
  48. private $server;
  49. /**
  50. * Plugin constructor.
  51. *
  52. * @param IRequest $request
  53. */
  54. public function __construct(IRequest $request) {
  55. if ($request->isUserAgent(self::ENABLE_FOR_CLIENTS)) {
  56. $this->enabled = true;
  57. }
  58. $magicHeader = $request->getHeader('X-NC-CalDAV-Webcal-Caching');
  59. if ($magicHeader === 'On') {
  60. $this->enabled = true;
  61. }
  62. }
  63. /**
  64. * This initializes the plugin.
  65. *
  66. * This function is called by Sabre\DAV\Server, after
  67. * addPlugin is called.
  68. *
  69. * This method should set up the required event subscriptions.
  70. *
  71. * @param Server $server
  72. */
  73. public function initialize(Server $server) {
  74. $this->server = $server;
  75. $server->on('beforeMethod:*', [$this, 'beforeMethod']);
  76. }
  77. /**
  78. * @param RequestInterface $request
  79. * @param ResponseInterface $response
  80. */
  81. public function beforeMethod(RequestInterface $request, ResponseInterface $response) {
  82. if (!$this->enabled) {
  83. return;
  84. }
  85. $path = $request->getPath();
  86. $pathParts = explode('/', ltrim($path, '/'));
  87. if (\count($pathParts) < 2) {
  88. return;
  89. }
  90. // $calendarHomePath will look like: calendars/username
  91. $calendarHomePath = $pathParts[0] . '/' . $pathParts[1];
  92. try {
  93. $calendarHome = $this->server->tree->getNodeForPath($calendarHomePath);
  94. if (!($calendarHome instanceof CalendarHome)) {
  95. //how did we end up here?
  96. return;
  97. }
  98. $calendarHome->enableCachedSubscriptionsForThisRequest();
  99. } catch (NotFound $ex) {
  100. return;
  101. }
  102. }
  103. /**
  104. * @return bool
  105. */
  106. public function isCachingEnabledForThisRequest():bool {
  107. return $this->enabled;
  108. }
  109. /**
  110. * This method should return a list of server-features.
  111. *
  112. * This is for example 'versioning' and is added to the DAV: header
  113. * in an OPTIONS response.
  114. *
  115. * @return string[]
  116. */
  117. public function getFeatures():array {
  118. return ['nc-calendar-webcal-cache'];
  119. }
  120. /**
  121. * Returns a plugin name.
  122. *
  123. * Using this name other plugins will be able to access other plugins
  124. * using Sabre\DAV\Server::getPlugin
  125. *
  126. * @return string
  127. */
  128. public function getPluginName():string {
  129. return 'nc-calendar-webcal-cache';
  130. }
  131. }