View source
<?php
use Drupal\Core\Language\Language;
use Drupal\cleantalk\CleantalkFuncs;
require_once dirname(__FILE__) . '/src/lib/autoload.php';
use Cleantalk\ApbctDrupal\Helper as CleantalkHelper;
$cleantalk_executed = false;
function cleantalk_page_attachments_alter(array &$page) {
if (\Drupal::request()->query
->get('search')) {
if (\Drupal::config('cleantalk.settings')
->get('cleantalk_search_noindex')) {
$skip_index_results = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'robots',
'content' => 'noindex',
],
];
$page['#attached']['html_head'][] = [
$skip_index_results,
'skip_index_results',
];
}
}
if (!\Drupal::request()->query
->has('amp') && !\Drupal::service('router.admin_context')
->isAdminRoute()) {
$js_template = "var ct_check_js_val = '%s';";
$js_addon_body = sprintf($js_template, CleantalkFuncs::_cleantalk_get_checkjs_value());
$drupal_ac_antibot_cookie_value = "var drupal_ac_antibot_cookie_value = '%s';";
$drupal_ac_antibot_js_body = sprintf($drupal_ac_antibot_cookie_value, CleantalkFuncs::create_ac_cookie_value());
$use_alt_cookies = \Drupal::config('cleantalk.settings')
->get('cleantalk_alternative_cookies_session') ? 1 : 0;
$use_alt_cookies_marker = "var ct_use_alt_cookies = " . $use_alt_cookies . ";";
$page['#attached']['html_head'][] = array(
array(
'#tag' => 'script',
'#value' => $js_addon_body . $drupal_ac_antibot_js_body . $use_alt_cookies_marker,
),
'ga_scripts',
);
$page['#attached']['library'][] = 'cleantalk/apbct-public';
}
if (\Drupal::config('cleantalk.settings')
->get('cleantalk_check_external')) {
$page['#attached']['library'][] = 'cleantalk/apbct-external';
}
}
function cleantalk_form_alter(&$form, &$form_state, $form_id) {
_cleantalk_form_alter($form, $form_state, $form_id);
}
function cleantalk_validate_register(&$form, &$form_state) {
if (\Drupal::config('cleantalk.settings')
->get('cleantalk_check_register') && CleantalkFuncs::_cleantalk_check_form_submit_handlers($form_state
->getSubmitHandlers())) {
$spam_check = array();
$spam_check['type'] = 'register';
$spam_check['sender_email'] = !empty($form_state
->getValue('mail')) ? $form_state
->getValue('mail') : '';
$spam_check['sender_nickname'] = !empty($form_state
->getValue('name')) ? $form_state
->getValue('name') : '';
$spam_check['timezone'] = !empty($form_state
->getValue('timezone')) ? $form_state
->getValue('timezone') : '';
$spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check, $form_state
->getErrors());
if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1) {
$form_state
->setErrorByName('mail', strip_tags($spam_result['ct_result_comment'], '<p><a>'));
}
}
}
function cleantalk_validate_comment(&$form, &$form_state) {
if (\Drupal::config('cleantalk.settings')
->get('cleantalk_check_comments') && CleantalkFuncs::_cleantalk_check_form_submit_handlers($form_state
->getSubmitHandlers())) {
$current_user = \Drupal::currentUser();
$ct_temp_msg_data = CleantalkHelper::get_fields_any(\Drupal::request()->request
->all(), \Drupal::config('cleantalk.settings')
->get('cleantalk_fields_exclusions'));
$comment_lang = !empty($form_state
->getValue('language')) ? $form_state
->getValue('language') : Language::LANGCODE_NOT_SPECIFIED;
$spam_check = array();
$spam_check['type'] = 'comment';
if ($current_user
->id()) {
$user = \Drupal\user\Entity\User::load($current_user
->id());
$spam_check['sender_nickname'] = !empty($user
->get('name')) ? $user
->get('name')->value : '';
$spam_check['sender_email'] = !empty($user
->get('mail')->value) ? $user
->get('mail')->value : '';
}
else {
if (empty($form_state
->getValue('name'))) {
$spam_check['sender_nickname'] = $ct_temp_msg_data['nickname'] ? $ct_temp_msg_data['nickname'] : '';
}
else {
$spam_check['sender_nickname'] = $form_state
->getValue('name');
}
if (empty($form_state
->getValue('mail'))) {
$spam_check['sender_email'] = $ct_temp_msg_data['email'] ? $ct_temp_msg_data['email'] : '';
}
else {
$spam_check['sender_email'] = $form_state
->getValue('mail');
}
}
if (empty($form_state
->getValue('subject')[0]['value'])) {
$spam_check['message_title'] = $ct_temp_msg_data['subject'] ? $ct_temp_msg_data['subject'] : '';
}
else {
$spam_check['message_title'] = $form_state
->getValue('subject')[0]['value'];
}
if (empty($form_state
->getValue('comment_body')[0]['value'])) {
$spam_check['message_body'] = $ct_temp_msg_data['message'] ? $ct_temp_msg_data['message'] : '';
}
else {
$spam_check['message_body'] = $form_state
->getValue('comment_body')[0]['value'];
}
$spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check, $form_state
->getErrors());
if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1) {
if (!\Drupal::config('cleantalk.settings')
->get('cleantalk_check_comments_automod') || $spam_result['stop_queue'] == 1) {
$form_state
->setErrorByName('comment_body', strip_tags($spam_result['ct_result_comment'], '<p><a>'));
}
}
}
}
function cleantalk_validate_contact_message(&$form, &$form_state) {
if (\Drupal::config('cleantalk.settings')
->get('cleantalk_check_contact_forms') && CleantalkFuncs::_cleantalk_check_form_submit_handlers($form_state
->getSubmitHandlers())) {
$current_user = \Drupal::currentUser();
$ct_temp_msg_data = CleantalkHelper::get_fields_any(\Drupal::request()->request
->all(), \Drupal::config('cleantalk.settings')
->get('cleantalk_fields_exclusions'));
$spam_check = array();
$spam_check['type'] = 'contact';
if ($current_user
->id()) {
$user = \Drupal\user\Entity\User::load($current_user
->id());
$spam_check['sender_nickname'] = !empty($user
->get('name')) ? $user
->get('name')->value : '';
}
else {
$spam_check['sender_nickname'] = $ct_temp_msg_data['nickname'] ? $ct_temp_msg_data['nickname'] : '';
}
$spam_check['sender_email'] = $ct_temp_msg_data['email'] ? $ct_temp_msg_data['email'] : '';
$spam_check['message_title'] = $ct_temp_msg_data['subject'] ? $ct_temp_msg_data['subject'] : '';
$spam_check['message_body'] = $ct_temp_msg_data['message'] ? $ct_temp_msg_data['message'] : '';
$spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check, $form_state
->getErrors());
if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1) {
$form_state
->setErrorByName('message', strip_tags($spam_result['ct_result_comment'], '<p><a>'));
}
}
}
function cleantalk_validate_forum_topic(&$form, &$form_state) {
if (\Drupal::config('cleantalk.settings')
->get('cleantalk_check_forum_topics') && CleantalkFuncs::_cleantalk_check_form_submit_handlers($form_state
->getSubmitHandlers())) {
$current_user = \Drupal::currentUser();
$ct_temp_msg_data = CleantalkHelper::get_fields_any(\Drupal::request()->request
->all(), \Drupal::config('cleantalk.settings')
->get('cleantalk_fields_exclusions'));
$spam_check = array();
$spam_check['type'] = 'forum_topic';
if ($current_user
->id()) {
$user = \Drupal\user\Entity\User::load($current_user
->id());
$spam_check['sender_nickname'] = !empty($user
->get('name')) ? $user
->get('name')->value : '';
}
else {
$spam_check['sender_nickname'] = $ct_temp_msg_data['nickname'] ? $ct_temp_msg_data['nickname'] : '';
}
$spam_check['sender_email'] = $ct_temp_msg_data['email'] ? $ct_temp_msg_data['email'] : '';
$spam_check['message_title'] = $ct_temp_msg_data['subject'] ? $ct_temp_msg_data['subject'] : '';
$spam_check['message_body'] = $ct_temp_msg_data['message'] ? $ct_temp_msg_data['message'] : '';
$spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check, $form_state
->getErrors());
if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1) {
$form_state
->setErrorByName('message', strip_tags($spam_result['ct_result_comment'], '<p><a>'));
}
}
}
function cleantalk_validate_webform(&$form, &$form_state) {
if (\Drupal::config('cleantalk.settings')
->get('cleantalk_check_webforms')) {
if (in_array('::submit', $form_state
->getSubmitHandlers()) || CleantalkFuncs::_cleantalk_check_form_submit_handlers($form_state
->getSubmitHandlers())) {
$current_user = \Drupal::currentUser();
$spam_check = array();
$spam_check['type'] = 'webform';
if (!empty($form_state
->get('multistep'))) {
$ct_temp_msg_data = CleantalkHelper::get_fields_any($form_state
->get('multistep'), \Drupal::config('cleantalk.settings')
->get('cleantalk_fields_exclusions'));
$spam_check['multistep_submit_time'] = $form_state
->get('multistep_submit_time');
}
else {
$ct_temp_msg_data = CleantalkHelper::get_fields_any(\Drupal::request()->request
->all(), \Drupal::config('cleantalk.settings')
->get('cleantalk_fields_exclusions'));
}
if ($current_user
->id()) {
$user = \Drupal\user\Entity\User::load($current_user
->id());
$spam_check['sender_nickname'] = !empty($user
->get('name')) ? $user
->get('name')->value : '';
}
else {
$spam_check['sender_nickname'] = $ct_temp_msg_data['nickname'] ? $ct_temp_msg_data['nickname'] : '';
}
$spam_check['sender_email'] = $ct_temp_msg_data['email'] ? $ct_temp_msg_data['email'] : '';
$spam_check['message_title'] = $ct_temp_msg_data['subject'] ? $ct_temp_msg_data['subject'] : '';
$spam_check['message_body'] = $ct_temp_msg_data['message'] ? $ct_temp_msg_data['message'] : '';
$spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check, $form_state
->getErrors());
if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1) {
$form_state
->setErrorByName('message', strip_tags($spam_result['ct_result_comment'], '<p><a>'));
}
}
else {
if (empty($form_state
->get('multistep'))) {
$form_state
->set('multistep', \Drupal::request()->request
->all());
$form_state
->set('multistep_submit_time', CleantalkFuncs::apbct_getcookie('apbct_timestamp'));
}
else {
$previous_storage = $form_state
->get('multistep');
$form_state
->set('multistep', array_merge($previous_storage, \Drupal::request()->request
->all()));
}
}
}
}
function cleantalk_comment_presave(\Drupal\comment\CommentInterface $comment) {
$ct_result = CleantalkFuncs::_cleantalk_ct_result();
if (!empty($ct_result['ct_request_id'])) {
if ($ct_result['ct_result_allow'] === 0 && \Drupal::config('cleantalk.settings')
->get('cleantalk_check_comments_automod')) {
if (version_compare(\Drupal::VERSION, '8.3.0') >= 0) {
$comment
->setUnpublished();
}
else {
$comment
->setPublished(false);
}
}
}
}
function _cleantalk_form_alter(&$form, &$form_state, $form_id) {
$url_exclusion = explode(",", \Drupal::config('cleantalk.settings')
->get('cleantalk_url_exclusions'));
if (is_array($url_exclusion) && count($url_exclusion)) {
$check_type = \Drupal::config('cleantalk.settings')
->get('cleantalk_url_regexp');
foreach ($url_exclusion as $key => $value) {
if (!empty($value)) {
if ($check_type == 1) {
if (@preg_match('/' . trim($value) . '/', $_SERVER['REQUEST_URI'])) {
return;
}
}
else {
if (strpos($_SERVER['REQUEST_URI'], $value) !== false) {
return;
}
}
}
}
}
if (\Drupal::currentUser()
->hasPermission('access administration menu')) {
return;
}
if (\Drupal::config('cleantalk.settings')
->get('cleantalk_link') && $form_id != 'search_form' && $form_id != 'user_login_form' && $form_id != 'search_block_form') {
$form['cleantalk_link'] = array(
'#type' => 'item',
'#markup' => t("<a href='https://cleantalk.org/drupal-anti-spam-module-wo-captcha'>Drupal spam</a> blocked by CleanTalk."),
'#required' => FALSE,
'#weight' => 999,
);
}
if ($form_id == 'user_register_form') {
$form['#validate'][] = 'cleantalk_validate_register';
}
else {
if (preg_match('|comment(.*?)_form|', $form_id)) {
$form['#validate'][] = 'cleantalk_validate_comment';
}
if (preg_match('|contact(.*?)_form|', $form_id)) {
$form['#validate'][] = 'cleantalk_validate_contact_message';
}
if (preg_match('|webform(.*?)_form|', $form_id)) {
$form['#validate'][] = 'cleantalk_validate_webform';
}
if (preg_match('|node_forum(.*?)_form|', $form_id)) {
$form['#validate'][] = 'cleantalk_validate_forum_topic';
}
if ($form_id == 'search_form' || $form_id == 'search_block_form') {
}
}
}
function cleantalk_uc_order($op, $order, $arg2) {
global $cleantalk_executed;
$ct_temp_msg_data = CleantalkHelper::get_fields_any(\Drupal::request()->request
->all(), \Drupal::config('cleantalk.settings')
->get('cleantalk_fields_exclusions'));
$spam_check = array();
$spam_check['type'] = 'comment';
$spam_check['sender_email'] = $ct_temp_msg_data['email'] ? $ct_temp_msg_data['email'] : '';
$spam_check['sender_nickname'] = $ct_temp_msg_data['nickname'] ? $ct_temp_msg_data['nickname'] : '';
$spam_check['message_title'] = $ct_temp_msg_data['subject'] ? $ct_temp_msg_data['subject'] : '';
$spam_check['message_body'] = $ct_temp_msg_data['message'] ? implode("\n", $ct_temp_msg_data['message']) : '';
if ($spam_check['sender_email'] != '' && !$cleantalk_executed && $op != 'total' && $op != 'presave' && $op != 'save') {
$spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check);
$cleantalk_executed = true;
if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1 && $spam_result['stop_queue'] == 1) {
CleantalkFuncs::_cleantalk_die($spam_result['ct_result_comment']);
}
}
}