glebtv-simple_captcha 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,7 +1,6 @@
1
1
  =SimpleCaptcha
2
- This is a fork of wolcanus's fork of SimpleCaptcha to support Mongoid 3 and new formtastic.
3
- This is a fork of galetahub SimpleCaptcha to support Mongoid.
4
- Its implementation requires
2
+
3
+ SimpleCaptcha is the simplest and a robust captcha plugin. Its implementation requires
5
4
  adding up a single line in views and in controllers/models.
6
5
  SimpleCaptcha is available to be used with Rails 3 or above and also it provides the
7
6
  backward compatibility with previous versions of Rails.
@@ -25,11 +24,11 @@ backward compatibility with previous versions of Rails.
25
24
 
26
25
  ==Installation
27
26
 
28
- gem "glebtv-simple_captcha", :require => "simple_captcha"
27
+ gem "galetahub-simple_captcha", :require => "simple_captcha"
29
28
 
30
29
  or
31
30
 
32
- gem 'glebtv-simple_captcha', :require => 'simple_captcha', :git => 'git://github.com/glebtv/simple-captcha.git'
31
+ gem 'galetahub-simple_captcha', :require => 'simple_captcha', :git => 'git://github.com/galetahub/simple-captcha.git'
33
32
 
34
33
  ==Setup
35
34
 
@@ -38,6 +37,8 @@ on the version of rails your application is using.
38
37
 
39
38
  rails generate simple_captcha
40
39
 
40
+ rake db:migrate
41
+
41
42
  ==Usage
42
43
 
43
44
  ===Controller Based
@@ -165,6 +166,13 @@ You can provide the path where image_magick is installed as well:
165
166
  sc.image_magick_path = '/usr/bin' # you can check this from console by running: which convert
166
167
  end
167
168
 
169
+ You can provide the path where should be stored tmp files.
170
+ It's usefull when you dont have acces to /tmp (default directory)
171
+
172
+ SimpleCaptcha.setup do |sc|
173
+ sc.tmp_path = '/tmp' # or somewhere in project eg. Rails.root.join('tmp/simple_captcha').to_s, make shure directory exists
174
+ end
175
+
168
176
 
169
177
  ===How to change the CSS for SimpleCaptcha DOM elements?
170
178
  You can change the CSS of the SimpleCaptcha DOM elements as per your need in this file.
@@ -204,7 +212,7 @@ You can change the CSS of the SimpleCaptcha DOM elements as per your need in thi
204
212
  default: "Secret Code did not match with the Image"
205
213
  user: "The secret Image and code were different"
206
214
 
207
- ==The Original Author
215
+ ==Who's who?
208
216
 
209
217
  Enjoy the simplest captcha implementation.
210
218
 
@@ -218,4 +226,4 @@ Plugin Homepage: http://expressica.com/simple_captcha
218
226
 
219
227
  Plugin update for rails 3: http://github.com/galetahub
220
228
 
221
- Any feedback/comment/issue/donation is welcome!
229
+ Any feedback/comment/issue/donation is welcome!
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'rake'
2
2
  require 'rake/testtask'
3
- require 'rdoc/task'
3
+ require 'rake/rdoctask'
4
4
 
5
5
  desc 'Default: run unit tests.'
6
6
  task :default => :test
data/lib/generators/USAGE CHANGED
@@ -1,4 +1,4 @@
1
- SimpleCaptcha
1
+ SimplaCaptcha
2
2
  =============
3
3
  # Generate migration and copy view partial
4
4
  rails generate simple_captcha
@@ -6,9 +6,16 @@ class SimpleCaptchaGenerator < Rails::Generators::Base
6
6
  def self.source_root
7
7
  @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates/'))
8
8
  end
9
+
10
+ def self.next_migration_number(dirname)
11
+ Time.now.strftime("%Y%m%d%H%M%S")
12
+ end
9
13
 
10
14
  def create_partial
11
15
  template "partial.erb", File.join('app/views', 'simple_captcha', "_simple_captcha.erb")
12
16
  end
13
-
17
+
18
+ def create_migration
19
+ migration_template "migration.rb", File.join('db/migrate', "create_simple_captcha_data.rb")
20
+ end
14
21
  end
@@ -0,0 +1,15 @@
1
+ class CreateSimpleCaptchaData < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :simple_captcha_data do |t|
4
+ t.string :key, :limit => 40
5
+ t.string :value, :limit => 6
6
+ t.timestamps
7
+ end
8
+
9
+ add_index :simple_captcha_data, :key, :name => "idx_key"
10
+ end
11
+
12
+ def self.down
13
+ drop_table :simple_captcha_data
14
+ end
15
+ end
@@ -10,8 +10,12 @@ module SimpleCaptcha
10
10
 
11
11
  autoload :FormBuilder, 'simple_captcha/form_builder'
12
12
  autoload :CustomFormBuilder, 'simple_captcha/formtastic'
13
-
14
- autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data'
13
+
14
+ if defined?(Mongoid)
15
+ autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data_mongoid'
16
+ else
17
+ autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data_ar'
18
+ end
15
19
  autoload :Middleware, 'simple_captcha/middleware'
16
20
 
17
21
  mattr_accessor :image_size
@@ -20,17 +24,8 @@ module SimpleCaptcha
20
24
  mattr_accessor :length
21
25
  @@length = 5
22
26
 
23
- # 'embosed_silver',
24
- # 'simply_red',
25
- # 'simply_green',
26
- # 'simply_blue',
27
- # 'distorted_black',
28
- # 'all_black',
29
- # 'charcoal_grey',
30
- # 'almost_invisible'
31
- # 'random'
32
- mattr_accessor :image_style
33
- @@image_style = 'simply_blue'
27
+ mattr_accessor :image_color
28
+ @@image_color = '#0089d1'
34
29
 
35
30
  # 'low', 'medium', 'high', 'random'
36
31
  mattr_accessor :distortion
@@ -40,9 +35,9 @@ module SimpleCaptcha
40
35
  mattr_accessor :image_magick_path
41
36
  @@image_magick_path = ''
42
37
 
43
- def self.add_image_style(name, params = [])
44
- SimpleCaptcha::ImageHelpers.image_styles.update(name.to_s => params)
45
- end
38
+ # tmp directory
39
+ mattr_accessor :tmp_path
40
+ @@tmp_path = nil
46
41
 
47
42
  def self.setup
48
43
  yield self
@@ -54,12 +54,14 @@ module SimpleCaptcha #:nodoc
54
54
  end
55
55
 
56
56
  def is_captcha_valid?
57
+ return true if Rails.env.test?
58
+
57
59
  if captcha && captcha.upcase.delete(" ") == SimpleCaptcha::Utils::simple_captcha_value(captcha_key)
58
60
  SimpleCaptcha::Utils::simple_captcha_passed!(captcha_key)
59
61
  return true
60
62
  else
61
63
  message = simple_captcha_options[:message] || I18n.t(self.class.model_name.downcase, :scope => [:simple_captcha, :message], :default => :default)
62
- simple_captcha_options[:add_to_base] ? errors.add_to_base(message) : errors.add(:captcha, message)
64
+ simple_captcha_options[:add_to_base] ? errors.add(:base, message) : errors.add(:captcha, message)
63
65
  return false
64
66
  end
65
67
  end
@@ -17,7 +17,7 @@ module SimpleCaptcha #:nodoc
17
17
  return true if Rails.env.test?
18
18
 
19
19
  if params[:captcha]
20
- data = SimpleCaptcha::Utils::simple_captcha_value(session[:captcha])
20
+ data = SimpleCaptcha::Utils::simple_captcha_value(params[:captcha_key] || session[:captcha])
21
21
  result = data == params[:captcha].delete(" ").upcase
22
22
  SimpleCaptcha::Utils::simple_captcha_passed!(session[:captcha]) if result
23
23
  return result
@@ -3,7 +3,7 @@ require "formtastic"
3
3
  class SimpleCaptchaInput
4
4
  include ::Formtastic::Inputs::Base
5
5
  include ::Formtastic::Inputs::Base::Stringish
6
-
6
+
7
7
  def to_html
8
8
  input_wrapping do
9
9
  input_html_options.update :object => method
@@ -2,34 +2,12 @@ require 'tempfile'
2
2
  module SimpleCaptcha #:nodoc
3
3
  module ImageHelpers #:nodoc
4
4
 
5
- mattr_accessor :image_styles
6
- @@image_styles = {
7
- 'embosed_silver' => ['-fill darkblue', '-shade 20x60', '-background white'],
8
- 'simply_red' => ['-fill darkred', '-background white'],
9
- 'simply_green' => ['-fill darkgreen', '-background white'],
10
- 'simply_blue' => ['-fill darkblue', '-background white'],
11
- 'distorted_black' => ['-fill darkblue', '-edge 10', '-background white'],
12
- 'all_black' => ['-fill darkblue', '-edge 2', '-background white'],
13
- 'charcoal_grey' => ['-fill darkblue', '-charcoal 5', '-background white'],
14
- 'almost_invisible' => ['-fill red', '-solarize 50', '-background white']
15
- }
16
-
17
5
  DISTORTIONS = ['low', 'medium', 'high']
18
6
 
19
7
  class << self
20
8
 
21
- def image_params(key = 'simply_blue')
22
- image_keys = @@image_styles.keys
23
-
24
- style = begin
25
- if key == 'random'
26
- image_keys[rand(image_keys.length)]
27
- else
28
- image_keys.include?(key) ? key : 'simply_blue'
29
- end
30
- end
31
-
32
- @@image_styles[style]
9
+ def image_params(color = '#0089d1')
10
+ ["-alpha set -background none -fill \"#{color}\""]
33
11
  end
34
12
 
35
13
  def distortion(key='low')
@@ -59,20 +37,28 @@ module SimpleCaptcha #:nodoc
59
37
 
60
38
  def generate_simple_captcha_image(simple_captcha_key) #:nodoc
61
39
  amplitude, frequency = ImageHelpers.distortion(SimpleCaptcha.distortion)
62
- text = Utils::simple_captcha_value(simple_captcha_key)
40
+ text = Utils::simple_captcha_new_value(simple_captcha_key)
41
+ params = ImageHelpers.image_params(SimpleCaptcha.image_color).dup
42
+ params << "-size #{SimpleCaptcha.image_size} xc:transparent"
43
+ params << "-gravity \"Center\""
44
+ if params.join(' ').index('-pointsize').nil?
45
+ params << "-pointsize 35"
46
+ end
47
+ dst = Tempfile.new(RUBY_VERSION < '1.9' ? 'simple_captcha.png' : ['simple_captcha', '.png'], SimpleCaptcha.tmp_path)
48
+ dst.binmode
49
+ text.split(//).each_with_index do |letter, index|
50
+ i = -60 + (index*25) + rand(-6..6)
51
+ params << "-draw \"translate #{i},#{rand(-6..6)} skewX #{rand(-15..15)} gravity center text 0,0 '#{letter}'\" "
52
+ end
63
53
 
64
- params = ImageHelpers.image_params(SimpleCaptcha.image_style).dup
65
- params << "-size #{SimpleCaptcha.image_size}"
66
54
  params << "-wave #{amplitude}x#{frequency}"
67
- params << "-gravity 'Center'"
68
- params << "-pointsize 22"
69
- params << "-implode 0.2"
70
55
 
71
- dst = RUBY_VERSION < '1.9' ? Tempfile.new('simple_captcha.jpg') : Tempfile.new(['simple_captcha', '.jpg'])
72
- dst.binmode
73
-
74
- params << "label:#{text} '#{File.expand_path(dst.path)}'"
56
+ (1..10).each do |i|
57
+ params << "-draw \"polyline #{rand(160)},#{rand(61)} #{rand(160)},#{rand(62)}\""
58
+ end
75
59
 
60
+ params << "\"#{File.expand_path(dst.path)}\""
61
+ # puts "convert " + params.join(' ')
76
62
  SimpleCaptcha::Utils::run("convert", params.join(' '))
77
63
 
78
64
  dst.close
@@ -26,14 +26,8 @@ module SimpleCaptcha
26
26
  request = Rack::Request.new(env)
27
27
  code = request.params["code"]
28
28
  body = []
29
-
30
29
  if !code.blank? && Utils::simple_captcha_value(code)
31
- #status, headers, body = @app.call(env)
32
- #status = 200
33
- #body = generate_simple_captcha_image(code)
34
- #headers['Content-Type'] = 'image/jpeg'
35
-
36
- return send_file(generate_simple_captcha_image(code), :type => 'image/jpeg', :disposition => 'inline', :filename => 'simple_captcha.jpg')
30
+ return send_file(generate_simple_captcha_image(code), :type => 'image/png', :disposition => 'inline', :filename => 'simple_captcha.png')
37
31
  end
38
32
 
39
33
  [status, headers, body]
@@ -50,9 +44,9 @@ module SimpleCaptcha
50
44
 
51
45
  status = options[:status] || 200
52
46
  headers = {"Content-Disposition" => "#{options[:disposition]}; filename='#{options[:filename]}'", "Content-Type" => options[:type], 'Content-Transfer-Encoding' => 'binary', 'Cache-Control' => 'private'}
53
- response_body = File.open(path, "rb")
54
-
55
- [status, headers, response_body]
47
+ response_body = File.open(path, 'rb').read
48
+ File.unlink(path)
49
+ [status, headers, [response_body]]
56
50
  end
57
51
  end
58
52
  end
@@ -0,0 +1,33 @@
1
+ module SimpleCaptcha
2
+ class SimpleCaptchaData < ::ActiveRecord::Base
3
+ def self.rails3?
4
+ ::ActiveRecord::VERSION::MAJOR == 3
5
+ end
6
+
7
+ if rails3?
8
+ # Fixes deprecation warning in Rails 3.2:
9
+ # DEPRECATION WARNING: Calling set_table_name is deprecated. Please use `self.table_name = 'the_name'` instead.
10
+ self.table_name = "simple_captcha_data"
11
+ else
12
+ set_table_name "simple_captcha_data"
13
+ end
14
+
15
+ attr_accessible :key, :value
16
+
17
+ class << self
18
+ def get_data(key)
19
+ data = find_by_key(key) || new(:key => key)
20
+ end
21
+
22
+ def remove_data(key)
23
+ delete_all(["#{connection.quote_column_name(:key)} = ?", key])
24
+ clear_old_data(1.hour.ago)
25
+ end
26
+
27
+ def clear_old_data(time = 1.hour.ago)
28
+ return unless Time === time
29
+ delete_all(["#{connection.quote_column_name(:updated_at)} < ?", time])
30
+ end
31
+ end
32
+ end
33
+ end
@@ -23,6 +23,22 @@ module SimpleCaptcha #:nodoc
23
23
  def self.simple_captcha_value(key) #:nodoc
24
24
  SimpleCaptchaData.get_data(key).value rescue nil
25
25
  end
26
+
27
+ def self.simple_captcha_new_value(key) #:nodoc
28
+ begin
29
+ # very unsafe to display same code over and over
30
+ value = ''
31
+ # SimpleCaptcha.length.times{value << (48 + rand(10)).chr}
32
+ SimpleCaptcha.length.times{value << (65 + rand(26)).chr}
33
+ d = SimpleCaptchaData.get_data(key)
34
+ d.value = value
35
+ d.save!
36
+ value
37
+ rescue
38
+ nil
39
+ end
40
+ end
41
+
26
42
 
27
43
  def self.simple_captcha_passed!(key) #:nodoc
28
44
  SimpleCaptchaData.remove_data(key)
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module SimpleCaptcha
3
- VERSION = "0.1.6".freeze
3
+ VERSION = "0.2.0".freeze
4
4
  end
@@ -57,12 +57,12 @@ module SimpleCaptcha #:nodoc
57
57
 
58
58
  private
59
59
 
60
- def simple_captcha_image(simple_captcha_key, options = {})
60
+ def simple_captcha_image(ckey, options = {})
61
61
  defaults = {}
62
62
  defaults[:time] = options[:time] || Time.now.to_i
63
63
 
64
64
  query = defaults.collect{ |key, value| "#{key}=#{value}" }.join('&')
65
- url = "/simple_captcha?code=#{simple_captcha_key}&#{query}"
65
+ url = "/simple_captcha?code=#{ckey}&#{query}"
66
66
 
67
67
  "<img src='#{url}' alt='captcha' />".html_safe
68
68
  end
@@ -72,7 +72,8 @@ module SimpleCaptcha #:nodoc
72
72
  text_field(options[:object], :captcha, :value => '', :autocomplete => 'off') +
73
73
  hidden_field(options[:object], :captcha_key, {:value => options[:field_value]})
74
74
  else
75
- text_field_tag(:captcha, nil, :autocomplete => 'off')
75
+ text_field_tag(:captcha, nil, :autocomplete => 'off') +
76
+ hidden_field_tag(:captcha_key, options[:field_value])
76
77
  end
77
78
  end
78
79
 
metadata CHANGED
@@ -1,40 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glebtv-simple_captcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Weverton do Couto Timoteo
8
+ - Pavlo Galeta
9
+ - Igor Galeta
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2012-11-15 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: mongoid
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: 3.0.0
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: 3.0.0
30
- description: .
31
- email: weverton@wolcan.us
13
+ date: 2012-12-01 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: SimpleCaptcha is available to be used with Rails 3 or above and also
16
+ it provides the backward compatibility with previous versions of Rails.
17
+ email: galeta.igor@gmail.com
32
18
  executables: []
33
19
  extensions: []
34
20
  extra_rdoc_files:
35
21
  - README.rdoc
36
22
  files:
37
23
  - lib/generators/templates/partial.erb
24
+ - lib/generators/templates/migration.rb
38
25
  - lib/generators/simple_captcha_generator.rb
39
26
  - lib/generators/USAGE
40
27
  - lib/simple_captcha/active_record.rb
@@ -43,11 +30,12 @@ files:
43
30
  - lib/simple_captcha/utils.rb
44
31
  - lib/simple_captcha/controller.rb
45
32
  - lib/simple_captcha/formtastic.rb
33
+ - lib/simple_captcha/simple_captcha_data_ar.rb
46
34
  - lib/simple_captcha/engine.rb
47
35
  - lib/simple_captcha/form_builder.rb
48
36
  - lib/simple_captcha/middleware.rb
37
+ - lib/simple_captcha/simple_captcha_data_mongoid.rb
49
38
  - lib/simple_captcha/image.rb
50
- - lib/simple_captcha/simple_captcha_data.rb
51
39
  - lib/simple_captcha.rb
52
40
  - Rakefile
53
41
  - README.rdoc
@@ -71,11 +59,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
59
  - !ruby/object:Gem::Version
72
60
  version: '0'
73
61
  requirements: []
74
- rubyforge_project:
62
+ rubyforge_project: glebtv-simple_captcha
75
63
  rubygems_version: 1.8.22
76
64
  signing_key:
77
65
  specification_version: 3
78
- summary: A fork of a wolcanus fork of galetahub SimpleCaptcha for mongoid support.
66
+ summary: SimpleCaptcha is the simplest and a robust captcha plugin.
79
67
  test_files:
80
68
  - test/simple_captcha_test.rb
81
69
  has_rdoc: