用过Cacti的小伙伴,一定会碰到一个很烦恼的问题,Cacti的页面并不是像我们平时那样写成静态页面,而是通过conf文件+封装的函数生成的。下面将给大家呈现Cacti页面的生成方式,方便大家做二次开发。
1.页面地址(http://xx.xx.xx.xx/cacti/user_admin?action=user_edit&id=…后面省略)
/* --------------------------
User Administration
-------------------------- */
function user_edit() {
global $colors, $fields_user_user_edit_host;
/* ================= input validation ================= */
input_validate_input_number(get_request_var("id"));
/* ==================================================== */
if (!empty($_GET["id"])) {
$user = db_fetch_row("SELECT * FROM user_auth WHERE id = " . get_request_var("id"));
$header_label = "[edit: " . $user["username"] . "]";
}else{
$header_label = "[new]";
}
api_plugin_hook_function('user_admin_edit', (isset($user) ? get_request_var("id") : 0));
html_start_box("<strong>用户管理</strong> $header_label", "100%", $colors["header"], "3", "center", "");
draw_edit_form(array(
"config" => array("form_name" => "chk"),
"fields" => inject_form_variables($fields_user_user_edit_host, (isset($user) ? $user : array()))
));
html_end_box();
if (!empty($_GET["id"])) {
/* draw user admin nav tabs */
?>
<table class='tabs' width='100%' cellspacing='0' cellpadding='3' align='center'>
<tr>
<td width='1'></td>
<td <?php print ((get_request_var_request("tab") == "user_realms_edit") ? "bgcolor='silver'" : "bgcolor='#DFDFDF'");?> nowrap='nowrap' width='150' align='center' class='tab'>
<span class='textHeader'><a href='<?php print htmlspecialchars("user_admin.php?action=user_edit&tab=user_realms_edit&id=" . $_GET["id"]);?>'>Realm Permissions</a></span>
</td>
<td width='1'></td>
<td <?php print ((get_request_var_request("tab") == "graph_perms_edit") ? "bgcolor='silver'" : "bgcolor='#DFDFDF'");?> nowrap='nowrap' width='150' align='center' class='tab'>
<span class='textHeader'><a href='<?php print htmlspecialchars("user_admin.php?action=user_edit&tab=graph_perms_edit&id=" . $_GET["id"]);?>'>Graph Permissions</a></span>
</td>
<td width='1'></td>
<td <?php print ((get_request_var_request("tab") == "graph_settings_edit") ? "bgcolor='silver'" : "bgcolor='#DFDFDF'");?> nowrap='nowrap' width='130' align='center' class='tab'>
<span class='textHeader'><a href='<?php print htmlspecialchars("user_admin.php?action=user_edit&tab=graph_settings_edit&id=" . $_GET["id"]);?>'>Graph Settings</a></span>
</td>
<?php api_plugin_hook('user_admin_tab');?>
<td></td>
</tr>
</table>
<?php
}
if (get_request_var_request("tab") == "graph_settings_edit") {
graph_settings_edit();
}elseif (get_request_var_request("tab") == "user_realms_edit") {
user_realms_edit();
}elseif (get_request_var_request("tab") == "graph_perms_edit") {
graph_perms_edit();
}else{
if (api_plugin_hook_function('user_admin_run_action', get_request_var_request("tab"))) {
user_realms_edit();
}
}
form_save_button("user_admin.php", "return");
}
可以看到上面有一个封装的函数draw_edit_form()
这个函数就是用来生成主要显示页面的,该函数在cacti目录下的lib/html_form.php中具体内容如下,当然可以自己编辑:
function draw_edit_form($array) {
global $colors;
if (sizeof($array) > 0) {
while (list($top_branch, $top_children) = each($array)) {
if ($top_branch == "config") {
$config_array = $top_children;
}elseif ($top_branch == "fields") {
$fields_array = $top_children;
}
}
}
$i = 0;
if (sizeof($fields_array) > 0) {
while (list($field_name, $field_array) = each($fields_array)) {
if ($i == 0) {
if (!isset($config_array["no_form_tag"])) {
print "<tr style='display:none;'><td><form method='post' autocomplete='off' action='" . ((isset($config_array["post_to"])) ? $config_array["post_to"] : basename($_SERVER["PHP_SELF"])) . "'" . ((isset($config_array["form_name"])) ? " name='" . $config_array["form_name"] . "'" : "") . ((isset($config_array["enctype"])) ? " enctype='" . $config_array["enctype"] . "'" : "") . "></td></tr>\n";
}
}
if ($field_array["method"] == "hidden") {
form_hidden_box($field_name, $field_array["value"], ((isset($field_array["default"])) ? $field_array["default"] : ""));
}elseif ($field_array["method"] == "hidden_zero") {
form_hidden_box($field_name, $field_array["value"], "0");
}elseif ($field_array["method"] == "spacer") {
print "<tr id='row_$field_name' bgcolor='#" . $colors["header_panel"] . "'><td colspan='2' class='tableSubHeaderColumn'>" . $field_array["friendly_name"] . "</td></tr>\n";
}else{
if (isset($config_array["force_row_color"])) {
print "<tr id='row_$field_name' bgcolor='#" . $config_array["force_row_color"] . "'>";
}else{
form_alternate_row_color($colors["form_alternate1"], $colors["form_alternate2"], $i, 'row_' . $field_name);
}
print "<td width='" . ((isset($config_array["left_column_width"])) ? $config_array["left_column_width"] : "50%") . "'>\n<font class='textEditTitle'>" . $field_array["friendly_name"] . "</font><br>\n";
if (isset($field_array["sub_checkbox"])) {
form_checkbox($field_array["sub_checkbox"]["name"],
$field_array["sub_checkbox"]["value"],
$field_array["sub_checkbox"]["friendly_name"],
((isset($field_array["sub_checkbox"]["default"])) ? $field_array["sub_checkbox"]["default"] : ""),
((isset($field_array["sub_checkbox"]["form_id"])) ? $field_array["sub_checkbox"]["form_id"] : ""),
((isset($field_array["sub_checkbox"]["class"])) ? $field_array["sub_checkbox"]["class"] : ""),
((isset($field_array["sub_checkbox"]["on_change"])) ? $field_array["sub_checkbox"]["on_change"] : ""));
}
print ((isset($field_array["description"])) ? $field_array["description"] : "") . "</td>\n";
print "<td>";
draw_edit_control($field_name, $field_array);
print "</td>\n</tr>\n";
}
$i++;
}
}
}
不过改方法中也是只是输出而已,如果想添加的话,还得去一个专门用来存数据的array中,添加数据,数据在cacti目录下include/global_form.php中,部分内容大致如下:
/* file: user_admin.php, action: user_edit (host) */
$fields_user_user_edit_host = array(
"username" => array(
"method" => "textbox",
"friendly_name" => "User Name",
"description" => "The login name for this user.",
"value" => "|arg1:username|",
"max_length" => "255"
),
"full_name" => array(
"method" => "textbox",
"friendly_name" => "Full Name",
"description" => "A more descriptive name for this user, that can include spaces or special characters.",
"value" => "|arg1:full_name|",
"max_length" => "255"
),
"password" => array(
"method" => "textbox_password",
"friendly_name" => "Password",
"description" => "Enter the password for this user twice. Remember that passwords are case sensitive!",
"value" => "",
"max_length" => "255"
),
"enabled" => array(
"method" => "checkbox",
"friendly_name" => "Enabled",
"description" => "Determines if user is able to login.",
"value" => "|arg1:enabled|",
"default" => ""
),
"grp1" => array(
"friendly_name" => "Account Options",
"method" => "checkbox_group",
"description" => "Set any user account-specific options here.",
"items" => array(
"must_change_password" => array(
"value" => "|arg1:must_change_password|",
"friendly_name" => "User Must Change Password at Next Login",
"form_id" => "|arg1:id|",
"default" => ""
),
"graph_settings" => array(
"value" => "|arg1:graph_settings|",
"friendly_name" => "Allow this User to Keep Custom Graph Settings",
"form_id" => "|arg1:id|",
"default" => "on"
)
)
),
"grp2" => array(
"friendly_name" => "Graph Options",
"method" => "checkbox_group",
"description" => "Set any graph-specific options here.",
"items" => array(
"show_tree" => array(
"value" => "|arg1:show_tree|",
"friendly_name" => "User Has Rights to Tree View",
"form_id" => "|arg1:id|",
"default" => "on"
),
"show_list" => array(
"value" => "|arg1:show_list|",
"friendly_name" => "User Has Rights to List View",
"form_id" => "|arg1:id|",
"default" => "on"
),
"show_preview" => array(
"value" => "|arg1:show_preview|",
"friendly_name" => "User Has Rights to Preview View",
"form_id" => "|arg1:id|",
"default" => "on"
)
)
),
"login_opts" => array(
"friendly_name" => "Login Options",
"method" => "radio",
"default" => "1",
"description" => "What to do when this user logs in.",
"value" => "|arg1:login_opts|",
"items" => array(
0 => array(
"radio_value" => "1",
"radio_caption" => "Show the page that user pointed their browser to."
),
1 => array(
"radio_value" => "2",
"radio_caption" => "Show the default console screen."
),
2 => array(
"radio_value" => "3",
"radio_caption" => "Show the default graph screen."
)
)
),
"realm" => array(
"method" => "drop_array",
"friendly_name" => "Authentication Realm",
"description" => "Only used if you have LDAP or Web Basic Authentication enabled. Changing this to an non-enabled realm will effectively disable the user.",
"value" => "|arg1:realm|",
"default" => 0,
"array" => $auth_realms,
),
"id" => array(
"method" => "hidden_zero",
"value" => "|arg1:id|"
),
"_policy_graphs" => array(
"method" => "hidden",
"default" => "2",
"value" => "|arg1:policy_graphs|"
),
"_policy_trees" => array(
"method" => "hidden",
"default" => "2",
"value" => "|arg1:policy_trees|"
),
"_policy_hosts" => array(
"method" => "hidden",
"default" => "2",
"value" => "|arg1:policy_hosts|"
),
"_policy_graph_templates" => array(
"method" => "hidden",
"default" => "2",
"value" => "|arg1:policy_graph_templates|"
),
"save_component_user" => array(
"method" => "hidden",
"value" => "1"
)
);
通过在array中添加元素,则可以在user_admin页面上面添加内容~