Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Definition of Drupal\Core\FileTransfer\FileTransfer.
*/
namespace Drupal\Core\FileTransfer;
/**
* Defines the base FileTransfer class.
Dries Buytaert
committed
*
Dries Buytaert
committed
* Classes extending this class perform file operations on directories not
* writable by the webserver. To achieve this, the class should connect back
Dries Buytaert
committed
* to the server using some backend (for example FTP or SSH). To keep security,
* the password should always be asked from the user and never stored. For
* safety, all methods operate only inside a "jail", by default the Drupal root.
Dries Buytaert
committed
*/
abstract class FileTransfer {
/**
* The username for this file transfer.
*
* @var string
*/
Dries Buytaert
committed
protected $username;
/**
* The password for this file transfer.
*
* @var string
*/
Dries Buytaert
committed
protected $password;
/**
* The hostname for this file transfer.
*
* @var string
*/
Dries Buytaert
committed
protected $hostname = 'localhost';
/**
* The port for this file transfer.
*
* @var int
*/
Dries Buytaert
committed
protected $port;
Dries Buytaert
committed
/**
* Constructs a Drupal\Core\FileTransfer\FileTransfer object.
*
* @param $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.
Dries Buytaert
committed
*/
Dries Buytaert
committed
function __construct($jail) {
Dries Buytaert
committed
$this->jail = $jail;
Dries Buytaert
committed
}
Dries Buytaert
committed
/**
* Defines a factory method for this class.
*
Dries Buytaert
committed
* Classes that extend this class must override the factory() static method.
* They should return a new instance of the appropriate FileTransfer subclass.
Dries Buytaert
committed
*
* @param string $jail
Angie Byron
committed
* 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.
Dries Buytaert
committed
* @param array $settings
Angie Byron
committed
* An array of connection settings for the FileTransfer subclass. If the
* getSettingsForm() method uses any nested settings, the same structure
* will be assumed here.
*
Angie Byron
committed
* @return object
* New instance of the appropriate FileTransfer subclass.
*
Alex Pott
committed
* @throws \Drupal\Core\FileTransfer\FileTransferException
Dries Buytaert
committed
*/
static function factory($jail, $settings) {
throw new FileTransferException('FileTransfer::factory() static method not overridden by FileTransfer subclass.');
}
Dries Buytaert
committed
Dries Buytaert
committed
/**
* 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.
Dries Buytaert
committed
*
* @param string $name
* The name of the variable to return.
*
* @return string|bool
* The variable specified in $name.
Dries Buytaert
committed
*/
function __get($name) {
if ($name == 'connection') {
Dries Buytaert
committed
$this->connect();
Dries Buytaert
committed
return $this->connection;
}
Dries Buytaert
committed
if ($name == 'chroot') {
$this->setChroot();
return $this->chroot;
}
Dries Buytaert
committed
}
Dries Buytaert
committed
/**
* Connects to the server.
Dries Buytaert
committed
*/
abstract public function connect();
Dries Buytaert
committed
/**
* Copies a directory.
*
* @param string $source
Dries Buytaert
committed
* The source path.
* @param string $destination
Dries Buytaert
committed
* The destination path.
*/
public final function copyDirectory($source, $destination) {
Dries Buytaert
committed
$source = $this->sanitizePath($source);
$destination = $this->fixRemotePath($destination);
Dries Buytaert
committed
$this->checkPath($destination);
$this->copyDirectoryJailed($source, $destination);
}
Dries Buytaert
committed
/**
* Changes the permissions of the specified $path (file or directory).
Dries Buytaert
committed
*
* @param string $path
* The file / directory to change the permissions of.
* @param int $mode
* The new file permission mode to be passed to chmod().
Dries Buytaert
committed
* @param bool $recursive
* Pass TRUE to recursively chmod the entire directory specified in $path.
*
Alex Pott
committed
* @throws \Drupal\Core\FileTransfer\FileTransferException
*
* @see http://php.net/chmod
Dries Buytaert
committed
*/
public final function chmod($path, $mode, $recursive = FALSE) {
if (!($this instanceof ChmodInterface)) {
Angie Byron
committed
throw new FileTransferException('Unable to change file permissions');
}
Dries Buytaert
committed
$path = $this->sanitizePath($path);
$path = $this->fixRemotePath($path);
$this->checkPath($path);
$this->chmodJailed($path, $mode, $recursive);
}
Dries Buytaert
committed
Dries Buytaert
committed
/**
* Creates a directory.
*
* @param string $directory
Dries Buytaert
committed
* The directory to be created.
*/
public final function createDirectory($directory) {
Dries Buytaert
committed
$directory = $this->fixRemotePath($directory);
Dries Buytaert
committed
$this->checkPath($directory);
$this->createDirectoryJailed($directory);
}
/**
* Removes a directory.
*
* @param string $directory
Dries Buytaert
committed
* The directory to be removed.
*/
public final function removeDirectory($directory) {
Dries Buytaert
committed
$directory = $this->fixRemotePath($directory);
Dries Buytaert
committed
$this->checkPath($directory);
$this->removeDirectoryJailed($directory);
}
/**
* Copies a file.
*
* @param string $source
Dries Buytaert
committed
* The source file.
* @param string $destination
Dries Buytaert
committed
* The destination file.
*/
public final function copyFile($source, $destination) {
Dries Buytaert
committed
$source = $this->sanitizePath($source);
$destination = $this->fixRemotePath($destination);
Dries Buytaert
committed
$this->checkPath($destination);
$this->copyFileJailed($source, $destination);
}
/**
* Removes a file.
*
* @param string $destination
Dries Buytaert
committed
* The destination file to be removed.
*/
public final function removeFile($destination) {
Dries Buytaert
committed
$destination = $this->fixRemotePath($destination);
Dries Buytaert
committed
$this->checkPath($destination);
$this->removeFileJailed($destination);
}
/**
* Checks that the path is inside the jail and throws an exception if not.
*
* @param string $path
Dries Buytaert
committed
* A path to check against the jail.
*
Alex Pott
committed
* @throws \Drupal\Core\FileTransfer\FileTransferException
Dries Buytaert
committed
*/
protected final function checkPath($path) {
Dries Buytaert
committed
$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) {
Dries Buytaert
committed
throw new FileTransferException('@directory is outside of the @jail', NULL, array('@directory' => $path, '@jail' => $this->jail));
}
}
Dries Buytaert
committed
/**
* 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.
Dries Buytaert
committed
*
Dries Buytaert
committed
* @return string
* The modified path.
Dries Buytaert
committed
*/
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;
}
/**
Dries Buytaert
committed
* Changes backslashes to slashes, also removes a trailing slash.
*
* @param string $path
* The path to modify.
*
* @return string
* The modified path.
*/
Dries Buytaert
committed
function sanitizePath($path) {
$path = str_replace('\\', '/', $path); // Windows path sanitization.
Dries Buytaert
committed
if (substr($path, -1) == '/') {
$path = substr($path, 0, -1);
}
return $path;
}
Dries Buytaert
committed
/**
* Copies a directory.
*
* We need a separate method to make sure the $destination is in the jail.
Dries Buytaert
committed
*
* @param string $source
Dries Buytaert
committed
* The source path.
* @param string $destination
Dries Buytaert
committed
* The destination path.
*/
Dries Buytaert
committed
protected function copyDirectoryJailed($source, $destination) {
if ($this->isDirectory($destination)) {
$destination = $destination . '/' . drupal_basename($source);
Dries Buytaert
committed
}
$this->createDirectory($destination);
Angie Byron
committed
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $filename => $file) {
Dries Buytaert
committed
$relative_path = substr($filename, strlen($source));
Dries Buytaert
committed
if ($file->isDir()) {
$this->createDirectory($destination . $relative_path);
}
else {
$this->copyFile($file->getPathName(), $destination . $relative_path);
}
}
}
/**
* Creates a directory.
*
* @param string $directory
Dries Buytaert
committed
* The directory to be created.
*/
Dries Buytaert
committed
abstract protected function createDirectoryJailed($directory);
Dries Buytaert
committed
/**
* Removes a directory.
*
* @param string $directory
Dries Buytaert
committed
* The directory to be removed.
*/
Dries Buytaert
committed
abstract protected function removeDirectoryJailed($directory);
Dries Buytaert
committed
/**
* Copies a file.
*
* @param string $source
Dries Buytaert
committed
* The source file.
* @param string $destination
Dries Buytaert
committed
* The destination file.
*/
Dries Buytaert
committed
abstract protected function copyFileJailed($source, $destination);
Dries Buytaert
committed
/**
* Removes a file.
*
* @param string $destination
Dries Buytaert
committed
* The destination file to be removed.
*/
Dries Buytaert
committed
abstract protected function removeFileJailed($destination);
Dries Buytaert
committed
Dries Buytaert
committed
/**
* Checks if a particular path is a directory.
Dries Buytaert
committed
*
* @param string $path
Dries Buytaert
committed
* The path to check
*
* @return bool
* TRUE if the specified path is a directory, FALSE otherwise.
Dries Buytaert
committed
*/
abstract public function isDirectory($path);
Dries Buytaert
committed
/**
* Checks if a particular path is a file (not a directory).
*
* @param string $path
* The path to check.
Dries Buytaert
committed
*
* @return bool
* TRUE if the specified path is a file, FALSE otherwise.
Dries Buytaert
committed
*/
abstract public function isFile($path);
/**
* Returns the chroot property for this connection.
Dries Buytaert
committed
*
* It does this by moving up the tree until it finds itself
Angie Byron
committed
*
* @return string|bool
* If successful, the chroot path for this connection, otherwise FALSE.
Dries Buytaert
committed
*/
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__;
Dries Buytaert
committed
$path = $this->fixRemotePath($path, FALSE);
$parts = explode('/', $path);
$chroot = '';
while (count($parts)) {
$check = implode($parts, '/');
if ($this->isFile($check . '/' . drupal_basename(__FILE__))) {
Dries Buytaert
committed
// Remove the trailing slash.
return substr($chroot, 0, -1);
Dries Buytaert
committed
}
$chroot .= array_shift($parts) . '/';
}
return FALSE;
}
/**
* Sets the chroot and changes the jail to match the correct path scheme.
Dries Buytaert
committed
*/
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;
}