|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cleantalk\Antispam\IntegrationsByClass; |
| 4 | + |
| 5 | +use Cleantalk\ApbctWP\Escape; |
| 6 | +use Cleantalk\ApbctWP\Variables\Post; |
| 7 | +use Cleantalk\ApbctWP\Variables\Server; |
| 8 | +use Cleantalk\Common\TT; |
| 9 | +use Cleantalk\ApbctWP\Sanitize; |
| 10 | +use Cleantalk\ApbctWP\Variables\Cookie; |
| 11 | +use Cleantalk\ApbctWP\State; |
| 12 | +use Cleantalk\ApbctWP\Honeypot; |
| 13 | +use DOMDocument; |
| 14 | + |
| 15 | +/** |
| 16 | + * @psalm-suppress UnusedClass |
| 17 | + */ |
| 18 | +class WPSearchForm extends IntegrationByClassBase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @return void |
| 22 | + * @psalm-suppress PossiblyUnusedMethod |
| 23 | + */ |
| 24 | + public function doAjaxWork() |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @return void |
| 30 | + * @psalm-suppress PossiblyUnusedMethod |
| 31 | + */ |
| 32 | + public function doPublicWork() |
| 33 | + { |
| 34 | + global $apbct; |
| 35 | + if ( $apbct->settings['forms__search_test'] ) { |
| 36 | + add_filter('get_search_form', array($this, 'apbctFormSearchAddFields'), 999); |
| 37 | + } |
| 38 | + if ( ! is_admin() && ! apbct_is_ajax() && ! apbct_is_customize_preview() ) { |
| 39 | + // Default search |
| 40 | + add_filter('get_search_query', array($this, 'testSpam')); |
| 41 | + add_action('wp_head', array($this, 'addNoindex'), 1); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return void |
| 47 | + * @psalm-suppress PossiblyUnusedMethod |
| 48 | + */ |
| 49 | + public function doAdminWork() |
| 50 | + { |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Prepare data to add honeypot to the WordPress default search form. |
| 55 | + * Fires ct_add_honeypot_field() on hook get_search_form when: |
| 56 | + * - method of the form is post |
| 57 | + * - spam test of search form is enabled |
| 58 | + * |
| 59 | + * @param string $form_html |
| 60 | + * @return string |
| 61 | + */ |
| 62 | + public function apbctFormSearchAddFields($form_html) |
| 63 | + { |
| 64 | + global $apbct; |
| 65 | + |
| 66 | + if ( !empty($form_html) && is_string($form_html) && $apbct->settings['forms__search_test'] == 1 ) { |
| 67 | + // extract method of the form with DOMDocument |
| 68 | + if ( class_exists('DOMDocument') ) { |
| 69 | + libxml_use_internal_errors(true); |
| 70 | + $dom = new DOMDocument(); |
| 71 | + if ( @$dom->loadHTML($form_html) ) { |
| 72 | + $search_form_dom = $dom->getElementById('searchform'); |
| 73 | + if ( !empty($search_form_dom) ) { |
| 74 | + $method = empty($search_form_dom->getAttribute('method')) |
| 75 | + //default method is get for any form if no method specified |
| 76 | + ? 'get' |
| 77 | + : $search_form_dom->getAttribute('method'); |
| 78 | + } |
| 79 | + } |
| 80 | + libxml_clear_errors(); |
| 81 | + unset($dom); |
| 82 | + } |
| 83 | + |
| 84 | + // retry extract method of the form with regex |
| 85 | + if ( empty($method) ) { |
| 86 | + preg_match('/form.*method="(.*?)"/', $form_html, $matches); |
| 87 | + $method = empty($matches[1]) |
| 88 | + ? 'get' |
| 89 | + : trim($matches[1]); |
| 90 | + } |
| 91 | + $form_method = strtolower($method); |
| 92 | + |
| 93 | + $resalt = str_replace('</form>', Honeypot::generateHoneypotField('search_form', $form_method) . '</form>', $form_html); |
| 94 | + return $resalt; |
| 95 | + } |
| 96 | + |
| 97 | + return $form_html; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test default search string for spam |
| 102 | + * |
| 103 | + * @param string $search |
| 104 | + * |
| 105 | + * @return string |
| 106 | + */ |
| 107 | + public function testSpam($search) |
| 108 | + { |
| 109 | + global $apbct, $cleantalk_executed; |
| 110 | + |
| 111 | + if ( |
| 112 | + empty($search) || |
| 113 | + $cleantalk_executed || |
| 114 | + $apbct->settings['forms__search_test'] == 0 || |
| 115 | + ($apbct->settings['data__protect_logged_in'] != 1 && is_user_logged_in()) // Skip processing for logged in users. |
| 116 | + ) { |
| 117 | + do_action('apbct_skipped_request', __FILE__ . ' -> ' . __FUNCTION__ . '():' . __LINE__, $_POST); |
| 118 | + return $search; |
| 119 | + } |
| 120 | + |
| 121 | + $user = apbct_is_user_logged_in() ? wp_get_current_user() : null; |
| 122 | + |
| 123 | + $data = array( |
| 124 | + 'message' => $search, |
| 125 | + 'sender_email' => $user !== null ? $user->user_email : null, |
| 126 | + 'sender_nickname' => $user !== null ? $user->user_login : null, |
| 127 | + 'post_info' => array('comment_type' => 'site_search_wordpress'), |
| 128 | + 'exception_action' => 0, |
| 129 | + ); |
| 130 | + |
| 131 | + $base_call_result = apbct_base_call($data); |
| 132 | + |
| 133 | + if ( isset($base_call_result['ct_result']) ) { |
| 134 | + $ct_result = $base_call_result['ct_result']; |
| 135 | + |
| 136 | + $cleantalk_executed = true; |
| 137 | + |
| 138 | + if ( $ct_result->allow == 0 ) { |
| 139 | + die($ct_result->comment); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return $search; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Add no-index meta to the page of search results |
| 148 | + * @return void |
| 149 | + */ |
| 150 | + public function addNoindex() |
| 151 | + { |
| 152 | + global $apbct; |
| 153 | + |
| 154 | + if ( |
| 155 | + ! is_search() || // If it is search results |
| 156 | + $apbct->settings['forms__search_test'] == 0 || |
| 157 | + ($apbct->settings['data__protect_logged_in'] != 1 && is_user_logged_in()) // Skip processing for logged in users. |
| 158 | + ) { |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + echo '<!-- meta by CleanTalk Anti-Spam Protection plugin -->' . "\n"; |
| 163 | + echo '<meta name="robots" content="noindex,nofollow" />' . "\n"; |
| 164 | + } |
| 165 | +} |
0 commit comments