subscription.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. IgnoreEntryUpdates bool
  16. FetchViaProxy bool
  17. AllowSelfSignedCertificates bool
  18. UserAgent string
  19. Cookie string
  20. Username string
  21. Password string
  22. ScraperRules string
  23. RewriteRules string
  24. UrlRewriteRules string
  25. BlocklistRules string
  26. KeeplistRules string
  27. BlockFilterEntryRules string
  28. KeepFilterEntryRules string
  29. DisableHTTP2 bool
  30. ProxyURL string
  31. }
  32. // Validate makes sure the form values locale.are valid.
  33. func (s *SubscriptionForm) Validate() *locale.LocalizedError {
  34. if s.URL == "" || s.CategoryID == 0 {
  35. return locale.NewLocalizedError("error.feed_mandatory_fields")
  36. }
  37. if !validator.IsValidURL(s.URL) {
  38. return locale.NewLocalizedError("error.invalid_feed_url")
  39. }
  40. if !validator.IsValidRegex(s.BlocklistRules) {
  41. return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
  42. }
  43. if !validator.IsValidRegex(s.KeeplistRules) {
  44. return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
  45. }
  46. if !validator.IsValidRegex(s.UrlRewriteRules) {
  47. return locale.NewLocalizedError("error.feed_invalid_urlrewrite_rule")
  48. }
  49. if s.ProxyURL != "" && !validator.IsValidURL(s.ProxyURL) {
  50. return locale.NewLocalizedError("error.invalid_feed_proxy_url")
  51. }
  52. return nil
  53. }
  54. // NewSubscriptionForm returns a new SubscriptionForm.
  55. func NewSubscriptionForm(r *http.Request) *SubscriptionForm {
  56. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  57. if err != nil {
  58. categoryID = 0
  59. }
  60. return &SubscriptionForm{
  61. URL: r.FormValue("url"),
  62. CategoryID: int64(categoryID),
  63. Crawler: r.FormValue("crawler") == "1",
  64. IgnoreEntryUpdates: r.FormValue("ignore_entry_updates") == "1",
  65. AllowSelfSignedCertificates: r.FormValue("allow_self_signed_certificates") == "1",
  66. FetchViaProxy: r.FormValue("fetch_via_proxy") == "1",
  67. UserAgent: r.FormValue("user_agent"),
  68. Cookie: r.FormValue("cookie"),
  69. Username: r.FormValue("feed_username"),
  70. Password: r.FormValue("feed_password"),
  71. ScraperRules: r.FormValue("scraper_rules"),
  72. RewriteRules: r.FormValue("rewrite_rules"),
  73. UrlRewriteRules: r.FormValue("urlrewrite_rules"),
  74. BlocklistRules: r.FormValue("blocklist_rules"),
  75. KeeplistRules: r.FormValue("keeplist_rules"),
  76. KeepFilterEntryRules: r.FormValue("keep_filter_entry_rules"),
  77. BlockFilterEntryRules: r.FormValue("block_filter_entry_rules"),
  78. DisableHTTP2: r.FormValue("disable_http2") == "1",
  79. ProxyURL: r.FormValue("proxy_url"),
  80. }
  81. }