summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal/Core/FileTransfer/Local.php
blob: 4f65e2273f0a965c59a41965789984a0541afbee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

/**
 * @file
 * Definition of Drupal\Core\FileTransfer\Local.
 */

namespace Drupal\Core\FileTransfer;

/**
 * Defines the local connection class for copying files as the httpd user.
 */
class Local extends FileTransfer implements ChmodInterface {

  /**
   * Implements Drupal\Core\FileTransfer\FileTransfer::connect().
   */
  public function connect() {
    // No-op
  }

  /**
   * Overrides Drupal\Core\FileTransfer\FileTransfer::factory().
   */
  static function factory($jail, $settings) {
    return new Local($jail);
  }

  /**
   * Implements Drupal\Core\FileTransfer\FileTransfer::copyFileJailed().
   */
  protected function copyFileJailed($source, $destination) {
    if (@!copy($source, $destination)) {
      throw new FileTransferException('Cannot copy %source to %destination.', NULL, array('%source' => $source, '%destination' => $destination));
    }
  }

  /**
   * Implements Drupal\Core\FileTransfer\FileTransfer::createDirectoryJailed().
   */
  protected function createDirectoryJailed($directory) {
    if (!is_dir($directory) && @!mkdir($directory, 0777, TRUE)) {
      throw new FileTransferException('Cannot create directory %directory.', NULL, array('%directory' => $directory));
    }
  }

  /**
   * Implements Drupal\Core\FileTransfer\FileTransfer::removeDirectoryJailed().
   */
  protected function removeDirectoryJailed($directory) {
    if (!is_dir($directory)) {
      // Programmer error assertion, not something we expect users to see.
      throw new FileTransferException('removeDirectoryJailed() called with a path (%directory) that is not a directory.', NULL, array('%directory' => $directory));
    }
    foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $filename => $file) {
      if ($file->isDir()) {
        if (@!drupal_rmdir($filename)) {
          throw new FileTransferException('Cannot remove directory %directory.', NULL, array('%directory' => $filename));
        }
      }
      elseif ($file->isFile()) {
        if (@!drupal_unlink($filename)) {
          throw new FileTransferException('Cannot remove file %file.', NULL, array('%file' => $filename));
        }
      }
    }
    if (@!drupal_rmdir($directory)) {
      throw new FileTransferException('Cannot remove directory %directory.', NULL, array('%directory' => $directory));
    }
  }

  /**
   * Implements Drupal\Core\FileTransfer\FileTransfer::removeFileJailed().
   */
  protected function removeFileJailed($file) {
    if (@!drupal_unlink($file)) {
      throw new FileTransferException('Cannot remove file %file.', NULL, array('%file' => $file));
    }
  }

  /**
   * Implements Drupal\Core\FileTransfer\FileTransfer::isDirectory().
   */
  public function isDirectory($path) {
    return is_dir($path);
  }

  /**
   * Implements Drupal\Core\FileTransfer\FileTransfer::isFile().
   */
  public function isFile($path) {
    return is_file($path);
  }

  /**
   * Implements Drupal\Core\FileTransfer\ChmodInterface::chmodJailed().
   */
  public function chmodJailed($path, $mode, $recursive) {
    if ($recursive && is_dir($path)) {
      foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as $filename => $file) {
        if (@!chmod($filename, $mode)) {
          throw new FileTransferException('Cannot chmod %path.', NULL, array('%path' => $filename));
        }
      }
    }
    elseif (@!chmod($path, $mode)) {
      throw new FileTransferException('Cannot chmod %path.', NULL, array('%path' => $path));
    }
  }
}