Module: CamaleonCms::CaptchaHelper

Included in:
CamaleonController
Defined in:
app/helpers/camaleon_cms/captcha_helper.rb

Instance Method Summary collapse

Instance Method Details

#cama_captcha_build(len = 5) ⇒ Object

build a captcha image len: Number or characters to include in captcha (default 5)



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 5

def cama_captcha_build(len = 5)
  img = MiniMagick::Image.open(File.join($camaleon_engine_dir.present? ? $camaleon_engine_dir : Rails.root.to_s,
                                         'lib', 'captcha', "captcha_#{rand(12)}.jpg").to_s)
  text = cama_rand_str(len)
  session[:cama_captcha] = [] unless session[:cama_captcha].present?
  session[:cama_captcha] << text
  img.combine_options do |c|
    c.gravity 'Center'
    c.fill('#FFFFFF')
    c.draw "text 0,5 #{text}"
    c.font File.join($camaleon_engine_dir.present? ? $camaleon_engine_dir : Rails.root.to_s, 'lib', 'captcha',
                     'bumpyroad.ttf')
    c.pointsize '30'
  end
  img
end

#cama_captcha_increment_attack(key) ⇒ Object

increment attempts for key by 1



67
68
69
70
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 67

def cama_captcha_increment_attack(key)
  session["cama_captcha_#{key}"] ||= 0
  session["cama_captcha_#{key}"] = session["cama_captcha_#{key}"].to_i + 1
end

#cama_captcha_reset_attack(key) ⇒ Object

reset the attacks counter for key key: a string to represent a url or form view



74
75
76
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 74

def cama_captcha_reset_attack(key)
  session["cama_captcha_#{key}"] = 0
end

#cama_captcha_tag(len = 5, img_args = { alt: '' }, input_args = {}, bootstrap_group_mode = false) ⇒ Object

build a captcha tag (image with captcha) img_args: attributes for image_tag input_args: attributes for input field



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 25

def cama_captcha_tag(len = 5, img_args = { alt: '' }, input_args = {}, bootstrap_group_mode = false)
  unless input_args[:placeholder].present?
    input_args[:placeholder] =
      I18n.t('camaleon_cms.captcha_placeholder',
             default: 'Please enter the text of the image')
  end
  img_args['onclick'] = "this.src = \"#{cama_captcha_url(len: len)}\"+\"&t=\"+(new Date().getTime());"
  img = "<img style='cursor: pointer;' src='#{cama_captcha_url(len: len,
                                                               t: Time.current.to_i)}' #{img_args.collect do |k, v|
                                                                                           "#{k}='#{v}'"
                                                                                         end.join(' ')} />"
  input = "<input type='text' name='captcha' #{input_args.collect { |k, v| "#{k}='#{v}'" }.join(' ')} />"
  if bootstrap_group_mode
    "<div class='input-group input-group-captcha'><span class='input-group-btn' style='vertical-align: top;'>#{img}</span>#{input}</div>"
  else
    "<div class='input-group-captcha'>#{img}#{input}</div>"
  end
end

#cama_captcha_tags_if_under_attack(key, captcha_parmas = [5, {}, { class: 'form-control required' }]) ⇒ Object

show captcha if under attack key: a string to represent a url or form view



86
87
88
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 86

def cama_captcha_tags_if_under_attack(key, captcha_parmas = [5, {}, { class: 'form-control required' }])
  cama_captcha_tag(*captcha_parmas) if cama_captcha_under_attack?(key)
end

#cama_captcha_total_attacks(key) ⇒ Object

return a number of attempts for key key: a string to represent a url or form view



80
81
82
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 80

def cama_captcha_total_attacks(key)
  session["cama_captcha_#{key}"] ||= 0
end

#cama_captcha_under_attack?(key) ⇒ Boolean

************************* captcha in attack helpers ***************************# check if the current visitor was submitted 5+ times key: a string to represent a url or form view key must be the same as the form “captcha_tags_if_under_attack(key, …)”

Returns:

  • (Boolean)


53
54
55
56
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 53

def cama_captcha_under_attack?(key)
  session["cama_captcha_#{key}"] ||= 0
  session["cama_captcha_#{key}"].to_i > current_site.get_option('max_try_attack', 5).to_i
end

#cama_captcha_verified?Boolean

verify captcha value

Returns:

  • (Boolean)


45
46
47
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 45

def cama_captcha_verified?
  (session[:cama_captcha] || []).include?((params[:cama_captcha] || params[:captcha]).to_s.upcase)
end

#captcha_verify_if_under_attack(key) ⇒ Object

verify captcha values if this key is under attack key: a string to represent a url or form view



60
61
62
63
64
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 60

def captcha_verify_if_under_attack(key)
  res = cama_captcha_under_attack?(key) ? cama_captcha_verified? : true
  session["cama_captcha_#{key}"] = 0 if cama_captcha_verified?
  res
end