source: companies/serpro/mobile/seguranca/captcha.php @ 903

Revision 903, 3.6 KB checked in by niltonneto, 15 years ago (diff)

Importacao inicial do Expresso do Serpro

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  class captcha
33  {
34
35    var $Length;
36    var $CaptchaString;
37    var $ImageType;
38    var $Font;
39    var $CharWidth;
40
41    function captcha ($length = 5, $type = 'jpeg')
42    {
43      $this->Length    = $length;
44      $this->ImageType = $type;
45      // $this->Font      = './hurryup.ttf';     
46      // $this->Font      = './anonymous.gdf';
47      $this->Font      = './monofont.ttf';
48      $this->CharWidth = 17;
49      $this->StringGen();
50    }
51
52    function Showcaptcha()
53    {
54      $this->SendHeader();
55      $this->MakeCaptcha();   
56    }
57
58    function StringGen ()
59    {
60      $uppercase  = range('A', 'Z');
61      $numeric    = range(0, 9);
62      $CharPool   = array_merge($uppercase, $numeric);
63      $PoolLength = count($CharPool) - 1;
64      for ($i = 0; $i < $this->Length; $i++)
65      {
66        $this->CaptchaString .= $CharPool[mt_rand(0, $PoolLength)];
67      }
68    }
69
70    function SendHeader ()
71    {
72      switch ($this->ImageType)
73      {
74        case 'jpeg': header('Content-type: image/jpeg'); break;
75        case 'png':  header('Content-type: image/png');  break;
76        default:     header('Content-type: image/png');  break;
77      }
78    }
79
80    function MakeCaptcha ()
81    {
82      $imagelength = $this->Length * $this->CharWidth + 16;
83      $imageheight = 37;
84      $image       = imagecreate($imagelength, $imageheight);
85      $bgcolor     = imagecolorallocate($image, 255, 255, 0);
86      $stringcolor = imagecolorallocate($image, 0, 0, 155);
87      $linecolor   = imagecolorallocate($image, 0, 0, 0);
88      imagettftext($image, 25, -4, 20, 25,
89                   $stringcolor,
90                   $this->Font,
91                   $this->CaptchaString);
92      switch ($this->ImageType)
93      {
94        case 'jpeg': imagejpeg($image); break;
95        case 'png':  imagepng($image);  break;
96        default:     imagepng($image);  break;
97      }
98    }
99
100    function GetCaptchaString ()
101    {
102      return $this->CaptchaString;
103    }
104   
105  }
106 
107 // ************  Fim da Classe  *************************
108 
109 
110  //Cria o CAPTCHA( gera o string e a imagem ...
111  $GLOBALS['captcha'] = new captcha;
112  // Guarda o string do captcha na session...
113  $_SESSION['CAPTCHAString'] = $GLOBALS['captcha'] ->GetCaptchaString();
114  // Vai exibir a imagem do captcha...
115  $GLOBALS['captcha'] ->Showcaptcha();
116?>
Note: See TracBrowser for help on using the repository browser.