import os import re from django.shortcuts import render from django.http import HttpResponse, HttpResponseRedirect, Http404 from django.core.urlresolvers import reverse from django.template import Context, loader import django.forms as forms from tinymce.widgets import TinyMCE from troggle.helper import login_required_if_public from troggle.flatpages.models import Redirect, EntranceRedirect from troggle.core.models_caves import Cave import troggle.core.views_caves import troggle.settings as settings def flatpage(request, path): try: r = Redirect.objects.get(originalURL = path) return HttpResponseRedirect(r.newURL) # Redirect after POST except Redirect.DoesNotExist: pass try: r = Cave.objects.get(url = path) return troggle.core.views_caves.caveSlug(request, r.slug()) except Cave.DoesNotExist: pass try: r = EntranceRedirect.objects.get(originalURL = path) return troggle.core.views_caves.entranceSlug(request, r.entrance.slug()) except EntranceRedirect.DoesNotExist: pass if path.startswith("noinfo") and settings.PUBLIC_SITE and not request.user.is_authenticated(): print(("flat path noinfo", path)) return HttpResponseRedirect(reverse("auth_login") + '?next=%s' % request.path) if path.endswith("/") or path == "": try: o = open(os.path.normpath(settings.EXPOWEB + path + "index.html"), "rb") path = path + "index.html" except IOError: try: o = open(os.path.normpath(settings.EXPOWEB + path + "index.htm"), "rb") path = path + "index.htm" except IOError: return render(request, 'pagenotfound.html', {'path': path}) else: try: filetobeopened = os.path.normpath(settings.EXPOWEB + path) o = open(filetobeopened, "rb") except IOError: return render(request, 'pagenotfound.html', {'path': path}) if path.endswith(".htm") or path.endswith(".html"): 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 + "Page could not be split into header and body") 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'(.*)