Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make logging optional, closes #47 #48

Merged
merged 3 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/code/community/Hackathon/HoneySpam/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,33 @@
class Hackathon_HoneySpam_Helper_Data extends Mage_Core_Helper_Abstract
{
const CONFIG_PATH_INPUT_NAME = 'hackathon/honeyspam/honeypotName';

const CONFIG_PATH_ENABLE_LOG = 'hackathon/honeyspam/enableLogging';

/**
* @return string
*/
public function getHoneypotName()
{
return Mage::getStoreConfig(self::CONFIG_PATH_INPUT_NAME);
}

/**
* @return bool
*/
public function isLoggingEnabled()
{
return Mage::getStoreConfigFlag(self::CONFIG_PATH_ENABLE_LOG);
}

/**
* @param string $message
* @param int $level
*/
public function log($message, $level = Zend_Log::INFO)
{
if ($this->isLoggingEnabled()) {
Mage::log($message, $level, 'honeyspam.log');
}
}
}
36 changes: 25 additions & 11 deletions app/code/community/Hackathon/HoneySpam/Model/Checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@

class Hackathon_HoneySpam_Model_Checker extends Mage_Core_Model_Abstract {

public function init($params) {

/**
* @param array $params
* @return int
*/
public function init($params)
{
$firstname = $params['firstname'];
$lastname = $params['lastname'];
$emailprefix = explode('@', $params['email']);
Expand All @@ -38,7 +42,17 @@ public function init($params) {
return $this->check($firstname, $lastname, $emailprefix, $params);
}

public function check($firstname, $lastname, $emailprefix, $params) {
/**
* @param string $firstname
* @param string $lastname
* @param string $emailprefix
* @param array $params
* @return int
*/
public function check($firstname, $lastname, $emailprefix, $params)
{
/* @var $helper Hackathon_HoneySpam_Helper_Data */
$helper = Mage::helper('hackathon_honeyspam');

$_index = 0;

Expand Down Expand Up @@ -73,42 +87,42 @@ public function check($firstname, $lastname, $emailprefix, $params) {
foreach ($params as $param) {
if (strlen($param) >= 15) { // item has more than 15 chars = spam possibility increases a little
$_index += 1;
Mage::log("SPAM: " . $param . " has more than 15 Characters");
$helper->log("SPAM: " . $param . " has more than 15 Characters");
}

if (is_numeric($param)) { // Param contains numbers only == spam (heavy rating!
$_index += 2.5;
Mage::log("SPAM: " . $param . " contains only numbers");
$helper->log("SPAM: " . $param . " contains only numbers");
}

if (preg_match("([b-df-hj-np-tv-z]{3})", $param, $matches)) { // More than 3 consecutive consonants == Spam!
if (!($matches[0] == "rrm")) { // Herrmann is okay
$_index += 1;
Mage::log("SPAM: " . $param . " contains 3 or more consecutive consonants");
$helper->log("SPAM: " . $param . " contains 3 or more consecutive consonants");
}
}

if (preg_match("([aeiou]{3})", $param, $matches)) { // More than 3 consecutive vouwels == spam
if (!($matches[0] == "eie")) {
Mage::log("matches: " . $matches[0]); // Meier is okay
$_index += 1;
Mage::log("SPAM: " . $param . " contains 3 consecutive vowels");
$helper->log("matches: " . $matches[0]); // Meier is okay
$helper->log("SPAM: " . $param . " contains 3 consecutive vowels");
}
}

if (preg_match("([A-Z]{2,})", substr($param, -4))) { // At least two CAPITALS at the end of a string == Spam!
$_index += 1;
Mage::log("SPAM: " . $param . " has at least 2 CAPITAL letters at the end");
$helper->log("SPAM: " . $param . " has at least 2 CAPITAL letters at the end");
}

if (preg_match_all("([A-Z])", $param, $matches) > 3) { // Param contains more than 3 Capital letters at all
$_index += 1;
Mage::log("SPAM: " . $param . " contains more than 3 CAPITALS at all");
$helper->log("SPAM: " . $param . " contains more than 3 CAPITALS at all");
}

if (preg_match("([a-z])", substr($param, 1, 1)) && preg_match("([A-Z])", substr($param, 1, 1))) { // Param starts with a lowercase+uppercase
$_index += 1;
Mage::log("SPAM: " . $param . " starts with a combination lc/uc. E.g. aJohn, bSmith...");
$helper->log("SPAM: " . $param . " starts with a combination lc/uc. E.g. aJohn, bSmith...");
}
}

Expand Down
28 changes: 16 additions & 12 deletions app/code/community/Hackathon/HoneySpam/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,43 @@ class Hackathon_HoneySpam_Model_Observer
*/
public function controllerActionPredispatchCustomerAccountCreatepost()
{
if (Mage::getStoreConfig('hackathon/honeyspam/enableHoneypotName')) {
if (Mage::getStoreConfigFlag('hackathon/honeyspam/enableHoneypotName')) {
$this->_checkHoneypot();
}

if (Mage::getStoreConfig('hackathon/honeyspam/enableHoneypotAccountCreateTime')) {
if (Mage::getStoreConfigFlag('hackathon/honeyspam/enableHoneypotAccountCreateTime')) {
$this->_checkTimestamp();
}

if (Mage::getStoreConfig('hackathon/honeyspam/enableSpamIndexing')) {
if (Mage::getStoreConfigFlag('hackathon/honeyspam/enableSpamIndexing')) {
$this->_indexLoginParams();
}
}

public function controllerActionPredispatchBlockReviewForm()
{
if (Mage::getStoreConfig('hackathon/honeyspam/enableHoneypotName')) {
if (Mage::getStoreConfigFlag('hackathon/honeyspam/enableHoneypotName')) {
$this->_checkHoneypot();
}
}

public function controllerActionPredispatchCustomerAccountForgotPasswordPost()
{
if (Mage::getStoreConfig('hackathon/honeyspam/enableHoneypotName')) {
if (Mage::getStoreConfigFlag('hackathon/honeyspam/enableHoneypotName')) {
$this->_checkHoneypot();
}
}

public function controllerActionPredispatchContactsIndexPost()
{
if (Mage::getStoreConfig('hackathon/honeyspam/enableHoneypotName')) {
if (Mage::getStoreConfigFlag('hackathon/honeyspam/enableHoneypotName')) {
$this->_checkHoneypot();
}
}

public function controllerActionPredispatchNewsletterSubscriberNew()
{
if (Mage::getStoreConfig('hackathon/honeyspam/enableHoneypotName')) {
if (Mage::getStoreConfigFlag('hackathon/honeyspam/enableHoneypotName')) {
$this->_checkHoneypot();
}
}
Expand All @@ -79,7 +79,7 @@ protected function _checkHoneypot()
/* @var $helper Hackathon_HoneySpam_Helper_Data */
$helper = Mage::helper('hackathon_honeyspam');
if (strlen(Mage::app()->getRequest()->getParam($helper->getHoneypotName()))) {
Mage::log('Honeypot Input filled. Aborted.',Zend_Log::WARN);
$helper->log('Honeypot Input filled. Aborted.',Zend_Log::WARN);

$e = new Mage_Core_Controller_Varien_Exception();
$e->prepareForward('index','error','honeyspam');
Expand All @@ -97,7 +97,9 @@ protected function _checkTimestamp()
if (
!$session->getAccountCreateTime(false) || ($session->getAccountCreateTime() > (time() - $accountCreateTime))
) {
Mage::log('Honeypot Timestamp filled. Aborted.',Zend_Log::WARN);
/* @var $helper Hackathon_HoneySpam_Helper_Data */
$helper = Mage::helper('hackathon_honeyspam');
$helper->log('Honeypot Timestamp filled. Aborted.', Zend_Log::WARN);

$e = new Mage_Core_Controller_Varien_Exception();
$e->prepareForward('index','error','honeyspam');
Expand All @@ -115,14 +117,16 @@ public function controllerActionPredispatchCustomerAccountCreate()
}

// Invoke indexing
public function _indexLoginParams() {

public function _indexLoginParams()
{
$checker = Mage::getModel('hackathon_honeyspam/checker');

$return = $checker->init(Mage::app()->getRequest()->getParams());

if ($return >= Mage::getStoreConfig('hackathon/honeyspam/spamIndexLevel')) {
Mage::log("Honeypot spam index at $return. Aborted.",Zend_Log::WARN);
/* @var $helper Hackathon_HoneySpam_Helper_Data */
$helper = Mage::helper('hackathon_honeyspam');
$helper->log("Honeypot spam index at $return. Aborted.", Zend_Log::WARN);

$e = new Mage_Core_Controller_Varien_Exception();
$e->prepareForward('index','error','honeyspam');
Expand Down
1 change: 1 addition & 0 deletions app/code/community/Hackathon/HoneySpam/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
<honeypotAccountCreateTime>5</honeypotAccountCreateTime>
<honeypotName>url</honeypotName>
<spamIndexLevel>2.5</spamIndexLevel>
<enableLogging>0</enableLogging>
</honeyspam>
</hackathon>
</default>
Expand Down
10 changes: 10 additions & 0 deletions app/code/community/Hackathon/HoneySpam/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@
<show_in_store>1</show_in_store>
<depends><enableSpamIndexing>1</enableSpamIndexing></depends>
</spamIndexLevel>

<enableLogging>
<label>Enable honeypot logging</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>70</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</enableLogging>
</fields>
</honeyspam>
</groups>
Expand Down