Module: CamaleonCms::CaptchaHelper

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

Overview

Camaleon CMS is a content management system

Copyright (C) 2015 by Owen Peredo Diaz
Email: [email protected]
This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the  GNU Affero General Public License (GPLv3) for more details.

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)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 13

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



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

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



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

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



31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 31

def cama_captcha_tag(len = 5, img_args = {alt: ""}, input_args = {}, bootstrap_group_mode = false)
  input_args[:placeholder] = I18n.t('camaleon_cms.captcha_placeholder', default: 'Please enter the text of the image') unless input_args[:placeholder].present?
  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{|k, v| "#{k}='#{v}'" }.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



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

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



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

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)


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

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)


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

def cama_captcha_verified?
  (session[:cama_captcha] || []).include?((params[:cama_captcha] || params[:captcha]).to_s.upcase) || Rails.env == "test"
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



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

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