yl_simple_captcha 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 49d4020265cd517eb6871543f8442c70ab14048f
4
+ data.tar.gz: ded8246373674be7d0a11b97886ed012f5696e12
5
+ SHA512:
6
+ metadata.gz: fdd8fc68c5925d709f2fb898d00e66ea79cef8da1a197bbe3b5b74e380ac655fb763d8f089769f68e1a408d8bc2f16355cdd381af54f3e42c1190d3eb646748f
7
+ data.tar.gz: edeec1675ece371acf2f6c231e22361bb295928a2edadf7bd30fe50830ef116b0b764aea6e3f29580890fb41062753d5f01285f046feeb39e5d121182bda4f59
data/README.md ADDED
@@ -0,0 +1,335 @@
1
+ #SimpleCaptcha2
2
+
3
+ [![Build Status](https://travis-ci.org/pludoni/simple-captcha.png?branch=master)](https://travis-ci.org/pludoni/simple-captcha)
4
+ [![Gem Version](https://badge.fury.io/rb/simple_captcha2.svg)](https://badge.fury.io/rb/simple_captcha2)
5
+
6
+ SimpleCaptcha(2) is the simplest and a robust captcha plugin. Its implementation requires adding up a single line in views and in controllers/models.
7
+ SimpleCaptcha2 is available to be used with Rails 3 + 4.
8
+ This is a fork of the popular Rubygem ``simple_captcha`` which got abandoned.
9
+
10
+ ##Features
11
+
12
+ * Zero FileSystem usage (secret code moved to db-store and image storage removed).
13
+ * Provides various image styles.
14
+ * Provides three level of complexity of images.
15
+ * Works absolutely fine in distributed environment(session and db based implementation works fine in distributed environment).
16
+ * Implementation is as easy as just writing a single line in your view. ```<%= show_simple_captcha %>``` within the 'form' tags.
17
+ * Flexible DOM and CSS handling(There is a separate view partial for rendering SimpleCaptcha DOM elements).
18
+ * Automated removal of 1 hour old unmatched simple_captcha data.
19
+
20
+ ##Requirements
21
+
22
+ * Ruby >= 1.9.3
23
+ * Rails >= 3.2
24
+ * ImageMagick should be installed on your machine to use this plugin.
25
+ visit http://www.imagemagick.org/script/index.php for more details.
26
+
27
+ You might need to install Ghostscript on a Mac-System:
28
+
29
+ ```
30
+ brew install ghostscript
31
+ ```
32
+
33
+ ##Installation
34
+
35
+ Put this into your Gemfile
36
+
37
+ ```ruby
38
+ gem 'yl_simple_captcha', require: 'simple_captcha', :git => 'git://github.com/jiawenliang/simple-captcha.git'
39
+ ```
40
+
41
+ and run ``bundle install``.
42
+
43
+ ## Setup
44
+
45
+ After installation, follow these simple steps to setup the plugin. The setup will depend on the version of rails your application is using.
46
+
47
+ ```bash
48
+ rails generate simple_captcha [template_format] # Available options erb, haml. Default: erb
49
+ rake db:migrate # Mongoid: skip this step and remove the migration
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ There are two usage scenarios:
55
+
56
+ ### Controller Based
57
+
58
+ Add the following line in the file "app/controllers/application.rb"
59
+
60
+ ```ruby
61
+ ApplicationController < ActionController::Base
62
+ include SimpleCaptcha::ControllerHelpers
63
+ end
64
+ ```
65
+
66
+ In the view file within the form tags add this code
67
+
68
+ ```erb
69
+ <%= show_simple_captcha %>
70
+ ```
71
+
72
+ and in the controller's action authenticate it as
73
+
74
+ ```ruby
75
+ if simple_captcha_valid?
76
+ do this
77
+ else
78
+ do that
79
+ end
80
+ ```
81
+
82
+ ### Model Based
83
+
84
+ This is suggested, if you want to integrate the error message into the normal form validation flow.
85
+
86
+ In the view file within the form tags write this code
87
+
88
+ ```erb
89
+ <%= show_simple_captcha(:object=>"user") %>
90
+ ```
91
+
92
+ and in the model class add this code
93
+
94
+ ```ruby
95
+ class User < ActiveRecord::Base
96
+ apply_simple_captcha
97
+ end
98
+ ```
99
+
100
+ Mongoid:
101
+
102
+ ```ruby
103
+ class User
104
+ include SimpleCaptcha::ModelHelpers
105
+ apply_simple_captcha
106
+ end
107
+ ```
108
+
109
+ #### Strong parameters (Rails 4.x)
110
+
111
+ Must add them:
112
+
113
+ ```ruby
114
+ :captcha, :captcha_key
115
+ ```
116
+
117
+ ####Form-Builder helper
118
+
119
+ ```erb
120
+ <%= form_for @user do |form| -%>
121
+ ...
122
+ <%= form.simple_captcha :label => "Enter numbers.." %>
123
+ ...
124
+ <% end -%>
125
+ ```
126
+
127
+ ####Validating with captcha
128
+
129
+ NOTE: @user.valid? will still work as it should, it will not validate the captcha code.
130
+
131
+ ```ruby
132
+ @user.valid_with_captcha?
133
+ ```
134
+
135
+ ####Saving with captcha
136
+
137
+ NOTE: @user.save will still work as it should, it will not validate the captcha code.
138
+
139
+ ```ruby
140
+ @user.save_with_captcha
141
+ ```
142
+
143
+ ###Formtastic integration
144
+
145
+ SimpleCaptcha detects if you are using Formtastic:
146
+
147
+ ```erb
148
+ <%= form.input :captcha, :as => :simple_captcha %>
149
+ ```
150
+
151
+ ### Tests
152
+
153
+ You can make the Captcha always pass with a initializer file: config/initializers/simple_captcha.rb
154
+
155
+ ```ruby
156
+ SimpleCaptcha.always_pass = Rails.env.test?
157
+ ```
158
+
159
+ You can also ask for the value, e.g. Acceptance Tests/Features:
160
+
161
+ ```ruby
162
+ visit '/pages/form_tag'
163
+ assert_equal 1, SimpleCaptcha::SimpleCaptchaData.count
164
+ fill_in 'captcha', with: SimpleCaptcha::SimpleCaptchaData.first.value
165
+ ```
166
+
167
+ ##Options & Examples
168
+
169
+ ###View Options
170
+
171
+ * ``:label`` - provides the custom text b/w the image and the text field, the default is "type the code from the image"
172
+ * ``:object`` - the name of the object of the model class, to implement the model based captcha.
173
+ * ``:code_type`` - return numeric only if set to 'numeric'
174
+ * ``:multiple`` - allow to use the same captcha in multiple forms in one page. True for the first appaerance and false for the rest.
175
+
176
+ ###Global options
177
+
178
+ * ``:image_style`` - provides the specific image style for the captcha image.
179
+ There are eight different styles available with the plugin as...
180
+
181
+ 1. simply_blue
182
+ 2. simply_red
183
+ 3. simply_green
184
+ 4. charcoal_grey
185
+ 5. embosed_silver
186
+ 6. all_black
187
+ 7. distorted_black
188
+ 8. almost_invisible
189
+
190
+ Default style is 'simply_blue'.
191
+ You can also specify 'random' to select the random image style.
192
+
193
+ * ``:distortion`` - handles the complexity of the image. The :distortion can be set to 'low', 'medium' or 'high'. Default is 'low'.
194
+
195
+ * ``:implode`` - handles the complexity of the image. The :implode can be set to 'none', 'low', 'medium' or 'high'. Default is 'medium'.
196
+
197
+ Create "./config/initializers/simple_captcha.rb"
198
+
199
+ ```ruby
200
+ SimpleCaptcha.setup do |sc|
201
+ # default: 100x28
202
+ sc.image_size = '120x40'
203
+
204
+ # default: 5
205
+ sc.length = 6
206
+
207
+ # when the table name is changed,should defined it.defalut: simple_captcha_data.
208
+ sc.table_name = "simple_captcha_data"
209
+
210
+ # default: simply_blue
211
+ # possible values:
212
+ # 'embosed_silver',
213
+ # 'simply_red',
214
+ # 'simply_green',
215
+ # 'simply_blue',
216
+ # 'distorted_black',
217
+ # 'all_black',
218
+ # 'charcoal_grey',
219
+ # 'almost_invisible'
220
+ # 'random'
221
+ sc.image_style = 'simply_green'
222
+
223
+ # default: low
224
+ # possible values: 'low', 'medium', 'high', 'random'
225
+ sc.distortion = 'medium'
226
+
227
+ # default: medium
228
+ # possible values: 'none', 'low', 'medium', 'high'
229
+ sc.implode = 'low'
230
+ end
231
+ ```
232
+
233
+ You can add your own style:
234
+
235
+ ```ruby
236
+ SimpleCaptcha.setup do |sc|
237
+ sc.image_style = 'mycaptha'
238
+ sc.add_image_style('mycaptha', [
239
+ "-background '#F4F7F8'",
240
+ "-fill '#86818B'",
241
+ "-border 1",
242
+ "-bordercolor '#E0E2E3'"])
243
+ end
244
+ ```
245
+
246
+ You can provide the path where image_magick is installed as well:
247
+
248
+ ```ruby
249
+ SimpleCaptcha.setup do |sc|
250
+ sc.image_magick_path = '/usr/bin' # you can check this from console by running: which convert
251
+ end
252
+ ```
253
+
254
+
255
+ ### How to change the CSS for SimpleCaptcha DOM elements?
256
+
257
+ You can change the CSS of the SimpleCaptcha DOM elements as per your need in this file.
258
+
259
+ ``app/views/simple_captcha/_simple_captcha.erb``
260
+
261
+ ###View's Examples
262
+ ####Controller Based Example
263
+
264
+ ```erb
265
+ <%= show_simple_captcha %>
266
+
267
+ <%= show_simple_captcha(:label => "human authentication") %>
268
+ ```
269
+
270
+ ####Model Based Example
271
+
272
+ ```erb
273
+ <%= show_simple_captcha(:object => 'user', :label => "human authentication") %>
274
+ ```
275
+
276
+ ####Model Options
277
+
278
+ * ``:message`` - provides the custom message on failure of captcha authentication the default is "Secret Code did not match with the Image"
279
+
280
+ * ``:add_to_base`` - if set to true, appends the error message to the base.
281
+
282
+ #####Model's Example
283
+
284
+ ```ruby
285
+ class User < ActiveRecord::Base
286
+ apply_simple_captcha
287
+ end
288
+
289
+ class User < ActiveRecord::Base
290
+ apply_simple_captcha :message => "The secret Image and code were different", :add_to_base => true
291
+ end
292
+ ```
293
+
294
+
295
+ ##I18n
296
+
297
+ ```yaml
298
+ en:
299
+ simple_captcha:
300
+ placeholder: "Enter the image value"
301
+ label: "Enter the code in the box:"
302
+ refresh_button_text: "Refresh"
303
+ message:
304
+ default: "Secret Code did not match with the Image"
305
+ user: "The secret Image and code were different"
306
+ ```
307
+
308
+ ##Contributing
309
+
310
+ For testing, generate a temporary Rails dummy app inside test:
311
+
312
+ ```bash
313
+ rake dummy:setup
314
+ rake app:db:migrate
315
+ rake app:db:migrate RAILS_ENV=test
316
+ export BUNDLE_GEMFILE=$PWD/Gemfile
317
+ cd test/dummy && rake db:migrate db:test:prepare
318
+ rake test
319
+ ```
320
+
321
+ Please add test cases when adding new functionality. I started with some basic example integration tests for a very basic coverage.
322
+
323
+ The tests will be run on [Travis-CI](https://travis-ci.org/pludoni/simple-captcha).
324
+
325
+ ##Who's who?
326
+
327
+ Enjoy the simplest captcha implementation.
328
+
329
+ Original Author of the Version for Rails 2:
330
+ Author: Sur, Blog: http://expressica.com, Contact: sur.max@gmail.com
331
+ Plugin Homepage: http://expressica.com/simple_captcha
332
+
333
+ Plugin update for rails 3: http://github.com/galetahub
334
+
335
+ update for Rails 4, tests and forked by Stefan Wienert (pludoni GmbH)
data/Rakefile ADDED
@@ -0,0 +1,63 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SimpleCaptcha'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ if File.exists? APP_RAKEFILE
19
+ load 'rails/tasks/engine.rake'
20
+ end
21
+
22
+ namespace :dummy do
23
+ desc 'Setup dummy Rails app for test purpose'
24
+ task :setup do
25
+ require 'rails'
26
+ require 'simple_captcha'
27
+ require File.expand_path('../test/lib/generators/simple_captcha/dummy/dummy_generator', __FILE__)
28
+ SimpleCaptcha::DummyGenerator.start %w(--quiet)
29
+ end
30
+
31
+ desc 'destroy dummy Rails app under test/dummy'
32
+ task :destroy do
33
+ FileUtils.rm_rf "test/dummy" if File.exists?("test/dummy")
34
+ end
35
+
36
+ desc 'redo'
37
+ task :redo do
38
+ sh 'rake dummy:destroy'
39
+ sh 'rake dummy:setup'
40
+ sh 'rake app:db:migrate'
41
+ sh 'rake app:db:migrate RAILS_ENV=test'
42
+ end
43
+
44
+
45
+ end
46
+
47
+
48
+
49
+
50
+ Bundler::GemHelper.install_tasks :name => 'simple_captcha2'
51
+
52
+ require 'rake/testtask'
53
+
54
+ Rake::TestTask.new(:test) do |t|
55
+ t.libs << 'lib'
56
+ t.libs << 'test'
57
+ t.pattern = 'test/**/*_test.rb'
58
+ t.verbose = false
59
+ end
60
+
61
+
62
+ task default: :test
63
+
@@ -0,0 +1,7 @@
1
+ SimpleCaptcha
2
+ =============
3
+ # Generate migration and copy view partial
4
+ rails generate simple_captcha [template_format]
5
+ template_format Sets template format to be generated
6
+ Available options: erb, haml
7
+ Default: erb
@@ -0,0 +1,22 @@
1
+ require 'rails/generators'
2
+
3
+ class SimpleCaptchaGenerator < Rails::Generators::Base
4
+ argument :template_format, :type => :string, :default => 'erb'
5
+ include Rails::Generators::Migration
6
+
7
+ def self.source_root
8
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates/'))
9
+ end
10
+
11
+ def self.next_migration_number(dirname)
12
+ Time.now.strftime("%Y%m%d%H%M%S")
13
+ end
14
+
15
+ def create_partial
16
+ template "partial.#{template_format}", File.join('app/views', 'simple_captcha', "_simple_captcha.#{template_format}")
17
+ end
18
+
19
+ def create_captcha_migration
20
+ migration_template "migration.rb", File.join('db/migrate', "create_simple_captcha_data.rb")
21
+ end
22
+ 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
@@ -0,0 +1,41 @@
1
+ <style type="text/css">
2
+ .simple_captcha{border: 1px solid #ccc; padding: 5px !important;}
3
+ .simple_captcha,
4
+ .simple_captcha div{display: table;}
5
+ .simple_captcha .simple_captcha_field,
6
+ .simple_captcha .simple_captcha_image{
7
+ border: 1px solid #ccc;
8
+ margin: 0px 0px 2px 0px !important;
9
+ padding: 0px !important;
10
+ }
11
+ .simple_captcha .simple_captcha_image img{
12
+ margin: 0px !important;
13
+ padding: 0px !important;
14
+ width: 110px !important;
15
+ }
16
+ .simple_captcha .simple_captcha_label{font-size: 12px;}
17
+ .simple_captcha .simple_captcha_field input{
18
+ width: 150px !important;
19
+ font-size: 16px;
20
+ border: none;
21
+ background-color: #efefef;
22
+ }
23
+ </style>
24
+
25
+ <div class='simple_captcha'>
26
+ <div class='simple_captcha_image'>
27
+ <%%= simple_captcha_options[:image] %>
28
+ </div>
29
+
30
+ <div class='simple_captcha_field'>
31
+ <%%= simple_captcha_options[:field] %>
32
+ </div>
33
+
34
+ <div class='simple_captcha_label'>
35
+ <%%= simple_captcha_options[:label] %>
36
+ </div>
37
+
38
+ <div class='simple_captcha_refresh_button'>
39
+ <%%= simple_captcha_options[:refresh_button] %>
40
+ </div>
41
+ </div>
@@ -0,0 +1,28 @@
1
+ :css
2
+ .simple_captcha{border: 1px solid #ccc; padding: 5px !important;}
3
+ .simple_captcha,
4
+ .simple_captcha div{display: table;}
5
+ .simple_captcha .simple_captcha_field,
6
+ .simple_captcha .simple_captcha_image{
7
+ border: 1px solid #ccc;
8
+ margin: 0px 0px 2px 0px !important;
9
+ padding: 0px !important;
10
+ }
11
+ .simple_captcha .simple_captcha_image img{
12
+ margin: 0px !important;
13
+ padding: 0px !important;
14
+ width: 110px !important;
15
+ }
16
+ .simple_captcha .simple_captcha_label{font-size: 12px;}
17
+ .simple_captcha .simple_captcha_field input{
18
+ width: 150px !important;
19
+ font-size: 16px;
20
+ border: none;
21
+ background-color: #efefef;
22
+ }
23
+
24
+ .simple_captcha
25
+ .simple_captcha_image= simple_captcha_options[:image]
26
+ .simple_captcha_field= simple_captcha_options[:field]
27
+ .simple_captcha_label= simple_captcha_options[:label]
28
+ .simple_captcha_refresh_button= simple_captcha_options[:refresh_button]
@@ -0,0 +1,32 @@
1
+ module SimpleCaptcha #:nodoc
2
+ module ControllerHelpers #:nodoc
3
+ # This method is to validate the simple captcha in controller.
4
+ # It means when the captcha is controller based i.e. :object has not been passed to the method show_simple_captcha.
5
+ #
6
+ # *Example*
7
+ #
8
+ # If you want to save an object say @user only if the captcha is validated then do like this in action...
9
+ #
10
+ # if simple_captcha_valid?
11
+ # @user.save
12
+ # else
13
+ # flash[:notice] = "captcha did not match"
14
+ # redirect_to :action => "myaction"
15
+ # end
16
+ def simple_captcha_valid?
17
+ return true if SimpleCaptcha.always_pass
18
+ return @_simple_captcha_result unless @_simple_captcha_result.nil?
19
+
20
+ if params[:captcha]
21
+ captcha_key = params[:captcha_key] || session[:captcha]
22
+ data = SimpleCaptcha::Utils::simple_captcha_value(captcha_key)
23
+ result = data == params[:captcha].delete(" ").upcase
24
+ SimpleCaptcha::Utils::simple_captcha_passed!(captcha_key) if result
25
+ @_simple_captcha_result = result
26
+ result
27
+ else
28
+ false
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ require 'rails'
3
+ require 'simple_captcha'
4
+
5
+ module SimpleCaptcha
6
+ class Engine < ::Rails::Engine
7
+ config.before_initialize do
8
+ ActiveSupport.on_load :active_record do
9
+ ActiveRecord::Base.send(:include, SimpleCaptcha::ModelHelpers)
10
+ end
11
+ end
12
+
13
+ config.after_initialize do
14
+ ActionView::Base.send(:include, SimpleCaptcha::ViewHelper)
15
+ ActionView::Helpers::FormBuilder.send(:include, SimpleCaptcha::FormBuilder)
16
+
17
+ if Object.const_defined?("Formtastic")
18
+ require 'simple_captcha/formtastic'
19
+ end
20
+ end
21
+
22
+ config.app_middleware.use SimpleCaptcha::Middleware
23
+ end
24
+ end
25
+
26
+
@@ -0,0 +1,43 @@
1
+ module SimpleCaptcha
2
+ module FormBuilder
3
+ def self.included(base)
4
+ base.send(:include, SimpleCaptcha::ViewHelper)
5
+ base.send(:include, SimpleCaptcha::FormBuilder::ClassMethods)
6
+ base.send(:include, ActionView::Helpers)
7
+ if defined? Sprockets::Helpers
8
+ base.send(:include, Sprockets::Helpers::RailsHelper)
9
+ base.send(:include, Sprockets::Helpers::IsolatedHelper)
10
+ end
11
+
12
+ base.delegate :render, :session, :to => :template
13
+ end
14
+
15
+ module ClassMethods
16
+ # Example:
17
+ # <% form_for :post, :url => posts_path do |form| %>
18
+ # ...
19
+ # <%= form.simple_captcha :label => "Enter numbers.." %>
20
+ # <% end %>
21
+ #
22
+ def simple_captcha(options = {})
23
+ options.update :object => @object_name
24
+ show_simple_captcha(objectify_options(options))
25
+ end
26
+
27
+ private
28
+
29
+ def template
30
+ @template
31
+ end
32
+
33
+ def simple_captcha_field(options={})
34
+ html = {:autocomplete => 'off', :autocorrect => 'off', :autocapitalize => 'off', :required => 'required', :value => ''}
35
+ html.merge!(options[:input_html] || {})
36
+ html[:placeholder] = options[:placeholder] || I18n.t('simple_captcha.placeholder')
37
+
38
+ text_field(:captcha, html) +
39
+ hidden_field(:captcha_key, {:value => options[:field_value], :id => simple_captch_hidden_field_id(options)})
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ if defined? Formtastic
2
+ require 'formtastic/version'
3
+ if Formtastic::VERSION < '2.2'
4
+ raise 'Only Formtastic Version 2.2 or greater is supported by SimpleCaptcha'
5
+ end
6
+
7
+ class SimpleCaptchaInput
8
+ include Formtastic::Inputs::Base
9
+ def to_html
10
+ options.update :object => sanitized_object_name
11
+ builder.send(:show_simple_captcha, options)
12
+ end
13
+ end
14
+
15
+ end