// Record details about who the viewer of a web page is // // (C) M.A.Smith University of Brighton // // Permission is granted to use this code // provided this declaration and copyright notice remains intact. // // 26 August 1995 // Updated for latest C++ 01-08-03 //#include "t99_type.h" #include #include #include #include #include #include #include #include "parse.h" #include "parse.cpp" #include "mas_cvo.cpp" void write_log_entry( char[], char[] ); void cgi_var_output(); char* getenv_n( char [] ); void gif_output( char [] ); // Parameters to the CGI program // passed in the QUERY_STRING environment variable // // file=mame - Names the file to which log information is written // page=name - The name of the page that will be written to the log // img=name - Names the image file which is returned // - If no name or unreadable an image of a . is returned // - If this parameter is omitted then a web page of CGI // - variables is returned to help in debuging // Remember all files name must be absolute or relative to the // directory in which the CGI script is run int main() { char *query_str = getenv("QUERY_STRING"); Parse list( query_str == 0 ? (char*)"file=mas&page=Test&" : query_str ); if ( list.get_item( "file" ) != NULL ) { write_log_entry( list.get_item_n( "file", 1, true ), list.get_item_n( "page" ) ); } if ( list.get_item( "img" ) != NULL ) { gif_output( list.get_item_n("img", 1, true) ); } else { html("Content-type: text/html"); html(""); cgi_var_output(); // debug option } return 0; } bool valid ( char book[] ) { std::ifstream inf( book ); bool res = false; if ( !inf.fail() ) { char mes[] = "12345678901234567890"; inf.read( mes, 20 ); if ( strncmp( mes, "Record-Security************", 20 ) == 0 ) { res = true; } else { //html("Recording failed" ); //html_( "Security marker: " ); html_( mes ); //html( " Not valid"); } inf.close(); } else { //html("Canot open recording file" ); } return res; } void write_log_entry( char file[], char page[] ) { //cout << "File = [" << file << "] page = [" << page << "]" << "\n"; //cout.flush(); if ( valid( file ) ) { std::ofstream inf( file, std::ios::app ); //inf.open( file, std::ios::out ); if ( !inf.fail() ) { time_t t; time( &t); char *str = ctime( &t ); str[24] = '\0'; inf << setiosflags( std::ios::left ); inf << std::setw(24) << str << " " << std::setw(10) << (page!=NULL? page : (char*)"Unknown" ) << " " << std::setw(18) << getenv_n("REMOTE_ADDR") << " " << std::setw(20) << getenv_n("REMOTE_HOST") << "\n"; // std::setw(20) << getenv_n("REMOTE_USER") << "\n"; } } } void gif_output( char gif[] ) { char square [] = { 'G', 'I', 'F', '8', '9', 'a', 0002, 0000, 0002, 0000, 0263, 0000, 0000, 0000, 0000, 0000, 0277, 0000, 0000, 0000, 0277, 0000, 0277, 0277, 0000, 0000, 0000, 0277, 0277, 0000, 0277, 0000, 0277, 0277, 0300, 0300, 0300, 0200, 0200, 0200, 0377, 0000, 0000, 0000, 0377, 0000, 0377, 0377, 0000, 0000, 0000, 0377, 0377, 0000, 0377, 0000, 0377, 0377, 0377, 0377, 0377, 0054, 0000, 0000, 0000, 0000, 0002, 0000, 0002, 0000, 0100, 0004, 0003, 0020, 0200, 0010, 0000, 0073 }; html("Content-type: image/gif"); html(""); if ( gif[0] != '\0' ) // Output gif file { std::ifstream in( gif ); if ( !in.fail() ) // Can read { char c; in.read( &c, 1 ); while ( !in.eof() ) { std::cout.write( &c, 1 ); in.read( &c, 1 ); } return; // Finish } } std::cout.write( square, (int) sizeof( square ) ); }