160 const SI_IMAGE_JPEG = 1;
165 const SI_IMAGE_PNG = 2;
170 const SI_IMAGE_GIF = 3;
176 const SI_CAPTCHA_STRING = 0;
181 const SI_CAPTCHA_MATHEMATIC = 1;
187 public $image_width = 215;
192 public $image_height = 80;
197 public $image_type = self::SI_IMAGE_PNG;
203 public $image_bg_color =
'#ffffff';
208 public $text_color =
'#707070';
213 public $line_color =
'#707070';
218 public $noise_color =
'#707070';
224 public $text_transparency_percentage = 50;
229 public $use_transparent_text =
false;
235 public $code_length = 6;
240 public $case_sensitive =
false;
245 public $charset =
'ABCDEFGHKLMNPRSTUVWYZabcdefghklmnprstuvwyz23456789';
250 public $expiry_time = 900;
257 public $session_name = null;
263 public $use_wordlist =
false;
269 public $perturbation = 0.75;
274 public $num_lines = 8;
279 public $noise_level = 0;
285 public $image_signature =
'';
290 public $signature_color =
'#707070';
295 public $signature_font;
301 public $use_sqlite_db =
false;
308 public $captcha_type = self::SI_CAPTCHA_STRING;
336 public $wordlist_file;
341 public $background_directory;
346 public $sqlite_database;
361 public $audio_noise_path;
371 public $audio_use_noise;
393 public $audio_mix_normalization = 0.6;
401 public $degrade_audio;
408 public $audio_gap_min = 0;
415 public $audio_gap_max = 600;
421 protected static $_captchaId = null;
426 protected $iscale = 5;
428 protected $securimage_path = null;
442 protected $code_display;
452 protected $display_value;
459 protected $captcha_code;
473 protected $no_session;
480 protected $send_headers;
483 protected $sqlite_handle;
486 protected $gdbgcolor;
487 protected $gdtextcolor;
488 protected $gdlinecolor;
489 protected $gdsignaturecolor;
509 $this->securimage_path = dirname(__FILE__);
511 if (is_array($options) &&
sizeof($options) > 0) {
512 foreach($options as $prop => $val) {
513 if ($prop ==
'captchaId') {
514 Securimage::$_captchaId = $val;
515 $this->use_sqlite_db =
true;
522 $this->image_bg_color = $this->
initColor($this->image_bg_color,
'#ffffff');
523 $this->text_color = $this->
initColor($this->text_color,
'#616161');
524 $this->line_color = $this->
initColor($this->line_color,
'#616161');
525 $this->noise_color = $this->
initColor($this->noise_color,
'#616161');
526 $this->signature_color = $this->
initColor($this->signature_color,
'#616161');
528 if (is_null($this->ttf_file)) {
529 $this->ttf_file = $this->securimage_path .
'/AHGBold.ttf';
532 $this->signature_font = $this->ttf_file;
534 if (is_null($this->wordlist_file)) {
535 $this->wordlist_file = $this->securimage_path .
'/words/words.txt';
538 if (is_null($this->sqlite_database)) {
539 $this->sqlite_database = $this->securimage_path .
'/database/securimage.sqlite';
542 if (is_null($this->audio_path)) {
543 $this->audio_path = $this->securimage_path .
'/audio/';
546 if (is_null($this->audio_noise_path)) {
547 $this->audio_noise_path = $this->audio_path .
'/noise/';
550 if (is_null($this->audio_use_noise)) {
551 $this->audio_use_noise =
true;
554 if (is_null($this->degrade_audio)) {
555 $this->degrade_audio =
true;
558 if (is_null($this->code_length) || (
int)$this->code_length < 1) {
559 $this->code_length = 6;
562 if (is_null($this->perturbation) || !is_numeric($this->perturbation)) {
563 $this->perturbation = 0.75;
566 if (is_null($this->
namespace) || !is_string($this->
namespace)) {
567 $this->
namespace = 'default';
570 if (is_null($this->no_exit)) {
571 $this->no_exit =
false;
574 if (is_null($this->no_session)) {
575 $this->no_session =
false;
578 #$this->no_session = true;
580 if (is_null($this->send_headers)) {
581 $this->send_headers =
true;
584 if ($this->no_session !=
true) {
586 if ( session_id() ==
'' ) {
587 if (!is_null($this->session_name) && trim($this->session_name) !=
'') {
588 session_name(trim($this->session_name));
601 return dirname(__FILE__);
612 public static function getCaptchaId($new =
true, array $options = array())
614 if ((
bool)$new ==
true) {
615 $id = sha1(uniqid($_SERVER[
'REMOTE_ADDR'],
true));
616 $opts = array(
'no_session' =>
true,
617 'use_sqlite_db' =>
true);
618 if (
sizeof($options) > 0) $opts = array_merge($opts, $options);
619 $si =
new self($opts);
620 Securimage::$_captchaId = $id;
625 return Securimage::$_captchaId;
638 $si =
new self(array(
'captchaId' => $id,
639 'no_session' =>
true,
640 'use_sqlite_db' =>
true));
642 if ($si->openDatabase()) {
643 $code = $si->getCodeFromDatabase();
645 if (is_array($code)) {
646 $si->code = $code[
'code'];
647 $si->code_display = $code[
'code_disp'];
650 if ($si->check($value)) {
674 public function show($background_image =
'')
676 set_error_handler(array(&$this,
'errorHandler'));
678 if($background_image !=
'' && is_readable($background_image)) {
679 $this->bgimg = $background_image;
700 $this->code_entered = $code;
702 return $this->correct_code;
716 set_error_handler(array(&$this,
'errorHandler'));
718 require_once dirname(__FILE__) .
'/WavFile.php';
722 }
catch (Exception $ex) {
723 if (($fp = @fopen(dirname(__FILE__) .
'/si.error_log',
'a+')) !==
false) {
724 fwrite($fp, date(
'Y-m-d H:i:s') .
': Securimage audio error "' . $ex->getMessage() .
'"' .
"\n");
732 if ($this->send_headers) {
733 $uniq = md5(uniqid(microtime()));
734 header(
"Content-Disposition: attachment; filename=\"securimage_audio-{$uniq}.wav\"");
735 header(
'Cache-Control: no-store, no-cache, must-revalidate');
736 header(
'Expires: Sun, 1 Jan 2000 12:00:00 GMT');
737 header(
'Last-Modified: ' . gmdate(
'D, d M Y H:i:s') .
'GMT');
738 header(
'Content-type: audio/x-wav');
740 if (extension_loaded(
'zlib')) {
741 ini_set(
'zlib.output_compression',
true);
743 header(
'Content-Length: ' . strlen($audio));
749 echo
'<hr /><strong>'
750 .
'Failed to generate audio file, content has already been '
751 .
'output.<br />This is most likely due to misconfiguration or '
752 .
'a PHP error was sent to the browser.</strong>';
755 restore_error_handler();
757 if (!$this->no_exit) exit;
772 if ($this->no_session !=
true) {
773 if (isset($_SESSION[
'securimage_code_value'][$this->
namespace]) &&
774 trim($_SESSION[
'securimage_code_value'][$this->
namespace]) !=
'') {
776 $_SESSION[
'securimage_code_ctime'][$this->
namespace]) ==
false) {
777 $code = $_SESSION[
'securimage_code_value'][$this->namespace];
778 $time = $_SESSION[
'securimage_code_ctime'][$this->namespace];
779 $disp = $_SESSION[
'securimage_code_disp'] [$this->namespace];
784 if ($this->use_sqlite_db ==
true && function_exists(
'sqlite_open')) {
790 if ($array ==
true) {
791 return array(
'code' => $code,
'ctime' => $time,
'display' => $disp);
802 if( ($this->use_transparent_text ==
true || $this->bgimg !=
'') && function_exists(
'imagecreatetruecolor')) {
803 $imagecreate =
'imagecreatetruecolor';
805 $imagecreate =
'imagecreate';
808 $this->im = $imagecreate($this->image_width, $this->image_height);
809 $this->tmpimg = $imagecreate($this->image_width * $this->iscale, $this->image_height * $this->iscale);
812 imagepalettecopy($this->tmpimg, $this->im);
822 if (is_string($this->display_value) && strlen($this->display_value) > 0) {
823 $this->code_display = $this->display_value;
824 $this->code = ($this->case_sensitive) ?
825 $this->display_value :
826 strtolower($this->display_value);
833 if (is_array($code)) {
834 $this->code = $code[
'code'];
835 $this->code_display = $code[
'code_disp'];
836 $code = $code[
'code'];
847 if ($this->noise_level > 0) {
853 if ($this->perturbation > 0 && is_readable($this->ttf_file)) {
857 if ($this->num_lines > 0) {
861 if (trim($this->image_signature) !=
'') {
874 $this->gdbgcolor = imagecolorallocate($this->im,
875 $this->image_bg_color->r,
876 $this->image_bg_color->g,
877 $this->image_bg_color->b);
879 $alpha = intval($this->text_transparency_percentage / 100 * 127);
881 if ($this->use_transparent_text ==
true) {
882 $this->gdtextcolor = imagecolorallocatealpha($this->im,
883 $this->text_color->r,
884 $this->text_color->g,
885 $this->text_color->b,
887 $this->gdlinecolor = imagecolorallocatealpha($this->im,
888 $this->line_color->r,
889 $this->line_color->g,
890 $this->line_color->b,
892 $this->gdnoisecolor = imagecolorallocatealpha($this->im,
893 $this->noise_color->r,
894 $this->noise_color->g,
895 $this->noise_color->b,
898 $this->gdtextcolor = imagecolorallocate($this->im,
899 $this->text_color->r,
900 $this->text_color->g,
901 $this->text_color->b);
902 $this->gdlinecolor = imagecolorallocate($this->im,
903 $this->line_color->r,
904 $this->line_color->g,
905 $this->line_color->b);
906 $this->gdnoisecolor = imagecolorallocate($this->im,
907 $this->noise_color->r,
908 $this->noise_color->g,
909 $this->noise_color->b);
912 $this->gdsignaturecolor = imagecolorallocate($this->im,
913 $this->signature_color->r,
914 $this->signature_color->g,
915 $this->signature_color->b);
925 imagefilledrectangle($this->im, 0, 0,
926 $this->image_width, $this->image_height,
928 imagefilledrectangle($this->tmpimg, 0, 0,
929 $this->image_width * $this->iscale, $this->image_height * $this->iscale,
932 if ($this->bgimg ==
'') {
933 if ($this->background_directory != null &&
934 is_dir($this->background_directory) &&
935 is_readable($this->background_directory))
944 if ($this->bgimg ==
'') {
948 $dat = @getimagesize($this->bgimg);
954 case 1: $newim = @imagecreatefromgif($this->bgimg);
break;
955 case 2: $newim = @imagecreatefromjpeg($this->bgimg);
break;
956 case 3: $newim = @imagecreatefrompng($this->bgimg);
break;
962 imagecopyresized($this->im, $newim, 0, 0, 0, 0,
963 $this->image_width, $this->image_height,
964 imagesx($newim), imagesy($newim));
974 if ( ($dh = opendir($this->background_directory)) !==
false) {
975 while (($file = readdir($dh)) !==
false) {
976 if (preg_match(
'/(jpg|gif|png)$/i', $file)) $images[] = $file;
981 if (
sizeof($images) > 0) {
982 return rtrim($this->background_directory,
'/') .
'/' . $images[rand(0,
sizeof($images)-1)];
996 switch($this->captcha_type) {
997 case self::SI_CAPTCHA_MATHEMATIC:
1000 $signs = array(
'+',
'-',
'x');
1001 $left = rand(1, 10);
1002 $right = rand(1, 5);
1003 $sign = $signs[rand(0, 2)];
1006 case 'x': $c = $left * $right;
break;
1007 case '-': $c = $left - $right;
break;
1008 default: $c = $left + $right;
break;
1013 $this->code_display =
"$left $sign $right";
1019 if ($this->use_wordlist && is_readable($this->wordlist_file)) {
1023 if ($this->code ==
false) {
1027 $this->code_display = $this->code;
1028 $this->code = ($this->case_sensitive) ? $this->code : strtolower($this->code);
1040 $width2 = $this->image_width * $this->iscale;
1041 $height2 = $this->image_height * $this->iscale;
1043 if (!is_readable($this->ttf_file)) {
1044 imagestring($this->im, 4, 10, ($this->image_height / 2) - 5,
'Failed to load TTF font file!', $this->gdtextcolor);
1046 if ($this->perturbation > 0) {
1047 $font_size = $height2 * .4;
1048 $bb = imageftbbox($font_size, 0, $this->ttf_file, $this->code_display);
1049 $tx = $bb[4] - $bb[0];
1050 $ty = $bb[5] - $bb[1];
1051 $x = floor($width2 / 2 - $tx / 2 - $bb[0]);
1052 $y = round($height2 / 2 - $ty / 2 - $bb[1]);
1054 imagettftext($this->tmpimg, $font_size, 0, $x, $y, $this->gdtextcolor, $this->ttf_file, $this->code_display);
1056 $font_size = $this->image_height * .4;
1057 $bb = imageftbbox($font_size, 0, $this->ttf_file, $this->code_display);
1058 $tx = $bb[4] - $bb[0];
1059 $ty = $bb[5] - $bb[1];
1060 $x = floor($this->image_width / 2 - $tx / 2 - $bb[0]);
1061 $y = round($this->image_height / 2 - $ty / 2 - $bb[1]);
1063 imagettftext($this->im, $font_size, 0, $x, $y, $this->gdtextcolor, $this->ttf_file, $this->code_display);
1080 for ($i = 0; $i < $numpoles; ++ $i) {
1081 $px[$i] = rand($this->image_width * 0.2, $this->image_width * 0.8);
1082 $py[$i] = rand($this->image_height * 0.2, $this->image_height * 0.8);
1083 $rad[$i] = rand($this->image_height * 0.2, $this->image_height * 0.8);
1084 $tmp = ((- $this->
frand()) * 0.15) - .15;
1085 $amp[$i] = $this->perturbation * $tmp;
1088 $bgCol = imagecolorat($this->tmpimg, 0, 0);
1089 $width2 = $this->iscale * $this->image_width;
1090 $height2 = $this->iscale * $this->image_height;
1091 imagepalettecopy($this->im, $this->tmpimg);
1093 for ($ix = 0; $ix < $this->image_width; ++ $ix) {
1094 for ($iy = 0; $iy < $this->image_height; ++ $iy) {
1097 for ($i = 0; $i < $numpoles; ++ $i) {
1098 $dx = $ix - $px[$i];
1099 $dy = $iy - $py[$i];
1100 if ($dx == 0 && $dy == 0) {
1103 $r = sqrt($dx * $dx + $dy * $dy);
1104 if ($r > $rad[$i]) {
1107 $rscale = $amp[$i] * sin(3.14 * $r / $rad[$i]);
1108 $x += $dx * $rscale;
1109 $y += $dy * $rscale;
1112 $x *= $this->iscale;
1113 $y *= $this->iscale;
1114 if ($x >= 0 && $x < $width2 && $y >= 0 && $y < $height2) {
1115 $c = imagecolorat($this->tmpimg, $x, $y);
1118 imagesetpixel($this->im, $ix, $iy, $c);
1129 for ($line = 0; $line < $this->num_lines; ++ $line) {
1130 $x = $this->image_width * (1 + $line) / ($this->num_lines + 1);
1131 $x += (0.5 - $this->
frand()) * $this->image_width / $this->num_lines;
1132 $y = rand($this->image_height * 0.1, $this->image_height * 0.9);
1134 $theta = ($this->
frand() - 0.5) * M_PI * 0.7;
1135 $w = $this->image_width;
1136 $len = rand($w * 0.4, $w * 0.7);
1139 $k = $this->
frand() * 0.6 + 0.2;
1141 $phi = $this->
frand() * 6.28;
1143 $dx = $step * cos($theta);
1144 $dy = $step * sin($theta);
1146 $amp = 1.5 * $this->
frand() / ($k + 5.0 / $len);
1147 $x0 = $x - 0.5 * $len * cos($theta);
1148 $y0 = $y - 0.5 * $len * sin($theta);
1150 $ldx = round(- $dy * $lwid);
1151 $ldy = round($dx * $lwid);
1153 for ($i = 0; $i < $n; ++ $i) {
1154 $x = $x0 + $i * $dx + $amp * $dy * sin($k * $i * $step + $phi);
1155 $y = $y0 + $i * $dy - $amp * $dx * sin($k * $i * $step + $phi);
1156 imagefilledrectangle($this->im, $x, $y, $x + $lwid, $y + $lwid, $this->gdlinecolor);
1166 if ($this->noise_level > 10) {
1169 $noise_level = $this->noise_level;
1172 $t0 = microtime(
true);
1174 $noise_level *= 125;
1176 $points = $this->image_width * $this->image_height * $this->iscale;
1177 $height = $this->image_height * $this->iscale;
1178 $width = $this->image_width * $this->iscale;
1179 for ($i = 0; $i < $noise_level; ++$i) {
1180 $x = rand(10, $width);
1181 $y = rand(10, $height);
1182 $size = rand(7, 10);
1183 if ($x - $size <= 0 && $y - $size <= 0)
continue;
1184 imagefilledarc($this->tmpimg, $x, $y, $size, $size, 0, 360, $this->gdnoisecolor, IMG_ARC_PIE);
1187 $t1 = microtime(
true);
1205 $bbox = imagettfbbox(10, 0, $this->signature_font, $this->image_signature);
1206 $textlen = $bbox[2] - $bbox[0];
1207 $x = $this->image_width - $textlen - 5;
1208 $y = $this->image_height - 3;
1210 imagettftext($this->im, 10, 0, $x, $y, $this->gdsignaturecolor, $this->signature_font, $this->image_signature);
1219 if ($this->send_headers) {
1224 header(
"Expires: Mon, 26 Jul 1997 05:00:00 GMT");
1225 header(
"Last-Modified: " . gmdate(
"D, d M Y H:i:s") .
"GMT");
1226 header(
"Cache-Control: no-store, no-cache, must-revalidate");
1227 header(
"Cache-Control: post-check=0, pre-check=0",
false);
1228 header(
"Pragma: no-cache");
1231 switch ($this->image_type) {
1232 case self::SI_IMAGE_JPEG:
1233 if ($this->send_headers) header(
"Content-Type: image/jpeg");
1234 imagejpeg($this->im, null, 90);
1236 case self::SI_IMAGE_GIF:
1237 if ($this->send_headers) header(
"Content-Type: image/gif");
1238 imagegif($this->im);
1241 if ($this->send_headers) header(
"Content-Type: image/png");
1242 imagepng($this->im);
1246 echo
'<hr /><strong>'
1247 .
'Failed to generate captcha image, content has already been '
1248 .
'output.<br />This is most likely due to misconfiguration or '
1249 .
'a PHP error was sent to the browser.</strong>';
1252 imagedestroy($this->im);
1253 restore_error_handler();
1255 if (!$this->no_exit) exit;
1268 if ($code[
'code'] ==
'') {
1273 if (preg_match(
'/(\d+) (\+|-|x) (\d+)/i', $code[
'display'], $eq)) {
1277 $sign = str_replace(array(
'+',
'-',
'x'), array(
'plus',
'minus',
'times'), $eq[2]);
1280 $letters = array($left, $sign, $right);
1284 $length = strlen($code[
'display']);
1286 for($i = 0; $i < $length; ++$i) {
1287 $letter = $code[
'display']{$i};
1288 $letters[] = $letter;
1294 }
catch(Exception $ex) {
1304 $fp = @fopen($this->wordlist_file,
'rb');
1305 if (!$fp)
return false;
1307 $fsize = filesize($this->wordlist_file);
1308 if ($fsize < 128)
return false;
1310 fseek($fp, rand(0, $fsize - 64), SEEK_SET);
1311 $data = fread($fp, 64);
1313 $data = preg_replace(
"/\r?\n/",
"\n", $data);
1315 $start = @strpos($data,
"\n", rand(0, 56)) + 1;
1316 $end = @strpos($data,
"\n", $start);
1318 if ($start ===
false) {
1320 }
else if ($end ===
false) {
1321 $end = strlen($data);
1324 return strtolower(substr($data, $start, $end - $start));
1334 for($i = 1, $cslen = strlen($this->charset); $i <= $this->code_length; ++$i) {
1335 $code .= $this->charset{rand(0, $cslen - 1)};
1349 if (!is_string($this->code) || strlen($this->code) == 0) {
1354 $code = $this->code;
1357 if ($this->case_sensitive ==
false && preg_match(
'/[A-Z]/', $code)) {
1360 $this->case_sensitive =
true;
1363 $code_entered = trim( (($this->case_sensitive) ? $this->code_entered
1364 : strtolower($this->code_entered))
1366 $this->correct_code =
false;
1369 if ($code == $code_entered) {
1370 $this->correct_code =
true;
1371 if ($this->no_session !=
true) {
1372 $_SESSION[
'securimage_code_value'][$this->namespace] =
'';
1373 $_SESSION[
'securimage_code_ctime'][$this->namespace] =
'';
1385 if ($this->no_session !=
true) {
1386 if (isset($_SESSION[
'securimage_code_value']) && is_scalar($_SESSION[
'securimage_code_value'])) {
1388 unset($_SESSION[
'securimage_code_value']);
1389 unset($_SESSION[
'securimage_code_ctime']);
1392 $_SESSION[
'securimage_code_disp'] [$this->namespace] = $this->code_display;
1393 $_SESSION[
'securimage_code_value'][$this->namespace] = $this->code;
1394 $_SESSION[
'securimage_code_ctime'][$this->namespace] = time();
1409 if ($this->use_sqlite_db && $this->sqlite_handle !==
false) {
1411 $ip = $_SERVER[
'REMOTE_ADDR'];
1418 $code = $this->code;
1419 $code_disp = $this->code_display;
1421 $query =
"INSERT OR REPLACE INTO codes(id, ip, code, code_display,"
1422 .
"namespace, created) VALUES('$id', '$ip', '$code', "
1423 .
"'$code_disp', '{$this->namespace}', $time)";
1425 $success = sqlite_query($this->sqlite_handle, $query);
1428 return $success !==
false;
1436 $this->sqlite_handle =
false;
1438 if ($this->use_sqlite_db ==
true && !function_exists(
'sqlite_open')) {
1439 trigger_error(
'Securimage use_sqlite_db option is enable, but SQLIte is not supported by this PHP installation', E_USER_WARNING);
1442 if ($this->use_sqlite_db && function_exists(
'sqlite_open')) {
1443 if (!file_exists($this->sqlite_database)) {
1444 $fp = fopen($this->sqlite_database,
'w+');
1446 trigger_error(
'Securimage failed to open sqlite database "' . $this->sqlite_database, E_USER_WARNING);
1450 chmod($this->sqlite_database, 0666);
1453 $this->sqlite_handle = sqlite_open($this->sqlite_database, 0666, $error);
1455 if ($this->sqlite_handle !==
false) {
1456 $res = sqlite_query($this->sqlite_handle,
"PRAGMA table_info(codes)");
1458 if (sqlite_num_rows($res) == 0) {
1459 $res = sqlite_query(
1460 $this->sqlite_handle,
1461 "CREATE TABLE codes (id VARCHAR(40) PRIMARY KEY, ip VARCHAR(32),
1462 code VARCHAR(32) NOT NULL, code_display VARCHAR(32) NOT NULL,
1463 namespace VARCHAR(32) NOT NULL, created INTEGER)"
1467 if (mt_rand(0, 100) / 100.0 == 1.0) {
1473 return $this->sqlite_handle !=
false;
1476 return $this->sqlite_handle;
1490 if ($this->use_sqlite_db && $this->sqlite_handle !==
false) {
1491 if (Securimage::$_captchaId !== null) {
1492 $query =
"SELECT * FROM codes WHERE id = '" . sqlite_escape_string(Securimage::$_captchaId) .
"'";
1494 $ip = $_SERVER[
'REMOTE_ADDR'];
1495 $ns = sqlite_escape_string($this->
namespace);
1496 $query =
"SELECT * FROM codes WHERE ip = '$ip' AND namespace = '$ns'";
1499 $res = sqlite_query($this->sqlite_handle, $query);
1500 if ($res && sqlite_num_rows($res) > 0) {
1501 $res = sqlite_fetch_array($res);
1504 if (Securimage::$_captchaId !== null) {
1506 $code = array(
'code' => $res[
'code'],
1507 'code_disp' => $res[
'code_display']);
1510 $code = $res[
'code'];
1523 if (is_resource($this->sqlite_handle)) {
1524 $ip = $_SERVER[
'REMOTE_ADDR'];
1525 $ns = sqlite_escape_string($this->
namespace);
1527 sqlite_query($this->sqlite_handle,
"DELETE FROM codes WHERE ip = '$ip' AND namespace = '$ns'");
1536 if ($this->use_sqlite_db && $this->sqlite_handle !==
false) {
1538 $limit = (!is_numeric($this->expiry_time) || $this->expiry_time < 1) ? 86400 : $this->expiry_time;
1540 sqlite_query($this->sqlite_handle,
"DELETE FROM codes WHERE $now - created > $limit");
1552 if (!is_numeric($this->expiry_time) || $this->expiry_time < 1) {
1554 }
else if (time() - $creation_time < $this->expiry_time) {
1572 foreach ($letters as $letter) {
1573 $letter = strtoupper($letter);
1576 $l =
new WavFile($this->audio_path .
'/' . $letter .
'.wav');
1580 $wavCaptcha->setSampleRate($l->getSampleRate())
1581 ->setBitsPerSample($l->getBitsPerSample())
1582 ->setNumChannels($l->getNumChannels());
1587 $wavCaptcha->appendWav($l);
1590 if ($this->audio_gap_max > 0 && $this->audio_gap_max > $this->audio_gap_min) {
1591 $wavCaptcha->insertSilence( mt_rand($this->audio_gap_min, $this->audio_gap_max) / 1000.0 );
1593 }
catch (Exception $ex) {
1603 if ($this->audio_use_noise ==
true) {
1605 $noiseFile = $this->getRandomNoiseFile();
1607 if ($noiseFile !==
false && is_readable($noiseFile)) {
1609 $wavNoise =
new WavFile($noiseFile,
false);
1610 }
catch(Exception $ex) {
1617 if ($wavNoise->getNumBlocks() > 2 * $wavCaptcha->getNumBlocks()) {
1618 $randBlock = rand(0, $wavNoise->getNumBlocks() - $wavCaptcha->getNumBlocks());
1619 $wavNoise->readWavData($randBlock * $wavNoise->getBlockAlign(), $wavCaptcha->getNumBlocks() * $wavNoise->getBlockAlign());
1621 $wavNoise->readWavData();
1622 $randOffset = rand(0, $wavNoise->getNumBlocks() - 1);
1626 $mixOpts = array(
'wav' => $wavNoise,
1628 'blockOffset' => $randOffset);
1630 $filters[WavFile::FILTER_MIX] = $mixOpts;
1631 $filters[WavFile::FILTER_NORMALIZE] = $this->audio_mix_normalization;
1635 if ($this->degrade_audio ==
true) {
1638 $filters[WavFile::FILTER_DEGRADE] = rand(95, 98) / 100.0;
1641 if (!empty($filters)) {
1642 $wavCaptcha->filter($filters);
1645 return $wavCaptcha->__toString();
1648 public function getRandomNoiseFile()
1652 if ( ($dh = opendir($this->audio_noise_path)) !==
false ) {
1655 while ( ($file = readdir($dh)) !==
false ) {
1656 if ($file ==
'.' || $file ==
'..')
continue;
1657 if (strtolower(substr($file, -4)) !=
'.wav')
continue;
1664 if (
sizeof($list) > 0) {
1665 $file = $list[array_rand($list, 1)];
1666 $return = $this->audio_noise_path . DIRECTORY_SEPARATOR . $file;
1680 return @file_get_contents(dirname(__FILE__) .
'/audio/error.wav');
1690 if (headers_sent()) {
1693 }
else if (strlen((
string)ob_get_contents()) > 0) {
1708 return 0.0001 * rand(0,9999);
1718 if ($color == null) {
1720 }
else if (is_string($color)) {
1723 }
catch(Exception $e) {
1726 }
else if (is_array($color) &&
sizeof($color) == 3) {
1748 public function errorHandler($errno, $errstr, $errfile =
'', $errline = 0, $errcontext = array())
1751 $level = error_reporting();
1754 if ($level == 0 || ($level & $errno) == 0) {
1791 $args = func_get_args();
1793 if (
sizeof($args) == 0) {
1797 }
else if (
sizeof($args) == 1) {
1799 if (substr($color, 0, 1) ==
'#') {
1800 $color = substr($color, 1);
1803 if (strlen($color) != 3 && strlen($color) != 6) {
1804 throw new InvalidArgumentException(
1805 'Invalid HTML color code passed to Securimage_Color'
1810 }
else if (
sizeof($args) == 3) {
1813 throw new InvalidArgumentException(
1814 'Securimage_Color constructor expects 0, 1 or 3 arguments; ' .
sizeof($args) .
' given'
1827 if ($red < 0) $red = 0;
1828 if ($red > 255) $red = 255;
1829 if ($green < 0) $green = 0;
1830 if ($green > 255) $green = 255;
1831 if ($blue < 0) $blue = 0;
1832 if ($blue > 255) $blue = 255;
1845 if (strlen($color) == 3) {
1846 $red = str_repeat(substr($color, 0, 1), 2);
1847 $green = str_repeat(substr($color, 1, 1), 2);
1848 $blue = str_repeat(substr($color, 2, 1), 2);
1850 $red = substr($color, 0, 2);
1851 $green = substr($color, 2, 2);
1852 $blue = substr($color, 4, 2);
1855 $this->r = hexdec($red);
1856 $this->g = hexdec($green);
1857 $this->b = hexdec($blue);