From e60bd7fb7d388514e41a9546c1a17b18f6c0a867 Mon Sep 17 00:00:00 2001 From: Samuel Denis-D'Ortun Date: Tue, 17 Jun 2014 23:39:04 -0400 Subject: [PATCH 1/3] Draft implementation for Custom status checks #102 Implementation is working, more handlers will be added, and clean-up in StatusUpdater class instansiation need to be done Signed-off-by: Samuel Denis-D'Ortun --- .../Server/Controller/LogController.class.php | 5 +- .../Controller/ServerController.class.php | 38 +++- src/psm/Util/Install/Installer.class.php | 18 +- src/psm/Util/Server/ServerValidator.class.php | 25 +-- src/psm/Util/Updater/StatusUpdater.class.php | 197 +++++++----------- .../Util/Updater/Updaters/AbstractUpdater.php | 60 ++++++ .../Util/Updater/Updaters/Ping.Updater.php | 92 ++++++++ .../Util/Updater/Updaters/Service.Updater.php | 79 +++++++ .../Util/Updater/Updaters/Website.Updater.php | 120 +++++++++++ src/templates/server/server.tpl.html | 6 +- 10 files changed, 489 insertions(+), 151 deletions(-) create mode 100644 src/psm/Util/Updater/Updaters/AbstractUpdater.php create mode 100644 src/psm/Util/Updater/Updaters/Ping.Updater.php create mode 100644 src/psm/Util/Updater/Updaters/Service.Updater.php create mode 100644 src/psm/Util/Updater/Updaters/Website.Updater.php diff --git a/src/psm/Module/Server/Controller/LogController.class.php b/src/psm/Module/Server/Controller/LogController.class.php index 61683541..c3f8262d 100644 --- a/src/psm/Module/Server/Controller/LogController.class.php +++ b/src/psm/Module/Server/Controller/LogController.class.php @@ -54,6 +54,9 @@ class LogController extends AbstractServerController { // get users $users = $this->db->select(PSM_DB_PREFIX.'users', null, array('user_id','name')); + $statusUpdater = new StatusUpdater($this->db); + + $users_labels = array(); foreach ($users as $user) { $users_labels[$user['user_id']] = $user['name']; @@ -67,7 +70,7 @@ class LogController extends AbstractServerController { $record['class'] = ($x & 1) ? 'odd' : 'even'; $record['users'] = ''; $record['server'] = $record['label']; - $record['type_icon'] = ($record['server_type'] == 'website') ? 'icon-globe' : 'icon-cog'; + $record['type_icon'] = $statusUpdater->GetHandler($record['server_type'])->GetIcon() ; $record['type_title'] = psm_get_lang('servers', 'type_' . $record['server_type']); $ip = '(' . $record['ip']; if(!empty($record['port']) && (($record['server_type'] != 'website') || ($record['port'] != 80))) { diff --git a/src/psm/Module/Server/Controller/ServerController.class.php b/src/psm/Module/Server/Controller/ServerController.class.php index ef41d72c..f9224f40 100644 --- a/src/psm/Module/Server/Controller/ServerController.class.php +++ b/src/psm/Module/Server/Controller/ServerController.class.php @@ -84,7 +84,9 @@ class ServerController extends AbstractServerController { $html_actions = ''; } - $sidebar->addButton( + + + $sidebar->addButton( 'update', psm_get_lang('menu', 'server_update'), psm_build_url(array('mod' => 'server_update')), @@ -97,6 +99,8 @@ class ServerController extends AbstractServerController { $servers = $this->getServers(); $server_count = count($servers); + $statusUpdater = new StatusUpdater($this->db); + for ($x = 0; $x < $server_count; $x++) { // template magic: push the actions html to the front of the server array // so the template handler will add it first. that way the other server vars @@ -104,8 +108,9 @@ class ServerController extends AbstractServerController { $servers[$x] = $html_actions + $servers[$x]; $servers[$x]['class'] = ($x & 1) ? 'odd' : 'even'; + + $servers[$x]['type_icon'] = $statusUpdater->GetHandler($servers[$x]['type'])->GetIcon() ; if($servers[$x]['type'] == 'website') { - $servers[$x]['type_icon'] = 'icon-globe'; // add link to label $ip = $servers[$x]['ip']; if(!empty($servers[$x]['port']) && ($servers[$x]['port'] != 80)) { @@ -114,7 +119,6 @@ class ServerController extends AbstractServerController { $servers[$x]['ip'] = ''.$ip.''; $servers[$x]['ip_short'] = $ip; } else { - $servers[$x]['type_icon'] = 'icon-cog'; $servers[$x]['ip_short'] = $servers[$x]['ip'] . ' : ' . $servers[$x]['port']; } if(($servers[$x]['active'] == 'yes')) { @@ -142,6 +146,8 @@ class ServerController extends AbstractServerController { $this->setTemplateId('server_update', 'server/server.tpl.html'); $back_to = isset($_GET['back_to']) ? $_GET['back_to'] : ''; + + $tpl_data = array( 'edit_server_id' => $this->server_id, // form url: @@ -186,13 +192,29 @@ class ServerController extends AbstractServerController { $edit_server[$key] = psm_POST($key, $value); } - $tpl_data = array_merge($tpl_data, array( + // generate language array + $updater = new \psm\Util\Updater\StatusUpdater($this->db); + + $types = array(); + foreach($updater->GetHandlers() as $key => $handler) { + + + $types[] = array( + 'value' => $key, + 'label' => $key, + 'selected' => ($key == $edit_server['type']) ? 'selected="selected"' : '', + ); + } + $this->tpl->addTemplateDataRepeat($this->getTemplateId(), 'types', $types ); + + + + $tpl_data = array_merge($tpl_data, array( 'edit_value_label' => $edit_server['label'], 'edit_value_ip' => $edit_server['ip'], 'edit_value_port' => $edit_server['port'], 'edit_value_pattern' => $edit_server['pattern'], 'edit_value_warning_threshold' => $edit_server['warning_threshold'], - 'edit_type_selected_' . $edit_server['type'] => 'selected="selected"', 'edit_active_selected_' . $edit_server['active'] => 'selected="selected"', 'edit_email_selected_' . $edit_server['email'] => 'selected="selected"', 'edit_sms_selected_' . $edit_server['sms'] => 'selected="selected"', @@ -224,10 +246,8 @@ class ServerController extends AbstractServerController { 'email' => in_array($_POST['email'], array('yes', 'no')) ? $_POST['email'] : 'no', 'sms' => in_array($_POST['sms'], array('yes', 'no')) ? $_POST['sms'] : 'no', ); - // make sure websites start with http:// - if($clean['type'] == 'website' && substr($clean['ip'], 0, 4) != 'http') { - $clean['ip'] = 'http://' . $clean['ip']; - } + $statusUpdater = new \psm\Util\Updater\StatusUpdater($this->db); + $clean['ip'] = $statusUpdater->GetHandler($clean['type'])->PrepareIP($clean['ip']); // validate the lot $server_validator = new \psm\Util\Server\ServerValidator($this->db); diff --git a/src/psm/Util/Install/Installer.class.php b/src/psm/Util/Install/Installer.class.php index 9b8deab7..37f1b2ab 100644 --- a/src/psm/Util/Install/Installer.class.php +++ b/src/psm/Util/Install/Installer.class.php @@ -206,7 +206,7 @@ class Installer { `ip` varchar(100) NOT NULL, `port` int(5) unsigned NOT NULL, `label` varchar(255) NOT NULL, - `type` enum('service','website') NOT NULL default 'service', + `type` varchar(255) NOT NULL default 'service', `pattern` varchar(255) NOT NULL, `status` enum('on','off') NOT NULL default 'on', `error` varchar(255) NULL, @@ -274,6 +274,12 @@ class Installer { // upgrade to 3.1.0 $this->upgrade310(); } + if(version_compare($version_from, '3.1.1', '<')) { + // upgrade to 3.1.1 + $this->upgrade311(); + } + + psm_update_conf('version', $version_to); } @@ -399,4 +405,14 @@ class Installer { ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; $this->execSQL($queries); } + + + + protected function upgrade311(){ + $queries = array(); + $queries[] = "ALTER TABLE `".PSM_DB_PREFIX."servers` CHANGE `type` `type` CHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'service'"; + $this->execSQL($queries); + + + } } diff --git a/src/psm/Util/Server/ServerValidator.class.php b/src/psm/Util/Server/ServerValidator.class.php index 6177e9eb..e67f5220 100644 --- a/src/psm/Util/Server/ServerValidator.class.php +++ b/src/psm/Util/Server/ServerValidator.class.php @@ -27,6 +27,7 @@ **/ namespace psm\Util\Server; +use psm\Util\Updater\StatusUpdater; /** * The ServerValidator helps you to check input data for servers. @@ -39,9 +40,13 @@ class ServerValidator { */ protected $db; - public function __construct(\psm\Service\Database $db) { + protected $statusUpdater ; + public function __construct(\psm\Service\Database $db) { $this->db = $db; - } + + $this->statusUpdater = new StatusUpdater($this->db); + + } /** * Check if the server id exists @@ -86,18 +91,8 @@ class ServerValidator { throw new \InvalidArgumentException('server_ip_bad_length'); } - switch($type) { - case 'website': - if(!filter_var($value, FILTER_VALIDATE_URL)) { - throw new \InvalidArgumentException('server_ip_bad_website'); - } - break; - case 'service': - if(!filter_var($value, FILTER_VALIDATE_IP)) { - throw new \InvalidArgumentException('server_ip_bad_service'); - } - break; - } + + $this->statusUpdater->GetHandler($type)->ValidateIP($value); return true; } @@ -109,7 +104,7 @@ class ServerValidator { * @throws \InvalidArgumentException */ public function type($type) { - if(!in_array($type, array('service', 'website'))) { + if( $this->statusUpdater->GetHandler($type) == null) { throw new \InvalidArgumentException('server_type_invalid'); } return true; diff --git a/src/psm/Util/Updater/StatusUpdater.class.php b/src/psm/Util/Updater/StatusUpdater.class.php index dacd784a..38381aef 100644 --- a/src/psm/Util/Updater/StatusUpdater.class.php +++ b/src/psm/Util/Updater/StatusUpdater.class.php @@ -32,15 +32,14 @@ * @see \psm\Util\Updater\Autorun */ namespace psm\Util\Updater; + use psm\Service\Database; +use psm\Util\Updater\Types; + + + class StatusUpdater { - public $error = ''; - - public $rtime = 0; - - public $status_new = false; - /** * Database service * @var \psm\Service\Database $db @@ -59,10 +58,57 @@ class StatusUpdater { */ protected $server; + protected $handlers = array(); function __construct(Database $db) { $this->db = $db; + + error_reporting(E_ALL); + ini_set('display_errors', 1); + $this->LoadHandlers(); } + + + private function LoadHandlers(){ + + foreach(glob(dirname(__FILE__ ) . DIRECTORY_SEPARATOR . 'Updaters' . DIRECTORY_SEPARATOR . '*.Updater.php') as $path) + { + $filename = basename($path); + + if(!preg_match('/(?P[a-zA-Z0-9]+).Updater.php$/',$filename , $matches)) + continue; + + if($matches === NULL || empty($matches['name'])) + continue; + + $name = $matches['name']; + if(array_key_exists($name , $this->handlers)) + continue; + + include_once $path; + + $class_name = 'psm\\Util\\Updater\\Types\\' . $name . 'Updater'; + + if(!class_exists($class_name)) + continue; + + $this->handlers[strtolower( $name) ] = new $class_name($this->db); + } + } + + + public function GetHandlers(){ + return $this->handlers; + } + + public function GetHandler($name){ + + if(!array_key_exists($name , $this->handlers)) + throw new \InvalidArgumentException('server_type_invalid'); + + return $this->handlers[strtolower( $name) ]; + } + /** * The function its all about. This one checks whether the given ip and port are up and running! * If the server check fails it will try one more time, depending on the $max_runs. @@ -84,31 +130,39 @@ class StatusUpdater { ), array( 'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status', 'active', 'warning_threshold', 'warning_threshold_counter', )); + + if(empty($this->server)) { return false; } - switch($this->server['type']) { - case 'service': - $this->status_new = $this->updateService($max_runs); - break; - case 'website': - $this->status_new = $this->updateWebsite($max_runs); - break; - } + + $cur_run = 1; + + $handler = $this->GetHandler($this->server['type']); + + + $status_new = true; + do{ + + + $status_new = $handler->Update($this->server); + + }while( $status_new == false && $cur_run++ <= $max_runs ) ; + // update server status $save = array( 'last_check' => date('Y-m-d H:i:s'), - 'error' => $this->error, - 'rtime' => $this->rtime, + 'error' => $handler->GetError(), + 'rtime' => $handler->GetRunTime(), ); // log the uptime before checking the warning threshold, // so that the warnings can still be reviewed in the server history. - psm_log_uptime($this->server_id, (int) $this->status_new, $this->rtime); + psm_log_uptime($this->server_id, (int) $status_new , $handler->GetRunTime()); - if($this->status_new == true) { + if($status_new == true) { // if the server is on, add the last_online value and reset the error threshold counter $save['status'] = 'on'; $save['last_online'] = date('Y-m-d H:i:s'); @@ -121,7 +175,7 @@ class StatusUpdater { // the server is offline but the error threshold has not been met yet. // so we are going to leave the status "on" for now while we are in a sort of warning state.. $save['status'] = 'on'; - $this->status_new = true; + $status_new = true; } else { $save['status'] = 'off'; } @@ -129,110 +183,7 @@ class StatusUpdater { $this->db->save(PSM_DB_PREFIX . 'servers', $save, array('server_id' => $this->server_id)); - return $this->status_new; + return $status_new ; } - - /** - * Check the current server as a service - * @param int $max_runs - * @param int $run - * @return boolean - */ - protected function updateService($max_runs, $run = 1) { - $errno = 0; - // save response time - $starttime = microtime(true); - - $fp = fsockopen ($this->server['ip'], $this->server['port'], $errno, $this->error, 10); - - $status = ($fp === false) ? false : true; - $this->rtime = (microtime(true) - $starttime); - - fclose($fp); - - // check if server is available and rerun if asked. - if(!$status && $run < $max_runs) { - return $this->updateService($max_runs, $run + 1); - } - - return $status; - } - - /** - * Check the current server as a website - * @param int $max_runs - * @param int $run - * @return boolean - */ - protected function updateWebsite($max_runs, $run = 1) { - $starttime = microtime(true); - - // We're only interested in the header, because that should tell us plenty! - // unless we have a pattern to search for! - $curl_result = psm_curl_get( - $this->server['ip'], - true, - ($this->server['pattern'] == '' ? false : true) - ); - - $this->rtime = (microtime(true) - $starttime); - - // the first line would be the status code.. - $status_code = strtok($curl_result, "\r\n"); - // keep it general - // $code[1][0] = status code - // $code[2][0] = name of status code - $code_matches = array(); - preg_match_all("/[A-Z]{2,5}\/\d\.\d\s(\d{3})\s(.*)/", $status_code, $code_matches); - - if(empty($code_matches[0])) { - // somehow we dont have a proper response. - $this->error = 'no response from server'; - $result = false; - } else { - $code = $code_matches[1][0]; - $msg = $code_matches[2][0]; - - // All status codes starting with a 4 or higher mean trouble! - if(substr($code, 0, 1) >= '4') { - $this->error = $code . ' ' . $msg; - $result = false; - } else { - $result = true; - } - } - if($this->server['pattern'] != '') { - // Check to see if the pattern was found. - if(!preg_match("/{$this->server['pattern']}/i", $curl_result)) { - $this->error = 'Pattern not found.'; - $result = false; - } - } - - // check if server is available and rerun if asked. - if(!$result && $run < $max_runs) { - return $this->updateWebsite($max_runs, $run + 1); - } - - return $result; - } - - /** - * Get the error returned by the update function - * - * @return string - */ - public function getError() { - return $this->error; - } - - /** - * Get the response time of the server - * - * @return string - */ - public function getRtime() { - return $this->rtime; - } } diff --git a/src/psm/Util/Updater/Updaters/AbstractUpdater.php b/src/psm/Util/Updater/Updaters/AbstractUpdater.php new file mode 100644 index 00000000..5d2f1a6d --- /dev/null +++ b/src/psm/Util/Updater/Updaters/AbstractUpdater.php @@ -0,0 +1,60 @@ +run_start = microtime(true); + + } + + protected function StopRun(){ + $this->run_time = (microtime(true) - $this->run_start); + } + + public function GetRunTime(){ + return $this->run_time; + } + + + public function GetError(){ + return $this->error; + } + + protected function SetError($error){ + $this->error = $error; + } + + + + public function PrepareIP($ip) { + return $ip; + } + public function ValidateIP($ip){ + + } + + abstract public function Update($server); + + + +} + + + + + diff --git a/src/psm/Util/Updater/Updaters/Ping.Updater.php b/src/psm/Util/Updater/Updaters/Ping.Updater.php new file mode 100644 index 00000000..aa25a850 --- /dev/null +++ b/src/psm/Util/Updater/Updaters/Ping.Updater.php @@ -0,0 +1,92 @@ +. + * + * @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 PingUpdater extends AbstractUpdater +{ + + /** + * Check the current server as a ping + * @param array $server + * @return boolean + */ + public function Update($server) { + /** + * Only run if is cron + * socket_create() need to run as root :( + * ugly cli hack i know + * might be a better way still have not found a solution when updating true website + */ + if(psm_is_cli()) { + + + $this->StartRun(); + + // if ipv6 we have to use AF_INET6 + if (psm_validate_ipv6($server['ip'])) { + // Need to remove [] on ipv6 address + $server['ip'] = trim($server['ip'], '[]'); + $socket = socket_create(AF_INET6, SOCK_RAW, 1); + } else { + $socket = socket_create(AF_INET, SOCK_RAW, 1); + } + + $timeout = 1; + $package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost"; /* ICMP ping packet with a pre-calculated checksum */ + + socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0)); + socket_connect($socket, $server['ip'], null); + socket_send($socket, $package, strLen($package), 0); + + // if ping fails it returns false + $status = (socket_read($socket, 255)) ? true : false; + + $this->StopRun(); + + socket_close($socket); + + return $status; + + + // If state on last update was 'on' and the update request is comming from the website + } elseif ($server['status'] == 'on') { + // need to set rtime to the value from last update, if not the latency will be 0 + $this->rtime = $server['rtime']; + $this->SetError( 'Update skipped, status will be updated on next cron script run.'); + return true; + } else { + return false; + } + } +} \ No newline at end of file diff --git a/src/psm/Util/Updater/Updaters/Service.Updater.php b/src/psm/Util/Updater/Updaters/Service.Updater.php new file mode 100644 index 00000000..dc9a28db --- /dev/null +++ b/src/psm/Util/Updater/Updaters/Service.Updater.php @@ -0,0 +1,79 @@ +. + * + * @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 ServiceUpdater extends AbstractUpdater +{ + + public function PrepareIP($ip){ + // make sure websites start with http:// + + return $ip; + } + public function ValidateIP($ip) + { + /* if(!filter_var($ip, FILTER_VALIDATE_IP)) { + throw new \InvalidArgumentException('server_ip_bad_service'); + }*/ + } + + + /** + * Check the current server as a service + * @param array $server + * @return boolean + */ + public function Update($server) { + $errno = 0; + $error = ''; + + $this->StartRun(); + + $fp = fsockopen ($server['ip'], $server['port'], $errno, $error, 10); + + $this->StopRun(); + + + + $status = ($fp === false) ? false : true; + + if($status == false && !empty ($error)) + $this->SetError($error); + + fclose($fp); + + return $status; + } + +} diff --git a/src/psm/Util/Updater/Updaters/Website.Updater.php b/src/psm/Util/Updater/Updaters/Website.Updater.php new file mode 100644 index 00000000..7dc1dd28 --- /dev/null +++ b/src/psm/Util/Updater/Updaters/Website.Updater.php @@ -0,0 +1,120 @@ +. + * + * @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 WebsiteUpdater extends AbstractUpdater +{ + + public function GetIcon(){ + return 'icon-globe'; + } + + public function PrepareIP($ip){ + // make sure websites start with http:// + if(substr($ip, 0, 4) != 'http') { + $ip = 'http://' . $ip; + } + return $ip; + } + public function ValidateIP($ip) + { + if(!filter_var($ip, FILTER_VALIDATE_URL)) { + throw new \InvalidArgumentException('server_ip_bad_website'); + } + } + + + + + /** + * Check the current server as a website + * @param array $server + * @return boolean + */ + public function Update($server) { + + $this->StartRun(); + // We're only interested in the header, because that should tell us plenty! + // unless we have a pattern to search for! + $curl_result = psm_curl_get( + $server['ip'], + true, + ($server['pattern'] == '' ? false : true) + ); + + $this->StopRun(); + + + $result = $this->CheckResponseCode($curl_result ); + + if($result != false && $server['pattern'] != '') { + // Check to see if the pattern was found. + if(!preg_match("/{$server['pattern']}/i", $curl_result)) { + $this->SetError('Pattern not found.'); + $result = false; + } + } + return $result; + } + + + private function CheckResponseCode($curl_result){ + + // the first line would be the status code.. + $status_code = strtok($curl_result, "\r\n"); + // keep it general + // $code[1][0] = status code + // $code[2][0] = name of status code + $code_matches = array(); + preg_match_all("/[A-Z]{2,5}\/\d\.\d\s(\d{3})\s(.*)/", $status_code, $code_matches); + + + if(empty($code_matches[0])) { + // somehow we dont have a proper response. + $this->SetError( 'no response from server'); + return false; + } else { + $code = $code_matches[1][0]; + $msg = $code_matches[2][0]; + + // All status codes starting with a 4 or higher mean trouble! + if(substr($code, 0, 1) >= '4') { + $this->SetError( $code . ' ' . $msg); + return false; + } else { + return true; + } + } + } +} diff --git a/src/templates/server/server.tpl.html b/src/templates/server/server.tpl.html index e62a2b26..b360dfa6 100755 --- a/src/templates/server/server.tpl.html +++ b/src/templates/server/server.tpl.html @@ -96,8 +96,10 @@
From abfc38178adc28d85d5de9cbad386ea0422281c0 Mon Sep 17 00:00:00 2001 From: Samuel Denis-D'Ortun Date: Wed, 18 Jun 2014 21:56:47 -0400 Subject: [PATCH 2/3] Fix namespaces, add manual SetRunTime() method Signed-off-by: Samuel Denis-D'Ortun --- src/psm/Module/Server/Controller/LogController.class.php | 2 +- src/psm/Module/Server/Controller/ServerController.class.php | 2 +- src/psm/Util/Updater/Updaters/AbstractUpdater.php | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/psm/Module/Server/Controller/LogController.class.php b/src/psm/Module/Server/Controller/LogController.class.php index c3f8262d..40c6caac 100644 --- a/src/psm/Module/Server/Controller/LogController.class.php +++ b/src/psm/Module/Server/Controller/LogController.class.php @@ -54,7 +54,7 @@ class LogController extends AbstractServerController { // get users $users = $this->db->select(PSM_DB_PREFIX.'users', null, array('user_id','name')); - $statusUpdater = new StatusUpdater($this->db); + $statusUpdater = new \psm\Util\Updater\StatusUpdater($this->db); $users_labels = array(); diff --git a/src/psm/Module/Server/Controller/ServerController.class.php b/src/psm/Module/Server/Controller/ServerController.class.php index f9224f40..36094e03 100644 --- a/src/psm/Module/Server/Controller/ServerController.class.php +++ b/src/psm/Module/Server/Controller/ServerController.class.php @@ -99,7 +99,7 @@ class ServerController extends AbstractServerController { $servers = $this->getServers(); $server_count = count($servers); - $statusUpdater = new StatusUpdater($this->db); + $statusUpdater = new \psm\Util\Updater\StatusUpdater($this->db); for ($x = 0; $x < $server_count; $x++) { // template magic: push the actions html to the front of the server array diff --git a/src/psm/Util/Updater/Updaters/AbstractUpdater.php b/src/psm/Util/Updater/Updaters/AbstractUpdater.php index 5d2f1a6d..55ff862c 100644 --- a/src/psm/Util/Updater/Updaters/AbstractUpdater.php +++ b/src/psm/Util/Updater/Updaters/AbstractUpdater.php @@ -19,13 +19,17 @@ abstract class AbstractUpdater protected function StartRun(){ $this->run_start = microtime(true); - } + protected function StopRun(){ $this->run_time = (microtime(true) - $this->run_start); } + protected function SetRunTime($time_microsec){ + $this->run_time = $time_microsec; + } + public function GetRunTime(){ return $this->run_time; } 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 3/3] 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; + } +}