AppleProvisioningPlugin.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Nils Wittenbrink <nilswittenbrink@web.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\Provisioning\Apple;
  28. use OCP\IL10N;
  29. use OCP\IRequest;
  30. use OCP\IURLGenerator;
  31. use OCP\IUserSession;
  32. use Sabre\DAV\Server;
  33. use Sabre\DAV\ServerPlugin;
  34. use Sabre\HTTP\RequestInterface;
  35. use Sabre\HTTP\ResponseInterface;
  36. class AppleProvisioningPlugin extends ServerPlugin {
  37. /**
  38. * @var Server
  39. */
  40. protected $server;
  41. /**
  42. * @var IURLGenerator
  43. */
  44. protected $urlGenerator;
  45. /**
  46. * @var IUserSession
  47. */
  48. protected $userSession;
  49. /**
  50. * @var \OC_Defaults
  51. */
  52. protected $themingDefaults;
  53. /**
  54. * @var IRequest
  55. */
  56. protected $request;
  57. /**
  58. * @var IL10N
  59. */
  60. protected $l10n;
  61. /**
  62. * @var \Closure
  63. */
  64. protected $uuidClosure;
  65. /**
  66. * AppleProvisioningPlugin constructor.
  67. */
  68. public function __construct(
  69. IUserSession $userSession,
  70. IURLGenerator $urlGenerator,
  71. \OC_Defaults $themingDefaults,
  72. IRequest $request,
  73. IL10N $l10n,
  74. \Closure $uuidClosure
  75. ) {
  76. $this->userSession = $userSession;
  77. $this->urlGenerator = $urlGenerator;
  78. $this->themingDefaults = $themingDefaults;
  79. $this->request = $request;
  80. $this->l10n = $l10n;
  81. $this->uuidClosure = $uuidClosure;
  82. }
  83. /**
  84. * @param Server $server
  85. */
  86. public function initialize(Server $server) {
  87. $this->server = $server;
  88. $this->server->on('method:GET', [$this, 'httpGet'], 90);
  89. }
  90. /**
  91. * @param RequestInterface $request
  92. * @param ResponseInterface $response
  93. * @return boolean
  94. */
  95. public function httpGet(RequestInterface $request, ResponseInterface $response):bool {
  96. if ($request->getPath() !== 'provisioning/' . AppleProvisioningNode::FILENAME) {
  97. return true;
  98. }
  99. $user = $this->userSession->getUser();
  100. if (!$user) {
  101. return true;
  102. }
  103. $serverProtocol = $this->request->getServerProtocol();
  104. $useSSL = ($serverProtocol === 'https');
  105. if (!$useSSL) {
  106. $response->setStatus(200);
  107. $response->setHeader('Content-Type', 'text/plain; charset=utf-8');
  108. $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()]));
  109. return false;
  110. }
  111. $absoluteURL = $this->urlGenerator->getBaseUrl();
  112. $parsedUrl = parse_url($absoluteURL);
  113. if (isset($parsedUrl['port'])) {
  114. $serverPort = $parsedUrl['port'];
  115. } else {
  116. $serverPort = 443;
  117. }
  118. $server_url = $parsedUrl['host'];
  119. $description = $this->themingDefaults->getName();
  120. $userId = $user->getUID();
  121. $reverseDomain = implode('.', array_reverse(explode('.', $parsedUrl['host'])));
  122. $caldavUUID = call_user_func($this->uuidClosure);
  123. $carddavUUID = call_user_func($this->uuidClosure);
  124. $profileUUID = call_user_func($this->uuidClosure);
  125. $caldavIdentifier = $reverseDomain . '.' . $caldavUUID;
  126. $carddavIdentifier = $reverseDomain . '.' . $carddavUUID;
  127. $profileIdentifier = $reverseDomain . '.' . $profileUUID;
  128. $caldavDescription = $this->l10n->t('Configures a CalDAV account');
  129. $caldavDisplayname = $description . ' CalDAV';
  130. $carddavDescription = $this->l10n->t('Configures a CardDAV account');
  131. $carddavDisplayname = $description . ' CardDAV';
  132. $filename = $userId . '-' . AppleProvisioningNode::FILENAME;
  133. $xmlSkeleton = $this->getTemplate();
  134. $body = vsprintf($xmlSkeleton, array_map(function ($v) {
  135. return \htmlspecialchars($v, ENT_XML1, 'UTF-8');
  136. }, [
  137. $description,
  138. $server_url,
  139. $userId,
  140. $serverPort,
  141. $caldavDescription,
  142. $caldavDisplayname,
  143. $caldavIdentifier,
  144. $caldavUUID,
  145. $description,
  146. $server_url,
  147. $userId,
  148. $serverPort,
  149. $carddavDescription,
  150. $carddavDisplayname,
  151. $carddavIdentifier,
  152. $carddavUUID,
  153. $description,
  154. $profileIdentifier,
  155. $profileUUID
  156. ]
  157. ));
  158. $response->setStatus(200);
  159. $response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
  160. $response->setHeader('Content-Type', 'application/xml; charset=utf-8');
  161. $response->setBody($body);
  162. return false;
  163. }
  164. /**
  165. * @return string
  166. */
  167. private function getTemplate():string {
  168. return <<<EOF
  169. <?xml version="1.0" encoding="UTF-8"?>
  170. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  171. <plist version="1.0">
  172. <dict>
  173. <key>PayloadContent</key>
  174. <array>
  175. <dict>
  176. <key>CalDAVAccountDescription</key>
  177. <string>%s</string>
  178. <key>CalDAVHostName</key>
  179. <string>%s</string>
  180. <key>CalDAVUsername</key>
  181. <string>%s</string>
  182. <key>CalDAVUseSSL</key>
  183. <true/>
  184. <key>CalDAVPort</key>
  185. <integer>%s</integer>
  186. <key>PayloadDescription</key>
  187. <string>%s</string>
  188. <key>PayloadDisplayName</key>
  189. <string>%s</string>
  190. <key>PayloadIdentifier</key>
  191. <string>%s</string>
  192. <key>PayloadType</key>
  193. <string>com.apple.caldav.account</string>
  194. <key>PayloadUUID</key>
  195. <string>%s</string>
  196. <key>PayloadVersion</key>
  197. <integer>1</integer>
  198. </dict>
  199. <dict>
  200. <key>CardDAVAccountDescription</key>
  201. <string>%s</string>
  202. <key>CardDAVHostName</key>
  203. <string>%s</string>
  204. <key>CardDAVUsername</key>
  205. <string>%s</string>
  206. <key>CardDAVUseSSL</key>
  207. <true/>
  208. <key>CardDAVPort</key>
  209. <integer>%s</integer>
  210. <key>PayloadDescription</key>
  211. <string>%s</string>
  212. <key>PayloadDisplayName</key>
  213. <string>%s</string>
  214. <key>PayloadIdentifier</key>
  215. <string>%s</string>
  216. <key>PayloadType</key>
  217. <string>com.apple.carddav.account</string>
  218. <key>PayloadUUID</key>
  219. <string>%s</string>
  220. <key>PayloadVersion</key>
  221. <integer>1</integer>
  222. </dict>
  223. </array>
  224. <key>PayloadDisplayName</key>
  225. <string>%s</string>
  226. <key>PayloadIdentifier</key>
  227. <string>%s</string>
  228. <key>PayloadRemovalDisallowed</key>
  229. <false/>
  230. <key>PayloadType</key>
  231. <string>Configuration</string>
  232. <key>PayloadUUID</key>
  233. <string>%s</string>
  234. <key>PayloadVersion</key>
  235. <integer>1</integer>
  236. </dict>
  237. </plist>
  238. EOF;
  239. }
  240. }