-
Notifications
You must be signed in to change notification settings - Fork 16
/
expire-endpoints-cron-example.php.txt
275 lines (224 loc) · 8.99 KB
/
expire-endpoints-cron-example.php.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
*@license
*
*Copyright 2021 Cisco Systems, Inc. or its affiliates
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/
// This cron example script can be used to set endpoints to expired in the database if they are expired, send a email notice to
// owners of endpoints that are about to expire, and to truncate logs within the iPSK Manager database vs doing so on login.
// Use of this cron script is optional.
// CONFIG BEGIN
// Change path to where you installed iPSK Manager
$ipskManagerPath = "/Users/nick/Documents/GitHub/iPSK-Manager";
// Enable/Disable Expire Endpoint Process
$expireEndpoints = false;
// Enable/Disable email warnings for expiring endpoints
// Set number of days before expire to start warning
$expireEmailNotice = false;
$expireWarningDays = 10;
// Enable/Disable Truncate Logs Process
$truncateLogs = false;
// CONFIG END
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require("supportfiles/include/phpmailer/Exception.php");
require("supportfiles/include/phpmailer/PHPMailer.php");
require("supportfiles/include/phpmailer/SMTP.php");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
//Check if Configuration file exists, if not redirect to Installer
if(file_exists($ipskManagerPath."/supportfiles/include/config.php")){
//Core Components
include($ipskManagerPath."/supportfiles/include/config.php");
include($ipskManagerPath."/supportfiles/include/iPSKManagerDatabase.php");
$ipskISEDB = new iPSKManagerDatabase($dbHostname, $dbUsername, $dbPassword, $dbDatabase);
$ipskISEDB->set_encryptionKey($encryptionKey);
//START-[DO NOT REMOVE] - EMPTIES/REMOVES ENCRYTION KEY/DB PASSWORD VARIABLE
$encryptionKey = "";
$dbPassword = "";
unset($encryptionKey);
unset($dbPassword);
//END-[DO NOT REMOVE] - EMPTIES/REMOVES ENCRYTION KEY/DB PASSWORD VARIABLE
//Call Class Function to perform Global update of expired endpoints 'accountExpired' DB value
if ($expireEndpoints) {
$ipskISEDB->updateExpiredEndpoints();
}
if ($truncateLogs) {
$adminSettings = $ipskISEDB->getGlobalClassSetting("admin-portal");
if($adminSettings['log-purge-interval'] != '') {
$ipskISEDB->purgeLogs($adminSettings['log-purge-interval']);
}
}
if ($expireEmailNotice){
$expiringEndpoints = $ipskISEDB->getExpiringEndpoints($expireWarningDays);
$smtpSettings = $ipskISEDB->getSmtpSettings();
if ($expiringEndpoints) {
if ($expiringEndpoints->num_rows > 0) {
while($row = $expiringEndpoints->fetch_assoc()) {
$macEmailArray[] = $row;
}
$emailAddresses = array_unique(array_column($macEmailArray, 'emailAddress'));
foreach ($emailAddresses as $email) {
// Fetch all MAC addresses for the current email address
$macAddresses = [];
foreach ($macEmailArray as $entry) {
if ($entry['emailAddress'] == $email) {
$macAddresses[] = $entry['macAddress'];
}
}
if($smtpSettings['enabled'] == 1) {
// Send email through SMTP server
sendSMTPEmail($email, $macAddresses, "iPSK Manager Expiring Endpoints",$smtpSettings, $ipskISEDB);
}
else {
// Send email through system mail process
sendHTMLEmail($email, $macAddresses, "iPSK Manager Expiring Endpoints",$smtpSettings, $ipskISEDB);
/*
*Second Method to Send Email. (Plain Text)
*
*sendEmail($email, $macAddresses, "iPSK Manager Expiring Endpoints",$smtpSettings);
*/
}
}
}
}
}
}else{
die();
}
function sendSMTPEmail($to, $macAddresses, $subject, $smtpSettings, $ipskISEDB){
$mail = new PHPMailer(true);
try {
//Server settings
//$mail->SMTPDebug = 3;
//$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = $smtpSettings['smtp-hostname'];
if(($smtpSettings['smtp-username'] !='') && ($smtpSettings['smtp-password'] !='')) {
$mail->SMTPAuth = true;
$mail->Username = $smtpSettings['smtp-username'];
$mail->Password = $smtpSettings['smtp-password'];
}
else {
$mail->SMTPAuth = false;
}
if($smtpSettings['smtp-encryption'] == 'STARTTLS') {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
}
if($smtpSettings['smtp-encryption'] == 'TLS') {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
}
$mail->Port = $smtpSettings['smtp-port'];
//Recipients
if($smtpSettings['smtp-fromaddress'] != ''){
$mailFrom = filter_var($smtpSettings['smtp-fromaddress'], FILTER_SANITIZE_EMAIL);
}else{
$mailFrom = "[email protected]";
}
$mail->setFrom($mailFrom, 'iPSK Manager');
$mail->addAddress($to);
$mail->addReplyTo($mailFrom, 'iPSK Manager');
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = $subject;
$mail->Body = htmlBody($macAddresses);
$mail->AltBody = "The following endpoints are expiring soon: \n\n".implode("\n", $macAddresses)."\n\nIf the endpoints need to be extended please login to iPSK Manager and extend before they expire.\n\nThank you!";
$mail->send();
$logMessage = "EMAIL:SUCCESS;ACTION:SEND;METHOD:SMTP;SERVER:".$smtpSettings['smtp-hostname'].";TO:".$to.";";
$ipskISEDB->addLogEntry($logMessage, __FILE__, __FUNCTION__, __CLASS__, __METHOD__, __LINE__);
} catch (Exception $e) {
$logMessage = "EMAIL:FAILURE;ACTION:SEND;METHOD:SMTP;ERROR:".$mail->ErrorInfo.":SERVER:".$smtpSettings['smtp-hostname'].";TO:".$to.";";
$ipskISEDB->addLogEntry($logMessage, __FILE__, __FUNCTION__, __CLASS__, __METHOD__, __LINE__);
}
}
function sendEmail($to, $macAddresses, $subject, $smtpSettings){
$body = "The following endpoints are expiring soon: \n\n".implode("\n", $macAddresses)."\n\nIf the endpoints need to be extended please login to iPSK Manager and extend before they expire.\n\nThank you!";
$sendTo = filter_var($to, FILTER_SANITIZE_EMAIL);
if($sendTo != ""){
if($smtpSettings['smtp-fromaddress'] != ''){
$mailFrom = filter_var($smtpSettings['smtp-fromaddress'], FILTER_SANITIZE_EMAIL);
}else{
$mailFrom = "[email protected]";
}
$headers = 'To: ' . $to . "\r\n";
$headers .= 'From: ' . $mailFrom . "\r\n";
$headers .= 'Reply-To: ' . $mailFrom . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $body, $headers, "-f ".$mailFrom)){
return true;
}else{
return false;
}
}else{
return false;
}
}
function sendHTMLEmail($to, $macAddresses, $smtpSettings, $ipskISEDB){
$sendTo = filter_var($to, FILTER_SANITIZE_EMAIL);
if($sendTo != ""){
if($smtpSettings['smtp-fromaddress'] != ''){
$mailFrom = filter_var($smtpSettings['smtp-fromaddress'], FILTER_SANITIZE_EMAIL);
}else{
$mailFrom = "[email protected]";
}
$headers = 'To: ' . $to . "\r\n";
$headers .= 'From: ' . $mailFrom . "\r\n";
$headers .= 'Reply-To: ' . $mailFrom . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = $subject;
$body = htmlBody($macAddresses);
if(mail($to, $subject, $body, $headers, "-f ".$mailFrom)){
$logMessage = "EMAIL:SUCCESS;ACTION:SEND;METHOD:LOCALSERVER;TO:".$to.";";
$ipskISEDB->addLogEntry($logMessage, __FILE__, __FUNCTION__, __CLASS__, __METHOD__, __LINE__);
return true;
}else{
$logMessage = "EMAIL:FAILURE;ACTION:SEND;METHOD:LOCALSERVER;TO:".$to.";";
$ipskISEDB->addLogEntry($logMessage, __FILE__, __FUNCTION__, __CLASS__, __METHOD__, __LINE__);
return false;
}
}else{
return false;
}
}
function htmlBody($macAddresses){
$macAddressesString = implode("<br>", $macAddresses);
$body = <<< HTML
<html>
<head>
<style>
table {
width: 100%;
}
</style>
</head>
<body>
<div style="width: 20%; float: left;"> </div>
<div style="width: 60%; float: left;">
<div style="background-color: #1ba0d7;"><h1 style="text-align:center; color: #ffffff;">Expiring Endpoints</h1></div>
<p>You have endpoints that are about to expire in iPSK Manager. Please extend any of the endpoints below if they still need access.</p>
$macAddressesString
<div style="background-color: #1ba0d7;"><h3><p style="color: #ffffff; text-align: center;">Copyright © 2024 Cisco and/or its affiliates.</p></h3></div>
</div>
<div style="width: 20%; float: right;"> </div>
</body>
</html>
HTML;
return $body;
}
?>