Files
code-bang/backend/vendor/topthink/think-captcha/src/Captcha.php
T

352 lines
17 KiB
PHP
Raw Normal View History

2026-09-22 16:43:47 +08:00
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\captcha;
use think\Session;
/**
* @property mixed $seKey
* @property mixed $expire
* @property mixed $length
* @property mixed $fontSize
* @property float|mixed $imageH
* @property float|int|mixed $imageW
* @property mixed $zhSet
* @property mixed $codeSet
* @property mixed $reset
* @property mixed $fontttf
* @property mixed $useZh
* @property mixed $useNoise
* @property mixed $useCurve
* @property mixed $useImgBg
* @property mixed $useArithmetic
*/
class Captcha
{
protected $config = [
'seKey' => 'ThinkPHP.CN',
// 撉諹����撖����
'codeSet' => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',
// 撉諹����蝚阡���
'expire' => 1800,
// 撉諹������𧒄�湛�s嚗�
'useZh' => false,
// 雿輻鍂銝剜�撉諹���
'zhSet' => '隞砌誑�穃�隞碶�雿𨀣𧒄閬�𢆡�賭漣����臬極撠勗僑�嗡��烐��冽��臬枂�賣䲮餈𥕦銁鈭���峕�憭扯�銝颱葉鈭箔�銝箸䔉���撖嫣�摮虫�蝥批𧑐銝芰鍂�諹��Z秩蝘滩��賢漲�抵���摮𣂼��芰冗�惩��箔�蝏誩�蝥踵𧋦�菟��誯鵭�𡁜�摰𧼮振摰𡁏楛瘜閗”��瘞渡��碶��唳�鈭諹絲�蹂�憟賢��䀹��靝蝙�批�蝑匧�雿枏��𡑒楝�暹�蝏梶洵�峕迤�啣�霈箔��拐�敶㮖舅鈭𥡝�憭抵�鈭钅��寧��脤��嗆�苷��游��餃�隞嗆𠯫�拍㮾�勗��䀹�銝帋誨�函��唳��笔紡撟喳��箸�����嗅�摨𥪜耦�喳�敹�甅撟脤��穃��喲䔮瘥𥪜�������𠰴�瘝∠�瘝餅�鈭磰圾蝟餅���掖蝢文仍�誩蘨�𤾸��㯄帕霈斗活���帋��∟��见��砍�憸��瘚���亙葉雿齿�餈𣂼膥撟園���硃�曄�憸䁅捶��遣�粹�瘣颱�敺���喟鸌甇文虜�喳撩���撠穃歇�孵��游𣪧蝏笔�頧砍��惩�銋苷��𤥁正���餅�餈硺遙敹𡑒�靚��銋�控蝔讠蓡�交凒閫���煺��剖��𧢲㺿蝞∪�撌勗�靽格𣈲霂��鞊∪������銝㮖��剖��瑞內憭滚�撣行�銝𨅯��坔�憌𤾸��堒嘀�唾蔭蝘穃��梶妖頧西恣蝏躰��𡁜𦛚鋡急㟲�娍郊蝐駁��瑕�皜抵��單神�亥蓬�𥪜��脣��桅�罸俈�脫�銝𤥁挽颲曉��箇�����埈�隡惩藁�剖���移�𤑳���ế���甇Z器皜�秐銝�&蝛嗡髡�舐𠶖��◆蝳餃��格絲鈭斗�銝𥪜��埝�霂��頞𢠃��怨�閫�鱻餈烐釣�𧼮��券���韏啗悅�踹��粹膄�砍�朣踹��𦦵�敶望��賣聢��蔭�函征�滚��嗥�餈唬��匧�敺瑁��亙榆�𦠜�憪讠��賢��嗅�閫匧��滨滯蝏剖��舀�霈圈𠗕摮䀹�憯怨澈蝝扳雯瘣曉��方��滨輕�輯捂�渲膩��瘨��摨羓眎�輻垢�笔�蟡硺噶韐箸����摰寥��硺�蝤冽��急挾蝞烾��挈�匧�潛�����枏蔽�齿𡟺�剝漲�𠹺縑�鍦蝱憯啗砲�餌�撘惩�摰喃劑�劐��𤏸�蝏批𢰧撅𧼮�銝亙��箸�撌阡△�𡑒��曇㜃�勗翰蝘啣�蝘餌漲撌湔����甇血畺�埈眾撣苷����擧�鈭砍𨭌����澆旦�枏鉄�堒���芦靚�凒憌笔�皞𣂷��湧��批㭂��雲�剖���恐�航氜擐硋偕瘜X㗁蝎㕑殿摨𣈯掉�讛����惩�皛∪井憭勗�雿譍��嘥��峕��冽擪撗拙�銝暹𤩅�亙�頞������W云璅∟換�誯翧�祆��𣂷漫�刻�����餅嵗�文𢅛蝔餃��砍𣈲颲𤘪�蝡坔𡖂�怠�曌枏��坔�敺桃裦����踹��𣂼�憟堒��鞾★雿坔�鍦㭘�𥕦��刻悟撉刻�撣桀��格偘隡睃�甇餅����摮�悌�扳��曉㙈鈭睲�頝蠘�蝎桃�瘥滨�憛鮋儍憿嗥��𣬚�霂舐��賊獈��站�暹�銝嘥戊�����阸鈭脤堺�瑕蝠撘寥����閫�䰾�剔���妟摰方蝠銵��滨撩�䀹陬撖毺�撖���脣𪃾憯斤��行��擧��条���撮�啣楨�𦠜散�望���打靘扳隋�𡝗泿頝肽圻��𠹭��繮�渡𡠺摰䀹毽蝥芯��芰��嗅捐�祉�皝踹�蝥孵��折��踹祠韐��蝔喳內蝖砌遠�芰蕃憟�睻憸��霂�粉������靘萇��賜��𡁶�瘜亥��𠰴枤蝞望�瘞扳��勗��暹熄�亦�蝥脣��勗�撠賭�蝻拇�����悄憟𧢲1頧質�撟澆𪑛�亥翰�见�瑽賢�埝𨘥�����斢𨅯嫃�∠�隞钅凃�𣂼摹�𡁏�閧��恍狍銝圈㦛�牐�銵𡑒𡟹韐肽��牐��㗇��墧�憿踵𣱣蝘埝�憪��璉桃���篅�嗉�餈蠘�鈭輻畆摨琿��折�撟�𤋮�磰恥擐躰�撘笔��𤩺�敹条��啗��交嚉�拐慾憌鮋蠧�貊�皜豢𥲤�滚亢隡滚��朞�颲匧�摨誩�蝥詨�銋∩��嗥撓憭孵艙�唳�瘝煺��堒���瘙賜ㄦ�唳榊�鍦���洽��‘�梯𡂝瘞貊𣑐�暸猐蝣單�憡���躰𡂝瘞貊𣑐�𦦵�甈抒讃憿箇㻛瘣贝�霂琿�誩虬�望𡠺�匧�蝚𤏸𥅾撠暹�憯格𠂔隡��蝛埈�瘙㗇�蝏踵��𥕢遢�𤘪𠳿蝘钅��餌�憭讐�撠𡝗�鈭閗晶撌噼挪�寡揻�𨀣窒�踵�摰W𡢢�望��箄��芾敞�𤩺佅隞日��匧ㄢ蝖怎�餈𡡞𤦉蝎䀹䔝銝渲��砍�蝳讐熊�拍兮�蹂�畾钅𡺨撱嗥��亦滲皜鞱�閗�瘜賣��賡�韏斤�憓�蔭璅芣��亙�瘙㰘揖�孵�鈭株��䀝��脫���脫�韐∪��脰揣隞芣��潮獄蝵芰��航膠蝛輯揮��朣鞾��賜𤫇擖脤�摨枏�蝑烐�甇���𨅯𢬢瘣𡑒�摨毺熙�嫣�敶閖�憒������阡埯韏鮋������儔蝡寡健�碶僚�𡁏‘憟乩摩韏嗅��娪�憯���芷��烾�靚见���紋����𥡝�鞉螱�舘��桀�蝚血��𡁶��拙��鮋�蝝a▽�嗥�皝㚚�隞�𨺗餈寧�隡貊��踵�鈭∠������𨺗��号�䁅笑璁�悚瘚枏�鋡剛�瘣芾陝�格��𤏸悖����钅𡡒摮拚�銋喳楊敺垍��嗡��臬繿蝝臬��㗇�銋𣂼��𥪜摩蝏拇�蝏滩��潛�撜圈妟�渡飢��歲撅��銝�圻蝔滩蕭璇���㛖2畾𠰴��𡝗������筍�瑁�銵∪𠹻�𦦵��駁彿獢��蝘抒��詨蝴�芸��芷曎皜𥪜襥�豢�瘣𥕦迫���銋唳檱摰㛖�韏𥟇�皛斤��剛��鞱𡢄�萘��瑟𧖣暺擧��埝�瘣䂿塳蝑埝�摰见憫��鬲瘨�㭠瘣亥��𡏭�����亙�鞊���急𠽌獢穃辺蝻肽郎�烐情�唳剳�游裦擖
// 銝剜�撉諹����蝚虫葡
'useImgBg' => false,
// 雿輻鍂�峕艶�曄�
'fontSize' => 25,
// 撉諹����雿枏之撠�(px)
'useCurve' => true,
// �臬炏�餅毽瘛�𤩅蝥�
'useNoise' => true,
// �臬炏瘛餃����
'imageH' => 0,
// 撉諹���㦛���摨�
'imageW' => 0,
// 撉諹���㦛��捐摨�
'length' => 5,
// 撉諹������
'fontttf' => '',
// 撉諹����雿橒�銝滩挽蝵桅��箄繮��
'bg' => [243, 251, 254],
// �峕艶憸𡏭𠧧
'reset' => true,
// 撉諹��𣂼��擧糓�阡�蝵�
'useArithmetic' => false //�臬炏雿輻鍂蝞埈钟撉諹���
];
private $im = null; // 撉諹���㦛���靘�
private $color = null; // 撉諹����雿㯄���
/**
* �嗆��寞� 霈曄蔭��㺭
* @access public
* @param array $config �滨蔭��㺭
*/
public function __construct($config = [])
{
$this->config = array_merge($this->config, $config);
}
/**
* 雿輻鍂 $this->name �瑕��滨蔭
* @access public
* @param string $name �滨蔭�滨妍
* @return mixed �滨蔭��
*/
public function __get($name)
{
return $this->config[$name];
}
/**
* 霈曄蔭撉諹����蝵�
* @access public
* @param string $name �滨蔭�滨妍
* @param string $value �滨蔭��
* @return void
*/
public function __set($name, $value)
{
if (isset($this->config[$name])) {
$this->config[$name] = $value;
}
}
/**
* 璉��仿�蝵�
* @access public
* @param string $name �滨蔭�滨妍
* @return bool
*/
public function __isset($name)
{
return isset($this->config[$name]);
}
/**
* 撉諹�撉諹���糓�行迤蝖�
* @access public
* @param string $code �冽�撉諹���
* @param string $id 撉諹����霂�
* @return bool �冽�撉諹���糓�行迤蝖�
*/
public function check($code, $id = '')
{
$key = $this->authcode($this->seKey) . $id;
// 撉諹�����賭蛹蝛�
$secode = Session::get($key, '');
if (empty($code) || empty($secode)) {
return false;
}
// session 餈��
if (time() - $secode['verify_time'] > $this->expire) {
Session::delete($key, '');
return false;
}
if ($this->authcode(strtoupper($code)) == $secode['verify_code']) {
$this->reset && Session::delete($key, '');
return true;
}
return false;
}
/**
* 颲枏枂撉諹���僎�𢠃�霂�����潔�摮条�session銝�
* 撉諹����摮睃�session��聢撘譍蛹嚗� array('verify_code' => '撉諹�����', 'verify_time' => '撉諹����撱箸𧒄��');
* @access public
* @param string $id 閬���鞾�霂�����霂�
* @return \think\Response
*/
public function entry($id = '')
{
// �曄�摰�(px)
$this->imageW || $this->imageW = $this->length * $this->fontSize * 1.5 + $this->length * $this->fontSize / 2;
// �曄�擃�(px)
$this->imageH || $this->imageH = $this->fontSize * 2.5;
// 撱箇�銝�撟� $this->imageW x $this->imageH ��㦛��
$this->im = imagecreate($this->imageW, $this->imageH);
// 霈曄蔭�峕艶
imagecolorallocate($this->im, $this->bg[0], $this->bg[1], $this->bg[2]);
// 撉諹����雿㯄��粹���
$this->color = imagecolorallocate($this->im, mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150));
if ($this->useImgBg) {
$this->background();
}
if ($this->useNoise) {
// 蝏䀹���
$this->writeNoise();
}
if ($this->useCurve) {
// 蝏睃僕�啁瑪
$this->writeCurve();
}
$ttfPath = __DIR__ . '/../assets/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/';
// 蝏㗛�霂��
$code = []; // 撉諹���
$codeNX = 0; // 撉諹���洵N銝芸�蝚衣�撌西器頝�
if ($this->useArithmetic) {
$this->fontttf = $ttfPath . '6.ttf';
$code = [mt_rand(1, 9), '+', mt_rand(1, 9)];
foreach ($code as $key => $char) {
$codeNX += $this->fontSize * 1.5;
imagettftext($this->im, $this->fontSize, 0, (int)$codeNX, (int)($this->fontSize * 1.6), $this->color, $this->fontttf, $char);
}
$code = $this->authcode($code[0] + $code[2]);
} else {
// 撉諹���蝙�券��箏�雿�
if (empty($this->fontttf)) {
$dir = dir($ttfPath);
$ttfs = [];
while (false !== ($file = $dir->read())) {
if ('.' != $file[0] && substr($file, -4) == '.ttf') {
$ttfs[] = $file;
}
}
$dir->close();
$this->fontttf = $ttfs[array_rand($ttfs)];
}
$this->fontttf = $ttfPath . $this->fontttf;
if ($this->useZh) {
// 銝剜�撉諹���
for ($i = 0; $i < $this->length; $i++) {
$code[$i] = iconv_substr($this->zhSet, floor(mt_rand(0, mb_strlen($this->zhSet, 'utf-8') - 1)), 1, 'utf-8');
imagettftext($this->im, $this->fontSize, mt_rand(-40, 40), (int)($this->fontSize * ($i + 1) * 1.5), (int)($this->fontSize + mt_rand(10, 20)), $this->color, $this->fontttf, $code[$i]);
}
} else {
for ($i = 0; $i < $this->length; $i++) {
$code[$i] = $this->codeSet[mt_rand(0, strlen($this->codeSet) - 1)];
$codeNX += mt_rand((int)($this->fontSize * 1.2), (int)($this->fontSize * 1.6));
imagettftext($this->im, $this->fontSize, mt_rand(-40, 40), (int)$codeNX, (int)($this->fontSize * 1.6), $this->color, $this->fontttf, $code[$i]);
}
}
$code = $this->authcode(strtoupper(implode('', $code)));
}
// 靽嘥�撉諹���
$key = $this->authcode($this->seKey);
$secode = [];
$secode['verify_code'] = $code; // �𦠜嵗撉𣬚�靽嘥��酒ession
$secode['verify_time'] = time(); // 撉諹����撱箸𧒄��
Session::set($key . $id, $secode, '');
ob_start();
// 颲枏枂�曉�
imagepng($this->im);
$content = ob_get_clean();
imagedestroy($this->im);
return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
}
/**
* �颱��∠眏銝斗辺餈𧼮銁銝�韏瑟��鞟��𤩺㦤甇�憐�賣㺭�脩瑪雿𨅯僕�啁瑪(雿惩虾隞交㺿�鞉凒撣���脩瑪�賣㺭)
*
* 擃䀝葉��㺭摮血�撘誩��賢�鈭��嚗���箸䔉
* 甇�憐�见遆�啗圾�𣂼�嚗䱭=Asin(�x+�)+b
* ��虜�啣�澆笆�賣㺭�曉���蔣�㵪�
* A嚗𡁜�摰𡁜陸�潘��喟熊�烐�隡詨�蝻拍��齿㺭嚗�
* b嚗朞”蝷箸郭敶W銁Y頧渡�雿滨蔭�喟頂�𣇉熊�𤑳宏�刻�蝳鳴�銝𠰴�銝见�嚗�
* �嚗𡁜�摰𡁏郭敶V�X頧港�蝵桀�蝟餅�璅芸�蝘餃𢆡頝萘氖嚗�椰�惩𢰧�𧶏�
* �嚗𡁜�摰𡁜𪂹�����撠𤩺迤�冽�T=2�/�␏争ê̄嚗�
*
*/
private function writeCurve()
{
$px = $py = 0;
// �脩瑪�漤���
$A = mt_rand(1, (int)($this->imageH / 2)); // �臬�
$b = mt_rand(-(int)($this->imageH / 4), (int)($this->imageH / 4)); // Y頧湔䲮�穃�蝘駁�
$f = mt_rand(-(int)($this->imageH / 4), (int)($this->imageH / 4)); // X頧湔䲮�穃�蝘駁�
$T = mt_rand((int)($this->imageH), (int)($this->imageW * 2)); // �冽�
$w = (2 * M_PI) / $T;
$px1 = 0; // �脩瑪璅芸���絲憪衤�蝵�
$px2 = mt_rand((int)($this->imageW / 2), (int)($this->imageW * 0.8)); // �脩瑪璅芸�����煺�蝵�
for ($px = $px1; $px <= $px2; $px = $px + 1) {
if (0 != $w) {
$py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(�x+�) + b
$i = (int)($this->fontSize / 5);
while ($i > 0) {
imagesetpixel($this->im, (int)($px + $i), (int)($py + $i), $this->color); // 餈䠷�(while)敺芰㴓�餃�蝝删�瘥㻳magettftext�𢁾magestring�典�雿枏之撠譍�甈∠𤫇�綽�銝滨鍂餈脒hile敺芰㴓嚗㗇�扯�閬�末敺��
$i--;
}
}
}
// �脩瑪�𡡞���
$A = mt_rand(1, (int)($this->imageH / 2)); // �臬�
$f = mt_rand(-(int)($this->imageH / 4), (int)($this->imageH / 4)); // X頧湔䲮�穃�蝘駁�
$T = mt_rand((int)($this->imageH), (int)($this->imageW * 2)); // �冽�
$w = (2 * M_PI) / $T;
$b = $py - $A * sin($w * $px + $f) - $this->imageH / 2;
$px1 = $px2;
$px2 = $this->imageW;
for ($px = $px1; $px <= $px2; $px = $px + 1) {
if (0 != $w) {
$py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(�x+�) + b
$i = (int)($this->fontSize / 5);
while ($i > 0) {
imagesetpixel($this->im, (int)($px + $i), (int)($py + $i), $this->color);
$i--;
}
}
}
}
/**
* �餅���
* 敺��曄�銝𠰴�銝滚�憸𡏭𠧧���瘥齿��啣�
*/
private function writeNoise()
{
$codeSet = '2345678abcdefhijkmnpqrstuvwxyz';
for ($i = 0; $i < 10; $i++) {
//���憸𡏭𠧧
$noiseColor = imagecolorallocate($this->im, mt_rand(150, 225), mt_rand(150, 225), mt_rand(150, 225));
for ($j = 0; $j < 5; $j++) {
// 蝏䀹���
imagestring($this->im, 5, mt_rand(-10, (int)($this->imageW)), mt_rand(-10, (int)($this->imageH)), $codeSet[mt_rand(0, strlen($codeSet) - 1)], $noiseColor);
}
}
}
/**
* 蝏睃��峕艶�曄�
* 瘜剁�憒��撉諹�����箏㦛���颲�之嚗���删鍂瘥磰�憭𡁶�蝟餌�韏��
*/
private function background()
{
$path = __DIR__ . '/../assets/bgs/';
$dir = dir($path);
$bgs = [];
while (false !== ($file = $dir->read())) {
if ('.' != $file[0] && substr($file, -4) == '.jpg') {
$bgs[] = $path . $file;
}
}
$dir->close();
$gb = $bgs[array_rand($bgs)];
list($width, $height) = @getimagesize($gb);
// Resample
$bgImage = @imagecreatefromjpeg($gb);
@imagecopyresampled($this->im, $bgImage, 0, 0, 0, 0, $this->imageW, $this->imageH, $width, $height);
@imagedestroy($bgImage);
}
/* �惩�撉諹��� */
private function authcode($str)
{
$key = substr(md5($this->seKey), 5, 8);
$str = substr(md5($str), 8, 10);
return md5($key . $str);
}
}