Commit afb8fe7d authored by Thomas Bella's avatar Thomas Bella

Add email functionality

parent 91ec202e
<?php
error_reporting(E_ALL);
require_once "config.inc.php";
require_once "assets/correct-config.php";
require_once "class/SQL.class.php";
require_once "class/CertificateReader.class.php";
require_once "class/phpmailer.class.php";
require_once "class/phpmailer.smtp.class.php";
require_once "assets/ports.php";
header("Content-Type: text/plain");
$SQL = new SQL(null, null, null, [ "file" => "monitor.sqlite", "driver" => "sqlite" ]);
if (EMAIL && !empty(EMAIL_TARGET)) {
if (EMAIL_TARGET !== false) {
// Multiple emails
if (strpos(EMAIL_TARGET, ";") !== false) {
$globalEmailList = explode(";", EMAIL_TARGET);
} else {
$globalEmailList = [ EMAIL_TARGET ];
}
} else {
$globalEmailList = [];
}
}
$ressources = $SQL->query("SELECT domain.*, log.* FROM domain LEFT JOIN log ON domain.id = log.domain_id ORDER BY log.last_check ASC");
foreach ($ressources as $row) {
......@@ -170,6 +187,72 @@ foreach ($ressources as $row) {
$SQL->query("UPDATE domain SET error = :error WHERE id = :id", [ "error" => $untrustedReason, "id" => $row["id"] ]);
}
// Process email only if email addresses are given
if (EMAIL == true && (count($globalEmailList) != 0 || strlen($row['email']) != 0)) {
// Check if multiple emails are given
if (strpos($row['email'], ";") !== false) {
$emailList = array_merge($globalEmailList, explode(";", $row['email']));
} else if (strlen($row['email']) > 0) {
$emailList = array_merge($globalEmailList, [ $row['email'] ]);
} else {
$emailList = $globalEmailList;
}
// Determine days until certificate expires
$expiryDate = CertificateReader::expiryDate($cert);
$daysUntilExpire = floor(($expiryDate - time()) / (60 * 60 * 24));
// Detect notification state
if ($daysUntilExpire < 0 && $row['notification_step'] < 4) {
$days = -1;
$index = 4;
} else if ($daysUntilExpire <= 3 && $row['notification_step'] < 3) {
$days = 3;
$index = 3;
} else if ($daysUntilExpire <= 7 && $row['notification_step'] < 2) {
$days = 7;
$index = 2;
} else if ($daysUntilExpire <= 29 && $row['notification_step'] < 1) {
$days = 29;
$index = 1;
} else {
// Reset expiry to 0 to reenable notifications
if ($daysUntilExpire > 29) {
$SQL->query("UPDATE log SET notification_step = :notstep WHERE domain_id = :id", [ "notstep" => 0, "id" => $row["id"] ]);
}
$days = false;
}
// Send notifications
if ($days !== false) {
$SQL->query("UPDATE log SET notification_step = :notstep WHERE domain_id = :id", [ "notstep" => $index, "id" => $row["id"] ]);
foreach ($emailList as $singleEmail) {
$mail = new PHPMailer;
if (EMAIL_SMTP) {
$mail->isSMTP();
$mail->Host = EMAIL_HOST;
$mail->Port = EMAIL_PORT;
$mail->SMTPSecure = EMAIL_ENCRYPT;
if (EMAIL_SMTPAUTH == true) {
$mail->SMTPAuth = true;
$mail->Username = EMAIL_USERNAME;
$mail->Password = EMAIL_PASSWORD;
}
}
$mail->setFrom(EMAIL_FROM, 'Certificate Monitor');
$mail->addAddress($singleEmail);
$mail->Subject = 'Certificate Monitor [' . $row['domain'] . ']';
$mail->Body = 'Your certificate for ' . $row['domain'] . ':' . $row['port'] . ' will expire in about ' . $days . ' days (' . date("d.m.Y H:i", $expiryDate) . ').' . "\n" .
'Please renew your certificate.' . "\n\n" . 'Certificate Monitor on ' . BASEURL;
}
} // $days not false
} // EMAIL enabled
unset($certs, $cert);
}
......
......@@ -26,6 +26,7 @@ if (!is_file("monitor.sqlite")) {
}
require_once "config.inc.php";
require_once "assets/correct-config.php";
require_once "class/SQL.class.php";
global $DBVERSION;
......
......@@ -75,10 +75,34 @@ if (isset($_POST["add"], $_POST["type"]) && $_POST["type"] == "domain") {
} else if (isset($_POST["cadel"])) {
$SQL->query("DELETE FROM custom_ca WHERE id = :id", [ "id" => (int)$_POST["id"] ]);
// add custom email notifications
} else if (isset($_POST["sdesav"], $_POST["emailList"])) {
// Empty list
if (strlen($_POST["emailList"]) == 0) {
$SQL->query("UPDATE domain SET email = NULL WHERE id = :domainid", [
'domainid' => $_POST["domain_id"]
]);
} else {
$emails = explode(';', $_POST["emailList"]);
$validList = '';
foreach($emails as $singleEmail) {
if (filter_var($singleEmail, FILTER_VALIDATE_EMAIL)) {
if ($validList != '') {
$validList .= ';';
}
$validList .= $singleEmail;
}
}
if (!empty($validList)) {
$SQL->query("UPDATE domain SET email = :email WHERE id = :domainid", [
'email' => $validList, 'domainid' => $_POST["domain_id"]
]);
}
}
}
// Select all domains
$records = $SQL->query("SELECT domain.id, domain.domain, domain.port, log.last_check FROM domain LEFT JOIN log ON domain.id = log.domain_id ORDER BY domain.domain ASC");
$records = $SQL->query("SELECT domain.id, domain.domain, domain.port, domain.email, log.last_check FROM domain LEFT JOIN log ON domain.id = log.domain_id ORDER BY domain.domain ASC");
// success notification of ca
if (!empty($sqlInsertCA)) {
?>
......@@ -90,7 +114,7 @@ if (!empty($sqlInsertCA)) {
}
?>
<div id="update-available" style="display: none;" class="alert alert-warning small" role="alert">
<div id="update-available" class="alert alert-warning small" role="alert">
<i class="fa fa-fw fa-exclamation" aria-hidden="true"></i> A new update is available. Please check <a href="https://git.bella.network/unterhaltungsbox/certmonitor">the git repository on git.bella.network</a> for more information.
</div>
......@@ -155,6 +179,7 @@ if( !empty($error) ){
<th>Domain</th>
<th>Port</th>
<th>Last check</th>
<?php if (EMAIL) { ?><th>E</th><?php } ?>
<th>Action</th>
</tr>
</thead>
......@@ -167,6 +192,7 @@ foreach ($records as $row) {
<td><?php echo $row["domain"]; ?></td>
<td class="minwidth"><?php echo $row["port"]; ?></td>
<td class="minwidth"><?php if ($row["last_check"] == 0){ echo "Never"; } else { echo date("d.m. H:i", $row["last_check"]); } ?></td>
<?php if (EMAIL) { ?><td class="minwidth"><i class="fa fa-fw fa-envelope<?php if (strlen($row["email"]) == 0) { echo "-o"; } ?> email-click" eid="<?php echo $row["id"]; ?>" uem="<?php echo $row["email"]; ?>" aria-hidden="true"></i></td><?php } ?>
<td class="minwidth"><form method="POST"><input type="hidden" name="id" value="<?php echo $row["id"]; ?>" /><button type="submit" name="del" class="btn btn-xs btn-danger">Delete</button></form></td>
</tr>
<?php
......@@ -252,3 +278,56 @@ if ($filesize < 1000) {
<div class="col-xs-7 col-sm-9" id="release">Checking ...</div>
</div>
<br />
<div class="fullpage-overlay">
<div class="overlay-box">
<div class="box-title"><i class="fa fa-fw fa-envelope" aria-hidden="true"></i> Edit email settings</div>
<div class="box-content">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>EMail</th>
<th>Action</th>
</tr>
</thead>
<tbody id="ebody">
<?php
if (!empty(EMAIL_TARGET)) {
// Multiple emails
if (strpos(EMAIL_TARGET, ";") !== false) {
$globalEmailList = explode(";", EMAIL_TARGET);
} else {
$globalEmailList = [ EMAIL_TARGET ];
}
} else {
$globalEmailList = [];
}
foreach ($globalEmailList as $singleGlobalEmail) {
?>
<tr class="global">
<td><?php echo htmlentities($singleGlobalEmail); ?></td>
<td class="minwidth"><button type="submit" name="cadel" class="btn btn-xs btn-danger disabled" disabled="disabled">Delete</button></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
<tr>
<td><input type="email" class="form-control input-sm" placeholder="mail@example.com"></td>
<td class="text-center emadd"><i class="fa fa-plus" aria-hidden="true"></i></td>
</tr>
</tfoot>
</table>
<div class="row">
<div class="col-sm-12 text-center">
<form method="POST" class="sdesav">
<input type="hidden" name="emailList" value="" />
<input type="hidden" name="domain_id" value="" />
<button type="submit" name="sdesav" class="btn btn-success">Save</button>
</form>
</div>
</div>
</div>
</div>
</div>
......@@ -25,4 +25,49 @@ $(document).ready(function(){
if ($("#maintable").length != 0) {
new Tablesort(document.getElementById('maintable'));
}
$(".email-click").click(function() {
$(".fullpage-overlay").slideDown("fast");
$("input[name=domain_id]").val($(this).attr('eid'));
if ($(this).attr('uem') != "") {
var emails = $(this).attr('uem').split(';');
$.each(emails, function(i, v){
$("#ebody").append(
$("<tr>").attr('dyn', 'gen').append(
$("<td>").text(v).attr('fname', 'given'),
$("<td>").html('<button class="btn btn-xs btn-danger sdedel">Delete</button>')
)
);
});
}
$(".emadd").find("i").click(function() {
$("#ebody").append(
$("<tr>").attr('dyn', 'gen').append(
$("<td>").text($(this).parents("tr").find("input").val()).attr('fname', 'given'),
$("<td>").html('<button class="btn btn-xs btn-danger sdedel">Delete</button>')
)
);
$(this).parents("tr").find("input").val('');
$(this).parents("tr").find("input").focus();
})
});
$(".sdesav").on('submit', function() {
var emailList = '';
$.each($("td[fname=given]"), function(i,v) {
if (emailList.length != 0) {
emailList += ';';
}
emailList += $(this).text();
});
$("input[name=emailList]").val(emailList);
return true;
});
// Delete - remove entity
$(document).on('click', '.sdedel', function() {
$(this).parents("tr").remove();
});
});
......@@ -86,6 +86,58 @@ td.center, tr.center th {
cursor: pointer;
}
.email-click {
cursor: pointer;
}
.fullpage-overlay {
display: none;
position: fixed;
z-index: 2;
top: 0px;
bottom: 0px;
left: 0px;
right: 0px;
background-color: rgba(0,0,0,0.4);
}
.overlay-box {
z-index: 3;
width: 400px;
min-height: 200px;
margin-top: 200px;
margin-left: auto;
margin-right: auto;
position: relative;
background-color: white;
box-shadow: 0px 0px 25px 10px rgba(0,0,0,0.4);
border-radius: 5px;
}
.overlay-box .box-title {
font-size: 120%;
border-bottom: 1px solid black;
padding: 5px 10px;
font-weight: bold;
color: rgba(0,0,0,0.8);
}
.overlay-box .box-content {
padding: 7px 8px 10px;
}
tr.global {
background-color: rgba(0,0,0,0.1) !important;
color: rgba(0,0,0,0.5);
font-style: italic;
}
.emadd {
color: green;
font-size: 160%;
}
.emadd i {
cursor: pointer;
}
#update-available {
display: none;
}
/* Tablesort */
th[role=columnheader]:not(.no-sort) {
cursor: pointer;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment