• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

cms/modules/form/captcha/class/captcha.class.php

Go to the documentation of this file.
00001 <?php
00002 
00003   /******************************************************************
00004 
00005    Projectname:   CAPTCHA class
00006    Version:       2.0
00007    Author:        Pascal Rehfeldt <Pascal@Pascal-Rehfeldt.com>
00008    Last modified: 15. January 2006
00009 
00010    * GNU General Public License (Version 2, June 1991)
00011    *
00012    * This program is free software; you can redistribute
00013    * it and/or modify it under the terms of the GNU
00014    * General Public License as published by the Free
00015    * Software Foundation; either version 2 of the License,
00016    * or (at your option) any later version.
00017    *
00018    * This program is distributed in the hope that it will
00019    * be useful, but WITHOUT ANY WARRANTY; without even the
00020    * implied warranty of MERCHANTABILITY or FITNESS FOR A
00021    * PARTICULAR PURPOSE. See the GNU General Public License
00022    * for more details.
00023 
00024    Description:
00025    This class can generate CAPTCHAs, see README for more details!
00026 
00027   ******************************************************************/
00028 
00029   require('filter.class.php');
00030   require('error.class.php');
00031 
00032   class captcha
00033   {
00034 
00035     var $Length;
00036     var $CaptchaString;
00037     var $fontpath;
00038     var $fonts;
00039     var $captchaImageUrl;
00040     var $sourceFolder;
00041     var $moduleFolder;
00042     var $uploadFolder;
00043     var $cmsFolder;
00044     var $urlRequestRoot;
00045 
00046     function captcha ($sourceFolder, $moduleFolder, $uploadFolder, $urlRequestRoot, $cmsFolder, $length = 5)
00047     {
00048                         $this->sourceFolder = $sourceFolder;
00049                         $this->moduleFolder = $moduleFolder;
00050                         $this->uploadFolder = $uploadFolder;
00051                         $this->urlRequestRoot = $urlRequestRoot;
00052                         $this->cmsFolder = $cmsFolder;
00053 
00054       // header('Content-type: image/png');
00055 
00056       $this->Length   = $length;
00057 
00058       //$this->fontpath = dirname($_SERVER['SCRIPT_FILENAME']) . '/fonts/';
00059       global $sourceFolder, $moduleFolder;
00060       $this->fontpath = "$sourceFolder/$moduleFolder/form/captcha/fonts/";
00061       $this->fonts    = $this->getFonts();
00062       $errormgr       = new error;
00063 
00064       if ($this->fonts == FALSE)
00065       {
00066 
00067         //$errormgr = new error;
00068         $errormgr->addError('No fonts available!');
00069         $errormgr->displayError();
00070 //              die();
00071 
00072       }
00073 
00074       if (function_exists('imagettftext') == FALSE)
00075       {
00076 
00077         $errormgr->addError('');
00078         $errormgr->displayError();
00079 //       die();
00080 
00081       }
00082 
00083       $this->stringGen();
00084 
00085       $this->makeCaptcha();
00086 
00087     } //captcha
00088 
00089     function getFonts ()
00090     {
00091 
00092       $fonts = array();
00093 
00094       if ($handle = @opendir($this->fontpath))
00095       {
00096 
00097         while (($file = readdir($handle)) !== FALSE)
00098         {
00099 
00100           $extension = strtolower(substr($file, strlen($file) - 3, 3));
00101 
00102           if ($extension == 'ttf')
00103           {
00104 
00105             $fonts[] = $file;
00106 
00107           }
00108 
00109         }
00110 
00111         closedir($handle);
00112 
00113       }
00114       else
00115       {
00116 
00117         return FALSE;
00118 
00119       }
00120 
00121       if (count($fonts) == 0)
00122       {
00123 
00124         return FALSE;
00125 
00126       }
00127       else
00128       {
00129 
00130         return $fonts;
00131 
00132       }
00133 
00134     } //getFonts
00135 
00136     function getRandFont ()
00137     {
00138 
00139       return $this->fontpath . $this->fonts[mt_rand(0, count($this->fonts) - 1)];
00140 
00141     } //getRandFont
00142 
00143     function stringGen ()
00144     {
00145 
00146       $uppercase  = array('A', 'B', 'D', 'E', 'G', 'H', 'M', 'N', 'R', 'T'); // range('A', 'Z');
00147       $lowercase  = array('a', 'b', 'd', 'e', 'g', 'h', 'm', 'n', 'q', 'r'); // range('a', 'z');
00148       $numeric    = array(2, 3, 4, 5, 6, 7, 8, 9); // range(0, 9);
00149 
00150       $CharPool   = array_merge($uppercase, $lowercase, $numeric);
00151       $PoolLength = count($CharPool) - 1;
00152 
00153       for ($i = 0; $i < $this->Length; $i++)
00154       {
00155 
00156         $this->CaptchaString .= $CharPool[mt_rand(0, $PoolLength)];
00157 
00158       }
00159 
00160     } //StringGen
00161 
00162     function makeCaptcha ()
00163     {
00164 
00165       $imagelength = $this->Length * 25 + 16;
00166       $imageheight = 75;
00167 
00168       $image       = imagecreate($imagelength, $imageheight);
00169 
00170       //$bgcolor     = imagecolorallocate($image, 222, 222, 222);
00171       $bgcolor     = imagecolorallocate($image, 255, 255, 255);
00172 
00173       $stringcolor = imagecolorallocate($image, 0, 0, 0);
00174 
00175       $filter      = new filters;
00176 
00177       $funcnumber = rand(0,1);
00178       switch ($funcnumber) {
00179         case 0 :
00180                 $randcellno = rand(2,7);
00181                 $filter->signs($image, $this->getRandFont(),$randcellno);
00182                 break;
00183         case 1 :
00184                 $randruns = rand(10,30);
00185             $filter->noise($image,$randruns);
00186             break;
00187         case 2 :
00188         default :
00189                 $randblurradius = rand(8,20);
00190             $filter->blur($image,15);
00191             break;
00192       }
00193 
00194       for ($i = 0; $i < strlen($this->CaptchaString); $i++)
00195       {
00196 
00197         imagettftext($image, 25, mt_rand(-15, 15), $i * 25 + 10,
00198                      mt_rand(30, 70),
00199                      $stringcolor,
00200                      $this->getRandFont(),
00201                      $this->CaptchaString{$i});
00202 
00203       }
00204 
00205       //$filter->noise($image, 10);
00206       //$filter->blur($image, 6);
00207 
00208                         $captchaImageFolder = "$this->sourceFolder/$this->uploadFolder/temp";
00209                         // exec('find "'.$captchaImageFolder.'" -maxdepth 1 -type 5 -mmin +60 | xargs -0 /bin/rm -f');
00210 
00211                         $captchaImageFile = scandir($captchaImageFolder, 1);
00212                         if(count($captchaImageFile) <= 1) {
00213                                 $captchaImageFile[0] = '000000.png';
00214                         }
00215                         $captchaImageFile = substr($captchaImageFile[0], 0, strrpos($captchaImageFile[0], '.'));
00216                         $captchaImageFile++;
00217                         $captchaImageFile = str_pad($captchaImageFile, 6, '0', STR_PAD_LEFT) . '.png';
00218 
00219                         $this->captchaImageUrl = "$this->urlRequestRoot/$this->cmsFolder/$this->uploadFolder/temp/$captchaImageFile";
00220 
00221       imagepng($image, $captchaImageFolder . '/' . $captchaImageFile);
00222       imagedestroy($image);
00223 
00224     } //MakeCaptcha
00225 
00226     function getCaptchaString ()
00227     {
00228 
00229       return $this->CaptchaString;
00230 
00231     } //GetCaptchaString
00232 
00233                 function getCaptchaUrl() {
00234                         return $this->captchaImageUrl;
00235                 }
00236 
00237   } //class: captcha
00238 
00239 

Generated on Sun Jan 2 2011 04:55:32 for Pragyan CMS by  doxygen 1.7.1