1
+ <?php namespace CodeIgniter \Honeypot ;
2
+
3
+ /**
4
+ * CodeIgniter
5
+ *
6
+ * An open source application development framework for PHP
7
+ *
8
+ * This content is released under the MIT License (MIT)
9
+ *
10
+ * Copyright (c) 2014-2018 British Columbia Institute of Technology
11
+ *
12
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ * of this software and associated documentation files (the "Software"), to deal
14
+ * in the Software without restriction, including without limitation the rights
15
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ * copies of the Software, and to permit persons to whom the Software is
17
+ * furnished to do so, subject to the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be included in
20
+ * all copies or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
+ * THE SOFTWARE.
29
+ *
30
+ * @package CodeIgniter
31
+ * @author CodeIgniter Dev Team
32
+ * @copyright 2014-2018 British Columbia Institute of Technology (https://bcit.ca/)
33
+ * @license https://opensource.org/licenses/MIT MIT License
34
+ * @link https://codeigniter.com
35
+ * @since Version 3.0.0
36
+ * @filesource
37
+ */
38
+
39
+ use CodeIgniter \Config \BaseConfig ;
40
+ use CodeIgniter \HTTP \RequestInterface ;
41
+ use CodeIgniter \HTTP \ResponseInterface ;
42
+ use Config \Honeypot as HoneypotConfig ;
43
+ use CodeIgniter \Honeypot \Exceptions \HoneypotException ;
44
+
45
+ class Honeypot
46
+ {
47
+
48
+ /**
49
+ * Honeypot Template
50
+ * @var String
51
+ */
52
+ protected $ template ;
53
+
54
+ /**
55
+ * Honeypot text field name
56
+ * @var String
57
+ */
58
+ protected $ name ;
59
+
60
+ /**
61
+ * Honeypot lable content
62
+ * @var String
63
+ */
64
+ protected $ label ;
65
+
66
+ /**
67
+ * Self Instance of Class
68
+ * @var Honeypot
69
+ */
70
+ protected $ config ;
71
+
72
+ //--------------------------------------------------------------------
73
+
74
+ function __construct (BaseConfig $ config ) {
75
+ $ this ->config = $ config ;
76
+
77
+ if ($ this ->config ->hidden === '' )
78
+ {
79
+ throw HoneypotException::forNoHiddenValue ();
80
+ }
81
+
82
+ if ($ this ->config ->template === '' )
83
+ {
84
+ throw HoneypotException::forNoTemplate ();
85
+ }
86
+
87
+ if ($ this ->config ->name === '' )
88
+ {
89
+ throw HoneypotException::forNoNameField ();
90
+ }
91
+ }
92
+
93
+ //--------------------------------------------------------------------
94
+
95
+ /**
96
+ * Checks the request if honeypot field has data.
97
+ *
98
+ * @param \CodeIgniter\HTTP\RequestInterface $request
99
+ *
100
+ */
101
+ public function hasContent (RequestInterface $ request )
102
+ {
103
+ if ($ request ->getVar ($ this ->config ->name ))
104
+ {
105
+ return true ;
106
+ }
107
+ return false ;
108
+ }
109
+
110
+ /**
111
+ * Attachs Honeypot template to response.
112
+ *
113
+ * @param \CodeIgniter\HTTP\ResponseInterface $response
114
+ */
115
+ public function attachHoneypot (ResponseInterface $ response )
116
+ {
117
+ $ prep_field = $ this ->prepareTemplate ($ this ->config ->template );
118
+
119
+ $ body = $ response ->getBody ();
120
+ $ body = str_ireplace ('</form> ' , $ prep_field , $ body );
121
+ $ response ->setBody ($ body );
122
+ }
123
+
124
+ /**
125
+ * Prepares the template by adding label
126
+ * content and field name.
127
+ *
128
+ * @param string $template
129
+ * @return string
130
+ */
131
+ protected function prepareTemplate ($ template ): string
132
+ {
133
+ $ template = str_ireplace ('{label} ' , $ this ->config ->label , $ template );
134
+ $ template = str_ireplace ('{name} ' , $ this ->config ->name , $ template );
135
+
136
+ if ($ this ->config ->hidden )
137
+ {
138
+ $ template = '<div style="display:none"> ' . $ template . '</div> ' ;
139
+ }
140
+ return $ template ;
141
+ }
142
+
143
+ }
0 commit comments