Plugin.php 3.4 KB

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