@@ -16,6 +16,7 @@ class Client implements SieveClient
1616{
1717 const SUPPORTED_AUTH_MECHS = ["DIGEST-MD5 " , "PLAIN " , "LOGIN " , "EXTERNAL " , "OAUTHBEARER " , "XOAUTH2 " ];
1818 const KNOWN_CAPABILITIES = ["IMPLEMENTATION " , "SASL " , "SIEVE " , "STARTTLS " , "NOTIFY " , "LANGUAGE " , "VERSION " ];
19+ private static $ connectionPool = [];
1920
2021 private $ readSize = 4096 ;
2122 private $ readTimeout = 5 ;
@@ -596,6 +597,17 @@ public function getCapabilities() {
596597 * @throws SocketException|SieveException
597598 */
598599 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+
599611 $ ctx = stream_context_create ();
600612 stream_context_set_option ($ ctx , 'ssl ' , 'verify_peer_name ' , false );
601613 stream_context_set_option ($ ctx , 'ssl ' , 'verify_peer ' , false );
@@ -606,7 +618,7 @@ public function connect($username, $password, $tls=false, $authz_id="", $auth_me
606618 }
607619
608620 $ this ->connected = true ;
609-
621+ self :: $ connectionPool [ $ connectionKey ] = $ this -> socket ;
610622 if (!$ this ->getCapabilitiesFromServer ()) {
611623 throw new SocketException ("Failed to read capabilities from the server " );
612624 }
@@ -616,6 +628,15 @@ public function connect($username, $password, $tls=false, $authz_id="", $auth_me
616628 throw new SieveException ("Error while trying to connect to ManageSieve " );
617629 }
618630
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+
619640 /**
620641 * @return void
621642 */
0 commit comments