/usr/lib/ruby/1.8/ramaze/helper/simple_captcha.rb is in libramaze-ruby1.8 2010.06.18-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | module Ramaze
module Helper
# Produce very simple question/answer pairs.
#
# The default is a trivial mathematical problem.
#
# Usage (trait is optional):
#
# class RegisterController < Ramaze::Controller
# trait :captcha => lambda{
# ["the answer to everything", "42"]
# }
#
# def index
# %(
# <form action="#{r(:answer}">
# What is #{simple_captcha}?
# <input type="text" name="answer" />"
# <input type="submit" />
# </form>
# ).strip
# end
#
# def answer
# check_captcha(request[:answer])
# end
# end
module SimpleCaptcha
include Ramaze::Traited
NUMBERS = [5, 10, 15, 20]
# lambda should return question and answer in [question, answer] form
trait :captcha => lambda{
ns = Array.new(2){ NUMBERS.sort_by{rand}.first }.sort
op = rand > 0.42 ? [ns[0], :+, ns[1]] : [ns[1], :-, ns[0]]
question = op.join(' ')
answer = op[0].send(op[1], op[2])
[question, answer]
}
# Call the trait[:captcha] and store question/answer in session
def simple_captcha
question, answer = ancestral_trait[:captcha].call
session[:CAPTCHA] = { :question => question, :answer => answer.to_s }
question
end
# check the given +answer+ against the answer stored in the session.
def check_captcha(answer)
return false unless captcha = session[:CAPTCHA]
answer.to_s.strip == captcha[:answer].to_s
end
end
end
end
|