<?php 
/* utf8-marker = äöü
==========================================
CMSimple Plugin Comments
Version comments 4.17
Release: 2024-12
Gert Ebersbach
ge-webdesign.de
==========================================
*/

header ("Content-type: image/png");
session_start();
include('config/config.php');

//lese zufälliges hintergrundbild (*.png) und zufälligen Font (*.ttf)
$randomvalue = 0;
$randompicture = array();
$randomfont = array();
$picturedir = 'captcha';

$k=0;
$f=0;
if ($picDir=opendir ($picturedir)) 
{
	while ($file = readdir ($picDir)) 
	{
		if ($file != '.' && $file != '..') 
		{
			if (stripos($file, '.png') !== false ) 
			{
				$randompicture[$k] = $file;
				$k++;
			}
			if (stripos($file, '.ttf') !== false ) 
			{
				$randomfont[$f] = $file;
				$f++;
			}
		}
	}
}

closedir($picDir);

$randomvalue = mt_rand(0, count($randompicture)-1);
$imageFile = getcwd() . '/captcha/' . $randompicture[$randomvalue];

$randomvalue = mt_rand(0, count($randomfont)-1);
$fontFile = getcwd() . '/captcha/' . $randomfont[$randomvalue];

if($plugin_cf['comments']['use_graphic_captcha'] == "true")
{
	//Hintergrundbild
	$im = ImageCreateFromPNG($imageFile); 

	// Farben Hintergrund und Text, Font, Position, Grösse, Winkel
	$color = ImageColorAllocate($im, 110, 120, 130);
	$ttf = $fontFile;
	$ttfsize = 32;
	$angle = rand(-2,4);
	$t_x = rand(10,30);
	$t_y = 48;

	// Zufallscode erzeugen (6 Zeichen A-Z,2-9) 
	for ($text = '', $i = 0, $z = strlen($a = 'ABCDEFGHKLMNPRSTUWXYZ123456789')-1; $i != 5; $x = rand(0,$z), $text.= $a[$x], $i++); 

	// Schreibe die Zeichenkette ins Bild
	imagettftext($im, $ttfsize, $angle, $t_x, $t_y, $color, $ttf, $text);

	// image filtern
	imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
}
else
{
	$im = @ImageCreate (95, 20) or die ("Kann keinen neuen GD-Bild-Stream erzeugen");
	$background_color = ImageColorAllocate ($im, 24, 24, 36);
	$x_cord = 30;
	$y_cord = 18;
	$text_color = ImageColorAllocate ($im, 160, 160, 160);

	// Zufallscode erzeugen (6 Zeichen A-Z,2-9) 
	for ($text = '', $i = 0, $z = strlen($a = 'ABCDEFGHKLMNPRSTUWXYZ123456789')-1; $i != 5; $x = rand(0,$z), $text.= $a[$x], $i++); 

	// Erzeugten Zufallscode in Bild schreiben 
	imagestring ( $im, 5, 9, 2, $text, $text_color );
}

// OKI: imagestring ( resource im, int font, int x, int y, string s, int col );
ImagePNG ($im);
imagedestroy($im);

// Erzeugten Zufallscode in $_SESSION schreiben 
$_SESSION['captcha']= $text;
?>