00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00055
00056 $this->Length = $length;
00057
00058
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
00068 $errormgr->addError('No fonts available!');
00069 $errormgr->displayError();
00070
00071
00072 }
00073
00074 if (function_exists('imagettftext') == FALSE)
00075 {
00076
00077 $errormgr->addError('');
00078 $errormgr->displayError();
00079
00080
00081 }
00082
00083 $this->stringGen();
00084
00085 $this->makeCaptcha();
00086
00087 }
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 }
00135
00136 function getRandFont ()
00137 {
00138
00139 return $this->fontpath . $this->fonts[mt_rand(0, count($this->fonts) - 1)];
00140
00141 }
00142
00143 function stringGen ()
00144 {
00145
00146 $uppercase = array('A', 'B', 'D', 'E', 'G', 'H', 'M', 'N', 'R', 'T');
00147 $lowercase = array('a', 'b', 'd', 'e', 'g', 'h', 'm', 'n', 'q', 'r');
00148 $numeric = array(2, 3, 4, 5, 6, 7, 8, 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 }
00161
00162 function makeCaptcha ()
00163 {
00164
00165 $imagelength = $this->Length * 25 + 16;
00166 $imageheight = 75;
00167
00168 $image = imagecreate($imagelength, $imageheight);
00169
00170
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
00206
00207
00208 $captchaImageFolder = "$this->sourceFolder/$this->uploadFolder/temp";
00209
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 }
00225
00226 function getCaptchaString ()
00227 {
00228
00229 return $this->CaptchaString;
00230
00231 }
00232
00233 function getCaptchaUrl() {
00234 return $this->captchaImageUrl;
00235 }
00236
00237 }
00238
00239