-
Notifications
You must be signed in to change notification settings - Fork 0
/
descarga.php
executable file
·395 lines (326 loc) · 21.5 KB
/
descarga.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
include ('getRealIP.php'); //Fichero que nos devolverá la direccion IP real del cliente, aun cuando navegue a traves de proxy
//Si no tiene sesion definida forzamos cierre de la ventana
if ( $_SESSION['tienesesion']=='NO' || empty($_GET['fichero']) )
{
echo "<script type='text/javascript'>
window.close();
</script>";
}
// Obtenemos la extension del fichero solicitado para saber si es un ZIP
if (!empty($_GET['fichero'])){
$_SESSION['extension'] = strtolower(substr(strrchr($_GET['fichero'], "."), 1));
}else{
header ("Location: http://sitar.aragon.es/datosdescarga/404.htm"); //Reenviamos en caso de que el parametro esté vaío
}
//******************************************************************************//
//*************************CASO de Fichero NORMAL. NO ZIP************************//
//******************************************************************************//
if (empty($_GET['inzip']))
{
//No se ha especificado el segundo parametro....
echo "<script type='text/javascript'>
document.getElementById('cargando').style.visibility = 'visible';
</script>";
// Path to downloadable files (will not be revealed to users so they will never know your file's real address)
$hiddenPath = "../03_datos/";
// VARIABLES
$_SESSION['IP'] = getRealIP();
$_SESSION['HOY'] = date("d.m.Y-H:i:s");
if (!empty($_GET['fichero'])){
$_SESSION['file'] = str_replace('%20', ' ', $_GET['fichero']);
}
//Construimos la URL definitiva de descarga
if (isset ($_GET['fichero']) ) { $_SESSION['fileadescargar'] = $_SESSION['file'];}
$_SESSION['rutadescarga'] = $hiddenPath;
$_SESSION['rutadescarga'] .= $_SESSION['fileadescargar'];
$_SESSION['file_real'] = $_SESSION['rutadescarga'];
//echo $_SESSION['fileadescargar'];exit;
// Si se nos ha pasado un file y si el file solicitado existe
if (!empty ($_SESSION['fileadescargar'] ) && file_exists($_SESSION['file_real']))
{
//echo 'ENTRO'; exit;
// Determine correct MIME type
switch($_SESSION['extension']){
case "asf": $_SESSION['TYPE'] = "video/x-ms-asf"; break;
case "asf": $_SESSION['TYPE'] = "video/x-ms-asf"; break;
case "avi": $_SESSION['TYPE'] = "video/x-msvideo"; break;
case "exe": $_SESSION['TYPE'] = "application/octet-stream"; break;
case "mov": $_SESSION['TYPE'] = "video/quicktime"; break;
case "mp3": $_SESSION['TYPE'] = "audio/mpeg"; break;
case "mpg": $_SESSION['TYPE'] = "video/mpeg"; break;
case "mpeg": $_SESSION['TYPE'] = "video/mpeg"; break;
case "rar": $_SESSION['TYPE'] = "encoding/x-compress"; break;
case "txt": $_SESSION['TYPE'] = "text/plain"; break;
case "wav": $_SESSION['TYPE'] = "audio/wav"; break;
case "wma": $_SESSION['TYPE'] = "audio/x-ms-wma"; break;
case "wmv": $_SESSION['TYPE'] = "video/x-ms-wmv"; break;
case "zip": $_SESSION['TYPE'] = "application/x-zip-compressed"; break;
case "pdf": $_SESSION['TYPE'] = "application/pdf"; break;
default: $_SESSION['TYPE'] = "application/force-download"; break;
}
// Fix IE bug [0]
//$header_file = (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) ? preg_replace('/\./', '%2e', $file, substr_count($file, '.') - 1) : $file;
$_SESSION['nameoffile'] = strtolower(substr(strrchr($_SESSION['file'], "/"), 1));
//Metodo Original
// Prepare headers
/* header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public", false);
header("Content-Description: File Transfer");
header("Content-Type: " . $type);
header("Accept-Ranges: bytes");
//header("Content-Disposition: attachment; filename=\"" . $header_file . "\";");
header("Content-Disposition: attachment; filename=\"" . $_SESSION['nameoffile'] . "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($_SESSION['file_real']));
// Send file for download
if ($stream = fopen($_SESSION['file_real'], 'rb')){
while(!feof($stream) && connection_status() == 0){
//reset time limit for big files
set_time_limit(0);
print(fread($stream,1024*8));
flush();
}
fclose($stream);
}*/
// Prepare headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public", false);
header("Content-Description: File Transfer");
header("Content-Type: " . $_SESSION['TYPE']);
header("Accept-Ranges: bytes");
header("Content-Disposition: attachment; filename=\"" . $_SESSION['nameoffile'] . "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($_SESSION['file_real']));
/*var_dump(headers_list());
flush();
print_r(apache_response_headers());
echo '<br><br><br><br><br>';
exit;*/
// Send file for download
ob_clean(); flush();
if(readfile($_SESSION['file_real'])){
//se ha enviado bien
$_SESSION['fp'] = fopen('descargas.log', 'a');
fwrite($_SESSION['fp'], $_SESSION['file_real']);fwrite($_SESSION['fp'], " ");
fwrite($_SESSION['fp'], $_SESSION['HOY']);fwrite($_SESSION['fp'], " ");
fwrite($_SESSION['fp'], $_SESSION['IP']);fwrite($_SESSION['fp'], " ");
fwrite($_SESSION['fp'], "\n");
fclose($_SESSION['fp']);
}
else{
//No se ha enviado bien o no se ha podido leer...
$_SESSION['fp'] = fopen('errores.log', 'a');
fwrite($_SESSION['fp'], $_SESSION['file_real']);fwrite($_SESSION['fp'], " ");
fwrite($_SESSION['fp'], $_SESSION['HOY']);fwrite($_SESSION['fp'], " ");
fwrite($_SESSION['fp'], $_SESSION['IP']);fwrite($_SESSION['fp'], " ");
fwrite($_SESSION['fp'], "Error en descarga");
fwrite($_SESSION['fp'], "\n");
fclose($_SESSION['fp']);
echo "<script type='text/javascript'>
window.close();
</script>";
};
flush();
}else{
//// (File NOT found) Error 404
$_SESSION['fp'] = fopen('errores.log', 'a');
if ($_SESSION['fileadescargar']==''){fwrite($_SESSION['fp'], 'NULO');fwrite($_SESSION['fp'], " ");}
if ($_SESSION['fileadescargar']!=''){fwrite($_SESSION['fp'], $_SESSION['fileadescargar']);fwrite($_SESSION['fp'], " ");}
fwrite($_SESSION['fp'], $_SESSION['HOY']);fwrite($_SESSION['fp'], " ");
fwrite($_SESSION['fp'], $_SESSION['IP']);fwrite($_SESSION['fp'], " ");
fwrite($_SESSION['fp'], "ERROR 404");
fwrite($_SESSION['fp'], "\n");
fclose($_SESSION['fp']);
header ("Location: http://sitar.aragon.es/datosdescarga/404.htm");
echo "<script type='text/javascript'>
window.location='http://sitar.aragon.es/datosdescarga/404.htm';
</script>";
exit;
}
}
//*************************FIN CASO de Fichero NORMAL NO ZIP************************//
//******************************************************************************//
//*************************CASO de Fichero ZIP *********************************//
//******************************************************************************//
if ( ($_SESSION['extension']=='zip') && (!empty($_GET['inzip'])) )
{
$_SESSION['Finzip'] = str_replace('%20', ' ', $_GET['inzip']);
$_SESSION['extensionZ'] = strtolower(substr(strrchr($_SESSION['Finzip'], "."), 1));
// Tomamos la extension para determinar el MIME
switch($_SESSION['extensionZ']){
case "asf": $_SESSION['TYPE'] = "video/x-ms-asf"; break;
case "asf": $_SESSION['TYPE'] = "video/x-ms-asf"; break;
case "avi": $_SESSION['TYPE'] = "video/x-msvideo"; break;
case "exe": $_SESSION['TYPE'] = "application/octet-stream"; break;
case "mov": $_SESSION['TYPE'] = "video/quicktime"; break;
case "mp3": $_SESSION['TYPE'] = "audio/mpeg"; break;
case "mpg": $_SESSION['TYPE'] = "video/mpeg"; break;
case "mpeg": $_SESSION['TYPE'] = "video/mpeg"; break;
case "rar": $_SESSION['TYPE'] = "encoding/x-compress"; break;
case "txt": $_SESSION['TYPE'] = "text/plain"; break;
case "wav": $_SESSION['TYPE'] = "audio/wav"; break;
case "wma": $_SESSION['TYPE'] = "audio/x-ms-wma"; break;
case "wmv": $_SESSION['TYPE'] = "video/x-ms-wmv"; break;
case "zip": $_SESSION['TYPE'] = "application/x-zip-compressed"; break;
default: $_SESSION['TYPE'] = "application/force-download"; break;
}
//echo $_SESSION['TYPE'];exit;
//Si es un zip y recibimos el segundo parametro del fichero dentro del zip....
if($_SESSION['extension']=='zip' && $_SESSION['Finzip']!='' )
{
require_once('pclzip.lib.php'); //Clase que usaremos para explorar el ZIP
// Path to downloadable files (will not be revealed to users so they will never know your file's real address)
$hiddenPath = "../03_datos/";
// VARIABLES
$_SESSION['IP'] = getRealIP();
$_SESSION['HOY'] = date("d.m.Y-H:i:s");
if (!empty($_GET['fichero'])){
$_SESSION['file'] = str_replace('%20', ' ', $_GET['fichero']);
}
//Construimos la URL definitiva de descarga
$_SESSION['rutadescarga'] = $hiddenPath;
$_SESSION['rutadescarga'] .= $_SESSION['file'];
//mostrar el fichero
//echo $_SESSION['rutadescarga'];echo $_SESSION['Finzip'] ; exit;
$_SESSION['archivo_zip'] = new PclZip($_SESSION['rutadescarga']); //Cargamos el archivo en un nuevo objeto del tipo pclzip
/* Ejecuta la extraccion del archivo en el directorio tmp_captcha */
$_SESSION['extractor'] = $_SESSION['archivo_zip']->extract( PCLZIP_OPT_PATH , 'tmp_captcha',PCLZIP_OPT_BY_NAME,$_SESSION['Finzip']);
$_SESSION['error_EX'] = $_SESSION['archivo_zip']->errorCode();
/* Gestionar error ocurrido (si $_SESSION['archivo_zip']->extract retorna cero a $_SESSION['extractor']) */
if ( $_SESSION['error_EX'] !=0 ) {
$_SESSION['error_enextra'] = "ERROR. Codigo: ".$_SESSION['archivo_zip']->errorCode()." ";
$_SESSION['error_enextra'] .= "Nombre: ".$_SESSION['archivo_zip']->errorName()." ";
$_SESSION['error_enextra'] .= "Descripcion: ".$_SESSION['archivo_zip']->errorInfo();
//echo $_SESSION['error_enextra'];exit;
//Escribimos en log de errores y cerramos ventana
$fp = fopen('errores.log', 'a');
fwrite($fp, $_SESSION['archivo_zip']);fwrite($fp, " ");
fwrite($fp, $_SESSION['Finzip']);fwrite($fp, " ");
fwrite($fp, $_SESSION['error_enextra']);fwrite($fp, " ");
fwrite($fp, $_SESSION['HOY']);fwrite($fp, " ");
fwrite($fp, $_SESSION['IP']);fwrite($fp, " ");
fwrite($fp, "\n");
fclose($fp);
flush();
echo "<script type='text/javascript'>
window.location='http://sitar.aragon.es/datosdescarga/captcha/error_zip.htm';
</script>";
exit;
}
//exit;
if ( $_SESSION['error_EX'] == 0 ) {
//echo "Archivo extraido existosamente!"; exit;
//Procedemos a la descarga...
// Prepare headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public", false);
header("Content-Description: File Transfer");
header("Content-Type: " . $_SESSION['extensionZ']);
header("Accept-Ranges: bytes");
header("Content-Disposition: attachment; filename=\"" . $_SESSION['Finzip'] . "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize("tmp_captcha/".$_SESSION['Finzip']));
//echo "tmp_captcha/".$_SESSION['Finzip'];
// Send file for download
ob_clean();
flush();
//readfile("tmp_captcha/".$_SESSION['Finzip']);exit;
if( readfile("tmp_captcha/".$_SESSION['Finzip']) )
{
/* Eliminamos el archivo extraido */
//$eliminar = $_SESSION['archivo_zip']->delete( PCLZIP_OPT_BY_NAME , "tmp_captcha/".$_SESSION['Finzip'] );
unlink("tmp_captcha/".$_SESSION['Finzip']);
//Escribimos el LOG
//se ha enviado bien
$_SESSION['fp'] = fopen('descargas.log', 'a');
fwrite($_SESSION['fp'], $_SESSION['Finzip']);fwrite($_SESSION['fp'], " ");
fwrite($_SESSION['fp'], $_SESSION['HOY']);fwrite($_SESSION['fp'], " ");
fwrite($_SESSION['fp'], $_SESSION['IP']);fwrite($_SESSION['fp'], " ");
fwrite($_SESSION['fp'], "\n");
fclose($_SESSION['fp']);
echo "<script type='text/javascript'>
window.close();
</script>";
}else{
//No ha podido leerse correctamente
//Escribimos en log de errores y cerramos ventana
$fp = fopen('errores.log', 'a');
fwrite($fp, $_SESSION['rutadescarga']);fwrite($fp, " ");
fwrite($fp, $_SESSION['Finzip']);fwrite($fp, " ");
fwrite($fp, " Error lectura zip extraido");fwrite($fp, " ");
fwrite($fp, $_SESSION['HOY']);fwrite($fp, " ");
fwrite($fp, $_SESSION['IP']);fwrite($fp, " ");
fwrite($fp, "\n");
fclose($fp);
flush();
echo "<script type='text/javascript'>
window.close();
</script>";
exit;
}
}
}else{
//El fichero NO está dentro del ZIP
//Escribimos en log de errores y cerramos ventana
$fp = fopen('errores.log', 'a');
fwrite($fp, $_SESSION['rutadescarga']);fwrite($fp, " ");
fwrite($fp, $_SESSION['Finzip']);fwrite($fp, " ");
fwrite($fp, " El fichero no está en el ZIP");fwrite($fp, " ");
fwrite($fp, $_SESSION['HOY']);fwrite($fp, " ");
fwrite($fp, $_SESSION['IP']);fwrite($fp, " ");
fwrite($fp, "\n");
fclose($fp);
flush();
echo "<script type='text/javascript'>
window.close();
</script>";
exit;
}
}//Segundo FOR
$_SESSION['rutadescarga'] = '';
$_SESSION['file']='';
$i='';
//************************* FIN CASO de Fichero ZIP *********************************//
//Descartes.....
// Send file for download
/* if ($stream = fopen($array['content'], 'rb')){
while(!feof($stream) && connection_status() == 0){
//reset time limit for big files
set_time_limit(0);
if (!fread($stream, 1024*8))
{
// La descarga no ha podido realizarse.....
//escribimos en el log de errores
$fp = fopen('errores.log', 'a');
fwrite($fp, $_SESSION['file_real']);fwrite($fp, " ");
fwrite($fp, $pais);fwrite($fp, " ");
fwrite($fp, $ciudad);fwrite($fp, " ");
fwrite($fp, $_SESSION['hoy']);fwrite($fp, " ");
fwrite($fp, $ip);fwrite($fp, " ");
fwrite($fp, $ip);fwrite($fp, "Error lectura del fichero");
fwrite($fp, "\n");
fclose($fp);
header ("Location: http://sitar.aragon.es/datosdescarga/404.htm");
}
print(fread($stream,1024*8));
// si no ha habido error escribimos en el log de descarga
$fp = fopen('descargas.log', 'a');
fwrite($fp, $file);fwrite($fp, " ");
fwrite($fp, $_SESSION['hoy']);fwrite($fp, " ");
fwrite($fp, $ciudad);fwrite($fp, " ");
fwrite($fp, $_SESSION['hoy']);fwrite($fp, " ");
fwrite($fp, $ip);fwrite($fp, " ");
fwrite($fp, "\n");
fclose($fp);
flush();
}
fclose($stream);
}*/
?>