fspath.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. class Kint_Parsers_FsPath extends kintParser
  3. {
  4. protected function _parse( & $variable )
  5. {
  6. if ( !KINT_PHP53
  7. || !is_string( $variable )
  8. || strlen( $variable ) > 2048
  9. || preg_match( '[[:?<>"*|]]', $variable )
  10. || !@is_readable( $variable ) # f@#! PHP and its random warnings
  11. ) return false;
  12. try {
  13. $fileInfo = new SplFileInfo( $variable );
  14. $flags = array();
  15. $perms = $fileInfo->getPerms();
  16. if ( ( $perms & 0xC000 ) === 0xC000 ) {
  17. $type = 'File socket';
  18. $flags[] = 's';
  19. } elseif ( ( $perms & 0xA000 ) === 0xA000 ) {
  20. $type = 'File symlink';
  21. $flags[] = 'l';
  22. } elseif ( ( $perms & 0x8000 ) === 0x8000 ) {
  23. $type = 'File';
  24. $flags[] = '-';
  25. } elseif ( ( $perms & 0x6000 ) === 0x6000 ) {
  26. $type = 'Block special file';
  27. $flags[] = 'b';
  28. } elseif ( ( $perms & 0x4000 ) === 0x4000 ) {
  29. $type = 'Directory';
  30. $flags[] = 'd';
  31. } elseif ( ( $perms & 0x2000 ) === 0x2000 ) {
  32. $type = 'Character special file';
  33. $flags[] = 'c';
  34. } elseif ( ( $perms & 0x1000 ) === 0x1000 ) {
  35. $type = 'FIFO pipe file';
  36. $flags[] = 'p';
  37. } else {
  38. $type = 'Unknown file';
  39. $flags[] = 'u';
  40. }
  41. // owner
  42. $flags[] = ( ( $perms & 0x0100 ) ? 'r' : '-' );
  43. $flags[] = ( ( $perms & 0x0080 ) ? 'w' : '-' );
  44. $flags[] = ( ( $perms & 0x0040 ) ? ( ( $perms & 0x0800 ) ? 's' : 'x' ) : ( ( $perms & 0x0800 ) ? 'S' : '-' ) );
  45. // group
  46. $flags[] = ( ( $perms & 0x0020 ) ? 'r' : '-' );
  47. $flags[] = ( ( $perms & 0x0010 ) ? 'w' : '-' );
  48. $flags[] = ( ( $perms & 0x0008 ) ? ( ( $perms & 0x0400 ) ? 's' : 'x' ) : ( ( $perms & 0x0400 ) ? 'S' : '-' ) );
  49. // world
  50. $flags[] = ( ( $perms & 0x0004 ) ? 'r' : '-' );
  51. $flags[] = ( ( $perms & 0x0002 ) ? 'w' : '-' );
  52. $flags[] = ( ( $perms & 0x0001 ) ? ( ( $perms & 0x0200 ) ? 't' : 'x' ) : ( ( $perms & 0x0200 ) ? 'T' : '-' ) );
  53. $this->type = $type;
  54. $this->size = sprintf( '%.2fK', $fileInfo->getSize() / 1024 );
  55. $this->value = implode( $flags );
  56. } catch ( Exception $e ) {
  57. return false;
  58. }
  59. }
  60. }