-
Notifications
You must be signed in to change notification settings - Fork 2
/
Route4Me.vbs
339 lines (276 loc) · 9.61 KB
/
Route4Me.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
Const SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS = 13056
CONST WHR_EnableRedirects = 6
Class Route4Me
Private m_fileName
Public Property Get OutputFile
OutputFile = m_fileName
End Property
Public Property Let OutputFile(ByVal value)
m_fileName = value
End Property
Public Sub Write2File(result)
Dim fileName
Dim spFile
If m_fileName = Empty Then
'MsgBox("File not defined")
fileName="file1.txt"
Else
'MsgBox("File Defined")
fileName=OutputFile
End If
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(fileName) Then
Set spFile = fso.OpenTextFile(fileName,2,True)
Else
Set spFile= fso.CreateTextFile(fileName,True)
End If
spFile.WriteLine(result)
spFile.Close
Set fso = nothing
End Sub
Public Function File2Json(jFile)
Dim spFile
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(jFile) Then
Set spFile = fso.OpenTextFile(jFile,1,True)
File2Json = spFile.ReadAll()
File2Json=Trim(File2Json)
Else
WScript.Echo "File " & fileName &" doesn't exists..."
File2Json = ""
End If
End Function
Public Sub HttpGetRequest(url)
Set WshShell = WScript.CreateObject("WScript.Shell")
'Set http = CreateObject("Microsoft.XmlHttp")
Set http = CreateObject("MSXML2.ServerXMLHTTP")
On Error Resume Next
http.open "GET",url,False
http.setOption 2, SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
http.send ""
If Err.Number = 0 Then
Write2File(http.responseText)
Else
WScript.Echo "error " & Err.Number& ":" & Err.Description
End If
Set WshShell = Nothing
Set http = Nothing
End Sub
Public Sub HttpGetRequest2(url,jFile)
Set WshShell = WScript.CreateObject("WScript.Shell")
'Set http = CreateObject("Microsoft.XmlHttp")
Set http = CreateObject("MSXML2.ServerXMLHTTP")
On Error Resume Next
http.open "GET",url,False
http.setOption 2, SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
On Error Resume Next
If jFile="" Then
http.send ""
Else
jText = File2Json(jFile)
http.setRequestHeader "Content-Length", Len(jText)
http.send jText
End If
If Err.Number = 0 Then
Write2File(http.responseText)
Else
WScript.Echo "error " & Err.Number& ":" & Err.Description
End If
Set WshShell = Nothing
Set http = Nothing
End Sub
Public Sub HttpPostRequest(url,jFile)
Dim jText
Set WshShell = WScript.CreateObject("WScript.Shell")
'Set http = CreateObject("Microsoft.XmlHttp")
Set http = CreateObject("MSXML2.ServerXMLHTTP")
http.open "POST", url, False
http.setOption 2, SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
http.setRequestHeader "Content-Type", "application/json"
On Error Resume Next
jText = File2Json(jFile)
http.setRequestHeader "Content-Length", Len(jText)
http.send jText
'http.waitForResponse(20)
If http.Status >= 400 And http.Status <= 599 Then
WScript.Echo "Error Occurred : " & http.status & " - " & http.statusText
End If
If Err.Number = 0 Then
Write2File(http.responseText)
Else
WScript.Echo "error " & Err.Number& ":" & Err.Description
End If
Set WshShell = Nothing
Set http = Nothing
End Sub
Public Sub HttpPostRequest2(url,jFile)
Dim jText
Set WshShell = WScript.CreateObject("WScript.Shell")
'Set http = CreateObject("MSXML2.ServerXMLHTTP")
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
http.open "POST", url, False
http.Option(2) = SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
http.Option(WHR_EnableRedirects) = False
http.setRequestHeader "Content-Type", "application/json"
On Error Resume Next
jText = File2Json(jFile)
http.setRequestHeader "Content-Length", Len(jText)
http.send jText
If http.Status >= 400 And http.Status <= 599 Then
WScript.Echo "Error Occurred : " & http.status & " - " & http.statusText
End If
If Err.Number = 0 Then
Write2File(http.responseText)
Else
WScript.Echo "error " & Err.Number& ":" & Err.Description
End If
Set WshShell = Nothing
Set http = Nothing
End Sub
Public Sub HttpPostFormRequest(url,FormData)
'Uses POST to send form data
'url is the URL (https://www.route4me.com/actions/merge_routes.php)
'FormData are pipe-delimited form data pairs (foo=bar|remove_origin=false)
Set WshShell = WScript.CreateObject("WScript.Shell")
'Set http = CreateObject("Microsoft.XmlHttp")
Set http = CreateObject("MSXML2.ServerXMLHTTP")
http.open "POST", url, False
http.setOption 2, SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
On Error Resume Next
http.setRequestHeader "Content-Length", Len(FormData)
http.send FormData
'http.waitForResponse(20)
If http.Status >= 400 And http.Status <= 599 Then
WScript.Echo "Error Occurred : " & http.status & " - " & http.statusText
End If
If Err.Number = 0 Then
Write2File(http.responseText)
Else
WScript.Echo "error " & Err.Number& ":" & Err.Description
End If
Set WshShell = Nothing
Set http = Nothing
End Sub
Sub Upload(strUploadUrl, strFilePath, strFileField, strDataPairs)
'Uses POST to upload a file and miscellaneous form data
'strUploadUrl is the URL (https://www.route4me.com/actions/upload/upload.php)
'strFilePath is the file to upload
'strFileField is the web page equivalent form field name for the file (strFilename)
'strDataPairs are pipe-delimited form data pairs (foo=bar|strFilename=filename)
Const MULTIPART_BOUNDARY = "---------------------------0123456789012"
Dim ado, rs
Dim lngCount
Dim bytFormData, bytFormStart, bytFormEnd, bytFile
Dim strFormStart, strFormEnd, strDataPair
Dim web
Const adLongVarBinary = 205
'Read the file into a byte array
Set ado = CreateObject("ADODB.Stream")
ado.Type = 1
ado.Open
ado.LoadFromFile strFilePath
bytFile = ado.Read
ado.Close
'Create the multipart form data.
'Define the end of form
strFormEnd = vbCrLf & "--" & MULTIPART_BOUNDARY & "--" & vbCrLf
'First add any ordinary form data pairs
strFormStart = ""
For Each strDataPair In Split(strDataPairs, "|")
strFormStart = strFormStart & "--" & MULTIPART_BOUNDARY & vbCrLf
strFormStart = strFormStart & "Content-Disposition: form-data; "
strFormStart = strFormStart & "name=""" & Split(strDataPair, "=")(0) & """"
strFormStart = strFormStart & vbCrLf & vbCrLf
strFormStart = strFormStart & Split(strDataPair, "=")(1)
strFormStart = strFormStart & vbCrLf
Next
'Now add the header for the uploaded file
strFormStart = strFormStart & "--" & MULTIPART_BOUNDARY & vbCrLf
strFormStart = strFormStart & "Content-Disposition: form-data; "
strFormStart = strFormStart & "name=""" & strFileField & """; "
strFormStart = strFormStart & "filename=""" & Mid(strFilePath, InStrRev(strFilePath, "\") + 1) & """"
strFormStart = strFormStart & vbCrLf
strFormStart = strFormStart & "Content-Type: application/upload" 'bogus, but it works
strFormStart = strFormStart & vbCrLf & vbCrLf
'Create a recordset large enough to hold everything
Set rs = CreateObject("ADODB.Recordset")
rs.Fields.Append "FormData", adLongVarBinary, Len(strFormStart) + LenB(bytFile) + Len(strFormEnd)
rs.Open
rs.AddNew
'Convert form data so far to zero-terminated byte array
For lngCount = 1 To Len(strFormStart)
bytFormStart = bytFormStart & ChrB(Asc(Mid(strFormStart, lngCount, 1)))
Next
rs("FormData").AppendChunk bytFormStart & ChrB(0)
bytFormStart = rs("formData").GetChunk(Len(strFormStart))
rs("FormData") = ""
'Get the end boundary as a zero-terminated byte array
For lngCount = 1 To Len(strFormEnd)
bytFormEnd = bytFormEnd & ChrB(Asc(Mid(strFormEnd, lngCount, 1)))
Next
rs("FormData").AppendChunk bytFormEnd & ChrB(0)
bytFormEnd = rs("formData").GetChunk(Len(strFormEnd))
rs("FormData") = ""
'Now merge it all
rs("FormData").AppendChunk bytFormStart
rs("FormData").AppendChunk bytFile
rs("FormData").AppendChunk bytFormEnd
bytFormData = rs("FormData")
rs.Close
'Upload it
Set web = CreateObject("WinHttp.WinHttpRequest.5.1")
web.Open "POST", strUploadUrl, False
web.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & MULTIPART_BOUNDARY
web.Send bytFormData
If Err.Number = 0 Then
Write2File(web.responseText)
Else
WScript.Echo "error " & Err.Number& ":" & Err.Description
End If
End Sub
Public Sub HttpPutRequest(url,jFile)
Dim jText
Set WshShell = WScript.CreateObject("WScript.Shell")
'Set http = CreateObject("Microsoft.XmlHttp")
Set http = CreateObject("MSXML2.ServerXMLHTTP")
http.open "PUT", url, False
http.setOption 2, SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
http.setRequestHeader "Content-Type", "application/json"
On Error Resume Next
jText = File2Json(jFile)
http.setRequestHeader "Content-Length", Len(jText)
http.send jText
If Err.Number = 0 Then
Write2File(http.responseText)
Else
WScript.Echo "error " & Err.Number& ":" & Err.Description
End If
Set WshShell = Nothing
Set http = Nothing
End Sub
Public Sub HttpDeleteRequest(url,jFile)
Dim jText
Set WshShell = WScript.CreateObject("WScript.Shell")
'Set http = CreateObject("Microsoft.XmlHttp")
Set http = CreateObject("MSXML2.ServerXMLHTTP")
http.open "DELETE", url, False
http.setOption 2, SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
http.setRequestHeader "Content-Type", "application/json"
On Error Resume Next
If jFile="" Then
http.send ""
Else
jText = File2Json(jFile)
http.setRequestHeader "Content-Length", Len(jText)
http.send jText
End If
If Err.Number = 0 Then
Write2File(http.responseText)
Else
WScript.Echo "error " & Err.Number& ":" & Err.Description
End If
Set WshShell = Nothing
Set http = Nothing
End Sub
End Class