1- <?php namespace CodeIgniter \Honeypot ;
1+ <?php
2+
3+ namespace CodeIgniter \Honeypot ;
24
35/**
46 * CodeIgniter
3537 * @since Version 3.0.0
3638 * @filesource
3739 */
38-
3940use CodeIgniter \Config \BaseConfig ;
4041use CodeIgniter \HTTP \RequestInterface ;
4142use CodeIgniter \HTTP \ResponseInterface ;
4243use 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