canUseWebklex = (version_compare(PHP_VERSION, '8.0') >= 0); if (!$this->canUseWebklex && !function_exists('imap_open')) { throw new Exception("The imap extension is not installed on this server."); } } public function validateUser( #[\SensitiveParameter] ?string $user, #[\SensitiveParameter] ?string $pass) { global $auth; // If required, check that the username is from the permitted domain if (isset($auth['imap_php']['user_domain'])) { if (!filter_var($user, FILTER_VALIDATE_EMAIL)) { return false; } list(, $domain) = explode('@', $user); if ($domain != $auth['imap_php']['user_domain']) { return false; } } if (!$this->canUseWebklex) { return $this->validateUserLegacy($user, $pass); } $cm = new ClientManager(); $config = [ 'host' => $auth['imap_php']['hostname'], 'username' => $user, 'password' => $pass, 'protocol' => 'imap' ]; // The defaults are chosen to be compatible with the legacy behaviour. $config['port'] = $auth['imap_php']['port'] ?? 143; $config['validate_cert'] = empty($auth['imap_php']['novalidate-cert']); if (!empty($auth['imap_php']['ssl'])) { $config['encryption'] = 'ssl'; } elseif (!empty($auth['imap_php']['tls'])) { $config['encryption'] = 'tls'; } else { $config['encryption'] = ''; } try { $client = $cm->make($config); $client->connect(); //Connect to the IMAP Server return $user; } catch (ImapServerErrorException | AuthFailedException $e) { // Don't do anything with these exceptions: they are normal when // authentication fails. } catch (\Exception $e) { // We weren't expecting any other exceptions so trigger an error. $message = "Caught exception '" . get_class($e) . "'\n"; $message .= $e->getMessage() . "\n" . $e->getTraceAsString(); trigger_error($message, E_USER_WARNING); } return false; } private function validateUserLegacy( #[\SensitiveParameter] ?string $user, #[\SensitiveParameter] ?string $pass) { global $auth; $location = '{' . $auth['imap_php']['hostname']; if (isset($auth['imap_php']['port'])) { $location .= ':' . $auth['imap_php']['port']; } $location .= '/imap'; if (!empty($auth['imap_php']['ssl'])) { $location .= '/ssl'; } if (!empty($auth['imap_php']['tls'])) { $location .= '/tls'; } if (!empty($auth['imap_php']['novalidate-cert'])) { $location .= '/novalidate-cert'; } $location .= '}INBOX'; $mbox = imap_open($location, $user, $pass); if ($mbox !== false) { imap_close($mbox); return $user; } return false; } /** */ public function canValidateByEmail() : bool { return true; } /** */ public function canValidateByUsername() : bool { return false; } }