[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Plugin 5 * @subpackage Content.emailcloak 6 * 7 * @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 10 * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace 11 */ 12 13 use Joomla\CMS\HTML\HTMLHelper; 14 use Joomla\CMS\Plugin\CMSPlugin; 15 use Joomla\String\StringHelper; 16 17 // phpcs:disable PSR1.Files.SideEffects 18 \defined('_JEXEC') or die; 19 // phpcs:enable PSR1.Files.SideEffects 20 21 /** 22 * Email cloak plugin class. 23 * 24 * @since 1.5 25 */ 26 class PlgContentEmailcloak extends CMSPlugin 27 { 28 /** 29 * @var \Joomla\CMS\Application\SiteApplication 30 * 31 * @since 3.9.0 32 */ 33 protected $app; 34 35 /** 36 * Plugin that cloaks all emails in content from spambots via Javascript. 37 * 38 * @param string $context The context of the content being passed to the plugin. 39 * @param mixed &$row An object with a "text" property or the string to be cloaked. 40 * @param mixed &$params Additional parameters. 41 * @param integer $page Optional page number. Unused. Defaults to zero. 42 * 43 * @return void 44 */ 45 public function onContentPrepare($context, &$row, &$params, $page = 0) 46 { 47 // Don't run if in the API Application 48 // Don't run this plugin when the content is being indexed 49 if ($this->app->isClient('api') || $context === 'com_finder.indexer') { 50 return; 51 } 52 53 if (is_object($row)) { 54 $this->_cloak($row->text, $params); 55 56 return; 57 } 58 59 $this->_cloak($row, $params); 60 } 61 62 /** 63 * Generate a search pattern based on link and text. 64 * 65 * @param string $link The target of an email link. 66 * @param string $text The text enclosed by the link. 67 * 68 * @return string A regular expression that matches a link containing the parameters. 69 */ 70 protected function _getPattern($link, $text) 71 { 72 $pattern = '~(?:<a ([^>]*)href\s*=\s*"mailto:' . $link . '"([^>]*))>' . $text . '</a>~i'; 73 74 return $pattern; 75 } 76 77 /** 78 * Cloak all emails in text from spambots via Javascript. 79 * 80 * @param string &$text The string to be cloaked. 81 * @param mixed &$params Additional parameters. Parameter "mode" (integer, default 1) 82 * replaces addresses with "mailto:" links if nonzero. 83 * 84 * @return void 85 */ 86 protected function _cloak(&$text, &$params) 87 { 88 /* 89 * Check for presence of {emailcloak=off} which is explicits disables this 90 * bot for the item. 91 */ 92 if (StringHelper::strpos($text, '{emailcloak=off}') !== false) { 93 $text = StringHelper::str_ireplace('{emailcloak=off}', '', $text); 94 95 return; 96 } 97 98 // Simple performance check to determine whether bot should process further. 99 if (StringHelper::strpos($text, '@') === false) { 100 return; 101 } 102 103 $mode = (int) $this->params->def('mode', 1); 104 $mode = $mode === 1; 105 106 // Example: [email protected] 107 $searchEmail = '([\w\.\'\-\+]+\@(?:[a-z0-9\.\-]+\.)+(?:[a-zA-Z0-9\-]{2,24}))'; 108 109 // Example: [email protected]?subject=anyText 110 $searchEmailLink = $searchEmail . '([?&][\x20-\x7f][^"<>]+)'; 111 112 // Any Text 113 $searchText = '((?:[\x20-\x7f]|[\xA1-\xFF]|[\xC2-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF4][\x80-\xBF]{3})[^<>]+)'; 114 115 // Any Image link 116 $searchImage = '(<img[^>]+>)'; 117 118 // Any Text with <span or <strong 119 $searchTextSpan = '(<span[^>]+>|<span>|<strong>|<strong><span[^>]+>|<strong><span>)' . $searchText . '(</span>|</strong>|</span></strong>)'; 120 121 // Any address with <span or <strong 122 $searchEmailSpan = '(<span[^>]+>|<span>|<strong>|<strong><span[^>]+>|<strong><span>)' . $searchEmail . '(</span>|</strong>|</span></strong>)'; 123 124 /* 125 * Search and fix derivatives of link code <a href="http://mce_host/ourdirectory/[email protected]" 126 * >[email protected]</a>. This happens when inserting an email in TinyMCE, cancelling its suggestion to add 127 * the mailto: prefix... 128 */ 129 $pattern = $this->_getPattern($searchEmail, $searchEmail); 130 $pattern = str_replace('"mailto:', '"http://mce_host([\x20-\x7f][^<>]+/)', $pattern); 131 132 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 133 $mail = $regs[3][0]; 134 $mailText = $regs[5][0]; 135 136 // Check to see if mail text is different from mail addy 137 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText); 138 139 // Replace the found address with the js cloaked email 140 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 141 } 142 143 /* 144 * Search and fix derivatives of link code <a href="http://mce_host/ourdirectory/[email protected]" 145 * >anytext</a>. This happens when inserting an email in TinyMCE, cancelling its suggestion to add 146 * the mailto: prefix... 147 */ 148 $pattern = $this->_getPattern($searchEmail, $searchText); 149 $pattern = str_replace('"mailto:', '"http://mce_host([\x20-\x7f][^<>]+/)', $pattern); 150 151 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 152 $mail = $regs[3][0]; 153 $mailText = $regs[5][0]; 154 155 // Check to see if mail text is different from mail addy 156 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText, 0); 157 158 // Replace the found address with the js cloaked email 159 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 160 } 161 162 /* 163 * Search for derivatives of link code <a href="mailto:[email protected]" 164 * >[email protected]</a> 165 */ 166 $pattern = $this->_getPattern($searchEmail, $searchEmail); 167 168 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 169 $mail = $regs[2][0]; 170 $mailText = $regs[4][0]; 171 172 // Check to see if mail text is different from mail addy 173 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText); 174 175 // Replace the found address with the js cloaked email 176 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 177 } 178 179 /* 180 * Search for derivatives of link code <a href="mailto:[email protected]" 181 * ><anyspan >[email protected]</anyspan></a> 182 */ 183 $pattern = $this->_getPattern($searchEmail, $searchEmailSpan); 184 185 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 186 $mail = $regs[2][0]; 187 $mailText = $regs[4][0] . $regs[5][0] . $regs[6][0]; 188 189 // Check to see if mail text is different from mail addy 190 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText); 191 192 // Replace the found address with the js cloaked email 193 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 194 } 195 196 /* 197 * Search for derivatives of link code <a href="mailto:[email protected]"> 198 * <anyspan >anytext</anyspan></a> 199 */ 200 $pattern = $this->_getPattern($searchEmail, $searchTextSpan); 201 202 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 203 $mail = $regs[2][0]; 204 $mailText = $regs[4][0] . $regs[5][0] . $regs[6][0]; 205 206 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText, 0); 207 208 // Replace the found address with the js cloaked email 209 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 210 } 211 212 /* 213 * Search for derivatives of link code <a href="mailto:[email protected]"> 214 * anytext</a> 215 */ 216 $pattern = $this->_getPattern($searchEmail, $searchText); 217 218 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 219 $mail = $regs[2][0]; 220 $mailText = $regs[4][0]; 221 222 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText, 0); 223 224 // Replace the found address with the js cloaked email 225 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 226 } 227 228 /* 229 * Search for derivatives of link code <a href="mailto:[email protected]"> 230 * <img anything></a> 231 */ 232 $pattern = $this->_getPattern($searchEmail, $searchImage); 233 234 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 235 $mail = $regs[2][0]; 236 $mailText = $regs[4][0]; 237 238 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText, 0); 239 240 // Replace the found address with the js cloaked email 241 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 242 } 243 244 /* 245 * Search for derivatives of link code <a href="mailto:[email protected]"> 246 * <img anything>[email protected]</a> 247 */ 248 $pattern = $this->_getPattern($searchEmail, $searchImage . $searchEmail); 249 250 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 251 $mail = $regs[2][0]; 252 $mailText = $regs[4][0] . $regs[5][0]; 253 254 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText); 255 256 // Replace the found address with the js cloaked email 257 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 258 } 259 260 /* 261 * Search for derivatives of link code <a href="mailto:[email protected]"> 262 * <img anything>any text</a> 263 */ 264 $pattern = $this->_getPattern($searchEmail, $searchImage . $searchText); 265 266 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 267 $mail = $regs[2][0]; 268 $mailText = $regs[4][0] . $regs[5][0]; 269 270 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText, 0); 271 272 // Replace the found address with the js cloaked email 273 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 274 } 275 276 /* 277 * Search for derivatives of link code <a href="mailto:[email protected]? 278 * subject=Text">[email protected]</a> 279 */ 280 $pattern = $this->_getPattern($searchEmailLink, $searchEmail); 281 282 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 283 $mail = $regs[2][0] . $regs[3][0]; 284 $mailText = $regs[5][0]; 285 286 // Needed for handling of Body parameter 287 $mail = str_replace('&', '&', $mail); 288 289 // Check to see if mail text is different from mail addy 290 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText); 291 292 // Replace the found address with the js cloaked email 293 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 294 } 295 296 /* 297 * Search for derivatives of link code <a href="mailto:[email protected]? 298 * subject=Text">anytext</a> 299 */ 300 $pattern = $this->_getPattern($searchEmailLink, $searchText); 301 302 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 303 $mail = $regs[2][0] . $regs[3][0]; 304 $mailText = $regs[5][0]; 305 306 // Needed for handling of Body parameter 307 $mail = str_replace('&', '&', $mail); 308 309 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText, 0); 310 311 // Replace the found address with the js cloaked email 312 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 313 } 314 315 /* 316 * Search for derivatives of link code <a href="mailto:[email protected]?subject= Text" 317 * ><anyspan >[email protected]</anyspan></a> 318 */ 319 $pattern = $this->_getPattern($searchEmailLink, $searchEmailSpan); 320 321 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 322 $mail = $regs[2][0] . $regs[3][0]; 323 $mailText = $regs[5][0] . $regs[6][0] . $regs[7][0]; 324 325 // Check to see if mail text is different from mail addy 326 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText); 327 328 // Replace the found address with the js cloaked email 329 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 330 } 331 332 /* 333 * Search for derivatives of link code <a href="mailto:[email protected]?subject= Text"> 334 * <anyspan >anytext</anyspan></a> 335 */ 336 $pattern = $this->_getPattern($searchEmailLink, $searchTextSpan); 337 338 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 339 $mail = $regs[2][0] . $regs[3][0]; 340 $mailText = $regs[5][0] . $regs[6][0] . $regs[7][0]; 341 342 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText, 0); 343 344 // Replace the found address with the js cloaked email 345 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 346 } 347 348 /* 349 * Search for derivatives of link code 350 * <a href="mailto:[email protected]?subject=Text"><img anything></a> 351 */ 352 $pattern = $this->_getPattern($searchEmailLink, $searchImage); 353 354 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 355 $mail = $regs[1][0] . $regs[2][0] . $regs[3][0]; 356 $mailText = $regs[5][0]; 357 358 // Needed for handling of Body parameter 359 $mail = str_replace('&', '&', $mail); 360 361 // Check to see if mail text is different from mail addy 362 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText, 0); 363 364 // Replace the found address with the js cloaked email 365 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 366 } 367 368 /* 369 * Search for derivatives of link code 370 * <a href="mailto:[email protected]?subject=Text"><img anything>[email protected]</a> 371 */ 372 $pattern = $this->_getPattern($searchEmailLink, $searchImage . $searchEmail); 373 374 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 375 $mail = $regs[1][0] . $regs[2][0] . $regs[3][0]; 376 $mailText = $regs[4][0] . $regs[5][0] . $regs[6][0]; 377 378 // Needed for handling of Body parameter 379 $mail = str_replace('&', '&', $mail); 380 381 // Check to see if mail text is different from mail addy 382 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText); 383 384 // Replace the found address with the js cloaked email 385 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 386 } 387 388 /* 389 * Search for derivatives of link code 390 * <a href="mailto:[email protected]?subject=Text"><img anything>any text</a> 391 */ 392 $pattern = $this->_getPattern($searchEmailLink, $searchImage . $searchText); 393 394 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 395 $mail = $regs[1][0] . $regs[2][0] . $regs[3][0]; 396 $mailText = $regs[4][0] . $regs[5][0] . $regs[6][0]; 397 398 // Needed for handling of Body parameter 399 $mail = str_replace('&', '&', $mail); 400 401 // Check to see if mail text is different from mail addy 402 $replacement = HTMLHelper::_('email.cloak', $mail, $mode, $mailText, 0); 403 404 // Replace the found address with the js cloaked email 405 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 406 } 407 408 /* 409 * Search for plain text email addresses, such as [email protected] but not within HTML tags: 410 * <img src="..." title="[email protected]"> or <input type="text" placeholder="[email protected]"> 411 * The '<[^<]*>(*SKIP)(*F)|' trick is used to exclude this kind of occurrences 412 */ 413 $pattern = '~<[^<]*>(*SKIP)(*F)|' . $searchEmail . '~i'; 414 415 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 416 $mail = $regs[1][0]; 417 $replacement = HTMLHelper::_('email.cloak', $mail, $mode); 418 419 // Replace the found address with the js cloaked email 420 $text = substr_replace($text, $replacement, $regs[1][1], strlen($mail)); 421 } 422 } 423 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |