Chris@0: jail = $jail; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Defines a factory method for this class. Chris@0: * Chris@0: * Classes that extend this class must override the factory() static method. Chris@0: * They should return a new instance of the appropriate FileTransfer subclass. Chris@0: * Chris@0: * @param string $jail Chris@0: * The full path where all file operations performed by this object will Chris@0: * be restricted to. This prevents the FileTransfer classes from being Chris@0: * able to touch other parts of the filesystem. Chris@0: * @param array $settings Chris@0: * An array of connection settings for the FileTransfer subclass. If the Chris@0: * getSettingsForm() method uses any nested settings, the same structure Chris@0: * will be assumed here. Chris@0: * Chris@0: * @return object Chris@0: * New instance of the appropriate FileTransfer subclass. Chris@0: * Chris@0: * @throws \Drupal\Core\FileTransfer\FileTransferException Chris@0: */ Chris@0: public static function factory($jail, $settings) { Chris@0: throw new FileTransferException('FileTransfer::factory() static method not overridden by FileTransfer subclass.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements the magic __get() method. Chris@0: * Chris@0: * If the connection isn't set to anything, this will call the connect() Chris@0: * method and return the result; afterwards, the connection will be returned Chris@0: * directly without using this method. Chris@0: * Chris@0: * @param string $name Chris@0: * The name of the variable to return. Chris@0: * Chris@0: * @return string|bool Chris@0: * The variable specified in $name. Chris@0: */ Chris@0: public function __get($name) { Chris@0: if ($name == 'connection') { Chris@0: $this->connect(); Chris@0: return $this->connection; Chris@0: } Chris@0: Chris@0: if ($name == 'chroot') { Chris@0: $this->setChroot(); Chris@0: return $this->chroot; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Connects to the server. Chris@0: */ Chris@0: abstract public function connect(); Chris@0: Chris@0: /** Chris@0: * Copies a directory. Chris@0: * Chris@0: * @param string $source Chris@0: * The source path. Chris@0: * @param string $destination Chris@0: * The destination path. Chris@0: */ Chris@0: final public function copyDirectory($source, $destination) { Chris@0: $source = $this->sanitizePath($source); Chris@0: $destination = $this->fixRemotePath($destination); Chris@0: $this->checkPath($destination); Chris@0: $this->copyDirectoryJailed($source, $destination); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Changes the permissions of the specified $path (file or directory). Chris@0: * Chris@0: * @param string $path Chris@0: * The file / directory to change the permissions of. Chris@0: * @param int $mode Chris@0: * The new file permission mode to be passed to chmod(). Chris@0: * @param bool $recursive Chris@0: * Pass TRUE to recursively chmod the entire directory specified in $path. Chris@0: * Chris@0: * @throws \Drupal\Core\FileTransfer\FileTransferException Chris@0: * Chris@0: * @see http://php.net/chmod Chris@0: */ Chris@0: final public function chmod($path, $mode, $recursive = FALSE) { Chris@0: if (!($this instanceof ChmodInterface)) { Chris@0: throw new FileTransferException('Unable to change file permissions'); Chris@0: } Chris@0: $path = $this->sanitizePath($path); Chris@0: $path = $this->fixRemotePath($path); Chris@0: $this->checkPath($path); Chris@0: $this->chmodJailed($path, $mode, $recursive); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a directory. Chris@0: * Chris@0: * @param string $directory Chris@0: * The directory to be created. Chris@0: */ Chris@0: final public function createDirectory($directory) { Chris@0: $directory = $this->fixRemotePath($directory); Chris@0: $this->checkPath($directory); Chris@0: $this->createDirectoryJailed($directory); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Removes a directory. Chris@0: * Chris@0: * @param string $directory Chris@0: * The directory to be removed. Chris@0: */ Chris@0: final public function removeDirectory($directory) { Chris@0: $directory = $this->fixRemotePath($directory); Chris@0: $this->checkPath($directory); Chris@0: $this->removeDirectoryJailed($directory); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Copies a file. Chris@0: * Chris@0: * @param string $source Chris@0: * The source file. Chris@0: * @param string $destination Chris@0: * The destination file. Chris@0: */ Chris@0: final public function copyFile($source, $destination) { Chris@0: $source = $this->sanitizePath($source); Chris@0: $destination = $this->fixRemotePath($destination); Chris@0: $this->checkPath($destination); Chris@0: $this->copyFileJailed($source, $destination); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Removes a file. Chris@0: * Chris@0: * @param string $destination Chris@0: * The destination file to be removed. Chris@0: */ Chris@0: final public function removeFile($destination) { Chris@0: $destination = $this->fixRemotePath($destination); Chris@0: $this->checkPath($destination); Chris@0: $this->removeFileJailed($destination); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that the path is inside the jail and throws an exception if not. Chris@0: * Chris@0: * @param string $path Chris@0: * A path to check against the jail. Chris@0: * Chris@0: * @throws \Drupal\Core\FileTransfer\FileTransferException Chris@0: */ Chris@0: final protected function checkPath($path) { Chris@0: $full_jail = $this->chroot . $this->jail; Chris@14: $full_path = \Drupal::service('file_system') Chris@14: ->realpath(substr($this->chroot . $path, 0, strlen($full_jail))); Chris@0: $full_path = $this->fixRemotePath($full_path, FALSE); Chris@0: if ($full_jail !== $full_path) { Chris@0: throw new FileTransferException('@directory is outside of the @jail', NULL, ['@directory' => $path, '@jail' => $this->jail]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a modified path suitable for passing to the server. Chris@0: * Chris@0: * If a path is a windows path, makes it POSIX compliant by removing the drive Chris@0: * letter. If $this->chroot has a value and $strip_chroot is TRUE, it is Chris@0: * stripped from the path to allow for chroot'd filetransfer systems. Chris@0: * Chris@0: * @param string $path Chris@0: * The path to modify. Chris@0: * @param bool $strip_chroot Chris@0: * Whether to remove the path in $this->chroot. Chris@0: * Chris@0: * @return string Chris@0: * The modified path. Chris@0: */ Chris@0: final protected function fixRemotePath($path, $strip_chroot = TRUE) { Chris@0: $path = $this->sanitizePath($path); Chris@0: // Strip out windows driveletter if its there. Chris@0: $path = preg_replace('|^([a-z]{1}):|i', '', $path); Chris@0: if ($strip_chroot) { Chris@0: if ($this->chroot && strpos($path, $this->chroot) === 0) { Chris@0: $path = ($path == $this->chroot) ? '' : substr($path, strlen($this->chroot)); Chris@0: } Chris@0: } Chris@0: return $path; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Changes backslashes to slashes, also removes a trailing slash. Chris@0: * Chris@0: * @param string $path Chris@0: * The path to modify. Chris@0: * Chris@0: * @return string Chris@0: * The modified path. Chris@0: */ Chris@0: public function sanitizePath($path) { Chris@0: // Windows path sanitization. Chris@0: $path = str_replace('\\', '/', $path); Chris@0: if (substr($path, -1) == '/') { Chris@0: $path = substr($path, 0, -1); Chris@0: } Chris@0: return $path; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Copies a directory. Chris@0: * Chris@0: * We need a separate method to make sure the $destination is in the jail. Chris@0: * Chris@0: * @param string $source Chris@0: * The source path. Chris@0: * @param string $destination Chris@0: * The destination path. Chris@0: */ Chris@0: protected function copyDirectoryJailed($source, $destination) { Chris@0: if ($this->isDirectory($destination)) { Chris@18: $destination = $destination . '/' . \Drupal::service('file_system')->basename($source); Chris@0: } Chris@0: $this->createDirectory($destination); Chris@0: foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $filename => $file) { Chris@0: $relative_path = substr($filename, strlen($source)); Chris@0: if ($file->isDir()) { Chris@0: $this->createDirectory($destination . $relative_path); Chris@0: } Chris@0: else { Chris@0: $this->copyFile($file->getPathName(), $destination . $relative_path); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a directory. Chris@0: * Chris@0: * @param string $directory Chris@0: * The directory to be created. Chris@0: */ Chris@0: abstract protected function createDirectoryJailed($directory); Chris@0: Chris@0: /** Chris@0: * Removes a directory. Chris@0: * Chris@0: * @param string $directory Chris@0: * The directory to be removed. Chris@0: */ Chris@0: abstract protected function removeDirectoryJailed($directory); Chris@0: Chris@0: /** Chris@0: * Copies a file. Chris@0: * Chris@0: * @param string $source Chris@0: * The source file. Chris@0: * @param string $destination Chris@0: * The destination file. Chris@0: */ Chris@0: abstract protected function copyFileJailed($source, $destination); Chris@0: Chris@0: /** Chris@0: * Removes a file. Chris@0: * Chris@0: * @param string $destination Chris@0: * The destination file to be removed. Chris@0: */ Chris@0: abstract protected function removeFileJailed($destination); Chris@0: Chris@0: /** Chris@0: * Checks if a particular path is a directory. Chris@0: * Chris@0: * @param string $path Chris@0: * The path to check Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the specified path is a directory, FALSE otherwise. Chris@0: */ Chris@0: abstract public function isDirectory($path); Chris@0: Chris@0: /** Chris@0: * Checks if a particular path is a file (not a directory). Chris@0: * Chris@0: * @param string $path Chris@0: * The path to check. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the specified path is a file, FALSE otherwise. Chris@0: */ Chris@0: abstract public function isFile($path); Chris@0: Chris@0: /** Chris@0: * Returns the chroot property for this connection. Chris@0: * Chris@0: * It does this by moving up the tree until it finds itself Chris@0: * Chris@0: * @return string|bool Chris@0: * If successful, the chroot path for this connection, otherwise FALSE. Chris@0: */ Chris@0: public function findChroot() { Chris@0: // If the file exists as is, there is no chroot. Chris@0: $path = __FILE__; Chris@0: $path = $this->fixRemotePath($path, FALSE); Chris@0: if ($this->isFile($path)) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: $path = __DIR__; Chris@0: $path = $this->fixRemotePath($path, FALSE); Chris@0: $parts = explode('/', $path); Chris@0: $chroot = ''; Chris@0: while (count($parts)) { Chris@0: $check = implode($parts, '/'); Chris@18: if ($this->isFile($check . '/' . \Drupal::service('file_system')->basename(__FILE__))) { Chris@0: // Remove the trailing slash. Chris@0: return substr($chroot, 0, -1); Chris@0: } Chris@0: $chroot .= array_shift($parts) . '/'; Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the chroot and changes the jail to match the correct path scheme. Chris@0: */ Chris@0: public function setChroot() { Chris@0: $this->chroot = $this->findChroot(); Chris@0: $this->jail = $this->fixRemotePath($this->jail); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a form to collect connection settings credentials. Chris@0: * Chris@0: * Implementing classes can either extend this form with fields collecting the Chris@0: * specific information they need, or override it entirely. Chris@0: * Chris@0: * @return array Chris@0: * An array that contains a Form API definition. Chris@0: */ Chris@0: public function getSettingsForm() { Chris@0: $form['username'] = [ Chris@0: '#type' => 'textfield', Chris@0: '#title' => t('Username'), Chris@0: ]; Chris@0: $form['password'] = [ Chris@0: '#type' => 'password', Chris@0: '#title' => t('Password'), Chris@0: '#description' => t('Your password is not saved in the database and is only used to establish a connection.'), Chris@0: ]; Chris@0: $form['advanced'] = [ Chris@0: '#type' => 'details', Chris@0: '#title' => t('Advanced settings'), Chris@0: ]; Chris@0: $form['advanced']['hostname'] = [ Chris@0: '#type' => 'textfield', Chris@0: '#title' => t('Host'), Chris@0: '#default_value' => 'localhost', Chris@0: '#description' => t('The connection will be created between your web server and the machine hosting the web server files. In the vast majority of cases, this will be the same machine, and "localhost" is correct.'), Chris@0: ]; Chris@0: $form['advanced']['port'] = [ Chris@0: '#type' => 'textfield', Chris@0: '#title' => t('Port'), Chris@0: '#default_value' => NULL, Chris@0: ]; Chris@0: return $form; Chris@0: } Chris@0: Chris@0: }