diff --git a/core/core.services.yml b/core/core.services.yml index a9b8c3e..6628022 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -639,6 +639,9 @@ services: flood: class: Drupal\Core\Flood\DatabaseBackend arguments: ['@database', '@request_stack'] + plugin.manager.filetransfer: + class: Drupal\Core\FileTransfer\FileTransferManager + arguments: ['@container.namespaces', '@module_handler'] plugin.manager.mail: class: Drupal\Core\Mail\MailManager arguments: ['@container.namespaces', '@cache.discovery', '@language_manager', '@module_handler', '@config.factory'] diff --git a/core/includes/common.inc b/core/includes/common.inc index 79b2224..b584f1e 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -4562,26 +4562,6 @@ function drupal_get_updaters() { } /** - * Assembles the Drupal FileTransfer registry. - * - * @return - * The Drupal FileTransfer class registry. - * - * @see \Drupal\Core\FileTransfer\FileTransfer - * @see hook_filetransfer_info() - * @see hook_filetransfer_info_alter() - */ -function drupal_get_filetransfer_info() { - $info = &drupal_static(__FUNCTION__); - if (!isset($info)) { - $info = \Drupal::moduleHandler()->invokeAll('filetransfer_info'); - \Drupal::moduleHandler()->alter('filetransfer_info', $info); - uasort($info, array('Drupal\Component\Utility\SortArray', 'sortByWeightElement')); - } - return $info; -} - -/** * @defgroup queue Queue operations * @{ * Queue items to allow later processing. diff --git a/core/lib/Drupal/Core/Annotation/FileTransfer.php b/core/lib/Drupal/Core/Annotation/FileTransfer.php new file mode 100644 index 0000000..58e9396 --- /dev/null +++ b/core/lib/Drupal/Core/Annotation/FileTransfer.php @@ -0,0 +1,44 @@ +username = $username; - $this->password = $password; - $this->hostname = $hostname; - $this->port = $port; - parent::__construct($jail); - } - - /** - * Overrides Drupal\Core\FileTransfer\FileTransfer::factory(). - */ - static function factory($jail, $settings) { - $username = empty($settings['username']) ? '' : $settings['username']; - $password = empty($settings['password']) ? '' : $settings['password']; - $hostname = empty($settings['advanced']['hostname']) ? 'localhost' : $settings['advanced']['hostname']; - $port = empty($settings['advanced']['port']) ? 21 : $settings['advanced']['port']; - - if (function_exists('ftp_connect')) { - $class = 'Drupal\Core\FileTransfer\FTPExtension'; - } - else { - throw new FileTransferException('No FTP backend available.'); - } - - return new $class($jail, $username, $password, $hostname, $port); - } - - /** - * Overrides Drupal\Core\FileTransfer\FileTransfer::getSettingsForm(). - */ - public function getSettingsForm() { - $form = parent::getSettingsForm(); - $form['advanced']['port']['#default_value'] = 21; - return $form; - } -} diff --git a/core/lib/Drupal/Core/FileTransfer/FTPExtension.php b/core/lib/Drupal/Core/FileTransfer/FTPExtension.php deleted file mode 100644 index f87293b..0000000 --- a/core/lib/Drupal/Core/FileTransfer/FTPExtension.php +++ /dev/null @@ -1,124 +0,0 @@ -connection = ftp_connect($this->hostname, $this->port); - - if (!$this->connection) { - throw new FileTransferException("Cannot connect to FTP Server, check settings"); - } - if (!ftp_login($this->connection, $this->username, $this->password)) { - throw new FileTransferException("Cannot log in to FTP server. Check username and password"); - } - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::copyFileJailed(). - */ - protected function copyFileJailed($source, $destination) { - if (!@ftp_put($this->connection, $destination, $source, FTP_BINARY)) { - throw new FileTransferException("Cannot move @source to @destination", NULL, array("@source" => $source, "@destination" => $destination)); - } - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::createDirectoryJailed(). - */ - protected function createDirectoryJailed($directory) { - if (!ftp_mkdir($this->connection, $directory)) { - throw new FileTransferException("Cannot create directory @directory", NULL, array("@directory" => $directory)); - } - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::removeDirectoryJailed(). - */ - protected function removeDirectoryJailed($directory) { - $pwd = ftp_pwd($this->connection); - if (!ftp_chdir($this->connection, $directory)) { - throw new FileTransferException("Unable to change to directory @directory", NULL, array('@directory' => $directory)); - } - $list = @ftp_nlist($this->connection, '.'); - if (!$list) { - $list = array(); - } - foreach ($list as $item) { - if ($item == '.' || $item == '..') { - continue; - } - if (@ftp_chdir($this->connection, $item)) { - ftp_cdup($this->connection); - $this->removeDirectory(ftp_pwd($this->connection) . '/' . $item); - } - else { - $this->removeFile(ftp_pwd($this->connection) . '/' . $item); - } - } - ftp_chdir($this->connection, $pwd); - if (!ftp_rmdir($this->connection, $directory)) { - throw new FileTransferException("Unable to remove to directory @directory", NULL, array('@directory' => $directory)); - } - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::removeFileJailed(). - */ - protected function removeFileJailed($destination) { - if (!ftp_delete($this->connection, $destination)) { - throw new FileTransferException("Unable to remove to file @file", NULL, array('@file' => $destination)); - } - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::isDirectory(). - */ - public function isDirectory($path) { - $result = FALSE; - $curr = ftp_pwd($this->connection); - if (@ftp_chdir($this->connection, $path)) { - $result = TRUE; - } - ftp_chdir($this->connection, $curr); - return $result; - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::isFile(). - */ - public function isFile($path) { - return ftp_size($this->connection, $path) != -1; - } - - /** - * Implements Drupal\Core\FileTransfer\ChmodInterface::chmodJailed(). - */ - function chmodJailed($path, $mode, $recursive) { - if (!ftp_chmod($this->connection, $mode, $path)) { - throw new FileTransferException("Unable to set permissions on %file", NULL, array('%file' => $path)); - } - if ($this->isDirectory($path) && $recursive) { - $filelist = @ftp_nlist($this->connection, $path); - if (!$filelist) { - //empty directory - returns false - return; - } - foreach ($filelist as $file) { - $this->chmodJailed($file, $mode, $recursive); - } - } - } -} diff --git a/core/lib/Drupal/Core/FileTransfer/FileTransfer.php b/core/lib/Drupal/Core/FileTransfer/FileTransfer.php deleted file mode 100644 index e121392..0000000 --- a/core/lib/Drupal/Core/FileTransfer/FileTransfer.php +++ /dev/null @@ -1,422 +0,0 @@ -jail = $jail; - } - - /** - * Defines a factory method for this class. - * - * Classes that extend this class must override the factory() static method. - * They should return a new instance of the appropriate FileTransfer subclass. - * - * @param string $jail - * The full path where all file operations performed by this object will - * be restricted to. This prevents the FileTransfer classes from being - * able to touch other parts of the filesystem. - * @param array $settings - * An array of connection settings for the FileTransfer subclass. If the - * getSettingsForm() method uses any nested settings, the same structure - * will be assumed here. - * - * @return object - * New instance of the appropriate FileTransfer subclass. - * - * @throws \Drupal\Core\FileTransfer\FileTransferException - */ - static function factory($jail, $settings) { - throw new FileTransferException('FileTransfer::factory() static method not overridden by FileTransfer subclass.'); - } - - /** - * Implements the magic __get() method. - * - * If the connection isn't set to anything, this will call the connect() - * method and return the result; afterwards, the connection will be returned - * directly without using this method. - * - * @param string $name - * The name of the variable to return. - * - * @return string|bool - * The variable specified in $name. - */ - function __get($name) { - if ($name == 'connection') { - $this->connect(); - return $this->connection; - } - - if ($name == 'chroot') { - $this->setChroot(); - return $this->chroot; - } - } - - /** - * Connects to the server. - */ - abstract protected function connect(); - - /** - * Copies a directory. - * - * @param string $source - * The source path. - * @param string $destination - * The destination path. - */ - public final function copyDirectory($source, $destination) { - $source = $this->sanitizePath($source); - $destination = $this->fixRemotePath($destination); - $this->checkPath($destination); - $this->copyDirectoryJailed($source, $destination); - } - - /** - * Changes the permissions of the specified $path (file or directory). - * - * @param string $path - * The file / directory to change the permissions of. - * @param int $mode - * The new file permission mode to be passed to chmod(). - * @param bool $recursive - * Pass TRUE to recursively chmod the entire directory specified in $path. - * - * @throws \Drupal\Core\FileTransfer\FileTransferException - * - * @see http://php.net/chmod - */ - public final function chmod($path, $mode, $recursive = FALSE) { - if (!($this instanceof ChmodInterface)) { - throw new FileTransferException('Unable to change file permissions'); - } - $path = $this->sanitizePath($path); - $path = $this->fixRemotePath($path); - $this->checkPath($path); - $this->chmodJailed($path, $mode, $recursive); - } - - /** - * Creates a directory. - * - * @param string $directory - * The directory to be created. - */ - public final function createDirectory($directory) { - $directory = $this->fixRemotePath($directory); - $this->checkPath($directory); - $this->createDirectoryJailed($directory); - } - - /** - * Removes a directory. - * - * @param string $directory - * The directory to be removed. - */ - public final function removeDirectory($directory) { - $directory = $this->fixRemotePath($directory); - $this->checkPath($directory); - $this->removeDirectoryJailed($directory); - } - - /** - * Copies a file. - * - * @param string $source - * The source file. - * @param string $destination - * The destination file. - */ - public final function copyFile($source, $destination) { - $source = $this->sanitizePath($source); - $destination = $this->fixRemotePath($destination); - $this->checkPath($destination); - $this->copyFileJailed($source, $destination); - } - - /** - * Removes a file. - * - * @param string $destination - * The destination file to be removed. - */ - public final function removeFile($destination) { - $destination = $this->fixRemotePath($destination); - $this->checkPath($destination); - $this->removeFileJailed($destination); - } - - /** - * Checks that the path is inside the jail and throws an exception if not. - * - * @param string $path - * A path to check against the jail. - * - * @throws \Drupal\Core\FileTransfer\FileTransferException - */ - protected final function checkPath($path) { - $full_jail = $this->chroot . $this->jail; - $full_path = drupal_realpath(substr($this->chroot . $path, 0, strlen($full_jail))); - $full_path = $this->fixRemotePath($full_path, FALSE); - if ($full_jail !== $full_path) { - throw new FileTransferException('@directory is outside of the @jail', NULL, array('@directory' => $path, '@jail' => $this->jail)); - } - } - - /** - * Returns a modified path suitable for passing to the server. - * - * If a path is a windows path, makes it POSIX compliant by removing the drive - * letter. If $this->chroot has a value and $strip_chroot is TRUE, it is - * stripped from the path to allow for chroot'd filetransfer systems. - * - * @param string $path - * The path to modify. - * @param bool $strip_chroot - * Whether to remove the path in $this->chroot. - * - * @return string - * The modified path. - */ - protected final function fixRemotePath($path, $strip_chroot = TRUE) { - $path = $this->sanitizePath($path); - $path = preg_replace('|^([a-z]{1}):|i', '', $path); // Strip out windows driveletter if its there. - if ($strip_chroot) { - if ($this->chroot && strpos($path, $this->chroot) === 0) { - $path = ($path == $this->chroot) ? '' : substr($path, strlen($this->chroot)); - } - } - return $path; - } - - /** - * Changes backslashes to slashes, also removes a trailing slash. - * - * @param string $path - * The path to modify. - * - * @return string - * The modified path. - */ - function sanitizePath($path) { - $path = str_replace('\\', '/', $path); // Windows path sanitization. - if (substr($path, -1) == '/') { - $path = substr($path, 0, -1); - } - return $path; - } - - /** - * Copies a directory. - * - * We need a separate method to make sure the $destination is in the jail. - * - * @param string $source - * The source path. - * @param string $destination - * The destination path. - */ - protected function copyDirectoryJailed($source, $destination) { - if ($this->isDirectory($destination)) { - $destination = $destination . '/' . drupal_basename($source); - } - $this->createDirectory($destination); - foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $filename => $file) { - $relative_path = substr($filename, strlen($source)); - if ($file->isDir()) { - $this->createDirectory($destination . $relative_path); - } - else { - $this->copyFile($file->getPathName(), $destination . $relative_path); - } - } - } - - /** - * Creates a directory. - * - * @param string $directory - * The directory to be created. - */ - abstract protected function createDirectoryJailed($directory); - - /** - * Removes a directory. - * - * @param string $directory - * The directory to be removed. - */ - abstract protected function removeDirectoryJailed($directory); - - /** - * Copies a file. - * - * @param string $source - * The source file. - * @param string $destination - * The destination file. - */ - abstract protected function copyFileJailed($source, $destination); - - /** - * Removes a file. - * - * @param string $destination - * The destination file to be removed. - */ - abstract protected function removeFileJailed($destination); - - /** - * Checks if a particular path is a directory. - * - * @param string $path - * The path to check - * - * @return bool - * TRUE if the specified path is a directory, FALSE otherwise. - */ - abstract public function isDirectory($path); - - /** - * Checks if a particular path is a file (not a directory). - * - * @param string $path - * The path to check. - * - * @return bool - * TRUE if the specified path is a file, FALSE otherwise. - */ - abstract public function isFile($path); - - /** - * Returns the chroot property for this connection. - * - * It does this by moving up the tree until it finds itself - * - * @return string|bool - * If successful, the chroot path for this connection, otherwise FALSE. - */ - function findChroot() { - // If the file exists as is, there is no chroot. - $path = __FILE__; - $path = $this->fixRemotePath($path, FALSE); - if ($this->isFile($path)) { - return FALSE; - } - - $path = __DIR__; - $path = $this->fixRemotePath($path, FALSE); - $parts = explode('/', $path); - $chroot = ''; - while (count($parts)) { - $check = implode($parts, '/'); - if ($this->isFile($check . '/' . drupal_basename(__FILE__))) { - // Remove the trailing slash. - return substr($chroot, 0, -1); - } - $chroot .= array_shift($parts) . '/'; - } - return FALSE; - } - - /** - * Sets the chroot and changes the jail to match the correct path scheme. - */ - function setChroot() { - $this->chroot = $this->findChroot(); - $this->jail = $this->fixRemotePath($this->jail); - } - - /** - * Returns a form to collect connection settings credentials. - * - * Implementing classes can either extend this form with fields collecting the - * specific information they need, or override it entirely. - * - * @return array - * An array that contains a Form API definition. - */ - public function getSettingsForm() { - $form['username'] = array( - '#type' => 'textfield', - '#title' => t('Username'), - ); - $form['password'] = array( - '#type' => 'password', - '#title' => t('Password'), - '#description' => t('Your password is not saved in the database and is only used to establish a connection.'), - ); - $form['advanced'] = array( - '#type' => 'details', - '#title' => t('Advanced settings'), - ); - $form['advanced']['hostname'] = array( - '#type' => 'textfield', - '#title' => t('Host'), - '#default_value' => 'localhost', - '#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.'), - ); - $form['advanced']['port'] = array( - '#type' => 'textfield', - '#title' => t('Port'), - '#default_value' => NULL, - ); - return $form; - } -} diff --git a/core/lib/Drupal/Core/FileTransfer/FileTransferBase.php b/core/lib/Drupal/Core/FileTransfer/FileTransferBase.php new file mode 100644 index 0000000..af8529a --- /dev/null +++ b/core/lib/Drupal/Core/FileTransfer/FileTransferBase.php @@ -0,0 +1,422 @@ +jail = $jail; + } + + /** + * Defines a factory method for this class. + * + * Classes that extend this class must override the create() static method. + * They should return a new instance of the appropriate FileTransfer subclass. + * + * @param string $jail + * The full path where all file operations performed by this object will + * be restricted to. This prevents the FileTransfer classes from being + * able to touch other parts of the filesystem. + * @param array $settings + * An array of connection settings for the FileTransfer subclass. If the + * getSettingsForm() method uses any nested settings, the same structure + * will be assumed here. + * + * @return object + * New instance of the appropriate FileTransfer subclass. + * + * @throws \Drupal\Core\FileTransfer\FileTransferException + */ + static function create($jail, $settings) { + throw new FileTransferException('FileTransfer::create() static method not overridden by FileTransfer subclass.'); + } + + /** + * Implements the magic __get() method. + * + * If the connection isn't set to anything, this will call the connect() + * method and return the result; afterwards, the connection will be returned + * directly without using this method. + * + * @param string $name + * The name of the variable to return. + * + * @return string|bool + * The variable specified in $name. + */ + public function __get($name) { + if ($name == 'connection') { + $this->connect(); + return $this->connection; + } + + if ($name == 'chroot') { + $this->setChroot(); + return $this->chroot; + } + } + + /** + * Connects to the server. + */ + abstract protected function connect(); + + /** + * Copies a directory. + * + * @param string $source + * The source path. + * @param string $destination + * The destination path. + */ + public final function copyDirectory($source, $destination) { + $source = $this->sanitizePath($source); + $destination = $this->fixRemotePath($destination); + $this->checkPath($destination); + $this->copyDirectoryJailed($source, $destination); + } + + /** + * Changes the permissions of the specified $path (file or directory). + * + * @param string $path + * The file / directory to change the permissions of. + * @param int $mode + * The new file permission mode to be passed to chmod(). + * @param bool $recursive + * Pass TRUE to recursively chmod the entire directory specified in $path. + * + * @throws \Drupal\Core\FileTransfer\FileTransferException + * + * @see http://php.net/chmod + */ + public final function chmod($path, $mode, $recursive = FALSE) { + if (!($this instanceof ChmodInterface)) { + throw new FileTransferException('Unable to change file permissions'); + } + $path = $this->sanitizePath($path); + $path = $this->fixRemotePath($path); + $this->checkPath($path); + $this->chmodJailed($path, $mode, $recursive); + } + + /** + * Creates a directory. + * + * @param string $directory + * The directory to be created. + */ + public final function createDirectory($directory) { + $directory = $this->fixRemotePath($directory); + $this->checkPath($directory); + $this->createDirectoryJailed($directory); + } + + /** + * Removes a directory. + * + * @param string $directory + * The directory to be removed. + */ + public final function removeDirectory($directory) { + $directory = $this->fixRemotePath($directory); + $this->checkPath($directory); + $this->removeDirectoryJailed($directory); + } + + /** + * Copies a file. + * + * @param string $source + * The source file. + * @param string $destination + * The destination file. + */ + public final function copyFile($source, $destination) { + $source = $this->sanitizePath($source); + $destination = $this->fixRemotePath($destination); + $this->checkPath($destination); + $this->copyFileJailed($source, $destination); + } + + /** + * Removes a file. + * + * @param string $destination + * The destination file to be removed. + */ + public final function removeFile($destination) { + $destination = $this->fixRemotePath($destination); + $this->checkPath($destination); + $this->removeFileJailed($destination); + } + + /** + * Checks that the path is inside the jail and throws an exception if not. + * + * @param string $path + * A path to check against the jail. + * + * @throws \Drupal\Core\FileTransfer\FileTransferException + */ + protected final function checkPath($path) { + $full_jail = $this->chroot . $this->jail; + $full_path = drupal_realpath(substr($this->chroot . $path, 0, strlen($full_jail))); + $full_path = $this->fixRemotePath($full_path, FALSE); + if ($full_jail !== $full_path) { + throw new FileTransferException('@directory is outside of the @jail', NULL, array('@directory' => $path, '@jail' => $this->jail)); + } + } + + /** + * Returns a modified path suitable for passing to the server. + * + * If a path is a windows path, makes it POSIX compliant by removing the drive + * letter. If $this->chroot has a value and $strip_chroot is TRUE, it is + * stripped from the path to allow for chroot'd filetransfer systems. + * + * @param string $path + * The path to modify. + * @param bool $strip_chroot + * Whether to remove the path in $this->chroot. + * + * @return string + * The modified path. + */ + protected final function fixRemotePath($path, $strip_chroot = TRUE) { + $path = $this->sanitizePath($path); + $path = preg_replace('|^([a-z]{1}):|i', '', $path); // Strip out windows driveletter if its there. + if ($strip_chroot) { + if ($this->chroot && strpos($path, $this->chroot) === 0) { + $path = ($path == $this->chroot) ? '' : substr($path, strlen($this->chroot)); + } + } + return $path; + } + + /** + * Changes backslashes to slashes, also removes a trailing slash. + * + * @param string $path + * The path to modify. + * + * @return string + * The modified path. + */ + public function sanitizePath($path) { + $path = str_replace('\\', '/', $path); // Windows path sanitization. + if (substr($path, -1) == '/') { + $path = substr($path, 0, -1); + } + return $path; + } + + /** + * Copies a directory. + * + * We need a separate method to make sure the $destination is in the jail. + * + * @param string $source + * The source path. + * @param string $destination + * The destination path. + */ + protected function copyDirectoryJailed($source, $destination) { + if ($this->isDirectory($destination)) { + $destination = $destination . '/' . drupal_basename($source); + } + $this->createDirectory($destination); + foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $filename => $file) { + $relative_path = substr($filename, strlen($source)); + if ($file->isDir()) { + $this->createDirectory($destination . $relative_path); + } + else { + $this->copyFile($file->getPathName(), $destination . $relative_path); + } + } + } + + /** + * Creates a directory. + * + * @param string $directory + * The directory to be created. + */ + abstract protected function createDirectoryJailed($directory); + + /** + * Removes a directory. + * + * @param string $directory + * The directory to be removed. + */ + abstract protected function removeDirectoryJailed($directory); + + /** + * Copies a file. + * + * @param string $source + * The source file. + * @param string $destination + * The destination file. + */ + abstract protected function copyFileJailed($source, $destination); + + /** + * Removes a file. + * + * @param string $destination + * The destination file to be removed. + */ + abstract protected function removeFileJailed($destination); + + /** + * Checks if a particular path is a directory. + * + * @param string $path + * The path to check + * + * @return bool + * TRUE if the specified path is a directory, FALSE otherwise. + */ + abstract public function isDirectory($path); + + /** + * Checks if a particular path is a file (not a directory). + * + * @param string $path + * The path to check. + * + * @return bool + * TRUE if the specified path is a file, FALSE otherwise. + */ + abstract public function isFile($path); + + /** + * Returns the chroot property for this connection. + * + * It does this by moving up the tree until it finds itself + * + * @return string|bool + * If successful, the chroot path for this connection, otherwise FALSE. + */ + function findChroot() { + // If the file exists as is, there is no chroot. + $path = __FILE__; + $path = $this->fixRemotePath($path, FALSE); + if ($this->isFile($path)) { + return FALSE; + } + + $path = __DIR__; + $path = $this->fixRemotePath($path, FALSE); + $parts = explode('/', $path); + $chroot = ''; + while (count($parts)) { + $check = implode($parts, '/'); + if ($this->isFile($check . '/' . drupal_basename(__FILE__))) { + // Remove the trailing slash. + return substr($chroot, 0, -1); + } + $chroot .= array_shift($parts) . '/'; + } + return FALSE; + } + + /** + * Sets the chroot and changes the jail to match the correct path scheme. + */ + function setChroot() { + $this->chroot = $this->findChroot(); + $this->jail = $this->fixRemotePath($this->jail); + } + + /** + * Returns a form to collect connection settings credentials. + * + * Implementing classes can either extend this form with fields collecting the + * specific information they need, or override it entirely. + * + * @return array + * An array that contains a Form API definition. + */ + public function getSettingsForm() { + $form['username'] = array( + '#type' => 'textfield', + '#title' => t('Username'), + ); + $form['password'] = array( + '#type' => 'password', + '#title' => t('Password'), + '#description' => t('Your password is not saved in the database and is only used to establish a connection.'), + ); + $form['advanced'] = array( + '#type' => 'details', + '#title' => t('Advanced settings'), + ); + $form['advanced']['hostname'] = array( + '#type' => 'textfield', + '#title' => t('Host'), + '#default_value' => 'localhost', + '#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.'), + ); + $form['advanced']['port'] = array( + '#type' => 'textfield', + '#title' => t('Port'), + '#default_value' => NULL, + ); + return $form; + } +} diff --git a/core/lib/Drupal/Core/FileTransfer/FileTransferManager.php b/core/lib/Drupal/Core/FileTransfer/FileTransferManager.php new file mode 100644 index 0000000..bdda280 --- /dev/null +++ b/core/lib/Drupal/Core/FileTransfer/FileTransferManager.php @@ -0,0 +1,55 @@ +alterInfo($module_handler, 'filetransfer_info'); + } + + /** + * {@inheritdoc} + */ + protected function findDefinitions() { + $definitions = parent::findDefinitions(); + + uasort($definitions, array('Drupal\Component\Utility\SortArray', 'sortByWeightElement')); + return $definitions; + } + +} diff --git a/core/lib/Drupal/Core/FileTransfer/Ftp.php b/core/lib/Drupal/Core/FileTransfer/Ftp.php new file mode 100644 index 0000000..349c38f --- /dev/null +++ b/core/lib/Drupal/Core/FileTransfer/Ftp.php @@ -0,0 +1,53 @@ +username = $username; + $this->password = $password; + $this->hostname = $hostname; + $this->port = $port; + parent::__construct($jail); + } + + /** + * @{inheritdoc} + */ + static function create($jail, $settings) { + $username = empty($settings['username']) ? '' : $settings['username']; + $password = empty($settings['password']) ? '' : $settings['password']; + $hostname = empty($settings['advanced']['hostname']) ? 'localhost' : $settings['advanced']['hostname']; + $port = empty($settings['advanced']['port']) ? 21 : $settings['advanced']['port']; + + if (function_exists('ftp_connect')) { + $class = 'Drupal\Core\FileTransfer\Plugin\FileTransfer\FTPExtension'; + } + else { + throw new FileTransferException('No FTP backend available.'); + } + + return new $class($jail, $username, $password, $hostname, $port); + } + + /** + * @{inheritdoc} + */ + public function getSettingsForm() { + $form = parent::getSettingsForm(); + $form['advanced']['port']['#default_value'] = 21; + return $form; + } +} diff --git a/core/lib/Drupal/Core/FileTransfer/Local.php b/core/lib/Drupal/Core/FileTransfer/Local.php deleted file mode 100644 index a0973be..0000000 --- a/core/lib/Drupal/Core/FileTransfer/Local.php +++ /dev/null @@ -1,110 +0,0 @@ - $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)); - } - } -} diff --git a/core/lib/Drupal/Core/FileTransfer/Plugin/FileTransfer/FtpExtension.php b/core/lib/Drupal/Core/FileTransfer/Plugin/FileTransfer/FtpExtension.php new file mode 100644 index 0000000..c85afa7 --- /dev/null +++ b/core/lib/Drupal/Core/FileTransfer/Plugin/FileTransfer/FtpExtension.php @@ -0,0 +1,134 @@ +connection = ftp_connect($this->hostname, $this->port); + + if (!$this->connection) { + throw new FileTransferException("Cannot connect to FTP Server, check settings"); + } + if (!ftp_login($this->connection, $this->username, $this->password)) { + throw new FileTransferException("Cannot log in to FTP server. Check username and password"); + } + } + + /** + * Implements Drupal\Core\FileTransfer\FileTransfer::copyFileJailed(). + */ + protected function copyFileJailed($source, $destination) { + if (!@ftp_put($this->connection, $destination, $source, FTP_BINARY)) { + throw new FileTransferException("Cannot move @source to @destination", NULL, array("@source" => $source, "@destination" => $destination)); + } + } + + /** + * Implements Drupal\Core\FileTransfer\FileTransfer::createDirectoryJailed(). + */ + protected function createDirectoryJailed($directory) { + if (!ftp_mkdir($this->connection, $directory)) { + throw new FileTransferException("Cannot create directory @directory", NULL, array("@directory" => $directory)); + } + } + + /** + * Implements Drupal\Core\FileTransfer\FileTransfer::removeDirectoryJailed(). + */ + protected function removeDirectoryJailed($directory) { + $pwd = ftp_pwd($this->connection); + if (!ftp_chdir($this->connection, $directory)) { + throw new FileTransferException("Unable to change to directory @directory", NULL, array('@directory' => $directory)); + } + $list = @ftp_nlist($this->connection, '.'); + if (!$list) { + $list = array(); + } + foreach ($list as $item) { + if ($item == '.' || $item == '..') { + continue; + } + if (@ftp_chdir($this->connection, $item)) { + ftp_cdup($this->connection); + $this->removeDirectory(ftp_pwd($this->connection) . '/' . $item); + } + else { + $this->removeFile(ftp_pwd($this->connection) . '/' . $item); + } + } + ftp_chdir($this->connection, $pwd); + if (!ftp_rmdir($this->connection, $directory)) { + throw new FileTransferException("Unable to remove to directory @directory", NULL, array('@directory' => $directory)); + } + } + + /** + * Implements Drupal\Core\FileTransfer\FileTransfer::removeFileJailed(). + */ + protected function removeFileJailed($destination) { + if (!ftp_delete($this->connection, $destination)) { + throw new FileTransferException("Unable to remove to file @file", NULL, array('@file' => $destination)); + } + } + + /** + * Implements Drupal\Core\FileTransfer\FileTransfer::isDirectory(). + */ + public function isDirectory($path) { + $result = FALSE; + $curr = ftp_pwd($this->connection); + if (@ftp_chdir($this->connection, $path)) { + $result = TRUE; + } + ftp_chdir($this->connection, $curr); + return $result; + } + + /** + * Implements Drupal\Core\FileTransfer\FileTransfer::isFile(). + */ + public function isFile($path) { + return ftp_size($this->connection, $path) != -1; + } + + /** + * Implements Drupal\Core\FileTransfer\ChmodInterface::chmodJailed(). + */ + function chmodJailed($path, $mode, $recursive) { + if (!ftp_chmod($this->connection, $mode, $path)) { + throw new FileTransferException("Unable to set permissions on %file", NULL, array('%file' => $path)); + } + if ($this->isDirectory($path) && $recursive) { + $filelist = @ftp_nlist($this->connection, $path); + if (!$filelist) { + //empty directory - returns false + return; + } + foreach ($filelist as $file) { + $this->chmodJailed($file, $mode, $recursive); + } + } + } +} diff --git a/core/lib/Drupal/Core/FileTransfer/Plugin/FileTransfer/Local.php b/core/lib/Drupal/Core/FileTransfer/Plugin/FileTransfer/Local.php new file mode 100644 index 0000000..eb2b0e1 --- /dev/null +++ b/core/lib/Drupal/Core/FileTransfer/Plugin/FileTransfer/Local.php @@ -0,0 +1,116 @@ + $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)); + } + } +} diff --git a/core/lib/Drupal/Core/FileTransfer/Plugin/FileTransfer/Ssh.php b/core/lib/Drupal/Core/FileTransfer/Plugin/FileTransfer/Ssh.php new file mode 100644 index 0000000..bc71528 --- /dev/null +++ b/core/lib/Drupal/Core/FileTransfer/Plugin/FileTransfer/Ssh.php @@ -0,0 +1,160 @@ +username = $username; + $this->password = $password; + $this->hostname = $hostname; + $this->port = $port; + parent::__construct($jail); + } + + /** + * @{inheritdoc} + */ + function connect() { + $this->connection = @ssh2_connect($this->hostname, $this->port); + if (!$this->connection) { + throw new FileTransferException('SSH Connection failed to @host:@port', NULL, array('@host' => $this->hostname, '@port' => $this->port)); + } + if (!@ssh2_auth_password($this->connection, $this->username, $this->password)) { + throw new FileTransferException('The supplied username/password combination was not accepted.'); + } + } + + /** + * @{inheritdoc} + */ + static function create($jail, $settings) { + $username = empty($settings['username']) ? '' : $settings['username']; + $password = empty($settings['password']) ? '' : $settings['password']; + $hostname = empty($settings['advanced']['hostname']) ? 'localhost' : $settings['advanced']['hostname']; + $port = empty($settings['advanced']['port']) ? 22 : $settings['advanced']['port']; + return new Ssh($jail, $username, $password, $hostname, $port); + } + + /** + * @{inheritdoc} + */ + protected function copyFileJailed($source, $destination) { + if (!@ssh2_scp_send($this->connection, $source, $destination)) { + throw new FileTransferException('Cannot copy @source_file to @destination_file.', NULL, array('@source' => $source, '@destination' => $destination)); + } + } + + /** + * @{inheritdoc} + */ + protected function copyDirectoryJailed($source, $destination) { + if (@!ssh2_exec($this->connection, 'cp -Rp ' . escapeshellarg($source) . ' ' . escapeshellarg($destination))) { + throw new FileTransferException('Cannot copy directory @directory.', NULL, array('@directory' => $source)); + } + } + + /** + * @{inheritdoc} + */ + protected function createDirectoryJailed($directory) { + if (@!ssh2_exec($this->connection, 'mkdir ' . escapeshellarg($directory))) { + throw new FileTransferException('Cannot create directory @directory.', NULL, array('@directory' => $directory)); + } + } + + /** + * @{inheritdoc} + */ + protected function removeDirectoryJailed($directory) { + if (@!ssh2_exec($this->connection, 'rm -Rf ' . escapeshellarg($directory))) { + throw new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $directory)); + } + } + + /** + * @{inheritdoc} + */ + protected function removeFileJailed($destination) { + if (!@ssh2_exec($this->connection, 'rm ' . escapeshellarg($destination))) { + throw new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $destination)); + } + } + + /** + * Implements Drupal\Core\FileTransfer\FileTransfer::isDirectory(). + * + * WARNING: This is untested. It is not currently used, but should do the + * trick. + */ + public function isDirectory($path) { + $directory = escapeshellarg($path); + $cmd = "[ -d {$directory} ] && echo 'yes'"; + if ($output = @ssh2_exec($this->connection, $cmd)) { + if ($output == 'yes') { + return TRUE; + } + return FALSE; + } + else { + throw new FileTransferException('Cannot check @path.', NULL, array('@path' => $path)); + } + } + + /** + * @{inheritdoc} + */ + public function isFile($path) { + $file = escapeshellarg($path); + $cmd = "[ -f {$file} ] && echo 'yes'"; + if ($output = @ssh2_exec($this->connection, $cmd)) { + if ($output == 'yes') { + return TRUE; + } + return FALSE; + } + else { + throw new FileTransferException('Cannot check @path.', NULL, array('@path' => $path)); + } + } + + /** + * @{inheritdoc} + */ + function chmodJailed($path, $mode, $recursive) { + $cmd = sprintf("chmod %s%o %s", $recursive ? '-R ' : '', $mode, escapeshellarg($path)); + if (@!ssh2_exec($this->connection, $cmd)) { + throw new FileTransferException('Cannot change permissions of @path.', NULL, array('@path' => $path)); + } + } + + /** + * @{inheritdoc} + */ + public function getSettingsForm() { + $form = parent::getSettingsForm(); + $form['advanced']['port']['#default_value'] = 22; + return $form; + } +} diff --git a/core/lib/Drupal/Core/FileTransfer/SSH.php b/core/lib/Drupal/Core/FileTransfer/SSH.php deleted file mode 100644 index 82edcda..0000000 --- a/core/lib/Drupal/Core/FileTransfer/SSH.php +++ /dev/null @@ -1,150 +0,0 @@ -username = $username; - $this->password = $password; - $this->hostname = $hostname; - $this->port = $port; - parent::__construct($jail); - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::connect(). - */ - function connect() { - $this->connection = @ssh2_connect($this->hostname, $this->port); - if (!$this->connection) { - throw new FileTransferException('SSH Connection failed to @host:@port', NULL, array('@host' => $this->hostname, '@port' => $this->port)); - } - if (!@ssh2_auth_password($this->connection, $this->username, $this->password)) { - throw new FileTransferException('The supplied username/password combination was not accepted.'); - } - } - - /** - * Overrides Drupal\Core\FileTransfer\FileTransfer::factory(). - */ - static function factory($jail, $settings) { - $username = empty($settings['username']) ? '' : $settings['username']; - $password = empty($settings['password']) ? '' : $settings['password']; - $hostname = empty($settings['advanced']['hostname']) ? 'localhost' : $settings['advanced']['hostname']; - $port = empty($settings['advanced']['port']) ? 22 : $settings['advanced']['port']; - return new SSH($jail, $username, $password, $hostname, $port); - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::copyFileJailed(). - */ - protected function copyFileJailed($source, $destination) { - if (!@ssh2_scp_send($this->connection, $source, $destination)) { - throw new FileTransferException('Cannot copy @source_file to @destination_file.', NULL, array('@source' => $source, '@destination' => $destination)); - } - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::copyDirectoryJailed(). - */ - protected function copyDirectoryJailed($source, $destination) { - if (@!ssh2_exec($this->connection, 'cp -Rp ' . escapeshellarg($source) . ' ' . escapeshellarg($destination))) { - throw new FileTransferException('Cannot copy directory @directory.', NULL, array('@directory' => $source)); - } - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::createDirectoryJailed(). - */ - protected function createDirectoryJailed($directory) { - if (@!ssh2_exec($this->connection, 'mkdir ' . escapeshellarg($directory))) { - throw new FileTransferException('Cannot create directory @directory.', NULL, array('@directory' => $directory)); - } - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::removeDirectoryJailed(). - */ - protected function removeDirectoryJailed($directory) { - if (@!ssh2_exec($this->connection, 'rm -Rf ' . escapeshellarg($directory))) { - throw new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $directory)); - } - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::removeFileJailed(). - */ - protected function removeFileJailed($destination) { - if (!@ssh2_exec($this->connection, 'rm ' . escapeshellarg($destination))) { - throw new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $destination)); - } - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::isDirectory(). - * - * WARNING: This is untested. It is not currently used, but should do the - * trick. - */ - public function isDirectory($path) { - $directory = escapeshellarg($path); - $cmd = "[ -d {$directory} ] && echo 'yes'"; - if ($output = @ssh2_exec($this->connection, $cmd)) { - if ($output == 'yes') { - return TRUE; - } - return FALSE; - } - else { - throw new FileTransferException('Cannot check @path.', NULL, array('@path' => $path)); - } - } - - /** - * Implements Drupal\Core\FileTransfer\FileTransfer::isFile(). - */ - public function isFile($path) { - $file = escapeshellarg($path); - $cmd = "[ -f {$file} ] && echo 'yes'"; - if ($output = @ssh2_exec($this->connection, $cmd)) { - if ($output == 'yes') { - return TRUE; - } - return FALSE; - } - else { - throw new FileTransferException('Cannot check @path.', NULL, array('@path' => $path)); - } - } - - /** - * Implements Drupal\Core\FileTransfer\ChmodInterface::chmodJailed(). - */ - function chmodJailed($path, $mode, $recursive) { - $cmd = sprintf("chmod %s%o %s", $recursive ? '-R ' : '', $mode, escapeshellarg($path)); - if (@!ssh2_exec($this->connection, $cmd)) { - throw new FileTransferException('Cannot change permissions of @path.', NULL, array('@path' => $path)); - } - } - - /** - * Overrides Drupal\Core\FileTransfer\FileTransfer::getSettingsForm(). - */ - public function getSettingsForm() { - $form = parent::getSettingsForm(); - $form['advanced']['port']['#default_value'] = 22; - return $form; - } -} diff --git a/core/modules/system/lib/Drupal/system/Tests/FileTransfer/FileTransferTest.php b/core/modules/system/lib/Drupal/system/Tests/FileTransfer/FileTransferTest.php index a08a42e..fcdc65d 100644 --- a/core/modules/system/lib/Drupal/system/Tests/FileTransfer/FileTransferTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/FileTransfer/FileTransferTest.php @@ -30,7 +30,7 @@ public static function getInfo() { function setUp() { parent::setUp(); - $this->testConnection = TestFileTransfer::factory(DRUPAL_ROOT, array('hostname' => $this->hostname, 'username' => $this->username, 'password' => $this->password, 'port' => $this->port)); + $this->testConnection = TestFileTransfer::create(DRUPAL_ROOT, array('hostname' => $this->hostname, 'username' => $this->username, 'password' => $this->password, 'port' => $this->port)); } function _getFakeModuleFiles() { diff --git a/core/modules/system/lib/Drupal/system/Tests/FileTransfer/TestFileTransfer.php b/core/modules/system/lib/Drupal/system/Tests/FileTransfer/TestFileTransfer.php index 92e736f..2055b98 100644 --- a/core/modules/system/lib/Drupal/system/Tests/FileTransfer/TestFileTransfer.php +++ b/core/modules/system/lib/Drupal/system/Tests/FileTransfer/TestFileTransfer.php @@ -7,13 +7,13 @@ namespace Drupal\system\Tests\FileTransfer; -use Drupal\Core\FileTransfer\FileTransfer; +use Drupal\Core\FileTransfer\FileTransferBase; use Drupal\Core\FileTransfer\FileTransferException; /** * Mock FileTransfer object for test case. */ -class TestFileTransfer extends FileTransfer { +class TestFileTransfer extends FileTransferBase { protected $host = NULL; protected $username = NULL; protected $password = NULL; @@ -24,45 +24,75 @@ class TestFileTransfer extends FileTransfer { */ public $shouldIsDirectoryReturnTrue = FALSE; + /** + * @(inheritdoc) + */ function __construct($jail, $username, $password, $hostname = 'localhost', $port = 9999) { parent::__construct($jail, $username, $password, $hostname, $port); } - static function factory($jail, $settings) { + /** + * @(inheritdoc) + */ + static function create($jail, $settings) { return new TestFileTransfer($jail, $settings['username'], $settings['password'], $settings['hostname'], $settings['port']); } + /** + * @(inheritdoc) + */ function connect() { $this->connection = new MockTestConnection(); $this->connection->connectionString = 'test://' . urlencode($this->username) . ':' . urlencode($this->password) . "@$this->host:$this->port/"; } + /** + * @(inheritdoc) + */ function copyFileJailed($source, $destination) { $this->connection->run("copyFile $source $destination"); } + /** + * @(inheritdoc) + */ protected function removeDirectoryJailed($directory) { $this->connection->run("rmdir $directory"); } + /** + * @(inheritdoc) + */ function createDirectoryJailed($directory) { $this->connection->run("mkdir $directory"); } + /** + * @(inheritdoc) + */ function removeFileJailed($destination) { if (!ftp_delete($this->connection, $item)) { throw new FileTransferException('Unable to remove to file @file.', NULL, array('@file' => $item)); } } + /** + * @(inheritdoc) + */ function isDirectory($path) { return $this->shouldIsDirectoryReturnTrue; } + /** + * @(inheritdoc) + */ function isFile($path) { return FALSE; } + /** + * @(inheritdoc) + */ function chmodJailed($path, $mode, $recursive) { return; } diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index 4447052..5096ba1 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -2755,57 +2755,14 @@ function hook_countries_alter(&$countries) { } /** - * Register information about FileTransfer classes provided by a module. - * - * The FileTransfer class allows transferring files over a specific type of - * connection. Core provides classes for FTP and SSH. Contributed modules are - * free to extend the FileTransfer base class to add other connection types, - * and if these classes are registered via hook_filetransfer_info(), those - * connection types will be available to site administrators using the Update - * manager when they are redirected to the authorize.php script to authorize - * the file operations. - * - * @return array - * Nested array of information about FileTransfer classes. Each key is a - * FileTransfer type (not human readable, used for form elements and - * variable names, etc), and the values are subarrays that define properties - * of that type. The keys in each subarray are: - * - 'title': Required. The human-readable name of the connection type. - * - 'class': Required. The name of the FileTransfer class. The constructor - * will always be passed the full path to the root of the site that should - * be used to restrict where file transfer operations can occur (the $jail) - * and an array of settings values returned by the settings form. - * - 'file': Required. The include file containing the FileTransfer class. - * This should be a separate .inc file, not just the .module file, so that - * the minimum possible code is loaded when authorize.php is running. - * - 'file path': Optional. The directory (relative to the Drupal root) - * where the include file lives. If not defined, defaults to the base - * directory of the module implementing the hook. - * - 'weight': Optional. Integer weight used for sorting connection types on - * the authorize.php form. - * - * @see \Drupal\Core\FileTransfer\FileTransfer - * @see authorize.php - * @see hook_filetransfer_info_alter() - * @see drupal_get_filetransfer_info() - */ -function hook_filetransfer_info() { - $info['sftp'] = array( - 'title' => t('SFTP (Secure FTP)'), - 'class' => 'Drupal\Core\FileTransfer\SFTP', - 'weight' => 10, - ); - return $info; -} - -/** * Alter the FileTransfer class registry. * * @param array $filetransfer_info * Reference to a nested array containing information about the FileTransfer * class registry. * - * @see hook_filetransfer_info() + * @see \Drupal\Core\FileTransfer\FileTransferManager + * @see Drupal\Core\Annotation\FileTransfer */ function hook_filetransfer_info_alter(&$filetransfer_info) { // Remove the FTP option entirely. diff --git a/core/modules/system/system.module b/core/modules/system/system.module index b1664ab..47b8ab0 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -818,7 +818,7 @@ function system_authorized_init($callback, $file, $arguments = array(), $page_ti // First, figure out what file transfer backends the site supports, and put // all of those in the SESSION so that authorize.php has access to all of // them via the class autoloader, even without a full bootstrap. - $_SESSION['authorize_filetransfer_info'] = drupal_get_filetransfer_info(); + $_SESSION['authorize_filetransfer_info'] = Drupal::service('plugin.manager.filetransfer')->getDefinitions(); // Now, define the callback to invoke. $_SESSION['authorize_operation'] = array( @@ -905,30 +905,18 @@ function system_updater_info() { } /** - * Implements hook_filetransfer_info(). + * Implements hook_filetransfer_info_alter(). */ -function system_filetransfer_info() { - $backends = array(); - +function system_filetransfer_info_alter(&$backends) { // This is the default, will be available on most systems. - if (function_exists('ftp_connect')) { - $backends['ftp'] = array( - 'title' => t('FTP'), - 'class' => 'Drupal\Core\FileTransfer\FTP', - 'weight' => 0, - ); + if (!function_exists('ftp_connect')) { + unset($backends['ftp']); } - // SSH2 lib connection is only available if the proper PHP extension is // installed. - if (function_exists('ssh2_connect')) { - $backends['ssh'] = array( - 'title' => t('SSH'), - 'class' => 'Drupal\Core\FileTransfer\SSH', - 'weight' => 20, - ); + if (!function_exists('ssh2_connect')) { + unset($backends['ssh']); } - return $backends; } /** diff --git a/core/modules/system/tests/modules/system_test/lib/Drupal/system_test/MockFileTransfer.php b/core/modules/system/tests/modules/system_test/lib/Drupal/system_test/MockFileTransfer.php deleted file mode 100644 index 900e0be..0000000 --- a/core/modules/system/tests/modules/system_test/lib/Drupal/system_test/MockFileTransfer.php +++ /dev/null @@ -1,36 +0,0 @@ - 'textfield', - '#title' => t('System Test Username'), - ); - return $form; - } -} diff --git a/core/modules/system/tests/modules/system_test/lib/Drupal/system_test/Plugin/FileTransfer/MockFileTransfer.php b/core/modules/system/tests/modules/system_test/lib/Drupal/system_test/Plugin/FileTransfer/MockFileTransfer.php new file mode 100644 index 0000000..65a4539 --- /dev/null +++ b/core/modules/system/tests/modules/system_test/lib/Drupal/system_test/Plugin/FileTransfer/MockFileTransfer.php @@ -0,0 +1,42 @@ + 'textfield', + '#title' => t('System Test Username'), + ); + return $form; + } +} diff --git a/core/modules/system/tests/modules/system_test/system_test.module b/core/modules/system/tests/modules/system_test/system_test.module index 3961833..384d383 100644 --- a/core/modules/system/tests/modules/system_test/system_test.module +++ b/core/modules/system/tests/modules/system_test/system_test.module @@ -164,19 +164,6 @@ function _system_test_second_shutdown_function($arg1, $arg2) { } /** - * Implements hook_filetransfer_info(). - */ -function system_test_filetransfer_info() { - return array( - 'system_test' => array( - 'title' => t('System Test FileTransfer'), - 'class' => 'Drupal\system_test\MockFileTransfer', - 'weight' => -10, - ), - ); -} - -/** * Page callback to initialize authorize.php during testing. * * @see system_authorized_init(). diff --git a/core/modules/update/tests/modules/update_test/lib/Drupal/update_test/MockFileTransfer.php b/core/modules/update/tests/modules/update_test/lib/Drupal/update_test/MockFileTransfer.php deleted file mode 100644 index b47fd6b..0000000 --- a/core/modules/update/tests/modules/update_test/lib/Drupal/update_test/MockFileTransfer.php +++ /dev/null @@ -1,36 +0,0 @@ - 'textfield', - '#title' => t('Update Test Username'), - ); - return $form; - } -} diff --git a/core/modules/update/tests/modules/update_test/lib/Drupal/update_test/Plugin/FileTransfer/MockFileTransfer.php b/core/modules/update/tests/modules/update_test/lib/Drupal/update_test/Plugin/FileTransfer/MockFileTransfer.php new file mode 100644 index 0000000..813ae47 --- /dev/null +++ b/core/modules/update/tests/modules/update_test/lib/Drupal/update_test/Plugin/FileTransfer/MockFileTransfer.php @@ -0,0 +1,42 @@ + 'textfield', + '#title' => t('Update Test Username'), + ); + return $form; + } +} diff --git a/core/modules/update/tests/modules/update_test/update_test.module b/core/modules/update/tests/modules/update_test/update_test.module index 536d330..a66ceaf 100644 --- a/core/modules/update/tests/modules/update_test/update_test.module +++ b/core/modules/update/tests/modules/update_test/update_test.module @@ -121,17 +121,11 @@ function update_test_archiver_info_alter(&$definitions) { } /** - * Implements hook_filetransfer_info(). + * Page callback: Displays an Error 503 (Service unavailable) page. + * + * @see update_test_menu() */ -function update_test_filetransfer_info() { - // Define a mock file transfer method, to ensure that there will always be - // at least one method available in the user interface (regardless of the - // environment in which the update manager tests are run). - return array( - 'system_test' => array( - 'title' => t('Update Test FileTransfer'), - 'class' => 'Drupal\update_test\MockFileTransfer', - 'weight' => -20, - ), - ); +function update_callback_service_unavailable() { + drupal_add_http_header('Status', '503 Service unavailable'); + print "503 Service Temporarily Unavailable"; } diff --git a/core/modules/update/update.manager.inc b/core/modules/update/update.manager.inc index 83203ae..784cc4e 100644 --- a/core/modules/update/update.manager.inc +++ b/core/modules/update/update.manager.inc @@ -37,7 +37,7 @@ */ use Drupal\Core\Updater\Updater; -use Drupal\Core\FileTransfer\Local; +use Drupal\Core\FileTransfer\Plugin\FileTransfer\Local; use Symfony\Component\HttpFoundation\RedirectResponse; /** @@ -588,7 +588,7 @@ function _update_manager_check_backends(&$form, $operation) { '#suffix' => '

', ); - $available_backends = drupal_get_filetransfer_info(); + $available_backends = Drupal::service('plugin.manager.filetransfer')->getDefinitions(); if (empty($available_backends)) { if ($operation == 'update') { $form['available_backends']['#markup'] = t('Your server does not support updating modules and themes from this interface. Instead, update modules and themes by uploading the new versions directly to the server, as described in the handbook.', array('@handbook_url' => 'http://drupal.org/getting-started/install-contrib'));