Use native php function to load information about curl response. Obtain from it HTTP status code, so get rid of regexing. Also used for rediret checks. Better usability in the future.

Removed logging of status message - some HTTP servers (like Tomcat, from version 9 I guess) do not return status message at all (and its not such a big deal to log it)
This commit is contained in:
Matej Kminek 2019-11-20 23:03:58 +01:00
parent 59b47da7e5
commit a5312265f8
2 changed files with 25 additions and 32 deletions

View File

@ -351,7 +351,7 @@ function psm_parse_msg($status, $type, $vars, $combi = false) {
* @param string|bool $website_password Password website
* @param string|null $request_method Request method like GET, POST etc.
* @param string|null $post_field POST data
* @return string cURL result
* @return array ["result" => cURL result, "info" => cURL info array]
*/
function psm_curl_get($href, $header = false, $body = true, $timeout = null, $add_agent = true, $website_username = false, $website_password = false, $request_method = null, $post_field = null) {
($timeout === null || $timeout > 0) ? PSM_CURL_TIMEOUT : intval($timeout);
@ -401,6 +401,8 @@ function psm_curl_get($href, $header = false, $body = true, $timeout = null, $ad
}
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if(defined('PSM_DEBUG') && PSM_DEBUG === true && psm_is_cli()) {
@ -409,7 +411,7 @@ function psm_curl_get($href, $header = false, $body = true, $timeout = null, $ad
echo PHP_EOL.'==============END cURL Resul for: '.$href.'==========================================='.PHP_EOL;
}
return $result;
return ["result" => $result, "info" => $info];
}
/**
@ -477,7 +479,7 @@ function psm_update_available() {
// been more than a week since update, lets go
// update last check date
psm_update_conf('last_update_check', time());
$latest = psm_curl_get(PSM_UPDATE_URL);
$latest = psm_curl_get(PSM_UPDATE_URL)["result"];
// extract latest version from Github.
preg_match('/"tag_name":"[v](([\d][.][\d][.][\d])(-?\w*))"/', $latest, $latest);
// add latest version to database

View File

@ -246,30 +246,22 @@ class StatusUpdater {
$this->server['request_method'],
$this->server['post_field']
);
$this->header = $curl_result;
$this->header = $curl_result["result"];
$code = $curl_result["info"]["http_code"];
$redirectUrl = $curl_result["info"]["redirect_url"];
$this->rtime = (microtime(true) - $starttime);
// the first line would be the status code..
$status_code = strtok($curl_result, "\r\n");
// keep it general
// $code[2][0] = status code
// $code[3][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])) {
if(empty($code)) {
// somehow we dont have a proper response.
$this->error = 'TIMEOUT ERROR: no response from server';
$result = false;
} else {
$code = $code_matches[2][0];
$msg = $code_matches[3][0];
$allow_http_status = explode("|", $this->server['allow_http_status']);
// All status codes starting with a 4 or higher mean trouble!
if (substr($code, 0, 1) >= '4' && !in_array($code ,$allow_http_status)) {
$this->error = "HTTP STATUS ERROR: ".$code.' '.$msg;
$this->error = "HTTP STATUS ERROR: ".$code;
$result = false;
} else {
$result = true;
@ -278,7 +270,7 @@ class StatusUpdater {
if ($this->server['pattern'] != '') {
// Check to see if the body should not contain specified pattern
// Check to see if the pattern was [not] found.
if (($this->server['pattern_online'] == 'yes') == !preg_match("/{$this->server['pattern']}/i", $curl_result)) {
if (($this->server['pattern_online'] == 'yes') == !preg_match("/{$this->server['pattern']}/i", $this->header)) {
$this->error = "TEXT ERROR : Pattern '{$this->server['pattern']}' ".
($this->server['pattern_online'] == 'yes' ? 'not' : 'was').
' found.';
@ -286,24 +278,23 @@ class StatusUpdater {
}
}
// Check if the website redirects to another domain
if ($this->server['redirect_check'] == 'bad'){
$location_matches = array();
preg_match('/([Ll]ocation: )(https*:\/\/)(www.)?([a-zA-Z.:0-9]*)([\/][[:alnum:][:punct:]]*)/', $curl_result, $location_matches);
if(!empty($location_matches)) {
$ip_matches = array();
preg_match('/(https*:\/\/)(www.)?([a-zA-Z.:0-9]*)([\/][[:alnum:][:punct:]]*)?/', $this->server['ip'], $ip_matches);
if (strtolower($location_matches[4]) !== strtolower($ip_matches[3])) {
$this->error = "The IP/URL redirects to another domain.";
$result = false;
}
}
}
// Check if the website redirects to another domain
if ($this->server['redirect_check'] == 'bad') {
if (!empty($redirectUrl)) {
$redirectPieces = parse_url($redirectUrl);
$ipPieces = parse_url($this->server['ip']);
if (strtolower($redirectPieces['host']) !== strtolower($ipPieces['host'])) {
$this->error = "The IP/URL redirects to another domain.";
$result = false;
}
}
}
// Should we check a header ?
// Should we check a header ?
if ($this->server['header_name'] != '' && $this->server['header_value'] != '') {
$header_flag = false;
$header_text = substr($curl_result, 0, strpos($curl_result, "\r\n\r\n")); // Only get the header text if the result also includes the body
$header_text = substr($this->header, 0, strpos($this->header, "\r\n\r\n")); // Only get the header text if the result also includes the body
foreach (explode("\r\n", $header_text) as $i => $line) {
if ($i === 0 || strpos($line, ':') == false) {
continue; // We skip the status code & other non-header lines. Needed for proxy or redirects