class-chrootlayer.php
154 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\Filesystem\Layer; |
| 4 | |
| 5 | use WordPress\ByteStream\ReadStream\ByteReadStream; |
| 6 | use WordPress\ByteStream\WriteStream\ByteWriteStream; |
| 7 | use WordPress\Filesystem\Filesystem; |
| 8 | use WordPress\Filesystem\LocalFilesystem; |
| 9 | |
| 10 | use function WordPress\Filesystem\wp_join_unix_paths; |
| 11 | use function WordPress\Filesystem\wp_unix_path_resolve_dots; |
| 12 | |
| 13 | /** |
| 14 | * A filesystem wrapper that chroot's the filesystem to a specific path. |
| 15 | */ |
| 16 | class ChrootLayer extends Layer { |
| 17 | |
| 18 | /** |
| 19 | * @var string |
| 20 | */ |
| 21 | private $chroot; |
| 22 | |
| 23 | /** |
| 24 | * @param Filesystem $fs The filesystem to chroot. |
| 25 | * @param string $chroot The root path to chroot to. |
| 26 | */ |
| 27 | public function __construct( Filesystem $fs, $chroot ) { |
| 28 | parent::__construct( $fs ); |
| 29 | $this->chroot = $this->forward_slashes_on_local_filesystem_on_windows( |
| 30 | rtrim( $chroot, '/' ) . '/' |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Transforms an absolute path or relative path to be contained within a chroot. |
| 36 | * |
| 37 | * @param string $path The path to normalize. |
| 38 | * |
| 39 | * @return string The normalized path. |
| 40 | */ |
| 41 | public function chrooted_path( $path ) { |
| 42 | $path = $this->forward_slashes_on_local_filesystem_on_windows( $path ); |
| 43 | return wp_join_unix_paths( |
| 44 | $this->chroot, |
| 45 | wp_unix_path_resolve_dots( $path ) |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Make sure we use forward slashes when addressing a local filesystem on |
| 51 | * a Windows host. This allows all the wp_unix_* functions to work. |
| 52 | * |
| 53 | * This is an abstraction leak! ChrootLayer is generic but it makes choices |
| 54 | * on behalf of LocalFilesystem. |
| 55 | * |
| 56 | * @TODO: Reorganize the code to avoid this. Otherwise, every Layer class |
| 57 | * will need to implement this logic. That's error-prone. |
| 58 | * |
| 59 | * @param string $path The path to normalize. |
| 60 | * |
| 61 | * @return string The normalized path. |
| 62 | */ |
| 63 | private function forward_slashes_on_local_filesystem_on_windows( $path ) { |
| 64 | if ( '\\' === DIRECTORY_SEPARATOR && $this->fs instanceof LocalFilesystem ) { |
| 65 | return str_replace( '\\', '/', $path ); |
| 66 | } |
| 67 | return $path; |
| 68 | } |
| 69 | |
| 70 | public function exists( $path ) { |
| 71 | $path = $this->chrooted_path( $path ); |
| 72 | |
| 73 | return $this->fs->exists( $path ); |
| 74 | } |
| 75 | |
| 76 | public function is_file( $path ) { |
| 77 | $path = $this->chrooted_path( $path ); |
| 78 | |
| 79 | return $this->fs->is_file( $path ); |
| 80 | } |
| 81 | |
| 82 | public function is_dir( $path ) { |
| 83 | $path = $this->chrooted_path( $path ); |
| 84 | |
| 85 | return $this->fs->is_dir( $path ); |
| 86 | } |
| 87 | |
| 88 | public function mkdir( $path, $options = array() ) { |
| 89 | $path = $this->chrooted_path( $path ); |
| 90 | |
| 91 | return $this->fs->mkdir( $path, $options ); |
| 92 | } |
| 93 | |
| 94 | public function rm( $path, $options = array() ) { |
| 95 | $path = $this->chrooted_path( $path ); |
| 96 | |
| 97 | return $this->fs->rm( $path, $options ); |
| 98 | } |
| 99 | |
| 100 | public function rmdir( $path, $options = array() ) { |
| 101 | $path = $this->chrooted_path( $path ); |
| 102 | |
| 103 | return $this->fs->rmdir( $path, $options ); |
| 104 | } |
| 105 | |
| 106 | public function ls( $path = '/' ) { |
| 107 | $path = $this->chrooted_path( $path ); |
| 108 | |
| 109 | return $this->fs->ls( $path ); |
| 110 | } |
| 111 | |
| 112 | public function open_read_stream( $path ): ByteReadStream { |
| 113 | $path = $this->chrooted_path( $path ); |
| 114 | |
| 115 | return $this->fs->open_read_stream( $path ); |
| 116 | } |
| 117 | |
| 118 | public function open_write_stream( $path ): ByteWriteStream { |
| 119 | $path = $this->chrooted_path( $path ); |
| 120 | |
| 121 | return $this->fs->open_write_stream( $path ); |
| 122 | } |
| 123 | |
| 124 | public function copy( $source, $destination, $options = array() ) { |
| 125 | $source = $this->chrooted_path( $source ); |
| 126 | $destination = $this->chrooted_path( $destination ); |
| 127 | |
| 128 | return $this->fs->copy( $source, $destination, $options ); |
| 129 | } |
| 130 | |
| 131 | public function rename( $source, $destination, $options = array() ) { |
| 132 | $source = $this->chrooted_path( $source ); |
| 133 | $destination = $this->chrooted_path( $destination ); |
| 134 | |
| 135 | return $this->fs->rename( $source, $destination, $options ); |
| 136 | } |
| 137 | |
| 138 | public function get_contents( $path ) { |
| 139 | $path = $this->chrooted_path( $path ); |
| 140 | |
| 141 | return $this->fs->get_contents( $path ); |
| 142 | } |
| 143 | |
| 144 | public function put_contents( $path, $contents, $options = array() ) { |
| 145 | $path = $this->chrooted_path( $path ); |
| 146 | |
| 147 | return $this->fs->put_contents( $path, $contents, $options ); |
| 148 | } |
| 149 | |
| 150 | public function get_meta(): array { |
| 151 | return array_merge( array( 'chroot' => $this->chroot ), $this->fs->get_meta() ); |
| 152 | } |
| 153 | } |
| 154 |