Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / FileTransfer / Local.php
1 <?php
2
3 namespace Drupal\Core\FileTransfer;
4
5 /**
6  * Defines the local connection class for copying files as the httpd user.
7  */
8 class Local extends FileTransfer implements ChmodInterface {
9
10   /**
11    * {@inheritdoc}
12    */
13   public function connect() {
14     // No-op
15   }
16
17   /**
18    * {@inheritdoc}
19    */
20   public static function factory($jail, $settings) {
21     return new Local($jail);
22   }
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function copyFileJailed($source, $destination) {
28     if (@!copy($source, $destination)) {
29       throw new FileTransferException('Cannot copy %source to %destination.', NULL, ['%source' => $source, '%destination' => $destination]);
30     }
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function createDirectoryJailed($directory) {
37     if (!is_dir($directory) && @!mkdir($directory, 0777, TRUE)) {
38       throw new FileTransferException('Cannot create directory %directory.', NULL, ['%directory' => $directory]);
39     }
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function removeDirectoryJailed($directory) {
46     if (!is_dir($directory)) {
47       // Programmer error assertion, not something we expect users to see.
48       throw new FileTransferException('removeDirectoryJailed() called with a path (%directory) that is not a directory.', NULL, ['%directory' => $directory]);
49     }
50     foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $filename => $file) {
51       if ($file->isDir()) {
52         if (@!drupal_rmdir($filename)) {
53           throw new FileTransferException('Cannot remove directory %directory.', NULL, ['%directory' => $filename]);
54         }
55       }
56       elseif ($file->isFile()) {
57         if (@!drupal_unlink($filename)) {
58           throw new FileTransferException('Cannot remove file %file.', NULL, ['%file' => $filename]);
59         }
60       }
61     }
62     if (@!drupal_rmdir($directory)) {
63       throw new FileTransferException('Cannot remove directory %directory.', NULL, ['%directory' => $directory]);
64     }
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   protected function removeFileJailed($file) {
71     if (@!drupal_unlink($file)) {
72       throw new FileTransferException('Cannot remove file %file.', NULL, ['%file' => $file]);
73     }
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function isDirectory($path) {
80     return is_dir($path);
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function isFile($path) {
87     return is_file($path);
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function chmodJailed($path, $mode, $recursive) {
94     if ($recursive && is_dir($path)) {
95       foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $filename => $file) {
96         if (@!chmod($filename, $mode)) {
97           throw new FileTransferException('Cannot chmod %path.', NULL, ['%path' => $filename]);
98         }
99       }
100     }
101     elseif (@!chmod($path, $mode)) {
102       throw new FileTransferException('Cannot chmod %path.', NULL, ['%path' => $path]);
103     }
104   }
105
106 }