InSite.apl.bak 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #INCLUDE "TOTVS.CH"
  2. #INCLUDE "APWEBEX.CH"
  3. #DEFINE SESSHTTP "GlbSessions"
  4. /*
  5. +----------------------------------------------------------------------------+
  6. ! FICHA TECNICA DO PROGRAMA !
  7. +----------------------------------------------------------------------------+
  8. ! DADOS DO PROGRAMA !
  9. +------------------+---------------------------------------------------------+
  10. !Tipo ! Portal SMS !
  11. +------------------+---------------------------------------------------------+
  12. !Modulo ! Portal SMS - Insite !
  13. +------------------+---------------------------------------------------------+
  14. !Nome ! InSite !
  15. +------------------+---------------------------------------------------------+
  16. !Descricao ! Abre comunicacao com o servidor Protheus e as tabelas !
  17. ! ! que serao utilizadas !
  18. +------------------+---------------------------------------------------------+
  19. !Autor ! Anderson Jose Zelenski !
  20. +------------------+---------------------------------------------------------+
  21. !Data de Criacao ! 19/09/2013 !
  22. +----------------------------------------------------------------------------+
  23. +------------------+---------------------------------------------------------+
  24. !Descricao ! Tratamento para verificar se a sessao do usuario esta ok!
  25. +------------------+---------------------------------------------------------+
  26. !Autor ! Pedro A. de Souza !
  27. +------------------+---------------------------------------------------------+
  28. !Data de Criacao ! 08/02/2021 !
  29. +----------------------------------------------------------------------------+
  30. */
  31. User Function InSite(lCheckSess)
  32. Local cHtml := ""
  33. Local cSrvEmpresa := ""
  34. Local cSrvFilial := ""
  35. Local cWebModulo := "SIGAFIN"
  36. Local lConectar := .T.
  37. local oObjLog := LogSMS():new()
  38. local oObjSess := LogSMS():new()
  39. Local nI := 0
  40. Local aInfo
  41. Local aDadSess
  42. Local lOk
  43. local nQtd := 0
  44. local nSecs
  45. default lCheckSess := .t.
  46. Public cEmpAnt
  47. Public cFilAnt
  48. cHtml:= '<META HTTP-EQUIV="Refresh" CONTENT="0 ; URL=u_PortalLogin.apw">'
  49. // Salvar o log da requisicao
  50. oObjLog:setFileName("\temp\"+Procname(1)+"_"+dtos(date())+".txt")
  51. oObjLog:saveMsg("("+ltrim(str(procline(1)))+") *** Portal ")
  52. if !empty(HTTPHEADIN->REMOTE_ADDR)
  53. oObjLog:saveMsg('HTTPHEADIN REMOTE_ADDR -> '+HTTPHEADIN->REMOTE_ADDR)
  54. endif
  55. if !empty(HTTPHEADIN->REMOTE_PORT)
  56. oObjLog:saveMsg('HTTPHEADIN REMOTE_PORT -> '+cValToChar(HTTPHEADIN->REMOTE_PORT))
  57. endif
  58. if !empty(HTTPCOOKIES->SESSIONID)
  59. oObjLog:saveMsg('HTTPCOOKIES SESSIONID -> '+HTTPCOOKIES->SESSIONID)
  60. endif
  61. if !empty(HttpSession->SESSIONID)
  62. oObjLog:saveMsg('SESSION SESSIONID -> '+HttpSession->SESSIONID)
  63. endif
  64. aInfo := HttpGet->aGets
  65. if valtype(aInfo) = "A"
  66. For nI := 1 to len(aInfo)
  67. oObjLog:saveMsg('GET '+str(nI,3)+' = '+aInfo[nI]+' -> '+&("HTTPGET->"+aInfo[nI]))
  68. Next
  69. endif
  70. aInfo := HttpPost->aPost
  71. if valtype(aInfo) = "A"
  72. For nI := 1 to len(aInfo)
  73. oObjLog:saveMsg('POST '+str(nI,3)+' = '+aInfo[nI]+' -> '+&("HTTPPOST->"+aInfo[nI]))
  74. Next
  75. endif
  76. if !empty(HttpSession->Tipo)
  77. oObjLog:saveMsg('SESSION = Tipo -> '+HttpSession->Tipo)
  78. endif
  79. If !empty(HttpSession->Superv)
  80. oObjLog:saveMsg('SESSION = Superv -> '+HttpSession->Superv)
  81. endif
  82. if !empty(HttpSession->CodVend)
  83. oObjLog:saveMsg('SESSION = CodVend -> '+HttpSession->CodVend)
  84. endif
  85. // Verificar filial logada
  86. if empty(HttpSession->Empresa)
  87. cSrvEmpresa := "01"
  88. else
  89. cSrvEmpresa := HttpSession->Empresa
  90. EndIf
  91. if empty(HttpSession->Filial)
  92. cSrvFilial := "01"
  93. else
  94. cSrvFilial := HttpSession->Filial
  95. endIf
  96. //if (cEmpAnt == cSrvEmpresa) .and. (cFilAnt == cSrvFilial) .and. SELECT("SX2") > 0 // valida se a empresa logada é a mesma passada
  97. if (cEmpAnt == cSrvEmpresa) .and. SELECT("SX2") > 0 // valida se a empresa logada é a mesma passada
  98. lConectar := .F.
  99. endIf
  100. //01.02.2022 - muda a filial corrente sem fazer o reset do ambiente. Leo+Pedro
  101. if !lConectar .and. cFilAnt <> cSrvFilial
  102. Conout("INSITE.apl - Trocando somente filial de "+cFilAnt+" para "+cSrvFilial+"...")
  103. cFilAnt:= cSrvFilial
  104. cNumEmp:= cEmpAnt+cFilAnt
  105. FWSM0Util():setSM0PositionBycFilAnt()
  106. Endif
  107. if lConectar
  108. nSecs:= Seconds()
  109. RpcClearEnv()
  110. //Tabelas a serem abertas
  111. // aSrvTabelas:={"SC1","SCR","SC7","SCE","SA1","SA2","SA6","SAK","SAL","SC8","SE1","SEA","SY1","CTT","CT1","SE4","SZX","SZZ","SZW","ZC1","AFR","ZZZ","CTN","CTS","CT3","ZV0","ZV1","ZV2","ZV3","ZM2","ZM0"}
  112. aSrvTabelas:={"SA1"}
  113. //RPCSetType(3) //Nao come licença
  114. While !LockbyName("INSITE",.F.,.F.)
  115. nQtd++
  116. Sleep(500)
  117. Conout("INSITE.apl - Aguardando lockbyname. Tentativa "+cValtochar(nQtd)+"...")
  118. if nQtd > 30
  119. Conout("INSITE.apl - Excedeu tentativas de lock - não abriu empresa.")
  120. //Final("não abriu empresa.")
  121. return (cHtml)
  122. Endif
  123. Enddo
  124. if !RPCSETENV(cSrvEmpresa, cSrvFilial, , , cWebModulo, , aSrvTabelas)
  125. conout("Falha ao conectar na empresa/filial "+cSrvEmpresa+"/"+cSrvFilial+" - "+DtoC(dDatabase)+" "+time())
  126. return (cHtml)
  127. else
  128. HttpSession->Empresa:= cSrvEmpresa
  129. HttpSession->Filial:= cSrvFilial
  130. cEmpAnt:= HttpSession->Empresa
  131. cFilAnt:= HttpSession->Filial
  132. conout("Logado na empresa/filial "+cSrvEmpresa+"/"+cSrvFilial+" - "+DtoC(dDatabase)+" "+time())
  133. endif
  134. UnLockByName("INSITE",.F.,.F.)
  135. nSecs:= Seconds() - nSecs
  136. Conout("[INSITE] - Tempo de abertura: "+cValtochar(nSecs)+"s")
  137. endIf
  138. oObjLog:saveMsg('SISTEMA = CNUMEMP -> '+CNUMEMP)
  139. oObjLog:saveMsg('SISTEMA = cFilAnt -> '+cFilAnt)
  140. // Verificar se o vendedor da sessao esta correto
  141. if lCheckSess
  142. // if empty(HttpSession->SESSIONID)
  143. // return cHtml
  144. // endif
  145. // if empty(HttpSession->CodVend)
  146. // return cHtml
  147. // endif
  148. // oObjSess:setFileName(getArqSess())
  149. // aInfo := oObjSess:readLog()
  150. // lOk := .f.
  151. // for nI := 1 to len(aInfo)
  152. // aDadSess := StrTokArr(aInfo[nI], ";")
  153. // if len(aDadSess) > 2
  154. // if aDadSess[2] == HttpSession->SESSIONID .and. aDadSess[3] == HttpSession->CodVend
  155. // lOk := .t.
  156. // exit
  157. // endif
  158. // endif
  159. // next
  160. // if !lOk
  161. // return cHtml
  162. // Endif
  163. if !checkSess()
  164. oObjLog:saveMsg('Deu Erro Vai Limpar -> HttpSession->CodVend')
  165. HttpSession->CodVend := nil
  166. // return cHtml
  167. endif
  168. endif
  169. return
  170. /*
  171. +----------------------------------------------------------------------------+
  172. ! FICHA TECNICA DO PROGRAMA !
  173. +----------------------------------------------------------------------------+
  174. ! DADOS DO PROGRAMA !
  175. +------------------+---------------------------------------------------------+
  176. !Tipo ! Portal SMS !
  177. +------------------+---------------------------------------------------------+
  178. !Modulo ! Portal SMS - Insite !
  179. +------------------+---------------------------------------------------------+
  180. !Nome ! getArqSess !
  181. +------------------+---------------------------------------------------------+
  182. !Descricao ! Retorna o nome do arquivo de controle de session do dia !
  183. +------------------+---------------------------------------------------------+
  184. !Autor ! Pedro A. de Souza !
  185. +------------------+---------------------------------------------------------+
  186. !Data de Criacao ! 08/02/2021 !
  187. +----------------------------------------------------------------------------+
  188. */
  189. static function getArqSess()
  190. return "\temp\session_"+dtos(date())+".txt"
  191. /*
  192. +----------------------------------------------------------------------------+
  193. ! FICHA TECNICA DO PROGRAMA !
  194. +----------------------------------------------------------------------------+
  195. ! DADOS DO PROGRAMA !
  196. +------------------+---------------------------------------------------------+
  197. !Tipo ! Portal SMS !
  198. +------------------+---------------------------------------------------------+
  199. !Modulo ! Portal SMS - Insite !
  200. +------------------+---------------------------------------------------------+
  201. !Nome ! svUsSes !
  202. +------------------+---------------------------------------------------------+
  203. !Descricao ! Salva o vendedor logado no arquivo de controle de !
  204. ! ! session do dia !
  205. +------------------+---------------------------------------------------------+
  206. !Autor ! Pedro A. de Souza !
  207. +------------------+---------------------------------------------------------+
  208. !Data de Criacao ! 08/02/2021 !
  209. +----------------------------------------------------------------------------+
  210. */
  211. user function svUsSes(cCodVend)
  212. local oObjSess := LogSMS():new()
  213. local cSession := cValToChar(HttpSession->SESSIONID)
  214. local aSessions := {}
  215. local dData := ctod("")
  216. oObjSess:setFileName(getArqSess())
  217. oObjSess:saveMsg(";"+cSession+";"+cCodVend)
  218. // Tratamento por variáveis globais
  219. GetGlbVars(SESSHTTP, dData, aSessions)
  220. if empty(dData)
  221. aSessions := {}
  222. dData := date()
  223. elseif dData <> date()
  224. ClearGlbValue(SESSHTTP)
  225. aSessions := {}
  226. dData := date()
  227. Endif
  228. if valtype(aSessions) <> "A"
  229. aSessions := {}
  230. endif
  231. aadd(aSessions, {cSession, cCodVend})
  232. PutGlbVars(SESSHTTP, dData, aSessions)
  233. return
  234. /*
  235. +----------------------------------------------------------------------------+
  236. ! FICHA TECNICA DO PROGRAMA !
  237. +----------------------------------------------------------------------------+
  238. ! DADOS DO PROGRAMA !
  239. +------------------+---------------------------------------------------------+
  240. !Tipo ! Portal SMS !
  241. +------------------+---------------------------------------------------------+
  242. !Modulo ! Portal SMS - Insite !
  243. +------------------+---------------------------------------------------------+
  244. !Nome ! checkSess !
  245. +------------------+---------------------------------------------------------+
  246. !Descricao ! Verificar se o vendedor logado no arquivo de controle de!
  247. ! ! session do dia !
  248. +------------------+---------------------------------------------------------+
  249. !Autor ! Pedro A. de Souza !
  250. +------------------+---------------------------------------------------------+
  251. !Data de Criacao ! 08/02/2021 !
  252. +----------------------------------------------------------------------------+
  253. */
  254. static function checkSess()
  255. local lOk := .t.
  256. local aSessions := {}
  257. local dData := ctod("")
  258. local aParm
  259. local cParm1 := cParm2 := ""
  260. if empty(HttpSession->SESSIONID)
  261. lOk := .f.
  262. endif
  263. if empty(HttpSession->CodVend)
  264. lOk := .f.
  265. endif
  266. if lOk
  267. // oObjSess:setFileName(getArqSess())
  268. // aInfo := oObjSess:readLog()
  269. // lOk := .f.
  270. // for nI := 1 to len(aInfo)
  271. // aDadSess := StrTokArr(aInfo[nI], ";")
  272. // if len(aDadSess) > 2
  273. // if aDadSess[2] == HttpSession->SESSIONID .and. aDadSess[3] == HttpSession->CodVend
  274. // lOk := .t.
  275. // exit
  276. // endif
  277. // endif
  278. // next
  279. // Tratamento por variáveis globais
  280. // GetGlbVars(SESSHTTP, dData, aSessions)
  281. // if empty(dData) .or. dData <> date() .or. valtype(aSessions) <> "A"
  282. // lOk := .f.
  283. // Endif
  284. // if lOk
  285. // lOk := !empty(ascan(aSessions, {|x| x[1] == HttpSession->SESSIONID .and. x[2] == HttpSession->CodVend}))
  286. // Endif
  287. lOk := .f.
  288. if !empty(HttpGet->PR)
  289. aParm := STRTOKARR(DECODE64(httpget->PR), "/")
  290. if len(aParm) >= 2
  291. cParm1 := aParm[1]
  292. cParm2 := aParm[2]
  293. if !empty(cParm2) .and. len(cParm1) = 16 .and. substr(cParm1, 11) = ":" .and. substr(cParm1, 14) = ":"
  294. if left(cParm1,8) = dtos(date()) .and. elaptime(right(cParm1,8), time()) <= "00:60:00"
  295. lOk := .t.
  296. endif
  297. endif
  298. endif
  299. endif
  300. endif
  301. return lOk
  302. /*
  303. +----------------------------------------------------------------------------+
  304. ! FICHA TECNICA DO PROGRAMA !
  305. +----------------------------------------------------------------------------+
  306. ! DADOS DO PROGRAMA !
  307. +------------------+---------------------------------------------------------+
  308. !Tipo ! Portal SMS !
  309. +------------------+---------------------------------------------------------+
  310. !Modulo ! Portal SMS - Insite !
  311. +------------------+---------------------------------------------------------+
  312. !Nome ! GetUsrPR !
  313. +------------------+---------------------------------------------------------+
  314. !Descricao ! Retorna o usuario do portal a partir do parametro get !
  315. ! ! PR !
  316. +------------------+---------------------------------------------------------+
  317. !Autor ! Pedro A. de Souza !
  318. +------------------+---------------------------------------------------------+
  319. !Data de Criacao ! 08/02/2021 !
  320. +----------------------------------------------------------------------------+
  321. */
  322. user function GetUsrPR()
  323. local aParm
  324. local cRet := ""
  325. if type("httpget->PR") <> "U"
  326. aParm := STRTOKARR(DECODE64(httpget->PR), "/")
  327. if len(aParm) >= 2
  328. cRet := aParm[2]
  329. endif
  330. Endif
  331. return cRet
  332. /*
  333. +----------------------------------------------------------------------------+
  334. ! FICHA TECNICA DO PROGRAMA !
  335. +----------------------------------------------------------------------------+
  336. ! DADOS DO PROGRAMA !
  337. +------------------+---------------------------------------------------------+
  338. !Tipo ! Portal SMS !
  339. +------------------+---------------------------------------------------------+
  340. !Modulo ! Portal SMS - Insite !
  341. +------------------+---------------------------------------------------------+
  342. !Nome ! setParPR !
  343. +------------------+---------------------------------------------------------+
  344. !Descricao ! Setar o parametro de "session" do usuario vendedor !
  345. ! ! !
  346. +------------------+---------------------------------------------------------+
  347. !Autor ! Pedro A. de Souza !
  348. +------------------+---------------------------------------------------------+
  349. !Data de Criacao ! 08/02/2021 !
  350. +----------------------------------------------------------------------------+
  351. */
  352. user function SetParPR(cVend)
  353. return ESCAPE(ENCODE64(DTOS(DATE())+TIME()+"/"+cVend))