Files
sld-web-health-check/functions/main.php
T

246 lines
6.9 KiB
PHP

<?php
use function PHPSTORM_META\type;
$weblist_file=file_get_contents(JSON_WEB_LIST);
$weblist_json=json_decode($weblist_file, true);
if (file_exists(JSON_WEB_CHECK)){
$webcheck_file=file_get_contents(JSON_WEB_CHECK);
$webcheck_json=json_decode($webcheck_file, true);
}
//var_dump($webcheck_json);
function checktoword($wordtochange){
if ( $wordtochange == "1"){
echo "OK";
}else{
echo "PROBLEM";
}
}
function get_status_class($ssl, $word, $expiry){
if ($ssl != 1 || $word != 1) {
return "alert_red";
}
$days = ssl_days_left($expiry);
if ($days !== null) {
if ($days < 0) {
return "alert_red";
}
elseif ($days <= SSL_CRITICAL_DAYS) {
return "alert_orange";
}
elseif ($days <= SSL_WARNING_DAYS) {
return "alert_yellow";
}
}
return "alert_green";
}
function word_col_status($input){
$col="#f7ce5f";
$style = " style='color: ". $col . "; text-decoration: underline; text-shadow: 0 0 0 yellow; animation: bumping 3s linear 2s infinite;' ";
if ($input != 1){
echo $style;
}
}
function get_checked_list(){
global $webcheck_json;
// Ordina: problemi prima
usort($webcheck_json, function($a, $b) {
// 1️⃣ Problemi contenuto o SSL
$a_problem = ($a['sslcheck_ok'] != 1 || $a['wordcheck_ok'] != 1);
$b_problem = ($b['sslcheck_ok'] != 1 || $b['wordcheck_ok'] != 1);
if ($a_problem && !$b_problem) return -1;
if (!$a_problem && $b_problem) return 1;
// 2️⃣ Giorni alla scadenza
$a_days = ssl_days_left($a['ssl-expiry']);
$b_days = ssl_days_left($b['ssl-expiry']);
// Se uno non ha data → mettilo in fondo
if ($a_days === null && $b_days !== null) return 1;
if ($b_days === null && $a_days !== null) return -1;
// 3️⃣ SSL già scaduti prima
if ($a_days < 0 && $b_days >= 0) return -1;
if ($b_days < 0 && $a_days >= 0) return 1;
// 4️⃣ Ordina per giorni crescenti (meno giorni prima)
return $a_days <=> $b_days;
});
foreach($webcheck_json as $check_element){
//VARIABLES
$host=$check_element['host'];
$domain=$check_element['domain'];
$path=$check_element['path'];
$checkword_ok=$check_element['wordcheck_ok'];
$sslcheck_ok=$check_element['sslcheck_ok'];
$wordtocheck=$check_element['wordtocheck'];
$ssl_released=$check_element['ssl-released'];
$ssl_expiry=$check_element['ssl-expiry'];
$ssl_cn=$check_element['ssl-cn'];
$ssl_org=$check_element['ssl-org'];
$http_error=$check_element['content_http_code'];
$content_error=$check_element['content_error'];
$ssl_error=$check_element['ssl_error'];
$expirying_days = ssl_days_left($ssl_expiry);
$expiry_col = get_expiry_style($expirying_days);
$status_class = get_status_class($sslcheck_ok, $checkword_ok, $ssl_expiry);
$col_red = "#ff4747";
$col_orange = "#ff830f";
$col_yellow = "#ffd70f";
$col_green = "#42c500";
if($expirying_days < 0){
$expiry_col='style="font-weight:bold; color:' . $col_red . ';"';
}elseif($expirying_days <= SSL_CRITICAL_DAYS ){
$expiry_col='style="font-weight:bold; color:' . $col_orange . ';"';
}elseif($expirying_days <= SSL_WARNING_DAYS){
$expiry_col='style="font-weight:bold; color:' . $col_yellow . ';"';
}else{
$expiry_col='style="font-weight:bold; color:' . $col_green . ';"';
}
if ($path == ""){
$path = "/";
}
?>
<div class="alert-container <?= $status_class ?>">
<div class="alert-info">
<div><b>HOST: </b><span><?= $host ?></span></div>
<div><b>DOMAIN: </b><span><?= $domain ?></span></div>
<div <?php word_col_status($checkword_ok) ?>><b>CONTENT: </b><span><?= checktoword($checkword_ok) ?></span></div>
<div <?php word_col_status($sslcheck_ok) ?>><b>SSL: </b><span><?= checktoword($sslcheck_ok) ?></span></div>
</div>
<div class="alert-details">
<?php
if (!$content_error || !$ssl_error){
?>
<div><b>WORD TO CHECK: </b> <span><?= $wordtocheck ?></span></div>
<div><b>SSL RELEASED: </b> <span><?= $ssl_released ?></span></div>
<div><b>SSL EXPIRY: </b> <span <?= $expiry_col ?>><?= $ssl_expiry ?></span></div>
<div><b>SSL DAYS LEFT: </b> <span <?= $expiry_col ?>><?= $expirying_days ?></span></span></div>
<div><b>SSL COMMON NAME(CN): </b> <span> <?= $ssl_cn ?></span></div>
<div><b>SSL ORGANISATION(O): </b> <span> <?= $ssl_org ?></span></div>
<div><b>PATH: </b> <span><?= $path ?></span></div>
<div><b>ERROR CODE: </b> <span><?= $http_error ?></span></div>
<?php }else{?>
<div style="color:red;"><b>ERROR CODE: </b> <span><?= $http_error ?></span></div>
<div style="color:red;"><b>CONTENT ERROR: </b> <span><?= $content_error ?></span></div>
<div style="color:red;"><b>SSL ERROR: </b> <span><?= $ssl_error ?></span></div>
<?php }?>
</div>
</div>
<?php
}
}
function get_totals($type){
global $webcheck_json;
$totaldomain=0;
$totalok=0;
$totalssl_p=0;
$totalcheck_p=0;
foreach($webcheck_json as $i => $element){
// Total domain to check
$totaldomain ++;
// Total fine domain
if ($element['sslcheck_ok'] == 1 && $element['wordcheck_ok'] == 1){
$totalok ++;
}
// Total SSL Problem
if ($element['sslcheck_ok'] !== 1){
$totalssl_p ++;
}
// Total Word-check Problem
if ($element['wordcheck_ok'] !== 1){
$totalcheck_p ++;
}
}
switch($type){
case "total":
echo $totaldomain;
break;
case "ok":
echo $totalok;
break;
case "ssl":
echo $totalssl_p;
break;
case "word":
echo $totalcheck_p;
break;
}
}
function ssl_days_left($expiry_date) {
if (empty($expiry_date)) return null;
$expiry = DateTime::createFromFormat('d/m/Y', $expiry_date);
if (!$expiry) return null;
$now = new DateTime();
$interval = $now->diff($expiry);
$days = (int)$interval->format('%r%a'); // giorni con segno
return $days;
}
function get_expiry_style($days){
$col_red = "#ff4747";
$col_orange = "#ff830f";
$col_yellow = "#ffd70f";
$col_green = "#42c500";
if ($days === null) return "";
if ($days < 0) {
return "style='font-weight:bold;color:$col_red;'";
}
elseif ($days <= SSL_CRITICAL_DAYS) {
return "style='font-weight:bold;color:$col_orange;'";
}
elseif ($days <= SSL_WARNING_DAYS) {
return "style='font-weight:bold;color:$col_yellow;'";
}
return "style='font-weight:bold;color:$col_green;'";
}
?>