/
OptoutController.php
executable file
·247 lines (217 loc) · 9.79 KB
/
OptoutController.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
<?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.
*
*/
/**
* optout
*
* @package LimeSurvey
* @copyright 2011
* @access public
*/
class OptoutController extends LSYii_Controller {
public $layout = 'bare';
public $defaultAction = 'tokens';
function actiontokens()
{
$iSurveyID = Yii::app()->request->getQuery('surveyid');
$sLanguageCode = Yii::app()->request->getQuery('langcode');
$sToken = Token::sanitizeToken(Yii::app()->request->getQuery('token'));
Yii::app()->loadHelper('database');
Yii::app()->loadHelper('sanitize');
//IF there is no survey id, redirect back to the default public page
if (!$iSurveyID) {
$this->redirect(array('/'));
}
$iSurveyID = (int) $iSurveyID; //Make sure it's an integer (protect from SQL injects)
//Check that there is a SID
// Get passed language from form, so that we dont lose this!
if (!isset($sLanguageCode) || $sLanguageCode == "" || !$sLanguageCode)
{
$sBaseLanguage = Survey::model()->findByPk($iSurveyID)->language;
} else
{
$sBaseLanguage = sanitize_languagecode($sLanguageCode);
}
Yii::app()->setLanguage($sBaseLanguage);
$aSurveyInfo = getSurveyInfo($iSurveyID, $sBaseLanguage);
if ($aSurveyInfo == false || !tableExists("{{tokens_{$iSurveyID}}}")) {
throw new CHttpException(404, "The survey in which you are trying to participate does not seem to exist. It may have been deleted or the link you were given is outdated or incorrect.");
} else {
$sMessage = "<p>".gT('Please confirm that you want to opt out of this survey by clicking the button below.').'<br>'.gT("After confirmation you won't receive any invitations or reminders for this survey anymore.")."</p>";
$sMessage .= '<p><a href="'.Yii::app()->createUrl('optout/removetokens', array('surveyid'=>$iSurveyID, 'langcode'=> $sBaseLanguage, 'token' => $sToken)).'" class="btn btn-default btn-lg">'.gT("I confirm").'</a><p>';
$this->_renderHtml($sMessage, $aSurveyInfo, $iSurveyID);
}
}
/**
* This function is run when opting out of an individual token table. The other function /optout/participants
* opts the user out of ALL survey invitations from the system
*/
function actionremovetokens()
{
$iSurveyID = Yii::app()->request->getQuery('surveyid');
$sLanguageCode = Yii::app()->request->getQuery('langcode');
$sToken = Token::sanitizeToken(Yii::app()->request->getQuery('token'));
Yii::app()->loadHelper('database');
Yii::app()->loadHelper('sanitize');
if (!$iSurveyID) //IF there is no survey id, redirect back to the default public page
{
$this->redirect(array('/'));
}
$iSurveyID = (int) $iSurveyID; //Make sure it's an integer (protect from SQL injects)
//Check that there is a SID
// Get passed language from form, so that we dont lose this!
if (!isset($sLanguageCode) || $sLanguageCode == "" || !$sLanguageCode)
{
$sBaseLanguage = Survey::model()->findByPk($iSurveyID)->language;
}
else
{
$sBaseLanguage = sanitize_languagecode($sLanguageCode);
}
Yii::app()->setLanguage($sBaseLanguage);
$aSurveyInfo = getSurveyInfo($iSurveyID, $sBaseLanguage);
if ($aSurveyInfo == false || !tableExists("{{tokens_{$iSurveyID}}}")) {
throw new CHttpException(404, "The survey in which you are trying to participate does not seem to exist. It may have been deleted or the link you were given is outdated or incorrect.");
} else
{
LimeExpressionManager::singleton()->loadTokenInformation($iSurveyID, $sToken, false);
$oToken = Token::model($iSurveyID)->findByAttributes(array('token'=>$sToken));
if (!isset($oToken))
{
$sMessage = gT('You are not a participant in this survey.');
//throw new CHttpException(404, "You are not a participant in this survey.");
} else
{
if (substr($oToken->emailstatus, 0, strlen('OptOut')) !== 'OptOut')
{
$oToken->emailstatus = 'OptOut';
$oToken->save();
$sMessage = gT('You have been successfully removed from this survey.');
} else
{
$sMessage = gT('You have been already removed from this survey.');
}
}
}
$this->_renderHtml($sMessage, $aSurveyInfo, $iSurveyID);
}
/**
* This function is run when opting out of the participants system. The other function /optout/token
* opts the user out of just a single token/survey invite list
*/
function actionparticipants()
{
$iSurveyID = Yii::app()->request->getQuery('surveyid');
$sLanguageCode = Yii::app()->request->getQuery('langcode');
$sToken = Token::sanitizeToken(Yii::app()->request->getQuery('token'));
Yii::app()->loadHelper('database');
Yii::app()->loadHelper('sanitize');
if (!$iSurveyID) //IF there is no survey id, redirect back to the default public page
{
$this->redirect(array('/'));
}
$iSurveyID = (int) $iSurveyID; //Make sure it's an integer (protect from SQL injects)
//Check that there is a SID
// Get passed language from form, so that we dont lose this!
if (!isset($sLanguageCode) || $sLanguageCode == "" || !$sLanguageCode)
{
$sBaseLanguage = Survey::model()->findByPk($iSurveyID)->language;
}
else
{
$sBaseLanguage = sanitize_languagecode($sLanguageCode);
}
Yii::app()->setLanguage($sBaseLanguage);
$aSurveyInfo = getSurveyInfo($iSurveyID, $sBaseLanguage);
if ($aSurveyInfo == false || !tableExists("{{tokens_{$iSurveyID}}}"))
{
throw new CHttpException(404, "The survey in which you are trying to participate does not seem to exist. It may have been deleted or the link you were given is outdated or incorrect.");
} else
{
LimeExpressionManager::singleton()->loadTokenInformation($iSurveyID, $sToken, false);
$oToken = Token::model($iSurveyID)->findByAttributes(array('token' => $sToken));
if (!isset($oToken))
{
$sMessage = gT('You are not a participant in this survey.');
} else
{
if (substr($oToken->emailstatus, 0, strlen('OptOut')) !== 'OptOut')
{
$oToken->emailstatus = 'OptOut';
$oToken->save();
$sMessage = gT('You have been successfully removed from this survey.');
} else
{
$sMessage = gT('You have been already removed from this survey.');
}
if (!empty($oToken->participant_id))
{
//Participant also exists in central db
$oParticipant = Participant::model()->findByPk($oToken->participant_id);
if ($oParticipant->blacklisted == "Y")
{
$sMessage .= "<br />";
$sMessage .= gT("You have already been removed from the central participants list for this site");
} else
{
$oParticipant->blacklisted = 'Y';
$oParticipant->save();
$sMessage .= "<br />";
$sMessage .= gT("You have been removed from the central participants list for this site");
}
}
}
}
$this->_renderHtml($sMessage, $aSurveyInfo, $iSurveyID);
}
/**
* Render something
*
* @param string $html
* @param array $aSurveyInfo
* @param int $iSurveyID
* @return void
*/
private function _renderHtml($html, $aSurveyInfo, $iSurveyID)
{
sendCacheHeaders();
doHeader();
$aSupportData = array('thissurvey'=>$aSurveyInfo);
$oTemplate = Template::model()->getInstance(null, $iSurveyID);
if ($oTemplate->cssFramework == 'bootstrap')
{
App()->bootstrap->register();
}
$thistpl = $oTemplate->pstplPath;
if ($oTemplate->cssFramework == 'bootstrap')
{
App()->bootstrap->register();
}
App()->clientScript->registerPackage('survey-template');
ob_start(function($buffer, $phase)
{
App()->getClientScript()->render($buffer);
App()->getClientScript()->reset();
return $buffer;
});
echo templatereplace(file_get_contents($thistpl.'startpage.pstpl'), array(), $aSupportData);
$aData['html'] = $html;
$aData['thistpl'] = $thistpl;
$this->renderPartial('/opt_view', $aData);
echo templatereplace(file_get_contents($thistpl.'endpage.pstpl'), array(), $aSupportData);
doFooter();
ob_flush();
}
}