Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
daimakuai committed Jan 27, 2018
0 parents commit b6556b8
Show file tree
Hide file tree
Showing 8 changed files with 935 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Captcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace daimakuai\captcha;
use Yii;
use daimakuai\captcha\yii2CaptchaAsset;

class Captcha extends \yii\captcha\Captcha
{
public $template = '<div class="daimakuai-captcha">{image} {input}</div>';
public function run()
{
$this->registerClientScript();
parent::run();
}

public function registerClientScript()
{
yii2CaptchaAsset::register(Yii::$app->getView());
parent::registerClientScript();
}
}
98 changes: 98 additions & 0 deletions CaptchaAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace daimakuai\captcha;
use Yii;
use yii\helpers\Url;
use yii\web\Response;

class CaptchaAction extends \yii\captcha\CaptchaAction
{
public $fontFile = '@daimakuai/captcha/font/LithosPro-Regular.otf';
public $foreColor = 0x999999;
public $type = 'default'; // numbers & letters
public $offset = -2;

public function run()
{
if($this->type == 'numbers')
{
$this->offset = -10;
}
if (Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR) !== null) {
// AJAX request for regenerating code
$code = $this->getVerifyCode(true);
Yii::$app->response->format = Response::FORMAT_JSON;
return [
'hash1' => $this->generateValidationHash($code),
'hash2' => $this->generateValidationHash(strtolower($code)),
// we add a random 'v' parameter so that FireFox can refresh the image
// when src attribute of image tag is changed
'url' => Url::to([$this->id, 'v' => uniqid()]),
];
} else {
$this->setHttpHeaders();
Yii::$app->response->format = Response::FORMAT_RAW;
return $this->renderImage($this->getVerifyCode());
}
// $view->registerJs('yii.captcha.validation.js');
}

protected function generateVerifyCode()
{
if ($this->minLength > $this->maxLength) {
$this->maxLength = $this->minLength;
}
if ($this->minLength < 4) {
$this->minLength = 4;
}
if ($this->maxLength > 9) {
$this->maxLength = 9;
}
$length = mt_rand($this->minLength, $this->maxLength);

$str = '0123456789bcdfghjklmnpqrstvwxyz';
$code = '';
for ($i = 0; $i < $length; $i++) {
switch ($this->type)
{
case 'numbers':
$code .= $str[mt_rand(0, 9)];
break;
case 'letters':
$code .= $str[mt_rand(10, 30)];
break;
default:
$code .= $str[mt_rand(0, 30)];
}
}

return $code;
}

public function validate($input, $caseSensitive)
{
$input = $this->_changeInvalidNumbers($input);
$code = $this->getVerifyCode();
$valid = $caseSensitive ? ($input === $code) : strcasecmp($input, $code) === 0;
$session = Yii::$app->getSession();
$session->open();
$name = $this->getSessionKey() . 'count';
$session[$name] = $session[$name] + 1;
if ($valid || $session[$name] > $this->testLimit && $this->testLimit > 0) {
$this->getVerifyCode(true);
}

return $valid;
}

private function _changeInvalidNumbers($input)
{
$enNumbers = ['0','1','2','3','4','5','6','7','8','9'];
$faNumbers = ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'];
$arNumbers = ['٠','١','٢','٣','٤','٥','٦','٧','٨','٩'];
$input = str_replace($faNumbers,$enNumbers,$input);
$input = str_replace($arNumbers,$enNumbers,$input);

return $input;
}
}
Loading

0 comments on commit b6556b8

Please sign in to comment.