0) { $exists = db_fetch_cell("SELECT id FROM graph_tree_items WHERE parent=" . $pdata['branch'] . " AND host_id=" . $data['host']); if ($exists) { print 'tbranch:' . $exists; return; } }elseif ($data['graph'] > 0) { $exists = db_fetch_cell("SELECT id FROM graph_tree_items WHERE parent=" . $pdata['branch'] . " AND local_graph_id=" . $data['graph']); if ($exists) { print 'tbranch:' . $exists; return; } }else{ $title = db_fetch_cell("SELECT title FROM graph_tree_items WHERE id=" . $data['branch']); } $save = array(); $save['parent'] = $pdata['branch']; $save['position'] = $position; $save['graph_tree_id'] = $tree_id; $save['local_graph_id'] = $data['graph']; $save['host_id'] = $data['host']; $save['host_grouping_type'] = 1; $save['sort_children_type'] = 1; $save['title'] = $title; $id = sql_save($save, 'graph_tree_items'); api_tree_reorder_branch($tree_id, $pdata['branch'], $id, $position); header('Content-Type: application/json; charset=utf-8'); print json_encode(array('id' => 'tbranch:' . $id)); } function api_tree_get_lock($lockname, $timeout = 10) { while (true) { $locked = db_fetch_cell("SELECT GET_LOCK('$lockname', $timeout)"); if ($locked) { return true; }else{ sleep(1); } } } function api_tree_release_lock($lockname) { $unlocked = db_fetch_cell("SELECT RELEASE_LOCK('$lockname')"); } function api_tree_reorder_branch($tree_id, $parent_id, $moved_branch, $position) { // Lock the table api_tree_get_lock('tree-lock', 10); $items = db_fetch_assoc("SELECT id, position FROM graph_tree_items WHERE graph_tree_id=$tree_id AND parent=$parent_id AND id!=$moved_branch ORDER BY position"); $i = 0; if (sizeof($items)) { foreach($items as $item) { if ($i == $position) { $i++; } db_execute("UPDATE graph_tree_items SET position=$i WHERE id=" . $item['id'] . " AND graph_tree_id=$tree_id"); $i++; } } // Un-Lock the table api_tree_release_lock('tree-lock'); } function api_tree_create_node() { input_validate_input_number(get_request_var_request('tree_id')); input_validate_input_number(get_request_var_request('position')); /* clean up text string */ if (isset($_REQUEST['text'])) { $_REQUEST['text'] = sanitize_search_string(get_request_var_request('text')); } $tree_id = $_REQUEST['tree_id']; $position = $_REQUEST['position']; $title = $_REQUEST['text']; $data = api_tree_parse_node_data($_REQUEST['id']); if (!isset($data['branch']) || $data['branch'] < 0) { cacti_log("ERROR: Invalid BranchID: '" . (isset($data['branch']) ? $data['branch']:'-') . "', Function create_node", false); return; } /* watch out for monkey business */ input_validate_input_number($data['branch']); $save = array(); $save['parent'] = $data['branch']; $save['position'] = $position; $save['graph_tree_id'] = $tree_id; $save['local_graph_id'] = 0; $save['host_id'] = 0; $save['host_grouping_type'] = 1; $save['sort_children_type'] = 1; $save['title'] = $title; $id = sql_save($save, 'graph_tree_items'); api_tree_reorder_branch($tree_id, $data['branch'], $id, $position); header('Content-Type: application/json; charset=utf-8'); print json_encode(array('id' => 'tbranch:' . $id)); } function api_tree_delete_node() { input_validate_input_number(get_request_var_request('tree_id')); // Basic Error Checking if (!isset($_REQUEST['tree_id']) || empty($_REQUEST['tree_id']) || $_REQUEST['tree_id'] < 0) { cacti_log("ERROR: Invalid TreeID: '" . $_REQUEST['tree_id'] . ", Function delete_node", false); return; } if (!isset($_REQUEST['id']) || empty($_REQUEST['id'])) { cacti_log("ERROR: Invalid NodeID: '" . $_REQUEST['id'] . ", Function delete_node", false); return; } $tree_id = $_REQUEST['tree_id']; $data = api_tree_parse_node_data($_REQUEST['id']); if (isset($data['branch']) && $data['branch'] > 0) { if ($data['host'] == 0 && $data['graph'] == 0) { api_tree_delete_node_content($tree_id, $data['branch']); } db_execute("DELETE FROM graph_tree_items WHERE graph_tree_id=$tree_id AND id=" . $data['branch']); } } function api_tree_delete_node_content($tree_id, $branch_id) { $children = db_fetch_assoc("SELECT * FROM graph_tree_items WHERE graph_tree_id=$tree_id AND parent=$branch_id"); if (sizeof($children)) { foreach($children as $child) { if ($child['host_id'] == 0 && $child['graph_id'] == 0) { api_tree_delete_node_content($tree_id, $child['id']); } db_execute("DELETE FROM graph_tree_items WHERE graph_tree_id=$tree_id AND id=" . $child['id']); } } } function api_tree_move_node() { input_validate_input_number(get_request_var_request('tree_id')); input_validate_input_number(get_request_var_request('position')); // Basic Error Checking if (!isset($_REQUEST['tree_id']) || empty($_REQUEST['tree_id']) || $_REQUEST['tree_id'] < 0) { cacti_log("ERROR: Invalid TreeID: '" . $_REQUEST['tree_id'] . ", Function delete_node", false); return; } if (!isset($_REQUEST['id']) || empty($_REQUEST['id'])) { cacti_log("ERROR: Invalid NodeID: '" . $_REQUEST['id'] . ", Function move_node", false); return; } $position = $_REQUEST['position']; if (!isset($_REQUEST['parent']) || empty($_REQUEST['parent'])) { cacti_log("ERROR: Invalid NodeID: '" . $_REQUEST['id'] . ", Function move_node", false); return; }elseif ($_REQUEST['parent'] == '#') { $pdata['branch'] = 0; }else{ $pdata = api_tree_parse_node_data($_REQUEST['parent']); } $tree_id = $_REQUEST['tree_id']; $data = api_tree_parse_node_data($_REQUEST['id']); if ($data['parent'] != $pdata['branch']) { db_execute("UPDATE graph_tree_items SET parent=" . $pdata['branch'] . ", position=$position WHERE id=" . $data['branch'] . " AND graph_tree_id=$tree_id"); api_tree_reorder_branch($tree_id, $pdata['branch'], $data['branch'], $position); }elseif (isset($data['branch']) && $data['branch'] > 0 && isset($pdata['branch']) && $pdata['branch'] >= 0) { db_execute("UPDATE graph_tree_items SET position=$position WHERE graph_tree_id=$tree_id AND id=" . $data['branch']); api_tree_reorder_branch($tree_id, $pdata['branch'], $data['branch'], $position); }else{ cacti_log("Invalid Source Destination Branches, Function move_node", false); return; } } function api_tree_parse_node_data($variable) { // Initialize some variables $branch_id = 0; $graph_id = 0; $host_id = 0; if ($variable != '#') { // Process the 'id' variable $ndata = explode('_', $variable); if (sizeof($ndata)) { foreach($ndata as $data) { list($type, $tid) = explode(':', $data); /* watch out for monkey business */ input_validate_input_number($tid); switch ($type) { case 'tbranch': $branch_id = $tid; break; case 'tgraph': $graph_id = $tid; break; case 'thost': $host_id = $tid; break; } } } } if ($branch_id > 0) { $parent = db_fetch_cell("SELECT parent FROM graph_tree_items WHERE id=$branch_id"); }else{ $parent = '0'; } return array('branch' => $branch_id, 'graph' => $graph_id, 'host' => $host_id, 'parent' => $parent); } function api_tree_rename_node() { input_validate_input_number(get_request_var_request('tree_id')); /* clean up text string */ if (isset($_REQUEST['text'])) { $_REQUEST['text'] = sanitize_search_string(get_request_var_request('text')); } // Basic Error Checking if (!isset($_REQUEST['tree_id']) || empty($_REQUEST['tree_id']) || $_REQUEST['tree_id'] < 0) { cacti_log("ERROR: Invalid TreeID: '" . $_REQUEST['tree_id'] . ", Function rename_node", false); return; } if (!isset($_REQUEST['id']) || empty($_REQUEST['id'])) { cacti_log("ERROR: Invalid NodeID: '" . $_REQUEST['id'] . ", Function rename_node", false); return; } $tree_id = $_REQUEST['tree_id']; // Initialize some variables $branch_id = 0; $graph_id = 0; $host_id = 0; // Process the 'id' variable $ndata = explode('_', $_REQUEST['id']); if (sizeof($ndata)) { foreach($ndata as $data) { list($type, $tid) = explode(':', $data); /* watch out for monkey business */ input_validate_input_number($tid); switch ($type) { case 'tbranch': $branch_id = $tid; break; case 'tgraph': $graph_id = $tid; break; case 'thost': $host_id = $tid; break; } } } if (isset($branch_id) && $branch_id > 0) { if ($host_id > 0 || $graph_id > 0) { // Ignore. Need to customize context }else{ db_execute("UPDATE graph_tree_items SET title='" . $_REQUEST['text'] . "' WHERE graph_tree_id=$tree_id AND id=$branch_id"); } } } function api_tree_get_main($tree_id, $parent = 0) { //$heirarchy = draw_dhtml_tree_level($tree_id, $parent); $is_root = false; if ($parent == -1) { $parent = 0; $is_root = true; $name = db_fetch_cell("SELECT name FROM graph_tree WHERE id=$tree_id"); print "