subscription.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package form // import "miniflux.app/v2/internal/ui/form"
  4. import (
  5. "net/http"
  6. "strconv"
  7. "miniflux.app/v2/internal/locale"
  8. "miniflux.app/v2/internal/validator"
  9. )
  10. // SubscriptionForm represents the subscription form.
  11. type SubscriptionForm struct {
  12. URL string
  13. CategoryID int64
  14. Crawler bool
  15. FetchViaProxy bool
  16. AllowSelfSignedCertificates bool
  17. UserAgent string
  18. Cookie string
  19. Username string
  20. Password string
  21. ScraperRules string
  22. RewriteRules string
  23. UrlRewriteRules string
  24. BlocklistRules string
  25. KeeplistRules string
  26. BlockFilterEntryRules string
  27. KeepFilterEntryRules string
  28. DisableHTTP2 bool
  29. ProxyURL string
  30. }
  31. // Validate makes sure the form values locale.are valid.
  32. func (s *SubscriptionForm) Validate() *locale.LocalizedError {
  33. if s.URL == "" || s.CategoryID == 0 {
  34. return locale.NewLocalizedError("error.feed_mandatory_fields")
  35. }
  36. if !validator.IsValidURL(s.URL) {
  37. return locale.NewLocalizedError("error.invalid_feed_url")
  38. }
  39. if !validator.IsValidRegex(s.BlocklistRules) {
  40. return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
  41. }
  42. if !validator.IsValidRegex(s.KeeplistRules) {
  43. return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
  44. }
  45. if !validator.IsValidRegex(s.UrlRewriteRules) {
  46. return locale.NewLocalizedError("error.feed_invalid_urlrewrite_rule")
  47. }
  48. if s.ProxyURL != "" && !validator.IsValidURL(s.ProxyURL) {
  49. return locale.NewLocalizedError("error.invalid_feed_proxy_url")
  50. }
  51. return nil
  52. }
  53. // NewSubscriptionForm returns a new SubscriptionForm.
  54. func NewSubscriptionForm(r *http.Request) *SubscriptionForm {
  55. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  56. if err != nil {
  57. categoryID = 0
  58. }
  59. return &SubscriptionForm{
  60. URL: r.FormValue("url"),
  61. CategoryID: int64(categoryID),
  62. Crawler: r.FormValue("crawler") == "1",
  63. AllowSelfSignedCertificates: r.FormValue("allow_self_signed_certificates") == "1",
  64. FetchViaProxy: r.FormValue("fetch_via_proxy") == "1",
  65. UserAgent: r.FormValue("user_agent"),
  66. Cookie: r.FormValue("cookie"),
  67. Username: r.FormValue("feed_username"),
  68. Password: r.FormValue("feed_password"),
  69. ScraperRules: r.FormValue("scraper_rules"),
  70. RewriteRules: r.FormValue("rewrite_rules"),
  71. UrlRewriteRules: r.FormValue("urlrewrite_rules"),
  72. BlocklistRules: r.FormValue("blocklist_rules"),
  73. KeeplistRules: r.FormValue("keeplist_rules"),
  74. KeepFilterEntryRules: r.FormValue("keep_filter_entry_rules"),
  75. BlockFilterEntryRules: r.FormValue("block_filter_entry_rules"),
  76. DisableHTTP2: r.FormValue("disable_http2") == "1",
  77. ProxyURL: r.FormValue("proxy_url"),
  78. }
  79. }