/
CaptchaManager.inc.php
144 lines (127 loc) · 3.39 KB
/
CaptchaManager.inc.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
<?php
/**
* @file classes/captcha/CaptchaManager.inc.php
*
* Copyright (c) 2000-2010 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class CaptchaManager
* @ingroup captcha
* @see CaptchaDAO, Captcha
*
* @brief Class providing captcha services.
*/
// $Id$
import('lib.pkp.classes.file.FileManager');
class CaptchaManager {
/**
* Constructor.
* Create a manager for handling temporary file uploads.
*/
function CaptchaManager() {
$this->_performPeriodicCleanup();
}
/**
* Create a CAPTCHA object.
* @param $length int The length, in characters, of the CAPTCHA test to create
* @return object Captcha
*/
function &createCaptcha($length = 6) {
$captchaDao =& DAORegistry::getDAO('CaptchaDAO');
$session =& Request::getSession();
if ($session && $this->isEnabled()) {
$captcha = new Captcha();
$captcha->setSessionId($session->getId());
$captcha->setValue(Validation::generatePassword($length));
$captchaDao->insertCaptcha($captcha);
} else {
$captcha = null;
}
return $captcha;
}
/**
* Get the width, in pixels, of the CAPTCHA test to create
* @return int Width
*/
function getWidth() {
return 300;
}
/**
* Get the height, in pixels, of the CAPTCHA test to create
* @return int Height
*/
function getHeight() {
return 100;
}
/**
* Get the MIME type of the CAPTCHA image that will be generated
* @return string
*/
function getMimeType() {
return 'image/png';
}
/**
* Generate and display the CAPTCHA image.
* @param $captcha object Captcha
*/
function generateImage(&$captcha) {
$width = $this->getWidth();
$height = $this->getHeight();
$length = String::strlen($captcha->getValue());
$value = $captcha->getValue();
$image = imagecreatetruecolor($width, $height);
$fg = imagecolorallocate($image, rand(128, 255), rand(128, 255), rand(128, 255));
$bg = imagecolorallocate($image, rand(0, 64), rand(0, 64), rand(0, 64));
imagefill($image, $width/2, $height/2, $bg);
$xStart = rand($width / 12, $width / 3);
$xEnd = rand($width * 2 / 3, $width * 11 / 12);
for ($i = 0; $i < $length; $i++) imagefttext(
$image,
rand(20, 34), // Size
rand(-15, 15), // Angle
$xStart + (($xEnd - $xStart) * $i / $length) + rand(-5, 5),
rand(40, 60), // Y position
$fg, // Colour
Config::getVar('captcha', 'font_location'), // Font
String::substr($value, $i, 1) // Text
);
// Add some noise to the image.
for ($i = 0; $i < 20; $i++) {
$color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
for ($j = 0; $j < 20; $j++) {
imagesetpixel(
$image,
rand(0, $this->getWidth()),
rand(0, $this->getHeight()),
$color
);
}
}
header ('Content-type: ' . $this->getMimeType());
imagepng($image);
imagedestroy($image);
}
/**
* Determine whether or not CAPTCHA testing is enabled and supported.
* @return boolean
*/
function isEnabled() {
return (
function_exists('imagecreatetruecolor') &&
Config::getVar('captcha', 'captcha')
);
}
/**
* Private function: Perform periodic cleanup on the CAPTCHA table.
*/
function _performPeriodicCleanup() {
if (time() % 100 == 0) {
$captchaDao =& DAORegistry::getDAO('CaptchaDAO');
$expiredCaptchas = $captchaDao->getExpiredCaptchas();
foreach ($expiredCaptchas as $expiredCaptcha) {
$captchaDao->deleteObject($expiredCaptcha);
}
}
}
}
?>