4 Star 7 Fork 1

Gitee 极速下载/Cacti

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/Cacti/cacti
克隆/下载
tree.php 64.91 KB
一键复制 编辑 原始数据 按行查看 历史
TheWitness 提交于 2024-04-19 08:28 . QA: Fix #5731 Correctly
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360
<?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('./include/auth.php');
include_once('./lib/api_tree.php');
include_once('./lib/html_tree.php');
include_once('./lib/data_query.php');
$actions = array(
1 => __x('dropdown action', 'Delete'),
2 => __x('dropdown action', 'Publish'),
3 => __x('dropdown action', 'Un-Publish'),
4 => __x('dropdown action', 'Un-Lock')
);
/* set default action */
set_default_action();
if (get_request_var('action') != '') {
/* ================= input validation and session storage ================= */
$filters = array(
'tree_id' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => ''
),
'leaf_id' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => ''
),
'graph_tree_id' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => ''
),
'parent_item_id' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => ''
),
'parent' => array(
'filter' => FILTER_VALIDATE_REGEXP,
'options' => array('options' => array('regexp' => '/([_\-a-z:0-9#]+)/')),
'pageset' => true,
'default' => ''
),
'position' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => ''
),
'nodeid' => array(
'filter' => FILTER_VALIDATE_REGEXP,
'options' => array('options' => array('regexp' => '/([_\-a-z:0-9#]+)/')),
'pageset' => true,
'default' => ''
),
'id' => array(
'filter' => FILTER_VALIDATE_REGEXP,
'options' => array('options' => array('regexp' => '/([_\-a-z:0-9#]+)/')),
'pageset' => true,
'default' => ''
)
);
validate_store_request_vars($filters);
/* ================= input validation ================= */
}
switch (get_request_var('action')) {
case 'save':
form_save();
break;
case 'actions':
form_actions();
break;
case 'sortasc':
tree_sort_name_asc();
header('Location: tree.php');
break;
case 'sortdesc':
tree_sort_name_desc();
header('Location: tree.php');
break;
case 'edit':
top_header();
tree_edit();
bottom_footer();
break;
case 'sites':
display_sites();
break;
case 'hosts':
display_hosts();
break;
case 'graphs':
display_graphs();
break;
case 'tree_up':
tree_up();
break;
case 'tree_down':
tree_down();
break;
case 'ajax_dnd':
tree_dnd();
break;
case 'lock':
api_tree_lock(get_request_var('id'), $_SESSION[SESS_USER_ID]);
tree_edit(true);
break;
case 'unlock':
api_tree_unlock(get_request_var('id'), $_SESSION[SESS_USER_ID]);
tree_edit(true);
break;
case 'copy_node':
api_tree_copy_node(get_request_var('tree_id'), get_request_var('id'), get_request_var('parent'), get_request_var('position'));
break;
case 'create_node':
api_tree_create_node(get_request_var('tree_id'), get_request_var('id'), get_request_var('position'), get_nfilter_request_var('text'));
break;
case 'delete_node':
api_tree_delete_node(get_request_var('tree_id'), get_request_var('id'));
break;
case 'move_node':
api_tree_move_node(get_request_var('tree_id'), get_request_var('id'), get_request_var('parent'), get_request_var('position'));
break;
case 'rename_node':
api_tree_rename_node(get_request_var('tree_id'), get_request_var('id'), get_nfilter_request_var('text'));
break;
case 'get_node':
api_tree_get_node(get_request_var('tree_id'), get_request_var('id'));
break;
case 'get_host_sort':
get_host_sort_type();
break;
case 'set_host_sort':
set_host_sort_type();
break;
case 'get_branch_sort':
get_branch_sort_type();
break;
case 'set_branch_sort':
set_branch_sort_type();
break;
default:
top_header();
tree();
bottom_footer();
break;
}
function tree_get_max_sequence() {
$max_seq = db_fetch_cell('SELECT MAX(sequence) FROM graph_tree');
if ($max_seq == null) {
return 0;
}
return $max_seq;
}
function tree_check_sequences() {
$bad_seq = db_fetch_cell('SELECT COUNT(sequence)
FROM graph_tree
WHERE sequence <= 0');
$dup_seq = db_fetch_cell('SELECT SUM(count)
FROM (
SELECT sequence, COUNT(sequence) AS count
FROM graph_tree
GROUP BY sequence
) AS t
WHERE t.count > 1');
// report any bad or duplicate sequences to the log for reporting purposes
if ($bad_seq > 0) {
cacti_log('WARN: Found ' . $bad_seq . ' Sequences in graph_tree Table', false, 'TREE', POLLER_VERBOSITY_HIGH);
}
if ($dup_seq > 0) {
cacti_log('WARN: Found ' . $dup_seq . ' Sequences in graph_tree Table', false, 'TREE', POLLER_VERBOSITY_HIGH);
}
if ($bad_seq > 0 || $dup_seq > 0) {
// resequence the list so it has no gaps, and 0 values will appear at the top
// since thats where they would have been displayed
db_execute('SET @seq = 0; UPDATE graph_tree SET sequence = (@seq:=@seq+1) ORDER BY sequence, id;');
}
}
function tree_sort_name_asc() {
// resequence the list so it has no gaps, alphabetically ascending
db_execute('SET @seq = 0; UPDATE graph_tree SET sequence = (@seq:=@seq+1) ORDER BY name;');
}
function tree_sort_name_desc() {
// resequence the list so it has no gaps, alphabetically ascending
db_execute('SET @seq = 0; UPDATE graph_tree SET sequence = (@seq:=@seq+1) ORDER BY name DESC;');
}
function tree_down() {
tree_check_sequences();
$tree_id = get_filter_request_var('id');
$seq = db_fetch_cell_prepared('SELECT sequence
FROM graph_tree
WHERE id = ?',
array($tree_id));
$new_seq = $seq + 1;
/* update the old tree first */
db_execute_prepared('UPDATE graph_tree
SET sequence = ?
WHERE sequence = ?',
array($seq, $new_seq));
/* update the tree in question */
db_execute_prepared('UPDATE graph_tree
SET sequence = ?
WHERE id = ?',
array($new_seq, $tree_id));
header('Location: tree.php');
exit;
}
function tree_up() {
tree_check_sequences();
$tree_id = get_filter_request_var('id');
$seq = db_fetch_cell_prepared('SELECT sequence
FROM graph_tree
WHERE id = ?',
array($tree_id));
$new_seq = $seq - 1;
/* update the old tree first */
db_execute_prepared('UPDATE graph_tree
SET sequence = ?
WHERE sequence = ?',
array($seq, $new_seq));
/* update the tree in question */
db_execute_prepared('UPDATE graph_tree
SET sequence = ?
WHERE id = ?',
array($new_seq, $tree_id));
header('Location: tree.php');
exit;
}
function tree_dnd() {
if (isset_request_var('tree_ids') && is_array(get_nfilter_request_var('tree_ids'))) {
$tids = get_nfilter_request_var('tree_ids');
$sequence = 1;
foreach ($tids as $id) {
$id = str_replace('line', '', $id);
input_validate_input_number($id, 'id');
db_execute_prepared('UPDATE graph_tree
SET sequence = ?
WHERE id = ?',
array($sequence, $id));
$sequence++;
}
/**
* Save the last time a tree branch was created/updated
* for Caching.
*/
set_config_option('time_last_change_branch', time());
}
header('Location: tree.php');
exit;
}
function get_host_sort_type() {
if (isset_request_var('nodeid')) {
$ndata = explode('_', get_request_var('nodeid'));
if (cacti_sizeof($ndata)) {
foreach ($ndata as $n) {
$parts = explode(':', $n);
if (isset($parts[0]) && $parts[0] == 'tbranch') {
$branch = $parts[1];
input_validate_input_number($branch, 'branch');
$sort_type = db_fetch_cell_prepared('SELECT host_grouping_type
FROM graph_tree_items
WHERE id = ?',
array($branch));
if ($sort_type == HOST_GROUPING_GRAPH_TEMPLATE) {
print 'hsgt';
} else {
print 'hsdq';
}
}
}
}
} else {
return '';
}
}
function set_host_sort_type() {
$type = '';
$branch = '';
/* clean up type string */
if (isset_request_var('type')) {
set_request_var('type', sanitize_search_string(get_request_var('type')));
}
if (isset_request_var('nodeid')) {
$ndata = explode('_', get_request_var('nodeid'));
if (cacti_sizeof($ndata)) {
foreach ($ndata as $n) {
$parts = explode(':', $n);
if (isset($parts[0]) && $parts[0] == 'tbranch') {
$branch = $parts[1];
input_validate_input_number($branch, 'branch');
if (get_request_var('type') == 'hsgt') {
$type = HOST_GROUPING_GRAPH_TEMPLATE;
} else {
$type = HOST_GROUPING_DATA_QUERY_INDEX;
}
db_execute_prepared('UPDATE graph_tree_items
SET host_grouping_type = ?
WHERE id = ?',
array($type, $branch));
break;
}
}
}
}
return;
}
function get_branch_sort_type() {
if (isset_request_var('nodeid')) {
$ndata = explode('_', get_request_var('nodeid'));
if (cacti_sizeof($ndata)) {
foreach ($ndata as $n) {
$parts = explode(':', $n);
if (isset($parts[0]) && $parts[0] == 'tbranch') {
$branch = $parts[1];
input_validate_input_number($branch, 'branch');
$sort_type = db_fetch_cell_prepared('SELECT sort_children_type
FROM graph_tree_items
WHERE id = ?',
array($branch));
switch($sort_type) {
case TREE_ORDERING_INHERIT:
print __x('ordering of tree items', 'inherit');
break;
case TREE_ORDERING_NONE:
print __x('ordering of tree items', 'manual');
break;
case TREE_ORDERING_ALPHABETIC:
print __x('ordering of tree items', 'alpha');
break;
case TREE_ORDERING_NATURAL:
print __x('ordering of tree items', 'natural');
break;
case TREE_ORDERING_NUMERIC:
print __x('ordering of tree items', 'numeric');
break;
default:
print '';
break;
}
break;
}
}
}
} else {
print '';
}
}
function set_branch_sort_type() {
$type = '';
$branch = '';
/* clean up type string */
if (isset_request_var('type')) {
set_request_var('type', sanitize_search_string(get_request_var('type')));
}
if (isset_request_var('nodeid')) {
$ndata = explode('_', get_request_var('nodeid'));
if (cacti_sizeof($ndata)) {
foreach ($ndata as $n) {
$parts = explode(':', $n);
if (isset($parts[0]) && $parts[0] == 'tbranch') {
$branch = $parts[1];
input_validate_input_number($branch, 'branch');
switch(get_request_var('type')) {
case 'inherit':
$type = TREE_ORDERING_INHERIT;
break;
case 'manual':
$type = TREE_ORDERING_NONE;
break;
case 'alpha':
$type = TREE_ORDERING_ALPHABETIC;
break;
case 'natural':
$type = TREE_ORDERING_NATURAL;
break;
case 'numeric':
$type = TREE_ORDERING_NUMERIC;
break;
default:
break;
}
if (is_numeric($type) && is_numeric($branch)) {
db_execute_prepared('UPDATE graph_tree_items
SET sort_children_type = ?
WHERE id = ?',
array($type, $branch));
}
$first_child = db_fetch_row_prepared('SELECT id, graph_tree_id
FROM graph_tree_items
WHERE parent = ?
ORDER BY position
LIMIT 1',
array($branch));
if (!empty($first_child)) {
api_tree_sort_branch($first_child['id'], $first_child['graph_tree_id']);
}
break;
}
}
}
}
}
function form_save() {
/* clear graph tree cache on save - affects current user only, other users should see changes in <5 minutes */
if (isset($_SESSION['dhtml_tree'])) {
unset($_SESSION['dhtml_tree']);
}
if (isset_request_var('save_component_tree')) {
/* ================= input validation ================= */
get_filter_request_var('id');
get_filter_request_var('sequence');
/* ==================================================== */
if (get_filter_request_var('id') > 0) {
$prev_order = db_fetch_cell_prepared('SELECT sort_type
FROM graph_tree
WHERE id = ?',
array(get_request_var('id')));
} else {
$prev_order = 1;
}
$save['id'] = get_request_var('id');
$save['name'] = form_input_validate(get_nfilter_request_var('name'), 'name', '', false, 3);
$save['sort_type'] = form_input_validate(get_nfilter_request_var('sort_type'), 'sort_type', '', true, 3);
$save['last_modified'] = date('Y-m-d H:i:s', time());
$save['enabled'] = isset_request_var('enabled') ? 'on':'';
$save['modified_by'] = $_SESSION[SESS_USER_ID];
if (isempty_request_var('sequence')) {
$save['sequence'] = tree_get_max_sequence() + 1;
} else {
$save['sequence'] = get_request_var('sequence');
}
if (empty($save['id'])) {
$save['user_id'] = $_SESSION[SESS_USER_ID];
}
if (!is_error_message()) {
$tree_id = sql_save($save, 'graph_tree');
if ($tree_id) {
raise_message(1);
/* sort the tree using the algorithm chosen by the user */
if ($save['sort_type'] != $prev_order) {
if ($save['sort_type'] != TREE_ORDERING_NONE) {
sort_recursive(0, $tree_id);
}
}
if (empty($save['id'])) {
/**
* Save the last time a tree was created/updated
* for Caching.
*/
set_config_option('time_last_change_tree', time());
}
} else {
raise_message(2);
}
}
header("Location: tree.php?action=edit&id=$tree_id");
exit;
}
}
function sort_recursive($branch, $tree_id) {
$leaves = db_fetch_assoc_prepared('SELECT *
FROM graph_tree_items
WHERE graph_tree_id = ?
AND parent = ?
AND local_graph_id = 0
AND host_id = 0',
array($tree_id, $branch));
if (cacti_sizeof($leaves)) {
foreach ($leaves as $leaf) {
if ($leaf['sort_children_type'] == TREE_ORDERING_INHERIT) {
$first_child = db_fetch_cell_prepared('SELECT id
FROM graph_tree_items
WHERE parent = ?',
array($leaf['id']));
if (!empty($first_child)) {
api_tree_sort_branch($first_child, $tree_id);
if (leaves_exist($leaf['id'], $tree_id)) {
sort_recursive($first_child, $tree_id);
}
}
}
}
}
}
function leaves_exist($parent, $tree_id) {
return db_fetch_assoc_prepared('SELECT COUNT(*)
FROM graph_tree_items
WHERE graph_tree_id = ?
AND parent = ?
AND local_graph_id = 0
AND host_id = 0',
array($tree_id, $parent));
}
function form_actions() {
global $actions;
/* ================= input validation ================= */
get_filter_request_var('drp_action', FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^([a-zA-Z0-9_]+)$/')));
/* ==================================================== */
/* if we are to save this form, instead of display it */
if (isset_request_var('selected_items')) {
$selected_items = sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items'));
if ($selected_items != false) {
if (get_nfilter_request_var('drp_action') == '1') { // delete
db_execute('DELETE FROM graph_tree WHERE ' . array_to_sql_or($selected_items, 'id'));
db_execute('DELETE FROM graph_tree_items WHERE ' . array_to_sql_or($selected_items, 'graph_tree_id'));
/**
* Save the last time a tree or branch was created/updated
* for Caching.
*/
set_config_option('time_last_change_tree', time());
set_config_option('time_last_change_branch', time());
} elseif (get_nfilter_request_var('drp_action') == '2') { // publish
db_execute("UPDATE graph_tree
SET enabled='on',
last_modified=NOW(),
modified_by=" . $_SESSION[SESS_USER_ID] . '
WHERE ' . array_to_sql_or($selected_items, 'id'));
/**
* Save the last time a tree or branch was created/updated
* for Caching.
*/
set_config_option('time_last_change_tree', time());
set_config_option('time_last_change_branch', time());
} elseif (get_nfilter_request_var('drp_action') == '3') { // un-publish
db_execute("UPDATE graph_tree
SET enabled='',
last_modified=NOW(),
modified_by=" . $_SESSION[SESS_USER_ID] . '
WHERE ' . array_to_sql_or($selected_items, 'id'));
/**
* Save the last time a tree or branch was created/updated
* for Caching.
*/
set_config_option('time_last_change_tree', time());
set_config_option('time_last_change_branch', time());
} elseif (get_nfilter_request_var('drp_action') == '4') { // un-lock
db_execute('UPDATE graph_tree
SET locked=0,
last_modified=NOW(),
modified_by=' . $_SESSION[SESS_USER_ID] . '
WHERE ' . array_to_sql_or($selected_items, 'id'));
}
}
header('Location: tree.php');
exit;
} else {
$ilist = '';
$iarray = array();
/* loop through each of the selected items */
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
/* ================= input validation ================= */
input_validate_input_number($matches[1], 'chk[1]');
/* ==================================================== */
$ilist .= '<li>' . html_escape(db_fetch_cell_prepared('SELECT name FROM graph_tree WHERE id = ?', array($matches[1]))) . '</li>';
$iarray[] = $matches[1];
}
}
$form_data = array(
'general' => array(
'page' => 'tree.php',
'actions' => $actions,
'optvar' => 'drp_action',
'item_array' => $iarray,
'item_list' => $ilist
),
'options' => array(
1 => array(
'smessage' => __('Click \'Continue\' to Delete the following Tree.'),
'pmessage' => __('Click \'Continue\' to Delete following Trees.'),
'scont' => __('Delete Tree'),
'pcont' => __('Delete Trees')
),
2 => array(
'smessage' => __('Click \'Continue\' to Publish the following Tree.'),
'pmessage' => __('Click \'Continue\' to Publish following Trees.'),
'scont' => __('Publish Tree'),
'pcont' => __('Publish Trees')
),
3 => array(
'smessage' => __('Click \'Continue\' to Un-Publish the following Tree.'),
'pmessage' => __('Click \'Continue\' to Un-Publish following Trees.'),
'scont' => __('Un-Publish Tree'),
'pcont' => __('Un-Publish Trees')
),
4 => array(
'smessage' => __('Click \'Continue\' to Un-Lock the following Tree.'),
'pmessage' => __('Click \'Continue\' to Un-Lock following Trees.'),
'scont' => __('Un-Lock Tree'),
'pcont' => __('Un-Lock Trees')
),
)
);
form_continue_confirmation($form_data);
}
}
function tree_edit($partial = false) {
global $fields_tree_edit;
/* ================= input validation ================= */
get_filter_request_var('id');
get_filter_request_var('type');
/* ==================================================== */
load_current_session_value('type', 'sess_tree_edit_type', '0');
if (!isempty_request_var('id')) {
$tree = db_fetch_row_prepared('SELECT *
FROM graph_tree
WHERE id = ?',
array(get_request_var('id')));
$header_label = __esc('Trees [edit: %s]', $tree['name']);
// Reset the cookie state if tree id has changed
if (isset($_SESSION['sess_tree_id']) && $_SESSION['sess_tree_id'] != get_request_var('id')) {
$select_first = true;
} else {
$select_first = false;
}
$_SESSION['sess_tree_id'] = get_request_var('id');
} else {
$tree = array();
$header_label = __('Trees [new]');
}
print '<div id="tree_edit_container">';
form_start('tree.php', 'tree_edit');
// Remove inherit from the main tree option
unset($fields_tree_edit['sort_type']['array'][0]);
html_start_box($header_label, '100%', true, '3', 'center', '');
if (!cacti_sizeof($tree)) {
unset($fields_tree_edit['enabled']);
}
draw_edit_form(
array(
'config' => array('no_form_tag' => true),
'fields' => inject_form_variables($fields_tree_edit, (isset($tree) ? $tree : array()))
)
);
html_end_box(true, true);
$lockdiv = '';
$editable = true;
if (isset($tree['locked']) && $tree['locked'] == 0) {
$lockdiv = "<div style='padding:5px 5px 5px 0px'><table><tr><td><input type='button' class='ui-button ui-corner-all ui-widget' id='lock' value='" . __esc('Edit Tree') . "'></td><td style='font-weight:bold;'>" . __('To Edit this tree, you must first lock it by pressing the Edit Tree button.') . "</td></tr></table></div>\n";
$editable = false;
} elseif (isset($tree['locked']) && $tree['locked'] == 1) {
$lockdiv = "<div style='padding:5px 5px 5px 0px'><table><tr><td><input type='button' class='ui-button ui-corner-all ui-widget' id='unlock' value='" . __esc('Finish Editing Tree') . "'></td><td><input type='button' class='ui-button ui-corner-all ui-widget' id='addbranch' value='" . __esc('Add Root Branch') . "' onClick='createNode()'></td><td style='font-weight:bold;'>" . __('This tree has been locked for Editing on %1$s by %2$s.', $tree['locked_date'], get_username($tree['modified_by']));
if ($tree['modified_by'] == $_SESSION[SESS_USER_ID]) {
$lockdiv .= '</td></tr></table></div>';
} else {
$editable = false;
$lockdiv .= __('To edit the tree, you must first unlock it and then lock it as yourself') . '</td></tr></table></div>';
}
} else {
$tree['id'] = 0;
}
if ($editable) {
form_save_button('tree.php', 'return');
}
if (!isempty_request_var('id')) {
print $lockdiv;
}
print '</div>';
if ($partial) {
return;
}
if (!isempty_request_var('id')) {
print "<table class='treeTable' style='width:100%;'>\n";
print "<tr class='even' id='tree_filter'>\n";
print "<td colspan='4'>";
print '<table><tr><td>' . __('Display') . '</td>';
print "<td>\n";
print "<select id='element'>\n";
print "<option id='0'>" . __('All') . '</option>';
print "<option id='1'>" . __('Sites') . '</option>';
print "<option id='2'>" . __('Devices') . '</option>';
print "<option id='3'>" . __('Graphs') . '</option>';
print '</select></td></tr></table></td></tr>';
print "<tr class='tableRow'><td class='treeArea'>\n";
html_start_box(__('Tree Items'), '100%', '', '3', 'center', '');
print "<tr class='tableRow'><td style='padding:7px;'><div id='ctree'></div></td></tr>\n";
html_end_box();
print "</td><td class='treeItemsArea treeItemsAreaSite'>\n";
html_start_box(__('Available Sites'), '100%', '', '3', 'center', '');
?>
<tr class='even noprint'>
<td>
<form id='form_tree_sites' action='tree.php'>
<table class='filterTable'>
<tr>
<td>
<?php print __('Search'); ?>
</td>
<td>
<input type='text' class='ui-state-default ui-corner-all' id='sfilter' name='sfilter' size='25' value='<?php print html_escape_request_var('sfilter');?>'>
</td>
</tr>
</table>
</form>
</td>
</tr>
<?php
html_end_box(false);
$display_text = array(__('Site Name'));
html_start_box('', '100%', '', '3', 'center', '');
html_header($display_text);
print "<tr class='tableRow'><td style='padding:7px;'><div id='sites' style='display:none;'>\n";
display_sites();
print "</div></td></tr>\n";
html_end_box();
print "</td><td class='treeItemsArea treeItemsAreaDevice'>\n";
html_start_box(__('Available Devices'), '100%', '', '3', 'center', '');
?>
<tr class='even noprint'>
<td>
<form id='form_tree_devices' action='tree.php'>
<table class='filterTable'>
<tr>
<td>
<?php print __('Search'); ?>
</td>
<td>
<input type='text' class='ui-state-default ui-corner-all' id='hfilter' name='hfilter' size='25' value='<?php print html_escape_request_var('hfilter');?>'>
</td>
</tr>
</table>
</form>
</td>
</tr>
<?php
html_end_box(false);
$display_text = array(__('Device Description'));
html_start_box('', '100%', '', '3', 'center', '');
html_header($display_text);
print "<tr class='tableRow'><td style='padding:7px;'><div id='hosts' style='display:none;'>\n";
display_hosts();
print "</div></td></tr>\n";
html_end_box();
print "</td><td class='treeItemsArea treeItemsAreaGraph'>\n";
html_start_box(__('Available Graphs'), '100%', '', '3', 'center', '');
?>
<tr class='even noprint'>
<td>
<form id='form_tree_graphs' action='tree.php'>
<table class='filterTable'>
<tr>
<td>
<?php print __('Search'); ?>
</td>
<td>
<input type='text' class='ui-state-default ui-corner-all' id='gfilter' name='gfilter' size='25' value='<?php print html_escape_request_var('gfilter');?>'>
</td>
</tr>
</table>
</form>
</td>
</tr>
<?php
html_end_box(false);
$display_text = array(__('Graph Name'));
html_start_box('', '100%', '', '3', 'center', '');
html_header($display_text);
print "<tr class='tableRow'><td style='padding:7px;'><div id='graphs' style='display:none;'>\n";
display_graphs();
print "</div></td></tr>\n";
html_end_box();
print "</td></tr></table>\n";
?>
<script type='text/javascript'>
<?php
if ($select_first) {
print "var reset=true;\n";
} else {
print "var reset=false;\n";
}
?>
var hostSortInfo = {};
var branchSortInfo = {};
var selectedItem = {};
if ($('#lock').length) {
var editable = false;
} else {
var editable = true;
}
function createNode() {
var ref = $('#ctree').jstree(true);
sel = ref.create_node('#', '<?php print __esc('New Node');?>', '0');
if (sel) {
ref.edit(sel);
}
};
function loadTreeEdit(url) {
var myMagic = csrfMagicToken;
$.post(url, { __csrf_magic: csrfMagicToken })
.done(function(data) {
$('#tree_edit_container').replaceWith(data);
$('#tree_edit').append('<input type="hidden" name="__csrf_magic" value="'+myMagic+'">');
initializeTreeEdit();
applySkin();
})
.fail(function(data) {
getPresentHTTPError(data);
});
}
function getGraphData() {
$.get('tree.php?action=graphs&filter='+$('#gfilter').val()
+ '&site_id=' + (selectedItem.site_id ? selectedItem.site_id:'')
+ '&host_id=' + (selectedItem.host_id ? selectedItem.host_id:''))
.done(function(data) {
$('#graphs').jstree('destroy');
$('#graphs').html(data);
dragable('graphs');
})
.fail(function(data) {
getPresentHTTPError(data);
});
}
function getHostData() {
$.get('tree.php?action=hosts&filter='+$('#hfilter').val()
+ '&site_id=' + (selectedItem.site_id ? selectedItem.site_id:''))
.done(function(data) {
$('#hosts').jstree('destroy');
$('#hosts').html(data);
dragable('hosts');
dragable('graphs');
})
.fail(function(data) {
getPresentHTTPError(data);
});
}
function getSiteData() {
$.get('tree.php?action=sites&filter='+$('#sfilter').val(), function(data) {
$('#sites').jstree('destroy');
$('#sites').html(data);
dragable('sites');
dragable('hosts');
dragable('graphs');
enableKeyups();
});
}
function setHostSortIcon(nodeid) {
if (hostSortInfo[nodeid]) {
// Already set
} else {
$.get('tree.php?action=get_host_sort&nodeid='+nodeid)
.done(function(data) {
hostSortInfo[nodeid] = data;
})
.fail(function(data) {
getPresentHTTPError(data);
});
}
}
function setBranchSortIcon(nodeid) {
if (branchSortInfo[nodeid]) {
// Already set
} else {
$.get('tree.php?action=get_branch_sort&nodeid='+nodeid)
.done(function(data) {
branchSortInfo[nodeid] = data;
})
.fail(function(data) {
getPresentHTTPError(data);
});
}
}
function getHostSortIcon(type, nodeid) {
if (hostSortInfo[nodeid] == type) {
return 'fa fa-check';
} else {
return 'false';
}
}
function getBranchSortIcon(type, nodeid) {
if (branchSortInfo[nodeid] == type) {
return 'fa fa-check';
} else {
return 'false';
}
}
function setBranchSortOrder(type, nodeid) {
$.get('tree.php?action=set_branch_sort&type='+type+'&nodeid='+nodeid)
.done(function(data) {
branchSortInfo[nodeid] = type;
})
.fail(function(data) {
getPresentHTTPError(data);
});
}
function setHostSortOrder(type, nodeid) {
$.get('tree.php?action=set_host_sort&type='+type+'&nodeid='+nodeid)
.done(function(data) {
hostSortInfo[nodeid] = type;
})
.fail(function(data) {
getPresentHTTPError(data);
});
}
function initializeTreeEdit() {
$('#lock').click(function() {
strURL = 'tree.php?action=lock&id=<?php print $tree['id'];?>';
loadTreeEdit(strURL);
$('#sfilter, #hfilter, #gfilter').each(function() {
$(this).prop('disabled', false);
$(this).removeClass('ui-state-disabled');
if ($(this).selectmenu('instance') !== undefined) {
$(this).selectmenu('enable');
}
});
editable = true;
reset = false;
drawTree();
dragable('graphs');
dragable('hosts');
dragable('sites');
enableKeyups();
});
$('#unlock').click(function() {
strURL = 'tree.php?action=unlock&id=<?php print $tree['id'];?>';
loadTreeEdit(strURL);
$('#sfilter, #hfilter, #gfilter').each(function() {
$(this).prop('disabled', true);
$(this).addClass('ui-state-disabled');
if ($(this).selectmenu('instance') !== undefined) {
$(this).selectmenu('disable');
}
});
editable = false;
reset = false;
drawTree();
dragable('graphs');
dragable('hosts');
dragable('sites');
enableKeyups();
});
if ($('#lock').length) {
editable = false;
$('select, input').not('#lock, #element').each(function() {
$(this).prop('disabled', true);
$(this).addClass('ui-state-disabled');
if ($(this).selectmenu('instance') !== undefined) {
$(this).selectmenu('disable');
}
});
} else {
editable = true;
}
}
function drawTree() {
if (editable) {
var plugins = [ 'state', 'wholerow', 'contextmenu', 'dnd', 'types' ];
} else {
var plugins = [ 'state', 'wholerow', 'types' ];
}
if ($('#ctree').jstree('instance')) {
$('#ctree').jstree('destroy');
}
$("#ctree").jstree({
'types' : {
'site' : {
icon : 'images/site.png',
max_children : 0
},
'device' : {
icon : 'images/server.png',
max_children : 0
},
'graph' : {
icon : 'images/server_chart_curve.png',
max_children : 0
}
},
'contextmenu' : {
'items': function(node) {
if (node.id.search('tgraph') > 0) {
var dataType = 'graph';
} else if (node.id.search('thost') > 0) {
var dataType = 'host';
} else if (node.id.search('tsite') > 0) {
var dataType = 'site';
}else {
var dataType = 'branch';
}
if (dataType == 'graph') {
return graphContext(node.id);
} else if (dataType == 'host') {
return hostContext(node.id);
} else if (dataType == 'site') {
return siteContext(node.id);
} else {
return branchContext(node.id);
}
}
},
'core' : {
'data' : {
'url' : 'tree.php?action=get_node&tree_id='+$('#id').val(),
'data' : function(node) {
return { 'id' : node.id }
}
},
'animation' : 0,
'check_callback' : true,
'force_text' : true
},
'themes' : {
'name' : 'default',
'responsive' : true,
'url' : true,
'dots' : false
},
'state': { 'key': 'tree_<?php print get_request_var('id');?>' },
'plugins' : plugins
})
.on('ready.jstree', function(e, data) {
if (reset == true) {
$('#ctree').jstree('clear_state');
}
if (!editable) {
$("#ctree").children().on('contextmenu', function(event) {
return false;
});
}
})
.on('delete_node.jstree', function (e, data) {
$.get('?action=delete_node', { 'id' : data.node.id, 'tree_id' : $('#id').val() })
.always(function() {
var st = data.instance.get_state();
data.instance.load_node(data.instance.get_parent(data.node.id), function () { this.set_state(st); });
});
})
.on('hover_node.jstree', function (e, data) {
if (data.node.id.search('thost') >= 0) {
setHostSortIcon(data.node.id);
} else if (data.node.id.search('thost') < 0 && data.node.id.search('tgraph') < 0 && data.node.id.search('tsite')) {
setBranchSortIcon(data.node.id);
}
})
.on('create_node.jstree', function (e, data) {
$.get('?action=create_node', { 'id' : data.node.parent, 'tree_id' : $('#id').val(), 'position' : data.position, 'text' : data.node.text })
.done(function (d) {
data.instance.set_id(data.node, d.id);
data.instance.set_text(data.node, d.text);
data.instance.edit(data.node);
if (d.text != '<?php print __esc('New Node');?>') {
$('.jstree').jstree(true).refresh();
}
})
.fail(function () {
var st = data.instance.get_state();
data.instance.load_node(data.instance.get_parent(data.node.id), function () { this.set_state(st); });
});
})
.on('rename_node.jstree', function (e, data) {
$.get('?action=rename_node', { 'id' : data.node.id, 'tree_id' : $('#id').val(), 'text' : data.text })
.done(function (d) {
if (d.result == 'false') {
data.instance.set_text(data.node, d.text);
data.instance.edit(data.node);
} else {
var st = data.instance.get_state();
data.instance.load_node(data.instance.get_parent(data.node.id), function () { this.set_state(st); });
}
})
.fail(function(data) {
getPresentHTTPError(data);
});
})
.on('move_node.jstree', function (e, data) {
$.get('?action=move_node', { 'id' : data.node.id, 'tree_id' : $('#id').val(), 'parent' : data.parent, 'position' : data.position })
.always(function () {
var st = data.instance.get_state();
data.instance.load_node(data.instance.get_parent(data.node.id), function () { this.set_state(st); });
});
})
.on('copy_node.jstree', function (e, data) {
oid = data.original.id;
if (oid.search('thost') >= 0) {
$('#hosts').jstree().deselect_all();
} else if (oid.search('tsite') >= 0) {
$('#sites').jstree().deselect_all();
} else {
$('#graphs').jstree().deselect_all();
}
$.get('?action=copy_node', { 'id' : data.original.id, 'tree_id' : $('#id').val(), 'parent' : data.parent, 'position' : data.position })
.always(function () {
var st = data.instance.get_state();
data.instance.load_node(data.instance.get_parent(data.node.id), function () { this.set_state(st); });
});
});
}
function enableKeyups() {
$('#gfilter').delayKeyup(getGraphData);
$('#hfilter').delayKeyup(getHostData);
$('#sfilter').delayKeyup(getSiteData);
}
function resizer() {
if ($('#ctree').length) {
var wheight = $(window).height();
var cTop = $('#ctree').parent().offset().top;
var sTop = $('#sites').parent().offset().top;
var height = wheight - cTop - 10;
var sheight = wheight - sTop - 10;
$('#ctree').css('height', height).css('overflow','auto');
$('#hosts').css('height', sheight).css('overflow','auto');
$('#sites').css('height', sheight).css('overflow','auto');
$('#graphs').css('height', sheight).css('overflow','auto');
switchDisplay();
}
}
function switchDisplay() {
var selected = $('#element').prop('selectedIndex');
var windowWidth = parseInt($(window).outerWidth());
var clientWidth = parseInt($(document).width());
if (selected == 0) {
if (clientWidth > windowWidth) {
$('#element').prop('selectedIndex', 1);
if ($('#element').selectmenu('instance')) {
$('#element').selectmenu('refresh');
}
selected = $('#element').prop('selectedIndex');
}
}
switch(selected) {
case 0:
$('.treeItemsAreaSite').show();
$('.treeItemsAreaDevice').show();
$('.treeItemsAreaGraph').show();
break;
case 1:
$('.treeItemsAreaSite').show();
$('.treeItemsAreaDevice').hide();
$('.treeItemsAreaGraph').hide();
break;
case 2:
$('.treeItemsAreaSite').hide();
$('.treeItemsAreaDevice').show();
$('.treeItemsAreaGraph').hide();
break;
case 3:
$('.treeItemsAreaSite').hide();
$('.treeItemsAreaDevice').hide();
$('.treeItemsAreaGraph').show();
break;
}
}
graphsDropSet = '';
hostsDropSet = '';
sitesDropSet = '';
$(function() {
drawTree();
if ($('#unlock').length) {
enableKeyups();
$('select, input').each(function() {
$(this).prop('disabled', false);
});
} else {
$('select, input').not('#lock, #element').each(function() {
$(this).prop('disabled', true);
$(this).addClass('ui-state-disabled');
if ($(this).selectmenu('instance') !== undefined) {
$(this).selectmenu('disable');
}
});
}
$('form').unbind().submit(function(event) {
event.preventDefault();
if ($(this).attr('id') == 'tree_edit') {
var options = {
url: 'tree.php',
}
var data = {
action: 'save',
name: $('#name').val(),
sort_type: $('#sort_type').val(),
enabled: $('#enabled').is(':checked'),
id: $('#id').val(),
save_component_tree: 1,
__csrf_magic: csrfMagicToken
}
postUrl(options, data);
}
});
var height = parseInt($(window).height()-$('#ctree').offset().top-10)+'px';
var sheight = parseInt($(window).height()-$('#sites').offset().top-10)+'px';
var hheight = parseInt($(window).height()-$('#hosts').offset().top-10)+'px';
var gheight = parseInt($(window).height()-$('#graphs').offset().top-10)+'px';
$('#element').change(function() {
resizer();
});
$(window).resize(function() {
resizer();
});
initializeTreeEdit();
$('#ctree').css('height', height).css('overflow','auto');;
dragable('graphs');
dragable('hosts');
dragable('sites');
});
function dragable(element) {
var id = '#'+element;
var divdata = '';
if (editable) {
var plugins = [ 'wholerow', 'dnd', 'types' ];
} else {
var plugins = [ 'wholerow', 'types' ];
}
// We have to reload the various trees due
// to plugins being imutable after load
// so reconstruct the tree from the current elements
// destroy the tree, and repaint to elements
// as they would have been.
if ($(id).hasClass('jstree')) {
$(id).find('.jstree-node').each(function() {
var text = DOMPurify.sanitize($(this).find('.jstree-anchor').text());
var id = $(this).attr('id');
var jsdata = $(this).attr('data-jstree');
divdata += "<ul><li id='" + id + "' data-jstree='" + jsdata + "'>" + text + '</li></ul>';
});
$(id).jstree('destroy').html(divdata);
}
$(id)
.jstree({
'types' : {
'site' : {
icon : 'images/site.png',
valid_children: 'none',
max_children : 0
},
'device' : {
icon : 'images/server.png',
valid_children: 'none',
max_children : 0
},
'graph' : {
icon : 'images/server_chart_curve.png',
valid_children: 'none',
max_children : 0
}
},
'core' : {
'animation' : 0,
'check_callback' : function(operation, node, node_parent, node_position, more) {
return false; // not dragging onto self
}
},
'dnd' : {
'always_copy' : true,
'check_while_dragging': true
},
'themes' : { 'stripes' : true },
'plugins' : plugins
})
.on('ready.jstree', function(e, data) {
if (reset == true) {
$('#ctree').jstree('clear_state');
}
});
if (editable) {
$(id)
.on('select_node.jstree', function(e, data) {
if (element == 'graphs') {
graphsDropSet = data;
} else {
hostsDropSet = data;
}
})
.on('activate_node.jstree', function(e, data) {
if (element == 'sites') {
var sites = [];
$('#sites').find('.jstree-clicked').each(function() {
sites.push(this.id.split(':')[1].replace('_anchor', ''));
});
selectedItem.site_id = sites;
selectedItem.host_id = '';
getHostData();
getGraphData();
} else if (element == 'hosts'){
var hosts = [];
$('#hosts').find('.jstree-clicked').each(function() {
hosts.push(this.id.split(':')[1].replace('_anchor', ''));
});
selectedItem.host_id = hosts;
getGraphData();
}
})
.on('deselect_node.jstree', function(e, data) {
if (element == 'graphs') {
graphsDropSet = data;
} else {
hostsDropSet = data;
}
});
}
$(id).find('.jstree-ocl').hide();
$(id).children().bind('contextmenu', function(event) {
return false;
});
$(id).show();
}
function branchContext(nodeid) {
return {
'create' : {
'separator_before' : false,
'separator_after' : true,
'icon' : 'fa fa-folder',
'_disabled' : false,
'label' : '<?php print __esc('Create');?>',
'action' : function (data) {
var inst = $.jstree.reference(data.reference);
var obj = inst.get_node(data.reference);
inst.create_node(obj, {}, 'last', function (new_node) {
setTimeout(function () { inst.edit(new_node); },0);
});
}
},
'rename' : {
'separator_before' : false,
'separator_after' : false,
'icon' : 'fa fa-pencil-alt',
'_disabled' : false,
'label' : '<?php print __esc('Rename');?>',
'action' : function (data) {
var inst = $.jstree.reference(data.reference);
var obj = inst.get_node(data.reference);
inst.edit(obj);
}
},
'remove' : {
'separator_before' : false,
'icon' : 'fa fa-times',
'separator_after' : false,
'_disabled' : false,
'label' : '<?php print __esc('Delete');?>',
'action' : function (data) {
var inst = $.jstree.reference(data.reference);
var obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.delete_node(inst.get_selected());
} else {
inst.delete_node(obj);
}
}
},
'bst' : {
'separator_before' : true,
'icon' : 'fa fa-sort',
'separator_after' : false,
'label' : '<?php print __esc('Branch Sorting');?>',
'action' : false,
'submenu' : {
'inherit' : {
'separator_before' : false,
'separator_after' : false,
'icon' : getBranchSortIcon('inherit', nodeid),
'label' : '<?php print __esc('Inherit');?>',
'action' : function (data) {
setBranchSortOrder('inherit', nodeid);
var inst = $.jstree.reference(data.reference);
var st = inst.get_state();
var obj = inst.get_node();
inst.refresh(obj);
inst.load_node(nodeid, function() { this.set_state(st); });
}
},
'manual' : {
'separator_before' : false,
'separator_after' : false,
'icon' : getBranchSortIcon('manual', nodeid),
'label' : '<?php print __esc('Manual');?>',
'action' : function (data) {
setBranchSortOrder('manual', nodeid);
var inst = $.jstree.reference(data.reference);
var st = inst.get_state();
var obj = inst.get_node();
inst.refresh(obj);
inst.load_node(nodeid, function() { this.set_state(st); });
}
},
'alpha' : {
'separator_before' : false,
'icon' : getBranchSortIcon('alpha', nodeid),
'separator_after' : false,
'label' : '<?php print __esc('Alphabetic');?>',
'action' : function (data) {
setBranchSortOrder('alpha', nodeid);
var inst = $.jstree.reference(data.reference);
var st = inst.get_state();
var obj = inst.get_node();
inst.refresh(obj);
inst.load_node(nodeid, function() { this.set_state(st); });
}
},
'natural' : {
'separator_before' : false,
'icon' : getBranchSortIcon('natural', nodeid),
'separator_after' : false,
'label' : '<?php print __esc('Natural');?>',
'action' : function (data) {
setBranchSortOrder('natural', nodeid);
var inst = $.jstree.reference(data.reference);
var st = inst.get_state();
var obj = inst.get_node();
inst.refresh(obj);
inst.load_node(nodeid, function () { this.set_state(st); });
}
},
'numeric' : {
'separator_before' : false,
'icon' : getBranchSortIcon('numeric', nodeid),
'separator_after' : false,
'label' : '<?php print __esc('Numeric');?>',
'action' : function (data) {
setBranchSortOrder('numeric', nodeid);
var inst = $.jstree.reference(data.reference);
var st = inst.get_state();
var obj = inst.get_node();
inst.refresh(obj);
inst.load_node(nodeid, function () { this.set_state(st); });
}
}
}
},
'ccp' : {
'separator_before' : true,
'icon' : 'fa fa-edit',
'separator_after' : false,
'label' : '<?php print __esc('Edit');?>',
'action' : false,
'submenu' : {
'cut' : {
'separator_before' : false,
'separator_after' : false,
'icon' : 'fa fa-cut',
'label' : '<?php print __esc('Cut');?>',
'action' : function (data) {
var inst = $.jstree.reference(data.reference);
var obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.cut(inst.get_selected());
} else {
inst.cut(obj);
}
}
},
'copy' : {
'separator_before' : false,
'icon' : 'fa fa-copy',
'separator_after' : false,
'label' : '<?php print __esc('Copy');?>',
'action' : function (data) {
var inst = $.jstree.reference(data.reference);
var obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.copy(inst.get_selected());
} else {
inst.copy(obj);
}
}
},
'paste' : {
'separator_before' : false,
'icon' : 'fa fa-clipboard',
'_disabled' : function (data) {
return !$.jstree.reference(data.reference).can_paste();
},
'separator_after' : false,
'label' : '<?php print __esc('Paste');?>',
'action' : function (data) {
var inst = $.jstree.reference(data.reference);
var obj = inst.get_node(data.reference);
inst.paste(obj);
}
}
}
}
};
}
function graphContext(nodeid) {
return {
'remove' : {
'separator_before' : false,
'icon' : 'fa fa-times',
'separator_after' : false,
'_disabled' : false, //(this.check('delete_node', data.reference, this.get_parent(data.reference), '')),
'label' : '<?php print __esc('Delete');?>',
'action' : function (data) {
var inst = $.jstree.reference(data.reference);
var obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.delete_node(inst.get_selected());
} else {
inst.delete_node(obj);
}
}
},
'ccp' : {
'separator_before' : true,
'icon' : 'fa fa-edit',
'separator_after' : false,
'label' : '<?php print __esc('Edit');?>',
'action' : false,
'submenu' : {
'cut' : {
'separator_before' : false,
'separator_after' : false,
'icon' : 'fa fa-cut',
'label' : '<?php print __esc('Cut');?>',
'action' : function (data) {
var inst = $.jstree.reference(data.reference);
var obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.cut(inst.get_selected());
} else {
inst.cut(obj);
}
}
},
'copy' : {
'separator_before' : false,
'icon' : 'fa fa-copy',
'separator_after' : false,
'label' : '<?php print __esc('Copy');?>',
'action' : function (data) {
var inst = $.jstree.reference(data.reference);
var obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.copy(inst.get_selected());
} else {
inst.copy(obj);
}
}
}
}
}
};
}
function siteContext(nodeid) {
return {
'remove' : {
'separator_before' : false,
'icon' : 'fa fa-times',
'separator_after' : false,
'_disabled' : false,
'label' : '<?php print __esc('Delete');?>',
'action' : function (data) {
var inst = $.jstree.reference(data.reference);
var obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.delete_node(inst.get_selected());
} else {
inst.delete_node(obj);
}
}
}
};
}
function hostContext(nodeid) {
return {
'remove' : {
'separator_before' : false,
'icon' : 'fa fa-times',
'separator_after' : false,
'_disabled' : false,
'label' : '<?php print __esc('Delete');?>',
'action' : function (data) {
var inst = $.jstree.reference(data.reference);
var obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.delete_node(inst.get_selected());
} else {
inst.delete_node(obj);
}
}
},
'hso' : {
'separator_before' : true,
'separator_after' : false,
'icon' : 'fa fa-sort',
'label' : '<?php print __esc('Sorting Type');?>',
'action' : false,
'submenu' : {
'hsgt' : {
'separator_before' : false,
'icon' : getHostSortIcon('hsgt', nodeid),
'separator_after' : false,
'label' : '<?php print __esc('Graph Template');?>',
'action' : function (data) {
setHostSortOrder('hsgt', nodeid);
}
},
'hsdq' : {
'separator_before' : false,
'icon' : getHostSortIcon('hsdq', nodeid),
'separator_after' : false,
'label' : '<?php print __esc('Data Query Index');?>',
'action' : function (data) {
setHostSortOrder('hsdq', nodeid);
}
}
}
},
'ccp' : {
'separator_before' : true,
'icon' : 'fa fa-edit',
'separator_after' : false,
'label' : '<?php print __esc('Edit');?>',
'action' : false,
'submenu' : {
'cut' : {
'separator_before' : false,
'separator_after' : false,
'icon' : 'fa fa-cut',
'label' : '<?php print __esc('Cut');?>',
'action' : function (data) {
var inst = $.jstree.reference(data.reference),
obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.cut(inst.get_selected());
} else {
inst.cut(obj);
}
}
},
'copy' : {
'separator_before' : false,
'icon' : 'fa fa-copy',
'separator_after' : false,
'label' : '<?php print __esc('Copy');?>',
'action' : function (data) {
var inst = $.jstree.reference(data.reference),
obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.copy(inst.get_selected());
} else {
inst.copy(obj);
}
}
}
}
}
};
}
</script>
<?php
}
}
function display_sites() {
if (get_nfilter_request_var('filter') != '') {
$sql_where = 'WHERE
name LIKE ' . db_qstr('%' . get_nfilter_request_var('filter') . '%') . '
OR city LIKE ' . db_qstr('%' . get_nfilter_request_var('filter') . '%') . '
OR state LIKE ' . db_qstr('%' . get_nfilter_request_var('filter') . '%') . '
OR country LIKE ' . db_qstr('%' . get_nfilter_request_var('filter') . '%');
} else {
$sql_where = '';
}
$sites = db_fetch_assoc("SELECT * FROM sites $sql_where");
if (cacti_sizeof($sites)) {
foreach ($sites as $s) {
print "<ul><li id='tsite:" . $s['id'] . "' data-jstree='{ \"type\" : \"site\"}'>" . html_escape($s['name']) . "</li></ul>\n";
}
}
}
function display_hosts() {
$sql_where = '';
$site_ids = get_filter_request_var('site_id', FILTER_VALIDATE_IS_NUMERIC_LIST);
if (get_nfilter_request_var('filter') != '') {
$sql_where .= 'h.hostname LIKE ' . db_qstr('%' . get_nfilter_request_var('filter') . '%') . '
OR h.description LIKE ' . db_qstr('%' . get_nfilter_request_var('filter') . '%');
}
if ($site_ids != '') {
$sql_where .= ($sql_where != '' ? ' AND ':'') . 'h.site_id IN( ' . $site_ids . ')';
}
$hosts = get_allowed_devices($sql_where, 'description', read_config_option('autocomplete_rows'));
if (cacti_sizeof($hosts)) {
foreach ($hosts as $h) {
print "<ul><li id='thost:" . $h['id'] . "' data-jstree='{ \"type\" : \"device\"}'>" . html_escape($h['description']) . ' (' . html_escape($h['hostname']) . ')' . "</li></ul>\n";
}
}
}
function display_graphs() {
$sql_where = '';
$site_ids = get_filter_request_var('site_id', FILTER_VALIDATE_IS_NUMERIC_LIST);
$host_ids = get_filter_request_var('host_id', FILTER_VALIDATE_IS_NUMERIC_LIST);
if (get_nfilter_request_var('filter') != '') {
$sql_where .= 'WHERE (
title_cache LIKE ' . db_qstr('%' . get_nfilter_request_var('filter') . '%') . '
OR gt.name LIKE ' . db_qstr('%' . get_nfilter_request_var('filter') . '%') . ')
AND local_graph_id > 0';
} else {
$sql_where .= 'WHERE local_graph_id > 0';
}
if ($site_ids != '') {
$sql_where .= ($sql_where != '' ? ' AND ': 'WHERE ') . 'h.site_id IN( ' . $site_ids . ')';
}
if ($host_ids != '') {
$sql_where .= ($sql_where != '' ? ' AND ':'WHERE ') . 'gl.host_id IN( ' . $host_ids . ')';
}
$graphs = db_fetch_assoc("SELECT
gtg.local_graph_id AS id,
gtg.title_cache AS title,
gt.name AS template_name
FROM graph_templates_graph AS gtg
LEFT JOIN graph_templates AS gt
ON gt.id=gtg.graph_template_id
LEFT JOIN graph_local AS gl
ON gtg.local_graph_id = gl.id
LEFT JOIN host as h
ON gl.host_id = h.id
$sql_where
ORDER BY title_cache
LIMIT " . read_config_option('autocomplete_rows'));
if (cacti_sizeof($graphs)) {
foreach ($graphs as $g) {
if (is_graph_allowed($g['id'])) {
print "<ul><li id='tgraph:" . $g['id'] . "' data-jstree='{ \"type\": \"graph\" }'>" . html_escape($g['title']) . '</li></ul>';
}
}
}
}
function tree() {
global $actions, $item_rows;
/* ================= input validation and session storage ================= */
$filters = array(
'rows' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'page' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
),
'filter' => array(
'filter' => FILTER_CALLBACK,
'default' => '',
'options' => array('options' => 'sanitize_search_string')
),
'sort_column' => array(
'filter' => FILTER_CALLBACK,
'default' => 'sequence',
'options' => array('options' => 'sanitize_search_string')
),
'sort_direction' => array(
'filter' => FILTER_CALLBACK,
'default' => 'ASC',
'options' => array('options' => 'sanitize_search_string')
)
);
validate_store_request_vars($filters, 'sess_tree');
/* ================= input validation ================= */
/* if the number of rows is -1, set it to the default */
if (get_request_var('rows') == -1) {
$rows = read_config_option('num_rows_table');
} else {
$rows = get_request_var('rows');
}
?>
<script type='text/javascript'>
function applyFilter() {
strURL = 'tree.php?rows=' + $('#rows').val();
strURL += '&filter=' + $('#filter').val();
loadUrl({url:strURL})
}
function clearFilter() {
strURL = 'tree.php?clear=1';
loadUrl({url:strURL})
}
$(function() {
$('#refresh').click(function() {
applyFilter();
});
$('#clear').click(function() {
clearFilter();
});
$('#sorta').click(function() {
loadUrl({url:'tree.php?action=sortasc'})
});
$('#sortd').click(function() {
loadUrl({url:'tree.php?action=sortdesc'})
});
$('#form_tree').submit(function(event) {
event.preventDefault();
applyFilter();
});
});
</script>
<?php
$buttons = array(
array(
'href' => 'tree.php?action=edit',
'callback' => true,
'title' => __esc('Add Tree'),
'class' => 'fa fa-plus'
),
array(
'href' => 'tree.php?action=sortasc',
'callback' => true,
'title' => __esc('Sort Trees Ascending'),
'class' => 'fa fa-sort-alpha-down'
),
array(
'href' => 'tree.php?action=sortdesc',
'callback' => true,
'title' => __esc('Sort Trees Descending'),
'class' => 'fa fa-sort-alpha-up'
)
);
html_start_box(__('Trees'), '100%', '', '3', 'center', $buttons);
?>
<tr class='even noprint'>
<td>
<form id='form_tree' action='tree.php'>
<table class='filterTable'>
<tr>
<td>
<?php print __('Search'); ?>
</td>
<td>
<input type='text' class='ui-state-default ui-corner-all' id='filter' size='25' value='<?php print html_escape_request_var('filter');?>'>
</td>
<td>
<?php print __('Trees'); ?>
</td>
<td>
<select id='rows' onChange='applyFilter()'>
<option value='-1'<?php print(get_request_var('rows') == '-1' ? ' selected>':'>') . __('Default');?></option>
<?php
if (cacti_sizeof($item_rows)) {
foreach ($item_rows as $key => $value) {
print "<option value='" . $key . "'";
if (get_request_var('rows') == $key) {
print ' selected';
} print '>' . html_escape($value) . "</option>\n";
}
}
?>
</select>
</td>
<td>
<span>
<input type='button' class='ui-button ui-corner-all ui-widget' id='refresh' value='<?php print __esc('Go');?>' title='<?php print __esc('Set/Refresh Filters');?>'>
<input type='button' class='ui-button ui-corner-all ui-widget' id='clear' value='<?php print __esc('Clear');?>' title='<?php print __esc('Clear Filters');?>'>
<button type='button' class='ui-button ui-corner-all ui-widget' id='sorta' title='<?php print __esc('Sort Trees Ascending');?>'><i class='fa fa-sort-alpha-down'></i></button>
<button type='button' class='ui-button ui-corner-all ui-widget' id='sortd' title='<?php print __esc('Sort Trees Descending');?>'><i class='fa fa-sort-alpha-up'></i></button>
</span>
</td>
</tr>
</table>
</form>
</td>
</tr>
<?php
html_end_box();
/* form the 'where' clause for our main sql query */
if (get_request_var('filter') != '') {
$sql_where = 'WHERE (
t.name LIKE ' . db_qstr('%' . get_request_var('filter') . '%') . '
OR ti.title LIKE ' . db_qstr('%' . get_request_var('filter') . '%') . ')';
} else {
$sql_where = '';
}
$sql_order = get_order_string();
$sql_limit = ' LIMIT ' . ($rows * (get_request_var('page') - 1)) . ',' . $rows;
$trees = db_fetch_assoc("SELECT t.*,
SUM(CASE WHEN ti.host_id > 0 THEN 1 ELSE 0 END) AS hosts,
SUM(CASE WHEN ti.local_graph_id > 0 THEN 1 ELSE 0 END) AS graphs,
SUM(CASE WHEN ti.local_graph_id = 0 AND host_id = 0 AND site_id = 0 THEN 1 ELSE 0 END) AS branches,
SUM(CASE WHEN ti.site_id > 0 THEN 1 ELSE 0 END) AS sites
FROM graph_tree AS t
LEFT JOIN graph_tree_items AS ti
ON t.id=ti.graph_tree_id
$sql_where
GROUP BY t.id
$sql_order
$sql_limit");
$sql = "SELECT COUNT(DISTINCT(t.id))
FROM graph_tree AS t
LEFT JOIN graph_tree_items AS ti
ON t.id=ti.graph_tree_id
$sql_where";
$total_rows = get_total_row_data($_SESSION[SESS_USER_ID], $sql, array(), 'tree');
$nav = html_nav_bar('tree.php?filter=' . get_request_var('filter'), MAX_DISPLAY_PAGES, get_request_var('page'), $rows, $total_rows, 11, __('Trees'), 'page', 'main');
form_start('tree.php', 'chk');
print $nav;
html_start_box('', '100%', '', '3', 'center', '');
$display_text = array(
'name' => array(
'display' => __('Tree Name'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The name by which this Tree will be referred to as.')
),
'id' => array(
'display' => __('ID'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The internal database ID for this Tree. Useful when performing automation or debugging.')
),
'enabled' => array(
'display' => __('Published'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('Unpublished Trees cannot be viewed from the Graph tab')
),
'locked' => array(
'display' => __('Locked'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('A Tree must be locked in order to be edited.')
),
'user_id' => array(
'display' => __('Owner'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The original author of this Tree.')
),
'sequence' => array(
'display' => __('Order'),
'align' => 'center',
'sort' => 'ASC',
'tip' => __('To change the order of the trees, first sort by this column, press the up or down arrows once they appear.')
),
'last_modified' => array(
'display' => __('Last Edited'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The date that this Tree was last edited.')
),
'modified_by' => array(
'display' => __('Edited By'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The last user to have modified this Tree.')
),
'sites' => array(
'display' => __('Sites'),
'align' => 'right',
'sort' => 'DESC',
'tip' => __('The total number of Site Branches in this Tree.')
),
'branches' => array(
'display' => __('Branches'),
'align' => 'right',
'sort' => 'DESC',
'tip' => __('The total number of Branches in this Tree.')
),
'hosts' => array(
'display' => __('Devices'),
'align' => 'right',
'sort' => 'DESC',
'tip' => __('The total number of individual Devices in this Tree.')
),
'graphs' => array(
'display' => __('Graphs'),
'align' => 'right',
'sort' => 'DESC',
'tip' => __('The total number of individual Graphs in this Tree.')
)
);
html_header_sort_checkbox($display_text, get_request_var('sort_column'), get_request_var('sort_direction'), false);
$i = 1;
if (cacti_sizeof($trees)) {
foreach ($trees as $tree) {
$sequence = '';
if (get_request_var('sort_column') == 'sequence' && get_request_var('sort_direction') == 'ASC') {
if ($i == 1 && cacti_sizeof($trees) == 1) {
$sequence .= '<span class="moveArrowNone"></span>';
$sequence .= '<span class="moveArrowNone"></span>';
} elseif ($i == 1) {
$sequence .= '<a class="pic fa fa-caret-down moveArrow" href="' . htmlspecialchars('tree.php?action=tree_down&id=' . $tree['id']) . '" title="' . __esc('Move Down') . '"></a>';
$sequence .= '<span class="moveArrowNone"></span>';
} elseif ($i == cacti_sizeof($trees)) {
$sequence .= '<span class="moveArrowNone"></span>';
$sequence .= '<a class="pic fa fa-caret-up moveArrow" href="' . html_escape('tree.php?action=tree_up&id=' . $tree['id']) . '" title="' . __esc('Move Up') . '"></a>';
} else {
$sequence .= '<a class="pic fa fa-caret-down moveArrow" href="' . html_escape('tree.php?action=tree_down&id=' . $tree['id']) . '" title="' . __esc('Move Down') . '"></a>';
$sequence .= '<a class="pic fa fa-caret-up moveArrow" href="' . html_escape('tree.php?action=tree_up&id=' . $tree['id']) . '" title="' . __esc('Move Up') . '"></a>';
}
}
form_alternate_row('line' . $tree['id'], true);
form_selectable_cell(filter_value($tree['name'], get_request_var('filter'), 'tree.php?action=edit&id=' . $tree['id']), $tree['id']);
form_selectable_cell($tree['id'], $tree['id'], '', 'right');
form_selectable_cell($tree['enabled'] == 'on' ? __('Yes'):__('No'), $tree['id']);
form_selectable_cell($tree['locked'] == '1' ? __('Yes'):__('No'), $tree['id']);
form_selectable_cell(get_username($tree['user_id']), $tree['id']);
form_selectable_cell($sequence, $tree['id'], '', 'nowrap center');
form_selectable_cell(substr($tree['last_modified'],0,16), $tree['id'], '', 'right');
form_selectable_cell(get_username($tree['modified_by']), $tree['id'], '', 'right');
form_selectable_cell($tree['sites'] > 0 ? number_format_i18n($tree['sites'], '-1'):'-', $tree['id'], '', 'right');
form_selectable_cell($tree['branches'] > 0 ? number_format_i18n($tree['branches'], '-1'):'-', $tree['id'], '', 'right');
form_selectable_cell($tree['hosts'] > 0 ? number_format_i18n($tree['hosts'], '-1'):'-', $tree['id'], '', 'right');
form_selectable_cell($tree['graphs'] > 0 ? number_format_i18n($tree['graphs'], '-1'):'-', $tree['id'], '', 'right');
form_checkbox_cell($tree['name'], $tree['id']);
form_end_row();
$i++;
}
} else {
print "<tr class='tableRow'><td colspan='" . (cacti_sizeof($display_text) + 1) . "'><em>" . __('No Trees Found') . '</em></td></tr>';
}
html_end_box(false);
if (cacti_sizeof($trees)) {
print $nav;
}
/* draw the dropdown containing a list of available actions for this form */
draw_actions_dropdown($actions);
form_end();
if (get_request_var('sort_column') == 'sequence' && get_request_var('sort_direction') == 'ASC') {
?>
<script type='text/javascript'>
$(function() {
$('#tree2_child').attr('id', 'tree_ids');
<?php if (read_config_option('drag_and_drop') == 'on') { ?>
$('#tree_ids').find('tr:first').addClass('nodrag').addClass('nodrop');
$('#tree_ids').tableDnD({
onDrop: function(table, row) {
loadUrl({url:'tree.php?action=ajax_dnd&'+$.tableDnD.serialize()})
}
});
<?php } ?>
});
</script>
<?php
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/Cacti.git
git@gitee.com:mirrors/Cacti.git
mirrors
Cacti
Cacti
develop

搜索帮助