diff --git a/src/includes/functions.inc.php b/src/includes/functions.inc.php index a2d807c8..269b7c29 100644 --- a/src/includes/functions.inc.php +++ b/src/includes/functions.inc.php @@ -277,6 +277,7 @@ function psm_curl_get($href, $header = false, $body = true, $timeout = 10, $add_ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_ENCODING, ''); curl_setopt($ch, CURLOPT_URL, $href); + if($add_agent) { curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; phpservermon/'.PSM_VERSION.'; +http://www.phpservermonitor.org)'); } @@ -536,9 +537,11 @@ function psm_is_cli() { * @return boolean */ function psm_validate_ipv6($ip) { - // if $ip is a valid ipv6 address it returns true + // Need to remove [] on ipv6 address before we can test + $ip = trim($ip, '[]'); + if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)){ - return true; + return true; } else { return false; } diff --git a/src/psm/Module/Server/Controller/ServerController.class.php b/src/psm/Module/Server/Controller/ServerController.class.php index 6cf8848d..cfa06d0f 100755 --- a/src/psm/Module/Server/Controller/ServerController.class.php +++ b/src/psm/Module/Server/Controller/ServerController.class.php @@ -108,7 +108,7 @@ class ServerController extends AbstractServerController { $servers[$x]['type_icon'] = 'icon-globe'; // add link to label $ip = $servers[$x]['ip']; - if(!empty($servers[$x]['port']) && ($servers[$x]['port'] != 80)) { + if(!empty($servers[$x]['port']) && ($servers[$x]['port'] != 80) && ($servers[$x]['port'] != 443)) { $ip .= ' : ' . $servers[$x]['port']; } $servers[$x]['ip'] = ''.$ip.''; @@ -215,10 +215,12 @@ 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']; + + // Make sure websites start with http:// or https:// if port is 443 + if($clean['type'] == 'website' && !preg_match('#^http(s)?://#', $clean['ip'])) { + $clean['ip'] = ($clean['port'] == 443 ? 'https' : 'http') . '://' . $clean['ip']; } + // check for edit or add if($this->server_id > 0) { diff --git a/src/psm/Util/Updater/StatusUpdater.class.php b/src/psm/Util/Updater/StatusUpdater.class.php index fcfcd354..78818643 100644 --- a/src/psm/Util/Updater/StatusUpdater.class.php +++ b/src/psm/Util/Updater/StatusUpdater.class.php @@ -82,7 +82,7 @@ class StatusUpdater { $this->server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array( 'server_id' => $server_id, ), array( - 'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status', 'active', 'warning_threshold', 'warning_threshold_counter', + 'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status', 'rtime', 'active', 'warning_threshold', 'warning_threshold_counter', )); if(empty($this->server)) { return false; @@ -146,7 +146,7 @@ class StatusUpdater { $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; @@ -169,8 +169,30 @@ class StatusUpdater { * @return boolean */ protected function updateWebsite($max_runs, $run = 1) { + $starttime = microtime(true); - + + // Parse a URL and return its components + $url = parse_url($this->server['ip']); + + // Build url + $this->server['ip'] = $url['scheme'] . '://' . (psm_validate_ipv6($url['host']) ? '['. $url['host'] .']' : $url['host']) . ':'.$this->server['port'] . (isset($url['path']) ? $url['path'] : '') . (isset($url['query']) ? '?'.$url['query'] : ''); + + /** + * + * Need php_http.dll extensions but might be a better tool for the job + * http://stackoverflow.com/questions/14056977/function-http-build-url + + $this->server['ip'] = http_build_url($this->server['ip'], + array( + "scheme" => $url['scheme'], + "host" => (psm_validate_ipv6($url['host']) ? '['. $url['host'] .']' : $url['host']), + "port" => $this->server['port'], + "path" => (isset($url['path']) ? $url['path'] : ''), + "query" => (isset($url['query']) ? '?'.$url['query'] : '') + ), HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT); + */ + // 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( @@ -191,7 +213,7 @@ class StatusUpdater { if(empty($code_matches[0])) { // somehow we dont have a proper response. - $this->error = 'no response from server'; + $this->error = 'No response from server.'; $result = false; } else { $code = $code_matches[1][0]; @@ -235,21 +257,23 @@ class StatusUpdater { // save response time $starttime = microtime(true); - /* 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 + /** + * 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()) { - - // IPv6 ready + if(psm_is_cli()) { + + // if ipv6 we have to use AF_INET6 if (psm_validate_ipv6($this->server['ip'])) { + // Need to remove [] on ipv6 address + $this->server['ip'] = trim($this->server['ip'], '[]'); $socket = socket_create(AF_INET6, SOCK_RAW, 1); } else { $socket = socket_create(AF_INET, SOCK_RAW, 1); } - socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0)); socket_connect($socket, $this->server['ip'], null); socket_send($socket, $package, strLen($package), 0); @@ -266,7 +290,15 @@ class StatusUpdater { } return $status; - //} + // If state on last update was 'on' and the update request is comming from the website + } elseif ($this->server['status'] == 'on') { + // need to set rtime to the value from last update, if not the latency will be 0 + $this->rtime = $this->server['rtime']; + $this->error = 'Update skipped, status will be updated on next cron script run.'; + return true; + } else { + return false; + } } /**