This class provides a way to use the ObfuscatedImage on a web page, in concert with an encrypted digest and key.
| [RW] | clean_up_interval | |
| [R] | digest | |
| [RW] | font | |
| [RW] | font_size | |
| [RW] | image_dir | |
| [RW] | image_uri | |
| [R] | key | |
| [RW] | rotation | |
| [RW] | template_file | |
| [RW] | x_spacing | |
| [RW] | y_wiggle |
Create a new Web object, configuring it from the file with the given name. Any variables not set in the configuration file will be given default values.
[ show source ]
# File lib/captcha.rb, line 236
236: def Web.from_configuration( file_name )
237: eval File.open( file_name, "r" ) { |file| file.read }
238:
239: options = Hash.new
240:
241: ( local_variables - [ "file_name", "options" ] ).each do |local|
242: local_sym = eval( ":#{local}" )
243: options[ local_sym ] = eval( local )
244: end
245:
246: return new( options )
247: end
Test the given key against the given digest. If the digest was created from the given key, return true, otherwise return false.
[ show source ]
# File lib/captcha.rb, line 308
308: def Web.is_valid( key, digest )
309: new_digest = OpenSSL::Digest::MD5.new( key ).hexdigest
310: return ( digest == new_digest )
311: end
Initialize a new Web object with the given values. The valid parameters are the same as the valid configuration variables (VALID_CONFIG_VARS).
[ show source ]
# File lib/captcha.rb, line 251
251: def initialize( options={} )
252: validate_and_instantiate_parameters( options, VALID_CONFIG_VARS )
253:
254: @key = @use_chars.random( @key_length )
255: @digest = OpenSSL::Digest::MD5.new( @key ).hexdigest
256: end
Look in the image directory, deleting any images there that are more than ‘clean_up_interval’ seconds old.
[ show source ]
# File lib/captcha.rb, line 297
297: def clean
298: Dir.foreach( @image_dir ) do |entry|
299: next if entry !~ /\.png$/
300: if Time.now - File.stat( File.join( @image_dir, entry ) ).mtime > @clean_up_interval
301: File.delete( File.join( @image_dir, entry ) )
302: end
303: end
304: end
Get the name of the image file. This will generate the file name if it has not yet been generated.
[ show source ]
# File lib/captcha.rb, line 260
260: def file_name
261: @file_name = "%08X.png" % [ rand(0xFFFFFFFF) ] if @file_name.nil?
262: @file_name
263: end
Get the ObfuscatedImage that will be shown on the page. If the image has not yet been created, this will create it.
[ show source ]
# File lib/captcha.rb, line 267
267: def image
268: if @image.nil?
269: @image = ObfuscatedImage.create( @key, @font, @font_size, @x_spacing, @y_wiggle, @rotation )
270: File.open( File.join( @image_dir, file_name ), "w" ) { |file| @image.png file }
271: end
272:
273: @image
274: end
Convert the object to HTML, using the given template file name if provided. If a template is not given here, the one used to configure the object will be used instead. The template file should contain the following tokens:
- %%IMAGE%%: the file name of the image
- %%IMAGEURI%%: the web-accessible directory containing the image
- %%DIGEST%%: the digest string
- %%IMAGEWIDTH%%: the width of the image file
- %%IMAGEHEIGHT%%: the height of the image file
[ show source ]
# File lib/captcha.rb, line 285
285: def to_html( template_file=nil )
286: template = File.open( ( template_file || @template_file ), "r" ) { |file| file.read }
287:
288: template.gsub( /%%IMAGE%%/, file_name ).
289: gsub( /%%IMAGEURI%%/, @image_uri ).
290: gsub( /%%DIGEST%%/, @digest ).
291: gsub( /%%IMAGEWIDTH%%/, image.width.to_s ).
292: gsub( /%%IMAGEHEIGHT%%/, image.height.to_s )
293: end