Last active
August 29, 2015 14:10
-
-
Save jesseschalken/05f2123d4454c44ca62c to your computer and use it in GitHub Desktop.
A PHP class to generate Unix symbolic permission notation (eg "-rw-r--r--") from lstat()/fileperms()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Perms { | |
/** @var int */ | |
private $mode; | |
/** @param string $path */ | |
function __construct($path) { | |
$this->mode = fileperms($path); | |
} | |
function symbolic() { | |
static $types = array( | |
015 => 'D', | |
014 => 's', | |
012 => 'l', | |
010 => '-', | |
006 => 'b', | |
004 => 'd', | |
002 => 'c', | |
001 => 'p', | |
); | |
return | |
$types[$this->bits(12, 4)] . | |
$this->perms(2, 'Ss') . | |
$this->perms(1, 'Ss') . | |
$this->perms(0, 'Tt'); | |
} | |
private function perms($i, $s) { | |
$r = '-r'; | |
$w = '-w'; | |
$x = $this->bits($i + 9) ? $s : '-x'; | |
return | |
$r[$this->bits($i * 3 + 2)] . | |
$w[$this->bits($i * 3 + 1)] . | |
$x[$this->bits($i * 3 + 0)]; | |
} | |
private function bits($i, $n = 1) { | |
return ($this->mode >> $i) & ~(~0 << $n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment