PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/content/emailcloak/emailcloak.php

https://github.com/joebushi/joomla
PHP | 161 lines | 69 code | 23 blank | 69 comment | 8 complexity | e2003d165395622330a0610210fef539 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. // No direct access.
  8. defined('_JEXEC') or die;
  9. jimport('joomla.plugin.plugin');
  10. /**
  11. * E-mail cloack plugin class.
  12. *
  13. * @package Joomla
  14. * @subpackage plg_emailcloak
  15. */
  16. class plgContentEmailcloak extends JPlugin
  17. {
  18. /**
  19. * Plugin that cloaks all emails in content from spambots via Javascript.
  20. *
  21. * @param mixed An object with a "text" property or the string to be cloaked.
  22. * @param array Additional parameters. See {@see plgEmailCloak()}.
  23. * @param int Optional page number. Unused. Defaults to zero.
  24. * @return boolean True on success.
  25. */
  26. public function onPrepareContent(&$row, &$params, $page = 0)
  27. {
  28. if (is_object($row)) {
  29. return $this->_cloak($row->text, $params);
  30. }
  31. return $this->_cloak($row, $params);
  32. }
  33. /**
  34. * Genarate a search pattern based on link and text.
  35. *
  36. * @param string The target of an e-mail link.
  37. * @param string The text enclosed by the link.
  38. * @return string A regular expression that matches a link containing the parameters.
  39. */
  40. protected function _getPattern ($link, $text) {
  41. $pattern = '~(?:<a [\w "\'=\@\.\-]*href\s*=\s*"mailto:'
  42. . $link . '"[\w "\'=\@\.\-]*)>' . $text . '</a>~i';
  43. return $pattern;
  44. }
  45. /**
  46. * Cloak all emails in text from spambots via Javascript.
  47. *
  48. * @param string The string to be cloaked.
  49. * @param array Additional parameters. Parameter "mode" (integer, default 1)
  50. * replaces addresses with "mailto:" links if nonzero.
  51. * @return boolean True on success.
  52. */
  53. protected function _cloak(&$text, &$params)
  54. {
  55. /*
  56. * Check for presence of {emailcloak=off} which is explicits disables this
  57. * bot for the item.
  58. */
  59. if (JString::strpos($text, '{emailcloak=off}') !== false) {
  60. $text = JString::str_ireplace('{emailcloak=off}', '', $text);
  61. return true;
  62. }
  63. // Simple performance check to determine whether bot should process further.
  64. if (JString::strpos($text, '@') === false) {
  65. return true;
  66. }
  67. $mode = $this->params->def('mode', 1);
  68. // any@email.address.com
  69. $searchEmail = '([\w\.\-]+\@(?:[a-z0-9\.\-]+\.)+(?:[a-z0-9\-]{2,4}))';
  70. // any@email.address.com?subject=anyText
  71. $searchEmailLink = $searchEmail . '([?&][\x20-\x7f][^"<>]+)';
  72. // anyText
  73. $searchText = '([\x20-\x7f][^<>]+)';
  74. /*
  75. * Search for derivatives of link code <a href="mailto:email@amail.com"
  76. * >email@amail.com</a>
  77. */
  78. $pattern = $this->_getPattern($searchEmail, $searchEmail);
  79. while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
  80. $mail = $regs[1][0];
  81. $mailText = $regs[2][0];
  82. // Check to see if mail text is different from mail addy
  83. $replacement = JHtml::_('email.cloak', $mail, $mode, $mailText);
  84. // Replace the found address with the js cloaked email
  85. $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0]));
  86. }
  87. /*
  88. * Search for derivatives of link code <a href="mailto:email@amail.com">
  89. * anytext</a>
  90. */
  91. $pattern = $this->_getPattern($searchEmail, $searchText);
  92. while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
  93. $mail = $regs[1][0];
  94. $mailText = $regs[2][0];
  95. $replacement = JHtml::_('email.cloak', $mail, $mode, $mailText, 0);
  96. // Replace the found address with the js cloaked email
  97. $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0]));
  98. }
  99. /*
  100. * Search for derivatives of link code <a href="mailto:email@amail.com?
  101. * subject=Text">email@amail.com</a>
  102. */
  103. $pattern = $this->_getPattern($searchEmailLink, $searchEmail);
  104. while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
  105. $mail = $regs[1][0] . $regs[2][0];
  106. $mailText = $regs[3][0];
  107. // Needed for handling of Body parameter
  108. $mail = str_replace('&amp;', '&', $mail);
  109. // Check to see if mail text is different from mail addy
  110. $replacement = JHtml::_('email.cloak', $mail, $mode, $mailText);
  111. // Replace the found address with the js cloaked email
  112. $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0]));
  113. }
  114. /*
  115. * Search for derivatives of link code <a href="mailto:email@amail.com?
  116. * subject=Text">anytext</a>
  117. */
  118. $pattern = $this->_getPattern($searchEmailLink, $searchText);
  119. while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
  120. $mail = $regs[1][0] . $regs[2][0];
  121. $mailText = $regs[3][0];
  122. // Needed for handling of Body parameter
  123. $mail = str_replace('&amp;', '&', $mail);
  124. $replacement = JHtml::_('email.cloak', $mail, $mode, $mailText, 0);
  125. // Replace the found address with the js cloaked email
  126. $text = substr_replace($text, $replacement, $regs[0][1], strlen($regs[0][0]));
  127. }
  128. // Search for plain text email@amail.com
  129. $pattern = '~' . $searchEmail . '([^a-z0-9]|$)~i';
  130. while (preg_match($pattern, $text, $regs, PREG_OFFSET_CAPTURE)) {
  131. $mail = $regs[1][0];
  132. $replacement = JHtml::_('email.cloak', $mail, $mode);
  133. // Replace the found address with the js cloaked email
  134. $text = substr_replace($text, $replacement, $regs[1][1], strlen($mail));
  135. }
  136. return true;
  137. }
  138. }