summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mcrute@gmail.com>2012-07-29 13:39:23 -0400
committerMike Crute <mcrute@gmail.com>2012-07-29 13:39:23 -0400
commitd4c4aaac0be5a52d5fcca67596fa522d57553acf (patch)
tree9f14c8d935cc1451d4f4b744af4cadb6575cab3d
downloadtiny-webapps-d4c4aaac0be5a52d5fcca67596fa522d57553acf.tar.bz2
tiny-webapps-d4c4aaac0be5a52d5fcca67596fa522d57553acf.tar.xz
tiny-webapps-d4c4aaac0be5a52d5fcca67596fa522d57553acf.zip
Initial import
-rw-r--r--email_gateway.cfg38
-rwxr-xr-xemail_gateway.py84
-rwxr-xr-xip_reflector.py11
-rwxr-xr-xthumbnailer.py125
4 files changed, 258 insertions, 0 deletions
diff --git a/email_gateway.cfg b/email_gateway.cfg
new file mode 100644
index 0000000..31a3691
--- /dev/null
+++ b/email_gateway.cfg
@@ -0,0 +1,38 @@
1; =====================
2; Example Configuration
3; =====================
4; [form-key]
5; to = you@example.com
6; subject = My awesome contact form
7; from = Contact Form <noreply@example.com>
8; message = First line of the message
9; redirect = /contact-thanks.html
10; site = http://(?:www\.)?example.com
11
12; ======================
13; Required Configuration
14; ======================
15; INI section is the form key to be passed by the form
16;
17; These must all be specified in some form or the app breaks
18;
19; to -- Email address (comma separated list) to receive the email
20; subject -- Email subject
21; from -- Email sent from email
22; message -- Static message subject
23; redirect -- URL to redirect to
24; site -- Regex to match against referrer header
25
26; ===================
27; Special Form Fields
28; ===================
29; mailer.form-key -- Form key from config file
30; mailer.redirect -- Target for a redirect
31; mailer.fields.ignore -- Ignored fields
32; mailer.labels.* -- Labels for form fields
33
34; mailer.fields.message -- Message field
35; mailer.fields.subject -- Subject field
36
37; mailer.subject -- Subject of message
38; mailer.message -- Message
diff --git a/email_gateway.py b/email_gateway.py
new file mode 100755
index 0000000..e73e3e2
--- /dev/null
+++ b/email_gateway.py
@@ -0,0 +1,84 @@
1#!/usr/bin/env python2.6
2
3import os
4import smtplib
5import re
6import urlparse
7from cStringIO import StringIO
8from email.mime.text import MIMEText
9from ConfigParser import SafeConfigParser as ConfigParser, NoSectionError
10
11
12config = ConfigParser()
13with open("/etc/email_gateway.cfg") as fp:
14 config.readfp(fp)
15
16
17def send_message(text, subject, to, from_email):
18 msg = MIMEText(text.getvalue(), "plain")
19
20 msg['Subject'] = subject
21 msg['From'] = from_email
22 msg['To'] = to
23
24 p = os.popen("/usr/sbin/sendmail -t", "w")
25 p.write(msg.as_string())
26 p.close()
27
28
29def email_app(environ, start_response):
30 ignored_fields = []
31 useful_fields = []
32 form_key = None
33 message_buffer = StringIO()
34
35 context = {}
36
37 fields = urlparse.parse_qsl(environ["wsgi.input"].read())
38 for key, value in fields:
39 if key == "mailer.form-key":
40 form_key = value
41 elif key == "mailer.redirect":
42 context["redirect"] = value
43 elif key == "mailer.subject":
44 context["subject"] = value
45 elif key == "mailer.message":
46 context["message"] = value
47 elif key == "mailer.fields.ignore":
48 ignored_fields = value.split(",")
49 else:
50 useful_fields.append((key, value))
51
52 try:
53 my_config = dict(config.items(form_key))
54 site_matcher = re.compile(my_config["site"])
55 except NoSectionError:
56 start_response('403 Forbidden', [('Content-Type', 'text/plain')])
57 return "Invalid form key!"
58
59 if not site_matcher.match(environ["HTTP_REFERER"]):
60 start_response('403 Forbidden', [('Content-Type', 'text/plain')])
61 return "Invalid send!"
62
63 useful_fields = ["{0}: {1}".format(*f)
64 for f in useful_fields
65 if f[0] not in ignored_fields]
66
67 message_buffer.write(context.get("message", my_config["message"]))
68 message_buffer.write("\n\n")
69 message_buffer.write("\n".join(useful_fields))
70
71 send_message(message_buffer,
72 context.get("subject", my_config["subject"]),
73 my_config["to"],
74 my_config["from"])
75
76 redirect_location = context.get("redirect", my_config["redirect"])
77 start_response('302 Found', [('Location', redirect_location)])
78
79 return ""
80
81
82if __name__ == "__main__":
83 from flup.server.fcgi_fork import WSGIServer
84 WSGIServer(email_app, maxSpare=1).run()
diff --git a/ip_reflector.py b/ip_reflector.py
new file mode 100755
index 0000000..8ccd9f4
--- /dev/null
+++ b/ip_reflector.py
@@ -0,0 +1,11 @@
1#!/usr/bin/env python2.6
2
3
4def reflector_app(environ, start_response):
5 start_response('200 OK', [('Content-Type', 'text/plain')])
6 return environ['REMOTE_ADDR']
7
8
9if __name__ == "__main__":
10 from flup.server.fcgi_fork import WSGIServer
11 WSGIServer(reflector_app, maxSpare=1).run()
diff --git a/thumbnailer.py b/thumbnailer.py
new file mode 100755
index 0000000..cebbf72
--- /dev/null
+++ b/thumbnailer.py
@@ -0,0 +1,125 @@
1#!/usr/bin/env python2.6
2
3import hmac
4import urllib2
5import urlparse
6from cStringIO import StringIO
7
8from PIL import Image, ImageOps
9from PIL.ImageFileIO import ImageFileIO
10
11
12def crop_image(img, width, height):
13 src_width, src_height = img.size
14 src_ratio = float(src_width) / float(src_height)
15 dst_width, dst_height = int(width), int(height)
16 dst_ratio = float(dst_width) / float(dst_height)
17
18 if dst_ratio < src_ratio:
19 crop_height = src_height
20 crop_width = crop_height * dst_ratio
21 x_offset = float(src_width - crop_width) / 2
22 y_offset = 0
23 else:
24 crop_width = src_width
25 crop_height = crop_width / dst_ratio
26 x_offset = 0
27 y_offset = float(src_height - crop_height) / 3
28
29 img = img.crop((x_offset, y_offset, x_offset+int(crop_width), y_offset+int(crop_height)))
30
31 return img
32
33
34def calculate_size(img, long_side):
35 width, height = img.size
36 width, height = float(width), float(height)
37
38 if height > width:
39 width = (width / height) * long_side
40 height = long_side
41 else:
42 height = (height / width) * long_side
43 width = long_side
44
45 return width, height
46
47
48def load_remote_image(url):
49 return Image.open(ImageFileIO(urllib2.urlopen(url)))
50
51
52def create_thumbnail(img, long_side):
53 width, height = calculate_size(img, int(long_side))
54 img.thumbnail((width, height), Image.ANTIALIAS)
55 return img
56
57
58def desaturate_image(img):
59 img = ImageOps.grayscale(img)
60 return img
61
62
63KEY = ""
64
65OPERATIONS = {
66 "thumb": (create_thumbnail, 1),
67 "crop": (crop_image, 2),
68 "desaturate": (desaturate_image, 0),
69}
70
71def generate_sig(query):
72 clean = ["{0}{1}".format(k,v) for k, v in query if k != "sig"]
73 return hmac.new(KEY, "".join(clean)).hexdigest()
74
75
76def thumbnailer_app(environ, start_response):
77 to_do = []
78 qs = []
79 for tup in urlparse.parse_qsl(environ.get('QUERY_STRING', '')):
80 if tup[0] == "sig":
81 sig = tup[1]
82 else:
83 qs.append(tup)
84
85 # Validate
86 if generate_sig(qs) != sig:
87 start_response('401 Forbidden', [('Content-Type', 'text/plain')])
88 return ["You may not use the thumbnailer for that site."]
89
90 need_values = 0
91 for key, value in qs:
92 if need_values > 0:
93 to_do[-1].append(value)
94 need_values -= 1
95 continue
96
97 if key == "image":
98 path = value
99 elif key == "op":
100 op, args = OPERATIONS[value]
101 need_values = args
102 to_do.append([op])
103
104 # Start the web response
105 start_response('200 OK', [('Content-Type', 'image/jpeg')])
106
107 img = load_remote_image(path)
108
109 for action in to_do:
110 func = action.pop(0)
111 img = func(img, *action)
112
113 return img.tostring('jpeg', img.mode)
114
115
116if __name__ == "__main__":
117 import sys
118
119 if len(sys.argv) == 2:
120 qs = urlparse.parse_qsl(urlparse.urlparse(sys.argv[-1]).query)
121 print "{0}&sig={1}".format(sys.argv[-1], generate_sig(qs))
122 sys.exit(0)
123
124 from flup.server.fcgi_fork import WSGIServer
125 WSGIServer(thumbnailer_app, debug=True, maxSpare=1).run()