<?php
/**
 * @package     Joomla.Plugin
 * @subpackage  Captcha
 *
 * @copyright   Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

/**
 * Simplecaptcha Plugin.
 */
class PlgCaptchaSimplecaptcha extends JPlugin
{
	/**
	 * Load the language file on instantiation.
	 *
	 * @var    boolean
	 * @since  3.1
	 */
	protected $autoloadLanguage = true;

	/**
	 * Initialise the captcha
	 *
	 * @param   string  $id  The id of the field.
	 *
	 * @return  Boolean	True on success, false otherwise
	 *
	 * @throws  Exception
	 *
	 * @since  2.5
	 */
	public function onInit($id = 'challenge')
	{
		return true;
	}

	/**
	 * Gets the challenge HTML
	 *
	 * @param   string  $name   The name of the field.
	 * @param   string  $id     The id of the field.
	 * @param   string  $class  The class of the field. This should be passed as
	 *                          e.g. 'class="required"'.
	 *
	 * @return  string  The HTML to be embedded in the form.
	 *
	 * @since  2.5
	 */
	public function onDisplay($name = 'challenge', $id = 'challenge', $class = 'required')
	{	
		return '<div class=' . $this->params->get("classquestion")  . '>' . $this->params->get("question") . '</div><input class=' . $this->params->get("classresponse")  . ' type="captcha" validate="captcha" name=' . $name . ' id=' . $id . '>';
	}

	/**
	 * Calls an HTTP POST function to verify if the user's guess was correct
	 *
	 * @param   string  $code  Answer provided by user.
	 *
	 * @return  True if the answer is correct, false otherwise
	 *
	 * @since  2.5
	 */
	public function onCheckAnswer($code)
	{
		$input       = JFactory::getApplication()->input;
		$response	 = $this->params->get('response');
		if ($code == $response)
		{
			return true;
		}
		else
		{
			$this->_subject->setError(JText::_('PLG_SIMPLECAPTCHA_ERROR'));
			return false;
		}

	}
	
}