[ Index ]

PHP Cross Reference of Mambo 4.6.5

[ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/includes/ -> captcha.php (source)

   1  <?php
   2  
   3  $c_name            = 'mos_captcha';
   4  $c_width        = '250';
   5  $c_height        = '70';
   6  $c_imgtype    = 'png';
   7  $c_codetype = 'true';
   8  
   9      /**
  10      * Spam Protection - Code Image Generator - 2006 Dominik Paulus
  11      * @license http://www.opensource.org/licenses/gpl-2.0.php GNU/GPL
  12      * @author: Dominik Paulus, [email]mail@dpaulus.de[/email]
  13      * @date: 06/24/06
  14      * @version: 3.4c
  15      *
  16      */
  17  
  18      // Imagecolors
  19      $colors = array(
  20      255, 244, 234, // Background
  21      255, 128, 0,   // Code
  22      255, 128, 0,   // Vertical Lines
  23      255, 128, 0    // Border (Last value without ',')
  24      );
  25  
  26      $x = $c_width;
  27      $y = $c_height;
  28      $y2 = $y/2; $x2 = $x/2;
  29  
  30      session_start();
  31      if (!isset($_SESSION['initiated'])) { 
  32          session_regenerate_id(true); 
  33          $_SESSION['initiated'] = true; 
  34      } 
  35      $_SESSION['captcha_img'] = "OK";  // debug
  36      
  37      mt_srand((double)microtime()*1000000);
  38  
  39      // Fontsetup
  40      $font = './captchaFonts/font1.ttf';
  41  
  42      // Numerical code
  43      if($c_codetype)
  44          $seccode = strval(mt_rand(10000, 99999));
  45      else {
  46          $string = "abcdefghjkmnpqrstuvwxyz0123456789";
  47          $stringlen = strlen($string);
  48          $seccode = "";
  49          for($i = 0; $i < 5; $i++)
  50          $seccode .= $string{mt_rand(0, $stringlen)};
  51      }
  52  
  53      $_SESSION['captcha_code'] = $seccode;
  54      $clen = strlen($seccode);
  55  
  56      // create image
  57      $im = ImageCreateTrueColor($x, $y) or die('ImageCreate error!');
  58  
  59      // Image colors
  60      $bgcolor     = ImageColorAllocate($im, $colors[0], $colors[1], $colors[2]);
  61      $fontcolor   = ImageColorAllocate($im, $colors[3], $colors[4], $colors[5]);
  62      $linecolor   = ImageColorAllocate($im, $colors[6], $colors[7], $colors[8]);
  63      $bordercolor = ImageColorAllocate($im, $colors[9], $colors[10], $colors[11]);
  64      $alphacolor  = ImageColorAllocate($im, 0, 255, 0);
  65      ImageFill($im, 0, 0, $bgcolor);
  66      
  67      // Code
  68      $xspace =70;
  69      $yspace = 60;
  70      $size = 25;
  71      $angle = 20;
  72      
  73      // Morph
  74  	function morph($im, $sx, $sy, $w, $h) {
  75  
  76          $morphx = $h;
  77          $morphy = mt_rand(3.5,5.2);
  78          $mx = $sx;
  79          $my = $sy;
  80          $mvalues = array();
  81  
  82          for($i = 0; $i < $morphx/2; $i++) {
  83              $mvalues[] = $mx-(log($i+1)*$morphy);
  84              ImageCopyMerge($im, $im, $mvalues[$i], $my+$i, $mx, $my+$i, $w+20, 1, 0);
  85          }
  86  
  87          $mvalues = array_reverse($mvalues);
  88          $mvcount = count($mvalues);
  89          for($i = 0; $i < $mvcount; $i++) {
  90              ImageCopyMerge($im, $im, $mvalues[$i], $my+$i+$mvcount, $mx, $my+$i+$mvcount, $w+20, 1, 0);
  91          }
  92  
  93      }    
  94      
  95      $ttfborders = array();
  96      for($i = 0; $i < $clen; $i++) {
  97          $tmp = ImageCreateTrueColor($xspace,$yspace);
  98          ImageFill($tmp, 0, 0, $bgcolor);
  99          $ttfborders[] = ImageTTFText($tmp, $size+mt_rand(0, 8), mt_rand(-$angle, $angle), 20,
 100          $yspace-10, $fontcolor, $font, $seccode{$i}
 101          );
 102          morph($tmp, 0, 0, 20, 20);
 103          ImageColorTransparent($tmp, $bgcolor);
 104          ImageCopyMerge($im, $tmp, ($i)*50, 2, 0, 0, $xspace, $yspace, 100);
 105          ImageDestroy($tmp);
 106      }
 107  
 108      // Wave
 109      ImageSetThickness($im, 3);
 110      $ux = $uy = 0;
 111      $vx = 0; //mt_rand(10,15);
 112      $vy = mt_rand($y2-3, $y2+3);
 113  
 114      for($i = 0; $i < 10; $i++) {
 115          $ux = $vx + mt_rand(20,30);
 116          $uy = mt_rand($y2-8,$y2+8);
 117          ImageSetThickness($im, mt_rand(1,2));
 118          ImageLine($im, $vx, $vy, $ux, $uy, $linecolor);
 119          $vx = $ux;
 120          $vy = $uy;
 121      }
 122      ImageLine($im, $vx, $vy, $x, $y2, $linecolor);
 123  
 124      // Triangle
 125      ImageSetThickness($im, 3);
 126      $ux = mt_rand($x2-10, $x2+10);
 127      $uy = mt_rand($y2-10, $y2-30);
 128      ImageLine($im, mt_rand(10,$x2-20), $y, $ux, $uy, $linecolor);
 129      ImageSetThickness($im, 1);
 130      ImageLine($im,  mt_rand($x2+20,$x-10), $y, $ux, $uy, $linecolor);
 131      ImageSetThickness($im, 1);
 132  
 133      // Border
 134      ImageSetThickness($im, 1);
 135      ImageLine($im, 0, 0, 0, $y, $bordercolor); // left
 136      ImageLine($im, 0, 0, $x, 0, $bordercolor); // top
 137      ImageLine($im, 0, $y-1, $x, $y-1, $bordercolor); // bottom
 138      ImageLine($im, $x-1, 0, $x-1, $y-1, $bordercolor); // right
 139  
 140      for($i = $x/$clen; $i < $x; $i+=$x/$clen)
 141      ImageLine($im, $i, 0, $i, $y, $bordercolor);
 142  
 143      switch($c_imgtype) {
 144          case 'jpeg':
 145              Header("Content-Type: image/jpeg");
 146              ImageJPEG($im,"",75);    
 147              break;
 148          case 'png':
 149              Header("Content-Type: image/png");
 150              ImagePNG($im);
 151              break;
 152          case 'gif':
 153              Header("Content-Type: image/gif");
 154              ImageGIF($im);
 155              break;
 156          default:
 157              die("Wrong \$type in captcha.php (should be jpeg, png or gif)\n");
 158      }
 159      
 160      ImageDestroy($im);
 161  ?>