Source for file emailcloak.php

Documentation is available at emailcloak.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Content.emailcloak
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Email cloack plugin class.
  14.  *
  15.  * @package     Joomla.Plugin
  16.  * @subpackage  Content.emailcloak
  17.  * @since       1.5
  18.  */
  19. class PlgContentEmailcloak extends JPlugin
  20. {
  21.     /**
  22.      * Plugin that cloaks all emails in content from spambots via Javascript.
  23.      *
  24.      * @param   string   $context  The context of the content being passed to the plugin.
  25.      * @param   mixed    &$row     An object with a "text" property or the string to be cloaked.
  26.      * @param   mixed    &$params  Additional parameters. See {@see PlgContentEmailcloak()}.
  27.      * @param   integer  $page     Optional page number. Unused. Defaults to zero.
  28.      *
  29.      * @return  boolean    True on success.
  30.      */
  31.     public function onContentPrepare($context&$row&$params$page 0)
  32.     {
  33.         // Don't run this plugin when the content is being indexed
  34.         if ($context == 'com_finder.indexer')
  35.         {
  36.             return true;
  37.         }
  38.  
  39.         if (is_object($row))
  40.         {
  41.             return $this->_cloak($row->text$params);
  42.         }
  43.  
  44.         return $this->_cloak($row$params);
  45.     }
  46.  
  47.     /**
  48.      * Generate a search pattern based on link and text.
  49.      *
  50.      * @param   string  $link  The target of an email link.
  51.      * @param   string  $text  The text enclosed by the link.
  52.      *
  53.      * @return  string    A regular expression that matches a link containing the parameters.
  54.      */
  55.     protected function _getPattern ($link$text)
  56.     {
  57.         $pattern '~(?:<a ([\w "\'=\@\.\-:;]*)href\s*=\s*"mailto:'
  58.             . $link '"([\w "\'=\@\.\-:;]*))>' $text '</a>~i';
  59.  
  60.         return $pattern;
  61.     }
  62.  
  63.     /**
  64.      * Adds an attributes to the js cloaked email.
  65.      *
  66.      * @param   string  $jsEmail  Js cloaked email.
  67.      * @param   string  $before   Attributes before email.
  68.      * @param   string  $after    Attributes after email.
  69.      *
  70.      * @return string Js cloaked email with attributes.
  71.      */
  72.     protected function _addAttributesToEmail($jsEmail$before$after)
  73.     {
  74.         if ($before !== "")
  75.         {
  76.             $before str_replace("'""\'"$before);
  77.             $jsEmail str_replace("document.write('<a '""document.write('<a {$before}'"$jsEmail);
  78.         }
  79.  
  80.         if ($after !== "")
  81.         {
  82.             $after str_replace("'""\'"$after);
  83.             $jsEmail str_replace("'\'>');""'\'{$after}>');"$jsEmail);
  84.         }
  85.  
  86.         return $jsEmail;
  87.     }
  88.  
  89.     /**
  90.      * Cloak all emails in text from spambots via Javascript.
  91.      *
  92.      * @param   string  &$text    The string to be cloaked.
  93.      * @param   mixed   &$params  Additional parameters. Parameter "mode" (integer, default 1)
  94.      *                              replaces addresses with "mailto:" links if nonzero.
  95.      *
  96.      * @return  boolean  True on success.
  97.      */
  98.     protected function _cloak(&$text&$params)
  99.     {
  100.         /*
  101.          * Check for presence of {emailcloak=off} which is explicits disables this
  102.          * bot for the item.
  103.          */
  104.         if (JString::strpos($text'{emailcloak=off}'!== false)
  105.         {
  106.             $text JString::str_ireplace('{emailcloak=off}'''$text);
  107.  
  108.             return true;
  109.         }
  110.  
  111.         // Simple performance check to determine whether bot should process further.
  112.         if (JString::strpos($text'@'=== false)
  113.         {
  114.             return true;
  115.         }
  116.  
  117.         $mode $this->params->def('mode'1);
  118.  
  119.         // any@email.address.com
  120.         $searchEmail '([\w\.\-]+\@(?:[a-z0-9\.\-]+\.)+(?:[a-zA-Z0-9\-]{2,10}))';
  121.  
  122.         // any@email.address.com?subject=anyText
  123.         $searchEmailLink $searchEmail '([?&][\x20-\x7f][^"<>]+)';
  124.  
  125.         // Any Text
  126.         $searchText '([\x20-\x7f][^<>]+)';
  127.  
  128.         // Any Image link
  129.         $searchImage    =    "(<img[^>]+>)";
  130.  
  131.         /*
  132.          * Search and fix derivatives of link code <a href="http://mce_host/ourdirectory/email@amail.com"
  133.          * >email@email.com</a>. This happens when inserting an email in TinyMCE, cancelling its suggestion to add
  134.          * the mailto: prefix...
  135.          */
  136.         $pattern $this->_getPattern($searchEmail$searchEmail);
  137.         $pattern str_replace('"mailto:''"http://mce_host([\x20-\x7f][^<>]+/)'$pattern);
  138.  
  139.         while (preg_match($pattern$text$regsPREG_OFFSET_CAPTURE))
  140.         {
  141.             $mail $regs[3][0];
  142.             $mailText $regs[5][0];
  143.  
  144.             // Check to see if mail text is different from mail addy
  145.             $replacement JHtml::_('email.cloak'$mail$mode$mailText);
  146.  
  147.             // Ensure that attributes is not stripped out by email cloaking
  148.             $replacement $this->_addAttributesToEmail($replacement$regs[1][0]$regs[4][0]);
  149.  
  150.             // Replace the found address with the js cloaked email
  151.             $text substr_replace($text$replacement$regs[0][1]strlen($regs[0][0]));
  152.         }
  153.  
  154.         /*
  155.          * Search and fix derivatives of link code <a href="http://mce_host/ourdirectory/email@amail.com"
  156.          * >anytext</a>. This happens when inserting an email in TinyMCE, cancelling its suggestion to add
  157.          * the mailto: prefix...
  158.          */
  159.         $pattern $this->_getPattern($searchEmail$searchText);
  160.         $pattern str_replace('"mailto:''"http://mce_host([\x20-\x7f][^<>]+/)'$pattern);
  161.  
  162.         while (preg_match($pattern$text$regsPREG_OFFSET_CAPTURE))
  163.         {
  164.             $mail $regs[3][0];
  165.             $mailText $regs[5][0];
  166.  
  167.             // Check to see if mail text is different from mail addy
  168.             $replacement JHtml::_('email.cloak'$mail$mode$mailText0);
  169.  
  170.             // Ensure that attributes is not stripped out by email cloaking
  171.             $replacement $this->_addAttributesToEmail($replacement$regs[1][0]$regs[4][0]);
  172.  
  173.             // Replace the found address with the js cloaked email
  174.             $text substr_replace($text$replacement$regs[0][1]strlen($regs[0][0]));
  175.         }
  176.  
  177.         /*
  178.          * Search for derivatives of link code <a href="mailto:email@amail.com"
  179.          * >email@amail.com</a>
  180.          */
  181.         $pattern $this->_getPattern($searchEmail$searchEmail);
  182.  
  183.         while (preg_match($pattern$text$regsPREG_OFFSET_CAPTURE))
  184.         {
  185.             $mail $regs[2][0];
  186.             $mailText $regs[4][0];
  187.  
  188.             // Check to see if mail text is different from mail addy
  189.             $replacement JHtml::_('email.cloak'$mail$mode$mailText);
  190.  
  191.             // Ensure that attributes is not stripped out by email cloaking
  192.             $replacement $this->_addAttributesToEmail($replacement$regs[1][0]$regs[3][0]);
  193.  
  194.             // Replace the found address with the js cloaked email
  195.             $text substr_replace($text$replacement$regs[0][1]strlen($regs[0][0]));
  196.         }
  197.  
  198.         /*
  199.          * Search for derivatives of link code <a href="mailto:email@amail.com">
  200.          * anytext</a>
  201.          */
  202.         $pattern $this->_getPattern($searchEmail$searchText);
  203.  
  204.         while (preg_match($pattern$text$regsPREG_OFFSET_CAPTURE))
  205.         {
  206.             $mail $regs[2][0];
  207.             $mailText $regs[4][0];
  208.  
  209.             $replacement JHtml::_('email.cloak'$mail$mode$mailText0);
  210.  
  211.             // Ensure that attributes is not stripped out by email cloaking
  212.             $replacement $this->_addAttributesToEmail($replacement$regs[1][0]$regs[3][0]);
  213.  
  214.             // Replace the found address with the js cloaked email
  215.             $text substr_replace($text$replacement$regs[0][1]strlen($regs[0][0]));
  216.         }
  217.  
  218.         /*
  219.          * Search for derivatives of link code <a href="mailto:email@amail.com">
  220.          * <img anything></a>
  221.          */
  222.         $pattern $this->_getPattern($searchEmail$searchImage);
  223.  
  224.         while (preg_match($pattern$text$regsPREG_OFFSET_CAPTURE))
  225.         {
  226.             $mail $regs[2][0];
  227.             $mailText $regs[4][0];
  228.  
  229.             $replacement JHtml::_('email.cloak'$mail$mode$mailText0);
  230.  
  231.             // Ensure that attributes is not stripped out by email cloaking
  232.             $replacement $this->_addAttributesToEmail($replacement$regs[1][0]$regs[3][0]);
  233.  
  234.             // Replace the found address with the js cloaked email
  235.             $text substr_replace($text$replacement$regs[0][1]strlen($regs[0][0]));
  236.         }
  237.  
  238.         /*
  239.          * Search for derivatives of link code <a href="mailto:email@amail.com?
  240.          * subject=Text">email@amail.com</a>
  241.          */
  242.         $pattern $this->_getPattern($searchEmailLink$searchEmail);
  243.  
  244.         while (preg_match($pattern$text$regsPREG_OFFSET_CAPTURE))
  245.         {
  246.             $mail $regs[2][0$regs[3][0];
  247.             $mailText $regs[5][0];
  248.  
  249.             // Needed for handling of Body parameter
  250.             $mail str_replace('&amp;''&'$mail);
  251.  
  252.             // Check to see if mail text is different from mail addy
  253.             $replacement JHtml::_('email.cloak'$mail$mode$mailText);
  254.  
  255.             // Ensure that attributes is not stripped out by email cloaking
  256.             $replacement $this->_addAttributesToEmail($replacement$regs[1][0]$regs[4][0]);
  257.  
  258.             // Replace the found address with the js cloaked email
  259.             $text substr_replace($text$replacement$regs[0][1]strlen($regs[0][0]));
  260.         }
  261.  
  262.         /*
  263.          * Search for derivatives of link code <a href="mailto:email@amail.com?
  264.          * subject=Text">anytext</a>
  265.          */
  266.         $pattern $this->_getPattern($searchEmailLink$searchText);
  267.  
  268.         while (preg_match($pattern$text$regsPREG_OFFSET_CAPTURE))
  269.         {
  270.             $mail $regs[2][0$regs[3][0];
  271.             $mailText $regs[5][0];
  272.  
  273.             // Needed for handling of Body parameter
  274.             $mail str_replace('&amp;''&'$mail);
  275.  
  276.             $replacement JHtml::_('email.cloak'$mail$mode$mailText0);
  277.  
  278.             // Ensure that attributes is not stripped out by email cloaking
  279.             $replacement $this->_addAttributesToEmail($replacement$regs[1][0]$regs[4][0]);
  280.  
  281.             // Replace the found address with the js cloaked email
  282.             $text substr_replace($text$replacement$regs[0][1]strlen($regs[0][0]));
  283.         }
  284.  
  285.         // Search for plain text email@amail.com
  286.         $pattern '~' $searchEmail '([^a-z0-9]|$)~i';
  287.  
  288.         while (preg_match($pattern$text$regsPREG_OFFSET_CAPTURE))
  289.         {
  290.             $mail $regs[1][0];
  291.             $replacement JHtml::_('email.cloak'$mail$mode);
  292.  
  293.             // Replace the found address with the js cloaked email
  294.             $text substr_replace($text$replacement$regs[1][1]strlen($mail));
  295.         }
  296.  
  297.         return true;
  298.     }
  299. }

Documentation generated on Tue, 19 Nov 2013 15:02:28 +0100 by phpDocumentor 1.4.3