Chris@0: username = $username; Chris@0: $this->password = $password; Chris@0: $this->hostname = $hostname; Chris@0: $this->port = $port; Chris@0: parent::__construct($jail); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function connect() { Chris@0: $this->connection = @ssh2_connect($this->hostname, $this->port); Chris@0: if (!$this->connection) { Chris@0: throw new FileTransferException('SSH Connection failed to @host:@port', NULL, ['@host' => $this->hostname, '@port' => $this->port]); Chris@0: } Chris@0: if (!@ssh2_auth_password($this->connection, $this->username, $this->password)) { Chris@0: throw new FileTransferException('The supplied username/password combination was not accepted.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function factory($jail, $settings) { Chris@0: $username = empty($settings['username']) ? '' : $settings['username']; Chris@0: $password = empty($settings['password']) ? '' : $settings['password']; Chris@0: $hostname = empty($settings['advanced']['hostname']) ? 'localhost' : $settings['advanced']['hostname']; Chris@0: $port = empty($settings['advanced']['port']) ? 22 : $settings['advanced']['port']; Chris@0: return new SSH($jail, $username, $password, $hostname, $port); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function copyFileJailed($source, $destination) { Chris@0: if (!@ssh2_scp_send($this->connection, $source, $destination)) { Chris@0: throw new FileTransferException('Cannot copy @source_file to @destination_file.', NULL, ['@source' => $source, '@destination' => $destination]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function copyDirectoryJailed($source, $destination) { Chris@0: if (@!ssh2_exec($this->connection, 'cp -Rp ' . escapeshellarg($source) . ' ' . escapeshellarg($destination))) { Chris@0: throw new FileTransferException('Cannot copy directory @directory.', NULL, ['@directory' => $source]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function createDirectoryJailed($directory) { Chris@0: if (@!ssh2_exec($this->connection, 'mkdir ' . escapeshellarg($directory))) { Chris@0: throw new FileTransferException('Cannot create directory @directory.', NULL, ['@directory' => $directory]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function removeDirectoryJailed($directory) { Chris@0: if (@!ssh2_exec($this->connection, 'rm -Rf ' . escapeshellarg($directory))) { Chris@0: throw new FileTransferException('Cannot remove @directory.', NULL, ['@directory' => $directory]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function removeFileJailed($destination) { Chris@0: if (!@ssh2_exec($this->connection, 'rm ' . escapeshellarg($destination))) { Chris@0: throw new FileTransferException('Cannot remove @directory.', NULL, ['@directory' => $destination]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements Drupal\Core\FileTransfer\FileTransfer::isDirectory(). Chris@0: * Chris@0: * WARNING: This is untested. It is not currently used, but should do the Chris@0: * trick. Chris@0: */ Chris@0: public function isDirectory($path) { Chris@0: $directory = escapeshellarg($path); Chris@0: $cmd = "[ -d {$directory} ] && echo 'yes'"; Chris@0: if ($output = @ssh2_exec($this->connection, $cmd)) { Chris@0: if ($output == 'yes') { Chris@0: return TRUE; Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: else { Chris@0: throw new FileTransferException('Cannot check @path.', NULL, ['@path' => $path]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isFile($path) { Chris@0: $file = escapeshellarg($path); Chris@0: $cmd = "[ -f {$file} ] && echo 'yes'"; Chris@0: if ($output = @ssh2_exec($this->connection, $cmd)) { Chris@0: if ($output == 'yes') { Chris@0: return TRUE; Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: else { Chris@0: throw new FileTransferException('Cannot check @path.', NULL, ['@path' => $path]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function chmodJailed($path, $mode, $recursive) { Chris@0: $cmd = sprintf("chmod %s%o %s", $recursive ? '-R ' : '', $mode, escapeshellarg($path)); Chris@0: if (@!ssh2_exec($this->connection, $cmd)) { Chris@0: throw new FileTransferException('Cannot change permissions of @path.', NULL, ['@path' => $path]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getSettingsForm() { Chris@0: $form = parent::getSettingsForm(); Chris@0: $form['advanced']['port']['#default_value'] = 22; Chris@0: return $form; Chris@0: } Chris@0: Chris@0: }