@@ -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 }
0 commit comments