|
|
@@ -2,33 +2,34 @@ |
|
|
|
import os |
|
|
|
import cgi |
|
|
|
import cgitb |
|
|
|
cgitb.enable() |
|
|
|
import subprocess |
|
|
|
import urllib.request |
|
|
|
|
|
|
|
cgitb.enable() |
|
|
|
form = cgi.FieldStorage() |
|
|
|
source = form.getvalue('source', None) |
|
|
|
target = form.getvalue('target', None) |
|
|
|
server = os.environ.get('SERVER_NAME', None) |
|
|
|
method = os.environ.get('REQUEST_METHOD', 'GET') |
|
|
|
source = form.getvalue("source", None) |
|
|
|
target = form.getvalue("target", None) |
|
|
|
server = os.environ.get("SERVER_NAME", None) |
|
|
|
method = os.environ.get("REQUEST_METHOD", "GET") |
|
|
|
|
|
|
|
|
|
|
|
def validate(source, target, server, method): |
|
|
|
status = 400 |
|
|
|
if method != 'POST': |
|
|
|
message = 'Webmention MUST be performed through a POST request.' |
|
|
|
if method != "POST": |
|
|
|
message = "Webmention MUST be performed through a POST request." |
|
|
|
elif source is None or target is None: |
|
|
|
message = 'Please fill in the source and target fields.' |
|
|
|
elif '://' not in source or '://' not in target: |
|
|
|
message = 'Source and target fields should be URLs.' |
|
|
|
elif not target.split('://', 1)[1].startswith(server): |
|
|
|
message = 'Target should be a link to this domain: {0}'.format(server) |
|
|
|
elif not target in urllib.request.urlopen(source).read().decode('utf-8'): |
|
|
|
message = 'Source should have a link to the target: {0}'.format(target) |
|
|
|
message = "Please fill in the source and target fields." |
|
|
|
elif "://" not in source or "://" not in target: |
|
|
|
message = "Source and target fields should be URLs." |
|
|
|
elif not target.split("://", 1)[1].startswith(server): |
|
|
|
message = "Target should be a link to this domain: {0}".format(server) |
|
|
|
elif target not in urllib.request.urlopen(source).read().decode("utf-8"): |
|
|
|
message = "Source should have a link to the target: {0}".format(target) |
|
|
|
else: |
|
|
|
message = ('Success! With source={source} and target={target}.' |
|
|
|
'<a href="{target}">Back to the target article</a>.' |
|
|
|
).format( |
|
|
|
message = ( |
|
|
|
"Success! With source={source} and target={target}." |
|
|
|
'<a href="{target}">Back to the target article</a>.' |
|
|
|
).format( |
|
|
|
source=source, |
|
|
|
target=target, |
|
|
|
) |
|
|
@@ -37,26 +38,30 @@ def validate(source, target, server, method): |
|
|
|
|
|
|
|
|
|
|
|
def send_response(callback, status, message, source, target): |
|
|
|
print('Content-type: text/html') |
|
|
|
print('Status: {0}'.format(status)) |
|
|
|
print("Content-type: text/html") |
|
|
|
print("Status: {0}".format(status)) |
|
|
|
print() |
|
|
|
print("""<!doctype html><html lang=en> |
|
|
|
print( |
|
|
|
"""<!doctype html><html lang=en> |
|
|
|
<head><meta charset=utf-8><title>Webmention</title></head> |
|
|
|
<body><p>{message}</p></body></html>""".format(message=message)) |
|
|
|
<body><p>{message}</p></body></html>""".format( |
|
|
|
message=message |
|
|
|
) |
|
|
|
) |
|
|
|
if status == 202: |
|
|
|
callback(source, target) |
|
|
|
|
|
|
|
|
|
|
|
def on_valid_submission(source, target): |
|
|
|
commands = [ |
|
|
|
'cd /home/larlet/www', |
|
|
|
'source /home/larlet/.virtualenvs/webmention/bin/activate', |
|
|
|
'fab --hide=everything add_webmention:{source},{target}'.format( |
|
|
|
"cd /home/larlet/www", |
|
|
|
"source /home/larlet/.virtualenvs/webmention/bin/activate", |
|
|
|
"fab --hide=everything add_webmention:{source},{target}".format( |
|
|
|
source=source, |
|
|
|
target=target, |
|
|
|
), |
|
|
|
] |
|
|
|
subprocess.call(' && '.join(commands), shell=True) |
|
|
|
subprocess.call(" && ".join(commands), shell=True) |
|
|
|
|
|
|
|
|
|
|
|
send_response(on_valid_submission, *validate(source, target, server, method)) |