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