PHP Класс Securimage, PHP-Fusion

A class for creating and validating secure CAPTCHA images and audio. The class contains many options regarding appearance, security, storage of captcha data and image/audio generation options.
Автор: Drew Phillips ([email protected])
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$audio_format string Type of audio file to generate (mp3 or wav)
$audio_path string Full path to the WAV files to use to make the audio files, include trailing /.
Name Files [A-Z0-9].wav
$background_directory string Scan this directory for gif, jpg, and png files to use as background images.
A random image file will be picked each time.
Change from null to the full path to your directory.
i.e. var $background_directory = $_SERVER['DOCUMENT_ROOT'] . '/securimage/backgrounds'; Make sure not to pass a background image to the show function, otherwise this directive is ignored.
$bgimg resource The background image resource
$charset string The character set for individual characters in the image.
Letters are converted to uppercase.
The font must support the letters or there may be problematic substitutions.
$code string The code generated by the script
$code_entered string The code that was entered by the user
$code_length integer The length of the code to generate.
$correct_code boolean Whether or not the correct code was entered
$draw_lines_over_text boolean Draw the lines over the text.
If fales lines will be drawn before putting the text on the image.
$expiry_time integer The amount of time in seconds that a code remains valid.
Any code older than this number will be considered invalid even if entered correctly.
Any non-numeric or value less than 1 disables this functionality.
$gd_font_file mixed Note: Use of GD fonts is not recommended as many distortion features are not available
The GD font to use.
Internal gd fonts can be loaded by their number.
Alternatively, a file path can be given and the font will be loaded from file.
$gd_font_size integer The approximate size of the font in pixels.
This does not control the size of the font because that is determined by the GD font itself.
This is used to aid the calculations of positioning used by this class.
$gdbgcolor integer Color resource for image background color
$gdlinecolor integer Color resource for image line color
$gdmulticolor array Array of colors for multi colored codes
$gdsignaturecolor integer Color resource for image signature color
$gdtextcolor integer Color resource for image font color
$im resource The gd image resource.
$image_bg_color Securimage_Color The background color for the image as a Securimage_Color.
$image_height integer The desired width of the CAPTCHA image.
$image_signature Text to write at the bottom corner of captcha image
$image_type integer The image format for output.
Valid options: SI_IMAGE_PNG, SI_IMAGE_JPG, SI_IMAGE_GIF
$image_width integer The desired width of the CAPTCHA image.
$iscale integer Internal scale factor for anti-alias @hkcaptcha
$line_color string Color of lines drawn over text
$multi_text_color array Array of Securimage_Colors which will be randomly selected for each letter.
$num_lines boolean Draw vertical and horizontal lines on the image.
$perturbation float How much to distort image, higher = more distortion.
Distortion is only available when using TTF fonts.
$session_name string The session name to use if not the default. Blank for none
$signature_color Securimage_Color Color to use for writing signature text
$sqlite_database string Path to the file to use for storing codes for users.
THIS FILE MUST ABSOLUTELY NOT BE ACCESSIBLE FROM A WEB BROWSER!!
Put this file in a directory below the web root or one that is restricted (i.e. an apache .htaccess file with deny from all)
If you cannot meet those requirements your forms may not be completely protected.
You could obscure the database file name but this is also not recommended.
$sqlite_handle resource Handle to SQLite database
$text_angle_maximum integer The minimum angle in degrees, with 0 degrees being left-to-right reading text.
Higher values represent a counter-clockwise rotation.
For example, a value of 90 would result in bottom-to-top reading text.
$text_angle_minimum integer The minimum angle in degrees, with 0 degrees being left-to-right reading text.
Higher values represent a counter-clockwise rotation.
For example, a value of 90 would result in bottom-to-top reading text.
This value along with maximum angle distance do not need to be very high with perturbation
$text_color Securimage_Color The text color to use for drawing characters as a Securimage_Color.
This value is ignored if $use_multi_text is set to true.
Make sure this contrasts well with the background color or image.
$text_transparency_percentage integer The percentage of transparency, 0 to 100.
A value of 0 is completely opaque, 100 is completely transparent (invisble)
$text_x_start integer The X-Position on the image where letter drawing will begin.
This value is in pixels from the left side of the image.
$tmpimg resource Temporary image for rendering
$ttf_file string The path to the TTF font file to load.
$use_gd_font Use a gd font instead of TTF
$use_multi_text boolean Set to true to use multiple colors for each character.
$use_sqlite_db Use an SQLite database for storing codes as a backup to sessions.
Note: Sessions will still be used
$use_transparent_text boolean Set to true to make the characters appear transparent.
$use_wordlist Use wordlist of not
$wordlist_file Create codes using this word list

Открытые методы

Метод Описание
Securimage ( ) 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.
addSignature ( ) Print signature text on image
allocateColors ( ) Allocate all colors that will be used in the CAPTCHA image
check ( string $code ) : boolean Validate the code entered by the user.
checkCode ( ) : boolean 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.
Modified for securimage
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 ( ) : float Generate random number less than 1
generateCode ( integer $len ) : string Generate a code
generateMP3 ( array $letters ) : string Generate an mp3 file by concatenating individual files
generateWAV ( array $letters ) : string Generate a wav file by concatenating individual files
getAudibleCode ( $format = 'wav' ) : string 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
getBackgroundFromDirectory ( ) : mixed Return the full path to a random gif, jpg, or png from the background directory.
getCode ( ) : string Get the captcha code
getCodeFromDatabase ( ) : string Get stored captcha code from sqlite database based on ip address hash
getIPHash ( ) : string Get hashed IP address of remote user
isCodeExpired ( $creation_time ) : boolean Check a code to see if it is expired based on creation time
openDatabase ( ) : boolean Open SQLite database
output ( ) Output image to the browser
outputAudioFile ( ) Output audio file with HTTP headers to browser $sound = new Securimage(); $sound->audio_format = 'mp3'; $sound->outputAudioFile();
purgeOldCodesFromDatabase ( ) Purge codes over a day old from database
readCodeFromFile ( ) : mixed Reads a word list file to get a code
saveCodeToDatabase ( ) : boolean 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.
Take care not to "break" the audio file by leaving the header data intact.
setAudioPath ( $audio_directory ) : boolean Set the path to the audio directory.
setBackground ( ) Set the background of the CAPTCHA image
show ( string $background_image = "" ) Generate a code and output the image to the browser.
validate ( ) Validate the code to the user code

Описание методов

Securimage() публичный Метод

$securimage = new Securimage();
public Securimage ( )

addSignature() публичный Метод

Print signature text on image
С версии: 2.0
public addSignature ( )

allocateColors() публичный Метод

Allocate all colors that will be used in the CAPTCHA image
С версии: 2.0.1
public allocateColors ( )

check() публичный Метод

$code = $_POST['code']; if ($securimage->check($code) == false) { die("Sorry, the code entered did not match."); } else { $valid = true; }
public check ( string $code ) : boolean
$code string The code the user entered
Результат boolean true if the code was correct, false if not

checkCode() публичный Метод

Check if the user entered code was correct
public checkCode ( ) : boolean
Результат boolean

clearCodeFromDatabase() публичный Метод

Delete a code from the database by ip address hash
С версии: 2.0.1

createCode() публичный Метод

Create a code and save to the session
С версии: 1.0.1
public createCode ( )

distortedCopy() публичный Метод

Warp text from temporary image onto final image.
Modified for securimage
С версии: 2.0
Автор: Han-Kwang Nienhuys modified
public distortedCopy ( )

doImage() публичный Метод

Generate and output the image
public doImage ( )

drawLines() публичный Метод

Draw random curvy lines over the image
Modified code from HKCaptcha
С версии: 2.0
public drawLines ( )

drawWord() публичный Метод

Draw the CAPTCHA code over the image
public drawWord ( )

frand() публичный Метод

Generate random number less than 1
С версии: 2.0
public frand ( ) : float
Результат float

generateCode() публичный Метод

Generate a code
public generateCode ( integer $len ) : string
$len integer The code length
Результат string

generateMP3() публичный Метод

Generate an mp3 file by concatenating individual files
С версии: 1.0.4
public generateMP3 ( array $letters ) : string
$letters array Array of letters to build a file from
Результат string MP3 file data

generateWAV() публичный Метод

Generate a wav file by concatenating individual files
С версии: 1.0.1
public generateWAV ( array $letters ) : string
$letters array Array of letters to build a file from
Результат string WAV file data

getAudibleCode() публичный Метод

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
С версии: 1.0.1
public getAudibleCode ( $format = 'wav' ) : string
Результат string WAV or MP3 data

getBackgroundFromDirectory() публичный Метод

Return the full path to a random gif, jpg, or png from the background directory.
См. также: Securimage::$background_directory
public getBackgroundFromDirectory ( ) : mixed
Результат mixed false if none found, string $path if found

getCode() публичный Метод

Get the captcha code
С версии: 1.0.1
public getCode ( ) : string
Результат string

getCodeFromDatabase() публичный Метод

Get stored captcha code from sqlite database based on ip address hash
С версии: 2.0.1
public getCodeFromDatabase ( ) : string
Результат string captcha code

getIPHash() публичный Метод

Get hashed IP address of remote user
С версии: 2.0.1
public getIPHash ( ) : string
Результат string

isCodeExpired() публичный Метод

Check a code to see if it is expired based on creation time
С версии: 2.0.1
public isCodeExpired ( $creation_time ) : boolean
$creation_time unix timestamp of code creation time
Результат boolean true if code has expired, false if not

openDatabase() публичный Метод

Open SQLite database
С версии: 2.0.1
public openDatabase ( ) : boolean
Результат boolean true if database was opened successfully

output() публичный Метод

Output image to the browser
public output ( )

outputAudioFile() публичный Метод

Output audio file with HTTP headers to browser $sound = new Securimage(); $sound->audio_format = 'mp3'; $sound->outputAudioFile();
С версии: 2.0
public outputAudioFile ( )

purgeOldCodesFromDatabase() публичный Метод

Purge codes over a day old from database
С версии: 2.0.1

readCodeFromFile() публичный Метод

Reads a word list file to get a code
С версии: 1.0.2
public readCodeFromFile ( ) : mixed
Результат mixed false on failure, a word on success

saveCodeToDatabase() публичный Метод

Save captcha code to sqlite database
С версии: 2.0.1
public saveCodeToDatabase ( ) : boolean
Результат boolean true if code was saved, false if not

saveData() публичный Метод

Save the code in the session
public saveData ( )

scrambleAudioData() публичный Метод

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.
С версии: 2.0
public scrambleAudioData ( &$data, $format )
$data Sound data in mp3 of wav format

setAudioPath() публичный Метод

Set the path to the audio directory.
С версии: 1.0.4
public setAudioPath ( $audio_directory ) : boolean
Результат boolean true if the directory exists and is readble, false if not

setBackground() публичный Метод

Set the background of the CAPTCHA image
public setBackground ( )

show() публичный Метод

show('bg.jpg');
public show ( string $background_image = "" )
$background_image string The path to an image to use as the background for the CAPTCHA

validate() публичный Метод

Validate the code to the user code
public validate ( )

Описание свойств

$audio_format публичное свойство

Type of audio file to generate (mp3 or wav)
public string $audio_format
Результат string

$audio_path публичное свойство

Full path to the WAV files to use to make the audio files, include trailing /.
Name Files [A-Z0-9].wav
С версии: 1.0.1
public string $audio_path
Результат string

$background_directory публичное свойство

Scan this directory for gif, jpg, and png files to use as background images.
A random image file will be picked each time.
Change from null to the full path to your directory.
i.e. var $background_directory = $_SERVER['DOCUMENT_ROOT'] . '/securimage/backgrounds'; Make sure not to pass a background image to the show function, otherwise this directive is ignored.
public string $background_directory
Результат string

$bgimg публичное свойство

The background image resource
public resource $bgimg
Результат resource

$charset публичное свойство

The character set for individual characters in the image.
Letters are converted to uppercase.
The font must support the letters or there may be problematic substitutions.
public string $charset
Результат string

$code публичное свойство

The code generated by the script
public string $code
Результат string

$code_entered публичное свойство

The code that was entered by the user
public string $code_entered
Результат string

$code_length публичное свойство

The length of the code to generate.
public int $code_length
Результат integer

$correct_code публичное свойство

Whether or not the correct code was entered
public bool $correct_code
Результат boolean

$draw_lines_over_text публичное свойство

Draw the lines over the text.
If fales lines will be drawn before putting the text on the image.
public bool $draw_lines_over_text
Результат boolean

$expiry_time публичное свойство

The amount of time in seconds that a code remains valid.
Any code older than this number will be considered invalid even if entered correctly.
Any non-numeric or value less than 1 disables this functionality.
public int $expiry_time
Результат integer

$gd_font_file публичное свойство

Note: Use of GD fonts is not recommended as many distortion features are not available
The GD font to use.
Internal gd fonts can be loaded by their number.
Alternatively, a file path can be given and the font will be loaded from file.
public mixed $gd_font_file
Результат mixed

$gd_font_size публичное свойство

The approximate size of the font in pixels.
This does not control the size of the font because that is determined by the GD font itself.
This is used to aid the calculations of positioning used by this class.
public int $gd_font_size
Результат integer

$gdbgcolor публичное свойство

Color resource for image background color
public int $gdbgcolor
Результат integer

$gdlinecolor публичное свойство

Color resource for image line color
public int $gdlinecolor
Результат integer

$gdmulticolor публичное свойство

Array of colors for multi colored codes
public array $gdmulticolor
Результат array

$gdsignaturecolor публичное свойство

Color resource for image signature color
public int $gdsignaturecolor
Результат integer

$gdtextcolor публичное свойство

Color resource for image font color
public int $gdtextcolor
Результат integer

$im публичное свойство

The gd image resource.
public resource $im
Результат resource

$image_bg_color публичное свойство

The background color for the image as a Securimage_Color.
public Securimage_Color $image_bg_color
Результат Securimage_Color

$image_height публичное свойство

The desired width of the CAPTCHA image.
public int $image_height
Результат integer

$image_signature публичное свойство

Text to write at the bottom corner of captcha image
С версии: 2.0
public $image_signature

$image_type публичное свойство

The image format for output.
Valid options: SI_IMAGE_PNG, SI_IMAGE_JPG, SI_IMAGE_GIF
public int $image_type
Результат integer

$image_width публичное свойство

The desired width of the CAPTCHA image.
public int $image_width
Результат integer

$iscale публичное свойство

Internal scale factor for anti-alias @hkcaptcha
С версии: 2.0
public int $iscale
Результат integer

$line_color публичное свойство

Color of lines drawn over text
public string $line_color
Результат string

$multi_text_color публичное свойство

Array of Securimage_Colors which will be randomly selected for each letter.
public array $multi_text_color
Результат array

$num_lines публичное свойство

Draw vertical and horizontal lines on the image.
См. также: Securimage::$line_color
См. также: Securimage::$draw_lines_over_text
public bool $num_lines
Результат boolean

$perturbation публичное свойство

How much to distort image, higher = more distortion.
Distortion is only available when using TTF fonts.
public float $perturbation
Результат float

$session_name публичное свойство

The session name to use if not the default. Blank for none
См. также: http://php.net/session_name
С версии: 2.0
public string $session_name
Результат string

$signature_color публичное свойство

Color to use for writing signature text
С версии: 2.0
public Securimage_Color $signature_color
Результат Securimage_Color

$sqlite_database публичное свойство

Path to the file to use for storing codes for users.
THIS FILE MUST ABSOLUTELY NOT BE ACCESSIBLE FROM A WEB BROWSER!!
Put this file in a directory below the web root or one that is restricted (i.e. an apache .htaccess file with deny from all)
If you cannot meet those requirements your forms may not be completely protected.
You could obscure the database file name but this is also not recommended.
public string $sqlite_database
Результат string

$sqlite_handle публичное свойство

Handle to SQLite database
public resource $sqlite_handle
Результат resource

$text_angle_maximum публичное свойство

The minimum angle in degrees, with 0 degrees being left-to-right reading text.
Higher values represent a counter-clockwise rotation.
For example, a value of 90 would result in bottom-to-top reading text.
public int $text_angle_maximum
Результат integer

$text_angle_minimum публичное свойство

The minimum angle in degrees, with 0 degrees being left-to-right reading text.
Higher values represent a counter-clockwise rotation.
For example, a value of 90 would result in bottom-to-top reading text.
This value along with maximum angle distance do not need to be very high with perturbation
public int $text_angle_minimum
Результат integer

$text_color публичное свойство

The text color to use for drawing characters as a Securimage_Color.
This value is ignored if $use_multi_text is set to true.
Make sure this contrasts well with the background color or image.
См. также: Securimage::$use_multi_text
public Securimage_Color $text_color
Результат Securimage_Color

$text_transparency_percentage публичное свойство

The percentage of transparency, 0 to 100.
A value of 0 is completely opaque, 100 is completely transparent (invisble)
См. также: Securimage::$use_transparent_text
public int $text_transparency_percentage
Результат integer

$text_x_start публичное свойство

The X-Position on the image where letter drawing will begin.
This value is in pixels from the left side of the image.
Устаревший: 2.0
public int $text_x_start
Результат integer

$tmpimg публичное свойство

Temporary image for rendering
public resource $tmpimg
Результат resource

$ttf_file публичное свойство

The path to the TTF font file to load.
public string $ttf_file
Результат string

$use_gd_font публичное свойство

Use a gd font instead of TTF
public $use_gd_font

$use_multi_text публичное свойство

Set to true to use multiple colors for each character.
См. также: Securimage::$multi_text_color
public bool $use_multi_text
Результат boolean

$use_sqlite_db публичное свойство

Use an SQLite database for storing codes as a backup to sessions.
Note: Sessions will still be used
public $use_sqlite_db

$use_transparent_text публичное свойство

Set to true to make the characters appear transparent.
См. также: Securimage::$text_transparency_percentage
public bool $use_transparent_text
Результат boolean

$use_wordlist публичное свойство

Use wordlist of not
public $use_wordlist

$wordlist_file публичное свойство

Create codes using this word list
public $wordlist_file