diff options
Diffstat (limited to 'php')
-rw-r--r-- | php/outlook.php | 114 | ||||
-rw-r--r-- | php/thunderbird.php | 91 |
2 files changed, 205 insertions, 0 deletions
diff --git a/php/outlook.php b/php/outlook.php new file mode 100644 index 0000000..f6edba6 --- /dev/null +++ b/php/outlook.php @@ -0,0 +1,114 @@ +<?php +// Copyright © 2021 Adam Spragg +// +// This file is part of emailautoconf +// +// emailautoconf is free software: you can redistribute it and/or modify +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// emailautoconf is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with emailautoconf. If not, see +// <https://www.gnu.org/licenses/>. +// +// SPDX-License-Identifier: LGPL-3.0-or-later + +// Outlook autoconfiguration described at: +// https://docs.microsoft.com/en-us/previous-versions/office/office-2010/cc511507(v=office.14) +// +// Should be installed at: +// https://[domain]/autodiscover/autodiscover.xml +// https://autodiscover.[domain]/autodiscover/autodiscover.xml + +if ($_SERVER['REQUEST_URI'] == "/autodiscover/autodiscover.xml" + && preg_match('/^autodiscover\.(.*)$/', $_SERVER['HTTP_HOST'], $matches)) +{ + $domain = $matches[1]; +} +else if ($_SERVER['REQUEST_URI'] == "/autodiscover/autodiscover.xml") { + $domain = $_SERVER['HTTP_HOST']; +} +else { + http_response_code(500); + header('Content-Type: text/plain'); + echo "Unable to determine email domain from //".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; + exit(1); +} + +$IMAP = dns_get_record("_imaps._tcp.".$domain, DNS_SRV) + ?: dns_get_record("_imap._tcp.".$domain, DNS_SRV); +$SMTP = dns_get_record("_submission._tcp.".$domain, DNS_SRV); + +if (!$IMAP || !$SMTP) { + http_response_code(500); + header('Content-Type: text/plain'); + if (!$IMAP) { + echo "Missing/unavailable IMAP(S) SRV record for ".$domain; + } + if (!$SMTP) { + echo "Missing/unavailable Submission SRV record for ".$domain; + } + exit(1); +} + +$sitename = getenv("EMAIL_SITENAME") ?: $domain; +$shortname = getenv("EMAIL_SHORTNAME") ?: $sitename; + +if (($xml = simplexml_load_string(file_get_contents('php://input'))) != false + && $xml->registerXPathNamespace('a', + 'http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006') + && ($emails = $xml->xpath("/a:Autodiscover/a:Request/a:EMailAddress")) != false + && $emails != null + && count($emails) == 1 + && preg_match("/^([^@]*)@([^@]*)$/", $emails[0], $parts)) +{ + $login = $parts[1]; +} +else { + $login = ""; +} + +header('Content-Type: application/xml'); +?> +<?xml version="1.0"?> +<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006"> + <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a"> + <User> + <DisplayName><?= htmlspecialchars($sitename, ENT_XML1) ?></DisplayName> + </User> + <Account> + <AccountType>email</AccountType> + <Action>settings</Action> +<?php foreach ($IMAP as $srv) { ?> + <Protocol> + <Type>IMAP</Type> + <Server><?= htmlspecialchars($srv['target'], ENT_XML1) ?></Server> + <Port><?= htmlspecialchars($srv['port'], ENT_XML1) ?></Port> + <DomainRequired>off</DomainRequired> + <SPA>off</SPA> + <SSL>on</SSL> + <AuthRequired>on</AuthRequired> + <LoginName><?= htmlspecialchars($login, ENT_XML1) ?></LoginName> + </Protocol> +<?php } ?> +<?php foreach ($SMTP as $srv) { ?> + <Protocol> + <Type>SMTP</Type> + <Server><?= htmlspecialchars($srv['target'], ENT_XML1) ?></Server> + <Port><?= htmlspecialchars($srv['port'], ENT_XML1) ?></Port> + <DomainRequired>off</DomainRequired> + <SPA>off</SPA> + <SSL>on</SSL> + <AuthRequired>on</AuthRequired> + <LoginName><?= htmlspecialchars($login, ENT_XML1) ?></LoginName> + </Protocol> +<?php } ?> + </Account> + </Response> +</Autodiscover> diff --git a/php/thunderbird.php b/php/thunderbird.php new file mode 100644 index 0000000..cc35f09 --- /dev/null +++ b/php/thunderbird.php @@ -0,0 +1,91 @@ +<?php +// Copyright © 2021 Adam Spragg +// +// This file is part of emailautoconf +// +// emailautoconf is free software: you can redistribute it and/or modify +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// emailautoconf is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with emailautoconf. If not, see +// <https://www.gnu.org/licenses/>. +// +// SPDX-License-Identifier: LGPL-3.0-or-later + +// Thunderbird autoconfiguration described at: +// https://wiki.mozilla.org/Thunderbird:Autoconfiguration +// https://wiki.mozilla.org/Thunderbird:Autoconfiguration:ConfigFileFormat +// +// Should be installed at: +// http://autoconfig.[domain]/mail/config-v1.1.xml +// http://[domain]/.well-known/autoconfig/mail/config-v1.1.xml + +if ($_SERVER['REQUEST_URI'] == "/mail/config-v1.1.xml" + && preg_match('/^autoconfig\.(.*)$/', $_SERVER['HTTP_HOST'], $matches)) +{ + $domain = $matches[1]; +} +else if ($_SERVER['REQUEST_URI'] == "/.well-known/autoconfig/mail/config-v1.1.xml") { + $domain = $_SERVER['HTTP_HOST']; +} +else { + http_response_code(500); + header('Content-Type: text/plain'); + echo "Unable to determine email domain from //".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; + exit(1); +} + +$IMAP = dns_get_record("_imaps._tcp.".$domain, DNS_SRV) + ?: dns_get_record("_imap._tcp.".$domain, DNS_SRV); +$SMTP = dns_get_record("_submission._tcp.".$domain, DNS_SRV); + +if (!$IMAP || !$SMTP) { + http_response_code(500); + header('Content-Type: text/plain'); + if (!$IMAP) { + echo "Missing/unavailable IMAP(S) SRV record for ".$domain; + } + if (!$SMTP) { + echo "Missing/unavailable Submission SRV record for ".$domain; + } + exit(1); +} + +$sitename = getenv("EMAIL_SITENAME") ?: $domain; +$shortname = getenv("EMAIL_SHORTNAME") ?: $sitename; + +header('Content-Type: application/xml'); +?> +<?xml version="1.0"?> +<clientConfig version="1.1"> + <emailProvider id="<?= htmlspecialchars($domain, ENT_XML1) ?>"> + <domain><?= htmlspecialchars($domain, ENT_XML1) ?></domain> + <displayName><?= htmlspecialchars($sitename, ENT_XML1) ?></displayName> + <displayShortName><?= htmlspecialchars($shortname, ENT_XML1) ?></displayShortName> +<?php foreach ($IMAP as $srv) { ?> + <incomingServer type="imap"> + <hostname><?= htmlspecialchars($srv['target'], ENT_XML1) ?></hostname> + <port><?= htmlspecialchars($srv['port'], ENT_XML1) ?></port> + <socketType>SSL</socketType> + <username>%EMAILLOCALPART%</username> + <authentication>password-cleartext</authentication> + </incomingServer> +<?php } ?> +<?php foreach ($SMTP as $srv) { ?> + <outgoingServer type="smtp"> + <hostname><?= htmlspecialchars($srv['target'], ENT_XML1) ?></hostname> + <port><?= htmlspecialchars($srv['port'], ENT_XML1) ?></port> + <socketType>STARTTLS</socketType> + <username>%EMAILLOCALPART%</username> + <authentication>password-cleartext</authentication> + </outgoingServer> +<?php } ?> + </emailProvider> +</clientConfig> |