OLD | NEW |
(Empty) | |
| 1 # coding: utf-8 |
| 2 |
| 3 # This file is part of the Adblock Plus web scripts, |
| 4 # Copyright (C) 2006-2015 Eyeo GmbH |
| 5 # |
| 6 # Adblock Plus is free software: you can redistribute it and/or modify |
| 7 # it under the terms of the GNU General Public License version 3 as |
| 8 # published by the Free Software Foundation. |
| 9 # |
| 10 # Adblock Plus is distributed in the hope that it will be useful, |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 # GNU General Public License for more details. |
| 14 # |
| 15 # You should have received a copy of the GNU General Public License |
| 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
| 18 import fcntl |
| 19 import hmac |
| 20 import hashlib |
| 21 import wsgiref.util |
| 22 from urlparse import parse_qs, urljoin |
| 23 from urllib import urlencode |
| 24 |
| 25 from sitescripts.utils import get_config, sendMail, encode_email_address |
| 26 from sitescripts.web import url_handler, form_handler |
| 27 |
| 28 VERIFICATION_PATH = '/verifyEmail' |
| 29 |
| 30 def sign(config, data): |
| 31 secret = config.get('submit_email', 'secret') |
| 32 return hmac.new(secret, data, hashlib.sha1).hexdigest() |
| 33 |
| 34 @url_handler('/submitEmail') |
| 35 @form_handler |
| 36 def submit_email(environ, start_response, data): |
| 37 email = data.get('email', '').strip() |
| 38 try: |
| 39 email = encode_email_address(email) |
| 40 except ValueError: |
| 41 start_response('400 Bad Request', [('Content-Type', 'text/plain')]) |
| 42 return ['No valid email address given.'] |
| 43 |
| 44 config = get_config() |
| 45 sendMail( |
| 46 config.get('submit_email', 'verification_email_template'), |
| 47 { |
| 48 'recipient': email, |
| 49 'verification_url': '%s?%s' % ( |
| 50 urljoin(wsgiref.util.application_uri(environ), VERIFICATION_PATH), |
| 51 urlencode([('email', email), ('signature', sign(config, email))]) |
| 52 ) |
| 53 } |
| 54 ) |
| 55 |
| 56 start_response('200 OK', [('Content-Type', 'text/plain')]) |
| 57 return ["Thanks for your submission! You'll receive a verification email short
ly."] |
| 58 |
| 59 @url_handler(VERIFICATION_PATH) |
| 60 def verify_email(environ, start_response): |
| 61 config = get_config() |
| 62 |
| 63 params = parse_qs(environ.get('QUERY_STRING', '')) |
| 64 email = params.get('email', [''])[0] |
| 65 signature = params.get('signature', [''])[0] |
| 66 |
| 67 if sign(config, email) == signature: |
| 68 filename = config.get('submit_email', 'filename') |
| 69 with open(filename, 'ab', 0) as file: |
| 70 fcntl.lockf(file, fcntl.LOCK_EX) |
| 71 try: |
| 72 print >>file, email |
| 73 finally: |
| 74 fcntl.lockf(file, fcntl.LOCK_UN) |
| 75 |
| 76 option = 'successful_verification_redirect_location' |
| 77 else: |
| 78 option = 'failed_verification_redirect_location' |
| 79 |
| 80 start_response('303 See Other', [('Location', config.get('submit_email', optio
n))]) |
| 81 return [] |
OLD | NEW |