/
RegisterController.php
288 lines (256 loc) · 11.5 KB
/
RegisterController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007-2011 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
* $Id$
*/
/**
* register
*
* @package LimeSurvey
* @copyright 2011
* @version $Id$
* @access public
*/
class RegisterController extends LSYii_Controller {
function actionAJAXRegisterForm
($surveyid)
{
Yii::app()->loadHelper('database');
Yii::app()->loadHelper('replacements');
$redata = compact(array_keys(get_defined_vars()));
$surveyid = sanitize_int($surveyid);
$row = Survey::model()->find('sid=:sid',array(':sid' => $surveyid)) or show_error("Can't find survey data");
$thistpl=getTemplatePath(validateTemplateDir($row->template));
$data['sid'] = $surveyid;
$data['startdate'] = $row->startdate;
$data['enddate'] = $row->expires;
Yii::import('application.libraries.Limesurvey_lang');
Yii::app()->lang = new Limesurvey_lang($baselang);
echo templatereplace(file_get_contents("$thistpl/register.pstpl"),array(),$redata,'register.php',false,NULL,$data);
}
/**
* register::index()
* Process register form data and take appropriate action
* @return
*/
function actionIndex($surveyid = null)
{
Yii::app()->loadHelper('database');
Yii::app()->loadHelper('replacements');
$postlang = Yii::app()->request->getPost('lang');
if($surveyid == null)
{
$surveyid = Yii::app()->request->getPost('sid');
}
if (!$surveyid)
{
Yii::app()->request->redirect(Yii::app()->baseUrl);
}
$usquery = "SELECT stg_value FROM {{settings_global}} where stg_name='SessionName'";
$usresult = dbExecuteAssoc($usquery,'',true); //Checked
if ($usresult->count() > 0)
{
$usrow = $usresult->read();
$stg_SessionName=$usrow['stg_value'];
Yii::app()->session->setSessionName("$stg_SessionName-runtime-$surveyid");
}
else
{
Yii::app()->setSessionName("LimeSurveyRuntime-$surveyid");
}
Yii::app()->session->setCookieParams(array(0, Yii::app()->getConfig('relativeurl').'/'));
// Get passed language from form, so that we dont loose this!
if (!isset($postlang) || $postlang == "" || !$postlang )
{
$baselang = Survey::model()->findByPk($surveyid)->language;
Yii::import('application.libraries.Limesurvey_lang');
Yii::app()->lang = new Limesurvey_lang($baselang);
$clang = Yii::app()->lang;
} else {
Yii::import('application.libraries.Limesurvey_lang');
Yii::app()->lang = new Limesurvey_lang($postlang);
$clang = Yii::app()->lang;
$baselang = $postlang;
}
$thissurvey=getSurveyInfo($surveyid,$baselang);
$register_errormsg = "";
// Check the security question's answer
if (function_exists("ImageCreate") && isCaptchaEnabled('registrationscreen',$thissurvey['usecaptcha']) )
{
if (!isset($_POST['loadsecurity']) ||
!isset(Yii::app()->session['secanswer']) ||
Yii::app()->request->getPost('loadsecurity') != Yii::app()->session['secanswer'])
{
$register_errormsg .= $clang->gT("The answer to the security question is incorrect.")."<br />\n";
}
}
//Check that the email is a valid style address
if (!validateEmailAddress(Yii::app()->request->getPost('register_email')))
{
$register_errormsg .= $clang->gT("The email you used is not valid. Please try again.");
}
// Check for additional fields
$attributeinsertdata = array();
foreach ($thissurvey['attributedescriptions'] as $field => $data)
{
if (empty($data['show_register']) || $data['show_register'] != 'Y')
continue;
$value = sanitize_xss_string(Yii::app()->request->getPost('register_' . $field));
if (trim($value) == '' && $data['mandatory'] == 'Y')
$register_errormsg .= sprintf($clang->gT("%s cannot be left empty"), $thissurvey['attributecaptions'][$field]);
$attributeinsertdata[$field] = $value;
}
if ($register_errormsg != "")
{
Yii::app()->request->redirect(Yii::app()->createUrl('survey/index/sid/'.$surveyid));
}
//Check if this email already exists in token database
$query = "SELECT email FROM {{tokens_$surveyid}}\n"
. "WHERE email = '".sanitize_email(Yii::app()->request->getPost('register_email'))."'";
$result = dbExecuteAssoc($query) or show_error("Unable to execute this query : \n <br/>".$query."<br />"); //Checked)
if (($result->count()) > 0)
{
$register_errormsg=$clang->gT("The email you used has already been registered.");
Yii::app()->request->redirect(Yii::app()->createUrl('survey/index/sid/'.$surveyid));
//include "index.php";
//exit;
}
$mayinsert = false;
// Get the survey settings for token length
//$this->load->model("surveys_model");
$tlresult = Survey::model()->findAllByAttributes(array("sid"=>$surveyid));
if (isset($tlresult[0])) {
$tlrow = $tlresult[0];
}
else
{
$tlrow = $tlresult;
}
$tokenlength = $tlrow['tokenlength'];
//if tokenlength is not set or there are other problems use the default value (15)
if(!isset($tokenlength) || $tokenlength == '')
{
$tokenlength = 15;
}
while ($mayinsert != true)
{
$newtoken = randomChars($tokenlength);
$ntquery = "SELECT * FROM {{tokens_$surveyid}} WHERE token='$newtoken'";
$ntresult = dbExecuteAssoc($ntquery); //Checked
if (!$ntresult->count()) {$mayinsert = true;}
}
$postfirstname=sanitize_xss_string(strip_tags(Yii::app()->request->getPost('register_firstname')));
$postlastname=sanitize_xss_string(strip_tags(Yii::app()->request->getPost('register_lastname')));
$starttime = sanitize_xss_string(Yii::app()->request->getPost('startdate'));
$endtime = sanitize_xss_string(Yii::app()->request->getPost('enddate'));
/*$postattribute1=sanitize_xss_string(strip_tags(returnGlobal('register_attribute1')));
$postattribute2=sanitize_xss_string(strip_tags(returnGlobal('register_attribute2'))); */
// Insert new entry into tokens db
Tokens_dynamic::sid($thissurvey['sid']);
$token = new Tokens_dynamic;
$token->firstname = $postfirstname;
$token->lastname = $postlastname;
$token->email = Yii::app()->request->getPost('register_email');
$token->emailstatus = 'OK';
$token->token = $newtoken;
if ($starttime && $endtime)
{
$token->validfrom = $starttime;
$token->validuntil = $endtime;
}
foreach ($attributeinsertdata as $k => $v)
$token->$k = $v;
$result = $token->save();
/**
$result = $connect->Execute($query, array($postfirstname,
$postlastname,
returnGlobal('register_email'),
'OK',
$newtoken)
// $postattribute1, $postattribute2)
) or safeDie ($query."<br />".$connect->ErrorMsg()); //Checked - According to adodb docs the bound variables are quoted automatically
*/
$tid = Yii::app()->db->getLastInsertID();;
$fieldsarray["{ADMINNAME}"]=$thissurvey['adminname'];
$fieldsarray["{ADMINEMAIL}"]=$thissurvey['adminemail'];
$fieldsarray["{SURVEYNAME}"]=$thissurvey['name'];
$fieldsarray["{SURVEYDESCRIPTION}"]=$thissurvey['description'];
$fieldsarray["{FIRSTNAME}"]=$postfirstname;
$fieldsarray["{LASTNAME}"]=$postlastname;
$fieldsarray["{EXPIRY}"]=$thissurvey["expiry"];
$message=$thissurvey['email_register'];
$subject=$thissurvey['email_register_subj'];
$from = "{$thissurvey['adminname']} <{$thissurvey['adminemail']}>";
if (getEmailFormat($surveyid) == 'html')
{
$useHtmlEmail = true;
$surveylink = $this->createUrl(''.$surveyid.'/lang-'.$baselang.'/tk-'.$newtoken);
$optoutlink = $this->createUrl('optout/local/'.$surveyid.'/'.$baselang.'/'.$newtoken);
$optinlink = $this->createUrl('optin/local/'.$surveyid.'/'.$baselang.'/'.$newtoken);
$fieldsarray["{SURVEYURL}"]="<a href='$surveylink'>".$surveylink."</a>";
$fieldsarray["{OPTOUTURL}"]="<a href='$optoutlink'>".$optoutlink."</a>";
$fieldsarray["{OPTINURL}"]="<a href='$optinlink'>".$optinlink."</a>";
}
else
{
$useHtmlEmail = false;
$fieldsarray["{SURVEYURL}"]= $this->createUrl(''.$surveyid.'/lang-'.$baselang.'/tk-'.$newtoken);
$fieldsarray["{OPTOUTURL}"]= $this->createUrl('optout/local/'.$surveyid.'/'.$baselang.'/'.$newtoken);
$fieldsarray["{OPTINURL}"]= $this->createUrl('optin/local/'.$surveyid.'/'.$baselang.'/'.$newtoken);
}
$message=ReplaceFields($message, $fieldsarray);
$subject=ReplaceFields($subject, $fieldsarray);
$html = ""; //Set variable
$sitename = Yii::app()->getConfig('sitename');
if (SendEmailMessage($message, $subject, Yii::app()->request->getPost('register_email'), $from, $sitename,$useHtmlEmail,getBounceEmail($surveyid)))
{
// TLR change to put date into sent
$today = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig('timeadjust'));
$query = "UPDATE {{tokens_$surveyid}}\n"
."SET sent='$today' WHERE tid=$tid";
$result=dbExecuteAssoc($query) or show_error("Unable to execute this query : $query<br />"); //Checked
$html="<center>".$clang->gT("Thank you for registering to participate in this survey.")."<br /><br />\n".$clang->gT("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".$clang->gT("Survey administrator")." {ADMINNAME} ({ADMINEMAIL})";
$html=ReplaceFields($html, $fieldsarray);
$html .= "<br /><br /></center>\n";
}
else
{
$html="Email Error";
}
//PRINT COMPLETED PAGE
if (!$thissurvey['template'])
{
$thistpl=getTemplatePath(validateTemplateDir('default'));
}
else
{
$thistpl=getTemplatePath(validateTemplateDir($thissurvey['template']));
}
sendCacheHeaders();
doHeader();
Yii::app()->lang = $clang;
foreach(file("$thistpl/startpage.pstpl") as $op)
{
echo templatereplace($op);
}
foreach(file("$thistpl/survey.pstpl") as $op)
{
echo "\t".templatereplace($op);
}
echo $html;
foreach(file("$thistpl/endpage.pstpl") as $op)
{
echo templatereplace($op);
}
doFooter();
}
}