@@ -16,6 +16,7 @@ class Client implements SieveClient
16
16
{
17
17
const SUPPORTED_AUTH_MECHS = ["DIGEST-MD5 " , "PLAIN " , "LOGIN " , "EXTERNAL " , "OAUTHBEARER " , "XOAUTH2 " ];
18
18
const KNOWN_CAPABILITIES = ["IMPLEMENTATION " , "SASL " , "SIEVE " , "STARTTLS " , "NOTIFY " , "LANGUAGE " , "VERSION " ];
19
+ private static $ connectionPool = [];
19
20
20
21
private $ readSize = 4096 ;
21
22
private $ readTimeout = 5 ;
@@ -596,6 +597,17 @@ public function getCapabilities() {
596
597
* @throws SocketException|SieveException
597
598
*/
598
599
public function connect ($ username , $ password , $ tls =false , $ authz_id ="" , $ auth_mechanism =null ) {
600
+ $ connectionKey = $ this ->getConnectionKey ($ username , $ tls );
601
+ if (isset (self ::$ connectionPool [$ connectionKey ])) {
602
+ $ connection = self ::$ connectionPool [$ connectionKey ];
603
+ if ($ this ->isConnectionValid ($ connection )) {
604
+ $ this ->socket = $ connection ;
605
+ return true ;
606
+ } else {
607
+ unset(self ::$ connectionPool [$ connectionKey ]);
608
+ }
609
+ }
610
+
599
611
$ ctx = stream_context_create ();
600
612
stream_context_set_option ($ ctx , 'ssl ' , 'verify_peer_name ' , false );
601
613
stream_context_set_option ($ ctx , 'ssl ' , 'verify_peer ' , false );
@@ -606,7 +618,7 @@ public function connect($username, $password, $tls=false, $authz_id="", $auth_me
606
618
}
607
619
608
620
$ this ->connected = true ;
609
-
621
+ self :: $ connectionPool [ $ connectionKey ] = $ this -> socket ;
610
622
if (!$ this ->getCapabilitiesFromServer ()) {
611
623
throw new SocketException ("Failed to read capabilities from the server " );
612
624
}
@@ -616,6 +628,15 @@ public function connect($username, $password, $tls=false, $authz_id="", $auth_me
616
628
throw new SieveException ("Error while trying to connect to ManageSieve " );
617
629
}
618
630
631
+ private function getConnectionKey ($ username , $ tls ) {
632
+ return md5 ($ username . '| ' . ($ tls ? 'TLS ' : 'PLAIN ' ) . '| ' . $ this ->addr . ': ' . $ this ->port );
633
+ }
634
+
635
+ private function isConnectionValid ($ socket ) {
636
+ return is_resource ($ socket ) && !feof ($ socket );
637
+ }
638
+
639
+
619
640
/**
620
641
* @return void
621
642
*/
0 commit comments