-
Notifications
You must be signed in to change notification settings - Fork 988
/
Tokens_dynamic.php
483 lines (435 loc) · 16.3 KB
/
Tokens_dynamic.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007 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.
*
*/
class Tokens_dynamic extends LSActiveRecord
{
protected static $sid = 0;
/**
* Returns the static model of Settings table
*
* @static
* @access public
* @param int $surveyid
* @return Tokens_dynamic
*/
public static function model($sid = NULL)
{
$refresh = false;
if (!is_null($sid)) {
self::sid($sid);
$refresh = true;
}
$model = parent::model(__CLASS__);
//We need to refresh if we changed sid
if ($refresh === true) $model->refreshMetaData();
return $model;
}
/**
* Sets the survey ID for the next model
*
* @static
* @access public
* @param int $sid
* @return void
*/
public static function sid($sid)
{
self::$sid = (int) $sid;
}
/**
* Returns the setting's table name to be used by the model
*
* @access public
* @return string
*/
public function tableName()
{
return '{{tokens_' . self::$sid . '}}';
}
/**
* Returns the primary key of this table
*
* @access public
* @return string
*/
public function primaryKey()
{
return 'tid';
}
/**
* Returns this model's validation rules
*
*/
public function rules()
{
return array(
array('remindercount','numerical', 'integerOnly'=>true,'allowEmpty'=>true),
array('usesleft','numerical', 'integerOnly'=>true,'allowEmpty'=>true),
array('mpid','numerical', 'integerOnly'=>true,'allowEmpty'=>true),
array('blacklisted', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
// array('validfrom','date', 'format'=>array('yyyy-MM-dd', 'yyyy-MM-dd HH:mm', 'yyyy-MM-dd HH:mm:ss',), 'allowEmpty'=>true),
// array('validuntil','date', 'format'=>array('yyyy-MM-dd', 'yyyy-MM-dd HH:mm', 'yyyy-MM-dd HH:mm:ss',), 'allowEmpty'=>true),
// Date rules currently don't work properly with MSSQL, deactivating for now
);
}
/**
* Returns summary information of this token table
*
* @access public
* @return array
*/
public function summary()
{
$sid = self::$sid;
if(Yii::app()->db->schema->getTable("{{tokens_$sid}}")){
$data=Yii::app()->db->createCommand()
->select("COUNT(*) as tkcount,
SUM(CASE WHEN (token IS NULL OR token='') THEN 1 ELSE 0 END) as tkinvalid,
SUM(CASE WHEN (sent!='N' AND sent<>'') THEN 1 ELSE 0 END) as tksent,
SUM(CASE WHEN (emailstatus LIKE 'OptOut%') THEN 1 ELSE 0 END) as tkoptout,
SUM(CASE WHEN (completed!='N' and completed<>'') THEN 1 ELSE 0 END) as tkcompleted
")
->from("{{tokens_$sid}}")
->queryRow();
}
else
{
$data=false;
}
return $data;
}
/**
* Checks to make sure that all required columns exist in this tokens table
* (some older tokens tables dont' get udated properly)
*
*/
public function checkColumns() {
$sid = self::$sid;
$surveytable='{{tokens_'.$sid.'}}';
$columncheck=array("tid", "participant_id", "firstname", "lastname", "email", "emailstatus","token","language","blacklisted","sent","remindersent","completed","usesleft","validfrom","validuntil");
$columns = Yii::app()->db->schema->getTable($surveytable)->getColumnNames();
$missingcolumns=array_diff($columncheck,$columns);
if(count($missingcolumns)>0) //Some columns are missing - we need to create them
{
Yii::app()->loadHelper('update/updatedb'); //Load the admin helper to allow column creation
setVarchar(); //Set the appropriate varchar settings according to the database
$sVarchar=Yii::app()->getConfig('varchar'); //Retrieve the db specific varchar setting
$columninfo=array('validfrom'=>'datetime',
'validuntil'=>'datetime',
'blacklisted'=>$sVarchar.'(17) NOT NULL',
'participant_id'=>$sVarchar.'(50) NOT NULL',
'remindercount'=>"integer DEFAULT '0'",
'usesleft'=>'integer NOT NULL default 1'); //Not sure if any other fields would ever turn up here - please add if you can think of any others
foreach($missingcolumns as $columnname) {
addColumn($surveytable,$columnname,$columninfo[$columnname]);
}
}
}
public function findUninvited($aTokenIds = false, $iMaxEmails = 0, $bEmail = true, $SQLemailstatuscondition = '', $SQLremindercountcondition = '', $SQLreminderdelaycondition = '')
{
$command = new CDbCriteria;
$command->condition = '';
$command->addCondition("(completed ='N') or (completed='')");
$command->addCondition("token <> ''");
$command->addCondition("email <> ''");
if ($bEmail) {
$command->addCondition("(sent = 'N') or (sent = '')");
} else {
$command->addCondition("(sent <> 'N') AND (sent <> '')");
}
if ($SQLemailstatuscondition)
$command->addCondition($SQLemailstatuscondition);
if ($SQLremindercountcondition)
$command->addCondition($SQLremindercountcondition);
if ($SQLreminderdelaycondition)
$command->addCondition($SQLreminderdelaycondition);
if ($aTokenIds)
$command->addCondition("tid IN ('".implode("', '", $aTokenIds)."')" );
if ($iMaxEmails)
$command->limit = $iMaxEmails;
$command->order = 'tid';
$oResult = Tokens_dynamic::model()->findAll($command);
return $oResult;
}
function insertParticipant($data)
{
$token = new self;
foreach ($data as $k => $v)
$token->$k = $v;
try
{
$token->save();
return $token->tid;
}
catch(Exception $e)
{
return false;
}
}
function insertToken($iSurveyID, $data)
{
self::sid($iSurveyID);
return Yii::app()->db->createCommand()->insert(self::tableName(), $data);
}
function updateToken($tid,$newtoken)
{
return Yii::app()->db->createCommand("UPDATE {$this->tableName()} SET token = :newtoken WHERE tid = :tid")
->bindParam(":newtoken", $newtoken, PDO::PARAM_STR)
->bindParam(":tid", $tid, PDO::PARAM_INT)
->execute();
}
/**
* Retrieve an array of records with an empty token, in the result is just the id (tid)
*
* @param int $iSurveyID
* @return array
*/
function selectEmptyTokens($iSurveyID)
{
return Yii::app()->db->createCommand("SELECT tid FROM {{tokens_{$iSurveyID}}} WHERE token IS NULL OR token=''")->queryAll();
}
public static function countAllAndCompleted($sid)
{
$select = array(
'count(*) AS cntAll',
'sum(CASE '. Yii::app()->db->quoteColumnName('completed') . '
WHEN '.Yii::app()->db->quoteValue('N').' THEN 0
ELSE 1
END) AS cntCompleted',
);
$result = Yii::app()->db->createCommand()->select($select)->from('{{tokens_' . $sid . '}}')->queryRow();
return $result;
}
/**
* Creates and inserts token for a specific token record and returns the token string created
*
* @param int $iTokenID
* @return string token string
*/
function createToken($iTokenID)
{
//get token length from survey settings
$tlrow = Survey::model()->findByAttributes(array("sid"=>self::$sid));
$iTokenLength = $tlrow->tokenlength;
//get all existing tokens
$criteria = $this->getDbCriteria();
$criteria->select = 'token';
$ntresult = $this->findAllAsArray($criteria);
foreach ($ntresult as $tkrow)
{
$existingtokens[] = $tkrow['token'];
}
//create new_token
$bIsValidToken = false;
while ($bIsValidToken == false)
{
$newtoken = randomChars($iTokenLength);
if (!in_array($newtoken, $existingtokens)) {
$existingtokens[] = $newtoken;
$bIsValidToken = true;
}
}
//update specific token row
$itresult = $this->updateToken($iTokenID, $newtoken);
return $newtoken;
}
/**
* Creates tokens for all token records that have empty token fields and returns the number
* of tokens created
*
* @param int $iSurveyID
* @return array ( int number of created tokens, int number to be created tokens)
*/
function createTokens($iSurveyID)
{
$tkresult = $this->selectEmptyTokens($iSurveyID);
//Exit early if there are not empty tokens
if (count($tkresult)===0) return array(0,0);
//get token length from survey settings
$tlrow = Survey::model()->findByAttributes(array("sid"=>$iSurveyID));
$iTokenLength = $tlrow->tokenlength;
//if tokenlength is not set or there are other problems use the default value (15)
if(empty($iTokenLength))
{
$iTokenLength = 15;
}
//Add some criteria to select only the token field
$criteria = $this->getDbCriteria();
$criteria->select = 'token';
$ntresult = $this->findAllAsArray($criteria); //Use AsArray to skip active record creation
// select all existing tokens
foreach ($ntresult as $tkrow)
{
$existingtokens[$tkrow['token']] = true;
}
$newtokencount = 0;
$invalidtokencount=0;
foreach ($tkresult as $tkrow)
{
$bIsValidToken = false;
while ($bIsValidToken == false && $invalidtokencount<50)
{
$newtoken = randomChars($iTokenLength);
if (!isset($existingtokens[$newtoken]))
{
$existingtokens[$newtoken] = true;
$bIsValidToken = true;
$invalidtokencount=0;
}
else
{
$invalidtokencount ++;
}
}
if($bIsValidToken)
{
$itresult = $this->updateToken($tkrow['tid'], $newtoken);
$newtokencount++;
}
else
{
break;
}
}
return array($newtokencount,count($tkresult));
}
/**
* This method is invoked before saving a record (after validation, if any).
* The default implementation raises the {@link onBeforeSave} event.
* You may override this method to do any preparation work for record saving.
* Use {@link isNewRecord} to determine whether the saving is
* for inserting or updating record.
* Make sure you call the parent implementation so that the event is raised properly.
* @return boolean whether the saving should be executed. Defaults to true.
*/
public function beforeSave()
{
// Postgres delivers bytea fields as streams :-o - if this is not done it looks like Postgres saves something unexpected
if ($this->usesleft>0)
{
$this->completed='N';
}
return parent::beforeSave();
}
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('tid',$this->tid,true);
$criteria->compare('firstname',$this->firstname,true);
$criteria->compare('lastname',$this->lastname,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('emailstatus',$this->emailstatus,true);
$criteria->compare('token',$this->token,true);
$criteria->compare('language',$this->language,true);
$criteria->compare('sent',$this->sent,true);
$criteria->compare('sentreminder',$this->sentreminder,true);
$criteria->compare('remindercount',$this->remindercount,true);
$criteria->compare('completed',$this->completed,true);
$criteria->compare('usesleft',$this->usesleft,true);
$criteria->compare('validfrom',$this->validfrom,true);
$criteria->compare('validuntil',$this->validuntil,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Get CDbCriteria for a json search string
*
* @param array $condition
* @return \CDbCriteria
*/
function getSearchMultipleCondition($condition)
{
$i=0;
$j=1;
$tobedonelater =array();
$command = new CDbCriteria;
$command->condition = '';
$iNumberOfConditions = (count($condition)+1)/4;
$sConnectingOperator = 'AND';
$aParams=array();
while($i < $iNumberOfConditions){
$sFieldname=$condition[$i*4];
$sOperator=$condition[($i*4)+1];
$sValue=$condition[($i*4)+2];
switch ($sOperator)
{
case 'equal':
$command->addCondition($sFieldname.' = :condition_'.$i, $sConnectingOperator);
$aParams[':condition_'.$i] = $sValue;
break;
case 'contains':
$command->addCondition($sFieldname.' LIKE :condition_'.$i, $sConnectingOperator);
$aParams[':condition_'.$i] = '%'.$sValue.'%';
break;
case 'notequal':
$command->addCondition($sFieldname.' <> :condition_'.$i, $sConnectingOperator);
$aParams[':condition_'.$i] = $sValue;
break;
case 'notcontains':
$command->addCondition($sFieldname.' NOT LIKE :condition_'.$i, $sConnectingOperator);
$aParams[':condition_'.$i] = '%'.$sValue.'%';
break;
case 'greaterthan':
$command->addCondition($sFieldname.' > :condition_'.$i, $sConnectingOperator);
$aParams[':condition_'.$i] = $sValue;
break;
case 'lessthan':
$command->addCondition($sFieldname.' < :condition_'.$i, $sConnectingOperator);
$aParams[':condition_'.$i] = $sValue;
break;
}
if (isset($condition[($i*4)+3]))
{
$sConnectingOperator=$condition[($i*4)+3];
}
else
{
$sConnectingOperator='AND';
}
$i++;
}
if (count($aParams)>0)
{
$command->params = $aParams;
}
return $command;
}
function deleteToken($tokenid)
{
$dlquery = "DELETE FROM ".Tokens_dynamic::tableName()." WHERE tid=:tokenid";
return Yii::app()->db->createCommand($dlquery)->bindParam(":tokenid", $tokenid)->query();
}
function deleteRecords($iTokenIds)
{
foreach($iTokenIds as &$currentrow)
$currentrow = Yii::app()->db->quoteValue($currentrow);
$dlquery = "DELETE FROM ".Tokens_dynamic::tableName()." WHERE tid IN (".implode(", ", $iTokenIds).")";
return Yii::app()->db->createCommand($dlquery)->query();
}
function getEmailStatus($token)
{
$command = Yii::app()->db->createCommand()
->select('emailstatus')
->from('{{tokens_'.intval(self::$sid).'}}')
->where('token=:token')
->bindParam(':token', $token, PDO::PARAM_STR);
return $command->queryRow();
}
function updateEmailStatus($token,$status)
{
return Yii::app()->db->createCommand()->update('{{tokens_'.intval(self::$sid).'}}',array('emailstatus' => $status),'token = :token',array(':token' => $token ));
}
}?>