diff options
Diffstat (limited to 'modules/system')
-rw-r--r-- | modules/system/system.api.php | 109 | ||||
-rw-r--r-- | modules/system/system.module | 86 | ||||
-rw-r--r-- | modules/system/system.test | 53 |
3 files changed, 124 insertions, 124 deletions
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'); + } +} |