simple_captcha 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +9 -0
- data/Rakefile +61 -0
- data/VERSION +1 -0
- data/assets/migrate/create_simple_captcha_data.rb +15 -0
- data/assets/migrate/create_simple_captcha_data_less_than_2.0.rb +16 -0
- data/assets/views/simple_captcha/_simple_captcha.erb +40 -0
- data/init.rb +9 -0
- data/install.rb +1 -0
- data/lib/simple_captcha_action_controller.rb +34 -0
- data/lib/simple_captcha_action_view.rb +124 -0
- data/lib/simple_captcha_active_record.rb +89 -0
- data/lib/simple_captcha_config.rb +26 -0
- data/lib/simple_captcha_controller.rb +18 -0
- data/lib/simple_captcha_data.rb +23 -0
- data/lib/simple_captcha_image.rb +108 -0
- data/lib/simple_captcha_setup.rb +59 -0
- data/tasks/simple_captcha_tasks.rake +15 -0
- data/test/simple_captcha_test.rb +10 -0
- metadata +79 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 [Sur http://expressica.com]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "simple_captcha"
|
8
|
+
gem.summary = %Q{SimpleCaptcha is the simplest and a robust captcha.}
|
9
|
+
gem.description = %Q{SimpleCaptcha is the simplest and a robust captcha. Its implementation requires adding up a single line in views and in controllers/models. SimpleCaptcha is available to be used with Rails2.0 or above and also it provides the backward compatibility with previous versions of Rails.}
|
10
|
+
gem.email = "joloudov@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/joloudov/simple_captcha"
|
12
|
+
gem.authors = ["joloudov"]
|
13
|
+
gem.files = [
|
14
|
+
'[A-Z]*',
|
15
|
+
'*.rb',
|
16
|
+
'assets/**/*.rb',
|
17
|
+
'assets/**/**/*.erb',
|
18
|
+
'lib/*.rb',
|
19
|
+
'tasks/*.rake',
|
20
|
+
]
|
21
|
+
end
|
22
|
+
Jeweler::GemcutterTasks.new
|
23
|
+
rescue LoadError
|
24
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Test the simple_captcha plugin.'
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib'
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
begin
|
36
|
+
require 'rcov/rcovtask'
|
37
|
+
Rcov::RcovTask.new do |test|
|
38
|
+
test.libs << 'test'
|
39
|
+
test.pattern = 'test/**/test_*.rb'
|
40
|
+
test.verbose = true
|
41
|
+
end
|
42
|
+
rescue LoadError
|
43
|
+
task :rcov do
|
44
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
task :test => :check_dependencies
|
49
|
+
|
50
|
+
task :default => :test
|
51
|
+
|
52
|
+
desc 'Generate documentation for the simple_captcha.'
|
53
|
+
require 'rake/rdoctask'
|
54
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
55
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
56
|
+
rdoc.rdoc_dir = 'rdoc'
|
57
|
+
rdoc.title = 'SimpleCaptcha'
|
58
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
59
|
+
rdoc.rdoc_files.include('README')
|
60
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
61
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# Copyright (c) 2008 [Sur http://expressica.com]
|
2
|
+
|
3
|
+
class CreateSimpleCaptchaData < ActiveRecord::Migration
|
4
|
+
def self.up
|
5
|
+
create_table :simple_captcha_data do |t|
|
6
|
+
t.string :key, :limit => 40
|
7
|
+
t.string :value, :limit => 6
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
drop_table :simple_captcha_data
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Copyright (c) 2008 [Sur http://expressica.com]
|
2
|
+
|
3
|
+
class CreateSimpleCaptchaData < ActiveRecord::Migration
|
4
|
+
def self.up
|
5
|
+
create_table :simple_captcha_data do |t|
|
6
|
+
t.column :key, :string, :limit => 40
|
7
|
+
t.column :value, :string, :limit => 6
|
8
|
+
t.column :created_at, :datetime
|
9
|
+
t.column :updated_at, :datetime
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
drop_table :simple_captcha_data
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
<!-- Copyright (c) 2007 [Sur http://expressica.com] -->
|
2
|
+
|
3
|
+
<style type="text/CSS">
|
4
|
+
|
5
|
+
#simple_captcha{border: 1px solid #ccc; padding: 5px !important;}
|
6
|
+
#simple_captcha,
|
7
|
+
#simple_captcha div{display: table;}
|
8
|
+
#simple_captcha .simple_captcha_field,
|
9
|
+
#simple_captcha .simple_captcha_image{
|
10
|
+
border: 1px solid #ccc;
|
11
|
+
margin: 0px 0px 2px 0px !important;
|
12
|
+
padding: 0px !important;
|
13
|
+
}
|
14
|
+
#simple_captcha .simple_captcha_image img{
|
15
|
+
margin: 0px !important;
|
16
|
+
padding: 0px !important;
|
17
|
+
width: 110px !important;
|
18
|
+
}
|
19
|
+
#simple_captcha .simple_captcha_label{font-size: 12px;}
|
20
|
+
#simple_captcha .simple_captcha_field input{
|
21
|
+
width: 150px !important;
|
22
|
+
font-size: 16px;
|
23
|
+
border: none;
|
24
|
+
background-color: #efefef;
|
25
|
+
}
|
26
|
+
</style>
|
27
|
+
|
28
|
+
<div id='simple_captcha'>
|
29
|
+
<div class='simple_captcha_image'>
|
30
|
+
<%= @simple_captcha_options[:image] %>
|
31
|
+
</div>
|
32
|
+
|
33
|
+
<div class='simple_captcha_field'>
|
34
|
+
<%= @simple_captcha_options[:field] %>
|
35
|
+
</div>
|
36
|
+
|
37
|
+
<div class='simple_captcha_label'>
|
38
|
+
<%= @simple_captcha_options[:label] %>
|
39
|
+
</div>
|
40
|
+
</div>
|
data/init.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# Copyright (c) 2008 [Sur http://expressica.com]
|
2
|
+
|
3
|
+
require 'simple_captcha_setup'
|
4
|
+
require 'simple_captcha_config'
|
5
|
+
require 'simple_captcha_image'
|
6
|
+
require 'simple_captcha_action_view'
|
7
|
+
require 'simple_captcha_action_controller'
|
8
|
+
require 'simple_captcha_active_record'
|
9
|
+
require 'simple_captcha_controller'
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# Copyright (c) 2008 [Sur http://expressica.com]
|
2
|
+
|
3
|
+
module SimpleCaptcha #:nodoc
|
4
|
+
module ControllerHelpers #:nodoc
|
5
|
+
|
6
|
+
include ConfigTasks
|
7
|
+
|
8
|
+
# This method is to validate the simple captcha in controller.
|
9
|
+
# It means when the captcha is controller based i.e. :object has not been passed to the method show_simple_captcha.
|
10
|
+
#
|
11
|
+
# *Example*
|
12
|
+
#
|
13
|
+
# If you want to save an object say @user only if the captcha is validated then do like this in action...
|
14
|
+
#
|
15
|
+
# if simple_captcha_valid?
|
16
|
+
# @user.save
|
17
|
+
# else
|
18
|
+
# flash[:notice] = "captcha did not match"
|
19
|
+
# redirect_to :action => "myaction"
|
20
|
+
# end
|
21
|
+
def simple_captcha_valid?
|
22
|
+
return true if RAILS_ENV == 'test'
|
23
|
+
if params[:captcha]
|
24
|
+
data = simple_captcha_value
|
25
|
+
result = data == params[:captcha].delete(" ").upcase
|
26
|
+
simple_captcha_passed! if result
|
27
|
+
return result
|
28
|
+
else
|
29
|
+
return false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
# Copyright (c) 2008 [Sur http://expressica.com]
|
2
|
+
|
3
|
+
module SimpleCaptcha #:nodoc
|
4
|
+
module ViewHelpers #:nodoc
|
5
|
+
|
6
|
+
include ConfigTasks
|
7
|
+
|
8
|
+
# Simple Captcha is a very simplified captcha.
|
9
|
+
#
|
10
|
+
# It can be used as a *Model* or a *Controller* based Captcha depending on what options
|
11
|
+
# we are passing to the method show_simple_captcha.
|
12
|
+
#
|
13
|
+
# *show_simple_captcha* method will return the image, the label and the text box.
|
14
|
+
# This method should be called from the view within your form as...
|
15
|
+
#
|
16
|
+
# <%= show_simple_captcha %>
|
17
|
+
#
|
18
|
+
# The available options to pass to this method are
|
19
|
+
# * label
|
20
|
+
# * image_syle
|
21
|
+
# * object
|
22
|
+
# * distortion
|
23
|
+
#
|
24
|
+
# <b>Label:</b>
|
25
|
+
#
|
26
|
+
# default label is "type the text from the image", it can be modified by passing :label as
|
27
|
+
#
|
28
|
+
# <%= show_simple_captcha(:label => "new captcha label") %>.
|
29
|
+
#
|
30
|
+
# <b>Image Style:</b>
|
31
|
+
#
|
32
|
+
# There are eight different styles of images available as...
|
33
|
+
# * embosed_silver
|
34
|
+
# * simply_red
|
35
|
+
# * simply_green
|
36
|
+
# * simply_blue
|
37
|
+
# * distorted_black
|
38
|
+
# * all_black
|
39
|
+
# * charcoal_grey
|
40
|
+
# * almost_invisible
|
41
|
+
#
|
42
|
+
# The default image is simply_blue and can be modified by passing any of the above style as...
|
43
|
+
#
|
44
|
+
# <%= show_simple_captcha(:image_style => "simply_red") %>
|
45
|
+
#
|
46
|
+
# The images can also be selected randomly by using *random* in the image_style as
|
47
|
+
#
|
48
|
+
# <%= show_simple_captcha(:image_style => "random") %>
|
49
|
+
#
|
50
|
+
# *Object*
|
51
|
+
#
|
52
|
+
# This option is needed to create a model based captcha.
|
53
|
+
# If this option is not provided, the captcha will be controller based and
|
54
|
+
# should be checked in controller's action just by calling the method simple_captcha_valid?
|
55
|
+
#
|
56
|
+
# To make a model based captcha give this option as...
|
57
|
+
#
|
58
|
+
# <%= show_simple_captcha(:object => "user") %>
|
59
|
+
# and also call the method apply_simple_captcha in the model
|
60
|
+
# this will consider "user" as the object of the model class.
|
61
|
+
#
|
62
|
+
# *Examples*
|
63
|
+
# * controller based
|
64
|
+
# <%= show_simple_captcha(:image_style => "embosed_silver", :label => "Human Authentication: type the text from image above") %>
|
65
|
+
# * model based
|
66
|
+
# <%= show_simple_captcha(:object => "person", :image_style => "simply_blue", :label => "Human Authentication: type the text from image above") %>
|
67
|
+
#
|
68
|
+
# Find more detailed examples with sample images here on my blog http://EXPRESSICA.com
|
69
|
+
#
|
70
|
+
# All Feedbacks/CommentS/Issues/Queries are welcome.
|
71
|
+
def show_simple_captcha(options={})
|
72
|
+
options[:field_value] = set_simple_captcha_data(options[:code_type])
|
73
|
+
@simple_captcha_options =
|
74
|
+
{:image => simple_captcha_image(options),
|
75
|
+
:label => options[:label] || "(type the code from the image)",
|
76
|
+
:field => simple_captcha_field(options)}
|
77
|
+
render :partial => 'simple_captcha/simple_captcha'
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def simple_captcha_image(options={})
|
83
|
+
url =
|
84
|
+
simple_captcha_url(
|
85
|
+
:action => 'simple_captcha',
|
86
|
+
:simple_captcha_key => simple_captcha_key,
|
87
|
+
:image_style => options[:image_style] || '',
|
88
|
+
:distortion => options[:distortion] || '',
|
89
|
+
:time => Time.now.to_i)
|
90
|
+
"<img src='#{url}' alt='simple_captcha.jpg' />"
|
91
|
+
end
|
92
|
+
|
93
|
+
def simple_captcha_field(options={})
|
94
|
+
options[:object] ?
|
95
|
+
text_field(options[:object], :captcha, :value => '') +
|
96
|
+
hidden_field(options[:object], :captcha_key, {:value => options[:field_value]}) :
|
97
|
+
text_field_tag(:captcha)
|
98
|
+
end
|
99
|
+
|
100
|
+
def set_simple_captcha_data(code_type)
|
101
|
+
key, value = simple_captcha_key, generate_simple_captcha_data(code_type)
|
102
|
+
data = SimpleCaptchaData.get_data(key)
|
103
|
+
data.value = value
|
104
|
+
data.save
|
105
|
+
key
|
106
|
+
end
|
107
|
+
|
108
|
+
def generate_simple_captcha_data(code)
|
109
|
+
value = ''
|
110
|
+
case code
|
111
|
+
when 'numeric'
|
112
|
+
6.times{value << (48 + rand(10)).chr}
|
113
|
+
else
|
114
|
+
6.times{value << (65 + rand(26)).chr}
|
115
|
+
end
|
116
|
+
return value
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
ActionView::Base.module_eval do
|
123
|
+
include SimpleCaptcha::ViewHelpers
|
124
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# Copyright (c) 2008 [Sur http://expressica.com]
|
2
|
+
|
3
|
+
module SimpleCaptcha #:nodoc
|
4
|
+
module ModelHelpers #:nodoc
|
5
|
+
|
6
|
+
# To implement model based simple captcha use this method in the model as...
|
7
|
+
#
|
8
|
+
# class User < ActiveRecord::Base
|
9
|
+
#
|
10
|
+
# apply_simple_captcha :message => "my customized message"
|
11
|
+
#
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# Customize the error message by using :message, the default message is "Captcha did not match".
|
15
|
+
# As in the applications captcha is needed with a very few cases like signing up the new user, but
|
16
|
+
# not every time you need to authenticate the captcha with @user.save. So as to maintain simplicity
|
17
|
+
# here we have the explicit method to save the instace with captcha validation as...
|
18
|
+
#
|
19
|
+
# * to validate the instance
|
20
|
+
#
|
21
|
+
# @user.valid_with_captcha? # whene captcha validation is required.
|
22
|
+
#
|
23
|
+
# @user.valid? # when captcha validation is not required.
|
24
|
+
#
|
25
|
+
# * to save the instance
|
26
|
+
#
|
27
|
+
# @user.save_with_captcha # whene captcha validation is required.
|
28
|
+
#
|
29
|
+
# @user.save # when captcha validation is not required.
|
30
|
+
module ClassMethods
|
31
|
+
def apply_simple_captcha(options = {})
|
32
|
+
instance_variable_set(:@add_to_base, options[:add_to_base])
|
33
|
+
instance_variable_set(:@captcha_invalid_message, options[:message] || "Secret Code did not match with the Image")
|
34
|
+
module_eval do
|
35
|
+
include SimpleCaptcha::ConfigTasks
|
36
|
+
attr_accessor :captcha, :captcha_key, :authenticate_with_captcha
|
37
|
+
alias_method :valid_without_captcha?, :valid?
|
38
|
+
alias_method :save_without_captcha, :save
|
39
|
+
include SimpleCaptcha::ModelHelpers::InstanceMethods
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module InstanceMethods
|
45
|
+
def valid?
|
46
|
+
return valid_without_captcha? if RAILS_ENV == 'test'
|
47
|
+
if authenticate_with_captcha
|
48
|
+
ret = valid_without_captcha?
|
49
|
+
if captcha && captcha.upcase.delete(" ") == simple_captcha_value(captcha_key)
|
50
|
+
ret = ret && true
|
51
|
+
else
|
52
|
+
ret = false
|
53
|
+
self.class.instance_variable_get(:@add_to_base) == true ?
|
54
|
+
self.errors.add_to_base(self.class.instance_variable_get(:@captcha_invalid_message)) :
|
55
|
+
self.errors.add(:captcha, self.class.instance_variable_get(:@captcha_invalid_message))
|
56
|
+
end
|
57
|
+
simple_captcha_passed!(captcha_key) if ret
|
58
|
+
return ret
|
59
|
+
else
|
60
|
+
return valid_without_captcha?
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def valid_with_captcha?
|
65
|
+
return valid_without_captcha? if RAILS_ENV == 'test'
|
66
|
+
self.authenticate_with_captcha = true
|
67
|
+
ret = self.valid?
|
68
|
+
self.authenticate_with_captcha = false
|
69
|
+
ret
|
70
|
+
end
|
71
|
+
|
72
|
+
def save_with_captcha
|
73
|
+
self.authenticate_with_captcha = true
|
74
|
+
ret = self.save_without_captcha
|
75
|
+
self.authenticate_with_captcha = false
|
76
|
+
ret
|
77
|
+
end
|
78
|
+
|
79
|
+
def save(check_validations=true)
|
80
|
+
self.authenticate_with_captcha = false
|
81
|
+
self.save_without_captcha(check_validations)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
ActiveRecord::Base.module_eval do
|
88
|
+
extend SimpleCaptcha::ModelHelpers::ClassMethods
|
89
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# Copyright (c) 2008 [Sur http://expressica.com]
|
2
|
+
|
3
|
+
require 'digest/sha1'
|
4
|
+
|
5
|
+
module SimpleCaptcha #:nodoc
|
6
|
+
module ConfigTasks #:nodoc
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def simple_captcha_image_path #:nodoc
|
11
|
+
"#{RAILS_ROOT}/vendor/plugins/simple_captcha/assets/images/simple_captcha/"
|
12
|
+
end
|
13
|
+
|
14
|
+
def simple_captcha_key #:nodoc
|
15
|
+
session[:simple_captcha] ||= Digest::SHA1.hexdigest(Time.now.to_s + session.session_id.to_s)
|
16
|
+
end
|
17
|
+
|
18
|
+
def simple_captcha_value(key = simple_captcha_key) #:nodoc
|
19
|
+
SimpleCaptchaData.get_data(key).value rescue nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def simple_captcha_passed!(key = simple_captcha_key) #:nodoc
|
23
|
+
SimpleCaptchaData.remove_data(key)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Copyright (c) 2008 [Sur http://expressica.com]
|
2
|
+
|
3
|
+
class SimpleCaptchaController < ActionController::Base
|
4
|
+
|
5
|
+
include SimpleCaptcha::ImageHelpers
|
6
|
+
|
7
|
+
def simple_captcha #:nodoc
|
8
|
+
send_data(
|
9
|
+
generate_simple_captcha_image(
|
10
|
+
:image_style => params[:image_style],
|
11
|
+
:distortion => params[:distortion],
|
12
|
+
:simple_captcha_key => params[:simple_captcha_key]),
|
13
|
+
:type => 'image/jpeg',
|
14
|
+
:disposition => 'inline',
|
15
|
+
:filename => 'simple_captcha.jpg')
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# Copyright (c) 2008 [Sur http://expressica.com]
|
2
|
+
|
3
|
+
class SimpleCaptchaData < ActiveRecord::Base
|
4
|
+
set_table_name "simple_captcha_data"
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def get_data(key)
|
8
|
+
data = find_by_key(key) || new(:key => key)
|
9
|
+
end
|
10
|
+
|
11
|
+
def remove_data(key)
|
12
|
+
clear_old_data
|
13
|
+
data = find_by_key(key)
|
14
|
+
data.destroy if data
|
15
|
+
end
|
16
|
+
|
17
|
+
def clear_old_data(time = 1.hour.ago)
|
18
|
+
return unless Time === time
|
19
|
+
destroy_all("updated_at < '#{time.to_s(:db)}'")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# Copyright (c) 2008 [Sur http://expressica.com]
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'RMagick'
|
5
|
+
|
6
|
+
module SimpleCaptcha #:nodoc
|
7
|
+
module ImageHelpers #:nodoc
|
8
|
+
|
9
|
+
include ConfigTasks
|
10
|
+
|
11
|
+
IMAGE_STYLES = [
|
12
|
+
'embosed_silver',
|
13
|
+
'simply_red',
|
14
|
+
'simply_green',
|
15
|
+
'simply_blue',
|
16
|
+
'distorted_black',
|
17
|
+
'all_black',
|
18
|
+
'charcoal_grey',
|
19
|
+
'almost_invisible'
|
20
|
+
]
|
21
|
+
|
22
|
+
DISTORTIONS = ['low', 'medium', 'high']
|
23
|
+
|
24
|
+
class << self
|
25
|
+
def image_style(key='simply_blue')
|
26
|
+
return IMAGE_STYLES[rand(IMAGE_STYLES.length)] if key=='random'
|
27
|
+
IMAGE_STYLES.include?(key) ? key : 'simply_blue'
|
28
|
+
end
|
29
|
+
|
30
|
+
def distortion(key='low')
|
31
|
+
key =
|
32
|
+
key == 'random' ?
|
33
|
+
DISTORTIONS[rand(DISTORTIONS.length)] :
|
34
|
+
DISTORTIONS.include?(key) ? key : 'low'
|
35
|
+
case key
|
36
|
+
when 'low' then return [0 + rand(2), 80 + rand(20)]
|
37
|
+
when 'medium' then return [2 + rand(2), 50 + rand(20)]
|
38
|
+
when 'high' then return [4 + rand(2), 30 + rand(20)]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def append_simple_captcha_code #:nodoc
|
46
|
+
color = @simple_captcha_image_options[:color]
|
47
|
+
text = Magick::Draw.new
|
48
|
+
text.annotate(@image, 0, 0, 0, 5, simple_captcha_value(@simple_captcha_image_options[:simple_captcha_key])) do
|
49
|
+
self.font_family = 'arial'
|
50
|
+
self.pointsize = 22
|
51
|
+
self.fill = color
|
52
|
+
self.gravity = Magick::CenterGravity
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def set_simple_captcha_image_style #:nodoc
|
57
|
+
amplitude, frequency = @simple_captcha_image_options[:distortion]
|
58
|
+
case @simple_captcha_image_options[:image_style]
|
59
|
+
when 'embosed_silver'
|
60
|
+
append_simple_captcha_code
|
61
|
+
@image = @image.wave(amplitude, frequency).shade(true, 20, 60)
|
62
|
+
when 'simply_red'
|
63
|
+
@simple_captcha_image_options[:color] = 'darkred'
|
64
|
+
append_simple_captcha_code
|
65
|
+
@image = @image.wave(amplitude, frequency)
|
66
|
+
when 'simply_green'
|
67
|
+
@simple_captcha_image_options[:color] = 'darkgreen'
|
68
|
+
append_simple_captcha_code
|
69
|
+
@image = @image.wave(amplitude, frequency)
|
70
|
+
when 'simply_blue'
|
71
|
+
append_simple_captcha_code
|
72
|
+
@image = @image.wave(amplitude, frequency)
|
73
|
+
when 'distorted_black'
|
74
|
+
append_simple_captcha_code
|
75
|
+
@image = @image.wave(amplitude, frequency).edge(10)
|
76
|
+
when 'all_black'
|
77
|
+
append_simple_captcha_code
|
78
|
+
@image = @image.wave(amplitude, frequency).edge(2)
|
79
|
+
when 'charcoal_grey'
|
80
|
+
append_simple_captcha_code
|
81
|
+
@image = @image.wave(amplitude, frequency).charcoal
|
82
|
+
when 'almost_invisible'
|
83
|
+
@simple_captcha_image_options[:color] = 'red'
|
84
|
+
append_simple_captcha_code
|
85
|
+
@image = @image.wave(amplitude, frequency).solarize
|
86
|
+
else
|
87
|
+
append_simple_captcha_code
|
88
|
+
@image = @image.wave(amplitude, frequency)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def generate_simple_captcha_image(options={}) #:nodoc
|
93
|
+
@image = Magick::Image.new(110, 30) do
|
94
|
+
self.background_color = 'white'
|
95
|
+
self.format = 'JPG'
|
96
|
+
end
|
97
|
+
@simple_captcha_image_options = {
|
98
|
+
:simple_captcha_key => options[:simple_captcha_key],
|
99
|
+
:color => 'darkblue',
|
100
|
+
:distortion => SimpleCaptcha::ImageHelpers.distortion(options[:distortion]),
|
101
|
+
:image_style => SimpleCaptcha::ImageHelpers.image_style(options[:image_style])
|
102
|
+
}
|
103
|
+
set_simple_captcha_image_style
|
104
|
+
@image.implode(0.2).to_blob
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
module SimpleCaptcha #:nodoc
|
4
|
+
module SetupTasks #:nodoc
|
5
|
+
|
6
|
+
def self.do_setup(version = :new)
|
7
|
+
@version = version
|
8
|
+
|
9
|
+
begin
|
10
|
+
puts "STEP 1"
|
11
|
+
generate_migration
|
12
|
+
write_migration_content
|
13
|
+
copy_view_file
|
14
|
+
puts "Followup Steps"
|
15
|
+
puts "STEP 2 -- run the task 'rake db:migrate'"
|
16
|
+
puts "STEP 3 -- edit the file config/routes.rb to add the route \"map.simple_captcha '/simple_captcha/:action', :controller => 'simple_captcha'\""
|
17
|
+
rescue StandardError => e
|
18
|
+
p e
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def self.generate_migration
|
25
|
+
puts "==============================================================================="
|
26
|
+
puts "ruby script/generate migration create_simple_captcha_data"
|
27
|
+
puts %x{ruby script/generate migration create_simple_captcha_data}
|
28
|
+
puts "================================DONE==========================================="
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.migration_source_file
|
32
|
+
@version == :old ?
|
33
|
+
File.join(File.dirname(__FILE__), "../assets", "migrate", "create_simple_captcha_data_less_than_2.0.rb") :
|
34
|
+
File.join(File.dirname(__FILE__), "../assets", "migrate", "create_simple_captcha_data.rb")
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.write_migration_content
|
38
|
+
copy_to_path = File.join(RAILS_ROOT, "db", "migrate")
|
39
|
+
migration_filename =
|
40
|
+
Dir.entries(copy_to_path).collect do |file|
|
41
|
+
number, *name = file.split("_")
|
42
|
+
file if name.join("_") == "create_simple_captcha_data.rb"
|
43
|
+
end.compact.first
|
44
|
+
migration_file = File.join(copy_to_path, migration_filename)
|
45
|
+
File.open(migration_file, "wb"){|f| f.write(File.read(migration_source_file))}
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.copy_view_file
|
49
|
+
puts "Copying SimpleCaptcha view file."
|
50
|
+
mkdir(File.join(RAILS_ROOT, "app/views/simple_captcha")) unless File.exist?(File.join(RAILS_ROOT, "app/views/simple_captcha"))
|
51
|
+
view_file = @version == :old ? '_simple_captcha.rhtml' : '_simple_captcha.erb'
|
52
|
+
FileUtils.cp_r(
|
53
|
+
File.join(File.dirname(__FILE__), "../assets/views/simple_captcha/_simple_captcha.erb"),
|
54
|
+
File.join(RAILS_ROOT, "app/views/simple_captcha/", view_file)
|
55
|
+
)
|
56
|
+
puts "================================DONE==========================================="
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# Copyright (c) 2008 [Sur http://expressica.com]
|
2
|
+
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
namespace :simple_captcha do
|
6
|
+
desc "Set up the plugin SimpleCaptcha for rails < 2.0"
|
7
|
+
task :setup_old => :environment do
|
8
|
+
SimpleCaptcha::SetupTasks.do_setup(:old)
|
9
|
+
end
|
10
|
+
|
11
|
+
desc "Set up the plugin SimpleCaptcha for rails >= 2.0"
|
12
|
+
task :setup => :environment do
|
13
|
+
SimpleCaptcha::SetupTasks.do_setup
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_captcha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- joloudov
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-14 00:00:00 +04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: SimpleCaptcha is the simplest and a robust captcha. Its implementation requires adding up a single line in views and in controllers/models. SimpleCaptcha is available to be used with Rails2.0 or above and also it provides the backward compatibility with previous versions of Rails.
|
22
|
+
email: joloudov@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README
|
29
|
+
files:
|
30
|
+
- MIT-LICENSE
|
31
|
+
- README
|
32
|
+
- Rakefile
|
33
|
+
- VERSION
|
34
|
+
- assets/migrate/create_simple_captcha_data.rb
|
35
|
+
- assets/migrate/create_simple_captcha_data_less_than_2.0.rb
|
36
|
+
- assets/views/simple_captcha/_simple_captcha.erb
|
37
|
+
- init.rb
|
38
|
+
- install.rb
|
39
|
+
- lib/simple_captcha_action_controller.rb
|
40
|
+
- lib/simple_captcha_action_view.rb
|
41
|
+
- lib/simple_captcha_active_record.rb
|
42
|
+
- lib/simple_captcha_config.rb
|
43
|
+
- lib/simple_captcha_controller.rb
|
44
|
+
- lib/simple_captcha_data.rb
|
45
|
+
- lib/simple_captcha_image.rb
|
46
|
+
- lib/simple_captcha_setup.rb
|
47
|
+
- tasks/simple_captcha_tasks.rake
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/joloudov/simple_captcha
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.3.7
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: SimpleCaptcha is the simplest and a robust captcha.
|
78
|
+
test_files:
|
79
|
+
- test/simple_captcha_test.rb
|