'.$message.''.$tag.'>'; if ($return) return $output; echo $output; } /** * Prints an error message. * * @param string The error message to be displayed * @param string Any other attributes to be set for the tag. * @param bool Whether to output the string or return it * @param string What tag to use (you could specify something else than * h3 like div or h2) * * @return string HTML code if return parameter is true. */ function print_error_message ($message, $attributes = '', $return = false, $tag = 'h3') { return print_message ($message, 'error', $attributes, $return, $tag); } /** * Prints an operation success message. * * @param string The message to be displayed * @param string Any other attributes to be set for the tag. * @param bool Whether to output the string or return it * @param string What tag to use (you could specify something else than * h3 like div or h2) * * @return string HTML code if return parameter is true. */ function print_success_message ($message, $attributes = '', $return = false, $tag = 'h3') { return print_message ($message, 'suc', $attributes, $return, $tag); } /** * Evaluates a result using empty() and then prints an error or success message * * @param mixed The results to evaluate. 0, NULL, false, '' or * array() is bad, the rest is good * @param string The string to be displayed if the result was good * @param string The string to be displayed if the result was bad * @param string Any other attributes to be set for the h3 * @param bool Whether to output the string or return it * @param string What tag to use (you could specify something else than * h3 like div or h2) * * @return string HTML code if return parameter is true. */ function print_result_message ($result, $good = '', $bad = '', $attributes = '', $return = false, $tag = 'h3') { if ($good == '' || $good === false) $good = __('Request successfully processed'); if ($bad == '' || $bad === false) $bad = __('Error processing request'); if (empty ($result)) { return print_error_message ($bad, $attributes, $return, $tag); } return print_success_message ($good, $attributes, $return, $tag); } /** * Evaluates a unix timestamp and returns a span (or whatever tag specified) * with as title the correctly formatted full timestamp and a time comparation * in the tag * * @param int Any type of timestamp really, but we prefer unixtime * @param bool Whether to output the string or return it * @param array An array with different options for this function * Key html_attr: which html attributes to add (defaults to none) * Key tag: Which html tag to use (defaults to span) * Key prominent: Overrides user preference and display "comparation" or "timestamp" * * @return string HTML code if return parameter is true. */ function print_timestamp ($unixtime, $return = false, $option = array ()) { global $config; //TODO: Add/use a javascript timer for the seconds so it automatically updates as time passes by if (isset ($option["html_attr"])) { $attributes = $option["html_attr"]; } else { $attributes = ""; } if (isset ($option["tag"])) { $tag = $option["tag"]; } else { $tag = "span"; } if (empty ($option["style"])) { $style = 'style="white-space:nowrap;"'; } else { $style = 'style="'.$option["style"].'"'; } if (!empty ($option["prominent"])) { $prominent = $option["prominent"]; } else { $prominent = $config["prominent_time"]; } if (!is_numeric ($unixtime)) { $unixtime = strtotime ($unixtime); } //prominent_time is either timestamp or comparation if ($unixtime == 0) { $title = __('Unknown').'/'.__('Never'); $data = __('Unknown'); } elseif ($prominent == "timestamp") { $title = human_time_comparation ($unixtime); $data = date ($config["date_format"], $unixtime); } else { $title = date ($config["date_format"], $unixtime); $data = human_time_comparation ($unixtime); } $output = '<'.$tag; switch ($tag) { default: //Usually tags have title attributes, so by default we add, then fall through to add attributes and data $output .= ' title="'.$title.'"'; case "h1": case "h2": case "h3": //Above tags don't have title attributes $output .= ' '.$attributes.' '.$style.'>'.$data.''.$tag.'>'; } if ($return) return $output; echo $output; } /** * Prints a username with real name, link to the user_edit page etc. * * @param string The username to render * @param bool Whether to return or print * * @return string HTML code if return parameter is true. */ function print_username ($username, $return = false) { $string = ''.get_user_fullname ($username).''; if ($return) return $string; echo $string; } /** * Print group icon within a link * * @param int Group id * @param bool Whether to return or print * @param string What path to use (relative to images/). Defaults to groups_small * * @return string HTML code if return parameter is true. */ function print_group_icon ($id_group, $return = false, $path = "groups_small", $style='', $link = true) { if($id_group > 0) $icon = (string) get_db_value ('icon', 'tgrupo', 'id_grupo', (int) $id_group); else $icon = "world"; if (empty ($icon)) { return "-"; } $output = ''; if ($link) $output = ''; $output .= ''; if ($link) $output .= ''; if (!$return) echo $output; return $output; } /** * Get the icon of an operating system. * * @param int Operating system id * @param bool Whether to also append the name of the OS after the icon * @param bool Whether to return or echo the result * * @return string HTML with icon of the OS */ function print_os_icon ($id_os, $name = true, $return = false) { $icon = (string) get_db_value ('icon_name', 'tconfig_os', 'id_os', (int) $id_os); $os_name = get_os_name ($id_os); if (empty ($icon)) { return "-"; } $output = ''; if ($name === true) { $output .= ' - '.$os_name; } if (!$return) echo $output; return $output; } /** * Prints an agent name with the correct link * * @param int Agent id * @param bool Whether to return the string or echo it too * @param int Now uses styles to accomplish this * @param string Style of name in css. * * @return string HTML with agent name and link */ function print_agent_name ($id_agent, $return = false, $cutoff = 0, $style = '') { $agent_name = (string) get_agent_name ($id_agent); $output = ''.$agent_name.''; //TODO: Add a pretty javascript (using jQuery) popup-box with agent details if ($return) return $output; echo $output; } /** * Formats a row from the alert table and returns an array usable in the table function * * @param array A valid (non empty) row from the alert table * @param bool Whether or not this is a combined alert * @param bool Whether to print the agent information with the module information * @param string Tab where the function was called from (used for urls) * * @return array A formatted array with proper html for use in $table->data (6 columns) */ function format_alert_row ($alert, $compound = false, $agent = true, $url = '') { $actionText = ""; require_once ("include/functions_alerts.php"); $isFunctionPolicies = enterprise_include_once ('include/functions_policies.php'); if ($isFunctionPolicies !== ENTERPRISE_NOT_HOOK) { if ($agent) { $index = array('policy' => 0, 'force_execution' => 1, 'agent_name' => 2, 'module_name' => 3, 'description' => 4, 'template' => 4, 'action' => 5, 'last_fired' => 6, 'status' => 7, 'validate' => 8); } else { $index = array('policy' => 0, 'force_execution' => 1, 'agent_name' => 2, 'module_name' => 2, 'description' => 3, 'template' => 3, 'action' => 4, 'last_fired' => 5, 'status' => 6, 'validate' => 7); } } else { if ($agent) { $index = array('force_execution' => 0, 'agent_name' => 1, 'module_name' => 2, 'description' => 3, 'template' => 3, 'action' => 4, 'last_fired' => 5, 'status' => 6, 'validate' => 6); } else { $index = array('force_execution' => 0, 'agent_name' => 1, 'module_name' => 1, 'description' => 2, 'template' => 2, 'action' => 3, 'last_fired' => 4, 'status' => 5, 'validate' => 6); } } if ($alert['disabled']) { $disabledHtmlStart = ''; $disabledHtmlEnd = ''; $styleDisabled = "font-style: italic; color: #aaaaaa;"; } else { $disabledHtmlStart = ''; $disabledHtmlEnd = ''; $styleDisabled = ""; } if (empty ($alert)) { if ($isFunctionPolicies !== ENTERPRISE_NOT_HOOK) return array ("", "", "", "", "", "", "", ""); else return array ("", "", "", "", "", "", ""); } // Get agent id if ($compound) { $id_agent = $alert['id_agent']; $description = $alert['description']; } else { $id_agent = get_agentmodule_agent ($alert['id_agent_module']); $template = get_alert_template ($alert['id_alert_template']); $description = safe_output($template['name']); } $data = array (); if (($isFunctionPolicies !== ENTERPRISE_NOT_HOOK) && (!$compound)) { $policyInfo = isAlertInPolicy($alert['id_agent_module'], $alert['id_alert_template'], false); if ($policyInfo === false) $data[$index['policy']] = ''; else { $img = 'images/policies.png'; $data[$index['policy']] = '' . print_image($img,true, array('title' => $policyInfo['name_policy'])) . ''; } } else if (($isFunctionPolicies !== ENTERPRISE_NOT_HOOK) && ($compound)) $data[$index['policy']] = ''; // Force alert execution $data[$index['force_execution']] = ''; if (! $compound) { if ($alert["force_execution"] == 0) { $data[$index['force_execution']] = ''; } else { $data[$index['force_execution']] = ''; } } $data[$index['agent_name']] = $disabledHtmlStart; if ($compound) { $data[$index['agent_name']] .= print_agent_name ($id_agent, true, 20, $styleDisabled); } elseif ($agent == 0) { $data[$index['module_name']] .= mb_substr (get_agentmodule_name ($alert["id_agent_module"]), 0, 20); } else { $data[$index['agent_name']] .= print_agent_name (get_agentmodule_agent ($alert["id_agent_module"]), true, 20, $styleDisabled); $data[$index['module_name']] = mb_substr (get_agentmodule_name ($alert["id_agent_module"]), 0, 20); } $data[$index['agent_name']] .= $disabledHtmlEnd; $data[$index['description']] = ''; if (! $compound) { $data[$index['template']] .= ''; $data[$index['template']] .= print_image ('images/zoom.png', true); $data[$index['template']] .= ' '; $actionDefault = get_db_value_sql("SELECT id_alert_action FROM talert_templates WHERE id = " . $alert['id_alert_template']); } else { $actionDefault = get_db_value_sql("SELECT id_alert_action FROM talert_compound_actions WHERE id_alert_compound = " . $alert['id']); } $data[$index['description']] .= $disabledHtmlStart . mb_substr (safe_input ($description), 0, 35) . $disabledHtmlEnd; $actions = get_alert_agent_module_actions ($alert['id'], false, $compound); if (!empty($actions)){ $actionText = '
require_css_file ('pandora');
// Would include include/styles/pandora.js
*
* @return bool True if the file was added. False if the file doesn't exist.
*/
function require_css_file ($name, $path = 'include/styles/') {
global $config;
$filename = $path.$name.'.css';
if (! isset ($config['css']))
$config['css'] = array ();
if (isset ($config['css'][$name]))
return true;
if (! file_exists ($filename) && ! file_exists ($config['homedir'].'/'.$filename))
return false;
$config['css'][$name] = $filename;
return true;
}
/**
* Add a javascript file to the HTML head tag.
*
* To make a javascript file available just put it in include/javascript. The
* file name should be like "name.js". The "name" would be the value
* needed to pass to this function.
* @param string Script name to add without the "jquery." prefix and the ".js"
* suffix. Example:
require_javascript_file ('pandora');
// Would include include/javascript/pandora.js
*
* @return bool True if the file was added. False if the file doesn't exist.
*/
function require_javascript_file ($name, $path = 'include/javascript/') {
global $config;
$filename = $path.$name.'.js';
if (! isset ($config['js']))
$config['js'] = array ();
if (isset ($config['js'][$name]))
return true;
/* We checks two paths because it may fails on enterprise */
if (! file_exists ($filename) && ! file_exists ($config['homedir'].'/'.$filename))
return false;
$config['js'][$name] = $filename;
return true;
}
/**
* Add a jQuery file to the HTML head tag.
*
* To make a jQuery script available just put it in include/javascript. The
* file name should be like "jquery.name.js". The "name" would be the value
* needed to pass to this function. Notice that this function does not manage
* jQuery denpendencies.
* @param string Script name to add without the "jquery." prefix and the ".js"
* suffix. Example:
require_jquery_file ('form');
// Would include include/javascript/jquery.form.js
*
* @return bool True if the file was added. False if the file doesn't exist.
*/
function require_jquery_file ($name, $path = 'include/javascript/') {
global $config;
$filename = $path.'jquery.'.$name.'.js';
if (! isset ($config['jquery']))
$config['jquery'] = array ();
if (isset ($config['jquery'][$name]))
return true;
/* We checks two paths because it may fails on enterprise */
if (! file_exists ($filename) && ! file_exists ($config['homedir'].'/'.$filename))
return false;
$config['jquery'][$name] = $filename;
return true;
}
/**
* Callback function to add stuff to the head. This allows us to add scripts
* to the header after the fact as well as extensive validation.
*
* DO NOT CALL print_f, echo, ob_start, ob_flush, ob_end functions here.
*
* To add css just put them in include/styles and then add them to the
* $config['css'] array
*
* @param string Callback will fill this with the current buffer.
* @param bitfield Callback will fill this with a bitfield (see ob_start)
*
* @return string String to return to the browser
*/
function process_page_head ($string, $bitfield) {
global $config;
if (isset ($config['ignore_callback']) && $config['ignore_callback'] == true) {
return;
}
$output = '';
if ($config["refr"] > 0) {
$query = get_url_refresh (false);
$output .= '';
}
$output .= "\n\t";
$output .= ''; echo ''; echo 'Parameter values:'; echo ''; foreach ($trace['args'] as $arg) { echo '
'; echo '- '; print_r ($arg); echo '
'; } echo '
'; print_r ($var); echo ''; return true; } /** * Prints icon of a module type * * @param int Module Type ID * @param bool Whether to return or print * * @return string An HTML string with the icon. Printed if return is false */ function print_moduletype_icon ($id_moduletype, $return = false) { global $config; $type = get_db_row ("ttipo_modulo", "id_tipo", (int) $id_moduletype, array ("descripcion", "icon")); if ($type === false) { $type = array (); $type["descripcion"] = __('Unknown type'); $type["icon"] = 'b_down.png'; } $imagepath = 'images/'.$type["icon"]; if (! file_exists ($config['homedir'].'/'.$imagepath)) $imagepath = ENTERPRISE_DIR.'/'.$imagepath; return print_image ($imagepath, $return, array ("border" => 0, "title" => $type["descripcion"])); } /** * Format a file size from bytes to a human readable meassure. * * @param int File size in bytes * @return string Bytes converted to a human readable meassure. */ function format_filesize ($bytes) { $bytes = (int) $bytes; $strs = array ('B', 'kB', 'MB', 'GB', 'TB'); if ($bytes <= 0) { return "0 ".$strs[0]; } $con = 1024; $log = (int) (log ($bytes, $con)); return format_numeric ($bytes / pow ($con, $log), 1).' '.$strs[$log]; } /** * Returns the current path to the selected image set to show the * status of agents and alerts. * * @return array An array with the image path, image width and image height. */ function get_status_images_path () { global $config; $imageset = $config["status_images_set"]; if (strpos ($imageset, ",") === false) $imageset .= ",40x18"; list ($imageset, $sizes) = preg_split ("/\,/", $imageset); if (strpos ($sizes, "x") === false) $sizes .= "x18"; list ($imagewidth, $imageheight) = preg_split ("/x/", $sizes); $imagespath = 'images/status_sets/'.$imageset; return array ($imagespath); } define ('STATUS_MODULE_OK', 'module_ok.png'); define ('STATUS_MODULE_CRITICAL', 'module_critical.png'); define ('STATUS_MODULE_WARNING', 'module_warning.png'); define ('STATUS_AGENT_CRITICAL', 'agent_critical.png'); define ('STATUS_AGENT_WARNING', 'agent_warning.png'); define ('STATUS_AGENT_DOWN', 'agent_down.png'); define ('STATUS_AGENT_OK', 'agent_ok.png'); define ('STATUS_AGENT_NO_DATA', 'agent_no_data.png'); define ('STATUS_AGENT_NO_MONITORS', 'agent_no_monitors.png'); define ('STATUS_ALERT_FIRED', 'alert_fired.png'); define ('STATUS_ALERT_NOT_FIRED', 'alert_not_fired.png'); define ('STATUS_ALERT_DISABLED', 'alert_disabled.png'); define ('STATUS_SERVER_OK', 'server_ok.png'); define ('STATUS_SERVER_DOWN', 'server_down.png'); /** * Prints an image representing a status. * * @param string * @param string * @param bool Whether to return an output string or echo now (optional, echo by default). * * @return string HTML code if return parameter is true. */ function print_status_image ($type, $title = "", $return = false) { list ($imagepath) = get_status_images_path (); $imagepath .= "/".$type; return print_image ($imagepath, $return, array ("title" => $title)); } /** * Prints a form to search agents. * * @param array Extra options for the function. To be documented. * @param array Extra and hidden filter for the agent search. */ function print_ui_agents_list ($options = false, $filter = false, $return = false) { global $config; $output = ''; $group_filter = true; $text_filter = true; $access = 'AR'; $table_heads = array (__('Name'), __('Group'), __('Status')); $table_renders = array ('view_link', 'group', 'status'); $table_align = array ('', '', 'center'); $table_size = array ('50%', '45%', '5%'); $fields = false; $show_filter_form = true; if (is_array ($options)) { if (isset ($options['group_filter'])) $group_filter = (bool) $options['group_filter']; if (isset ($options['text_filter'])) $text_filter = (bool) $options['text_filter']; if (isset ($options['access'])) $access = (string) $options['access']; if (isset ($options['table_heads'])) $table_heads = (array) $options['table_heads']; if (isset ($options['table_renders'])) $table_renders = (array) $options['table_renders']; if (isset ($options['table_align'])) $table_align = (array) $options['table_align']; if (isset ($options['table_size'])) $table_size = (array) $options['table_size']; if (isset ($options['fields'])) $fields = (array) $options['fields']; if (isset ($options['show_filter_form'])) $show_filter_form = (bool) $options['show_filter_form']; if (count ($table_renders) != count ($table_heads)) trigger_error ('Different count for table_renders and table_heads options'); unset ($options); } if ($return) return get_include_contents ($config['homedir'].'/general/ui/agents_list.php', get_defined_vars ()); include ($config['homedir'].'/general/ui/agents_list.php'); } /** * Get the content of a PHP file instead of dumping to the output. * * Picked from PHP official doc. * * @param string File name to include and get content. * @param array Extra parameters in an indexed array to be passed to the file. * * @return string Content of the file after being processed. False if the file * could not be included. */ function get_include_contents ($filename, $params = false) { ob_start (); if (is_array ($params)) { extract ($params); } $result = include ($filename); if ($result === false) { ob_end_clean (); return false; } $contents = ob_get_contents (); ob_end_clean (); return $contents; } /** * Construct and return the URL to be used in order to refresh the current page correctly. * * @param array Extra parameters to be added to the URL. It has prevalence over * GET and POST. False values will be ignored. * @param bool Whether to return the relative URL or the absolute URL. Returns * relative by default * @param bool Whether to add POST values to the URL. */ function get_url_refresh ($params = false, $relative = true, $add_post = true) { // Agent selection filters and refresh global $config; $url = ''; if (sizeof ($_REQUEST)) { //Some (old) browsers don't like the ?&key=var $url .= '?'; } if (! is_array ($params)) $params = array (); /* Avoid showing login info */ $params['pass'] = false; $params['nick'] = false; $params['unnamed'] = false; //We don't clean these variables up as they're only being passed along foreach ($_GET as $key => $value) { if (isset ($params[$key])) continue; if (strstr ($key, 'create')) continue; if (strstr ($key, 'update')) continue; if (strstr ($key, 'new')) continue; if (strstr ($key, 'delete')) continue; $url .= $key.'='.$value.'&'; } if ($add_post) { foreach ($_POST as $key => $value) { if (isset ($params[$key])) continue; if (strstr ($key, 'create')) continue; if (strstr ($key, 'update')) continue; if (strstr ($key, 'new')) continue; if (strstr ($key, 'delete')) continue; $url .= $key.'='.$value.'&'; } } foreach ($params as $key => $value) { if ($value === false) continue; $url .= $key.'='.$value.'&'; } /* Removes final & */ $pos = strrpos ($url, '&', 0); if ($pos) { $url = substr_replace ($url, '', $pos, 5); } $url = htmlspecialchars ($url); if (! $relative) { return get_full_url ($url); } return $url; } /** * Returns a full URL in Pandora. * * An example of full URL is http:/localhost/pandora_console/index.php?sec=gsetup&sec2=godmode/setup/setup * * @param string If provided, it will be added after the index.php * * @return string A full URL in Pandora. */ function get_full_url ($url = false) { global $config; $was_empty = false; if (empty ($url)) { $was_empty = true; $url = $_SERVER['REQUEST_URI']; } if ($config['https']) { //When $config["https"] is set, always force https $protocol = 'https'; $ssl = true; } elseif (isset ($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === true || $_SERVER['HTTPS'] == 'on')) { $protocol = 'https'; $ssl = true; } else { $protocol = 'http'; $ssl = false; } $fullurl = $protocol.'://' . $_SERVER['SERVER_NAME']; if ((!$ssl && $_SERVER['SERVER_PORT'] != 80) || ($ssl && $_SERVER['SERVER_PORT'] != 443)) { $fullurl .= ":".$_SERVER['SERVER_PORT']; } if (! $was_empty) { $fullurl .= $_SERVER['SCRIPT_NAME']; } return $fullurl.$url; } /** * Return a standard page header (Pandora FMS 3.1 version) * * @param string Title * @param string Icon path * @param boolean Return (false will print using a echo) * @param boolean help (Help ID to print the Help link) * @param boolean Godmode (false = operation mode). * @param string Options (HTML code for make tabs or just a brief info string * @return string Header HTML */ function print_page_header ($title, $icon = "", $return = false, $help = "", $godmode = false, $options = ""){ if (($icon == "") && ($godmode == true)){ $icon = "images/setup.png"; } if (($icon == "") && ($godmode == false)){ $icon = "images/comments.png"; } if ($godmode == true){ $type = "nomn"; $type2 = "menu_tab_frame"; } else { $type = "view"; $type2 = "menu_tab_frame_view"; } $buffer = '