[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/data/captcha/ -> captcha.class.php (source)

   1  <?php
   2      
   3      /**
   4       * default expiration time for old images, 1h
   5       */
   6      define( "CAPTCHA_DEFAULT_EXPIRATION_TIME", 3600 );
   7      
   8      /**
   9       * background folder and background default image
  10       */
  11      define( "CAPTCHA_BACKGROUND_FOLDER", PLOG_CLASS_PATH."imgs/authimage/" );
  12      define( "CAPTCHA_BACKGROUND_FILE", "sky.gif" );
  13      /**
  14       * change this to your default key, used for the "encryption"
  15       */    
  16      define( "CAPTCHA_DEFAULT_KEY", "default-key" );
  17      /**
  18       * where you would like to store the images
  19       */
  20      define( "CAPTCHA_CACHE_FOLDER", "./tmp" );
  21      /** 
  22       * default length of the code
  23       */
  24      define( "CAPTCHA_DEFAULT_CODE_LENGTH", 6 );
  25      
  26      /**
  27       * \ingroup Data
  28       *    
  29       * Class to generate CAPTCHA images, based on Mark Wu's AuthImage plugin. Requires support
  30       * for GD built-in.
  31       *
  32       * Usage:
  33       * <pre>
  34       *  $auth = new Captcha();
  35       *  $auth->generate();
  36       *  ...
  37       *  if( $auth->validate( $_POST["authImage"] )) {
  38       *     // validation ok!
  39       *  }
  40       *  else {
  41       *    // error in validation!
  42       *  }
  43       * </pre>
  44       */
  45      class Captcha
  46      {
  47          /**
  48           * Constructor. It takes no parameter but there are several public attributes that
  49           * can be set after the constructor has been called:
  50           *
  51           * - key
  52           * - cacheFolder
  53           * - expiredTime
  54           * - length
  55           */
  56  		function Captcha()
  57          {
  58              lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
  59              
  60              $config =& Config::getConfig();
  61              $this->cacheFolder = $config->getValue( "temp_folder", CAPTCHA_CACHE_FOLDER );
  62              /**
  63               * Activate the line below and comment the line above if you have moved
  64               * your temporary folder outside of the web server tree
  65               */
  66              //$this->cacheFolder = CAPTCHA_CACHE_FOLDER;
  67              
  68              $this->key = CAPTCHA_DEFAULT_KEY;
  69              $this->expiredTime = CAPTCHA_DEFAULT_EXPIRATION_TIME;
  70              $this->length = CAPTCHA_DEFAULT_CODE_LENGTH;
  71          }
  72          
  73          /**
  74           * @private
  75           */
  76          function encrypt($string, $key) {
  77              $plainText = $string.$key;
  78              $encodeText = md5($plainText);
  79              return $encodeText;
  80          }
  81          
  82          /**
  83           * @private
  84           */
  85          function generateCode() {
  86              $code = "";
  87              for($i=0; $i < $this->length; $i++) $code .= rand(0,9);
  88              return $code;
  89          }        
  90          
  91          /**
  92           * generates a new image and returns the url
  93           *
  94           * @return a url to the captcha image
  95           */
  96  		function generate()
  97          {
  98              // Delete those cached authimage files that never used
  99              $this->purgeOld($this->expiredTime);
 100              
 101              $code = $this->generateCode();
 102              $encrypt = $this->encrypt($code, $this->key);
 103              $background = CAPTCHA_BACKGROUND_FOLDER.CAPTCHA_BACKGROUND_FILE;
 104              $tempFile = $this->cacheFolder."/".$encrypt.".gif";
 105  
 106              if(function_exists ( 'imagecreatefromgif' )){
 107                  $image = @imagecreatefromgif($background) or die("Cannot Initialize new GD image stream");
 108              }
 109              else if(function_exists ( 'imagecreatefrompng' )){
 110                  $image = @imagecreatefrompng($background) or die("Cannot Initialize new GD image stream"); 
 111              } 
 112              else {
 113                die("Server doesn't support GIF or PNG creation. Sorry.");
 114              }           
 115              
 116              $textColor = imageColorAllocate($image, 0x00, 0x00, 0x00);
 117              ImageString($image, 5, 7, 2, $code, $textColor);
 118  
 119              if ( !function_exists ( 'ImageGIF' ) ) {
 120                  ImagePNG($image, $tempFile);
 121              } else {            
 122                  ImageGIF($image, $tempFile);
 123              }
 124              
 125              // Now chmod it so it can be deleted later by the user
 126              chmod($tempFile, 0666);       
 127              
 128              return( $tempFile );
 129          }
 130          
 131          /**
 132           * checks whether the given code validates with any of the authimages
 133           * previously generated
 134           *
 135           * @param code The code
 136           * @return True if the code is valid or false otherwise
 137           */
 138  		function validate( $code )
 139          {
 140              lt_include( PLOG_CLASS_PATH."class/file/file.class.php" );
 141              $encrypt = $this->encrypt($code, $this->key);
 142              $tempFile = $this->cacheFolder."/".$encrypt.".gif";
 143                 $result = File::exists( $tempFile );
 144  
 145              return( $result );
 146          }
 147          
 148          /**
 149           * Removes the old captcha images that are not needed anymre
 150           *Ê@private
 151           */
 152  		function purgeOld( $expireTime = CAPTCHA_DEFAULT_EXPIRATION_TIME )
 153          {
 154              lt_include( PLOG_CLASS_PATH."class/misc/glob.class.php" );
 155              $files = Glob::myGlob( $this->cacheFolder, "*.gif" );
 156              if( $files ) {
 157                  foreach( $files as $file ) {
 158                      $diff = time() - filectime( $file );
 159                      if ($diff > $expireTime) 
 160                          unlink( $file );
 161                  }
 162              }
 163          }
 164      }
 165  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics