#!/home/tony/d4/cp/d4x

CGI_INIT; DO_CGI; #quit

Z<-CGI_INIT
|initialisations
EV.KB<-"CGI_ERROR"
QTRACE<-1
USERDATA<-"../userdata.txt"
UPALPHA<- #av i 256 & UPALPHA[97+i26]<-ALPHA<-#av 65+i26
IF 0=#fstat USERDATA; 0 r "" #zsave USERDATA
Z<-2 300 #use "FORM.DD"
Z<-""

Z<-GOTO_URL DST
PRINT "Location: ",DST
PRINT ""
Z<-#quit

Z<-OPEN_TRACE
PRINT"Context-type: text/plain"
PRINT""
PRINT"D4X GCI-BIN"
PRINTENV
Z<-""

Z<-DO_CGI;L
IF QTRACE; OPEN_TRACE
L<-#fi $ENVP["CONTENT_LENGTH"]
IF L=0; #quit
Z<-UPDATE_FORM #sstomat "&", $VA[0 0, L]
Z<-GOTO_URL $ENVP["HTTP_REFERER"]

Z<-PRINTENV;I;N;S
I<-0 & N<-1tr:Z<-Z[#sort Z<-#sstomat #use "$ENVP"]
WHILE I<N;S<-#deb Z[I++ :] & PRINT S,"=",$ENVP[S] & WEND
Z<-""

Z<-UPDATE_FORM TAB;NBITS;IDX;NN;USR;SEQ;X
Z<-SPLIT_FORM TAB
NN<-#sstomat #use"FORM.DD"
0 r 0 #sed ":r ",USERDATA
SEQ<-"/ /0/*" #rxsubs 6 z r #nfind"<LI>"
NBITS<-#fi FORM.DD["nbits"]
X<-NBITS r "0"
Z<-(,("bit" #find NN)[:0])/:NN
IDX<-#fi ," ",0 3 d Z
X[IDX]<-"1"
USR<-"/ /_/*" #rxsubs 8 t FORM.DD["user"]
IF QTRACE; PRINT "<LI>", USR, SEQ," ", X
#print "<LI>", USR, SEQ," ", X
T<-#deb FORM.DD["usercom"]
IF QTRACE; PRINT "comment=<",T,">"
IF 0<rT; #print "<CO>",USR,SEQ," ",T
0 r 0 #sed":u ", USERDATA
Z<-""

Z<-SPLIT_FORM TAB;J;K;N
|Split form fields
N<-1tr:TAB & J<-0
WHILE J<N
Z<-TAB[J++:]
K<-Zi"="
FORM.DD[KtZ]<-"/+/ /*" #rxsubs (K+1)dZ
WEND
Z<-""

Z<-PRINT X
$VA[0]<-X, #av 10
Z<-""

Z<-CHARS URL_ENCODE STR;J;U;X
|Url encode by changing characters to %xx
CHARS<-" #!%()[]<>" #ifndef "CHARS"
J<-(U<-STR e CHARS)/irSTR
Z<-(1+2*U)/STR
X<-,"%","0123456789ABCDEF"[16 16 n #av STR[J]]
J<-J+2*irJ
Z[,J #outer+ i3 ]<-X

Z<-URL_DECODE STR;J;K;U
|Use % as escape character for hex strings
J<-("%"=STR)/irSTR
X<-UPALPHA [#av STR[K<-J #outer+ 1 2]]
X<-#av 16 16 b "0123456789ABCDEF"iX
STR[J]<-X
U<-(rSTR)r1  & U[,K]<-0 & Z<-U/STR

Z<-CGI_ERROR
PRINT"cgi error ****"
#quit








