<?PHP
//================================================================================
// PLEASE DO NOT REMOVE THIS HEADER!!!
//
// COPYRIGHT NOTICE
// This script is licensed under the GPL version 2 or (at your option) any later version.
// 
// Copyright 2007-2008 Alias 454 Studios and Brandon Keep (c) All rights reserved.
// Created 04/12/2007
//
// Brandon Keep,    http://www.openautoclassifieds.com
//                  http://www.alias454studios.com/scripts/
//
// Last Modified 04-12-2008 by
// Brandon Keep,    http://alias454studios.com
//
// Original code Information. This script was modified from existing code:
// Original Author: Simon Jarvis
// Original Copyright: 2006 Simon Jarvis
// Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
//================================================================================
// This software IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//================================================================================

require_once './Includes/configs/functions.php';
require_once './Includes/libs/Smarty.class.php';
include_once "./Includes/language/$site_lang.php";

$font = './Includes/fonts/monofont.ttf';

$width = isset($_GET['width']) && $_GET['height'] < 600 ? $_GET['width'] : '120';
$height = isset($_GET['height']) && $_GET['height'] < 200 ? $_GET['height'] : '40';
$len = isset($_GET['len']) && $_GET['characters'] > 2 ? $_GET['len'] : '6';

function generateCaptcha($len)
{
	// list all possible characters, similar looking characters and have been removed */
    $possible = '23456789abcdefghjkmnpqrstvwxyzABCDEFGHJKLMNPQRSTVWXYZ';
    $code = '';
    $i = 0;
	while ($i < $len) { 
    	$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
		$i++;
    }
	return $code;
}

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

// set the colours
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);

// 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, $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, $font , $code);// or die('Error in imagettftext function');

// output captcha image to browser
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION["security_code"] = $code;
?>