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
|
<?php
/**
*
* @package VC
* @version $Id$
* @copyright (c) 2006 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* This class holds the code shared by the two default 3.0 CAPTCHAs.
*/
abstract class phpbb_default_captcha implements phpbb_captcha_plugin
{
protected $confirm_id;
protected $confirm_code;
protected $code;
protected $seed;
protected $type;
protected $solved = false;
protected $min_chars = 4;
protected $max_chars = 7;
function init($type)
{
// read input
$this->confirm_id = request_var('confirm_id', '');
$this->confirm_code = request_var('confirm_code', '');
$this->type = (int) $type;
if (!strlen($this->confirm_id))
{
// we have no confirm ID, better get ready to display something
$this->generate_code();
}
}
function execute_demo()
{
$this->code = gen_rand_string(mt_rand($this->min_chars, $this->max_chars));
$this->seed = hexdec(substr(unique_id(), 4, 10));
// compute $seed % 0x7fffffff
$this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
captcha::execute($this->code, $this->seed);
}
function execute()
{
if (empty($this->code))
{
if (!$this->load_code())
{
// invalid request, bail out
return false;
}
}
captcha::execute($this->code, $this->seed);
}
function get_template()
{
phpbb::$template->set_filenames(array(
'captcha' => 'captcha_default.html',
));
phpbb::$template->assign_vars(array(
'CONFIRM_IMAGE' => append_sid('ucp', 'mode=confirm&confirm_id=' . $this->confirm_id . '&type=' . $this->type),
'CONFIRM_ID' => $this->confirm_id,
));
return phpbb::$template->assign_display('captcha');
}
function get_demo_template($id)
{
phpbb::$template->set_filenames(array(
'captcha_demo' => 'captcha_default_acp_demo.html',
));
// acp_captcha has a delivery function; let's use it
phpbb::$template->assign_vars(array(
'CONFIRM_IMAGE' => append_sid(PHPBB_ADMIN_PATH . 'index.' . PHP_EXT, 'captcha_demo=1&mode=visual&i=' . $id . '&select_captcha=' . $this->get_class_name()),
'CONFIRM_ID' => $this->confirm_id,
));
return phpbb::$template->assign_display('captcha_demo');
}
function get_hidden_fields()
{
$hidden_fields = array();
// this is required for postig.php - otherwise we would forget about the captcha being already solved
if ($this->solved)
{
$hidden_fields['confirm_code'] = $this->confirm_code;
}
$hidden_fields['confirm_id'] = $this->confirm_id;
return $hidden_fields;
}
static function garbage_collect($type)
{
$sql = 'SELECT DISTINCT c.session_id
FROM ' . CONFIRM_TABLE . ' c
LEFT JOIN ' . SESSIONS_TABLE . ' s ON (c.session_id = s.session_id)
WHERE s.session_id IS NULL' .
((empty($type)) ? '' : ' AND c.confirm_type = ' . (int) $type);
$result = phpbb::$db->sql_query($sql);
if ($row = phpbb::$db->sql_fetchrow($result))
{
$sql_in = array();
do
{
$sql_in[] = (string) $row['session_id'];
}
while ($row = phpbb::$db->sql_fetchrow($result));
if (sizeof($sql_in))
{
$sql = 'DELETE FROM ' . CONFIRM_TABLE . '
WHERE ' . phpbb::$db->sql_in_set('session_id', $sql_in);
phpbb::$db->sql_query($sql);
}
}
phpbb::$db->sql_freeresult($result);
}
function uninstall()
{
self::garbage_collect(0);
}
function install()
{
return;
}
function validate()
{
$this->confirm_code = request_var('confirm_code', '');
if (!$this->confirm_id)
{
$error = phpbb::$user->lang['CONFIRM_CODE_WRONG'];
}
else
{
if ($this->check_code())
{
// $this->delete_code(); commented out to allow posting.php to repeat the question
$this->solved = true;
}
else
{
$error = phpbb::$user->lang['CONFIRM_CODE_WRONG'];
}
}
if (strlen($error))
{
// okay, inorect answer. Let's ask a new question
$this->generate_code();
return $error;
}
else
{
return false;
}
}
/**
* The old way to generate code, suitable for GD and non-GD. Resets the internal state.
*/
protected function generate_code()
{
$this->code = gen_rand_string(mt_rand($this->min_chars, $this->max_chars));
$this->confirm_id = md5(unique_id(phpbb::$user->ip));
$this->seed = hexdec(substr(unique_id(), 4, 10));
$this->solved = false;
// compute $seed % 0x7fffffff
$this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
$sql = 'INSERT INTO ' . CONFIRM_TABLE . ' ' . phpbb::$db->sql_build_array('INSERT', array(
'confirm_id' => (string) $this->confirm_id,
'session_id' => (string) phpbb::$user->session_id,
'confirm_type' => (int) $this->type,
'code' => (string) $this->code,
'seed' => (int) $this->seed)
);
phpbb::$db->sql_query($sql);
}
/**
* Look up everything we need for painting&checking.
*/
protected function load_code()
{
$sql = 'SELECT code, seed
FROM ' . CONFIRM_TABLE . "
WHERE confirm_id = '" . phpbb::$db->sql_escape($this->confirm_id) . "'
AND session_id = '" . phpbb::$db->sql_escape(phpbb::$user->session_id) . "'
AND confirm_type = " . $this->type;
$result = phpbb::$db->sql_query($sql);
$row = phpbb::$db->sql_fetchrow($result);
phpbb::$db->sql_freeresult($result);
if ($row)
{
$this->code = $row['code'];
$this->seed = $row['seed'];
return true;
}
return false;
}
protected function check_code()
{
if (empty($this->code))
{
if (!$this->load_code())
{
return false;
}
}
return (strcasecmp($this->code, $this->confirm_code) === 0);
}
protected function delete_code()
{
$sql = 'DELETE FROM ' . CONFIRM_TABLE . "
WHERE confirm_id = '" . phpbb::$db->sql_escape($this->confirm_id) . "'
AND session_id = '" . phpbb::$db->sql_escape(phpbb::$user->session_id) . "'
AND confirm_type = " . $this->type;
phpbb::$db->sql_query($sql);
}
function get_attempt_count()
{
$sql = 'SELECT COUNT(session_id) AS attempts
FROM ' . CONFIRM_TABLE . "
WHERE session_id = '" . phpbb::$db->sql_escape(phpbb::$user->session_id) . "'
AND confirm_type = " . $this->type;
$result = phpbb::$db->sql_query($sql);
$attempts = (int) phpbb::$db->sql_fetchfield('attempts');
phpbb::$db->sql_freeresult($result);
return $attempts;
}
function reset()
{
$sql = 'DELETE FROM ' . CONFIRM_TABLE . "
WHERE session_id = '" . phpbb::$db->sql_escape(phpbb::$user->session_id) . "'
AND confirm_type = " . (int) $this->type;
phpbb::$db->sql_query($sql);
// we leave the class usable by generating a new question
$this->generate_code();
}
}
?>
|