// CGI.h // Written by Trans - Euro I.T Ltd // (c) 1998. // This header file enables the creation of a CGI object // containing methods that enable the screen to be // formatted with HTML statement by using this classes public methods // #define CGI_VERSION "1.10" #define LAST_ITEM '\033' #include #include #include #include using namespace std; class CGI { private: #define MAX_PARAMS 256 #define MAX_BUFFER 4096 #define MAX_STR 256 #define PERC '\045' int dex, idex; char *name[MAX_PARAMS]; char *value[MAX_PARAMS]; char inbuffer[MAX_BUFFER]; char *token; char *name_ptr; char *value_ptr; char unedited[MAX_STR + 128]; char edited[MAX_STR + 128]; char query_string[MAX_BUFFER]; public: CGI() { cout << "Content-type: text/html\n\n"; cout << "\n"; this->init_arrays(); this->parse_data(); this->comment(CGI_VERSION); this->comment("Written by Trans-Euro I.T Ltd"); this->comment("(c) 1998"); } void CGI::start_html(char Author[], char Title[], char Color[]) { cout << ""; cout << "\n"; cout << "" << Title << "\n"; cout << "\n"; cout << "\n"; } void CGI::start_html_ts(char Author[], char Title[], char Color[]) { cout << ""; cout << "\n"; cout << "" << Title << "\n"; for (dex = 0; dex < strlen(Title); dex++) { cout << ""; for (idex = 0; idex <= dex; idex++) { cout << Title[idex]; } cout << "\n"; } for (dex = strlen(Title); dex >= 0; dex--) { cout << ""; for (idex = 0; idex <= dex; idex++) { cout << Title[idex]; } cout << "\n"; } cout << "" << Title << "\n"; cout << "\n"; cout << "\n"; } void CGI::keywords(char *Keywords[]) { cout << "\n"; } void CGI::description(char Desc[]) { cout << "\n"; } void CGI::end_html() { cout << "\n"; } void CGI::end_html(char Logger[]) { cout << "\n"; cout << "\n"; } void CGI::comment(char Comment[]) { cout << "\n"; } void CGI::mailto(char Email[], char Adressee[]) { cout << ""; cout << Adressee << "\n"; } void CGI::mailto(char Email[], char Image_url[], char Alt[]) { cout << ""; cout << "\"""; cout << "\n"; } void CGI::mailto(char Email[], char Image_url[], char Alt[], char Border[]) { cout << ""; cout << "\"""; cout << "\n"; } void CGI::mailto(char Email[], char Image_url[], char Alt[], char Border[], char Height[], char Width[]) { cout << ""; cout << "\"""; cout << "\n"; } void CGI::bgsound(char Source[], char loop[]) { cout << "\n"; } void CGI::ul(char *List[]) { cout << "
    \n"; for (dex = 0; strcmp(List[dex], "LAST_ITEM"); dex++) { cout << "
  • " << List[dex] << "\n"; } cout << "
\n"; } void CGI::ul(char *List[], char Type[]) { cout << "
    \n"; for (dex = 0; strcmp(List[dex], "LAST_ITEM"); dex++) { cout << "
  • " << List[dex] << "\n"; } cout << "
\n"; } void CGI::ol(char *List[], char Start[]) { cout << "
    \n"; for (dex = 0; strcmp(List[dex], "LAST_ITEM"); dex++) { cout << "
  1. " << List[dex] << "\n"; } cout << "
\n"; } void CGI::ol(char *List[], char Start[], char Type[]) { cout << "
    \n"; for (dex = 0; strcmp(List[dex], "LAST_ITEM"); dex++) { cout << "
  1. " << List[dex] << "\n"; } cout << "
\n"; } void CGI::dir(char *List_dir[]) { cout << "\n"; for (dex = 0; strcmp(List_dir[dex], "LAST_ITEM"); dex++) { cout << "
  • " << List_dir[dex] << "\n"; } cout << "
  • \n"; } void CGI::dir(char *List_dir[], char Type[]) { cout << "\n"; for (dex = 0; strcmp(List_dir[dex], "LAST_ITEM"); dex++) { cout << "
  • " << List_dir[dex] << "\n"; } cout << "\n"; } void CGI::menu(char *Menu[]) { cout << "\n"; for (dex = 0; strcmp(Menu[dex], "LAST_ITEM"); dex++) { cout << "
  • " << Menu[dex] << "\n"; } cout << "
  • \n"; } void CGI::menu(char *Menu[], char Type[]) { cout << "\n"; for (dex = 0; strcmp(Menu[dex], "LAST_ITEM"); dex++) { cout << "
  • " << Menu[dex] << "\n"; } cout << "\n"; } void CGI::dl(char *def_tag[], char *def[]) { cout << "
    \n"; for (dex = 0; strcmp(def_tag[dex], "LAST_ITEM"); dex++) { cout << "
    " << def_tag[dex] << "\n"; cout << "\t
    " << def[dex] << "\n"; } cout << "
    \n"; } void CGI::form_start(char action[], char method[]) { cout << "\n"; } void CGI::form_end() { cout << "\n"; } void CGI::text(char Name[], char size[]) { cout << "\n"; } void CGI::text(char Name[], char size[], char Maxlength[]) { cout << "\n"; } void CGI::text(char Name[], char size[], char Maxlength[], char Value[]) { cout << "\n"; } void CGI::radio(char Name[], char Value[]) { cout << "\n"; } void CGI::radio(char Name[], char Value[], char Checked[]) { cout << "\n"; } void CGI::submit() { cout << "\n"; } void CGI::submit(char Value[]) { cout << "\n"; } void CGI::submit(char Value[], char Name[]) { cout << "\n"; } void CGI::reset() { cout << "\n"; } void CGI::reset(char Value[]) { cout << "\n"; } void CGI::image(char Src[], char Name[]) { cout << "\n"; } void CGI::image(char Src[], char Name[], char Border[]) { cout << "\n"; } void CGI::image(char Src[], char Name[], char Border[], char Align[]) { cout << "\n"; } void CGI::image(char Src[], char Name[], char Border[], char Align[], char Width[], char Height[]) { cout << "\n"; } void CGI::hidden(char Name[], char Value[]) { cout << "\n"; } void CGI::textarea(char Name[], char Cols[], char Rows[]) { cout << "\n"; cout << "\n"; } void CGI::textarea(char Name[], char Cols[], char Rows[], char Wrap[]) { cout << "\n"; cout << "\n"; } void CGI::textarea(char Name[], char Cols[], char Rows[], char Wrap[], char Value[]) { cout << "\n"; cout << Value << "\n"; cout << "\n"; } void CGI::select(char Name[], char *List[], char Size[]) { cout << "\n"; } void CGI::select(char Name[], char *List[], char Size[], char Selected[]) { cout << "\n"; } void CGI::scrolling(char Name[], char *List[], char Size[]) { cout << "\n"; } char *CGI::get_value(char arg[]) { int u_ptr, e_ptr; e_ptr = 0; if (this->gvi(arg) != NULL) { strcpy(unedited, this->gvi(arg)); } else { return NULL; } for (u_ptr = 0; u_ptr <= strlen(unedited); u_ptr++) { if (unedited[u_ptr] == (char) PERC) { edited[e_ptr] = x2c( unedited[u_ptr + 1], unedited[u_ptr + 2]); u_ptr += 2; } else { edited[e_ptr] = unedited[u_ptr]; } e_ptr++; } return edited; } private: void CGI::init_arrays() { for (dex = 0; dex <= MAX_PARAMS; dex++) { name[dex] = new char[MAX_STR]; value[dex] = new char[MAX_STR]; } } void CGI::parse_data() { char invalid_method[64]; if (getenv("REQUEST_METHOD")) { if (!strcmp(getenv("REQUEST_METHOD"), "POST")) { get_post(); return; } if (!strcmp(getenv("REQUEST_METHOD"), "post")) { get_post(); return; } if (!strcmp(getenv("REQUEST_METHOD"), "GET")) { get_get(); return; } if (!strcmp(getenv("REQUEST_METHOD"), "get")) { get_get(); return; } strcpy(invalid_method, getenv("REQUEST_METHOD")); cout << invalid_method << " is not supported.\n"; exit(1); } else { cout << "Invoked from command Line\n"; } } void CGI::get_post() { for (dex = 0; !feof(stdin); dex++) { inbuffer[dex] = getc(stdin); } inbuffer[dex - 1] = '\000'; for (dex = 0; inbuffer[dex] != (char) '\000'; dex++) { if (inbuffer[dex] == (char) '\053') { inbuffer[dex] = (char) '\040'; } } this->parse_buffer(); } void CGI::parse_buffer() { dex = 0; token = strtok(inbuffer, "&"); this->break_token(token, dex); for (dex = 1; token != NULL; dex++) { token = strtok(NULL, "&"); this->break_token(token, dex); } } void CGI::break_token(char *line, int dex) { if (line == NULL) { return; } name_ptr = line; value_ptr = strchr(line, '='); *value_ptr = '\000'; value_ptr++; strcpy(name[dex], name_ptr); strcpy(value[dex], value_ptr); } void CGI::get_get() { int pdex = 0; if (getenv("QUERY_STRING")) { strcpy(query_string, getenv("QUERY_STRING")); for (pdex = 0; query_string[pdex] != (char) '\000'; pdex++) { if (query_string[pdex] == (char) '\053') { query_string[pdex] = (char) '\040'; } } } else { strcpy(query_string, '\000'); } strcpy(inbuffer, query_string); this->parse_buffer(); } char CGI::x2c(char hi, char lo) { register char digit; register int hibyte, lobyte, index; char hextable[] = "0123456789ABCDEF"; index = 0; while (hi != hextable[index]) { index++; } hibyte = index; hibyte *= 16; index = 0; while (lo != hextable[index]) { index++; } lobyte = index; hibyte = hibyte + lobyte; digit = (char) hibyte; return (digit); } char *CGI::gvi(char param[]) { int pdex; if (getenv("QUERY_STRING") == NULL) { return NULL; } pdex = 0; while (pdex <= MAX_PARAMS) { if (!strcmp(name[pdex], param)) { strcpy(unedited, value[pdex]); return unedited; } pdex++; } return NULL; } };