mobileconfig.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. require_once 'inc/prerequisites.inc.php';
  3. if (empty($mailcow_hostname)) {
  4. exit();
  5. }
  6. if (!isset($_SESSION['mailcow_cc_role']) || $_SESSION['mailcow_cc_role'] != 'user') {
  7. session_destroy();
  8. // probably better than appending the whole current http query string
  9. $append_get = (isset($_GET['only_email'])) ? '&only_email' : '';
  10. $append_get .= (isset($_GET['app_password'])) ? '&app_password' : '';
  11. header('Location: index.php?mobileconfig' . $append_get);
  12. die();
  13. }
  14. error_reporting(0);
  15. header('Content-Type: application/x-apple-aspen-config');
  16. header('Content-Disposition: attachment; filename="'.$UI_TEXTS['main_name'].'.mobileconfig"');
  17. $email = $_SESSION['mailcow_cc_username'];
  18. $domain = explode('@', $_SESSION['mailcow_cc_username'])[1];
  19. $identifier = implode('.', array_reverse(preg_split( '/(@|\.)/', $email))) . '.appleprofile.'.preg_replace('/[^a-zA-Z0-9]+/', '', $UI_TEXTS['main_name']);
  20. try {
  21. $stmt = $pdo->prepare("SELECT `name` FROM `mailbox` WHERE `username`= :username");
  22. $stmt->execute(array(':username' => $email));
  23. $MailboxData = $stmt->fetch(PDO::FETCH_ASSOC);
  24. $displayname = htmlspecialchars(empty($MailboxData['name']) ? $email : $MailboxData['name'], ENT_NOQUOTES);
  25. }
  26. catch(PDOException $e) {
  27. $displayname = $email;
  28. }
  29. if (isset($_GET['only_email'])) {
  30. $onlyEmailAccount = true;
  31. $description = 'IMAP';
  32. } else {
  33. $onlyEmailAccount = false;
  34. $description = 'IMAP, CalDAV, CardDAV';
  35. }
  36. if (isset($_GET['app_password'])) {
  37. $app_password = true;
  38. $description .= ' with application password';
  39. if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') !== FALSE)
  40. $platform = 'iPad';
  41. elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== FALSE)
  42. $platform = 'iPhone';
  43. elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Macintosh') !== FALSE)
  44. $platform = 'Mac';
  45. else
  46. $platform = $_SERVER['HTTP_USER_AGENT'];
  47. $password = bin2hex(openssl_random_pseudo_bytes(16));
  48. $attr = array(
  49. 'app_name' => $platform,
  50. 'app_passwd' => $password,
  51. 'app_passwd2' => $password,
  52. 'active' => 1,
  53. 'protocols' => array('imap_access', 'smtp_access'),
  54. );
  55. if (!$onlyEmailAccount) {
  56. $attr['protocols'][] = 'dav_access';
  57. }
  58. app_passwd("add", $attr);
  59. } else {
  60. $app_password = false;
  61. }
  62. echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  63. ?>
  64. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  65. <plist version="1.0">
  66. <dict>
  67. <key>PayloadContent</key>
  68. <array>
  69. <dict>
  70. <key>EmailAccountDescription</key>
  71. <string><?=$email?></string>
  72. <key>EmailAccountType</key>
  73. <string>EmailTypeIMAP</string>
  74. <key>EmailAccountName</key>
  75. <string><?=$displayname?></string>
  76. <key>EmailAddress</key>
  77. <string><?=$email?></string>
  78. <key>IncomingMailServerAuthentication</key>
  79. <string>EmailAuthPassword</string>
  80. <key>IncomingMailServerHostName</key>
  81. <string><?=$autodiscover_config['imap']['server']?></string>
  82. <key>IncomingMailServerPortNumber</key>
  83. <integer><?=$autodiscover_config['imap']['port']?></integer>
  84. <key>IncomingMailServerUseSSL</key>
  85. <true/>
  86. <key>IncomingMailServerUsername</key>
  87. <string><?=$email?></string>
  88. <?php if($app_password === true): ?>
  89. <key>IncomingPassword</key>
  90. <string><?=$password?></string>
  91. <?php endif; ?>
  92. <key>OutgoingMailServerAuthentication</key>
  93. <string>EmailAuthPassword</string>
  94. <key>OutgoingMailServerHostName</key>
  95. <string><?=$autodiscover_config['smtp']['server']?></string>
  96. <key>OutgoingMailServerPortNumber</key>
  97. <integer><?=$autodiscover_config['smtp']['port']?></integer>
  98. <key>OutgoingMailServerUseSSL</key>
  99. <true/>
  100. <key>OutgoingMailServerUsername</key>
  101. <string><?=$email?></string>
  102. <key>OutgoingPasswordSameAsIncomingPassword</key>
  103. <true/>
  104. <key>PayloadDescription</key>
  105. <string>Configures email account.</string>
  106. <key>PayloadDisplayName</key>
  107. <string>IMAP Account (<?=$email?>)</string>
  108. <key>PayloadIdentifier</key>
  109. <string><?=$identifier?>.email</string>
  110. <key>PayloadOrganization</key>
  111. <string></string>
  112. <key>PayloadType</key>
  113. <string>com.apple.mail.managed</string>
  114. <key>PayloadUUID</key>
  115. <string><?=getGUID()?></string>
  116. <key>PayloadVersion</key>
  117. <integer>1</integer>
  118. <key>PreventAppSheet</key>
  119. <false/>
  120. <key>PreventMove</key>
  121. <false/>
  122. <key>SMIMESigningUserOverrideable</key>
  123. <true/>
  124. <key>SMIMESigningCertificateUUIDUserOverrideable</key>
  125. <true/>
  126. <key>SMIMEEncryptByDefaultUserOverrideable</key>
  127. <true/>
  128. <key>SMIMEEncryptionCertificateUUIDUserOverrideable</key>
  129. <true/>
  130. <key>SMIMEEnableEncryptionPerMessageSwitch</key>
  131. <true/>
  132. </dict>
  133. <?php if($onlyEmailAccount === false): ?>
  134. <dict>
  135. <key>CalDAVAccountDescription</key>
  136. <string><?=$email?></string>
  137. <key>CalDAVHostName</key>
  138. <string><?=$autodiscover_config['caldav']['server']?></string>
  139. <key>CalDAVPort</key>
  140. <real><?=$autodiscover_config['caldav']['port']?></real>
  141. <key>CalDAVPrincipalURL</key>
  142. <string>/SOGo/dav/<?=$email?></string>
  143. <key>CalDAVUseSSL</key>
  144. <true/>
  145. <key>CalDAVUsername</key>
  146. <string><?=$email?></string>
  147. <?php if($app_password === true): ?>
  148. <key>CalDAVPassword</key>
  149. <string><?=$password?></string>
  150. <?php endif; ?>
  151. <key>PayloadDescription</key>
  152. <string>Configures CalDAV account.</string>
  153. <key>PayloadDisplayName</key>
  154. <string>CalDAV (<?=$email?>)</string>
  155. <key>PayloadIdentifier</key>
  156. <string><?=$identifier?>.CalDAV</string>
  157. <key>PayloadOrganization</key>
  158. <string></string>
  159. <key>PayloadType</key>
  160. <string>com.apple.caldav.account</string>
  161. <key>PayloadUUID</key>
  162. <string><?=getGUID()?></string>
  163. <key>PayloadVersion</key>
  164. <integer>1</integer>
  165. </dict>
  166. <dict>
  167. <key>CardDAVAccountDescription</key>
  168. <string><?=$email?></string>
  169. <key>CardDAVHostName</key>
  170. <string><?=$autodiscover_config['carddav']['server']?></string>
  171. <key>CardDAVPort</key>
  172. <integer><?=$autodiscover_config['carddav']['port']?></integer>
  173. <key>CardDAVPrincipalURL</key>
  174. <string>/SOGo/dav/<?=$email?></string>
  175. <key>CardDAVUseSSL</key>
  176. <true/>
  177. <key>CardDAVUsername</key>
  178. <string><?=$email?></string>
  179. <?php if($app_password === true): ?>
  180. <key>CardDAVPassword</key>
  181. <string><?=$password?></string>
  182. <?php endif; ?>
  183. <key>PayloadDescription</key>
  184. <string>Configures CardDAV accounts</string>
  185. <key>PayloadDisplayName</key>
  186. <string>CardDAV (<?=$email?>)</string>
  187. <key>PayloadIdentifier</key>
  188. <string><?=$identifier?>.carddav</string>
  189. <key>PayloadOrganization</key>
  190. <string></string>
  191. <key>PayloadType</key>
  192. <string>com.apple.carddav.account</string>
  193. <key>PayloadUUID</key>
  194. <string><?=getGUID()?></string>
  195. <key>PayloadVersion</key>
  196. <integer>1</integer>
  197. </dict>
  198. <?php endif; ?>
  199. </array>
  200. <key>PayloadDescription</key>
  201. <string><?=$description?></string>
  202. <key>PayloadDisplayName</key>
  203. <string><?=$email?></string>
  204. <key>PayloadIdentifier</key>
  205. <string><?=$identifier?></string>
  206. <key>PayloadOrganization</key>
  207. <string><?=$UI_TEXTS['main_name']?></string>
  208. <key>PayloadRemovalDisallowed</key>
  209. <false/>
  210. <key>PayloadType</key>
  211. <string>Configuration</string>
  212. <key>PayloadUUID</key>
  213. <string><?=getGUID()?></string>
  214. <key>PayloadVersion</key>
  215. <integer>1</integer>
  216. </dict>
  217. </plist>