import os import re from pathlib import Path from urllib.parse import urljoin, unquote as urlunquote from urllib.request import urlopen from django.shortcuts import render, redirect from django.http import HttpResponse, HttpResponseRedirect, Http404 from django.urls import reverse, resolve from django.template import Context, loader from django.views.decorators.csrf import ensure_csrf_cookie import django.forms as forms from troggle.helper import login_required_if_public from troggle.core.models_caves import Cave import troggle.core.views_caves import troggle.settings as settings def expofiles_redirect(request, path): '''This is used only when running as a test system without a local copy of /expofiles/ ''' return redirect(urljoin('http://expo.survex.com/expofiles/', path)) def expofilessingle(request, filepath): '''sends a single binary file to the user, ''' fn=urlunquote(filepath) fn = Path(settings.EXPOFILES,filepath) if fn.is_dir(): return expofilesdir(request, Path(fn), Path(filepath)) # print(" - expofilessingle {}:{}:{}:".format(filepath, fn, getmimetype(fn))) return HttpResponse(content=open(fn, "rb"),content_type=getmimetype(filepath)) # any file def expofilesdir(request, dirpath, filepath): '''does a directory display. If there is an index.html file we should display that. - dirpath is a Path() and it does not have /expofiles/ in it ''' # print(" - expofilesdir {}".format(dirpath)) urlpath = 'expofiles' / Path(filepath) fileitems = [] diritems = [] for f in dirpath.iterdir(): if f.is_dir(): diritems.append((urlpath / f.parts[-1], str(f.parts[-1]))) else: # if f.parts[-1].lower() == 'index.htm' or f.parts[-1].lower() == 'index.html': # css cwd problem # return HttpResponse(content=open(f, "rb"),content_type=getmimetype(filepath)) # any file # return expofilessingle(request, str(Path(filepath / f.parts[-1]))) fileitems.append((Path(urlpath) / f.parts[-1], str(f.parts[-1]), getmimetype(f))) return render(request, 'dirdisplay.html', { 'filepath': urlpath, 'fileitems':fileitems, 'diritems': diritems,'settings': settings }) def flatpage(request, path): '''Either renders an HTML page from expoweb with all the menus, or serves an unadorned binary file with mime type This is a horrible mess and some code is redundant and unreachable because of urls.py setup ''' # print(" - FLATPAGES delivering the file: {} as MIME type: {}".format(path,getmimetype(path)),flush=True) if path.startswith("noinfo") and settings.PUBLIC_SITE and not request.user.is_authenticated(): # print((" - FLATPAGES redirect to logon: flat path noinfo", path)) return HttpResponseRedirect(urljoin(reverse("auth_login"),'?next={}'.format(request.path))) expowebpath = Path(settings.EXPOWEB) if path.endswith("/") or path == "": # print(" - FLATPAGES the file: {} ENDSWITH ...".format(path)) try: o = open(os.path.normpath(expowebpath / path / "index.html"), "rb") path = path + "index.html" except IOError: try: o = open(os.path.normpath(expowebpath / path / "index.htm"), "rb") path = path + "index.htm" except IOError: return render(request, 'pagenotfound.html', {'path': path}) else: # print(" - FLATPAGES the file: '{}' ...".format(path)) if path.startswith('site_media'): # print(" - MEDIA_ROOT: {} ...{}".format(settings.MEDIA_ROOT, path)) path = path.replace("site_media", settings.MEDIA_ROOT) filetobeopened = os.path.normpath(path) elif path.startswith("static"): # print(" - STATIC_ROOT: {} ...{}".format(settings.MEDIA_ROOT, path)) path = path.replace("static", settings.MEDIA_ROOT) filetobeopened = os.path.normpath(path) else: # print(" - NO _ROOT: {} ...".format(expowebpath)) filetobeopened = os.path.normpath(expowebpath / path) # print(" - FLATPAGES full path : {} ...".format(filetobeopened)) try: o = open(filetobeopened, "rb") #print(" - FLATPAGES full path no error: {} ...".format(filetobeopened)) except IOError: #print(" - FLATPAGES ERROR: {} ...".format(filetobeopened)) #o.close() return render(request, 'pagenotfound.html', {'path': path}) if path.endswith(".htm") or path.endswith(".html"): # add the menus etc. with open(os.path.normpath(expowebpath / path), "rb") as o: html = o.read() m = re.search(rb'(.*)<\s*head([^>]*)>(.*)<\s*/head\s*>(.*)<\s*body([^>]*)>(.*)<\s*/body\s*>(.*)', html, re.DOTALL + re.IGNORECASE) if m: preheader, headerattrs, head, postheader, bodyattrs, body, postbody = m.groups() else: return HttpResponse(html + "HTML Parsing failure: Page could not be split into header and body: failed in flatpages.views.py") m = re.search(rb"(.*)", head, re.DOTALL + re.IGNORECASE) if m: title, = m.groups() else: title = "" m = re.search(rb"]*)noedit", head, re.DOTALL + re.IGNORECASE) if m: editable = False else: editable = True has_menu = False menumatch = re.match(rb'(.*)