Skip to content

Commit

Permalink
Merge pull request #2 from aetiom/dev
Browse files Browse the repository at this point in the history
Push last bug fixes to v1.0
  • Loading branch information
aetiom authored Feb 3, 2019
2 parents 1d5b9d0 + 34639bf commit eae9c63
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 443 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"require":
{
"php": "^7.0.0",
"aetiom/php-extended": "dev-dev"
"aetiom/php-extended": "dev-dev"
},
"minimum-stability": "stable",
"autoload":
Expand Down
69 changes: 34 additions & 35 deletions src/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace VoightKampff;

/**
* VoightKampff captcha manager
* VoightKampff captcha
*
* @author Aetiom <aetiom@protonmail.com>
* @package VoightKampff
Expand Down Expand Up @@ -43,17 +43,23 @@ class Captcha {



/**
* Get captcha identifier
* @return string : captcha identifier
*/
public function getId()
{
return $this->collection->getId();
}


/**
* Get collection of images to display
* @return array : image collection
*/
public function getImages()
{
if ($this->collection !== null) {
return $this->collection->getImages();
}

return array();
return $this->collection->getImages();
}

/**
Expand All @@ -65,10 +71,6 @@ public function getImages()
*/
public function getDirective($lang = '')
{
if ($this->collection == null) {
return '';
}

return $this->formatDirective($lang);
}

Expand All @@ -89,16 +91,24 @@ public function getError()
* @param string $id : captcha identifier
* @param array $param : optional parameters
*/
public function __construct($id, $param = array())
public function __construct($id, $param)
{
$this->initiate($param);
$this->options = $param;

$this->directiveCol = new \aetiom\PhpExt\MultiLang\Collection(
$this->options['directiveCollection'],
$this->options['defaultLang']);

$this->errorCol = new \aetiom\PhpExt\MultiLang\Collection(
$this->options['errorCollection'],
$this->options['defaultLang']);

$this->collection = new Collection($id, $this->options);
$this->security = new Security($this->options['security']);

if ($this->security->get_timeout_status()) {
if ($this->security->getTimeoutStatus()) {
$this->error = $this->errorCol->createContainer('timeout',
array('%TIME%' => $this->security->get_timeout_remaining()));
array('%TIME%' => $this->security->getTimeoutRemaining()));

$this->collection->clear();
}
Expand All @@ -116,6 +126,12 @@ public function verify($userAnswers)
{
$answerList = $this->collection->getAnswers();

if (!$this->security->isSessionActive()) {
$this->error = $this->errorCol->createContainer('inactive');
$this->collection->clear();
return false;
}

if (empty($userAnswers)) {
$this->error = $this->errorCol->createContainer('emptyAnswers');
return false;
Expand All @@ -127,7 +143,7 @@ public function verify($userAnswers)
return true;
}

if (!$this->security->add_attempt()) {
if (!$this->security->addAttempt()) {
$this->collection->clear();
}

Expand All @@ -137,23 +153,6 @@ public function verify($userAnswers)



/**
* Initiate options and collections
* @param array $param : optional parameters
*/
private function initiate($param)
{
$this->options = VoightKampff::mergeWithDefaultOptions($param);

$this->directiveCol = new \aetiom\PhpExt\MultiLang\Collection(
$this->options['directiveCollection'],
$this->options['defaultLang']);

$this->errorCol = new \aetiom\PhpExt\MultiLang\Collection(
$this->options['errorCollection'],
$this->options['defaultLang']);
}

/**
* Check user answers
*
Expand All @@ -164,8 +163,8 @@ private function initiate($param)
*/
private function checkAnswers($userAnswers, $expectedAnswers)
{
// return false if user check more answers than exepcted
if (count($userAnswers) > $this->options['requestCount']) {
// return false if user does not send count of expected answers
if (count($userAnswers) !== $this->options['requestCount']) {
return false;
}

Expand Down Expand Up @@ -213,7 +212,7 @@ private function formatDirective($lang)
}


$dirStr .= $kwIn->getMessage($lang).$q->getMessage($lang).$kwOut->getMessage($lang);
$dirStr .= $kwIn->getMessage($lang).$q[$lang].$kwOut->getMessage($lang);

}

Expand Down
11 changes: 10 additions & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace VoightKampff;

/**
* Captcha collection
* VoightKampff captcha collection
*
* @author Aetiom <aetiom@protonmail.com>
* @package VoightKampff
Expand Down Expand Up @@ -38,6 +38,15 @@ class Collection {



/**
* Get collection identifier
* @return string : collection identifier
*/
public function getId()
{
return $this->id;
}

/**
* Get captcha images
* @return array list of images
Expand Down
14 changes: 11 additions & 3 deletions src/Config/defaultOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/**
* @var string cbPrefix : prefix used for/by checkbox elements (for id, name and label)
*/
'cbPrefix' => 'sc-cb',
'cbPrefix' => 'vk-cb',

/**
* @var security : security configuration (user max attemps and timeout system)
Expand Down Expand Up @@ -70,9 +70,9 @@
'options' => [

/**
* @var string fa5_style : fontawesome 5
* @var string fa5Style : fontawesome 5
*/
'fa5_style' => 'regular',
'fa5Style' => 'regular',
],

/**
Expand Down Expand Up @@ -171,6 +171,14 @@
'timeout' => [
'en' => 'Too much wrong answers, please try again in %TIME% second(s).',
'fr' => 'Trop de mauvaises réponses, réessayez dans %TIME% seconde(s).'
],

/**
* @var array inactive : error message when user get inactive
*/
'inactive' => [
'en' => 'You were too long to answer, please try again.',
'fr' => 'Vous avez été trop long à répondre, veuillez réessayer.'
]
]
];
Loading

0 comments on commit eae9c63

Please sign in to comment.