Class: FastCaptcha

Inherits:
Object
  • Object
show all
Defined in:
lib/fastcaptcha.rb,
ext/fastcaptcha.c

Defined Under Namespace

Classes: Challenge

Constant Summary collapse

CHARSET =
('A'..'Z').to_a + (0..9).to_a - ['I', 1, 'O', 0, 'G']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache = nil, level = 1, w = 200, h = 50) ⇒ FastCaptcha

Returns a new instance of FastCaptcha.



10
11
12
13
14
15
# File 'lib/fastcaptcha.rb', line 10

def initialize cache = nil, level = 1, w = 200, h = 50
  @cache  = cache || cache_connection
  @level  = level
  @width  = w
  @height = h
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



8
9
10
# File 'lib/fastcaptcha.rb', line 8

def cache
  @cache
end

Instance Method Details

#cache_connectionObject



43
44
45
46
# File 'lib/fastcaptcha.rb', line 43

def cache_connection
  require 'moneta/redis2'
  Moneta::Redis2.new
end

#generate(ttl = 60, png = true, text = nil) ⇒ Object



17
18
19
20
21
# File 'lib/fastcaptcha.rb', line 17

def generate ttl = 60, png = true, text = nil
  text = text || 6.times.map { CHARSET[rand(CHARSET.length)] }.join('')
  key  = store(text.strip.upcase, ttl)
  Challenge.new(key, png ? image(text, @level, @width, @height) : nil)
end

#hash(text) ⇒ Object



23
24
25
# File 'lib/fastcaptcha.rb', line 23

def hash text
  'fastcaptcha:%s' % Digest::SHA1.hexdigest("#{text}#{Time.now.to_f}")
end

#image(*args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'ext/fastcaptcha.c', line 44

VALUE rb_captcha_image(int argc, VALUE* argv, VALUE self) {
    VALUE string, lv, w, h;

    rb_scan_args(argc, argv, "22", &string, &lv, &w, &h);

    CvScalar c;
    CvPoint *pts, pt;
    CvFont font[5];
    char text[2];
    IplImage *img, *dst;
    CvMat *out;
    int i, j, x, y;
    char *cstr = RSTRING_PTR(string);
    int W=strlen(cstr)*30+20, H=50;
    int level = NUM2INT(lv);
    double sx, sy;
    img = cvCreateImage(cvSize(W, H), 8, 3);
    cvSet(img, cvScalarAll(255), 0);
    pts = (CvPoint *)malloc(sizeof(CvPoint)*(strlen(cstr)+1));
    for (i = 0; i < 5; i++) {
        sx = (float)(rand()%4)*0.1 + 1.0;
        sy = (float)(rand()%4)*0.1 + 1.0;
        cvInitFont(&font[i], CV_FONT_HERSHEY_SIMPLEX, sx, sy, 1, 2, 8);
    }
    if (level > SILLY && level < HARD) {
        for (i = 0; i < W; i += 5) {
            c = CV_RGB(rand()%50 + 200, rand()%50 + 200, rand()%50 + 200);
            cvLine(img, cvPoint(i, 0), cvPoint(i, H), c, rand()%2+1, 8, 0);
        }
        for (i = 0; i < H; i += 5) {
            c = CV_RGB(rand()%50 + 200, rand()%50 + 200, rand()%50 + 200);
            cvLine(img, cvPoint(0, i), cvPoint(W, i), c, rand()%2+1, 8, 0);
        }
    }
    if (level >= MEDIUM) {
        bgWarp(img, img);
        for (i = 0; i < 10; i++) {
            j = rand()%50 + 120;
            pt = cvPoint(rand()%W, rand()%H);
            cvEllipse(img, pt, cvSize(rand()%50+10, rand()%50+10), rand()%90, 0, 360, CV_RGB(j,j,j), 1, 8, 0);
        }
    }
    x = rand()%40;
    memset(text, 0, 2);
    for (i = 0; i < strlen(cstr); i++) {
        y = (H>>1) - (10 - rand()%10) + 20;
        pts[i] = cvPoint(x, y - rand()%20);
        text[0] = cstr[i];
        c = CV_RGB(rand()%150 + 50, rand()%150 + 50, rand()%150 + 50);
        cvPutText(img, text, cvPoint(x, y), &font[i%5], c);
        x += 25;
        pts[i+1] = cvPoint(x, y - rand()%20);
    }
    if (level >= HARD) {
        for (i = 0; i < strlen(cstr); i++) {
            c = CV_RGB(rand()%50 + 120, rand()%50 + 120, rand()%50 + 120);
            cvLine(img, pts[i], pts[i+1], c, 2, 8, 0);
        }
    }

    w = NIL_P(w) ? INT2NUM(W) : w;
    h = NIL_P(h) ? INT2NUM(H) : h;

    dst = cvCreateImage(cvSize(NUM2INT(w), NUM2INT(h)), 8, 3);
    cvResize(img, dst, CV_INTER_CUBIC);
    out = cvEncodeImage(".png", dst, 0);
    VALUE imgdata = rb_str_new((const char *)out->data.ptr, out->step);
    cvReleaseImage(&img);
    cvReleaseImage(&dst);
    cvReleaseMat(&out);
    free(pts);
    return imgdata;
}

#refresh(key) ⇒ Object



33
34
35
36
# File 'lib/fastcaptcha.rb', line 33

def refresh key
  text = cache[key]
  text ? image(text, @level, @width, @height) : nil
end

#store(text, ttl) ⇒ Object



27
28
29
30
31
# File 'lib/fastcaptcha.rb', line 27

def store text, ttl
  key = hash(text)
  cache.store(key, text, expires_in: ttl)
  key
end

#validate(key, response) ⇒ Object



38
39
40
41
# File 'lib/fastcaptcha.rb', line 38

def validate key, response
  rv = response && (cache[key] == response.upcase.strip)
  rv && cache.delete(key)
end