From b1c96cabcd4fd6a38fe48a0ec8f2118509933708 Mon Sep 17 00:00:00 2001 From: Samuel Denis-D'Ortun Date: Wed, 18 Jun 2014 22:48:27 -0400 Subject: [PATCH] Basic IMAP Updater Signed-off-by: Samuel Denis-D'Ortun --- src/lang/en_US.lang.php | 1 + .../Util/Updater/Updaters/Imap.Updater.php | 94 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/psm/Util/Updater/Updaters/Imap.Updater.php diff --git a/src/lang/en_US.lang.php b/src/lang/en_US.lang.php index 919a9377..60d9fb8c 100644 --- a/src/lang/en_US.lang.php +++ b/src/lang/en_US.lang.php @@ -154,6 +154,7 @@ $sm_lang = array( 'error_server_ip_bad_length' => 'The domain / IP must be between 1 and 255 characters.', 'error_server_ip_bad_service' => 'The IP address is not valid.', 'error_server_ip_bad_website' => 'The website URL is not valid.', + 'error_server_ip_bad_imap' => 'The IMAP URL need to be in format imap://username:password@hostname/options ', 'error_server_type_invalid' => 'The selected server type is invalid.', 'error_server_warning_threshold_invalid' => 'The warning threshold must be a valid integer greater than 0.', ), diff --git a/src/psm/Util/Updater/Updaters/Imap.Updater.php b/src/psm/Util/Updater/Updaters/Imap.Updater.php new file mode 100644 index 00000000..0e72f2c2 --- /dev/null +++ b/src/psm/Util/Updater/Updaters/Imap.Updater.php @@ -0,0 +1,94 @@ +. + * + * @package phpservermon + * @author Samuel Denis-D'Ortun + * @copyright Copyright (c) 2014 Samuel Denis-D'Ortun + * @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3 + * @version Release: @package_version@ + * @link http://www.phpservermonitor.org/ + **/ +namespace psm\Util\Updater\Types; + + +require_once 'AbstractUpdater.php'; + +use psm\Service\Database; +use psm\Util\Updater; + + +class ImapUpdater extends AbstractUpdater +{ + + public function GetIcon(){ + return 'icon-globe'; + } + + + public function ValidateIP($ip) + { + // make sure websites start with http:// + if(substr($ip, 0, 7) != 'imap://') { + throw new \InvalidArgumentException('server_ip_bad_imap'); + } + if(!filter_var($ip, FILTER_VALIDATE_URL)) { + throw new \InvalidArgumentException('server_ip_bad_imap'); + } + } + + /** + * Check the current server as a IMAP Server + * + * Allow to open connections to IMAP server. + * + * IP must be provided in a URL encoded string (See PHP imap_open()) + * - imap://user:pass@domain.com/OPTIONS + * + * When host require full email address, replace the @ with %40 in the user field. + * - imap://mailbox%40domain.com:mypassword@domain.com/novalidate-cert + * + * @param array $server + * @return boolean + */ + public function Update($server) { + + $this->StartRun(); + + $url = parse_url($server['ip']); + + imap_errors(); // clean error list + + $stream = @imap_open( '{' . $url['host'] . $url['path'] . '}' ,urldecode( $url['user'] ) ,urldecode($url['pass']) , OP_HALFOPEN ); + + if($stream === false) + { + $errors = @imap_errors() ; + + $this->SetError( implode(" ; " , $errors)); + } + else + imap_close($stream); + + $result = ($stream !== false) ? true: false; + + $this->StopRun(); + + return $result; + } +}