[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/ -> make_captcha_img.php (source)

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


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7