Completed
Push — master ( 9aaf17...bab7fd )
by Fabio
13s
created

captcha.php ➔ addBackground()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 7
eloc 17
nc 64
nop 8
dl 0
loc 26
rs 6.7272

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * CAPTCHA generator script.
4
 *
5
 * @author Qiang Xue <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
8
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9
 * @package System.Web.UI.WebControls.assets
10
 */
11
12
define('THEME_OPAQUE_BACKGROUND',0x0001);
13
define('THEME_NOISY_BACKGROUND',0x0002);
14
define('THEME_HAS_GRID',0x0004);
15
define('THEME_HAS_SCRIBBLE',0x0008);
16
define('THEME_MORPH_BACKGROUND',0x0010);
17
define('THEME_SHADOWED_TEXT',0x0020);
18
19
require_once(dirname(__FILE__).'/captcha_key.php');
20
21
$token='error';
22
$theme=0;
23
24
if(isset($_GET['options']))
25
{
26
	$str=base64_decode($_GET['options']);
27
	if(strlen($str)>32)
28
	{
29
		$hash=substr($str,0,32);
30
		$str=substr($str,32);
31
		if(md5($privateKey.$str)===$hash)
32
		{
33
			$options=unserialize($str);
34
			$publicKey=$options['publicKey'];
35
			$tokenLength=$options['tokenLength'];
36
			$caseSensitive=$options['caseSensitive'];
37
			$alphabet=$options['alphabet'];
38
			$fontSize=$options['fontSize'];
39
			$theme=$options['theme'];
40
			if(($randomSeed=$options['randomSeed'])>0)
41
				srand($randomSeed);
42
			else
43
				srand((int)(microtime()*1000000));
44
			$token=generateToken($publicKey,$privateKey,$alphabet,$tokenLength,$caseSensitive);
45
		}
46
	}
47
}
48
49
displayToken($token,$fontSize,$theme);
50
51
function generateToken($publicKey,$privateKey,$alphabet,$tokenLength,$caseSensitive)
52
{
53
	$token=substr(hash2string(md5($publicKey.$privateKey),$alphabet).hash2string(md5($privateKey.$publicKey),$alphabet),0,$tokenLength);
54
	return $caseSensitive?$token:strtoupper($token);
55
}
56
57
function hash2string($hex,$alphabet)
58
{
59
	if(strlen($alphabet)<2)
60
		$alphabet='234578adefhijmnrtABDEFGHJLMNRT';
61
	$hexLength=strlen($hex);
62
	$base=strlen($alphabet);
63
	$result='';
64
	for($i=0;$i<$hexLength;$i+=6)
65
	{
66
		$number=hexdec(substr($hex,$i,6));
67
		while($number)
68
		{
69
			$result.=$alphabet[$number%$base];
70
			$number=floor($number/$base);
71
		}
72
	}
73
	return $result;
74
}
75
76
function displayToken($token,$fontSize,$theme)
77
{
78
	if(($fontSize=(int)$fontSize)<22)
79
		$fontSize=22;
80
	if($fontSize>100)
81
		$fontSize=100;
82
	$length=strlen($token);
83
	$padding=10;
84
	$fontWidth=$fontSize;
85
	$fontHeight=floor($fontWidth*1.5);
86
	$width=$fontWidth*$length+$padding*2;
87
	$height=$fontHeight;
88
	$image=imagecreatetruecolor($width,$height);
89
90
	addBackground
91
	(
92
		$image, $width, $height,
93
		$theme&THEME_OPAQUE_BACKGROUND,
94
		$theme&THEME_NOISY_BACKGROUND,
95
		$theme&THEME_HAS_GRID,
96
		$theme&THEME_HAS_SCRIBBLE,
97
		$theme&THEME_MORPH_BACKGROUND
98
	);
99
100
	$font=dirname(__FILE__).DIRECTORY_SEPARATOR.'verase.ttf';
101
102
	if(function_exists('imagefilter'))
103
    	imagefilter($image,IMG_FILTER_GAUSSIAN_BLUR);
104
105
	$hasShadow=($theme&THEME_SHADOWED_TEXT);
106
    for($i=0;$i<$length;$i++)
107
	{
108
        $color=imagecolorallocate($image,rand(150,220),rand(150,220),rand(150,220));
109
        $size=rand($fontWidth-10,$fontWidth);
110
        $angle=rand(-30,30);
111
        $x=$padding+$i*$fontWidth;
112
        $y=rand($fontHeight-15,$fontHeight-10);
113
        imagettftext($image,$size,$angle,$x,$y,$color,$font,$token[$i]);
114
        if($hasShadow)
115
        	imagettftext($image,$size,$angle,$x+2,$y+2,$color,$font,$token[$i]);
116
        imagecolordeallocate($image,$color);
117
    }
118
119
	header('Content-Type: image/png');
120
	imagepng($image);
121
	imagedestroy($image);
122
}
123
124
function addBackground($image,$width,$height,$opaque,$noisy,$hasGrid,$hasScribble,$morph)
125
{
126
	$background=imagecreatetruecolor($width*2,$height*2);
127
	$white=imagecolorallocate($background,255,255,255);
128
	imagefill($background,0,0,$white);
129
130
	if($opaque)
131
		imagefill($background,0,0,imagecolorallocate($background,100,100,100));
132
133
	if($noisy)
134
		addNoise($background,$width*2,$height*2);
135
136
	if($hasGrid)
137
		addGrid($background,$width*2,$height*2);
138
139
	if($hasScribble)
140
		addScribble($background,$width*2,$height*2);
141
142
	if($morph)
143
		morphImage($background,$width*2,$height*2);
144
145
	imagecopy($image,$background,0,0,30,30,$width,$height);
146
147
	if(!$opaque)
148
		imagecolortransparent($image,$white);
149
}
150
151
function addNoise($image,$width,$height)
152
{
153
	for($x=0;$x<$width;++$x)
154
	{
155
		for($y=0;$y<$height;++$y)
156
		{
157
			if(rand(0,100)<25)
158
			{
159
				$color=imagecolorallocate($image,rand(150,220),rand(150,220),rand(150,220));
160
				imagesetpixel($image,$x,$y,$color);
161
	            imagecolordeallocate($image,$color);
162
	        }
163
		}
164
	}
165
}
166
167
function addGrid($image,$width,$height)
168
{
169
	for($i=0;$i<$width;$i+=rand(15,25))
170
	{
171
		imagesetthickness($image,rand(2,6));
172
		$color=imagecolorallocate($image,rand(100,180),rand(100,180),rand(100,180));
173
		imageline($image,$i+rand(-10,20),0,$i+rand(-10,20),$height,$color);
174
		imagecolordeallocate($image,$color);
175
	}
176
	for($i=0;$i<$height;$i+=rand(15,25))
177
	{
178
		imagesetthickness($image,rand(2,6));
179
		$color=imagecolorallocate($image,rand(100,180),rand(100,180),rand(100,180));
180
		imageline($image,0,$i+rand(-10,20),$width,$i+rand(-10,20),$color);
181
		imagecolordeallocate($image,$color);
182
	}
183
}
184
185
function addScribble($image,$width,$height)
186
{
187
	for($i=0;$i<8;$i++)
188
	{
189
		$color=imagecolorallocate($image,rand(100,180),rand(100,180),rand(100,180));
190
		$points=array();
191
		for($j=1;$j<rand(5,10);$j++)
192
		{
193
			$points[]=rand(2*(20*($i+1)),2*(50*($i+1)));
194
			$points[]=rand(30,$height+30);
195
		}
196
		imagesetthickness($image,rand(2,6));
197
		imagepolygon($image,$points,intval(sizeof($points)/2),$color);
198
		imagecolordeallocate($image,$color);
199
	}
200
}
201
202
function morphImage($image,$width,$height)
203
{
204
	$tempImage=imagecreatetruecolor($width,$height);
205
	$chunk=rand(1,5);
0 ignored issues
show
Unused Code introduced by
$chunk is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
206
	for($x=$y=0;$x<$width;$x+=$chunk)
207
	{
208
		$chunk=rand(1,5);
209
		$y+=rand(-1,1);
210
		if($y>=$height)	$y=$height-5;
211
		if($y<0) $y=5;
212
		imagecopy($tempImage,$image,$x,0,$x,$y,$chunk,$height);
213
	}
214
	for($x=$y=0;$y<$height;$y+=$chunk)
215
	{
216
		$chunk=rand(1,5);
217
		$x+=rand(-1,1);
218
		if($x>=$width)	$x=$width-5;
219
		if($x<0) $x=5;
220
		imagecopy($image,$tempImage,$x,$y,0,$y,$width,$chunk);
221
	}
222
}
223
224