2022-09-02 18:39:19 +02:00
|
|
|
package clean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/list"
|
2022-09-02 18:39:19 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var EmailRegexp = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
// Handle returns the sanitized username with trimmed whitespace and in lowercase.
|
|
|
|
func Handle(s string) string {
|
2023-03-14 16:36:30 +01:00
|
|
|
s, _, _ = strings.Cut(s, "@")
|
2023-03-15 22:41:59 +01:00
|
|
|
|
|
|
|
if d, u, found := strings.Cut(s, "\\"); found && u != "" {
|
|
|
|
s = u
|
|
|
|
} else {
|
|
|
|
s = d
|
|
|
|
}
|
|
|
|
|
2023-03-14 16:36:30 +01:00
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
2022-09-02 18:39:19 +02:00
|
|
|
// Remove unwanted characters.
|
|
|
|
s = strings.Map(func(r rune) rune {
|
2023-03-14 16:36:30 +01:00
|
|
|
if r <= 31 || r == 127 {
|
2022-09-02 18:39:19 +02:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
switch r {
|
2023-03-15 22:41:59 +01:00
|
|
|
case ' ', '"', '\'', '(', ')', '#', '&', '$', ',', '+', '=', '`', '~', '?', '|', '*', '/', '\\', ':', ';', '<', '>', '{', '}':
|
|
|
|
return '.'
|
2022-09-02 18:39:19 +02:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}, s)
|
|
|
|
|
|
|
|
// Empty or too long?
|
|
|
|
if s == "" || reject(s, txt.ClipUserName) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.ToLower(s)
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
// Username returns the sanitized distinguished name (Username) with trimmed whitespace and in lowercase.
|
|
|
|
func Username(s string) string {
|
2023-01-24 06:05:31 +01:00
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
|
|
|
// Remove unwanted characters.
|
|
|
|
s = strings.Map(func(r rune) rune {
|
|
|
|
if r <= 31 || r == 127 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
switch r {
|
2023-03-15 22:41:59 +01:00
|
|
|
case '"', '\'', '(', ')', '#', '&', '$', ',', '+', '=', '`', '~', '?', '|', '*', '/', ':', ';', '<', '>', '{', '}':
|
2023-01-24 06:05:31 +01:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}, s)
|
|
|
|
|
|
|
|
// Empty or too long?
|
|
|
|
if s == "" || reject(s, txt.ClipEmail) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.ToLower(s)
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Email returns the sanitized email with trimmed whitespace and in lowercase.
|
2022-09-02 18:39:19 +02:00
|
|
|
func Email(s string) string {
|
|
|
|
// Empty or too long?
|
|
|
|
if s == "" || reject(s, txt.ClipEmail) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.ToLower(strings.TrimSpace(s))
|
|
|
|
|
|
|
|
if EmailRegexp.MatchString(s) {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Role returns the sanitized role with trimmed whitespace and in lowercase.
|
2022-09-02 18:39:19 +02:00
|
|
|
func Role(s string) string {
|
|
|
|
// Remove unwanted characters.
|
|
|
|
s = strings.Map(func(r rune) rune {
|
|
|
|
if r <= 42 || r == 127 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
switch r {
|
|
|
|
case '`', '~', '?', '|', '*', '\\', '%', '$', '@', ':', ';', '<', '>', '{', '}':
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}, s)
|
|
|
|
|
|
|
|
// Empty or too long?
|
|
|
|
if s == "" || reject(s, txt.ClipRole) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.ToLower(s)
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Attr returns the sanitized attributes.
|
|
|
|
func Attr(s string) string {
|
|
|
|
return list.ParseAttr(s).String()
|
|
|
|
}
|
|
|
|
|
2023-06-19 17:24:02 +02:00
|
|
|
// Password returns the password string with all leading and trailing white space removed.
|
2022-09-02 18:39:19 +02:00
|
|
|
func Password(s string) string {
|
2023-06-19 17:24:02 +02:00
|
|
|
return strings.TrimSpace(s)
|
2022-09-02 18:39:19 +02:00
|
|
|
}
|