<?php
    /* Copyright 2007, 2008 Tobias Frost <tobi@coldtobi.de> http://blog.coldtobi.de

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    */

	lt_include( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
	lt_include( PLOG_CLASS_PATH."plugins/recaptcha/library/recaptchalib.php");

	class PluginRecaptcha extends PluginBase
	{
		var $pluginEnabled;
		var $PubKey;
		var $PrivKey; 
		var $MangleKey;
		var $Theme;
		private $recaptcha_locales = array( "en","nl","fr","de","pt","ru","es","tr" );
		
		function PluginReCaptcha( $source = "" )
		{
			$this->PluginBase($source);

			$this->id      = "recaptcha";
			$this->desc    = "This plugin implements the recaptcha captcha generation. <br/>See <a href=\"http://recaptcha.net/\">http://recaptcha.net/</a> for details. To obtain a API Key, click <a href=http://recaptcha.net/api/getkey?app=lifetype>here</a>. <br>For updates or beta versions of this plugin, check out <a href=\"http://blog.coldtobi.de/1_coldtobis_blog/archive/128_recaptcha_plugin.html\"> http://blog.coldtobi.de/1_coldtobis_blog/archive/128_recaptcha_plugin.html</a>";
			$this->author  = "Tobias Frost";
			$this->locales = Array( "en_UK","de_DE" );
            		$this->version = "20080401";

			if( $source == "admin" )
				$this->initAdmin();
			else
				$this->init();
		}

		function init()
		{
			lt_include(PLOG_CLASS_PATH."plugins/recaptcha/class/security/recaptchafilter.class.php"); 
			$this->registerFilter( "ReCaptchaFilter" ); 
		}

		function initAdmin()
		{
			$this->init();
			
			$this->registerAdminAction( "recaptcha", "PluginRecaptchaConfigAction" ); 
			$this->registerAdminAction( "updateRecaptchaConfig", "PluginRecaptchaUpdateConfigAction" ); 
			
			$menu =& Menu::getMenu();
			if( !$menu->entryExists( "/menu/controlCenter/manageAntiSpamPlugins" ))
				$this->addMenuEntry( "/menu/controlCenter", "manageAntiSpamPlugins", "", "", true, false );
				$this->addMenuEntry( "/menu/controlCenter/manageAntiSpamPlugins", "plugin_recaptcha", "?op=recaptcha", "" ); 
		}
	
		function register()
		{
			$blogSettings 		 = $this->blogInfo->getSettings();
			$this->pluginEnabled = $blogSettings->getValue( "plugin_recaptcha_enabled" );
			$this->PubKey 	     = $blogSettings->getValue( "plugin_recaptcha_pubkey" );
			$this->PrivKey		 = $blogSettings->getValue( "plugin_recaptcha_privkey" );
			$this->MangleKey	 = $blogSettings->getValue( "plugin_recaptcha_manglekey" );
			$this->Theme	     = $blogSettings->getValue( "plugin_recaptcha_theme" );
		}
			
		function show() {

			$result="<!-- thank you for using ReCaptcha for Lifetype coldtobi -->";
			$jsoptions="";

			if ( $this->Theme != "" )
			{
				$jsoptions = $jsoptions . "theme : '".$this->Theme . "' ";
			}

			$blogSettings  = $this->blogInfo->getSettings();
			$lang = $blogSettings->getValue( "locale" );
			$lang=strtok($lang,"_");

			if ( $lang != "" && in_array($lang, $this->recaptcha_locales) )
			{			
				$jsoptions = $jsoptions . ", lang : '". $lang . "' ";
			}

			if ( $jsoptions != "" )	
			{
				$result= $result ."<script> var RecaptchaOptions = { ". $jsoptions . "};</script>";
			}

			$result = $result . recaptcha_get_html ($this->PubKey) . "<p style=\"font-size:6pt;padding-left:2em;width:500px;\">&nbsp;<a href=\"http://blog.coldtobi.de\">This is the ReCaptcha Plugin for Lifetype </a></p>";
			return $result;
		}

		function isEnabled()
		{
			return $this->pluginEnabled;
		}
	
		function getPluginConfigurationKeys()
		{
			lt_include(PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php");
			lt_include(PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php");

			return( Array(
				Array( "name" => "plugin_recaptcha_enabled", "type" => "boolean" ),
				Array( "name" => "plugin_recaptcha_privkey", "validator" => new StringValidator(),  "type" => "string", "allowEmpty" => true ),
				Array( "name" => "plugin_recaptcha_pubkey", "validator" => new StringValidator(), "type" => 	"string", "allowEmpty" => true ),
				Array( "name" => "plugin_recaptcha_theme", "validator" => new StringValidator(), "type" => 	"string", "allowEmpty" => true ),
				Array( "name" => "plugin_recaptcha_manglekey", "type" => "boolean" )));
		}
}
?>
