|
noalyss
Version-6.7.2
|
Public Member Functions | |
| 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. | |
| Securimage () | |
| Class constructor. | |
| 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 | |
| $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 | |
Definition at line 107 of file securimage.php.
Print signature text on image.
Definition at line 1357 of file securimage.php.
References image_height, image_signature, and image_width.
Referenced by doImage().
{
if ($this->use_gd_font) {
imagestring($this->im, 5, $this->image_width - (strlen($this->image_signature) * 10), $this->image_height - 20, $this->image_signature, $this->gdsignaturecolor);
} else {
$bbox = imagettfbbox(10, 0, $this->signature_font, $this->image_signature);
$textlen = $bbox[2] - $bbox[0];
$x = $this->image_width - $textlen - 5;
$y = $this->image_height - 3;
imagettftext($this->im, 10, 0, $x, $y, $this->gdsignaturecolor, $this->signature_font, $this->image_signature);
}
}
Allocate all colors that will be used in the CAPTCHA image.
Definition at line 692 of file securimage.php.
References 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().
{
// allocate bg color first for imagecreate
$this->gdbgcolor = imagecolorallocate($this->im, $this->image_bg_color->r, $this->image_bg_color->g, $this->image_bg_color->b);
$alpha = intval($this->text_transparency_percentage / 100 * 127);
if ($this->use_transparent_text == true) {
$this->gdtextcolor = imagecolorallocatealpha($this->im, $this->text_color->r, $this->text_color->g, $this->text_color->b, $alpha);
$this->gdlinecolor = imagecolorallocatealpha($this->im, $this->line_color->r, $this->line_color->g, $this->line_color->b, $alpha);
} else {
$this->gdtextcolor = imagecolorallocate($this->im, $this->text_color->r, $this->text_color->g, $this->text_color->b);
$this->gdlinecolor = imagecolorallocate($this->im, $this->line_color->r, $this->line_color->g, $this->line_color->b);
}
$this->gdsignaturecolor = imagecolorallocate($this->im, $this->signature_color->r, $this->signature_color->g, $this->signature_color->b);
if ($this->use_multi_text == true) {
$this->gdmulticolor = array();
foreach($this->multi_text_color as $color) {
if ($this->use_transparent_text == true) {
$this->gdmulticolor[] = imagecolorallocatealpha($this->im, $color->r, $color->g, $color->b, $alpha);
} else {
$this->gdmulticolor[] = imagecolorallocate($this->im, $color->r, $color->g, $color->b);
}
}
}
}
| 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; }
| string | $code | The code the user entered |
Definition at line 605 of file securimage.php.
References $code, and validate().
Check if the user entered code was correct.
private
Definition at line 1196 of file securimage.php.
{
return $this->correct_code;
}
Delete a code from the database by ip address hash.
private
Definition at line 1467 of file securimage.php.
References getIPHash().
Referenced by validate().
{
if ($this->sqlite_handle !== false) {
$ip = $this->getIPHash();
sqlite_query($this->sqlite_handle, "DELETE FROM codes WHERE iphash = '$ip'");
}
}
Create a code and save to the session.
private
Definition at line 978 of file securimage.php.
References code_length, generateCode(), readCodeFromFile(), saveData(), and use_wordlist.
Referenced by doImage(), and getAudibleCode().
{
$this->code = false;
if ($this->use_wordlist && is_readable($this->wordlist_file)) {
$this->code = $this->readCodeFromFile();
}
if ($this->code == false) {
$this->code = $this->generateCode($this->code_length);
}
$this->saveData();
}
Warp text from temporary image onto final image.
Modified for securimage
private
Definition at line 918 of file securimage.php.
References $r, $rad, $tmp, frand(), image_height, image_width, and perturbation.
Referenced by doImage().
{
$numpoles = 3; // distortion factor
// make array of poles AKA attractor points
for ($i = 0; $i < $numpoles; ++$i) {
$px[$i] = rand($this->image_width * 0.3, $this->image_width * 0.7);
$py[$i] = rand($this->image_height * 0.3, $this->image_height * 0.7);
$rad[$i] = rand($this->image_width * 0.4, $this->image_width * 0.7);
$tmp = -$this->frand() * 0.15 - 0.15;
$amp[$i] = $this->perturbation * $tmp;
}
$bgCol = imagecolorat($this->tmpimg, 0, 0);
$width2 = $this->iscale * $this->image_width;
$height2 = $this->iscale * $this->image_height;
imagepalettecopy($this->im, $this->tmpimg); // copy palette to final image so text colors come across
// loop over $img pixels, take pixels from $tmpimg with distortion field
for ($ix = 0; $ix < $this->image_width; ++$ix) {
for ($iy = 0; $iy < $this->image_height; ++$iy) {
$x = $ix;
$y = $iy;
for ($i = 0; $i < $numpoles; ++$i) {
$dx = $ix - $px[$i];
$dy = $iy - $py[$i];
if ($dx == 0 && $dy == 0) continue;
$r = sqrt($dx * $dx + $dy * $dy);
if ($r > $rad[$i]) continue;
$rscale = $amp[$i] * sin(3.14 * $r / $rad[$i]);
$x += $dx * $rscale;
$y += $dy * $rscale;
}
$c = $bgCol;
$x *= $this->iscale;
$y *= $this->iscale;
if ($x >= 0 && $x < $width2 && $y >= 0 && $y < $height2) {
$c = imagecolorat($this->tmpimg, $x, $y);
}
if ($c != $bgCol) { // only copy pixels of letters to preserve any background image
imagesetpixel($this->im, $ix, $iy, $c);
}
}
}
}
Generate and output the image.
private
Definition at line 652 of file securimage.php.
References addSignature(), allocateColors(), bgimg, createCode(), distortedCopy(), drawLines(), drawWord(), image_height, image_signature, image_width, num_lines, output(), setBackground(), trim(), and use_transparent_text.
Referenced by show().
{
if ($this->use_gd_font == true) {
$this->iscale = 1;
}
if($this->use_transparent_text == true || $this->bgimg != "") {
$this->im = imagecreatetruecolor($this->image_width, $this->image_height);
$this->tmpimg = imagecreatetruecolor($this->image_width * $this->iscale, $this->image_height * $this->iscale);
} else { //no transparency
$this->im = imagecreate($this->image_width, $this->image_height);
$this->tmpimg = imagecreate($this->image_width * $this->iscale, $this->image_height * $this->iscale);
}
$this->allocateColors();
imagepalettecopy($this->tmpimg, $this->im);
$this->setBackground();
$this->createCode();
if (!$this->draw_lines_over_text && $this->num_lines > 0) $this->drawLines();
$this->drawWord();
if ($this->use_gd_font == false && is_readable($this->ttf_file)) $this->distortedCopy();
if ($this->draw_lines_over_text && $this->num_lines > 0) $this->drawLines();
if (trim($this->image_signature) != '') $this->addSignature();
$this->output();
}
Draw random curvy lines over the image
Modified code from HKCaptcha.
Definition at line 795 of file securimage.php.
References $line, $step, $w, frand(), image_height, image_width, and num_lines.
Referenced by doImage().
{
for ($line = 0; $line < $this->num_lines; ++$line) {
$x = $this->image_width * (1 + $line) / ($this->num_lines + 1);
$x += (0.5 - $this->frand()) * $this->image_width / $this->num_lines;
$y = rand($this->image_height * 0.1, $this->image_height * 0.9);
$theta = ($this->frand()-0.5) * M_PI * 0.7;
$w = $this->image_width;
$len = rand($w * 0.4, $w * 0.7);
$lwid = rand(0, 2);
$k = $this->frand() * 0.6 + 0.2;
$k = $k * $k * 0.5;
$phi = $this->frand() * 6.28;
$step = 0.5;
$dx = $step * cos($theta);
$dy = $step * sin($theta);
$n = $len / $step;
$amp = 1.5 * $this->frand() / ($k + 5.0 / $len);
$x0 = $x - 0.5 * $len * cos($theta);
$y0 = $y - 0.5 * $len * sin($theta);
$ldx = round(-$dy * $lwid);
$ldy = round($dx * $lwid);
for ($i = 0; $i < $n; ++$i) {
$x = $x0 + $i * $dx + $amp * $dy * sin($k * $i * $step + $phi);
$y = $y0 + $i * $dy - $amp * $dx * sin($k * $i * $step + $phi);
imagefilledrectangle($this->im, $x, $y, $x + $lwid, $y + $lwid, $this->gdlinecolor);
}
}
}
Draw the CAPTCHA code over the image.
private
Definition at line 835 of file securimage.php.
References image_height, image_width, multi_text_color, text_angle_maximum, text_angle_minimum, and use_multi_text.
Referenced by doImage().
{
$width2 = $this->image_width * $this->iscale;
$height2 = $this->image_height * $this->iscale;
if ($this->use_gd_font == true || !is_readable($this->ttf_file)) {
if (!is_int($this->gd_font_file)) { //is a file name
$font = @imageloadfont($this->gd_font_file);
if ($font == false) {
trigger_error("Failed to load GD Font file {$this->gd_font_file} ", E_USER_WARNING);
return;
}
} else { //gd font identifier
$font = $this->gd_font_file;
}
imagestring($this->im, $font, $this->text_x_start, ($this->image_height / 2) - ($this->gd_font_size / 2), $this->code, $this->gdtextcolor);
} else { //ttf font
$font_size = $height2 * .35;
$bb = imagettfbbox($font_size, 0, $this->ttf_file, $this->code);
$tx = $bb[4] - $bb[0];
$ty = $bb[5] - $bb[1];
$x = floor($width2 / 2 - $tx / 2 - $bb[0]);
$y = round($height2 / 2 - $ty / 2 - $bb[1]);
$strlen = strlen($this->code);
if (!is_array($this->multi_text_color)) $this->use_multi_text = false;
if ($this->use_multi_text == false && $this->text_angle_minimum == 0 && $this->text_angle_maximum == 0) { // no angled or multi-color characters
imagettftext($this->tmpimg, $font_size, 0, $x, $y, $this->gdtextcolor, $this->ttf_file, $this->code);
} else {
for($i = 0; $i < $strlen; ++$i) {
$angle = rand($this->text_angle_minimum, $this->text_angle_maximum);
$y = rand($y - 5, $y + 5);
if ($this->use_multi_text == true) {
$font_color = $this->gdmulticolor[rand(0, sizeof($this->gdmulticolor) - 1)];
} else {
$font_color = $this->gdtextcolor;
}
$ch = $this->code{$i};
imagettftext($this->tmpimg, $font_size, $angle, $x, $y, $font_color, $this->ttf_file, $ch);
// estimate character widths to increment $x without creating spaces that are too large or too small
// these are best estimates to align text but may vary between fonts
// for optimal character widths, do not use multiple text colors or character angles and the complete string will be written by imagettftext
if (strpos('abcdeghknopqsuvxyz', $ch) !== false) {
$min_x = $font_size - ($this->iscale * 6);
$max_x = $font_size - ($this->iscale * 6);
} else if (strpos('ilI1', $ch) !== false) {
$min_x = $font_size / 5;
$max_x = $font_size / 3;
} else if (strpos('fjrt', $ch) !== false) {
$min_x = $font_size - ($this->iscale * 12);
$max_x = $font_size - ($this->iscale * 12);
} else if ($ch == 'wm') {
$min_x = $font_size;
$max_x = $font_size + ($this->iscale * 3);
} else { // numbers, capitals or unicode
$min_x = $font_size + ($this->iscale * 2);
$max_x = $font_size + ($this->iscale * 5);
}
$x += rand($min_x, $max_x);
} //for loop
} // angled or multi-color
} //else ttf font
//$this->im = $this->tmpimg;
//$this->output();
} //function
Generate random number less than 1.
Definition at line 1345 of file securimage.php.
Referenced by distortedCopy(), and drawLines().
{
return 0.0001*rand(0,9999);
}
| Securimage::generateCode | ( | $ | len | ) |
Generate a code.
private
| int | $len | The code length |
Definition at line 1000 of file securimage.php.
References $code.
Referenced by createCode().
| Securimage::generateMP3 | ( | $ | letters | ) |
Generate an mp3 file by concatenating individual files.
| array | $letters | Array of letters to build a file from |
Definition at line 1317 of file securimage.php.
References $data, $filename, $letter, and scrambleAudioData().
Referenced by getAudibleCode().
{
$data_len = 0;
$files = array();
$out_data = '';
foreach ($letters as $letter) {
$filename = $this->audio_path . strtoupper($letter) . '.mp3';
$fp = fopen($filename, 'rb');
$data = fread($fp, filesize($filename)); // read file in
$this->scrambleAudioData($data, 'mp3');
$out_data .= $data;
fclose($fp);
}
return $out_data;
}
| Securimage::generateWAV | ( | $ | letters | ) |
Generate a wav file by concatenating individual files.
| array | $letters | Array of letters to build a file from |
Definition at line 1209 of file securimage.php.
References $data, $file, $filename, $header, $letter, and scrambleAudioData().
Referenced by getAudibleCode().
{
$data_len = 0;
$files = array();
$out_data = '';
foreach ($letters as $letter) {
$filename = $this->audio_path . strtoupper($letter) . '.wav';
$fp = fopen($filename, 'rb');
$file = array();
$data = fread($fp, filesize($filename)); // read file in
$header = substr($data, 0, 36);
$body = substr($data, 44);
$data = unpack('NChunkID/VChunkSize/NFormat/NSubChunk1ID/VSubChunk1Size/vAudioFormat/vNumChannels/VSampleRate/VByteRate/vBlockAlign/vBitsPerSample', $header);
$file['sub_chunk1_id'] = $data['SubChunk1ID'];
$file['bits_per_sample'] = $data['BitsPerSample'];
$file['channels'] = $data['NumChannels'];
$file['format'] = $data['AudioFormat'];
$file['sample_rate'] = $data['SampleRate'];
$file['size'] = $data['ChunkSize'] + 8;
$file['data'] = $body;
if ( ($p = strpos($file['data'], 'LIST')) !== false) {
// If the LIST data is not at the end of the file, this will probably break your sound file
$info = substr($file['data'], $p + 4, 8);
$data = unpack('Vlength/Vjunk', $info);
$file['data'] = substr($file['data'], 0, $p);
$file['size'] = $file['size'] - (strlen($file['data']) - $p);
}
$files[] = $file;
$data = null;
$header = null;
$body = null;
$data_len += strlen($file['data']);
fclose($fp);
}
$out_data = '';
for($i = 0; $i < sizeof($files); ++$i) {
if ($i == 0) { // output header
$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(' '));
$out_data .= pack('VvvVVvv',
16,
$files[$i]['format'],
$files[$i]['channels'],
$files[$i]['sample_rate'],
$files[$i]['sample_rate'] * (($files[$i]['bits_per_sample'] * $files[$i]['channels']) / 8),
($files[$i]['bits_per_sample'] * $files[$i]['channels']) / 8,
$files[$i]['bits_per_sample'] );
$out_data .= pack('C4', ord('d'), ord('a'), ord('t'), ord('a'));
$out_data .= pack('V', $data_len);
}
$out_data .= $files[$i]['data'];
}
$this->scrambleAudioData($out_data, 'wav');
return $out_data;
}
| 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
Definition at line 1086 of file securimage.php.
References $code, createCode(), generateMP3(), generateWAV(), and getCode().
Referenced by outputAudioFile().
{
$letters = array();
$code = $this->getCode();
if ($code == '') {
$this->createCode();
$code = $this->getCode();
}
for($i = 0; $i < strlen($code); ++$i) {
$letters[] = $code{$i};
}
if ($format == 'mp3') {
return $this->generateMP3($letters);
} else {
return $this->generateWAV($letters);
}
}
Return the full path to a random gif, jpg, or png from the background directory.
private
Definition at line 768 of file securimage.php.
References $file, and background_directory.
Referenced by setBackground().
{
$images = array();
if ($dh = opendir($this->background_directory)) {
while (($file = readdir($dh)) !== false) {
if (preg_match('/(jpg|gif|png)$/i', $file)) $images[] = $file;
}
closedir($dh);
if (sizeof($images) > 0) {
return rtrim($this->background_directory, '/') . '/' . $images[rand(0, sizeof($images)-1)];
}
}
return false;
}
Get the captcha code.
Definition at line 1179 of file securimage.php.
References getCodeFromDatabase(), and openDatabase().
Referenced by getAudibleCode().
{
if (isset($_SESSION['securimage_code_value']) && !empty($_SESSION['securimage_code_value'])) {
return strtolower($_SESSION['securimage_code_value']);
} else {
if ($this->sqlite_handle == false) $this->openDatabase();
return $this->getCodeFromDatabase(); // attempt to get from database, returns empty string if sqlite is not available or disabled
}
}
Get stored captcha code from sqlite database based on ip address hash.
private
Definition at line 1441 of file securimage.php.
References $code, $res, getIPHash(), and isCodeExpired().
Referenced by getCode(), and validate().
{
$code = '';
if ($this->use_sqlite_db && $this->sqlite_handle !== false) {
$ip = $this->getIPHash();
$res = sqlite_query($this->sqlite_handle, "SELECT * FROM codes WHERE iphash = '$ip'");
if ($res && sqlite_num_rows($res) > 0) {
$res = sqlite_fetch_array($res);
if ($this->isCodeExpired($res['created']) == false) {
$code = $res['code'];
}
}
}
return $code;
}
Get hashed IP address of remote user.
private
Definition at line 1379 of file securimage.php.
Referenced by clearCodeFromDatabase(), getCodeFromDatabase(), and saveCodeToDatabase().
{
return strtolower(md5($_SERVER['REMOTE_ADDR']));
}
| Securimage::isCodeExpired | ( | $ | creation_time | ) |
Check a code to see if it is expired based on creation time.
private
| $creation_time | unix timestamp of code creation time |
Definition at line 1500 of file securimage.php.
Referenced by getCodeFromDatabase(), and validate().
{
$expired = true;
if (!is_numeric($this->expiry_time) || $this->expiry_time < 1) {
$expired = false;
} else if (time() - $creation_time < $this->expiry_time) {
$expired = false;
}
return $expired;
}
Open SQLite database.
private
Definition at line 1391 of file securimage.php.
References $res.
Referenced by getCode(), saveCodeToDatabase(), and validate().
{
$this->sqlite_handle = false;
if ($this->use_sqlite_db && function_exists('sqlite_open')) {
$this->sqlite_handle = sqlite_open($this->sqlite_database, 0666, $error);
if ($this->sqlite_handle !== false) {
$res = sqlite_query($this->sqlite_handle, "PRAGMA table_info(codes)");
if (sqlite_num_rows($res) == 0) {
sqlite_query($this->sqlite_handle, "CREATE TABLE codes (iphash VARCHAR(32) PRIMARY KEY, code VARCHAR(32) NOT NULL, created INTEGER)");
}
}
return $this->sqlite_handle != false;
}
return $this->sqlite_handle;
}
Output image to the browser.
private
Definition at line 1048 of file securimage.php.
References exit, and image_type.
Referenced by doImage().
{
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
switch($this->image_type)
{
case SI_IMAGE_JPEG:
header("Content-Type: image/jpeg");
imagejpeg($this->im, null, 90);
break;
case SI_IMAGE_GIF:
header("Content-Type: image/gif");
imagegif($this->im);
break;
default:
header("Content-Type: image/png");
imagepng($this->im);
break;
}
imagedestroy($this->im);
exit;
}
Output audio file with HTTP headers to browser.
$sound = new Securimage(); $sound->audio_format = 'mp3'; $sound->outputAudioFile();
Definition at line 623 of file securimage.php.
References $ext, audio_format, echo, exit, and getAudibleCode().
{
if (strtolower($this->audio_format) == 'wav') {
header('Content-type: audio/x-wav');
$ext = 'wav';
} else {
header('Content-type: audio/mpeg'); // default to mp3
$ext = 'mp3';
}
header("Content-Disposition: attachment; filename=\"securimage_audio.{$ext}\"");
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Expires: Sun, 1 Jan 2000 12:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT');
$audio = $this->getAudibleCode($ext);
header('Content-Length: ' . strlen($audio));
echo $audio;
exit;
}
Purge codes over a day old from database.
private
Definition at line 1482 of file securimage.php.
{
if ($this->use_sqlite_db && $this->sqlite_handle !== false) {
$now = time();
$limit = (!is_numeric($this->expiry_time) || $this->expiry_time < 1) ? 86400 : $this->expiry_time;
sqlite_query($this->sqlite_handle, "DELETE FROM codes WHERE $now - created > $limit");
}
}
Reads a word list file to get a code.
private
Definition at line 1017 of file securimage.php.
References $data, $end, $max, and $start.
Referenced by createCode().
{
$fp = @fopen($this->wordlist_file, 'rb');
if (!$fp) return false;
$fsize = filesize($this->wordlist_file);
if ($fsize < 32) return false; // too small of a list to be effective
if ($fsize < 128) {
$max = $fsize; // still pretty small but changes the range of seeking
} else {
$max = 128;
}
fseek($fp, rand(0, $fsize - $max), SEEK_SET);
$data = fread($fp, 128); // read a random 128 bytes from file
fclose($fp);
$data = preg_replace("/\r?\n/", "\n", $data);
$start = strpos($data, "\n", rand(0, 100)) + 1; // random start position
$end = strpos($data, "\n", $start); // find end of word
return strtolower(substr($data, $start, $end - $start)); // return substring in 128 bytes
}
Save captcha code to sqlite database.
private
Definition at line 1418 of file securimage.php.
References $code, getIPHash(), and openDatabase().
Referenced by saveData().
{
$success = false;
$this->openDatabase();
if ($this->use_sqlite_db && $this->sqlite_handle !== false) {
$ip = $this->getIPHash();
$time = time();
$code = $_SESSION['securimage_code_value']; // hash code for security - if cookies are disabled the session still exists at this point
$success = sqlite_query($this->sqlite_handle, "INSERT OR REPLACE INTO codes(iphash, code, created) VALUES('$ip', '$code', $time)");
}
return $success !== false;
}
Save the code in the session.
private
Definition at line 1129 of file securimage.php.
References saveCodeToDatabase().
Referenced by createCode().
{
$_SESSION['securimage_code_value'] = strtolower($this->code);
$_SESSION['securimage_code_ctime'] = time();
$this->saveCodeToDatabase();
}
| 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.
| $data | Sound data in mp3 of wav format |
Definition at line 1290 of file securimage.php.
Referenced by generateMP3(), and generateWAV().
{
if ($format == 'wav') {
$start = strpos($data, 'data') + 4; // look for "data" indicator
if ($start === false) $start = 44; // if not found assume 44 byte header
} else { // mp3
$start = 4; // 4 byte (32 bit) frame header
}
$start += rand(1, 64); // randomize starting offset
$datalen = strlen($data) - $start - 256; // leave last 256 bytes unchanged
for ($i = $start; $i < $datalen; $i += 64) {
$ch = ord($data{$i});
if ($ch < 9 || $ch > 119) continue;
$data{$i} = chr($ch + rand(-8, 8));
}
}
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 506 of file securimage.php.
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, trim(), use_multi_text, use_transparent_text, and use_wordlist.
{
// Initialize session or attach to existing
if ( session_id() == '' ) { // no session has been started yet, which is needed for validation
if (trim($this->session_name) != '') {
session_name($this->session_name); // set session name if provided
}
session_start();
}
// Set Default Values
$this->image_width = 230;
$this->image_height = 80;
$this->image_type = SI_IMAGE_PNG;
$this->code_length = 6;
$this->charset = 'ABCDEFGHKLMNPRSTUVWYZabcdefghklmnprstuvwyz23456789';
$this->wordlist_file = './words/words.txt';
$this->use_wordlist = false;
$this->gd_font_file = 'gdfonts/automatic.gdf';
$this->use_gd_font = false;
$this->gd_font_size = 24;
$this->text_x_start = 15;
$this->ttf_file = './AHGBold.ttf';
$this->perturbation = 0.75;
$this->iscale = 5;
$this->text_angle_minimum = 0;
$this->text_angle_maximum = 0;
$this->image_bg_color = new Securimage_Color(0xff, 0xff, 0xff);
$this->text_color = new Securimage_Color(0x3d, 0x3d, 0x3d);
$this->multi_text_color = array(new Securimage_Color(0x0, 0x20, 0xCC),
new Securimage_Color(0x0, 0x30, 0xEE),
new Securimage_color(0x0, 0x40, 0xCC),
new Securimage_Color(0x0, 0x50, 0xEE),
new Securimage_Color(0x0, 0x60, 0xCC));
$this->use_multi_text = false;
$this->use_transparent_text = false;
$this->text_transparency_percentage = 30;
$this->num_lines = 10;
$this->line_color = new Securimage_Color(0x3d, 0x3d, 0x3d);
$this->draw_lines_over_text = true;
$this->image_signature = '';
$this->signature_color = new Securimage_Color(0x20, 0x50, 0xCC);
$this->signature_font = './AHGBold.ttf';
$this->audio_path = './audio/';
$this->audio_format = 'mp3';
$this->session_name = '';
$this->expiry_time = 900;
$this->sqlite_database = 'database/securimage.sqlite';
$this->use_sqlite_db = false;
$this->sqlite_handle = false;
}
| Securimage::setAudioPath | ( | $ | audio_directory | ) |
Set the path to the audio directory.
Definition at line 1113 of file securimage.php.
{
if (is_dir($audio_directory) && is_readable($audio_directory)) {
$this->audio_path = $audio_directory;
return true;
} else {
return false;
}
}
Set the background of the CAPTCHA image.
private
Definition at line 728 of file securimage.php.
References $img, background_directory, bgimg, getBackgroundFromDirectory(), image_height, and image_width.
Referenced by doImage().
{
imagefilledrectangle($this->im, 0, 0, $this->image_width * $this->iscale, $this->image_height * $this->iscale, $this->gdbgcolor);
imagefilledrectangle($this->tmpimg, 0, 0, $this->image_width * $this->iscale, $this->image_height * $this->iscale, $this->gdbgcolor);
if ($this->bgimg == '') {
if ($this->background_directory != null && is_dir($this->background_directory) && is_readable($this->background_directory)) {
$img = $this->getBackgroundFromDirectory();
if ($img != false) {
$this->bgimg = $img;
}
}
}
$dat = @getimagesize($this->bgimg);
if($dat == false) {
return;
}
switch($dat[2]) {
case 1: $newim = @imagecreatefromgif($this->bgimg); break;
case 2: $newim = @imagecreatefromjpeg($this->bgimg); break;
case 3: $newim = @imagecreatefrompng($this->bgimg); break;
case 15: $newim = @imagecreatefromwbmp($this->bgimg); break;
case 16: $newim = @imagecreatefromxbm($this->bgimg); break;
default: return;
}
if(!$newim) return;
imagecopyresized($this->im, $newim, 0, 0, 0, 0, $this->image_width, $this->image_height, imagesx($newim), imagesy($newim));
}
| 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'); ?>
| string | $background_image | The path to an image to use as the background for the CAPTCHA |
Definition at line 582 of file securimage.php.
Validate the code to the user code.
private
Definition at line 1143 of file securimage.php.
References $code, $code_entered, clearCodeFromDatabase(), getCodeFromDatabase(), isCodeExpired(), openDatabase(), and trim().
Referenced by check().
{
// retrieve code from session, if no code exists check sqlite database if supported.
if (isset($_SESSION['securimage_code_value']) && trim($_SESSION['securimage_code_value']) != '') {
if ($this->isCodeExpired($_SESSION['securimage_code_ctime']) == false) {
$code = $_SESSION['securimage_code_value'];
}
} else if ($this->use_sqlite_db == true && function_exists('sqlite_open')) { // no code in session - may mean user has cookies turned off
$this->openDatabase();
$code = $this->getCodeFromDatabase();
} else {
// session code invalid or non-existant and code not found in sqlite db or sqlite is not available
$code = '';
}
$code = trim(strtolower($code));
$code_entered = trim(strtolower($this->code_entered));
$this->correct_code = false;
if ($code != '') {
if ($code == $code_entered) {
$this->correct_code = true;
$_SESSION['securimage_code_value'] = '';
$_SESSION['securimage_code_ctime'] = '';
$this->clearCodeFromDatabase();
}
}
}
| Securimage::$audio_format |
Definition at line 348 of file securimage.php.
| Securimage::$audio_path |
Definition at line 341 of file securimage.php.
| Securimage::$background_directory = null |
Definition at line 248 of file securimage.php.
| Securimage::$bgimg |
Definition at line 420 of file securimage.php.
| Securimage::$charset |
Definition at line 145 of file securimage.php.
| Securimage::$code |
Definition at line 428 of file securimage.php.
Referenced by check(), generateCode(), getAudibleCode(), getCodeFromDatabase(), saveCodeToDatabase(), and validate().
| Securimage::$code_entered |
Definition at line 436 of file securimage.php.
Referenced by validate().
| Securimage::$code_length |
Definition at line 136 of file securimage.php.
| Securimage::$correct_code |
Definition at line 444 of file securimage.php.
| Securimage::$draw_lines_over_text |
Definition at line 316 of file securimage.php.
| Securimage::$expiry_time |
Definition at line 366 of file securimage.php.
| Securimage::$gd_font_file |
Definition at line 169 of file securimage.php.
| Securimage::$gd_font_size |
Definition at line 178 of file securimage.php.
| Securimage::$gdbgcolor |
Definition at line 492 of file securimage.php.
| Securimage::$gdlinecolor |
Definition at line 460 of file securimage.php.
| Securimage::$gdmulticolor |
Definition at line 468 of file securimage.php.
| Securimage::$gdsignaturecolor |
Definition at line 484 of file securimage.php.
| Securimage::$gdtextcolor |
Definition at line 476 of file securimage.php.
| Securimage::$im |
Definition at line 395 of file securimage.php.
| Securimage::$image_bg_color |
Definition at line 237 of file securimage.php.
| Securimage::$image_height |
Definition at line 121 of file securimage.php.
| Securimage::$image_signature |
Definition at line 324 of file securimage.php.
| Securimage::$image_type |
Definition at line 129 of file securimage.php.
| Securimage::$image_width |
Definition at line 114 of file securimage.php.
| Securimage::$iscale |
Definition at line 412 of file securimage.php.
| Securimage::$line_color |
Definition at line 308 of file securimage.php.
| Securimage::$multi_text_color |
Definition at line 273 of file securimage.php.
| Securimage::$num_lines |
Definition at line 301 of file securimage.php.
| Securimage::$perturbation |
Definition at line 202 of file securimage.php.
| Securimage::$session_name = '' |
Definition at line 357 of file securimage.php.
| Securimage::$signature_color |
Definition at line 332 of file securimage.php.
| Securimage::$sqlite_database |
Definition at line 377 of file securimage.php.
| Securimage::$sqlite_handle |
Definition at line 452 of file securimage.php.
| Securimage::$text_angle_maximum |
Definition at line 221 of file securimage.php.
| Securimage::$text_angle_minimum |
Definition at line 212 of file securimage.php.
| Securimage::$text_color |
Definition at line 258 of file securimage.php.
| Securimage::$text_transparency_percentage |
Definition at line 290 of file securimage.php.
| Securimage::$text_x_start |
Definition at line 230 of file securimage.php.
| Securimage::$tmpimg |
Definition at line 403 of file securimage.php.
| Securimage::$ttf_file |
Definition at line 194 of file securimage.php.
| Securimage::$use_gd_font |
Definition at line 185 of file securimage.php.
| Securimage::$use_multi_text |
Definition at line 266 of file securimage.php.
| 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 383 of file securimage.php.
| Securimage::$use_transparent_text |
Definition at line 281 of file securimage.php.
| Securimage::$use_wordlist = false |
Definition at line 159 of file securimage.php.
| Securimage::$wordlist_file |
Definition at line 152 of file securimage.php.