Project

General

Profile

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
?>
(5-5/11)