You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppleProvisioningPlugin.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Nils Wittenbrink <nilswittenbrink@web.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\Provisioning\Apple;
  26. use OCP\IL10N;
  27. use OCP\IRequest;
  28. use OCP\IURLGenerator;
  29. use OCP\IUserSession;
  30. use Sabre\DAV\Server;
  31. use Sabre\DAV\ServerPlugin;
  32. use Sabre\HTTP\RequestInterface;
  33. use Sabre\HTTP\ResponseInterface;
  34. class AppleProvisioningPlugin extends ServerPlugin {
  35. /**
  36. * @var Server
  37. */
  38. protected $server;
  39. /**
  40. * @var IURLGenerator
  41. */
  42. protected $urlGenerator;
  43. /**
  44. * @var IUserSession
  45. */
  46. protected $userSession;
  47. /**
  48. * @var \OC_Defaults
  49. */
  50. protected $themingDefaults;
  51. /**
  52. * @var IRequest
  53. */
  54. protected $request;
  55. /**
  56. * @var IL10N
  57. */
  58. protected $l10n;
  59. /**
  60. * @var \closure
  61. */
  62. protected $uuidClosure;
  63. /**
  64. * AppleProvisioningPlugin constructor.
  65. *
  66. * @param IUserSession $userSession
  67. * @param IURLGenerator $urlGenerator
  68. * @param \OC_Defaults $themingDefaults
  69. * @param IRequest $request
  70. * @param IL10N $l10n
  71. * @param \closure $uuidClosure
  72. */
  73. public function __construct(IUserSession $userSession, IURLGenerator $urlGenerator,
  74. \OC_Defaults $themingDefaults, IRequest $request,
  75. IL10N $l10n, \closure $uuidClosure) {
  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 = (int) $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. }