forked from phpLicenseWatcher/phpLicenseWatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlicense_alert.php
More file actions
162 lines (137 loc) · 5.74 KB
/
license_alert.php
File metadata and controls
162 lines (137 loc) · 5.74 KB
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
<?php
############################################################################
# Purpose: This script is used to e-mail alerts on licenses that are
# due to expire some time in the future. This script should
# be run out of cron preferably every day. Check config.php
# to configure e-mail address reports should be sent to
# as well as how much ahead should the user be warned about
# expiration ie. 10 days before license expires.
############################################################################
require_once __DIR__ . "/common.php";
require_once __DIR__ . "/tools.php";
require_once __DIR__ . "/html_table.php";
if (file_exists(__DIR__ . "/vendor/autoload.php")) {
require_once __DIR__ . "/vendor/autoload.php";
}
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
db_connect($db);
$servers = db_get_servers($db, array('name', 'label', 'license_manager'));
$db->close();
// Date when the licenses will expire
$expire_date = mktime(0, 0, 0, date("m"), date("d") + $lead_time, date("Y"));
$today = mktime (0, 0, 0, date("m"), date("d"), date("Y"));
foreach ($servers as $i => $server) {
build_license_expiration_array($server, $expiration_array[$i]);
}
$table = new html_table(array('class'=>"table alt-rows-bgcolor"));
$colHeaders = array("Server", "Server label", "Feature expiring", "Expiration date",
"Days to expiration", "Number of license(s) expiring");
$table->add_row($colHeaders, array(), "th");
// Now after the expiration has been built loop through all the fileservers
$max_expiration_array = count($expiration_array);
for ($i = 0; $i < $max_expiration_array; $i++) {
if (array_key_exists($i, $expiration_array)) {
foreach ($expiration_array[$i] as $key => $myarray) {
$max_myarray = count($myarray);
for ($j = 0; $j < $max_myarray; $j++) {
$bgcolor_class = "";
switch (true) {
case $myarray[$j]["days_to_expiration"] === "permanent":
case $myarray[$j]["days_to_expiration"] === "N/A":
case $myarray[$j]["days_to_expiration"] > $lead_time:
continue 2;
}
if ($myarray[$j]["days_to_expiration"] < 0) {
$myarray[$j]["days_to_expiration"] = "<span class='bold-text'>Already expired</span>";
$bgcolor_class = " bg-danger"; // change cell background to light red via bootstrap
}
$table->add_row(array(
$servers[$i]['name'],
$servers[$i]['label'],
$key,
$myarray[$j]['expiration_date'],
$myarray[$j]['days_to_expiration'],
$myarray[$j]['num_licenses']
));
$table->update_cell(($table->get_rows_count()-1), 1, array('class'=>"center-text"));
$table->update_cell(($table->get_rows_count()-1), 3, array('class'=>"center-text"));
$table->update_cell(($table->get_rows_count()-1), 4, array('class'=>"center-text{$bgcolor_class}"));
$table->update_cell(($table->get_rows_count()-1), 5, array('class'=>"center-text"));
}
}
}
}
// Dump the table HTML into a variable
$table_html = $table->get_html();
// Message body for either browser view or email.
$message = <<<HTML
These licenses will expire within {$lead_time} days.
Licenses will expire at 23:59 on the day of expiration.
<p>
{$table_html}
HTML;
// More reliable check to determine if we're running on CLI than php_sapi_name()
if (empty(preg_grep("/^HTTP_/", array_keys($_SERVER)))) {
// Script run from command line. Send license alerts via email.
global $send_email_notifications; // defined in config.php
if ($send_email_notifications) {
send_email($message);
}
} else {
print_view($message);
}
exit(0);
// FUNCTIONS ------------------------------------------------------------------
function send_email($message) {
// Check for PHPMailer library before proceeding.
if (!class_exists("PHPMailer\PHPMailer\PHPMailer", true)) {
fprintf(STDERR, "Cannot mail license alerts. PHPMailer library not found.\n");
exit(1);
}
// globals are defined in config.php
global $smtp_host, $smtp_login, $smtp_password, $smtp_tls, $smtp_port, $notify_address, $reply_address, $lead_time;
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->SMTPAuth = true;
$mail->Host = $smtp_host;
$mail->Port = $smtp_port;
$mail->Username = $smtp_login;
$mail->Password = $smtp_password;
switch ($smtp_tls) {
case "smtps":
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
break;
case "starttls":
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
break;
case "none":
fprintf(STDERR, "No encryption of sent email.\n");
$mail->SMTPAuth = false;
$mail->SMTPSecure = '';
$mail->SMTPAutoTLS = false;
break;
default:
fprintf(STDERR, "Cannot mail license alerts.\n\$smtp_tls not properly set in config.php\n");
exit(1);
}
$mail->setFrom($reply_address, 'phpLicenseWatcher');
$mail->addReplyTo($reply_address);
$mail->addAddress($notify_address);
$mail->isHTML(true);
$mail->Subject = "ALERT: License expiration within {$lead_time} days";
$mail->Body = $message;
if (!$mail->send()) {
fprintf(STDERR, "Cannot mail license alerts.\nMailer Error: %s\n", $mail->ErrorInfo);
exit(1);
}
}
function print_view($message) {
print_header();
print "<h1>License Alert</h1>\n";
print $message;
print_footer();
}
?>