Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
alekorhonen committed Apr 26, 2021
1 parent 167798f commit edaadcc
Show file tree
Hide file tree
Showing 96 changed files with 353 additions and 0 deletions.
10 changes: 10 additions & 0 deletions captcha-img.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
session_start();

require("inc/captcha.class.php");


$Captcha = new Captcha();
$Captcha->getCaptcha();

?>
3 changes: 3 additions & 0 deletions icons/Icons made by Webalys.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Visit their website: https://www.webalys.com ~ http://www.streamlineicons.com

License: http://www.ego-icons.com/license.html
Binary file added icons/icon-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-26.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-27.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-28.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-29.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-31.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-32.png
Binary file added icons/icon-33.png
Binary file added icons/icon-34.png
Binary file added icons/icon-35.png
Binary file added icons/icon-36.png
Binary file added icons/icon-37.png
Binary file added icons/icon-38.png
Binary file added icons/icon-39.png
Binary file added icons/icon-4.png
Binary file added icons/icon-40.png
Binary file added icons/icon-41.png
Binary file added icons/icon-42.png
Binary file added icons/icon-43.png
Binary file added icons/icon-44.png
Binary file added icons/icon-45.png
Binary file added icons/icon-46.png
Binary file added icons/icon-47.png
Binary file added icons/icon-48.png
Binary file added icons/icon-49.png
Binary file added icons/icon-5.png
Binary file added icons/icon-50.png
Binary file added icons/icon-51.png
Binary file added icons/icon-52.png
Binary file added icons/icon-53.png
Binary file added icons/icon-54.png
Binary file added icons/icon-55.png
Binary file added icons/icon-56.png
Binary file added icons/icon-57.png
Binary file added icons/icon-58.png
Binary file added icons/icon-59.png
Binary file added icons/icon-6.png
Binary file added icons/icon-60.png
Binary file added icons/icon-61.png
Binary file added icons/icon-62.png
Binary file added icons/icon-63.png
Binary file added icons/icon-64.png
Binary file added icons/icon-65.png
Binary file added icons/icon-66.png
Binary file added icons/icon-67.png
Binary file added icons/icon-68.png
Binary file added icons/icon-69.png
Binary file added icons/icon-7.png
Binary file added icons/icon-70.png
Binary file added icons/icon-71.png
Binary file added icons/icon-72.png
Binary file added icons/icon-73.png
Binary file added icons/icon-74.png
Binary file added icons/icon-75.png
Binary file added icons/icon-76.png
Binary file added icons/icon-77.png
Binary file added icons/icon-78.png
Binary file added icons/icon-79.png
Binary file added icons/icon-8.png
Binary file added icons/icon-80.png
Binary file added icons/icon-81.png
Binary file added icons/icon-82.png
Binary file added icons/icon-83.png
Binary file added icons/icon-84.png
Binary file added icons/icon-85.png
Binary file added icons/icon-86.png
Binary file added icons/icon-87.png
Binary file added icons/icon-88.png
Binary file added icons/icon-89.png
Binary file added icons/icon-9.png
Binary file added icons/icon-90.png
Binary file added icons/icon-91.png
163 changes: 163 additions & 0 deletions inc/captcha.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

class Captcha {
protected $session = array(
'folder_path' => null,
'total_icons' => 0,
'hashes' => array(),
'icon_requests' => 0,
'correct_icon' => null,
'incorrect_icon' => null,
'noise' => 0
);

public $available = false; // Determines whether the captcha is good to use or not.

public function createSession() {
$this->incorrect_icon = 0;
$this->correct_icon = mt_rand(1, $this->session['total_icons']);

while($this->incorrect_icon === 0) {
$random = mt_rand(1, $this->session['total_icons']);

if($random !== $this->correct_icon) {
$this->incorrect_icon = $random;
}
}

$icon_array = $this->shuffleIcons();
$hash_array = array();
for($i = 0; $i < 5; $i++) {
$hash = $this->getHash("icon-" . $icon_array[$i] . "-" . $i);

$hash_array[$hash] = $icon_array[$i];
}

$this->session['hashes'] = $hash_array;
$this->session['icon_requests'] = 0;
$this->session['correct_icon'] = $this->correct_icon;
$this->session['incorrect_icon'] = $this->incorrect_icon;

$this->saveSession();
}

public function validateInput($input) {

if(empty($input) || !is_array($input) || count($input) === 0 || !isset($_SESSION['icon_captcha'])) {
return false;
}

$this->session = $_SESSION['icon_captcha'];
$correct_icons_count = 0;

foreach($input as $raw_hash) {
//Clean the hash
$hash = preg_replace("/[^a-zA-Z0-9]+/", "", $raw_hash);

//Check whether the hash exists in our current session, to avoid missing key errors
if(!array_key_exists($hash, $this->session['hashes'])) {
return false;
}

//Check whether an incorrect icon is selected
if($this->session['hashes'][$hash] === $this->session['incorrect_icon']) {
return false;
}

if($this->session['hashes'][$hash] === $this->session['correct_icon']) {
$correct_icons_count++;
}
}

if($correct_icons_count < 2) {
return false;
}

return true;
}

public function getCaptcha() {

header('Content-type: image/png');
header('Expires: 0');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');

if($_SESSION['icon_captcha']['icon_requests'] > 0 || !isset($_SESSION['icon_captcha'])) {
return;
}

$this->generateSprite();

$this->session = $_SESSION['icon_captcha']; // Get the current session
$this->session['icon_requests'] = 1; // Update the session values
$this->saveSession();
}

public function generateSprite() {
// Create the size of image or blank image
$sprite = imagecreate(150, 30);

// Set the background color of image
$background_color = imagecolorallocate($sprite, 255, 255, 255);

$session = $_SESSION['icon_captcha'];
$foreach_index = 0;
foreach($session['hashes'] as $hash => $icon_index) {
$tmp = imagecreatefrompng($session['folder_path'] . "/icon-" . ($icon_index) . ".png");
imagecopy($sprite, $tmp, ($foreach_index * 30), 0, 0, 0, 30, 30);
imagedestroy($tmp);

$foreach_index++;
}

if($session['noise'] > 0) {
$noise_color = imagecolorallocatealpha($sprite, 0, 0, 0, 126);

// Add some random pixels to the icon
for ($i = 0; $i < $session['noise']; $i++) {
$randX = mt_rand(0, 150);
$randY = mt_rand(0, 30);

imagesetpixel($sprite, $randX, $randY, $noise_color);
}
}

imagepng($sprite);
imagedestroy($sprite);
}

protected function shuffleIcons() {
$hashes = array($this->incorrect_icon, $this->incorrect_icon, $this->incorrect_icon, $this->correct_icon, $this->correct_icon);
shuffle($hashes);
return $hashes;
}

protected function getHash($image = null) {
return hash('tiger192,3', $image . hash('crc32b', uniqid('ic_', true)));
}

public function setIconsFolderPath($folder_path) {
$files = glob($folder_path . "/*.png");
if($files) {
$this->session['folder_path'] = $folder_path;
$this->session['total_icons'] = count($files);
}
}

public function addNoise($amount) {
$this->session['noise'] = (int)$amount;
}

public function hashArray() {
return $this->session['hashes'];
}

public function saveSession() {
$_SESSION['icon_captcha'] = $this->session;
}

}

?>
96 changes: 96 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

session_start();

require("inc/captcha.class.php");

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./style.css" type="text/css" />
<title>Icon Captcha</title>
<style>
.captcha-icon {
background-image: url('captcha-img.php');
background-repeat: no-repeat;
width: 30px;
height: 30px;
}

.captcha-icon.icon-1 {
background-position: 0 0;
}

.captcha-icon.icon-2 {
background-position: -30px 0;
}

.captcha-icon.icon-3 {
background-position: -60px 0;
}

.captcha-icon.icon-4 {
background-position: -90px 0;
}

.captcha-icon.icon-5 {
background-position: -120px 0;
}
</style>
</head>
<body>
<div class="container">
<?php

$Captcha = new Captcha();
$Captcha->setIconsFolderPath("./icons");
$Captcha->addNoise(1000);

if(isset($_POST['submit'])) {
if(!isset($_POST['captcha']) || !$Captcha->validateInput($_POST['captcha'])) {
echo "Invalid captcha";
}
}

$Captcha->createSession();
$captcha_values = $Captcha->hashArray();

?>
<form action="" method="POST">
<div class="captcha-holder">
<div class="captcha-title">
Select the image(s) that do not belong in the row
</div>
<div class="captcha-icons">
<?php
$index = 1;
foreach($captcha_values as $hash => $image):
?>

<label class="captcha-selector" for="icon-<?php echo $index; ?>">
<div class="captcha-checkbox">
<input type="checkbox" id="icon-<?php echo $index; ?>" name="captcha[]" value="<?php echo $hash; ?>">
<span class="check"></spam>
</div>
<div class="captcha-icon icon-<?php echo $index; ?>"></div>
</label>

<?php
$index++;
endforeach;

?>
</div>
</div>
<div style="margin-top: 1em; text-align: center">
<button type="submit" name="submit">Validate captcha</button>
</div>
</form>
</div>
</body>
</html>
81 changes: 81 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
html, body {
margin: 0;
padding: 0;
}

body {
background: #cccccc;
color: #171717;
font-size: 16px;
}

.container {
padding: 10px;
background: #ffffff;
position: absolute;
left: 0;
right: 0;
top: 50%;
width: 400px;
margin: 0 auto;
}

.container .captcha-holder {
margin: 0 auto;
}

.captcha-holder {
position: relative;
max-width: 323px;
padding: 5px;
padding-bottom: 0;
border: 1px solid rgba(0, 0, 0, 0.2);
}

.captcha-holder .captcha-title {
padding: 5px;
text-align: center;
font-size: 14px;
border-bottom: 1px solid rgba(0, 0, 0, 0.2);
}

.captcha-holder .captcha-icons {
display: flex;
flex-direction: row;
width: 100%;
}

.captcha-holder .captcha-icons .captcha-selector {
position: relative;
flex-basis: 20%;
text-align: center;
padding: 10px 0;
cursor: pointer;
}

.captcha-holder .captcha-icons .captcha-selector .captcha-icon {
margin: 0 auto;
}

.captcha-holder .captcha-icons .captcha-selector .captcha-checkbox {
position: absolute;
bottom: 0;
width: 100%;
height: 3px;
}

.captcha-holder .captcha-selector .captcha-checkbox input[type=checkbox] {
display: none;
}

.captcha-holder .captcha-selector .captcha-checkbox .check {
position: absolute;
width: 100%;
height: 3px;
left: 0;
bottom: 0;
}

.captcha-holder .captcha-selector .captcha-checkbox input[type=checkbox]:checked+span {
background: dodgerblue;
}

0 comments on commit edaadcc

Please sign in to comment.