Skip to content

Commit b6ca88e

Browse files
author
davydovct
committed
Fix: Missing UTF-8 func, correct sender_email handler in comments.
1 parent 7f6eca9 commit b6ca88e

File tree

2 files changed

+153
-128
lines changed

2 files changed

+153
-128
lines changed

cleantalk.antispam/include.php

Lines changed: 137 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -46,112 +46,112 @@ class CleantalkAntispam {
4646
const KEYS_NUM = 12; // 12 last JS keys are valid
4747

4848
const APBCT_REMOTE_CALL_SLEEP = 10;
49-
50-
/**
51-
* Wrapper for Bitrix agent to prevent database block ofr 10 minutes
52-
*
53-
* @return string
54-
*/
55-
static public function sfw_update__agent(){
56-
57-
self::sfw_update();
58-
59-
return 'CleantalkAntispam::sfw_update__agent();';
60-
}
61-
62-
/**
63-
* Updates SFW local database
64-
*
65-
* @param string $key
66-
*
67-
* @return string
68-
*/
69-
static public function sfw_update( $key = '' ){
70-
71-
$is_sfw = COption::GetOptionInt( 'cleantalk.antispam', 'form_sfw', 0 );
72-
$key = $key ? $key : COption::GetOptionString( 'cleantalk.antispam', 'key', '' );
73-
$key_is_ok = COption::GetOptionInt( 'cleantalk.antispam', 'key_is_ok', 0);
74-
$host_url = COption::GetOptionString( 'cleantalk.antispam', 'host_url', 0);
75-
76-
COption::SetOptionString( 'cleantalk.antispam', 'sfw_update_result', 'OK' );
77-
78-
if( ! empty( $key ) && ! empty( $key_is_ok ) ){
79-
80-
if( $is_sfw ){
81-
82-
$sfw = new CleantalkSFW( $key );
83-
84-
$file_url_hash = isset($_GET['file_url_hash']) ? urldecode($_GET['file_url_hash']) : null;
85-
86-
$file_url_nums = isset($_GET['file_url_nums']) ? urldecode($_GET['file_url_nums']) : null;
87-
$file_url_nums = isset($file_url_nums) ? explode(',', $file_url_nums) : null;
88-
89-
if( ! isset( $file_url_hash, $file_url_nums ) ){
90-
91-
$sfw->sfw_update( $host_url );
92-
93-
}elseif( $file_url_hash && is_array( $file_url_nums ) && count( $file_url_nums ) ){
94-
95-
$result = $sfw->sfw_update( $host_url, $file_url_hash, $file_url_nums[0] );
96-
97-
if( empty( $result['error'] ) ){
98-
99-
array_shift( $file_url_nums );
100-
101-
if( count( $file_url_nums ) ){
102-
103-
CleantalkHelper::http__request(
104-
$host_url,
105-
array(
106-
'spbc_remote_call_token' => md5( $key ),
107-
'spbc_remote_call_action' => 'sfw_update',
108-
'plugin_name' => 'apbct',
109-
'file_url_hash' => $file_url_hash,
110-
'file_url_nums' => implode(',', $file_url_nums),
111-
),
112-
array( 'get', 'async' )
113-
);
114-
115-
// Success. Update completed.
116-
}else
117-
COption::SetOptionInt('cleantalk.antispam', 'sfw_last_update', time());
118-
} else
119-
COption::SetOptionString( 'cleantalk.antispam', 'sfw_update_result', json_encode( $result ) );
120-
}else
121-
COption::SetOptionString( 'cleantalk.antispam', 'sfw_update_result', json_encode( array( 'error' => 'SFW_UPDATE WRONG_FILE_URLS', 'file_url_hash' => $file_url_hash, '$file_url_nums' => $file_url_nums) ) );
122-
}else
123-
COption::SetOptionString( 'cleantalk.antispam', 'sfw_update_result', json_encode( array( 'error' => 'SFW_IS_DISABLED' ) ) );
49+
50+
/**
51+
* Wrapper for Bitrix agent to prevent database block ofr 10 minutes
52+
*
53+
* @return string
54+
*/
55+
static public function sfw_update__agent(){
56+
57+
self::sfw_update();
58+
59+
return 'CleantalkAntispam::sfw_update__agent();';
60+
}
61+
62+
/**
63+
* Updates SFW local database
64+
*
65+
* @param string $key
66+
*
67+
* @return string
68+
*/
69+
static public function sfw_update( $key = '' ){
70+
71+
$is_sfw = COption::GetOptionInt( 'cleantalk.antispam', 'form_sfw', 0 );
72+
$key = $key ? $key : COption::GetOptionString( 'cleantalk.antispam', 'key', '' );
73+
$key_is_ok = COption::GetOptionInt( 'cleantalk.antispam', 'key_is_ok', 0);
74+
$host_url = COption::GetOptionString( 'cleantalk.antispam', 'host_url', 0);
75+
76+
COption::SetOptionString( 'cleantalk.antispam', 'sfw_update_result', 'OK' );
77+
78+
if( ! empty( $key ) && ! empty( $key_is_ok ) ){
79+
80+
if( $is_sfw ){
81+
82+
$sfw = new CleantalkSFW( $key );
83+
84+
$file_url_hash = isset($_GET['file_url_hash']) ? urldecode($_GET['file_url_hash']) : null;
85+
86+
$file_url_nums = isset($_GET['file_url_nums']) ? urldecode($_GET['file_url_nums']) : null;
87+
$file_url_nums = isset($file_url_nums) ? explode(',', $file_url_nums) : null;
88+
89+
if( ! isset( $file_url_hash, $file_url_nums ) ){
90+
91+
$sfw->sfw_update( $host_url );
92+
93+
}elseif( $file_url_hash && is_array( $file_url_nums ) && count( $file_url_nums ) ){
94+
95+
$result = $sfw->sfw_update( $host_url, $file_url_hash, $file_url_nums[0] );
96+
97+
if( empty( $result['error'] ) ){
98+
99+
array_shift( $file_url_nums );
100+
101+
if( count( $file_url_nums ) ){
102+
103+
CleantalkHelper::http__request(
104+
$host_url,
105+
array(
106+
'spbc_remote_call_token' => md5( $key ),
107+
'spbc_remote_call_action' => 'sfw_update',
108+
'plugin_name' => 'apbct',
109+
'file_url_hash' => $file_url_hash,
110+
'file_url_nums' => implode(',', $file_url_nums),
111+
),
112+
array( 'get', 'async' )
113+
);
114+
115+
// Success. Update completed.
116+
}else
117+
COption::SetOptionInt('cleantalk.antispam', 'sfw_last_update', time());
118+
} else
119+
COption::SetOptionString( 'cleantalk.antispam', 'sfw_update_result', json_encode( $result ) );
120+
}else
121+
COption::SetOptionString( 'cleantalk.antispam', 'sfw_update_result', json_encode( array( 'error' => 'SFW_UPDATE WRONG_FILE_URLS', 'file_url_hash' => $file_url_hash, '$file_url_nums' => $file_url_nums) ) );
122+
}else
123+
COption::SetOptionString( 'cleantalk.antispam', 'sfw_update_result', json_encode( array( 'error' => 'SFW_IS_DISABLED' ) ) );
124124
}else
125-
COption::SetOptionString( 'cleantalk.antispam', 'sfw_update_result', json_encode( array( 'error' => 'NO_VALID_APIKEY_PROVIDED_OR_SFW_DISABLED' ) ) );
126-
127-
return 'CleantalkAntispam::sfw_update();';
125+
COption::SetOptionString( 'cleantalk.antispam', 'sfw_update_result', json_encode( array( 'error' => 'NO_VALID_APIKEY_PROVIDED_OR_SFW_DISABLED' ) ) );
126+
127+
return 'CleantalkAntispam::sfw_update();';
128128
}
129129

130130
/*
131131
* Sends and clean local logs storage
132132
*/
133133
static public function sfw_send_logs( $key = '' ){
134-
135-
$is_sfw = COption::GetOptionInt( 'cleantalk.antispam', 'form_sfw', 0 );
136-
$key = $key ? $key : COption::GetOptionString( 'cleantalk.antispam', 'key', '' );
137-
$key_is_ok = COption::GetOptionInt( 'cleantalk.antispam', 'key_is_ok', 0);
138-
139-
if( ! empty( $key ) && ! empty( $key_is_ok ) ){
140-
141-
if( ! empty( $is_sfw ) ){
142-
143-
$sfw = new CleantalkSFW($key);
144-
$result = $sfw->send_logs();
145-
146-
if( ! empty( $result['error'] ) )
147-
COption::SetOptionString( 'cleantalk.antispam', 'sfw_send_log_result', json_encode( $result ) );
148-
else
149-
COption::SetOptionInt( 'cleantalk.antispam', 'sfw_last_send_log', time());
150-
151-
}else
152-
COption::SetOptionString( 'cleantalk.antispam', 'sfw_send_log_result', json_encode( array( 'error' => 'SFW_IS_DISABLED' ) ) );
153-
}else
154-
COption::SetOptionString( 'cleantalk.antispam', 'sfw_send_log_result', json_encode( array( 'error' => 'NO_VALID_APIKEY_PROVIDED' ) ) );
134+
135+
$is_sfw = COption::GetOptionInt( 'cleantalk.antispam', 'form_sfw', 0 );
136+
$key = $key ? $key : COption::GetOptionString( 'cleantalk.antispam', 'key', '' );
137+
$key_is_ok = COption::GetOptionInt( 'cleantalk.antispam', 'key_is_ok', 0);
138+
139+
if( ! empty( $key ) && ! empty( $key_is_ok ) ){
140+
141+
if( ! empty( $is_sfw ) ){
142+
143+
$sfw = new CleantalkSFW($key);
144+
$result = $sfw->send_logs();
145+
146+
if( ! empty( $result['error'] ) )
147+
COption::SetOptionString( 'cleantalk.antispam', 'sfw_send_log_result', json_encode( $result ) );
148+
else
149+
COption::SetOptionInt( 'cleantalk.antispam', 'sfw_last_send_log', time());
150+
151+
}else
152+
COption::SetOptionString( 'cleantalk.antispam', 'sfw_send_log_result', json_encode( array( 'error' => 'SFW_IS_DISABLED' ) ) );
153+
}else
154+
COption::SetOptionString( 'cleantalk.antispam', 'sfw_send_log_result', json_encode( array( 'error' => 'NO_VALID_APIKEY_PROVIDED' ) ) );
155155

156156
return 'CleantalkAntispam::sfw_send_logs();';
157157
}
@@ -215,18 +215,18 @@ public function OnPageStartHandler()
215215
$sfw_last_update = COption::GetOptionInt( 'cleantalk.antispam', 'sfw_last_update', 0);
216216
$sfw_last_send_log = COption::GetOptionInt( 'cleantalk.antispam', 'sfw_last_send_log', 0);
217217
$new_checked = time();
218-
218+
219219
// Don't take any actions if module is disabled
220-
if( ! $ct_status )
221-
return;
222-
220+
if( ! $ct_status )
221+
return;
222+
223223
// Remote calls
224224
if(isset( $_GET['spbc_remote_call_token'], $_GET['spbc_remote_call_action'], $_GET['plugin_name']) && in_array($_GET['plugin_name'], array('antispam','anti-spam', 'apbct'))){
225225
self::apbct_remote_call__perform();
226226
}
227227

228-
if( ! $USER->IsAdmin() ){
229-
228+
if( ! $USER->IsAdmin() ){
229+
230230
// Set cookies
231231
if( ! headers_sent() )
232232
self::ct_cookie();
@@ -236,8 +236,8 @@ public function OnPageStartHandler()
236236
$sfw = new CleantalkSFW($ct_key);
237237
$sfw->check_ip();
238238
}
239-
240-
// Global check
239+
240+
// Global check
241241
if ($ct_status == 1 && $ct_global == 1) {
242242
// Exclusions
243243
if( empty($_POST) ||
@@ -633,6 +633,7 @@ function OnBeforePrmediaCommentAddHandler(&$arFields) {
633633
*/
634634
function OnBeforeCommentAddHandler(&$arFields) {
635635
global $APPLICATION, $USER;
636+
636637
$ct_status = COption::GetOptionInt('cleantalk.antispam', 'status', 0);
637638
$ct_comment_blog = COption::GetOptionInt('cleantalk.antispam', 'form_comment_blog', 0);
638639
if ($ct_status == 1 && $ct_comment_blog == 1) {
@@ -651,7 +652,13 @@ function OnBeforeCommentAddHandler(&$arFields) {
651652
}
652653
$aComment['sender_email'] = $USER->GetEmail();
653654
} else {
654-
$aComment['sender_email'] = isset($arFields['EMAIL']) ? $arFields['EMAIL'] : '';
655+
if (isset($arFields['EMAIL'])) {
656+
$aComment['sender_email'] = $arFields['EMAIL'];
657+
} elseif (isset($arFields['AUTHOR_EMAIL'])) {
658+
$aComment['sender_email'] = $arFields['AUTHOR_EMAIL'];
659+
} else {
660+
$aComment['sender_email'] = '';
661+
}
655662
}
656663

657664

@@ -660,24 +667,24 @@ function OnBeforeCommentAddHandler(&$arFields) {
660667
$aComment['subject'] = '';
661668
$aComment['message'] = isset($arFields['POST_TEXT']) ? array($arFields['POST_TEXT']) : array();
662669
$aComment['example'] = array();
663-
664-
if(COption::GetOptionInt('cleantalk.antispam', 'form_send_example', 0) == 1){
665-
$arPost = CBlogPost::GetByID($arFields['POST_ID']);
666-
if(is_array($arPost)){
667-
$aComment['example']['title'] = $arPost['TITLE'];
668-
$aComment['example']['body'] = $arPost['DETAIL_TEXT'];
669-
// Find last 10 approved comments
670-
$db_res = CBlogComment::GetList(
671-
array('DATE_CREATE' => 'DESC'),
672-
array('POST_ID'=> $arFields['POST_ID'], 'PUBLISH_STATUS' => BLOG_PUBLISH_STATUS_PUBLISH),
673-
false,
674-
array('nTopCount' => 10),
675-
array('POST_TEXT')
676-
);
677-
while($ar_res = $db_res->Fetch())
678-
$aComment['example']['comments'] .= $ar_res['TITLE'] . "\n\n" . $ar_res['POST_TEXT'] . "\n\n";
679-
$aComment['example']['comments'] = json_encode($aComment['example']['comments']);
680-
}
670+
671+
if(COption::GetOptionInt('cleantalk.antispam', 'form_send_example', 0) == 1){
672+
$arPost = CBlogPost::GetByID($arFields['POST_ID']);
673+
if(is_array($arPost)){
674+
$aComment['example']['title'] = $arPost['TITLE'];
675+
$aComment['example']['body'] = $arPost['DETAIL_TEXT'];
676+
// Find last 10 approved comments
677+
$db_res = CBlogComment::GetList(
678+
array('DATE_CREATE' => 'DESC'),
679+
array('POST_ID'=> $arFields['POST_ID'], 'PUBLISH_STATUS' => BLOG_PUBLISH_STATUS_PUBLISH),
680+
false,
681+
array('nTopCount' => 10),
682+
array('POST_TEXT')
683+
);
684+
while($ar_res = $db_res->Fetch())
685+
$aComment['example']['comments'] .= $ar_res['TITLE'] . "\n\n" . $ar_res['POST_TEXT'] . "\n\n";
686+
$aComment['example']['comments'] = json_encode($aComment['example']['comments']);
687+
}
681688
}
682689

683690
$aResult = self::CheckAllBefore($aComment, TRUE);
@@ -1370,8 +1377,10 @@ static function CheckAllBefore(&$arEntity, $bSendEmail = FALSE, $form_errors = n
13701377
$request_params['message'] = $arEntity['message'];
13711378
$request_params['example'] = $arEntity['example'];
13721379
$request_params['post_info']['comment_type'] = 'comment';
1380+
13731381
$ct_request = new CleantalkRequest($request_params);
13741382
$ct_result = $ct->isAllowMessage($ct_request);
1383+
13751384
break;
13761385

13771386
case 'order':
@@ -1436,6 +1445,7 @@ static function CheckAllBefore(&$arEntity, $bSendEmail = FALSE, $form_errors = n
14361445
$ct_request = new CleantalkRequest($request_params);
14371446
$ct_result = $ct->isAllowMessage($ct_request);
14381447
}
1448+
14391449
$ret_val = array();
14401450
$ret_val['ct_request_id'] = $ct_result->id;
14411451

@@ -1860,7 +1870,7 @@ private static function ct_cookies_test()
18601870
private static function apbct_remote_call__perform()
18611871
{
18621872
$remote_calls_config = json_decode(COption::GetOptionString('cleantalk.antispam','remote_calls', ''),true);
1863-
1873+
18641874
$remote_action = $_GET['spbc_remote_call_action'];
18651875
$auth_key = trim(COption::GetOptionString('cleantalk.antispam', 'key', ''));
18661876

cleantalk.antispam/lib/Cleantalk/Antispam/Cleantalk.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,22 @@ private function httpRequest($msg) {
299299

300300
return $response;
301301
}
302-
302+
303+
/**
304+
* Function convert string from UTF8
305+
* param string
306+
* param string
307+
* @return string
308+
*/
309+
public function stringFromUTF8($str, $data_codepage = null){
310+
if (preg_match('//u', $str) && function_exists('mb_convert_encoding') && $data_codepage !== null)
311+
{
312+
return mb_convert_encoding($str, $data_codepage, 'UTF-8');
313+
}
314+
315+
return $str;
316+
}
317+
303318
/**
304319
* Function DNS request
305320
* @param $host

0 commit comments

Comments
 (0)