mirror of
https://github.com/knadh/listmonk.git
synced 2024-11-10 17:13:04 +08:00
Add regexp template tag validation
This commit is contained in:
parent
a7614ebfe8
commit
8a952c137b
2 changed files with 8 additions and 4 deletions
|
@ -208,7 +208,7 @@ func (s SubscriberAttribs) Scan(src interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CompileTemplate compiles a campaign body template into its base
|
// CompileTemplate compiles a campaign body template into its base
|
||||||
// template and sets the resultant template to Campaign.Tpl
|
// template and sets the resultant template to Campaign.Tpl.
|
||||||
func (c *Campaign) CompileTemplate(f template.FuncMap) error {
|
func (c *Campaign) CompileTemplate(f template.FuncMap) error {
|
||||||
// Compile the base template.
|
// Compile the base template.
|
||||||
t := regexpLinkTag.ReplaceAllString(c.TemplateBody, regexpLinkTagReplace)
|
t := regexpLinkTag.ReplaceAllString(c.TemplateBody, regexpLinkTagReplace)
|
||||||
|
|
10
templates.go
10
templates.go
|
@ -5,8 +5,8 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/asaskevich/govalidator"
|
"github.com/asaskevich/govalidator"
|
||||||
"github.com/knadh/listmonk/models"
|
"github.com/knadh/listmonk/models"
|
||||||
|
@ -32,6 +32,10 @@ type dummyMessage struct {
|
||||||
UnsubscribeURL string
|
UnsubscribeURL string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
regexpTplTag = regexp.MustCompile(`{{(\s+)?template\s+?"content"(\s+)?\.(\s+)?}}`)
|
||||||
|
)
|
||||||
|
|
||||||
// handleGetTemplates handles retrieval of templates.
|
// handleGetTemplates handles retrieval of templates.
|
||||||
func handleGetTemplates(c echo.Context) error {
|
func handleGetTemplates(c echo.Context) error {
|
||||||
var (
|
var (
|
||||||
|
@ -76,7 +80,7 @@ func handlePreviewTemplate(c echo.Context) error {
|
||||||
)
|
)
|
||||||
|
|
||||||
if body != "" {
|
if body != "" {
|
||||||
if strings.Count(body, tplTag) != 1 {
|
if !regexpTplTag.MatchString(body) {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest,
|
return echo.NewHTTPError(http.StatusBadRequest,
|
||||||
fmt.Sprintf("Template body should contain the %s placeholder exactly once", tplTag))
|
fmt.Sprintf("Template body should contain the %s placeholder exactly once", tplTag))
|
||||||
}
|
}
|
||||||
|
@ -243,7 +247,7 @@ func validateTemplate(o models.Template) error {
|
||||||
return errors.New("invalid length for `name`")
|
return errors.New("invalid length for `name`")
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.Count(o.Body, tplTag) != 1 {
|
if !regexpTplTag.MatchString(o.Body) {
|
||||||
return fmt.Errorf("template body should contain the %s placeholder exactly once", tplTag)
|
return fmt.Errorf("template body should contain the %s placeholder exactly once", tplTag)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue