<commit_before><?php
namespace Craft;

/**
 * @=SpamGuard
 *
 * SpamGuard allows you to use the powerful Akismet API to fight spam
 *
 * @author  <NAME> - http://twitter.com/selvinortiz
 * @package SpamGuard
 * @version 0.4
 *
 *
 * @example (Template) Retuns true/false
 *
 * $content: 	The blog post comment or submitted content to check
 * $author:		The submitted content author name
 * $email: 		The submitted content author email
 *
 * <code>
 *  craft.SpamGuard.isSpam(
 *  	{
 *  		content: "Potential spammy comment or submitted content",
 *  		author: "<NAME>",
 *  		email: "<EMAIL>"
 *  	}
 *  )
 * </code>
 *
 * @example (Service) Returns true/false
 *
 * <code>
 *  craft()->spamGuard->isSpam(SpamGuardModel $model)
 * </code>
 */

class SpamGuardPlugin extends BasePlugin
{
	const PLUGIN_NAME			= 'Spam Guard';
	const PLUGIN_HANDLE			= 'spamGuard';
	const PLUGIN_VERSION		= '0.4';
	const PLUGIN_DEVELOPER		= '<NAME>';
	const PLUGIN_DEVELOPER_URL	= 'http://twitter.com/selvinortiz';
	const PLUGIN_SETTINGS_TMPL	= 'spamguard/__settings.twig';

	//--------------------------------------------------------------------------------
	
	public function __construct()
	{
		require_once __DIR__.'/rocket/Rocket.php';
		Rocket::launch();
	}

	//--------------------------------------------------------------------------------
	
	public function getName()
	{
		return Rocket::getPluginName($this, self::PLUGIN_NAME);
	}

	//--------------------------------------------------------------------------------
	
	public function getVersion()
	{
		return self::PLUGIN_VERSION;
	}

	//--------------------------------------------------------------------------------

	public function getDeveloper()
	{
		return self::PLUGIN_DEVELOPER;
	}

	//--------------------------------------------------------------------------------

	public function getDeveloperUrl()
	{
		return self::PLUGIN_DEVELOPER_URL;
	}

	//--------------------------------------------------------------------------------

	public function getCpUrl()
	{
		// Is this already available via a Craft service provider?
		return sprintf('/%s/%s', craft()->config->get('cpTrigger'), strtolower(self::PLUGIN_HANDLE) );
	}

	//--------------------------------------------------------------------------------

	public function hasCpSection()
	{
		return true;
	}

	//--------------------------------------------------------------------------------

	public function defineSettings()
	{
		return array(
			'pluginName'		=> array(AttributeType::String, 'maxLength'=>50),
			'pluginNickname'	=> array(AttributeType::String, 'maxLength'=>50),
			'akismetApiKey'		=> array(AttributeType::String, 'required'=>true, 'maxLength'=>50),
			'akismetOriginUrl'	=> array(AttributeType::String, 'required'=>true, 'maxLength'=>255)
		);
	}

	//--------------------------------------------------------------------------------

	public function getSettingsHtml()
	{
		return craft()->templates->render(self::PLUGIN_SETTINGS_TMPL, array('settings'=>$this->getSettings()));
	}

	//--------------------------------------------------------------------------------
	
	public function prepSettings( $settings=array() )
	{
		if ( array_key_exists('pluginName', $settings) && ! empty($settings['pluginName']) )
		{
			return $settings;
		}

		return array_merge( $settings, array('pluginName'=>Rocket::getPluginName($this, self::PLUGIN_NAME) ) );
	}

	//--------------------------------------------------------------------------------
	
	public function onAfterInstall()
	{
		$dbCommand		= craft()->db->createCommand();
		$pluginClass	= Rocket::getClassName($this);
		$pluginSettings	= array( 'pluginName'=>$this->getName(), 'pluginNickname'=>$this->getName() );

		$dbCommand->update(
			'plugins', array('settings'=>toJson($pluginSettings)),
			'class=:className', array(':className'=>$pluginClass)
		);

		$toSettings = sprintf('/%s/%s', craft()->config->get('cpTrigger'), strtolower(SpamGuardPlugin::PLUGIN_HANDLE));
		
		craft()->request->redirect( $toSettings );
	}

	//--------------------------------------------------------------------------------
	
	public function hookRegisterCpRoutes()
	{
		return array();
	}

	//--------------------------------------------------------------------------------
	// @HOOKS
	//--------------------------------------------------------------------------------
	
	/**
	 * spamGuardPostedContent()
	 *
	 * @param  array $params The associative array of data and callbacks
	 *
	 * <code>
	 * 	$params = array( data=>array(content=>'', author=>'', email=>''), onSuccess=>function(){}, onFailure=>function(){})
	 * </code>
	 */
	public function spamGuardPostedContent($params=false)
	{
		// I was planning on using onSpam/onClean but went with the more generic onSuccess/onFailure
		$data 		= array();
		$onSuccess	= null;
		$onFailure	= null;

		if ( $params && is_array($params) )
		{
			@extract($params);
		}

		// Should I be calling a controller method here instead of the service?
		$isContentSpam = craft()->spamGuard->isSpam($data);

		if ( $isContentSpam && $onFailure )
		{
			if (is_callable($onFailure))
			{
				$onFailure();
			}
		}
		elseif ( $onSuccess )
		{
			if (is_callable($onSuccess))
			{
				$onSuccess();
			}
		}

		return $isContentSpam;
	}
}<commit_msg>[FIX] Fixes logic issue where Spam Guard was calling onSuccess even if spammy content was found.<commit_after><?php
namespace Craft;

/**
 * @=SpamGuard
 *
 * SpamGuard allows you to use the powerful Akismet API to fight spam
 *
 * @author  <NAME> - http://twitter.com/selvinortiz
 * @package SpamGuard
 * @version 0.4
 *
 *
 * @example (Template) Retuns true/false
 *
 * $content: 	The blog post comment or submitted content to check
 * $author:		The submitted content author name
 * $email: 		The submitted content author email
 *
 * <code>
 *  craft.SpamGuard.isSpam(
 *  	{
 *  		content: "Potential spammy comment or submitted content",
 *  		author: "<NAME>",
 *  		email: "<EMAIL>"
 *  	}
 *  )
 * </code>
 *
 * @example (Service) Returns true/false
 *
 * <code>
 *  craft()->spamGuard->isSpam(SpamGuardModel $model)
 * </code>
 */

class SpamGuardPlugin extends BasePlugin
{
	const PLUGIN_NAME			= 'Spam Guard';
	const PLUGIN_HANDLE			= 'spamGuard';
	const PLUGIN_VERSION		= '0.4';
	const PLUGIN_DEVELOPER		= '<NAME>';
	const PLUGIN_DEVELOPER_URL	= 'http://twitter.com/selvinortiz';
	const PLUGIN_SETTINGS_TMPL	= 'spamguard/__settings.twig';

	//--------------------------------------------------------------------------------
	
	public function __construct()
	{
		require_once __DIR__.'/rocket/Rocket.php';
		Rocket::launch();
	}

	//--------------------------------------------------------------------------------
	
	public function getName()
	{
		return Rocket::getPluginName($this, self::PLUGIN_NAME);
	}

	//--------------------------------------------------------------------------------
	
	public function getVersion()
	{
		return self::PLUGIN_VERSION;
	}

	//--------------------------------------------------------------------------------

	public function getDeveloper()
	{
		return self::PLUGIN_DEVELOPER;
	}

	//--------------------------------------------------------------------------------

	public function getDeveloperUrl()
	{
		return self::PLUGIN_DEVELOPER_URL;
	}

	//--------------------------------------------------------------------------------

	public function getCpUrl()
	{
		// Is this already available via a Craft service provider?
		return sprintf('/%s/%s', craft()->config->get('cpTrigger'), strtolower(self::PLUGIN_HANDLE) );
	}

	//--------------------------------------------------------------------------------

	public function hasCpSection()
	{
		return true;
	}

	//--------------------------------------------------------------------------------

	public function defineSettings()
	{
		return array(
			'pluginName'		=> array(AttributeType::String, 'maxLength'=>50),
			'pluginNickname'	=> array(AttributeType::String, 'maxLength'=>50),
			'akismetApiKey'		=> array(AttributeType::String, 'required'=>true, 'maxLength'=>50),
			'akismetOriginUrl'	=> array(AttributeType::String, 'required'=>true, 'maxLength'=>255)
		);
	}

	//--------------------------------------------------------------------------------

	public function getSettingsHtml()
	{
		return craft()->templates->render(self::PLUGIN_SETTINGS_TMPL, array('settings'=>$this->getSettings()));
	}

	//--------------------------------------------------------------------------------
	
	public function prepSettings( $settings=array() )
	{
		if ( array_key_exists('pluginName', $settings) && ! empty($settings['pluginName']) )
		{
			return $settings;
		}

		return array_merge( $settings, array('pluginName'=>Rocket::getPluginName($this, self::PLUGIN_NAME) ) );
	}

	//--------------------------------------------------------------------------------
	
	public function onAfterInstall()
	{
		$dbCommand		= craft()->db->createCommand();
		$pluginClass	= Rocket::getClassName($this);
		$pluginSettings	= array( 'pluginName'=>$this->getName(), 'pluginNickname'=>$this->getName() );

		$dbCommand->update(
			'plugins', array('settings'=>toJson($pluginSettings)),
			'class=:className', array(':className'=>$pluginClass)
		);

		$toSettings = sprintf('/%s/%s', craft()->config->get('cpTrigger'), strtolower(SpamGuardPlugin::PLUGIN_HANDLE));
		
		craft()->request->redirect( $toSettings );
	}

	//--------------------------------------------------------------------------------
	
	public function hookRegisterCpRoutes()
	{
		return array();
	}

	//--------------------------------------------------------------------------------
	// @HOOKS
	//--------------------------------------------------------------------------------
	
	/**
	 * spamGuardPostedContent()
	 *
	 * @param  array $params The associative array of data and callbacks
	 *
	 * <code>
	 * 	$params = array( data=>array(content=>'', author=>'', email=>''), onSuccess=>function(){}, onFailure=>function(){})
	 * </code>
	 */
	public function spamGuardPostedContent($params=false)
	{
		// I was planning on using onSpam/onClean but went with the more generic onSuccess/onFailure
		$data 		= array();
		$onSuccess	= null;
		$onFailure	= null;

		if ( $params && is_array($params) )
		{
			@extract($params);
		}

		// Should I be calling a controller method here instead of the service?
		$isContentSpam = craft()->spamGuard->isSpam($data);

		if ( $isContentSpam && $onFailure )
		{
			if (is_callable($onFailure))
			{
				$onFailure();
			}
		}
		elseif ( ! $isContentSpam && $onSuccess )
		{
			if (is_callable($onSuccess))
			{
				$onSuccess();
			}
		}

		return $isContentSpam;
	}
}
{'fraction_non_alphanumeric': 0.34019061998678873, 'fraction_numerical': 0.0024535245824289895, 'mean_word_length': 1.704951505870342, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5243587', 'n_tokens_mistral': 3204, 'n_tokens_neox': 2904, 'n_words': 582}