Completed
Branch master (099915)
by Fabio
08:02
created

captcha.php ➔ displayToken()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
nc 24
nop 3
dl 0
loc 51
ccs 0
cts 45
cp 0
crap 42
rs 8.4468
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * CAPTCHA generator script.
4
 *
5
 * @author Qiang Xue <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 * @package Prado\Web\UI\WebControls\assets
9
 */
10
11
namespace Prado\Web\UI\WebControls\assets;
12
13
define('THEME_OPAQUE_BACKGROUND', 0x0001);
14
define('THEME_NOISY_BACKGROUND', 0x0002);
15
define('THEME_HAS_GRID', 0x0004);
16
define('THEME_HAS_SCRIBBLE', 0x0008);
17
define('THEME_MORPH_BACKGROUND', 0x0010);
18
define('THEME_SHADOWED_TEXT', 0x0020);
19
20
require_once(__DIR__ . '/captcha_key.php');
21
22
$token = 'error';
23
$theme = 0;
24
25
if (isset($_GET['options'])) {
26
	$str = base64_decode($_GET['options']);
27
	if (strlen($str) > 32) {
28
		$hash = substr($str, 0, 32);
29
		$str = substr($str, 32);
30
		if (md5($privateKey . $str) === $hash) {
31
			$options = unserialize($str);
32
			$publicKey = $options['publicKey'];
33
			$tokenLength = $options['tokenLength'];
34
			$caseSensitive = $options['caseSensitive'];
35
			$alphabet = $options['alphabet'];
36
			$fontSize = $options['fontSize'];
37
			$theme = $options['theme'];
38
			if (($randomSeed = $options['randomSeed']) > 0) {
39
				srand($randomSeed);
40
			} else {
41
				srand((int) (microtime() * 1000000));
42
			}
43
			$token = generateToken($publicKey, $privateKey, $alphabet, $tokenLength, $caseSensitive);
44
		}
45
	}
46
}
47
48
displayToken($token, $fontSize, $theme);
49
50
function generateToken($publicKey, $privateKey, $alphabet, $tokenLength, $caseSensitive)
51
{
52
	$token = substr(hash2string(md5($publicKey . $privateKey), $alphabet) . hash2string(md5($privateKey . $publicKey), $alphabet), 0, $tokenLength);
53
	return $caseSensitive ? $token : strtoupper($token);
54
}
55
56
function hash2string($hex, $alphabet)
57
{
58
	if (strlen($alphabet) < 2) {
59
		$alphabet = '234578adefhijmnrtABDEFGHJLMNRT';
60
	}
61
	$hexLength = strlen($hex);
62
	$base = strlen($alphabet);
63
	$result = '';
64
	for ($i = 0;$i < $hexLength;$i += 6) {
65
		$number = hexdec(substr($hex, $i, 6));
66
		while ($number) {
67
			$result .= $alphabet[$number % $base];
68
			$number = floor($number / $base);
69
		}
70
	}
71
	return $result;
72
}
73
74
function displayToken($token, $fontSize, $theme)
75
{
76
	if (($fontSize = (int) $fontSize) < 22) {
77
		$fontSize = 22;
78
	}
79
	if ($fontSize > 100) {
80
		$fontSize = 100;
81
	}
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);
0 ignored issues
show
Bug introduced by
$height of type double is incompatible with the type integer expected by parameter $height of imagecreatetruecolor(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
	$image = imagecreatetruecolor($width, /** @scrutinizer ignore-type */ $height);
Loading history...
89
90
	addBackground(
91
		$image,
92
		$width,
93
		$height,
94
		$theme & THEME_OPAQUE_BACKGROUND,
95
		$theme & THEME_NOISY_BACKGROUND,
96
		$theme & THEME_HAS_GRID,
97
		$theme & THEME_HAS_SCRIBBLE,
98
		$theme & THEME_MORPH_BACKGROUND
99
	);
100
101
	$font = __DIR__ . DIRECTORY_SEPARATOR . 'verase.ttf';
102
103
	if (function_exists('imagefilter')) {
104
		imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $image of imagefilter() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
		imagefilter(/** @scrutinizer ignore-type */ $image, IMG_FILTER_GAUSSIAN_BLUR);
Loading history...
105
	}
106
107
	$hasShadow = ($theme & THEME_SHADOWED_TEXT);
108
	for ($i = 0;$i < $length;$i++) {
109
		$color = imagecolorallocate($image, rand(150, 220), rand(150, 220), rand(150, 220));
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $image of imagecolorallocate() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
		$color = imagecolorallocate(/** @scrutinizer ignore-type */ $image, rand(150, 220), rand(150, 220), rand(150, 220));
Loading history...
110
		$size = rand($fontWidth - 10, $fontWidth);
111
		$angle = rand(-30, 30);
112
		$x = $padding + $i * $fontWidth;
113
		$y = rand($fontHeight - 15, $fontHeight - 10);
0 ignored issues
show
Bug introduced by
$fontHeight - 10 of type double is incompatible with the type integer expected by parameter $max of rand(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
		$y = rand($fontHeight - 15, /** @scrutinizer ignore-type */ $fontHeight - 10);
Loading history...
Bug introduced by
$fontHeight - 15 of type double is incompatible with the type integer expected by parameter $min of rand(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
		$y = rand(/** @scrutinizer ignore-type */ $fontHeight - 15, $fontHeight - 10);
Loading history...
114
		imagettftext($image, $size, $angle, $x, $y, $color, $font, $token[$i]);
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $image of imagettftext() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
		imagettftext(/** @scrutinizer ignore-type */ $image, $size, $angle, $x, $y, $color, $font, $token[$i]);
Loading history...
115
		if ($hasShadow) {
116
			imagettftext($image, $size, $angle, $x + 2, $y + 2, $color, $font, $token[$i]);
117
		}
118
		imagecolordeallocate($image, $color);
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $image of imagecolordeallocate() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
		imagecolordeallocate(/** @scrutinizer ignore-type */ $image, $color);
Loading history...
119
	}
120
121
	header('Content-Type: image/png');
122
	imagepng($image);
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $image of imagepng() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

122
	imagepng(/** @scrutinizer ignore-type */ $image);
Loading history...
123
	imagedestroy($image);
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $image of imagedestroy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
	imagedestroy(/** @scrutinizer ignore-type */ $image);
Loading history...
124
}
125
126
function addBackground($image, $width, $height, $opaque, $noisy, $hasGrid, $hasScribble, $morph)
127
{
128
	$background = imagecreatetruecolor($width * 2, $height * 2);
129
	$white = imagecolorallocate($background, 255, 255, 255);
0 ignored issues
show
Bug introduced by
It seems like $background can also be of type false; however, parameter $image of imagecolorallocate() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
	$white = imagecolorallocate(/** @scrutinizer ignore-type */ $background, 255, 255, 255);
Loading history...
130
	imagefill($background, 0, 0, $white);
0 ignored issues
show
Bug introduced by
It seems like $background can also be of type false; however, parameter $image of imagefill() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
	imagefill(/** @scrutinizer ignore-type */ $background, 0, 0, $white);
Loading history...
131
132
	if ($opaque) {
133
		imagefill($background, 0, 0, imagecolorallocate($background, 100, 100, 100));
134
	}
135
136
	if ($noisy) {
137
		addNoise($background, $width * 2, $height * 2);
138
	}
139
140
	if ($hasGrid) {
141
		addGrid($background, $width * 2, $height * 2);
142
	}
143
144
	if ($hasScribble) {
145
		addScribble($background, $width * 2, $height * 2);
146
	}
147
148
	if ($morph) {
149
		morphImage($background, $width * 2, $height * 2);
150
	}
151
152
	imagecopy($image, $background, 0, 0, 30, 30, $width, $height);
0 ignored issues
show
Bug introduced by
It seems like $background can also be of type false; however, parameter $src_im of imagecopy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
	imagecopy($image, /** @scrutinizer ignore-type */ $background, 0, 0, 30, 30, $width, $height);
Loading history...
153
154
	if (!$opaque) {
155
		imagecolortransparent($image, $white);
156
	}
157
}
158
159
function addNoise($image, $width, $height)
160
{
161
	for ($x = 0;$x < $width;++$x) {
162
		for ($y = 0;$y < $height;++$y) {
163
			if (rand(0, 100) < 25) {
164
				$color = imagecolorallocate($image, rand(150, 220), rand(150, 220), rand(150, 220));
165
				imagesetpixel($image, $x, $y, $color);
166
				imagecolordeallocate($image, $color);
167
			}
168
		}
169
	}
170
}
171
172
function addGrid($image, $width, $height)
173
{
174
	for ($i = 0;$i < $width;$i += rand(15, 25)) {
175
		imagesetthickness($image, rand(2, 6));
176
		$color = imagecolorallocate($image, rand(100, 180), rand(100, 180), rand(100, 180));
177
		imageline($image, $i + rand(-10, 20), 0, $i + rand(-10, 20), $height, $color);
178
		imagecolordeallocate($image, $color);
179
	}
180
	for ($i = 0;$i < $height;$i += rand(15, 25)) {
181
		imagesetthickness($image, rand(2, 6));
182
		$color = imagecolorallocate($image, rand(100, 180), rand(100, 180), rand(100, 180));
183
		imageline($image, 0, $i + rand(-10, 20), $width, $i + rand(-10, 20), $color);
184
		imagecolordeallocate($image, $color);
185
	}
186
}
187
188
function addScribble($image, $width, $height)
0 ignored issues
show
Unused Code introduced by
The parameter $width is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

188
function addScribble($image, /** @scrutinizer ignore-unused */ $width, $height)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
189
{
190
	for ($i = 0;$i < 8;$i++) {
191
		$color = imagecolorallocate($image, rand(100, 180), rand(100, 180), rand(100, 180));
192
		$points = [];
193
		for ($j = 1;$j < rand(5, 10);$j++) {
194
			$points[] = rand(2 * (20 * ($i + 1)), 2 * (50 * ($i + 1)));
195
			$points[] = rand(30, $height + 30);
196
		}
197
		imagesetthickness($image, rand(2, 6));
198
		imagepolygon($image, $points, (int) (count($points) / 2), $color);
199
		imagecolordeallocate($image, $color);
200
	}
201
}
202
203
function morphImage($image, $width, $height)
204
{
205
	$tempImage = imagecreatetruecolor($width, $height);
206
	$chunk = rand(1, 5);
0 ignored issues
show
Unused Code introduced by
The assignment to $chunk is dead and can be removed.
Loading history...
207
	for ($x = $y = 0;$x < $width;$x += $chunk) {
208
		$chunk = rand(1, 5);
209
		$y += rand(-1, 1);
210
		if ($y >= $height) {
211
			$y = $height - 5;
212
		}
213
		if ($y < 0) {
214
			$y = 5;
215
		}
216
		imagecopy($tempImage, $image, $x, 0, $x, $y, $chunk, $height);
0 ignored issues
show
Bug introduced by
It seems like $tempImage can also be of type false; however, parameter $dst_im of imagecopy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

216
		imagecopy(/** @scrutinizer ignore-type */ $tempImage, $image, $x, 0, $x, $y, $chunk, $height);
Loading history...
217
	}
218
	for ($x = $y = 0;$y < $height;$y += $chunk) {
219
		$chunk = rand(1, 5);
220
		$x += rand(-1, 1);
221
		if ($x >= $width) {
222
			$x = $width - 5;
223
		}
224
		if ($x < 0) {
225
			$x = 5;
226
		}
227
		imagecopy($image, $tempImage, $x, $y, 0, $y, $width, $chunk);
0 ignored issues
show
Bug introduced by
It seems like $tempImage can also be of type false; however, parameter $src_im of imagecopy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

227
		imagecopy($image, /** @scrutinizer ignore-type */ $tempImage, $x, $y, 0, $y, $width, $chunk);
Loading history...
228
	}
229
}
230