subscription.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/urllib"
  9. "miniflux.app/v2/internal/validator"
  10. )
  11. // SubscriptionForm represents the subscription form.
  12. type SubscriptionForm struct {
  13. CategoryID int64
  14. URL string
  15. UserAgent string
  16. Cookie string
  17. Username string
  18. Password string
  19. ScraperRules string
  20. RewriteRules string
  21. UrlRewriteRules string
  22. BlocklistRules string
  23. KeeplistRules string
  24. BlockFilterEntryRules string
  25. KeepFilterEntryRules string
  26. ProxyURL string
  27. DisableHTTP2 bool
  28. Crawler bool
  29. IgnoreEntryUpdates bool
  30. FetchViaProxy bool
  31. AllowSelfSignedCertificates bool
  32. }
  33. // Validate makes sure the form values locale.are valid.
  34. func (s *SubscriptionForm) Validate() *locale.LocalizedError {
  35. if s.URL == "" || s.CategoryID == 0 {
  36. return locale.NewLocalizedError("error.feed_mandatory_fields")
  37. }
  38. if !urllib.IsAbsoluteURL(s.URL) {
  39. return locale.NewLocalizedError("error.invalid_feed_url")
  40. }
  41. if !validator.IsValidRegex(s.BlocklistRules) {
  42. return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
  43. }
  44. if !validator.IsValidRegex(s.KeeplistRules) {
  45. return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
  46. }
  47. if !validator.IsValidURLRewriteRules(s.UrlRewriteRules) {
  48. return locale.NewLocalizedError("error.feed_invalid_urlrewrite_rule")
  49. }
  50. if err := validator.IsValidRewriteRules(s.RewriteRules); err != nil {
  51. return err
  52. }
  53. if err := validator.IsValidScraperRules(s.ScraperRules); err != nil {
  54. return err
  55. }
  56. if s.ProxyURL != "" && !urllib.IsValidProxyURL(s.ProxyURL) {
  57. return locale.NewLocalizedError("error.invalid_feed_proxy_url")
  58. }
  59. if s.BlockFilterEntryRules != "" {
  60. if err := validator.IsValidFilterRules(s.BlockFilterEntryRules, "block"); err != nil {
  61. return err
  62. }
  63. }
  64. if s.KeepFilterEntryRules != "" {
  65. if err := validator.IsValidFilterRules(s.KeepFilterEntryRules, "keep"); err != nil {
  66. return err
  67. }
  68. }
  69. return nil
  70. }
  71. // NewSubscriptionForm returns a new SubscriptionForm.
  72. func NewSubscriptionForm(r *http.Request) *SubscriptionForm {
  73. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  74. if err != nil {
  75. categoryID = 0
  76. }
  77. return &SubscriptionForm{
  78. URL: r.FormValue("url"),
  79. CategoryID: int64(categoryID),
  80. Crawler: r.FormValue("crawler") == "1",
  81. IgnoreEntryUpdates: r.FormValue("ignore_entry_updates") == "1",
  82. AllowSelfSignedCertificates: r.FormValue("allow_self_signed_certificates") == "1",
  83. FetchViaProxy: r.FormValue("fetch_via_proxy") == "1",
  84. UserAgent: r.FormValue("user_agent"),
  85. Cookie: r.FormValue("cookie"),
  86. Username: r.FormValue("feed_username"),
  87. Password: r.FormValue("feed_password"),
  88. ScraperRules: r.FormValue("scraper_rules"),
  89. RewriteRules: r.FormValue("rewrite_rules"),
  90. UrlRewriteRules: r.FormValue("urlrewrite_rules"),
  91. BlocklistRules: r.FormValue("blocklist_rules"),
  92. KeeplistRules: r.FormValue("keeplist_rules"),
  93. KeepFilterEntryRules: r.FormValue("keep_filter_entry_rules"),
  94. BlockFilterEntryRules: r.FormValue("block_filter_entry_rules"),
  95. DisableHTTP2: r.FormValue("disable_http2") == "1",
  96. ProxyURL: r.FormValue("proxy_url"),
  97. }
  98. }