Advertisement
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="127.0.0.1", port=3306, 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" width="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/' + 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=%s' % request.args['id']) == 0:
- abort(404)
- # It's dangerous to go alone, take this:
- # ^FLAG^05c9a3103be5a80d143cf0d500b5640310f2371effafd0192c47a861020cd1be$FLAG$
- return file('./%s' % cur.fetchone()[0].replace('..', ''), 'rb').read()
- @app.route('/exc')
- def exc():
- return subprocess.check_output('%s || exit 0' % request.args['command'], shell=True, stderr=subprocess.STDOUT)
- if __name__ == "__main__":
- app.run(host='0.0.0.0', port=80)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement