PageRenderTime 46ms CodeModel.GetById 1ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 0ms

/www/captcha.php

https://bitbucket.org/wayfarer/verse
PHP | 237 lines | 150 code | 43 blank | 44 comment | 23 complexity | 5e8edb105b23fefe7034ce67aa020c90 MD5 | raw file
  1<?php
  2 // generates captcha image and stores the key in the session
  3// used for candles
  4include("inc/verse.inc.php"); //main header - initializes Verse environment
  5
  6captchaimage();
  7
  8function captchaimage() {
  9
 10    // start session before!
 11
 12    // ##### IMAGE SETTINGS
 13    $width = 200;
 14    $height = 100;
 15
 16
 17    // ##### SET UP IMAGE AND COLORS
 18    $image = imagecreatetruecolor($width, $height);
 19    imagesetthickness($image, 1);
 20    imagealphablending($image, true);
 21    $color_black = imagecolorallocatealpha($image, 0, 0, 0, 0);
 22    $color_black_semi = imagecolorallocatealpha($image, 0, 0, 0, 115);
 23    $color_white = imagecolorallocatealpha($image, 255, 255, 255, 0);
 24    imagefill($image, 0, 0, $color_white);
 25    imagecolortransparent($image, $color_white);
 26
 27
 28    // ##### BUILD RANDOM PASSWORD
 29    $acceptedCharsV = "AEIOUY";
 30    $acceptedCharsC = "BCDFGHJKLMNPQRSTVWXZ";
 31    $wordbuild = array(
 32        "cvcc", "ccvc", "ccvcc", "cvccc", // monosyllabic nominal stems
 33        "cvcvc", "cvcv", "cvccv", "ccvcv" // disyllabic nominal stems
 34    );
 35    $thisword = $wordbuild[mt_rand(0, sizeof($wordbuild) - 1)];
 36    $stringlength = strlen($thisword);
 37    for ($i = 0; $i < $stringlength; $i++) {
 38        if ($thisword[$i] == "c") {
 39            $password .= $acceptedCharsC{mt_rand(0, strlen($acceptedCharsC) - 1)};
 40        }
 41        if ($thisword[$i] == "v") {
 42            $password .= $acceptedCharsV{mt_rand(0, strlen($acceptedCharsV) - 1)};
 43        }
 44    }
 45
 46
 47    // ##### DRAW RANDOM LETTERS
 48    for ($i = 0; $i < 50; $i++) {
 49        $color = imagecolorallocatealpha($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), 110);
 50        imagestring($image, mt_rand(1, 3), mt_rand(-$width * 0.25, $width * 1.25), mt_rand(-$height * 0.25, $height * 1.25),
 51                    $acceptedCharsC{mt_rand(0, strlen($acceptedCharsC) - 1)}, $color);
 52        imagestring($image, mt_rand(1, 3), mt_rand(-$width * 0.25, $width * 1.25), mt_rand(-$height * 0.25, $height * 1.25),
 53                    $acceptedCharsV{mt_rand(0, strlen($acceptedCharsV) - 1)}, $color);
 54    }
 55
 56
 57    // ##### DRAW PASSWORD
 58    for ($i = 0; $i < $stringlength; $i++) {
 59        $buffer = imagecreatetruecolor(40, 40);
 60        imagefill($buffer, 0, 0, $color_white);
 61        imagecolortransparent($buffer, $color_white);
 62
 63        $buffer2 = imagecreatetruecolor(40, 40);
 64        imagefill($buffer2, 0, 0, $color_white);
 65        imagecolortransparent($buffer2, $color_white);
 66
 67        $red = 0;
 68        $green = 0;
 69        $blue = 0;
 70        while ($red + $green + $blue < 400 || $red + $green + $blue > 450) {
 71            $red = mt_rand(0, 255);
 72            $green = mt_rand(0, 255);
 73            $blue = mt_rand(0, 255);
 74        }
 75
 76        $color = imagecolorallocate($buffer, $red, $green, $blue);
 77        imagestring($buffer, 2, 0, 0, substr($password, $i, 1), $color);
 78
 79        imagecopyresized($buffer2, $buffer, 2, -5, 0, 0, mt_rand(30, 40), mt_rand(30, 40), 10, 14);
 80        //  $buffer=imagerotate($buffer2,mt_rand(-25,25),$color_white);
 81        $buffer = imageRotateBicubic($buffer2, mt_rand(-25, 25));
 82
 83        $xpos = $i / $stringlength * ($width - 30) + (($width - 30) / $stringlength / 2) + 5 + mt_rand(-8, 8);
 84        $ypos = (($height - 50) / 2) + 5 + mt_rand(-8, 8);
 85
 86        imagecolortransparent($buffer, $color_white);
 87
 88        imagecopymerge($image, $buffer, $xpos, $ypos, 0, 0, imagesx($buffer), imagesy($buffer), 100);
 89        imagedestroy($buffer);
 90        imagedestroy($buffer2);
 91    }
 92
 93
 94    // ##### DRAW ELLIPSES
 95    for ($i = 0; $i < 12; $i++) {
 96        $color = imagecolorallocatealpha($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200), 110);
 97        imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(10, 40), mt_rand(10, 40), $color);
 98    }
 99
100
101    // ##### DRAW LINES
102    for ($i = 0; $i < 12; $i++) {
103        $color = imagecolorallocatealpha($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200), 110);
104        imagesetthickness($image, mt_rand(8, 20));
105        imageline($image, mt_rand(-$width * 0.25, $width * 1.25), mt_rand(-$height * 0.25, $height * 1.25),
106                  mt_rand(-$width * 0.25, $width * 1.25), mt_rand(-$height * 0.25, $height * 1.25), $color);
107        imagesetthickness($image, 1);
108    }
109
110    /*
111     // ##### WOBBLE HORIZONTALLY
112     $sindivide=mt_rand(1,20);
113     $sinwidth=mt_rand(1,$sindivide)/4;
114     for ($i=0;$i<$height;$i++) {
115      $buffer=imagecreatetruecolor($width,1);
116      imagecopy($buffer,$image,0,0,0,$i,$width,1);
117      imageline($image,0,$i,$width,$i,$color_white);
118      imagecopy($image,$buffer,(sin($i/$sindivide)-.5)*2*$sinwidth,$i,0,0,$width,1);
119      imagedestroy($buffer);
120     }
121
122
123     // ##### WOBBLE VERTICALLY
124     $sindivide=mt_rand(1,20);
125     $sinwidth=mt_rand(1,$sindivide)/4;
126     for ($i=0;$i<$width;$i++) {
127      $buffer=imagecreatetruecolor(1,$height);
128      imagecopy($buffer,$image,0,0,$i,0,1,$height);
129      imageline($image,$i,0,$i,$width,$color_white);
130      imagecopy($image,$buffer,$i,(sin($i/$sindivide)-.5)*2*$sinwidth,0,0,1,$height);
131      imagedestroy($buffer);
132     }
133    */
134
135    // ##### GRADIENT BACKGROUND HORIZONTALLY
136    $red_from = mt_rand(0, 255);
137    $red_to = mt_rand(0, 255);
138    $green_from = mt_rand(0, 255);
139    $green_to = mt_rand(0, 255);
140    $blue_from = mt_rand(0, 255);
141    $blue_to = mt_rand(0, 255);
142
143    for ($i = 0; $i < $height; $i++) {
144        $color = imagecolorallocatealpha($image, $red_from + ($red_to - $red_from) / $height * $i, $green_from + ($green_to - $green_from) / $height * $i, $blue_from + ($blue_to - $blue_from) / $height * $i, 100);
145        imageline($image, 0, $i, $width, $i, $color);
146    }
147
148
149    // ##### GRADIENT BACKGROUND VERTICALLY
150    $red_from = mt_rand(0, 255);
151    $red_to = mt_rand(0, 255);
152    $green_from = mt_rand(0, 255);
153    $green_to = mt_rand(0, 255);
154    $blue_from = mt_rand(0, 255);
155    $blue_to = mt_rand(0, 255);
156
157    for ($i = 0; $i < $width; $i++) {
158        $color = imagecolorallocatealpha($image, $red_from + ($red_to - $red_from) / $width * $i, $green_from + ($green_to - $green_from) / $width * $i, $blue_from + ($blue_to - $blue_from) / $width * $i, 100);
159        imageline($image, $i, 0, $i, $height, $color);
160    }
161
162
163    // ##### TAG
164    $color = imagecolorallocatealpha($image, 255, 255, 255, 90);
165    imagefilledrectangle($image, 1, 1, 146, 8, $color);
166    $color = imagecolorallocatealpha($image, 0, 0, 0, 100);
167    // imagestring($image,1,2,1,"jhfCAPTCHA: find ".strlen($password)." characters",$color);
168
169    // ##### STORE PASSWORD
170    $_SESSION["captcha"] = $password;
171
172    // ##### OUTPUT
173    header('Content-Type: image/png');
174    imagepng($image);
175    imagedestroy($image);
176}
177
178function imageRotateBicubic($src_img, $angle, $bicubic = false) {
179
180    // convert degrees to radians
181    $angle = $angle + 180;
182    $angle = deg2rad($angle);
183
184    $src_x = imagesx($src_img);
185    $src_y = imagesy($src_img);
186
187    $center_x = floor($src_x / 2);
188    $center_y = floor($src_y / 2);
189
190    $rotate = imagecreatetruecolor($src_x, $src_y);
191    imagealphablending($rotate, false);
192    imagesavealpha($rotate, true);
193
194    $cosangle = cos($angle);
195    $sinangle = sin($angle);
196
197    for ($y = 0; $y < $src_y; $y++) {
198        for ($x = 0; $x < $src_x; $x++) {
199            // rotate...
200            $old_x = (($center_x - $x) * $cosangle + ($center_y - $y) * $sinangle)
201                     + $center_x;
202            $old_y = (($center_y - $y) * $cosangle - ($center_x - $x) * $sinangle)
203                     + $center_y;
204
205            if ($old_x >= 0 && $old_x < $src_x
206                && $old_y >= 0 && $old_y < $src_y
207            ) {
208                if ($bicubic == true) {
209                    $sY = $old_y + 1;
210                    $siY = $old_y;
211                    $siY2 = $old_y - 1;
212                    $sX = $old_x + 1;
213                    $siX = $old_x;
214                    $siX2 = $old_x - 1;
215
216                    $c1 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY2));
217                    $c2 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY));
218                    $c3 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY2));
219                    $c4 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY));
220
221                    $r = ($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) << 14;
222                    $g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6;
223                    $b = ($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) >> 2;
224                    $a = ($c1['alpha'] + $c2['alpha'] + $c3['alpha'] + $c4['alpha']) >> 2;
225                    $color = imagecolorallocatealpha($src_img, $r, $g, $b, $a);
226                } else {
227                    $color = imagecolorat($src_img, $old_x, $old_y);
228                }
229            } else {
230                // this line sets the background colour
231                $color = imagecolorallocatealpha($src_img, 255, 255, 255, 127);
232            }
233            imagesetpixel($rotate, $x, $y, $color);
234        }
235    }
236    return $rotate;
237}