Skip to content

Commit 29c0ad7

Browse files
committed
User signup honeypot spam protection
1 parent f684f3e commit 29c0ad7

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

app/controller/user/signup.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,48 @@
2323
namespace Vvveb\Controller\User;
2424

2525
use function Vvveb\__;
26-
use function Vvveb\url;
2726
use function Vvveb\email;
2827
use function Vvveb\siteSettings;
2928
use Vvveb\System\Event;
29+
use Vvveb\System\Sites;
30+
use Vvveb\System\Traits\Spam;
3031
use Vvveb\System\User\User;
3132
use Vvveb\System\Validator;
32-
use Vvveb\System\Sites;
33+
use function Vvveb\url;
3334

3435
class Signup extends \Vvveb\Controller\Base {
36+
use Spam;
37+
3538
function addUser() {
3639
//$this->checkAlreadyLoggedIn();
3740
$validator = new Validator(['signup']);
3841

3942
if ($this->request->post &&
4043
($this->view->errors['login'] = $validator->validate($this->request->post)) === true) {
44+
$isSpam = $this->isSpam($this->request->post);
45+
4146
//allow only fields that are in the validator list and remove the rest
4247
$userInfo = $validator->filter($this->request->post);
4348
$userInfo['display_name'] = $userInfo['first_name'] . ' ' . $userInfo['last_name'];
4449
$userInfo['username'] = $userInfo['first_name'] . $userInfo['last_name'];
50+
$userInfo['spam'] = $isSpam;
4551

4652
list($userInfo) = Event :: trigger(__CLASS__, __FUNCTION__ , $userInfo);
4753

54+
//plugins can also be used to detect spam and set the flag
55+
if ($userInfo['spam']) {
56+
$this->view->errors['login'] = __('Spam');
57+
58+
return;
59+
}
60+
4861
if ($userInfo) {
4962
$result = User::add($userInfo);
5063

5164
$this->view->errors['login'] = [];
5265

5366
if ($result) {
54-
if (is_array($result)) {
67+
if (isset($result['user'])) {
5568
$message = __('User created!');
5669
$this->session->set('success', ['login' => $message]);
5770
$this->view->success['login'][] = $message;
@@ -86,7 +99,7 @@ function addUser() {
8699
$this->view->errors['login'] = __('This email is already in use. Please use another one.');
87100
}
88101
} else {
89-
$this->view->errors['login'] = __('Error creating user!');
102+
$this->view->errors['login'] = __('Error creating account!');
90103
}
91104
}
92105
}

system/traits/spam.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* Vvveb
5+
*
6+
* Copyright (C) 2022 Ziadin Givan
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
namespace Vvveb\System\Traits;
24+
25+
trait Spam {
26+
//these must be empty, they are hidden in html and only bots will fill them
27+
//todo: add dynamic field name
28+
29+
protected $spamFields = [
30+
'firstname-empty',
31+
'lastname-empty',
32+
'subject-empty',
33+
'contact-form',
34+
];
35+
36+
function checkIfSpam(&$message) {
37+
return $message;
38+
}
39+
40+
function isSpam(&$message) {
41+
$spam = false;
42+
43+
foreach ($this->spamFields as $field) {
44+
if (isset($message[$field]) && ! empty($message[$field])) {
45+
return $spam = true;
46+
}
47+
}
48+
49+
return $spam;
50+
}
51+
52+
function removeSpamCatchFields(&$message) {
53+
foreach ($this->spamFields as $field) {
54+
if (isset($message[$field])) {
55+
unset($message[$field]);
56+
}
57+
}
58+
59+
return $message;
60+
}
61+
}

0 commit comments

Comments
 (0)