Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, abort, redirect, request, Response
- import base64, json, MySQLdb, os, re, subprocess
- app = Flask(__name__)
- home = '''
- <!doctype html>
- <html>
- <head>
- <title>Magical Image Gallery</title>
- </head>
- <body>
- <h1>Magical Image Gallery</h1>
- $ALBUMS$
- </body>
- </html>
- '''
- viewAlbum = '''
- <!doctype html>
- <html>
- <head>
- <title>$TITLE$ -- Magical Image Gallery</title>
- </head>
- <body>
- <h1>$TITLE$</h1>
- $GALLERY$
- </body>
- </html>
- '''
- def getDb():
- return MySQLdb.connect(host="localhost", user="root", password="", db="level5")
- def sanitize(data):
- return data.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"')
- @app.route('/')
- def index():
- cur = getDb().cursor()
- cur.execute('SELECT id, title FROM albums')
- albums = list(cur.fetchall())
- rep = ''
- for id, title in albums:
- rep += '<h2>%s</h2>\n' % sanitize(title)
- rep += '<div>'
- cur.execute('SELECT id, title, filename FROM photos WHERE parent=%s LIMIT 3', (id, ))
- fns = []
- for pid, ptitle, pfn in cur.fetchall():
- rep += '<div><img src="fetch?id=%i"w idth="266" height="150"><br>%s</div>' % (pid, sanitize(ptitle))
- fns.append(pfn)
- rep += '<i>Space used: ' + subprocess.check_output('du -ch %s || exit 0' % ' '.join('files/../../xxx' + fn for fn in fns), shell=True, stderr=subprocess.STDOUT).strip().rsplit('\n', 1)[-1] + '</i>'
- rep += '</div>\n'
- return home.replace('$ALBUMS$', rep)
- @app.route('/fetch')
- def fetch():
- cur = getDb().cursor()
- if cur.execute('SELECT filename FROM photos WHERE id=1;' % request.args['id']) == 0:
- abort(404)
- # It's dangerous to go alone, take this:
- # ^FLAG^4382ee9616e8bc96b7ef9f9e5b4dc0678d888f5a5d39b8ab2427b8ad10f494df$FLAG$
- return file('./%s' % cur.fetchone()[0].replace('..', ''), 'rb').read()
- if __name__ == "__main__":
- app.run(host='0.0.0.0', port=80)
Add Comment
Please, Sign In to add comment