aboutsummaryrefslogtreecommitdiff
path: root/web.go
blob: 7016dfd729e68beab43a12b2158e627e41f38ed8 (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
package main

import (
	"fmt"
	"html"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strings"
)

func htErr(w http.ResponseWriter, msg string, err error) {
	w.Header().Set("Content-Type", "text/plain")
	w.Header().Set("Cache-Control", *cacheCtl)
	fmt.Fprintln(w, msg, ":", err)
	log.Printf("error: %v : %v", msg, err)
}

func header(w http.ResponseWriter, uDir, sort string) {
	eDir := html.EscapeString(uDir)
	w.Header().Set("Content-Type", "text/html")
	w.Header().Set("Cache-Control", *cacheCtl)
	w.Write([]byte(`
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <HTML LANG="en">
    <HEAD>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=UTF-8">
    <META HTTP-EQUIV="Content-Language" CONTENT="en-US">
    <META HTTP-EQUIV="google" CONTENT="notranslate">
    <META HTTP-EQUIV="charset" CONTENT="UTF-8">
    <META HTTP-EQUIV="encoding" CONTENT="UTF-8">
    <META NAME="viewport" CONTENT="width=device-width">
    <LINK REL="icon" TYPE="image/x-icon" HREF="/favicon.ico">
    <LINK REL="shortcut icon" HREF="/favicon.ico?">
    <TITLE>WFM ` + eDir + `</TITLE>
    <STYLE TYPE="text/css"><!--
            A:link {text-decoration: none; color:#0000CE; }
            A:visited {text-decoration: none; color:#0000CE; }
            A:active {text-decoration: none; color:#FF0000; }
            A:hover {text-decoration: none; background-color: #FF8000; color: #FFFFFF; }
            html, body, table { margin:0px; padding:0px; border:none;  }
            td, th { font-family: Tahoma, Arial, Geneva, sans-serif; font-size:13px; margin:0px; padding:2px; border:none; }
            input { border-color:#000000; border-style:solid; font-family: Tahoma, Arial, Geneva, sans-serif; font-size:13px; }
            .thov tr:hover { background-color: #FF8000; color: #FFFFFF; }
            .tbr { border-width: 1px; border-style: solid solid solid solid; border-color: #AAAAAA #555555 #555555 #AAAAAA; }
            .nb { border-style:none; background-color: #EEEEEE; }
    --></STYLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF">
    <FORM ACTION="` + *wfmPfx + `" METHOD="POST" ENCTYPE="multipart/form-data">
    <INPUT TYPE="hidden" NAME="dir" VALUE="` + eDir + `">
    <INPUT TYPE="hidden" NAME="sort" VALUE="` + sort + `">
    `))
}

func footer(w http.ResponseWriter) {
	w.Write([]byte(`
    </FORM></BODY></HTML>
    `))
}

func redirect(w http.ResponseWriter, uUrl string) {
	w.Header().Set("Location", uUrl)
	w.Header().Set("Content-Type", "text/html")
	w.Header().Set("Cache-Control", *cacheCtl)
	w.WriteHeader(302)

	w.Write([]byte(`
    <HTML><BODY>
    <A HREF="` + html.EscapeString(uUrl) + `">Go here...</A>
    </BODY></HTML>
    `))
}

func emit(s string, c int) string {
	o := strings.Builder{}
	for c > 0 {
		o.WriteString(s)
		c--
	}
	return o.String()
}

func upDnDir(uDir, uBn string) string {
	if deniedPfx(uDir) {
		return ""
	}
	o := strings.Builder{}
	o.WriteString("<OPTION VALUE=\"/\">/ - Root</OPTION>\n")
	p := "/"
	i := 0
	for _, n := range strings.Split(uDir, string(os.PathSeparator))[1:] {
		p = p + n + "/"
		opt := ""
		if p == uDir+"/" {
			opt = "DISABLED"
		}
		i++
		o.WriteString("<OPTION " + opt + " VALUE=\"" +
			html.EscapeString(filepath.Clean(p+"/"+uBn)) + "\">" +
			emit("&nbsp;&nbsp;", i) + " L " +
			html.EscapeString(n) + "</OPTION>\n")
	}
	d, err := ioutil.ReadDir(uDir)
	if err != nil {
		return o.String()
	}
	for _, n := range d {
		if !n.IsDir() || strings.HasPrefix(n.Name(), ".") || deniedPfx(uDir+"/"+n.Name()) {
			continue
		}
		o.WriteString("<OPTION VALUE=\"" +
			html.EscapeString(uDir+"/"+n.Name()+"/"+uBn) + "\">" +
			emit("&nbsp;&nbsp;&nbsp;", i) + " L " +
			html.EscapeString(n.Name()) + "</OPTION>\n")
	}
	return o.String()
}