Skip to content

Commit d17f631

Browse files
authored
Merge pull request #38 from CleanTalk/dev
Updating to 2.14
2 parents 1da671e + eed2a32 commit d17f631

10 files changed

+186
-150
lines changed

CHANGELOG

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
Version 2.13 June 20 July, 2017
1+
Version 2.14 Septebmer 14, 2017
2+
------------------------------
3+
- Support for large bases in users check.
4+
- Error fixes.
5+
6+
Version 2.13 June 20, 2017
27
------------------------------
38
- Compatibility for PHP versions lower 5.2, servers without Apache.
49
- Error fixes.

CleantalkMod.php

+116-99
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
require_once(dirname(__FILE__) . '/CleantalkSFW.php');
2525

2626
// Common CleanTalk options
27-
define('CT_AGENT_VERSION', 'smf-213');
27+
define('CT_AGENT_VERSION', 'smf-214');
2828
define('CT_SERVER_URL', 'http://moderate.cleantalk.org');
2929
define('CT_DEBUG', false);
3030

@@ -36,7 +36,7 @@ function cleantalk_sfw_check()
3636
{
3737
global $modSettings, $user_info;
3838

39-
if(!empty($modSettings['cleantalk_sfw']) && !$user_info['is_admin']){
39+
if(!empty($modSettings['cleantalk_sfw']) && !empty($modSettings['cleantalk_api_key_is_ok']) && !$user_info['is_admin']){
4040

4141
$sfw = new CleantalkSFW();
4242
$key = $modSettings['cleantalk_api_key'];
@@ -49,6 +49,7 @@ function cleantalk_sfw_check()
4949
if(isset($_COOKIE['ct_sfw_pass_key']) && $_COOKIE['ct_sfw_pass_key'] == md5($curr_ip.$key)){
5050

5151
$is_sfw_check=false;
52+
5253
if(!empty($_COOKIE['ct_sfw_passed'])){
5354
$sfw->sfw_update_logs($curr_ip, 'passed');
5455
setcookie ('ct_sfw_passed', '0', 1, "/");
@@ -412,10 +413,10 @@ function cleantalk_print_js_input()
412413
$value = cleantalk_get_checkjs_code();
413414

414415
return '<script type="text/javascript">
415-
var d = new Date(),
416+
var ct_date = new Date(),
416417
ctTimeMs = new Date().getTime(),
417418
ctMouseEventTimerFlag = true, //Reading interval flag
418-
ctMouseData = "[",
419+
ctMouseData = [],
419420
ctMouseDataCounter = 0;
420421
421422
function ctSetCookie(c_name, value) {
@@ -429,7 +430,7 @@ function ctSetCookie(c_name, value) {
429430
430431
setTimeout(function(){
431432
ctSetCookie("ct_checkjs", "'.$value.'");
432-
ctSetCookie("ct_timezone", d.getTimezoneOffset()/60*(-1));
433+
ctSetCookie("ct_timezone", ct_date.getTimezoneOffset()/60*(-1));
433434
},1000);
434435
435436
//Writing first key press timestamp
@@ -441,20 +442,24 @@ function ctSetCookie(c_name, value) {
441442
442443
//Reading interval
443444
var ctMouseReadInterval = setInterval(function(){
444-
ctMouseEventTimerFlag = true;
445-
}, 150);
445+
ctMouseEventTimerFlag = true;
446+
}, 150);
446447
447448
//Writting interval
448449
var ctMouseWriteDataInterval = setInterval(function(){
449-
var ctMouseDataToSend = ctMouseData.slice(0,-1).concat("]");
450-
ctSetCookie("ct_pointer_data", ctMouseDataToSend);
451-
}, 1200);
450+
ctSetCookie("ct_pointer_data", JSON.stringify(ctMouseData));
451+
}, 1200);
452452
453-
//Logging mouse position each 300 ms
453+
//Logging mouse position each 150 ms
454454
var ctFunctionMouseMove = function output(event){
455455
if(ctMouseEventTimerFlag == true){
456-
var mouseDate = new Date();
457-
ctMouseData += "[" + Math.round(event.pageY) + "," + Math.round(event.pageX) + "," + Math.round(mouseDate.getTime() - ctTimeMs) + "],";
456+
457+
ctMouseData.push([
458+
Math.round(event.pageY),
459+
Math.round(event.pageX),
460+
Math.round(new Date().getTime() - ctTimeMs)
461+
]);
462+
458463
ctMouseDataCounter++;
459464
ctMouseEventTimerFlag = false;
460465
if(ctMouseDataCounter >= 100){
@@ -465,10 +470,11 @@ function ctSetCookie(c_name, value) {
465470
466471
//Stop mouse observing function
467472
function ctMouseStopData(){
468-
if(typeof window.addEventListener == "function")
473+
if(typeof window.addEventListener == "function"){
469474
window.removeEventListener("mousemove", ctFunctionMouseMove);
470-
else
475+
}else{
471476
window.detachEvent("onmousemove", ctFunctionMouseMove);
477+
}
472478
clearInterval(ctMouseReadInterval);
473479
clearInterval(ctMouseWriteDataInterval);
474480
}
@@ -584,7 +590,7 @@ function cleantalk_load()
584590
$ct_request->auth_key = cleantalk_get_api_key();
585591
$ct_request->feedback = '0:'.CT_AGENT_VERSION;
586592
$ct_result = $ct->sendFeedback($ct_request);
587-
// unset($ct, $ct_request);
593+
unset($ct, $ct_request);
588594

589595
// Check if key is valid
590596
if($_POST['cleantalk_api_key'] != $modSettings['cleantalk_api_key']){
@@ -604,9 +610,35 @@ function cleantalk_load()
604610
false
605611
);
606612

613+
}else{
614+
updateSettings(
615+
array(
616+
'cleantalk_api_key_is_ok' => 0,
617+
'cleantalk_show_notice' => 0,
618+
'cleantalk_renew' => 0,
619+
'cleantalk_trial' => 0,
620+
'cleantalk_user_token' => '',
621+
'cleantalk_spam_count' => 0,
622+
'cleantalk_moderate_ip' => 0,
623+
'cleantalk_show_review' => 0,
624+
'cleantalk_ip_license' => 0,
625+
)
626+
);
607627
}
608-
else
609-
updateSettings(array('cleantalk_api_key_is_ok' => '0', false));
628+
}else{
629+
updateSettings(
630+
array(
631+
'cleantalk_api_key_is_ok' => 0,
632+
'cleantalk_show_notice' => 0,
633+
'cleantalk_renew' => 0,
634+
'cleantalk_trial' => 0,
635+
'cleantalk_user_token' => '',
636+
'cleantalk_spam_count' => 0,
637+
'cleantalk_moderate_ip' => 0,
638+
'cleantalk_show_review' => 0,
639+
'cleantalk_ip_license' => 0,
640+
)
641+
);
610642
}
611643
}
612644

@@ -618,7 +650,7 @@ function cleantalk_load()
618650

619651
// Deleting selected users
620652
if(isset($_POST['ct_del_user']))
621-
{
653+
{
622654
checkSession('request');
623655

624656
if (!isset($db_connection) || $db_connection === false)
@@ -657,7 +689,7 @@ function cleantalk_load()
657689
}
658690

659691
/* Cron for update SFW */
660-
if(!empty($modSettings['cleantalk_api_key_is_ok']) && !empty($modSettings['cleantalk_sfw']) && isset($modSettings['cleantalk_sfw_last_update']) && $modSettings['cleantalk_sfw_last_update'] < time() || !empty($doing_cron)){
692+
if(!empty($modSettings['cleantalk_sfw']) && (!empty($doing_cron) || (!empty($modSettings['cleantalk_api_key_is_ok']) && ((isset($modSettings['cleantalk_sfw_last_update']) && $modSettings['cleantalk_sfw_last_update'] < time()) || !isset($modSettings['cleantalk_sfw_last_update']) ) ))){
661693

662694
$sfw = new CleantalkSFW;
663695
$sfw->sfw_update($modSettings['cleantalk_api_key']);
@@ -667,7 +699,7 @@ function cleantalk_load()
667699
}
668700

669701
/* Cron for send SFW logs */
670-
if(!empty($modSettings['cleantalk_api_key_is_ok']) && !empty($modSettings['cleantalk_sfw']) && isset($modSettings['cleantalk_sfw_last_logs_sent']) && $modSettings['cleantalk_sfw_last_logs_sent'] < time() || !empty($doing_cron)){
702+
if(!empty($modSettings['cleantalk_sfw']) && (!empty($doing_cron) || (!empty($modSettings['cleantalk_api_key_is_ok']) && ((isset($modSettings['cleantalk_sfw_last_logs_sent']) && $modSettings['cleantalk_sfw_last_logs_sent'] < time()) || !isset($modSettings['cleantalk_sfw_last_logs_sent']) ) ))){
671703

672704
$sfw = new CleantalkSFW;
673705
$sfw->send_logs($modSettings['cleantalk_api_key']);
@@ -797,7 +829,7 @@ function template_cleantalk_above()
797829
// }
798830

799831
// Bad key banner
800-
if(empty($modSettings['cleantalk_api_key_is_ok']) && (empty($_GET['area']) || (isset($_GET['area'])&& $_GET['area'] != 'modsettings'))){
832+
if(empty($modSettings['cleantalk_api_key_is_ok']) && (empty($_GET['area']) || (isset($_GET['area']) && $_GET['area'] != 'modsettings'))){
801833

802834
echo "<div style='margin-bottom: 1.2em; padding: 5px;'>"
803835
."<h1>"
@@ -836,7 +868,7 @@ function cleantalk_buffer($buffer)
836868
if (SMF == 'SSI')
837869
return $buffer;
838870

839-
if($user_info['is_admin'] && isset($_GET['action'], $_GET['area']) && $_GET['action'] == 'admin' && $_GET['area'] == 'modsettings'){// && false){
871+
if($user_info['is_admin'] && isset($_GET['action'], $_GET['area']) && $_GET['action'] == 'admin' && $_GET['area'] == 'modsettings'){
840872

841873
if(strpos($forum_version, 'SMF 2.0')===false){
842874

@@ -863,91 +895,76 @@ function cleantalk_buffer($buffer)
863895

864896
db_extend('packages');
865897

866-
$cols = $smcFunc['db_list_columns'] ('{db_prefix}members', 0);
898+
// Unmark all users
899+
$sql = 'UPDATE {db_prefix}members SET ct_marked = 0';
900+
$result = $smcFunc['db_query']('', $sql, Array());
901+
$smcFunc['db_free_result']($result);
867902

868-
if(in_array('ct_marked', $cols)){
903+
// Cicle params
904+
$offset = 0;
905+
$per_request = 500;
869906

870-
$sql = 'UPDATE {db_prefix}members set ct_marked=0';
871-
$result = $smcFunc['db_query']('', $sql, Array());
872-
$sql = 'SELECT * FROM {db_prefix}members where passwd<>""';
907+
// Getting users to check
908+
// Making at least one DB query
909+
do{
910+
911+
$sql = "SELECT id_member, member_name, date_registered, last_login, email_address, member_ip FROM {db_prefix}members where passwd <> '' LIMIT $offset,$per_request";
873912
$result = $smcFunc['db_query']('', $sql, Array());
874-
$users=Array();
875-
$users[0]=Array();
876-
$data=Array();
877-
$data[0]=Array();
878-
$cnt=0;
913+
914+
// Break if result is empty.
915+
if($smcFunc['db_num_rows'] ($result) == 0)
916+
break;
917+
918+
// Setting data
919+
$data = array();
879920
while($row = $smcFunc['db_fetch_assoc'] ($result)){
880-
881-
$users[$cnt][] = array(
882-
'name' => $row['member_name'],
883-
'id' => $row['id_member'],
884-
'email' => $row['email_address'],
885-
'ip' => $row['member_ip'],
886-
'joined' => $row['date_registered'],
887-
'visit' => $row['last_login'],
888-
);
889-
890-
$data[$cnt][]=$row['email_address'];
891-
$data[$cnt][]=$row['member_ip'];
892-
893-
if(sizeof($users[$cnt])>450){
894-
$cnt++;
895-
$users[$cnt]=Array();
896-
$data[$cnt]=Array();
897-
}
921+
$data[] = $row['email_address'];
922+
$data[] = $row['member_ip'];
898923
}
899-
900-
$error="";
901-
902-
for($i=0;$i<sizeof($users);$i++){
903-
$send=implode(',',$data[$i]);
904-
$req="data=$send";
905-
$opts = array(
906-
'http'=>array(
907-
'method'=>"POST",
908-
'content'=>$req,
909-
)
910-
);
911-
912-
$context = stream_context_create($opts);
913-
$result = @file_get_contents("https://api.cleantalk.org/?method_name=spam_check_cms&auth_key=".cleantalk_get_api_key(), 0, $context);
914-
$result=json_decode($result);
915-
if(isset($result->error_message)){
916-
$error=$result->error_message;
917-
}else{
924+
925+
// Request
926+
$api_result = CleantalkHelper::spamCheckCms(cleantalk_get_api_key(), $data);
927+
928+
// Error handling
929+
if(!empty($api_result['error'])){
930+
break;
931+
}else{
918932

919-
if(isset($result->data)){
920-
921-
foreach($result->data as $key=>$value){
922-
923-
if($key === filter_var($key, FILTER_VALIDATE_IP)){
924-
925-
if($value->appears==1){
926-
927-
$sql = 'UPDATE {db_prefix}members set ct_marked=1 where member_ip="'.$key.'"';
928-
$result = $smcFunc['db_query']('', $sql, Array('db_error_skip' => true));
929-
}
930-
931-
}else{
932-
933-
if($value->appears==1){
934-
$sql = 'UPDATE {db_prefix}members set ct_marked=1 where email_address="'.$key.'"';
935-
$result = $smcFunc['db_query']('', $sql, Array('db_error_skip' => true));
936-
}
937-
}
933+
foreach($api_result as $key => $value){
934+
935+
// Mark spam users
936+
if($key === filter_var($key, FILTER_VALIDATE_IP)){
937+
if($value['appears'] == 1){
938+
$sql = 'UPDATE {db_prefix}members set ct_marked=1 where member_ip="'.$key.'"';
939+
$sub_result = $smcFunc['db_query']('', $sql, Array('db_error_skip' => true));
940+
$smcFunc['db_free_result']($sub_result);
941+
}
942+
}else{
943+
if($value['appears'] == 1){
944+
$sql = 'UPDATE {db_prefix}members set ct_marked=1 where email_address="'.$key.'"';
945+
$sub_result = $smcFunc['db_query']('', $sql, Array('db_error_skip' => true));
946+
$smcFunc['db_free_result']($sub_result);
938947
}
939948
}
940949
}
950+
unset($key, $value);
941951
}
952+
953+
$offset += $per_request;
954+
955+
} while( true );
956+
957+
// Free result when it's all done
958+
$smcFunc['db_free_result']($result);
942959

943-
if($error!='')
944-
$html.='<center>'
945-
.'<div style="border:2px solid red;color:red;font-size:16px;width:300px;padding:5px;">'
946-
.'<b>'.$error.'</b>'
947-
.'</div>'
948-
.'<br>'
949-
.'</center>';
950-
960+
// Error output
961+
if(!empty($api_result['error'])){
962+
$html.='<center>'
963+
.'<div style="border:2px solid red;color:red;font-size:16px;width:300px;padding:5px;">'
964+
.'<b>'.$error.'</b>'
965+
.'</div>'
966+
.'<br>'
967+
.'</center>';
951968
}
952969

953970
$sql = "SELECT * FROM {db_prefix}members WHERE ct_marked=1";
@@ -972,7 +989,7 @@ function cleantalk_buffer($buffer)
972989
$sql = "SELECT * FROM {db_prefix}members WHERE ct_marked=1 LIMIT $offset, $on_page";
973990
$result = $smcFunc['db_query']('', $sql, Array());
974991

975-
if($pages && $pages != 1){
992+
if($pages > 1){
976993
$html.= "<div style='margin: 10px;'>"
977994
."<b>".$txt['cleantalk_check_users_pages'].":</b>"
978995
."<ul style='display: inline-block; margin: 0; padding: 0;'>";
@@ -1020,7 +1037,7 @@ function cleantalk_buffer($buffer)
10201037

10211038
$html.="</tbody></table></center>";
10221039

1023-
if($pages && $pages != 1){
1040+
if($pages > 1){
10241041
$html.= "<div style='margin: 10px;'>"
10251042
."<b>".$txt['cleantalk_check_users_pages'].":</b>"
10261043
."<ul style='display: inline-block; margin: 0; padding: 0;'>";

RoboFile.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class RoboFile extends \Robo\Tasks
99
{
1010
const PACKAGE = 'antispam_cleantalk_smf';
11-
const VERSION = '2.13';
11+
const VERSION = '2.14';
1212

1313
const SMF_VERSION = '2.0.14'; // for forumPrepare
1414

0 commit comments

Comments
 (0)