[ Index ]
 

Code source de Mantis 1.1.0rc3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/ -> make_captcha_img.php (source)

   1  <?php
   2  # Mantis - a php based bugtracking system
   3  
   4  # Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
   5  # Copyright (C) 2002 - 2007  Mantis Team   - mantisbt-dev@lists.sourceforge.net
   6  
   7  # Mantis is free software: you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation, either version 2 of the License, or
  10  # (at your option) any later version.
  11  #
  12  # Mantis is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  #
  17  # You should have received a copy of the GNU General Public License
  18  # along with Mantis.  If not, see <http://www.gnu.org/licenses/>.
  19  
  20      # --------------------------------------------------------
  21      # $Id: make_captcha_img.php,v 1.8.20.1 2007-10-13 22:33:22 giallu Exp $
  22      # --------------------------------------------------------
  23  
  24      # ======================================================================
  25      # Author: Marcello Scata' <marcelloscata at users.sourceforge.net> ITALY
  26      # ======================================================================
  27  
  28  #    ob_start();
  29      require_once ( 'core.php' );
  30  #    ob_flush();
  31  
  32      $f_public_key = gpc_get_int( 'public_key' );
  33  
  34      $t_key = strtolower( substr( md5( config_get( 'password_confirm_hash_magic_string' ) . $f_public_key ), 1, 5) );
  35      $t_system_font_folder = config_get( 'system_font_folder' );
  36      $t_font_per_captcha = config_get( 'font_per_captcha' );
  37  
  38      $t_captcha_init = array(
  39          'TTF_folder'     => $t_system_font_folder,
  40          'TTF_RANGE'      => array( $t_font_per_captcha )
  41      );
  42  
  43      $captcha =& new masc_captcha( $t_captcha_init );
  44      $captcha->make_captcha( $t_key );
  45  
  46  #
  47  # The class below was derived from
  48  # http://www.phpclasses.org/browse/package/1163.html
  49  #
  50  # *** 3.0 Author
  51  # Pascal Rehfeldt
  52  # Pascal@Pascal-Rehfeldt.com
  53  #
  54  # http://www.phpclasses.org/browse.html/author/102754.html
  55  #
  56  #
  57  # *** 3.1 License
  58  # GNU General Public License (Version 2, June 1991)
  59  #
  60  # This program is free software; you can redistribute
  61  # it and/or modify it under the terms of the GNU
  62  # General Public License as published by the Free
  63  # Software Foundation; either version 2 of the License,
  64  # or (at your option) any later version.
  65  #
  66  # This program is distributed in the hope that it will
  67  # be useful, but WITHOUT ANY WARRANTY; without even the
  68  # implied warranty of MERCHANTABILITY or FITNESS FOR A
  69  # PARTICULAR PURPOSE. See the GNU General Public License
  70  # for more details.
  71  #
  72  
  73      class masc_captcha
  74      {
  75          var $TTF_folder;
  76          var $TTF_RANGE  = array('ARIAL.TTF');
  77          var $chars        = 5;
  78          var $minsize    = 15;
  79          var $maxsize    = 15;
  80          var $maxrotation = 30;
  81          var $noise        = FALSE;
  82          var $websafecolors = TRUE;
  83          var $debug = FALSE;
  84  
  85          var $lx;                // width of picture
  86          var $ly;                // height of picture
  87          var $jpegquality = 80;    // image quality
  88          var $noisefactor = 9;    // this will multiplyed with number of chars
  89          var $nb_noise;            // number of background-noise-characters
  90          var $TTF_file;            // holds the current selected TrueTypeFont
  91          var $gd_version;        // holds the Version Number of GD-Library
  92          var $r;
  93          var $g;
  94          var $b;
  95  
  96  
  97  			function masc_captcha( $config )
  98              {
  99                  // Test for GD-Library(-Version)
 100                  $this->gd_version = get_gd_version();
 101                  if($this->gd_version == 0) die("There is no GD-Library-Support enabled. The Captcha-Class cannot be used!");
 102                  if($this->debug) echo "\n<br />-Captcha-Debug: The available GD-Library has major version ".$this->gd_version;
 103  
 104                  // extracts config array
 105                  if(is_array($config))
 106                  {
 107                      if($this->debug) echo "\n<br />-Captcha-Debug: Extracts Config-Array in unsecure-mode!";
 108                      foreach($config as $k=>$v) $this->$k = $v;
 109                  }
 110  
 111                  // check vars for maxtry, secretposition and min-max-size
 112                  if($this->minsize > $this->maxsize)
 113                  {
 114                      $temp = $this->minsize;
 115                      $this->minsize = $this->maxsize;
 116                      $this->maxsize = $temp;
 117                      if($this->debug) echo "<br />-Captcha-Debug: Arrghh! What do you think I mean with min and max? Switch minsize with maxsize.";
 118                  }
 119  
 120                  // check TrueTypeFonts
 121                  if(is_array($this->TTF_RANGE))
 122                  {
 123                      if($this->debug) echo "\n<br />-Captcha-Debug: Check given TrueType-Array! (".count($this->TTF_RANGE).")";
 124                      $temp = array();
 125                      foreach($this->TTF_RANGE as $k=>$v)
 126                      {
 127                          if(is_readable($this->TTF_folder.$v)) $temp[] = $v;
 128                      }
 129                      $this->TTF_RANGE = $temp;
 130                      if($this->debug) echo "\n<br />-Captcha-Debug: Valid TrueType-files: (".count($this->TTF_RANGE).")";
 131                      //if(count($this->TTF_RANGE) < 1) die('No Truetypefont available for the CaptchaClass.');
 132                  }
 133                  else
 134                  {
 135                      if($this->debug) echo "\n<br />-Captcha-Debug: Check given TrueType-File! (".$this->TTF_RANGE.")";
 136                      if(!is_readable($this->TTF_folder.$this->TTF_RANGE)) die('No Truetypefont available for the CaptchaClass.');
 137                  }
 138  
 139                  // select first TrueTypeFont
 140                  $this->change_TTF();
 141                  if($this->debug) echo "\n<br />-Captcha-Debug: Set current TrueType-File: (".$this->TTF_file.")";
 142  
 143                  // get number of noise-chars for background if is enabled
 144                  $this->nb_noise = $this->noise ? ($this->chars * $this->noisefactor) : 0;
 145                  if($this->debug) echo "\n<br />-Captcha-Debug: Set number of noise characters to: (".$this->nb_noise.")";
 146  
 147                  // set dimension of image
 148                  $this->lx = ($this->chars + 1) * (int)(($this->maxsize + $this->minsize) / 1.5);
 149                  $this->ly = (int)(2.4 * $this->maxsize);
 150                  if($this->debug) echo "\n<br />-Captcha-Debug: Set image dimension to: (".$this->lx." x ".$this->ly.")";
 151              }
 152  
 153  			function make_captcha( $private_key )
 154              {
 155                  if($this->debug) echo "\n<br />-Captcha-Debug: Generate private key: ($private_key)";
 156  
 157                  // create Image and set the apropriate function depending on GD-Version & websafecolor-value
 158                  if($this->gd_version >= 2 && !$this->websafecolors)
 159                  {
 160                      $func1 = 'imagecreatetruecolor';
 161                      $func2 = 'imagecolorallocate';
 162                  }
 163                  else
 164                  {
 165                      $func1 = 'imageCreate';
 166                      $func2 = 'imagecolorclosest';
 167                  }
 168                  $image = $func1($this->lx,$this->ly);
 169                  if($this->debug) echo "\n<br />-Captcha-Debug: Generate ImageStream with: ($func1())";
 170                  if($this->debug) echo "\n<br />-Captcha-Debug: For colordefinitions we use: ($func2())";
 171  
 172  
 173                  // Set Backgroundcolor
 174                  $this->random_color(224, 255);
 175                  $back =  @imagecolorallocate($image, $this->r, $this->g, $this->b);
 176                  @ImageFilledRectangle($image,0,0,$this->lx,$this->ly,$back);
 177                  if($this->debug) echo "\n<br />-Captcha-Debug: We allocate one color for Background: (".$this->r."-".$this->g."-".$this->b.")";
 178  
 179                  // allocates the 216 websafe color palette to the image
 180                  if($this->gd_version < 2 || $this->websafecolors) $this->makeWebsafeColors($image);
 181  
 182  
 183                  // fill with noise or grid
 184                  if($this->nb_noise > 0)
 185                  {
 186                      // random characters in background with random position, angle, color
 187                      if($this->debug) echo "\n<br />-Captcha-Debug: Fill background with noise: (".$this->nb_noise.")";
 188                      for($i=0; $i < $this->nb_noise; $i++)
 189                      {
 190                          srand((double)microtime()*1000000);
 191                          $size    = intval(rand((int)($this->minsize / 2.3), (int)($this->maxsize / 1.7)));
 192                          srand((double)microtime()*1000000);
 193                          $angle    = intval(rand(0, 360));
 194                          srand((double)microtime()*1000000);
 195                          $x        = intval(rand(0, $this->lx));
 196                          srand((double)microtime()*1000000);
 197                          $y        = intval(rand(0, (int)($this->ly - ($size / 5))));
 198                          $this->random_color(160, 224);
 199                          $color    = $func2($image, $this->r, $this->g, $this->b);
 200                          srand((double)microtime()*1000000);
 201                          $text    = chr(intval(rand(45,250)));
 202                          if(count ($this->TTF_RANGE)>0){
 203                              @ImageTTFText($image, $size, $angle, $x, $y, $color, $this->change_TTF(), $text);
 204                          } else {
 205                              imagestring($image,5,$x,$y,$text,$color);
 206                          }
 207                      }
 208                  }
 209                  else
 210                  {
 211                      // generate grid
 212                      if($this->debug) echo "\n<br />-Captcha-Debug: Fill background with x-gridlines: (".(int)($this->lx / (int)($this->minsize / 1.5)).")";
 213                      for($i=0; $i < $this->lx; $i += (int)($this->minsize / 1.5))
 214                      {
 215                          $this->random_color(160, 224);
 216                          $color    = $func2($image, $this->r, $this->g, $this->b);
 217                          @imageline($image, $i, 0, $i, $this->ly, $color);
 218                      }
 219                      if($this->debug) echo "\n<br />-Captcha-Debug: Fill background with y-gridlines: (".(int)($this->ly / (int)(($this->minsize / 1.8))).")";
 220                      for($i=0 ; $i < $this->ly; $i += (int)($this->minsize / 1.8))
 221                      {
 222                          $this->random_color(160, 224);
 223                          $color    = $func2($image, $this->r, $this->g, $this->b);
 224                          @imageline($image, 0, $i, $this->lx, $i, $color);
 225                      }
 226                  }
 227  
 228                  // generate Text
 229                  if($this->debug) echo "\n<br />-Captcha-Debug: Fill forground with chars and shadows: (".$this->chars.")";
 230                  for($i=0, $x = intval(rand($this->minsize,$this->maxsize)); $i < $this->chars; $i++)
 231                  {
 232                      $text    = strtoupper(substr($private_key, $i, 1));
 233                      srand((double)microtime()*1000000);
 234                      $angle    = intval(rand(($this->maxrotation * -1), $this->maxrotation));
 235                      srand((double)microtime()*1000000);
 236                      $size    = intval(rand($this->minsize, $this->maxsize));
 237                      srand((double)microtime()*1000000);
 238                      $y        = intval(rand((int)($size * 1.5), (int)($this->ly - ($size / 7))));
 239                      $this->random_color(0, 127);
 240                      $color    =  $func2($image, $this->r, $this->g, $this->b);
 241                      $this->random_color(0, 127);
 242                      $shadow = $func2($image, $this->r + 127, $this->g + 127, $this->b + 127);
 243                      if(count($this->TTF_RANGE) > 0){
 244                          @ImageTTFText($image, $size, $angle, $x + (int)($size / 15), $y, $shadow, $this->change_TTF(), $text);
 245                          @ImageTTFText($image, $size, $angle, $x, $y - (int)($size / 15), $color, $this->TTF_file, $text);
 246                      } else {
 247                          $t_font = rand(3,5);
 248                          imagestring($image,$t_font,$x + (int)($size / 15),$y-20,$text,$color);
 249                          imagestring($image,$t_font,$x,$y - (int)($size / 15)-20,$text,$color);
 250                      }
 251                      $x += (int)($size + ($this->minsize / 5));
 252                  }
 253                  header('Content-type: image/jpeg');
 254                  @ImageJPEG($image, '', $this->jpegquality);
 255                  @ImageDestroy($image);
 256                  if($this->debug) echo "\n<br />-Captcha-Debug: Destroy Imagestream.";
 257              }
 258  
 259              /** @private **/
 260  			function makeWebsafeColors(&$image)
 261              {
 262                  for($r = 0; $r <= 255; $r += 51)
 263                  {
 264                      for($g = 0; $g <= 255; $g += 51)
 265                      {
 266                          for($b = 0; $b <= 255; $b += 51)
 267                          {
 268                              $color = imagecolorallocate($image, $r, $g, $b);
 269                              //$a[$color] = array('r'=>$r,'g'=>$g,'b'=>$b);
 270                          }
 271                      }
 272                  }
 273                  if($this->debug) echo "\n<br />-Captcha-Debug: Allocate 216 websafe colors to image: (".imagecolorstotal($image).")";
 274              }
 275  
 276  			function random_color($min,$max)
 277              {
 278                  srand((double)microtime() * 1000000);
 279                  $this->r = intval(rand($min,$max));
 280                  srand((double)microtime() * 1000000);
 281                  $this->g = intval(rand($min,$max));
 282                  srand((double)microtime() * 1000000);
 283                  $this->b = intval(rand($min,$max));
 284              }
 285  
 286  			function change_TTF()
 287              {
 288                  if(count($this->TTF_RANGE) > 0){
 289                      if(is_array($this->TTF_RANGE))
 290                      {
 291                          srand((float)microtime() * 10000000);
 292                          $key = array_rand($this->TTF_RANGE);
 293                          $this->TTF_file = $this->TTF_folder.$this->TTF_RANGE[$key];
 294                      }
 295                      else
 296                      {
 297                          $this->TTF_file = $this->TTF_folder.$this->TTF_RANGE;
 298                      }
 299                      return $this->TTF_file;
 300                  }
 301              }
 302  
 303      } // END CLASS masc_captcha
 304  
 305  ?>


Généré le : Thu Nov 29 09:42:17 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics