InSite.apl 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 := "FAT"
  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. */
  125. if !RPCSETENV(cSrvEmpresa, cSrvFilial, , , cWebModulo, , aSrvTabelas)
  126. conout("Falha ao conectar na empresa/filial "+cSrvEmpresa+"/"+cSrvFilial+" - "+DtoC(dDatabase)+" "+time())
  127. return (cHtml)
  128. else
  129. HttpSession->Empresa:= cSrvEmpresa
  130. HttpSession->Filial:= cSrvFilial
  131. cEmpAnt:= HttpSession->Empresa
  132. cFilAnt:= HttpSession->Filial
  133. conout("Logado na empresa/filial "+cSrvEmpresa+"/"+cSrvFilial+" - "+DtoC(dDatabase)+" "+time())
  134. endif
  135. UnLockByName("INSITE",.F.,.F.)
  136. nSecs:= Seconds() - nSecs
  137. Conout("[INSITE] - Tempo de abertura: "+cValtochar(nSecs)+"s")
  138. endIf
  139. oObjLog:saveMsg('SISTEMA = CNUMEMP -> '+CNUMEMP)
  140. oObjLog:saveMsg('SISTEMA = cFilAnt -> '+cFilAnt)
  141. // Verificar se o vendedor da sessao esta correto
  142. if lCheckSess
  143. // if empty(HttpSession->SESSIONID)
  144. // return cHtml
  145. // endif
  146. // if empty(HttpSession->CodVend)
  147. // return cHtml
  148. // endif
  149. // oObjSess:setFileName(getArqSess())
  150. // aInfo := oObjSess:readLog()
  151. // lOk := .f.
  152. // for nI := 1 to len(aInfo)
  153. // aDadSess := StrTokArr(aInfo[nI], ";")
  154. // if len(aDadSess) > 2
  155. // if aDadSess[2] == HttpSession->SESSIONID .and. aDadSess[3] == HttpSession->CodVend
  156. // lOk := .t.
  157. // exit
  158. // endif
  159. // endif
  160. // next
  161. // if !lOk
  162. // return cHtml
  163. // Endif
  164. if !checkSess()
  165. oObjLog:saveMsg('Deu Erro Vai Limpar -> HttpSession->CodVend')
  166. HttpSession->CodVend := nil
  167. // return cHtml
  168. endif
  169. endif
  170. return
  171. /*
  172. +----------------------------------------------------------------------------+
  173. ! FICHA TECNICA DO PROGRAMA !
  174. +----------------------------------------------------------------------------+
  175. ! DADOS DO PROGRAMA !
  176. +------------------+---------------------------------------------------------+
  177. !Tipo ! Portal SMS !
  178. +------------------+---------------------------------------------------------+
  179. !Modulo ! Portal SMS - Insite !
  180. +------------------+---------------------------------------------------------+
  181. !Nome ! getArqSess !
  182. +------------------+---------------------------------------------------------+
  183. !Descricao ! Retorna o nome do arquivo de controle de session do dia !
  184. +------------------+---------------------------------------------------------+
  185. !Autor ! Pedro A. de Souza !
  186. +------------------+---------------------------------------------------------+
  187. !Data de Criacao ! 08/02/2021 !
  188. +----------------------------------------------------------------------------+
  189. */
  190. static function getArqSess()
  191. return "\temp\session_"+dtos(date())+".txt"
  192. /*
  193. +----------------------------------------------------------------------------+
  194. ! FICHA TECNICA DO PROGRAMA !
  195. +----------------------------------------------------------------------------+
  196. ! DADOS DO PROGRAMA !
  197. +------------------+---------------------------------------------------------+
  198. !Tipo ! Portal SMS !
  199. +------------------+---------------------------------------------------------+
  200. !Modulo ! Portal SMS - Insite !
  201. +------------------+---------------------------------------------------------+
  202. !Nome ! svUsSes !
  203. +------------------+---------------------------------------------------------+
  204. !Descricao ! Salva o vendedor logado no arquivo de controle de !
  205. ! ! session do dia !
  206. +------------------+---------------------------------------------------------+
  207. !Autor ! Pedro A. de Souza !
  208. +------------------+---------------------------------------------------------+
  209. !Data de Criacao ! 08/02/2021 !
  210. +----------------------------------------------------------------------------+
  211. */
  212. user function svUsSes(cCodVend)
  213. local oObjSess := LogSMS():new()
  214. local cSession := cValToChar(HttpSession->SESSIONID)
  215. local aSessions := {}
  216. local dData := ctod("")
  217. oObjSess:setFileName(getArqSess())
  218. oObjSess:saveMsg(";"+cSession+";"+cCodVend)
  219. // Tratamento por variáveis globais
  220. GetGlbVars(SESSHTTP, dData, aSessions)
  221. if empty(dData)
  222. aSessions := {}
  223. dData := date()
  224. elseif dData <> date()
  225. ClearGlbValue(SESSHTTP)
  226. aSessions := {}
  227. dData := date()
  228. Endif
  229. if valtype(aSessions) <> "A"
  230. aSessions := {}
  231. endif
  232. aadd(aSessions, {cSession, cCodVend})
  233. PutGlbVars(SESSHTTP, dData, aSessions)
  234. return
  235. /*
  236. +----------------------------------------------------------------------------+
  237. ! FICHA TECNICA DO PROGRAMA !
  238. +----------------------------------------------------------------------------+
  239. ! DADOS DO PROGRAMA !
  240. +------------------+---------------------------------------------------------+
  241. !Tipo ! Portal SMS !
  242. +------------------+---------------------------------------------------------+
  243. !Modulo ! Portal SMS - Insite !
  244. +------------------+---------------------------------------------------------+
  245. !Nome ! checkSess !
  246. +------------------+---------------------------------------------------------+
  247. !Descricao ! Verificar se o vendedor logado no arquivo de controle de!
  248. ! ! session do dia !
  249. +------------------+---------------------------------------------------------+
  250. !Autor ! Pedro A. de Souza !
  251. +------------------+---------------------------------------------------------+
  252. !Data de Criacao ! 08/02/2021 !
  253. +----------------------------------------------------------------------------+
  254. */
  255. static function checkSess()
  256. local lOk := .t.
  257. local aSessions := {}
  258. local dData := ctod("")
  259. local aParm
  260. local cParm1 := cParm2 := ""
  261. if empty(HttpSession->SESSIONID)
  262. lOk := .f.
  263. endif
  264. if empty(HttpSession->CodVend)
  265. lOk := .f.
  266. endif
  267. if lOk
  268. // oObjSess:setFileName(getArqSess())
  269. // aInfo := oObjSess:readLog()
  270. // lOk := .f.
  271. // for nI := 1 to len(aInfo)
  272. // aDadSess := StrTokArr(aInfo[nI], ";")
  273. // if len(aDadSess) > 2
  274. // if aDadSess[2] == HttpSession->SESSIONID .and. aDadSess[3] == HttpSession->CodVend
  275. // lOk := .t.
  276. // exit
  277. // endif
  278. // endif
  279. // next
  280. // Tratamento por variáveis globais
  281. // GetGlbVars(SESSHTTP, dData, aSessions)
  282. // if empty(dData) .or. dData <> date() .or. valtype(aSessions) <> "A"
  283. // lOk := .f.
  284. // Endif
  285. // if lOk
  286. // lOk := !empty(ascan(aSessions, {|x| x[1] == HttpSession->SESSIONID .and. x[2] == HttpSession->CodVend}))
  287. // Endif
  288. lOk := .f.
  289. if !empty(HttpGet->PR)
  290. aParm := STRTOKARR(DECODE64(httpget->PR), "/")
  291. if len(aParm) >= 2
  292. cParm1 := aParm[1]
  293. cParm2 := aParm[2]
  294. if !empty(cParm2) .and. len(cParm1) = 16 .and. substr(cParm1, 11) = ":" .and. substr(cParm1, 14) = ":"
  295. if left(cParm1,8) = dtos(date()) .and. elaptime(right(cParm1,8), time()) <= "00:60:00"
  296. lOk := .t.
  297. endif
  298. endif
  299. endif
  300. endif
  301. endif
  302. return lOk
  303. /*
  304. +----------------------------------------------------------------------------+
  305. ! FICHA TECNICA DO PROGRAMA !
  306. +----------------------------------------------------------------------------+
  307. ! DADOS DO PROGRAMA !
  308. +------------------+---------------------------------------------------------+
  309. !Tipo ! Portal SMS !
  310. +------------------+---------------------------------------------------------+
  311. !Modulo ! Portal SMS - Insite !
  312. +------------------+---------------------------------------------------------+
  313. !Nome ! GetUsrPR !
  314. +------------------+---------------------------------------------------------+
  315. !Descricao ! Retorna o usuario do portal a partir do parametro get !
  316. ! ! PR !
  317. +------------------+---------------------------------------------------------+
  318. !Autor ! Pedro A. de Souza !
  319. +------------------+---------------------------------------------------------+
  320. !Data de Criacao ! 08/02/2021 !
  321. +----------------------------------------------------------------------------+
  322. */
  323. user function GetUsrPR()
  324. local aParm
  325. local cRet := ""
  326. if type("httpget->PR") <> "U"
  327. aParm := STRTOKARR(DECODE64(httpget->PR), "/")
  328. if len(aParm) >= 2
  329. cRet := aParm[2]
  330. endif
  331. Endif
  332. return cRet
  333. /*
  334. +----------------------------------------------------------------------------+
  335. ! FICHA TECNICA DO PROGRAMA !
  336. +----------------------------------------------------------------------------+
  337. ! DADOS DO PROGRAMA !
  338. +------------------+---------------------------------------------------------+
  339. !Tipo ! Portal SMS !
  340. +------------------+---------------------------------------------------------+
  341. !Modulo ! Portal SMS - Insite !
  342. +------------------+---------------------------------------------------------+
  343. !Nome ! setParPR !
  344. +------------------+---------------------------------------------------------+
  345. !Descricao ! Setar o parametro de "session" do usuario vendedor !
  346. ! ! !
  347. +------------------+---------------------------------------------------------+
  348. !Autor ! Pedro A. de Souza !
  349. +------------------+---------------------------------------------------------+
  350. !Data de Criacao ! 08/02/2021 !
  351. +----------------------------------------------------------------------------+
  352. */
  353. user function SetParPR(cVend)
  354. return ESCAPE(ENCODE64(DTOS(DATE())+TIME()+"/"+cVend))