Initial
This commit is contained in:
38
system/Honeypot/Exceptions/HoneypotException.php
Normal file
38
system/Honeypot/Exceptions/HoneypotException.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Honeypot\Exceptions;
|
||||
|
||||
use CodeIgniter\Exceptions\ConfigException;
|
||||
use CodeIgniter\Exceptions\ExceptionInterface;
|
||||
|
||||
class HoneypotException extends ConfigException implements ExceptionInterface
|
||||
{
|
||||
public static function forNoTemplate()
|
||||
{
|
||||
return new static(lang('Honeypot.noTemplate'));
|
||||
}
|
||||
|
||||
public static function forNoNameField()
|
||||
{
|
||||
return new static(lang('Honeypot.noNameField'));
|
||||
}
|
||||
|
||||
public static function forNoHiddenValue()
|
||||
{
|
||||
return new static(lang('Honeypot.noHiddenValue'));
|
||||
}
|
||||
|
||||
public static function isBot()
|
||||
{
|
||||
return new static(lang('Honeypot.theClientIsABot'));
|
||||
}
|
||||
}
|
||||
92
system/Honeypot/Honeypot.php
Normal file
92
system/Honeypot/Honeypot.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Honeypot;
|
||||
|
||||
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use Config\Honeypot as HoneypotConfig;
|
||||
|
||||
/**
|
||||
* class Honeypot
|
||||
*/
|
||||
class Honeypot
|
||||
{
|
||||
/**
|
||||
* Our configuration.
|
||||
*
|
||||
* @var HoneypotConfig
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @throws HoneypotException
|
||||
*/
|
||||
public function __construct(HoneypotConfig $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
|
||||
if (! $this->config->hidden) {
|
||||
throw HoneypotException::forNoHiddenValue();
|
||||
}
|
||||
|
||||
if (empty($this->config->container) || strpos($this->config->container, '{template}') === false) {
|
||||
$this->config->container = '<div style="display:none">{template}</div>';
|
||||
}
|
||||
|
||||
if ($this->config->template === '') {
|
||||
throw HoneypotException::forNoTemplate();
|
||||
}
|
||||
|
||||
if ($this->config->name === '') {
|
||||
throw HoneypotException::forNoNameField();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the request if honeypot field has data.
|
||||
*/
|
||||
public function hasContent(RequestInterface $request)
|
||||
{
|
||||
return ! empty($request->getPost($this->config->name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches Honeypot template to response.
|
||||
*/
|
||||
public function attachHoneypot(ResponseInterface $response)
|
||||
{
|
||||
$prepField = $this->prepareTemplate($this->config->template);
|
||||
|
||||
$body = $response->getBody();
|
||||
$body = str_ireplace('</form>', $prepField . '</form>', $body);
|
||||
$response->setBody($body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the template by adding label
|
||||
* content and field name.
|
||||
*/
|
||||
protected function prepareTemplate(string $template): string
|
||||
{
|
||||
$template = str_ireplace('{label}', $this->config->label, $template);
|
||||
$template = str_ireplace('{name}', $this->config->name, $template);
|
||||
|
||||
if ($this->config->hidden) {
|
||||
$template = str_ireplace('{template}', $template, $this->config->container);
|
||||
}
|
||||
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user