Skip to content

Commit

Permalink
Release of version 3.1.0
Browse files Browse the repository at this point in the history
Release of version 3.1.0
  • Loading branch information
fabianwennink authored Oct 8, 2022
2 parents cbbad9e + 940971c commit a622b86
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# IconCaptcha - PHP & JavaScript

![Version](https://img.shields.io/badge/Version-3.0.1-orange.svg?style=flat-square)
![Version](https://img.shields.io/badge/Version-3.1.0-orange.svg?style=flat-square)
![License](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)
[![Issues](https://img.shields.io/github/issues/fabianwennink/IconCaptcha-Plugin-jQuery-PHP?style=flat-square)](https://github.com/fabianwennink/IconCaptcha-Plugin-jQuery-PHP/issues)
[![Support](https://img.shields.io/badge/Support-PayPal-yellow.svg?style=flat-square)](https://paypal.me/nlgamevideosnl)
Expand Down
4 changes: 2 additions & 2 deletions assets/css/icon-captcha.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/demo.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* IconCaptcha Plugin: v3.0.1
* IconCaptcha Plugin: v3.1.0
* Copyright © 2022, Fabian Wennink (https://www.fabianwennink.nl)
*
* Licensed under the MIT license: https://www.fabianwennink.nl/projects/IconCaptcha/license
Expand Down
2 changes: 1 addition & 1 deletion assets/demo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* IconCaptcha Plugin: v3.0.1
* IconCaptcha Plugin: v3.1.0
* Copyright © 2022, Fabian Wennink (https://www.fabianwennink.nl)
*
* Licensed under the MIT license: https://www.fabianwennink.nl/projects/IconCaptcha/license
Expand Down
4 changes: 2 additions & 2 deletions assets/js/icon-captcha.min.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions examples/ajax-form.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* IconCaptcha Plugin: v3.0.1
* IconCaptcha Plugin: v3.1.0
* Copyright © 2022, Fabian Wennink (https://www.fabianwennink.nl)
*
* Licensed under the MIT license: https://www.fabianwennink.nl/projects/IconCaptcha/license
Expand Down Expand Up @@ -62,7 +62,7 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>IconCaptcha v3.0.1 - By Fabian Wennink</title>
<title>IconCaptcha v3.1.0 - By Fabian Wennink</title>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<meta name="author" content="Fabian Wennink © <?= date('Y') ?>" />
Expand All @@ -89,7 +89,7 @@
<div class="shields">
<div class="shields-row">
<a href="https://github.com/fabianwennink/IconCaptcha-Plugin-jQuery-PHP/releases" target="_blank" rel="noopener">
<img src="https://img.shields.io/badge/Version-3.0.1-orange.svg?style=flat-square" alt="Version 3.0.1 Badge" />
<img src="https://img.shields.io/badge/Version-3.1.0-orange.svg?style=flat-square" alt="Version 3.1.0 Badge" />
</a>
<a href="https://github.com/fabianwennink/IconCaptcha-Plugin-jQuery-PHP/blob/master/LICENSE" target="_blank" rel="noopener">
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square" alt="License-MIT Badge" />
Expand Down Expand Up @@ -213,6 +213,8 @@
// console.log('Event: Captcha refreshed', e.detail.captchaId);
// }).bind('invalidated', function(e) {
// console.log('Event: Invalidated', e.detail.captchaId);
// }).bind('reset', function(e) {
// console.log('Event: Reset', e.detail.captchaId);
// }).bind('success', function(e) {
// console.log('Event: Correct input', e.detail.captchaId);
// }).bind('error', function(e) {
Expand Down
2 changes: 1 addition & 1 deletion examples/form/ajax-submit.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* IconCaptcha Plugin: v3.0.1
* IconCaptcha Plugin: v3.1.0
* Copyright © 2022, Fabian Wennink (https://www.fabianwennink.nl)
*
* Licensed under the MIT license: https://www.fabianwennink.nl/projects/IconCaptcha/license
Expand Down
8 changes: 5 additions & 3 deletions examples/regular-form.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* IconCaptcha Plugin: v3.0.1
* IconCaptcha Plugin: v3.1.0
* Copyright © 2022, Fabian Wennink (https://www.fabianwennink.nl)
*
* Licensed under the MIT license: https://www.fabianwennink.nl/projects/IconCaptcha/license
Expand Down Expand Up @@ -62,7 +62,7 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>IconCaptcha v3.0.1 - By Fabian Wennink</title>
<title>IconCaptcha v3.1.0 - By Fabian Wennink</title>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<meta name="author" content="Fabian Wennink © <?= date('Y') ?>" />
Expand All @@ -89,7 +89,7 @@
<div class="shields">
<div class="shields-row">
<a href="https://github.com/fabianwennink/IconCaptcha-Plugin-jQuery-PHP/releases" target="_blank" rel="noopener">
<img src="https://img.shields.io/badge/Version-3.0.1-orange.svg?style=flat-square" alt="Version 3.0.1 Badge" />
<img src="https://img.shields.io/badge/Version-3.1.0-orange.svg?style=flat-square" alt="Version 3.1.0 Badge" />
</a>
<a href="https://github.com/fabianwennink/IconCaptcha-Plugin-jQuery-PHP/blob/master/LICENSE" target="_blank" rel="noopener">
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square" alt="License-MIT Badge" />
Expand Down Expand Up @@ -215,6 +215,8 @@
// console.log('Event: Captcha refreshed', e.detail.captchaId);
// }).bind('invalidated', function(e) {
// console.log('Event: Invalidated', e.detail.captchaId);
// }).bind('reset', function(e) {
// console.log('Event: Reset', e.detail.captchaId);
// }).bind('success', function(e) {
// console.log('Event: Correct input', e.detail.captchaId);
// }).bind('error', function(e) {
Expand Down
6 changes: 3 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* IconCaptcha Plugin: v3.0.1
* IconCaptcha Plugin: v3.1.0
* Copyright © 2022, Fabian Wennink (https://www.fabianwennink.nl)
*
* Licensed under the MIT license: https://www.fabianwennink.nl/projects/IconCaptcha/license
Expand All @@ -9,7 +9,7 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>IconCaptcha v3.0.1 - By Fabian Wennink</title>
<title>IconCaptcha v3.1.0 - By Fabian Wennink</title>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<meta name="author" content="Fabian Wennink © <?= date('Y') ?>" />
Expand All @@ -28,7 +28,7 @@
<div class="shields">
<div class="shields-row">
<a href="https://github.com/fabianwennink/IconCaptcha-Plugin-jQuery-PHP/releases" target="_blank" rel="noopener">
<img src="https://img.shields.io/badge/Version-3.0.1-orange.svg?style=flat-square" alt="Version 3.0.1 Badge" />
<img src="https://img.shields.io/badge/Version-3.1.0-orange.svg?style=flat-square" alt="Version 3.1.0 Badge" />
</a>
<a href="https://github.com/fabianwennink/IconCaptcha-Plugin-jQuery-PHP/blob/master/LICENSE" target="_blank" rel="noopener">
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square" alt="License-MIT Badge" />
Expand Down
2 changes: 1 addition & 1 deletion src/captcha-request.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* IconCaptcha Plugin: v3.0.1
* IconCaptcha Plugin: v3.1.0
* Copyright © 2022, Fabian Wennink (https://www.fabianwennink.nl)
*
* Licensed under the MIT license: https://www.fabianwennink.nl/projects/IconCaptcha/license
Expand Down
38 changes: 22 additions & 16 deletions src/captcha-session.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* IconCaptcha Plugin: v3.0.1
* IconCaptcha Plugin: v3.1.0
* Copyright © 2022, Fabian Wennink (https://www.fabianwennink.nl)
*
* Licensed under the MIT license: https://www.fabianwennink.nl/projects/IconCaptcha/license
Expand All @@ -11,13 +11,16 @@

class CaptchaSession
{
const ICON_CAPTCHA = 'iconcaptcha';

/**
* @var string The captcha identifier.
*/
protected $id;

/**
* @var string The name/key of the session.
*/
protected $key;

/**
* @var array The session data.
*/
Expand All @@ -27,11 +30,13 @@ class CaptchaSession
* Creates a new CaptchaSession object. Session data regarding the
* captcha (given identifier) will be stored and can be retrieved when necessary.
*
* @param string $key The name of the session key.
* @param int $id The captcha identifier.
*/
public function __construct($id = 0)
public function __construct($key, $id = 0)
{
$this->id = $id;
$this->key = $key;

// Try to load the captcha data from the session, if any data exists.
$this->load();
Expand All @@ -56,25 +61,25 @@ public function clear()
*/
public function destroy()
{
unset($_SESSION[self::ICON_CAPTCHA][$this->id]);
unset($_SESSION[$this->key][$this->id]);
}

/**
* Loads the captcha's session data based on the earlier set captcha identifier.
*/
public function load()
{
if (self::exists($this->id)) {
$this->session = $_SESSION[self::ICON_CAPTCHA][$this->id];
if (self::exists($this->key, $this->id)) {
$this->session = $_SESSION[$this->key][$this->id];
} else {
$this->session = [
'icons' => [], // The correct icon position.
'iconIds' => [], // List of all used icon IDs.
'correctId' => 0, // The correct icon ID.
'mode' => 'light', // The captcha's icon color name.
'requested' => false, // If the icons image has been requested by the captcha.
'icons' => [], // The positions of the icon on the generated image.
'iconIds' => [], // List of used icon IDs.
'correctId' => 0, // The icon ID of the correct answer/icon.
'mode' => 'light', // The name of the theme used by the captcha instance.
'requested' => false, // If the captcha image has been requested yet.
'completed' => false, // If the captcha was completed (correct icon selected) or not.
'attempts' => 0, // The number of times the captcha has failed.
'attempts' => 0, // The number of times an incorrect answer was given.
];
}
}
Expand All @@ -85,19 +90,20 @@ public function load()
public function save()
{
// Write the data to the session.
$_SESSION[self::ICON_CAPTCHA][$this->id] = $this->session;
$_SESSION[$this->key][$this->id] = $this->session;
}

/**
* Checks if the given captcha identifier has session data stored.
*
* @param string $key The name of the session key.
* @param int $id The captcha identifier.
*
* @return boolean TRUE if any session data exists, FALSE if not.
*/
public static function exists($id)
public static function exists($key, $id)
{
return isset($_SESSION[self::ICON_CAPTCHA][$id]);
return isset($_SESSION[$key][$id]);
}

/**
Expand Down
30 changes: 23 additions & 7 deletions src/captcha.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* IconCaptcha Plugin: v3.0.1
* IconCaptcha Plugin: v3.1.0
* Copyright © 2022, Fabian Wennink (https://www.fabianwennink.nl)
*
* Licensed under the MIT license: https://www.fabianwennink.nl/projects/IconCaptcha/license
Expand Down Expand Up @@ -112,7 +112,22 @@ public static function token()
if (!isset($_SESSION[self::SESSION_NAME], $_SESSION[self::SESSION_NAME][self::SESSION_TOKEN])) {

// Create a secure captcha session token.
$token = bin2hex(openssl_random_pseudo_bytes(self::CAPTCHA_TOKEN_LENGTH));
if (function_exists('random_bytes')) {
// Only available for PHP 7 or higher.
try {
$token = bin2hex(random_bytes(self::CAPTCHA_TOKEN_LENGTH));
} catch (\Exception $e) {
// Using a fallback in case of an exception.
$token = str_shuffle(md5(uniqid(rand(), true)));
}
} elseif (function_exists('openssl_random_pseudo_bytes')) {
// Only available when the OpenSSL extension is installed.
$token = bin2hex(openssl_random_pseudo_bytes(self::CAPTCHA_TOKEN_LENGTH));
} else {
// If not on PHP 7+ or having the OpenSSL extension installed, use this fallback.
$token = str_shuffle(md5(uniqid(rand(), true)));
}

$_SESSION[self::SESSION_NAME][self::SESSION_TOKEN] = $token;
}

Expand Down Expand Up @@ -246,7 +261,8 @@ public static function validateSubmission($post)
}

// Check if the captcha ID is set.
if (!isset($post[self::CAPTCHA_FIELD_ID]) || !is_numeric($post[self::CAPTCHA_FIELD_ID]) || !CaptchaSession::exists($post[self::CAPTCHA_FIELD_ID])) {
if (!isset($post[self::CAPTCHA_FIELD_ID]) || !is_numeric($post[self::CAPTCHA_FIELD_ID])
|| !CaptchaSession::exists(self::SESSION_NAME, $post[self::CAPTCHA_FIELD_ID])) {
self::setErrorMessage(4, self::$options['messages']['invalid_id']);
return false;
}
Expand Down Expand Up @@ -515,7 +531,7 @@ public static function invalidateSession($identifier)
private static function createSession($identifier = 0)
{
// Load the captcha session for the current identifier.
self::$session = new CaptchaSession($identifier);
self::$session = new CaptchaSession(self::SESSION_NAME, $identifier);

// If the general captcha options haven't been loaded/set, load them from the session.
self::getOptions();
Expand Down Expand Up @@ -544,7 +560,7 @@ public static function validateToken($payloadToken, $headerToken = null)
$sessionToken = self::getToken();

// If the token is empty but the option is enabled, the token was never requested.
if(empty($sessionToken)) {
if (empty($sessionToken)) {
return false;
}

Expand Down Expand Up @@ -607,8 +623,8 @@ private static function calculateIconAmounts($iconCount, $smallestIconCount = 1)
if ($left > $smallestIconCount && $right > $smallestIconCount) {
return [$left, $right];
}
} // If no decimals: only return the divided numbers if it is larger than the smallest number.
else if ($pickDivided === true && $remainderDivided > $smallestIconCount) {
} elseif ($pickDivided === true && $remainderDivided > $smallestIconCount) {
// If no decimals: only return the divided numbers if it is larger than the smallest number.
return [$remainderDivided, $remainderDivided];
}

Expand Down

0 comments on commit a622b86

Please sign in to comment.