Skip to content

Commit d8ec4a5

Browse files
authored
Merge pull request #1314 from jim-parry/testing/honeypot
Fix & expand Honeypot & its tests
2 parents 1d1ef70 + 7feb800 commit d8ec4a5

5 files changed

Lines changed: 265 additions & 210 deletions

File tree

application/Filters/Honeypot.php

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,45 @@
1-
<?php namespace App\Filters;
1+
<?php
2+
3+
namespace App\Filters;
24

35
use CodeIgniter\Filters\FilterInterface;
46
use CodeIgniter\HTTP\RequestInterface;
57
use CodeIgniter\HTTP\ResponseInterface;
68
use Config\Services;
79
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
10+
use CodeIgniter\Honeypot\Honeypot;
811

9-
class Honeypot implements FilterInterface
12+
class Honeypot implements FilterInterface
1013
{
1114

12-
/**
13-
* Checks if Honeypot field is empty, if so
14-
* then the requester is a bot,show a blank
15-
* page
15+
/**
16+
* Checks if Honeypot field is empty; if not
17+
* then the requester is a bot
1618
*
17-
* @param RequestInterface|\CodeIgniter\HTTP\IncomingRequest $request
19+
* @param CodeIgniter\HTTP\RequestInterface $request
1820
*
1921
* @return mixed
2022
*/
23+
public function before(RequestInterface $request)
24+
{
25+
$honeypot = new Honeypot(new \Config\Honeypot());
26+
if ($honeypot->hasContent($request))
27+
{
28+
throw HoneypotException::isBot();
29+
}
30+
}
2131

22-
public function before (RequestInterface $request)
23-
{
24-
25-
// Checks honeypot field if value was entered then show blank if so.
26-
27-
$honeypot = Services::honeypot(new \Config\Honeypot());
28-
if($honeypot->hasContent($request))
29-
{
30-
throw HoneypotException::isBot();
31-
}
32-
33-
}
34-
35-
/**
36-
* Checks if Honeypot field is empty, if so
37-
* then the requester is a bot,show a blank
38-
* page
32+
/**
33+
* Attach a honypot to the current response.
3934
*
40-
* @param RequestInterface|\CodeIgniter\HTTP\IncomingRequest $request
41-
* @param ResponseInterface|\CodeIgniter\HTTP\Response $response
35+
* @param CodeIgniter\HTTP\RequestInterface $request
36+
* @param CodeIgniter\HTTP\ResponseInterface $response
4237
* @return mixed
4338
*/
39+
public function after(RequestInterface $request, ResponseInterface $response)
40+
{
41+
$honeypot = new Honeypot(new \Config\Honeypot());
42+
$honeypot->attachHoneypot($response);
43+
}
4444

45-
public function after (RequestInterface $request, ResponseInterface $response)
46-
{
47-
48-
$honeypot = Services::honeypot(new \Config\Honeypot());
49-
$honeypot->attachHoneypot($response);
50-
}
5145
}

system/Honeypot/Honeypot.php

Lines changed: 57 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace CodeIgniter\Honeypot;
1+
<?php
2+
3+
namespace CodeIgniter\Honeypot;
24

35
/**
46
* CodeIgniter
@@ -35,108 +37,85 @@
3537
* @since Version 3.0.0
3638
* @filesource
3739
*/
38-
3940
use CodeIgniter\Config\BaseConfig;
4041
use CodeIgniter\HTTP\RequestInterface;
4142
use CodeIgniter\HTTP\ResponseInterface;
4243
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
4344

44-
class Honeypot
45+
class Honeypot
4546
{
4647

47-
/**
48-
* Honeypot Template
49-
* @var String
50-
*/
51-
protected $template;
52-
53-
/**
54-
* Honeypot text field name
55-
* @var String
48+
/**
49+
* @var BaseConfig
5650
*/
57-
protected $name;
51+
protected $config;
5852

59-
/**
60-
* Honeypot lable content
61-
* @var String
62-
*/
63-
protected $label;
53+
//--------------------------------------------------------------------
6454

65-
/**
66-
* Self Instance of Class
67-
* @var Honeypot
68-
*/
69-
protected $config;
55+
function __construct(BaseConfig $config)
56+
{
57+
$this->config = $config;
7058

71-
//--------------------------------------------------------------------
59+
if ($this->config->hidden === '')
60+
{
61+
throw HoneypotException::forNoHiddenValue();
62+
}
7263

73-
function __construct (BaseConfig $config) {
74-
$this->config = $config;
64+
if ($this->config->template === '')
65+
{
66+
throw HoneypotException::forNoTemplate();
67+
}
7568

76-
if($this->config->hidden === '')
77-
{
78-
throw HoneypotException::forNoHiddenValue();
79-
}
69+
if ($this->config->name === '')
70+
{
71+
throw HoneypotException::forNoNameField();
72+
}
73+
}
8074

81-
if($this->config->template === '')
82-
{
83-
throw HoneypotException::forNoTemplate();
84-
}
75+
//--------------------------------------------------------------------
8576

86-
if($this->config->name === '')
87-
{
88-
throw HoneypotException::forNoNameField();
89-
}
90-
}
91-
92-
//--------------------------------------------------------------------
93-
94-
/**
77+
/**
9578
* Checks the request if honeypot field has data.
9679
*
9780
* @param \CodeIgniter\HTTP\RequestInterface $request
9881
*
9982
*/
100-
public function hasContent(RequestInterface $request)
101-
{
102-
if($request->getVar($this->config->name))
103-
{
104-
return true;
105-
}
106-
return false;
107-
}
108-
109-
/**
110-
* Attachs Honeypot template to response.
83+
public function hasContent(RequestInterface $request)
84+
{
85+
return ( ! empty($request->getPost($this->config->name))) ? true : false;
86+
}
87+
88+
/**
89+
* Attaches Honeypot template to response.
11190
*
11291
* @param \CodeIgniter\HTTP\ResponseInterface $response
11392
*/
114-
public function attachHoneypot(ResponseInterface $response)
115-
{
116-
$prep_field = $this->prepareTemplate($this->config->template);
117-
118-
$body = $response->getBody();
119-
$body = str_ireplace('</form>', $prep_field, $body);
120-
$response->setBody($body);
121-
}
122-
123-
/**
93+
public function attachHoneypot(ResponseInterface $response)
94+
{
95+
$prep_field = $this->prepareTemplate($this->config->template);
96+
97+
$body = $response->getBody();
98+
$body = str_ireplace('</form>', $prep_field, $body);
99+
$response->setBody($body);
100+
}
101+
102+
/**
124103
* Prepares the template by adding label
125-
* content and field name.
104+
* content and field name.
126105
*
127106
* @param string $template
128107
* @return string
129108
*/
130-
protected function prepareTemplate($template): string
131-
{
132-
$template = str_ireplace('{label}', $this->config->label, $template);
133-
$template = str_ireplace('{name}', $this->config->name, $template);
134-
135-
if($this->config->hidden)
136-
{
137-
$template = '<div style="display:none">'. $template . '</div>';
138-
}
139-
return $template;
140-
}
141-
142-
}
109+
protected function prepareTemplate($template): string
110+
{
111+
$template = str_ireplace('{label}', $this->config->label, $template);
112+
$template = str_ireplace('{name}', $this->config->name, $template);
113+
114+
if ($this->config->hidden)
115+
{
116+
$template = '<div style="display:none">' . $template . '</div>';
117+
}
118+
return $template;
119+
}
120+
121+
}

0 commit comments

Comments
 (0)