@@ -60,28 +60,127 @@ public function checkIfNewExperience(Comment $original, Comment $updated): bool
60
60
$ newExperience = false ;
61
61
try {
62
62
$ maxlen = max (strlen ($ updatedText ), strlen ($ originalText ));
63
- $ calculator = new LevenshteinDistance (false , 0 , 1000 ** 2 );
63
+ $ calculator = new LevenshteinDistance (false , 0 , 1000 ** 2 );
64
64
$ iteration = 0 ;
65
65
$ maxIteration = $ maxlen / 1000 ;
66
66
while ($ iteration < $ maxIteration && !$ newExperience ) {
67
-
68
67
$ currentUpdatedText = substr ($ updatedText , $ iteration * 1000 , 1000 );
69
68
$ currentOriginalText = substr ($ originalText , $ iteration * 1000 , 1000 );
70
69
$ levenshteinDistance = ($ calculator ->calculate (
71
70
$ currentUpdatedText ,
72
- $ currentOriginalText )
71
+ $ currentOriginalText
72
+ )
73
73
)['distance ' ];
74
74
75
75
if ($ levenshteinDistance >= max (strlen ($ currentUpdatedText ), strlen ($ currentOriginalText )) / 7 ) {
76
76
$ newExperience = true ;
77
77
}
78
78
$ iteration ++;
79
79
}
80
- } catch (Throwable $ e ) {
80
+ } catch (Throwable $ e ) {
81
81
// ignore exception and just return false (likely consumed too much memory)
82
82
return $ newExperience ;
83
83
}
84
84
85
85
return $ newExperience ;
86
86
}
87
+
88
+ public function checkCommentSpam (Member $ loggedInMember , Comment $ comment ): bool
89
+ {
90
+ $ spamCheckParams = [
91
+ ['duration ' => '00:02:00 ' , 'count ' => 1 ],
92
+ ['duration ' => '00:20:00 ' , 'count ' => 5 ],
93
+ ['duration ' => '06:00:00 ' , 'count ' => 25 ],
94
+ ];
95
+
96
+ $ check1 = $ this ->checkCommentsDuration ($ loggedInMember , $ comment , $ spamCheckParams [0 ]);
97
+ $ check2 = $ this ->checkCommentsDuration ($ loggedInMember , $ comment , $ spamCheckParams [1 ]);
98
+ $ check3 = $ this ->checkCommentsDuration ($ loggedInMember , $ comment , $ spamCheckParams [2 ]);
99
+
100
+ return $ check1 || $ check2 || $ check3 ;
101
+ }
102
+
103
+ private function checkCommentsDuration (Member $ member , Comment $ comment , array $ params ): bool
104
+ {
105
+ $ duration = $ params ['duration ' ];
106
+ $ count = $ params ['count ' ];
107
+
108
+ $ result = false ;
109
+ $ commentCount = $ this ->entityManager
110
+ ->getConnection ()
111
+ ->executeQuery (
112
+ "
113
+ SELECT
114
+ COUNT(*) as cnt
115
+ FROM
116
+ comments c
117
+ WHERE
118
+ c.IdFromMember = :memberId
119
+ AND TIMEDIFF(NOW(), created) < :duration
120
+ " ,
121
+ [ ':memberId ' => $ member ->getId (), ':duration ' => $ duration ]
122
+ )
123
+ ->fetchOne ()
124
+ ;
125
+
126
+ if ($ commentCount >= $ count ) {
127
+ // Okay limit was hit, check for comment quality
128
+ // Get all comments written during the given duration
129
+ $ comments = $ this ->entityManager
130
+ ->getConnection ()
131
+ ->executeQuery (
132
+ "
133
+ SELECT
134
+ c.TextFree
135
+ FROM
136
+ comments c
137
+ WHERE
138
+ c.IdFromMember = :memberId
139
+ AND TIMEDIFF(NOW(), created) < :duration
140
+ " ,
141
+ [ ':memberId ' => $ member ->getId (), ':duration ' => $ duration ]
142
+ )
143
+ ->fetchAllAssociative ()
144
+ ;
145
+ $ result = $ this ->checkCommentSimilarity ($ comments , $ comment );
146
+ }
147
+
148
+ return $ result ;
149
+ }
150
+
151
+ private function checkCommentSimilarity (array $ comments , Comment $ comment ): bool
152
+ {
153
+ $ similar = 0 ;
154
+ $ comments [count ($ comments )] = ['TextFree ' => $ comment ->getTextfree ()];
155
+ $ count = count ($ comments );
156
+ for ($ i = 0 ; $ i < $ count - 1 ; $ i ++) {
157
+ for ($ j = $ i + 1 ; $ j < $ count ; $ j ++) {
158
+ similar_text (
159
+ $ comments [$ i ]['TextFree ' ],
160
+ $ comments [$ j ]['TextFree ' ],
161
+ $ percent
162
+ );
163
+ if ($ percent > 95 ) {
164
+ $ similar ++;
165
+ }
166
+ }
167
+ }
168
+ return $ similar != $ count * ($ count - 1 );
169
+ }
170
+
171
+ public function checkForEmailAddress (Comment $ comment ): bool
172
+ {
173
+ $ commentText = $ comment ->getTextfree ();
174
+ $ atPos = strpos ($ commentText , '@ ' );
175
+ $ whiteSpaceBefore = strrpos (substr ($ commentText , 0 , $ atPos ), ' ' );
176
+ $ whiteSpaceAfter = strpos ($ commentText , ' ' , $ atPos );
177
+ if (false === $ whiteSpaceAfter ) {
178
+ $ whiteSpaceAfter = strlen ($ commentText );
179
+ }
180
+ $ potentialEmailAddress =
181
+ substr ($ commentText , $ whiteSpaceBefore + 1 , $ whiteSpaceAfter - $ whiteSpaceBefore - 1 );
182
+ $ emailAddressFound = filter_var ($ potentialEmailAddress , FILTER_VALIDATE_EMAIL ) !== false ;
183
+
184
+ return $ emailAddressFound ;
185
+ }
87
186
}
0 commit comments