#! /usr/local/bin/perl
################################################################################
$VERSION = "survey.pl v1.1"; # Apr 06-96     Dale Bewley     <dale@bewley.net>
# v.9 01-23-96, v1.0 02-23-96, v1.01 03-18-96, v1.02 03-26-96
#------------------------------------------------------------------------------#
# This script and others found at http://www.bewley.net/perl/
#
# Survey. Gathers comments on any number of pages using one standard
# survey or a unique survey for each page.
# Designed to be inobtrusive, it knows the page a user referenced it 
# from and will return them to that page once they submit the form.
#
# Copyright (C) 1996 Dale Bewley <dale@bewley.net>
#
# 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.
#
# Changes in this ver:
#	see http://www.bewley.net/perl/survey.html for up to date docs.
#
# Todo:
#	o Read survey from external URL to make more portable?
#	o Add file locking.
#	o Need to work on output format. Particularly if using external surveys
#	  Currently output is hard coded. This is next on the list.
#
# Notes:
#	Since I am avoiding open ended input (allowing check boxes only)
#	there are no checks for malicious input yet.
#
#	If you would like to modify this to work with your site start by 
#	running survey.pl and saving the form that it outputs for you. 
#	 
#		survey.pl > survey.html
#
#	The names in the form are hard coded in survey.pl as is the form of 
#	course. Modify the form to your needs and make sure you don't break 
#	any variables in survey.pl. You will also have to work on the output
#	format if you change the survey. It is modeled closely after the
#	hard coded survey below.
#
#	See the comments in printSurvey for notes on creating your
#	survey form.
# 
##############################################################################


#- User Configurable Variables ----------------------------------------------#
$SURVEY_FORM = "/sparcus/users/dbewley/www/survey.html";#default survey 
$OUTFILE = "/home/stu/d/dbewley/www/sparcus/survey-out.html";#default output

#- Survey Mappings ----------------------------------------------------------#
#
# I'm trying to decide on a format that will be easy to configure for even a
# perl novice. Entries are of the format 
#	"partial URL|/path/to/survey/form.html",
# The quotes and the comma is necessary. No spaces around the | character.
# An entry should be continuous and not span lines!
# The URL is passed into a regex so make it SPECIFIC if you have similar URLs
#
@SURVEY_MAP=(
	"dbewley/perl|/sparcus/users/dbewley/www/survey.html",
	"dbewley/test|/sparcus/users/dbewley/www/survey.html",
	"engr/entrance|/sparcus/users/dbewley/www/entrance-survey.html",
	"dbewley/work/test|/sparcus/users/dbewley/www/test-survey.html",
	);
#----------------------------------------------------------------------------#

#- Main Program -------------------------------------------------------------#
($sec, $min, $hour, $mday, $mon, $year) = localtime($^T);
$mon++; 

&determineSurveyForm; # what survey will user take?

&getInput; # what did user have to say?

#(&oldPrintSurvey && exit) if (!%FORM); # this will use hard coded survey

(&printSurvey && exit) if (!%FORM); # user didn't say anything yet

&oldLogSurvey; # IF we made it this far we have a completed survey. write log
#&logSurvey; # IF we made it this far we have a completed survey. write log


#- Determine Which Survey to Print ------------------------------------------#
sub determineSurveyForm {
	# use @SURVEY_MAP to pick associated survey for accessed URL. 

	foreach (@SURVEY_MAP) {
		($url, $survey_form) = split(/\|/,$_);
		$SURVEYS{$url}=$survey_form;
	}

	foreach $url (keys %SURVEYS) {
		# assuming referer is passed!
		($ENV{'HTTP_REFERER'} =~ /$url/)&&($SURVEY_FORM=$SURVEYS{$url});
	}
#	return $SURVEY_FORM; # this default is set in user config variables
}

#- Get Input ----------------------------------------------------------------#
sub getInput {
	# assumes method=post
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	@pairs = split(/&/, $buffer);

	foreach $pair (@pairs) {
		($name, $value) = split(/=/, $pair);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		$FORM{$name} = $value;
	}
}

#- Print survey results to file ---------------------------------------------#
sub logSurvey {
	# yet to write this. this will eliminate hard coding in oldLogSurvey
	# and finally enable you to use this program with an arbitrary survey,
	# theoretically. :)
}

#- Print survey results to file ---------------------------------------------#
sub oldLogSurvey {
	# may output to different format at some time

	# print survey results to a file.
	open (RESULTS, ">>$OUTFILE") ||
		die "Content-type: text/html\n\nCan't open $OUTFILE.\n";
	# Output now goes to $OUTFILE
	local($oldfh) = select(RESULTS); 
	if ($ENV{'HTTP_REFERER'}) {
		print "<P><B>Commenting from:</B>",
		 " <A HREF=$FORM{'httpReferer'}>$FORM{'httpReferer'}</A><BR>\n";
	} else {
		print "<B>User agent,</B> $ENV{'HTTP_USER_AGENT'},",
			" did not report referring page.<BR>\n";
	}

	print "<B>User host IP:</B> $ENV{'REMOTE_ADDR'}<BR>\n";
	print "<B>Browser:</B> ",
		"$ENV{'HTTP_USER_AGENT'}<BR>\n" if $ENV{'HTTP_REFERER'};
	printf ("<B>Time:</B> %02d/%02d/%02d %02d:%02d:%02d<BR>\n", 
		$mday, $mon, $year, $hour, $min, $sec);

	if ($FORM{'pageWasHelpful'} =~ /yes/i) {
		print "<P>Page <B>was</B> helpful.<BR>\n"; 
	} else {
		print "<P>Page was <B>not</B> helpful.<BR>\n"; 
	} 
	if ($FORM{'foundIt'} =~ /yes/i) {
		print "I <B>did</B> find what I was looking for.<BR>\n"; 
	} else {
		print "I did <B>not</B> find what I was looking for.<BR>\n"; 
	} 
	print "<TABLE>\n";
	print "<TR VALIGN=top>\n<TD>\n<DL>\n";
	print "<DT><B>I liked:</B>\n";
	print " <DD>The scripts.\n" if ($FORM{'likedScripts'}); 
	print " <DD>The documentation.\n" if ($FORM{'likedDocumentation'}); 
	print " <DD>The graphics.\n" if ($FORM{'likedGraphics'}); 
	print " <DD>The navigation.\n" if ($FORM{'likedNavigation'});
	print "</TD>";

	print "<TD>\n<DT><B>You can improve:</B>\n";
	print " <DD>The scripts.\n" if ($FORM{'improveScripts'}); 
	print " <DD>The documentation.\n" if ($FORM{'improveDocumentation'}); 
	print " <DD>The graphics.\n" if ($FORM{'improveGraphics'}); 
	print " <DD>The navigation.\n" if ($FORM{'improveNavigation'});
	print "</DL>\n";
	print "</TD>\n</TR>\n</TABLE>\n";

	print "<HR>\n";

	close RESULTS;
	select($oldfh); # Output now goes to browser 

	# send user back to calling page.
	print "Content-type: text/html\n";
	print "Location: $FORM{'httpReferer'}\n";
	print "\n";
	print "<P>Return to: <A HREF=\"$FORM{'httpReferer'}\">$FORM{'httpReferer'}</A>\n\n";
}



#- Print External Survey Form -----------------------------------------------#
sub printSurvey {
	# got $SURVEY_FORM from determineSurveyForm
	print "Content-type: text/html\n\n";
	open (SURVEY, "<$SURVEY_FORM") || 
		die "Can't open $SURVEY_FORM for input. Stopped,";

	while (<SURVEY>) {
		# The string HTTP_REFERER in your survey.html will be replaced
		# by the page survey.pl was accessed from.
		#
		# You definitely need this exact line in your survey.html:
		#  <INPUT TYPE="hidden" NAME="httpReferer" VALUE="HTTP_REFERER">
		# You may want this line in your survey.html:
		#  <B>Commenting from:</B> HTTP_REFERER<BR>

		$_ =~ s/HTTP_REFERER/$ENV{'HTTP_REFERER'}/o;
		print;
	}
	return "1";
}


#- Print Survey Form --------------------------------------------------------#
sub oldPrintSurvey {
	# I'm leaving this hard coded survey as documentation on how to set
	# yours up. You will want to create your own survey and describe it
	# in @SURVEY_MAP

	print "Content-type: text/html\n\n";
print <<EOF_FORM;
<HEAD>
<TITLE>Dale Bewley's Five Second Survey</TITLE>
</HEAD>
<BODY>
<H1>Dale Bewley's Five Second Survey</H1>
<HR>
<FORM METHOD=POST ACTION="/cgi-bin/cgiwrap/~dbewley/survey.pl">
<INPUT TYPE="hidden" VALUE="$ENV{'HTTP_REFERER'}" NAME="httpReferer">
<B>Commenting from:</B> $ENV{'HTTP_REFERER'}<BR>

<TABLE CELLPADDING=5>
<TR>
<TD>
<DL>
 <P><DT><B>Did you find this page helpful?</B>
	<DD><INPUT TYPE=RADIO NAME="pageWasHelpful" VALUE="yes">yes
	<INPUT TYPE=RADIO NAME="pageWasHelpful" VALUE="no">no
</DL>
</TD>
<TD>
<DL>
 <P><DT><B>Did you find what you were looking for?</B>
	<DD><INPUT TYPE=RADIO NAME="foundIt" VALUE="yes">yes
	<INPUT TYPE=RADIO NAME="foundIt" VALUE="no">no
</DL>
</TD>
<TD>
</TR>
<TR>
<TD>
 <DL>
 <P><DT><B>What did you like?</B>
	<DD><INPUT TYPE=CHECKBOX NAME="likedScripts" VALUE="yes"> Scripts
	<DD><INPUT TYPE=CHECKBOX NAME="likedDocumentation" VALUE="yes"> Documentation
	<DD><INPUT TYPE=CHECKBOX NAME="likedGraphics" VALUE="yes"> Graphics
	<DD><INPUT TYPE=CHECKBOX NAME="likedNavigation" VALUE="yes"> Ease of Navigation
 </DL>
 </TD>
 <TD>
 <DL>
 <P><DT><B>What can I improve?</B>
	<DD><INPUT TYPE=CHECKBOX NAME="improveScripts" VALUE="yes"> Scripts
	<DD><INPUT TYPE=CHECKBOX NAME="improveDocumentation" VALUE="yes"> Documentation
	<DD><INPUT TYPE=CHECKBOX NAME="improveGraphics" VALUE="yes"> Graphics
	<DD><INPUT TYPE=CHECKBOX NAME="improveNavigation" VALUE="yes"> Ease of Navigation
 </DL>
 </TD>
</TR>
</TABLE>
 
<P> <INPUT TYPE=SUBMIT VALUE="Submit Survey">
 <INPUT TYPE=RESET VALUE="Clear">
</FORM>
<HR>
If you have more specific comments please <A HREF="mailto:dale\@bewley.net">email me</A>.

<HR size=4>
<FONT size=-1>
 <B>Original:</B> 22 January 1996 Dale Bewley<BR>
 <B>Updated:</B> 26 March 1996 Dale Bewley<BR>
 <B>Comments:</B> <A HREF=mailto:dale\@bewley.net>dale\@bewley.net</A>
 <P><ADDRESS>URL: http://www.bewley.net/cgi-bin/survey.pl</ADDRESS>
</FONT>
</BODY> 
EOF_FORM
}

