2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package form // import "miniflux.app/ui/form"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2017-11-28 00:30:04 -05:00
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/errors"
|
2021-02-07 21:38:45 -05:00
|
|
|
"miniflux.app/validator"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// SubscriptionForm represents the subscription form.
|
2017-11-20 00:10:04 -05:00
|
|
|
type SubscriptionForm struct {
|
2021-02-21 16:42:49 -05:00
|
|
|
URL string
|
|
|
|
CategoryID int64
|
|
|
|
Crawler bool
|
|
|
|
FetchViaProxy bool
|
|
|
|
AllowSelfSignedCertificates bool
|
|
|
|
UserAgent string
|
2021-03-22 23:27:58 -04:00
|
|
|
Cookie string
|
2021-02-21 16:42:49 -05:00
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
ScraperRules string
|
|
|
|
RewriteRules string
|
|
|
|
BlocklistRules string
|
|
|
|
KeeplistRules string
|
2022-07-12 00:12:26 -04:00
|
|
|
UrlRewriteRules string
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// Validate makes sure the form values are valid.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *SubscriptionForm) Validate() error {
|
|
|
|
if s.URL == "" || s.CategoryID == 0 {
|
2018-09-21 21:53:29 -04:00
|
|
|
return errors.NewLocalizedError("error.feed_mandatory_fields")
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2021-02-07 21:38:45 -05:00
|
|
|
if !validator.IsValidURL(s.URL) {
|
|
|
|
return errors.NewLocalizedError("error.invalid_feed_url")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validator.IsValidRegex(s.BlocklistRules) {
|
|
|
|
return errors.NewLocalizedError("error.feed_invalid_blocklist_rule")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validator.IsValidRegex(s.KeeplistRules) {
|
|
|
|
return errors.NewLocalizedError("error.feed_invalid_keeplist_rule")
|
|
|
|
}
|
|
|
|
|
2022-07-12 00:12:26 -04:00
|
|
|
if !validator.IsValidRegex(s.UrlRewriteRules) {
|
|
|
|
return errors.NewLocalizedError("error.feed_invalid_urlrewrite_rule")
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// NewSubscriptionForm returns a new SubscriptionForm.
|
2017-11-20 00:10:04 -05:00
|
|
|
func NewSubscriptionForm(r *http.Request) *SubscriptionForm {
|
|
|
|
categoryID, err := strconv.Atoi(r.FormValue("category_id"))
|
|
|
|
if err != nil {
|
|
|
|
categoryID = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SubscriptionForm{
|
2021-02-21 16:42:49 -05:00
|
|
|
URL: r.FormValue("url"),
|
|
|
|
CategoryID: int64(categoryID),
|
|
|
|
Crawler: r.FormValue("crawler") == "1",
|
|
|
|
AllowSelfSignedCertificates: r.FormValue("allow_self_signed_certificates") == "1",
|
|
|
|
FetchViaProxy: r.FormValue("fetch_via_proxy") == "1",
|
|
|
|
UserAgent: r.FormValue("user_agent"),
|
2021-03-22 23:27:58 -04:00
|
|
|
Cookie: r.FormValue("cookie"),
|
2021-02-21 16:42:49 -05:00
|
|
|
Username: r.FormValue("feed_username"),
|
|
|
|
Password: r.FormValue("feed_password"),
|
|
|
|
ScraperRules: r.FormValue("scraper_rules"),
|
|
|
|
RewriteRules: r.FormValue("rewrite_rules"),
|
|
|
|
BlocklistRules: r.FormValue("blocklist_rules"),
|
|
|
|
KeeplistRules: r.FormValue("keeplist_rules"),
|
2022-07-12 00:12:26 -04:00
|
|
|
UrlRewriteRules: r.FormValue("urlrewrite_rules"),
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
}
|