Project

General

Profile

« Previous | Next » 

Revision 615

Added by thorn almost 17 years ago

Added new CAPTCHA (calc_ttf_image). Changed call_captcha() to output a table-layout. Some minor changes to captcha-code.

View differences:

trunk/wb/include/captcha/captchas/ttf_image.php
31 31
unset($_SESSION['captcha_time']);
32 32

  
33 33
// get lists of fonts and backgrounds
34
require_once(WB_PATH.'/search/search_modext.php');
35
list($fonts, $dirs) = list_files_dirs(WB_PATH.'/include/captcha/fonts', false);
36
list($bgs, $dirs) = list_files_dirs(WB_PATH.'/include/captcha/backgrounds', false);
37
$fonts = clear_filelist($fonts, '\.ttf$');
38
$bgs = clear_filelist($bgs, '\.png$');
34
require_once(WB_PATH.'/framework/functions.php');
35
$t_fonts = file_list(WB_PATH.'/include/captcha/fonts');
36
$t_bgs = file_list(WB_PATH.'/include/captcha/backgrounds');
37
$fonts = array();
38
$bgs = array();
39
foreach($t_fonts as $file) if(eregi('\.ttf$',$file)) $fonts[]=$file;
40
foreach($t_bgs as $file) if(eregi('\.png$',$file)) $bgs[]=$file;
39 41

  
40 42
// make random string
41 43
if(!function_exists('randomString')) {
......
61 63
list($width, $height, $type, $attr) = getimagesize($bg);
62 64

  
63 65
// create image
64

  
65 66
$image = ImageCreateFromPNG($bg); // background image
66 67
$grey = rand(0,50);
67 68
$color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color
68 69
$ttf = $font;
69 70
$ttfsize = 25; // fontsize
70
$angle = rand(-15,15);
71
$x = rand(10,25);
72
$y = rand($height-10,$height-2);
73 71

  
74 72
if(rand(0,2)==0) { // 1 out of 3
75 73

  
76 74
	// draw each character individualy
77 75
	$count = 0;
78 76
	$image_failed = true;
77
	$angle = rand(-15,15);
78
	$x = rand(10,25);
79
	$y = rand($height-10,$height-2);
79 80
	do {
80
		for($i=0;$i<5;$i++) {
81
		for($i=0;$i<strlen($text);$i++) {
81 82
			$res = imagettftext($image, $ttfsize, $angle, $x, $y, $color, $ttf, $text{$i});
82 83
			$angle = rand(-15,15);
83 84
			$x = rand($res[4],$res[4]+10);
......
104 105
		$ttf = $font;
105 106
		$ttfsize = 25; // fontsize
106 107
		$angle = rand(0,5);
107
		$t_x = rand(5,30);
108
		$t_y = rand($height-10,$height-2);
109
		$res = imagettftext($image, $ttfsize, $angle, $t_x, $t_y, $color, $ttf, $text);
108
		$x = rand(5,30);
109
		$y = rand($height-10,$height-2);
110
		$res = imagettftext($image, $ttfsize, $angle, $x, $y, $color, $ttf, $text);
110 111
		// check if text fits into the image
111 112
		if(($res[0]>0 && $res[0]<$width) && ($res[1]>0 && $res[1]<$height) && 
112 113
			 ($res[2]>0 && $res[2]<$width) && ($res[3]>0 && $res[3]<$height) && 
......
115 116
		) {
116 117
			$image_failed = false;
117 118
		}
118
		if(++$count > 5) // too many tries! Use the image
119
		if(++$count > 4) // too many tries! Use the image
119 120
			break;
120 121
	} while($image_failed);
121 122
	
trunk/wb/include/captcha/captchas/calc_ttf_image.php
1
<?php
2

  
3
// $Id$
4

  
5
/*
6

  
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2008, Ryan Djurovich
9

  
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14

  
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19

  
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23

  
24
*/
25

  
26
require_once("../../../config.php");
27
require_once(WB_PATH.'/include/captcha/captcha.php');
28

  
29
if(!isset($_SESSION['captcha_time']))
30
	exit;
31
unset($_SESSION['captcha_time']);
32

  
33
// get lists of fonts and backgrounds
34
require_once(WB_PATH.'/framework/functions.php');
35
$t_fonts = file_list(WB_PATH.'/include/captcha/fonts');
36
$t_bgs = file_list(WB_PATH.'/include/captcha/backgrounds');
37
$fonts = array();
38
$bgs = array();
39
foreach($t_fonts as $file) if(eregi('\.ttf$',$file)) $fonts[]=$file;
40
foreach($t_bgs as $file) if(eregi('\.png$',$file)) $bgs[]=$file;
41

  
42
// Captcha
43
$_SESSION['captcha'] = '';
44
mt_srand((double)microtime()*1000000);
45
$n = mt_rand(1,3);
46
switch ($n) {
47
	case 1:
48
		$x = mt_rand(1,9);
49
		$y = mt_rand(1,9);
50
		$_SESSION['captcha'] = $x + $y;
51
		$cap = "$x+$y"; 
52
		break; 
53
	case 2:
54
		$x = mt_rand(10,20);
55
		$y = mt_rand(1,9);
56
		$_SESSION['captcha'] = $x - $y; 
57
		$cap = "$x-$y"; 
58
		break;
59
	case 3:
60
		$x = mt_rand(2,10);
61
		$y = mt_rand(2,5);
62
		$_SESSION['captcha'] = $x * $y; 
63
		$cap = "$x*$y"; 
64
		break;
65
}
66
$text = $cap;
67

  
68
// choose a font and background
69
$font = $fonts[array_rand($fonts)];
70
$bg = $bgs[array_rand($bgs)];
71
// get image-dimensions
72
list($width, $height, $type, $attr) = getimagesize($bg);
73

  
74
// create image
75
$image = ImageCreateFromPNG($bg); // background image
76
$grey = rand(0,50);
77
$color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color
78
$ttf = $font;
79
$ttfsize = 25; // fontsize
80

  
81
if(rand(0,2)==0) { // 1 out of 3
82

  
83
	// draw each character individualy
84
	$count = 0;
85
	$image_failed = true;
86
	$angle = rand(-10,10);
87
	$x = rand(20,35);
88
	$y = rand($height-10,$height-2);
89
	do {
90
		for($i=0;$i<strlen($text);$i++) {
91
			$res = imagettftext($image, $ttfsize, $angle, $x, $y, $color, $ttf, $text{$i});
92
			$angle = rand(-10,10);
93
			$x = rand($res[4],$res[4]+10);
94
			$y = rand($height-12,$height-7);
95
		}
96
		if($res[4] > $width) {
97
			$image_failed = true;
98
		} else {
99
			$image_failed = false;
100
		}
101
		if(++$count > 4) // too many tries! Use the image
102
			break;
103
	} while($image_failed);
104
	
105
} else {
106
	
107
	// draw whole string at once
108
	$image_failed = true;
109
	$count=0;
110
	do {
111
		$image = ImageCreateFromPNG($bg); // background image
112
		$grey = rand(0,50);
113
		$color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color
114
		$angle = rand(0,5);
115
		$x = rand(20,35);
116
		$y = rand($height-10,$height-2);
117
		$res = imagettftext($image, $ttfsize, $angle, $x, $y, $color, $ttf, $text);
118
		// check if text fits into the image
119
		if(($res[0]>0 && $res[0]<$width) && ($res[1]>0 && $res[1]<$height) && 
120
			 ($res[2]>0 && $res[2]<$width) && ($res[3]>0 && $res[3]<$height) && 
121
			 ($res[4]>0 && $res[4]<$width) && ($res[5]>0 && $res[5]<$height) && 
122
			 ($res[6]>0 && $res[6]<$width) && ($res[7]>0 && $res[7]<$height)
123
		) {
124
			$image_failed = false;
125
		}
126
		if(++$count > 4) // too many tries! Use the image
127
			break;
128
	} while($image_failed);
129
	
130
}
131

  
132
captcha_header();
133
ob_start();
134
imagepng($image);
135
header("Content-Length: ".ob_get_length()); 
136
ob_end_flush();
137
imagedestroy($image);
138

  
139
?>
0 140

  
trunk/wb/include/captcha/captchas/calc_image.php
32 32

  
33 33
// Captcha
34 34
$_SESSION['captcha'] = '';
35
mt_srand((double)microtime()*100000);
35
mt_srand((double)microtime()*1000000);
36 36
$n = mt_rand(1,3);
37 37
switch ($n) {
38 38
	case 1:
39
		mt_srand((double)microtime()*1000000);
40 39
		$x = mt_rand(1,9);
41 40
		$y = mt_rand(1,9);
42 41
		$_SESSION['captcha'] = $x + $y;
43 42
		$cap = "$x+$y"; 
44 43
		break; 
45 44
	case 2:
46
		mt_srand((double)microtime()*1000000);
47 45
		$x = mt_rand(10,20);
48 46
		$y = mt_rand(1,9);
49 47
		$_SESSION['captcha'] = $x - $y; 
50 48
		$cap = "$x-$y"; 
51 49
		break;
52 50
	case 3:
53
		mt_srand((double)microtime()*1000000);
54 51
		$x = mt_rand(2,10);
55 52
		$y = mt_rand(2,5);
56 53
		$_SESSION['captcha'] = $x * $y; 
trunk/wb/include/captcha/captchas/calc_text.php
27 27
if(defined('WB_PATH') == false) { exit("Cannot access this file directly"); }
28 28

  
29 29
$_SESSION['captcha'] = '';
30
mt_srand((double)microtime()*100000);
30
mt_srand((double)microtime()*1000000);
31 31
$n = mt_rand(1,3);
32 32
switch ($n) {
33 33
	case 1:
34
		mt_srand((double)microtime()*1000000);
35 34
		$x = mt_rand(1,9);
36 35
		$y = mt_rand(1,9);
37 36
		$_SESSION['captcha'] = $x + $y;
38 37
		$cap = "$x {$MOD_CAPTCHA['ADDITION']} $y"; 
39 38
		break; 
40 39
	case 2:
41
		mt_srand((double)microtime()*1000000);
42 40
		$x = mt_rand(10,20);
43 41
		$y = mt_rand(1,9);
44 42
		$_SESSION['captcha'] = $x - $y; 
45 43
		$cap = "$x {$MOD_CAPTCHA['SUBTRAKTION']} $y"; 
46 44
		break;
47 45
	case 3:
48
		mt_srand((double)microtime()*1000000);
49 46
		$x = mt_rand(2,10);
50 47
		$y = mt_rand(2,5);
51 48
		$_SESSION['captcha'] = $x * $y; 
trunk/wb/include/captcha/captcha.php
23 23

  
24 24
*/
25 25

  
26
/* CAPTCHA - How to use:
27
 * use 
28
 *	require_once(WB_PATH.'/include/captcha/captcha.php');
29
 * and put 
30
 *	<?php call_captcha(); ?>
31
 * into your form.
32
 *
33
 * captcha-code is in $_SESSION['captcha']
34
 * user input in $_POST['captcha']
35
 */
36

  
37 26
// Must include code to stop this file being accessed directly
38 27
if(!defined('WB_PATH')) { exit("Cannot access this file directly"); }
39 28

  
......
70 59
	$useable_captchas = array(
71 60
		'calc_text'=>$MOD_CAPTCHA_CONTROL['CALC_TEXT'],
72 61
		'calc_image'=>$MOD_CAPTCHA_CONTROL['CALC_IMAGE'],
62
		'calc_ttf_image'=>$MOD_CAPTCHA_CONTROL['CALC_TTF_IMAGE'],
73 63
		'ttf_image'=>$MOD_CAPTCHA_CONTROL['TTF_IMAGE'],
74 64
		'old_image'=>$MOD_CAPTCHA_CONTROL['OLD_IMAGE']
75 65
	);
......
94 84
		switch(CAPTCHA_TYPE) {
95 85
			// two special cases
96 86
			case 'calc_text': // calculation as text
97
				?>
98
				<?php include(WB_PATH.'/include/captcha/captchas/calc_text.php'); ?> = 
99
				<input type="text" name="captcha" maxlength="5"  style="width:20px" />&nbsp;&nbsp;<?php echo $MOD_CAPTCHA['VERIFICATION_INFO_RES']; ?></font>
100
				<?php
87
				?><table class="captcha_table"><tr>
88
				<td><?php include(WB_PATH.'/include/captcha/captchas/calc_text.php'); ?>&nbsp;=&nbsp;</td>
89
				<td><input type="text" name="captcha" maxlength="5"  style="width:20px" /></td>
90
				<td class="captcha_expl"><?php echo $MOD_CAPTCHA['VERIFICATION_INFO_RES']; ?></td>
91
				</tr></table><?php
101 92
				break;
102 93
			case 'calc_image': // calculation with image (old captcha)
103
				?>
104
				<img src="<?php echo WB_URL."/include/captcha/captchas/calc_image.php?t=$t"; ?>" align="middle" alt="Captcha" /> = 
105
				<input type="text" name="captcha" maxlength="5" style="width:20px" />&nbsp;&nbsp;<?php echo $MOD_CAPTCHA['VERIFICATION_INFO_RES']; ?></font>
106
				<?php
94
				?><table class="captcha_table"><tr>
95
				<td><img src="<?php echo WB_URL."/include/captcha/captchas/calc_image.php?t=$t"; ?>" alt="Captcha" />&nbsp;=&nbsp;</td>
96
				<td><input type="text" name="captcha" maxlength="5" style="width:20px" /></td>
97
				<td class="captcha_expl"><?php echo $MOD_CAPTCHA['VERIFICATION_INFO_RES']; ?></td>
98
				</tr></table><?php
107 99
				break;
108 100
			// normal images
109 101
			case 'ttf_image': // captcha with varying background and ttf-font
102
			case 'calc_ttf_image': // calculation with varying background and ttf-font
110 103
			case 'old_image': // old captcha
111
				?>
112
				<img src="<?php echo WB_URL.'/include/captcha/captchas/'.CAPTCHA_TYPE.".php?t=$t"; ?>" align="middle" alt="Captcha" />
113
				<input type="text" name="captcha" maxlength="5" style="width:50px" />&nbsp;&nbsp;<?php echo $MOD_CAPTCHA['VERIFICATION_INFO_TEXT']; ?></font>
114
				<?php
104
				?><table class="captcha_table"><tr>
105
				<td><img src="<?php echo WB_URL.'/include/captcha/captchas/'.CAPTCHA_TYPE.".php?t=$t"; ?>" alt="Captcha" /></td>
106
				<td><input type="text" name="captcha" maxlength="5" style="width:50px" /></td>
107
				<td class="captcha_expl"><?php echo $MOD_CAPTCHA['VERIFICATION_INFO_TEXT']; ?></td>
108
				</tr></table><?php
115 109
				break;
116 110
		}
117 111
	}
118 112
}
119
?>
113
?>
trunk/wb/include/captcha/readme.txt
26 26
- and by adding TrueType-fonts to fonts/
27 27

  
28 28

  
29
The CAPTCHA-string is allways stored in $_SESSION['captcha']
30
The input-field is called "captcha", too.
29
How to use:
30
use 
31
	require_once(WB_PATH.'/include/captcha/captcha.php'); // will output a table with 3 columns: |CAPTCHA|Input|Text|
32
and put 
33
	<?php call_captcha(); ?>
34
into your form.
31 35

  
36

  
37
The CAPTCHA-code is allways stored in $_SESSION['captcha']
38
The user-input is in $_POST['captcha'] (or $_GET['captcha']).
39

  
40

  
41
call_captcha() will output code like this
42
<table class="captcha_table"><tr>
43
  <td><img src="<?php echo WB_URL.'/include/captcha/captchas/'.CAPTCHA_TYPE.".php?t=$t"; ?>" alt="Captcha" /></td>
44
  <td><input type="text" name="captcha" maxlength="5" style="width:50px" /></td>
45
  <td class="captcha_expl"><?php echo $MOD_CAPTCHA['VERIFICATION_INFO_TEXT']; ?></td>
46
</tr></table>
trunk/wb/modules/captcha_control/tool.php
74 74
	pics["calc_image"] = new Image();
75 75
	pics["calc_image"].src = "<?php echo WB_URL.'/include/captcha/captchas/calc_image.png'?>";
76 76
	
77
	pics["calc_ttf_image"] = new Image();
78
	pics["calc_ttf_image"].src = "<?php echo WB_URL.'/include/captcha/captchas/calc_ttf_image.png'?>";
79

  
77 80
	pics["old_image"] = new Image();
78 81
	pics["old_image"].src = "<?php echo WB_URL.'/include/captcha/captchas/old_image.png'?>";
79 82
	
80 83
	pics["calc_text"] = new Image();
81 84
	pics["calc_text"].src = "<?php echo WB_URL.'/include/captcha/captchas/calc_text.png'?>";
82

  
85
	
86
	function load_captcha_image() {
87
		document.captcha_example.src = pics[document.store_settings.captcha_type.value].src;
88
	}
83 89
</script>
84 90
<?php
85 91

  
......
109 115
		<table>
110 116
			<tr height="50px">
111 117
			<td><?php echo $MOD_CAPTCHA_CONTROL['CAPTCHA_TYPE'];?>:</td>
112
			<td align="right" width="150px"><img name="captcha_example" id="captcha_example" src="<?php echo WB_URL.'/include/captcha/captchas/calc_text.png'?>" onload="javascript: document.captcha_example.src = pics[document.store_settings.captcha_type.value].src;"></td>
118
			<td align="right" width="150px"><img name="captcha_example" id="captcha_example" src="<?php echo WB_URL.'/include/captcha/captchas/'.$captcha_type.'.png'?>" ></td>
113 119
			</tr>
114 120
		</table>
115 121
		<td>
......
129 135
				name="enabled_captcha" value="0"><?php echo $MOD_CAPTCHA_CONTROL['DISABLED'];?>
130 136
		</td>
131 137
	</tr>
138
	<tr><td>&nbsp;</td><td style="font-size:smaller;"><?php echo $MOD_CAPTCHA_CONTROL['CAPTCHA_EXP'];?></td></tr>
132 139
	<tr><td colspan="2"><br /><strong><?php echo $MOD_CAPTCHA_CONTROL['ASP_CONF'];?>:</strong></td></tr>
133 140
	<tr>
134 141
		<td><?php echo $MOD_CAPTCHA_CONTROL['ASP_TEXT'];?>:</td>
......
139 146
				name="enabled_asp" value="0"><?php echo $MOD_CAPTCHA_CONTROL['DISABLED'];?>
140 147
		</td>
141 148
	</tr>
149
	<tr><td>&nbsp;</td><td style="font-size:smaller;"><?php echo $MOD_CAPTCHA_CONTROL['ASP_EXP'];?></td></tr>
142 150
	</table>
143 151
	<input type="submit" name="save_settings" style="margin-top:10px; width:140px;" value="<?php echo $TEXT['SAVE']; ?>" />
144 152
</form>
trunk/wb/modules/captcha_control/languages/EN.php
33 33
// Text and captions of form elements
34 34
$MOD_CAPTCHA_CONTROL['CAPTCHA_CONF']      = 'CAPTCHA Configuration';
35 35
$MOD_CAPTCHA_CONTROL['CAPTCHA_TYPE']      = 'Type of CAPTCHA';
36
$MOD_CAPTCHA_CONTROL['CAPTCHA_EXP']       = 'CAPTCHA settings for modules are located in the respective module settings';
36 37
$MOD_CAPTCHA_CONTROL['USE_SIGNUP_CAPTCHA']= 'Activate CAPTCHA for signup';
37 38
$MOD_CAPTCHA_CONTROL['ENABLED']           = 'Enabled';
38 39
$MOD_CAPTCHA_CONTROL['DISABLED']          = 'Disabled';
39 40
$MOD_CAPTCHA_CONTROL['ASP_CONF']          = 'Advanced Spam Protection Configuration';
40 41
$MOD_CAPTCHA_CONTROL['ASP_TEXT']          = 'Activate ASP (if available)';
42
$MOD_CAPTCHA_CONTROL['ASP_EXP']           = 'ASP tries to determine if a form-inputs was originated from a human or a spam-bot.';
41 43
$MOD_CAPTCHA_CONTROL['CALC_TEXT']         = "Calculation as text";
42
$MOD_CAPTCHA_CONTROL['CALC_IMAGE']        = "Calculation as image"; 
44
$MOD_CAPTCHA_CONTROL['CALC_IMAGE']        = "Calculation as image";
45
$MOD_CAPTCHA_CONTROL['CALC_TTF_IMAGE']    = "Calculation as image with varying fonts and backgrounds"; 
43 46
$MOD_CAPTCHA_CONTROL['TTF_IMAGE']         = "Image with varying fonts and backgrounds";
44 47
$MOD_CAPTCHA_CONTROL['OLD_IMAGE']         = "Old style (not recommended)";
45 48

  
trunk/wb/modules/captcha_control/languages/DE.php
36 36
// Text and captions of form elements
37 37
$MOD_CAPTCHA_CONTROL['CAPTCHA_CONF']      = 'CAPTCHA-Einstellungen';
38 38
$MOD_CAPTCHA_CONTROL['CAPTCHA_TYPE']      = 'CAPTCHA-Typ';
39
$MOD_CAPTCHA_CONTROL['CAPTCHA_EXP']       = 'Die CAPTCHA-Einstellungen für die Module befinden sich in den jeweiligen Modul-Optionen';
39 40
$MOD_CAPTCHA_CONTROL['USE_SIGNUP_CAPTCHA']= 'CAPTCHA f&uuml;r Registrierung aktivieren';
40 41
$MOD_CAPTCHA_CONTROL['ENABLED']           = 'Aktiviert';
41 42
$MOD_CAPTCHA_CONTROL['DISABLED']          = 'Ausgeschaltet';
42
$MOD_CAPTCHA_CONTROL['ASP_CONF']          = 'Advanced Spam Protection Configuration';
43
$MOD_CAPTCHA_CONTROL['ASP_CONF']          = 'Erweiterter-Spam-Schutz (ASP) Einstellungen';
43 44
$MOD_CAPTCHA_CONTROL['ASP_TEXT']          = 'ASP benutzen (wenn im Modul vorhanden)';
45
$MOD_CAPTCHA_CONTROL['ASP_EXP']           = 'ASP versucht anhand der verschiedenen Verhaltensweisen zu erkennen, ob eine Formular-Eingabe von einem Menschen oder einem Spam-Bot kommt.';
44 46
$MOD_CAPTCHA_CONTROL['CALC_TEXT']         = "Rechnung als Text";
45
$MOD_CAPTCHA_CONTROL['CALC_IMAGE']        = "Rechnung als Bild"; 
47
$MOD_CAPTCHA_CONTROL['CALC_IMAGE']        = "Rechnung als Bild";
48
$MOD_CAPTCHA_CONTROL['CALC_TTF_IMAGE']    = "Rechnung als Bild mit wechselnden Schriften und Hintergr&uuml;nden";
46 49
$MOD_CAPTCHA_CONTROL['TTF_IMAGE']         = "Bild mit wechselnden Schriften und Hintergr&uuml;nden";
47 50
$MOD_CAPTCHA_CONTROL['OLD_IMAGE']         = "Alter Stil (nicht empfohlen)";
48 51

  
49
$MOD_CAPTCHA['VERIFICATION']           = 'Verifizieren';
52
$MOD_CAPTCHA['VERIFICATION']           = 'Prüfziffer';
50 53
$MOD_CAPTCHA['ADDITION']               = 'plus';
51 54
$MOD_CAPTCHA['SUBTRAKTION']            = 'minus';
52 55
$MOD_CAPTCHA['MULTIPLIKATION']         = 'mal';

Also available in: Unified diff