<?php

//fetch the session variable for the captcha code
/*
define( '_JEXEC', 1 );
chdir("../../../../../");
getcwd();
define('JPATH_BASE', getcwd() );

define( 'DS', DIRECTORY_SEPARATOR );


	global $mainframe;
	require_once(JURI::root().'includes'.DS.'defines.php');
echo DS;
*/
define( '_JEXEC', 1 );
chdir("../../../");
getcwd();
define('JPATH_BASE', getcwd() );

//define('JPATH_BASE', dirname(__FILE__) );

define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

JDEBUG ? $_PROFILER->mark( 'afterLoad' ) : null;

/**
 * CREATE THE APPLICATION
 *
 * NOTE :
 */
$mainframe =& JFactory::getApplication('site');

	$mainframe =& JFactory::getApplication('site');
	$mainframe->initialise();
	$database =& JFactory::getDBO();

	// session_start();

	/*===============================================================
		General captcha settings
	  ===============================================================*/
	// captcha width
	$captcha_w = 150;
	// captcha height
	$captcha_h = 50;
	// minimum font size; each operation element changes size
	$min_font_size = 12;
	// maximum font size
	$max_font_size = 18;
	// rotation angle
	$angle = 20;
	// background grid size
	$bg_size = 13;
	// path to font - needed to display the operation elements
	//$font_path = 'fonts/courbd.ttf';
	//$font_path = JPATH_ROOT.DS.'modules'.DS.'mod_ccnewsletter'.DS.'tmpl'.DS.'fonts'.DS.'courbd.ttf';
	$font_path = JPATH_ROOT.DS.'media'.DS.'fonts'.DS.'courbd.ttf';
	// array of possible operators
	$operators=array('+','-','*');
	// first number random value; keep it lower than $second_num
	$first_num = rand(1,5);
	// second number random value
	$second_num = rand(6,11);

	/*===============================================================
		From here on you may leave the code intact unless you want
		or need to make it specific changes.
	  ===============================================================*/

	shuffle($operators);
	$expression = $second_num.$operators[0].$first_num;
	/*
		operation result is stored in $session_var
	*/
	eval("\$session_var=".$second_num.$operators[0].$first_num.";");
	/*
		save the operation result in session to make verifications
	*/

	$session =& JFactory::getSession();
	$session->set('security_number', $session_var);

	/*
		start the captcha image
	*/
	$img = imagecreate( $captcha_w, $captcha_h );
	/*
		Some colors. Text is $black, background is $white, grid is $grey
	*/
	$black = imagecolorallocate($img,0,0,0);
	$white = imagecolorallocate($img,255,255,255);
	$grey = imagecolorallocate($img,215,215,215);
	/*
		make the background white
	*/
	imagefill( $img, 0, 0, $white );
	/* the background grid lines - vertical lines */
	for ($t = $bg_size; $t<$captcha_w; $t+=$bg_size){
		imageline($img, $t, 0, $t, $captcha_h, $grey);
	}
	/* background grid - horizontal lines */
	for ($t = $bg_size; $t<$captcha_h; $t+=$bg_size){
		imageline($img, 0, $t, $captcha_w, $t, $grey);
	}

	/*
		this determinates the available space for each operation element
		it's used to position each element on the image so that they don't overlap
	*/
	$item_space = $captcha_w/3;

	/* first number */
	imagettftext(
		$img,
		rand(
			$min_font_size,
			$max_font_size
		),
		rand( -$angle , $angle ),
		rand( 10, $item_space-20 ),
		rand( 25, $captcha_h-25 ),
		$black,
		$font_path,
		$second_num);

	/* operator */
	imagettftext(
		$img,
		rand(
			$min_font_size,
			$max_font_size
		),
		rand( -$angle, $angle ),
		rand( $item_space, 2*$item_space-20 ),
		rand( 25, $captcha_h-25 ),
		$black,
		$font_path,
		$operators[0]);

	/* second number */
	imagettftext(
		$img,
		rand(
			$min_font_size,
			$max_font_size
		),
		rand( -$angle, $angle ),
		rand( 2*$item_space, 3*$item_space-20),
		rand( 25, $captcha_h-25 ),
		$black,
		$font_path,
		$first_num);

	/* image is .jpg */
	header("Content-type:image/jpeg");
	/* name is secure.jpg */
	header("Content-Disposition:inline ; filename=secure.jpg");
	/* output image */
	imagejpeg($img);
?>
