代码拉取完成,页面将自动刷新
同步操作将从 hanguofeng/gocaptcha 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
// Copyright 2013 hanguofeng. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gocaptcha
import (
"errors"
"image"
"time"
)
//Captcha is the core captcha struct
type Captcha struct {
store StoreInterface
wordManager *WordManager
captchaConfig *CaptchaConfig
imageConfig *ImageConfig
filterConfig *FilterConfig
storeConfig *StoreConfig
}
//CreateCaptcha is a method to create new Captcha struct
func CreateCaptcha(wordManager *WordManager, captchaConfig *CaptchaConfig, imageConfig *ImageConfig, filterConfig *FilterConfig, storeConfig *StoreConfig) (*Captcha, error) {
var retErr error
captcha := new(Captcha)
store, err := createStore(storeConfig)
if nil == err {
captcha.store = store
} else {
retErr = err
}
if nil != wordManager {
captcha.wordManager = wordManager
} else {
retErr = errors.New("CreateCaptcha fail:invalid wordManager")
}
captcha.captchaConfig = captchaConfig
captcha.imageConfig = imageConfig
captcha.filterConfig = filterConfig
return captcha, retErr
}
//CreateCaptchaFromConfigFile is a method to create new Captcha struct
func CreateCaptchaFromConfigFile(configFile string) (*Captcha, error) {
var captcha *Captcha
var retErr error
err, wordDict, captchaConfig, imageConfig, filterConfig, storeConfig := loadConfigFromFile(configFile)
if nil == err {
wordmgr, err := CreateWordManagerFromDataFile(wordDict)
if nil == err {
captcha, retErr = CreateCaptcha(wordmgr, captchaConfig, imageConfig, filterConfig, storeConfig)
} else {
retErr = err
}
} else {
retErr = err
}
return captcha, retErr
}
//GetKey will generate a key with required length
func (captcha *Captcha) GetKey(length int) (string, error) {
var retErr error
var rst string
text, err := captcha.wordManager.Get(length)
if nil != err {
retErr = err
} else {
info := new(CaptchaInfo)
info.Text = text
info.CreateTime = time.Now()
rst = captcha.store.Add(info)
}
return rst, retErr
}
//Verify will verify the user's input and the server stored captcha text
func (captcha *Captcha) Verify(key, textToVerify string) (bool, string) {
info := captcha.store.Get(key)
if nil == info {
return false, "captcha info not found"
}
if info.CreateTime.Add(captcha.captchaConfig.LifeTime).Before(time.Now()) {
return false, "captcha expires"
}
if info.Text != textToVerify {
return false, "captcha text not match"
}
captcha.store.Del(key)
return true, ""
return false, "not reachable"
}
//GetImage will generate the binary image data
func (captcha *Captcha) GetImage(key string) (image.Image, error) {
info := captcha.store.Get(key)
if nil == info {
return nil, errors.New("captcha info not found")
}
if info.CreateTime.Add(captcha.captchaConfig.LifeTime).Before(time.Now()) {
return nil, errors.New("captcha expires")
}
cimg := captcha.genImage(info.Text)
return cimg, nil
}
func createStore(config *StoreConfig) (StoreInterface, error) {
var store StoreInterface
var err error
switch config.Engine {
case STORE_ENGINE_BUILDIN:
store = CreateCStore(config.LifeTime, config.GcProbability, config.GcDivisor)
break
case STORE_ENGINE_MEMCACHE:
store = CreateMCStore(config.LifeTime, config.Servers)
break
default:
err = errors.New("Not supported engine:'" + config.Engine + "'")
}
return store, err
}
func (captcha *Captcha) genImage(text string) *CImage {
cimg := CreateCImage(captcha.imageConfig)
cimg.drawString(text)
filtermanager := CreateImageFilterManagerByConfig(captcha.filterConfig)
for _, filter := range filtermanager.GetFilters() {
filter.Proc(cimg)
}
return cimg
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。