AppleProvisioningPlugin.php 5.4 KB

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