* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* nastavi cesty k westwindovym adresarum, vytvori objekt oHTTP a loSerializer
* vytvori globalni promenne locker, token, gldebug_print (ta ovlivni debugove vypisy komunikace)
*
* procedura DOTAZ posila http request, pokud je potreba autentizace, sama zavola proceduru AHOJ, ktera obdrzi keyhole pro vypocet tokenu
*
* funkce JSON_OK vrati .T., pokud "retval" obsahuje "0" a "OK"
* 
* funkce STRIP_WHITESPACES da pred carku a "}" se da tabulator, ktery se v dalsim parsovani uziva jako ukoncovac itemu
*
* funkce NAJDI_ITEM (radek, item) vraci z radku hodnotu za "<item>":
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
PARAMETERS p_test
CLEAR 

IF m.p_test
	IF VARTYPE(gcini_path) # "C"
	    PUBLIC gcini_path
	ENDIF
	gcini_path = LOWER(JUSTPATH(sys(16))) + "\"

	CHDIR          (m.gcini_path)
	SET DEFAULT TO (m.gcini_path)

	set Path To (gcini_path)
	set Path To (gcini_path + "west-wind")  addi
	set Path To (gcini_path + "west-wind\classes") addi
	_screen.FontName = "Courier"

	SET PROCEDURE TO (SYS(16)) ADDITIVE 
ENDIF 
IF VARTYPE(locker) # "C"
    PUBLIC locker
ENDIF
locker = GETENV("COMPUTERNAME")

IF VARTYPE(token) # "C"
    PUBLIC token
ENDIF

IF VARTYPE(adresa) # "C"
    PUBLIC adresa
ENDIF
adresa =  m.hk_url		&& "http://transakce.mine.nu:9988/"

IF VARTYPE(gldebug_print) # "C"
    PUBLIC gldebug_print
ENDIF
gldebug_print = .F.

*     *     *     *     *     *     *     *     *     *     *     *     *     
DO wwHTTP
SET CLASSLIB TO wwIPStuff ADDITIVE

IF VARTYPE(oHTTP) # "O"
    PUBLIC oHTTP
ENDIF
oHTTP=CREATEOBJECT("wwHttp")
oHTTP.nconnecttimeout = 6

DO wwJsonSerializer 
IF VARTYPE(loSerializer) # "O"
    PUBLIC loSerializer
ENDIF
loSerializer = CREATEOBJECT("wwJsonSerializer")
IF gldebug_print
  ? REPLICATE("- ", 9) + "   WestWind initialized   " + REPLICATE(" -", 9)
ENDIF 

**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
PROCEDURE dotaz
PARAMETERS parametry
stranka = m.adresa + parametry

IF .not. EMPTY(oHTTP.cPostbuffer)
   IF (.not. "locker=" $ oHTTP.cPostbuffer) .and. (.not. "token=" $ oHTTP.cPostbuffer)
       oHTTP.AddPostKey("locker", m.locker)
       oHTTP.AddPostKey("token",  m.token)
   ENDIF 
ENDIF 

post_buff = oHTTP.cPostbuffer

lcHTML = oHTTP.HTTPGet(m.stranka)

IF 'ACCESS DENIED' $ m.lcHTML
   DO pozdrav
   oHTTP.cPostbuffer = m.post_buff
   oHTTP.AddPostKey("token",  m.token)
   lcHTML = oHTTP.HTTPGet(m.stranka)
ENDIF 


IF gldebug_print
    tiskni_hlavicku(m.parametry)
    tiskni_lcHTML ()
ENDIF 
RETURN m.lcHTML

**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
FUNCTION json_ok (json)
*
* hleda v parametru retezec za "retval" hodnotu "0" a "OK"
*
retval = LEFT(SUBSTR(json, AT('"retval":', m.json)+8), 222)
retval = SUBSTR(m.retval, AT('"', m.retval)+1)
retval = LEFT(m.retval, AT('"', m.retval)-1)
RETURN ("0" $ m.retval .and. "OK" $ m.retval)



**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
* pred carku a "}" se da tabulator, ktery se v dalsim parsovani uziva jako ukoncovac itemu
FUNCTION strip_whitespaces (json_txt)
LOCAL json, znak, ii
json = ""
uvnitr_stringu = .F.
FOR ii = 1 TO LEN(m.json_txt)
   znak = SUBSTR(m.json_txt, m.ii, 1)
   DO CASE 
   CASE m.znak = '"'
      uvnitr_stringu = .not. uvnitr_stringu
      json = m.json + m.znak

   CASE m.uvnitr_stringu
      IF m.znak = CHR(9)
         json = m.json + " "
      ELSE
         json = m.json + m.znak
      ENDIF 

   OTHERWISE     && vne stringu
      DO CASE 
      CASE m.znak = ','
        json = m.json + CHR(9) + m.znak
      CASE m.znak = '}'
        json = m.json + CHR(9) + m.znak
      CASE m.znak > ' '
        json = m.json + m.znak
      ENDCASE 
   ENDCASE 
ENDFOR 
RETURN m.json


**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
FUNCTION najdi_item (radek, item)
IF AT('"'+m.item+'":', m.radek) > 0
   rv = SUBSTR(m.radek, AT('"'+m.item+'":', m.radek)+LEN(m.item)+3)
   rv = LEFT(m.rv, AT(CHR(9), m.rv)-1)
   IF LEFT(rv, 1) = '"' .and. RIGHT(rv, 1) = '"'
      rv = SUBSTR(m.rv, 2, LEN(m.rv)-2)
   ENDIF 
   IF gldebug_print
     ? item + ":" + rv
   ENDIF 
   RETURN rv
ELSE
   IF gldebug_print
     ? item, "NENENENENENE"
   ENDIF 
   RETURN ""
ENDIF 



**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
PROCEDURE tiskni_lcHTML
predposledni = "QWE"
delka_radku = 0
FOR ii = 1 TO LEN(lcHTML)
   pismenko = SUBSTR(m.lcHTML, ii, 1)
   IF (pismenko # CHR(10)) .and. (pismenko # CHR(13))
       IF (pismenko = "{")  .or.  (m.pismenko = '"' .and. predposledni = ',' .and. m.delka_radku > 133)  .or.  (m.delka_radku > 155)
          ?? CHR(13)+CHR(10)
          delka_radku = 0
       ELSE
          delka_radku = delka_radku + 1
       ENDIF
       ?? m.pismenko
       predposledni = m.pismenko
   ENDIF
ENDFOR 

STRTOFILE(m.lcHTML, "html.txt", 0)

**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
PROCEDURE tiskni_hlavicku
PARAMETERS hlavicka
? CHR(13)+CHR(10) + REPLICATE("*", LEN(m.hlavicka) + 8) 
? + "**  " + m.hlavicka + "  **" 
IF .not. EMPTY(m.post_buff)
   ? "**  " + PADC(m.post_buff, LEN(m.hlavicka) + 0) + "  **" 
ENDIF 
? REPLICATE("*", LEN(m.hlavicka) + 8)

**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
PROCEDURE pozdrav
PRIVATE oldExact
stranka_pozdravu = m.adresa + "ahoj?locker=" + m.locker
lcHTML = oHTTP.HTTPGet(m.stranka_pozdravu)

oldExact = SET("exact")
SET EXACT OFF 		&& Bacha, serializer vyzaduje set exact off
rr = loSerializer.Deserialize(m.lcHTML)
token = keyhole(rr.keyhole)
SET EXACT &oldExact
IF gldebug_print
  ? "Pozdrav:", m.lcHTML, " computed token:" + m.token
ENDIF 

**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
PROCEDURE keyhole
PARAMETERS seminko
soucet = 0
retez = UPPER(locker) + seminko
FOR ii = 1 TO len(retez)
   soucet = m.soucet + (ASC( SUBSTR(retez, ii) )-32) * m.ii
ENDFOR
RETURN ALLTRIM(STR(soucet))

**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
FUNCTION d_to_s
LPARAMETERS datum
pom_d = DTOS(m.datum)
RETURN LEFT(m.pom_d, 4) + "-" + SUBSTR(m.pom_d, 5,2) + "-" + SUBSTR(m.pom_d, 7,2)