ChmodLib.php 2.7 KB

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