1 | <!DOCTYPE html> |
---|
2 | <html lang="es"> |
---|
3 | <head> |
---|
4 | <?php require_once "./url.php" ?> |
---|
5 | |
---|
6 | <!-- Metadata & Essential styles and scripts --> |
---|
7 | <?php require_once "./headEssential.php" ?> |
---|
8 | </head> |
---|
9 | <body> |
---|
10 | |
---|
11 | <!-- Header --> |
---|
12 | <?php require_once "./header.php" ?> |
---|
13 | |
---|
14 | <!-- Navbar --> |
---|
15 | <?php require_once "./navbar.php" ?> |
---|
16 | |
---|
17 | <main class="container py-5"> |
---|
18 | <h2 class="h3 border-bottom text-secondary text-center pb-3 mb-3"> Documento firmado</h2> |
---|
19 | <div class="row"> |
---|
20 | <div class="col-10 offset-1 col-md-6 offset-md-3"> |
---|
21 | <?php |
---|
22 | session_start(); |
---|
23 | |
---|
24 | |
---|
25 | /** |
---|
26 | * ValidateFiles |
---|
27 | * valida de archivos subidos |
---|
28 | * @param temp (ruta del archivo temporal) |
---|
29 | * @param route (ruta relativa del archivo subido) |
---|
30 | * @return bool |
---|
31 | * |
---|
32 | * @author Angelo Osorio (@engelpain) |
---|
33 | * @licence CENDITEL 2.0 |
---|
34 | */ |
---|
35 | function ValidateFiles($temp, $route) { |
---|
36 | move_uploaded_file($temp, $route) or die("Error al intentar subir el archivo." . $route); |
---|
37 | } |
---|
38 | |
---|
39 | // Datetime |
---|
40 | $datetime = date_timestamp_get(date_create()); // nombre que tendrán los archivos temporales cargados |
---|
41 | |
---|
42 | // Frase de paso del p12 |
---|
43 | $passphase = base64_decode($_POST["password"]); // Frase de paso cifrada |
---|
44 | openssl_private_decrypt($passphase, $password, $_SESSION["private_key_pem"]); // Frase de paso decifrada |
---|
45 | |
---|
46 | // PDF |
---|
47 | $route_pdf = '../tmp/' . $datetime . '.pdf'; // ruta donde se alojará el pdf |
---|
48 | $pdf = $_FILES['pdf']['tmp_name']; // ruta del archivo temporal del pdf subido |
---|
49 | |
---|
50 | // PKCS12 |
---|
51 | $route_pkcs12 = '../tmp/' . $datetime . '.p12'; // Ruta donde se alojará el p12 |
---|
52 | $pkcs12 = $_FILES['pkcs12']['tmp_name']; // ruta del archivo temporal del p12 subido |
---|
53 | |
---|
54 | print_r(ValidateFiles($pdf, $route_pdf)); |
---|
55 | print_r(ValidateFiles($pkcs12, $route_pkcs12)); |
---|
56 | |
---|
57 | // Ejecución del script que conecta con el API de PortableSigner |
---|
58 | |
---|
59 | $callPortable = "java -jar ../PortableSigner.jar -n"; |
---|
60 | |
---|
61 | $error_log = "../tmp/error_log" . $datetime; |
---|
62 | $run_shell = `$callPortable -t $route_pdf -o ../tmp/$datetime-signed.pdf -s $route_pkcs12 -p $password 2> $error_log`; |
---|
63 | $run = ($run_shell) ? false : true; |
---|
64 | |
---|
65 | $archivo = fopen ("../tmp/error_log" . $datetime, "r"); |
---|
66 | $num_lineas = 0; |
---|
67 | while (!feof ($archivo)) { |
---|
68 | if ($linea = fgets($archivo)){ |
---|
69 | $num_lineas++; |
---|
70 | } |
---|
71 | } |
---|
72 | fclose($archivo); |
---|
73 | |
---|
74 | |
---|
75 | if ($run && $num_lineas == 1 ) { |
---|
76 | echo '<a class="btn btn-primary btn-block" href="../tmp/'.$datetime.'-signed.pdf" target="_blank"> |
---|
77 | <i class="fa fa-file-download"></i> Descargar PDF firmado |
---|
78 | </a>'; |
---|
79 | session_destroy(); |
---|
80 | } else { |
---|
81 | echo '<p class="text-center alert-danger text-danger py-4">La constraseña es incorrecta.</p>'; |
---|
82 | // En caso de requerir los resultados del log, descomentar |
---|
83 | /* |
---|
84 | $myfile = fopen("../tmp/error_log" . $datetime, "r") or die("Unable to open file!"); |
---|
85 | echo fread($myfile, filesize("../tmp/error_log" . $datetime)); |
---|
86 | fclose($myfile); |
---|
87 | */ |
---|
88 | } |
---|
89 | |
---|
90 | unlink($route_pdf) or die("No se pudo eliminar el pdf"); |
---|
91 | unlink($route_pkcs12) or die("No se pudo eliminar el p12"); |
---|
92 | unlink($error_log) or die("No se pudo eliminar el log"); |
---|
93 | |
---|
94 | ?> |
---|
95 | </div> |
---|
96 | <div class="col-10 offset-1 col-md-6 offset-md-3 pt-3"> |
---|
97 | <a class="btn btn-secondary btn-block" href="<?php returnURL(); ?>/"> |
---|
98 | <i class="fa fa-hand-point-left"></i> Volver atrás |
---|
99 | </a> |
---|
100 | </div> |
---|
101 | </div> |
---|
102 | </main> |
---|
103 | |
---|
104 | <!-- Footer --> |
---|
105 | <?php require_once "./footer.php" ?> |
---|
106 | </body> |
---|
107 | </html> |
---|