Skip to content

Commit

Permalink
StopForumSpam : before & after callback. Stop processing, overridde p…
Browse files Browse the repository at this point in the history
…arameters, and override return status
  • Loading branch information
Anthony committed Oct 12, 2021
1 parent 9dabab3 commit 5eaa8a3
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 52 deletions.
62 changes: 34 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,40 +138,46 @@ use Coercive\Security\Authentication\StopForumSpam;

$sfspam = new StopForumSpam;

# Global callback is used before each check
$sfspam->setCallbackBefore(function ($type, $value) {

# Do something...
if($type === StopForumSpam::TYPE_EMAIL && $value === 'test@email.com') {
echo 'hello world';
}

# Return not-null => stop processing and force return boolean casted value of your return
return true;
return false;

# No return or return null => continue processing
return null;
});
# Global callback is used after each check
$sfspam->setCallback(function ($status) {
if($status) {
$sfspam->setCallbackAfter(function ($type, $status, $value) {
echo $value;
if($type === StopForumSpam::TYPE_EMAIL && $status) {
exit;
}
});

# Email callback is used after email check, before global check
$sfspam->setCallbackEmail(function ($status, $email) {
if($status) {
error_log(print_r("The email : $email, is a spammer.", true));
}
else {
error_log(print_r("The email ; $email, is not a spammer.", true));
}
});
# Return not-null => override api status and force return boolean casted value of your return
return true;
return false;

# Email callback is used after IP check, before global check
$sfspam->setCallbackIp(function ($status, $ip) {
if($status) {
error_log(print_r("The ip : $ip, is a spammer.", true));
}
else {
error_log(print_r("The ip : $ip, is not a spammer.", true));
}
# No return or return null => return api status
return null;
});

# Email callback is used after username check, before global check
$sfspam->setCallbackUserName(function ($status, $name) {
if($status) {
error_log(print_r("The username : $name, is a spammer.", true));
}
else {
error_log(print_r("The username : $name, is not a spammer.", true));
}
# You can override value when pass a parameter as a reference
$sfspam->setCallbackBefore(function ($type, &$value) {
$value = 'new value';
});

# You have also specific callback for each type
$sfspam->setCallbackBeforeEmail(function ($email) {});
$sfspam->setCallbackAfterEmail(function ($status, $email) {});
$sfspam->setCallbackBeforeIp(function ($ip) {});
$sfspam->setCallbackAfterIp(function ($status, $ip) {});
$sfspam->setCallbackBeforeIp(function ($name) {});
$sfspam->setCallbackAfterUserName(function ($status, $name) {});
```
175 changes: 151 additions & 24 deletions dist/StopForumSpam.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,33 @@ class StopForumSpam
{
const ENDPOINT = 'http://api.stopforumspam.com/api?json&';

const TYPE_USERNAME = 'username';
const TYPE_EMAIL = 'email';
const TYPE_IP = 'ip';

/** @var callable */
private $callbackBefore;

/** @var callable */
private $callbackAfter;

/** @var callable */
private $callbackBeforeEmail;

/** @var callable */
private $callback;
private $callbackAfterEmail;

/** @var callable */
private $callbackEmail;
private $callbackBeforeIp;

/** @var callable */
private $callbackIp;
private $callbackAfterIp;

/** @var callable */
private $callbackUserName;
private $callbackBeforeUserName;

/** @var callable */
private $callbackAfterUserName;

/**
* @param array $query
Expand All @@ -61,6 +77,18 @@ private function get(array $query): array
*/
public function checkEmail(string $email, bool $hash = false): bool
{
if($this->callbackBefore) {
$rcs = ($this->callbackBefore)(self::TYPE_EMAIL, $email);
if(null !== $rcs) {
return (bool) $rcs;
}
}
if($this->callbackBeforeEmail) {
$rcs = ($this->callbackBeforeEmail)($email);
if(null !== $rcs) {
return (bool) $rcs;
}
}
if($hash) {
$query = [
'emailhash' => md5($email)
Expand All @@ -73,11 +101,17 @@ public function checkEmail(string $email, bool $hash = false): bool
}
$arr = $this->get($query);
$status = boolval($arr[$hash ? 'emailhash' : 'email']['appears'] ?? 0);
if($this->callbackEmail) {
($this->callbackEmail)($status, $email);
if($this->callbackAfterEmail) {
$rcs = ($this->callbackAfterEmail)($status, $email);
if(null !== $rcs) {
return (bool) $rcs;
}
}
if($this->callback) {
($this->callback)($status);
if($this->callbackAfter) {
$rcs = ($this->callbackAfter)(self::TYPE_EMAIL, $status, $email);
if(null !== $rcs) {
return (bool) $rcs;
}
}
return $status;
}
Expand All @@ -91,15 +125,34 @@ public function checkEmail(string $email, bool $hash = false): bool
*/
public function checkIp(string $ip): bool
{
if($this->callbackBefore) {
$rcs = ($this->callbackBefore)(self::TYPE_IP, $ip);
if(null !== $rcs) {
return (bool) $rcs;
}
}
if($this->callbackBeforeIp) {
$rcs = ($this->callbackBeforeIp)($ip);
if(null !== $rcs) {
return (bool) $rcs;
}
}
$arr = $this->get([
'ip' => $ip
]);
$status = boolval($arr['ip']['appears'] ?? 0);
if($this->callbackIp) {
($this->callbackIp)($status, $ip);
if($this->callbackAfterIp) {
$rcs = ($this->callbackAfterIp)($status, $ip);
if(null !== $rcs) {
return (bool) $rcs;
}
}
if($this->callback) {
($this->callback)($status);
if($this->callbackAfter) {
$rcs = ($this->callbackAfter)(self::TYPE_IP, $status, $ip);
if(null !== $rcs) {
return (bool) $rcs;
}

}
return $status;
}
Expand All @@ -113,19 +166,51 @@ public function checkIp(string $ip): bool
*/
public function checkUserName(string $name): bool
{
if($this->callbackBefore) {
$rcs = ($this->callbackBefore)(self::TYPE_USERNAME, $name);
if(null !== $rcs) {
return (bool) $rcs;
}
}
if($this->callbackBeforeUserName) {
$rcs = ($this->callbackBeforeUserName)($name);
if(null !== $rcs) {
return (bool) $rcs;
}
}
$arr = $this->get([
'username' => $name
]);
$status = boolval($arr['username']['appears'] ?? 0);
if($this->callbackUserName) {
($this->callbackUserName)($status, $name);
if($this->callbackAfterUserName) {
$rcs = ($this->callbackAfterUserName)($status, $name);
if(null !== $rcs) {
return (bool) $rcs;
}
}
if($this->callback) {
($this->callback)($status);
if($this->callbackAfter) {
$rcs = ($this->callbackAfter)(self::TYPE_USERNAME, $status, $name);
if(null !== $rcs) {
return (bool) $rcs;
}
}
return $status;
}

/**
* Add general callback before all checks
*
* Receive only the status in parameter
*
* @param callable $function
* @return $this
*/
public function setCallbackBefore(callable $function): StopForumSpam
{
$this->callbackBefore = $function;
return $this;
}

/**
* Add general callback after all checks
*
Expand All @@ -134,9 +219,23 @@ public function checkUserName(string $name): bool
* @param callable $function
* @return $this
*/
public function setCallback(callable $function): StopForumSpam
public function setCallbackAfter(callable $function): StopForumSpam
{
$this->callbackAfter = $function;
return $this;
}

/**
* Add callback before email checks
*
* Receive status and email in parameter
*
* @param callable $function
* @return $this
*/
public function setCallbackBeforeEmail(callable $function): StopForumSpam
{
$this->callback = $function;
$this->callbackBeforeEmail = $function;
return $this;
}

Expand All @@ -148,9 +247,23 @@ public function setCallback(callable $function): StopForumSpam
* @param callable $function
* @return $this
*/
public function setCallbackEmail(callable $function): StopForumSpam
public function setCallbackAfterEmail(callable $function): StopForumSpam
{
$this->callbackAfterEmail = $function;
return $this;
}

/**
* Add callback before ip checks
*
* Receive status and ip in parameter
*
* @param callable $function
* @return $this
*/
public function setCallbackBeforeIp(callable $function): StopForumSpam
{
$this->callbackEmail = $function;
$this->callbackBeforeIp = $function;
return $this;
}

Expand All @@ -162,9 +275,23 @@ public function setCallbackEmail(callable $function): StopForumSpam
* @param callable $function
* @return $this
*/
public function setCallbackIp(callable $function): StopForumSpam
public function setCallbackAfterIp(callable $function): StopForumSpam
{
$this->callbackAfterIp = $function;
return $this;
}

/**
* Add callback before user name checks
*
* Receive status and name in parameter
*
* @param callable $function
* @return $this
*/
public function setCallbackBeforeUserName(callable $function): StopForumSpam
{
$this->callbackIp = $function;
$this->callbackBeforeUserName = $function;
return $this;
}

Expand All @@ -176,9 +303,9 @@ public function setCallbackIp(callable $function): StopForumSpam
* @param callable $function
* @return $this
*/
public function setCallbackUserName(callable $function): StopForumSpam
public function setCallbackAfterUserName(callable $function): StopForumSpam
{
$this->callbackUserName = $function;
$this->callbackAfterUserName = $function;
return $this;
}
}

0 comments on commit 5eaa8a3

Please sign in to comment.