AppleQuirksPlugin.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Connector\Sabre;
  7. use Sabre\DAV\Server;
  8. use Sabre\DAV\ServerPlugin;
  9. use Sabre\HTTP\RequestInterface;
  10. use Sabre\HTTP\ResponseInterface;
  11. /**
  12. * A plugin which tries to work-around peculiarities of the MacOS DAV client
  13. * apps. The following problems are addressed:
  14. *
  15. * - OSX calendar client sends REPORT requests to a random principal
  16. * collection but expects to find all principals (forgot to set
  17. * {DAV:}principal-property-search flag?)
  18. */
  19. class AppleQuirksPlugin extends ServerPlugin {
  20. /*
  21. private const OSX_CALENDAR_AGENT = 'CalendarAgent';
  22. private const OSX_DATAACCESSD_AGENT = 'dataaccessd';
  23. private const OSX_ACCOUNTSD_AGENT = 'accountsd';
  24. private const OSX_CONTACTS_AGENT = 'AddressBookCore';
  25. */
  26. private const OSX_AGENT_PREFIX = 'macOS';
  27. /** @var bool */
  28. private $isMacOSDavAgent = false;
  29. /**
  30. * Sets up the plugin.
  31. *
  32. * This method is automatically called by the server class.
  33. *
  34. * @return void
  35. */
  36. public function initialize(Server $server) {
  37. $server->on('beforeMethod:REPORT', [$this, 'beforeReport'], 0);
  38. $server->on('report', [$this, 'report'], 0);
  39. }
  40. /**
  41. * Triggered before any method is handled.
  42. *
  43. * @return void
  44. */
  45. public function beforeReport(RequestInterface $request, ResponseInterface $response) {
  46. $userAgent = $request->getRawServerValue('HTTP_USER_AGENT') ?? 'unknown';
  47. $this->isMacOSDavAgent = $this->isMacOSUserAgent($userAgent);
  48. }
  49. /**
  50. * This method handles HTTP REPORT requests.
  51. *
  52. * @param string $reportName
  53. * @param mixed $report
  54. * @param mixed $path
  55. *
  56. * @return bool
  57. */
  58. public function report($reportName, $report, $path) {
  59. if ($reportName == '{DAV:}principal-property-search' && $this->isMacOSDavAgent) {
  60. /** @var \Sabre\DAVACL\Xml\Request\PrincipalPropertySearchReport $report */
  61. $report->applyToPrincipalCollectionSet = true;
  62. }
  63. return true;
  64. }
  65. /**
  66. * Check whether the given $userAgent string pretends to originate from OSX.
  67. *
  68. * @param string $userAgent
  69. *
  70. * @return bool
  71. */
  72. protected function isMacOSUserAgent(string $userAgent):bool {
  73. return str_starts_with(self::OSX_AGENT_PREFIX, $userAgent);
  74. }
  75. /**
  76. * Decode the given OSX DAV agent string.
  77. *
  78. * @param string $agent
  79. *
  80. * @return null|array
  81. */
  82. protected function decodeMacOSAgentString(string $userAgent):?array {
  83. // OSX agent string is like: macOS/13.2.1 (22D68) dataaccessd/1.0
  84. if (preg_match('|^' . self::OSX_AGENT_PREFIX . '/([0-9]+)\\.([0-9]+)\\.([0-9]+)\s+\((\w+)\)\s+([^/]+)/([0-9]+)(?:\\.([0-9]+))?(?:\\.([0-9]+))?$|i', $userAgent, $matches)) {
  85. return [
  86. 'macOSVersion' => [
  87. 'major' => $matches[1],
  88. 'minor' => $matches[2],
  89. 'patch' => $matches[3],
  90. ],
  91. 'macOSAgent' => $matches[5],
  92. 'macOSAgentVersion' => [
  93. 'major' => $matches[6],
  94. 'minor' => $matches[7] ?? null,
  95. 'patch' => $matches[8] ?? null,
  96. ],
  97. ];
  98. }
  99. return null;
  100. }
  101. }