2015-10-26 13:55:46 +00:00
|
|
|
require 'rails'
|
|
|
|
require 'action_controller'
|
|
|
|
require 'active_support/all'
|
2017-02-09 09:40:39 +00:00
|
|
|
require 'rucaptcha/rucaptcha'
|
|
|
|
require 'rucaptcha/version'
|
|
|
|
require 'rucaptcha/configuration'
|
|
|
|
require 'rucaptcha/controller_helpers'
|
|
|
|
require 'rucaptcha/view_helpers'
|
|
|
|
require 'rucaptcha/cache'
|
|
|
|
require 'rucaptcha/engine'
|
2017-12-07 08:49:42 +00:00
|
|
|
require 'rucaptcha/errors/configuration'
|
2015-10-26 06:09:39 +00:00
|
|
|
|
|
|
|
module RuCaptcha
|
|
|
|
class << self
|
|
|
|
def config
|
|
|
|
return @config if defined?(@config)
|
|
|
|
@config = Configuration.new
|
2017-12-07 09:04:25 +00:00
|
|
|
@config.style = :colorful
|
|
|
|
@config.length = 5
|
|
|
|
@config.strikethrough = true
|
2018-09-06 11:33:18 +00:00
|
|
|
@config.outline = false
|
2017-12-07 09:04:25 +00:00
|
|
|
@config.expires_in = 2.minutes
|
|
|
|
|
2016-10-29 03:06:10 +00:00
|
|
|
if Rails.application
|
|
|
|
@config.cache_store = Rails.application.config.cache_store
|
|
|
|
else
|
2017-03-08 03:54:36 +00:00
|
|
|
@config.cache_store = :mem_cache_store
|
2016-10-29 03:06:10 +00:00
|
|
|
end
|
2017-03-08 03:54:36 +00:00
|
|
|
@config.cache_store
|
2015-10-26 06:09:39 +00:00
|
|
|
@config
|
|
|
|
end
|
|
|
|
|
|
|
|
def configure(&block)
|
|
|
|
config.instance_exec(&block)
|
|
|
|
end
|
2017-01-22 02:16:57 +00:00
|
|
|
|
|
|
|
def generate()
|
|
|
|
style = config.style == :colorful ? 1 : 0
|
2017-12-07 08:49:42 +00:00
|
|
|
length = config.length
|
2017-12-07 09:04:25 +00:00
|
|
|
|
|
|
|
unless length.in?(3..7)
|
|
|
|
raise Rucaptcha::Errors::Configuration, 'length config error, value must in 3..7'
|
|
|
|
end
|
|
|
|
|
|
|
|
strikethrough = config.strikethrough ? 1 : 0
|
2018-09-06 11:33:18 +00:00
|
|
|
outline = config.outline ? 1 : 0
|
|
|
|
self.create(style, length, strikethrough, outline)
|
2017-01-22 02:16:57 +00:00
|
|
|
end
|
2017-03-08 03:54:36 +00:00
|
|
|
|
|
|
|
def check_cache_store!
|
|
|
|
cache_store = RuCaptcha.config.cache_store
|
|
|
|
store_name = cache_store.is_a?(Array) ? cache_store.first : cache_store
|
|
|
|
if [:memory_store, :null_store, :file_store].include?(store_name)
|
|
|
|
RuCaptcha.config.cache_store = [:file_store, Rails.root.join('tmp/cache/rucaptcha/session')]
|
|
|
|
|
|
|
|
puts "
|
|
|
|
|
|
|
|
RuCaptcha's cache_store requirements are stored across processes and machines,
|
|
|
|
such as :mem_cache_store, :redis_store, or other distributed storage.
|
|
|
|
But your current set is #{cache_store}, it has changed to :file_store for working.
|
|
|
|
NOTE: :file_store is still not a good way, it only works with single server case.
|
|
|
|
|
2017-06-08 01:36:23 +00:00
|
|
|
Please make config file `config/initializers/rucaptcha.rb` to setup `cache_store`.
|
2017-03-08 03:54:36 +00:00
|
|
|
More infomation please read GitHub RuCaptcha README file.
|
|
|
|
https://github.com/huacnlee/rucaptcha
|
|
|
|
|
|
|
|
"
|
|
|
|
end
|
|
|
|
end
|
2015-10-26 06:09:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-15 04:03:24 +00:00
|
|
|
ActiveSupport.on_load(:action_controller) do
|
|
|
|
ActionController::Base.send :include, RuCaptcha::ControllerHelpers
|
|
|
|
end
|
|
|
|
|
2017-02-15 04:31:25 +00:00
|
|
|
ActiveSupport.on_load(:action_view) do
|
2017-02-15 04:03:24 +00:00
|
|
|
include RuCaptcha::ViewHelpers
|
|
|
|
end
|