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 ["Oops! You didn't enter a valid email address."] |
| 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 ["A confirmation email has been sent. " |
| 58 "Please check your email and click the confirmation link."] |
| 59 |
| 60 @url_handler(VERIFICATION_PATH) |
| 61 def verify_email(environ, start_response): |
| 62 config = get_config() |
| 63 |
| 64 params = parse_qs(environ.get('QUERY_STRING', '')) |
| 65 email = params.get('email', [''])[0] |
| 66 signature = params.get('signature', [''])[0] |
| 67 |
| 68 if sign(config, email) == signature: |
| 69 filename = config.get('submit_email', 'filename') |
| 70 with open(filename, 'ab', 0) as file: |
| 71 fcntl.lockf(file, fcntl.LOCK_EX) |
| 72 try: |
| 73 print >>file, email |
| 74 finally: |
| 75 fcntl.lockf(file, fcntl.LOCK_UN) |
| 76 |
| 77 option = 'successful_verification_redirect_location' |
| 78 else: |
| 79 option = 'failed_verification_redirect_location' |
| 80 |
| 81 start_response('303 See Other', [('Location', config.get('submit_email', optio
n))]) |
| 82 return [] |
OLD | NEW |