/************************************************************************
 *
 * CGIUTILS.H - Header file for common CGI functions
 * Copyright (c) 1999-2002  Ethan Galstad (nagios@nagios.org)
 * Last Modified: 07-27-2002
 *
 * License:
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************/

#ifndef _CGIUTILS_H
#define _CGIUTILS_H

#include "../common/objects.h"
#include "auth.h"
#include "edata.h"


#undef USE_STATUSMAP		/* should we compile and use the statusmap CGI? */
#undef USE_STATUSWRL 		/* should we compile and use the statuswrl CGI? */
#undef USE_TRENDS		/* should we compile and use the trends CGI? */
#undef USE_HISTOGRAM            /* should we compile and use the histogram CGI? */


/**************************** CGI REFRESH RATE ******************************/

#define DEFAULT_REFRESH_RATE	60	/* 60 second refresh rate for CGIs */


/******************************* CGI NAMES **********************************/

#define STATUS_CGI		"status.cgi"
#define STATUSMAP_CGI		"statusmap.cgi"
#define STATUSWORLD_CGI	        "statuswrl.cgi"
#define COMMAND_CGI		"cmd.cgi"
#define EXTINFO_CGI		"extinfo.cgi"
#define SHOWLOG_CGI		"showlog.cgi"
#define NOTIFICATIONS_CGI	"notifications.cgi"
#define HISTORY_CGI		"history.cgi"
#define CONFIG_CGI              "config.cgi"
#define OUTAGES_CGI		"outages.cgi"
#define TRENDS_CGI		"trends.cgi"
#define AVAIL_CGI		"avail.cgi"
#define TAC_CGI			"tac.cgi"
#define STATUSWML_CGI           "statuswml.cgi"
#define TRACEROUTE_CGI		"traceroute.cgi"
#define HISTOGRAM_CGI		"histogram.cgi"
#define CHECKSANITY_CGI	   	"checksanity.cgi"
#define MINISTATUS_CGI          "ministatus.cgi"
#define SUMMARY_CGI	        "summary.cgi"


/**************************** SYLE SHEET NAMES ******************************/

#define SHOWLOG_CSS		"showlog.css"
#define STATUS_CSS		"status.css"
#define STATUSMAP_CSS		"statusmap.css"
#define COMMAND_CSS		"cmd.css"
#define EXTINFO_CSS		"extinfo.css"
#define NOTIFICATIONS_CSS	"notifications.css"
#define HISTORY_CSS		"history.css"
#define CONFIG_CSS		"config.css"
#define OUTAGES_CSS		"outages.css"
#define TRENDS_CSS		"trends.css"
#define AVAIL_CSS		"avail.css"
#define TAC_CSS			"tac.css"
#define HISTOGRAM_CSS		"histogram.css"
#define CHECKSANITY_CSS		"checksanity.css"
#define MINISTATUS_CSS          "ministatus.css"
#define SUMMARY_CSS             "summary.css"


/********************************* ICONS ************************************/

#define STATUS_ICON_WIDTH		20
#define STATUS_ICON_HEIGHT		20

#define INFO_ICON			"info.png"
#define INFO_ICON_ALT			"Informational Message"
#define START_ICON			"start.gif"
#define START_ICON_ALT			"Program Start"
#define STOP_ICON			"stop.gif"
#define STOP_ICON_ALT			"Program End"
#define RESTART_ICON			"restart.gif" 
#define RESTART_ICON_ALT		"Program Restart"
#define OK_ICON				"recovery.png" 
#define OK_ICON_ALT			"Service Ok"
#define CRITICAL_ICON			"critical.png" 
#define CRITICAL_ICON_ALT		"Service Critical"
#define WARNING_ICON			"warning.png" 
#define WARNING_ICON_ALT		"Service Warning"
#define UNKNOWN_ICON			"unknown.png" 
#define UNKNOWN_ICON_ALT		"Service Unknown"
#define NOTIFICATION_ICON		"notify.gif" 
#define NOTIFICATION_ICON_ALT		"Service Notification"
#define LOG_ROTATION_ICON		"logrotate.png" 
#define LOG_ROTATION_ICON_ALT		"Log Rotation"
#define EXTERNAL_COMMAND_ICON		"command.png" 
#define EXTERNAL_COMMAND_ICON_ALT	"External Command"

#define STATUS_DETAIL_ICON		"status2.gif"
#define STATUS_OVERVIEW_ICON		"status.gif"
#define STATUSMAP_ICON                  "status3.gif"
#define STATUSWORLD_ICON                "status4.gif"
#define EXTINFO_ICON                	"extinfo.gif"
#define HISTORY_ICON			"history.gif"
#define CONTACTGROUP_ICON		"contactgroup.gif"
#define TRENDS_ICON			"trends.gif"

#define DISABLED_ICON			"disabled.gif"
#define ENABLED_ICON			"enabled.gif"
#define PASSIVE_ONLY_ICON		"passiveonly.gif"
#define NOTIFICATIONS_DISABLED_ICON	"ndisabled.gif"
#define ACKNOWLEDGEMENT_ICON            "ack.gif"
#define REMOVE_ACKNOWLEDGEMENT_ICON     "noack.gif"
#define COMMENT_ICON			"comment.gif"
#define DELETE_ICON			"delete.gif"
#define DELAY_ICON			"delay.gif"
#define DOWNTIME_ICON			"downtime.gif"
#define PASSIVE_ICON			"unknown.png"
#define RIGHT_ARROW_ICON		"right.gif"
#define LEFT_ARROW_ICON			"left.gif"
#define UP_ARROW_ICON			"up.gif"
#define DOWN_ARROW_ICON			"down.gif"
#define FLAPPING_ICON			"flapping.gif"
#define SCHEDULED_DOWNTIME_ICON		"downtime.gif"
#define EMPTY_ICON			"empty.gif"

#define ACTIVE_ICON			"active.gif"
#define ACTIVE_ICON_ALT                 "Active Mode"
#define STANDBY_ICON			"standby.gif"
#define STANDBY_ICON_ALT                "Standby Mode"

#define HOST_DOWN_ICON			"critical.png"
#define HOST_DOWN_ICON_ALT		"Host Down"
#define HOST_UNREACHABLE_ICON		"critical.png"
#define HOST_UNREACHABLE_ICON_ALT	"Host Unreachable"
#define HOST_UP_ICON			"recovery.png"
#define HOST_UP_ICON_ALT		"Host Up"
#define HOST_NOTIFICATION_ICON		"notify.gif"
#define HOST_NOTIFICATION_ICON_ALT	"Host Notification"

#define SERVICE_EVENT_ICON		"serviceevent.gif"
#define SERVICE_EVENT_ICON_ALT		"Service Event Handler"
#define HOST_EVENT_ICON			"hostevent.gif"
#define HOST_EVENT_ICON_ALT		"Host Event Handler"

#define THERM_OK_IMAGE			"thermok.png"
#define THERM_WARNING_IMAGE		"thermwarn.png"
#define THERM_CRITICAL_IMAGE		"thermcrit.png"

#define CONFIGURATION_ICON		"config.gif"
#define NOTES_ICON			"notes.gif"

#define PARENT_TRAVERSAL_ICON		"parentup.gif"

#define TAC_DISABLED_ICON		"tacdisabled.png"
#define TAC_ENABLED_ICON		"tacenabled.png"

#define ZOOM1_ICON			"zoom1.gif"
#define ZOOM2_ICON			"zoom2.gif"

#define CONTEXT_HELP_ICON1		"contexthelp1.gif"
#define CONTEXT_HELP_ICON2		"contexthelp2.gif"



/************************** PLUGIN RETURN VALUES ****************************/

#define STATE_OK		0
#define STATE_WARNING		1
#define STATE_CRITICAL		2
#define STATE_UNKNOWN		3       /* changed from -1 on 02/24/2001 */


/********************* EXTENDED INFO CGI DISPLAY TYPES  *********************/

#define DISPLAY_PROCESS_INFO		0
#define DISPLAY_HOST_INFO		1
#define DISPLAY_SERVICE_INFO		2
#define DISPLAY_COMMENTS		3
#define DISPLAY_PERFORMANCE		4
#define DISPLAY_HOSTGROUP_INFO		5
#define DISPLAY_DOWNTIME		6
#define DISPLAY_SCHEDULING_QUEUE	7


/************************ COMMAND CGI COMMAND MODES *************************/

#define CMDMODE_NONE            0
#define CMDMODE_REQUEST         1
#define CMDMODE_COMMIT          2



/******************** HOST AND SERVICE NOTIFICATION TYPES ******************/

#define NOTIFICATION_ALL		0	/* all service and host notifications */
#define NOTIFICATION_SERVICE_ALL	1	/* all types of service notifications */
#define NOTIFICATION_HOST_ALL		2	/* all types of host notifications */
#define NOTIFICATION_SERVICE_WARNING	4
#define NOTIFICATION_SERVICE_UNKNOWN	8
#define NOTIFICATION_SERVICE_CRITICAL	16
#define NOTIFICATION_SERVICE_RECOVERY	32
#define NOTIFICATION_HOST_DOWN		64
#define NOTIFICATION_HOST_UNREACHABLE	128
#define NOTIFICATION_HOST_RECOVERY	256
#define NOTIFICATION_SERVICE_ACK	512
#define NOTIFICATION_HOST_ACK		1024


/********************** HOST AND SERVICE ALERT TYPES **********************/

#define HISTORY_ALL			0	/* all service and host alert */
#define HISTORY_SERVICE_ALL		1	/* all types of service alerts */
#define HISTORY_HOST_ALL		2	/* all types of host alerts */
#define HISTORY_SERVICE_WARNING		4
#define HISTORY_SERVICE_UNKNOWN		8
#define HISTORY_SERVICE_CRITICAL	16
#define HISTORY_SERVICE_RECOVERY	32
#define HISTORY_HOST_DOWN		64
#define HISTORY_HOST_UNREACHABLE	128
#define HISTORY_HOST_RECOVERY		256


/****************************** SORT TYPES  *******************************/

#define SORT_NONE			0
#define SORT_ASCENDING			1
#define SORT_DESCENDING			2


/***************************** SORT OPTIONS  ******************************/

#define SORT_NOTHING			0
#define SORT_HOSTNAME			1
#define SORT_SERVICENAME		2
#define SORT_SERVICESTATUS		3
#define SORT_LASTCHECKTIME		4
#define SORT_CURRENTATTEMPT		5
#define SORT_STATEDURATION		6
#define SORT_NEXTCHECKTIME		7
#define SORT_HOSTSTATUS                 8


/****************** HOST AND SERVICE FILTER PROPERTIES  *******************/

#define HOST_SCHEDULED_DOWNTIME		1
#define HOST_NO_SCHEDULED_DOWNTIME	2
#define HOST_STATE_ACKNOWLEDGED		4
#define HOST_STATE_UNACKNOWLEDGED	8
#define HOST_CHECKS_DISABLED		16
#define HOST_CHECKS_ENABLED		32
#define HOST_EVENT_HANDLER_DISABLED	64
#define HOST_EVENT_HANDLER_ENABLED	128
#define HOST_FLAP_DETECTION_DISABLED	256
#define HOST_FLAP_DETECTION_ENABLED	512
#define HOST_IS_FLAPPING		1024
#define HOST_IS_NOT_FLAPPING		2048
#define HOST_NOTIFICATIONS_DISABLED	4096
#define HOST_NOTIFICATIONS_ENABLED	8192

#define SERVICE_SCHEDULED_DOWNTIME	1
#define SERVICE_NO_SCHEDULED_DOWNTIME	2
#define SERVICE_STATE_ACKNOWLEDGED	4
#define SERVICE_STATE_UNACKNOWLEDGED	8
#define SERVICE_CHECKS_DISABLED		16
#define SERVICE_CHECKS_ENABLED		32
#define SERVICE_EVENT_HANDLER_DISABLED	64
#define SERVICE_EVENT_HANDLER_ENABLED	128
#define SERVICE_FLAP_DETECTION_ENABLED	256
#define SERVICE_FLAP_DETECTION_DISABLED	512
#define SERVICE_IS_FLAPPING		1024
#define SERVICE_IS_NOT_FLAPPING		2048
#define SERVICE_NOTIFICATIONS_DISABLED	4096
#define SERVICE_NOTIFICATIONS_ENABLED	8192
#define SERVICE_PASSIVE_CHECKS_DISABLED	16384
#define SERVICE_PASSIVE_CHECKS_ENABLED	32768
#define SERVICE_PASSIVE_CHECK           65536
#define SERVICE_ACTIVE_CHECK            131072


/****************************** SSI TYPES  ********************************/

#define SSI_HEADER                      0
#define SSI_FOOTER                      1



/************************ CONTEXT-SENSITIVE HELP  *************************/

#define CONTEXTHELP_STATUS_DETAIL	"A1"
#define CONTEXTHELP_STATUS_OVERVIEW	"A2"
#define CONTEXTHELP_STATUS_SUMMARY	"A3"
#define CONTEXTHELP_STATUS_GRID		"A4"
#define CONTEXTHELP_STATUS_SVCPROBLEMS	"A5"
#define CONTEXTHELP_STATUS_HOST_DETAIL  "A6"
#define CONTEXTHELP_STATUS_HOSTPROBLEMS "A7"

#define CONTEXTHELP_TAC			"B1"

#define CONTEXTHELP_MAP			"C1"

#define CONTEXTHELP_LOG			"D1"

#define CONTEXTHELP_HISTORY		"E1"

#define CONTEXTHELP_NOTIFICATIONS	"F1"

#define CONTEXTHELP_TRENDS_MENU1	"G1"
#define CONTEXTHELP_TRENDS_MENU2	"G2"
#define CONTEXTHELP_TRENDS_MENU3	"G3"
#define CONTEXTHELP_TRENDS_MENU4	"G4"
#define CONTEXTHELP_TRENDS_HOST		"G5"
#define CONTEXTHELP_TRENDS_SERVICE	"G6"

#define CONTEXTHELP_AVAIL_MENU1		"H1"
#define CONTEXTHELP_AVAIL_MENU2		"H2"
#define CONTEXTHELP_AVAIL_MENU3		"H3"
#define CONTEXTHELP_AVAIL_MENU4		"H4"
#define CONTEXTHELP_AVAIL_MENU5		"H5"
#define CONTEXTHELP_AVAIL_HOSTGROUP	"H6"
#define CONTEXTHELP_AVAIL_HOST		"H7"
#define CONTEXTHELP_AVAIL_SERVICE	"H8"

#define CONTEXTHELP_EXT_HOST		"I1"
#define CONTEXTHELP_EXT_SERVICE		"I2"
#define CONTEXTHELP_EXT_HOSTGROUP	"I3"
#define CONTEXTHELP_EXT_PROCESS		"I4"
#define CONTEXTHELP_EXT_PERFORMANCE	"I5"
#define CONTEXTHELP_EXT_COMMENTS	"I6"
#define CONTEXTHELP_EXT_DOWNTIME	"I7"
#define CONTEXTHELP_EXT_QUEUE		"I8"

#define CONTEXTHELP_CMD_INPUT		"J1"
#define CONTEXTHELP_CMD_COMMIT		"J2"

#define CONTEXTHELP_OUTAGES		"K1"

#define CONTEXTHELP_CONFIG_MENU			"L1"
#define CONTEXTHELP_CONFIG_HOSTS		"L2"
#define CONTEXTHELP_CONFIG_HOSTDEPENDENCIES	"L3"
#define CONTEXTHELP_CONFIG_HOSTESCALATIONS	"L4"
#define CONTEXTHELP_CONFIG_HOSTGROUPS		"L5"
#define CONTEXTHELP_CONFIG_HOSTGROUPESCALATIONS	"L6"
#define CONTEXTHELP_CONFIG_SERVICES		"L7"
#define CONTEXTHELP_CONFIG_SERVICEDEPENDENCIES	"L8"
#define CONTEXTHELP_CONFIG_SERVICEESCALATIONS	"L9"
#define CONTEXTHELP_CONFIG_CONTACTS		"L10"
#define CONTEXTHELP_CONFIG_CONTACTGROUPS	"L11"
#define CONTEXTHELP_CONFIG_TIMEPERIODS		"L12"
#define CONTEXTHELP_CONFIG_COMMANDS		"L13"

#define CONTEXTHELP_HISTOGRAM_MENU1	"M1"
#define CONTEXTHELP_HISTOGRAM_MENU2	"M2"
#define CONTEXTHELP_HISTOGRAM_MENU3	"M3"
#define CONTEXTHELP_HISTOGRAM_MENU4	"M4"
#define CONTEXTHELP_HISTOGRAM_HOST	"M5"
#define CONTEXTHELP_HISTOGRAM_SERVICE	"M6"

#define CONTEXTHELP_SUMMARY_MENU                   "N1"
#define CONTEXTHELP_SUMMARY_RECENT_ALERTS          "N2"
#define CONTEXTHELP_SUMMARY_ALERT_TOTALS           "N3"
#define CONTEXTHELP_SUMMARY_HOSTGROUP_ALERT_TOTALS "N4"
#define CONTEXTHELP_SUMMARY_HOST_ALERT_TOTALS      "N5"
#define CONTEXTHELP_SUMMARY_SERVICE_ALERT_TOTALS   "N6"
#define CONTEXTHELP_SUMMARY_ALERT_PRODUCERS        "N7"



/******************************** FUNCTIONS *******************************/

void reset_cgi_vars(void);
void free_cgi_vars(void);
void free_memory(void);

int read_cgi_config_file(char *);
int read_main_config_file(char *);
int read_all_object_configuration_data(char *,int);
int read_all_status_data(char *,int);


void strip(char *);                                		/* strips newlines, carriage returns, and spaces from end of buffer */
void sanitize_plugin_output(char *);                            /* strips HTML and bad characters from plugin output */
char * read_raw_line(char *,int,FILE *);              		/* reads a line of text from a file and strip()s it clean */
char * read_line(char *,int,FILE *);                  		/* reads only valid lines (no comments or blank lines) */

void get_time_string(time_t *,char *,int,int);			/* gets a date/time string */
void get_interval_time_string(int,char *,int);			/* gets a time string for an interval of time */
void get_expire_time_string(time_t *,char *,int);		/* gets a date/time string in the format used for Expire: tags*/

char * my_strtok(char *,char *);				/* replacement for strtok() function - doesn't skip multiple tokens */
char * my_strsep (char **, const char *);

char * url_encode(char *);		        		/* encodes a string in proper URL format */
char * html_encode(char *);					/* encodes a string in HTML format (for what the user sees) */

int get_nagios_process_info(void);				/* gets information about the state of the nagios process */

void get_time_breakdown(unsigned long,int *,int *,int *,int *);	/* given total seconds, get days, hours, minutes, seconds */

void get_log_archive_to_use(int,char *,int);			/* determines the name of the log archive to use */
void determine_log_rotation_times(int);
int determine_archive_to_use_from_time(time_t);

void calculate_host_state_times(char *, unsigned long *, unsigned long *, float *, unsigned long *, float *, unsigned long *, float *);
void calculate_service_state_times(char *, char *, unsigned long *, unsigned long *, float *, unsigned long *, float *, unsigned long *, float *, unsigned long *, float *);

void print_host_notes_url(hostextinfo *);
void print_service_notes_url(serviceextinfo *);

void display_info_table(char *,int,authdata *);
void display_nav_table(char *,int);

void include_ssi_files(char *,int);                             /* include user-defined SSI footers/headers */
void include_ssi_file(char *);                                  /* include user-defined SSI footer/header */

void cgi_config_file_error(char *);
void main_config_file_error(char *);
void object_data_error(void);
void status_data_error(void);

void display_context_help(char *);				/* displays context-sensitive help window */

#endif

