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

Fix & expand Honeypot & its tests #1314

Merged
merged 2 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 28 additions & 34 deletions application/Filters/Honeypot.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@
<?php namespace App\Filters;
<?php

namespace App\Filters;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;
use Config\Services;
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
use CodeIgniter\Honeypot\Honeypot;

class Honeypot implements FilterInterface
class Honeypot implements FilterInterface
{

/**
* Checks if Honeypot field is empty, if so
* then the requester is a bot,show a blank
* page
/**
* Checks if Honeypot field is empty; if not
* then the requester is a bot
*
* @param RequestInterface|\CodeIgniter\HTTP\IncomingRequest $request
* @param CodeIgniter\HTTP\IncomingRequest $request
*
* @return mixed
*/
public function before(IncomingRequest $request)
{
$honeypot = new Honeypot(new \Config\Honeypot());
if ($honeypot->hasContent($request))
{
throw HoneypotException::isBot();
}
}

public function before (RequestInterface $request)
{

// Checks honeypot field if value was entered then show blank if so.

$honeypot = Services::honeypot(new \Config\Honeypot());
if($honeypot->hasContent($request))
{
throw HoneypotException::isBot();
}

}

/**
* Checks if Honeypot field is empty, if so
* then the requester is a bot,show a blank
* page
/**
* Attach a honypot to the current response.
*
* @param RequestInterface|\CodeIgniter\HTTP\IncomingRequest $request
* @param ResponseInterface|\CodeIgniter\HTTP\Response $response
* @param CodeIgniter\HTTP\IncomingRequest $request
* @param CodeIgniter\HTTP\Response $response
* @return mixed
*/
public function after(IncomingRequest $request, Response $response)
{
$honeypot = new Honeypot(new \Config\Honeypot());
$honeypot->attachHoneypot($response);
}

public function after (RequestInterface $request, ResponseInterface $response)
{

$honeypot = Services::honeypot(new \Config\Honeypot());
$honeypot->attachHoneypot($response);
}
}
143 changes: 61 additions & 82 deletions system/Honeypot/Honeypot.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace CodeIgniter\Honeypot;
<?php

namespace CodeIgniter\Honeypot;

/**
* CodeIgniter
Expand Down Expand Up @@ -35,108 +37,85 @@
* @since Version 3.0.0
* @filesource
*/

use CodeIgniter\Config\BaseConfig;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;
use CodeIgniter\Honeypot\Exceptions\HoneypotException;

class Honeypot
class Honeypot
{

/**
* Honeypot Template
* @var String
*/
protected $template;

/**
* Honeypot text field name
* @var String
/**
* @var BaseConfig
*/
protected $name;
protected $config;

/**
* Honeypot lable content
* @var String
*/
protected $label;
//--------------------------------------------------------------------

/**
* Self Instance of Class
* @var Honeypot
*/
protected $config;
function __construct(BaseConfig $config)
{
$this->config = $config;

//--------------------------------------------------------------------
if ($this->config->hidden === '')
{
throw HoneypotException::forNoHiddenValue();
}

function __construct (BaseConfig $config) {
$this->config = $config;
if ($this->config->template === '')
{
throw HoneypotException::forNoTemplate();
}

if($this->config->hidden === '')
{
throw HoneypotException::forNoHiddenValue();
}
if ($this->config->name === '')
{
throw HoneypotException::forNoNameField();
}
}

if($this->config->template === '')
{
throw HoneypotException::forNoTemplate();
}
//--------------------------------------------------------------------

if($this->config->name === '')
{
throw HoneypotException::forNoNameField();
}
}

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

/**
/**
* Checks the request if honeypot field has data.
*
* @param \CodeIgniter\HTTP\RequestInterface $request
* @param \CodeIgniter\HTTP\IncomingRequest $request
*
*/
public function hasContent(RequestInterface $request)
{
if($request->getVar($this->config->name))
{
return true;
}
return false;
}

/**
* Attachs Honeypot template to response.
public function hasContent(IncomingRequest $request)
{
return ( ! empty($request->getPost($this->config->name))) ? true : false;
}

/**
* Attaches Honeypot template to response.
*
* @param \CodeIgniter\HTTP\ResponseInterface $response
* @param \CodeIgniter\HTTP\Response $response
*/
public function attachHoneypot(ResponseInterface $response)
{
$prep_field = $this->prepareTemplate($this->config->template);
$body = $response->getBody();
$body = str_ireplace('</form>', $prep_field, $body);
$response->setBody($body);
}

/**
public function attachHoneypot(Response $response)
{
$prep_field = $this->prepareTemplate($this->config->template);

$body = $response->getBody();
$body = str_ireplace('</form>', $prep_field, $body);
$response->setBody($body);
}

/**
* Prepares the template by adding label
* content and field name.
* content and field name.
*
* @param string $template
* @return string
*/
protected function prepareTemplate($template): string
{
$template = str_ireplace('{label}', $this->config->label, $template);
$template = str_ireplace('{name}', $this->config->name, $template);

if($this->config->hidden)
{
$template = '<div style="display:none">'. $template . '</div>';
}
return $template;
}
}
protected function prepareTemplate($template): string
{
$template = str_ireplace('{label}', $this->config->label, $template);
$template = str_ireplace('{name}', $this->config->name, $template);

if ($this->config->hidden)
{
$template = '<div style="display:none">' . $template . '</div>';
}
return $template;
}

}
Loading