forked from netcommons/NetCommons2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCAPTCHA.php
More file actions
253 lines (219 loc) · 6.26 KB
/
CAPTCHA.php
File metadata and controls
253 lines (219 loc) · 6.26 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
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
<?php
/**
* Text_CAPTCHA - creates a CAPTCHA for Turing tests
*
* Class to create a Turing test for websites by
* creating an image, ASCII art or something else
* with some (obfuscated) characters
*
*
* @package Text_CAPTCHA
* @license PHP License, version 3.0
* @author Christian Wenz <wenz@php.net>
* @category Text
*/
/**
*
* Require PEAR class for error handling.
*
*/
require_once 'PEAR.php';
/**
*
* Require Text_Password class for generating the phrase.
*
*/
require_once 'Text/Password.php';
/**
* Text_CAPTCHA - creates a CAPTCHA for Turing tests
*
* Class to create a Turing test for websites by
* creating an image, ASCII art or something else
* with some (obfuscated) characters
*
* @package Text_CAPTCHA
*/
/*
// This is a simple example script
<?php
if (!function_exists('file_put_contents')) {
function file_put_contents($filename, $content) {
if (!($file = fopen($filename, 'w'))) {
return false;
}
$n = fwrite($file, $content);
fclose($file);
return $n ? $n : false;
}
}
// Start PHP session support
session_start();
$ok = false;
$msg = 'Please enter the text in the image in the field below!';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['phrase']) && is_string($_SESSION['phrase']) && isset($_SESSION['phrase']) &&
strlen($_POST['phrase']) > 0 && strlen($_SESSION['phrase']) > 0 &&
$_POST['phrase'] == $_SESSION['phrase']) {
$msg = 'OK!';
$ok = true;
unset($_SESSION['phrase']);
} else {
$msg = 'Please try again!';
}
unlink(md5(session_id()) . '.png');
}
print "<p>$msg</p>";
if (!$ok) {
require_once 'Text/CAPTCHA.php';
// Set CAPTCHA image options (font must exist!)
$imageOptions = array(
'font_size' => 24,
'font_path' => './',
'font_file' => 'COUR.TTF',
'text_color' => '#DDFF99',
'lines_color' => '#CCEEDD',
'background_color' => '#555555'
);
// Set CAPTCHA options
$options = array(
'width' => 200,
'height' => 80,
'output' => 'png',
'imageOptions' => $imageOptions
);
// Generate a new Text_CAPTCHA object, Image driver
$c = Text_CAPTCHA::factory('Image');
$retval = $c->init($options);
if (PEAR::isError($retval)) {
printf('Error initializing CAPTCHA: %s!',
$retval->getMessage());
exit;
}
// Get CAPTCHA secret passphrase
$_SESSION['phrase'] = $c->getPhrase();
// Get CAPTCHA image (as PNG)
$png = $c->getCAPTCHA();
if (PEAR::isError($png)) {
printf('Error generating CAPTCHA: %s!',
$png->getMessage());
exit;
}
file_put_contents(md5(session_id()) . '.png', $png);
echo '<form method="post">' .
'<img src="' . md5(session_id()) . '.png?' . time() . '" />' .
'<input type="text" name="phrase" />' .
'<input type="submit" /></form>';
}
?>
*/
class Text_CAPTCHA {
/**
* Version number
*
* @access private
* @var string
*/
var $_version = '0.3.1';
/**
* Phrase
*
* @access private
* @var string
*/
var $_phrase;
/**
* Create a new Text_CAPTCHA object
*
* @param string $driver name of driver class to initialize
*
* @return mixed a newly created Text_CAPTCHA object, or a PEAR
* error object on error
*
* @see PEAR::isError()
*/
function &factory($driver)
{
if ($driver == '') {
return PEAR::raiseError('No CAPTCHA type specified ... aborting. You must call ::factory() with one parameter, the CAPTCHA type.', true);
}
$driver = basename($driver);
include_once "Text/CAPTCHA/Driver/$driver.php";
$classname = "Text_CAPTCHA_Driver_$driver";
$obj =& new $classname;
return $obj;
}
/**
* Create random CAPTCHA phrase
*
* This method creates a random phrase, 8 characters long
*
* @access private
*/
function _createPhrase()
{
$len = 8;
$this->_phrase = Text_Password::create($len);
}
/**
* Return secret CAPTCHA phrase
*
* This method returns the CAPTCHA phrase
*
* @access public
* @return phrase secret phrase
*/
function getPhrase()
{
return $this->_phrase;
}
/**
* Sets secret CAPTCHA phrase
*
* This method sets the CAPTCHA phrase
* (use null for a random phrase)
*
* @access public
* @param string $phrase the (new) phrase
* @void
*/
function setPhrase($phrase = null)
{
if (!empty($phrase)) {
$this->_phrase = $phrase;
} else {
$this->_createPhrase();
}
}
/**
* Place holder for the real init() method
* used by extended classes to initialize CAPTCHA
*
* @access private
* @return PEAR_Error
*/
function init() {
return PEAR::raiseError('CAPTCHA type not selected', true);
}
/**
* Place holder for the real _createCAPTCHA() method
* used by extended classes to generate CAPTCHA from phrase
*
* @access private
* @return PEAR_Error
*/
function _createCAPTCHA() {
return PEAR::raiseError('CAPTCHA type not selected', true);
}
/**
* Place holder for the real getCAPTCHA() method
* used by extended classes to return the generated CAPTCHA
* (as an image resource, as an ASCII text, ...)
*
* @access private
* @return PEAR_Error
*/
function getCAPTCHA() {
return PEAR::raiseError('CAPTCHA type not selected', true);
}
}
?>