Class Securimage
Securimage CAPTCHA Class.
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.
Copyright: 2016 Drew Phillips
Author: Drew Phillips drew@drew-phillips.com
Version: 3.5.2
Located at securimage.php
Methods summary
public
|
#
__construct( array $options = array() )
Create a new securimage object, pass options to set in the constructor. |
public static
string
|
|
public static
null|string
|
#
getCaptchaId( boolean $new = true, array $options = array() )
Generate a new captcha ID or retrieve the current ID (if exists). |
public static
boolean
|
#
checkByCaptchaId( string $id, string $value, array $options = array() )
Validate a captcha code input against a captcha ID |
public
|
|
public
boolean
|
|
public static
string
|
#
getCaptchaHtml( array $options = array(), integer $parts = Securimage::HTML_ALL )
Returns HTML code for displaying the captcha image, audio button, and form text input. |
public
integer
|
|
public
|
#
setNamespace( string $namespace )
Set the namespace for the captcha being stored in the session or database. |
public
|
#
outputAudioFile( string $format = null )
Generate an audible captcha in WAV format and send it to the browser with appropriate headers. Example: |
public
|
#
rangeDownload( string $audio )
Output audio data with http range support. Typically this shouldn't be called directly unless being used with a custom implentation. Use Securimage::outputAudioFile instead. |
public
array|string
|
|
protected
|
|
protected
|
|
protected
|
|
protected
string|boolean
|
|
public
|
|
protected
|
|
protected
|
|
protected
|
|
protected
|
|
protected
|
|
protected
|
|
protected
string
|
|
protected
string|array|boolean
|
#
readCodeFromFile( integer $numWords = 1 )
Gets a captcha code from a file containing a list of words. |
protected
string
|
|
protected
|
|
protected
|
|
protected
|
|
protected
string|boolean
|
|
protected
|
|
protected
boolean
|
|
protected
boolean
|
|
protected
string
|
|
protected
boolean
|
|
protected
boolean
|
|
protected
string|array
|
#
getCodeFromDatabase( )
Retrieves a stored code from the database for based on the captchaId or IP address if captcha ID not used. |
protected
|
|
protected
|
|
protected
boolean
|
#
isCodeExpired( integer $creation_time )
Checks to see if the captcha code has expired and can no longer be used. |
protected
string
|
|
public
boolean|string
|
#
getRandomNoiseFile( )
Gets and returns the path to a random noise file from the audio noise directory. |
protected
string
|
#
getSoxEffectChain( integer $numEffects = 2 )
Get a random effect or chain of effects to apply to a segment of the audio file. |
protected
string
|
#
getSoxNoiseData( float $duration, integer $numChannels, integer $sampleRate, integer $bitRate )
This function is not yet used. |
protected
string
|
|
protected
string
|
|
protected
boolean
|
#
canSendHeaders( )
Checks to see if headers can be sent and if any error has been output to the browser |
public
float
|
|
protected
|
#
initColor( string $color,
Convert an html color code to a Securimage_Color |
public
boolean
|
#
errorHandler( integer $errno, string $errstr, string $errfile = '', integer $errline = 0, array $errcontext = array() )
The error handling function used when outputting captcha image or audio. |
Constants summary
integer |
SI_IMAGE_JPEG
Constant for rendering captcha as a JPEG image |
#
1
|
integer |
SI_IMAGE_PNG
Constant for rendering captcha as a PNG image (default) |
#
2
|
integer |
SI_IMAGE_GIF
Constant for rendering captcha as a GIF image |
#
3
|
integer |
SI_CAPTCHA_STRING
Constant for generating a normal alphanumeric captcha based on the character set |
#
0
|
integer |
SI_CAPTCHA_MATHEMATIC
Constant for generating a captcha consisting of a simple math problem |
#
1
|
integer |
SI_CAPTCHA_WORDS
Constant for generating a word based captcha using 2 words from a list |
#
2
|
string |
SI_DRIVER_MYSQL
MySQL option identifier for database storage option |
#
'mysql'
|
string |
SI_DRIVER_PGSQL
PostgreSQL option identifier for database storage option |
#
'pgsql'
|
string |
SI_DRIVER_SQLITE3
SQLite option identifier for database storage option |
#
'sqlite'
|
integer |
HTML_IMG
getCaptchaHtml() display constant for HTML Captcha Image |
#
1
|
integer |
HTML_AUDIO
getCaptchaHtml() display constant for HTML5 Audio code |
#
2
|
integer |
HTML_INPUT
getCaptchaHtml() display constant for Captcha Input text box |
#
4
|
integer |
HTML_INPUT_LABEL
getCaptchaHtml() display constant for Captcha Text HTML label |
#
8
|
integer |
HTML_ICON_REFRESH
getCaptchaHtml() display constant for HTML Refresh button |
#
16
|
integer |
HTML_ALL
getCaptchaHtml() display constant for all HTML elements (default) |
#
0xffffffff
|
Properties summary
public
integer
|
$image_width
The width of the captcha image |
#
215
|
public
integer
|
$image_height
The height of the captcha image |
#
80
|
public
float
|
$font_ratio
Font size is calculated by image height and this ratio. Leave blank for default ratio of 0.4. |
|
public
integer
|
$image_type
The type of the image, default = png |
#
self::SI_IMAGE_PNG
|
public
|
$image_bg_color
The background color of the captcha |
#
'#ffffff'
|
public
|
$text_color
The color of the captcha text |
#
'#707070'
|
public
|
$line_color
The color of the lines over the captcha |
#
'#707070'
|
public
|
$noise_color
The color of the noise that is drawn |
#
'#707070'
|
public
integer
|
$text_transparency_percentage
How transparent to make the text. |
#
20
|
public
boolean
|
$use_transparent_text
Whether or not to draw the text transparently. |
#
true
|
public
integer
|
$code_length
The length of the captcha code |
#
6
|
public
boolean
|
$case_sensitive
Whether the captcha should be case sensitive or not. |
#
false
|
public
string
|
$charset
The character set to use for generating the captcha code |
#
'ABCDEFGHKLMNPRSTUVWYZabcdefghklmnprstuvwyz23456789'
|
public
integer
|
$expiry_time
How long in seconds a captcha remains valid, after this time it will be considered incorrect. |
#
900
|
public
string
|
$session_name
The session name securimage should use. |
#
null
|
public
boolean
|
$use_wordlist
true to use the wordlist file, false to generate random captcha codes |
#
false
|
public
float
|
$perturbation
The level of distortion. |
#
0.85
|
public
integer
|
$num_lines
How many lines to draw over the captcha code to increase security |
#
5
|
public
integer
|
$noise_level
The level of noise (random dots) to place on the image, 0-10 |
#
2
|
public
string
|
$image_signature
The signature text to draw on the bottom corner of the image |
#
''
|
public
|
$signature_color
The color of the signature text |
#
'#707070'
|
public
string
|
$signature_font
The path to the ttf font file to use for the signature text. Defaults to $ttf_file (AHGBold.ttf) |
|
public
boolean
|
$use_sqlite_db
No longer used. |
#
false
|
public
boolean
|
$use_database
Use a database backend for code storage. Provides a fallback to users with cookies disabled. Required when using captcha IDs. |
#
false
|
public
boolean
|
$skip_table_check
Whether or not to skip checking if Securimage tables exist when using a database. |
#
false
|
public
string
|
$database_driver
Database driver to use for database support. Allowable values: mysql, pgsql, sqlite. Default: sqlite |
#
self::SI_DRIVER_SQLITE3
|
public
string
|
$database_host
Database host to connect to when using mysql or postgres |
#
'localhost'
|
public
string
|
$database_user
Database username for connection (mysql, postgres only) Default is an empty string |
#
''
|
public
string
|
$database_pass
Database password for connection (mysql, postgres only) Default is empty string |
#
''
|
public
string
|
$database_name
Name of the database to select (mysql, postgres only) |
#
''
|
public
string
|
$database_table
Database table where captcha codes are stored |
#
'captcha_codes'
|
public
string
|
$database_file
Fully qualified path to the database file when using SQLite3. |
|
public
integer
|
$captcha_type
The type of captcha to create. |
#
self::SI_CAPTCHA_STRING
|
public
string
|
$namespace
The captcha namespace used for having multiple captchas on a page or to separate captchas from differen forms on your site. Example: |
|
public
string
|
$ttf_file
The TTF font file to use to draw the captcha code. |
|
public
string
|
$wordlist_file
The path to the wordlist file to use. |
|
public
string
|
$wordlist_file_encoding
Character encoding of the wordlist file. Requires PHP Multibyte String (mbstring) support. Allows word list to contain characters other than US-ASCII (requires compatible TTF font). |
#
null
|
public
string
|
$background_directory
The directory to scan for background images, if set a random background will be chosen from this folder |
|
public
string
|
$sqlite_database
No longer used |
|
public
string
|
$audio_path
The path to the audio files to be used for audio captchas. |
|
public
boolean
|
$audio_use_sox
Use SoX (The Swiss Army knife of audio manipulation) for audio effects and processing. |
#
false
|
public
string
|
$sox_binary_path
The path to the SoX binary on your system |
#
'/usr/bin/sox'
|
public static
string
|
$lame_binary_path
The path to the lame (mp3 encoder) binary on your system Static so that Securimage::getCaptchaHtml() has access to this value. |
#
'/usr/bin/lame'
|
public
string
|
$audio_noise_path
The path to the directory containing audio files that will be selected randomly and mixed with the captcha audio. |
|
public
boolean
|
$audio_use_noise
Whether or not to mix background noise files into captcha audio |
|
public
float
|
$audio_mix_normalization
The method and threshold (or gain factor) used to normalize the mixing with background noise. |
#
0.8
|
public
boolean
|
$degrade_audio
Whether or not to degrade audio by introducing random noise. |
|
public
float
|
$audio_gap_min
Minimum delay to insert between captcha audio letters in milliseconds |
#
0
|
public
float
|
$audio_gap_max
Maximum delay to insert between captcha audio letters in milliseconds |
#
3000
|
protected static
string
|
$_captchaId
Captcha ID if using static captcha |
#
null
|
protected
resource
|
$im
The GD image resource of the captcha image |
|
protected
resource
|
$tmpimg
A temporary GD image resource of the captcha image for distortion |
|
protected
string
|
$bgimg
The background image GD resource |
|
protected
integer
|
$iscale
Scale factor for magnification of distorted captcha image |
#
5
|
public
string
|
$securimage_path
Absolute path to securimage directory. |
#
null
|
protected
string|boolean
|
$code
The captcha challenge value. |
|
protected
string
|
$code_display
The display value of the captcha to draw on the image |
|
public
string
|
$display_value
Alternate text to draw as the captcha image text |
|
protected
string
|
$captcha_code
Captcha code supplied by user [set from Securimage::check()] |
|
protected
integer
|
$_timeToSolve
Time (in seconds) that the captcha was solved in (correctly or incorrectly). |
#
0
|
protected
boolean
|
$no_exit
Flag that can be specified telling securimage not to call exit after generating a captcha image or audio file |
|
protected
boolean
|
$no_session
Flag indicating whether or not a PHP session should be started and used |
|
protected
boolean
|
$send_headers
Flag indicating whether or not HTTP headers will be sent when outputting captcha image/audio |
|
protected
PDO|boolean
|
$pdo_conn
PDO connection when a database is used |
|
protected
integer
|
$gdbgcolor
The GD color for the background color |
|
protected
integer
|
$gdtextcolor
The GD color for the text color |
|
protected
integer
|
$gdlinecolor
The GD color for the line color |
|
protected
integer
|
$gdsignaturecolor
The GD color for the signature text color |