Hola Hector,

  Puedes guardar la entrada del php en un fichero para inspeccionarla:

       $sig=file_get_contents("php://input");
       file_put_contents("X_File", $sig);

 Ahí verás si hay algo raro en el query string, también puedes
trocearla e intentar decodificarla.

Saludos.
Paúl.

El día 13 de abril de 2010 09:16, Héctor Espí Hernández
<[email protected]> escribió:
> Hola a todos. Tengo un problema a la hora de almacenar el PDF firmado desde
> PHP.
>
> Utilizo la función signUrlToUrl, pasándole a la url de destino (el php que
> me da problemas) un parámetro adicional con la ruta en la que quiero que
> almacene al PDF firmado.
>
> El proceso de firma se realiza correctamente y se invoca al PHP pasándole
> por POST los parámetros: content (con el contenido del PDF en base64,
> teóricamente) y path (con la ruta). El caso es que la decodificación a
> base64 parece estar fallando, pues se genera un fichero con tamaño 0. No
> obstante el parámetro $_POST["content"] tiene datos, sino no pasaría del
> if.... ¿alguien ve o sabe que se hace mal?.
>
> Os envío los ficheros implicados:
>
> PHP:
>
> <?php
>
> //Procesar los includes
> $g_path = $_SERVER['DOCUMENT_ROOT'] . "/FLUX";
> $g_path = str_replace("//", "/", $g_path);
>
> /*****************/
> /** PROPIEDADES    **/
> /*****************/
>
> $g_res = "";
> $g_rutaFichero = "";
> $g_rutaBackup = "";
>
> //Comprobar parámetros
> if(isset($_POST["content"]) && isset($_POST["path"]))
> {
>     //Obtener el contenido del fichero
>     if(($g_contenidoFichero = base64_decode($_POST["content"])) === FALSE)
>     {
>         $g_res = "Fallo al decodificar el contenido del fichero: " .
> $_POST["content"];
>     }
>     else
>     {
>         //$g_contenidoFichero = $_POST["content"];
>
>         //Obtener las rutas al documento
>         $g_rutaFichero = $g_path . "/" . $_POST["path"];
>         $g_rutaBackup = "$g_rutaFichero.bak";
>
>         //Comprobar
>         if(file_exists($g_rutaFichero))
>         {
>             //Hacer un backup del fichero a reemplazar
>             if(file_exists($g_rutaBackup))
>             {
>                 unlink($g_rutaBackup);
>             }
>
>             copy($g_rutaFichero, $g_rutaBackup);
>
>             //Crear el fichero firmado
>             if(($fh = fopen($g_rutaFichero, 'w')) === FALSE)
>             {
>               $g_res = "No se puede sobreescribir el documento original";
>             }
>             else
>             {
>               if(fwrite($fh, $g_contenidoFichero) === FALSE)
>               {
>                   $g_res = "Fallo al escribir el contenido:
> $g_contenidoFichero";
>
>                   //Restablecer el backup
>                   fclose($fh);
>                   copy($g_rutaBackup, $g_rutaFichero);
>               }
>               else
>               {
>                   fclose($fh);
>               }
>
>             }
>         }
>         else
>         {
>             $g_res = "El fichero a firmar no existe";
>         }
>
>     }
>
> }
> else
> {
> $g_res = "El fichero firmado está vacío";
> }
>
> echo $g_res;
>
> ?>
>
> URLOUTPUTPARAMS.JAVA
>
> package es.uji.security.ui.applet.io;
>
> import java.io.DataOutputStream;
> import java.io.File;
> import java.io.IOException;
> import java.io.InputStream;
> import java.net.HttpURLConnection;
> import java.net.URL;
> import java.net.URLEncoder;
> import java.util.StringTokenizer;
>
> import netscape.javascript.JSObject;
>
> import org.apache.log4j.Logger;
>
> import es.uji.security.crypto.config.OS;
> import es.uji.security.ui.applet.JSCommands;
> import es.uji.security.ui.applet.SignatureApplet;
>
> public class URLOutputParams extends AbstractData implements OutputParams
> {
>     private Logger log = Logger.getLogger(URLOutputParams.class);
>
>     private String[] urls = null;
>     private int current = 0;
>     private boolean signOkInvoked = false;
>     private boolean loginOkInvoked = false;
>     private int _count = 1;
>     private int outputcount = 0;
>     private int conn_timeout = 10000;
>     private int read_timeout = 60000;
>     private String postVariable = "content";
>
>     public URLOutputParams(String[] urls)
>     {
>         this(urls, "content");
>     }
>
>     public URLOutputParams(String[] urls, String postVariable)
>     {
>         this.urls = urls;
>         this.postVariable = postVariable;
>     }
>
>     public void setOutputCount(int oCount)
>     {
>         this.outputcount = oCount;
>     }
>
>     public void setSignData(InputStream is) throws IOException
>     {
>         String cookies = "";
>
>         // Try to obtain and configure Cookies
>         try
>         {
>             log.debug("Recover JavaScript member: document");
>             JSObject document = (JSObject)
> JSCommands.getWindow().getMember("document");
>
>             cookies = (String) document.getMember("cookie");
>             log.debug("Cookies: " + cookies);
>         }
>         catch (Exception e)
>         {
>             log.debug("Cookies can not be obtained", e);
>         }
>
>         String urlOk= null;
>         String urlOriginal = null;
>         if (this.urls.length>1)
>             urlOriginal = this.urls[current];
>         else
>             urlOriginal = this.urls[0];
>
>         if (urlOriginal.indexOf('?') > -1)
>         {
>             urlOk = urlOriginal.substring(0, urlOriginal.indexOf('?'));
>         }
>         else
>         {
>             urlOk = urlOriginal;
>         }
>
>
>         log.debug("Posting data to " + urlOk + ", with post parameter
> variable " + postVariable);
>
>         URL url = new URL(urlOk);
>
>         HttpURLConnection urlConn = (HttpURLConnection)
> url.openConnection();
>
>         urlConn.setConnectTimeout(conn_timeout);
>         urlConn.setReadTimeout(read_timeout);
>
>         urlConn.setRequestMethod("POST");
>         urlConn.setRequestProperty("Content-type",
> "application/x-www-form-urlencoded");
>         urlConn.setRequestProperty("Cookie", "");
>
>         urlConn.setDoOutput(true);
>         urlConn.setDoInput(true);
>
>         DataOutputStream out = new
> DataOutputStream(urlConn.getOutputStream());
>
>         byte[] buffer = new byte[2048];
>         int length = 0;
>
>         out.writeBytes(postVariable + "=");
>
>         while ((length = is.read(buffer)) >= 0)
>         {
>             out.writeBytes(URLEncoder.encode(new String(buffer,0,length),
> "ISO-8859-1"));
>         }
>
>         out.writeBytes("&item=" + URLEncoder.encode("" + _count,
> "ISO-8859-1"));
>
>         //Enviar los par�metros asociados a la url original
>         StringTokenizer strTok = new
> StringTokenizer(urlOriginal.substring(urlOriginal.indexOf('?') + 1), "&");
>
>         while (strTok.hasMoreTokens())
>         {
>             String strAux = strTok.nextToken();
>             log.debug("PROCESANDO TOKEN: " + strAux);
>
>             if (strAux.indexOf("=") > -1)
>             {
>                 String var = strAux.substring(0, strAux.indexOf("="));
>                 String value = strAux.substring(strAux.indexOf("=") + 1);
>                 log.debug("ENVIANDO EN EL POST : " + var + "=" + value);
>
>                 out.writeBytes("&" + var + "=" + URLEncoder.encode(new
> String(value), "ISO-8859-1"));
>             }
>         }
>
>         out.flush();
>         out.close();
>
>         log.debug("Respuesta del servidor: " +
> urlConn.getResponseMessage());
>
>         if (urlConn.getResponseCode() >= 400)
>         {
>             log.error("Error en el post: " +
> urlConn.getResponseCode());
>             throw new IOException("Error en el post: " +
> urlConn.getResponseCode());
>         }
>
>         _count++;
>         current++;
>
>         try
>         {
>             is.close();
>             new File(OS.getSystemTmpDir() + "/signature.xsig").delete();
>         }
>         catch(Exception e)
>         {
>         }
>     }
>
>     public void setLoginData(String data)
>     {
>         //TODO: Not supported
>     }
>
>
>     public void setSignFormat(SignatureApplet base, byte[] signFormat)
>     {
>     }
>
>     public void setSignFormat(byte[] signFormat) throws IOException
>     {
>     }
>
>     public void signOk()
>     {
>         if (!signOkInvoked)
>         {
>             log.debug("Call JavaScript method: onSignOk");
>             JSCommands.getWindow().call("onSignOk", new String[] { "" });
>         }
>     }
>
>     public void loginOk()
>     {
>         if (!loginOkInvoked)
>         {
>             log.debug("Call JavaScript method: onLoginOk");
>             JSCommands.getWindow().call("onLoginOk", new String[] { "" });
>         }
>     }
>
>     public void flush()
>     {
>         _count = 1;
>         current = 0;
>     }
> }
>
> Ya no se me ocurren muchas cosas más, así que agradecería cualquier tipo de
> ayuda.
>
> Gracias. Un saludo.
>
> --
> Héctor Espí Hernández
>
> _______________________________________________
> CryptoApplet mailing list
> [email protected]
> http://llistes.uji.es/mailman/listinfo/cryptoapplet
>
>
_______________________________________________
CryptoApplet mailing list
[email protected]
http://llistes.uji.es/mailman/listinfo/cryptoapplet

Responder a