We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17855
    • 43 Posts
    I've just installed:

    https://github.com/wshawn/ObfuscateEmail/blob/master/obfuscateEmail.php

    Via package management, and it's not working under 2.3.3. I'm a bit out of the loop these days and can't see what's wrong. Anyone got it working?

    <?php
    /**
     * ObfuscateEmail plugin for MODx.
     * By Aloysius Lim.
     *
     * Version 0.9.2 (Jun 03, 2011) by W. Shawn Wilkerson
     * Updated Snippet to be compliant with changes in Revolution 2.1
     *
     * Version: 0.9.1 (Apr 15, 2007)
     *
     * This plugin searches for all email addresses and "mailto:" strings in the
     * html output, both inside and outside href attributes. In other words, it also
     * encodes link text.
     *
     * It can find all common email addresses as specified by RFC2822, including all
     * unusual but allowed characters. Any email addresses that satisfy the
     * the construct below will be detected:
     *
     * The plugin than randomly leaves 10% of the characters alone, encodes 45% of
     * them in decimal, and 45% of them in hexadecimal.
     *
     * Changelog:
     * Version 0.9.1 (Apr 15, 2007)
     * Fixed: Regex for atom allowed empty string.
     *
     * Version 0.9.0 (Mar 16, 2007)
     * Original release.
     **/
    function email_regex()
    {
        /* Set up email regex that partially conforms to RFC2822
        * (the ignored parts are indicated):
        *
        * addr-spec       =       local-part "@" domain
        *
        * local-part      =       dot-atom
        *                         / quoted-string              // Ignored
        *                         / obs-local-part             // Ignored
        *
        * domain          =       dot-atom
        *                         / domain-literal             // Ignored
        *                         / obs-domain                 // Ignored
        *
        * dot-atom        =       [CFWS] dot-atom-text [CFWS]  // Ignored CFWS
        *
        * dot-atom-text   =       1*atext *("." 1*atext)
        * atext           =       ALPHA / DIGIT / ; Any character except controls,
        *                         "!" / "#" /     ;  SP, and specials.
        *                         "$" / "%" /     ;  Used for atoms
        *                         "&" / "'" /
        *                         "*" / "+" /
        *                         "-" / "/" /
        *                         "=" / "?" /
        *                         "^" / "_" /
        *                         "`" / "{" /
        *                         "|" / "}" /
        *                         "~"
        */
        $atom = "[-!#$%&'*+/=?^_`{|}~0-9A-Za-z]+";
        $email_half = $atom . '(?:\\.' . $atom . ')*';
        $email = $email_half . '@' . $email_half;
        $email_regex = '<(' . $email . ')>';
        return $email_regex;
    }
    function replaceEntities($matches)
    {
        $address = html_entity_decode($matches[1]);
        $replaced = '';
        for ($i = 0; $i < strlen($address); $i++) {
            $char = $address[$i];
            $r = rand(0, 100);
            # roughly 10% raw, 45% hex, 45% dec
            if ($r > 90) {
                $replaced .= $char;
            }
            else if ($r < 45) {
                $replaced .= '&#x' . dechex(ord($char)) . ';';
            }
            else
            {
                $replaced .= '&#' . ord($char) . ';';
            }
        }
        return $replaced;
    }
    $output = &$modx->resource->_output;
    $output = preg_replace_callback(email_regex(), "replaceEntities", $output);
    $output = preg_replace_callback('/(mailto:)/', "replaceEntities", $output);

      • 3749
      • 24,544 Posts
      Try adding this at the end:

      return '';
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 17855
        • 43 Posts
        thanks BobRay

        I've just tried adding

        return '';


        after

        $output = preg_replace_callback('/(mailto:)/', "replaceEntities", $output);


        And things aren't working. Bit of an odd one.
          • 3749
          • 24,544 Posts
          The only potential problem I see with current versions of PHP is that an E_NOTICE error will be thrown if $matches[1] is not set. I don't understand the code well enough to suggest a fix, though.

          Personally, I never put email addresses on the screen or in the source code of a page, so I don't need obfuscation. I've never really understood the need for it.

            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 17855
            • 43 Posts
            I don't like email in the wild either particularly but it's a style/tone decision on the site I'm working on, we have a contact form but want to use a relaxed tone:

            Got questions? Why not get in touch and say [email protected]

            I've implement this approach:

            http://www.codeforest.net/obfuscate-your-email-address-with-php-javascript-and-css

            Using a snippet and some frontend JS. It works quite well. From some reading around I'm not sure just using encoding would have beaten the spammers. This approach might not either!

              • 13428 ☆ A M B ☆
              • 1,031 Posts
              Emo does a similar thing out of the box.
                • 3749
                • 24,544 Posts
                I see the idea, though I think I'd try to convince the client to use SPForm or something similar and just say "Got Questions? Why not get in touch and say Hello!"

                SPForm is quite spam-resistant and it hides all email addresses.
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting