-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.php
More file actions
209 lines (178 loc) · 7.49 KB
/
Copy pathconfig.php
File metadata and controls
209 lines (178 loc) · 7.49 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
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
<?php
/*
================================================================================
File: config.php
Description: Database configuration and helper functions.
================================================================================
*/
// --- Database Connection ---
function getDbConnection() {
try {
// This path is for the Docker setup. For non-Docker, use __DIR__ . '/monitor.db'
$db_path = '/data/monitor.db';
$db = new PDO('sqlite:' . $db_path);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $db;
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
}
// --- Core Ping Function ---
/**
* Pings a host and returns true if it's up, false if it's down.
* @param string $host The IP address or hostname to ping.
* @return bool True on success, false on failure.
*/
function pingHost($host) {
// Detect the operating system to use the correct ping command.
$isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
if ($isWindows) {
// Windows command: -n 1 (1 packet), -w 1000 (1-second timeout in ms)
$command = "ping -n 1 -w 1000 " . escapeshellarg($host);
} else {
// Linux/macOS command: -c 1 (1 packet), -W 1 (1-second timeout)
// Prepending 'sudo' to use the passwordless permission granted in the Dockerfile.
$command = "sudo /bin/ping -c 1 -W 1 " . escapeshellarg($host);
}
// Execute the command
exec($command, $output, $return_var);
// Check the return status. 0 usually means success.
return $return_var === 0;
}
// --- Notification Dispatcher ---
/**
* Sends notifications to all enabled channels.
* @param array $monitor The monitor that changed status.
* @param string $status 'UP' or 'DOWN'.
* @param PDO $db The database connection.
*/
function sendNotification($monitor, $status, $db) {
try {
$settings_stmt = $db->query("SELECT key, value FROM settings");
$settings = $settings_stmt->fetchAll(PDO::FETCH_KEY_PAIR);
$enable_email = $settings['enable_email'] ?? '0';
$enable_discord = $settings['enable_discord'] ?? '0';
if ($enable_email === '1') {
sendEmailNotification($monitor, $status, $settings);
}
if ($enable_discord === '1') {
sendDiscordNotification($monitor, $status, $settings['discord_webhook_url'] ?? '');
}
} catch (PDOException $e) {
error_log("Failed to send notifications: " . $e->getMessage());
}
}
// --- Email Notification Function (Now with SMTP) ---
/**
* Sends an email notification using SMTP settings from the database.
* @param array $monitor The monitor that changed status.
* @param string $status 'UP' or 'DOWN'.
* @param array $settings The application settings from the database.
*/
function sendEmailNotification($monitor, $status, $settings) {
// Extract settings
$to = $settings['email_to'] ?? '';
$host = $settings['smtp_host'] ?? '';
$port = $settings['smtp_port'] ?? 587;
$user = $settings['smtp_user'] ?? '';
$pass = $settings['smtp_pass'] ?? '';
$encryption = $settings['smtp_encryption'] ?? 'tls';
// Don't proceed if essential settings are missing
if (empty($to) || empty($host)) {
error_log("Email not sent: Recipient email or SMTP host is not configured.");
return;
}
$from_email = $user ?: "monitor@" . ($_SERVER['SERVER_NAME'] ?? 'localhost');
$from_name = 'PHPing';
$subject = "PHPing Status Alert: {$monitor['name']} is {$status}";
$body = "Hello,\r\n\r\n" .
"This is an automated alert from PHPing.\r\n\r\n" .
"Monitor Details:\r\n" .
"- Name: {$monitor['name']}\r\n" .
"- Host: {$monitor['ip_address']}\r\n" .
"- Status: {$status}\r\n" .
"- Time: " . date('Y-m-d H:i:s') . "\r\n\r\n" .
"Thank you,\r\nPHPing";
// Build headers
$headers = "From: \"{$from_name}\" <{$from_email}>\r\n";
$headers .= "To: <{$to}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "Date: " . date('r') . "\r\n";
$headers .= "Content-Type: text/plain; charset=utf-8\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$smtp_protocol = ($encryption === 'ssl') ? 'ssl://' : '';
$smtp = @fsockopen($smtp_protocol . $host, $port, $errno, $errstr, 15);
if (!$smtp) {
error_log("SMTP Connection Failed: [$errno] $errstr");
return;
}
// Helper function to send commands and log responses
function send_smtp_command($smtp, $cmd, &$log_array) {
fputs($smtp, $cmd . "\r\n");
$response = fgets($smtp, 512);
// Optionally log for debugging: error_log("SMTP CMD: $cmd | RESP: $response");
return $response;
}
fgets($smtp, 512); // Get server greeting
send_smtp_command($smtp, "HELO " . ($_SERVER['SERVER_NAME'] ?? 'localhost'), $log);
if ($encryption === 'tls') {
send_smtp_command($smtp, "STARTTLS", $log);
if (!stream_socket_enable_crypto($smtp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
error_log("Failed to start TLS encryption.");
fclose($smtp);
return;
}
send_smtp_command($smtp, "HELO " . ($_SERVER['SERVER_NAME'] ?? 'localhost'), $log);
}
if (!empty($user) && !empty($pass)) {
send_smtp_command($smtp, "AUTH LOGIN", $log);
send_smtp_command($smtp, base64_encode($user), $log);
$response = send_smtp_command($smtp, base64_encode($pass), $log);
if (substr($response, 0, 3) != "235") {
error_log("SMTP Authentication failed: " . $response);
fclose($smtp);
return;
}
}
send_smtp_command($smtp, "MAIL FROM: <{$from_email}>", $log);
send_smtp_command($smtp, "RCPT TO: <{$to}>", $log);
send_smtp_command($smtp, "DATA", $log);
fputs($smtp, $headers . "\r\n" . $body . "\r\n.\r\n");
fgets($smtp, 512); // Get response after DATA
send_smtp_command($smtp, "QUIT", $log);
fclose($smtp);
}
// --- Discord Webhook Notification Function ---
/**
* @param string $webhookUrl The Discord webhook URL from the database.
*/
function sendDiscordNotification($monitor, $status, $webhookUrl) {
if (empty($webhookUrl)) {
error_log("Discord notification not sent: Webhook URL is not configured.");
return;
}
$statusUpper = strtoupper($status);
$color = ($statusUpper === 'UP') ? 3066993 : 15158332; // Green : Red
$embed = [
'title' => "PHPing Status: {$monitor['name']} is {$statusUpper}",
'description' => "The monitor for `{$monitor['ip_address']}` has changed state.",
'color' => $color,
'timestamp' => date('c'),
'footer' => ['text' => 'PHPing']
];
$payload = json_encode(['username' => 'PHPing Alert', 'embeds' => [$embed]]);
$ch = curl_init($webhookUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if (curl_errno($ch)) {
error_log('Discord Webhook Error: ' . curl_error($ch));
}
curl_close($ch);
}
// Best practice: Omit the closing PHP tag in files that contain only PHP code.
// This prevents accidental whitespace from being sent to the browser.