Re: Euphoria and CGI scripts
- Posted by "Marco A. Achury P." <achury at ELDISH.NET> Sep 06, 1999
- 658 views
Este es un mensaje multipartes en formato MIME. --------------1682FED3043CF3CAC8A67873 Hi: A question for all the cgi gurus When I call a cgi from a link without parameters in my web page (not a form), there is not any data to cgiparse. I'm doing a guest book. I want call the cgi from a link to view the all the messages and a form to add a new one. When the same cgi is called from the form, add the message to the database before do the 'normal' operation. I have problems with some '0' that appear in the database between the registers. What is the problem? -- +-+-+-+-+-+-+-+-+-+ Marco A. Achury P. http://members.xoom.com/achury mailto:achury at eldish.net ICQ: 19390207 Caracas, Venezuela --------------1682FED3043CF3CAC8A67873 name="Mensajes.exu" Content-Disposition: inline; filename="Mensajes.exu" #!/usr/euphoria/bin/exu global integer ArchDatos constant archivo="../mensajes/mensajes.db" global atom dump --Archivo al que se volcan variables include get.e -- Interpreta la entrada estandar proveniente de un formulario function cgi_parse(sequence entrada) sequence hex sequence val atom car -- Caracter a calculado con hexagesimal -- atom dig -- Digito hexagesimal integer i -- Caracter siendo analizado integer j -- Indica numero de "registro", consecutivo de parametros integer k -- Indica numero de "campo" del registro: 1->Nombre 2->Valor sequence parametros hex={"0123456789abcdefABCDEF"} if equal(entrada, {}) then return {} end if i=1 j=1 k=1 parametros={{{},{}}} while i<length(entrada) do if entrada[i]='+' then parametros[j][k]=append(parametros[j][k], ' ') elsif entrada[i]='=' then k=2 elsif entrada[i]='&' then k=1 j=j+1 parametros = append(parametros, {{},{}}) elsif entrada[i]='%' then --trace(1) car=0 car=val[find(entrada[i+1],hex[1])]*16+val[find(entrada[i+2],hex[1])] parametros[j][k]=append(parametros[j][k], car) i=i+2 trace(0) else parametros[j][k]=append(parametros[j][k], entrada[i]) end if i=i+1 end while puts(dump, "Salida de cgi-parse") print(dump, parametros) return parametros end function procedure NuevoMensaje(sequence entrada) --Pone un nuevo mensaje en la base de datos sequence mensajes sequence Nombre --Remitente del mensaje sequence Texto --Contiene el texto del mensaje if sequence(entrada[1][2]) and sequence( entrada[2][2]) then Nombre = entrada[1][2] Texto = entrada[2][2] --Abrir base de datos... ArchDatos=open(archivo,"r") if ArchDatos!=-1 then --Si existe el archivo --Lee la estructura de datos mensajes=get(ArchDatos) close(ArchDatos) else --Si no existe, tendremos que crear una nueva... mensajes={} end if if equal(mensajes,{}) then mensajes ={Nombre, Texto} -- Poner la entrada al final de la hilera de mensajes else mensajes=prepend(mensajes, {Nombre, Texto}) end if -- Si hay muchos mensajes elimina los mas viejos if length(mensajes)>20 then mensajes=mensajes[1..20] end if -- Grabar nuestra estructura en la base de datos ArchDatos=open(archivo,"w") print(ArchDatos, mensajes) close(ArchDatos) end if end procedure procedure encabezados() puts (1,"Content-type: text/html\n\n") puts (1,"<html><body>\n") puts (1,"<a href=\"/index.html\">Volver a nuestra pagina principal</a>\n") puts (1,"<h1>Tablero de mensajes</h1>\n") puts (1,"Bienvenido a nuestro sistema de mensajes<br>\n") end procedure procedure formulario() -- Abrir archivo html, y volcarlo en el formulario puts(1,"<h2>Enviar Mensajes</h2>\n" & "<form action=\"/cgi-bin/mensajes.exu\" method=\"POST\">\n"& "Nombre: <input type=\"text\" name=\"nombre\"><BR>\n"& "Comentario:<textarea name=comentario rows=3 cols=60></textarea>\n"& "<input type=submit value=\"ENVIAR\"><input type=reset value=\"BORRAR\">\n"& "<hr><p>Marco Antonio Achury<br>Agosto de 1999") end procedure procedure MostrarMensajes() sequence mensajes sequence nombre object contenido mensajes=get(ArchDatos) if mensajes[1] = GET_SUCCESS then mensajes=mensajes[2..length(mensajes)] puts (1,"Encontrados mensajes <br>") for r=1 to length(mensajes) do -- ? mensajes nombre=mensajes[r][1] contenido=mensajes[r][2] puts(1, "<p>Mensaje de: <b>") puts(1, nombre) puts(1, "</b><p>") puts(1, contenido) puts(1, "<hr>") end for end if close(ArchDatos) end procedure procedure pagina() -- Probar si existe la base de datos encabezados() ArchDatos=open(archivo,"r") if ArchDatos=-1 then --Si la base de datos no existe puts(1, "En este momento no hay mensajes.<br>") else MostrarMensajes() end if formulario() end procedure -- procedure @ProgramaPrincipal() object entrada entrada = gets (0) -- Si la llamada trae datos (hecha desde formulario) dump =open("../mensajes/dump", "w") if sequence(entrada) then -- Poner nuevo mensaje en la base de datos NuevoMensaje(cgi_parse(entrada)) end if --Presentar la pagina actualizada pagina() --------------1682FED3043CF3CAC8A67873--