/*	File		:	F1Forms.h
**	Author		:	drclue@drclue.net
**	Description	:	The Forms class is designed to
**		handle just about anything the CGI protocol can dish out,
**		including the elusive FILE UPLOAD protocol available in
**		HTML FORMS. Of course it handles POST,GET,IMAGEMAPS and
**		all that other rot.
**	
**		The other source files in the package provide some very
**		powerfull tools in general for applications development,
**		and exploiting them will more than justify the filesize.
**		All classes are the ones I use in day to day programming
**		here on the site, and they will therefore be constantly
**		updated as needed.
**
**		This class compiles well with cygwin for windows as well as regular unix
**		variations. At one point I'll probably make another whole 
**		#ifdef worth of stuff to make it work in dev studio
*/

#ifndef _F1FORMS_H
#define _F1FORMS_H

#include <string>
using namespace std;

#include <unistd.h>

/*	Used to access the umask function for messing with
**	file permissions for uploaded files.
*/
#include <sys/types.h>
#include <sys/stat.h>

/*	Almost all code on this site references this include file , for 
**	common delarations ,defines and includes.
*/
#include <F1types.h>
/*	Used with imagemap processing
**	to  test various geometric areas
*/
#include <F1Geometry.h>
/*	The F1String code provides a fairly robust class for managing
**	string buffers. Much of it's syntax was modeled after the JAVA and
**	Javascript functionality. It now also uses the std::string as a base class
**	eleminating the need for much of the previous code from before the std:string
**	settled in as a standard.
*/
#include <F1String.h>
/*	The F1Memory code provides a method for maintaining a dynamic 
**	array of name / value pairs. This technology is replacing the old fixed
**	buffer approach, and should result in a generally more useable class
**	in which things like the file upload decoder can insert fields more
**	easily, and you can modify the names and values on the fly too..
*/
#include <F1Memory.h>

/*	I like to keep my output all to one place , so
**	there is an output class that that's all it's for.
*/
#include <F1Out.h>

/******************************************************************
* F1FORMS.C Is a class intended for working with HTML Forms
* ===========================================================
* FORMS Supports the following types of HTML Communication
*
* GET		Get forms pass their data as an enviromental variable
*		named QUERY_STRING.
* POST		Post forms pass their data as STDIN input
* PATH_INFO	Forms pass data in an enviromental variable of the
*		name PATH_INFO. Any data that comes after the CGI
*		scripts path, but before the "?" is passed in this
*		variable.
*		EXMAPLE: To pass the string "MYFORMNAME.HTML" to the
*		forms.cgi program.
*
*			http://www.drclue.net/forms.cgi/MYFORMNAME.HTML
*
* ARGV		Information comming after the "?" is passed on the
*		command line as ARGV[1]..ARGV[2]..... etc
* IMAGEMAPS	Not only does it do normal imagemaps , but
*		It can do them with image buttons to msintain state
*
* FILE UPLOAD	Allows upload of file(s) via http.
*
*******************************************************************/
F1_pChar CHOP(F1_pChar VALUE,F1_pChar	CHARS="\r\n\0x7F"	);
F1_pChar CHOP(F1_pChar VALUE,F1_char	CHAR			);

typedef enum	{	F1CGI_EMPTY	=0,
			F1CGI_GET	=1,
			F1CGI_POST	=2,
			F1CGI_UPLOAD	=3} F1CGI_REQUEST_TYPE;
		
		
typedef class f1_form	F1_FORM					;
typedef 		F1_FORM*	F1_pFORM		;

	class f1_form :	public F1_MEMORY,  public F1_OUT
	{
protected:
	F1_bool			bCommandline		;
	F1_int			FIND_INDEX		;


	F1_STRING		obPATH_INFO		,
				obAGENT_STRING		,
				obCALLER		,
				obFTP_UPLOAD_PATH	,
				obFTP_TEMP_FILENAME	,
				obCOMMAND_LINE		,
				obCOOKIE		,
				obCOMMAND		,
				obHTTP_COOKIE		,//***	Environmental variable
				obREQUEST_METHOD	,//***	GET ,POST etc
				obCONTENT_TYPE		;
	F1CGI_REQUEST_TYPE	eMethod			;//***	Empty=0,Get=1,Post=2.upload=3

	F1_STRING		obAGENT_STANDARD	,
				obAGENT_VTEXT		,
				obAGENT_VMAJOR		,
				obAGENT_VMINOR		;
	//***	Buffer used for CGI FILE UPLOADS			
	F1_char			ZBUFF			[1000   	],
				varNAME			[128    	],
				varVALUE		[512		];
public:
	//***	Imagemaps
friend	class			f1_geometry					;

	F1_MEMORY		omENVIROMENT					,
				omCOOKIES					;
	F1_bool			is_commandline(){return bCommandline==TRUE;}
	F1_STRING		obQUERY_STRING					;
	//***	Used in imagemap processing
	F1_GEOMETRY		MAP						;
	F1_int			IFPrint			(F1_pChar		,
							 F1_pChar		,
							 F1_int = 0		);
	F1_int			ISNETSCAPE		(			);
/*
** In-Line Member Functions BEGIN
*/
	F1_pChar	AGENT		(void			){return obAGENT_STANDARD();		};
	F1_pChar	AGENTTEXT	(void			){return obAGENT_VTEXT();		};

	F1_int		AGENTVMAJOR	(void			){return obAGENT_VMAJOR.toInt();	};
	F1_int		AGENTVMINOR	(void			){return obAGENT_VMINOR.toInt();	};
			
	F1_pChar	FTP_TEMPNAME	(void			){return obFTP_TEMP_FILENAME();		};
	F1_pChar	PATH		(F1_pChar Newpath	);
	F1_pChar	PATH		(void			);
	F1_pChar	CALLER		(F1_pChar NewCaller	);
	F1_pChar	CALLER		(void			);
	F1_pChar	COMMANDLINE	(void			);
	F1_pChar	COMMANDLINE	(F1_pChar NewCommandline);
	F1_pChar	COOKIE		(void			){return obHTTP_COOKIE();		};
	F1_pMEMORY	COOKIES		(void			){return &omCOOKIES;}
	F1_pMEMORY	COOKIES		(F1_pChar	szCookies		,
					 F1_pMEMORY	pmTarget	=NULL	,
					 F1_bool	bClear		=TRUE	);
//{return &omCOOKIES;}

/*
** In-Line Member Functions END
*/
	F1_int		APPEND_FIELD	(F1_pChar	szName		,
					 F1_pChar	szValue		);
	F1_int		DECODE_FTP  	(				);
	F1_char 	SCANFOR		(F1_pSTRING	obBUFF		,
					 F1_char	cTarget		,
					 F1_int		&x		);
	F1_int		DECODE_QUERY	(				);
	F1_pChar	DECODE_URL	(F1_pChar	Ustring		);
        F1_int		varVALUEcnt	;

	F1_int		WRITE_FTP_BYTE	(F1_int		ftp_Char	);
	F1_int		READ_FTP_BYTE	(F1_int		&ftp_Char	);
	F1_int		InXfer		,InVar				;
	F1_int		FTP_STATE	,FTP_STATE_LAST			;

        FILE		*FTP_UPLOAD						;
	F1_void		boilerplate	(	F1_pChar	szFilename	);
	F1_int  	CASECMP		(	F1_pChar	szS1		,
					 	F1_pChar	szS2		,
					 	F1_int		iLen=0		);
	F1_int		INITIALIZE	(	void				);
	F1_int		operator[]	(	F1_int		iIndexno		);
	F1_int		FIELD_NO	(	void  	                	)
						{return FIND_INDEX;		};
	F1_pChar	FIELD_NAME	(	F1_int		iX=-1		);
	F1_double	VAL		(	F1_pChar	szItem =NULL	,
						F1_bool		bStealth=0	);
	F1_pChar	STR		(	F1_pChar	szItem =NULL	,
						F1_bool		bStealth=0	);
	F1_pChar	STR		(	F1_int		iX		,
						F1_bool		bStealth=0	);
	F1_pSTRING	GETSTR		(	F1_pChar	szItem		,
						F1_bool		bStealth=0	);
	F1_pSTRING	LOADSTR		(	F1_pSTRING 	obBuffer	,
						F1_pChar 	Field_Name	,
						F1_pChar 	Default=NULL	);
	F1_pChar	LOADSTR		(	F1_pChar 	szBuffer	,
						F1_pChar 	Field_Name	,
						F1_pChar 	Default=NULL	);
	F1_int		LOADINT		(	F1_pInt 	iBuffer		,
						F1_pChar	Field_Name	,
						F1_int		iDefault	);
        F1_int		FIND		(	F1_pChar	szItem =NULL	,
						F1_bool		bStealth=0	);
	F1_int		LIST		(	F1_void				);
	F1_int		LINFO		(	F1_pChar	szEnv_Var	);
virtual F1_int		DO		(	F1_void				);
	F1_int		INFO		(	F1_void				);
	F1_void		NamValRow	(	F1_pChar szName,F1_pChar szValue);
/*
** Tied output functions. If you use these , then it becomes much easier
** to embed your code in other systems later.
*/
	F1_void		HTML_START	(	F1_pChar	From		=NULL		,
						F1_pChar	ContentType	="text/html"	);
/*
**  CONSTRUCTORS / DESTRUCTOR ********
*/
	 f1_form			(	F1_void					);
	 f1_form			(	F1_pChar	QueryString		,
  						F1_bool		no_htmlstart=FALSE	);
	 f1_form			(const	F1_int		argc			,
						F1_ppChar	argv			,
						F1_bool		no_htmlstart=FALSE	);
	~f1_form();
            };

#define szdFORMSVersion "\n<font size=\"4\">\
\nForms Info V3.0b Copyright 1994,95,96,97,98,99,2000,01,02,03,04,05 Dr. Clue</font>\
\n<br />\nSource Code is available <a href=\"http://www.drclue.net/F1.cgi/HTML/HTML.html\"> HERE </a>\
\n<br />\nLast Build " __DATE__ 

#define szdFORMStyle "body, td,th { font-family:sans-serif;font-weight:bold;}\
body	{background-color:#003333;	}\
.section{text-align:center;background-color:#006699;color:#ffffcc;}\
.fname	{text-align:top;		}\
.fvalue	{text-align:top;color:#ffff00;	}"


#endif

