BBQ*_*hef 2 ruby-on-rails-3 ruby-on-rails-3.2
我想知道rails提供什么来混淆电子邮件地址以保护它免受爬虫,垃圾邮件收发器和邮件收集器的攻击,收集地址以发送垃圾邮件.
可能是我使用了错误的关键字,但实际上找不到宝石.
我找到了一个统计数据,比较了掩盖邮件地址的不同方法:http: //techblog.tilllate.com/2008/07/20/ten-methods-to-obfuscate-e-mail-addresses-compared/
我写了一个结合前两种方法的片段.
剪辑尚未成熟,但无论如何我想分享它,它可能是面临同样问题的其他人的起点. (下一步将是使用模糊的纯文本替换已链接的地址.)
在继续之前,我想知道rails中的最佳实践.这是一个常见的问题,我一定错过了处理它的宝石!?
如果我使用我的方法,在我的应用程序中集成/触发它的最佳方法是什么?
任何一种before_filter?渲染之前??? 那样的东西?
或者就像我现在这样做,在视图中将其作为helper_methode调用?
它甚至可以添加到字符串类...
在我的 application_helper.rb
def obfuscate_emails(content, domain_prefix = 'nirvana', clss = 'maildecode')
# This shall protect emails from spam spiders/crawlers gathering emails from webpages
# Add the following SASS to your Stylesheets
#
# span.maildecode
# direction: rtl
# unicode-bidi: bidi-override
#
# Further more you might want to use Javascript(.erb) to add links to the email addresses like this
#
# $(document).ready(function() {
# function link_emails(subdomain){
# console.log("Find an replace reverse emails, fake subdomain is "+subdomain);
# $(".maildecode").each(function() {
# email = $(this).text().replace('.'+subdomain,'').split("").reverse().join("");
# console.log("- clean email is "+email);
# // $(this).html($(this).text().replace('.'+subdomain,'')); // uncomment if you like to clean up the html a bit
# $(this).wrap('<a href="mailto:'+email+'">');
# });
# }
#
# link_emails('<%= ENV['OBFUSCATE_EMAIL_SUBDOMAIN'] %>');
# });
#
# Thanks to
# http://techblog.tilllate.com/2008/07/20/ten-methods-to-obfuscate-e-mail-addresses-compared/
email_re = /[\w.!#\$%+-]+@[\w-]+(?:\.[\w-]+)+/
content.scan(email_re).each do |mail|
obfuscate_mail = "<span class='#{clss}'>#{mail.reverse.split('@')[0]}<span style='display: none;'>.#{domain_prefix}</span>@#{mail.reverse.split('@')[1]}</span>"
content = content.sub(mail, obfuscate_mail)
end
content # use raw(obfuscate_emails(content)) otherwise rails will escape the html
end
Run Code Online (Sandbox Code Playgroud)
只需使用mail_toRails拥有的内置帮助程序......
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-mail_to
mail_to 'email@here.com', 'click to email', :encode => .... # couple of encoding options
Run Code Online (Sandbox Code Playgroud)
注意:这不再适用于Rails 4.从文档:在Rails 4.0之前,mail_to提供了编码地址的选项,以阻止电子邮件收集者.要利用这些选项,请安装actionview-encoded_mail_to gem.(感谢@zwippie)
| 归档时间: |
|
| 查看次数: |
1465 次 |
| 最近记录: |