Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. <?php
  2. class Permissions
  3. {
  4. public static function permStringToInt($PermString)
  5. {
  6. $strings = str_split($PermString);
  7. if (count($strings) == 9) {
  8. $chunks = array_chunk($strings, 3);
  9.  
  10. $return_text = "";
  11. foreach ($chunks as $groups) {
  12. $group_number = 0;
  13. foreach ($groups as $permission) {
  14. if (in_array($permission, array('r', 'w', 'x', '-'))) {
  15. if ($permission == 'r') {
  16. $group_number+=4;
  17. } else if ($permission == 'w') {
  18. $group_number+=2;
  19. } else if ($permission == 'x') {
  20. $group_number+=1;
  21. }
  22. } else {
  23. return "Invalid string!";
  24. }
  25. }
  26.  
  27. if ($group_number <= 7) {
  28. $return_text .= $group_number;
  29. } else {
  30. return "Invalid string!";
  31. }
  32. }
  33. } else {
  34. return "Invalid string length!";
  35. }
  36.  
  37. return $return_text;
  38. //throw new Exception('Not implemented');
  39. }
  40. }
  41.  
  42. // For test purposes only
  43. // echo Permissions::permStringToInt("rwxr-x-w-");
  44. echo Permissions::permStringToInt("rwxrwx---");
  45. // Should yield 752
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement