GlisWeb framework
_file.php
Vai alla documentazione di questo file.
1 <?php
2 
14  // inclusione del framework
15  require '../../_src/_config.php';
16 
17  // risposta
18  $reply = array();
19 
20  // log
21  logWrite( 'chiamata API file', 'uploader', LOG_DEBUG );
22 
23  // TODO verifico che l'utente possa caricare il file
24  if( true ) {
25 
26  // dati ricevuti dagli header della richiesta ajax
27  $fileName = $_SERVER['HTTP_X_FILE_NAME'];
28  $fileSize = $_SERVER['HTTP_X_FILE_SIZE'];
29  $chunkNumber = $_SERVER['HTTP_X_CHUNK_NUMBER'];
30  $totalChunks = $_SERVER['HTTP_X_CHUNK_TOTAL'];
31 
32  // log
33  logWrite( 'inizio caricamento file: ' . $fileName . ' chunk ' . $chunkNumber . '/' . $totalChunks, 'uploader', LOG_NOTICE );
34 
35  // nome del file scritto
36  $collisionCounter = 0;
37  $targetFolder = ( isset( $_SERVER['HTTP_X_TARGET_FOLDER'] ) ) ? $_SERVER['HTTP_X_TARGET_FOLDER'] : 'tmp/';
38  $targetRelativePath = DIRECTORY_BASE . $targetFolder;
39 
40  // controllo il percorso
41  checkFolder( $targetFolder );
42 
43  // gestione delle collisioni
44  do {
45 
46  $targetFileRelativePath = $targetRelativePath . $fileName;
47  $targetFileAbsolutePath = $targetFolder . $fileName;
48 
49  if( file_exists( $targetFileRelativePath ) ) {
50 
51  $collision = true;
52 
53  $arrayNomeFile = explode( '.', $fileName );
54  $estensione = array_pop( $arrayNomeFile );
55  $arrayNomeFile[] = $collisionCounter;
56  $arrayNomeFile[] = $estensione;
57  $fileName = implode( '.', $arrayNomeFile );
58 
59  $collisionCounter++;
60 
61  } else {
62 
63  $collision = false;
64 
65  }
66 
67  } while( $collision == true );
68 
69  // nome e percorso dei chunk
70  $estensioneChunk = '.part' . sprintf( '%04d' , $chunkNumber );
71  $targetFileRelativePathWithChunk = $targetFileRelativePath . $estensioneChunk;
72 
73  // apertura degli stream di input e di output
74  $input = fopen( 'php://input' , 'r' );
75  $output = fopen( $targetFileRelativePathWithChunk , 'w' );
76 
77  // scrittura dati
78  while( $data = fread( $input, 1024 ) ) {
79  fwrite( $output, $data );
80  }
81 
82  // chiusura degli stream di input e di output
83  fclose( $input );
84  fclose( $output );
85 
86  // dimensione del file scritto
87  $writeData = filesize( $targetFileRelativePathWithChunk );
88 
89  // se questo chunk era l'ultimo creo il file completo
90  if( $chunkNumber == $totalChunks ) {
91 
92  // costruisco la lista dei parziali
93  $arrayChunks = glob( $targetFileRelativePath . '.part*' );
94 
95  // apro il file base
96  $h = fopen( $targetFileRelativePath , "ab" );
97 
98  // riassemblo il file originale...
99  foreach( $arrayChunks as $chunk ) {
100 
101  // apro il chunk
102  $in = fopen( $chunk , "rb" );
103 
104  if( $in ) {
105  while( $buff = fread( $in, 1048576 ) ) {
106  fwrite( $h , $buff );
107  }
108  }
109 
110  fclose( $in );
111  unlink( $chunk );
112 
113  }
114 
115  // chiudo il file base
116  fclose( $h );
117 
118  }
119 
120  // risposta ajax
121  $reply['debug'] = 'on';
122  $reply['fileSize'] = $fileSize;
123  $reply['writtenData'] = $writeData;
124  $reply['fileUrl'] = $cf['site']['url'].$targetFileAbsolutePath;
125  $reply['filePath'] = $targetFileAbsolutePath;
126 
127 
128  }
129 
130  // invio risposta
131  buildJson( $reply );
132 
133 /*
134 
135 // TODO - RISCRIVERE, QUESTO E' UN FILE DEL VECCHIO STANDARD!!!
136 
137 
138  // dati ricevuti dagli header della richiesta ajax
139  $fileName = $_SERVER['HTTP_X_FILE_NAME'];
140  $fileSize = $_SERVER['HTTP_X_FILE_SIZE'];
141  $chunkNumber = $_SERVER['HTTP_X_CHUNK_NUMBER'];
142  $totalChunks = $_SERVER['HTTP_X_CHUNK_TOTAL'];
143 
144  // log
145  logFactory( "avvio upload file $fileName (dimensione: $fileSize)", 'upload', LOG_INFO );
146 
147  // nome del file scritto
148  $collisionCounter = 0;
149  $targetFolder = ( isset( $_SERVER['HTTP_X_TARGET_FOLDER'] ) ) ? $_SERVER['HTTP_X_TARGET_FOLDER'] : 'tmp/';
150  $targetRelativePath = DIRECTORY_BASE . $targetFolder;
151 
152  // controllo il percorso
153  checkFolder( $targetFolder );
154 
155  // gestione delle collisioni
156  do {
157 
158  $targetFileRelativePath = $targetRelativePath . $fileName;
159  $targetFileAbsolutePath = $targetFolder . $fileName;
160 
161  if( file_exists( $targetFileRelativePath ) ) {
162 
163  $collision = true;
164 
165  $arrayNomeFile = explode( '.', $fileName );
166  $estensione = array_pop( $arrayNomeFile );
167  $arrayNomeFile[] = $collisionCounter;
168  $arrayNomeFile[] = $estensione;
169  $fileName = implode( '.', $arrayNomeFile );
170 
171  $collisionCounter++;
172 
173  } else {
174 
175  $collision = false;
176 
177  }
178 
179  } while( $collision == true );
180 
181  // nome e percorso dei chunk
182  $estensioneChunk = '.part' . sprintf( '%04d' , $chunkNumber );
183  $targetFileRelativePathWithChunk = $targetFileRelativePath . $estensioneChunk;
184 
185  // apertura degli stream di input e di output
186  $input = fopen( 'php://input' , 'r' );
187  $output = fopen( $targetFileRelativePathWithChunk , 'w' );
188 
189  // scrittura dati
190  while( $data = fread( $input, 1024 ) ) {
191  fwrite( $output, $data );
192  }
193 
194  // chiusura degli stream di input e di output
195  fclose( $input );
196  fclose( $output );
197 
198  // dimensione del file scritto
199  $writeData = filesize( $targetFileRelativePathWithChunk );
200 
201  // se questo chunk era l'ultimo creo il file completo
202  if( $chunkNumber == $totalChunks ) {
203 
204  // costruisco la lista dei parziali
205  $arrayChunks = glob( $targetFileRelativePath . '.part*' );
206 
207  // apro il file base
208  $h = fopen( $targetFileRelativePath , "ab" );
209 
210  // riassemblo il file originale...
211  foreach( $arrayChunks as $chunk ) {
212 
213  // apro il chunk
214  $in = fopen( $chunk , "rb" );
215 
216  if( $in ) {
217  while( $buff = fread( $in, 1048576 ) ) {
218  fwrite( $h , $buff );
219  }
220  }
221 
222  fclose( $in );
223  unlink( $chunk );
224 
225  }
226 
227  // chiudo il file base
228  fclose( $h );
229 
230  }
231 
232  // risposta ajax
233  $reply['debug'] = 'on';
234  $reply['fileSize'] = $fileSize;
235  $reply['writtenData'] = $writeData;
236  $reply['fileUrl'] = $cf['site']['url'].$targetFileAbsolutePath;
237  $reply['filePath'] = $targetFileAbsolutePath;
238 
239  // scrivo la risposta ajax
240  echo json_encode( $reply );
241 
242 
243 */
244 
245 
246 
247 
248 
249 
250 /*
251 
252 
253 die();
254 
255  // pagina corrente; normalmente la pagina corrente viene ricavata normalmente dalla coda GET
256  $cf['folders']['page'] = 'admin';
257 
258  // livelli
259  $config['cartelle']['livelli'] = $cf['folders']['levels'] = '../../';
260  $config['cartelle']['cartellaLocal'] = "local/";
261  $config['generalia']['paginaCorrente'] = "admin_s";
262 
263  // percorso assoluto del sito
264  $cf['folders']['levelsNum'] = substr_count($cf['folders']['levels'],'../');
265  $cf['folders']['pathArray'] = explode( "/" , dirname( getenv( 'SCRIPT_FILENAME' ) ) );
266  $cf['folders']['absPath'] = implode( "/" ,
267  array_slice( $cf['folders']['pathArray'] , 0 , ( count( $cf['folders']['pathArray'] ) - $cf['folders']['levelsNum'] ) )
268  ) . '/';
269 
270 #die($cf['folders']['absPath']);
271 
272 #$h = fopen( './'.time().'.log' , 'w+' );
273 
274  require $cf['folders']['levels'] . 'local/config.php';
275 
276  // controllo permessi
277  // if( checkWritePermissions() ) {
278  if( true ) {
279 
280  // defaults
281  $reply = array(); // array per la risposta
282  $collision = true; // trigger per nomi di files duplicati
283  $counter = 0; // contatore per la deduplica dei nomi di files
284 
285  // debug
286  // echo '<pre>'.print_r($_REQUEST,true).'</pre>';
287 
288  // dati ricevuti dagli header della richiesta ajax
289  $fileName = $_SERVER['HTTP_X_FILE_NAME'];
290  $fileSize = $_SERVER['HTTP_X_FILE_SIZE'];
291  $token = $_SERVER['HTTP_X_ROW_TOKEN'];
292  $fieldName = $_SERVER['HTTP_X_ROW_FIELD'];
293  $chunkNumber = $_SERVER['HTTP_X_CHUNK_NUMBER'];
294  $totalChunks = $_SERVER['HTTP_X_CHUNK_TOTAL'];
295 
296  // recupero i dati dal db in base al token
297  $q = "SELECT * FROM _tokens_upload WHERE id = '".$token."'";
298  $r = safeQuery( $q , true );
299  $rsTk = mysql_fetch_assoc( $r );
300 
301 #fwrite( $h , $token . ' -> '.$q . PHP_EOL );
302 #fwrite( $h , 'scrittura su ' . $rsTk['path'] . PHP_EOL );
303 #fwrite( $h , 'risultato ' . print_r( $r , true ) . PHP_EOL );
304 #fwrite( $h , 'risultato fetchato ' . print_r( $rsTk , true ) . PHP_EOL );
305 
306  // gestione delle collisioni fra files (nomi duplicati)
307  while( $collision == true ) {
308 
309  if( ! empty( $rsTk['path'] ) ) {
310 
311  // nome custom del file / cartella di destinazione (percorso assoluto)
312  $isFileArr = explode( '.' , basename( $rsTk['path'] ) );
313  if( count($isFileArr)>1 ) {
314  $fileName = basename( $rsTk['path'] );
315  $targetFolder = dirname( $rsTk['path'] ) . '/';
316  } else {
317  $targetFolder = $rsTk['path'];
318  }
319 
320  // cartella di destinazione (percorso relativo)
321  $targetRelativePath = $cf['folders']['levels'] . $targetFolder;
322 
323  // definizione del file destinazione (percorso relativo)
324  $targetFileRelativePath = $targetRelativePath . $fileName;
325 
326  // definizione del file destinazione (percorso assoluto)
327  $targetFileAbsolutePath = $targetFolder . $fileName;
328 
329  // verifico se la cartella di destinazione esiste
330  checkFolder( $targetFolder );
331 
332  // test di duplicazione
333  if( file_exists( $targetFileRelativePath ) ) {
334  $estensione = findFileType( $fileName , true );
335  $fileName = str_replace('.'.$estensione,'_'.( $counter++ ).'.'.$estensione,$fileName);
336  } else {
337  $collision = false;
338  }
339 
340  } else {
341  $collision = false;
342  }
343 
344  }
345 
346  // creo l'estensione per il chunk
347  $estensioneChunk = '.part' . sprintf( '%04d' , $chunkNumber );
348  $targetFileRelativePathWithChunk = $targetFileRelativePath . $estensioneChunk;
349 
350  // log
351  // logFactory( "inizio upload di ".$targetFileRelativePathWithChunk." (".$fileSize." bytes -> ".writeByte($fileSize).")",'filesystem');
352 
353  // apertura degli stream di input e di output
354  $input = fopen( 'php://input' , 'r' );
355  $output = fopen( $targetFileRelativePathWithChunk , 'w' );
356 
357  // scrittura dati
358  while( $data = fread( $input, 1024 ) ) {
359 
360  fwrite( $output, $data );
361 
362  }
363 
364  // chiusura degli stream di input e di output
365  fclose( $input );
366  fclose( $output );
367 
368  // dimensione del file scritto
369  $writeData = filesize( $targetFileRelativePathWithChunk );
370 
371  // log
372  // logFactory( "fine upload di ".$targetFileRelativePathWithChunk." (".$fileSize." bytes -> ".writeByte($fileSize).")",'filesystem');
373 
374  // se questo era l'ultimo parziale di un file splittato devo riassemblarlo
375  if( $chunkNumber == $totalChunks ) {
376 
377  // costruisco la lista dei parziali
378  $arrayChunks = glob( $targetFileRelativePath . '.part*' );
379 
380  // log
381  // logFactory( "componenti trovati: " . implode( ', ' , $arrayChunks ) ,'filesystem');
382  // logFactory( "riassemblo su: " . $targetFileRelativePath ,'filesystem');
383 
384  // apro il file base
385  $h = fopen( $targetFileRelativePath , "ab" );
386 
387  // riassemblo il file originale...
388  foreach( $arrayChunks as $chunk ) {
389 
390  // apro il chunk
391  $in = fopen( $chunk , "rb" );
392  if( $in ) {
393  while( $buff = fread( $in, 1048576 ) ) {
394  fwrite( $h , $buff );
395  }
396  }
397  fclose( $in );
398  unlink( $chunk );
399 
400  }
401 
402  // chiudo il file base
403  fclose( $h );
404 
405  // inserimento dell'oggetto nel db se non presente
406  if( !isset( $rsTk['id_riga'] ) || $rsTk['id_riga'] == '' ) {
407 
408  $rsTk['id_riga'] = safeQuery( "INSERT INTO ".$rsTk['tabella_da_usare']." ( id , ".$fieldName." ) VALUES ( '' , '".$targetFileAbsolutePath."' )" );
409  $reply['insertedId'] = $rsTk['id_riga'];
410 
411  }
412 
413  // esecuzione macro sul file caricato
414  if( $rsTk['macro'] != '' ) {
415 
416  require $cf['folders']['levels'] . $rsTk['macro'];
417 
418  }
419 
420  // scrittura sul db
421  $q = "UPDATE ".$rsTk['tabella_da_usare']." SET ".$fieldName." = '".$targetFileAbsolutePath."' WHERE id = '".$rsTk['id_riga']."'";
422  safeQuery( $q );
423 
424  }
425 
426  // risposta ajax
427  $reply['debug'] = 'on';
428  $reply['token'] = $token;
429  $reply['fileName'] = $fileName;
430  $reply['fieldName'] = $fieldName;
431  $reply['fileSize'] = $fileSize;
432  $reply['targetFileRelativePath'] = $targetFileRelativePath;
433  $reply['targetFileAbsolutePath'] = $targetFileAbsolutePath;
434  $reply['writtenFileSize'] = $writeData;
435  $reply['query'] = $q;
436 
437  // scrivo la risposta ajax
438  echo json_encode( $reply );
439 
440  }
441 
442 */
443 
444 ?>
$input
Definition: progress.php:7
logWrite( $m, $f='site', $l=LOG_NOTICE, $d=DIRECTORY_LOG, $t=CURRENT_LOG_LEVEL, $s=SITE_STATUS)
scrive un messaggio nei log del sito
Definition: _log.utils.php:48
$reply
Definition: _file.php:18
$cf['ricerca']['template']
Definition: _030.common.php:7
checkFolder( $p, &$e=NULL)
verifica l&#39;esistenza di un path di directory creando quelle mancanti
const DIRECTORY_BASE
Definition: _osm.php:3
buildJson( $content, $encoding=ENCODING_UTF8, $headers=array())