2020-08-09 19:33:47 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2020-11-14 10:39:18 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2020-08-31 17:52:26 +00:00
|
|
|
"net/http"
|
2021-01-28 19:07:21 +00:00
|
|
|
"regexp"
|
2020-08-09 19:33:47 +00:00
|
|
|
"strings"
|
2021-03-02 16:36:57 +00:00
|
|
|
"unicode"
|
2020-08-09 19:33:47 +00:00
|
|
|
)
|
|
|
|
|
2021-03-06 09:40:47 +00:00
|
|
|
// TODO: make names match to fields of config file
|
2020-10-25 15:06:51 +00:00
|
|
|
var (
|
2021-03-06 09:40:47 +00:00
|
|
|
SiteName string
|
|
|
|
SiteNavIcon string
|
|
|
|
|
|
|
|
HomePage string
|
|
|
|
UserHypha string
|
|
|
|
HeaderLinksHypha string
|
|
|
|
|
|
|
|
ServerPort string
|
|
|
|
URL string
|
|
|
|
GeminiCertPath string
|
|
|
|
|
|
|
|
WikiDir string
|
|
|
|
ConfigFilePath string
|
2021-04-12 14:26:49 +00:00
|
|
|
|
|
|
|
UseFixedAuth bool
|
|
|
|
FixedCredentialsPath string
|
|
|
|
UseRegistration bool
|
|
|
|
RegistrationCredentialsPath string
|
2021-04-19 16:39:25 +00:00
|
|
|
LimitRegistration int
|
2020-10-25 15:06:51 +00:00
|
|
|
)
|
2020-08-09 19:33:47 +00:00
|
|
|
|
2021-03-02 16:36:57 +00:00
|
|
|
// LettersNumbersOnly keeps letters and numbers only in the given string.
|
|
|
|
func LettersNumbersOnly(s string) string {
|
|
|
|
var (
|
|
|
|
ret strings.Builder
|
|
|
|
usedUnderscore bool
|
|
|
|
)
|
|
|
|
for _, r := range s {
|
|
|
|
if unicode.IsLetter(r) || unicode.IsNumber(r) {
|
|
|
|
ret.WriteRune(r)
|
|
|
|
usedUnderscore = false
|
|
|
|
} else if !usedUnderscore {
|
|
|
|
ret.WriteRune('_')
|
|
|
|
usedUnderscore = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Trim(ret.String(), "_")
|
|
|
|
}
|
|
|
|
|
2020-08-09 19:33:47 +00:00
|
|
|
// ShorterPath is used by handlerList to display shorter path to the files. It simply strips WikiDir.
|
|
|
|
func ShorterPath(path string) string {
|
|
|
|
if strings.HasPrefix(path, WikiDir) {
|
|
|
|
tmp := strings.TrimPrefix(path, WikiDir)
|
|
|
|
if tmp == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return tmp[1:]
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
2020-08-31 17:52:26 +00:00
|
|
|
|
2021-03-05 12:20:51 +00:00
|
|
|
// HTTP404Page writes a 404 error in the status, needed when no content is found on the page.
|
|
|
|
func HTTP404Page(w http.ResponseWriter, page string) {
|
|
|
|
w.Header().Set("Content-Type", "text/html;charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
w.Write([]byte(page))
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:52:26 +00:00
|
|
|
// HTTP200Page wraps some frequently used things for successful 200 responses.
|
|
|
|
func HTTP200Page(w http.ResponseWriter, page string) {
|
|
|
|
w.Header().Set("Content-Type", "text/html;charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(page))
|
|
|
|
}
|
2020-10-03 16:56:56 +00:00
|
|
|
|
2020-11-14 10:39:18 +00:00
|
|
|
func RandomString(n int) (string, error) {
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(bytes), nil
|
|
|
|
}
|
2021-01-10 11:58:02 +00:00
|
|
|
|
|
|
|
// Strip hypha name from all ancestor names, replace _ with spaces, title case
|
|
|
|
func BeautifulName(uglyName string) string {
|
2021-01-11 16:13:41 +00:00
|
|
|
if uglyName == "" {
|
|
|
|
return uglyName
|
|
|
|
}
|
2021-01-14 11:39:54 +00:00
|
|
|
return strings.Title(strings.ReplaceAll(uglyName, "_", " "))
|
2021-01-10 11:58:02 +00:00
|
|
|
}
|
2021-01-28 19:07:21 +00:00
|
|
|
|
|
|
|
// CanonicalName makes sure the `name` is canonical. A name is canonical if it is lowercase and all spaces are replaced with underscores.
|
|
|
|
func CanonicalName(name string) string {
|
2021-04-19 16:39:25 +00:00
|
|
|
return strings.ToLower(
|
|
|
|
strings.ReplaceAll(
|
|
|
|
strings.TrimRight(
|
|
|
|
strings.TrimLeft(name, "_"),
|
|
|
|
"_",
|
|
|
|
), " ", "_"))
|
2021-01-28 19:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HyphaPattern is a pattern which all hyphae must match.
|
|
|
|
var HyphaPattern = regexp.MustCompile(`[^?!:#@><*|"\'&%{}]+`)
|
|
|
|
|
2021-04-19 16:39:25 +00:00
|
|
|
var UsernamePattern = regexp.MustCompile(`[^?!:#@><*|"\'&%{}/]+`)
|
|
|
|
|
2021-01-28 19:07:21 +00:00
|
|
|
// IsCanonicalName checks if the `name` is canonical.
|
|
|
|
func IsCanonicalName(name string) bool {
|
|
|
|
return HyphaPattern.MatchString(name)
|
|
|
|
}
|
2021-04-19 16:39:25 +00:00
|
|
|
|
|
|
|
func IsPossibleUsername(username string) bool {
|
|
|
|
return UsernamePattern.MatchString(strings.TrimSpace(username))
|
|
|
|
}
|