source: branches/2.2/security/captcha.php @ 3444

Revision 3444, 4.4 KB checked in by amuller, 14 years ago (diff)

Ticket #990 - Correção na geração da imagem do Captcha

Line 
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, rand(0,100), rand(0,155));
86      $linecolor   = imagecolorallocate($image, 0, 0, 0);
87      imagettftext($image, 20, rand(-4,4),8,30,
88                   $stringcolor,
89                   $this->Font,
90                   $this->CaptchaString);
91      imagecolortransparent($image,$bgcolor);
92
93
94      $src = imagecreatefrompng('captcha_images/img00'.rand(1,4).'.png');
95      imagecopymerge($image, $src, 0, 0, 0, 0, 151, 37, 25);
96
97      switch ($this->ImageType)
98      {
99        case 'jpeg': imagejpeg($image); break;
100        case 'png':  imagepng($image);  break;
101        default:     imagepng($image);  break;
102      }
103    }
104
105    function GetCaptchaString ()
106    {
107      return $this->CaptchaString;
108    }
109   
110  }
111 
112 // ************  Fim da Classe  *************************
113
114function session_convert($str,$ky='')
115    {
116        if($ky=='') return $str;
117        $ky=str_replace(chr(32),'',$ky);
118        if(strlen($ky)<8) return '';
119        $kl=strlen($ky)<32?strlen($ky):32;
120        $k=array();
121        for($i=0;$i<$kl;$i++)
122            {
123                $k[$i]=ord($ky{$i})&0x1F;
124            }
125        $j=0;
126        for($i=0;$i<strlen($str);$i++)
127            {
128                $e=ord($str{$i});
129                $str{$i}=$e&0xE0?chr($e^$k[$j]):chr($e);
130                $j++;$j=$j==$kl?0:$j;
131            }
132        return $str;
133    }
134
135  $key_convert = md5_file($_SERVER["DOCUMENT_ROOT"].'header.inc.php');
136  //Cria o CAPTCHA,  gera o string e a imagem ...
137  $GLOBALS['captcha'] = new captcha;
138  // Guarda o string do captcha na session...
139  session_name('ZABX');
140  session_id(substr(session_convert(base64_decode($_REQUEST['ZABX']),$key_convert),32));
141  session_start();
142  $_SESSION['CAPTCHAString'] = $GLOBALS['captcha'] ->GetCaptchaString();
143  // Vai exibir a imagem do captcha...
144  $GLOBALS['captcha'] ->Showcaptcha();
145?>
Note: See TracBrowser for help on using the repository browser.