-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormDiceCaptcha.module
More file actions
146 lines (114 loc) · 4.24 KB
/
FormDiceCaptcha.module
File metadata and controls
146 lines (114 loc) · 4.24 KB
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
<?php
/**
* Dice Captcha
*
* @copyright 2015, Roman Seidl
* Licensed under GNU/GPL v3, see LICENSE.TXT
*
*/
class FormDiceCaptcha extends WireData implements Module, ConfigurableModule
{
public static function getModuleInfo()
{
return array(
'title' => 'Dice Captcha',
'summary' => 'Creates a dice captcha',
'singular' => true,
'autoload' => true,
'version' => 12
);
}
protected static $defaultConfigData = array("num_dices" => 3, 'dice_size' => 50);
public function __construct()
{
foreach (self::$defaultConfigData as $key => $value) {
$this->data[$key] = $value;
}
}
private $captchadir;
const CAPTCHA_DIR = "captcha/";
const CAPTCHA_KEY = "dice_captcha";
public function init()
{
$this->captchadir = $this->config->paths->assets . self::CAPTCHA_DIR;
if (!is_dir($this->captchadir)) {
wireMkdir($this->captchadir);
} else {
//delete old files
$files = glob($this->captchadir . "/*");
$now = time();
foreach ($files as $file)
if (is_file($file))
if ($now - filemtime($file) >= 60 * 2) // 2 Minutes
unlink($file);
}
}
public function captcha()
{
$images = array();
for ($i = 1; $i <= 6; $i++) {
$imgfile = dirname(__FILE__) ."/". $i . ".jpg";
$images[$i] = imagecreatefromjpeg($imgfile);
}
$dice_size = $this->data['dice_size'];
$num_dices = $this->data['num_dices'];
$canvas_image = imagecreatetruecolor($num_dices * $dice_size, $dice_size);
$current_x = 0;
$sum = 0;
for ($i = 0; $i < $num_dices; $i++) {
$dice = rand(1, 6);
$sum += $dice;
imagecopy($canvas_image, // destination image
$images[$dice], // source image
$current_x, // x co-ordinate of destination
0, // y co-ordinate of destination
0, // x co-ordinate of source
0, // y co-ordinate of source
$dice_size, // source img width
$dice_size // source img height
);
$current_x = $current_x + $dice_size;
}
$filename = tempnam($this->captchadir, "c");
unlink($filename);
$filename .= ".jpg";
$captcha = imagejpeg($canvas_image, $filename);
imagedestroy($canvas_image);
for ($i = 1; $i <= 6; $i++) {
imagedestroy($images[$i]);
}
$this->session->set(self::CAPTCHA_KEY, $sum);
return $this->config->urls->assets . self::CAPTCHA_DIR . basename($filename);
}
public function validate($number)
{
return $number == $this->session->get(self::CAPTCHA_KEY);
}
public static function getModuleConfigInputfields(array $data)
{
$modules = wire('modules');
$config = wire('config');
$inputfields = new InputfieldWrapper();
foreach (self::$defaultConfigData as $key => $value) {
if (!isset($data[$key]))
$data[$key] = $value;
}
$fieldset = $modules->get('InputfieldFieldset');
$fieldset->columnWidth = 50;
$fieldset->label = __("Global Settings");
$f = $modules->get('InputfieldText');
$f->name = "num_dices";
$f->label = __("num_dices");
$f->description = __("The number of dice shown.");
$f->value = $data["num_dices"];
$fieldset->append($f);
$f = $modules->get('InputfieldText');
$f->name = "dice_size";
$f->label = __("dice_size");
$f->description = __("Dice image size in pixels. Dice must must be square images.");
$f->value = $data["dice_size"];
$fieldset->append($f);
$inputfields->append($fieldset);
return $inputfields;
}
}