AppleProvisioningPlugin.php 5.8 KB

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