noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
Securimage Class Reference
+ Collaboration diagram for Securimage:

Public Member Functions

 __construct ()
 Class constructor.
 
 addSignature ()
 Print signature text on image.
 
 allocateColors ()
 Allocate all colors that will be used in the CAPTCHA image.
 
 check ($code)
 Validate the code entered by the user.
 
 checkCode ()
 Check if the user entered code was correct.
 
 clearCodeFromDatabase ()
 Delete a code from the database by ip address hash.
 
 createCode ()
 Create a code and save to the session.
 
 distortedCopy ()
 Warp text from temporary image onto final image.
 
 doImage ()
 Generate and output the image.
 
 drawLines ()
 Draw random curvy lines over the image
Modified code from HKCaptcha.
 
 drawWord ()
 Draw the CAPTCHA code over the image.
 
 frand ()
 Generate random number less than 1.
 
 generateCode ($len)
 Generate a code.
 
 generateMP3 ($letters)
 Generate an mp3 file by concatenating individual files.
 
 generateWAV ($letters)
 Generate a wav file by concatenating individual files.
 
 getAudibleCode ($format='wav')
 Get WAV or MP3 file data of the spoken code.
 
 getBackgroundFromDirectory ()
 Return the full path to a random gif, jpg, or png from the background directory.
 
 getCode ()
 Get the captcha code.
 
 getCodeFromDatabase ()
 Get stored captcha code from sqlite database based on ip address hash.
 
 getIPHash ()
 Get hashed IP address of remote user.
 
 isCodeExpired ($creation_time)
 Check a code to see if it is expired based on creation time.
 
 openDatabase ()
 Open SQLite database.
 
 output ()
 Output image to the browser.
 
 outputAudioFile ()
 Output audio file with HTTP headers to browser.
 
 purgeOldCodesFromDatabase ()
 Purge codes over a day old from database.
 
 readCodeFromFile ()
 Reads a word list file to get a code.
 
 saveCodeToDatabase ()
 Save captcha code to sqlite database.
 
 saveData ()
 Save the code in the session.
 
 scrambleAudioData (&$data, $format)
 Randomly modify the audio data to scramble sound and prevent binary recognition.
 
 setAudioPath ($audio_directory)
 Set the path to the audio directory.
 
 setBackground ()
 Set the background of the CAPTCHA image.
 
 show ($background_image="")
 Generate a code and output the image to the browser.
 
 validate ()
 Validate the code to the user code.
 

Data Fields

 $audio_format
 
 $audio_path
 
 $background_directory = null
 
 $bgimg
 
 $charset
 
 $code
 
 $code_entered
 
 $code_length
 
 $correct_code
 
 $draw_lines_over_text
 
 $expiry_time
 
 $gd_font_file
 
 $gd_font_size
 
 $gdbgcolor
 
 $gdlinecolor
 
 $gdmulticolor
 
 $gdsignaturecolor
 
 $gdtextcolor
 
 $im
 
 $image_bg_color
 
 $image_height
 
 $image_signature
 
 $image_type
 
 $image_width
 
 $iscale
 
 $line_color
 
 $multi_text_color
 
 $num_lines
 
 $perturbation
 
 $session_name = ''
 
 $signature_color
 
 $signature_font
 
 $sqlite_database
 
 $sqlite_handle
 
 $text_angle_maximum
 
 $text_angle_minimum
 
 $text_color
 
 $text_transparency_percentage
 
 $text_x_start
 
 $tmpimg
 
 $ttf_file
 
 $use_gd_font
 
 $use_multi_text
 
 $use_sqlite_db
 Use an SQLite database for storing codes as a backup to sessions.
 
 $use_transparent_text
 
 $use_wordlist = false
 
 $wordlist_file
 

Detailed Description

Definition at line 107 of file securimage.php.

Constructor & Destructor Documentation

◆ __construct()

Securimage::__construct ( )

Class constructor.


Because the class uses sessions, this will attempt to start a session if there is no previous one.
If you do not start a session before calling the class, the constructor must be called before any output is sent to the browser.

$securimage = new Securimage();

Definition at line 507 of file securimage.php.

508 {
509 // Initialize session or attach to existing
510 if ( session_id() == '' ) { // no session has been started yet, which is needed for validation
511 if (trim($this->session_name) != '') {
512 session_name($this->session_name); // set session name if provided
513 }
514 session_start();
515 }
516
517 // Set Default Values
518 $this->image_width = 230;
519 $this->image_height = 80;
520 $this->image_type = SI_IMAGE_PNG;
521
522 $this->code_length = 6;
523 $this->charset = 'ABCDEFGHKLMNPRSTUVWYZabcdefghklmnprstuvwyz23456789';
524 $this->wordlist_file = './words/words.txt';
525 $this->use_wordlist = false;
526
527 $this->gd_font_file = 'gdfonts/automatic.gdf';
528 $this->use_gd_font = false;
529 $this->gd_font_size = 24;
530 $this->text_x_start = 15;
531
532 $this->ttf_file = './AHGBold.ttf';
533
534 $this->perturbation = 0.75;
535 $this->iscale = 5;
536 $this->text_angle_minimum = 0;
537 $this->text_angle_maximum = 0;
538
539 $this->image_bg_color = new Securimage_Color(0xff, 0xff, 0xff);
540 $this->text_color = new Securimage_Color(0x3d, 0x3d, 0x3d);
541 $this->multi_text_color = array(new Securimage_Color(0x0, 0x20, 0xCC),
542 new Securimage_Color(0x0, 0x30, 0xEE),
543 new Securimage_color(0x0, 0x40, 0xCC),
544 new Securimage_Color(0x0, 0x50, 0xEE),
545 new Securimage_Color(0x0, 0x60, 0xCC));
546 $this->use_multi_text = false;
547
548 $this->use_transparent_text = false;
550
551 $this->num_lines = 10;
552 $this->line_color = new Securimage_Color(0x3d, 0x3d, 0x3d);
553 $this->draw_lines_over_text = true;
554
555 $this->image_signature = '';
556 $this->signature_color = new Securimage_Color(0x20, 0x50, 0xCC);
557 $this->signature_font = './AHGBold.ttf';
558
559 $this->audio_path = './audio/';
560 $this->audio_format = 'mp3';
561 $this->session_name = '';
562 $this->expiry_time = 900;
563
564 $this->sqlite_database = 'database/securimage.sqlite';
565 $this->use_sqlite_db = false;
566
567 $this->sqlite_handle = false;
568 }
$img audio_format
$img image_height
$img use_multi_text
$img text_transparency_percentage
$img image_type
$img num_lines
$img perturbation
$img line_color
$img image_width
$img text_angle_minimum
$img image_signature
$img use_transparent_text
$img multi_text_color
$img text_angle_maximum
$img image_bg_color
$img signature_color
$img use_wordlist

References audio_format, code_length, image_bg_color, image_height, image_signature, image_type, image_width, line_color, multi_text_color, num_lines, perturbation, signature_color, text_angle_maximum, text_angle_minimum, text_color, text_transparency_percentage, use_multi_text, use_transparent_text, and use_wordlist.

Member Function Documentation

◆ addSignature()

Securimage::addSignature ( )

Print signature text on image.

Since
2.0 @access private

Definition at line 1379 of file securimage.php.

1380 {
1381 if ($this->use_gd_font) {
1382 imagestring($this->im, 5, $this->image_width - (strlen($this->image_signature) * 10), $this->image_height - 20, $this->image_signature, $this->gdsignaturecolor);
1383 } else {
1384
1385 $bbox = imagettfbbox(10, 0, $this->signature_font, $this->image_signature);
1386 $textlen = $bbox[2] - $bbox[0];
1387 $x = $this->image_width - $textlen - 5;
1388 $y = $this->image_height - 3;
1389
1390 imagettftext($this->im, 10, 0, $x, $y, $this->gdsignaturecolor, $this->signature_font, $this->image_signature);
1391 }
1392 }

References $x, image_height, image_signature, and image_width.

Referenced by doImage().

◆ allocateColors()

Securimage::allocateColors ( )

Allocate all colors that will be used in the CAPTCHA image.

Since
2.0.1 @access private

Definition at line 693 of file securimage.php.

694 {
695 // allocate bg color first for imagecreate
696 $this->gdbgcolor = imagecolorallocate($this->im, $this->image_bg_color->r??0, $this->image_bg_color->g??0, $this->image_bg_color->b??0);
697
698 $this->text_color->r=$this->text_color->r??rand(0,255);
699 $this->text_color->g=$this->text_color->g??rand(0,255);
700 $this->text_color->b=$this->text_color->b??rand(0,255);
701 $this->line_color->r=$this->line_color->r??rand(0,255);
702 $this->line_color->g=$this->line_color->g??rand(0,255);
703 $this->line_color->b=$this->line_color->b??rand(0,255);
704
705
706
707 $alpha = intval($this->text_transparency_percentage / 100 * 127);
708
709 if ($this->use_transparent_text == true) {
710 $this->gdtextcolor = imagecolorallocatealpha($this->im, $this->text_color->r, $this->text_color->g, $this->text_color->b, $alpha);
711 $this->gdlinecolor = imagecolorallocatealpha($this->im, $this->line_color->r, $this->line_color->g, $this->line_color->b, $alpha);
712 } elseif ( $this->text_color != null ) {
713 $this->gdtextcolor = imagecolorallocate($this->im, $this->text_color->r, $this->text_color->g, $this->text_color->b);
714 $this->gdlinecolor = imagecolorallocate($this->im, $this->line_color->r, $this->line_color->g, $this->line_color->b);
715 } else {
716 $red=rand(0,255);
717 $green=rand(0,255);
718 $blue=rand(0,255);
719 $this->gdtextcolor = imagecolorallocate($this->im, $red, $green,$blue);
720 $this->gdlinecolor = imagecolorallocate($this->im, $red, $green, $blue);
721 }
722
723 $this->gdsignaturecolor = imagecolorallocate($this->im, $this->signature_color->r??0, $this->signature_color->g??0, $this->signature_color->b??0);
724
725 if ($this->use_multi_text == true) {
726 $this->gdmulticolor = array();
727
728 foreach($this->multi_text_color??[] as $color) {
729 $color->r=$color->r??rand(0,255);
730 $color->g= $color->g??rand(0,255);
731 $color->b=$color->b??rand(0,255);
732 if ($this->use_transparent_text == true) {
733 $this->gdmulticolor[] = imagecolorallocatealpha($this->im, $color->r, $color->g, $color->b, $alpha);
734 } else {
735 $this->gdmulticolor[] = imagecolorallocate($this->im, $color->r, $color->g, $color->b);
736 }
737 }
738 }
739 }
if( $delta< 0) elseif( $delta==0)

References $color, elseif, image_bg_color, line_color, multi_text_color, signature_color, text_color, text_transparency_percentage, use_multi_text, and use_transparent_text.

Referenced by doImage().

◆ check()

Securimage::check ( $code)

Validate the code entered by the user.

$code = $_POST['code']; if ($securimage->check($code) == false) { die("Sorry, the code entered did not match."); } else { $valid = true; }

Parameters
string$codeThe code the user entered
Returns
boolean true if the code was correct, false if not

Definition at line 606 of file securimage.php.

607 {
608 $this->code_entered = $code;
609 $this->validate();
610 return $this->correct_code;
611 }
validate()
Validate the code to the user code.

References $code, and validate().

+ Here is the call graph for this function:

◆ checkCode()

Securimage::checkCode ( )

Check if the user entered code was correct.

@access private

Returns
boolean

Definition at line 1218 of file securimage.php.

1219 {
1220 return $this->correct_code;
1221 }

◆ clearCodeFromDatabase()

Securimage::clearCodeFromDatabase ( )

Delete a code from the database by ip address hash.

@access private

Since
2.0.1

Definition at line 1489 of file securimage.php.

1490 {
1491 if ($this->sqlite_handle !== false) {
1492 $ip = $this->getIPHash();
1493
1494 sqlite_query($this->sqlite_handle, "DELETE FROM codes WHERE iphash = '$ip'");
1495 }
1496 }
getIPHash()
Get hashed IP address of remote user.

References getIPHash().

Referenced by validate().

+ Here is the call graph for this function:

◆ createCode()

Securimage::createCode ( )

Create a code and save to the session.

@access private

Since
1.0.1

Definition at line 999 of file securimage.php.

1000 {
1001 $this->code = false;
1002
1003 if ($this->use_wordlist && is_readable($this->wordlist_file)) {
1004 $this->code = $this->readCodeFromFile();
1005 }
1006
1007 if ($this->code == false) {
1008 $this->code = $this->generateCode($this->code_length);
1009 }
1010
1011 $this->saveData();
1012 }
saveData()
Save the code in the session.
readCodeFromFile()
Reads a word list file to get a code.

References code_length, generateCode(), readCodeFromFile(), saveData(), and use_wordlist.

Referenced by doImage(), and getAudibleCode().

+ Here is the call graph for this function:

◆ distortedCopy()

Securimage::distortedCopy ( )

Warp text from temporary image onto final image.


Modified for securimage

@access private

Since
2.0
Author
Han-Kwang Nienhuys modified

Definition at line 939 of file securimage.php.

940 {
941 $numpoles = 3; // distortion factor
942
943 // make array of poles AKA attractor points
944 for ($i = 0; $i < $numpoles; ++$i) {
945 $px[$i] = rand((int) ($this->image_width * 0.3), (int)( $this->image_width * 0.7));
946 $py[$i] = rand((int) ($this->image_height * 0.3),(int) ($this->image_height * 0.7));
947 $rad[$i] = rand((int) ($this->image_width * 0.4), (int) ($this->image_width * 0.7));
948 $tmp = -$this->frand() * 0.15 - 0.15;
949 $amp[$i] = $this->perturbation * $tmp;
950 }
951
952 $bgCol = imagecolorat($this->tmpimg, 0, 0);
953 $width2 = $this->iscale * $this->image_width;
954 $height2 = $this->iscale * $this->image_height;
955
956 imagepalettecopy($this->im, $this->tmpimg); // copy palette to final image so text colors come across
957
958 // loop over $img pixels, take pixels from $tmpimg with distortion field
959 for ($ix = 0; $ix < $this->image_width; ++$ix) {
960 for ($iy = 0; $iy < $this->image_height; ++$iy) {
961 $x = $ix;
962 $y = $iy;
963
964 for ($i = 0; $i < $numpoles; ++$i) {
965 $dx = $ix - $px[$i];
966 $dy = $iy - $py[$i];
967 if ($dx == 0 && $dy == 0) continue;
968
969 $r = sqrt($dx * $dx + $dy * $dy);
970 if ($r > $rad[$i]) continue;
971
972 $rscale = $amp[$i] * sin(3.14 * $r / $rad[$i]);
973 $x += $dx * $rscale;
974 $y += $dy * $rscale;
975 }
976
977 $c = $bgCol;
978 $x *= $this->iscale;
979 $y *= $this->iscale;
980
981 if ($x >= 0 && $x < $width2 && $y >= 0 && $y < $height2) {
982 $c = imagecolorat($this->tmpimg, (int) $x,(int) $y);
983 }
984
985 if ($c != $bgCol) { // only copy pixels of letters to preserve any background image
986 imagesetpixel($this->im, $ix, $iy, $c);
987 }
988 }
989 }
990 }
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
frand()
Generate random number less than 1.
$c
Definition compute.php:48

References $c, $i, $ix, $r, $rad, $tmp, $x, frand(), image_height, image_width, and perturbation.

Referenced by doImage().

+ Here is the call graph for this function:

◆ doImage()

Securimage::doImage ( )

Generate and output the image.

@access private

Definition at line 653 of file securimage.php.

654 {
655 if ($this->use_gd_font == true || $this->iscale == null || $this->iscale == 0) {
656 $this->iscale = 1;
657 }
658 if($this->use_transparent_text == true || $this->bgimg != "") {
659 $this->im = imagecreatetruecolor($this->image_width, $this->image_height);
660 $this->tmpimg = imagecreatetruecolor($this->image_width * $this->iscale, $this->image_height * $this->iscale);
661
662 } else { //no transparency
663 $this->im = imagecreate($this->image_width, $this->image_height);
664 $this->tmpimg = imagecreate($this->image_width * $this->iscale, $this->image_height * $this->iscale);
665 }
666
667 $this->allocateColors();
668 imagepalettecopy($this->tmpimg, $this->im);
669
670 $this->setBackground();
671
672 $this->createCode();
673
674 if (!$this->draw_lines_over_text && $this->num_lines > 0) $this->drawLines();
675
676 $this->drawWord();
677 if ($this->use_gd_font == false && is_readable($this->ttf_file)) $this->distortedCopy();
678
679 if ($this->draw_lines_over_text && $this->num_lines > 0) $this->drawLines();
680
681 if (trim($this->image_signature) != '') $this->addSignature();
682
683 $this->output();
684
685 }
drawLines()
Draw random curvy lines over the image Modified code from HKCaptcha.
output()
Output image to the browser.
allocateColors()
Allocate all colors that will be used in the CAPTCHA image.
drawWord()
Draw the CAPTCHA code over the image.
createCode()
Create a code and save to the session.
distortedCopy()
Warp text from temporary image onto final image.
setBackground()
Set the background of the CAPTCHA image.
addSignature()
Print signature text on image.
$img bgimg

References addSignature(), allocateColors(), bgimg, createCode(), distortedCopy(), drawLines(), drawWord(), image_height, image_signature, image_width, num_lines, output(), setBackground(), and use_transparent_text.

Referenced by show().

+ Here is the call graph for this function:

◆ drawLines()

Securimage::drawLines ( )

Draw random curvy lines over the image
Modified code from HKCaptcha.

Since
2.0 @access private

Definition at line 814 of file securimage.php.

815 {
816 for ($line = 0; $line < $this->num_lines; ++$line) {
817 $x = $this->image_width * (1 + $line) / ($this->num_lines + 1);
818 $x += (0.5 - $this->frand()) * $this->image_width / $this->num_lines;
819 $y = rand($this->image_height * 0.1, $this->image_height * 0.9);
820
821 $theta = ($this->frand()-0.5) * M_PI * 0.7;
822 $w = $this->image_width;
823 $len = rand($w * 0.4, $w * 0.7);
824 $lwid = rand(0, 2);
825
826 $k = $this->frand() * 0.6 + 0.2;
827 $k = $k * $k * 0.5;
828 $phi = $this->frand() * 6.28;
829 $step = 0.5;
830 $dx = $step * cos($theta);
831 $dy = $step * sin($theta);
832 $n = $len / $step;
833 $amp = 1.5 * $this->frand() / ($k + 5.0 / $len);
834 $x0 = $x - 0.5 * $len * cos($theta);
835 $y0 = $y - 0.5 * $len * sin($theta);
836
837 $ldx = round(-$dy * $lwid);
838 $ldy = round($dx * $lwid);
839
840 for ($i = 0; $i < $n; ++$i) {
841 $x = $x0 + $i * $dx + $amp * $dy * sin($k * $i * $step + $phi);
842 $y = $y0 + $i * $dy - $amp * $dx * sin($k * $i * $step + $phi);
843 $x=(int) $x;
844 $y=(int) $y;
845 imagefilledrectangle($this->im, $x, $y, $x + $lwid, $y + $lwid, $this->gdlinecolor);
846 }
847 }
848 }
foreach($array as $idx=> $m) $w
$n
Definition compute.php:54

References $i, $line, $n, $step, $w, $x, frand(), image_height, and image_width.

Referenced by doImage().

+ Here is the call graph for this function:

◆ drawWord()

Securimage::drawWord ( )

Draw the CAPTCHA code over the image.

@access private

Definition at line 856 of file securimage.php.

857 {
858 $width2 = $this->image_width * $this->iscale;
859 $height2 = $this->image_height * $this->iscale;
860
861 if ($this->use_gd_font == true || !is_readable($this->ttf_file??"")) {
862 if (!is_int($this->gd_font_file)) { //is a file name
863 $font = @imageloadfont($this->gd_font_file??"");
864 if ($font == false) {
865 trigger_error("Failed to load GD Font file {$this->gd_font_file} ", E_USER_WARNING);
866 return;
867 }
868 } else { //gd font identifier
869 $font = $this->gd_font_file;
870 }
871
872 imagestring($this->im, $font, $this->text_x_start, ($this->image_height / 2) - ($this->gd_font_size / 2), $this->code, $this->gdtextcolor);
873 } else { //ttf font
874 $font_size = $height2 * .35;
875 $bb = imagettfbbox($font_size, 0, $this->ttf_file, $this->code);
876 $tx = $bb[4] - $bb[0];
877 $ty = $bb[5] - $bb[1];
878 $x = floor($width2 / 2 - $tx / 2 - $bb[0]);
879 $y = round($height2 / 2 - $ty / 2 - $bb[1]);
880
881 $strlen = strlen($this->code);
882 if (!is_array($this->multi_text_color)) $this->use_multi_text = false;
883
884
885 if ($this->use_multi_text == false && $this->text_angle_minimum == 0 && $this->text_angle_maximum == 0) { // no angled or multi-color characters
886 imagettftext($this->tmpimg, $font_size, 0, $x, $y, $this->gdtextcolor, $this->ttf_file, $this->code);
887 } else {
888 for($i = 0; $i < $strlen; ++$i) {
889 $angle = rand($this->text_angle_minimum, $this->text_angle_maximum);
890 $y = rand($y - 5, $y + 5);
891 if ($this->use_multi_text == true) {
892 $font_color = $this->gdmulticolor[rand(0, sizeof($this->gdmulticolor) - 1)];
893 } else {
894 $font_color = $this->gdtextcolor;
895 }
896
897 $ch = $this->code[$i];
898
899 imagettftext($this->tmpimg, $font_size, $angle, $x, $y, $font_color, $this->ttf_file, $ch);
900
901 // estimate character widths to increment $x without creating spaces that are too large or too small
902 // these are best estimates to align text but may vary between fonts
903 // for optimal character widths, do not use multiple text colors or character angles and the complete string will be written by imagettftext
904 if (strpos('abcdeghknopqsuvxyz', $ch) !== false) {
905 $min_x = $font_size - ($this->iscale * 6);
906 $max_x = $font_size - ($this->iscale * 6);
907 } else if (strpos('ilI1', $ch) !== false) {
908 $min_x = $font_size / 5;
909 $max_x = $font_size / 3;
910 } else if (strpos('fjrt', $ch) !== false) {
911 $min_x = $font_size - ($this->iscale * 12);
912 $max_x = $font_size - ($this->iscale * 12);
913 } else if ($ch == 'wm') {
914 $min_x = $font_size;
915 $max_x = $font_size + ($this->iscale * 3);
916 } else { // numbers, capitals or unicode
917 $min_x = $font_size + ($this->iscale * 2);
918 $max_x = $font_size + ($this->iscale * 5);
919 }
920
921 $x += rand((int)$min_x,(int) $max_x);
922 } //for loop
923 } // angled or multi-color
924 } //else ttf font
925 //$this->im = $this->tmpimg;
926 //$this->output();
927 } //function

References $i, $x, image_height, image_width, multi_text_color, text_angle_maximum, text_angle_minimum, and use_multi_text.

Referenced by doImage().

◆ frand()

Securimage::frand ( )

Generate random number less than 1.

Since
2.0 @access private
Returns
float

Definition at line 1367 of file securimage.php.

1368 {
1369 return 0.0001*rand(0,9999);
1370 }

Referenced by distortedCopy(), and drawLines().

◆ generateCode()

Securimage::generateCode ( $len)

Generate a code.

@access private

Parameters
int$lenThe code length
Returns
string

Definition at line 1021 of file securimage.php.

1022 {
1023 $code = '';
1024
1025 for($i = 1, $cslen = strlen($this->charset??""); $i <= $len; ++$i) {
1026 $code .= $this->charset[rand(0, $cslen - 1)];
1027 }
1028 return $code;
1029 }

References $code, and $i.

Referenced by createCode().

◆ generateMP3()

Securimage::generateMP3 ( $letters)

Generate an mp3 file by concatenating individual files.

Since
1.0.4 @access private
Parameters
array$lettersArray of letters to build a file from
Returns
string MP3 file data

Definition at line 1339 of file securimage.php.

1340 {
1341 $data_len = 0;
1342 $files = array();
1343 $out_data = '';
1344
1345 foreach ($letters as $letter) {
1346 $filename = $this->audio_path . strtoupper($letter) . '.mp3';
1347
1348 $fp = fopen($filename, 'rb');
1349 $data = fread($fp, filesize($filename)); // read file in
1350
1351 $this->scrambleAudioData($data, 'mp3');
1352 $out_data .= $data;
1353
1354 fclose($fp);
1355 }
1356
1357
1358 return $out_data;
1359 }
scrambleAudioData(&$data, $format)
Randomly modify the audio data to scramble sound and prevent binary recognition.

References $data, $letter, and scrambleAudioData().

Referenced by getAudibleCode().

+ Here is the call graph for this function:

◆ generateWAV()

Securimage::generateWAV ( $letters)

Generate a wav file by concatenating individual files.

Since
1.0.1 @access private
Parameters
array$lettersArray of letters to build a file from
Returns
string WAV file data

Definition at line 1231 of file securimage.php.

1232 {
1233 $data_len = 0;
1234 $files = array();
1235 $out_data = '';
1236
1237 foreach ($letters as $letter) {
1238 $filename = $this->audio_path . strtoupper($letter) . '.wav';
1239
1240 $fp = fopen($filename, 'rb');
1241
1242 $file = array();
1243
1244 $data = fread($fp, filesize($filename)); // read file in
1245
1246 $header = substr($data, 0, 36);
1247 $body = substr($data, 44);
1248
1249
1250 $data = unpack('NChunkID/VChunkSize/NFormat/NSubChunk1ID/VSubChunk1Size/vAudioFormat/vNumChannels/VSampleRate/VByteRate/vBlockAlign/vBitsPerSample', $header);
1251
1252 $file['sub_chunk1_id'] = $data['SubChunk1ID'];
1253 $file['bits_per_sample'] = $data['BitsPerSample'];
1254 $file['channels'] = $data['NumChannels'];
1255 $file['format'] = $data['AudioFormat'];
1256 $file['sample_rate'] = $data['SampleRate'];
1257 $file['size'] = $data['ChunkSize'] + 8;
1258 $file['data'] = $body;
1259
1260 if ( ($p = strpos($file['data'], 'LIST')) !== false) {
1261 // If the LIST data is not at the end of the file, this will probably break your sound file
1262 $info = substr($file['data'], $p + 4, 8);
1263 $data = unpack('Vlength/Vjunk', $info);
1264 $file['data'] = substr($file['data'], 0, $p);
1265 $file['size'] = $file['size'] - (strlen($file['data']) - $p);
1266 }
1267
1268 $files[] = $file;
1269 $data = null;
1270 $header = null;
1271 $body = null;
1272
1273 $data_len += strlen($file['data']);
1274
1275 fclose($fp);
1276 }
1277
1278 $out_data = '';
1279 for($i = 0; $i < sizeof($files); ++$i) {
1280 if ($i == 0) { // output header
1281 $out_data .= pack('C4VC8', ord('R'), ord('I'), ord('F'), ord('F'), $data_len + 36, ord('W'), ord('A'), ord('V'), ord('E'), ord('f'), ord('m'), ord('t'), ord(' '));
1282
1283 $out_data .= pack('VvvVVvv',
1284 16,
1285 $files[$i]['format'],
1286 $files[$i]['channels'],
1287 $files[$i]['sample_rate'],
1288 $files[$i]['sample_rate'] * (($files[$i]['bits_per_sample'] * $files[$i]['channels']) / 8),
1289 ($files[$i]['bits_per_sample'] * $files[$i]['channels']) / 8,
1290 $files[$i]['bits_per_sample'] );
1291
1292 $out_data .= pack('C4', ord('d'), ord('a'), ord('t'), ord('a'));
1293
1294 $out_data .= pack('V', $data_len);
1295 }
1296
1297 $out_data .= $files[$i]['data'];
1298 }
1299
1300 $this->scrambleAudioData($out_data, 'wav');
1301 return $out_data;
1302 }
$p
Definition calendar.php:9
if(count($a_accounting)==0) $header

References $data, $file, $header, $i, $letter, $p, and scrambleAudioData().

Referenced by getAudibleCode().

+ Here is the call graph for this function:

◆ getAudibleCode()

Securimage::getAudibleCode ( $format = 'wav')

Get WAV or MP3 file data of the spoken code.


This is appropriate for output to the browser as audio/x-wav or audio/mpeg

Since
1.0.1
Returns
string WAV or MP3 data

Definition at line 1107 of file securimage.php.

1108 {
1109 $letters = array();
1110 $code = $this->getCode();
1111
1112 if ($code == '') {
1113 $this->createCode();
1114 $code = $this->getCode();
1115 }
1116
1117 for($i = 0; $i < strlen($code); ++$i) {
1118 $letters[] = $code[$i];
1119 }
1120
1121 if ($format == 'mp3') {
1122 return $this->generateMP3($letters);
1123 } else {
1124 return $this->generateWAV($letters);
1125 }
1126 }
generateMP3($letters)
Generate an mp3 file by concatenating individual files.
generateWAV($letters)
Generate a wav file by concatenating individual files.
getCode()
Get the captcha code.

References $code, $i, createCode(), generateMP3(), generateWAV(), and getCode().

Referenced by outputAudioFile().

+ Here is the call graph for this function:

◆ getBackgroundFromDirectory()

Securimage::getBackgroundFromDirectory ( )

Return the full path to a random gif, jpg, or png from the background directory.

@access private

See also
Securimage\$background_directory
Returns
mixed false if none found, string $path if found

Definition at line 787 of file securimage.php.

788 {
789 $images = array();
790
791 if ($dh = opendir($this->background_directory)) {
792 while (($file = readdir($dh)) !== false) {
793 if (preg_match('/(jpg|gif|png)$/i', $file)) $images[] = $file;
794 }
795
796 closedir($dh);
797
798 if (sizeof($images) > 0) {
799 return rtrim($this->background_directory, '/') . '/' . $images[rand(0, sizeof($images)-1)];
800 }
801 }
802
803 return false;
804 }
$img background_directory

References $file, and background_directory.

Referenced by setBackground().

◆ getCode()

Securimage::getCode ( )

Get the captcha code.

Since
1.0.1
Returns
string

Definition at line 1201 of file securimage.php.

1202 {
1203 if (isset($_SESSION['securimage_code_value']) && !empty($_SESSION['securimage_code_value'])) {
1204 return strtolower($_SESSION['securimage_code_value']);
1205 } else {
1206 if ($this->sqlite_handle == false) $this->openDatabase();
1207
1208 return $this->getCodeFromDatabase(); // attempt to get from database, returns empty string if sqlite is not available or disabled
1209 }
1210 }

References getCodeFromDatabase(), and openDatabase().

Referenced by getAudibleCode().

+ Here is the call graph for this function:

◆ getCodeFromDatabase()

Securimage::getCodeFromDatabase ( )

Get stored captcha code from sqlite database based on ip address hash.

@access private

Since
2.0.1
Returns
string captcha code

Definition at line 1463 of file securimage.php.

1464 {
1465 $code = '';
1466
1467 if ($this->use_sqlite_db && $this->sqlite_handle !== false) {
1468 $ip = $this->getIPHash();
1469
1470 $res = sqlite_query($this->sqlite_handle, "SELECT * FROM codes WHERE iphash = '$ip'");
1471 if ($res && sqlite_num_rows($res) > 0) {
1472 $res = sqlite_fetch_array($res);
1473
1474 if ($this->isCodeExpired($res['created']) == false) {
1475 $code = $res['code'];
1476 }
1477 }
1478 }
1479
1480 return $code;
1481 }
isCodeExpired($creation_time)
Check a code to see if it is expired based on creation time.

References $code, $res, getIPHash(), and isCodeExpired().

Referenced by getCode(), and validate().

+ Here is the call graph for this function:

◆ getIPHash()

Securimage::getIPHash ( )

Get hashed IP address of remote user.

@access private

Since
2.0.1
Returns
string

Definition at line 1401 of file securimage.php.

1402 {
1403 return strtolower(md5($_SERVER['REMOTE_ADDR']));
1404 }

Referenced by clearCodeFromDatabase(), getCodeFromDatabase(), and saveCodeToDatabase().

◆ isCodeExpired()

Securimage::isCodeExpired ( $creation_time)

Check a code to see if it is expired based on creation time.

@access private

Since
2.0.1
Parameters
$creation_timeunix timestamp of code creation time
Returns
bool true if code has expired, false if not

Definition at line 1522 of file securimage.php.

1523 {
1524 $expired = true;
1525
1526 if (!is_numeric($this->expiry_time) || $this->expiry_time < 1) {
1527 $expired = false;
1528 } else if (time() - $creation_time < $this->expiry_time) {
1529 $expired = false;
1530 }
1531
1532 return $expired;
1533 }

Referenced by getCodeFromDatabase(), and validate().

◆ openDatabase()

Securimage::openDatabase ( )

Open SQLite database.

@access private

Since
2.0.1
Returns
bool true if database was opened successfully

Definition at line 1413 of file securimage.php.

1414 {
1415 $this->sqlite_handle = false;
1416
1417 if ($this->use_sqlite_db && function_exists('sqlite_open')) {
1418 $this->sqlite_handle = sqlite_open($this->sqlite_database, 0666, $error);
1419
1420 if ($this->sqlite_handle !== false) {
1421 $res = sqlite_query($this->sqlite_handle, "PRAGMA table_info(codes)");
1422 if (sqlite_num_rows($res) == 0) {
1423 sqlite_query($this->sqlite_handle, "CREATE TABLE codes (iphash VARCHAR(32) PRIMARY KEY, code VARCHAR(32) NOT NULL, created INTEGER)");
1424 }
1425 }
1426
1427 return $this->sqlite_handle != false;
1428 }
1429
1430 return $this->sqlite_handle;
1431 }

References $error, and $res.

Referenced by getCode(), saveCodeToDatabase(), and validate().

◆ output()

Securimage::output ( )

Output image to the browser.

@access private

Definition at line 1069 of file securimage.php.

1070 {
1071 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
1072 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
1073 header("Cache-Control: no-store, no-cache, must-revalidate");
1074 header("Cache-Control: post-check=0, pre-check=0", false);
1075 header("Pragma: no-cache");
1076
1077 switch($this->image_type)
1078 {
1079 case SI_IMAGE_JPEG:
1080 header("Content-Type: image/jpeg");
1081 imagejpeg($this->im, null, 90);
1082 break;
1083
1084 case SI_IMAGE_GIF:
1085 header("Content-Type: image/gif");
1086 imagegif($this->im);
1087 break;
1088
1089 default:
1090 header("Content-Type: image/png");
1091 imagepng($this->im);
1092 break;
1093 }
1094
1095 imagedestroy($this->im);
1096 exit;
1097 }
for($e=0; $e< count($afiche); $e++) exit

References exit, and image_type.

Referenced by doImage().

◆ outputAudioFile()

Securimage::outputAudioFile ( )

Output audio file with HTTP headers to browser.

$sound = new Securimage(); $sound->audio_format = 'mp3'; $sound->outputAudioFile();

Since
2.0

Definition at line 624 of file securimage.php.

625 {
626 if (strtolower($this->audio_format) == 'wav') {
627 header('Content-type: audio/x-wav');
628 $ext = 'wav';
629 } else {
630 header('Content-type: audio/mpeg'); // default to mp3
631 $ext = 'mp3';
632 }
633
634 header("Content-Disposition: attachment; filename=\"securimage_audio.{$ext}\"");
635 header('Cache-Control: no-store, no-cache, must-revalidate');
636 header('Expires: Sun, 1 Jan 2000 12:00:00 GMT');
637 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT');
638
639 $audio = $this->getAudibleCode($ext);
640
641 header('Content-Length: ' . strlen($audio));
642
643 echo $audio;
644 exit;
645 }
getAudibleCode($format='wav')
Get WAV or MP3 file data of the spoken code.

References $ext, audio_format, exit, and getAudibleCode().

+ Here is the call graph for this function:

◆ purgeOldCodesFromDatabase()

Securimage::purgeOldCodesFromDatabase ( )

Purge codes over a day old from database.

@access private

Since
2.0.1

Definition at line 1504 of file securimage.php.

1505 {
1506 if ($this->use_sqlite_db && $this->sqlite_handle !== false) {
1507 $now = time();
1508 $limit = (!is_numeric($this->expiry_time) || $this->expiry_time < 1) ? 86400 : $this->expiry_time;
1509
1510 sqlite_query($this->sqlite_handle, "DELETE FROM codes WHERE $now - created > $limit");
1511 }
1512 }

References $limit.

◆ readCodeFromFile()

Securimage::readCodeFromFile ( )

Reads a word list file to get a code.

@access private

Since
1.0.2
Returns
mixed false on failure, a word on success

Definition at line 1038 of file securimage.php.

1039 {
1040 $fp = @fopen($this->wordlist_file, 'rb');
1041 if (!$fp) return false;
1042
1043 $fsize = filesize($this->wordlist_file);
1044 if ($fsize < 32) return false; // too small of a list to be effective
1045
1046 if ($fsize < 128) {
1047 $max = $fsize; // still pretty small but changes the range of seeking
1048 } else {
1049 $max = 128;
1050 }
1051
1052 fseek($fp, rand(0, $fsize - $max), SEEK_SET);
1053 $data = fread($fp, 128); // read a random 128 bytes from file
1054 fclose($fp);
1055 $data = preg_replace("/\r?\n/", "\n", $data);
1056
1057 $start = strpos($data, "\n", rand(0, 100)) + 1; // random start position
1058 $end = strpos($data, "\n", $start); // find end of word
1059
1060 return strtolower(substr($data, $start, $end - $start)); // return substring in 128 bytes
1061 }

References $data, $end, $max, and $start.

Referenced by createCode().

◆ saveCodeToDatabase()

Securimage::saveCodeToDatabase ( )

Save captcha code to sqlite database.

@access private

Since
2.0.1
Returns
bool true if code was saved, false if not

Definition at line 1440 of file securimage.php.

1441 {
1442 $success = false;
1443
1444 $this->openDatabase();
1445
1446 if ($this->use_sqlite_db && $this->sqlite_handle !== false) {
1447 $ip = $this->getIPHash();
1448 $time = time();
1449 $code = $_SESSION['securimage_code_value']; // hash code for security - if cookies are disabled the session still exists at this point
1450 $success = sqlite_query($this->sqlite_handle, "INSERT OR REPLACE INTO codes(iphash, code, created) VALUES('$ip', '$code', $time)");
1451 }
1452
1453 return $success !== false;
1454 }
openDatabase()
Open SQLite database.

References $code, getIPHash(), and openDatabase().

Referenced by saveData().

+ Here is the call graph for this function:

◆ saveData()

Securimage::saveData ( )

Save the code in the session.

@access private

Definition at line 1150 of file securimage.php.

1151 {
1152 $_SESSION['securimage_code_value'] = strtolower($this->code);
1153 $_SESSION['securimage_code_ctime'] = time();
1154
1155 $this->saveCodeToDatabase();
1156 }
saveCodeToDatabase()
Save captcha code to sqlite database.

References saveCodeToDatabase().

Referenced by createCode().

+ Here is the call graph for this function:

◆ scrambleAudioData()

Securimage::scrambleAudioData ( & $data,
$format )

Randomly modify the audio data to scramble sound and prevent binary recognition.


Take care not to "break" the audio file by leaving the header data intact.

Since
2.0 @access private
Parameters
$dataSound data in mp3 of wav format

Definition at line 1312 of file securimage.php.

1313 {
1314 if ($format == 'wav') {
1315 $start = strpos($data, 'data') + 4; // look for "data" indicator
1316 if ($start === false) $start = 44; // if not found assume 44 byte header
1317 } else { // mp3
1318 $start = 4; // 4 byte (32 bit) frame header
1319 }
1320
1321 $start += rand(1, 64); // randomize starting offset
1322 $datalen = strlen($data) - $start - 256; // leave last 256 bytes unchanged
1323
1324 for ($i = $start; $i < $datalen; $i += 64) {
1325 $ch = ord($data[$i]);
1326 if ($ch < 9 || $ch > 119) continue;
1327
1328 $data[$i] = chr($ch + rand(-8, 8));
1329 }
1330 }

References $data, $i, and $start.

Referenced by generateMP3(), and generateWAV().

◆ setAudioPath()

Securimage::setAudioPath ( $audio_directory)

Set the path to the audio directory.


Since
1.0.4
Returns
bool true if the directory exists and is readble, false if not

Definition at line 1134 of file securimage.php.

1135 {
1136 if (is_dir($audio_directory) && is_readable($audio_directory)) {
1137 $this->audio_path = $audio_directory;
1138 return true;
1139 } else {
1140 return false;
1141 }
1142 }

◆ setBackground()

Securimage::setBackground ( )

Set the background of the CAPTCHA image.

@access private

Definition at line 747 of file securimage.php.

748 {
749 imagefilledrectangle($this->im, 0, 0, $this->image_width * $this->iscale, $this->image_height * $this->iscale, $this->gdbgcolor);
750 imagefilledrectangle($this->tmpimg, 0, 0, $this->image_width * $this->iscale, $this->image_height * $this->iscale, $this->gdbgcolor);
751
752 if ($this->bgimg == '') {
753 if ($this->background_directory != null && is_dir($this->background_directory) && is_readable($this->background_directory)) {
755 if ($img != false) {
756 $this->bgimg = $img;
757 }
758 }
759 }
760
761 $dat = @getimagesize($this->bgimg);
762 if($dat == false) {
763 return;
764 }
765
766 switch($dat[2]) {
767 case 1: $newim = @imagecreatefromgif($this->bgimg); break;
768 case 2: $newim = @imagecreatefromjpeg($this->bgimg); break;
769 case 3: $newim = @imagecreatefrompng($this->bgimg); break;
770 case 15: $newim = @imagecreatefromwbmp($this->bgimg); break;
771 case 16: $newim = @imagecreatefromxbm($this->bgimg); break;
772 default: return;
773 }
774
775 if(!$newim) return;
776
777 imagecopyresized($this->im, $newim, 0, 0, 0, 0, $this->image_width, $this->image_height, imagesx($newim), imagesy($newim));
778 }
getBackgroundFromDirectory()
Return the full path to a random gif, jpg, or png from the background directory.

References $img, background_directory, bgimg, getBackgroundFromDirectory(), image_height, and image_width.

Referenced by doImage().

+ Here is the call graph for this function:

◆ show()

Securimage::show ( $background_image = "")

Generate a code and output the image to the browser.

<?php include 'securimage.php'; $securimage = new Securimage(); $securimage->show('bg.jpg'); ?>

Parameters
string$background_imageThe path to an image to use as the background for the CAPTCHA

Definition at line 583 of file securimage.php.

584 {
585 if($background_image != "" && is_readable($background_image)) {
586 $this->bgimg = $background_image;
587 }
588
589 $this->doImage();
590 }

References bgimg, and doImage().

+ Here is the call graph for this function:

◆ validate()

Securimage::validate ( )

Validate the code to the user code.

@access private

Definition at line 1164 of file securimage.php.

1165 {
1166 $code='';
1167 // retrieve code from session, if no code exists check sqlite database if supported.
1168
1169 if (isset($_SESSION['securimage_code_value']) && trim($_SESSION['securimage_code_value']) != '') {
1170 if ($this->isCodeExpired($_SESSION['securimage_code_ctime']) == false) {
1171 $code = $_SESSION['securimage_code_value'];
1172 }
1173 } else if ($this->use_sqlite_db == true && function_exists('sqlite_open')) { // no code in session - may mean user has cookies turned off
1174 $this->openDatabase();
1175 $code = $this->getCodeFromDatabase();
1176 } else {
1177 // session code invalid or non-existant and code not found in sqlite db or sqlite is not available
1178 $code = '';
1179 }
1180
1181 $code = trim(strtolower($code));
1182 $code_entered = trim(strtolower($this->code_entered));
1183 $this->correct_code = false;
1184
1185 if ($code != '') {
1186 if ($code == $code_entered) {
1187 $this->correct_code = true;
1188 $_SESSION['securimage_code_value'] = '';
1189 $_SESSION['securimage_code_ctime'] = '';
1190 $this->clearCodeFromDatabase();
1191 }
1192 }
1193 }
getCodeFromDatabase()
Get stored captcha code from sqlite database based on ip address hash.
clearCodeFromDatabase()
Delete a code from the database by ip address hash.

References $code, $code_entered, clearCodeFromDatabase(), getCodeFromDatabase(), isCodeExpired(), and openDatabase().

Referenced by check().

+ Here is the call graph for this function:

Field Documentation

◆ $audio_format

Securimage::$audio_format

Definition at line 349 of file securimage.php.

◆ $audio_path

Securimage::$audio_path

Definition at line 342 of file securimage.php.

◆ $background_directory

Securimage::$background_directory = null

Definition at line 248 of file securimage.php.

◆ $bgimg

Securimage::$bgimg

Definition at line 421 of file securimage.php.

◆ $charset

Securimage::$charset

Definition at line 145 of file securimage.php.

◆ $code

Securimage::$code

◆ $code_entered

Securimage::$code_entered

Definition at line 437 of file securimage.php.

Referenced by validate().

◆ $code_length

Securimage::$code_length

Definition at line 136 of file securimage.php.

◆ $correct_code

Securimage::$correct_code

Definition at line 445 of file securimage.php.

◆ $draw_lines_over_text

Securimage::$draw_lines_over_text

Definition at line 316 of file securimage.php.

◆ $expiry_time

Securimage::$expiry_time

Definition at line 367 of file securimage.php.

◆ $gd_font_file

Securimage::$gd_font_file

Definition at line 169 of file securimage.php.

◆ $gd_font_size

Securimage::$gd_font_size

Definition at line 178 of file securimage.php.

◆ $gdbgcolor

Securimage::$gdbgcolor

Definition at line 493 of file securimage.php.

◆ $gdlinecolor

Securimage::$gdlinecolor

Definition at line 461 of file securimage.php.

◆ $gdmulticolor

Securimage::$gdmulticolor

Definition at line 469 of file securimage.php.

◆ $gdsignaturecolor

Securimage::$gdsignaturecolor

Definition at line 485 of file securimage.php.

◆ $gdtextcolor

Securimage::$gdtextcolor

Definition at line 477 of file securimage.php.

◆ $im

Securimage::$im

Definition at line 396 of file securimage.php.

◆ $image_bg_color

Securimage::$image_bg_color

Definition at line 237 of file securimage.php.

◆ $image_height

Securimage::$image_height

Definition at line 121 of file securimage.php.

◆ $image_signature

Securimage::$image_signature

Definition at line 324 of file securimage.php.

◆ $image_type

Securimage::$image_type

Definition at line 129 of file securimage.php.

◆ $image_width

Securimage::$image_width

Definition at line 114 of file securimage.php.

◆ $iscale

Securimage::$iscale

Definition at line 413 of file securimage.php.

◆ $line_color

Securimage::$line_color

Definition at line 308 of file securimage.php.

◆ $multi_text_color

Securimage::$multi_text_color

Definition at line 273 of file securimage.php.

◆ $num_lines

Securimage::$num_lines

Definition at line 301 of file securimage.php.

◆ $perturbation

Securimage::$perturbation

Definition at line 202 of file securimage.php.

◆ $session_name

Securimage::$session_name = ''

Definition at line 358 of file securimage.php.

◆ $signature_color

Securimage::$signature_color

Definition at line 332 of file securimage.php.

◆ $signature_font

Securimage::$signature_font

Definition at line 334 of file securimage.php.

◆ $sqlite_database

Securimage::$sqlite_database

Definition at line 378 of file securimage.php.

◆ $sqlite_handle

Securimage::$sqlite_handle

Definition at line 453 of file securimage.php.

◆ $text_angle_maximum

Securimage::$text_angle_maximum

Definition at line 221 of file securimage.php.

◆ $text_angle_minimum

Securimage::$text_angle_minimum

Definition at line 212 of file securimage.php.

◆ $text_color

Securimage::$text_color

Definition at line 258 of file securimage.php.

◆ $text_transparency_percentage

Securimage::$text_transparency_percentage

Definition at line 290 of file securimage.php.

◆ $text_x_start

Securimage::$text_x_start

Definition at line 230 of file securimage.php.

◆ $tmpimg

Securimage::$tmpimg

Definition at line 404 of file securimage.php.

◆ $ttf_file

Securimage::$ttf_file

Definition at line 194 of file securimage.php.

◆ $use_gd_font

Securimage::$use_gd_font

Definition at line 185 of file securimage.php.

◆ $use_multi_text

Securimage::$use_multi_text

Definition at line 266 of file securimage.php.

◆ $use_sqlite_db

Securimage::$use_sqlite_db

Use an SQLite database for storing codes as a backup to sessions.


Note: Sessions will still be used

Definition at line 384 of file securimage.php.

◆ $use_transparent_text

Securimage::$use_transparent_text

Definition at line 281 of file securimage.php.

◆ $use_wordlist

Securimage::$use_wordlist = false

Definition at line 159 of file securimage.php.

◆ $wordlist_file

Securimage::$wordlist_file

Definition at line 152 of file securimage.php.


The documentation for this class was generated from the following file: