<?php
//+-----------------------------------------------------+//
//+ Copyright Floopo.com
//+ captcha.php
//+ Generate and validates captcha image
//+-----------------------------------------------------+//

class CaptchaSecurityImages {


    //________Font to be used in image________________________
    var $font;
    var $captcha_code;
    


    //________Start Here______________________________________
    function CaptchaSecurityImages($width = '120', $height = '40', $characters = '6') {


        $this->font = BASEPATH.'/com/com_captcha/verdanab.ttf';
        $code = $this->generateCode($characters);

        /* font size will be 75% of the image height */
        $font_size = $height * 0.50;
        $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');

        /* set the colours */
        $background_color = imagecolorallocate($image, 255, 255, 255);

        /* black text */
        $text_color = imagecolorallocate($image, 1, 1, 1);

        $rndR = rand(98, 198);
        $rndG = rand(98, 198);
        $rndB = rand(98, 198);
        $noise_color = imagecolorallocate($image, $rndR, $rndG, $rndB);

        /* generate random dots in background */
        for ($i = 0; $i < ($width * $height) / 3; $i++) {
            imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color);
        }

        /* generate random lines in background */
        for ($i = 0; $i < ($width * $height) / 150; $i++) {
            imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
                mt_rand(0, $height), $noise_color);
        }

        /* create textbox and add text */
        $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
        $x = ($width - $textbox[4]) / 2;
        $y = ($height - $textbox[5]) / 2;
        imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $code) or
            die('Error in imagettftext function');

        // Show the image.
        if (function_exists('imagegif')) {
            header('Content-type: image/gif');
            imagegif($image);
        }
        else {
            header('Content-type: image/png');
            imagepng($image);
        }

        // Bail out.
        imagedestroy($image);
        
        
        // Save the code to a session
        $this->SavetoSession();

    }



    //________Generate Random Code________________________
    function generateCode($characters) {
        /* list all possible characters, similar looking characters and vowels have been removed */
        $possible = 'BCDFGHJKMNPQRSTVWXYZ23456789bcdfghjkmnpqrstvwxyz';
        $code = '';
        $i = 0;
        while ($i < $characters) {
            $code .= substr($possible, mt_rand(0, strlen($possible) - 1), 1);
            $i++;
        }
        $this->captcha_code = $code;
        return $code;
    }



    //________Save to a sessions________________________
    function SavetoSession() {
        //Save this to a session
        @session_start();
        @session_register('com_captcha');
        $_SESSION['com_captcha'] = strtolower($this->captcha_code);
        die();
    }

}



//________Run this Class________________________
$width = '132';
$height = '36';
$characters = '6';

$captcha = new CaptchaSecurityImages($width, $height, $characters);


/*==================================================================================================================

DESCRIPTION:
------------
This component will out put a captcha image and will also store the captcha code as a $_SESSION['com_captcha'];


DEPEDENCIES:
------------
Constant : BASEPATH to be able to load the font file.


USAGE:
-------------

Use it as a normal image in template file and then check the posted data against the sessions var.
        
		//in html
        <img src="[var.env_base_url]/index.php?option=com_captcha" alt="captcha" />


        //in php - check captca (always turn to lower)
        if (strtolower($_POST['captcha']) == $_SESSION['com_captcha'])

================================================================================================================*/






?>