Zend Framework Source Code Snippets

Mailto Obfuscator

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
<?php
 
class Vendor_View_Filter_ObfuscateMailto implements Zend_Filter_Interface
{
        /**         * Regex to find mailto links
         * Note that it only finds the links,
         * it does not care about the validity of the email-address
         *
         * @var string         */
        protected $_pattern = '/<a(.*)href="mailto:([^"]*)"(.*)>(.*)</a>/iu';
        
        /**
         * Defined in Zend_Filter_Interface         *
         * @param       mixed   $value  Value to filter
         * @return      mixed                   Filtered value
         */
        public function filter($value)        {
                return preg_replace_callback($this->_pattern, array($this, '_obfuscate'), $value);
        }
        
        /**         * Obfuscates found mailto links to encoded javascript
         *
         * @param       array   $matches        Matches from regex
         * @return      string                          Obfuscated mailto link
         */        protected function _obfuscate(array $matches)
        {
                // javascript to be executed
                $javascript = "document.write('". $matches[0] ."')";
                                // empty string that will hold encoded version of javascript 
                $encodedJavascript = '';
                
                // encode each character from $javascript to hex and append it to $encodedJavascript
                for($i = 0; $i < strlen($javascript); $i++) {                        $encodedJavascript .= '%' . bin2hex($javascript[$i]);
                }
                
                // return as html script-tag
                return '<script type="text/javascript">eval(unescape(''. $encodedJavascript .''))</script>';        }
}
1
2
$view->addFilterPath('Vendor/View/Filter', 'Vendor_View_Filter');
$view->addFilter('ObfuscateMailto');

Comments

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

Output filter to obfuscate mailto: links

Obfuscation method is 'kindly borrowed' from smarty template engine.
Uses javascript as obfuscation method, but has no fallback when javascript is disabled.

Snippet details

Created:
Mr. Zogs Mr. Zogs
1 year ago
Edited:
Mr. Zogs Mr. Zogs
1 year ago
Revision Id:
112
Edit Message:
Initial Release
Tags:
mailto obfuscate
Comments:
0
Views:
268
Points:
0 (0 votes)

History

r112

Initial Release

Mr. Zogs Mr. Zogs
1 year ago
diff
r111

Initial Release

Mr. Zogs Mr. Zogs
1 year ago
diff
r110

Initial Release

Mr. Zogs Mr. Zogs
1 year ago
diff
r109

Initial Release

Mr. Zogs Mr. Zogs
1 year ago