/**********************************************************************
 *
 * cgiutils.h -- general cgi utilities
 * Copyright (C) 1999  The New Zealand Digital Library Project
 *
 * A component of the Greenstone digital library software
 * from the New Zealand Digital Library Project at the
 * University of Waikato, New Zealand.
 *
 * 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 "gsdlconf.h"
#include "text_t.h"
#include "cgiargs.h"

// parse data obtained through a CGI POST request
text_t parse_post_data (text_t &content_type, text_t &raw_post_data, 
			fileupload_tmap &fileuploads, const text_t &gsdlhome);

// convert %xx and + to their appropriate equivalents
void decode_cgi_arg (text_t &argstr);
// convert &,%,+,=,space to encoded versions so that post args can be put together into a get style string
void cgi_safe_post_arg(text_t &argstr);

// split up the cgi arguments
void split_cgi_args (const cgiargsinfoclass &argsinfo, text_t argstr, 
		     cgiargsclass &args);

// url-decode selected chars of a given string
void unsafe_cgi_arg(const text_t &chars_to_decode, text_t &str);

text_t encode_commas (const text_t &intext);
text_t decode_commas (const text_t &intext);

// turns any '-' in a cgi argument into "(-)"
// set utf8 to true if input is in utf-8, otherwise expects input in unicode
text_t minus_safe (const text_t &intext, bool utf8);

// returns the encoded version of a cgi argument
// the original text should be in utf8
text_t cgi_safe_utf8 (const text_t &intext);
// the original text should be in unicode
text_t cgi_safe_unicode (const text_t &intext);

// check_save_conf_str checks the configuration string for
// the saved args and makes sure it does not conflict with
// the information about the arguments. If an error is encountered
// it will return false and the program should not produce any
// output.
bool check_save_conf_str (const text_t &saveconf, 
			  const cgiargsinfoclass &argsinfo,
			  ostream &logout);

// create_save_conf_str will create a configuration string
// based on the information in argsinfo. This method of configuration
// is not recomended as small changes can produce large changes in
// the resulting configuration string (for instance a totally different
// ordering). Only arguments which "must" be saved are included in
// the resulting string.
text_t create_save_conf_str (const cgiargsinfoclass &argsinfo,
			     ostream &logout);

// expand_compress_args will expand the compressed arguments based
// on compressconf placing the results in args if they are not
// already defined. If it encounters an error it will return false
// and output more information to logout.
bool expand_save_args (const cgiargsinfoclass &argsinfo,
		       const text_t &saveconf, 
		       cgiargsclass &args,
		       ostream &logout);

// adds the default values for those arguments which have not
// been specified
void add_default_args (const cgiargsinfoclass &argsinfo,
		       cgiargsclass &args,
		       ostream &logout);

void add_fileupload_args (const cgiargsinfoclass &argsinfo,
			  cgiargsclass &args,
			  fileupload_tmap &fileuploads,
			  ostream &logout);

// compress_save_args will compress the arguments and return
// them in compressed_args. If an error was encountered
// compressed_args will be set to to "", an error will be
// written to logout, and the function will return false.
bool compress_save_args (const cgiargsinfoclass &argsinfo,
			 const text_t &saveconf,
			 cgiargsclass &args,
			 text_t &compressed_args,
			 outconvertclass &outconvert,
			 ostream &logout);

// args_tounicode converts any arguments which are not in unicode
// to unicode using inconvert
void args_tounicode (cgiargsclass &args, inconvertclass &inconvert);

text_t gsdl_getenv (const text_t &name, text_tmap &fcgienv);

#endif
