Skip to content

Commit

Permalink
Refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmunir committed Feb 10, 2016
1 parent e4dc975 commit 96a9383
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 399 deletions.
159 changes: 124 additions & 35 deletions CaptchaAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,57 @@

namespace mdm\captcha;

use \Yii;
use yii\helpers\Url;
use Yii;
use yii\web\Response;
use yii\helpers\Url;

/**
* Description of CaptchaAction 2520
* Description of CaptchaAction
*
* @author MDMunir
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class CaptchaAction extends \yii\captcha\CaptchaAction
{
const JPEG_FORMAT = 'jpeg';
const PNG_FORMAT = 'png';

/**
* Avaliable value are 'jpeg' or 'png'
* @var string
*/
public $imageFormat = self::JPEG_FORMAT;

/**
* Dificully level
* @var int
*/
public $level = 1;

/**
* Font size.
* @var int
*/
public $size = 14;

/**
*
* @var Formula
* Allow decimal
* @var boolean
*/
private $_formula;
public $allowDecimal = false;

/**
* Registered equation class
* @var array
*/
public static $classes;

/**
* @inheritdoc
*/
public function init()
{
$this->_formula = new Formula($this->level);

}

/**
Expand All @@ -38,77 +63,141 @@ public function run()
if (Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR) !== null) {
// AJAX request for regenerating code
$code = $this->getVerifyCode(true);

return json_encode([
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->_formula->getExpresion($this->getFormulaCode()));
return $this->renderImage($this->getVerifyCode(false, true));
}
}

/**
* Gets the verification code.
* @param boolean $regenerate whether the verification code should be regenerated.
* @return string the verification code.
* @inheritdoc
*/
public function getVerifyCode($regenerate = false)
public function getVerifyCode($regenerate = false, $code = false)
{
$session = Yii::$app->getSession();
$session->open();
$name = $this->getSessionKey();
if ($session[$name] === null || $regenerate) {
$code = $session[$name . 'code'] = $this->generateVerifyCode();
$session[$name] = $this->_formula->getValue($code);
$session[$name . 'code'] = $this->generateVerifyCode();
$session[$name] = $this->getValue($session[$name . 'code']);
$session[$name . 'count'] = 1;
}
return $session[$name];

return $code ? $session[$name . 'code'] : $session[$name];
}

protected function getFormulaCode()
/**
* @inheritdoc
*/
public function validate($input)
{
$code = $this->getVerifyCode(false, true);
$value = $this->getValue($code);
if ($this->allowDecimal) {
$valid = abs(round($input, 2) - round($value, 2)) <= 0.02;
} else {
$valid = $input == $value;
}

$session = Yii::$app->getSession();
$session->open();
$name = $this->getSessionKey();
if ($session[$name] === null) {
$code = $session[$name . 'code'] = $this->generateVerifyCode();
$session[$name] = $this->_formula->getValue($code);
$session[$name . 'count'] = 1;
$name = $this->getSessionKey() . 'count';
$session[$name] = $session[$name] + 1;
if ($valid || $session[$name] > $this->testLimit && $this->testLimit > 0) {
$this->getVerifyCode(true);
}
return $session[$name . 'code'];

return $valid;
}

/**
* @inheritdoc
*/
protected function generateVerifyCode()
{
if ($this->fixedVerifyCode !== null) {
return $this->fixedVerifyCode;
}
mt_srand(time());
$code = [];
for ($i = 0; $i <= 5; $i++) {
$code = [mt_rand(0, 100)];
for ($i = 1; $i <= 5; $i++) {
$code[$i] = mt_rand(0, 10);
}
$code[6]= mt_rand(0, 100);

return $code;
}

/**
* @inheritdoc
*/
protected function renderImage($code)
{
require __DIR__ . '/mathpublisher.php';

$formula = new \expression_math(tableau_expression(trim($code)));
$formula = new \expression_math(tableau_expression(trim($this->getExpresion($code))));
$formula->dessine($this->size);

ob_start();
imagepng($formula->image);
switch ($this->imageFormat) {
case self::JPEG_FORMAT:
imagejpeg($formula->image);
break;
case self::PNG_FORMAT:
imagepng($formula->image);
break;
}
imagedestroy($formula->image);

return ob_get_clean();
}
}

/**
* Sets the HTTP headers needed by image response.
*/
protected function setHttpHeaders()
{
Yii::$app->getResponse()->getHeaders()
->set('Pragma', 'public')
->set('Expires', '0')
->set('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
->set('Content-Transfer-Encoding', 'binary')
->set('Content-type', "image/{$this->imageFormat}");
}

/**
* Get expresion formula .
* @param array $code
* @return string
*/
protected function getExpresion($code)
{
if ($this->fixedVerifyCode !== null) {
return $this->fixedVerifyCode;
}
$class = static::$classes[$this->level][$code[0] % count(static::$classes[$this->level])];
return $class::getExpresion($code, $this->allowDecimal);
}

/**
* Get value of formula
* @param array $code
* @return int|float
*/
protected function getValue($code)
{
if ($this->fixedVerifyCode !== null) {
return $this->fixedVerifyCode;
}
$class = static::$classes[$this->level][$code[0] % count(static::$classes[$this->level])];
return $class::getValue($code, $this->allowDecimal);
}
}

//
CaptchaAction::$classes = require(__DIR__ . '/equations/classes.php');
37 changes: 0 additions & 37 deletions Formula.php

This file was deleted.

20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@ The preferred way to install this extension is through [composer](http://getcomp
Either run

```
php composer.phar require --prefer-dist mdmsoft/yii2-captcha "1.0.0"
php composer.phar require --prefer-dist mdmsoft/yii2-captcha "~1.0"
```

or add

```
"mdmsoft/yii2-captcha": "1.0.0"
```

for dev, using

```
"mdmsoft/yii2-captcha": "1.0.*@dev"
"mdmsoft/yii2-captcha": "~1.0"
```

to the require section of your `composer.json` file.
Expand All @@ -43,5 +37,15 @@ public function actions()
],
];
}
```

In view
```php
<?=
$form->field($model, 'verifyCode')->widget(Captcha::className(), [
'template' => '<div class="row"><div class="col-lg-12">{image}</div><div class="col-lg-12">{input}</div></div>',
])
?>

```
![screenshot](https://lh3.googleusercontent.com/-ACmPR-FSnfE/U4Rz2f3tqqI/AAAAAAAAAgw/D6xuLeobLU4/w804-h496-no/Screenshot+from+2014-05-27+16%253A47%253A07.png)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "1.x-dev"
}
}
}
37 changes: 5 additions & 32 deletions equations/AddSub.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,15 @@
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class AddSub implements EquationInterface
class AddSub
{

protected static function format($code)
{
switch ($code[5] % 5) {
case 0:
$a = $code[1] + $code[2];
$b = $code[3] + 11;
$c = $code[4];
break;
$a = $code[1] + $code[2] + $code[3] * $code[5];
$b = $code[3] + $code[4] + 12;
$c = $code[2] + $code[4] + $code[5] + 1;

case 1:
$a = $code[1] * $code[2];
$b = $code[3] + 11;
$c = $code[4];
break;

case 2:
$a = $code[1] + $code[2];
$b = $code[3] + 11;
$c = $code[3] + $code[4];
break;

case 3:
$a = $code[1] * $code[2];
$b = $code[3] + 11;
$c = $code[3] + $code[4];
break;

default:
$a = 2 * ($code[1] + $code[2]);
$b = $code[3] + 11;
$c = $code[3] + $code[4];
break;
}
return [$a, $b, $c];
}

Expand All @@ -59,4 +32,4 @@ public static function getValue($code)
list($a, $b, $c) = static::format($code);
return $a + $b - $c;
}
}
}
Loading

0 comments on commit 96a9383

Please # to comment.