Guest User

mkperms

a guest
Jun 15th, 2010
3,006
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. function mkperms($perms, $return_as_string = false, $filename = '') {
  3.     $perms = explode(',', $perms);
  4.     $generated = array('u'=>array(),'g'=>array(),'o'=>array());
  5.     if(!empty($filename)) {
  6.         $fperms = substr(decoct(fileperms($filename)), 3); // Credits to jchris dot fillionr at kitware dot com
  7.        // Fill array $generated
  8.         $fperms = str_split($fperms);
  9.         $fperms['u'] = $fperms[0]; unset($fperms[0]);
  10.         $fperms['g'] = $fperms[1]; unset($fperms[1]);
  11.         $fperms['o'] = $fperms[2]; unset($fperms[2]);
  12.         foreach($fperms as $key=>$fperm) {
  13.             if($fperm >= 4) {
  14.                $generated[$key]['r'] = true;
  15.                $fperm -= 4;
  16.             }
  17.             if($fperm >= 2) {
  18.                $generated[$key]['w'] = true;
  19.                $fperm -= 2;
  20.             }
  21.             if($fperm >= 1) {
  22.                $generated[$key]['x'] = true;
  23.                $fperm--;
  24.             }
  25.         }
  26.     }
  27.     foreach($perms as $perm) {
  28.          if(!preg_match('#^([ugo]*)([\+=-])([rwx]+|[\-])$#i', $perm, $matches)) {
  29.              trigger_error('Wrong input format for mkperms'); return 0644;
  30.              // Wrong format => generate default
  31.          }
  32.          $targets = str_split($matches[1]);
  33.          $addrem = $matches[2];
  34.          $perms_ = str_split($matches[3]);
  35.          $fromTheLoop = 0; // To make sure we clear it only once for direct affectation
  36.          foreach($targets as $target) {
  37.                  foreach($perms_ as $perms__) {
  38.                      if($addrem == '=') {
  39.                          if(!$fromTheLoop) {
  40.                              unset($generated[$target]['r']);
  41.                              unset($generated[$target]['w']);
  42.                              unset($generated[$target]['x']);
  43.                          }
  44.                          $fromTheLoop++;
  45.                          $addrem = '+';
  46.                      }
  47.                      if($perms__ == '-') {
  48.                          unset($generated[$target]['r']);
  49.                          unset($generated[$target]['w']);
  50.                          unset($generated[$target]['x']);
  51.                      } else {
  52.                          if($addrem == '+') {
  53.                              $generated[$target][$perms__] = true;
  54.                          } elseif($addrem == '-') {
  55.                              unset($generated[$target][$perms__]);
  56.                          } elseif($addrem == '=') {
  57.                              
  58.                          }
  59.                      }
  60.                  }
  61.          }
  62.     }
  63.     $generated_chars    = array(0, 0, 0);
  64.     $corresponding      = array('u'=>0, 'g'=>1, 'o'=>2);
  65.     $correspondingperms = array('r'=>4, 'w'=>2, 'x'=>1);
  66.  
  67.     foreach($generated as $key=>$generated_) {
  68.         foreach($generated_ as $generated__=>$useless) {
  69.             $generated_chars[$corresponding[$key]] += $correspondingperms[$generated__];
  70.         }
  71.     }
  72.     if($return_as_string) return implode($generated_chars);
  73.  else return base_convert(implode($generated_chars), 8, 10);
  74. }
  75. ?>
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×