| 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 wsgiref.util | 
 |  20 from urlparse import parse_qs, urljoin | 
 |  21 from urllib import urlencode | 
 |  22  | 
 |  23 from sitescripts.utils import get_config, sendMail, encode_email_address | 
 |  24 from sitescripts.web import url_handler, form_handler | 
 |  25 from sitescripts.signing import sign, verify | 
 |  26  | 
 |  27 VERIFICATION_PATH = '/verifyEmail' | 
 |  28  | 
 |  29 @url_handler('/submitEmail') | 
 |  30 @form_handler | 
 |  31 def submit_email(environ, start_response, data): | 
 |  32   email = data.get('email', '').strip() | 
 |  33   try: | 
 |  34     email = encode_email_address(email) | 
 |  35   except ValueError: | 
 |  36     start_response('400 Bad Request', [('Content-Type', 'text/plain')]) | 
 |  37     return ['No valid email address given.'] | 
 |  38  | 
 |  39   sendMail( | 
 |  40     get_config().get('submit_email', 'verification_email_template'), | 
 |  41     { | 
 |  42       'recipient': email, | 
 |  43       'verification_url': '%s?%s' % ( | 
 |  44         urljoin(wsgiref.util.application_uri(environ), VERIFICATION_PATH), | 
 |  45         urlencode([('email', email), ('signature', sign(email))]) | 
 |  46       ) | 
 |  47     } | 
 |  48   ) | 
 |  49  | 
 |  50   start_response('200 OK', [('Content-Type', 'text/plain')]) | 
 |  51   return ["Thanks for your submission! You'll receive a verification email short
    ly."] | 
 |  52  | 
 |  53 @url_handler(VERIFICATION_PATH) | 
 |  54 def verify_email(environ, start_response): | 
 |  55   config = get_config() | 
 |  56  | 
 |  57   params = parse_qs(environ.get('QUERY_STRING', '')) | 
 |  58   email = params.get('email', [''])[0] | 
 |  59   signature = params.get('signature', [''])[0] | 
 |  60  | 
 |  61   if verify(email, signature): | 
 |  62     filename = config.get('submit_email', 'filename') | 
 |  63     with open(filename, 'a', 0) as file: | 
 |  64       fcntl.lockf(file, fcntl.LOCK_EX) | 
 |  65       try: | 
 |  66         print >>file, email | 
 |  67       finally: | 
 |  68         fcntl.lockf(file, fcntl.LOCK_UN) | 
 |  69  | 
 |  70     option = 'successful_verification_redirect_location' | 
 |  71   else: | 
 |  72     option = 'failed_verification_redirect_location' | 
 |  73  | 
 |  74   start_response('303 See Other', [('Location', config.get('submit_email', optio
    n))]) | 
 |  75   return [] | 
| OLD | NEW |