diff --git a/.gitignore b/.gitignore index 6621d40..f857b7e 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ .git* locales/po/*.mo +.omc/ diff --git a/functions.php b/functions.php index c3627e0..e44a96c 100644 --- a/functions.php +++ b/functions.php @@ -98,7 +98,7 @@ function graph_export($id = 0, $force = false) { db_execute_prepared('UPDATE graph_exports SET last_checked = NOW() WHERE id = ?', - array($export['id'])); + [$export['id']]); $runnow = false; if (!$force) { @@ -108,7 +108,7 @@ function graph_export($id = 0, $force = false) { db_execute_prepared('UPDATE graph_exports SET next_start = ? WHERE id = ?', - array($next_start, $export['id'])); + [$next_start, $export['id']]); } } else { $runnow = true; @@ -207,7 +207,7 @@ function run_export(&$export) { export_pre_ftp_upload($export, $stExportDir); export_log('Using ncftpput.'); - export_ftp_ncftpput_execute($export, $stExportDir); + export_ftp_ncftpput_execute($stExportDir); export_post_ftp_upload($export, $stExportDir); break; @@ -253,7 +253,7 @@ function run_export(&$export) { export_fatal($export, 'Export method not specified. Exporting can not continue. Please set method properly in Cacti configuration.'); } - db_execute_prepared('UPDATE graph_exports SET export_pid = 0 WHERE id = ?', array($export['id'])); + db_execute_prepared('UPDATE graph_exports SET export_pid = 0 WHERE id = ?', [$export['id']]); config_export_stats($export, $exported); } @@ -263,7 +263,7 @@ function export_rsync_execute(&$export, $stExportDir) { $user = $export['export_user']; $port = $export['export_port']; $host = $export['export_host']; - $output = array(); + $output = []; $prune = ''; $retvar = 0; @@ -340,7 +340,7 @@ function export_scp_execute(&$export, $stExportDir) { $user = $export['export_user']; $port = $export['export_port']; $host = $export['export_host']; - $output = array(); + $output = []; $retvar = 0; if ($export['export_private_key_path'] != '') { @@ -444,9 +444,9 @@ function config_export_stats(&$export, $exported) { db_execute_prepared('UPDATE graph_exports SET last_runtime = ?, total_graphs = ?, last_ended=NOW(), status=0 WHERE id = ?', - array($end - $start, $exported, $export['id'])); + [$end - $start, $exported, $export['id']]); - db_execute_prepared(sprintf("REPLACE INTO settings (name,value) values ('stats_export_%s', ?)", $export['id']), array($export_stats)); + db_execute_prepared(sprintf("REPLACE INTO settings (name,value) values ('stats_export_%s', ?)", $export['id']), [$export_stats]); } /* export_fatal - a simple export logging function that indicates a @@ -460,7 +460,7 @@ function export_fatal(&$export, $stMessage) { db_execute_prepared('UPDATE graph_exports SET last_error = ?, last_ended=NOW(), last_errored=NOW(), status=2 WHERE id = ?', - array($stMessage, $export['id'])); + [$stMessage, $export['id']]); exit; } @@ -558,7 +558,7 @@ function check_cacti_paths(&$export, $export_path) { /* check for bad directories within the cacti path */ if (strcasecmp($root_path, $export_path) < 0) { - $cacti_system_paths = array( + $cacti_system_paths = [ 'include', 'lib', 'install', @@ -567,7 +567,7 @@ function check_cacti_paths(&$export, $export_path) { 'scripts', 'plugins', 'images', - 'resource'); + 'resource']; foreach($cacti_system_paths as $cacti_system_path) { if (substr_count(strtolower($export_path), strtolower($cacti_system_path)) > 0) { @@ -591,7 +591,7 @@ function check_cacti_paths(&$export, $export_path) { function check_system_paths(&$export, $export_path) { /* don't allow to export to system paths */ - $system_paths = array( + $system_paths = [ '/boot', '/lib', '/usr', @@ -606,7 +606,7 @@ function check_system_paths(&$export, $export_path) { '/etc', 'windows', 'winnt', - 'program files'); + 'program files']; foreach($system_paths as $system_path) { if (substr($system_path, 0, 1) == '/') { @@ -676,16 +676,16 @@ function export_graphs(&$export, $export_path) { $sites = $export['graph_site']; $export_id = $export['id']; - $ntree = array(); - $graphs = array(); - $ngraph = array(); + $ntree = []; + $graphs = []; + $ngraph = []; $limit = 1000; $sql_where = ''; $hosts = ''; $total_rows = 0; $exported = 0; - $metadata = array(); + $metadata = []; if ($user == 0) { $user = -1; @@ -792,7 +792,7 @@ function export_graphs(&$export, $export_path) { SET last_error="WARNING: Max number of Graphs ' . $export['graph_max'] . ' reached", last_errored=NOW() WHERE id = ?', - array($export['id'])); + [$export['id']]); break; } @@ -807,9 +807,21 @@ function export_graphs(&$export, $export_path) { } function delTree($dir, $skip = false) { - $files = array_diff(scandir($dir), array('.','..')); + $dir = realpath($dir); + if ($dir === false) { + return false; + } + + $files = array_diff(scandir($dir), ['.','..']); foreach ($files as $file) { - (is_dir("$dir/$file") && !is_link($dir)) ? delTree("$dir/$file") : unlink("$dir/$file"); + $path = "$dir/$file"; + if (is_link($path)) { + unlink($path); + } elseif (is_dir($path)) { + delTree($path); + } else { + unlink($path); + } } return ($skip ? 0 : rmdir($dir)); } @@ -875,7 +887,7 @@ function export_graph_monitor_tasks($export) { WHERE status = 1 AND id = ? AND pid = ?', - array($id, $pid)); + [$id, $pid]); $thread_adj--; } } @@ -920,7 +932,7 @@ function export_graph_prepare_task($export_id, $user, $folder, $local_graph_id) // MJV: Do something here db_execute_prepared('INSERT INTO graph_exports_tasks (export_id, local_graph_id, user, folder) - VALUES (?, ?, ?, ?)', array($export_id, $local_graph_id, $user, $folder)); + VALUES (?, ?, ?, ?)', [$export_id, $local_graph_id, $user, $folder]); } function export_graph_start_task($task_id) { @@ -930,7 +942,7 @@ function export_graph_start_task($task_id) { $task = db_fetch_row_prepared('SELECT * FROM graph_exports_tasks WHERE id = ?', - array($task_id)); + [$task_id]); if (!sizeof($task)) { export_warn('TASKS Launched ' . $task_id . ' - Invalid ID, Aborting'); @@ -944,14 +956,14 @@ function export_graph_start_task($task_id) { $export = db_fetch_row_prepared('SELECT * FROM graph_exports WHERE id = ?', - array($task['export_id'])); + [$task['export_id']]); $exports = export_graph_files($export, $task['user'], $task['folder'], $task['local_graph_id']); db_execute_prepared('UPDATE graph_exports_tasks SET status = 2 WHERE id = ?', - array($task['id'])); + [$task['id']]); } $end = microtime(true); @@ -1207,6 +1219,12 @@ function export_ftp_php_uploaddir($dir, $oFtpConnection) { function export_ftp_ncftpput_execute($stExportDir) { global $aFtpExport; + $stExportDir = realpath($stExportDir); + if ($stExportDir === false) { + cacti_log('ERROR: Export directory does not exist for ncftpput', true, 'EXPORT'); + return; + } + chdir($stExportDir); /* set the initial command structure */ @@ -1224,7 +1242,7 @@ function export_ftp_ncftpput_execute($stExportDir) { $iExecuteReturns = 0; system($stExecute, $iExecuteReturns); - $aNcftpputStatusCodes = array ( + $aNcftpputStatusCodes = [ 'Success.', 'Could not connect to remote host.', 'Could not connect to remote host - timed out.', @@ -1236,7 +1254,7 @@ function export_ftp_ncftpput_execute($stExportDir) { 'Usage error.', 'Error in login configuration file.', 'Library initialization failed.', - 'Session initialization failed.'); + 'Session initialization failed.']; export_log('Ncftpput returned: ' . $aNcftpputStatusCodes[$iExecuteReturns]); } @@ -1281,9 +1299,17 @@ function export_post_ftp_upload(&$export, $stExportDir) { @arg $user - the effective user to use for export, -1 indicates no permission check @arg $export_path - the location to store the json array configuration file */ function write_branch_conf($tree_site_id, $branch_id, $type, $host_id, $sub_id, $user, $export_path) { - static $json_files = array(); + static $json_files = []; $total_rows = 0; - $graph_array = array(); + $graph_array = []; + + $export_path = realpath($export_path); + if ($export_path === false) { + return 0; + } + $tree_site_id = (int) $tree_site_id; + $branch_id = (int) $branch_id; + $host_id = (int) $host_id; if ($type == 'branch') { $json_file = $export_path . '/tree_' . $tree_site_id . '_branch_' . $branch_id . '.json'; @@ -1295,47 +1321,55 @@ function write_branch_conf($tree_site_id, $branch_id, $type, $host_id, $sub_id, WHERE graph_tree_id = ? AND parent = ? AND local_graph_id > 0 - ORDER BY position', array($tree_site_id, $branch_id)); + ORDER BY position', [$tree_site_id, $branch_id]); } elseif ($type == 'gtbranch') { $json_file = $export_path . '/site_' . $tree_site_id . '_gtbranch_0.json'; if (isset($json_files[$json_file])) return; - $graphs = array(); + $graphs = []; } elseif ($type == 'dqbranch') { $json_file = $export_path . '/site_' . $tree_site_id . '_gtbranch_0.json'; if (isset($json_files[$json_file])) return; - $graphs = array(); + $graphs = []; } elseif ($type == 'site') { $json_file = $export_path . '/site_' . $tree_site_id . '.json'; if (isset($json_files[$json_file])) return; - $graphs = array(); + $graphs = []; } elseif ($type == 'site_dt') { $json_file = $export_path . '/site_' . $tree_site_id . '_dt_' . $sub_id . '.json'; if (isset($json_files[$json_file])) return; - $graphs = array(); + $graphs = []; } elseif ($type == 'site_gt') { $json_file = $export_path . '/site_' . $tree_site_id . '_gt_' . $sub_id . '.json'; if (isset($json_files[$json_file])) return; - $devices = array_rekey(db_fetch_assoc_prepared('SELECT id FROM host WHERE site_id = ?', array($tree_site_id)), 'id', 'id'); + $devices = array_rekey(db_fetch_assoc_prepared('SELECT id FROM host WHERE site_id = ?', [$tree_site_id]), 'id', 'id'); - $graphs = get_allowed_graphs('(gl.host_id IN(' . implode(',', $devices) . ') AND gt.id = ' . $sub_id . ')', 'gtg.title_cache', '', $total_rows, $user); + if (empty($devices)) { + $graphs = []; + } else { + $graphs = get_allowed_graphs('(gl.host_id IN(' . implode(',', $devices) . ') AND gt.id = ' . $sub_id . ')', 'gtg.title_cache', '', $total_rows, $user); + } } elseif ($type == 'site_dq') { $json_file = $export_path . '/site_' . $tree_site_id . '_dq_' . $sub_id . '.json'; if (isset($json_files[$json_file])) return; - $devices = array_rekey(db_fetch_assoc_prepared('SELECT id FROM host WHERE site_id = ?', array($tree_site_id)), 'id', 'id'); + $devices = array_rekey(db_fetch_assoc_prepared('SELECT id FROM host WHERE site_id = ?', [$tree_site_id]), 'id', 'id'); - $graphs = get_allowed_graphs('(gl.host_id IN(' . implode(',', $devices) . ') AND gl.snmp_query_id = ' . $sub_id . ')', 'gtg.title_cache', '', $total_rows, $user); + if (empty($devices)) { + $graphs = []; + } else { + $graphs = get_allowed_graphs('(gl.host_id IN(' . implode(',', $devices) . ') AND gl.snmp_query_id = ' . $sub_id . ')', 'gtg.title_cache', '', $total_rows, $user); + } } elseif ($type == 'site_dqi') { $parts = explode(':', $sub_id); $dq = $parts[0]; @@ -1346,7 +1380,7 @@ function write_branch_conf($tree_site_id, $branch_id, $type, $host_id, $sub_id, if (isset($json_files[$json_file])) return; - $devices = array_rekey(db_fetch_assoc_prepared('SELECT id FROM host WHERE site_id = ?', array($tree_site_id)), 'id', 'id'); + $devices = array_rekey(db_fetch_assoc_prepared('SELECT id FROM host WHERE site_id = ?', [$tree_site_id]), 'id', 'id'); $sql_where = ''; if (is_array($values) && cacti_sizeof($values)) { @@ -1358,7 +1392,11 @@ function write_branch_conf($tree_site_id, $branch_id, $type, $host_id, $sub_id, $sql_where .= ')'; } - $graphs = get_allowed_graphs('(gl.host_id IN(' . implode(',', $devices) . ') AND gl.snmp_query_id=' . $dq . $sql_where . ')', 'gtg.title_cache', '', $total_rows, $user); + if (empty($devices)) { + $graphs = []; + } else { + $graphs = get_allowed_graphs('(gl.host_id IN(' . implode(',', $devices) . ') AND gl.snmp_query_id=' . $dq . $sql_where . ')', 'gtg.title_cache', '', $total_rows, $user); + } } elseif ($type == 'host') { $json_file = $export_path . '/host_' . $host_id . '.json'; @@ -1411,7 +1449,7 @@ function write_branch_conf($tree_site_id, $branch_id, $type, $host_id, $sub_id, $json_files[$json_file] = true; - return cacti_sizeof($graph_array);; + return cacti_sizeof($graph_array); } /* export_generate_tree_html - create jstree compatible static tree html. This is a @@ -1441,7 +1479,7 @@ function export_generate_tree_html($export_path, $tree, $parent, $expand_hosts, AND host_id = 0 AND graph_tree_id = ? AND parent = ?', - array($tree['id'], $parent)); + [$tree['id'], $parent]); if (cacti_sizeof($branches)) { foreach($branches as $branch) { @@ -1452,7 +1490,7 @@ function export_generate_tree_html($export_path, $tree, $parent, $expand_hosts, WHERE graph_tree_id = ? AND local_graph_id = 0 AND parent = ?', - array($tree['id'], $branch['id'])); + [$tree['id'], $branch['id']]); $jstree .= str_repeat("\t", $depth) . '
  • ' . $branch['title']; @@ -1475,7 +1513,7 @@ function export_generate_tree_html($export_path, $tree, $parent, $expand_hosts, WHERE graph_tree_id = ? AND parent = ? AND host_id > 0 - ORDER BY position', array($tree['id'], $parent)); + ORDER BY position', [$tree['id'], $parent]); if (cacti_sizeof($hosts)) { foreach($hosts as $host) { @@ -1519,7 +1557,7 @@ function export_generate_tree_html($export_path, $tree, $parent, $expand_hosts, INNER JOIN host_snmp_query AS hsq ON sq.id=hsq.snmp_query_id WHERE hsq.host_id = ?', - array($host['host_id'])); + [$host['host_id']]); $data_queries[] = array('id' => '0', 'name' => __('Non Query Based', 'gexport')); @@ -1543,9 +1581,9 @@ function export_generate_tree_html($export_path, $tree, $parent, $expand_hosts, FROM graph_local AS gl WHERE host_id = ? AND snmp_query_id = ?', - array($host['host_id'], $query['id'])); + [$host['host_id'], $query['id']]); - $dqi = array(); + $dqi = []; foreach($graphs as $graph) { $dqi[$graph['snmp_index']] = $graph['snmp_index']; } @@ -1621,7 +1659,7 @@ function export_generate_site_html($export_path, $site, $parent, $expand_hosts, FROM host_template AS dt INNER JOIN host AS h ON h.host_template_id=dt.id - WHERE h.site_id = ?', array($site['id'])); + WHERE h.site_id = ?', [$site['id']]); if (cacti_sizeof($device_templates)) { foreach($device_templates as $branch) { @@ -1638,7 +1676,7 @@ function export_generate_site_html($export_path, $site, $parent, $expand_hosts, WHERE site_id = ? AND host_template_id = ? ORDER BY description', - array($site['id'], $branch['id'])); + [$site['id'], $branch['id']]); if (cacti_sizeof($hosts)) { foreach($hosts as $host) { @@ -1682,7 +1720,7 @@ function export_generate_site_html($export_path, $site, $parent, $expand_hosts, INNER JOIN host_snmp_query AS hsq ON sq.id=hsq.snmp_query_id WHERE hsq.host_id = ?', - array($host['host_id'])); + [$host['host_id']]); $data_queries[] = array('id' => '0', 'name' => __('Non Query Based', 'gexport')); @@ -1706,9 +1744,9 @@ function export_generate_site_html($export_path, $site, $parent, $expand_hosts, FROM graph_local AS gl WHERE host_id = ? AND snmp_query_id = ?', - array($host['host_id'], $query['id'])); + [$host['host_id'], $query['id']]); - $dqi = array(); + $dqi = []; foreach($graphs as $graph) { $dqi[$graph['snmp_index']] = $graph['snmp_index']; } @@ -1765,7 +1803,7 @@ function export_generate_site_html($export_path, $site, $parent, $expand_hosts, INNER JOIN host AS h ON h.id = gl.host_id WHERE h.site_id = ? - ORDER BY gt.name', array($site['id'])); + ORDER BY gt.name', [$site['id']]); if (cacti_sizeof($graph_templates)) { $jstree .= str_repeat("\t", $depth) . "
  • " . __('Graph Templates', 'gexport') . "\n"; @@ -1794,7 +1832,7 @@ function export_generate_site_html($export_path, $site, $parent, $expand_hosts, ON dq.id=gl.snmp_query_id INNER JOIN host AS h ON h.id = gl.host_id - WHERE h.site_id = ?', array($site['id'])); + WHERE h.site_id = ?', [$site['id']]); if (cacti_sizeof($data_queries)) { $jstree .= str_repeat("\t", $depth) . "
  • " . __('Data Queries', 'gexport') . "\n"; @@ -1820,11 +1858,11 @@ function export_generate_site_html($export_path, $site, $parent, $expand_hosts, ON gl.host_id=h.id WHERE h.site_id = ? AND snmp_query_id = ?', - array($site['id'], $branch['id'])); + [$site['id'], $branch['id']]); - $sort_field_data = array(); + $sort_field_data = []; - $dqi = array(); + $dqi = []; foreach($graphs as $graph) { if (!isset($sort_field_data[$graph['host_id']])) { $sort_field_data[$graph['host_id']] = get_formatted_data_query_indexes($graph['host_id'], $branch['id']); @@ -1882,9 +1920,9 @@ function tree_site_export(&$export, $export_path) { $jstree .= str_repeat("\t", 5) . "var theme='" . $export['export_theme'] . "';\n"; $jstree .= str_repeat("\t", 4) . "\n"; - $jstree .= str_repeat("\t", 4) . "