-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsformhoneypot.php
244 lines (217 loc) · 5.74 KB
/
rsformhoneypot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
* @package RSForm!Pro
* @copyright (C) 2020 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/
// No direct access
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
class plgSystemRsformhoneypot extends CMSPlugin
{
/**
* @var bool
*/
protected $autoloadLanguage = true;
/**
* @param $args
*
*
* @since 3.0.0
*/
public function onRsformFrontendBeforeFormValidation($args)
{
$formId = $args['post']['formId'];
$data = $this->getData($formId);
if ((int) $data->HoneypotState === 1)
{
// Https://webaim.org/blog/spam_free_accessible_forms/
$spam = false;
// Detect form elements for the most common header injections and other code
if (preg_match("/bcc:|cc:|multipart|\[url|Content-Type:/i", implode($_POST['form'])))
{
$spam = true;
}
// Detect more than 3 outgoing links
if (preg_match_all("/<a|https?:/i", implode($_POST['form']), $out) > 3)
{
$spam = true;
}
// Detect content within a hidden form element
if (!empty($_POST['form'][ucfirst($data->HoneypotName)]))
{
$spam = true;
}
// // Ensure the form is posted from your server
// if ((isset($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])))
// {
// $spam = true;
// }
// Sent spammer to somewhere else
if ($spam)
{
header('Location: ' . $data->HoneypotUrl);
die;
}
}
}
/**
* @param $from
* @param $to
* @param $content
*
* @return array|string|string[]|null
*
* @since 3.0.0
*/
public function str_replace_first($from, $to, $content)
{
$from = '/' . preg_quote($from, '/') . '/';
return preg_replace($from, $to, $content, 1);
}
/**
* @param $args
*
*
* @since 3.0.0
*/
public function onRsformFrontendBeforeFormDisplay($args)
{
$formId = $args['formId'];
$formLayout = &$args['formLayout'];
$formLayoutName = &$args['formLayoutName'];
$data = $this->getData($formId);
if ($data->HoneypotState !== '1' || empty($data->HoneypotName))
{
return;
}
$style = '#rsform_' . $formId . '_page_0 .rsform-block-' . $data->HoneypotName . ' {display:none!important;visibility:hidden;}';
$newField = $this->getNewfield($formLayoutName, $data->HoneypotName);
$formLayout = self::str_replace_first('formSpan12">', 'formSpan12">' . "\n" . $newField, $formLayout);
$formLayout = self::str_replace_first('columns">', 'columns">' . "\n" . $newField, $formLayout);
RSFormProAssets::addStyleDeclaration($style);
}
/**
* @param $formLayoutName
* @param $HoneypotName
*
* @return string
*
* @since 3.0.0
*/
public function getNewField($formLayoutName, $HoneypotName)
{
switch ($formLayoutName)
{
case 'responsive':
$newField = "
<div class=\"rsform-block rsform-block-" . $HoneypotName . "\">\n"
. " <label class=\"formControlLabel\" for=\"" . ucfirst($HoneypotName) . "\">" . ucfirst($HoneypotName) . "</label>\n"
. " <div class=\"formControls\">\n"
. " <div class=\"formBody\">\n"
. " <input type=\"text\" value=\"\" size=\"20\" name=\"form[" . ucfirst($HoneypotName) . "]\" id=\"" . ucfirst($HoneypotName) . "\" class=\"rsform-input-box\">\n"
. " </div>\n"
. " </div>\n"
. " </div>\n";
break;
case 'foundation':
$newField = '
<div class="row rsform-block rsform-block-' . $HoneypotName . '">'
. '<div class="medium-3 columns">'
. '<label class="formControlLabel has-tip" data-tooltip="" aria-haspopup="true" data-disable-hover="false" tabindex="1" title="" for="' . ucfirst($HoneypotName) . '">'
. ucfirst($HoneypotName) . '</label>'
. '</div>'
. '<div class="medium-9 columns formControls">'
. '<input type="text" value="" size="20" name="form[' . ucfirst($HoneypotName) . ']" id="' . ucfirst($HoneypotName) . '" class="rsform-input-box" />'
. '</div>'
. '</div>';
break;
default:
$newField = '';
}
return $newField;
}
/**
*
*
* @since 3.0.0
*/
public function onRsformBackendAfterShowFormEditTabsTab()
{
echo '<li>';
echo HTMLHelper::_('link',
'javascript: void(0);',
'<span class="rsficon rsficon-code"></span><span class="inner-text">Honeypot</span>',
[
'id' => 'honeypot'
]
);
echo '</li>';
}
/**
*
*
* @since 3.0.0
*/
public function onRsformBackendAfterShowFormEditTabs()
{
$formId = (int) Factory::$application->input->get('formId');
$data = $this->getData($formId);
$form = new Form('honeypot');
$form->loadFile(JPATH_PLUGINS . '/system/rsformhoneypot/forms/tab.xml');
$form->bind($data);
?>
<div id="honeypot">
<fieldset class="form-horizontal">
<legend class="rsfp-legend"><?php echo Text::_('PLG_SYSTEM_RSFORMHONEYPOT_SETTINGS'); ?></legend>
<div class="alert alert-info"><?php echo Text::_('PLG_SYSTEM_RSFORMHONEYPOT_SETTINGS_DESC'); ?></div>
<?php
foreach ($form->getFieldsets() as $fieldset)
{
if ($fields = $form->getFieldset($fieldset->name))
{
foreach ($fields as $field)
{
echo $field->renderField();
}
}
}
?>
</fieldset>
</div>
<?php
}
/**
* @param $formId
*
* @return mixed|null
*
* @since 3.0.0
*/
function getData($formId)
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select(
$db->quoteName(
[
'f.HoneypotName',
'f.HoneypotUrl',
'f.HoneypotState'
]
)
)
->from(
$db->quoteName('#__rsform_forms', 'f')
)
->where(
$db->quoteName('f.FormId') . ' = ' . (int) $formId
);
$db->setQuery($query);
return $db->loadObject();
}
}