ChmodLib.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. *
  4. * TODO: change names of convertFromOctal and convertToOctal
  5. *
  6. */
  7. /**
  8. * PHP5
  9. * u=user, g=group, o=other
  10. */
  11. class ChmodLib {
  12. //protected $dir;
  13. protected $modes = ['user' => 0, 'group' => 0, 'other' => 0];
  14. /*** calc octal ***/
  15. /**
  16. * From Octal 0xxx back to STRING with leading zero added on leading zero = true
  17. * e.g. 0777 => 0777, '755' => 0755
  18. *
  19. * @return string
  20. */
  21. public static function convertFromOctal($mode, $leadingZero = false) {
  22. $res = (string)substr(sprintf('%o', $mode), -4);
  23. if ($leadingZero === true) {
  24. $res = '0' . $res;
  25. }
  26. return $res;
  27. }
  28. /**
  29. * From INT or STRING with or without leading 0 -> Octal 0xxx
  30. *
  31. * @return int
  32. */
  33. public static function convertToOctal($mode) {
  34. return intval((string)$mode, 8);
  35. }
  36. /*** set/get modes ***/
  37. public function setUser($read, $write, $execute) {
  38. $this->modes['user'] = $this->setMode($read, $write, $execute);
  39. }
  40. public function setGroup($read, $write, $execute) {
  41. $this->modes['group'] = $this->setMode($read, $write, $execute);
  42. }
  43. public function setOther($read, $write, $execute) {
  44. $this->modes['other'] = $this->setMode($read, $write, $execute);
  45. }
  46. /**
  47. * Get mode as octal value or
  48. *
  49. * @param options
  50. * - string: string/int/symbolic
  51. * @return int Mode
  52. */
  53. public function getMode($options = []) {
  54. $mode = (string)($this->modes['user'] . $this->modes['group'] . $this->modes['other']);
  55. if (!empty($options['type'])) {
  56. if ($options['type'] === 'string') {
  57. return $mode;
  58. } elseif ($options['type'] === 'int') {
  59. return (int)$mode;
  60. } elseif ($options['type'] === 'symbolic') {
  61. $mode = $this->symbol($this->modes['user']) . $this->symbol($this->modes['group']) . $this->symbol($this->modes['other']);
  62. return $mode;
  63. }
  64. }
  65. return intval($mode, 8);
  66. }
  67. /**
  68. * Full table with all rights
  69. * //TODO
  70. */
  71. public function table() {
  72. $res = [];
  73. return $res;
  74. }
  75. /**
  76. * Get symbol for
  77. * read(4) = 'r', write(2) = 'w', execute(1) = 'x'
  78. * e.g: 4 for = r--
  79. *
  80. * @return string Symbol
  81. */
  82. protected function symbol($mode) {
  83. $res = '---';
  84. if ($mode == 7) {
  85. $res = 'rwx';
  86. } elseif ($mode == 6) {
  87. $res = 'rw-';
  88. } elseif ($mode == 5) {
  89. $res = 'r-x';
  90. } elseif ($mode == 4) {
  91. $res = 'r--';
  92. } elseif ($mode == 3) {
  93. $res = '-wx';
  94. } elseif ($mode == 2) {
  95. $res = '-w-';
  96. } elseif ($mode == 1) {
  97. $res = '--x';
  98. }
  99. return $res;
  100. }
  101. /**
  102. * ChmodLib::setMode()
  103. *
  104. * @param int $r
  105. * @param int $w
  106. * @param int $e
  107. * @return int
  108. */
  109. protected function setMode($r, $w, $e) {
  110. $mode = 0;
  111. if ($r) {
  112. $mode += 4;
  113. }
  114. if ($w) {
  115. $mode += 2;
  116. }
  117. if ($e) {
  118. $mode += 1;
  119. }
  120. return $mode;
  121. }
  122. }