forked from ACSPRI/queXS
/
email.php
370 lines (301 loc) · 11.7 KB
/
email.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
* Popup screen to manage calling and hanging up and assigning outcomes to calls
*
*
* This file is part of queXS
*
* queXS 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.
*
* queXS 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 queXS; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*
* @author Adam Zammit <adam.zammit@acspri.org.au>
* @copyright Australian Consortium for Social and Political Research Incorporated (ACSPRI) 2013
* @package queXS
* @subpackage user
* @link http://www.acspri.org.au/ queXS was writen for ACSPRI
* @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License (GPL) Version 2
*
*/
/**
* Configuration file
*/
include ("config.inc.php");
/**
* Database file
*/
include ("db.inc.php");
/**
* Authentication
*/
require ("auth-interviewer.php");
/**
* XHTML functions
*/
include ("functions/functions.xhtml.php");
/**
* Operator functions
*/
include("functions/functions.operator.php");
/**
* Input functions
*/
include("functions/functions.input.php");
/**
* LimeSurvey functions
*/
include("functions/functions.limesurvey.php");
global $db;
$operator_id = get_operator_id();
$msg = "";
if (isset($_POST['email']) && ((isset($_POST['firstname']) && !empty($_POST['firstname'])) || (isset($_POST['lastname']) && !empty($_POST['lastname']))))
{
//validate email address
if (validate_email($_POST['email']))
{
$case_id = get_case_id($operator_id);
$lime_sid = get_lime_sid($case_id);
$ca = get_call_attempt($operator_id);
$token = get_token($case_id);
$email = $db->qstr($_POST['email']);
$firstname = $db->qstr($_POST['firstname']);
$lastname = $db->qstr($_POST['lastname']);
//update the limesurvey database email details
$sql = "UPDATE " . LIME_PREFIX ."tokens_{$lime_sid}
SET email = $email, firstname = $firstname, lastname = $lastname, emailstatus = 'OK'
WHERE token = '$token'";
$db->Execute($sql);
//Send email
include_once("include/limesurvey/classes/phpmailer/class.phpmailer.php");
$fieldsarray = array();
$fieldsarray["{EMAIL}"]=$_POST['email'];
$fieldsarray["{FIRSTNAME}"]=$_POST['firstname'];
$fieldsarray["{LASTNAME}"]=$_POST['lastname'];
$fieldsarray["{TOKEN}"]=$token;
$fieldsarray["{LANGUAGE}"]=DEFAULT_LOCALE;
$fieldsarray["{SID}"]=$fieldsarray["{SURVEYID}"]=$lime_sid;
//$fieldsarray["{SURVEYNAME}"]=$thissurvey["surveyls_title"];
$sql = "SELECT sivr.var,sv.val
FROM `sample_var` as sv, `sample_import_var_restrict` as sivr, `case` as c
WHERE c.case_id = $case_id
AND sv.sample_id = c.sample_id
AND sivr.var_id = sv.var_id";
$attributes = $db->GetAssoc($sql);
//Assign sample variables
foreach ($attributes as $attributefield=>$val)
{
$fieldsarray['{SAMPLE:'.strtoupper($attributefield).'}']=$val;
}
$fieldsarray["{OPTOUTURL}"]=LIME_URL . "optout.php?lang=".trim(DEFAULT_LOCALE)."&sid=$lime_sid&token={$token}";
$fieldsarray["{SURVEYURL}"]=LIME_URL . "index.php?lang=".trim(DEFAULT_LOCALE)."&sid=$lime_sid&token={$token}";
$barebone_link=$fieldsarray["{SURVEYURL}"];
$customheaders = array( '1' => "X-surveyid: ".$lime_sid, '2' => "X-tokenid: ".$fieldsarray["{TOKEN}"]);
$sql = "SELECT surveyls_email_invite_subj, surveyls_email_invite
FROM `lime_surveys_languagesettings`
WHERE `surveyls_survey_id` = $lime_sid
order by surveyls_language LIKE '" . DEFAULT_LOCALE . "' DESC";
$econtent = $db->GetRow($sql);
$modsubject = $econtent['surveyls_email_invite_subj'];
$modmessage = $econtent['surveyls_email_invite'];
foreach ( $fieldsarray as $key => $value )
{
$modsubject=str_replace($key, $value, $modsubject);
}
foreach ( $fieldsarray as $key => $value )
{
$modmessage=str_replace($key, $value, $modmessage);
}
$modsubject = str_replace("@@SURVEYURL@@", $barebone_link, $modsubject);
$modmessage = str_replace("@@SURVEYURL@@", $barebone_link, $modmessage);
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$sql = "SELECT stg_value
FROM " . LIME_PREFIX . "settings_global
WHERE stg_name = 'emailsmtpssl'";
$emailsmtpssl = $db->GetOne($sql);
if (isset($emailsmtpssl) && trim($emailsmtpssl)!=='' && $emailsmtpssl!==0)
{
if ($emailsmtpssl===1)
{
$mail->SMTPSecure = "ssl";
}
else
{
$mail->SMTPSecure = $emailsmtpssl;
}
}
$sql = "SELECT stg_value
FROM " . LIME_PREFIX . "settings_global
WHERE stg_name = 'emailmethod'";
$emailmethod = $db->GetOne($sql);
switch ($emailmethod) {
case "qmail":
$mail->IsQmail();
break;
case "smtp":
$mail->IsSMTP();
$sql = "SELECT stg_name,stg_value
FROM " . LIME_PREFIX . "settings_global";
$ec =$db->GetAssoc($sql);
if (strpos($ec['emailsmtphost'],':')>0)
{
$mail->Host = substr($ec['emailsmtphost'],0,strpos($ec['emailsmtphost'],':'));
$mail->Port = substr($ec['emailsmtphost'],strpos($ec['emailsmtphost'],':')+1);
}
else {
$mail->Host = $ec['emailsmtphost'];
}
$mail->Username =$ec['emailsmtpuser'];
$mail->Password =$ec['emailsmtppassword'];
if (trim($ec['emailsmtpuser'])!="")
{
$mail->SMTPAuth = true;
}
break;
case "sendmail":
$mail->IsSendmail();
break;
default:
//Set to the default value to rule out incorrect settings.
$emailmethod="mail";
$mail->IsMail();
}
$sql = "SELECT admin,adminemail
FROM " . LIME_PREFIX . "surveys
WHERE sid = $lime_sid";
$from = $db->GetRow($sql);
$mail->SetFrom($from['adminemail'],$from['admin']);
$mail->Sender = $from['adminemail'];
$mail->AddAddress($_POST['email']);
foreach ($customheaders as $key=>$val)
{
$mail->AddCustomHeader($val);
}
$mail->AddCustomHeader("X-Surveymailer: queXS Emailer (quexs.sourceforge.net)");
$mail->IsHTML(true);
$mail->Body = $modmessage;
$mail->AltBody = trim(strip_tags(html_entity_decode($modmessage,ENT_QUOTES,'UTF-8')));
$mail->Subject = $modsubject;
if ($mail->Send())
{
// Put call attempt id in to sent
$sql = "UPDATE ". LIME_PREFIX . "tokens_{$lime_sid}
SET sent='$ca'
WHERE token='$token'";
$db->Execute($sql);
//Add a note that sent
$sql = "INSERT INTO `case_note` (case_id,operator_id,note,datetime)
VALUES ($case_id,$operator_id,'" . TQ_("Self completion invitation sent via email to") . ": " . $_POST['email'] . "',CONVERT_TZ(NOW(),'System','UTC'))";
$db->Execute($sql);
//set to start frm the first page if the format for the respondent is not question by question
$sql = "SELECT q.lime_mode
FROM questionnaire as q, `case` as c
WHERE c.case_id = $case_id
AND q.questionnaire_id = c.questionnaire_id";
$lmode = $db->GetOne($sql);
if ($lmode != "question")
{
$sql = "UPDATE " . LIME_PREFIX ."survey_{$lime_sid}
SET lastpage = 0
WHERE token = '$token'";
$db->Execute($sql);
}
if (isset($_POST['submith']))
{
end_call($operator_id,41); //end with outcome sent
if (is_voip_enabled($operator_id))
{
include("functions/functions.voip.php");
$v = new voip();
$v->connect(VOIP_SERVER);
$v->hangup(get_extension($operator_id));
}
//disable recording
$newtext = T_("Start REC");
if (isset($_GET['interface2'])) { if (browser_ie()) $js = "js/window_ie6_interface2.js"; else $js = "js/window_interface2.js";}
else { if (browser_ie()) $js = "js/window_ie6.js"; else $js = "js/window.js"; }
if (isset($_GET['interface2']))
{
xhtml_head(T_("Invitation Email"),true,array("css/call.css"),array($js),"onload='openParent(\"endcase=endcase\");'");
}
else
{
xhtml_head(T_("Invitation Email"),true,array("css/call.css"),array($js),"onload='toggleRec(\"$newtext\",\"record.php?start=start\",\"offline\"); openParentObject(\"main-content\",\"" . get_respondentselection_url($operator_id) . "\"); parent.closePopup();'");
}
}
else if (isset($_POST['submit']))
{
xhtml_head(T_("Invitation Email"),true,array("css/call.css"),false,"onload='parent.closePopup();'");
}
xhtml_foot();
die();
}
else
{
$msg = T_("The email did not send");
}
}
else
{
$msg = T_("The email address is not valid");
}
}
$case_id = get_case_id($operator_id);
if (isset($_GET['interface2'])) { if (browser_ie()) $js = "js/window_ie6_interface2.js"; else $js = "js/window_interface2.js"; }
else { if (browser_ie()) $js = "js/window_ie6.js"; else $js = "js/window.js"; }
xhtml_head(T_("Invitation Email"),true,array("include/bootstrap/css/bootstrap.min.css"),array($js));
$sql = "SELECT q.self_complete
FROM questionnaire as q, `case` as c
WHERE c.case_id = $case_id
AND c.questionnaire_id = q.questionnaire_id";
$sc = $db->GetOne($sql);
if ($sc == 1)
{
$sql = "SELECT
(SELECT sv.val from sample_var as sv, `sample_import_var_restrict` as sivr WHERE sivr.var_id = sv.var_id AND sv.sample_id = c.sample_id AND sivr.type =6) as firstname,
(SELECT sv.val from sample_var as sv, `sample_import_var_restrict` as sivr WHERE sivr.var_id = sv.var_id AND sv.sample_id = c.sample_id AND sivr.type =7) as lastname,
(SELECT sv.val from sample_var as sv, `sample_import_var_restrict` as sivr WHERE sivr.var_id = sv.var_id AND sv.sample_id = c.sample_id AND sivr.type =8) as email
FROM `case` as c
WHERE c.case_id = $case_id";
$rs = $db->GetRow($sql);
print "<h4>" . T_("Email respondent for self completion") . "</h4>";
if (!empty($msg)) print "<p class='alert alert-warning'>$msg</p>";
print "<form action='?";
if (isset($_GET['interface2']))
{
print "interface2=true";
}
print "' method='post' class='form-horizontal col-md-12'>";
print "<div class='form-group '><label for='firstname' class='control-label'>" . T_("First name") . "</label>
<input type='text' value='{$rs['firstname']}' name='firstname' id='firstname' class='form-control'/>
</div>";
print "<div class='form-group '><label for='lastname' class='control-label'>" . T_("Last name") . "</label>
<input type='text' value='{$rs['lastname']}' name='lastname' id='lastname' class='form-control'/>
</div>";
print "<div class='form-group '><label for='email' class='control-label'>" . T_("Email") . "</label>
<input type='email' value='{$rs['email']}' name='email' id='email' class='form-control' required />
</div>";
if (!isset($_GET['interface2'])) {
print "<div class='form-group '><input type='submit' class='btn btn-primary' value=\"" . T_("Send invitation") . "\" name='submit' id='submit'/></div>";
}
print "<div class='form-group '><input type='submit' class='btn btn-primary' value=\"" . T_("Send invitation and Hang up") . "\" name='submith' id='submith'/>
<div class='col-md-6 pull-right'><a class='btn btn-default pull-right' href='javascript:parent.closePopup();'>".T_("Cancel")."</a></div><div class='clearfix'></div>
</div></form>";
}
else
{
print "<p>" . T_("Self completion email not available for this questionnaire") . "</p>";
}
xhtml_foot();
?>