|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cleantalk\Antispam\EmailEncoder; |
| 4 | + |
| 5 | +use Cleantalk\ApbctWP\State; |
| 6 | +use Cleantalk\ApbctWP\Variables\Cookie; |
| 7 | +use Cleantalk\ApbctWP\Variables\Post; |
| 8 | +use Cleantalk\ApbctWP\Variables\Server; |
| 9 | +use Cleantalk\Common\TT; |
| 10 | + |
| 11 | +class ExclusionsService |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Keep arrays of exclusion signs in the array |
| 15 | + * @var array |
| 16 | + */ |
| 17 | + private $content_exclusions_signs = array( |
| 18 | + //divi contact forms additional emails |
| 19 | + array('_unique_id', 'et_pb_contact_form'), |
| 20 | + //divi builder contact forms |
| 21 | + array('_builder_version', 'custom_contact_email'), |
| 22 | + //ninja form noscript content exclusion |
| 23 | + array('ninja-forms-noscript-message'), |
| 24 | + //enfold theme contact form content exclusion - this fired during buffer interception |
| 25 | + array('av_contact', 'email', 'from_email'), |
| 26 | + // Stylish Cost Calculator |
| 27 | + array('scc-form-field-item'), |
| 28 | + // Exclusion of maps from leaflet |
| 29 | + array('leaflet'), |
| 30 | + // prevent broking elementor swiper gallery |
| 31 | + array('class', 'elementor-swiper', 'elementor-testimonial', 'swiper-pagination'), |
| 32 | + // ics-calendar |
| 33 | + array('ics_calendar'), |
| 34 | + ); |
| 35 | + |
| 36 | + /** |
| 37 | + * @return string|false |
| 38 | + * @psalm-suppress PossiblyUnusedReturnValue |
| 39 | + */ |
| 40 | + public function doSkipBeforeAnything() |
| 41 | + { |
| 42 | + global $apbct; |
| 43 | + if ( $this->byAccessKeyFail($apbct) ) { |
| 44 | + return 'byAccessKeyFail'; |
| 45 | + } |
| 46 | + |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @return string|false |
| 52 | + * @psalm-suppress PossiblyUnusedReturnValue |
| 53 | + */ |
| 54 | + public function doSkipBeforeModifyingHooksAdded() |
| 55 | + { |
| 56 | + global $apbct; |
| 57 | + |
| 58 | + if ( $this->byPluginSetting($apbct) ) { |
| 59 | + return 'byPluginSetting'; |
| 60 | + } |
| 61 | + |
| 62 | + // Excluded request |
| 63 | + if ( $this->byServerVars() ) { |
| 64 | + return 'byServerVars'; |
| 65 | + } |
| 66 | + |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param $content |
| 72 | + * |
| 73 | + * @return string|false |
| 74 | + * @psalm-suppress PossiblyUnusedReturnValue |
| 75 | + * @psalm-suppress PossiblyUnusedMethod |
| 76 | + */ |
| 77 | + public function doReturnContentBeforeModify($content) |
| 78 | + { |
| 79 | + global $apbct; |
| 80 | + |
| 81 | + if ( !(bool)$apbct->settings['data__email_decoder_encode_email_addresses'] && !(bool)$apbct->settings['data__email_decoder_encode_phone_numbers'] ) { |
| 82 | + return 'globallyDisabledBothEncoding'; |
| 83 | + } |
| 84 | + |
| 85 | + if ( $this->byLoggedIn() ) { |
| 86 | + return 'byLoggedIn'; |
| 87 | + } |
| 88 | + |
| 89 | + //skip empty or invalid content |
| 90 | + if ( $this->byEmptyContent($content) ) { |
| 91 | + return 'byEmptyContent'; |
| 92 | + } |
| 93 | + |
| 94 | + if ( $this->byUrlOnHooks() ) { |
| 95 | + return 'byUrlOnHooks'; |
| 96 | + } |
| 97 | + |
| 98 | + if ( $this->byContentSigns($content) ) { |
| 99 | + return 'byContentSigns'; |
| 100 | + } |
| 101 | + |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Excluded requests |
| 107 | + * @return bool |
| 108 | + */ |
| 109 | + private function byServerVars() |
| 110 | + { |
| 111 | + // Excluded request by alt cookie |
| 112 | + $apbct_email_encoder_passed = Cookie::get('apbct_email_encoder_passed'); |
| 113 | + if ( $apbct_email_encoder_passed === apbct_get_email_encoder_pass_key() ) { |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + if ( |
| 118 | + apbct_is_plugin_active('ultimate-member/ultimate-member.php') && |
| 119 | + isset($_POST['um_request']) && |
| 120 | + array_key_exists('REQUEST_METHOD', $_SERVER) && |
| 121 | + strtoupper($_SERVER['REQUEST_METHOD']) === 'POST' && |
| 122 | + empty(Post::get('encodedEmail')) |
| 123 | + ) { |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param State $apbct |
| 132 | + * |
| 133 | + * @return bool |
| 134 | + */ |
| 135 | + private function byPluginSetting($apbct) |
| 136 | + { |
| 137 | + return ! $apbct->settings['data__email_decoder']; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @param State $apbct |
| 142 | + * |
| 143 | + * @return bool |
| 144 | + */ |
| 145 | + private function byAccessKeyFail($apbct) |
| 146 | + { |
| 147 | + return !$apbct->key_is_ok || ! apbct_api_key__is_correct(); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Check content if it contains exclusions from exclusion list |
| 152 | + * @param $content - content to check |
| 153 | + * @return bool - true if exclusions found, else - false |
| 154 | + */ |
| 155 | + private function byContentSigns($content) |
| 156 | + { |
| 157 | + if ( is_array($this->content_exclusions_signs) ) { |
| 158 | + foreach ( array_values($this->content_exclusions_signs) as $_signs_array => $signs ) { |
| 159 | + //process each of subarrays of signs |
| 160 | + $signs_found_count = 0; |
| 161 | + if ( isset($signs) && is_array($signs) ) { |
| 162 | + //chek all the signs in the sub-array |
| 163 | + foreach ( $signs as $sign ) { |
| 164 | + if ( is_string($sign) ) { |
| 165 | + if ( strpos($content, $sign) === false ) { |
| 166 | + continue; |
| 167 | + } else { |
| 168 | + $signs_found_count++; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + //if each of signs in the sub-array are found return true |
| 173 | + if ( $signs_found_count === count($signs) ) { |
| 174 | + if (in_array('et_pb_contact_form', $signs) && !is_admin()) { |
| 175 | + return false; |
| 176 | + } |
| 177 | + return true; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + //no signs found |
| 183 | + return false; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * @param string $content |
| 188 | + * |
| 189 | + * @return bool |
| 190 | + */ |
| 191 | + private function byEmptyContent($content) |
| 192 | + { |
| 193 | + //skip empty or invalid content |
| 194 | + return empty($content) || !is_string($content); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * @return bool |
| 199 | + */ |
| 200 | + private function byLoggedIn() |
| 201 | + { |
| 202 | + return apbct_is_user_logged_in() && !apbct_is_in_uri('options-general.php?page=cleantalk'); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Skip encoder run on hooks. |
| 207 | + * |
| 208 | + * 1. Applies filter "apbct_hook_skip_email_encoder_on_url_list" to get modified list of URI chunks that needs to skip. |
| 209 | + * @return bool |
| 210 | + */ |
| 211 | + private function byUrlOnHooks() |
| 212 | + { |
| 213 | + $skip_encode = false; |
| 214 | + $url_chunk_list = array(); |
| 215 | + |
| 216 | + // Apply filter "apbct_hook_skip_email_encoder_on_url_list" to get the URI chunk list. |
| 217 | + $url_chunk_list = apply_filters('apbct_skip_email_encoder_on_uri_chunk_list', $url_chunk_list); |
| 218 | + |
| 219 | + if ( !empty($url_chunk_list) && is_array($url_chunk_list) ) { |
| 220 | + foreach ($url_chunk_list as $chunk) { |
| 221 | + if (is_string($chunk) && strpos(TT::toString(Server::get('REQUEST_URI')), $chunk) !== false) { |
| 222 | + $skip_encode = true; |
| 223 | + break; |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + return $skip_encode; |
| 229 | + } |
| 230 | +} |
0 commit comments