local.inc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * The local connection class for copying files as the httpd user.
  4. */
  5. class FileTransferLocal extends FileTransfer implements FileTransferChmodInterface {
  6. function connect() {
  7. // No-op
  8. }
  9. static function factory($jail, $settings) {
  10. return new FileTransferLocal($jail);
  11. }
  12. protected function copyFileJailed($source, $destination) {
  13. if (@!copy($source, $destination)) {
  14. throw new FileTransferException('Cannot copy %source to %destination.', NULL, array('%source' => $source, '%destination' => $destination));
  15. }
  16. }
  17. protected function createDirectoryJailed($directory) {
  18. if (!is_dir($directory) && @!mkdir($directory, 0777, TRUE)) {
  19. throw new FileTransferException('Cannot create directory %directory.', NULL, array('%directory' => $directory));
  20. }
  21. }
  22. protected function removeDirectoryJailed($directory) {
  23. if (!is_dir($directory)) {
  24. // Programmer error assertion, not something we expect users to see.
  25. throw new FileTransferException('removeDirectoryJailed() called with a path (%directory) that is not a directory.', NULL, array('%directory' => $directory));
  26. }
  27. foreach (new RecursiveIteratorIterator(new SkipDotsRecursiveDirectoryIterator($directory), RecursiveIteratorIterator::CHILD_FIRST) as $filename => $file) {
  28. if ($file->isDir()) {
  29. if (@!drupal_rmdir($filename)) {
  30. throw new FileTransferException('Cannot remove directory %directory.', NULL, array('%directory' => $filename));
  31. }
  32. }
  33. elseif ($file->isFile()) {
  34. if (@!drupal_unlink($filename)) {
  35. throw new FileTransferException('Cannot remove file %file.', NULL, array('%file' => $filename));
  36. }
  37. }
  38. }
  39. if (@!drupal_rmdir($directory)) {
  40. throw new FileTransferException('Cannot remove directory %directory.', NULL, array('%directory' => $directory));
  41. }
  42. }
  43. protected function removeFileJailed($file) {
  44. if (@!drupal_unlink($file)) {
  45. throw new FileTransferException('Cannot remove file %file.', NULL, array('%file' => $file));
  46. }
  47. }
  48. public function isDirectory($path) {
  49. return is_dir($path);
  50. }
  51. public function isFile($path) {
  52. return is_file($path);
  53. }
  54. public function chmodJailed($path, $mode, $recursive) {
  55. if ($recursive && is_dir($path)) {
  56. foreach (new RecursiveIteratorIterator(new SkipDotsRecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST) as $filename => $file) {
  57. if (@!chmod($filename, $mode)) {
  58. throw new FileTransferException('Cannot chmod %path.', NULL, array('%path' => $filename));
  59. }
  60. }
  61. }
  62. elseif (@!chmod($path, $mode)) {
  63. throw new FileTransferException('Cannot chmod %path.', NULL, array('%path' => $path));
  64. }
  65. }
  66. }