Beispiel #1
0
function api_graph_tree_item_fields_validate(&$_fields_graph_tree_item, $graph_tree_item_field_name_format = "|field|") {
	require_once(CACTI_BASE_PATH . "/include/graph_tree/graph_tree_constants.php");
	require_once(CACTI_BASE_PATH . "/lib/graph_tree/graph_tree_info.php");

	if (sizeof($_fields_graph_tree_item) == 0) {
		return array();
	}

	/* array containing errored fields */
	$error_fields = array();

	/* get a complete field list */
	$fields_graph_tree_item = api_graph_tree_item_form_list();

	/* the type of input allowed in the 'item_value' field varies depending upon the selected 'item_type' */
	if (isset($_fields_graph_tree_item["item_type"])) {
		if ($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_HEADER) {
			$fields_graph_tree_item["item_value"]["validate_regexp"] = "";
		}else if ($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_GRAPH) {
			$fields_graph_tree_item["item_value"]["validate_regexp"] = "^[0-9]+$";
		}else if ($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_HOST) {
			$fields_graph_tree_item["item_value"]["validate_regexp"] = "^[0-9]+$";
		}
	}else{
		/* if no item type is passed in, we have no way of validating the item value field */
		unset($fields_graph_tree_item["item_value"]);
	}

	/* base fields */
	while (list($_field_name, $_field_array) = each($fields_graph_tree_item)) {
		if ((isset($_fields_graph_tree_item[$_field_name])) && (isset($_field_array["validate_regexp"])) && (isset($_field_array["validate_empty"]))) {
			$form_field_name = str_replace("|field|", $_field_name, $graph_tree_item_field_name_format);

			if (!form_input_validate($_fields_graph_tree_item[$_field_name], $form_field_name, $_field_array["validate_regexp"], $_field_array["validate_empty"])) {
				$error_fields[] = $form_field_name;
			}
		}
	}

	return $error_fields;
}
Beispiel #2
0
function api_graph_tree_item_list($graph_tree_id, $filter_array = "", $limit_sub_tree_id = "", $show_sub_tree_parent = false, $show_sub_tree_children = true, $current_page = 0, $rows_per_page = 0) {
	/* sanity checks */
	validate_id_die($graph_tree_id, "graph_tree_id");

	require_once(CACTI_BASE_PATH . "/lib/graph_tree/graph_tree_form.php");

	$sql_where = "";
	/* validation and setup for the WHERE clause */
	if ((is_array($filter_array)) && (sizeof($filter_array) > 0)) {
		/* validate each field against the known master field list */
		$field_errors = api_graph_tree_item_fields_validate(sql_filter_array_to_field_array($filter_array));

		/* if a field input error has occured, register the error in the session and return */
		if (sizeof($field_errors) > 0) {
			field_register_error($field_errors);
			return false;
		/* otherwise, form an SQL WHERE string using the filter fields */
		}else{
			$sql_where = sql_filter_array_to_where_string($filter_array, api_graph_tree_item_form_list(), false);
		}
	}

	$sql_limit = "";
	/* validation and setup for the LIMIT clause */
	if ((is_numeric($current_page)) && (is_numeric($rows_per_page)) && (!empty($current_page)) && (!empty($rows_per_page))) {
		$sql_limit = "limit " . ($rows_per_page * ($current_page - 1)) . ",$rows_per_page";
	}

	/* only show tree items under this item if specified */
	if (db_integer_validate($limit_sub_tree_id, false, false)) {
		$graph_tree_item = api_graph_tree_item_get($limit_sub_tree_id);
		$search_key = substr($graph_tree_item["order_key"], 0, (api_graph_tree_item_depth_get($graph_tree_item["order_key"]) * CHARS_PER_TIER));

		if ($show_sub_tree_children == true) {
			$sql_where .= "and graph_tree_items.order_key like '$search_key%%'";
		}else{
			$sql_where .= " and graph_tree_items.order_key like '$search_key" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', (MAX_TREE_DEPTH * CHARS_PER_TIER) - (strlen($search_key) + CHARS_PER_TIER)) . "'";
		}

		if ($show_sub_tree_parent == false) {
			$sql_where .= "and graph_tree_items.id != $limit_sub_tree_id";
		}
	}

	return db_fetch_assoc("select
		graph_tree_items.id,
		graph_tree_items.order_key,
		graph_tree_items.sort_children_type,
		graph_tree_items.device_grouping_type,
		graph_tree_items.item_type,
		graph_tree_items.item_value,
		graph.title_cache as graph_title,
		host.description as host_description,
		host.hostname as host_hostname
		from graph_tree_items
		left join graph on (graph_tree_items.item_value = graph.id and graph_tree_items.item_type = " . TREE_ITEM_TYPE_GRAPH . ")
		left join host on (graph_tree_items.item_value = host.id and graph_tree_items.item_type = " . TREE_ITEM_TYPE_HOST . ")
		where graph_tree_items.graph_tree_id = " . sql_sanitize($graph_tree_id) . "
		$sql_where
		order by graph_tree_items.order_key
		$sql_limit");
}
Beispiel #3
0
function api_graph_tree_item_save($graph_tree_item_id, &$_fields_graph_tree_item) {
	require_once(CACTI_BASE_PATH . "/include/graph_tree/graph_tree_constants.php");
	require_once(CACTI_BASE_PATH . "/lib/graph_tree/graph_tree_utility.php");
	require_once(CACTI_BASE_PATH . "/lib/graph_tree/graph_tree_info.php");

	/* sanity checks */
	validate_id_die($graph_tree_item_id, "graph_tree_item_id", true);

	/* sanity check for $graph_tree_id */
	if ((empty($graph_tree_item_id)) && (empty($_fields_graph_tree_item["graph_tree_id"]))) {
		log_save("Required graph_tree_id when graph_tree_item_id = 0", SEV_ERROR);
		return false;
	}else if ((isset($_fields_graph_tree_item["graph_tree_id"])) && (!db_integer_validate($_fields_graph_tree_item["graph_tree_id"]))) {
		return false;
	}

	/* sanity check for $item_type */
	if ((!isset($_fields_graph_tree_item["item_type"])) || (!db_integer_validate($_fields_graph_tree_item["item_type"]))) {
		log_save("Missing required item_type", SEV_ERROR);
		return false;
	}

	/* sanity check for $item_value */
	if ((empty($graph_tree_item_id)) && (empty($_fields_graph_tree_item["item_value"]))) {
		log_save("Required item_value when graph_tree_item_id = 0", SEV_ERROR);
		return false;
	}else if ((isset($_fields_graph_tree_item["item_value"])) && ( (($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_GRAPH) || ($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_HOST)) && (!db_integer_validate($_fields_graph_tree_item["item_value"])) )) {
		return false;
	}

	/* sanity check for $parent_item_id */
	if ((!isset($_fields_graph_tree_item["parent_item_id"])) || (!db_integer_validate($_fields_graph_tree_item["parent_item_id"], true))) {
		log_save("Missing required parent_item_id", SEV_ERROR);
		return false;
	}

	/* field: id */
	$_fields["id"] = array("type" => DB_TYPE_INTEGER, "value" => $graph_tree_item_id);

	/* field: graph_tree_id */
	if (isset($_fields_graph_tree_item["graph_tree_id"])) {
		$_fields["graph_tree_id"] = array("type" => DB_TYPE_INTEGER, "value" => $_fields_graph_tree_item["graph_tree_id"]);
	}

	/* get a copy of the parent tree item id */
	if ($_fields_graph_tree_item["parent_item_id"] == "0") {
		$parent_order_key = "";
		$parent_sort_type = TREE_ORDERING_NONE;
	}else{
		$parent_graph_tree_item = api_graph_tree_item_get($_fields_graph_tree_item["parent_item_id"]);
		$parent_order_key = $parent_graph_tree_item["order_key"];
		$parent_sort_type = $parent_graph_tree_item["sort_children_type"];
	}

	/* generate a new order key if this is a new graph tree item */
	if (empty($graph_tree_item_id)) {
		$_fields["order_key"] = array("type" => DB_TYPE_STRING, "value" => api_graph_tree_item_available_order_key_get($_fields_graph_tree_item["graph_tree_id"], $parent_order_key));
	}else{
		$graph_tree_item = api_graph_tree_item_get($graph_tree_item_id);
		$_fields["order_key"] = array("type" => DB_TYPE_STRING, "value" => $graph_tree_item["order_key"]);
	}

	/* if this item is a graph, make sure it is not being added to the same branch twice */
	$search_key = substr($parent_order_key, 0, (api_graph_tree_item_depth_get($parent_order_key) * CHARS_PER_TIER));
	if (($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_GRAPH) && (sizeof(db_fetch_assoc("select id from graph_tree_items where item_value = " . $_fields_graph_tree_item["item_value"] . " and item_type = " . TREE_ITEM_TYPE_GRAPH . " and graph_tree_id = " . $_fields_graph_tree_item["graph_tree_id"] . " and order_key like '$search_key" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', (MAX_TREE_DEPTH * CHARS_PER_TIER) - (strlen($search_key) + CHARS_PER_TIER)) . "'")) > 0)) {
		return true;
	}

	/* convert the input array into something that is compatible with db_replace() */
	$_fields += sql_get_database_field_array($_fields_graph_tree_item, api_graph_tree_item_form_list());

	/* check for an empty field list */
	if (sizeof($_fields) == 1) {
		return true;
	}

	if (db_replace("graph_tree_items", $_fields, array("id"))) {
		if (empty($graph_tree_item_id)) {
			$graph_tree_item_id = db_fetch_insert_id();
		}

		/* re-parent the branch if the parent item has changed */
		if ($_fields_graph_tree_item["parent_item_id"] != api_graph_tree_item_parent_get_bykey($_fields["order_key"]["value"], $_fields_graph_tree_item["graph_tree_id"])) {
			api_graph_tree_item_reparent($graph_tree_item_id, $_fields_graph_tree_item["parent_item_id"]);
		}

		$parent_tree = api_graph_tree_get($_fields_graph_tree_item["graph_tree_id"]);

		/* tree item ordering */
		if ($parent_tree["sort_type"] == TREE_ORDERING_NONE) {
			/* resort our parent */
			if ($parent_sort_type != TREE_ORDERING_NONE) {
				echo $parent_sort_type;
				api_graph_tree_item_sort(SORT_TYPE_TREE_ITEM, $_fields_graph_tree_item["parent_item_id"], $parent_sort_type);
			}

			/* if this is a header, sort direct children */
			if (($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_HEADER) && ($parent_sort_type != TREE_ORDERING_NONE)) {
				api_graph_tree_item_sort(SORT_TYPE_TREE_ITEM, $graph_tree_item_id, $parent_sort_type);
			}
		/* tree ordering */
		}else{
			/* potential speed savings for large trees */
			if (api_graph_tree_item_depth_get($_fields["order_key"]["value"]) == 1) {
				api_graph_tree_item_sort(SORT_TYPE_TREE, $_fields_graph_tree_item["graph_tree_id"], $parent_tree["sort_type"]);
			}else{
				api_graph_tree_item_sort(SORT_TYPE_TREE_ITEM, $_fields_graph_tree_item["parent_item_id"], $parent_tree["sort_type"]);
			}
		}

		/* if the user checked the 'Propagate Changes' box */
		if (($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_HEADER) && (isset($_fields_graph_tree_item["sort_children_type"])) && (!empty($_fields_graph_tree_item["propagate_changes"]))) {
			$graph_tree_items = api_graph_tree_item_list($_fields_graph_tree_item["graph_tree_id"], array("item_type" => TREE_ITEM_TYPE_HEADER), $graph_tree_item_id, false, false);

			if (is_array($graph_tree_items) > 0) {
				foreach ($graph_tree_items as $graph_tree_item) {
					db_update("graph_tree_items",
						array(
							"id" => array("type" => DB_TYPE_INTEGER, "value" => $graph_tree_item["id"]),
							"sort_children_type" => array("type" => DB_TYPE_INTEGER, "value" => $_fields_graph_tree_item["sort_children_type"])
							),
						array("id"));

					if ($_fields_graph_tree_item["sort_children_type"] != TREE_ORDERING_NONE) {
						api_graph_tree_item_sort(SORT_TYPE_TREE_ITEM, $graph_tree_item["id"], $_fields_graph_tree_item["sort_children_type"]);
					}
				}
			}
		}

		return $graph_tree_item_id;
	}else{
		return false;
	}
}