1
|
|
|
package requests |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"net/url" |
5
|
|
|
"strconv" |
6
|
|
|
"strings" |
7
|
|
|
"unicode" |
8
|
|
|
|
9
|
|
|
"github.com/NdoleStudio/httpsms/pkg/entities" |
10
|
|
|
|
11
|
|
|
"github.com/nyaruka/phonenumbers" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
type request struct{} |
15
|
|
|
|
16
|
|
|
func (input *request) sanitizeAddresses(value []string) []string { |
17
|
|
|
var result []string |
18
|
|
|
for _, address := range value { |
19
|
|
|
result = append(result, input.sanitizeAddress(address)) |
20
|
|
|
} |
21
|
|
|
return result |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
func (input *request) sanitizeAddress(value string) string { |
25
|
|
|
value = strings.TrimSpace(value) |
26
|
|
|
if !strings.HasPrefix(value, "+") && input.isDigits(value) && len(value) > 9 { |
27
|
|
|
value = "+" + value |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if number, err := phonenumbers.Parse(value, phonenumbers.UNKNOWN_REGION); err == nil { |
31
|
|
|
value = phonenumbers.Format(number, phonenumbers.E164) |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return value |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
func (input *request) sanitizeContact(owner string, contact string) string { |
38
|
|
|
contact = strings.TrimSpace(contact) |
39
|
|
|
|
40
|
|
|
if len(contact) < 8 || !input.isDigits(contact) { |
41
|
|
|
return contact |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
regionPhoneNumber, err := phonenumbers.Parse(owner, phonenumbers.UNKNOWN_REGION) |
45
|
|
|
if err != nil { |
46
|
|
|
return contact |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if number, err := phonenumbers.Parse(contact, phonenumbers.GetRegionCodeForNumber(regionPhoneNumber)); err == nil { |
50
|
|
|
contact = phonenumbers.Format(number, phonenumbers.E164) |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return contact |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// sanitizeBool sanitizes a boolean string |
57
|
|
|
func (input *request) sanitizeBool(value string) string { |
58
|
|
|
value = strings.TrimSpace(value) |
59
|
|
|
if value == "1" || strings.ToLower(value) == "true" { |
60
|
|
|
value = "true" |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if value == "0" || strings.ToLower(value) == "false" { |
64
|
|
|
value = "false" |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return value |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
func (input *request) sanitizeSIM(value string) string { |
71
|
|
|
if value == entities.SIM1.String() || value == entities.SIM2.String() { |
72
|
|
|
return value |
73
|
|
|
} |
74
|
|
|
return entities.SIM1.String() |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
func (input *request) sanitizeURL(value string) string { |
78
|
|
|
value = strings.TrimSpace(value) |
79
|
|
|
website, err := url.Parse(value) |
80
|
|
|
if err != nil { |
81
|
|
|
return value |
82
|
|
|
} |
83
|
|
|
if website.Scheme == "" { |
84
|
|
|
return "https://" + value |
85
|
|
|
} |
86
|
|
|
return value |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
func (input *request) sanitizeStringPointer(value string) *string { |
90
|
|
|
value = strings.TrimSpace(value) |
91
|
|
|
if value == "" { |
92
|
|
|
return nil |
93
|
|
|
} |
94
|
|
|
return &value |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
func (input *request) removeStringDuplicates(values []string) []string { |
98
|
|
|
cache := map[string]struct{}{} |
99
|
|
|
for _, value := range values { |
100
|
|
|
cache[value] = struct{}{} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
var result []string |
104
|
|
|
for key := range cache { |
105
|
|
|
result = append(result, key) |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return result |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
func (input *request) sanitizeMessageID(value string) string { |
112
|
|
|
id := strings.Builder{} |
113
|
|
|
for _, char := range value { |
114
|
|
|
if char == '.' { |
115
|
|
|
return id.String() |
116
|
|
|
} |
117
|
|
|
id.WriteRune(char) |
118
|
|
|
} |
119
|
|
|
return id.String() |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// getLimit gets the take as a string |
123
|
|
|
func (input *request) getBool(value string) bool { |
124
|
|
|
if value == "true" { |
125
|
|
|
return true |
126
|
|
|
} |
127
|
|
|
return false |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// getLimit gets the take as a string |
131
|
|
|
func (input *request) getInt(value string) int { |
132
|
|
|
val, _ := strconv.Atoi(value) |
133
|
|
|
return val |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
func (input *request) isDigits(value string) bool { |
137
|
|
|
for _, c := range value { |
138
|
|
|
if !unicode.IsDigit(c) { |
139
|
|
|
return false |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
return true |
143
|
|
|
} |
144
|
|
|
|