-
Notifications
You must be signed in to change notification settings - Fork 9
/
functions.php
351 lines (289 loc) · 11.9 KB
/
functions.php
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
include_once(__DIR__ . '/constants.php');
include_once(__DIR__ . '/arrays.php');
include_once(__DIR__ . '/../classes/cURL.php');
include_once(__DIR__ . '/../classes/mxlookup.php');
function webseer_show_tab($current_tab) {
global $config;
$tabs = array(
'webseer.php' => __('Checks', 'webseer'),
'webseer_servers.php' => __('Servers', 'webseer'),
'webseer_proxies.php' => __('Proxies', 'webseer')
);
if (get_request_var('action') == 'history') {
if ($current_tab == 'webseer.php') {
$current_tab = 'webseer.php?action=history&id=' . get_filter_request_var('id');
$tabs[$current_tab] = __('Log History', 'webeer');
} else {
$current_tab = 'webseer_servers.php?action=history&id=' . get_filter_request_var('id');
$tabs[$current_tab] = __('Log History', 'webeer');
}
}
print "<div class='tabs'><nav><ul>\n";
if (cacti_sizeof($tabs)) {
foreach ($tabs as $url => $name) {
print "<li><a class='" . (($url == $current_tab) ? 'pic selected' : 'pic') . "' href='" . $config['url_path'] .
"plugins/webseer/$url'>$name</a></li>";
}
}
print '</ul></nav></div>';
}
function plugin_webseer_refresh_servers() {
$server = db_fetch_row('SELECT * FROM plugin_webseer_servers WHERE master = 1');
$server['debug_type'] = 'Server';
$cc = new cURL(true, 'cookies.txt', 'gzip', '', $server);
$data = array();
$data['action'] = 'GETSERVERS';
$results = $cc->post($server['url'], $data);
$results = explode("\n", $results);
foreach ($results as $r) {
if (substr($r, 0, 8) == 'SERVERS=') {
$servers = substr($r, 8);
$servers = unserialize(base64_decode($servers));
if (isset($servers[0]['id'])) {
db_execute('TRUNCATE TABLE plugin_webseer_servers');
foreach ($servers as $save) {
db_execute_prepared('REPLACE INTO plugin_webseer_servers (id, enabled, master, name, url, ip, location)
VALUES (?,?,?,?,?,?,?)',
array(
$save['id'], $save['enabled'], $save['master'], $save['name'], $save['url'], $save['ip'] , $save['location']
)
);
}
}
break;
}
}
}
function plugin_webseer_refresh_urls () {
$server = db_fetch_row('SELECT * FROM plugin_webseer_servers WHERE master = 1');
$server['debug_type'] = 'Server';
$cc = new cURL(true, 'cookies.txt', 'gzip', '', $server);
$data = array();
$data['action'] = 'GETURLS';
$results = $cc->post($server['url'], $data);
$results = explode("\n", $results);
foreach ($results as $r) {
if (substr($r, 0, 5) == 'URLS=') {
$urls = substr($r, 5);
$urls = unserialize(base64_decode($urls));
if (isset($urls[0]['id'])) {
db_execute('TRUNCATE TABLE plugin_webseer_urls');
foreach ($urls as $save) {
db_execute_prepared('REPLACE INTO plugin_webseer_urls
(id, enabled, requiresauth, checkcert, ip, display_name, notify_list, notify_accounts, url, search, search_maint, search_failed, notify_extra, downtrigger)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
array(
$save['id'], $save['enabled'], $save['requiresauth'], $save['checkcert'],
$save['ip'], $save['display_name'], $save['notify_list'], $save['notify_accounts'],
$save['url'], $save['search'], $save['search_maint'],
$save['search_failed'], $save['notify_extra'], $save['downtrigger']
)
);
}
}
break;
}
}
}
function plugin_webseer_remove_old_users () {
$users = db_fetch_assoc('SELECT id FROM user_auth');
$u = array();
foreach ($users as $user) {
$u[] = $user['id'];
}
$contacts = db_fetch_assoc('SELECT DISTINCT user_id FROM plugin_webseer_contacts');
foreach ($contacts as $c) {
if (!in_array($c['user_id'], $u)) {
db_execute_prepared('DELETE FROM plugin_webseer_contacts WHERE user_id = ?', array($c['user_id']));
}
}
}
function plugin_webseer_check_dns ($host) {
$results = false;
if (cacti_sizeof($host)) {
$results = array();
$results['result'] = 0;
$results['options']['http_code'] = 0;
$results['error'] = '';
$results['options']['total_time'] = 0;
$results['options']['namelookup_time'] = 0;
$results['options']['connect_time'] = 0;
$results['options']['redirect_time'] = 0;
$results['options']['redirect_count'] = 0;
$results['options']['size_download'] = 0;
$results['options']['speed_download'] = 0;
$results['time'] = time();
$s = microtime(true);
$a = new mxlookup($host['search'], $host['url']);
$t = microtime(true) - $s;
$results['options']['connect_time'] = $results['options']['total_time'] = $results['options']['namelookup_time'] = round($t, 4);
$results['data'] = '';
foreach ($a->arrMX as $m) {
$results['data'] .= "A RECORD: $m\n";
if ($m == $host['search_maint']) {
$results['result'] = 1;
}
}
}
return $results;
}
function plugin_webseer_set_remote_masters ($ip) {
$servers = db_fetch_assoc('SELECT * FROM plugin_webseer_servers WHERE isme = 0');
foreach ($servers as $server) {
$server['debug_type'] = 'Server';
plugin_webseer_set_remote_master ($server['url'], $ip);
}
db_execute('UPDATE plugin_webseer_servers set master = 0');
db_execute_prepared('UPDATE plugin_webseer_servers set master = 1 WHERE ip = ?', array($ip));
}
function plugin_webseer_set_remote_master ($url, $ip) {
$cc = new cURL(true, 'cookies.txt', 'gzip', '', $url);
$data = array();
$data['action'] = 'SETMASTER';
$data['ip'] = $ip;
$results = $cc->post($url['url'], $data);
}
function plugin_webseer_enable_remote_hosts ($id, $value = true) {
$servers = db_fetch_assoc('SELECT * FROM plugin_webseer_servers WHERE isme = 0');
foreach ($servers as $server) {
$cc = new cURL(true, 'cookies.txt', 'gzip', '', $server);
$data = array();
$data['action'] = ($value ? 'ENABLEURL' : 'DISABLEURL');
$data['id'] = $id;
$results = $cc->post($server['url'], $data);
}
}
function plugin_webseer_delete_remote_hosts ($id) {
$servers = db_fetch_assoc('SELECT * FROM plugin_webseer_servers WHERE isme = 0');
foreach ($servers as $server) {
$cc = new cURL(true, 'cookies.txt', 'gzip', '', $server);
$data = array();
$data['action'] = 'DELETEURL';
$data['id'] = $id;
$results = $cc->post($server['url'], $data);
}
}
function plugin_webseer_add_remote_hosts ($id, $save) {
$servers = db_fetch_assoc('SELECT * FROM plugin_webseer_servers WHERE isme = 0');
foreach ($servers as $server) {
$server['debug_type'] = 'Server';
$cc = new cURL(true, 'cookies.txt', 'gzip', '', $server);
$save['action'] = 'ADDURL';
$save['id'] = $id;
$results = $cc->post($server['url'], $save);
}
}
function plugin_webseer_update_remote_hosts ($save) {
$servers = db_fetch_assoc('SELECT * FROM plugin_webseer_servers WHERE isme = 0');
foreach ($servers as $server) {
$server['debug_type'] = 'Server';
$cc = new cURL(true, 'cookies.txt', 'gzip', '', $server);
$save['action'] = 'UPDATEURL';
$results = $cc->post($server['url'], $save);
}
}
function plugin_webseer_add_remote_server ($id, $save) {
$servers = db_fetch_assoc('SELECT * FROM plugin_webseer_servers WHERE isme = 0');
foreach ($servers as $server) {
$server['debug_type'] = 'Server';
$cc = new cURL(true, 'cookies.txt', 'gzip', '', $server);
$save['action'] = 'ADDSERVER';
$save['id'] = $id;
$results = $cc->post($server['url'], $save);
}
}
function plugin_webseer_update_remote_server ($save) {
$servers = db_fetch_assoc('SELECT * FROM plugin_webseer_servers WHERE isme = 0');
foreach ($servers as $server) {
$server['debug_type'] = 'Server';
$cc = new cURL(true, 'cookies.txt', 'gzip', '', $server);
$save['action'] = 'UPDATESERVER';
$results = $cc->post($server['url'], $save);
}
}
function plugin_webseer_enable_remote_server ($id, $value = true) {
$servers = db_fetch_assoc('SELECT * FROM plugin_webseer_servers WHERE isme = 0');
foreach ($servers as $server) {
$server['debug_type'] = 'Server';
$cc = new cURL(true, 'cookies.txt', 'gzip', '', $server);
$data = array();
$data['action'] = ($value ? 'ENABLESERVER' : 'DISABLESERVER');
$data['id'] = $id;
$results = $cc->post($server['url'], $data);
}
}
function plugin_webseer_delete_remote_server ($id) {
$servers = db_fetch_assoc('SELECT * FROM plugin_webseer_servers WHERE isme = 0');
foreach ($servers as $server) {
$server['debug_type'] = 'Server';
$cc = new cURL(true, 'cookies.txt', 'gzip', '', $server);
$data = array();
$data['action'] = 'DELETESERVER';
$data['id'] = $id;
$results = $cc->post($server['url'], $data);
}
}
function plugin_webseer_down_remote_hosts ($save) {
$servers = db_fetch_assoc('SELECT * FROM plugin_webseer_servers WHERE isme = 0');
foreach ($servers as $server) {
$cc = new cURL(true, 'cookies.txt', 'gzip', '', $server);
$save['action'] = 'HOSTDOWN';
$results = $cc->post($server['url'], $save);
}
}
function plugin_webseer_update_contacts() {
$users = db_fetch_assoc("SELECT id, 'email' AS type, email_address FROM user_auth WHERE email_address!=''");
if (cacti_sizeof($users)) {
foreach($users as $u) {
$cid = db_fetch_cell('SELECT id FROM plugin_webseer_contacts WHERE type="email" AND user_id=' . $u['id']);
if ($cid) {
db_execute("REPLACE INTO plugin_webseer_contacts (id, user_id, type, data) VALUES ($cid, " . $u['id'] . ", 'email', '" . $u['email_address'] . "')");
} else {
db_execute("REPLACE INTO plugin_webseer_contacts (user_id, type, data) VALUES (" . $u['id'] . ", 'email', '" . $u['email_address'] . "')");
}
}
}
}
function plugin_webseer_check_debug() {
global $debug;
if (!$debug) {
$plugin_debug = read_config_option('selective_plugin_debug');
if (preg_match('/(^|[, ]+)(webseer)($|[, ]+)/', $plugin_debug, $matches)) {
$debug = (cacti_sizeof($matches) == 4 && $matches[2] == 'webseer');
}
}
}
function plugin_webseer_debug($message='', $host=array()) {
global $debug;
if ($debug) {
$prefix = (empty($host['id']) && empty($host['debug_type'])) ? '' : '[';
$suffix = (empty($host['id']) && empty($host['debug_type'])) ? '' : '] ';
$spacer = (empty($host['id']) || empty($host['debug_type'])) ? '' : ' ';
$host_id = (empty($host['id'])) ? '' : $host['id'];
$host_dt = (empty($host['debug_type'])) ? '' : $host['debug_type'];
cacti_log('DEBUG: ' . $prefix . $host_dt . $spacer . $host_id . $suffix . trim($message), true, 'WEBSEER');
}
}