From 9d8cfe0e804d5da40cf1771660e8bf5972c25493 Mon Sep 17 00:00:00 2001 From: Jim Mason Date: Sun, 1 Dec 2019 16:38:59 +0000 Subject: [PATCH] improved reporting of installation and configuration issues satisfies #5 --- Auth/IMAPUserAuthProvider.php | 25 +++++++++++++++++-------- Plugin.php | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/Auth/IMAPUserAuthProvider.php b/Auth/IMAPUserAuthProvider.php index a50b341..744123e 100644 --- a/Auth/IMAPUserAuthProvider.php +++ b/Auth/IMAPUserAuthProvider.php @@ -22,15 +22,15 @@ class IMAPUserAuthProvider implements PasswordAuthenticationProviderInterface { protected $mailbox; protected $domain; - protected $db; + protected $plugin; const DEFAULT_MAILBOX='{%DOMAIN%:993/imap/ssl/novalidate-cert/notls}'; - public function __construct($configModel, $db) { - $this->mailbox = $configModel->get('plugin_imap_user_auth_mailbox', ''); - $this->domain = $configModel->get('plugin_imap_user_auth_domain', ''); + public function __construct($plugin) { + $this->mailbox = $plugin->configModel->get('plugin_imap_user_auth_mailbox', ''); + $this->domain = $plugin->configModel->get('plugin_imap_user_auth_domain', ''); - $this->db = $db; + $this->plugin = $plugin; } public function getUser() { @@ -57,8 +57,17 @@ public function authenticate() { if(empty($mailbox) && !empty($this->domain)) $mailbox = str_replace('%DOMAIN%', $this->domain, self::DEFAULT_MAILBOX); - if(!function_exists('imap_open') || empty($mailbox)) - return false; + if(!function_exists('imap_open')) { + $this->plugin->raiseException([ + t("The PHP IMAP module is missing."), + t("See the [Documentation]({PluginHomepage}) for setup instructions.") + ]); + } else if(empty($mailbox)) { + $this->plugin->raiseException([ + t("The plugin's configuration settings are missing."), + t("See the [Documentation]({PluginHomepage}) for setup instructions.") + ]); + } // supply curly braces if not already present if(strpos('x' . trim($mailbox), '{', 1) != 1) @@ -74,7 +83,7 @@ public function authenticate() { } // lookup user in the local database - $user = $this->db + $user = $this->plugin->db ->table(UserModel::TABLE) ->columns('id') ->eq('username', $this->username) diff --git a/Plugin.php b/Plugin.php index 9ea65c3..9f42bad 100644 --- a/Plugin.php +++ b/Plugin.php @@ -19,7 +19,7 @@ class Plugin extends Base public function initialize() { $this->authenticationManager->register( - new IMAPUserAuthProvider($this->configModel, $this->db)); + new IMAPUserAuthProvider($this)); $this->template->hook->attach('template:config:integrations', 'IMAPUserAuth:config'); } @@ -46,12 +46,40 @@ public function getPluginAuthor() public function getPluginVersion() { - return '1.0.2'; + return '1.0.3'; } public function getPluginHomepage() { return 'https://github.com/RocketMan/plugin-imap-user-auth'; } + + /** + * replace all instances of {PluginXXX} with the results of the + * evaluation $this->getPluginXXX() + */ + public function resolveMetadata($text) + { + return preg_replace_callback('/\{Plugin(.*?)\}/', function($matched) { + return call_user_func([$this, "getPlugin".$matched[1]]); }, $text); + } + + /** + * resolve all metadata and Markdown hyperlinks in the supplied text + */ + public function resolveMarkdown($text) + { + return preg_replace('/\[(.*?)\]\((.*?)\)/', "$1", + $this->resolveMetadata($text)); + } + + public function raiseException($messages) + { + $output = ""; + foreach($messages as $message) + $output .= "

".$this->resolveMarkdown($message)."

"; + + throw new \LogicException($output); + } }