Module: CustomCaptcha::Utils

Defined in:
lib/custom_captcha/utils.rb

Constant Summary collapse

@@image_files_paths =
[]
@@image_files_paths_choice_size =
@@image_files_paths.size

Class Method Summary collapse

Class Method Details

.captcha_value_valid?(captcha_value, captcha_digested) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/custom_captcha/utils.rb', line 32

def captcha_value_valid?(captcha_value, captcha_digested)
  image_file_basename_suffix(captcha_value) == captcha_digested
end

.choice_image_file_path_and_generate_captcha_digested_keyObject



25
26
27
28
29
30
# File 'lib/custom_captcha/utils.rb', line 25

def choice_image_file_path_and_generate_captcha_digested_key
  image_file_path = random_choice_image_file_path
  captcha_digested = get_captcha_digested(image_file_path)
  key = hexdigest_text([Time.now.to_i, SecureRandom.hex(10)].join())
  [image_file_path, captcha_digested, key]
end

.clear_image_filesObject



14
15
16
# File 'lib/custom_captcha/utils.rb', line 14

def clear_image_files
  FileUtils.rm_r Dir[File.join(image_file_dirname(), "**", "*")]
end

.generate_image_files(number = 1, &block) ⇒ Object



10
11
12
# File 'lib/custom_captcha/utils.rb', line 10

def generate_image_files(number=1, &block)
  CustomCaptcha::Image.create(number, &block)
end

.get_captcha_digested(image_file_path) ⇒ Object



87
88
89
90
# File 'lib/custom_captcha/utils.rb', line 87

def get_captcha_digested(image_file_path)
  image_file_basename = File.basename(image_file_path, image_file_extname())
  image_file_basename[-32..-1]
end

.hexdigest_text(text) ⇒ Object



40
41
42
# File 'lib/custom_captcha/utils.rb', line 40

def hexdigest_text(text)
  Digest::MD5.hexdigest([text.to_s.downcase, CustomCaptcha::Configuration.salt].join())
end

.image_file_basename(text) ⇒ Object



57
58
59
# File 'lib/custom_captcha/utils.rb', line 57

def image_file_basename(text)
  [image_file_basename_prefix, image_file_basename_suffix(text)].join()
end

.image_file_basename_prefixObject



48
49
50
51
# File 'lib/custom_captcha/utils.rb', line 48

def image_file_basename_prefix
#          ("a".."z").to_a.sort_by!{rand()}.take(8).join()
  SecureRandom.hex(4)
end

.image_file_basename_suffix(text) ⇒ Object



53
54
55
# File 'lib/custom_captcha/utils.rb', line 53

def image_file_basename_suffix(text)
  hexdigest_text(text)
end

.image_file_dirnameObject



44
45
46
# File 'lib/custom_captcha/utils.rb', line 44

def image_file_dirname
  CustomCaptcha::Configuration.images_path_definite
end

.image_file_extnameObject



61
62
63
# File 'lib/custom_captcha/utils.rb', line 61

def image_file_extname
  ".gif"
end

.random_choice_image_file_pathObject



81
82
83
84
85
# File 'lib/custom_captcha/utils.rb', line 81

def random_choice_image_file_path
  reset_image_files_paths()
  @@image_files_paths_choice_size -= 1
  @@image_files_paths[@@image_files_paths_choice_size]
end

.reload_image_files_pathsObject Also known as: init_image_files_paths



18
19
20
21
# File 'lib/custom_captcha/utils.rb', line 18

def reload_image_files_paths
  @@image_files_paths.clear
  reset_image_files_paths()
end

.reset_image_files_pathsObject

if image file path empty, then auto generating 3 image file.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/custom_captcha/utils.rb', line 66

def reset_image_files_paths
  if @@image_files_paths.blank? or @@image_files_paths_choice_size == 0
    n = 0
    while true
      @@image_files_paths = Dir[File.join(image_file_dirname(), ["[a-z0-9]*", image_file_extname()].join())]
      break unless self.image_files_paths.blank?
      generate_image_files(3)
      raise InitImageFilesKeyError if n >= 3
      n += 1
    end
    @@image_files_paths.sort_by!{rand()}
    @@image_files_paths_choice_size = @@image_files_paths.size
  end
end