| [ Index ] |
PHP Cross Reference of Joomla 1.5.26 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @version $Id: emailcloak.php 14401 2010-01-26 14:10:00Z louis $ 4 * @package Joomla 5 * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved. 6 * @license GNU/GPL, see LICENSE.php 7 * Joomla! is free software. This version may have been modified pursuant 8 * to the GNU General Public License, and as distributed it includes or 9 * is derivative of works licensed under the GNU General Public License or 10 * other free or open source software licenses. 11 * See COPYRIGHT.php for copyright notices and details. 12 */ 13 14 // no direct access 15 defined( '_JEXEC' ) or die( 'Restricted access' ); 16 17 $mainframe->registerEvent('onPrepareContent', 'plgContentEmailCloak'); 18 19 /** 20 * Plugin that cloaks all emails in content from spambots via Javascript. 21 * 22 * @param object|string An object with a "text" property or the string to be 23 * cloaked. 24 * @param array Additional parameters. See {@see plgEmailCloak()}. 25 * @param int Optional page number. Unused. Defaults to zero. 26 * @return boolean True on success. 27 */ 28 function plgContentEmailCloak(&$row, &$params, $page=0) 29 { 30 if (is_object($row)) { 31 return plgEmailCloak($row->text, $params); 32 } 33 return plgEmailCloak($row, $params); 34 } 35 36 /** 37 * Genarate a search pattern based on link and text. 38 * 39 * @param string The target of an e-mail link. 40 * @param string The text enclosed by the link. 41 * @return string A regular expression that matches a link containing the 42 * parameters. 43 */ 44 function plgContentEmailCloak_searchPattern ($link, $text) { 45 // <a href="mailto:anyLink">anyText</a> 46 $pattern = '~(?:<a [\w "\'=\@\.\-]*href\s*=\s*"(mailto:|https?://(?:[a-z0-9][a-z0-9\-]*[a-z0-9]\.)*(?:[a-z0-9]+)(?::\d+)?[a-z0-9;/\?:\@&=+\$,\-_\.!\~*\'\(\)%]+?%3C)' 47 . $link . '(%3E)?"([\w "\'=\@\.\-]*))>' . $text . '</a>~i'; 48 49 return $pattern; 50 } 51 52 /** 53 * Cloak all emails in text from spambots via Javascript. 54 * 55 * @param string The string to be cloaked. 56 * @param array Additional parameters. Parameter "mode" (integer, default 1) 57 * replaces addresses with "mailto:" links if nonzero. 58 * @return boolean True on success. 59 */ 60 function plgEmailCloak(&$text, &$params) 61 { 62 63 /* 64 * Check for presence of {emailcloak=off} which is explicits disables this 65 * bot for the item. 66 */ 67 if (JString::strpos($text, '{emailcloak=off}') !== false) { 68 $text = JString::str_ireplace('{emailcloak=off}', '', $text); 69 return true; 70 } 71 72 // Simple performance check to determine whether bot should process further. 73 if (JString::strpos($text, '@') === false) { 74 return true; 75 } 76 77 $plugin = & JPluginHelper::getPlugin('content', 'emailcloak'); 78 79 // Load plugin params info 80 $pluginParams = new JParameter($plugin->params); 81 $mode = $pluginParams->def('mode', 1); 82 83 // split the string into parts to exclude strcipt tags from being handled 84 $text = explode( '<script', $text ); 85 foreach ( $text as $i => $str ) { 86 if ( $i == 0 ) { 87 plgEmailCloakString( $text[$i], $mode ); 88 } else { 89 $str_split = explode( '</script>', $str ); 90 foreach ( $str_split as $j => $str_split_part ) { 91 if ( ( $j % 2 ) == 1 ) { 92 plgEmailCloakString( $str_split[$i], $mode ); 93 } 94 } 95 $text[$i] = implode( '</script>', $str_split ); 96 } 97 } 98 $text = implode( '<script', $text ); 99 return true; 100 } 101 102 /** 103 * Cloak all emails in text from spambots via Javascript. 104 * 105 * @param string The string to be cloaked. 106 * @param string The mode. 107 * replaces addresses with "mailto:" links if nonzero. 108 * @return boolean True on success. 109 */ 110 function plgEmailCloakString(&$text, $mode = 1) 111 { 112 // Simple performance check to determine whether bot should process further. 113 if (JString::strpos($text, '@') === false) { 114 return true; 115 } 116 117 118 // any@email.address.com 119 $searchEmail = '([\w\.\-]+\@(?:[a-z0-9\.\-]+\.)+(?:[a-z0-9\-]{2,4}))'; 120 // any@email.address.com?subject=anyText 121 $searchEmailLink = $searchEmail . '([?&][\x20-\x7f][^"<>]+)'; 122 // anyText 123 $searchText = '((?:[\x20-\x7f]|[\xA1-\xFF]|[\xC2-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF4][\x80-\xBF]{3})[^<>]+)'; 124 125 //$searchText = '(+)'; 126 //Any Image link 127 $searchImage = "(<img[^>]+>)"; 128 129 /* 130 * Search for derivatives of link code <a href="mailto:email@amail.com" 131 * >email@amail.com</a> 132 */ 133 $pattern = plgContentEmailCloak_searchPattern($searchEmail, $searchEmail); 134 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 135 $mail = $regs[2][0]; 136 $mailText = $regs[3][0]; 137 138 // Check to see if mail text is different from mail addy 139 $replacement = JHTML::_('email.cloak', $mail, $mode, $mailText); 140 141 // Replace the found address with the js cloaked email 142 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 143 } 144 145 /* 146 * Search for derivatives of link code <a href="mailto:email@amail.com"> 147 * anytext</a> 148 */ 149 $pattern = plgContentEmailCloak_searchPattern($searchEmail, $searchText); 150 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 151 $prefix = $regs[1][0]; 152 $mail = $regs[2][0]; 153 $suffix = $regs[3][0]; 154 $attribs = $regs[4][0]; 155 $mailText = $regs[5][0]; 156 157 $replacement = JHTML::_('email.cloak', $mail, $mode, $mailText, 0, $prefix, $suffix, $attribs); 158 159 // Replace the found address with the js cloaked email 160 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 161 } 162 163 /* 164 * Search for derivatives of link code <a href="mailto:email@amail.com"> 165 * <img anything></a> 166 */ 167 $pattern = plgContentEmailCloak_searchPattern($searchEmail, $searchImage); 168 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 169 $prefix = $regs[1][0]; 170 $mail = $regs[2][0]; 171 $suffix = $regs[3][0]; 172 $attribs = $regs[4][0]; 173 $mailText = $regs[5][0]; 174 175 $replacement = JHTML::_('email.cloak', $mail, $mode, $mailText, 0, $prefix, $suffix, $attribs); 176 177 // Replace the found address with the js cloaked email 178 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 179 } 180 181 /* 182 * Search for derivatives of link code <a href="mailto:email@amail.com? 183 * subject=Text">email@amail.com</a> 184 */ 185 $pattern = plgContentEmailCloak_searchPattern($searchEmailLink, $searchEmail); 186 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 187 $mail = $regs[2][0] . $regs[3][0]; 188 $mailText = $regs[6][0]; 189 // Needed for handling of Body parameter 190 $mail = str_replace( '&', '&', $mail ); 191 192 // Check to see if mail text is different from mail addy 193 $replacement = JHTML::_('email.cloak', $mail, $mode, $mailText); 194 195 // Replace the found address with the js cloaked email 196 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 197 } 198 199 /* 200 * Search for derivatives of link code <a href="mailto:email@amail.com? 201 * subject=Text">anytext</a> 202 */ 203 $pattern = plgContentEmailCloak_searchPattern($searchEmailLink, $searchText); 204 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 205 $mail = $regs[2][0] . $regs[3][0]; 206 $mailText = $regs[6][0]; 207 // Needed for handling of Body parameter 208 $mail = str_replace('&', '&', $mail); 209 210 $replacement = JHTML::_('email.cloak', $mail, $mode, $mailText, 0); 211 212 // Replace the found address with the js cloaked email 213 $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0])); 214 } 215 216 // Search for plain text email@amail.com 217 $pattern = '~' . $searchEmail . '([^a-z0-9]|$)~i'; 218 while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) { 219 $mail = $regs[1][0]; 220 $replacement = JHTML::_('email.cloak', $mail, $mode); 221 222 // Replace the found address with the js cloaked email 223 $text = substr_replace($text, $replacement, $regs[1][1], strlen($mail)); 224 } 225 return true; 226 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Mar 28 15:54:07 2012 | Cross-referenced by PHPXref 0.7.1 |