-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdatabase.php
More file actions
504 lines (437 loc) · 13.8 KB
/
Copy pathdatabase.php
File metadata and controls
504 lines (437 loc) · 13.8 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php
/*
+-------------------------------------------------------------------------+
| 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. |
+-------------------------------------------------------------------------+
| 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/ |
+-------------------------------------------------------------------------+
*/
/**
* flowview_db_connect_real - makes a connection to the database server
* on this machine
*
* @param mixed $host
* @param mixed $user
* @param mixed $pass
* @param mixed $db_name
* @param mixed $db_type
* @param string $port
* @param int $retries
* @param string $db_ssl
* @param string $db_ssl_key
* @param string $db_ssl_cert
* @param string $db_ssl_ca
*
* @return mixed
*/
function flowview_db_connect_real($host, $user, $pass, $db_name, $db_type, string $port = '3306', int $retries = 20, string $db_ssl = '',
string $db_ssl_key = '', string $db_ssl_cert = '', string $db_ssl_ca = '') {
return db_connect_real($host, $user, $pass, $db_name, $db_type, (int) $port, $retries, (bool) $db_ssl, $db_ssl_key, $db_ssl_cert, $db_ssl_ca);
}
/**
* flowview_db_close - closes the open connection
*
* @param mixed $flowview_cnn
*
* @return mixed
*/
function flowview_db_close(&$flowview_cnn) {
return db_close($flowview_cnn);
}
/**
* flowview_db_check_reconnect - check the flowview database connection. If
* the connection is gone, attempt to reconnect and update the global
* flowview connection with the new one.
*
* @param bool $log
*
* @return mixed
*/
function flowview_db_check_reconnect(bool $log = true) {
global $flowview_cnn, $config, $local_db_cnn_id, $remote_db_cnn_id;
$previous_cnn = $flowview_cnn;
$result = db_check_reconnect($flowview_cnn, $log);
/**
* When flowview is sharing the main Cacti database, $flowview_cnn is
* just an alias of $local_db_cnn_id/$remote_db_cnn_id set up by
* flowview_connect(). Propagate the reconnected handle back to
* whichever one it was aliasing, otherwise a subsequent call to
* flowview_connect() will overwrite $flowview_cnn with the stale,
* dead connection again.
*/
if ($flowview_cnn !== $previous_cnn) {
if ($config['poller_id'] == 1 && $local_db_cnn_id === $previous_cnn) {
$local_db_cnn_id = $flowview_cnn;
} elseif ($remote_db_cnn_id === $previous_cnn) {
$remote_db_cnn_id = $flowview_cnn;
}
}
return $result;
}
/**
* flowview_db_execute - run an sql query and do not return any output
*
* @param mixed $sql
* @param bool $log
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_execute($sql, bool $log = true, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_execute($sql, $log, $flowview_cnn);
}
/**
* flowview_db_execute_prepared - run an sql query and do not return any output
*
* @param mixed $sql
* @param array $parms
* @param bool $log
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_execute_prepared($sql, array $parms = [], bool $log = true, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_execute_prepared($sql, $parms, $log, $flowview_cnn);
}
/**
* flowview_db_fetch_cell - run a 'select' sql query and return the first column of the
* first row found
*
* @param mixed $sql
* @param string $col_name
* @param bool $log
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_fetch_cell($sql, string $col_name = '', bool $log = true, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_fetch_cell($sql, $col_name, $log, $flowview_cnn);
}
/**
* flowview_db_fetch_cell_prepared - run a 'select' sql query and return the first column of the
* first row found
*
* @param mixed $sql
* @param array $params
* @param string $col_name
* @param bool $log
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_fetch_cell_prepared($sql, array $params = [], string $col_name = '', bool $log = true, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_fetch_cell_prepared($sql, $params, $col_name, $log, $flowview_cnn);
}
/**
* flowview_db_fetch_row - run a 'select' sql query and return the first row found
*
* @param mixed $sql
* @param bool $log
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_fetch_row($sql, bool $log = true, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_fetch_row($sql, $log, $flowview_cnn);
}
/**
* flowview_db_fetch_row_prepared - run a 'select' sql query and return the first row found
*
* @param mixed $sql
* @param array $params
* @param bool $log
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_fetch_row_prepared($sql, array $params = [], bool $log = true, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_fetch_row_prepared($sql, $params, $log, $flowview_cnn);
}
/**
* flowview_db_fetch_assoc - run a 'select' sql query and return all rows found
*
* @param mixed $sql
* @param bool $log
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_fetch_assoc($sql, bool $log = true, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_fetch_assoc($sql, $log, $flowview_cnn);
}
/**
* flowview_db_fetch_assoc_prepared - run a 'select' sql query and return all rows found
*
* @param mixed $sql
* @param array $params
* @param bool $log
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_fetch_assoc_prepared($sql, array $params = [], bool $log = true, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_fetch_assoc_prepared($sql, $params, $log, $flowview_cnn);
}
/**
* flowview_db_fetch_insert_id - get the last insert_id or auto incriment
*
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_fetch_insert_id(mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_fetch_insert_id($flowview_cnn);
}
/**
* flowview_db_replace - replaces the data contained in a particular row
*
* @param mixed $table_name
* @param mixed $array_items
* @param mixed $keyCols
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_replace($table_name, $array_items, $keyCols, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_replace($table_name, $array_items, $keyCols, $flowview_cnn);
}
/**
* flowview_sql_save - saves data to an sql table
*
* @param mixed $array_items
* @param mixed $table_name
* @param string $key_cols
* @param bool $autoinc
* @param $cnn_id
*
* @return mixed
*/
function flowview_sql_save($array_items, $table_name, string $key_cols = 'id', bool $autoinc = true, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return sql_save($array_items, $table_name, $key_cols, $autoinc, $flowview_cnn);
}
/**
* flowview_db_table_exists - checks whether a table exists
*
* @param mixed $table
* @param bool $log
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_table_exists($table, bool $log = true, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
$matched = preg_match('/^(?:`?(?<database>[A-Za-z0-9_]+)`?\.)?`?(?<table>[A-Za-z0-9_]+)`?$/D', $table, $matches);
if ($matched === 1) {
$table_pattern = addcslashes($matches['table'], '\\%_');
$sql = 'SHOW TABLES LIKE \'' . $table_pattern . '\'';
return (db_fetch_cell($sql, '', $log, $flowview_cnn) ? true : false);
}
return false;
}
/**
* flowview_db_table_create - creates a database table if it does not already exist
*
* @param mixed $table
* @param mixed $data
* @param $cnn_id
*
* @return void
*/
function flowview_db_table_create($table, $data, mixed $cnn_id = false): void {
$flowview_cnn = flowview_get_connection($cnn_id);
$result = flowview_db_fetch_assoc('SHOW TABLES');
$tables = [];
foreach($result as $index => $arr) {
foreach ($arr as $t) {
$tables[] = $t;
}
}
if (!in_array($table, $tables)) {
$c = 0;
$sql = 'CREATE TABLE IF NOT EXISTS `' . $table . "` (\n";
foreach ($data['columns'] as $column) {
if (isset($column['name'])) {
if ($c > 0) {
$sql .= ",\n";
}
$sql .= '`' . $column['name'] . '`';
if (isset($column['type'])) {
$sql .= ' ' . $column['type'];
}
if (isset($column['unsigned'])) {
$sql .= ' unsigned';
}
if (isset($column['NULL']) && $column['NULL'] == false) {
$sql .= ' NOT NULL';
}
if (isset($column['NULL']) && $column['NULL'] == true && !isset($column['default'])) {
$sql .= ' default NULL';
}
if (isset($column['default'])) {
if (strtolower($column['type']) == 'timestamp' && $column['default'] === 'CURRENT_TIMESTAMP') {
$sql .= ' default CURRENT_TIMESTAMP';
} else {
$sql .= ' default ' . (is_numeric($column['default']) ? $column['default'] : "'" . $column['default'] . "'");
}
}
if (isset($column['auto_increment'])) {
$sql .= ' auto_increment';
}
$c++;
}
}
if (isset($data['primary'])) {
$sql .= ",\n PRIMARY KEY (`" . $data['primary'] . '`)';
}
if (isset($data['keys']) && cacti_sizeof($data['keys'])) {
foreach ($data['keys'] as $key) {
if (isset($key['name'])) {
$sql .= ",\n INDEX `" . $key['name'] . '` (' . db_format_index_create($key['columns']) . ')';
}
}
}
if (isset($data['unique_keys'])) {
foreach ($data['unique_keys'] as $key) {
if (isset($key['name'])) {
$sql .= ",\n UNIQUE INDEX `" . $key['name'] . '` (' . db_format_index_create($key['columns']) . ')';
}
}
}
$sql .= ') ENGINE = ' . $data['type'];
if (isset($data['charset'])) {
$sql .= ' DEFAULT CHARSET = ' . $data['charset'];
}
if (isset($data['row_format']) && strtolower(db_get_global_variable('innodb_file_format')) == 'barracuda') {
$sql .= ' ROW_FORMAT = ' . $data['row_format'];
}
if (isset($data['comment'])) {
$sql .= " COMMENT = '" . $data['comment'] . "'";
}
if (flowview_db_execute($sql)) {
db_execute_prepared("REPLACE INTO plugin_db_changes
(plugin, `table`, `column`, `method`)
VALUES (?, ?, '', 'create')",
['flowview', $table]);
if (isset($data['collate'])) {
flowview_db_execute("ALTER TABLE `$table` COLLATE = " . $data['collate']);
}
}
}
}
/**
* flowview_db_column_exists - checks whether a column exists
*
* @param mixed $table
* @param mixed $column
* @param bool $log
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_column_exists($table, $column, bool $log = true, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_column_exists($table, $column, $log, $flowview_cnn);
}
/**
* flowview_db_add_column - adds a column to a table if it does not already exist
*
* @param mixed $table
* @param mixed $column
* @param bool $log
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_add_column($table, $column, bool $log = true, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_add_column($table, $column, $log, $flowview_cnn);
}
/**
* flowview_db_affected_rows - return the number of rows affected by the last transaction
* or false on error
*
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_affected_rows(mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_affected_rows($flowview_cnn);
}
/**
* flowview_db_index_exists - checks whether an index exists
*
* @param mixed $table
* @param mixed $index
* @param bool $log
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_index_exists($table, $index, bool $log = true, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
return db_index_exists($table, $index, $log, $flowview_cnn);
}
/**
* flowview_get_connection - resolves the database connection to use for a
* flowview_db_* call, falling back to this plugin's default connection
* when no explicit connection id is given
* the plugin's default connection
*
* @param mixed $cnn_id
*
* @return mixed
*/
function flowview_get_connection($cnn_id) {
global $flowview_cnn;
if ($cnn_id === false) {
return $flowview_cnn;
} else {
return $cnn_id;
}
}
/**
* flowview_db_get_table_column_types - returns all the types for each column of a table
*
* @param mixed $table
* @param $cnn_id
*
* @return mixed
*/
function flowview_db_get_table_column_types($table, mixed $cnn_id = false) {
$flowview_cnn = flowview_get_connection($cnn_id);
$columns = db_fetch_assoc("SHOW COLUMNS FROM $table", false, $flowview_cnn);
$cols = [];
if (cacti_sizeof($columns)) {
foreach($columns as $col) {
$cols[$col['Field']] = ['type' => $col['Type'], 'null' => $col['Null'], 'default' => $col['Default'], 'extra' => $col['Extra']];
}
}
return $cols;
}