File: //proc/thread-self/root/opt/cpguard/app/scripts/webmin_suspend_hook.php
#!/opt/cpguard/cpg-php-fpm/bin/php
<?php
## DO NOT CUSTOMISE THIS FILE
## This file may be updated during software update
## Please make a copy of the file for customising it
sleep(2);
$input = json_decode($argv[1], true);
/*
$input['emails'] - (array) Primary and secondary notification emails
$input['user'] - (string) The user to be suspended
$input['reason'] - (string) Reason for suspendsion
*/
if (!isUserSuspended($input['user'])) {
//Suspend the user
suspendUser($input['user'], $input['reason']);
//Send a notification in slack
//slack_notification($input);
//Send an email
//send_email_notification($input);
}
function isUserSuspended($user)
{
//code to check if user is already suspended
//Your code here...
}
function suspendUser($user, $reason)
{
//code to suspend user
//Your code here...
#/usr/sbin/virtualmin modify-user --user=cpgnewera --disable
}
/* -------------------------------------------------------------------------
* SLACK WEBHOOKS
* REFER https://api.slack.com/messaging/webhooks
* ---------------------------------------------------------------------- */
function slack_notification($input)
{
$server = gethostname();
//Update the webhook url below
$webhook_url = "https://hooks.slack.com/services/xxxxxxxxxxxxxxx";
$data = array(
"text" => $input['user'] . " account suspended! on $server",
"blocks" => array(
array(
"type" => "section",
"text" => array(
"type" => "mrkdwn",
"text" => "*" . $input['user'] . " account suspended! on $server*\nReason : " . $input['reason']
)
)
)
);
$data_string = json_encode($data);
$ch = curl_init($webhook_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
)
);
$result = curl_exec($ch);
}
/* -------------------------------------------------------------------------
* SENDING EMAILS TO END USERS
* ---------------------------------------------------------------------- */
function send_email_notification($input)
{
if (empty($input['emails'])) {
echo "User email ids are not available. Email not sent";
return false;
}
$server = gethostname();
$to_address = implode(',', $input['emails']);
$subject = $input['user'] . " account suspended on $server";
$message = "
<html>
<head>
<title>" . $input['user'] . " account suspended on $server</title>
</head>
<body>
<h2>" . $input['user'] . " account suspended on $server</h2>
<p>Reason : " . $input['reason'] . "</p>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= "From: cpguard@$server" . "\r\n";
//$headers .= 'Cc: myboss@example.com' . "\r\n";
mail($to_address, $subject, $message, $headers);
}