Skip to content

Commit 2c35626

Browse files
author
tchapi
committed
Fixups
1 parent e500d40 commit 2c35626

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

.env

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ AUTH_REALM=SabreDAV
4848
# "Basic", "IMAP", or "LDAP"
4949
AUTH_METHOD=Basic
5050

51-
# In case of IMAP Auth, you must specify the url of the mailbox in the following format {host[:port]}.
51+
# In case of IMAP Auth, you must specify the url of the mailbox in the following format host[:port].
5252
IMAP_AUTH_URL=null
53+
IMAP_ENCRYPTION_METHOD=ssl
54+
IMAP_CERTIFICATE_VALIDATION=true
5355
IMAP_AUTH_USER_AUTOCREATE=false
5456

5557
# In case of LDAP Auth, you must specify the url of the LDAP server

config/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ services:
3434
arguments:
3535
$IMAPAuthUrl: "%env(IMAP_AUTH_URL)%"
3636
$autoCreate: "%env(bool:IMAP_AUTH_USER_AUTOCREATE)%"
37+
$IMAPEncryptionMethod: "%env(IMAP_ENCRYPTION_METHOD)%"
38+
$IMAPCertificateValidation: "%env(bool:IMAP_CERTIFICATE_VALIDATION)%"
3739

3840
App\Services\LDAPAuth:
3941
arguments:

src/Services/BirthdayService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function buildDataFromContact(string $cardData): ?VCalendar
229229
$vEvent->{'TRANSP'} = 'TRANSPARENT';
230230

231231
// Set a reminder, if needed
232-
if (strtolower($this->birthdayReminderOffset) !== "false") {
232+
if ('false' !== strtolower($this->birthdayReminderOffset)) {
233233
$alarm = $vCal->createComponent('VALARM');
234234
$alarm->add($vCal->createProperty('TRIGGER', $this->birthdayReminderOffset, ['VALUE' => 'DURATION']));
235235
$alarm->add($vCal->createProperty('ACTION', 'DISPLAY'));

src/Services/IMAPAuth.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,41 @@ final class IMAPAuth extends AbstractBasic
3333
private $autoCreate;
3434

3535
/**
36-
* IMAP server in the form {host[:port]}.
36+
* IMAP server in the form host[:port].
3737
*
3838
* @var string
3939
*/
4040
private $IMAPAuthUrl;
4141

42-
public function __construct(ManagerRegistry $doctrine, Utils $utils, string $IMAPAuthUrl, bool $autoCreate)
42+
/**
43+
* IMAP encryption method. Could be ssl, tls or false.
44+
*
45+
* @var mixed (string or bool)
46+
*/
47+
private $IMAPEncryptionMethod;
48+
49+
/**
50+
* Should we validate the certificate?
51+
*
52+
* @var bool
53+
*/
54+
private $IMAPCertificateValidation;
55+
56+
public function __construct(ManagerRegistry $doctrine, Utils $utils, string $IMAPAuthUrl, bool $autoCreate, string $IMAPEncryptionMethod, bool $IMAPCertificateValidation)
4357
{
4458
$this->IMAPAuthUrl = $IMAPAuthUrl;
4559

60+
// We're making sure that only ssl, tls or 'false' are passed down to the IMAP client
61+
$IMAPEncryptionMethodCleaned = strtolower($IMAPEncryptionMethod);
62+
if ('false' === $IMAPEncryptionMethodCleaned) {
63+
$this->IMAPEncryptionMethod = false;
64+
} elseif ('tls' === $IMAPEncryptionMethodCleaned) {
65+
$this->IMAPEncryptionMethod = 'tls';
66+
} else {
67+
$this->IMAPEncryptionMethod = 'ssl';
68+
}
69+
$this->IMAPCertificateValidation = $IMAPCertificateValidation;
70+
4671
$this->autoCreate = $autoCreate;
4772

4873
$this->doctrine = $doctrine;
@@ -55,7 +80,6 @@ public function __construct(ManagerRegistry $doctrine, Utils $utils, string $IMA
5580
*/
5681
protected function imapOpen(string $username, string $password): bool
5782
{
58-
// $cm = new ClientManager('path/to/config/imap.php');
5983
$cm = new ClientManager($options = []);
6084

6185
$components = parse_url($this->IMAPAuthUrl);
@@ -66,12 +90,21 @@ protected function imapOpen(string $username, string $password): bool
6690
return false;
6791
}
6892

93+
// Trying to choose the best port if it was not provided
94+
if ($components['port']) {
95+
$port = $components['port'];
96+
} elseif (false === $this->IMAPEncryptionMethod) {
97+
$port = 143;
98+
} else {
99+
$port = 993;
100+
}
101+
69102
// Create a new instance of the IMAP client manually
70103
$client = $cm->make([
71104
'host' => $components['host'],
72-
'port' => $components['port'] ?? 993,
73-
'encryption' => 'ssl',
74-
'validate_cert' => true,
105+
'port' => $port,
106+
'encryption' => $this->IMAPEncryptionMethod,
107+
'validate_cert' => $this->IMAPCertificateValidation,
75108
'username' => $username,
76109
'password' => $password,
77110
'protocol' => 'imap',
@@ -110,7 +143,7 @@ protected function imapOpen(string $username, string $password): bool
110143
/**
111144
* Validates a username and password by trying to authenticate against IMAP.
112145
*/
113-
protected function validateUserPass($username, $password)
146+
protected function validateUserPass($username, $password): bool
114147
{
115148
return $this->imapOpen($username, $password);
116149
}

templates/dashboard.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<span>{{ "dashboard.version"|trans }} : <code>{{ version }}</code> (SabreDAV <code>{{ sabredav_version }}</code>)</span>
4343
<a class="github-link" href="https://github.com/tchapi/davis" data-bs-toggle="popover" data-bs-trigger="hover" data-bs-content="Project Github page" data-bs-placement="bottom"></a>
4444
</li>
45-
<li class="list-group-item list-group-item-secondary">{{ "dashboard.auth"|trans }} : <code>{{ authMethod }}</code> ({{ "dashboard.auth_realm"|trans }}: <code>{{ authRealm }}</code>)</li>
45+
<li class="list-group-item list-group-item-secondary">{{ "dashboard.auth"|trans }} : <code>{{ authMethod }}</code>{% if authMethod == 'Basic' %} ({{ "dashboard.auth_realm"|trans }}: <code>{{ authRealm }}</code>){% endif %}</li>
4646
<li class="list-group-item list-group-item-secondary">{{ "dashboard.invite_from_address"|trans }} : <code>{{ invite_from_address|default('Not set') }}</code></li>
4747
<li class="list-group-item list-group-item-secondary d-flex justify-content-between align-items-center ">
4848
<span>{{ "dashboard.server_timezone"|trans }} : <code>{{ timezone.actual_default }}</code></span>

0 commit comments

Comments
 (0)