-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetup.php
More file actions
347 lines (302 loc) · 13.6 KB
/
Copy pathsetup.php
File metadata and controls
347 lines (302 loc) · 13.6 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
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
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2009-2013 Andrew Aloia |
| Copyright (C) 2014 Wixiweb |
| Copyright (C) 2004-2026 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. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
/**
* Installs the GPS Map plugin: registers its Cacti hooks (top_header_tabs,
* top_graph_header_tabs, config_arrays, config_settings,
* draw_navigation_text, api_device_save, config_form, poller_bottom,
* page_head), registers its two realms (Configure Maps, View Maps), and
* creates/verifies its database schema. Invoked by Cacti's plugin
* architecture when an administrator installs this plugin from Console >
* Plugin Management.
*
* @return void
*
* @global array $config Cacti global configuration array; used to load
* includes/setup/database.php.
*/
function plugin_gpsmap_install() {
global $config;
api_plugin_register_hook('gpsmap', 'top_header_tabs', 'gpsmap_show_tab', 'includes/setup/tabs.php');
api_plugin_register_hook('gpsmap', 'top_graph_header_tabs', 'gpsmap_show_tab', 'includes/setup/tabs.php');
api_plugin_register_hook('gpsmap', 'config_arrays', 'gpsmap_config_arrays', 'includes/setup/settings.php');
api_plugin_register_hook('gpsmap', 'config_settings', 'gpsmap_config_settings', 'includes/setup/settings.php');
api_plugin_register_hook('gpsmap', 'draw_navigation_text', 'gpsmap_draw_navigation_text', 'includes/setup/settings.php');
api_plugin_register_hook('gpsmap', 'api_device_save', 'gpsmap_api_device_save', 'includes/setup/settings.php');
api_plugin_register_hook('gpsmap', 'config_form', 'gpsmap_config_form', 'setup.php');
api_plugin_register_hook('gpsmap', 'poller_bottom', 'gpsmap_poller_bottom', 'includes/polling.php');
api_plugin_register_hook('gpsmap', 'page_head', 'gpsmap_page_head', 'setup.php');
api_plugin_register_realm('gpsmap', 'gpstemplates.php,gpstemplates_add.php',__('Configure Maps', 'gpsmap'), 1);
api_plugin_register_realm('gpsmap', 'gpsmap.php', __('View Maps', 'gpsmap'), 1);
include_once($config['base_path'] . '/plugins/gpsmap/includes/setup/database.php');
if (gpsmap_setup_database()) {
$info = plugin_gpsmap_version();
if (empty($info['version'])) {
cacti_log('ERROR: gpsmap plugin INFO file is missing required fields, skipping version registration', false, 'GPSMAP');
return;
}
$version = $info['version'];
if (db_execute_prepared('UPDATE plugin_config SET version = ? WHERE directory = ?', [$version, 'gpsmap'])) {
set_config_option('plugin_gpsmap_version', $version);
}
} else {
cacti_log('ERROR: gpsmap installation could not create or verify the required database schema; correct the database error and retry from Plugin Management', false, 'GPSMAP');
}
}
/**
* Uninstalls the GPS Map plugin; currently a no-op placeholder. Invoked
* by Cacti's plugin architecture when an administrator uninstalls this
* plugin from Console > Plugin Management.
*
* Tables and settings created on install are intentionally left in place
* on uninstall to prevent data loss on accidental removal. A future
* release should call gpsmap_remove_database() here with explicit
* confirmation from the administrator.
*
* @return void
*/
function plugin_gpsmap_uninstall() {
}
/**
* Verifies the plugin's configuration by triggering its upgrade check.
* Invoked by Cacti's plugin architecture on relevant page loads.
*
* @return bool Always returns true.
*/
function plugin_gpsmap_check_config() {
gpsmap_check_upgrade();
return true;
}
/**
* Performs any schema/data migrations needed when upgrading to a newer
* version of this plugin, by force-running the upgrade check regardless
* of the current page. Invoked by Cacti's plugin architecture when an
* installed plugin's version increases.
*
* @return bool Always returns false.
*/
function plugin_gpsmap_upgrade() {
gpsmap_check_upgrade(true);
return false;
}
/**
* Reads this plugin's INFO file and returns its [info] section, via
* gpsmap_version(). Used by Cacti's plugin architecture via the
* api_plugin_version hook.
*
* @return array The parsed [info] section of the plugin's INFO file.
*/
function plugin_gpsmap_version() {
return gpsmap_version();
}
/**
* Detects whether the installed plugin version differs from this
* plugin's INFO file version and, if so, runs the database schema
* upgrade; also self-heals a legacy misspelled 'gpsmap_latutude' setting
* key by migrating it to 'gpsmap_latitude'. Only runs on gpsmap.php/
* gpstemplates.php/plugins.php/poller.php unless $force is set. Called
* from plugin_gpsmap_check_config()/plugin_gpsmap_upgrade() and on
* relevant page loads.
*
* Migrate the misspelled 'gpsmap_latutude' key to 'gpsmap_latitude'. Runs
* on every version transition so it self-heals on first upgrade.
* read_config_option() returns '' for missing keys in most Cacti
* versions, but some older versions return false or null. The triple-
* check guards against all known return values so the DELETE only fires
* when the old key actually exists with a non-empty value.
*
* @param bool $force Whether to run the upgrade check regardless of the
* current page; defaults to false.
*
* @return void
*
* @global array $config Cacti global configuration array; used to load
* includes/setup/database.php.
*/
function gpsmap_check_upgrade(bool $force = false) {
global $config;
$files = ['gpsmap.php', 'gpstemplates.php', 'plugins.php', 'poller.php'];
if (!$force && !in_array(get_current_page(), $files, true)) {
return;
}
$info = plugin_gpsmap_version();
if (empty($info['version'])) {
cacti_log('ERROR: gpsmap plugin INFO file is missing required fields, skipping upgrade check', false, 'GPSMAP');
return;
}
$current = $info['version'];
$old = read_config_option('plugin_gpsmap_version', true);
if ($current != $old) {
include_once($config['base_path'] . '/plugins/gpsmap/includes/setup/database.php');
gpsmap_upgrade_database((string) $old, $force);
}
/* Migrate the misspelled 'gpsmap_latutude' key to 'gpsmap_latitude'.
* Runs on every version transition so it self-heals on first upgrade.
* read_config_option() returns '' for missing keys in most Cacti versions,
* but some older versions return false or null. The triple-check guards
* against all known return values so the DELETE only fires when the old
* key actually exists with a non-empty value. */
$old_lat = read_config_option('gpsmap_latutude');
if ($old_lat !== false && $old_lat !== null && $old_lat !== '') {
$current_lat = read_config_option('gpsmap_latitude');
if ($current_lat === false || $current_lat === null || $current_lat === '') {
set_config_option('gpsmap_latitude', $old_lat);
}
db_execute_prepared('DELETE FROM settings WHERE name = ?', ['gpsmap_latutude']);
}
}
/**
* Hook implementation for Cacti's 'page_head' filter. Includes the
* Google Maps JavaScript API (with the configured API key, when set) and
* this plugin's GPSMaps.js/infobubble.js scripts on every page. Called by
* Cacti core via api_plugin_hook('page_head', ...) while rendering the
* page <head> section.
*
* @return void Outputs HTML directly.
*
* @global array $config Cacti global configuration array; used to build
* this plugin's script URLs.
*/
function gpsmap_page_head() {
global $config;
$apiKey = read_config_option('gpsmap_apikey');
print "<script type='text/javascript' src='https://maps.googleapis.com/maps/api/js?" . (empty($apiKey) === false ? 'key=' . rawurlencode($apiKey) . '&' : '') . "libraries=geometry'></script>" . PHP_EOL;
print "<script type='text/javascript' src='" . $config['url_path'] . "plugins/gpsmap/js/GPSMaps.js'></script>" . PHP_EOL;
print "<script type='text/javascript' src='" . $config['url_path'] . "plugins/gpsmap/js/infobubble.js'></script>" . PHP_EOL;
}
/**
* Reads this plugin's INFO file and returns its [info] section. Called
* from plugin_gpsmap_version()/gpsmap_check_upgrade() to detect/report
* the plugin's version.
*
* @return array The parsed [info] section of the plugin's INFO file (keys
* such as name, version, author).
*
* @global array $config Cacti global configuration array; used to locate
* the plugin's base path.
*/
function gpsmap_version() {
global $config;
$info = parse_ini_file($config['base_path'] . '/plugins/gpsmap/INFO', true);
return isset($info['info']) && is_array($info['info']) ? $info['info'] : [];
}
/**
* Hook implementation for Cacti's 'config_form' filter. Adds this
* plugin's Map Settings section (coverage-overlay inclusion, latitude/
* longitude, and, for Access Point host templates, directional coverage
* start/stop degrees and radius) to the Device edit form, immediately
* after the 'disabled' field. Called by Cacti core via
* api_plugin_hook('config_form', ...) while building the Device edit
* form.
*
* Defines latitude and longitude for devices.
*
* @return void
*
* @global array $fields_host_edit The Device edit form's field
* definitions, rebuilt here with this
* plugin's fields inserted after
* 'disabled'.
* @global string $url_path Cacti's configured URL path; used to
* detect whether the current page is
* host.php before querying Access Point
* fields.
*/
function gpsmap_config_form() {
global $fields_host_edit, $url_path;
$fields_host_edit2 = $fields_host_edit;
$fields_host_edit3 = [];
foreach ($fields_host_edit2 as $f => $a) {
$fields_host_edit3[$f] = $a;
if ($f == 'disabled') {
$fields_host_edit3['gpsSpacer'] = [
'friendly_name' => __('Map Settings', 'gpsmap'),
'method' => 'spacer',
];
$fields_host_edit3['GPScoverage'] = [
'friendly_name' => __('Overlay Inclusion', 'gpsmap'),
'description' => __('Disable to plot host only, not included in coverage overlay.', 'gpsmap'),
'method' => 'checkbox',
'value' => '|arg1:GPScoverage|',
'default' => 'on',
];
$fields_host_edit3['latitude'] = [
'friendly_name' => __('Latitude', 'gpsmap'),
'description' => __('The devices latitude coordinates', 'gpsmap'),
'method' => 'textbox',
'max_length' => 13,
'value' => '|arg1:latitude|',
'default' => '',
];
$fields_host_edit3['longitude'] = [
'friendly_name' => __('Longitude', 'gpsmap'),
'description' => __('The devices longitude coordinates', 'gpsmap'),
'method' => 'textbox',
'max_length' => 13,
'value' => '|arg1:longitude|',
'default' => '',
];
if (isset_request_var('id') && get_current_page() == $url_path . 'host.php') {
$did = get_filter_request_var('id');
$row = db_fetch_row_prepared('SELECT AP
FROM `host`
RIGHT JOIN gpsmap_templates
ON host.host_template_id = gpsmap_templates.templateID
WHERE id = ?',
[$did]);
if (is_array($row) && cacti_sizeof($row) && $row['AP'] == 1) {
$fields_host_edit3['start'] = [
'friendly_name' => __('Starting Degree', 'gpsmap'),
'description' => __('Starting degree for directional area between 0-360', 'gpsmap'),
'method' => 'textbox',
'max_length' => 4,
'value' => '|arg1:start|',
'default' => '0',
];
$fields_host_edit3['stop'] = [
'friendly_name' => __('Stopping Degree', 'gpsmap'),
'description' => __('Stopping degree for directional area, must be greater than the Starting Degree', 'gpsmap'),
'method' => 'textbox',
'max_length' => 4,
'value' => '|arg1:stop|',
'default' => '360',
];
$fields_host_edit3['rdistance'] = [
'friendly_name' => __('Specify Radius', 'gpsmap'),
'description' => __('Manually specify radius for Access Point. Set to 0 to determine radius based on grouped devices', 'gpsmap'),
'method' => 'textbox',
'max_length' => 10,
'value' => '|arg1:rdistance|',
'default' => '0',
];
}
}
$fields_host_edit3['groupnum'] = [
'friendly_name' => __('Group ID', 'gpsmap'),
'description' => __('Groups define what devices are included in the coverage overlay. Will be checked against AP device group number. 0 to disable', 'gpsmap'),
'method' => 'textbox',
'max_length' => 3,
'value' => '|arg1:groupnum|',
'default' => '0',
];
}
}
$fields_host_edit = $fields_host_edit3;
}