captcha.php
2 weeks ago
font-1.ttf
2 weeks ago
font-2.ttf
2 weeks ago
font-3.ttf
2 weeks ago
font-4.ttf
2 weeks ago
no-gd-library.png
2 weeks ago
captcha.php
179 lines
| 1 | <?php |
| 2 | /* |
| 3 | PHP Captcha by Codepeople.net |
| 4 | http://www.codepeople.net |
| 5 | */ |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 8 | |
| 9 | if (!ini_get("zlib.output_compression")) ob_clean(); |
| 10 | |
| 11 | header("Cache-Control: no-store, no-cache, must-revalidate"); |
| 12 | header("Pragma: no-cache"); |
| 13 | |
| 14 | if (!isset($_GET["ps"])) $_GET["ps"] = ''; |
| 15 | if (!isset($_GET["bcolor"]) || $_GET["bcolor"] == '') $_GET["bcolor"] = "FFFFFF"; |
| 16 | if (!isset($_GET["border"]) || $_GET["border"] == '') $_GET["border"] = "FFFFFF"; |
| 17 | |
| 18 | //configuration |
| 19 | $imgX = min( ( isset($_GET["width"]) && is_numeric( $_GET["width"] ) )? intval($_GET["width"]) : "180" , 800); |
| 20 | $imgY = min( ( isset($_GET["height"]) && is_numeric( $_GET["height"] ) )? intval($_GET["height"]) : "60" , 600); |
| 21 | |
| 22 | $letter_count = min( ( isset($_GET["letter_count"]) && is_numeric( $_GET["letter_count"] ) )? intval($_GET["letter_count"]) : "5", 20); |
| 23 | $min_size = min( ( isset($_GET["min_size"]) && is_numeric( $_GET["min_size"] ) )? intval($_GET["min_size"]) : "35", 200); |
| 24 | $max_size = min( ( isset($_GET["max_size"]) && is_numeric( $_GET["max_size"] ) )? intval($_GET["max_size"]) : "45", 200); |
| 25 | $noise = min( ( isset($_GET["noise"]) && is_numeric( $_GET["noise"] ) )? intval($_GET["noise"]) : "200", 5000); |
| 26 | $noiselength = min( ( isset($_GET["noiselength"]) && is_numeric( $_GET["noiselength"] ) )? intval($_GET["noiselength"]) : "5", 50); |
| 27 | $bcolor = cpcff_decodeColor($_GET["bcolor"]); |
| 28 | $border = cpcff_decodeColor($_GET["border"]); |
| 29 | |
| 30 | $noisecolor = 0xcdcdcd; |
| 31 | $random_noise_color= true; |
| 32 | $tcolor = cpcff_decodeColor("666666"); |
| 33 | $random_text_color= true; |
| 34 | |
| 35 | function cpcff_decodeColor($hexcolor) |
| 36 | { |
| 37 | $color = hexdec($hexcolor); |
| 38 | $c["b"] = $color % 256; |
| 39 | $color = $color / 256; |
| 40 | $c["g"] = $color % 256; |
| 41 | $color = $color / 256; |
| 42 | $c["r"] = $color % 256; |
| 43 | return $c; |
| 44 | } |
| 45 | |
| 46 | function cpcff_similarColors($c1, $c2) |
| 47 | { |
| 48 | return sqrt( pow($c1["r"]-$c2["r"],2) + pow($c1["g"]-$c2["g"],2) + pow($c1["b"]-$c2["b"],2)) < 125; |
| 49 | } |
| 50 | |
| 51 | if (function_exists('session_start')) @session_start(); |
| 52 | |
| 53 | function cpcff_make_seed() { |
| 54 | list($usec, $sec) = explode(' ', microtime()); |
| 55 | return (float) $sec + ((float) $usec * 100000); |
| 56 | } |
| 57 | mt_srand(cpcff_make_seed()); |
| 58 | $randval = mt_rand(); |
| 59 | |
| 60 | $str = ""; |
| 61 | $length = 0; |
| 62 | for ($i = 0; $i < $letter_count; $i++) { |
| 63 | $str .= chr(mt_rand(97, 122))." "; |
| 64 | } |
| 65 | $_SESSION['rand_code'.sanitize_key($_GET["ps"])] = str_replace(" ", "", $str); |
| 66 | |
| 67 | $uidt = uniqid(); |
| 68 | set_transient( "cpeople-captcha-".$uidt , str_replace(" ", "", $str) , 1800 ); |
| 69 | setCookie('rand_code'.sanitize_key($_GET["ps"]), $uidt, time()+36000,"/"); |
| 70 | |
| 71 | if (!function_exists('imagecreatetruecolor')) |
| 72 | { |
| 73 | header("Content-type: image/png"); |
| 74 | readfile( dirname( __FILE__ ) . "/no-gd-library.png"); |
| 75 | exit; |
| 76 | } |
| 77 | |
| 78 | $image = imagecreatetruecolor($imgX, $imgY); |
| 79 | $backgr_col = imagecolorallocate($image, $bcolor["r"],$bcolor["g"],$bcolor["b"]); |
| 80 | $border_col = imagecolorallocate($image, $border["r"],$border["g"],$border["b"]); |
| 81 | |
| 82 | if ($random_text_color) |
| 83 | { |
| 84 | do |
| 85 | { |
| 86 | $selcolor = mt_rand(0,256*256*256); |
| 87 | } while ( cpcff_similarColors(cpcff_decodeColor($selcolor), $bcolor) ); |
| 88 | $tcolor = cpcff_decodeColor($selcolor); |
| 89 | } |
| 90 | |
| 91 | $text_col = imagecolorallocate($image, $tcolor["r"],$tcolor["g"],$tcolor["b"]); |
| 92 | |
| 93 | if (empty($_GET["font"])) |
| 94 | $selected_font = "font-1.ttf"; |
| 95 | else |
| 96 | { |
| 97 | switch (@$_GET["font"]) { |
| 98 | case "font-2.ttf": |
| 99 | case "font2": |
| 100 | $selected_font = "font-2.ttf"; |
| 101 | break; |
| 102 | case "font-3.ttf": |
| 103 | case "font3": |
| 104 | $selected_font = "font-3.ttf"; |
| 105 | break; |
| 106 | case "font-4.ttf": |
| 107 | case "font4": |
| 108 | $selected_font = "font-4.ttf"; |
| 109 | break; |
| 110 | default: |
| 111 | $selected_font = "font-1.ttf"; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | $font = dirname( __FILE__ ) . "/". $selected_font; |
| 116 | |
| 117 | // 1. Create Image and Colors |
| 118 | $image = imagecreatetruecolor($imgX, $imgY); |
| 119 | imagealphablending($image, true); |
| 120 | imagesavealpha($image, true); |
| 121 | |
| 122 | $bgColor = imagecolorallocate($image, $bcolor['r'], $bcolor['g'], $bcolor['b']); |
| 123 | $borderColor = imagecolorallocate($image, $border['r'], $border['g'], $border['b']); |
| 124 | imagefill($image, 0, 0, $bgColor); |
| 125 | |
| 126 | // 1. Setup Margins |
| 127 | $leftPadding = 15; // Explicit safe zone for the first character |
| 128 | $rightPadding = 15; // Safe zone for the last character |
| 129 | $availableWidth = $imgX - ($leftPadding + $rightPadding); |
| 130 | |
| 131 | $charArray = str_split($str); |
| 132 | $totalChars = count($charArray); |
| 133 | $cellWidth = $availableWidth / $totalChars; |
| 134 | |
| 135 | // 2. Decorative Background Noise |
| 136 | for ($i = 0; $i < 6; $i++) { |
| 137 | $shapeAlpha = imagecolorallocatealpha($image, rand(220, 245), rand(220, 245), rand(220, 245), 90); |
| 138 | imagefilledellipse($image, rand(0, $imgX), rand(0, $imgY), rand(20, $imgX), rand(20, $imgY), $shapeAlpha); |
| 139 | } |
| 140 | |
| 141 | // 3. Render Characters with Safe Positioning |
| 142 | foreach ($charArray as $i => $char) { |
| 143 | $fontSize = rand($min_size, $max_size); |
| 144 | $angle = rand(-10, 10); |
| 145 | |
| 146 | $bbox = imagettfbbox($fontSize, $angle, $font, $char); |
| 147 | |
| 148 | // Calculate dimensions from bounding box |
| 149 | $charWidth = abs($bbox[4] - $bbox[0]); |
| 150 | $charHeight = abs($bbox[5] - $bbox[1]); |
| 151 | |
| 152 | // X = Base padding + (index * cell width) + centering within that cell |
| 153 | $x = $leftPadding + ($i * $cellWidth) + ($cellWidth - $charWidth) / 2; |
| 154 | |
| 155 | // Y = Centered vertically with a slight random jitter for modern feel |
| 156 | $y = ($imgY / 2) + ($charHeight / 2) - rand(-2, 2); |
| 157 | |
| 158 | $textColor = imagecolorallocatealpha($image, rand(40, 70), rand(40, 70), rand(80, 110), 0); |
| 159 | |
| 160 | imagettftext($image, $fontSize, $angle, $x, $y, $textColor, $font, $char); |
| 161 | } |
| 162 | |
| 163 | // 4. Fine Grain Noise |
| 164 | for ($i = 0; $i < $noise; $i++) { |
| 165 | $noiseColor = imagecolorallocatealpha($image, 80, 80, 80, rand(90, 110)); |
| 166 | $x1 = rand(0, $imgX); |
| 167 | $y1 = rand(0, $imgY); |
| 168 | // Using noiselength to define the sprawl of the noise dots/lines |
| 169 | imageline($image, $x1, $y1, $x1 + rand(1, $noiselength), $y1 + rand(1, $noiselength), $noiseColor); |
| 170 | } |
| 171 | |
| 172 | // 5. Border and Output |
| 173 | imagerectangle($image, 0, 0, $imgX - 1, $imgY - 1, $borderColor); |
| 174 | |
| 175 | header('Content-Type: image/png'); |
| 176 | imagepng($image); |
| 177 | imagedestroy($image); |
| 178 | exit; |
| 179 | ?> |