<?php
/*
Script Check
*/
class ScriptCheck {
var $image_dir = './scriptcheck/';
var $extensions = array();
var $images = array();
var $_instance;
var $watermark = 'watermark.png';
var $watermark_size = null;
var $tempimagesdir = './scriptcheck/temp';
var $types = array ('nature', 'technology');
var $max_correct_percent = 0.6;
function ScriptCheck($image_dir = null, $extensions = null, $padding = null) {
session_start();
$this->image_dir = $image_dir ? $image_dir : dirname(__FILE__);
$this->image_dir = rtrim($this->image_dir, '/');
$this->watermark = $this->image_dir.'/'.$this->watermark;
$this->extensions = $extensions ? $extensions : array ('jpg');
$this->padding = $padding ? (int) $padding : 5;
$this->scanDirForImages();
$this->watermark_size = getimagesize($this->watermark);
}
function scanDirForImages() {
if(!file_exists($this->image_dir) || !is_dir($this->image_dir)) {
return false;
}
elseif(is_readable($this->image_dir)) {
$directory_list = opendir($this->image_dir);
while (false !== ($file = readdir($directory_list))) {
if($file != '.' && $file != '..') {
$path = $this->image_dir.'/'.$file;
if(is_readable($path)) {
$subdirectories = explode('/', $path);
if(is_file($path)) {
$ext = explode('.', end($subdirectories));
$extension = end($ext);
if(in_array($extension, $this->extensions) && in_array($ext[1], $this->types)) {
$this->images[$ext[1]][] = $path;
}
}
}
}
}
closedir($directory_list);
foreach ($this->types as $key => $type) {
if (!is_array($this->images[$type]) || count($this->images[$type]) < 1) {
unset($this->images[$type]);
unset($this->types[$key]);
}
}
return true;
}else{
return false;
}
}
function deleteImages($files) {
foreach ($files as $file => $temp) {
@unlink($this->tempimagesdir.'/'.$file.'.png');
}
}
function showImages() {
if (is_array($_SESSION['scrpt_images'])) {
return $this->outputHTML();
}
$images = $this->images;
$types = $this->types;
$selected = array();
$total = 8;
$total_types = count($types);
$correct_type = mt_rand(0, $total_types-1);
$correct_total = count($images[$types[$correct_type]]);
$correct_amount = mt_rand(1, round($total*$this->max_correct_percent)-1);
$_SESSION['correct_type'] = $types[$correct_type];
$_SESSION['correct_amount'] = $correct_amount;
for ($i=0;$i<$correct_amount;$i++) {
$img = mt_rand(0, $correct_total-1);
$image = $images[$types[$correct_type]][$img];
$selected[] = $this->processImage($image, true);
$correct_total--;
unset($images[$types[$correct_type]][$img]);
$images[$types[$correct_type]] = @array_values($images[$types[$correct_type]]);
}
unset($images[$types[$correct_type]]);
unset($types[$correct_type]);
$types = array_values($types);
$total_types--;
for ($i=0;$i<($total-$correct_amount);$i++) {
$type = mt_rand(0, $total_types-1);
$img = mt_rand(0, count($images[$types[$type]])-1);
$selected[] = $this->processImage($images[$types[$type]][$img], false);
unset($images[$types[$type]][$img]);
$images[$types[$type]] = @array_values($images[$types[$type]]);
}
$_SESSION['scrpt_images'] = $this->shuffleImages($_SESSION['scrpt_images']);
return $this->outputHTML();
}
function outputHTML() {
include($this->image_dir.'/show.php');
}
function shuffleImages ($array) {
while (count($array) > 0) {
$val = array_rand($array);
$new_arr[$val] = $array[$val];
unset($array[$val]);
}
return $new_arr;
}
function processImage($image_path, $add = true) {
$type = getimagesize($image_path);
$open = null;
switch ($type[2]) {
case IMAGETYPE_GIF:
$open = imagecreatefromgif($image_path);
break;
case IMAGETYPE_JPEG:
$open = imagecreatefromjpeg($image_path);
break;
case IMAGETYPE_PNG:
$open = imagecreatefrompng($image_path);
break;
default:
return false;
break;
}
$size = getimagesize($image_path);
$max_x = $size[0] - $this->watermark_size[0] - 5;
$max_y = $size[1] - $this->watermark_size[1] - 5;
$min_x = 5;
$min_y = 5;
$x = mt_rand($min_x, $max_x);
$y = mt_rand($min_y, $max_y);
$water = imagecreatefrompng($this->watermark);
imageAlphaBlending($water, false);
imageSaveAlpha($water, true);
imagecopy($open, $water, $x, $y, 0, 0, $this->watermark_size[0], $this->watermark_size[1]);
imagedestroy($water);
$name = md5(microtime());
imagepng($open, $this->tempimagesdir.'/'.$name.'.png');
if ($add) {
$_SESSION['scrpt_images'][$name] = 1;
}
else {
$_SESSION['scrpt_images'][$name] = 0;
}
imagedestroy($open);
return $name;
}
function testImages($images) {
$match = true;
if (!$_SESSION['scrpt_images']) {
return false;
}
foreach ($images as $image => $key) {
if ($_SESSION['scrpt_images'][$image] != (int)$key) {
$match = false;
}
}
$this->deleteImages($_SESSION['scrpt_images']);
unset($_SESSION['scrpt_images']);
return $match;
}
/*static function getInstance($image_dir = null, $extensions = null, $padding = null) {
if (null === self::$_instance) {
self::$_instance = new self($image_dir, $extensions, $padding);
}
return self::$_instance;
}*/
}
ob_end_clean();
if ($_POST['images'])
{
$scriptcheck = new ScriptCheck('./scriptcheck/');
$check = $scriptcheck->testImages($_POST['images']);
if ($check)
{
$newtime = time() + (60 * 10);
mysql_query("UPDATE user_info SET last_script='$newtime' WHERE username='$username'");
// echo "<div class='error'>Well done!</div>";
echo "<script>document.location=document.location</script>";
}
else
{
echo "<div class='error'>Wrong!</div>";
}
// echo ($check?'Well Done!':'Nuhuh');
// $scriptcheck->showImages();
}
else {
$scriptcheck = new ScriptCheck('./scriptcheck/');
$scriptcheck->showImages();
}
?>