subscription.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. URL string
  14. CategoryID int64
  15. Crawler bool
  16. IgnoreEntryUpdates bool
  17. FetchViaProxy bool
  18. AllowSelfSignedCertificates bool
  19. UserAgent string
  20. Cookie string
  21. Username string
  22. Password string
  23. ScraperRules string
  24. RewriteRules string
  25. UrlRewriteRules string
  26. BlocklistRules string
  27. KeeplistRules string
  28. BlockFilterEntryRules string
  29. KeepFilterEntryRules string
  30. DisableHTTP2 bool
  31. ProxyURL string
  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.IsValidRegex(s.UrlRewriteRules) {
  48. return locale.NewLocalizedError("error.feed_invalid_urlrewrite_rule")
  49. }
  50. if s.ProxyURL != "" && !urllib.IsAbsoluteURL(s.ProxyURL) {
  51. return locale.NewLocalizedError("error.invalid_feed_proxy_url")
  52. }
  53. return nil
  54. }
  55. // NewSubscriptionForm returns a new SubscriptionForm.
  56. func NewSubscriptionForm(r *http.Request) *SubscriptionForm {
  57. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  58. if err != nil {
  59. categoryID = 0
  60. }
  61. return &SubscriptionForm{
  62. URL: r.FormValue("url"),
  63. CategoryID: int64(categoryID),
  64. Crawler: r.FormValue("crawler") == "1",
  65. IgnoreEntryUpdates: r.FormValue("ignore_entry_updates") == "1",
  66. AllowSelfSignedCertificates: r.FormValue("allow_self_signed_certificates") == "1",
  67. FetchViaProxy: r.FormValue("fetch_via_proxy") == "1",
  68. UserAgent: r.FormValue("user_agent"),
  69. Cookie: r.FormValue("cookie"),
  70. Username: r.FormValue("feed_username"),
  71. Password: r.FormValue("feed_password"),
  72. ScraperRules: r.FormValue("scraper_rules"),
  73. RewriteRules: r.FormValue("rewrite_rules"),
  74. UrlRewriteRules: r.FormValue("urlrewrite_rules"),
  75. BlocklistRules: r.FormValue("blocklist_rules"),
  76. KeeplistRules: r.FormValue("keeplist_rules"),
  77. KeepFilterEntryRules: r.FormValue("keep_filter_entry_rules"),
  78. BlockFilterEntryRules: r.FormValue("block_filter_entry_rules"),
  79. DisableHTTP2: r.FormValue("disable_http2") == "1",
  80. ProxyURL: r.FormValue("proxy_url"),
  81. }
  82. }