[ Index ]

MailPress 7.2

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

title

Body

[close]

/mp-includes/class/options/form/field_types/captcha_gd1/captcha/ -> captcha.php (source)

   1  <?php
   2  if ( !extension_loaded( 'gd' ) ) return;
   3  
   4  /**
   5   * From an original idea of :
   6   *
   7  // -----------------------------------------------
   8  // Cryptographp v1.4
   9  // ( c ) 2006-2007 Sylvain BRISON 
  10  //
  11  // www.cryptographp.com 
  12  // cryptographp@alphpa.com 
  13  //
  14  // Licence CeCILL modifiée
  15  // => Voir fichier Licence_CeCILL_V2-fr.txt )
  16  // -----------------------------------------------
  17  */
  18  
  19  class MP_Form_field_type_captcha_gd1_cryptogra
  20  {
  21  	function __construct()
  22      {
  23          session_start();
  24  
  25          $field = MP_Form_field::get( filter_input( INPUT_GET, 'id' ) );
  26  
  27          $root = __DIR__ . '/cfg/';
  28          if ( is_dir( $root ) ) 
  29          {
  30              $dir = @opendir( $root );
  31              if ( $dir ) while ( ( $style = readdir( $dir ) ) !== false ) if ( $style[0] != '.' ) $xstyles[$style] = $style;
  32              @closedir( $dir );
  33          }
  34          $xstyle = ( isset( $field->settings['options']['form_img_style'] ) ) ? $field->settings['options']['form_img_style'] : false;
  35          if ( !isset( $xstyles[$xstyle] ) ) $xstyle = 'random';
  36          if ( $xstyle == 'random' ) $xstyle = $xstyles[array_rand( $xstyles )];
  37          $xsettings = __DIR__ . "/cfg/$xstyle/settings.php";
  38  
  39             list( $hash, $word, $format, $img )     = self::create( $xstyle, $xsettings );
  40  
  41          $_SESSION['cryptogra']['style']    = $xstyle;
  42          $_SESSION['cryptogra']['settings']     = $xsettings;
  43  
  44          switch ( strtolower( $hash ) ) 
  45          {    
  46              case 'md5'  : $_SESSION['cryptogra']['code'] = md5( $word );     break;
  47              case 'sha1' : $_SESSION['cryptogra']['code'] = sha1( $word ); break;
  48                default     : $_SESSION['cryptogra']['code'] = $word;     break;
  49          }
  50          $_SESSION['cryptogra']['time'] = time();
  51  //        $_SESSION['cryptogra']['use']++;
  52  
  53          ob_end_clean();
  54          switch ( strtolower( $format ) ) 
  55          {  
  56              case 'jpg'  :
  57              case 'jpeg' : 
  58                  if ( imagetypes() & IMG_JPG )  
  59                  {
  60                              header( 'Content-type: image/jpeg' );
  61                              imagejpeg( $img, '', 80 );
  62                  }
  63              break;
  64              case 'gif'  : 
  65                  if ( imagetypes() & IMG_GIF )
  66                  {
  67                      header( 'Content-type: image/gif' );
  68                      imagegif( $img );
  69                  }
  70                  break;
  71              case 'png'  : 
  72              default     : 
  73                  if ( imagetypes() & IMG_PNG )
  74                  {
  75                      header( 'Content-type: image/png' );
  76                      imagepng( $img );
  77                  }
  78          }
  79          imagedestroy ( $img );
  80          die();
  81      }
  82  
  83  	function create( $style, $settings )
  84      {
  85          global $img, $ink, $charclear, $charcolorrnd, $charcolorrndlevel, $charnb, $charspace, $charR, $charG, $charB, $tword,  $xvariation;
  86          global $bg, $brushsize, $noisecolorchar;
  87          global $cryptwidth, $cryptheight, $nbcirclemin, $nbcirclemax, $noiselinemin, $noiselinemax, $noisepxmin, $noisepxmax;
  88  
  89          include( $settings );
  90  
  91          // Création du cryptogramme temporaire
  92          $imgtmp     = imagecreatetruecolor( $cryptwidth, $cryptheight );
  93          $blank     = imagecolorallocate( $imgtmp, 255, 255, 255 );
  94          $black     = imagecolorallocate( $imgtmp,   0,   0,   0 );
  95          imagefill( $imgtmp, 0, 0, $blank );
  96  
  97          $word      = '';
  98          $x = 10;
  99          $pair      = rand( 0, 1 );
 100          $charnb      = rand( $charnbmin, $charnbmax );
 101          for ( $i = 1; $i <= $charnb; $i++ ) 
 102          {
 103              $tword[$i]['font']     = $tfont[array_rand( $tfont, 1 )];
 104              $tword[$i]['angle']    = ( rand( 1, 2 ) == 1 ) ? rand( 0, $charanglemax ) : rand( 360 - $charanglemax, 360 );
 105              $tword[$i]['element']     = ( $crypteasy ) ? ( ( !$pair ) ? $charelc{rand( 0, strlen( $charelc ) - 1 )} : $charelv{rand( 0, strlen( $charelv ) - 1 )} ) : $charel{rand( 0, strlen( $charel ) - 1 )};
 106              $pair             = !$pair;
 107              $tword[$i]['size']     = rand( $charsizemin, $charsizemax );
 108              $tword[$i]['y']        = ( $charup ) ? ( $cryptheight/2 ) + rand( 0, ( $cryptheight/5 ) ) : ( $cryptheight/1.5 );
 109  
 110              $word .= $tword[$i]['element'];
 111  
 112              imagettftext( $imgtmp, $tword[$i]['size'], $tword[$i]['angle'], $x, $tword[$i]['y'], $black, __DIR__ . '/fonts/' . $tword[$i]['font'], $tword[$i]['element'] );
 113  
 114              $x +=$charspace;
 115          }
 116  
 117          // Calcul du racadrage horizontal du cryptogramme temporaire
 118          $xbegin = $x = 0;
 119          while ( !$xbegin && ( $x < $cryptwidth ) ) 
 120          {
 121              $y = 0;
 122              while ( !$xbegin && ( $y < $cryptheight ) ) 
 123              {
 124                  if ( imagecolorat( $imgtmp, $x, $y ) != $blank ) $xbegin = $x;
 125                  $y++;
 126              }
 127              $x++;
 128          } 
 129      
 130          $xend = 0;
 131          $x    = $cryptwidth - 1;
 132          while ( !$xend && ( $x > 0 ) ) 
 133          {
 134              $y = 0;
 135              while ( !$xend && ( $y < $cryptheight ) ) 
 136              {
 137                  if ( imagecolorat( $imgtmp, $x, $y ) != $blank ) $xend = $x;
 138                  $y++;
 139              }
 140              $x--;
 141          } 
 142  
 143          $xvariation = round( ( $cryptwidth/2 ) - ( ( $xend - $xbegin )/2 ) );
 144          imagedestroy ( $imgtmp );
 145  
 146          // Création du cryptogramme définitif
 147          // Création du fond
 148  
 149          $img = imagecreatetruecolor( $cryptwidth, $cryptheight ); 
 150  
 151           if ( $bgimg ) $bgimg = __DIR__ . "/cfg/$style/$bgimg";
 152  
 153          if ( $bgimg and is_dir( $bgimg ) ) 
 154          {
 155              $dh = opendir( $bgimg );
 156              while ( ( $filename = readdir( $dh ) ) !== false ) if ( MP_::is_image( $filename ) ) $files[] = $filename;
 157              closedir( $dh );
 158              $bgimg = "$bgimg/" . $files[array_rand( $files, 1 )];
 159          }
 160  
 161          if ( $bgimg )
 162          {
 163              list( $getwidth, $getheight, $gettype, $getattr ) = getimagesize( $bgimg );
 164              switch ( $gettype ) 
 165              {
 166                  case '1' : $imgread = imagecreatefromgif ( $bgimg ); break;
 167                  case '2' : $imgread = imagecreatefromjpeg( $bgimg ); break;
 168                  case '3' : $imgread = imagecreatefrompng ( $bgimg ); break;
 169              }
 170                  if ( isset( $imgread ) )
 171                  {
 172                  imagecopyresized ( $img, $imgread, 0, 0, 0, 0, $cryptwidth, $cryptheight, $getwidth, $getheight );
 173                  imagedestroy ( $imgread );
 174                  }
 175          }
 176              else 
 177          {
 178              $bg = imagecolorallocate( $img, $bgR, $bgG, $bgB );
 179              imagefill( $img, 0, 0, $bg );
 180              if ( $bgclear ) imagecolortransparent( $img, $bg );
 181          }
 182  
 183          if ( $noiseup ) { self::ecriture(); self::bruit(); } else  { self::bruit(); self::ecriture(); }
 184  
 185          // Création du cadre
 186          if ( $bgframe ) 
 187          {
 188              $framecol = imagecolorallocate( $img, ( $bgR * 3 + $charR )/4, ( $bgG * 3 + $charG )/4, ( $bgB * 3 + $charB )/4 );
 189              imagerectangle( $img, 0, 0, $cryptwidth - 1, $cryptheight - 1, $framecol );
 190          }
 191              
 192          // Transformations supplémentaires: Grayscale et Brouillage
 193          // Vérifie si la fonction existe dans la version PHP installée
 194  
 195          if ( function_exists( 'imagefilter' ) ) 
 196          {
 197              if ( $cryptgrayscal )     imagefilter( $img, IMG_FILTER_GRAYSCALE );
 198              if ( $cryptgaussianblur ) imagefilter( $img, IMG_FILTER_GAUSSIAN_BLUR );
 199          }
 200  
 201          // Conversion du cryptogramme en Majuscule si insensibilité à la casse
 202  
 203          if ( $difuplow ) $word = strtoupper( $word );
 204  
 205          return array( $cryptsecure, $word, $cryptformat, $img );
 206      }
 207  
 208  	function ecriture()                // Création de l'écriture
 209      {
 210          global $img, $ink, $charclear, $charcolorrnd, $charcolorrndlevel, $charnb, $charspace, $charR, $charG, $charB, $tword,  $xvariation;
 211  
 212          $ink = ( function_exists ( 'imagecolorallocatealpha' ) ) ? imagecolorallocatealpha( $img, $charR, $charG, $charB, $charclear ) : imagecolorallocate( $img, $charR, $charG, $charB );
 213  
 214          $x = $xvariation;
 215          for ( $i = 1; $i <= $charnb; $i++ )
 216          {       
 217         
 218              if ( $charcolorrnd )          // Choisit des couleurs au hasard
 219              { 
 220                  $ok = false;
 221                  do 
 222                  {
 223                      $rndR = rand( 0, 255 ); $rndG = rand( 0, 255 ); $rndB = rand( 0, 255 );
 224                      $rndcolor = $rndR + $rndG + $rndB;
 225                      switch ( $charcolorrndlevel ) 
 226                      {
 227                          case 1  : if ( $rndcolor<200 ) $ok = true; break;     // tres sombre
 228                          case 2  : if ( $rndcolor<400 ) $ok = true; break;     // sombre
 229                          case 3  : if ( $rndcolor>500 ) $ok = true; break;        // claires
 230                          case 4  : if ( $rndcolor>650 ) $ok = true; break;     // très claires
 231                          default : $ok = true;               
 232                      }
 233                  } while ( !$ok );
 234            
 235                  $rndink = ( function_exists ( 'imagecolorallocatealpha' ) ) ? imagecolorallocatealpha( $img, $rndR, $rndG, $rndB, $charclear ) : imagecolorallocate ( $img, $rndR, $rndG, $rndB );          
 236              }  
 237           
 238              imagettftext( $img, $tword[$i]['size'], $tword[$i]['angle'], $x, $tword[$i]['y'], ( $charcolorrnd ) ? $rndink : $ink, __DIR__ . '/fonts/' . $tword[$i]['font'], $tword[$i]['element'] );
 239  
 240              $x += $charspace;
 241          } 
 242      }
 243  
 244  	function bruit()            // Ajout de bruits: point, lignes et cercles aléatoires
 245      {
 246          global $img, $cryptwidth, $cryptheight, $nbcirclemin, $nbcirclemax, $noiselinemin, $noiselinemax, $noisepxmin, $noisepxmax;
 247  
 248          $nbpx     = rand( $noisepxmin,   $noisepxmax );
 249          $nbline     = rand( $noiselinemin, $noiselinemax );
 250          $nbcircle     = rand( $nbcirclemin,  $nbcirclemax );
 251          for ( $i = 1; $i <  $nbpx;     $i++ )    imagesetpixel ( $img, rand( 0, $cryptwidth - 1 ), rand( 0, $cryptheight - 1 ), self::noisecolor() );
 252          for ( $i = 1; $i <= $nbline;     $i++ )    imageline      ( $img, rand( 0, $cryptwidth - 1 ), rand( 0, $cryptheight - 1 ), rand( 0, $cryptwidth - 1 ), rand( 0, $cryptheight - 1 ), self::noisecolor() );
 253          for ( $i = 1; $i <= $nbcircle;    $i++ ) imagearc      ( $img, rand( 0, $cryptwidth - 1 ), rand( 0, $cryptheight - 1 ), $rayon = rand( 5, $cryptwidth/3 ), $rayon, 0, 360, self::noisecolor() );
 254      } 
 255  
 256  	function noisecolor()        // Fonction permettant de déterminer la couleur du bruit et la forme du pinceau
 257      {
 258          global $img, $bg, $brushsize, $ink, $noisecolorchar;
 259  
 260          switch ( $noisecolorchar ) 
 261          {
 262              case 1  : $noisecol = $ink; break;
 263              case 2  : $noisecol = $bg;  break;
 264              case 3  : 
 265              default : $noisecol = imagecolorallocate ( $img, rand( 0, 255 ), rand( 0, 255 ), rand( 0, 255 ) ); break;               
 266          }
 267  
 268          if ( $brushsize && ( $brushsize > 1 ) && function_exists( 'imagesetbrush' ) ) 
 269          {
 270              $brush = imagecreatetruecolor( $brushsize, $brushsize );
 271              imagefill( $brush, 0, 0, $noisecol );
 272              imagesetbrush( $img, $brush );
 273              $noisecol = IMG_COLOR_BRUSHED;
 274          }
 275          return $noisecol;    
 276      }
 277  }
 278  new MP_Form_field_type_captcha_gd1_cryptogra();


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1