First commit
This commit is contained in:
commit
c6e2478c40
13918 changed files with 2303184 additions and 0 deletions
427
includes/filetransfer/filetransfer.inc
Normal file
427
includes/filetransfer/filetransfer.inc
Normal file
|
@ -0,0 +1,427 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Base FileTransfer class.
|
||||
*
|
||||
* Classes extending this class perform file operations on directories not
|
||||
* writable by the webserver. To achieve this, the class should connect back
|
||||
* 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.
|
||||
*/
|
||||
abstract class FileTransfer {
|
||||
protected $username;
|
||||
protected $password;
|
||||
protected $hostname = 'localhost';
|
||||
protected $port;
|
||||
|
||||
/**
|
||||
* The constructor for the UpdateConnection class. This method is also called
|
||||
* from the classes that extend this class and override this method.
|
||||
*/
|
||||
function __construct($jail) {
|
||||
$this->jail = $jail;
|
||||
}
|
||||
|
||||
/**
|
||||
* Classes that extend this class must override the factory() static method.
|
||||
*
|
||||
* @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.
|
||||
*/
|
||||
static function factory($jail, $settings) {
|
||||
throw new FileTransferException('FileTransfer::factory() static method not overridden by FileTransfer subclass.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of the magic __get() method.
|
||||
*
|
||||
* If the connection isn't set to anything, this will call the connect() method
|
||||
* and set it to and return the result; afterwards, the connection will be
|
||||
* returned directly without using this method.
|
||||
*/
|
||||
function __get($name) {
|
||||
if ($name == 'connection') {
|
||||
$this->connect();
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
if ($name == 'chroot') {
|
||||
$this->setChroot();
|
||||
return $this->chroot;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to the server.
|
||||
*/
|
||||
abstract protected function connect();
|
||||
|
||||
/**
|
||||
* Copies a directory.
|
||||
*
|
||||
* @param $source
|
||||
* The source path.
|
||||
* @param $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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see http://php.net/chmod
|
||||
*
|
||||
* @param string $path
|
||||
* @param long $mode
|
||||
* @param bool $recursive
|
||||
*/
|
||||
public final function chmod($path, $mode, $recursive = FALSE) {
|
||||
if (!in_array('FileTransferChmodInterface', class_implements(get_class($this)))) {
|
||||
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 $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 $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 $source
|
||||
* The source file.
|
||||
* @param $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 $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 $path
|
||||
* A path to check against the jail.
|
||||
*/
|
||||
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, it is stripped from the path to allow for
|
||||
* chroot'd filetransfer systems.
|
||||
*
|
||||
* @param $path
|
||||
* @param $strip_chroot
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
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
|
||||
* @return string
|
||||
*/
|
||||
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 the $destination is in the jail.
|
||||
*
|
||||
* @param $source
|
||||
* The source path.
|
||||
* @param $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 SkipDotsRecursiveDirectoryIterator($source), 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 $directory
|
||||
* The directory to be created.
|
||||
*/
|
||||
abstract protected function createDirectoryJailed($directory);
|
||||
|
||||
/**
|
||||
* Removes a directory.
|
||||
*
|
||||
* @param $directory
|
||||
* The directory to be removed.
|
||||
*/
|
||||
abstract protected function removeDirectoryJailed($directory);
|
||||
|
||||
/**
|
||||
* Copies a file.
|
||||
*
|
||||
* @param $source
|
||||
* The source file.
|
||||
* @param $destination
|
||||
* The destination file.
|
||||
*/
|
||||
abstract protected function copyFileJailed($source, $destination);
|
||||
|
||||
/**
|
||||
* Removes a file.
|
||||
*
|
||||
* @param $destination
|
||||
* The destination file to be removed.
|
||||
*/
|
||||
abstract protected function removeFileJailed($destination);
|
||||
|
||||
/**
|
||||
* Checks if a particular path is a directory
|
||||
*
|
||||
* @param $path
|
||||
* The path to check
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
abstract public function isDirectory($path);
|
||||
|
||||
/**
|
||||
* Checks if a particular path is a file (not a directory).
|
||||
*
|
||||
* @param $path
|
||||
* The path to check
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
abstract public function isFile($path);
|
||||
|
||||
/**
|
||||
* Return the chroot property for this connection.
|
||||
*
|
||||
* It does this by moving up the tree until it finds itself. If successful,
|
||||
* it will return the chroot, otherwise FALSE.
|
||||
*
|
||||
* @return
|
||||
* The chroot path for this connection or 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 = dirname(__FILE__);
|
||||
$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.
|
||||
*/
|
||||
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' => 'fieldset',
|
||||
'#title' => t('Advanced settings'),
|
||||
'#collapsible' => TRUE,
|
||||
'#collapsed' => TRUE,
|
||||
);
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* FileTransferException class.
|
||||
*/
|
||||
class FileTransferException extends Exception {
|
||||
public $arguments;
|
||||
|
||||
function __construct($message, $code = 0, $arguments = array()) {
|
||||
parent::__construct($message, $code);
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A FileTransfer Class implementing this interface can be used to chmod files.
|
||||
*/
|
||||
interface FileTransferChmodInterface {
|
||||
|
||||
/**
|
||||
* Changes the permissions of the file / directory specified in $path
|
||||
*
|
||||
* @param string $path
|
||||
* Path to change permissions of.
|
||||
* @param long $mode
|
||||
* The new file permission mode to be passed to chmod().
|
||||
* @param boolean $recursive
|
||||
* Pass TRUE to recursively chmod the entire directory specified in $path.
|
||||
*/
|
||||
function chmodJailed($path, $mode, $recursive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides an interface for iterating recursively over filesystem directories.
|
||||
*
|
||||
* Manually skips '.' and '..' directories, since no existing method is
|
||||
* available in PHP 5.2.
|
||||
*
|
||||
* @todo Depreciate in favor of RecursiveDirectoryIterator::SKIP_DOTS once PHP
|
||||
* 5.3 or later is required.
|
||||
*/
|
||||
class SkipDotsRecursiveDirectoryIterator extends RecursiveDirectoryIterator {
|
||||
/**
|
||||
* Constructs a SkipDotsRecursiveDirectoryIterator
|
||||
*
|
||||
* @param $path
|
||||
* The path of the directory to be iterated over.
|
||||
*/
|
||||
function __construct($path) {
|
||||
parent::__construct($path);
|
||||
$this->skipdots();
|
||||
}
|
||||
|
||||
function rewind() {
|
||||
parent::rewind();
|
||||
$this->skipdots();
|
||||
}
|
||||
|
||||
function next() {
|
||||
parent::next();
|
||||
$this->skipdots();
|
||||
}
|
||||
|
||||
protected function skipdots() {
|
||||
while ($this->isDot()) {
|
||||
parent::next();
|
||||
}
|
||||
}
|
||||
}
|
144
includes/filetransfer/ftp.inc
Normal file
144
includes/filetransfer/ftp.inc
Normal file
|
@ -0,0 +1,144 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Base class for FTP implementations.
|
||||
*/
|
||||
abstract class FileTransferFTP extends FileTransfer {
|
||||
|
||||
public function __construct($jail, $username, $password, $hostname, $port) {
|
||||
$this->username = $username;
|
||||
$this->password = $password;
|
||||
$this->hostname = $hostname;
|
||||
$this->port = $port;
|
||||
parent::__construct($jail);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an object which can implement the FTP protocol.
|
||||
*
|
||||
* @param string $jail
|
||||
* @param array $settings
|
||||
* @return FileTransferFTP
|
||||
* The appropriate FileTransferFTP subclass based on the available
|
||||
* options. If the FTP PHP extension is available, use it.
|
||||
*/
|
||||
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 = 'FileTransferFTPExtension';
|
||||
}
|
||||
else {
|
||||
throw new FileTransferException('No FTP backend available.');
|
||||
}
|
||||
|
||||
return new $class($jail, $username, $password, $hostname, $port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the form to configure the FileTransfer class for FTP.
|
||||
*/
|
||||
public function getSettingsForm() {
|
||||
$form = parent::getSettingsForm();
|
||||
$form['advanced']['port']['#default_value'] = 21;
|
||||
return $form;
|
||||
}
|
||||
}
|
||||
|
||||
class FileTransferFTPExtension extends FileTransferFTP implements FileTransferChmodInterface {
|
||||
|
||||
public function connect() {
|
||||
$this->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");
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
protected function createDirectoryJailed($directory) {
|
||||
if (!ftp_mkdir($this->connection, $directory)) {
|
||||
throw new FileTransferException("Cannot create directory @directory", NULL, array("@directory" => $directory));
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
protected function removeFileJailed($destination) {
|
||||
if (!ftp_delete($this->connection, $destination)) {
|
||||
throw new FileTransferException("Unable to remove to file @file", NULL, array('@file' => $destination));
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public function isFile($path) {
|
||||
return ftp_size($this->connection, $path) != -1;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('ftp_chmod')) {
|
||||
function ftp_chmod($ftp_stream, $mode, $filename) {
|
||||
return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
|
||||
}
|
||||
}
|
76
includes/filetransfer/local.inc
Normal file
76
includes/filetransfer/local.inc
Normal file
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* The local connection class for copying files as the httpd user.
|
||||
*/
|
||||
class FileTransferLocal extends FileTransfer implements FileTransferChmodInterface {
|
||||
|
||||
function connect() {
|
||||
// No-op
|
||||
}
|
||||
|
||||
static function factory($jail, $settings) {
|
||||
return new FileTransferLocal($jail);
|
||||
}
|
||||
|
||||
protected function copyFileJailed($source, $destination) {
|
||||
if (@!copy($source, $destination)) {
|
||||
throw new FileTransferException('Cannot copy %source to %destination.', NULL, array('%source' => $source, '%destination' => $destination));
|
||||
}
|
||||
}
|
||||
|
||||
protected function createDirectoryJailed($directory) {
|
||||
if (!is_dir($directory) && @!mkdir($directory, 0777, TRUE)) {
|
||||
throw new FileTransferException('Cannot create directory %directory.', NULL, array('%directory' => $directory));
|
||||
}
|
||||
}
|
||||
|
||||
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 SkipDotsRecursiveDirectoryIterator($directory), 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));
|
||||
}
|
||||
}
|
||||
|
||||
protected function removeFileJailed($file) {
|
||||
if (@!drupal_unlink($file)) {
|
||||
throw new FileTransferException('Cannot remove file %file.', NULL, array('%file' => $file));
|
||||
}
|
||||
}
|
||||
|
||||
public function isDirectory($path) {
|
||||
return is_dir($path);
|
||||
}
|
||||
|
||||
public function isFile($path) {
|
||||
return is_file($path);
|
||||
}
|
||||
|
||||
public function chmodJailed($path, $mode, $recursive) {
|
||||
if ($recursive && is_dir($path)) {
|
||||
foreach (new RecursiveIteratorIterator(new SkipDotsRecursiveDirectoryIterator($path), 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));
|
||||
}
|
||||
}
|
||||
}
|
110
includes/filetransfer/ssh.inc
Normal file
110
includes/filetransfer/ssh.inc
Normal file
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* The SSH connection class for the update module.
|
||||
*/
|
||||
class FileTransferSSH extends FileTransfer implements FileTransferChmodInterface {
|
||||
|
||||
function __construct($jail, $username, $password, $hostname = "localhost", $port = 22) {
|
||||
$this->username = $username;
|
||||
$this->password = $password;
|
||||
$this->hostname = $hostname;
|
||||
$this->port = $port;
|
||||
parent::__construct($jail);
|
||||
}
|
||||
|
||||
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.');
|
||||
}
|
||||
}
|
||||
|
||||
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 FileTransferSSH($jail, $username, $password, $hostname, $port);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
protected function createDirectoryJailed($directory) {
|
||||
if (@!ssh2_exec($this->connection, 'mkdir ' . escapeshellarg($directory))) {
|
||||
throw new FileTransferException('Cannot create directory @directory.', NULL, array('@directory' => $directory));
|
||||
}
|
||||
}
|
||||
|
||||
protected function removeDirectoryJailed($directory) {
|
||||
if (@!ssh2_exec($this->connection, 'rm -Rf ' . escapeshellarg($directory))) {
|
||||
throw new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $directory));
|
||||
}
|
||||
}
|
||||
|
||||
protected function removeFileJailed($destination) {
|
||||
if (!@ssh2_exec($this->connection, 'rm ' . escapeshellarg($destination))) {
|
||||
throw new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $destination));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the form to configure the FileTransfer class for SSH.
|
||||
*/
|
||||
public function getSettingsForm() {
|
||||
$form = parent::getSettingsForm();
|
||||
$form['advanced']['port']['#default_value'] = 22;
|
||||
return $form;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue