-
Notifications
You must be signed in to change notification settings - Fork 0
/
TFRcustomToken.php
126 lines (115 loc) · 4.59 KB
/
TFRcustomToken.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
<?php
class TFRcustomToken extends PluginBase {
protected $storage = 'DbStorage';
static protected $name = 'TFRcustomToken';
static protected $description = 'Numeric, non-ambiguous or CAPITAL tokens';
public function init()
{
/**
* Here you should handle subscribing to the events your plugin will handle
*/
$this->subscribe('afterGenerateToken', 'generateCustomToken');
// Provides survey specific settings.
$this->subscribe('beforeSurveySettings');
// Saves survey specific settings.
$this->subscribe('newSurveySettings');
// Clean up on deactivate
$this->subscribe('beforeDeactivate');
}
/**
* The custom generate function
*/
public function generateCustomToken()
{
$event = $this->getEvent();
$iSurveyID=$event->get('surveyId');
$iTokenLength = $event->get('iTokenLength');
$token = "";
if ($this->get('TFRcustomToken', 'Survey', $iSurveyID) == 0) {
// 0 = No custom function for this survey: return without changes in $event
return;
}
else if ($this->get('TFRcustomToken', 'Survey', $iSurveyID) == 1) {
// 1 = Numeric tokens
$token = randomChars($iTokenLength, '123456789');
}
else if ($this->get('TFRcustomToken', 'Survey', $iSurveyID) == 2) {
// 2 = Without ambiguous characters including 'hard to manually enter'
// https://github.com/LimeSurvey/LimeSurvey/commit/154e026fbe6e53037e46a8c30f2b837459235acc
$token = str_replace(
array('~','_','0','O','1','l','I'),
array('a','z','7','P','8','k','K'), Yii::app()->securityManager->generateRandomString($iTokenLength));
}
else if ($this->get('TFRcustomToken', 'Survey', $iSurveyID) == 3) {
// 3 = CAPITALS ONLY
if (function_exists('crypto_rand_secure')) {
/**
* Adjusted from Yii::app()->securityManager->generateRandomString($length=32)
* https://github.com/LimeSurvey/LimeSurvey/blob/master/application/core/web/LSYii_SecurityManager.php#L71
* Use crypto_rand_secure($min, $max) defined in application/helpers/common_helper.php
*/
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for($i=0;$i<$iTokenLength;$i++){
$token .= $codeAlphabet[crypto_rand_secure(0,strlen($codeAlphabet))];
}
} else {
/**
* Secure enough, although not cryptographically secure
* https://www.php.net/manual/en/function.rand.php
*/
for($i=0;$i<$iTokenLength;$i++){
$token .= chr(64+rand(1, 26));
}
}
}
$event->set('token', $token);
}
/**
* This event is fired by the administration panel to gather extra settings
* available for a survey. Example URL in LS 3.17:
* /index.php/admin/survey/sa/rendersidemenulink/subaction/plugins/surveyid/46159
*/
public function beforeSurveySettings()
{
$pluginsettings = $this->getPluginSettings(true);
$event = $this->getEvent();
$iSurveyID=$event->get('iSurveyID');
$event->set("surveysettings.{$iSurveyID}", array(
'name' => get_class($this),
'settings' => array(
'TFRcustomToken' => array(
'type' => 'select',
'options'=>array(
0=>'No custom function for this survey',
1=>'Numeric tokens',
2=>'Without ambiguous characters',
3=>'CAPITALS ONLY'
),
'default' => 0,
'label' => 'Custom token:',
'current' => $this->get('TFRcustomToken', 'Survey', $event->get('survey'))
)
)
));
}
/**
* Save the settings
*/
public function newSurveySettings()
{
$event = $this->getEvent();
foreach ($event->get('settings') as $name => $value)
{
$this->set($name, $value, 'Survey', $event->get('survey'));
}
}
/**
* Clean up the plugin settings table
*/
public function beforeDeactivate()
{
$sDBPrefix = Yii::app()->db->tablePrefix;
$sql = "DELETE FROM {$sDBPrefix}plugin_settings WHERE `key` LIKE :key";
Yii::app()->db->createCommand($sql)->execute(array(':key' => "TFRcustomToken"));
}
}