8000 Implement spam checker and auto-banner · laravelio/laravel.io@8910649 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8910649

Browse files
committed
Implement spam checker and auto-banner
1 parent b8ade8d commit 8910649

File tree

8 files changed

+251
-2
lines changed

8 files changed

+251
-2
lines changed

app/Lio/Forum/Threads/ThreadCreator.php

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php namespace Lio\Forum\Threads;
22

3+
use Illuminate\Support\MessageBag;
4+
use Lio\Accounts\User;
5+
use Lio\Validators\DoesNotContainPhoneNumbers;
6+
37
/**
48
* This class can call the following methods on the listener object:
59
*
@@ -8,11 +12,24 @@
812
*/
913
class ThreadCreator
1014
{
15+
/**
16+
* @var \Lio\Forum\Threads\ThreadRepository
17+
*/
1118
protected $threads;
1219

13-
public function __construct(ThreadRepository $threads)
20+
/**
21+
* @var \Lio\Validators\DoesNotContainPhoneNumbers
22+
*/
23+
protected $phoneNumbers;
24+
25+
/**
26+
* @param \Lio\Forum\Threads\ThreadRepository $threads
27+
* @param \Lio\Validators\DoesNotContainPhoneNumbers $phoneNumbers
28+
*/
29+
public function __construct(ThreadRepository $threads, DoesNotContainPhoneNumbers $phoneNumbers)
1430
{
1531
$this->threads = $threads;
32+
$this->phoneNumbers = $phoneNumbers;
1633
}
1734

1835
// an additional validator is optional and will be run first, an example of a usage for
@@ -41,8 +58,24 @@ private function getNew($data)
4158

4259
private function validateAndSave($thread, $listener, $data)
4360
{
61+
if ($this->containsSpam($thread->subject)) {
62+
$this->increaseUserSpamCount($thread->author);
63+
64+
return $listener->threadCreationError(
65+
new MessageBag(['subject' => 'Title contains spam. Your account has been flagged.'])
66+
);
67+
}
68+
69+
if ($this->containsSpam($thread->body)) {
70+
$this->increaseUserSpamCount($thread->author);
71+
72+
return $listener->threadCreationError(
73+
new MessageBag(['body' => 'Body contains spam. Your account has been flagged.'])
74+
);
75+
}
76+
4477
// check the model validation
45-
if ( ! $this->threads->save($thread)) {
78+
if (! $this->threads->save($thread)) {
4679
return $listener->threadCreationError($thread->getErrors());
4780
}
4881

@@ -53,4 +86,33 @@ private function validateAndSave($thread, $listener, $data)
5386

5487
return $listener->threadCreated($thread);
5588
}
89+
90+
/**
91+
* Determines if one or more subject contain spam
92+
*
93+
* @param string|array $subject
94+
* @return bool
95+
*/
96+
private function containsSpam($subject)
97+
{
98+
// If the validator detects phone numbers, return false.
99+
return ! $this->phoneNumbers->validate($subject);
100+
}
101+
102+
/**
103+
* Increases a user's spam count
104+
*
105+
* @param \Lio\Accounts\User $user
106+
*/
107+
private function increaseUserSpamCount(User $user)
108+
{
109+
$user->spam_count = $user->spam_count + 1;
110+
111+
// If the user reaches a spam threshold of 3 or more, automatically ban him
112+
if ($user->spam_count >= 3) {
113+
$user->is_banned = true;
114+
}
115+
116+
$user->save();
117+
}
56118
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace Lio\Validators;
3+
4+
class DoesNotContainPhoneNumbers
5+
{
6+
/**
7+
* Determines if the value contains phone numbers
8+
*
9+
* @param string $value
10+
* @return bool
11+
*/
12+
public function validate($value)
13+
{
14+
return ! (bool) preg_match('/\+(?:\d{1,2})?[\s-+]*?(?:\d{10,})/', $value);
15+
}
16+
}

app/controllers/AuthController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ public function userFound($user)
158158

159159
public function userIsBanned($user)
160160
{
161+
Session::flash('error', 'Your account has been banned. If you\'d like to appeal, please contact us through the support widget below.');
162+
161163
return $this->redirectHome();
162164
}
163165

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class AddSpamCounterToUsersTable extends Migration {
7+
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('users', function (Blueprint $table) {
16+
$table->tinyInteger('spam_count');
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*
23+
* @return void
24+
*/
25+
public function down()
26+
{
27+
Schema::table('users', function (Blueprint $table) {
28+
$table->dropColumn('spam_count');
29+
});
30+
}
31+
}

app/filters.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
return Redirect::action('AuthController@getLoginRequired');
3838
} elseif (Auth::user()->is_banned) {
3939
// Don't allow people who are banned to log in.
40+
Session::flash('error', 'Your account has been banned. If you\'d like to appeal, please contact us through the support widget below.');
41+
4042
Auth::logout();
4143

4244
return Redirect::home();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace Lio\Tests\Unit\Validators;
3+
4+
use Lio\Validators\DoesNotContainPhoneNumbers;
5+
6+
class DoesNotContainPhoneNumbersTest extends \PHPUnit_Framework_TestCase
7+
{
8+
/** @test */
9+
function it_can_detect_phone_numbers_in_a_string()
10+
{
11+
$validator = new DoesNotContainPhoneNumbers();
12+
13+
$this->assertFalse($validator->validate(file_get_contents(__DIR__ . '/phone_numbers.txt')));
14+
}
15+
16+
/** @test */
17+
function it_passes_when_no_phone_numbers_are_detected()
18+
{
19+
$validator = new DoesNotContainPhoneNumbers();
20+
21+
$this->assertTrue($validator->validate(file_get_contents(__DIR__ . '/no_phone_numbers.txt')));
22+
}
23+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
WORLD FAMOUS ASTROLOGER}...NAGESHEWAR BABA JI [DIAMOND
2+
GOLD MEDLIST]....[] Make one call and change your life
3+
with in 21 hours call soon get solve your all problems with in 21 hours
4+
that is 101% guaranteed We Provides Highly Effective Solutions For All
5+
Problems Like
6+
To Get Your Lost Love Back
7+
To Attract Any Girl/Boy Towards You With Heart
8+
To Make Agree 15+935=108Your Or Your Partner s Parents To Love Marriage
9+
To Solve The Problems In Any Relationship
10+
To Control The Mi1nd Of Husband/Wife Or A Desired Person
11+
To Improve Professional And Personal Relationships With Others
12+
To Wealth And Peace In Home
13+
To Kundli And Match Making
14+
To Manglik Dosh Niwaran
15+
To Tell Your15 + 935 = 108 Future
16+
To Control Of Others By Hypnotism
17+
To Dosh Nivarana Like Manglik Dosh, Kalasarp Yoga
18+
To Hawan/ Anusthan Etc.
19+
To Get Your Lost Love Back By Vashikaran
20+
To Get Your Lost Love Back By Black Magic
21+
To Get High Income From Business
22+
To Create A Good Impression On Others
23+
To Create Love And Affection In Other s Heart And Mind
24+
To Get Anything From True Astrology
25+
To Get Your Ex Back By Hypnotism
26+
To Get Your Lost Love
27+
LOVE AFFARIS
28+
HEART PIECE
29+
BLACK MAGIC
30+
31+
32+
919799138999
33+
91 9799138999
34+
91 - 9799138999
35+
91 979 913 8999
36+
91-979-913-8999
37+
91+979+913+8999
38+
39+
+91 979 913 8999
40+
+91-979-913-8999
41+
+91+979+913+8999
42+
+93
43+
15+77=21
44+
45+
LOVE BACK +91 (3) 42228242
46+
INTERCAST MARRIAGE
47+
LOVE MARRIGES
48+
CHILDREN NOT IN ETC.
49+
To Win Favors From Others
50+
Get
51+
Your Love Back By Vashikaran, Black Magic, Hypnotism, Get Your Love
52+
Back By Vashikaran, Get Your Love Back By Vashikaran, Get Your Love Back
53+
By Vashikaran, Help Me To Get My Love Back By Vashikaran, Kala Jadu,
54+
Tantra Mantra, Tankarik Baba, Indian Astrologer
55+
Just Give A Call & Get Your Love Back one call and change your life nageshewarbaba.blogspot.com email id nageshewarbaba@yahoo.com
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
+91-8872522276 WORLD FAMOUS ASTROLOGER}...NAGESHEWAR BABA JI [DIAMOND
2+
GOLD MEDLIST]....[+91-8872522276] Make one call and change your life
3+
with in 21 hours call soon get solve your all problems with in 21 hours
4+
that is 101% guaranteed We Provides Highly Effective Solutions For All
5+
Problems Like
6+
To Get Your Lost Love Back
7+
To Attract Any Girl/Boy Towards You With Heart
8+
To Make Agree 15+935=108Your Or Your Partner s Parents To Love Marriage
9+
To Solve The Problems In Any Relationship
10+
To Control The Mi1nd Of Husband/Wife Or A Desired Person
11+
To Improve Professional And Personal Relationships With Others
12+
To Wealth And Peace In Home
13+
To Kundli And Match Making
14+
To Manglik Dosh Niwaran
15+
To Tell Your15 + 935 = 108 Future
16+
To Control Of Others By Hypnotism
17+
To Dosh Nivarana Like Manglik Dosh, Kalasarp Yoga
18+
To Hawan/ Anusthan Etc.
19+
To Get Your Lost Love Back By Vashikaran
20+
To Get Your Lost Love Back By Black Magic
21+
To Get High+919799138999 Income From Business
22+
To Create A Good Impression On Others
23+
To Create Love And Affection In Other s Heart And Mind
24+
To Get Anything From True Astrology
25+
To Get Your Ex Back By Hypnotism
26+
To Get Your Lost Love
27+
LOVE 91+7742228242 AFFARIS
28+
HEART PIECE
29+
BLACK MAGIC
30+
31+
32+
919799138999
33+
91 9799138999
34+
91 - 9799138999
35+
91 979 913 8999
36+
91-979-913-8999
37+
91+979+913+8999
38+
39+
+919799138999
40+
+91 9799138999
41+
+91 - 9799138999
42+
+91 979 913 8999
43+
+91-979-913-8999
44+
+91+979+913+8999
45+
+93
46+
15+77=21
47+
48+
LOVE BACK +91 (3) 42228242
49+
INTERCAST MARRIAGE
50+
LOVE MARRIGES
51+
CHILDREN NOT IN +91+7742228242FAVOUR ETC.
52+
To Win Favors From Others
53+
Get
54+
Your Love Back By Vashikaran, Black Magic, Hypnotism, Get Your Love
55+
Back By Vashikaran, Get Your Love Back By Vashikaran, Get Your Love Back
56+
By Vashikaran, Help +91 - 8872522276 Me To Get My Love Back By Vashikaran, Kala Jadu,
57+
Tantra Mantra, Tankarik Baba, Indian Astrologer
58+
Just Give A Call & Get Your Love Back one call and change your life +91-8872522276 nageshewarbaba.blogspot.com email id nageshewarbaba@yahoo.com

0 commit comments

Comments
 (0)
0