Your IP : 216.73.216.201


Current Path : /home/sunflowe/www2/j15/components/com_easycaptcha/
Upload File :
Current File : /home/sunflowe/www2/j15/components/com_easycaptcha/class.easycaptcha.php

<?php
/**
* This file provides the EasyCaptcha class
* @version $Id: class.easycaptcha.php 656 2008-04-05 09:17:38Z snipersister $
* @package EasyCaptcha
* @license Released under the terms of the GNU General Public License (see LICENSE.php in the Joomla! root directory)
* @author EasyJoomla.org - Project
**/

defined('_JEXEC') or die('Restricted access');
// Define includepath
defined('ABSOLUTEPATH') or define( 'ABSOLUTEPATH', dirname(__FILE__) );

/**
* EasyCaptcha, a class which provides the possibility to include a captcha image in 3rd-party extensions.
* The class is developed to be used inside extensions for the Joomla! CMS version 1.5.x
* @package EasyCaptcha
* @author David Jardin
* @copyright David Jardin
**/
class EasyCaptcha
{
	/**
	 * @var object Captcha parameters
	 */
	var $_captcha_params;
	/**
	 * @var string Captcha name
	 */
	var $captcha_name;
	/**
	 * @var integer Captcha id
	 */
	var $captcha_id;
	/**
	 * @var string Captcha alternative text
	 */
	var $alt_text;
	/**
	 * @var string Captcha image url
	 */
	var $image_url;
	/**
	 * @var string Correct, md5 hashed value of the captcha
	 */
	var $captcha_val;
	
	/**
	 * Constructur
	 * @param integer $captcha_id ID of the generated captcha
	 **/
	function easyCaptcha( $captcha_id=0 )
	{
		$this->getCaptchaName();
		$this->getCaptchaParams();
		$this->setCaptchaId( $captcha_id );
		$this->setAltText();
		$this->setImageUrl();
		$this->_getLanguage();
	}
	
	/**
	 * Sets the ID of the generated captcha
	 * @param integer $captcha_id Captcha ID which should be set
	 **/
	function setCaptchaId( $captcha_id )
	{
		if(!is_numeric($captcha_id) || $captcha_id == "0")
		{
		mt_srand((double)microtime()*1000000);
		$captcha_id = mt_rand($this->_captcha_params->random_min,$this->_captcha_params->random_max);
		}
		$this->captcha_id = $captcha_id;
	}
	
	/**
	 * Sets alternative Text of the image
	 * @access private
	 **/
	function setAltText()
	{
		if(isset($this->_captcha_params->AltTextFile) && $this->_captcha_params->AltTextFile == 1)
		{
			if(file_exists(ABSOLUTEPATH.DS."captchas".DS.$this->captcha_name.DS."alt.php")){
				include_once(ABSOLUTEPATH.DS."captchas".DS.$this->captcha_name.DS."alt.php");
				$this->alt_text = altText($this->captcha_id);
			}
			else
			{
				$this->alt_text = "Captcha Image";
			}
		}
		else
		{
			$this->alt_text = "Captcha Image";
		}
	}
	
	/**
	 * Sets the url of the captcha image
	 * @access private
	 **/	
	function setImageUrl()
	{
		$this->image_url = JURI::base()."components/com_easycaptcha/captchas/".$this->captcha_name."/captcha.php?captcha_id=".$this->captcha_id;	
	}
	
	/**
	 * Checks whether the user-entered code is correct
	 * @param string $entered_code User-Entered code
	 * @return bool Was entered Code correct or not
	 **/
	function checkEnteredCode($entered_code)
	{
		$this->getCodeVal();
		$entered_code = md5(strtolower($entered_code));
		if($entered_code == $this->captcha_val)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * Reads captcha parameters out of the database
	 * @return object Parameters of active captcha
	 * @access private
	 */
	function getCaptchaParams()
	{
		$db	=& JFactory::getDBO();
		$registry = new JRegistry();
		
		$db->setQuery("SELECT params FROM #__easycaptchas WHERE published = '1' LIMIT 1");
		$captcha_params = $db->loadResult();
		$registry->loadINI( $captcha_params );
		$this->_captcha_params = $registry->toObject( );		
	}
	
	 /**
	 * Reads captcha name out of the database
	 * @return string Name of active captcha
	 */
	function getCaptchaName()
	{
		$db	=& JFactory::getDBO();
		$db->setQuery("SELECT name FROM #__easycaptchas WHERE published = '1' LIMIT 1");
		$this->captcha_name = $db->loadResult();	
	}
	
	 /**
	 * Gets the md5-encoded, correct value of the captcha
	 * @return string Correct Value of the captcha (md5 encoded)
	 * @access private
	 */	
	function getCodeVal()
	{
		include_once(ABSOLUTEPATH.DS."captchas".DS.$this->captcha_name.DS."validate.php");
		$this->captcha_val = getCode($this->captcha_id);
		return $this->captcha_val;
	}
	
	 /**
	 * Gets the captcha ID
	 * @return integer Captcha ID
	 */	
	function getCaptchaId()
	{
		return $this->captcha_id;
	}
	/**
	 * Gets the reload javascript
	 * @return text Reload Javascript
	 */	
	function getReloadCode()
	{
		include_once(ABSOLUTEPATH.DS."captchas".DS.$this->captcha_name.DS."reload.php");
		return reloadCode($this);
	}
	
	/**
	 * Gets the reload button
	 * @param string $id HTML elementId of the captcha image
	 * @param string $image Imagesrc to an image which should be used as button
	 * @return text Reload Button
	 */	
	function getReloadButton($id, $image=null)
	{
		include_once(ABSOLUTEPATH.DS."captchas".DS.$this->captcha_name.DS."reload.php");
		return reloadButton($id, $image);
	}
	
	/**
	 * Gets the alternative text of the captcha-image
	 * @return string Alternative text of captcha image
	 */	
	function getAltText()
	{
		return $this->alt_text;
	}
	
	/**
	 * Generates Javascript code needed for AJAX Support
	 * @param string $onComplete Name of the JS Function which should be called after retriving the result of the request
	 * @return string Javascript Code
	 **/
	function getAjax($onComplete)
	{
?>
		<script type="text/javascript">
		/*<[CDATA[*/
		function checkEasyCaptcha (Id) {
		http_request = null;
		if (window.XMLHttpRequest) {
			http_request = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if(!http_request) {
			alert("<?php echo JTEXT::_('Could not send HTTP-Request'); ?>");
		} else {
			var url = "<?php echo JURI::base(); ?>index2.php?option=com_easycaptcha&no_html=1&captcha_id=<?php echo $this->captcha_id;?>&captcha_code="+document.getElementById(Id).value;
			http_request.open("GET",url, true);
			http_request.onreadystatechange = <?php echo $onComplete;?>;
			http_request.send(null);
		}
		return false;
	}
	/*]]>*/
	</script>
<?php
	}
	
	/**
	 * Gets the image url of the captcha-image
	 * @return string URL of captcha image
	 */	
	function getImageUrl()
	{
		return $this->image_url;
	}
	
	/**
	 * Includes needed languagefiles
	 * 
	 */	
	function _getLanguage()
	{
		$lang =& JFactory::getLanguage();
      	$lang->load( 'com_easycaptcha' );
	}
}

?>