Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Slack/Rocketchat integration #1

Merged
merged 5 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ Also change the `$current_domain` variable, it is used in all the email addresse
And `$current_link`, which may or may not be the same. It is used in the confirm and unsubscribe links, and depends on your webserver configuration. `example.com/subdir` here means your unsubscribe links will start `https://example.com/subdir/unsubscribe.php`.

$current_link = "certificatemonitor.org";

If you use Slack/Rocketchat, you can set up automatic posts whenever a subscription is added or removed, or checked and found to be expiring soon, expired or failed. To do this, create an [incoming webhook](https://api.slack.com/incoming-webhooks) and add its URL to the configuration (if you don't want this function, leave the string empty):

$slack_webhook = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX";

or

$slack_webhook = "https://my.rocketchat.com/XXXXXXXXXXXXXXXXXXXXXXXX";

Set up the cronjob to run once a day:

Expand Down
17 changes: 15 additions & 2 deletions functions/add_check.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,20 @@ function add_domain_check($id,$visitor_ip) {
'List-Unsubscribe: <https://' . $current_link . "/unsubscribe.php?id=" . $id . ">" . "\r\n" .
'X-Mailer: PHP/4.1.1';


slack_send_array(array('attachments' => array(array(
'fallback' => $subject,
'color' => '#00ff00',
'pretext' => $subject,
'title' => $json_a[$id]['domain'],
'title_link' => 'https://' . $json_a[$id]['domain'],
'text' => 'Subscription confirmed for this domain:',
'fields' => array(
array('title' => 'Domain', 'value' => $json_a[$id]['domain']),
array('title' => 'Email', 'value' => $json_a[$id]['email']),
array('title' => 'Confirmed from', 'value' => $visitor_ip, 'short' => TRUE),
array('title' => 'Confirmed date', 'value' => date("Y-m-d H:i:s T"), 'short' => TRUE),
)
))));

if (mail($to, $subject, $message, $headers) === true) {
$result['success'][] = true;
Expand All @@ -131,4 +144,4 @@ function add_domain_check($id,$visitor_ip) {
}

return $result;
}
}
68 changes: 68 additions & 0 deletions functions/email.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ function send_error_mail($domain, $email, $errors) {
'List-Unsubscribe: <https://' . $current_link . "/unsubscribe.php?id=" . $id . ">" . "\r\n" .
'X-Mailer: PHP/4.1.1';

slack_send_array(array('attachments' => array(array(
'fallback' => $subject,
'color' => '#ff0000',
'pretext' => $subject,
'title' => $domain,
'title_link' => 'https://' . $domain,
'text' => 'Failed to check certificates for this domain:',
'fields' => array(
array('title' => 'Errors', 'value' => $errors),
array('title' => 'Failures', 'value' => $failures)
)
))));

if (mail($to, $subject, $message, $headers) === true) {
echo "\t\tEmail sent to $to.\n";
return true;
Expand Down Expand Up @@ -117,6 +130,22 @@ function send_cert_expired_email($days, $domain, $email, $raw_cert) {
'List-Unsubscribe: <https://' . $current_link . "/unsubscribe.php?id=" . $id . ">" . "\r\n" .
'X-Mailer: PHP/4.1.1';

slack_send_array(array('attachments' => array(array(
'fallback' => $subject,
'color' => '#ff0000',
'pretext' => $subject,
'title' => $domain,
'title_link' => 'https://' . $domain,
'text' => 'The following certificate in the chain for this domain has expired:',
'fields' => array(
array('title' => 'Common Name', 'value' => $cert_cn),
array('title' => 'Subject', 'value' => $cert_subject),
array('title' => 'Serial', 'value' => $cert_serial),
array('title' => 'Valid From', 'value' => date("Y-m-d H:i:s T", $cert_validfrom_date), 'short' => TRUE),
array('title' => 'Valid Until', 'value' => date("Y-m-d H:i:s T", $cert_expiry_date), 'short' => TRUE)
)
))));

if (mail($to, $subject, $message, $headers) === true) {
echo "\t\tEmail sent to $to.\n";
return true;
Expand Down Expand Up @@ -178,6 +207,22 @@ function send_expires_in_email($days, $domain, $email, $raw_cert) {
'List-Unsubscribe: <https://' . $current_link . "/unsubscribe.php?id=" . $id . ">" . "\r\n" .
'X-Mailer: PHP/4.1.1';

slack_send_array(array('attachments' => array(array(
'fallback' => $subject,
'color' => '#ffff00',
'pretext' => $subject,
'title' => $domain,
'title_link' => 'https://' . $domain,
'text' => 'The following certificate in the chain for this domain is about to expire:',
'fields' => array(
array('title' => 'Common Name', 'value' => $cert_cn),
array('title' => 'Subject', 'value' => $cert_subject),
array('title' => 'Serial', 'value' => $cert_serial),
array('title' => 'Valid From', 'value' => date("Y-m-d H:i:s T", $cert_validfrom_date), 'short' => TRUE),
array('title' => 'Valid Until', 'value' => date("Y-m-d H:i:s T", $cert_expiry_date), 'short' => TRUE)
)
))));

if (mail($to, $subject, $message, $headers) === true) {
echo "\t\tEmail sent to $to.\n";
return true;
Expand All @@ -189,5 +234,28 @@ function send_expires_in_email($days, $domain, $email, $raw_cert) {
}
}

function slack_send_array($payload) {
global $slack_webhook;
if (!$payload) {
echo "\t\tNot sending empty Slack payload.\n";
return FALSE;
}
if (!$slack_webhook) {
echo "\t\tNo Slack webhook URL set.\n";
return FALSE;
}
$ch = curl_init($slack_webhook);
$postdata = array('payload' => json_encode($payload));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($result && $http == 200) {
echo "\n\t\tSlack message sent.\n";
} else {
echo "\n\t\tError sending Slack message.\n";
}
return $result;
}

?>
19 changes: 17 additions & 2 deletions functions/remove_check.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,22 @@ function remove_domain_check($id,$visitor_ip) {
'List-Unsubscribe: <https://' . $current_link . "/unsubscribe.php?id=" . $id . ">" . "\r\n" .
'X-Mailer: PHP/4.1.1';

if (mail($to, $subject, $message, $headers) === true) {
slack_send_array(array('attachments' => array(array(
'fallback' => $subject,
'color' => '#ffff00',
'pretext' => $subject,
'title' => $deleted_json_a[$id]['domain'],
'title_link' => 'https://' . $deleted_json_a[$id]['domain'],
'text' => 'Subscription removed for this domain:',
'fields' => array(
array('title' => 'Domain', 'value' => $deleted_json_a[$id]['domain']),
array('title' => 'Email', 'value' => $deleted_json_a[$id]['email']),
array('title' => 'Removed from', 'value' => $visitor_ip, 'short' => TRUE),
array('title' => 'Removed date', 'value' => date("Y-m-d H:i:s T"), 'short' => TRUE),
)
))));

if (mail($to, $subject, $message, $headers) === true) {
$result['success'][] = true;
} else {
$result['errors'][] = "Can't send email.";
Expand All @@ -100,4 +115,4 @@ function remove_domain_check($id,$visitor_ip) {
return $result;
}
}
}
}
2 changes: 2 additions & 0 deletions functions/variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
$current_domain = "certificatemonitor.org";
$current_link = "certificatemonitor.org";

$slack_webhook = "";

$showListOfDomains = false;
$showEmailsOnListOfDomains = false;
$showClickToUnsubscribeOnListOfDomains = false;
Expand Down