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[:captcha] = text
  img.combine_options do |c|
    c.resize "150x40"
    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


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

def cama_captcha_increment_attack(key)
  session[key] ||= 0
  session[key] = session[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


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

def cama_captcha_reset_attack(key)
  session[key] = 0
end

#cama_captcha_tag(len = 5, img_args = {alt: ""}, input_args = {}) ⇒ 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
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 31

def cama_captcha_tag(len = 5, img_args = {alt: ""}, input_args = {})
  input_args[:placeholder] = "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());"
  "<div><img style='cursor: pointer;' src='#{cama_captcha_url(len: len)}' #{img_args.collect{|k, v| "#{k}='#{v}'" }.join(" ") } /> <input type='text' name='captcha' #{input_args.collect{|k, v| "#{k}='#{v}'" }.join(" ") } /> </div>"
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


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

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


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

def cama_captcha_total_attacks(key)
  session[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)

46
47
48
49
50
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 46

def cama_captcha_under_attack?(key)
  session[key] ||= 0
  # if session[key].to_i > 10 then send an email to administrator with request info (ip, browser, if logged then send user info
  session[key].to_i > current_site.get_option("max_try_attack", 5).to_i
end

#cama_captcha_verified?Boolean

verify captcha value

Returns:

  • (Boolean)

38
39
40
# File 'app/helpers/camaleon_cms/captcha_helper.rb', line 38

def cama_captcha_verified?
  (params[:captcha].present? && params[:captcha].upcase == session[:captcha]) || 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


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

def captcha_verify_if_under_attack(key)
  cama_captcha_under_attack?(key) ? cama_captcha_verified? : true
end