root/include/captcha.php

Revision 307, 16.9 kB (checked in by Nafania, 2 years ago)

Создаем табличку для чата (забыл совсем про нее). Меняем записи в логе о редактировании торрента и ставим flags = 9, только тогда, когда надо а не всегда. Замена везде exit на нормальные функции, которые вырубает кеш и закрывают соединение с базой, ибо это правильно. Небольшая фича на форуме, выделяем текст поста + клик на нике = быстрая цитата. Баны в чате и закрытие комментов в юзер панели. Вроде все.

Line 
1 <?php
2 /***************************************************************************</span>
3 <span class="code-comment"> *                            usercp_confirm.php
4  *                            -------------------
5  *   begin                : Feb 23, 2006
6  *   copyright            : (C) 2006 AmigaLink
7  *   website              : www.AmigaLink.de
8  *
9  *   $Id: usercp_confirm.php,v 2.0.18.5 2006/11/03 23:48:00 AmigaLink Exp $
10  *
11  ***************************************************************************/
12
13 /***************************************************************************
14  *
15  *   This program is free software; you can redistribute it and/or modify
16  *   it under the terms of the GNU General Public License as published by
17  *   the Free Software Foundation; either version 2 of the License, or
18  *   (at your option) any later version.
19  *
20  ***************************************************************************/
21
22 $confirm_id = request_var('id', '');
23 $sid = request_var('sid', '');
24 // Do we have an id? No, then just exit
25 if (!$confirm_id)</span>
26 <span class="code-keyword">{
27     gc();
28 }
29
30 #if (!preg_match('/^[A-Za-z0-9]+$/', $confirm_id))
31 if (!preg_match('/^[[:alnum:]]+$/', $confirm_id))</span>
32 <span class="code-keyword">{
33     $confirm_id = '';
34 }
35
36 if ( $confirm_id === 'Admin' )
37 {
38     if ( !$userdata['session_logged_in'] || !$userdata['session_admin'] )
39     {
40         trigger_error('Hacking attempt');
41     }
42     $code = '';
43     $font_debug = true;
44     $bg_img_debug = true;
45 }
46 else
47 {
48     // Try and grab code for this id and session
49     $sql = 'SELECT code
50         FROM ' . CONFIRM_TABLE . "
51         WHERE session_id = '" . $sid . "'
52         AND confirm_id = '$confirm_id'";
53     $result = $db->sql_query($sql);
54
55     // If we have a row then grab data else create a new id
56     if ($row = $db->sql_fetchrow($result))
57     {
58         $db->sql_freeresult($result);
59         $code = $row['code'];
60     }
61     else
62     {
63         gc();
64     }
65
66     $font_debug = false;
67     $bg_img_debug = false;
68 }
69
70 srand((double)microtime()*1000000);
71 mt_srand((double)microtime()*1000000);
72
73 // Define allowed bg-image types
74 $bg_image_activetype_allow = array();</span>
75 <span class="code-lang">(imagetypes() & IMG_PNG) ? $bg_image_activetype_allow[] = 'png' : '';
76 (imagetypes() & IMG_JPEG) ? $bg_image_activetype_allow[] = 'jpg' : '';
77
78 // Prefs
79 $code = ($confirm_id === 'Admin') ? $config['captcha_example_code'] : $code;
80
81 $total_width = ($config['captcha_width'] < (strlen($code)*15)+10) ? (strlen($code)*15)+10 : $config['captcha_width'];
82 $total_height = ($config['captcha_height'] < 30 ) ? 30 : $config['captcha_height'];
83
84 $hex_bg_color = hex_to_rgb($config['captcha_background_color']);
85 $bg_color = array();
86 $bg_color = explode(",", $hex_bg_color);
87 $bg_image_active = $config['captcha_image'];
88 $bg_transition = $config['captcha_bg_transition'];
89 $trans_letters = $config['captcha_trans_letters'];
90
91 $jpeg = $config['captcha_jpeg'];
92 $img_quality = $config['captcha_jpeg_quality']; // Max quality is 95
93
94 $pre_letters = $config['captcha_pre_letters'];
95 $pre_letter_great = $config['captcha_pre_letters_great'];
96 $rnd_font = $config['captcha_font'];
97
98 $chess = $config['captcha_chess'];
99 $ellipses = $config['captcha_ellipses'];
100 $arcs = $config['captcha_arcs'];
101 $lines = $config['captcha_lines'];
102
103 $gammacorrect = $config['captcha_gammacorrect'];
104
105 $foreground_lattice_y = $config['captcha_foreground_lattice_width'];
106 $foreground_lattice_x = $config['captcha_foreground_lattice_height'];
107 $hex_lattice_color = hex_to_rgb($config['captcha_lattice_color']);
108 $rgb_lattice_color = array();
109 $rgb_lattice_color = explode(",", $hex_lattice_color);
110
111 $bg_image_file = '';
112
113 // Images init
114 if ($bg_image_active)</span>
115 <span class="code-keyword">{
116     $bg_imgs = array();
117     if ($img_dir = opendir($root_path . 'captcha/pics/'))
118     {
119         while (true == ($file = @readdir($img_dir)))
120         {
121             if (array_search(substr(strtolower($file), -3), $bg_image_activetype_allow) !== false)
122             {
123                 $bg_imgs[] = $file;
124             }
125         }
126         closedir($img_dir);
127     }
128     // Grab a random Background Image or set Error if none was found
129     $bg_img = rand(0, (count($bg_imgs)-1));
130     $bg_err = ($bg_imgs[$bg_img] != '') ? false : true;
131
132     if (!$bg_err)
133     {
134         // Get image size and check type
135         $bg_img_data = array();
136         $bg_img_data = @getimagesize($root_path . 'captcha/pics/' . $bg_imgs[$bg_img]);
137         switch($bg_img_data[2])
138         {
139             case '1': # GIF
140                 $bg_img_type = 'gif';
141                 break;
142             case '2':    # JPG
143                 $bg_img_type = 'jpg';
144                 break;
145             case '3':    # PNG
146                 $bg_img_type = 'png';
147                 break;
148             case '4':    # SWF
149                 $bg_img_type = 'swf';
150                 break;
151             case '5':    # PSD
152                 $bg_img_type = 'psd';
153                 break;
154             case '6':    # BMP
155                 $bg_img_type = 'bmp';
156                 break;
157             case '7':    # TIFF (intel byte order)
158                 $bg_img_type = 'tiff';
159                 break;
160             case '8':    # TIFF (motorola byte order)
161                 $bg_img_type = 'tiff';
162                 break;
163             case '9':    # JPC
164                 $bg_img_type = 'jpc';
165                 break;
166             case '10':    # JP2
167                 $bg_img_type = 'jp2';
168                 break;
169             case '11':    # JPX
170                 $bg_img_type = 'jpx';
171                 break;
172             case '12':    # JB2
173                 $bg_img_type = 'jb2';
174                 break;
175             case '13':    # SWC
176                 $bg_img_type = 'swc';
177                 break;
178             case '14':    # IFF
179                 $bg_img_type = 'iff';
180                 break;
181             case '15':    # WBMP
182                 $bg_img_type = 'wbmp';
183                 break;
184             case '16':    # XBM
185                 $bg_img_type = 'xbm';
186                 break;
187             default:
188                 $bg_img_type = 'unknown';
189         }
190         (array_search($bg_img_type, $bg_image_activetype_allow) !== false) ? '' : $bg_err = 2;
191     }
192     $bg_image_file = (!$bg_err) ? $root_path . 'captcha/pics/'.$bg_imgs[$bg_img] : '';
193 }
194
195 // Fonts init
196 $fonts = array();</span>
197 <span class="code-lang">if ($fonts_dir = opendir($root_path.'captcha/fonts/'))
198 {
199     while (true == ($file = @readdir($fonts_dir)))
200     {
201         if ((substr(strtolower($file), -3) == 'ttf'))
202         {
203             $fonts[] = $file;
204         }
205     }
206     closedir($fonts_dir);
207 }
208 $font = rand(0, (count($fonts)-1));
209
210 // Generate image
211 $image = (gdVersion() >= 2) ? imagecreatetruecolor($total_width, $total_height) : imagecreate($total_width, $total_height);
212 $background_color = imagecolorallocate($image, $bg_color[0], $bg_color[1], $bg_color[2]);
213 imageinterlace($image, 1);
214 imagecolortransparent($image, $background_color);
215 imagefill($image, 0, 0, $background_color);
216
217 // Backgrund
218 if ($chess == '1' || $chess == '2' && rand(0,1))</span>
219 <span class="code-keyword">{
220     // Draw rectangles
221     $rec_size = round(rand(25, 50));
222     $x_rec = ceil($total_width / $rec_size);
223     $y_rec = ceil($total_height / $rec_size);
224     $rest_x = rand(0, round($total_width - (($x_rec - 1) * $rec_size)));
225     $rest_y = rand(0, round($total_height - (($y_rec - 1) * $rec_size)));
226
227     for ($y = 0; $y <= $y_rec; $y++)
228     {
229         $y1 = ($y == 0) ? 0 : ($y * $rec_size) + $rest_y;
230         ($y != 0) ? $y1 = $y1 - $rec_size : '';
231         $y2 = ($y == 0) ? $rest_y : $y1 + $rec_size + $rest_y;
232
233         for ($x = 0; $x <= $x_rec; $x++)
234         {
235             $x1 = ($x == 0) ? 0 : ($x * $rec_size) + $rest_x;
236             ($x != 0) ? $x1 = $x1 - $rec_size : '';
237             $x2 = ($x == 0) ? $rest_x : $x1 + $rec_size + $rest_x;
238
239             $rectanglecolor = imagecolorallocate($image, rand(100,200),rand(100,200),rand(100,200));
240             imagefilledrectangle($image, $x1, $y1, $x2, $y2, $rectanglecolor);
241         }
242     }
243 }
244 if ($ellipses == '1' || $ellipses == '2' && rand(0,1))
245 {
246     // Draw random ellipses
247     for ($i = 1; $i <= 60; $i++)
248     {
249         $ellipsecolor = imagecolorallocate($image, rand(100,250),rand(100,250),rand(100,250));
250         imagefilledellipse($image, round(rand(0, $total_width)), round(rand(0, $total_height)), round(rand(0, $total_width/8)), round(rand(0, $total_height/4)), $ellipsecolor);
251     }
252 }
253 if ($arcs == '1' || $arcs == '2' && rand(0,1))
254 {
255     // Draw random partial ellipses
256     for ($i = 0; $i <= 30; $i++)
257     {
258         $linecolor = imagecolorallocate($image, rand(120,255),rand(120,255),rand(120,255));
259         $cx = round(rand(1, $total_width));
260         $cy = round(rand(1, $total_height));
261         $int_w = round(rand(1, $total_width/2));
262         $int_h = round(rand(1, $total_height));
263         imagearc($image, $cx, $cy, $int_w, $int_h, round(rand(0, 190)), round(rand(191, 360)), $linecolor);
264         imagearc($image, $cx-1, $cy-1, $int_w, $int_h, round(rand(0, 190)), round(rand(191, 360)), $linecolor);
265     }
266 }
267 if ($lines == '1' || $lines == '2' && rand(0,1))
268 {
269     // Draw random lines
270     for ($i = 0; $i <= 50; $i++)
271     {
272         $linecolor = imagecolorallocate($image, rand(120,255),rand(120,255),rand(120,255));
273         imageline($image, round(rand(1, $total_width*3)), round(rand(1, $total_height*5)), round(rand(1, $total_width/2)), round(rand(1, $total_height*2)), $linecolor);
274     }
275 }
276 //</span>
277 <span class="code-comment">
278 // Merge with background image?
279 if ($bg_image_active && !$bg_err && !$trans_letters)</span>
280 <span class="code-keyword">{
281     @mergeimages($image, $total_height, $total_width, $bg_image_file, $bg_img_data[0], $bg_img_data[1], $bg_img_type, $bg_transition);
282 }
283
284
285 // Letters
286 $text_color_array = array('255,51,0', '51,77,255', '204,51,102', '0,153,0', '255,166,2', '255,0,255', '255,0,0', '0,255,0', '0,0,255', '0,255,255');
287 shuffle($text_color_array);
288 $pre_text_color_array = array('255,71,20', '71,20,224', '224,71,122', '20,173,20', '255,186,22', '25,25,25');
289 shuffle($pre_text_color_array);
290 $white = imagecolorallocate($image, 255, 255, 255);
291 $gray = imagecolorallocate($image, 100, 100, 100);
292 $black = imagecolorallocate($image, 0, 0, 0);
293 $lattice_color = imagecolorallocate($image, $rgb_lattice_color[0], $rgb_lattice_color[1], $rgb_lattice_color[2]);
294
295 $code_area_width = floor($total_width - rand((strlen($code) * 2), (strlen($code) * 4))) - 10;
296 $code_walk = rand(0, ($total_width - ($code_area_width + 10)));
297 $min_char_size = (floor($total_height / 4.5) < 12) ? 12 : floor($total_height / 4.5);
298 $max_char_size = (ceil($total_height / 2.5) > ($code_area_width / (strlen($code) + 2))) ? ($code_area_width / (strlen($code) + 2)) : ceil($total_height / 2.5);
299 $tc_light = mt_rand(180, 220);
300 $tc_dark = mt_rand(55, 85);</span>
301 <span class="code-lang">
302 for ($i = 0; $i < strlen($code); $i++)
303 {
304     mt_srand((double)microtime()*1000000);
305
306     $char = $code{$i};
307     $size = mt_rand($min_char_size, $max_char_size);
308     $font = ($rnd_font) ? mt_rand(0, (count($fonts)-1)) : $font;
309     $angle = mt_rand(-35, 30);
310
311     $x_char_position = rand( round((($code_area_width - (strlen($code) * 2)) / strlen($code))), round((($code_area_width - (strlen($code) * 4)) / strlen($code))) );
312
313     $char_pos = array();
314     $char_pos = imagettfbbox($size, $angle, $root_path.'captcha/fonts/'.$fonts[$font], $char);
315     $letter_width = abs($char_pos[0]) + abs($char_pos[4]);
316     $letter_height = abs($char_pos[1]) + abs($char_pos[5]);
317
318     $x_pos = floor(($x_char_position - ($letter_width / 2)) + ($i * $x_char_position)) + $code_walk;
319     ($i == strlen($code)-1 && $x_pos >= ($total_width - ($letter_width + 5))) ? $x_pos = ($total_width - ($letter_width + 5)) : '';
320     $y_pos = mt_rand(($size * 1.4 ), $total_height - ($size * 0.5));
321
322 //    Pre letters</span>
323 <span class="code-comment">    $size = ($pre_letter_great) ? $size + (2 * $pre_letters) : $size - (2 * $pre_letters);
324     for ($count = 1; $count <= $pre_letters; $count++)
325     {
326         $pre_angle = $angle + mt_rand(-20, 20);
327
328         $text_color = $pre_text_color_array[mt_rand(0, count($pre_text_color_array)-1)];
329         $text_color = explode(",", $text_color);
330         $textcolor = imagecolorallocate($image, $text_color[0], $text_color[1], $text_color[2]);
331         $textcolor_light = imagecolorallocate($image, $tc_light, $tc_light, $tc_light);
332         $textcolor_dark = imagecolorallocate($image, $tc_dark, $tc_dark, $tc_dark);
333
334         imagettftext($image, $size, $pre_angle, $x_pos, $y_pos-2, $textcolor_light, $root_path.'captcha/fonts/'.$fonts[$font], $char);
335         imagettftext($image, $size, $pre_angle, $x_pos+2, $y_pos, $textcolor_dark, $root_path.'captcha/fonts/'.$fonts[$font], $char);
336         imagettftext($image, $size, $pre_angle, $x_pos+1, $y_pos-1, $textcolor, $root_path.'captcha/fonts/'.$fonts[$font], $char);
337
338         $size = ($pre_letter_great) ? $size - 2 : $size + 2;
339     }
340
341 //    Final letters</span>
342 <span class="code-comment">    $text_color = $text_color_array[mt_rand(0, count($text_color_array)-1)];
343     $text_color = explode(",", $text_color);
344     $textcolor = imagecolorallocate($image, $text_color[0], $text_color[1], $text_color[2]);
345     $textcolor_light = imagecolorallocate($image, $tc_light, $tc_light, $tc_light);
346     $textcolor_dark = imagecolorallocate($image, $tc_dark, $tc_dark, $tc_dark);
347
348     imagettftext($image, $size, $angle, $x_pos, $y_pos-2, $textcolor_light, $root_path.'captcha/fonts/'.$fonts[$font], $char);
349     imagettftext($image, $size, $angle, $x_pos+2, $y_pos, $textcolor_dark, $root_path.'captcha/fonts/'.$fonts[$font], $char);
350     imagettftext($image, $size, $angle, $x_pos+1, $y_pos-1, $textcolor, $root_path.'captcha/fonts/'.$fonts[$font], $char);
351 }
352
353
354 // Merge with background image?
355 if ($bg_image_active && !$bg_err && $trans_letters)</span>
356 <span class="code-keyword">{
357     @mergeimages($image, $total_height, $total_width, $bg_image_file, $bg_img_data[0], $bg_img_data[1], $bg_img_type, $bg_transition);
358 }
359
360 ($gammacorrect) ? imagegammacorrect($image, 1.0, $gammacorrect) : '';
361
362 // Generate a lattice in foreground
363 if ($foreground_lattice_y)</span>
364 <span class="code-keyword">{
365     // x lines
366     $ih = round($total_height / $foreground_lattice_y);
367     for ($i = 0; $i <= $ih; $i++)
368     {
369         imageline($image, 0, $i*$foreground_lattice_y, $total_width, $i*$foreground_lattice_y, $lattice_color);
370     }
371 }
372 if ($foreground_lattice_x)
373 {
374     // y lines
375     $iw = round($total_width / $foreground_lattice_x);
376     for ($i = 0; $i <= $iw; $i++)
377     {
378         imageline($image, $i*$foreground_lattice_x, 0, $i*$foreground_lattice_x, $total_height, $lattice_color);
379     }
380 }
381
382 // Font debug
383 if ($font_debug && !$rnd_font)</span>
384 <span class="code-keyword">{
385     imagestring($image, 4, 2, 0, ($font + 1).'/'.count($fonts).': '.$fonts[$font], $white);
386     imagestring($image, 4, 5, 0, ($font + 1).'/'.count($fonts).': '.$fonts[$font], $white);
387     imagestring($image, 4, 3, 3, ($font + 1).'/'.count($fonts).': '.$fonts[$font], $white);
388     imagestring($image, 4, 4, 2, ($font + 1).'/'.count($fonts).': '.$fonts[$font], $gray);
389     imagestring($image, 4, 3, 1, ($font + 1).'/'.count($fonts).': '.$fonts[$font], $black);
390 }
391 // Bg-image debug
392 if ($bg_img_debug && $bg_image_active)</span>
393 <span class="code-keyword">{
394     $lang['AVC_bg-error'][0] = ($bg_img + 1).'/'.count($bg_imgs).': '.$bg_imgs[$bg_img];
395     $lang['AVC_bg-error'][1] = 'No picture available';
396     $lang['AVC_bg-error'][2] = 'Wrong datatype';
397     $lang['AVC_bg-error'][3] = $bg_imgs[$bg_img].' corrupt';
398
399     $bg_img_type = ($bg_img_type && $bg_err) ? ' ('.$bg_img_type.')' : '';
400     imagestring($image, 4, 2, $total_height-15, $lang['AVC_bg-error'][$bg_err].$bg_img_type, $white);
401     imagestring($image, 4, 5, $total_height-15, $lang['AVC_bg-error'][$bg_err].$bg_img_type, $white);
402     imagestring($image, 4, 3, $total_height-18, $lang['AVC_bg-error'][$bg_err].$bg_img_type, $white);
403     imagestring($image, 4, 4, $total_height-17, $lang['AVC_bg-error'][$bg_err].$bg_img_type, $gray);
404     imagestring($image, 4, 3, $total_height-16, $lang['AVC_bg-error'][$bg_err].$bg_img_type, $black);
405 }
406
407 // Display
408 header("Last-Modified: " . gmdate("D, d M Y H:i:s") ." GMT");
409 header("Pragma: no-cache");
410 header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate");</span>
411 <span class="code-lang">(!$jpeg) ? header("Content-Type: image/png") : header("Content-Type: image/jpeg");
412
413 (!$jpeg) ? imagepng($image) : imagejpeg($image, '', $img_quality);
414 imagedestroy($image);
415 gc();
416
417 ///////////////</span>
418 <span class="code-comment">// Functions //
419 ///////////////
420 function mergeimages($image, $total_height, $total_width, $bg_image_file, $bg_image_width, $bg_image_height, $bg_img_type, $bg_transition)</span>
421 <span class="code-keyword">{
422     switch( $bg_img_type )
423     {
424         case 'jpg':
425             $bg_image = imageCreateFromJPEG($bg_image_file);
426             break;
427         case 'png':
428             $bg_image = imageCreateFromPNG($bg_image_file);
429             break;
430         default:
431             $bg_err = true;
432             break;
433     }
434     if (!$bg_err)
435     {
436         imageinterlace($bg_image, 1);
437         $source_x = ($bg_image_width > $total_width) ? rand(0, $bg_image_width - $total_width) : 0;
438         $source_y = ($bg_image_height > $total_height) ? rand(0, $bg_image_height - $total_height) : 0;
439         $dest_x1 = ($bg_image_width < $total_width) ? rand(0, $total_width - $bg_image_width) : 0;
440         $dest_y1 = ($bg_image_height < $total_height) ? rand(0, $total_height - $bg_image_height) : 0;
441         $dest_x2 = ($bg_image_width < $total_width) ? $bg_image_width : $total_width;
442         $dest_y2 = ($bg_image_height < $total_height) ? $bg_image_height : $total_height;
443
444         imageCopyMerge($image, $bg_image, $dest_x1, $dest_y1, $source_x, $source_y, $dest_x2, $dest_y2, $bg_transition);
445         ImageDestroy($bg_image);
446     }
447 }
448
449 function hex_to_rgb($hex)
450 {
451     $hex = str_replace('#', '', strtoupper($hex));
452     if (strlen($hex) != 6)
453     {
454         if (strlen($hex) == 3)
455         {
456             $hex = $hex{0}.$hex{0}.$hex{1}.$hex{1}.$hex{2}.$hex{2};
457         } else {
458             return '239,239,239';
459         }
460     }
461
462     $rgb = array();
463     $rgb['r'] = hexdec($hex{0}.$hex{1});
464     $rgb['g'] = hexdec($hex{2}.$hex{3});
465     $rgb['b'] = hexdec($hex{4}.$hex{5});
466
467     return $rgb['r'].','.$rgb['g'].','.$rgb['b'];
468 }
469
470 // Function  gdVersion by Hagan Fox</span>
471 <span class="code-comment">// http://de3.php.net/manual/en/function.gd-info.php#52481
472 function gdVersion($user_ver = 0)</span>
473 <span class="code-keyword">{
474     if (! extension_loaded('gd')) { return; }
475     static $gd_ver = 0;
476     // Just accept the specified setting if it's 1.
477     if ($user_ver == 1) { $gd_ver = 1; return 1; }
478     // Use the static variable if function was called previously.
479     if ($user_ver !=2 && $gd_ver > 0 ) { return $gd_ver; }
480     // Use the gd_info() function if possible.
481     if (function_exists('gd_info'))
482     {
483         $ver_info = gd_info();
484         preg_match('/\d/', $ver_info['GD Version'], $match);
485         $gd_ver = $match[0];
486         return $match[0];
487     }
488     // If phpinfo() is disabled use a specified / fail-safe choice...
489     if (preg_match('/phpinfo/', ini_get('disable_functions')))
490     {
491         if ($user_ver == 2) {
492             $gd_ver = 2;
493             return 2;
494         } else {
495             $gd_ver = 1;
496             return 1;
497         }
498     }
499     // ...otherwise use phpinfo().
500     ob_start();
501     phpinfo(8);
502     $info = ob_get_contents();
503     ob_end_clean();
504     $info = stristr($info, 'gd version');
505     preg_match('/\d/', $info, $match);
506     $gd_ver = $match[0];
507     return $match[0];
508 } // End gdVersion()
509 ?>
510
Note: See TracBrowser for help on using the browser.