rucaptcha 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +18 -0
- data/README.md +62 -0
- data/app/controllers/ru_captcha/captcha_controller.rb +11 -0
- data/config/locales/rucaptcha.en.yml +3 -0
- data/config/locales/rucaptcha.zh-CN.yml +3 -0
- data/config/locales/rucaptcha.zh-TW.yml +3 -0
- data/config/routes.rb +3 -0
- data/lib/rucaptcha.rb +29 -0
- data/lib/rucaptcha/captcha.rb +45 -0
- data/lib/rucaptcha/configuration.rb +5 -0
- data/lib/rucaptcha/controller_helpers.rb +29 -0
- data/lib/rucaptcha/engine.rb +5 -0
- data/lib/rucaptcha/version.rb +4 -0
- data/lib/rucaptcha/view_helpers.rb +13 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ccd765f9ebc13571f084bf1837a5ac6f106343df
|
4
|
+
data.tar.gz: 1322b0e7d63a098c2a62bf14f75ee8eadad623fe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 78516faa943ed475cd405ae69987a6ae113e9d4e0bcc8ca3271991c89a10501324a59c28fe89ab2eb2e04ea4b6908cb06119f87bc4ade5e264f25962777cab44
|
7
|
+
data.tar.gz: 4e6d4e20380512ba8c195070cb0757dc37ff24788e7d1a46e42aadc9e91bc9ae9a86728d70d17a7563f2108e59608ad87c6b1135a0fefdfbac2728e8dc083d03
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
0.1.2
|
2
|
+
-----
|
3
|
+
|
4
|
+
- No case sensitive;
|
5
|
+
- Export config.implode;
|
6
|
+
- Improve image color and style;
|
7
|
+
- Don't generate chars in 'l,o,0,1'.
|
8
|
+
- Render lower case chars on image.
|
9
|
+
|
10
|
+
0.1.1
|
11
|
+
-----
|
12
|
+
|
13
|
+
- Include default validation I18n messages (en, zh-CN, zh-TW).
|
14
|
+
|
15
|
+
0.1.0
|
16
|
+
-----
|
17
|
+
|
18
|
+
- First release.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# RuCaptcha
|
2
|
+
|
3
|
+
|
4
|
+
This is a Captcha gem for Rails Application. It run ImageMagick command to draw Captcha image.
|
5
|
+
|
6
|
+
So it's NO performance issue, and memory leak issue.
|
7
|
+
|
8
|
+
Idea by: https://ruby-china.org/topics/20558#reply4
|
9
|
+
|
10
|
+
### Requirements
|
11
|
+
|
12
|
+
- ImageMagick
|
13
|
+
|
14
|
+
### Usage
|
15
|
+
|
16
|
+
Put rucaptcha in your `Gemfile`:
|
17
|
+
|
18
|
+
```
|
19
|
+
gem 'rucaptcha'
|
20
|
+
```
|
21
|
+
|
22
|
+
Create `config/initializes/rucaptcha.rb`
|
23
|
+
|
24
|
+
```rb
|
25
|
+
RuCaptcha.configure do
|
26
|
+
# Number of chars, default: 4
|
27
|
+
self.len = 4
|
28
|
+
# Image width, default: 180
|
29
|
+
self.width = 180
|
30
|
+
# Image height, default: 48
|
31
|
+
self.height = 48
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Controller `app/controller/account_controller.rb`
|
36
|
+
|
37
|
+
```rb
|
38
|
+
class AccountController < ApplicationController
|
39
|
+
def create
|
40
|
+
@user = User.new(params[:user])
|
41
|
+
if verify_rucaptcha?(@user) && @user.save
|
42
|
+
redirect_to root_path, notice: 'Sign up successed.'
|
43
|
+
else
|
44
|
+
render 'account/new'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
View `app/views/account/new.html.erb`
|
51
|
+
|
52
|
+
```erb
|
53
|
+
<form>
|
54
|
+
...
|
55
|
+
<div class="form-group">
|
56
|
+
<%= rucaptcha_input_tag(class: 'form-control', placeholder: 'Input Captcha') %>
|
57
|
+
<%= rucaptcha_image_tag(alt: 'Captcha') %>
|
58
|
+
</div>
|
59
|
+
...
|
60
|
+
</form>
|
61
|
+
```
|
62
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module RuCaptcha
|
2
|
+
class CaptchaController < ActionController::Base
|
3
|
+
def index
|
4
|
+
headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
|
5
|
+
headers['Pragma'] = 'no-cache'
|
6
|
+
|
7
|
+
send_data generate_rucaptcha, disposition: 'inline', type: 'image/png'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
data/config/routes.rb
ADDED
data/lib/rucaptcha.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
require_relative 'rucaptcha/version'
|
3
|
+
require_relative 'rucaptcha/configuration'
|
4
|
+
require_relative 'rucaptcha/controller_helpers'
|
5
|
+
require_relative 'rucaptcha/view_helpers'
|
6
|
+
require_relative 'rucaptcha/captcha'
|
7
|
+
require_relative 'rucaptcha/engine'
|
8
|
+
|
9
|
+
module RuCaptcha
|
10
|
+
class << self
|
11
|
+
def config
|
12
|
+
return @config if defined?(@config)
|
13
|
+
@config = Configuration.new
|
14
|
+
@config.len = 4
|
15
|
+
@config.width = 150
|
16
|
+
@config.height = 48
|
17
|
+
@config.implode = 0.4
|
18
|
+
@config
|
19
|
+
end
|
20
|
+
|
21
|
+
def configure(&block)
|
22
|
+
config.instance_exec(&block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
ActionController::Base.send :include, RuCaptcha::ControllerHelpers
|
29
|
+
ActionView::Base.send :include, RuCaptcha::ViewHelpers
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'posix-spawn'
|
2
|
+
|
3
|
+
module RuCaptcha
|
4
|
+
class Captcha
|
5
|
+
def self.rand_color
|
6
|
+
r = rand(129).to_s(8).to_i
|
7
|
+
rgb = [rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)]
|
8
|
+
|
9
|
+
"rgba(#{rgb.join(',')},1)"
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.create(code)
|
13
|
+
size = "#{RuCaptcha.config.width}x#{RuCaptcha.config.height}"
|
14
|
+
font_size = (RuCaptcha.config.height * 0.8).to_i
|
15
|
+
half_width = RuCaptcha.config.width / 2
|
16
|
+
half_height = RuCaptcha.config.height / 2
|
17
|
+
line_color = rand_color
|
18
|
+
|
19
|
+
chars = code.split('')
|
20
|
+
text_opts = []
|
21
|
+
text_top = (RuCaptcha.config.height - font_size) / 2
|
22
|
+
text_padding = 5
|
23
|
+
text_width = (RuCaptcha.config.width / chars.size) - text_padding * 2
|
24
|
+
text_left = 5
|
25
|
+
chars.each_with_index do |char, i|
|
26
|
+
text_opts << %(-fill '#{rand_color}' -draw 'text #{(text_left + text_width) * i + text_left},#{text_top} "#{char}"')
|
27
|
+
end
|
28
|
+
|
29
|
+
command = <<-CODE
|
30
|
+
convert -size #{size} #{text_opts.join(' ')} \
|
31
|
+
-draw 'stroke #{line_color} line #{rand(10)},#{rand(20)} #{half_width + rand(half_width)},#{rand(half_height)}' \
|
32
|
+
-draw 'stroke #{line_color} line #{rand(10)},#{rand(25)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \
|
33
|
+
-draw 'stroke #{line_color} line #{rand(10)},#{rand(30)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \
|
34
|
+
-wave #{rand(2) + 2}x#{rand(2) + 1} \
|
35
|
+
-gravity NorthWest -sketch 1x10+#{rand(1)} -pointsize #{font_size} -weight 700 \
|
36
|
+
-implode #{RuCaptcha.config.implode} label:- png:-
|
37
|
+
CODE
|
38
|
+
command.strip!
|
39
|
+
# puts command
|
40
|
+
pid, stdin, stdout, stderr = POSIX::Spawn.popen4(command)
|
41
|
+
Process.waitpid(pid)
|
42
|
+
stdout.read
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module RuCaptcha
|
2
|
+
module ControllerHelpers
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
helper_method :verify_rucaptcha?
|
7
|
+
end
|
8
|
+
|
9
|
+
def generate_rucaptcha
|
10
|
+
session[:_rucaptcha] = random_rucaptcha_chars
|
11
|
+
return RuCaptcha::Captcha.create(session[:_rucaptcha])
|
12
|
+
end
|
13
|
+
|
14
|
+
def random_rucaptcha_chars
|
15
|
+
chars = SecureRandom.hex(RuCaptcha.config.len / 2).downcase
|
16
|
+
chars.gsub!(/[0ol1]/i, (rand(9) + 1).to_s)
|
17
|
+
chars
|
18
|
+
end
|
19
|
+
|
20
|
+
def verify_rucaptcha?(resource = nil)
|
21
|
+
params[:_rucaptcha] ||= ''
|
22
|
+
right = params[:_rucaptcha].downcase.strip == session[:_rucaptcha]
|
23
|
+
if resource && resource.respond_to?(:errors)
|
24
|
+
resource.errors.add(:base, t('rucaptcha.invalid')) unless right
|
25
|
+
end
|
26
|
+
right
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module RuCaptcha
|
2
|
+
module ViewHelpers
|
3
|
+
def rucaptcha_input_tag(opts = {})
|
4
|
+
opts[:name] = '_rucaptcha'
|
5
|
+
tag(:input, opts)
|
6
|
+
end
|
7
|
+
|
8
|
+
def rucaptcha_image_tag(opts = {})
|
9
|
+
opts[:class] = opts[:class] || 'rucaptcha-image'
|
10
|
+
image_tag(ru_captcha.root_path, opts)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rucaptcha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason Lee
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: posix-spawn
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.3.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.3.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.3.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.3.0
|
55
|
+
description:
|
56
|
+
email: huacnlee@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- CHANGELOG.md
|
62
|
+
- README.md
|
63
|
+
- app/controllers/ru_captcha/captcha_controller.rb
|
64
|
+
- config/locales/rucaptcha.en.yml
|
65
|
+
- config/locales/rucaptcha.zh-CN.yml
|
66
|
+
- config/locales/rucaptcha.zh-TW.yml
|
67
|
+
- config/routes.rb
|
68
|
+
- lib/rucaptcha.rb
|
69
|
+
- lib/rucaptcha/captcha.rb
|
70
|
+
- lib/rucaptcha/configuration.rb
|
71
|
+
- lib/rucaptcha/controller_helpers.rb
|
72
|
+
- lib/rucaptcha/engine.rb
|
73
|
+
- lib/rucaptcha/version.rb
|
74
|
+
- lib/rucaptcha/view_helpers.rb
|
75
|
+
homepage: https://github.com/huacnlee/rucaptcha
|
76
|
+
licenses: []
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.4.8
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: This is a Captcha gem for Rails Application. It run ImageMagick command to
|
98
|
+
draw Captcha image.
|
99
|
+
test_files: []
|