Project

General

Profile

1
<?php
2

    
3
// $Id: calc_ttf_image.php 1693 2012-08-18 11:10:01Z Luisehahne $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2009, 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('missing captcha_time');
31
//unset($_SESSION['captcha_time']);		// otherwise there can't be 2 captchas on one page!
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(preg_match('/\.ttf/',$file)) { $fonts[]=$file; } }
40
foreach($t_bgs as $file) { if(preg_match('/\.png/',$file)) { $bgs[]=$file; } }
41

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

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

    
76
// create reload-image
77
$reload = ImageCreateFromPNG(WB_PATH.'/include/captcha/reload_140_40.png'); // reload-overlay
78

    
79
if(mt_rand(0,2)==0) { // 1 out of 3
80

    
81
	// draw each character individualy
82
	$image = ImageCreateFromPNG($bg); // background image
83
	$grey = mt_rand(0,50);
84
	$color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color
85
	$ttf = $font;
86
	$ttfsize = 25; // fontsize
87
	$count = 0;
88
	$image_failed = true;
89
	$angle = mt_rand(-10,10);
90
	$x = mt_rand(20,35);
91
	$y = mt_rand($height-10,$height-2);
92
	do {
93
		for($i=0;$i<strlen($text);$i++) {
94
			$res = imagettftext($image, $ttfsize, $angle, $x, $y, $color, $ttf, $text{$i});
95
			$angle = mt_rand(-10,10);
96
			$x = mt_rand($res[4],$res[4]+10);
97
			$y = mt_rand($height-12,$height-7);
98
		}
99
		if($res[4] > $width) {
100
			$image_failed = true;
101
		} else {
102
			$image_failed = false;
103
		}
104
		if(++$count > 4) // too many tries! Use the image
105
			break;
106
	} while($image_failed);
107

    
108
} else {
109

    
110
	// draw whole string at once
111
	$image_failed = true;
112
	$count=0;
113
	do {
114
		$image = ImageCreateFromPNG($bg); // background image
115
		$grey = mt_rand(0,50);
116
		$color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color
117
		$ttf = $font;
118
		$ttfsize = 25; // fontsize
119
		$angle = mt_rand(0,5);
120
		$x = mt_rand(20,35);
121
		$y = mt_rand($height-10,$height-2);
122
		$res = imagettftext($image, $ttfsize, $angle, $x, $y, $color, $ttf, $text);
123
		// check if text fits into the image
124
		if(($res[0]>0 && $res[0]<$width) && ($res[1]>0 && $res[1]<$height) &&
125
			 ($res[2]>0 && $res[2]<$width) && ($res[3]>0 && $res[3]<$height) &&
126
			 ($res[4]>0 && $res[4]<$width) && ($res[5]>0 && $res[5]<$height) &&
127
			 ($res[6]>0 && $res[6]<$width) && ($res[7]>0 && $res[7]<$height)
128
		) {
129
			$image_failed = false;
130
		}
131
		if(++$count > 4) // too many tries! Use the image
132
			break;
133
	} while($image_failed);
134

    
135
}
136

    
137
imagealphablending($reload, TRUE);
138
imagesavealpha($reload, TRUE);
139

    
140
// overlay
141
imagecopy($reload, $image, 0,0,0,0, 140,40);
142
imagedestroy($image);
143
$image = $reload;
144

    
145
captcha_header();
146
ob_start();
147
imagepng($image);
148
header("Content-Length: ".ob_get_length());
149
ob_end_flush();
150
imagedestroy($image);
(5-5/13)