AppleProvisioningPlugin.php 6.8 KB

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