-
Notifications
You must be signed in to change notification settings - Fork 24
/
toba_ei_firma.php
331 lines (299 loc) · 9.73 KB
/
toba_ei_firma.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
* Genera un editor de código
* @package Componentes
* @subpackage Eis
* @jsdoc ei ei
* @wiki Referencia/Objetos/ei_firma
*/
class toba_ei_firma extends toba_ei
{
protected $_ancho;
protected $_alto;
protected $_motivo_firma = "";
protected $_mostrar_pdf = true;
protected $_pdf_altura = "500px";
protected $_multiple = false;
protected $_url_pdf_embebido = null;
final function __construct($id)
{
parent::__construct($id);
$this->_ancho = get_var($this->_info_firma['ancho'], '500px');
$this->_alto = get_var($this->_info_firma['alto'], '120px');
if (isset($this->_memoria['dir_actual'])) {
$this->_multiple = $this->_memoria['multiple'];
}
}
/**
* Destructor del componente
*/
function destruir()
{
$this->_memoria['multiple'] = $this->_multiple;
parent::destruir();
}
//-------------------------------------------------------------------------------
//---- UTIL PARA LA CONSTRUCCIÓN DEL COMPONENTE ---------------------------------
//-------------------------------------------------------------------------------
protected function get_datos()
{
return array();
}
protected function get_dimensiones()
{
return array($this->_ancho, $this->_alto);
}
function disparar_eventos()
{
if (isset($_POST[$this->_submit]) && $_POST[$this->_submit]!="") {
$evento = $_POST[$this->_submit];
if (isset($this->_memoria['eventos'][$evento])) {
//Me fijo si el evento requiere validacion
$maneja_datos = ($this->_memoria['eventos'][$evento] == apex_ei_evt_maneja_datos);
if($maneja_datos) {
$parametros = $_POST[$this->_id_post_codigo];
} else {
$parametros = null;
}
//El evento es valido, lo reporto al contenedor
$this->reportar_evento( $evento, $parametros );
}
}
}
function set_datos($datos)
{
}
function generar_html()
{
//Genero la interface
echo "\n\n<!-- ***************** Inicio EI FIRMA ( ". $this->_id[1] ." ) *********** -->\n\n";
echo toba_form::hidden($this->_submit, '');
//echo toba_form::hidden($this->_id_post_codigo, ''); // Aca viaja el codigo
echo $this->get_html_barra_editor();
$this->generar_html_barra_sup(null, true,"ei-form-barra-sup");
$this->generar_applet();
}
function generar_applet()
{
$sesion = $this->generar_sesion();
$cookie = session_name()."=".session_id();
$url_jar = toba_recurso::url_toba()."/firmador_pdf/firmador.jar";
$valor_cross_token = toba::memoria()->get_dato_operacion(apex_sesion_csrt);
$destino = array($this->_id);
$url_enviar = $this->get_url_enviar_pdf(true);
$url_base = $this->get_url_base_actual();
$url_recibir = toba::vinculador()->get_url(null, null, array('accion' => 'recibir'),array('servicio' => 'ejecutar',
'objetos_destino' => $destino), true);
$url_recibir = $url_base.$url_recibir;
echo "<applet id='AppletFirmador' code='ar/gob/onti/firmador/view/FirmaApplet' scriptable='true''
archive='$url_jar' width='{$this->_ancho}' height='{$this->_alto}' >\n";
if (! $this->_multiple) {
echo "<param name='URL_DESCARGA' value='$url_enviar' />\n";
} else {
echo "<param name='MULTIPLE' value='true' />\n";
}
echo "<param name='URL_SUBIR' value='$url_recibir' />
<param name='MOTIVO' value='{$this->_motivo_firma}' />
<param name='CODIGO' value='$sesion' />
<param name='PREGUNTAS' value='{ \"preguntasRespuestas\": []}' />
<param name='COOKIE' value='$cookie' />
<param name='classloader_cache' value='false' />
<param name='codebase_lookup' value='false' />
<param name='TOKID' value='".apex_sesion_csrt."'/>
<param name='TOKVAL' value='$valor_cross_token'/>
</applet>
";
if ($this->_mostrar_pdf) {
$this->_url_pdf_embebido = $this->get_url_enviar_pdf(false);
$this->_url_pdf_embebido .= '&codigo='.$sesion;
if ($this->_multiple) {
$texto_alternativo = 'Haga click en los documentos para visualizarlos.';
} else {
$texto_alternativo = "Parece que no tiene Adobe Reader o soporte PDF en este navegador.</br>Para configurar correctamente instale Adobe Reader y siga <a href='http://helpx.adobe.com/acrobat/using/display-pdf-browser-acrobat-xi.html'>estas instrucciones</a>.";
}
echo "<div id='pdf' style='display: none; height: {$this->_pdf_altura}; text-align: center'><div style='margin-top: 40px; color: gray'>$texto_alternativo</div></div>";
}
}
/**
*
* @param boolean $usa_url_encode El Applet necesita que la URL este encodeada (por el infame caracter || que usa toba), mientras que JS no lo necesita
* @return string
*/
function get_url_enviar_pdf($usa_url_encode)
{
$destino = array($this->_id);
$url_base = $this->get_url_base_actual();
$url_enviar = toba::vinculador()->get_url(null, null, array('accion' => 'enviar'),array('servicio' => 'ejecutar',
'objetos_destino' => $destino), $usa_url_encode);
$url_enviar = $url_base.$url_enviar;
return $url_enviar;
}
function get_url_base_actual()
{
$url = toba_http::get_url_actual();
return $url;
}
function generar_sesion()
{
if (! isset($this->_memoria['token'])) {
$this->_memoria['token'] = hash('sha256', uniqid(mt_rand(), true));
}
return $this->_memoria['token'];
}
function set_motivo_firma($motivo)
{
$this->_motivo_firma;
}
function set_multiple($multiple)
{
$this->_multiple = $multiple;
}
function set_dimension($ancho, $alto)
{
$this->_ancho = $ancho;
$this->_alto = $alto;
}
function set_mostrar_pdf($mostrar)
{
$this->_mostrar_pdf = $mostrar;
}
function set_alto_pdf($alto)
{
$this->_pdf_altura = $alto;
}
/**
* Servicio que se ejecuta cuando el applet busca/envia el PDF
* @param array $parametros
* @ignore
*/
function servicio__ejecutar($parametros = null)
{
toba::memoria()->desactivar_reciclado();
$accion = toba::memoria()->get_parametro('accion');
switch ($accion) {
case 'enviar':
$this->accion_enviar($parametros);
break;
case 'recibir':
$this->accion_recibir($parametros);
break;
default:
return;
}
}
protected function accion_enviar($parametros)
{
$codigo = toba::memoria()->get_parametro('codigo');
if (! isset($codigo) || is_null($codigo)) {
header('HTTP/1.1 500 Internal Server Error');
throw new toba_error_seguridad("Falta indicar el codigo");
}
if ($this->_memoria['token'] != $codigo) {
header('HTTP/1.1 500 Internal Server Error');
throw new toba_error_seguridad("Codigo invalido");
}
$numero_documento = null;
if ($this->_multiple) {
$test_val = toba::memoria()->get_parametro('id');
if (! isset($test_val)) {
header('HTTP/1.1 500 Internal Server Error');
throw new toba_error_seguridad("Falta indicar el ID del documento a enviar");
}
$numero_documento = (int) toba::memoria()->get_parametro('id');
}
//Reportar evento al padre
$pdf = $this->reportar_evento_interno('enviar_pdf', $this->_memoria['token'], $numero_documento);
if (isset($pdf) && $pdf !== apex_ei_evt_sin_rpta) {
//Enviar PDF
header("Cache-Control: private");
header("Content-type: application/pdf");
header("Pragma: no-cache");
header("Expires: 0");
echo $pdf;
} else {
toba::logger()->error("toba_ei_fimar: No se atrapo el evento enviar_pdf!");
}
return;
}
protected function accion_recibir($parametros)
{
if (! isset($_POST['codigo'])) {
header('HTTP/1.1 500 Internal Server Error');
throw new toba_error_seguridad("Falta indicar el codigo");
}
if ($this->_memoria['token'] != $_POST['codigo']) {
header('HTTP/1.1 500 Internal Server Error');
throw new toba_error_seguridad("Codigo invalido");
}
if ($_FILES["md5_fileSigned"]["error"] != UPLOAD_ERR_OK) {
error_log("Error uploading file");
header('HTTP/1.1 500 Internal Server Error');
throw new toba_error_seguridad("Error: ".$_FILES["md5_fileSigned"]["error"]);
}
$numero_documento = null;
if ($this->_multiple) {
if (! isset($_POST['id'])) {
header('HTTP/1.1 500 Internal Server Error');
throw new toba_error_seguridad("Falta indicar el ID del documento a recibir");
}
$numero_documento = (int) $_POST['id'];
}
$temp = rand();
$destino = toba::proyecto()->get_path_temp()."/$temp.pdf";
$path = $_FILES['md5_fileSigned']['tmp_name'];
if (! move_uploaded_file($path, $destino)) {
error_log("Error uploading file");
header('HTTP/1.1 500 Internal Server Error');
return;
}
try {
$this->reportar_evento_interno('recibir_pdf_firmado', $destino, $this->_memoria['token'], $numero_documento);
} catch (Exception $e) {
error_log("Error al atender el evento recibir_pdf_firmado");
header('HTTP/1.1 500 Internal Server Error');
throw $e;
}
if (file_exists($destino)) {
unlink($destino);
}
return;
}
//-------------------------------------------------------------------------------
//---- JAVASCRIPT ---------------------------------------------------------------
//-------------------------------------------------------------------------------
/**
* @ignore
*/
protected function crear_objeto_js()
{
$identado = toba_js::instancia()->identado();
$id = toba_js::arreglo($this->_id, false);
echo $identado."window.{$this->objeto_js} = new ei_firma($id,
'{$this->_submit}',
".($this->_multiple ? 'true' : 'false').");\n";
echo "
function appletLoaded() {
{$this->objeto_js}.applet_cargado();
}
function firmaOk() {
{$this->objeto_js}.firma_ok();
}
if (! {$this->objeto_js}._multiple) {
window.onload = function () {
{$this->objeto_js}.ver_pdf_inline('{$this->_url_pdf_embebido}');
};
}
";
}
/**
* @ignore
*/
function get_consumo_javascript()
{
$consumo = parent::get_consumo_javascript();
$consumo[] = 'componentes/ei_firma';
$consumo[] = 'utilidades/pdfobject.min';
return $consumo;
}
}
?>