CACIC_Comm.pas
13.5 KB
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
unit CACIC_Comm;
interface
uses IdTCPConnection,
IdTCPClient,
IdHTTP,
IdFTP,
IdFTPCommon,
IdBaseComponent,
IdComponent,
Classes,
StrUtils,
WinSock,
SysUtils,
CACIC_Library,
CACIC_WMI;
function Comm(pStrFullURL, pStrFieldsAndValuesToRequest, pStrLocalFolderName : String; pStrActionMessage : String = '') : String;
function FTPdown(pStrHost, pStrUser, pStrPass, pStrSourcePath, pStrFileNameAtSource, pStrTargetPath, pStrFileNameAtTarget, pStrTransferType, pStrLocalFolderName : String; pIntPort : integer) : String;
var objCacicCOMM : TCACIC;
strGerColsInfFileName,
strChkSisInfFileName : String;
implementation
uses DateUtils;
function Comm(pStrFullURL, pStrFieldsAndValuesToRequest, pStrLocalFolderName : String; pStrActionMessage : String = '') : String;
var tStringStrResponseCS : TStringStream;
tstrRequest : TStrings;
idHTTP1 : TIdHTTP;
intLoopComm : integer;
strWin32_ComputerSystem,
strWin32_NetworkAdapterConfiguration,
strWin32_OperatingSystem,
strWin32_SoftwareFeature,
strTeDebugging : String;
Begin
Try
tStringStrResponseCS := TStringStream.Create('');
idHTTP1 := TIdHTTP.Create(nil);
objCacicCOMM := TCACIC.Create;
objCacicCOMM.setBoolCipher(not objCacicCOMM.isInDebugMode);
objCacicCOMM.setLocalFolderName(pStrLocalFolderName);
strGerColsInfFileName := objCacicCOMM.getLocalFolderName + 'GerCols.inf';
strChkSisInfFileName := objCacicCOMM.getWinDir + 'chksis.inf';
objCacicCOMM.setMainProgramName(objCacicCOMM.getValueFromFile('Configs','MainProgramName', strChkSisInfFileName));
tstrRequest := objCacicCOMM.explode(pStrFieldsAndValuesToRequest,',');
strWin32_OperatingSystem := fetchWmiValues('Win32_OperatingSystem' ,objCacicCOMM.getLocalFolderName);
strWin32_ComputerSystem := fetchWmiValues('Win32_ComputerSystem' ,objCacicCOMM.getLocalFolderName);
strWin32_NetworkAdapterConfiguration := fetchWmiValues('Win32_NetworkAdapterConfiguration',objCacicCOMM.getLocalFolderName);
// if (not (pos('get/test', pStrFullURL) > 0)) and (not (pos('get/config', pStrFullURL) > 0)) then
// begin
// strWin32_SoftwareFeature := fetchWmiValues('Win32_SoftwareFeature' ,objCacicCOMM.getLocalFolderName);
// tstrRequest.Values['SoftwareFeature']:= objCacicCOMM.replaceInvalidHTTPChars(objCacicCOMM.enCrypt(strWin32_SoftwareFeature));
// end;
objCacicCOMM.writeDebugLog('Comm: Povoando lista com valores padrão para cabeçalho de comunicação');
objCacicCOMM.writeDebugLog('Comm: pStrActionMessage: "' + pStrActionMessage + '"');
With tstrRequest do
Begin
Values['OperatingSystem' ] := objCacicCOMM.replaceInvalidHTTPChars(objCacicCOMM.enCrypt(strWin32_OperatingSystem ));
Values['ComputerSystem' ] := objCacicCOMM.replaceInvalidHTTPChars(objCacicCOMM.enCrypt(strWin32_ComputerSystem ));
Values['cs_cipher' ] := '1';
Values['cs_debug' ] := '0';
Values['cs_compress' ] := '0';
Values['HTTP_USER_AGENT' ] := objCacicCOMM.replaceInvalidHTTPChars(objCacicCOMM.enCrypt('AGENTE_CACIC',true,true ));
Values['ModuleFolderName' ] := objCacicCOMM.replaceInvalidHTTPChars(objCacicCOMM.enCrypt(ExtractFilePath(ParamStr(0)) ));
Values['ModuleProgramName' ] := objCacicCOMM.replaceInvalidHTTPChars(objCacicCOMM.enCrypt(ExtractFileName(ParamStr(0)) ));
Values['NetworkAdapterConfiguration'] := objCacicCOMM.replaceInvalidHTTPChars(objCacicCOMM.enCrypt(strWin32_NetworkAdapterConfiguration ));
Values['PHP_AUTH_PW' ] := objCacicCOMM.replaceInvalidHTTPChars(objCacicCOMM.enCrypt('PW_CACIC',true,true ));
Values['PHP_AUTH_USER' ] := objCacicCOMM.replaceInvalidHTTPChars(objCacicCOMM.enCrypt('USER_CACIC',true,true ));
Values['te_so' ] := objCacicCOMM.replaceInvalidHTTPChars(objCacicCOMM.getWindowsStrId() );
Values['te_versao_cacic' ] := objCacicCOMM.replaceInvalidHTTPChars(objCacicCOMM.getVersionInfo(objCacicCOMM.getLocalFolderName + objCacicCOMM.getMainProgramName));
Values['te_versao_gercols' ] := objCacicCOMM.replaceInvalidHTTPChars(objCacicCOMM.getVersionInfo(objCacicCOMM.getLocalFolderName + 'Modules\gercols.exe' ));
End;
if objCacicCOMM.isInDebugMode then
Begin
tstrRequest.Values['ComputerSystem' ] := objCacicCOMM.replaceInvalidHTTPChars(strWin32_ComputerSystem);
tstrRequest.Values['cs_cipher' ] := '0';
tstrRequest.Values['cs_debug' ] := '1';
tstrRequest.Values['NetworkAdapterConfiguration'] := objCacicCOMM.replaceInvalidHTTPChars(strWin32_NetworkAdapterConfiguration);
tstrRequest.Values['OperatingSystem' ] := objCacicCOMM.replaceInvalidHTTPChars(strWin32_OperatingSystem);
objCacicCOMM.writeDebugLog('Comm: ' + DupeString('*',100));
objCacicCOMM.writeDebugLog('Comm: Valores POST para contato com "' + pStrFullURL + '":');
for intLoopComm := 0 to tstrRequest.Count -1 do
objCacicCOMM.writeDebugLog('Comm: tstrRequest[' + intToStr(intLoopComm) + '] = "' + tstrRequest[intLoopComm] + '"');
objCacicCOMM.writeDebugLog('Comm: ' + DupeString('*',100));
End;
objCacicCOMM.writeDebugLog('Comm: ***** Iniciando Comunicação *****');
objCacicCOMM.writeDebugLog('Comm: Requisitando Endereço: ' + pStrFullURL);
Try
idHTTP1.AllowCookies := true;
idHTTP1.AuthRetries := 1;
idHTTP1.HandleRedirects := false;
idHTTP1.ProxyParams.BasicAuthentication := false;
idHTTP1.ProxyParams.ProxyPort := 0;
idHTTP1.ReadTimeout := 0;
idHTTP1.RedirectMaximum := 15;
idHTTP1.Request.Accept := 'text/html, */*';
idHTTP1.Request.BasicAuthentication := true;
idHTTP1.Request.ContentLength := -1;
idHTTP1.Request.ContentRangeStart := 0;
idHTTP1.Request.ContentRangeEnd := 0;
idHTTP1.Request.ContentType := 'text/html';
idHTTP1.Tag := 0;
// ATENÇÃO: Substituo os sinais de "+" acima por [[MAIS]] devido a problemas encontrados no envio POST (vide HTMLentities)
Result := '0';
Try
IdHTTP1.Post(pStrFullURL, tstrRequest, tStringStrResponseCS);
Except
on E : Exception do
Begin
objCacicCOMM.writeExceptionLog(E.Message,E.ClassName,'ERRO! Comunicação impossível com o endereço ' + pStrFullURL);
End;
End;
objCacicCOMM.writeDebugLog('Comm: Retorno -> ' + tStringStrResponseCS.DataString);
idHTTP1.Disconnect;
Except
on E : Exception do
Begin
objCacicCOMM.writeExceptionLog(E.Message,E.ClassName,'ERRO! Comunicação impossível com o endereço ' + pStrFullURL);
objCacicCOMM.writeDailyLog('Comm: ERRO! Comunicação impossível com o endereço ' + pStrFullURL);
End;
end;
objCacicCOMM.writeDebugLog(tStringStrResponseCS.DataString); //Adicionada 10/07
Try
if (objCacicCOMM.getValueFromTags('Comm_Status', tStringStrResponseCS.DataString, '<>') <> 'OK') Then
Begin
objCacicCOMM.writeDailyLog('Comm: PROBLEMAS DURANTE A COMUNICAÇÃO:');
objCacicCOMM.writeDailyLog('Comm: Endereço: ' + pStrFullURL);
objCacicCOMM.writeDailyLog('Comm: Mensagem: ' + tStringStrResponseCS.DataString);
end
Else
Begin
Result := objCacicCOMM.replacePseudoTagsWithCorrectChars(tStringStrResponseCS.DataString);
strTeDebugging := objCacicCOMM.getValueFromTags('TeDebugging', Result, '<>');
objCacicCOMM.deleteFileOrFolder(objCacicCOMM.getLocalFolderName + 'Temp\Debugging');
if (strTeDebugging <> '') then
Begin
ForceDirectories(objCacicCOMM.getLocalFolderName + 'Temp\Debugging');
objCacicCOMM.setValueToFile('Configs','TeDebugging', strTeDebugging, objCacicCOMM.getLocalFolderName + 'Temp\Debugging\Debugging.conf');
objCacicCOMM.writeDebugLog('Comm: ' + DupeString('*',100));
objCacicCOMM.writeDebugLog('Comm: Pasta "' + objCacicCOMM.getLocalFolderName + 'Temp\Debugging' + '" Criada!!!');
objCacicCOMM.writeDebugLog('Comm: TeDebugging => "' + strTeDebugging + '"');
objCacicCOMM.writeDebugLog('Comm: ' + DupeString('*',100));
End;
End;
Except
on E : Exception do
Begin
objCacicCOMM.writeExceptionLog(E.Message,E.ClassName,'Comm: PROBLEMAS DURANTE A COMUNICAÇÃO');
objCacicCOMM.writeDailyLog('Comm: PROBLEMAS DURANTE A COMUNICAÇÃO:');
objCacicCOMM.writeDailyLog('Comm: Endereço: ' + pStrFullURL);
objCacicCOMM.writeDailyLog('Comm: Mensagem: ' + tStringStrResponseCS.DataString);
End;
End;
Finally
idHTTP1.Free;
tstrRequest.Free;
tStringStrResponseCS.Free;
objCacicCOMM.Free;
End;
end;
function FTPdown(pStrHost, pStrUser, pStrPass, pStrSourcePath, pStrFileNameAtSource, pStrTargetPath, pStrFileNameAtTarget, pStrTransferType, pStrLocalFolderName : String; pIntPort : integer) : String;
var IdFTP1 : TIdFTP;
begin
Try
IdFTP1 := TIdFTP.Create(IdFTP1);
objCacicCOMM := TCACIC.Create;
objCacicCOMM.setLocalFolderName(pStrLocalFolderName);
strGerColsInfFileName := objCacicCOMM.getLocalFolderName + 'gercols.inf';
objCacicCOMM.writeDebugLog('FTPdown: Objeto FTP instanciado!');
IdFTP1.Host := pStrHost;
IdFTP1.Username := pStrUser;
IdFTP1.Password := pStrPass;
IdFTP1.Port := pIntPort;
IdFTP1.Passive := true;
if (pStrTransferType = 'ASC') then
IdFTP1.TransferType := ftASCII
else
IdFTP1.TransferType := ftBinary;
objCacicCOMM.writeDebugLog('FTPdown: Iniciando FTP de ' + pStrSourcePath + '/' + pStrFileNameAtSource + ' para '+StringReplace(pStrTargetPath + '\' + pStrFileNameAtTarget,'\\','\',[rfReplaceAll]));
objCacicCOMM.writeDebugLog('FTPdown: Host........ =' + IdFTP1.Host);
objCacicCOMM.writeDebugLog('FTPdown: UserName.... =' + IdFTP1.Username);
objCacicCOMM.writeDebugLog('FTPdown: Port........ =' + inttostr(IdFTP1.Port));
objCacicCOMM.writeDebugLog('FTPdown: Transfer Type=' + pStrTransferType);
objCacicCOMM.writeDebugLog('FTPdown: Pasta Origem =' + pStrSourcePath);
Try
if IdFTP1.Connected = true then
begin
objCacicCOMM.writeDebugLog('FTPdown: Objeto FTP já conectado!!! DESCONECTANDO!');
IdFTP1.Disconnect;
end;
objCacicCOMM.writeDebugLog('FTPdown: Conectando objeto FTP.');
IdFTP1.Connect;
objCacicCOMM.writeDebugLog('FTPdown: Fazendo ChangeDir para "' + pStrSourcePath + '"');
IdFTP1.ChangeDir(pStrSourcePath);
objCacicCOMM.writeDebugLog('FTPdown: ChangeDir OK!');
Try
objCacicCOMM.writeDebugLog('FTPdown: Baixando "' + pStrSourcePath + '/' + pStrFileNameAtSource+'"');
IdFTP1.Get(pStrFileNameAtSource, StringReplace(pStrTargetPath + '\' + pStrFileNameAtTarget,'\\','\',[rfReplaceAll]), True);
objCacicCOMM.writeDebugLog('FTPdown: Download normal!');
Finally
result := 'Download de "' + pStrFileNameAtSource + '" Efetuado com Sucesso!';
objCacicCOMM.writeDebugLog('FTPdown: Size de "'+pStrTargetPath + '\' + pStrFileNameAtTarget +'" Após em Finally => ' + objCacicCOMM.getFileSize(pStrTargetPath + '\' + pStrFileNameAtTarget,true));
End;
Except
on E : Exception do
Begin
objCacicCOMM.writeExceptionLog(E.Message,E.ClassName,'FTP: Erro Baixando "'+pStrSourcePath + '/' + pStrFileNameAtSource +'" para "'+pStrTargetPath + '\' + pStrFileNameAtTarget);
objCacicCOMM.writeDebugLog('FTPdown: Erro Baixando "'+pStrSourcePath + '/' + pStrFileNameAtSource +'" para "'+pStrTargetPath + '\' + pStrFileNameAtTarget);
result := 'Download de "' + pStrFileNameAtSource + '" Não Efetuado!';
End;
end;
Finally
objCacicCOMM.writeDebugLog('FTPdown: Desconectando objeto FTP');
IdFTP1.Disconnect;
objCacicCOMM.writeDebugLog('FTPdown: Liberando objeto FTP');
IdFTP1.Free;
objCacicCOMM.writeDebugLog('FTPdown: Liberando objCacicCOMM');
objCacicCOMM.Free;
End;
end;
end.