def rewrite_urls(body): from adhocracy.lib.helpers import base_url if not config.get_bool('adhocracy.track_outgoing_links'): return body doc = lxml.etree.fromstring('<body>' + body + '</body>') for a in doc.xpath('.//a[@href]'): if re.match(r'ftps?:|https?:|//', a.attrib['href']): url = a.attrib['href'] # Is it a link to our own site? base = base_url('/', instance=None) if (url.startswith(base) and not (url.startswith('//') and base == '/')): continue encoded_url = base64.urlsafe_b64encode(url.encode('utf-8')) signed_url = sign(encoded_url, salt=REDIRECT_SALT) redirect_url = u'/outgoing_link/' + signed_url.decode('utf-8') a.attrib['href'] = base_url(redirect_url) res = lxml.etree.tostring(doc) return res[len(b'<body>'):-len(b'</body>')]
def rewrite_urls(body): from adhocracy.lib.helpers import base_url if not config.get_bool('adhocracy.track_outgoing_links'): return body doc = lxml.etree.fromstring('<body>' + body + '</body>') for a in doc.xpath('.//a[@href]'): if re.match(r'ftps?:|https?:|//', a.attrib['href']): url = a.attrib['href'] # Is it a link to our own site? base = base_url('/', instance=None) if (url.startswith(base) and not(url.startswith('//') and base == '/')): continue encoded_url = base64.urlsafe_b64encode(url.encode('utf-8')) signed_url = sign(encoded_url, salt=REDIRECT_SALT) redirect_url = u'/outgoing_link/' + signed_url.decode('utf-8') a.attrib['href'] = base_url(redirect_url) res = lxml.etree.tostring(doc) return res[len(b'<body>'):-len(b'</body>')]
def encode(self, value): byte_val = base64.b64encode(_encode_json(value)) encoded = sign(byte_val, self._secret, _SALT) return encoded.decode('ascii')