Skip to content

Commit

Permalink
Merge pull request #1 from IntegReady/20170225-dev-Code_refactor
Browse files Browse the repository at this point in the history
Code refactoring
  • Loading branch information
Stern87 authored Feb 27, 2017
2 parents fb9d596 + 368eb7d commit 85de32d
Show file tree
Hide file tree
Showing 10 changed files with 1,989 additions and 1,784 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# phpstorm project files
.idea

# netbeans project files
nbproject

# zend studio for eclipse project files
.buildpath
.project
.settings

# windows thumbnail cache
Thumbs.db

# composer vendor dir
/vendor

# composer itself is not needed
composer.phar
composer.lock

# Mac DS_Store Files
.DS_Store

# phpunit itself is not needed
phpunit.phar
# local phpunit config
/phpunit.xml


#ricoStore
web/store/*
163 changes: 75 additions & 88 deletions CaptchaAction.php
Original file line number Diff line number Diff line change
@@ -1,58 +1,39 @@
<?php

namespace legront\captcha;
namespace integready\simplemathcaptcha;

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

/**
* Description of CaptchaAction
*
* @author Henk Reitsma <[email protected]>
* @author IntegReady
* @since 1.0
*
* @property array $operators One of these operators will be used.
* @property int $minValue Min value of variables
* @property int $maxValue Max value of variables
* @property int $fontSize Font size
* @property string $imageFormat Avaliable values are 'jpeg' or 'png'
*/
class CaptchaAction extends \yii\captcha\CaptchaAction
{
const JPEG_FORMAT = 'jpeg';
const PNG_FORMAT = 'png';

/**
* One of these operators will be used.
* @var int
*/
public $operators = ['+'];

/**
* Min value of variables.
* @var int
*/
public $minValue = 1;

/**
* Max value of variables.
* @var int
*/
public $maxValue = 10;

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

/**
* Avaliable values are 'jpeg' or 'png'
* @var string
*/
const PNG_FORMAT = 'png';

public $operators = ['+'];
public $minValue = 1;
public $maxValue = 10;
public $fontSize = 14;
public $imageFormat = self::PNG_FORMAT;

/**
* @inheritdoc
*/
public function init()
{

}

/**
Expand All @@ -62,18 +43,20 @@ public function run()
{
if (Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR) !== null) {
// AJAX request for regenerating code
$equation = $this->getVerifyCode(true);
$equation = $this->getVerifyCode(true);
Yii::$app->response->format = Response::FORMAT_JSON;

return [
'hash1' => $this->generateValidationHash($equation),
'hash2' => $this->generateValidationHash(strtolower($equation)),
// 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()]),
'url' => Url::to([$this->id, 'v' => uniqid()]),
];
} else {
$this->setHttpHeaders();
Yii::$app->response->format = Response::FORMAT_RAW;

return $this->renderImage($this->getVerifyCode(false, true));
}
}
Expand All @@ -87,8 +70,8 @@ public function getVerifyCode($regenerate = false, $equation = false)
$session->open();
$name = $this->getSessionKey();
if ($session[$name] === null || $regenerate) {
$session[$name . 'code'] = $this->generateVerifyCode();
$session[$name] = $this->getValue($session[$name . 'code']);
$session[$name . 'code'] = $this->generateVerifyCode();
$session[$name] = $this->getValue($session[$name . 'code']);
$session[$name . 'count'] = 1;
}

Expand All @@ -98,48 +81,56 @@ public function getVerifyCode($regenerate = false, $equation = false)
/**
* @inheritdoc
*/
public function validate($input,$caseSensitive = false)
protected function generateVerifyCode()
{
$equation = $this->getVerifyCode(false, true);
$value = $this->getValue($equation);

$valid = $input == $value;
mt_srand(time());

$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);
}
$equation = [
'first' => mt_rand($this->minValue, $this->maxValue),
'operator' => $this->operators[array_rand($this->operators)],
'second' => mt_rand($this->minValue, $this->maxValue),
];

return $valid;
return $equation;
}

/**
* @inheritdoc
* Get value of formula
*
* @param array $equation
*
* @return int|float
*/
protected function generateVerifyCode()
protected function getValue($equation)
{
mt_srand(time());

$equation = [
'first' => mt_rand($this->minValue, $this->maxValue),
'operator' => $this->operators[array_rand($this->operators)],
'second' => mt_rand($this->minValue, $this->maxValue)
];
if ($this->fixedVerifyCode !== null) {
return $this->fixedVerifyCode;
}

return $equation;
return eval('return ' . $equation['first'] . $equation['operator'] . $equation['second'] . ';');
}

/**
* @inheritdoc
* Sets the HTTP headers needed by image response.
*/
protected function renderImage($equation)
protected function setHttpHeaders()
{
require __DIR__ . '/mathpublisher.php';
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}");
}

$formula = new \expression_math(tableau_expression(trim($this->getExpression($equation))));
/**
* @param array $equation
*
* @return string
*/
protected function renderImage($equation)
{
$formula = new ExpressionMath(MathPublisher::tableauExpression(trim($this->getExpression($equation))));
$formula->dessine($this->fontSize);

ob_start();
Expand All @@ -156,44 +147,40 @@ protected function renderImage($equation)
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 $equation
*
* @return string
*/
protected function getExpression($equation)
{
if ($this->fixedVerifyCode !== null) {
return $this->fixedVerifyCode;
}
return $equation['first'] ."~". $equation['operator'] ."~". $equation['second'] ."~=";

return $equation['first'] . '~' . $equation['operator'] . '~' . $equation['second'] . '~=';
}

/**
* Get value of formula
* @param array $equation
* @return int|float
* @inheritdoc
*/
protected function getValue($equation)
public function validate($input, $caseSensitive = false)
{
if ($this->fixedVerifyCode !== null) {
return $this->fixedVerifyCode;
$equation = $this->getVerifyCode(false, true);
$value = $this->getValue($equation);

$valid = $input == $value;

$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 eval('return '. $equation['first'] . $equation['operator'] . $equation['second'] .';');

return $valid;
}
}
14 changes: 14 additions & 0 deletions Expression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace integready\simplemathcaptcha;

/**
* Class Expression
* @package integready\simplemathcaptcha
*/
class Expression
{
public $texte;
public $image;
public $base_verticale;
}
Loading

0 comments on commit 85de32d

Please sign in to comment.