PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/chmod/chmod.php

http://htsh.googlecode.com/
PHP | 47 lines | 43 code | 4 blank | 0 comment | 21 complexity | 0b336f6dc14328ff746540709e7f7506 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. function htsh_chmod($args) {
  3. if (isset($args['params'][0]) && isset($args['params'][1])) {
  4. $mode = intval($args['params'][0], 8);
  5. if (!$mode) return array('result' => 'Invalid mode.');
  6. foreach (array_slice($args['params'], 1) as $param) {
  7. $files = glob($param);
  8. foreach ($files as $file) {
  9. if (in_array('-R', $args['options']) || in_array('--recursive', $args['options'])) {
  10. if (!chmod_r($file, $mode)) {
  11. return array('result' => "Error: I could not chmod {$file}.");
  12. }
  13. }
  14. if (!chmod($file, $mode)) {
  15. return array('result' => "Error: I could not chmod {$file}.");
  16. }
  17. }
  18. }
  19. return array('result' => '');
  20. } elseif (in_array('-h', $args['options']) || in_array('--help', $args['options'])) {
  21. return array('result' => "chmod [OPTION]... OCTAL-MODE FILE...\nChange the mode of each FILE to OCTAL-MODE.\n\n -R, --recursive\tchange files and directories recursively");
  22. } else {
  23. return array('result' => 'chmod --help for help');
  24. }
  25. }
  26. function chmod_r($path, $mode) {
  27. if (!is_dir($path)) return chmod($path, $mode);
  28. $dh = opendir($path);
  29. while ($file = readdir($dh)) {
  30. if ($file != '.' && $file != '..') {
  31. $fullpath = $path . '/' . $file;
  32. if (!is_dir($fullpath)) {
  33. if (!chmod($fullpath, $mode)) return false;
  34. } else {
  35. if (!chmod_r($fullpath, $mode)) return false;
  36. }
  37. }
  38. }
  39. closedir($dh);
  40. if (chmod($path, $filemode)) return true;
  41. else return false;
  42. }
  43. ?>