summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorAngie Byron <webchick@24967.no-reply.drupal.org>2010-12-01 00:23:36 +0000
committerAngie Byron <webchick@24967.no-reply.drupal.org>2010-12-01 00:23:36 +0000
commit9f5cd5395a9bae17fbbacf11da5bb1e90fda82a6 (patch)
tree0961b4fd416bf22236517894b982bdbe017141db /modules
parent5c3fdc3ee1635b06a4136e7accc0a7f360840e92 (diff)
downloadbrdo-9f5cd5395a9bae17fbbacf11da5bb1e90fda82a6.tar.gz
brdo-9f5cd5395a9bae17fbbacf11da5bb1e90fda82a6.tar.bz2
#609772 by dww, JacobSingh, ksenzee: Fixed Impossible to extend the FileTransfer class system in contrib
Diffstat (limited to 'modules')
-rw-r--r--modules/simpletest/tests/system_test.module48
-rw-r--r--modules/system/system.api.php109
-rw-r--r--modules/system/system.module86
-rw-r--r--modules/system/system.test53
-rw-r--r--modules/update/update.manager.inc12
5 files changed, 184 insertions, 124 deletions
diff --git a/modules/simpletest/tests/system_test.module b/modules/simpletest/tests/system_test.module
index 76d2701cf..4d7042e72 100644
--- a/modules/simpletest/tests/system_test.module
+++ b/modules/simpletest/tests/system_test.module
@@ -16,6 +16,12 @@ function system_test_menu() {
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
+ $items['system-test/authorize-init/%'] = array(
+ 'page callback' => 'system_test_authorize_init_page',
+ 'page arguments' => array(2),
+ 'access arguments' => array('administer software updates'),
+ 'type' => MENU_CALLBACK,
+ );
$items['system-test/redirect/%'] = array(
'title' => 'Redirect',
'page callback' => 'system_test_redirect',
@@ -311,3 +317,45 @@ function _system_test_second_shutdown_function($arg1, $arg2) {
throw new Exception('Drupal is <blink>awesome</blink>.');
}
+/**
+ * Implements hook_filetransfer_info().
+ */
+function system_test_filetransfer_info() {
+ return array(
+ 'system_test' => array(
+ 'title' => t('System Test FileTransfer'),
+ 'file' => 'system_test.module', // Should be a .inc, but for test, ok.
+ 'class' => 'SystemTestFileTransfer',
+ 'weight' => -10,
+ ),
+ );
+}
+
+/**
+ * Mock FileTransfer object to test the settings form functionality.
+ */
+class SystemTestFileTransfer {
+ public static function factory() {
+ return new SystemTestFileTransfer;
+ }
+
+ public function getSettingsForm() {
+ $form = array();
+ $form['system_test_username'] = array(
+ '#type' => 'textfield',
+ '#title' => t('System Test Username'),
+ );
+ return $form;
+ }
+}
+
+/**
+ * Page callback to initialize authorize.php during testing.
+ *
+ * @see system_authorized_init().
+ */
+function system_test_authorize_init_page($page_title) {
+ $authorize_url = $GLOBALS['base_url'] . '/authorize.php';
+ system_authorized_init('system_test_authorize_run', drupal_get_path('module', 'system_test') . '/system_test.module', array(), $page_title);
+ drupal_goto($authorize_url);
+}
diff --git a/modules/system/system.api.php b/modules/system/system.api.php
index be46d5d10..259c608a4 100644
--- a/modules/system/system.api.php
+++ b/modules/system/system.api.php
@@ -4279,52 +4279,6 @@ function hook_countries_alter(&$countries) {
}
/**
- * Provide information on available file transfer backends.
- *
- * File transfer backends are used by modules to transfer files from remote
- * locations to Drupal sites. For instance, update.module uses a file transfer
- * backend to download new versions of modules and themes from drupal.org.
- *
- * @return
- * An associative array of information about the file transfer backend(s).
- * being provided. This array can contain the following keys:
- * - title: Title of the backend to be shown to the end user.
- * - class: Name of the PHP class which implements this backend.
- * - settings_form: An optional callback function that provides additional
- * configuration information required by this backend (for instance a port
- * number.)
- * - weight: Controls what order the backends are presented to the user.
- *
- * @see authorize.php
- * @see FileTransfer
- */
-function hook_filetransfer_backends() {
- $backends = array();
-
- // This is the default, will be available on most systems.
- if (function_exists('ftp_connect')) {
- $backends['ftp'] = array(
- 'title' => t('FTP'),
- 'class' => 'FileTransferFTP',
- 'settings_form' => 'system_filetransfer_backend_form_ftp',
- 'weight' => 0,
- );
- }
-
- // 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;
-}
-
-/**
* Control site status before menu dispatching.
*
* The hook is called after checking whether the site is offline but before
@@ -4350,5 +4304,68 @@ function hook_menu_site_status_alter(&$menu_site_status, $path) {
}
/**
+ * Register information about FileTransfer classes provided by a module.
+ *
+ * The FileTransfer class allows transfering 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 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)'),
+ 'file' => 'sftp.filetransfer.inc',
+ 'class' => 'FileTransferSFTP',
+ '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()
+ */
+function hook_filetransfer_info_alter(&$filetransfer_info) {
+ if (variable_get('paranoia', FALSE)) {
+ // Remove the FTP option entirely.
+ unset($filetransfer_info['ftp']);
+ // Make sure the SSH option is listed first.
+ $filetransfer_info['ssh']['weight'] = -10;
+ }
+}
+
+/**
* @} End of "addtogroup hooks".
*/
diff --git a/modules/system/system.module b/modules/system/system.module
index 5d2e91f17..8a415ab38 100644
--- a/modules/system/system.module
+++ b/modules/system/system.module
@@ -1725,7 +1725,7 @@ function _system_themes_access($theme) {
*
* @see authorize.php
* @see FileTransfer
- * @see hook_filetransfer_backends()
+ * @see hook_filetransfer_info()
*/
/**
@@ -1756,7 +1756,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_backends'] = module_invoke_all('filetransfer_backends');
+ $_SESSION['authorize_filetransfer_info'] = drupal_get_filetransfer_info();
// Now, define the callback to invoke.
$_SESSION['authorize_operation'] = array(
@@ -1831,9 +1831,9 @@ function system_updater_info() {
}
/**
- * Implements hook_filetransfer_backends().
+ * Implements hook_filetransfer_info().
*/
-function system_filetransfer_backends() {
+function system_filetransfer_info() {
$backends = array();
// This is the default, will be available on most systems.
@@ -1841,7 +1841,8 @@ function system_filetransfer_backends() {
$backends['ftp'] = array(
'title' => t('FTP'),
'class' => 'FileTransferFTP',
- 'settings_form' => 'system_filetransfer_backend_form_ftp',
+ 'file' => 'ftp.inc',
+ 'file path' => 'includes/filetransfer',
'weight' => 0,
);
}
@@ -1852,7 +1853,8 @@ function system_filetransfer_backends() {
$backends['ssh'] = array(
'title' => t('SSH'),
'class' => 'FileTransferSSH',
- 'settings_form' => 'system_filetransfer_backend_form_ssh',
+ 'file' => 'ssh.inc',
+ 'file path' => 'includes/filetransfer',
'weight' => 20,
);
}
@@ -1860,78 +1862,6 @@ function system_filetransfer_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['advanced']['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['advanced']['port']['#default_value'] = 22;
- return $form;
-}
-
-/**
- * Helper function because SSH and FTP backends share the same elements
- */
-function _system_filetransfer_backend_form_common() {
- $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;
-}
-
-/**
* Implements hook_init().
*/
function system_init() {
diff --git a/modules/system/system.test b/modules/system/system.test
index 65c3c587a..549df44b0 100644
--- a/modules/system/system.test
+++ b/modules/system/system.test
@@ -2092,3 +2092,56 @@ class SystemAdminTestCase extends DrupalWebTestCase {
$this->assertTrue($this->cookies['Drupal.visitor.admin_compact_mode']['value'], t('Compact mode persists on new requests.'));
}
}
+
+/**
+ * Tests authorize.php and related hooks.
+ */
+class SystemAuthorizeCase extends DrupalWebTestCase {
+ public static function getInfo() {
+ return array(
+ 'name' => 'Authorize API',
+ 'description' => 'Tests the authorize.php script and related API.',
+ 'group' => 'System',
+ );
+ }
+
+ function setUp() {
+ parent::setUp(array('system_test'));
+
+ variable_set('allow_authorize_operations', TRUE);
+
+ // Create an administrator user.
+ $this->admin_user = $this->drupalCreateUser(array('administer software updates'));
+ $this->drupalLogin($this->admin_user);
+ }
+
+ /**
+ * Helper function to initialize authorize.php and load it via drupalGet().
+ *
+ * Initializing authorize.php needs to happen in the child Drupal
+ * installation, not the parent. So, we visit a menu callback provided by
+ * system_test.module which calls system_authorized_init() to initialize the
+ * $_SESSION inside the test site, not the framework site. This callback
+ * redirects to authorize.php when it's done initializing.
+ *
+ * @see system_authorized_init().
+ */
+ function drupalGetAuthorizePHP($page_title = 'system-test-auth') {
+ $this->drupalGet('system-test/authorize-init/' . $page_title);
+ }
+
+ /**
+ * Tests the FileTransfer hooks
+ */
+ function testFileTransferHooks() {
+ $page_title = $this->randomName(16);
+ $this->drupalGetAuthorizePHP($page_title);
+ $this->assertTitle(strtr('@title | Drupal', array('@title' => $page_title)), 'authorize.php page title is correct.');
+ $this->assertNoText('It appears you have reached this page in error.');
+ $this->assertText('To continue, provide your server connection details');
+ // Make sure we see the new connection method added by system_test.
+ $this->assertRaw('System Test FileTransfer');
+ // Make sure the settings form callback works.
+ $this->assertText('System Test Username');
+ }
+}
diff --git a/modules/update/update.manager.inc b/modules/update/update.manager.inc
index efc2d281e..1bc3ce586 100644
--- a/modules/update/update.manager.inc
+++ b/modules/update/update.manager.inc
@@ -654,6 +654,18 @@ function update_manager_archive_extract($file, $directory) {
if (!$archiver) {
throw new Exception(t('Cannot extract %file, not a valid archive.', array ('%file' => $file)));
}
+
+ // Remove the directory if it exists, otherwise it might contain a mixture of
+ // old files mixed with the new files (e.g. in cases where files were removed
+ // from a later release).
+ $files = $archiver->listContents();
+ // Unfortunately, we can only use the directory name for this. :(
+ $project = drupal_substr($files[0], 0, -1);
+ $extract_location = $directory . '/' . $project;
+ if (file_exists($extract_location)) {
+ file_unmanaged_delete_recursive($extract_location);
+ }
+
$archiver->extract($directory);
return $archiver;
}