GlisWeb framework
_filesystem.tools.php
Vai alla documentazione di questo file.
1 <?php
2 
59  // definizione delle costanti della libreria
60  define( 'READ_FILE_AS_ARRAY' , 'READ_ARRAY' );
61  define( 'READ_FILE_AS_STRING' , 'READ_STRING' );
62  define( 'WRITE_FILE_OVERWRITE' , 'w+' );
63  define( 'WRITE_FILE_APPEND' , 'a+' );
64 
65  // SDF-CGDL
85  function writeToFile( $t , $f , &$e = NULL ) {
86 
87  $r = NULL;
88 
90 
91  if( $h ) {
92 
93  $r = fwrite( $h , $t );
94  closeFile( $h );
95 
96  } else {
97 
98  $m = 'impossibile scrivere su ' . $f;
99  $e .= $m . PHP_EOL;
100  error_log( $m );
101  $r = false;
102 
103  }
104 
105  return $r;
106 
107  }
108 
109  //SDF-CGDL
125  function appendToFile( $t , $f , &$e = NULL ) {
126 
127  $r = NULL;
128 
130 
131  if( $h ) {
132 
133  $r = fwrite( $h , $t );
134  closeFile( $h );
135 
136  } else {
137 
138  $m = 'impossibile scrivere su ' . $f;
139  $e .= $m . PHP_EOL;
140  error_log( $m );
141  $r = false;
142 
143  }
144 
145  return $r;
146 
147  }
148 
165  function openFile( $f , $m = WRITE_FILE_APPEND , &$e = NULL ) {
166 
167  // path completo
168  fullPath( $f );
169 
170  // verifico il percorso
171  checkFolder( dirname( $f ), $e );
172 
173  // pertura del file
174  $r = @fopen( $f , $m );
175 
176  // restituzione del risultato
177  return $r;
178 
179  }
180 
189  function closeFile( $h ) {
190 
191  return fclose( $h );
192 
193  }
194 
203  function deleteFile( $f, &$e = NULL ) {
204 
205  // path completo
206  fullPath( $f );
207 
208  // cancellazione del file
209  if( file_exists( $f ) && is_writeable( $f ) ) {
210  $r = @unlink( $f );
211  if( $r === false ) {
212  $m = 'errore nella cancellazione di ' . $f;
213  $e .= $m . PHP_EOL;
214  error_log( $m );
215  }
216  } else {
217  $m = 'impossibile cancellare di ' . $f;
218  $e .= $m . PHP_EOL;
219  error_log( $m );
220  $r = false;
221  }
222 
223  // true o false
224  return $r;
225 
226  }
227 
234  function deleteDir( $f ) {
235 
236  // path completo
237  fullPath( $f );
238 
239  // cancellazione del file
240  if( file_exists( $f ) && is_writeable( $f ) ) {
241  $r = rmdir( $f );
242  if( $r === false ) { error_log( 'errore nella cancellazione di ' . $f ); }
243  } else {
244  error_log( 'impossibile cancellare ' . $f );
245  $r = false;
246  }
247 
248  // true o false
249  return $r;
250 
251  }
252 
259  function recursiveDelete( $f ) {
260 
261  // path completo
262  fullPath( $f );
263 
264  // elenco dei file
265  $files = new RecursiveIteratorIterator(
266  new RecursiveDirectoryIterator( $f, RecursiveDirectoryIterator::SKIP_DOTS ),
267  RecursiveIteratorIterator::CHILD_FIRST
268  );
269 
270  // rimozione dei file
271  foreach( $files as $fileinfo ) {
272  $todo = ( $fileinfo->isDir() ) ? 'rmdir' : 'unlink';
273  $todo( $fileinfo->getRealPath() );
274  }
275 
276  // rimozione della cartella radice
277  // TODO questa cosa metterla come opzione
278  rmdir( $f );
279 
280  }
281 
290  function getFileSize( $f ) {
291 
292  // path completo
293  fullPath( $f );
294 
295  // restituisco la dimensione del file in bytes o false in caso di fallimento
296  return @filesize( $f );
297 
298  }
299 
308 
309  // path completo
310  fullPath( $f );
311 
312  if( file_exists( $f ) && is_readable( $f ) ) {
313 
314  switch( $m ) {
315 
316  case READ_FILE_AS_ARRAY:
317  return file( $f );
318  break;
319  case READ_FILE_AS_STRING:
320  return file_get_contents( $f );
321  break;
322  default:
323  return false;
324  break;
325 
326  }
327 
328  } else {
329 
330  return false;
331 
332  }
333 
334  }
335 
341  function readStringFromFile( $f ) {
342 
344 
345  }
346 
357  function checkFolder( $p , &$e = NULL ) {
358 
359  $f = DIRECTORY_BASE;
360 
361  shortPath( $p );
362 
363  $p = rtrim( $p , '/' );
364 
365  $passi = explode( '/' , $p );
366 
367  foreach( $passi as $passo ) {
368 
369  $f .= $passo . '/';
370 
371  if( ! is_dir( $f ) ) {
372 
373  if( @mkdir( $f ) ) {
374 
375  chmod( $f , 0775 );
376 
377  } else {
378 
379  $m = 'impossibile creare ' . $f;
380  $e .= $m . PHP_EOL;
381  error_log( $m );
382 
383  }
384 
385  }
386 
387  }
388 
389  return true;
390 
391  }
392 
398  function checkFile( $f ) {
399 
400  fullPath( $f );
401  checkFolder( dirname( $f ) );
402  fclose( fopen( $f, 'a+' ) );
403 
404  }
405 
411  function getFilteredFileList( $d , $f = '*' ) {
412 
413  fullPath( $d );
414 
415  $a = glob( $d . $f , GLOB_BRACE );
416 
417  $r = array();
418 
419  foreach( $a as $t ) {
420  if( is_file( $t ) ) {
421  shortPath( $t );
422  $r[] = $t;
423  }
424  }
425 
426  return $r;
427 
428  }
429 
435  function getFileList( $d, $full = false ) {
436 
437  $t = array();
438 
439  fullPath( $d );
440 
441  foreach( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $d, FilesystemIterator::SKIP_DOTS ), RecursiveIteratorIterator::CHILD_FIRST ) as $f ) {
442  if( $f->isFile() ) {
443  $t[] = ( $full === true ) ? $f->getRealPath() : $f->getFileName();
444  }
445  }
446 
447  return $t;
448 
449  }
450 
456  function getDirList( $d, $full = false ) {
457 
458  $t = array();
459 
460  fullPath( $d );
461 
462  foreach( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $d, FilesystemIterator::SKIP_DOTS ), RecursiveIteratorIterator::CHILD_FIRST ) as $f ) {
463  if( $f->isDir() ) {
464  $t[] = ( $full === true ) ? $f->getRealPath() : $f->getFileName();
465  }
466  }
467 
468  return $t;
469 
470  }
471 
479  function getDirSize( $d ) {
480 
481  $t = 0;
482 
483  fullPath( $d );
484 
485  if( is_dir( $d ) ) {
486  foreach( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $d ) ) as $f ) {
487  $t += $f->getSize();
488  }
489  } else {
490  $t = getSize( $d );
491  }
492 
493  return $t;
494 
495  }
496 
502  function getRecursiveDirList( $d ) {
503 
504  fullPath( $d );
505 
506  $r = array();
507 
508  foreach( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $d ) ) as $f ) {
509 
510  if( $f->isDir() )
511  $r[] = $f->getRealPath();
512 
513  }
514 
515  return $r;
516 
517  }
518 
529  function getSize( $f ) {
530 
531  fullPath( $f );
532 
533  return @filesize( $f );
534 
535  }
536 
537  //SDF-CGDL
548  function getFileExtension( $f ) {
549 
550  return substr( basename( $f ) , strrpos( basename( $f ) , '.' ) + 1 );
551 
552  }
553 
559  function moveFile( $f1 , $f2 ) {
560 
561  checkFolder( dirname( $f1 ) );
562 
563  checkFolder( dirname( $f2 ) );
564 
565  return rename( DIRECTORY_BASE . $f1 , DIRECTORY_BASE . $f2 );
566 
567  }
568 
574  function copyFile( $f1 , $f2 ) {
575 
576  fullPath( $f2 );
577  checkFolder( dirname( $f2 ) );
578 
579  if( filter_var( $f1, FILTER_VALIDATE_URL ) ) {
580 
581  $ch = curl_init();
582  curl_setopt($ch, CURLOPT_URL, $f1);
583  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
584  $data = curl_exec ($ch);
585  $error = curl_error($ch);
586  curl_close($ch);
587 
588  $h = fopen($f2, 'w+');
589  fputs($h, $data);
590  fclose($h);
591 
592  return ( ( empty( $error ) && file_exists( $f2 ) ) ? true : false );
593 
594  } else {
595 
596  fullPath( $f1 );
597  checkFolder( dirname( $f1 ) );
598 
599  return copy( $f1, $f2 );
600 
601  }
602 
603  }
604 
610  function globRecursive( $path , $find ) {
611 
612  $r = array();
613 
614  $dh = @opendir( $path );
615 
616  if( $dh ) {
617  while( ( $file = readdir( $dh ) ) !== false ) {
618 
619  if( substr( $file, 0, 1) == '.' ) continue;
620 
621  $rfile = "{$path}/{$file}";
622 
623  if ( is_dir( $rfile ) ) {
624 
625  array_merge( $r , globRecursive( $rFile , $find ) );
626 
627  } else {
628 
629  if( fnmatch( $find, $file ) ) {
630 
631  $r[] = $file;
632 
633  }
634 
635  }
636 
637  }
638 
639  closedir( $dh );
640 
641  } else { $r = false; }
642 
643  return $r;
644 
645  }
646 
652  function findFileExtension( $f ) {
653 
654  $e = explode( '.', $f );
655  $a = array_reverse( $e );
656  return array_shift( $a );
657 
658  }
659 
665  function findFileType( $f ) {
666 
667  fullPath( $f );
668 
669  return mime_content_type( $f );
670 
671  }
672 
678  function fullPath( &$f ) {
679 
680  if( strpos( $f, DIRECTORY_BASE ) === false ) {
681  $f = DIRECTORY_BASE . $f;
682  }
683 
684  return $f;
685 
686  }
687 
693  function shortPath( &$f ) {
694 
695  $f = str_replace( DIRECTORY_BASE, NULL, $f );
696 
697  return $f;
698 
699  }
700 
706  function emptyDir( $d ) {
707 
708  $t = 0;
709 
710  fullPath( $d );
711 
712  foreach( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $d, FilesystemIterator::SKIP_DOTS ), RecursiveIteratorIterator::CHILD_FIRST ) as $f ) {
713 
714  if( $f->isFile() ) {
715  deleteFile( $f->getRealPath() );
716  } elseif( $f->isDir() ) {
717  deleteDir( $f->getRealPath() );
718  }
719 
720  }
721 
722  return $t;
723 
724  }
725 
731  function fileExists( $f ) {
732 
733  if( filter_var( $f, FILTER_VALIDATE_URL ) ) {
734  $ch = curl_init( $f);
735  curl_setopt( $ch, CURLOPT_NOBODY, true );
736  curl_exec( $ch );
737  $code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
738  $status = ( $code == 200 ) ? true : false;
739  curl_close( $ch );
740  return $status;
741  } else {
742  fullPath( $f );
743  return file_exists( $f );
744  }
745 
746  }
747 
753  function fileCachedExists( $m, $f, $t = MEMCACHE_DEFAULT_TTL, &$e = array() ) {
754 
755  // calcolo la chiave della query
756  $k = md5( $f );
757 
758  // cerco il valore in cache
759  $r = memcacheRead( $m, $k );
760 
761  // se il valore non è stato trovato
762  if( empty( $r ) || $r === false ) {
763  $r = fileExists( $f );
764  memcacheWrite( $m, $k, serialize( $r ), $t );
765  } else {
766  $r = unserialize( $r );
767  }
768 
769  // restituisco il risultato
770  return $r;
771 
772  }
773 
779  function fileGetCachedContents( $m, $f, $t = MEMCACHE_DEFAULT_TTL, &$e = array() ) {
780 
781  // calcolo la chiave della query
782  $k = md5( $f );
783 
784  // cerco il valore in cache
785  $r = memcacheRead( $m, $k );
786 
787  // se il valore non è stato trovato
788  if( empty( $r ) || $r === false ) {
789  $r = file_get_contents( $f );
790  memcacheWrite( $m, $k, serialize( $r ), $t );
791  } else {
792  $r = unserialize( $r );
793  }
794 
795  // restituisco il risultato
796  return $r;
797 
798  }
799 
805  function fileModifiedTime( $f ) {
806 
807  if( filter_var( $f, FILTER_VALIDATE_URL ) ) {
808  $curl = curl_init();
809  curl_setopt( $curl , CURLOPT_URL , $f );
810  curl_setopt( $curl , CURLOPT_HEADER , 1 );
811  curl_setopt( $curl , CURLOPT_NOBODY , 1 );
812  curl_setopt( $curl , CURLOPT_RETURNTRANSFER , 1 );
813  curl_setopt( $curl , CURLOPT_TIMEOUT , 5 );
814  $curl_result = curl_exec( $curl );
815 
816  if ( $curl_result !== false && strpos( $curl_result, '200 OK' ) !== false ) {
817  $headers = explode( "\n" , $curl_result );
818  $last_modified = explode( 'Last-Modified: ' , $headers[3] );
819  return strtotime( $last_modified[1] );
820  } else {
821  return false;
822  }
823 
824  } else {
825 
826  fullPath( $f );
827  return filemtime( $f );
828 
829  }
830 
831  }
832 
838  function file2array( $f ) {
840  }
841 
847  function array2file( $f, $a ) {
848  return writeToFile( trim( implode( PHP_EOL, $a ) ), $f );
849  }
850 
851 ?>
fileModifiedTime( $f)
const READ_FILE_AS_ARRAY
const READ_FILE_AS_STRING
const WRITE_FILE_APPEND
appendToFile( $t, $f, &$e=NULL)
aggiunge una stringa a un file
file2array( $f)
moveFile( $f1, $f2)
deleteDir( $f)
readStringFromFile( $f)
$p['ricerca']
$f
Definition: _filesystem.php:21
if(!empty($_REQUEST['id'])) $d
checkFile( $f)
getFilteredFileList( $d, $f=' *')
findFileType( $f)
$file
Definition: _osm.php:32
recursiveDelete( $f)
$a
Definition: _slack.php:21
fileGetCachedContents( $m, $f, $t=MEMCACHE_DEFAULT_TTL, &$e=array())
writeToFile( $t, $f, &$e=NULL)
scrive una stringa su un file
getFileList( $d, $full=false)
findFileExtension( $f)
getFileSize( $f)
restituisce la dimensione di un file
memcacheRead( $conn, $key)
array2file( $f, $a)
getSize( $f)
calcola la dimensione di un file in byte
checkFolder( $p, &$e=NULL)
verifica l&#39;esistenza di un path di directory creando quelle mancanti
fileExists( $f)
$r
Definition: _osm.php:25
closeFile( $h)
chiude un puntatore a un file
const WRITE_FILE_OVERWRITE
$e
Definition: _slack.php:121
openFile( $f, $m=WRITE_FILE_APPEND, &$e=NULL)
apre un handler a un file
const DIRECTORY_BASE
Definition: _osm.php:3
readFromFile( $f, $m=READ_FILE_AS_ARRAY)
legge il contenuto di un file in una stringa o in un array di stringhe
globRecursive( $path, $find)
emptyDir( $d)
copyFile( $f1, $f2)
memcacheWrite( $conn, $key, $data, $ttl=MEMCACHE_DEFAULT_TTL, $seed=MEMCACHE_UNIQUE_SEED)
getFileExtension( $f)
recupera l&#39;estensione del file
const MEMCACHE_DEFAULT_TTL
Definition: _045.cache.php:16
if(! isset( $_REQUEST['__view__'][ $ct['view']['id']]['__extra__']['assegnato'])|| $_REQUEST['__view__'][ $ct['view']['id']]['__extra__']['assegnato']=='__me__') elseif($_REQUEST[ '__view__'][$ct[ 'view'][ 'id']][ '__extra__'][ 'assegnato']=='__nessuno__')
fileCachedExists( $m, $f, $t=MEMCACHE_DEFAULT_TTL, &$e=array())
getRecursiveDirList( $d)
shortPath(&$f)
getDirSize( $d)
calcola ricorsivamente lo spazio occupato da una directory
deleteFile( $f, &$e=NULL)
elimina un file
fullPath(&$f)
getDirList( $d, $full=false)