[2615] | 1 | <?php |
---|
| 2 | /****************************************************************** |
---|
| 3 | Projectname: CAPTCHA class |
---|
| 4 | Version: 1.1 |
---|
| 5 | Author: Pascal Rehfeldt <Pascal@Pascal-Rehfeldt.com> |
---|
| 6 | Last modified: 15. March 2004 |
---|
| 7 | Copyright (C): 2003, 2004 Pascal Rehfeldt, all rights reserved |
---|
| 8 | |
---|
| 9 | * GNU General Public License (Version 2, June 1991) |
---|
| 10 | * |
---|
| 11 | * This program is free software; you can redistribute |
---|
| 12 | * it and/or modify it under the terms of the GNU |
---|
| 13 | * General Public License as published by the Free |
---|
| 14 | * Software Foundation; either version 2 of the License, |
---|
| 15 | * or (at your option) any later version. |
---|
| 16 | * |
---|
| 17 | * This program is distributed in the hope that it will |
---|
| 18 | * be useful, but WITHOUT ANY WARRANTY; without even the |
---|
| 19 | * implied warranty of MERCHANTABILITY or FITNESS FOR A |
---|
| 20 | * PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 21 | * for more details. |
---|
| 22 | |
---|
| 23 | Description: |
---|
| 24 | This class can generate CAPTCHAs, see README for more details! |
---|
| 25 | |
---|
| 26 | Get the "Hurry up!" Font for the Captcha and |
---|
| 27 | save it in the same directory as this file. |
---|
| 28 | |
---|
| 29 | "Hurry up!" Font (c) by Andi |
---|
| 30 | See http://www.1001fonts.com/font_details.html?font_id=2366 |
---|
| 31 | ******************************************************************/ |
---|
| 32 | |
---|
| 33 | class captcha |
---|
| 34 | { |
---|
| 35 | |
---|
| 36 | var $Length; |
---|
| 37 | var $CaptchaString; |
---|
| 38 | var $ImageType; |
---|
| 39 | var $Font; |
---|
| 40 | var $CharWidth; |
---|
| 41 | |
---|
| 42 | function captcha ($length = 5, $type = 'png') |
---|
| 43 | { |
---|
| 44 | $this->Length = $length; |
---|
| 45 | $this->ImageType = $type; |
---|
| 46 | $this->Font = './hurryup.ttf'; |
---|
| 47 | $this->CharWidth = 27; |
---|
| 48 | $this->StringGen(); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | function Showcaptcha() |
---|
| 52 | { |
---|
| 53 | $this->SendHeader(); |
---|
| 54 | $this->MakeCaptcha(); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | function StringGen () |
---|
| 58 | { |
---|
| 59 | $uppercase = range('A', 'Z'); |
---|
| 60 | $numeric = range(0, 9); |
---|
| 61 | $CharPool = array_merge($uppercase, $numeric); |
---|
| 62 | $PoolLength = count($CharPool) - 1; |
---|
| 63 | for ($i = 0; $i < $this->Length; $i++) |
---|
| 64 | { |
---|
| 65 | $this->CaptchaString .= $CharPool[mt_rand(0, $PoolLength)]; |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | function SendHeader () |
---|
| 70 | { |
---|
| 71 | switch ($this->ImageType) |
---|
| 72 | { |
---|
| 73 | case 'jpeg': header('Content-type: image/jpeg'); break; |
---|
| 74 | case 'png': header('Content-type: image/png'); break; |
---|
| 75 | default: header('Content-type: image/png'); break; |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | function MakeCaptcha () |
---|
| 80 | { |
---|
| 81 | $imagelength = $this->Length * $this->CharWidth + 16; |
---|
| 82 | $imageheight = 37; |
---|
| 83 | $image = imagecreate($imagelength, $imageheight); |
---|
| 84 | $bgcolor = imagecolorallocate($image, 146, 176, 212); |
---|
| 85 | $stringcolor = imagecolorallocate($image, 0, 0, 155); |
---|
| 86 | $linecolor = imagecolorallocate($image, 0, 0, 0); |
---|
| 87 | imagettftext($image, 25, -4, 8, 25, |
---|
| 88 | $stringcolor, |
---|
| 89 | $this->Font, |
---|
| 90 | $this->CaptchaString); |
---|
| 91 | switch ($this->ImageType) |
---|
| 92 | { |
---|
| 93 | case 'jpeg': imagejpeg($image); break; |
---|
| 94 | case 'png': imagepng($image); break; |
---|
| 95 | default: imagepng($image); break; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | function GetCaptchaString () |
---|
| 100 | { |
---|
| 101 | return $this->CaptchaString; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | // ************ Fim da Classe ************************* |
---|
| 107 | |
---|
| 108 | //Cria o CAPTCHA( gera o string e a imagem ... |
---|
| 109 | $GLOBALS['captcha'] = new captcha; |
---|
| 110 | // Guarda o string do captcha na session... |
---|
| 111 | session_id($_REQUEST['xsid']); |
---|
| 112 | session_start(); |
---|
| 113 | $_SESSION['CAPTCHAString'] = $GLOBALS['captcha'] ->GetCaptchaString(); |
---|
| 114 | // Vai exibir a imagem do captcha... |
---|
| 115 | $GLOBALS['captcha'] ->Showcaptcha(); |
---|
| 116 | ?> |
---|