| LEFT | RIGHT |
| 1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
| 2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present eyeo GmbH |
| 3 # | 3 # |
| 4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
| 5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
| 6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
| 7 # | 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
| 12 # | 12 # |
| 13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 15 | 15 |
| 16 import re | 16 import re |
| 17 import random | 17 import random |
| 18 from urlparse import parse_qsl | |
| 19 from sitescripts.utils import get_config, get_template | 18 from sitescripts.utils import get_config, get_template |
| 20 from sitescripts.web import url_handler | 19 from sitescripts.web import url_handler, form_handler |
| 21 from sitescripts.reports.utils import (calculateReportSecret, | 20 from sitescripts.reports.utils import (calculateReportSecret, |
| 22 calculateReportSecret_compat, getReport, | 21 calculateReportSecret_compat, getReport, |
| 23 saveReport, sendUpdateNotification, | 22 saveReport, sendUpdateNotification, |
| 24 getUserId, updateUserUsefulness) | 23 getUserId, updateUserUsefulness) |
| 25 | 24 |
| 25 GUID_REGEX = r'^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$' |
| 26 |
| 26 | 27 |
| 27 @url_handler('/updateReport') | 28 @url_handler('/updateReport') |
| 28 def handleRequest(environ, start_response): | 29 @form_handler |
| 29 if environ['REQUEST_METHOD'].upper() != 'POST' or not environ.get('CONTENT_T
YPE', '').startswith('application/x-www-form-urlencoded'): | 30 def handleRequest(environ, start_response, params): |
| 30 return showError('Unsupported request method', start_response) | |
| 31 | |
| 32 try: | |
| 33 request_body_length = int(environ['CONTENT_LENGTH']) | |
| 34 except: | |
| 35 return showError('Invalid or missing Content-Length header', start_respo
nse) | |
| 36 | |
| 37 request_body = environ['wsgi.input'].read(request_body_length) | |
| 38 params = {} | |
| 39 for key, value in parse_qsl(request_body): | |
| 40 params[key] = value.decode('utf-8') | |
| 41 | |
| 42 guid = params.get('guid', '').lower() | 31 guid = params.get('guid', '').lower() |
| 43 if not re.match(r'^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$'
, guid): | 32 if not re.match(GUID_REGEX, guid): |
| 44 return showError('Invalid or missing report GUID', start_response) | 33 return showError('Invalid or missing report GUID', start_response) |
| 45 | 34 |
| 46 reportData = getReport(guid) | 35 reportData = getReport(guid) |
| 47 | 36 |
| 48 if reportData == None: | 37 if reportData is None: |
| 49 return showError('Report does not exist', start_response) | 38 return showError('Report does not exist', start_response) |
| 50 | 39 |
| 51 secret = calculateReportSecret(guid) | 40 secret = calculateReportSecret(guid) |
| 52 if params.get('secret', '') != secret and params.get('secret', '') != calcul
ateReportSecret_compat(guid): | 41 if (params.get('secret', '') != secret and |
| 42 params.get('secret', '') != calculateReportSecret_compat(guid)): |
| 53 return showError('Wrong secret value', start_response) | 43 return showError('Wrong secret value', start_response) |
| 54 | 44 |
| 55 reportData['status'] = params.get('status', '') | 45 reportData['status'] = params.get('status', '') |
| 56 if len(reportData['status']) > 1024: | 46 if len(reportData['status']) > 1024: |
| 57 reportData['status'] = reportData['status'][:1024] | 47 reportData['status'] = reportData['status'][:1024] |
| 58 | 48 |
| 59 oldusefulness = reportData.get('usefulness', '0') | 49 oldusefulness = reportData.get('usefulness', '0') |
| 60 reportData['usefulness'] = params.get('usefulness', '0') | 50 reportData['usefulness'] = params.get('usefulness', '0') |
| 61 | 51 |
| 62 if 'email' in reportData: | 52 if 'email' in reportData: |
| 63 updateUserUsefulness(getUserId(reportData['email']), reportData['usefuln
ess'], oldusefulness) | 53 updateUserUsefulness(getUserId(reportData['email']), |
| 54 reportData['usefulness'], oldusefulness) |
| 64 | 55 |
| 65 saveReport(guid, reportData) | 56 saveReport(guid, reportData) |
| 66 | 57 |
| 67 if params.get('notify', '') and 'email' in reportData: | 58 if params.get('notify', '') and 'email' in reportData: |
| 68 email = reportData['email'] | 59 email = reportData['email'] |
| 69 email = re.sub(r' at ', r'@', email) | 60 email = re.sub(r' at ', r'@', email) |
| 70 email = re.sub(r' dot ', r'.', email) | 61 email = re.sub(r' dot ', r'.', email) |
| 71 if re.match(r'^[\w.%+-]+@[\w.%+-]+(\.[\w.%+-]+)+', email): | 62 if re.match(r'^[\w.%+-]+@[\w.%+-]+(\.[\w.%+-]+)+', email): |
| 72 sendUpdateNotification({ | 63 sendUpdateNotification({ |
| 73 'email': email, | 64 'email': email, |
| 74 'url': get_config().get('reports', 'urlRoot') + guid, | 65 'url': get_config().get('reports', 'urlRoot') + guid, |
| 75 'status': reportData['status'], | 66 'status': reportData['status'], |
| 76 }) | 67 }) |
| 77 | 68 |
| 78 newURL = get_config().get('reports', 'urlRoot') + guid | 69 newURL = get_config().get('reports', 'urlRoot') + guid |
| 79 newURL += '?updated=' + str(int(random.uniform(0, 10000))) | 70 newURL += '?updated=' + str(int(random.uniform(0, 10000))) |
| 80 newURL += '#secret=' + secret | 71 newURL += '#secret=' + secret |
| 81 start_response('302 Found', [('Location', newURL.encode('utf-8'))]) | 72 start_response('302 Found', [('Location', newURL.encode('utf-8'))]) |
| 82 return [] | 73 return [] |
| 83 | 74 |
| 84 | 75 |
| 85 def showError(message, start_response): | 76 def showError(message, start_response): |
| 86 template = get_template(get_config().get('reports', 'errorTemplate')) | 77 template = get_template(get_config().get('reports', 'errorTemplate')) |
| 87 start_response('400 Processing Error', [('Content-Type', 'application/xhtml+
xml; charset=utf-8')]) | 78 start_response('400 Processing Error', |
| 79 [('Content-Type', 'application/xhtml+xml; charset=utf-8')]) |
| 88 return [template.render({'message': message}).encode('utf-8')] | 80 return [template.render({'message': message}).encode('utf-8')] |
| LEFT | RIGHT |