summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2009-07-01 13:44:53 +0000
committerDries Buytaert <dries@buytaert.net>2009-07-01 13:44:53 +0000
commitb300a7a5936260bca7b0ec598cb1473814726f26 (patch)
tree737054b5247649f4069574a80a15a79cf0148df5 /modules
parent5962cc5ab22bc07995b5886305255f93cab2a165 (diff)
downloadbrdo-b300a7a5936260bca7b0ec598cb1473814726f26.tar.gz
brdo-b300a7a5936260bca7b0ec598cb1473814726f26.tar.bz2
- Patch #395472 by JacobSingh, chx, cwgordon7: more improvements to the file transfer system to pave the path for a plugin manager. Heroic effort.
Diffstat (limited to 'modules')
-rw-r--r--modules/simpletest/simpletest.info1
-rw-r--r--modules/simpletest/tests/filetransfer.test199
-rw-r--r--modules/system/system.module149
3 files changed, 318 insertions, 31 deletions
diff --git a/modules/simpletest/simpletest.info b/modules/simpletest/simpletest.info
index a90e5dd2c..0a0737070 100644
--- a/modules/simpletest/simpletest.info
+++ b/modules/simpletest/simpletest.info
@@ -19,6 +19,7 @@ files[] = tests/common.test
files[] = tests/database_test.test
files[] = tests/error.test
files[] = tests/file.test
+files[] = tests/filetransfer.test
files[] = tests/form.test
files[] = tests/graph.test
files[] = tests/image.test
diff --git a/modules/simpletest/tests/filetransfer.test b/modules/simpletest/tests/filetransfer.test
new file mode 100644
index 000000000..795cd3d27
--- /dev/null
+++ b/modules/simpletest/tests/filetransfer.test
@@ -0,0 +1,199 @@
+<?php
+// $Id$
+
+
+class FileTranferTest extends DrupalWebTestCase {
+ protected $hostname = 'localhost';
+ protected $username = 'drupal';
+ protected $password = 'password';
+ protected $port = '42';
+
+ public static function getInfo() {
+ return array(
+ 'name' => t('FileTransfer unit tests'),
+ 'description' => t('Test that the jail is respected and that protocols using recursive file move operations work.'),
+ 'group' => t('System')
+ );
+ }
+
+ function setUp() {
+ $this->testConnection = TestFileTransfer::factory(DRUPAL_ROOT, array('hostname' => $this->hostname, 'username' => $this->username, 'password' => $this->password, 'port' => $this->port));
+ }
+
+ function _getFakeModuleFiles() {
+ $files = array(
+ 'fake.module',
+ 'fake.info',
+ 'theme' => array(
+ 'fake.tpl.php'
+ ),
+ 'inc' => array(
+ 'fake.inc'
+ )
+ );
+ return $files;
+ }
+
+ function _buildFakeModule() {
+ $location = file_directory_temp() . '/fake';
+ if (is_dir($location)) {
+ $ret = 0;
+ $output = array();
+ exec('rm -Rf ' . escapeshellarg($location), $output, $ret);
+ if ($ret != 0) {
+ throw new Exception('Error removing fake module directory.');
+ }
+ }
+
+ $files = $this->_getFakeModuleFiles();
+ $this->_writeDirectory($location, $files);
+ return $location;
+ }
+
+ function _writeDirectory($base, $files = array()) {
+ mkdir($base);
+ foreach ($files as $key => $file) {
+ if (is_array($file)) {
+ $this->_writeDirectory($base . DIRECTORY_SEPARATOR . $key, $file);
+ }
+ else {
+ //just write the filename into the file
+ file_put_contents($base . DIRECTORY_SEPARATOR . $file, $file);
+ }
+ }
+ }
+
+ function testJail() {
+ $source = $this->_buildFakeModule();
+
+ // This convoluted piece of code is here because our testing framework does
+ // not support expecting exceptions.
+ $gotit = FALSE;
+ try {
+ $this->testConnection->copyDirectory($source, '/tmp');
+ }
+ catch (FileTransferException $e) {
+ $gotit = TRUE;
+ }
+ $this->assertTrue($gotit, 'Was not able to copy a directory outside of the jailed area.');
+
+ $gotit = TRUE;
+ try {
+ $this->testConnection->copyDirectory($source, DRUPAL_ROOT . '/'. file_directory_path());
+ }
+ catch (FileTransferException $e) {
+ $gotit = FALSE;
+ }
+ $this->assertTrue($gotit, 'Was able to copy a directory inside of the jailed area');
+ }
+
+ function testCopyDirectory() {
+ $directory = $this->_buildFakeModule();
+ $drupal_root = DRUPAL_ROOT;
+
+ $this->testConnection->shouldIsDirectoryReturnTrue = TRUE;
+ $this->testConnection->copyDirectory($directory, "{$drupal_root}/sites/all/modules");
+ $expected_commands = array(
+ "mkdir {$drupal_root}/sites/all/modules/fake",
+ "copyFile {$directory}/fake.info {$drupal_root}/sites/all/modules/fake/fake.info",
+ "copyFile {$directory}/fake.module {$drupal_root}/sites/all/modules/fake/fake.module",
+ "mkdir {$drupal_root}/sites/all/modules/fake/inc",
+ "copyFile {$directory}/inc/fake.inc {$drupal_root}/sites/all/modules/fake/inc/fake.inc",
+ "mkdir {$drupal_root}/sites/all/modules/fake/theme",
+ "copyFile {$directory}/theme/fake.tpl.php {$drupal_root}/sites/all/modules/fake/theme/fake.tpl.php",
+ );
+
+ $received_commands = $this->testConnection->connection->flushCommands();
+ $this->assertEqual($received_commands, $expected_commands, 'Expected copy files operations made to sites/all/modules');
+
+ $this->testConnection->shouldIsDirectoryReturnTrue = FALSE;
+ $this->testConnection->copyDirectory($directory, "{$drupal_root}/sites/all/modules/fake");
+ $expected_commands = array(
+ "mkdir {$drupal_root}/sites/all/modules/fake",
+ "copyFile {$directory}/fake.info {$drupal_root}/sites/all/modules/fake/fake.info",
+ "copyFile {$directory}/fake.module {$drupal_root}/sites/all/modules/fake/fake.module",
+ "mkdir {$drupal_root}/sites/all/modules/fake/inc",
+ "copyFile {$directory}/inc/fake.inc {$drupal_root}/sites/all/modules/fake/inc/fake.inc",
+ "mkdir {$drupal_root}/sites/all/modules/fake/theme",
+ "copyFile {$directory}/theme/fake.tpl.php {$drupal_root}/sites/all/modules/fake/theme/fake.tpl.php",
+ );
+
+ $received_commands = $this->testConnection->connection->flushCommands();
+ dd($expected_commands);
+ dd($received_commands);
+ $this->assertEqual($received_commands, $expected_commands, 'Expected copy files operations made to sites/all/modules/fake');
+
+
+ }
+}
+
+/**
+ * Mock FileTransfer object for test case.
+ */
+class TestFileTransfer extends FileTransfer {
+ protected $host = NULL;
+ protected $username = NULL;
+ protected $password = NULL;
+ protected $port = NULL;
+
+ /**
+ * This is for testing the CopyRecursive logic.
+ */
+ public $shouldIsDirectoryReturnTrue = FALSE;
+
+ function __construct($jail, $username, $password, $hostname = 'localhost', $port = 9999) {
+ parent::__construct($jail, $username, $password, $hostname, $port);
+ }
+
+ static function factory($jail, $settings) {
+ return new TestFileTransfer($jail, $settings['username'], $settings['password'], $settings['hostname'], $settings['port']);
+ }
+
+ function connect() {
+ $parts = explode(':', $this->hostname);
+ $port = (count($parts) == 2) ? $parts[1] : $this->port;
+ $this->connection = new MockTestConnection();
+ $this->connection->connectionString = 'test://' . urlencode($this->username) . ':' . urlencode($this->password) . "@$this->host:$this->port/";
+ }
+
+ function copyFileJailed($source, $destination) {
+ $this->connection->run("copyFile $source $destination");
+ }
+
+ protected function removeDirectoryJailed($directory) {
+ $this->connection->run("rmdir $directory");
+ }
+
+ function createDirectoryJailed($directory) {
+ $this->connection->run("mkdir $directory");
+ }
+
+ function removeFileJailed($destination) {
+ if (!ftp_delete($this->connection, $item)) {
+ throw new FileTransferException('Unable to remove to file @file.', NULL, array('@file' => $item));
+ }
+ }
+
+ function isDirectory($path) {
+ return $this->shouldIsDirectoryReturnTrue;
+ }
+}
+
+/**
+ * Mock connection object for test case.
+ */
+class MockTestConnection {
+
+ var $commandsRun = array();
+ var $connectionString;
+
+ function run($cmd) {
+ $this->commandsRun[] = $cmd;
+ }
+
+ function flushCommands() {
+ $out = $this->commandsRun;
+ $this->commandsRun = array();
+ return $out;
+ }
+}
diff --git a/modules/system/system.module b/modules/system/system.module
index 567cf8989..e8472de88 100644
--- a/modules/system/system.module
+++ b/modules/system/system.module
@@ -828,36 +828,115 @@ function system_admin_menu_block_access($path, $permission) {
}
/**
- * Implementation of hook_filetransfer_backends().
+ * Implement hook_filetransfer_backends().
*/
function system_filetransfer_backends() {
$backends = array();
- // 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' => 'FileTransferSSH',
- );
- }
+ //This is the default, will be available on most systems
if (function_exists('ftp_connect')) {
$backends['ftp_extension'] = array(
- 'title' => t('FTP Extension'),
+ 'title' => t('FTP'),
'class' => 'FileTransferFTPExtension',
+ 'settings_form' => 'system_filetransfer_backend_form_ftp',
+ 'weight' => 0,
);
}
-
+
if (ini_get('allow_url_fopen')) {
$backends['ftp_wrapper'] = array(
- 'title' => t('FTP Wrapper'),
+ 'title' => t('FTP using file streams'),
'class' => 'FileTransferFTPWrapper',
+ 'settings_form' => 'system_filetransfer_backend_form_ftp',
+ 'weight' => 10,
+ );
+ }
+
+ // 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' => 'FileTransferSSH',
+ 'settings_form' => 'system_filetransfer_backend_form_ssh',
+ 'weight' => 20,
);
}
return $backends;
}
/**
+ * Helper function to return a form for configuring a filetransfer backend.
+ *
+ * @param string $filetransfer_backend_name
+ * The name of the backend to return a form for.
+ *
+ * @param string $defaults
+ * An associative array of settings to pre-populate the form with.
+ */
+function system_get_filetransfer_settings_form($filetransfer_backend_name, $defaults) {
+ $available_backends = module_invoke_all('filetransfer_backends');
+ $form = call_user_func($available_backends[$filetransfer_backend_name]['settings_form']);
+
+ foreach ($form as $name => &$element) {
+ if (isset($defaults[$name])) {
+ $element['#default_value'] = $defaults[$name];
+ }
+ }
+ return $form;
+}
+
+/**
+ * Returns the form to configure the filetransfer class for FTP
+ */
+function system_filetransfer_backend_form_ftp() {
+ $form = _system_filetransfer_backend_form_common();
+ $form['port']['#default_value'] = 21;
+ return $form;
+}
+
+/**
+ * Returns the form to configure the filetransfer class for SSH
+ */
+function system_filetransfer_backend_form_ssh() {
+ $form = _system_filetransfer_backend_form_common();
+ $form['port']['#default_value'] = 22;
+ return $form;
+}
+
+/**
+ * Helper function because SSH and FTP backends share the same elements
+ */
+function _system_filetransfer_backend_form_common() {
+ $form = array();
+
+ $form['hostname'] = array (
+ '#type' => 'textfield',
+ '#title' => t('Host'),
+ '#default_value' => 'localhost',
+ );
+
+ $form['port'] = array (
+ '#type' => 'textfield',
+ '#title' => t('Port'),
+ '#default_value' => NULL,
+ );
+
+ $form['username'] = array (
+ '#type' => 'textfield',
+ '#title' => t('Username'),
+ );
+
+ $form['password'] = array (
+ '#type' => 'password',
+ '#title' => t('Password'),
+ '#description' => t('This is not saved in the database and is only used to test the connection'),
+ );
+
+ return $form;
+}
+
+/**
* Implement hook_init().
*/
function system_init() {
@@ -2567,29 +2646,37 @@ function system_image_toolkits() {
/**
* Attempts to get a file using drupal_http_request and to store it locally.
*
- * @param $path
+ * @param $url
* The URL of the file to grab.
+ *
+ * @param $destination
+ * Where the file should be saved, if a directory is provided, file is saved
+ * in that directory with its original name. If a filename is provided,
+ * remote fileis stored to that location. NOTE: Relative to drupal "files" directory"
+ *
+ * @param $overwrite boolean
+ * Defaults to TRUE, will overwrite existing files of the same name.
+ *
* @return
* On success the address the files was saved to, FALSE on failure.
*/
-function system_retrieve_file($path) {
- // Get each of the specified files.
- $parsed_url = parse_url($path);
- $local = file_directory_temp() . '/update-cache/' . basename($parsed_url['path']);
- if (!file_exists(file_directory_temp() . '/update-cache/')) {
- mkdir(file_directory_temp() . '/update-cache/');
- }
-
- // Check the cache and download the file if needed.
- if (!file_exists($local)) {
- // $result->data is the actual contents of the downloaded file. This saves
- // it into a local file, whose path is stored in $local. $local is stored
- // relative to the Drupal installation.
- $result = drupal_http_request($path);
- if ($result->code != 200 || !file_save_data($result->data, $local)) {
- drupal_set_message(t('@remote could not be saved.', array('@remote' => $path)), 'error');
- return FALSE;
- }
+function system_retrieve_file($url, $destination = NULL, $overwrite = TRUE) {
+ if (!$destination) {
+ $destination = file_directory_temp();
+ }
+ $parsed_url = parse_url($url);
+ $local = is_dir(file_directory_path() . '/' . $destination) ? $destination . '/' . basename($parsed_url['path']) : $destination;
+
+ if (!$overwrite && file_exists($local)) {
+ drupal_set_message(t('@remote could not be saved. @local already exists', array('@remote' => $url, '@local' => $local)), 'error');
+ return FALSE;
+ }
+
+ $result = drupal_http_request($url);
+ if ($result->code != 200 || !file_save_data($result->data, $local)) {
+ drupal_set_message(t('@remote could not be saved.', array('@remote' => $url)), 'error');
+ return FALSE;
}
+
return $local;
}