ChmodLib.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * @access static Chmod::convertFromOctal(mode, leadingZero)
  21. * 2009-07-26 ms
  22. */
  23. public static function convertFromOctal($mode, $leadingZero = false) {
  24. $res = (String)substr(sprintf('%o', $mode), -4);
  25. if ($leadingZero===true) {
  26. $res = '0'.$res;
  27. }
  28. return $res;
  29. }
  30. /**
  31. * from INT or STRING with or without leading 0 -> Octal 0xxx
  32. * @access static Chmod::converttoOctal(mode)
  33. * 2009-07-26 ms
  34. */
  35. public static function convertToOctal($mode) {
  36. return intval((string)$mode, 8);
  37. }
  38. /*** set/get modes ***/
  39. public function setUser($read, $write, $execute) {
  40. $this->modes['user'] = $this->setMode($read, $write, $execute);
  41. }
  42. public function setGroup($read, $write, $execute) {
  43. $this->modes['group'] = $this->setMode($read, $write, $execute);
  44. }
  45. public function setOther($read, $write, $execute) {
  46. $this->modes['other'] = $this->setMode($read, $write, $execute);
  47. }
  48. /**
  49. * get mode as octal value or
  50. * @param options
  51. * - string: string/int/symbolic
  52. * 2010-06-21 ms
  53. */
  54. public function getMode($options = array()) {
  55. $mode = (string)($this->modes['user'] . $this->modes['group'] . $this->modes['other']);
  56. if (!empty($options['type'])) {
  57. if ($options['type'] == 'string') {
  58. return $mode;
  59. } elseif ($options['type'] == 'int') {
  60. return (int)$mode;
  61. } elseif ($options['type'] == 'symbolic') {
  62. $mode = $this->symbol($this->modes['user']).$this->symbol($this->modes['group']).$this->symbol($this->modes['other']);
  63. return $mode;
  64. }
  65. }
  66. return intval($mode, 8);
  67. }
  68. /**
  69. * full table with all rights
  70. * //TODO
  71. * 2010-06-21 ms
  72. */
  73. public function table() {
  74. $res = array();
  75. return $res;
  76. }
  77. /**
  78. * get symbol for
  79. * read(4) = 'r', write(2) = 'w', execute(1) = 'x'
  80. * e.g: 4 for = r--
  81. * 2010-06-21 ms
  82. */
  83. protected function symbol($mode) {
  84. $res = '---';
  85. if ($mode == 7) {
  86. $res = 'rwx';
  87. } elseif ($mode == 6) {
  88. $res = 'rw-';
  89. } elseif ($mode == 5) {
  90. $res = 'r-x';
  91. } elseif ($mode == 4) {
  92. $res = 'r--';
  93. } elseif ($mode == 3) {
  94. $res = '-wx';
  95. } elseif ($mode == 2) {
  96. $res = '-w-';
  97. } elseif ($mode == 1) {
  98. $res = '--x';
  99. }
  100. return $res;
  101. }
  102. protected function setMode($r, $w, $e) {
  103. $mode = 0;
  104. if ($r) $mode+=4;
  105. if ($w) $mode+=2;
  106. if ($e) $mode+=1;
  107. return $mode;
  108. }
  109. }