Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
captcha_abstract.php
001 <?php
002 /**
003 *
004 * This file is part of the phpBB Forum Software package.
005 *
006 * @copyright (c) phpBB Limited <https://www.phpbb.com>
007 * @license GNU General Public License, version 2 (GPL-2.0)
008 *
009 * For full copyright and license information, please see
010 * the docs/CREDITS.txt file.
011 *
012 */
013
014 namespace phpbb\captcha\plugins;
015
016 /**
017 * This class holds the code shared by the two default 3.0.x CAPTCHAs.
018 */
019 abstract class captcha_abstract
020 {
021 var $confirm_id;
022 var $confirm_code;
023 var $code;
024 var $seed;
025 var $attempts = 0;
026 var $type;
027 var $solved = 0;
028 var $captcha_vars = false;
029
030 /**
031 * @var string name of the service.
032 */
033 protected $service_name;
034
035 function init($type)
036 {
037 global $config, $db, $user;
038
039 // read input
040 $this->confirm_id = request_var('confirm_id', '');
041 $this->confirm_code = request_var('confirm_code', '');
042 $refresh = request_var('refresh_vc', false) && $config['confirm_refresh'];
043
044 $this->type = (int) $type;
045
046 if (!strlen($this->confirm_id) || !$this->load_code())
047 {
048 // we have no confirm ID, better get ready to display something
049 $this->generate_code();
050 }
051 else if ($refresh)
052 {
053 $this->regenerate_code();
054 }
055 }
056
057 function execute_demo()
058 {
059 global $user;
060
061 $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
062 $this->seed = hexdec(substr(unique_id(), 4, 10));
063
064 // compute $seed % 0x7fffffff
065 $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
066
067 $generator = $this->get_generator_class();
068 $captcha = new $generator();
069 define('IMAGE_OUTPUT', 1);
070 $captcha->execute($this->code, $this->seed);
071 }
072
073 function execute()
074 {
075 if (empty($this->code))
076 {
077 if (!$this->load_code())
078 {
079 // invalid request, bail out
080 return false;
081 }
082 }
083 $generator = $this->get_generator_class();
084 $captcha = new $generator();
085 define('IMAGE_OUTPUT', 1);
086 $captcha->execute($this->code, $this->seed);
087 }
088
089 function get_template()
090 {
091 global $config, $user, $template, $phpEx, $phpbb_root_path;
092
093 if ($this->is_solved())
094 {
095 return false;
096 }
097 else
098 {
099 $link = append_sid($phpbb_root_path . 'ucp.' . $phpEx, 'mode=confirm&confirm_id=' . $this->confirm_id . '&type=' . $this->type);
100 $contact_link = phpbb_get_board_contact_link($config, $phpbb_root_path, $phpEx);
101 $explain = $user->lang(($this->type != CONFIRM_POST) ? 'CONFIRM_EXPLAIN' : 'POST_CONFIRM_EXPLAIN', '<a href="' . $contact_link . '">', '</a>');
102
103 $template->assign_vars(array(
104 'CONFIRM_IMAGE_LINK' => $link,
105 'CONFIRM_IMAGE' => '<img src="' . $link . '" />',
106 'CONFIRM_IMG' => '<img src="' . $link . '" />',
107 'CONFIRM_ID' => $this->confirm_id,
108 'S_CONFIRM_CODE' => true,
109 'S_TYPE' => $this->type,
110 'S_CONFIRM_REFRESH' => ($config['enable_confirm'] && $config['confirm_refresh'] && $this->type == CONFIRM_REG) ? true : false,
111 'L_CONFIRM_EXPLAIN' => $explain,
112 ));
113
114 return 'captcha_default.html';
115 }
116 }
117
118 function get_demo_template($id)
119 {
120 global $config, $user, $template, $phpbb_admin_path, $phpEx;
121
122 $variables = '';
123
124 if (is_array($this->captcha_vars))
125 {
126 foreach ($this->captcha_vars as $captcha_var => $template_var)
127 {
128 $variables .= '&' . rawurlencode($captcha_var) . '=' . request_var($captcha_var, (int) $config[$captcha_var]);
129 }
130 }
131
132 // acp_captcha has a delivery function; let's use it
133 $template->assign_vars(array(
134 'CONFIRM_IMAGE' => append_sid($phpbb_admin_path . 'index.' . $phpEx, 'captcha_demo=1&mode=visual&i=' . $id . '&select_captcha=' . $this->get_service_name()) . $variables,
135 'CONFIRM_ID' => $this->confirm_id,
136 ));
137
138 return 'captcha_default_acp_demo.html';
139 }
140
141 function get_hidden_fields()
142 {
143 $hidden_fields = array();
144
145 // this is required for posting.php - otherwise we would forget about the captcha being already solved
146 if ($this->solved)
147 {
148 $hidden_fields['confirm_code'] = $this->confirm_code;
149 }
150 $hidden_fields['confirm_id'] = $this->confirm_id;
151 return $hidden_fields;
152 }
153
154 function garbage_collect($type)
155 {
156 global $db, $config;
157
158 $sql = 'SELECT DISTINCT c.session_id
159 FROM ' . CONFIRM_TABLE . ' c
160 LEFT JOIN ' . SESSIONS_TABLE . ' s ON (c.session_id = s.session_id)
161 WHERE s.session_id IS NULL' .
162 ((empty($type)) ? '' : ' AND c.confirm_type = ' . (int) $type);
163 $result = $db->sql_query($sql);
164
165 if ($row = $db->sql_fetchrow($result))
166 {
167 $sql_in = array();
168 do
169 {
170 $sql_in[] = (string) $row['session_id'];
171 }
172 while ($row = $db->sql_fetchrow($result));
173
174 if (sizeof($sql_in))
175 {
176 $sql = 'DELETE FROM ' . CONFIRM_TABLE . '
177 WHERE ' . $db->sql_in_set('session_id', $sql_in);
178 $db->sql_query($sql);
179 }
180 }
181 $db->sql_freeresult($result);
182 }
183
184 function uninstall()
185 {
186 $this->garbage_collect(0);
187 }
188
189 function install()
190 {
191 return;
192 }
193
194 function validate()
195 {
196 global $config, $db, $user;
197
198 if (empty($user->lang))
199 {
200 $user->setup();
201 }
202
203 $error = '';
204 if (!$this->confirm_id)
205 {
206 $error = $user->lang['CONFIRM_CODE_WRONG'];
207 }
208 else
209 {
210 if ($this->check_code())
211 {
212 $this->solved = true;
213 }
214 else
215 {
216 $error = $user->lang['CONFIRM_CODE_WRONG'];
217 }
218 }
219
220 if (strlen($error))
221 {
222 // okay, incorrect answer. Let's ask a new question.
223 $this->new_attempt();
224 return $error;
225 }
226 else
227 {
228 return false;
229 }
230 }
231
232 /**
233 * The old way to generate code, suitable for GD and non-GD. Resets the internal state.
234 */
235 function generate_code()
236 {
237 global $db, $user;
238
239 $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
240 $this->confirm_id = md5(unique_id($user->ip));
241 $this->seed = hexdec(substr(unique_id(), 4, 10));
242 $this->solved = 0;
243 // compute $seed % 0x7fffffff
244 $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
245
246 $sql = 'INSERT INTO ' . CONFIRM_TABLE . ' ' . $db->sql_build_array('INSERT', array(
247 'confirm_id' => (string) $this->confirm_id,
248 'session_id' => (string) $user->session_id,
249 'confirm_type' => (int) $this->type,
250 'code' => (string) $this->code,
251 'seed' => (int) $this->seed)
252 );
253 $db->sql_query($sql);
254 }
255
256 /**
257 * New Question, if desired.
258 */
259 function regenerate_code()
260 {
261 global $db, $user;
262
263 $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
264 $this->seed = hexdec(substr(unique_id(), 4, 10));
265 $this->solved = 0;
266 // compute $seed % 0x7fffffff
267 $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
268
269 $sql = 'UPDATE ' . CONFIRM_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
270 'code' => (string) $this->code,
271 'seed' => (int) $this->seed)) . '
272 WHERE
273 confirm_id = \'' . $db->sql_escape($this->confirm_id) . '\'
274 AND session_id = \'' . $db->sql_escape($user->session_id) . '\'';
275 $db->sql_query($sql);
276 }
277
278 /**
279 * New Question, if desired.
280 */
281 function new_attempt()
282 {
283 global $db, $user;
284
285 $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
286 $this->seed = hexdec(substr(unique_id(), 4, 10));
287 $this->solved = 0;
288 // compute $seed % 0x7fffffff
289 $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
290
291 $sql = 'UPDATE ' . CONFIRM_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
292 'code' => (string) $this->code,
293 'seed' => (int) $this->seed)) . '
294 , attempts = attempts + 1
295 WHERE
296 confirm_id = \'' . $db->sql_escape($this->confirm_id) . '\'
297 AND session_id = \'' . $db->sql_escape($user->session_id) . '\'';
298 $db->sql_query($sql);
299 }
300
301 /**
302 * Look up everything we need for painting&checking.
303 */
304 function load_code()
305 {
306 global $db, $user;
307
308 $sql = 'SELECT code, seed, attempts
309 FROM ' . CONFIRM_TABLE . "
310 WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
311 AND session_id = '" . $db->sql_escape($user->session_id) . "'
312 AND confirm_type = " . $this->type;
313 $result = $db->sql_query($sql);
314 $row = $db->sql_fetchrow($result);
315 $db->sql_freeresult($result);
316
317 if ($row)
318 {
319 $this->code = $row['code'];
320 $this->seed = $row['seed'];
321 $this->attempts = $row['attempts'];
322 return true;
323 }
324
325 return false;
326 }
327
328 function check_code()
329 {
330 return (strcasecmp($this->code, $this->confirm_code) === 0);
331 }
332
333 function get_attempt_count()
334 {
335 return $this->attempts;
336 }
337
338 function reset()
339 {
340 global $db, $user;
341
342 $sql = 'DELETE FROM ' . CONFIRM_TABLE . "
343 WHERE session_id = '" . $db->sql_escape($user->session_id) . "'
344 AND confirm_type = " . (int) $this->type;
345 $db->sql_query($sql);
346
347 // we leave the class usable by generating a new question
348 $this->generate_code();
349 }
350
351 function is_solved()
352 {
353 if (request_var('confirm_code', false) && $this->solved === 0)
354 {
355 $this->validate();
356 }
357 return (bool) $this->solved;
358 }
359
360 /**
361 * API function
362 */
363 function has_config()
364 {
365 return false;
366 }
367
368 /**
369 * @return string the name of the service corresponding to the plugin
370 */
371 function get_service_name()
372 {
373 return $this->service_name;
374 }
375
376 /**
377 * Set the name of the plugin
378 *
379 * @param string $name
380 */
381 public function set_name($name)
382 {
383 $this->service_name = $name;
384 }
385
386 /**
387 * @return string the name of the class used to generate the captcha
388 */
389 abstract function get_generator_class();
390 }
391