<?php
/**
* @version SVN: $Id$
* @package FormCalc
* @subpackage Base
* @author EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
* @author Constantine Poltyrev {@link http://bugs.rallycars.ru}
* @author Created on 06-Dec-2010
*/
//-- No direct access
defined('_JEXEC') or die('=;)');
jimport('joomla.application.component.controller');
/**
* FormCalc default Controller
*
* @package FormCalc
* @subpackage Controllers
*/
class FormCalcController extends JController
{
/**
* Method to display the view
*
* @access public
*/
function display()
{
parent::display();
}// function
function checkCaptcha()
{
$o = new StdClass;
$o->success = true;
if(($captcha = JRequest::getString('captcha', '-none-')) != '-none-' && $captcha != JFactory::getApplication()->getUserState('security_code'))
$o->success = false;
echo json_encode($o);
}
function submit()
{
$backLink = JRoute::_('index.php?option=com_formcalc&formid='.JRequest::getInt('formid').'&Itemid='.JRequest::getInt('Itemid'), false);
if(!JRequest::checkToken())
{
$this->setRedirect($backLink, JText::_('Invalid form token'));
return;
}
if(($captcha = JRequest::getString('captcha', '-none-')) != '-none-')
{
if($captcha != JFactory::getApplication()->getUserState('security_code')) {
$this->setRedirect($backLink, JText::_('Invalid captcha input'));
JFactory::getApplication()->setUserState('security_code', null);
return;
}
JFactory::getApplication()->setUserState('security_code', null);
}
/** @var FormCalcModelFormCalc $model */
$model = $this->getModel('formcalc');
$msg = '';
if(!$model->submitData())
$msg = JError::getError()->getMessage();
$this->setRedirect(JRoute::_('index.php?option=com_formcalc&view=submit&Itemid='.JRequest::getInt('Itemid'), false), $msg);
/* $o = new StdClass;
$o->success = true;
$o->redirect = JRoute::_('index.php?option=com_formcalc&view=submit');
echo json_encode($o);*/
}
function generateCode($characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
$possible = '23456789bcdfghjkmnpqrstvwxyz';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}
function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
$code = $this->generateCode($characters);
JFactory::getApplication()->setUserState('security_code', $code);
$this->font = JPATH_COMPONENT.DS.'lib'.DS.'monofont.ttf';
//echo $this->font;
/* 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, $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');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
JFactory::getApplication()->close();
}
}// class