Commit 44451a48 authored by catch's avatar catch
Browse files

Issue #3308744 by andypost, bbrala: Fix magic connection property access from...

Issue #3308744 by andypost, bbrala: Fix magic connection property access from \Drupal\Core\FileTransfer\FileTransfer::__get()

(cherry picked from commit 7db1fcdb)
parent f1de4134
Loading
Loading
Loading
Loading
+62 −4
Original line number Diff line number Diff line
@@ -41,6 +41,27 @@ abstract class FileTransfer {
   */
  protected $port;

  /**
   * Full path to directory where file-transfer is restricted to.
   *
   * @var string
   */
  protected $jail;

  /**
   * Path to connection chroot.
   *
   * @var string|false|null
   */
  private $chrootPath;

  /**
   * The instantiated connection object.
   *
   * @var object|false|null
   */
  private $connectionHandle;

  /**
   * Constructs a Drupal\Core\FileTransfer\FileTransfer object.
   *
@@ -93,12 +114,49 @@ public static function factory($jail, $settings) {
  public function __get($name) {
    if ($name == 'connection') {
      $this->connect();
      return $this->connection;
      return $this->connectionHandle;
    }

    if ($name == 'chroot') {
      $this->setChroot();
      return $this->chroot;
      return $this->chrootPath;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function __set(string $name, $value): void {
    if ($name == 'connection') {
      $this->connectionHandle = $value;
    }
    elseif ($name == 'chroot') {
      $this->chrootPath = $value;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function __isset(string $name): bool {
    if ($name == 'connection') {
      return isset($this->connectionHandle);
    }
    if ($name == 'chroot') {
      return isset($this->chrootPath);
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function __unset(string $name): void {
    if ($name == 'connection') {
      unset($this->connectionHandle);
    }
    elseif ($name == 'chroot') {
      unset($this->chrootPath);
    }
  }

@@ -235,8 +293,8 @@ final protected function fixRemotePath($path, $strip_chroot = TRUE) {
    // Strip out windows drive letter if its there.
    $path = preg_replace('|^([a-z]{1}):|i', '', $path);
    if ($strip_chroot) {
      if ($this->chroot && strpos($path, $this->chroot) === 0) {
        $path = ($path == $this->chroot) ? '' : substr($path, strlen($this->chroot));
      if ($this->chrootPath && strpos($path, $this->chrootPath) === 0) {
        $path = ($path == $this->chrootPath) ? '' : substr($path, strlen($this->chrootPath));
      }
    }
    return $path;
+3 −2
Original line number Diff line number Diff line
@@ -45,8 +45,9 @@ public static function factory($jail, $settings) {
  }

  public function connect() {
    $this->connection = new MockTestConnection();
    $this->connection->connectionString = 'test://' . urlencode($this->username) . ':' . urlencode($this->password) . "@$this->host:$this->port/";
    $connection = new MockTestConnection();
    $connection->connectionString = 'test://' . urlencode($this->username) . ':' . urlencode($this->password) . "@$this->host:$this->port/";
    $this->connection = $connection;
  }

  public function copyFileJailed($source, $destination) {