chmod_lib.php 2.6 KB

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