0.8.7g-2.1+pia2p9+neutron1
[debian/cacti] / tree.php
1 <?php
2 /*
3  +-------------------------------------------------------------------------+
4  | Copyright (C) 2004-2010 The Cacti Group                                 |
5  |                                                                         |
6  | This program is free software; you can redistribute it and/or           |
7  | modify it under the terms of the GNU General Public License             |
8  | as published by the Free Software Foundation; either version 2          |
9  | of the License, or (at your option) any later version.                  |
10  |                                                                         |
11  | This program is distributed in the hope that it will be useful,         |
12  | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
13  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
14  | GNU General Public License for more details.                            |
15  +-------------------------------------------------------------------------+
16  | Cacti: The Complete RRDTool-based Graphing Solution                     |
17  +-------------------------------------------------------------------------+
18  | This code is designed, written, and maintained by the Cacti Group. See  |
19  | about.php and/or the AUTHORS file for specific developer information.   |
20  +-------------------------------------------------------------------------+
21  | http://www.cacti.net/                                                   |
22  +-------------------------------------------------------------------------+
23 */
24
25 include("./include/auth.php");
26 include_once('./lib/api_tree.php');
27 include_once('./lib/tree.php');
28 include_once('./lib/html_tree.php');
29
30 input_validate_input_number(get_request_var('tree_id'));
31 input_validate_input_number(get_request_var('leaf_id'));
32 input_validate_input_number(get_request_var_post('graph_tree_id'));
33 input_validate_input_number(get_request_var_post('parent_item_id'));
34
35 /* set default action */
36 if (!isset($_REQUEST["action"])) { $_REQUEST["action"] = ""; }
37
38 switch ($_REQUEST["action"]) {
39         case 'save':
40                 form_save();
41
42                 break;
43         case 'item_movedown':
44                 item_movedown();
45
46                 header("Location: tree.php?action=edit&id=" . $_GET["tree_id"]);
47                 break;
48         case 'item_moveup':
49                 item_moveup();
50
51                 header("Location: tree.php?action=edit&id=" . $_GET["tree_id"]);
52                 break;
53         case 'item_edit':
54                 include_once("./include/top_header.php");
55
56                 item_edit();
57
58                 include_once("./include/bottom_footer.php");
59                 break;
60         case 'item_remove':
61                 item_remove();
62
63                 header("Location: tree.php?action=edit&id=" . $_GET["tree_id"]);
64                 break;
65         case 'remove':
66                 tree_remove();
67
68                 header("Location: tree.php");
69                 break;
70         case 'edit':
71                 include_once("./include/top_header.php");
72
73                 tree_edit();
74
75                 include_once("./include/bottom_footer.php");
76                 break;
77         default:
78                 include_once("./include/top_header.php");
79
80                 tree();
81
82                 include_once("./include/bottom_footer.php");
83                 break;
84 }
85
86 /* --------------------------
87     The Save Function
88    -------------------------- */
89 function form_save() {
90
91         /* clear graph tree cache on save - affects current user only, other users should see changes in <5 minutes */
92         if (isset($_SESSION['dhtml_tree'])) {
93                 unset($_SESSION['dhtml_tree']);
94         }
95
96         if (isset($_POST["save_component_tree"])) {
97                 $save["id"] = $_POST["id"];
98                 $save["name"] = form_input_validate($_POST["name"], "name", "", false, 3);
99                 $save["sort_type"] = form_input_validate($_POST["sort_type"], "sort_type", "", true, 3);
100
101                 if (!is_error_message()) {
102                         $tree_id = sql_save($save, "graph_tree");
103
104                         if ($tree_id) {
105                                 raise_message(1);
106
107                                 /* sort the tree using the algorithm chosen by the user */
108                                 sort_tree(SORT_TYPE_TREE, $tree_id, $_POST["sort_type"]);
109                         }else{
110                                 raise_message(2);
111                         }
112                 }
113
114                 header("Location: tree.php?action=edit&id=" . (empty($tree_id) ? $_POST["id"] : $tree_id));
115         }elseif (isset($_POST["save_component_tree_item"])) {
116                 $tree_item_id = api_tree_item_save($_POST["id"], $_POST["graph_tree_id"], $_POST["type"], $_POST["parent_item_id"],
117                         (isset($_POST["title"]) ? $_POST["title"] : ""), (isset($_POST["local_graph_id"]) ? $_POST["local_graph_id"] : "0"),
118                         (isset($_POST["rra_id"]) ? $_POST["rra_id"] : "0"), (isset($_POST["host_id"]) ? $_POST["host_id"] : "0"),
119                         (isset($_POST["host_grouping_type"]) ? $_POST["host_grouping_type"] : "1"), (isset($_POST["sort_children_type"]) ? $_POST["sort_children_type"] : "1"),
120                         (isset($_POST["propagate_changes"]) ? true : false));
121
122                 if (is_error_message()) {
123                         header("Location: tree.php?action=item_edit&tree_item_id=" . (empty($tree_item_id) ? $_POST["id"] : $tree_item_id) . "&tree_id=" . $_POST["graph_tree_id"] . "&parent_id=" . $_POST["parent_item_id"]);
124                 }else{
125                         header("Location: tree.php?action=edit&id=" . $_POST["graph_tree_id"]);
126                 }
127         }
128 }
129
130 /* -----------------------
131     Tree Item Functions
132    ----------------------- */
133
134 function item_edit() {
135         global $colors, $tree_sort_types;
136         global $tree_item_types, $host_group_types;
137
138         /* ================= input validation ================= */
139         input_validate_input_number(get_request_var("id"));
140         input_validate_input_number(get_request_var("tree_id"));
141         /* ==================================================== */
142
143         if (!empty($_GET["id"])) {
144                 $tree_item = db_fetch_row("select * from graph_tree_items where id=" . $_GET["id"]);
145
146                 if ($tree_item["local_graph_id"] > 0) { $db_type = TREE_ITEM_TYPE_GRAPH; }
147                 if ($tree_item["title"] != "") { $db_type = TREE_ITEM_TYPE_HEADER; }
148                 if ($tree_item["host_id"] > 0) { $db_type = TREE_ITEM_TYPE_HOST; }
149         }
150
151         if (isset($_GET["type_select"])) {
152                 $current_type = $_GET["type_select"];
153         }elseif (isset($db_type)) {
154                 $current_type = $db_type;
155         }else{
156                 $current_type = TREE_ITEM_TYPE_HEADER;
157         }
158
159         $tree_sort_type = db_fetch_cell("select sort_type from graph_tree where id='" . $_GET["tree_id"] . "'");
160
161         print "<form method='post' action='tree.php' name='form_tree'>\n";
162
163         html_start_box("<strong>Tree Items</strong>", "100%", $colors["header"], "3", "center", "");
164
165         form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],0); ?>
166                 <td width="50%">
167                         <font class="textEditTitle">Parent Item</font><br>
168                         Choose the parent for this header/graph.
169                 </td>
170                 <td>
171                         <?php grow_dropdown_tree($_GET["tree_id"], "parent_item_id", (isset($_GET["parent_id"]) ? $_GET["parent_id"] : get_parent_id($tree_item["id"], "graph_tree_items", "graph_tree_id=" . $_GET["tree_id"])));?>
172                 </td>
173         </tr>
174         <?php form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],1); ?>
175                 <td width="50%">
176                         <font class="textEditTitle">Tree Item Type</font><br>
177                         Choose what type of tree item this is.
178                 </td>
179                 <td>
180                         <select name="type_select" onChange="window.location=document.form_tree.type_select.options[document.form_tree.type_select.selectedIndex].value">
181                                 <?php
182                                 while (list($var, $val) = each($tree_item_types)) {
183                                         print "<option value='tree.php?action=item_edit" . (isset($_GET["id"]) ? "&id=" . $_GET["id"] : "") . (isset($_GET["parent_id"]) ? "&parent_id=" . $_GET["parent_id"] : "") . "&tree_id=" . $_GET["tree_id"] . "&type_select=$var'"; if ($var == $current_type) { print " selected"; } print ">$val</option>\n";
184                                 }
185                                 ?>
186                         </select>
187                 </td>
188         </tr>
189         <tr bgcolor='#<?php print $colors["header_panel"];?>'>
190                 <td colspan="2" class='textSubHeaderDark'>Tree Item Value</td>
191         </tr>
192         <?php
193         switch ($current_type) {
194         case TREE_ITEM_TYPE_HEADER:
195                 $i = 0;
196
197                 /* it's nice to default to the parent sorting style for new items */
198                 if (empty($_GET["id"])) {
199                         $default_sorting_type = db_fetch_cell("select sort_children_type from graph_tree_items where id=" . $_GET["parent_id"]);
200                 }else{
201                         $default_sorting_type = TREE_ORDERING_NONE;
202                 }
203
204                 form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i); $i++; ?>
205                         <td width="50%">
206                                 <font class="textEditTitle">Title</font><br>
207                                 If this item is a header, enter a title here.
208                         </td>
209                         <td>
210                                 <?php form_text_box("title", (isset($tree_item["title"]) ? $tree_item["title"] : ""), "", "255", 30, "text", (isset($_GET["id"]) ? $_GET["id"] : "0"));?>
211                         </td>
212                 </tr>
213                 <?php
214                 /* don't allow the user to change the tree item ordering if a tree order has been specified */
215                 if ($tree_sort_type == TREE_ORDERING_NONE) {
216                         form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i); $i++; ?>
217                                 <td width="50%">
218                                         <font class="textEditTitle">Sorting Type</font><br>
219                                         Choose how children of this branch will be sorted.
220                                 </td>
221                                 <td>
222                                         <?php form_dropdown("sort_children_type", $tree_sort_types, "", "", (isset($tree_item["sort_children_type"]) ? $tree_item["sort_children_type"] : $default_sorting_type), "", "");?>
223                                 </td>
224                         </tr>
225                         <?php
226                 }
227
228                 if ((!empty($_GET["id"])) && ($tree_sort_type == TREE_ORDERING_NONE)) {
229                         form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i); $i++; ?>
230                                 <td width="50%">
231                                         <font class="textEditTitle">Propagate Changes</font><br>
232                                         Propagate all options on this form (except for 'Title') to all child 'Header' items.
233                                 </td>
234                                 <td>
235                                         <?php form_checkbox("propagate_changes", "", "Propagate Changes", "", "", "", 0);?>
236                                 </td>
237                         </tr>
238                         <?php
239                 }
240                 break;
241         case TREE_ITEM_TYPE_GRAPH:
242                 form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],0); ?>
243                         <td width="50%">
244                                 <font class="textEditTitle">Graph</font><br>
245                                 Choose a graph from this list to add it to the tree.
246                         </td>
247                         <td>
248                                 <?php form_dropdown("local_graph_id", db_fetch_assoc("select graph_templates_graph.local_graph_id as id,graph_templates_graph.title_cache as name from (graph_templates_graph,graph_local) where graph_local.id=graph_templates_graph.local_graph_id and local_graph_id != 0 order by title_cache"), "name", "id", (isset($tree_item["local_graph_id"]) ? $tree_item["local_graph_id"] : ""), "", "");?>
249                         </td>
250                 </tr>
251                 <?php form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],1); ?>
252                         <td width="50%">
253                                 <font class="textEditTitle">Round Robin Archive</font><br>
254                                 Choose a round robin archive to control how the Graph Thumbnail is displayed when using Tree Export.
255                         </td>
256                         <td>
257                                 <?php form_dropdown("rra_id", db_fetch_assoc("select id,name from rra order by timespan"), "name", "id", (isset($tree_item["rra_id"]) ? $tree_item["rra_id"] : ""), "", "");?>
258                         </td>
259                 </tr>
260                 <?php
261                 break;
262         case TREE_ITEM_TYPE_HOST:
263                 form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],0); ?>
264                         <td width="50%">
265                                 <font class="textEditTitle">Host</font><br>
266                                 Choose a host here to add it to the tree.
267                         </td>
268                         <td>
269                                 <?php form_dropdown("host_id", db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from host order by description,hostname"), "name", "id", (isset($tree_item["host_id"]) ? $tree_item["host_id"] : ""), "", "");?>
270                         </td>
271                 </tr>
272                 <?php form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],1); ?>
273                         <td width="50%">
274                                 <font class="textEditTitle">Graph Grouping Style</font><br>
275                                 Choose how graphs are grouped when drawn for this particular host on the tree.
276                         </td>
277                         <td>
278                                 <?php form_dropdown("host_grouping_type", $host_group_types, "", "", (isset($tree_item["host_grouping_type"]) ? $tree_item["host_grouping_type"] : "1"), "", "");?>
279                         </td>
280                 </tr>
281                 <?php form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],1); ?>
282                         <td width="50%">
283                                 <font class="textEditTitle">Round Robin Archive</font><br>
284                                 Choose a round robin archive to control how Graph Thumbnails are displayed when using Tree Export.
285                         </td>
286                         <td>
287                                 <?php form_dropdown("rra_id", db_fetch_assoc("select id,name from rra order by timespan"), "name", "id", (isset($tree_item["rra_id"]) ? $tree_item["rra_id"] : ""), "", "");?>
288                         </td>
289                 </tr>
290                 <?php
291                 break;
292         }
293         ?>
294         </tr>
295         <?php
296
297         form_hidden_box("id", (isset($_GET["id"]) ? $_GET["id"] : "0"), "");
298         form_hidden_box("graph_tree_id", $_GET["tree_id"], "");
299         form_hidden_box("type", $current_type, "");
300         form_hidden_box("save_component_tree_item", "1", "");
301
302         html_end_box();
303
304         form_save_button("tree.php?action=edit&id=" . $_GET["tree_id"]);
305 }
306
307 function item_moveup() {
308         /* ================= input validation ================= */
309         input_validate_input_number(get_request_var("id"));
310         input_validate_input_number(get_request_var("tree_id"));
311         /* ==================================================== */
312
313         $order_key = db_fetch_cell("SELECT order_key FROM graph_tree_items WHERE id=" . $_GET["id"]);
314         if ($order_key > 0) { branch_up($order_key, 'graph_tree_items', 'order_key', 'graph_tree_id=' . $_GET["tree_id"]); }
315
316         /* clear graph tree cache on save - affects current user only, other users should see changes in <5 minutes */
317         if (isset($_SESSION['dhtml_tree'])) {
318                 unset($_SESSION['dhtml_tree']);
319         }
320
321 }
322
323 function item_movedown() {
324         /* ================= input validation ================= */
325         input_validate_input_number(get_request_var("id"));
326         input_validate_input_number(get_request_var("tree_id"));
327         /* ==================================================== */
328
329         $order_key = db_fetch_cell("SELECT order_key FROM graph_tree_items WHERE id=" . $_GET["id"]);
330         if ($order_key > 0) { branch_down($order_key, 'graph_tree_items', 'order_key', 'graph_tree_id=' . $_GET["tree_id"]); }
331
332         /* clear graph tree cache on save - affects current user only, other users should see changes in <5 minutes */
333         if (isset($_SESSION['dhtml_tree'])) {
334                 unset($_SESSION['dhtml_tree']);
335         }
336
337 }
338
339 function item_remove() {
340         /* ================= input validation ================= */
341         input_validate_input_number(get_request_var("id"));
342         input_validate_input_number(get_request_var("tree_id"));
343         /* ==================================================== */
344
345         if ((read_config_option("deletion_verification") == "on") && (!isset($_GET["confirm"]))) {
346                 $graph_tree_item = db_fetch_row("select title,local_graph_id,host_id from graph_tree_items where id=" . $_GET["id"]);
347
348                 if (!empty($graph_tree_item["local_graph_id"])) {
349                         $text = "Are you sure you want to delete the graph item <strong>'" . db_fetch_cell("select title_cache from graph_templates_graph where local_graph_id=" . $graph_tree_item["local_graph_id"]) . "'</strong>?";
350                 }elseif ($graph_tree_item["title"] != "") {
351                         $text = "Are you sure you want to delete the header item <strong>'" . $graph_tree_item["title"] . "'</strong>?";
352                 }elseif (!empty($graph_tree_item["host_id"])) {
353                         $text = "Are you sure you want to delete the host item <strong>'" . db_fetch_cell("select CONCAT_WS('',description,' (',hostname,')') as hostname from host where id=" . $graph_tree_item["host_id"]) . "'</strong>?";
354                 }
355
356                 include("./include/top_header.php");
357                 form_confirm("Are You Sure?", $text, htmlspecialchars("tree.php?action=edit&id=" . $_GET["tree_id"]), htmlspecialchars("tree.php?action=item_remove&id=" . $_GET["id"] . "&tree_id=" . $_GET["tree_id"]));
358                 include("./include/bottom_footer.php");
359                 exit;
360         }
361
362         if ((read_config_option("deletion_verification") == "") || (isset($_GET["confirm"]))) {
363                 delete_branch($_GET["id"]);
364         }
365
366         /* clear graph tree cache on save - affects current user only, other users should see changes in <5 minutes */
367         if (isset($_SESSION['dhtml_tree'])) {
368                 unset($_SESSION['dhtml_tree']);
369         }
370
371         header("Location: tree.php?action=edit&id=" . $_GET["tree_id"]); exit;
372 }
373
374
375 /* ---------------------
376     Tree Functions
377    --------------------- */
378
379 function tree_remove() {
380         /* ================= input validation ================= */
381         input_validate_input_number(get_request_var("id"));
382         /* ==================================================== */
383
384         if ((read_config_option("deletion_verification") == "on") && (!isset($_GET["confirm"]))) {
385                 include("./include/top_header.php");
386                 form_confirm("Are You Sure?", "Are you sure you want to delete the tree <strong>'" . db_fetch_cell("select name from graph_tree where id=" . $_GET["id"]) . "'</strong>?", htmlspecialchars("tree.php"), htmlspecialchars("tree.php?action=remove&id=" . $_GET["id"]));
387                 include("./include/bottom_footer.php");
388                 exit;
389         }
390
391         if ((read_config_option("deletion_verification") == "") || (isset($_GET["confirm"]))) {
392                 db_execute("delete from graph_tree where id=" . $_GET["id"]);
393                 db_execute("delete from graph_tree_items where graph_tree_id=" . $_GET["id"]);
394         }
395
396         /* clear graph tree cache on save - affects current user only, other users should see changes in <5 minutes */
397         if (isset($_SESSION['dhtml_tree'])) {
398                 unset($_SESSION['dhtml_tree']);
399         }
400
401 }
402
403 function tree_edit() {
404         global $colors, $fields_tree_edit;
405
406         /* ================= input validation ================= */
407         input_validate_input_number(get_request_var("id"));
408         /* ==================================================== */
409
410         /* clean up subaction */
411         if (isset($_REQUEST["subaction"])) {
412                 $_REQUEST["subaction"] = sanitize_search_string(get_request_var("subaction"));
413         }
414
415         if (!empty($_GET["id"])) {
416                 $tree = db_fetch_row("select * from graph_tree where id=" . $_GET["id"]);
417                 $header_label = "[edit: " . htmlspecialchars($tree["name"]) . "]";
418         }else{
419                 $header_label = "[new]";
420         }
421
422         html_start_box("<strong>Graph Trees</strong> $header_label", "100%", $colors["header"], "3", "center", "");
423
424         draw_edit_form(array(
425                 "config" => array(),
426                 "fields" => inject_form_variables($fields_tree_edit, (isset($tree) ? $tree : array()))
427                 ));
428
429         html_end_box();
430
431         if (!empty($_GET["id"])) {
432                 html_start_box("<strong>Tree Items</strong>", "100%", $colors["header"], "3", "center", "tree.php?action=item_edit&tree_id=" . $tree["id"] . "&parent_id=0");
433
434                 ?>
435                 <td>
436                 <input type='button' onClick='return document.location="tree.php?action=edit&id=<?php print $_GET["id"];?>&subaction=expand_all"' value='Expand All' title='Expand All Trees'>
437                 <input type='button' onClick='return document.location="tree.php?action=edit&id=<?php print $_GET["id"];?>&subaction=collapse_all"' value='Collapse All' title='Collapse All Trees'></a>
438                 </td>
439                 <?php
440
441                 print "<tr bgcolor='#" . $colors["header_panel"] . "'>";
442                         DrawMatrixHeaderItem("Item",$colors["header_text"],1);
443                         DrawMatrixHeaderItem("Value",$colors["header_text"],1);
444                         DrawMatrixHeaderItem("&nbsp;",$colors["header_text"],2);
445                 print "</tr>";
446
447                 grow_edit_graph_tree($_GET["id"], "", "");
448                 html_end_box();
449         }
450
451         form_save_button("tree.php", "return");
452 }
453
454 function tree() {
455         global $colors;
456
457         html_start_box("<strong>Graph Trees</strong>", "100%", $colors["header"], "3", "center", "tree.php?action=edit");
458
459         print "<tr bgcolor='#" . $colors["header_panel"] . "'>";
460                 DrawMatrixHeaderItem("Name",$colors["header_text"],1);
461                 DrawMatrixHeaderItem("&nbsp;",$colors["header_text"],1);
462         print "</tr>";
463
464         $trees = db_fetch_assoc("SELECT * FROM graph_tree ORDER BY name");
465
466         $i = 0;
467         if (sizeof($trees) > 0) {
468         foreach ($trees as $tree) {
469                 form_alternate_row_color($colors["alternate"],$colors["light"],$i); $i++;
470                         ?>
471                         <td>
472                                 <a class="linkEditMain" href="<?php print htmlspecialchars("tree.php?action=edit&id=" . $tree["id"]);?>"><?php print htmlspecialchars($tree["name"]);?></a>
473                         </td>
474                         <td align="right">
475                                 <a href="<?php print htmlspecialchars("tree.php?action=remove&id=" . $tree["id"]);?>"><img src="images/delete_icon.gif" style="height:10px;width:10px;" border="0" alt="Delete"></a>
476                         </td>
477                 </tr>
478         <?php
479         }
480         }else{
481                 print "<tr><td><em>No Graphs Trees</em></td></tr>\n";
482         }
483         html_end_box();
484 }
485  ?>