Skip to content

Commit

Permalink
Added the beginnings of "registration" functionality for token based …
Browse files Browse the repository at this point in the history
…surveys. Still need to modify admin files and database.

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/trunk/unstable@1003 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
jcleeland committed Apr 25, 2004
1 parent 11b5e8c commit 86814ac
Show file tree
Hide file tree
Showing 2 changed files with 267 additions and 1 deletion.
60 changes: 59 additions & 1 deletion index.php
Expand Up @@ -50,6 +50,8 @@
//CHECK FOR REQUIRED INFORMATION (sid)
if (!$sid)
{
$langfilename="$publicdir/lang/$defaultlang.lang.php";
require($langfilename);
//A nice exit
sendcacheheaders();
echo "<html>\n";
Expand Down Expand Up @@ -119,7 +121,7 @@
$langfilename="$langdir/{$thissurvey['language']}.lang.php";
//Use the default language file if the $thissurvey['language'] file doesn't exist
if (!is_file($langfilename)) {$langfilename="$langdir/$defaultlang.lang.php";}
require($langfilename);
require_once($langfilename);

//MAKE SURE SURVEY HASN'T EXPIRED
if ($thissurvey['expiry'] < date("Y-m-d") && $thissurvey['expiry'] != "0000-00-00")
Expand Down Expand Up @@ -806,6 +808,7 @@ function buildsurveysession()
global $thissurvey;
global $tokensexist, $thistpl;
global $sid, $dbprefix;
global $register_errormsg;

//This function builds all the required session variables when a survey is first started.
//It is called from each various format script (ie: group.php, question.php, survey.php)
Expand All @@ -825,6 +828,60 @@ function buildsurveysession()
{
echo templatereplace($op);
}
if (isset($thissurevey) && $thissurvey('allowregister') == "Y")
{
define ("_RG_REGISTER1", "You must be registered to complete this survey");
define ("_RG_REGISTER2", "You may register for this survey if you wish to take part.<br />\n"
."Enter your details below, and an email containing the link to "
."participate in this survey will be sent immediately.");
define ("_RG_EMAIL", "Email Address");
define ("_RG_FIRSTNAME", "First Name");
define ("_RG_LASTNAME", "Last Name");
define ("_RG_ATTRIBUTE1", "Attribute 1");
define ("_RG_ATTRIBUTE2", "Attribute 2");

?>
<center><br />
<?php if (isset($register_errormsg))
{
echo "<font color='red'>$register_errormsg</font><br /><br />\n";
}
?>
<?php echo _RG_REGISTER1 ?><br /><br />
<?php echo _RG_REGISTER2 ?><br />&nbsp;
<table align='center'>
<form method='post' action='register.php'>
<input type='hidden' name='sid' value='<?php echo $sid ?>'>
<tr><td align='right'>
<?php echo _RG_FIRSTNAME ?>:</td>
<td align='left'><input class='text' type='text' name='register_firstname'<?php
if (isset($_POST['register_firstname'])) {echo " value='".returnglobal('register_firstname')."'";}
?>></td></tr>
<tr><td align='right'><?php echo _RG_LASTNAME ?>:</td>
<td align='left'><input class='text' type='text' name='register_lastname'<?php
if (isset($_POST['register_lastname'])) {echo " value='".returnglobal('register_lastname')."'";}
?>></td></tr>
<tr><td align='right'><?php echo _RG_EMAIL ?>:</td>
<td align='left'><input class='text' type='text' name='register_email'<?php
if (isset($_POST['register_email'])) {echo " value='".returnglobal('register_email')."'";}
?>></td></tr>
<tr><td align='right'><?php echo _RG_ATTRIBUTE1 ?>:</td>
<td align='left'><input class='text' type='text' name='register_attribute1'<?php
if (isset($_POST['register_attribute1'])) {echo " value='".returnglobal('register_attribute1')."'";}
?>></td></tr>
<tr><td align='right'><?php echo _RG_ATTRIBUTE2 ?>:</td>
<td align='left'><input class='text' type='text' name='register_attribute2'<?php
if (isset($_POST['register_attribute2'])) {echo " value='".returnglobal('register_attribute2')."'";}
?>></td></tr>
<tr><td></td><td><input class='submit' type='submit' value='<?php echo _CONTINUE_PS ?>'>
</td></tr>
</form>
</table>
<br />&nbsp;</center>
<?php
}
else
{
?>
<center><br />
<?php echo _NOTOKEN1 ?><br /><br />
Expand All @@ -842,6 +899,7 @@ function buildsurveysession()
</table>
<br />&nbsp;</center>
<?php
}
foreach(file("$thistpl/endpage.pstpl") as $op)
{
echo templatereplace($op);
Expand Down
208 changes: 208 additions & 0 deletions register.php
@@ -0,0 +1,208 @@
<?php
/*
#############################################################
# >>> PHP Surveyor #
#############################################################
# > Author: Jason Cleeland #
# > E-mail: jason@cleeland.org #
# > Mail: Box 99, Trades Hall, 54 Victoria St, #
# > CARLTON SOUTH 3053, AUSTRALIA #
# > Date: 20 February 2003 #
# #
# This set of scripts allows you to develop, publish and #
# perform data-entry on surveys. #
#############################################################
# #
# Copyright (C) 2003 Jason Cleeland #
# #
# 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., 59 Temple Place - #
# Suite 330, Boston, MA 02111-1307, USA. #
#############################################################
*/
//THESE WILL BEMOVED INTO THE LANGUAGE FILE ONCE COMPLETED
define ("_RG_INVALIDEMAIL", "The email you used is not valid. Please try again.");
define ("_RG_USEDEMAIL", "The email you used is already registered to someone else.");
define ("_RG_EMAILINVITATION", "Dear {FIRSTNAME},\n\n"
."You, or someone using your email address, have registered to\n"
."participate in an online survey titled {SURVEYNAME}.\n\n"
."To complete this survey, click on the following URL:\n\n"
."{SURVEYURL}\n\n"
."If you have any questions about this survey, or if you\n"
."did not register to participate and believe this email\n"
."is in error, please contact {ADMINNAME} at {ADMINEMAIL}.");
define ("_RG_EMAILSUBJECT", "{SURVEYNAME} Registration Confirmation");
define ("_RG_REGISTRATIONCOMPLETE", "Thank you for registering to participate in this survey.<br /><br />\n"
."An email has been sent to the address you provided with access details"
."for this survey. Please follow the link in that email to proceed.<br /><br />\n"
."Survey Administrator {ADMINNAME} ({ADMINEMAIL})");

require_once("./admin/config.php");

$sid=returnglobal('sid');

//Check that there is a SID
if (!isset($sid))
{
//You must have an SID to use this
include "index.php";
exit;
}

//Check that the email is a valid style address
if (!validate_email(returnglobal('register_email')))
{
$register_errormsg=_RG_INVALIDEMAIL;
include "index.php";
exit;
}

//Check if this email already exists in token database
$query = "SELECT email FROM {$dbprefix}tokens_$sid\n"
. "WHERE email = '".returnglobal('register_email')."'";
$result = mysql_query($query) or die ($query."<br />".mysql_error());
if (mysql_num_rows($result) > 0)
{
$register_errormsg=_RG_USEDEMAIL;
include "index.php";
exit;
}

if (phpversion() < "4.2.0")
{
srand((double)microtime()*1000000);
}
$insert = "NO";
while ($insert != "OK")
{
if (isset($THISOS) && $THISOS == "solaris")
{
$nt1=mysql_query("SELECT RAND()");
while ($row=mysql_fetch_row($nt1)) {$newtoken="R".(int)(sprintf("%09s", $row[0]*100000000));}
}
else
{
$newtoken = "R".sprintf("%09s", rand(1, 1000000000));
}
$ntquery = "SELECT * FROM {$dbprefix}tokens_$sid WHERE token='$newtoken'";
$ntresult = mysql_query($ntquery);
if (!mysql_num_rows($ntresult)) {$insert = "OK";}
}

//Insert new entry into tokens db
$query = "INSERT INTO {$dbprefix}tokens_$sid\n"
. "(`firstname`, `lastname`, `email`, `token`, `attribute_1`, `attribute_2`)\n"
. "VALUES ('".mysql_escape_string(returnglobal('register_firstname'))."',\n"
. "'".mysql_escape_string(returnglobal('register_lastname'))."',\n"
. "'".mysql_escape_string(returnglobal('register_email'))."',\n"
. "'$newtoken',\n"
. "'".mysql_escape_string(returnglobal('register_attribute1'))."',\n"
. "'".mysql_escape_string(returnglobal('register_attribute2'))."')";
$result = mysql_query($query) or die ($query."<br />".mysql_error());
$tid=mysql_insert_id();

$esquery = "SELECT * FROM {$dbprefix}surveys WHERE sid=$sid";
$esresult = mysql_query($esquery);
while ($esrow = mysql_fetch_array($esresult))
{
$surveyname = $esrow['short_title'];
$surveydescription = $esrow['description'];
$surveyadmin = $esrow['admin'];
$surveyadminemail = $esrow['adminemail'];
$surveytemplate = $esrow['template'];
$surveylanguage = $esrow['language'];
}
if (!$surveyadminemail) {$surveyadminemail=$siteadminemail; $surveyadmin=$siteadminname;}

//Get the language file
$langdir="$publicdir/lang";
$langfilename="$langdir/$surveylanguage.lang.php";
//Use the default language file if the $thissurvey['language'] file doesn't exist
if (!is_file($langfilename)) {$langfilename="$langdir/$defaultlang.lang.php";}
require_once($langfilename);


$message=_RG_EMAILINVITATION;
$message=str_replace("{ADMINNAME}", $surveyadmin, $message);
$message=str_replace("{ADMINEMAIL}", $surveyadminemail, $message);
$message=str_replace("{SURVEYNAME}", $surveyname, $message);
$message=str_replace("{SURVEYDESCRIPTION}", $surveydescription, $message);
$message=str_replace("{SURVEYURL}", "$publicurl/index.php?sid=$sid&token=$newtoken", $message);
$message=str_replace("{FIRSTNAME}", returnglobal('register_firstname'), $message);
$message=str_replace("{LASTNAME}", returnglobal('register_lastname'), $message);
$message=str_replace("{ATTRIBUTE_1}", returnglobal('register_attribute1'), $message);
$message=str_replace("{ATTRIBUTE_2}", returnglobal('register_attribute2'), $message);
$message=str_replace("\n", "\r\n", $message);

$headers = "From: $surveyadmin <$surveyadminemail>\r\n"
. "X-Mailer: $sitename Emailer (phpsurveyor.sourceforge.net)\r\n";

$subject=_RG_EMAILSUBJECT;
$subject=str_replace("{ADMINNAME}", $surveyadmin, $subject);
$subject=str_replace("{ADMINEMAIL}", $surveyadminemail, $subject);
$subject=str_replace("{SURVEYNAME}", $surveyname, $subject);
$subject=str_replace("{SURVEYDESCRIPTION}", $surveydescription, $subject);

if (mail(returnglobal('register_email'), $subject, $message, $headers))
{
$query = "UPDATE {$dbprefix}tokens_$sid\n"
."SET sent='Y' WHERE tid=$tid";
$result=mysql_query($query) or die ("$query<br />".mysql_error());
$html=_RG_REGISTRATIONCOMPLETE;
$html=str_replace("{ADMINNAME}", $surveyadmin, $html);
$html=str_replace("{ADMINEMAIL}", $surveyadminemail, $html);
$html=str_replace("{SURVEYNAME}", $surveyname, $html);
}

//PRINT COMPLETED PAGE
if (!$publicdir) {$publicdir=".";}
if (!$surveytemplate) {$thistpl="$publicdir/templates/default";} else {$thistpl="$publicdir/templates/$surveytemplate";}
if (!is_dir($thistpl)) {$thistpl="$publicdir/templates/default";}

sendcacheheaders();
echo "<html>\n";

foreach(file("$thistpl/startpage.pstpl") as $op)
{
echo templatereplace1($op);
}
foreach(file("$thistpl/survey.pstpl") as $op)
{
echo "\t".templatereplace1($op);
}
echo $html;
foreach(file("$thistpl/endpage.pstpl") as $op)
{
echo templatereplace1($op);
}

function templatereplace1($line)
{
global $surveyname, $surveydescription, $surveytemplate, $sid;
global $publicurl, $templatedir, $token;

if ($surveytemplate) {$templateurl="$publicurl/templates/$surveytemplate/";}
else {$templateurl="$publicurl/templates/default/";}

$line=str_replace("{SURVEYNAME}", $surveyname, $line);
$line=str_replace("{SURVEYDESCRIPTION}", $surveydescription, $line);
$line=str_replace("{TOKEN}", $token, $line);
$line=str_replace("{SID}", $sid, $line);
$line=str_replace("{TEMPLATEURL}", $templateurl, $line);
$line=str_replace("{PERCENTCOMPLETE}", "", $line);
return $line;
}
?>

0 comments on commit 86814ac

Please sign in to comment.