Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import os
- from pathlib import Path
- def httpheader():
- print("Content-Type: text/html")
- print("""
- <!DOCTYPE html>
- <html>
- <head>
- <title>HTTP File Browser</title>
- """)
- print('<link rel="stylesheet" href="{}">'.format("../style.css"))
- print("""
- </head>
- <body>
- """)
- def httpfooter():
- print("""
- <br>
- <a href="../index.html">Home</a>
- <br><br><br>
- <footer>
- All icons used are sourced from www.iconarchive.com
- under the <a href="https://creativecommons.org/licenses/by-nc-nd/4.0/">CC BY-NC-ND 4.0</a> license. <br>
- Artists: Hopstart, IconsMind
- </footer>
- </body>
- </html>
- """)
- def loadblacklist(dir_blacklist="blacklist"):
- with open(dir_blacklist, "r") as f:
- safe_dirlist = [line.rstrip() for line in f]
- return safe_dirlist
- def input_check(inp_str):
- 'Returns false if [inp_str] fails sanitation checks'
- all_same = lambda s: s == len(s) * s[0]
- if "." in inp_str:
- return False
- elif all_same(inp_str) and inp_str[0] == "/":
- return False
- else:
- return True
- def subdir(subdir_path, delmode=True):
- dir_icon = '<img src="icons/folder.png" class="icon">'
- frm_head = '<form action="filemanscript.py" method="post">'
- subdir_button = "<input type='submit' class='subdirbutton' name='directory' value='{}' />".format(subdir_path)
- backdir_data = "<input type='hidden' name='directory' value='{}' />".format(str(Path(subdir_path).parent))
- trash_button = "<button type='submit' class='del_button' name='trashitem' value='{}'>Delete</button>".format(subdir_path)
- trash_form = frm_head + backdir_data + trash_button + "</form>"
- if not delmode:
- trash_form = ""
- dir_icon = '<img src="icons/back.png" class="icon">'
- subdir_form = frm_head + dir_icon + subdir_button + "</form>"
- return "<div style='display: flex;'>" + subdir_form + trash_form + "</div>"
- def dirlist(server_root, local_root, dir_selected, up_msg=False):
- print("<h1>" + dir_selected + "/</h1>")
- print("<div style='display: flex;'>")
- # File upload block
- print("""
- <form enctype="multipart/form-data" style="padding-right: 15px;" action="filemanscript.py" method="post">
- <h4>File Upload</h4>
- <p><input type="file" name="uploadname" /><input type="submit" value="Upload" /></p>
- """)
- print("<input type='hidden' name='directory' value='{}' />".format(dir_selected))
- print("</form>")
- # Mkdir block
- print("<form action='filemanscript.py' method='post'>")
- print("<h4>Create Directory</h4>")
- print("<p><input type='text' name='newdir' /> <input type='submit' value='Make' /></p>")
- print("<input type='hidden' name='directory' value='{}' />".format(dir_selected))
- print("</form>")
- print("</div>")
- if up_msg: print("<p>" + up_msg + "</p>")
- print("<hr>")
- dir_list = os.listdir(local_root + dir_selected)
- dir_list.sort()
- dir_server = server_root + dir_selected
- # link_order is used to allow directories to be put at the top of the page
- link_order = []
- # File download block
- for filename in dir_list:
- linux_path = local_root + dir_selected + "/" + filename
- server_path = dir_server + "/" + filename
- if os.path.isdir(linux_path):
- link_order.insert(0, subdir(dir_selected + "/" + filename))
- else:
- frm_head = """
- <form id="man_form" action="filemanscript.py" method="post">
- <input type='hidden' name='directory' value='{}' />
- """.format(dir_selected)
- dwn_button = "<a class='button' href='{}' download>Download</a>".format(server_path)
- prv_button = "<a class='button' href='{}'>Preview</a>".format(server_path)
- del_button = "<button type='submit' class='del_button' name='trashitem' value='{}'>Delete</button>".format(
- dir_selected + "/" + filename)
- link_order.append("{}<p><img src='icons/file.png' class='icon'>{}{}{}{}</p></form>".format(
- frm_head, filename, dwn_button, prv_button, del_button))
- if len(dir_selected.split("/")) > 1:
- # Add button to go back to previous directory
- back_dir = str(Path(dir_selected).parent)
- link_order.insert(0, subdir(back_dir, delmode=False))
- for item in link_order:
- print(item)
- def wrongpage(dir_selected):
- print("<h1>Something's gone wrong</h1>")
- print("You're attempting to access either an illegal or<br>")
- print("nonexistant directory.<br>")
- print("The culprit:<br>")
- print()
- print("<h3>Selected directory: " + dir_selected + "</h3>")
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement