summaryrefslogtreecommitdiffstats
path: root/core/views_expo.py
blob: 49f8bc7b2a1b16ab52e91197c4d6a56290c9cf3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
from django.contrib import admin

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

'''Formerly a separate package  'flatpages' written by Martin Green 2011. 
This was NOT django.contrib.flatpages which stores HTML in the database, so the name was chnaged to expopages.
Then it was incorporated into troggle directly, rather than being an unnecessary external package.
'''

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 expowebpage(request, expowebpath, path):
    '''Adds memnus and serves an HTML page
    '''
    if not Path(expowebpath / path).is_file():
        return render(request, 'pagenotfound.html', {'path': path})
        
    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 expowebpage in views_expo.py")
    m = re.search(rb"<title>(.*)</title>", head, re.DOTALL + re.IGNORECASE)
    if m:
        title, = m.groups()
    else:
        title = ""
    m = re.search(rb"<meta([^>]*)noedit", head, re.DOTALL + re.IGNORECASE)
    if m:
        editable = False
    else:
        editable = True
    
    has_menu = False
    menumatch = re.match(rb'(.*)<div id="menu">', body, re.DOTALL + re.IGNORECASE)
    if menumatch:
        has_menu = True
    menumatch = re.match(rb'(.*)<ul id="links">', body, re.DOTALL + re.IGNORECASE)
    if menumatch:
        has_menu = True
    return render(request, 'flatpage.html', {'editable': editable, 'path': path, 'title': title, 
                'body': body, 'homepage': (path == "index.htm"), 'has_menu': has_menu})
    

def expopage(request, path):
    '''Either renders an HTML page from expoweb with all the menus,
    or serves an unadorned binary file with mime type
    '''
    #print(" - EXPOPAGES delivering the file: '{}':{} as MIME type: {}".format(request.path, path,getmimetype(path)),flush=True)

    if path.startswith("noinfo") and settings.PUBLIC_SITE and not request.user.is_authenticated():
        return HttpResponseRedirect(urljoin(reverse("auth_login"),'?next={}'.format(request.path)))

    if path.startswith("admin/"):
        # don't even attempt to handle these sorts of mistakes
        return HttpResponseRedirect("/admin/")

    expowebpath = Path(settings.EXPOWEB)

    if path == "":
        return expowebpage(request, expowebpath, "index.htm")

    if path.endswith(".htm") or path.endswith(".html"):
        return expowebpage(request, expowebpath, path)
    
    if Path(expowebpath / path ).is_dir():
        for p in ["index.html", "index.htm", "default.html"]:
            try:
                o = open(os.path.normpath(expowebpath / path / p), "rb")
            except IOError:
                pass
            else: # no exception, so file was found
                return expowebpage(request, expowebpath, Path(path) / p)
        return render(request, 'pagenotfound.html', {'path': Path(path) / "index.html"})
        
    if path.endswith("/"):
        # we already know it is not a directory.
        # the final / may have been appended by middleware if there was no page without it
        # do not redirect to a file path without the slash as we may get in a loop. Let the user fix it:
        return render(request, 'dirnotfound.html', {'path': path, 'subpath': path[0:-1]})
   
    if path.startswith('site_media'): # BUT we may have missing files, directories or .html here too?!
        # print(" - MEDIA_ROOT: {}  ...{}".format(settings.MEDIA_ROOT, path))
        npath = path.replace("site_media", settings.MEDIA_ROOT)
        filetobeopened = os.path.normpath(npath)
    elif path.startswith("static"):
        # print(" - STATIC_ROOT: {}  ...{}".format(settings.MEDIA_ROOT, path))
        npath = path.replace("static", settings.MEDIA_ROOT)
        filetobeopened = os.path.normpath(npath)
    else:
        filetobeopened = os.path.normpath(expowebpath / path)
    
    try:
        return HttpResponse(content=open(filetobeopened, "rb"), content_type=getmimetype(path))
    except IOError:
        return render(request, 'pagenotfound.html', {'path': path})
 
 

def getmimetype(path):
    path = str(path)
    if path.lower().endswith(".css"): return "text/css"
    if path.lower().endswith(".txt"): return "text/css"
    if path.lower().endswith(".js"): return "application/javascript"
    if path.lower().endswith(".json"): return "application/javascript"
    if path.lower().endswith(".ico"): return "image/vnd.microsoft.icon"
    if path.lower().endswith(".png"): return "image/png"
    if path.lower().endswith(".tif"): return "image/tif"
    if path.lower().endswith(".gif"): return "image/gif"
    if path.lower().endswith(".jpeg"): return "image/jpeg"
    if path.lower().endswith(".jpg"): return "image/jpeg"
    if path.lower().endswith("svg"): return "image/svg+xml"
    if path.lower().endswith("xml"): return "application/xml" # we use "text/xhtml" for tunnel files
    if path.lower().endswith(".pdf"): return "application/pdf"
    if path.lower().endswith(".ps"): return "application/postscript"
    if path.lower().endswith(".svx"): return "application/x-survex-svx"
    if path.lower().endswith(".3d"): return "application/x-survex-3d"
    if path.lower().endswith(".pos"): return "application/x-survex-pos"
    if path.lower().endswith(".err"): return "application/x-survex-err"
    if path.lower().endswith(".odt"): return "application/vnd.oasis.opendocument.text"
    if path.lower().endswith(".ods"): return "application/vnd.oasis.opendocument.spreadsheet"
    if path.lower().endswith(".docx"): return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    if path.lower().endswith(".xslx"): return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    if path.lower().endswith(".gz"): return "application/x-7z-compressed"
    if path.lower().endswith(".7z"): return "application/x-7z-compressed"
    if path.lower().endswith(".zip"): return "application/zip"
    return ""

@login_required_if_public
@ensure_csrf_cookie
def editexpopage(request, path):
    try:
        r = Cave.objects.get(url = path)
        return troggle.core.views_caves.editCave(request, r.cave.slug)
    except Cave.DoesNotExist:
        pass


    try:
        filepath = Path(settings.EXPOWEB) / path
        o = open(filepath, "r")
        html = o.read()
        autogeneratedmatch = re.search(r"\<\!--\s*(.*?(Do not edit|auto-generated).*?)\s*--\>", html, re.DOTALL + re.IGNORECASE)
        if autogeneratedmatch:
            return HttpResponse(autogeneratedmatch.group(1))
        m = re.search(r"(.*)<head([^>]*)>(.*)</head>(.*)<body([^>]*)>(.*)</body>(.*)", html, re.DOTALL + re.IGNORECASE)
        if m:
            filefound = True
            preheader, headerargs, head, postheader, bodyargs, body, postbody = m.groups()
            linksmatch = re.match(r'(.*)(<ul\s+id="links">.*)', body, re.DOTALL + re.IGNORECASE)
            if linksmatch:
                body, links = linksmatch.groups()
#            if re.search(r"iso-8859-1", html):
#                body = str(body, "iso-8859-1")
        else:
            return HttpResponse("Page could not be split into header and body")
    except IOError:
        print("### File not found ### ", filepath)
        filefound = False
        

    if request.method == 'POST': # If the form has been submitted...
        flatpageForm = FlatPageForm(request.POST) # A form bound to the POST data
        if flatpageForm.is_valid():# Form valid therefore write file
            print("### \n", str(flatpageForm)[0:300])
            print("### \n csrfmiddlewaretoken: ",request.POST['csrfmiddlewaretoken'])
            if filefound:
                headmatch =  re.match(r"(.*)<title>.*</title>(.*)", head, re.DOTALL + re.IGNORECASE)
                if headmatch:
                    head = headmatch.group(1) + "<title>" + flatpageForm.cleaned_data["title"] + "</title>" + headmatch.group(2)
                else:
                    head = "<title>" + flatpageForm.cleaned_data["title"] + "</title>"
            else:
                head = "<title>" + flatpageForm.cleaned_data["title"] + "</title>"
                preheader = "<html>"
                headerargs = ""
                postheader = ""
                bodyargs = ""
                postbody = "</html>" 
            body = flatpageForm.cleaned_data["html"]
            body = body.replace("\r", "")
            result = "%s<head%s>%s</head>%s<body%s>\n%s</body>%s" % (preheader, headerargs, head, postheader, bodyargs, body, postbody)
            f = open(filepath, "w")
            f.write(result)
            f.close()
            return HttpResponseRedirect(reverse('flatpage', args=[path])) # Redirect after POST
    else:
        if filefound:
            m = re.search(r"<title>(.*)</title>", head, re.DOTALL + re.IGNORECASE)
            if m: 
                title, = m.groups()
            else:
                title = ""
            flatpageForm = FlatPageForm({"html": body, "title": title})
        else:
            body = "### File not found ###\n"    + str(filepath)
            flatpageForm = FlatPageForm({"html": body, "title": "Missing"})
    return render(request, 'editexpopage.html', {'path': path, 'form': flatpageForm, })

class FlatPageForm(forms.Form):
    title = forms.CharField(widget=forms.TextInput(attrs={'size':'60'}))

    #html = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 20}))
    html = forms.CharField(widget=forms.Textarea(attrs={"cols":80, "rows":20}))