AppleProvisioningPlugin.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Provisioning\Apple;
  7. use OCP\AppFramework\Http;
  8. use OCP\IL10N;
  9. use OCP\IRequest;
  10. use OCP\IURLGenerator;
  11. use OCP\IUserSession;
  12. use Sabre\DAV\Server;
  13. use Sabre\DAV\ServerPlugin;
  14. use Sabre\HTTP\RequestInterface;
  15. use Sabre\HTTP\ResponseInterface;
  16. class AppleProvisioningPlugin extends ServerPlugin {
  17. /**
  18. * @var Server
  19. */
  20. protected $server;
  21. /**
  22. * @var \OC_Defaults
  23. */
  24. protected $themingDefaults;
  25. /**
  26. * AppleProvisioningPlugin constructor.
  27. */
  28. public function __construct(
  29. protected IUserSession $userSession,
  30. protected IURLGenerator $urlGenerator,
  31. \OC_Defaults $themingDefaults,
  32. protected IRequest $request,
  33. protected IL10N $l10n,
  34. protected \Closure $uuidClosure,
  35. ) {
  36. $this->themingDefaults = $themingDefaults;
  37. }
  38. /**
  39. * @param Server $server
  40. */
  41. public function initialize(Server $server) {
  42. $this->server = $server;
  43. $this->server->on('method:GET', [$this, 'httpGet'], 90);
  44. }
  45. /**
  46. * @param RequestInterface $request
  47. * @param ResponseInterface $response
  48. * @return boolean
  49. */
  50. public function httpGet(RequestInterface $request, ResponseInterface $response):bool {
  51. if ($request->getPath() !== 'provisioning/' . AppleProvisioningNode::FILENAME) {
  52. return true;
  53. }
  54. $user = $this->userSession->getUser();
  55. if (!$user) {
  56. return true;
  57. }
  58. $serverProtocol = $this->request->getServerProtocol();
  59. $useSSL = ($serverProtocol === 'https');
  60. if (!$useSSL) {
  61. $response->setStatus(Http::STATUS_OK);
  62. $response->setHeader('Content-Type', 'text/plain; charset=utf-8');
  63. $response->setBody($this->l10n->t('Your %s needs to be configured to use HTTPS in order to use CalDAV and CardDAV with iOS/macOS.', [$this->themingDefaults->getName()]));
  64. return false;
  65. }
  66. $absoluteURL = $this->urlGenerator->getBaseUrl();
  67. $parsedUrl = parse_url($absoluteURL);
  68. $serverPort = $parsedUrl['port'] ?? 443;
  69. $server_url = $parsedUrl['host'];
  70. $description = $this->themingDefaults->getName();
  71. $userId = $user->getUID();
  72. $reverseDomain = implode('.', array_reverse(explode('.', $parsedUrl['host'])));
  73. $caldavUUID = call_user_func($this->uuidClosure);
  74. $carddavUUID = call_user_func($this->uuidClosure);
  75. $profileUUID = call_user_func($this->uuidClosure);
  76. $caldavIdentifier = $reverseDomain . '.' . $caldavUUID;
  77. $carddavIdentifier = $reverseDomain . '.' . $carddavUUID;
  78. $profileIdentifier = $reverseDomain . '.' . $profileUUID;
  79. $caldavDescription = $this->l10n->t('Configures a CalDAV account');
  80. $caldavDisplayname = $description . ' CalDAV';
  81. $carddavDescription = $this->l10n->t('Configures a CardDAV account');
  82. $carddavDisplayname = $description . ' CardDAV';
  83. $filename = $userId . '-' . AppleProvisioningNode::FILENAME;
  84. $xmlSkeleton = $this->getTemplate();
  85. $body = vsprintf($xmlSkeleton, array_map(function (string $v) {
  86. return \htmlspecialchars($v, ENT_XML1, 'UTF-8');
  87. }, [
  88. $description,
  89. $server_url,
  90. $userId,
  91. $serverPort,
  92. $caldavDescription,
  93. $caldavDisplayname,
  94. $caldavIdentifier,
  95. $caldavUUID,
  96. $description,
  97. $server_url,
  98. $userId,
  99. $serverPort,
  100. $carddavDescription,
  101. $carddavDisplayname,
  102. $carddavIdentifier,
  103. $carddavUUID,
  104. $description,
  105. $profileIdentifier,
  106. $profileUUID
  107. ]
  108. ));
  109. $response->setStatus(Http::STATUS_OK);
  110. $response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
  111. $response->setHeader('Content-Type', 'application/xml; charset=utf-8');
  112. $response->setBody($body);
  113. return false;
  114. }
  115. /**
  116. * @return string
  117. */
  118. private function getTemplate():string {
  119. return <<<EOF
  120. <?xml version="1.0" encoding="UTF-8"?>
  121. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  122. <plist version="1.0">
  123. <dict>
  124. <key>PayloadContent</key>
  125. <array>
  126. <dict>
  127. <key>CalDAVAccountDescription</key>
  128. <string>%s</string>
  129. <key>CalDAVHostName</key>
  130. <string>%s</string>
  131. <key>CalDAVUsername</key>
  132. <string>%s</string>
  133. <key>CalDAVUseSSL</key>
  134. <true/>
  135. <key>CalDAVPort</key>
  136. <integer>%s</integer>
  137. <key>PayloadDescription</key>
  138. <string>%s</string>
  139. <key>PayloadDisplayName</key>
  140. <string>%s</string>
  141. <key>PayloadIdentifier</key>
  142. <string>%s</string>
  143. <key>PayloadType</key>
  144. <string>com.apple.caldav.account</string>
  145. <key>PayloadUUID</key>
  146. <string>%s</string>
  147. <key>PayloadVersion</key>
  148. <integer>1</integer>
  149. </dict>
  150. <dict>
  151. <key>CardDAVAccountDescription</key>
  152. <string>%s</string>
  153. <key>CardDAVHostName</key>
  154. <string>%s</string>
  155. <key>CardDAVUsername</key>
  156. <string>%s</string>
  157. <key>CardDAVUseSSL</key>
  158. <true/>
  159. <key>CardDAVPort</key>
  160. <integer>%s</integer>
  161. <key>PayloadDescription</key>
  162. <string>%s</string>
  163. <key>PayloadDisplayName</key>
  164. <string>%s</string>
  165. <key>PayloadIdentifier</key>
  166. <string>%s</string>
  167. <key>PayloadType</key>
  168. <string>com.apple.carddav.account</string>
  169. <key>PayloadUUID</key>
  170. <string>%s</string>
  171. <key>PayloadVersion</key>
  172. <integer>1</integer>
  173. </dict>
  174. </array>
  175. <key>PayloadDisplayName</key>
  176. <string>%s</string>
  177. <key>PayloadIdentifier</key>
  178. <string>%s</string>
  179. <key>PayloadRemovalDisallowed</key>
  180. <false/>
  181. <key>PayloadType</key>
  182. <string>Configuration</string>
  183. <key>PayloadUUID</key>
  184. <string>%s</string>
  185. <key>PayloadVersion</key>
  186. <integer>1</integer>
  187. </dict>
  188. </plist>
  189. EOF;
  190. }
  191. }