summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorAngie Byron <webchick@24967.no-reply.drupal.org>2009-10-15 21:19:31 +0000
committerAngie Byron <webchick@24967.no-reply.drupal.org>2009-10-15 21:19:31 +0000
commit6abcc47e25e936aea84c2f1e287bc5e1a045fff4 (patch)
tree800c3c256246f6c0399b9f3ec89d8a8aa83f5005 /includes
parent945fd9e269fd1ddee861a079660862c56ee54f74 (diff)
downloadbrdo-6abcc47e25e936aea84c2f1e287bc5e1a045fff4.tar.gz
brdo-6abcc47e25e936aea84c2f1e287bc5e1a045fff4.tar.bz2
#538660 by JacobSingh, dww, JoshuaRogers, adrian, Crell, chx, anarcat, and cwgordon7: Add a functioning Plugin Manager to core. Can you say module installation and updates through the UI? I knew you could! :D
Diffstat (limited to 'includes')
-rw-r--r--includes/authorize.inc232
-rw-r--r--includes/common.inc56
-rw-r--r--includes/filetransfer/ftp.inc30
-rw-r--r--includes/theme.maintenance.inc45
-rw-r--r--includes/updater.inc415
5 files changed, 756 insertions, 22 deletions
diff --git a/includes/authorize.inc b/includes/authorize.inc
new file mode 100644
index 000000000..b97d8a288
--- /dev/null
+++ b/includes/authorize.inc
@@ -0,0 +1,232 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Helper functions and form handlers used for the authorize.php script.
+ */
+
+/**
+ * Build the form for choosing a FileTransfer type and supplying credentials.
+ */
+function authorize_filetransfer_form($form_state) {
+ global $base_url;
+ $form = array();
+
+ $form['#action'] = $base_url . '/authorize.php';
+ // CSS we depend on lives in modules/system/maintenance.css, which is loaded
+ // via the default maintenance theme.
+ $form['#attached']['js'][] = $base_url . '/misc/authorize.js';
+
+ // Get all the available ways to transfer files.
+ if (empty($_SESSION['authorize_filetransfer_backends'])) {
+ drupal_set_message(t('Unable to continue, no available methods of file transfer'), 'error');
+ return array();
+ }
+ $available_backends = $_SESSION['authorize_filetransfer_backends'];
+ uasort($available_backends, 'drupal_sort_weight');
+
+ // Decide on a default backend.
+ if (isset($form_state['values']['connection_settings']['authorize_filetransfer_default'])) {
+ $authorize_filetransfer_default = $form_state['values']['connection_settings']['authorize_filetransfer_default'];
+ }
+ elseif ($authorize_filetransfer_default = variable_get('authorize_filetransfer_default', NULL));
+ else {
+ $authorize_filetransfer_default = key($available_backends);
+ }
+
+ $form['information']['main_header'] = array(
+ '#prefix' => '<h3>',
+ '#markup' => t('To continue please provide your server connection details'),
+ '#suffix' => '</h3>',
+ );
+
+ $form['connection_settings']['#tree'] = TRUE;
+ $form['connection_settings']['authorize_filetransfer_default'] = array(
+ '#type' => 'select',
+ '#title' => t('Connection method'),
+ '#default_value' => $authorize_filetransfer_default,
+ '#weight' => -10,
+ );
+
+ /*
+ * Here we create two submit buttons. For a JS enabled client, they will
+ * only ever see submit_process. However, if a client doesn't have JS
+ * enabled, they will see submit_connection on the first form (whden picking
+ * what filetranfer type to use, and submit_process on the second one (which
+ * leads to the actual operation)
+ */
+ $form['submit_connection'] = array(
+ '#prefix' => "<br style='clear:both'/>",
+ '#name' => 'enter_connection_settings', // This is later changed in JS.
+ '#type' => 'submit',
+ '#value' => t('Enter connetion settings'), // As is this. @see authorize.js.
+ '#weight' => 100,
+ );
+
+ $form['submit_process'] = array(
+ '#name' => 'process_updates', // This is later changed in JS.
+ '#type' => 'submit',
+ '#value' => t('Process Updates'), // As is this. @see authorize.js
+ '#weight' => 100,
+ '#attributes' => array('style' => 'display:none'),
+ );
+
+ // Build a hidden fieldset for each one.
+ foreach ($available_backends as $name => $backend) {
+ $form['connection_settings']['authorize_filetransfer_default']['#options'][$name] = $backend['title'];
+ $form['connection_settings'][$name] = array(
+ '#type' => 'fieldset',
+ '#attributes' => array('class' => "filetransfer-$name filetransfer"),
+ '#title' => t('@backend connection settings', array('@backend' => $backend['title'])),
+ );
+
+ $current_settings = variable_get("authorize_filetransfer_connection_settings_" . $name, array());
+ $form['connection_settings'][$name] += system_get_filetransfer_settings_form($name, $current_settings);
+
+ // Start non-JS code.
+ if (isset($form_state['values']['connection_settings']['authorize_filetransfer_default']) && $form_state['values']['connection_settings']['authorize_filetransfer_default'] == $name) {
+
+ // If the user switches from JS to non-JS, Drupal (and Batch API) will
+ // barf. This is a known bug: http://drupal.org/node/229825.
+ setcookie('has_js', '', time() - 3600, '/');
+ unset($_COOKIE['has_js']);
+
+ // Change the submit button to the submit_process one.
+ $form['submit_process']['#attributes'] = array();
+ unset($form['submit_connection']);
+
+ // Activate the proper filetransfer settings form.
+ $form['connection_settings'][$name]['#attributes']['style'] = 'display:block';
+ // Disable the select box.
+ $form['connection_settings']['authorize_filetransfer_default']['#disabled'] = TRUE;
+
+ // Create a button for changing the type of connection.
+ $form['connection_settings']['change_connection_type'] = array(
+ '#name' => 'change_connection_type',
+ '#type' => 'submit',
+ '#value' => t('Change connection type'),
+ '#weight' => -5,
+ '#attributes' => array('class' => 'filetransfer-change-connection-type'),
+ );
+ }
+ // End non-JS code.
+ }
+ return $form;
+}
+
+/**
+ * Validate callback for the filetransfer authorization form.
+ *
+ * @see authorize_filetransfer_form()
+ */
+function authorize_filetransfer_form_validate($form, &$form_state) {
+ if (isset($form_state['values']['connection_settings'])) {
+ $backend = $form_state['values']['connection_settings']['authorize_filetransfer_default'];
+ $filetransfer = authorize_get_filetransfer($backend, $form_state['values']['connection_settings'][$backend]);
+ try {
+ if (!$filetransfer) {
+ throw new Exception(t("Error, this type of connection protocol (%backend) doesn't exist.", array('%backend' => $backend)));
+ }
+ $filetransfer->connect();
+ }
+ catch (Exception $e) {
+ form_set_error('connection_settings', $e->getMessage());
+ }
+ }
+}
+
+/**
+ * Submit callback when a file transfer is being authorized.
+ *
+ * @see authorize_filetransfer_form()
+ */
+function authorize_filetransfer_form_submit($form, &$form_state) {
+ global $base_url;
+ switch ($form_state['clicked_button']['#name']) {
+ case 'process_updates':
+
+ // Save the connection settings to the DB.
+ $filetransfer_backend = $form_state['values']['connection_settings']['authorize_filetransfer_default'];
+
+ // If the database is available then try to save our settings. We have
+ // to make sure it is available since this code could potentially (will
+ // likely) be called during the installation process, before the
+ // database is set up.
+ if (db_is_active()) {
+ $connection_settings = array();
+ foreach ($form_state['values']['connection_settings'][$filetransfer_backend] as $key => $value) {
+ // We do *not* want to store passwords in the database, unless the
+ // backend explicitly says so via the magic #filetransfer_save form
+ // property. Otherwise, we store everything that's not explicitly
+ // marked with #filetransfer_save set to FALSE.
+ if (!isset($form['connection_settings'][$filetransfer_backend][$key]['#filetransfer_save'])) {
+ if ($form['connection_settings'][$filetransfer_backend][$key]['#type'] != 'password') {
+ $connection_settings[$key] = $value;
+ }
+ }
+ // The attribute is defined, so only save if set to TRUE.
+ elseif ($form['connection_settings'][$filetransfer_backend][$key]['#filetransfer_save']) {
+ $connection_settings[$key] = $value;
+ }
+ }
+ // Set this one as the default authorize method.
+ variable_set('authorize_filetransfer_default', $filetransfer_backend);
+ // Save the connection settings minus the password.
+ variable_set("authorize_filetransfer_connection_settings_" . $filetransfer_backend, $connection_settings);
+
+ $filetransfer = authorize_get_filetransfer($filetransfer_backend, $form_state['values']['connection_settings'][$filetransfer_backend]);
+
+ // Now run the operation.
+ authorize_run_operation($filetransfer);
+ }
+ break;
+
+ case 'enter_connection_settings':
+ $form_state['rebuild'] = TRUE;
+ break;
+
+ case 'change_connection_type':
+ $form_state['rebuild'] = TRUE;
+ unset($form_state['values']['connection_settings']['authorize_filetransfer_default']);
+ break;
+ }
+}
+
+/**
+ * Run the operation specified in $_SESSION['authorize_operation']
+ *
+ * @param $filetransfer
+ * The FileTransfer object to use for running the operation.
+ */
+function authorize_run_operation($filetransfer) {
+ $operation = $_SESSION['authorize_operation'];
+ unset($_SESSION['authorize_operation']);
+
+ if (!empty($operation['page_title'])) {
+ drupal_set_title(check_plain($operation['page_title']));
+ }
+
+ require_once DRUPAL_ROOT . '/' . $operation['file'];
+ call_user_func_array($operation['callback'], array_merge(array($filetransfer), $operation['arguments']));
+}
+
+/**
+ * Get a FileTransfer class for a specific transfer method and settings.
+ *
+ * @param $backend
+ * The FileTransfer backend to get the class for.
+ * @param $settings
+ * Array of settings for the FileTransfer.
+ * @return
+ * An instantiated FileTransfer object for the requested method and settings,
+ * or FALSE if there was an error finding or instantiating it.
+ */
+function authorize_get_filetransfer($backend, $settings = array()) {
+ $filetransfer = FALSE;
+ if (!empty($_SESSION['authorize_filetransfer_backends'][$backend])) {
+ $filetransfer = call_user_func_array(array($_SESSION['authorize_filetransfer_backends'][$backend]['class'], 'factory'), array(DRUPAL_ROOT, $settings));
+ }
+ return $filetransfer;
+}
+
diff --git a/includes/common.inc b/includes/common.inc
index a72039566..bcc2cbd22 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -4993,23 +4993,10 @@ function drupal_common_theme() {
'arguments' => array('page' => NULL),
'template' => 'page',
),
- 'maintenance_page' => array(
- 'arguments' => array('content' => NULL, 'show_messages' => TRUE),
- 'template' => 'maintenance-page',
- ),
- 'update_page' => array(
- 'arguments' => array('content' => NULL, 'show_messages' => TRUE),
- ),
- 'install_page' => array(
- 'arguments' => array('content' => NULL),
- ),
'region' => array(
'arguments' => array('elements' => NULL),
'template' => 'region',
),
- 'task_list' => array(
- 'arguments' => array('items' => NULL, 'active' => NULL),
- ),
'status_messages' => array(
'arguments' => array('display' => NULL),
),
@@ -5064,6 +5051,26 @@ function drupal_common_theme() {
'indentation' => array(
'arguments' => array('size' => 1),
),
+ // from theme.maintenance.inc
+ 'maintenance_page' => array(
+ 'arguments' => array('content' => NULL, 'show_messages' => TRUE),
+ 'template' => 'maintenance-page',
+ ),
+ 'update_page' => array(
+ 'arguments' => array('content' => NULL, 'show_messages' => TRUE),
+ ),
+ 'install_page' => array(
+ 'arguments' => array('content' => NULL),
+ ),
+ 'task_list' => array(
+ 'arguments' => array('items' => NULL, 'active' => NULL),
+ ),
+ 'authorize_message' => array(
+ 'arguments' => array('message' => NULL, 'success' => TRUE),
+ ),
+ 'authorize_report' => array(
+ 'arguments' => array('messages' => array()),
+ ),
// from pager.inc
'pager' => array(
'arguments' => array('tags' => array(), 'element' => 0, 'parameters' => array(), 'quantity' => 9),
@@ -5989,3 +5996,26 @@ function archiver_get_archiver($file) {
}
}
+/**
+ * Drupal Updater registry.
+ *
+ * An Updater is a class that knows how to update various parts of the Drupal
+ * file system, for example to update modules that have newer releases, or to
+ * install a new theme.
+ *
+ * @return
+ * Returns the Drupal Updater class registry.
+ *
+ * @see hook_updater_info()
+ * @see hook_updater_info_alter()
+ */
+function drupal_get_updaters() {
+ $updaters = &drupal_static(__FUNCTION__);
+ if (!isset($updaters)) {
+ $updaters = module_invoke_all('updater_info');
+ drupal_alter('updater_info', $updaters);
+ uasort($updaters, 'drupal_sort_weight');
+ }
+ return $updaters;
+}
+
diff --git a/includes/filetransfer/ftp.inc b/includes/filetransfer/ftp.inc
index 32e02901a..48b745ab2 100644
--- a/includes/filetransfer/ftp.inc
+++ b/includes/filetransfer/ftp.inc
@@ -5,6 +5,15 @@
* Connection class using the FTP URL wrapper.
*/
class FileTransferFTPWrapper 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);
+ }
+
function connect() {
$this->connection = 'ftp://' . urlencode($this->username) . ':' . urlencode($this->password) . '@' . $this->hostname . ':' . $this->port . '/';
if (!is_dir($this->connection)) {
@@ -19,29 +28,29 @@ class FileTransferFTPWrapper extends FileTransfer {
}
function createDirectoryJailed($directory) {
- if (!@drupal_mkdir($directory)) {
+ if (!@drupal_mkdir($this->connection . $directory)) {
$exception = new FileTransferException('Cannot create directory @directory.', NULL, array('@directory' => $directory));
throw $exception;
}
}
function removeDirectoryJailed($directory) {
- if (is_dir($directory)) {
- $dh = opendir($directory);
+ if (is_dir($this->connection . $directory)) {
+ $dh = opendir($this->connection . $directory);
while (($resource = readdir($dh)) !== FALSE) {
if ($resource == '.' || $resource == '..') {
continue;
}
$full_path = $directory . DIRECTORY_SEPARATOR . $resource;
- if (is_file($full_path)) {
+ if (is_file($this->connection . $full_path)) {
$this->removeFile($full_path);
}
- elseif (is_dir($full_path)) {
+ elseif (is_dir($this->connection . $full_path)) {
$this->removeDirectory($full_path . '/');
}
}
closedir($dh);
- if (!rmdir($directory)) {
+ if (!rmdir($this->connection . $directory)) {
$exception = new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $directory));
throw $exception;
}
@@ -70,15 +79,18 @@ class FileTransferFTPWrapper extends FileTransfer {
}
/**
- * This is impossible with the stream wrapper,
- * So we cheat and use the other implementation
+ * This is impossible with the stream wrapper, so an exception is thrown.
+ *
+ * If the ftp extenstion is available, we will cheat and use it.
*
- * @staticvar FileTransferFTPExtension $ftp_ext_file_transfer
* @param string $path
* @param long $mode
* @param bool $recursive
*/
function chmodJailed($path, $mode, $recursive) {
+ if (!function_exists('ftp_connect')) {
+ throw new FileTransferException('Unable to set permissions on @path. Change umask settings on server to be world executable.', array('@path' => $path));
+ }
static $ftp_ext_file_transfer;
if (!$ftp_ext_file_transfer) {
diff --git a/includes/theme.maintenance.inc b/includes/theme.maintenance.inc
index e2ebc9342..a625e15c0 100644
--- a/includes/theme.maintenance.inc
+++ b/includes/theme.maintenance.inc
@@ -202,3 +202,48 @@ function theme_update_page($variables) {
return theme_render_template('themes/garland/maintenance-page.tpl.php', $variables);
}
+
+/**
+ * Generate a report of the results from an operation run via authorize.php.
+ *
+ * @param array $variables
+ * - messages: An array of result messages.
+ */
+function theme_authorize_report($variables) {
+ $messages = $variables['messages'];
+ $output = '';
+ if (!empty($messages)) {
+ $output .= '<div id="authorize-results">';
+ foreach ($messages as $heading => $logs) {
+ $output .= '<h3>' . check_plain($heading) . '</h3>';
+ foreach ($logs as $number => $log_message) {
+ if ($number === '#abort') {
+ continue;
+ }
+ $output .= theme('authorize_message', array('message' => $log_message['message'], 'success' => $log_message['success']));
+ }
+ }
+ $output .= '</div>';
+ }
+ return $output;
+}
+
+/**
+ * Render a single log message from the authorize.php batch operation.
+ *
+ * @param $variables
+ * - message: The log message.
+ * - success: A boolean indicating failure or success.
+ */
+function theme_authorize_message($variables) {
+ $output = '';
+ $message = $variables['message'];
+ $success = $variables['success'];
+ if ($success) {
+ $output .= '<li class="success">' . $message . '</li>';
+ }
+ else {
+ $output .= '<li class="failure"><strong>' . t('Failed') . ':</strong> ' . $message . '</li>';
+ }
+ return $output;
+}
diff --git a/includes/updater.inc b/includes/updater.inc
new file mode 100644
index 000000000..e11f8e1af
--- /dev/null
+++ b/includes/updater.inc
@@ -0,0 +1,415 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Classes used for updating various files in the Drupal webroot. These
+ * classes use a FileTransfer object to actually perform the operations.
+ * Normally, the FileTransfer is provided when the site owner is redirected to
+ * authorize.php as part of a multistep process.
+ */
+
+/**
+ * Interface for a class which can update a Drupal project.
+ *
+ * An Updater currently serves the following purposes:
+ * - It can take a given directory, and determine if it can operate on it.
+ * - It can move the contents of that directoy into the appropriate place
+ * on the system using FileTransfer classes.
+ * - It can return a list of "next steps" after an update or install.
+ * - In the future, it will most likely perform some of those steps as well.
+ */
+interface DrupalUpdaterInterface {
+
+ /**
+ * Checks if the project is installed.
+ * @return bool
+ */
+ public function isInstalled();
+
+ /**
+ * Returns the system name of the project.
+ *
+ * @param string $directory
+ * A directory containing a project.
+ */
+ public static function getProjectName($directory);
+
+ /**
+ * @return string an absolute path to the default install location.
+ */
+ public function getInstallDirectory();
+
+ /**
+ * Determine if the Updater can handle the project provided in $directory.
+ *
+ * @todo: Provide something more rational here, like a project spec file.
+ *
+ * @param string $directory
+ *
+ * @return bool
+ */
+ public static function canUpdateDirectory($directory);
+
+ /**
+ * Actions to run after an install has occurred.
+ */
+ public function postInstall();
+
+ /**
+ * Actions to run after an update has occurred.
+ */
+ public function postUpdate();
+}
+
+/**
+ * Base class for Updaters used in Drupal.
+ */
+class Updater {
+
+ /**
+ * @var string $source Directory to install from.
+ */
+ public $source;
+
+ public function __construct($source) {
+ $this->source = $source;
+ $this->name = self::getProjectName($source);
+ $this->title = self::getProjectTitle($source);
+ }
+
+ /**
+ * Return an Updater of the appropriate type depending on the source.
+ *
+ * If a directory is provided which contains a module, will return a
+ * ModuleUpdater.
+ *
+ * @param string $source
+ * Directory of a Drupal project.
+ *
+ * @return object Updater
+ */
+ public static function factory($source) {
+ if (is_dir($source)) {
+ $updater = self::getUpdaterFromDirectory($source);
+ }
+ else {
+ throw new UpdaterException(t('Unable to determine the type of the source directory.'));
+ }
+ return new $updater($source);
+ }
+
+ /**
+ * Determine which Updater class can operate on the given directory.
+ *
+ * @param string $directory
+ * Extracted Drupal project.
+ * @return string
+ * The class name which can work with this project type.
+ */
+ public static function getUpdaterFromDirectory($directory) {
+ // Gets a list of possible implementing classes.
+ $updaters = drupal_get_updaters();
+ foreach ($updaters as $updater) {
+ $class = $updater['class'];
+ if (call_user_func("{$class}::canUpdateDirectory", $directory)) {
+ return $class;
+ }
+ }
+ throw new UpdaterException(t('Cannot determine the type of project.'));
+ }
+
+ /**
+ * Figure out what the most important (or only) info file is in a directory.
+ *
+ * Since there is no enforcement of which info file is the project's "main"
+ * info file, this will get one with the same name as the directory, or the
+ * first one it finds. Not ideal, but needs a larger solution.
+ *
+ * @param string $directory
+ * Directory to search in.
+ * @return string
+ * Path to the info file.
+ */
+ public static function findInfoFile($directory) {
+ $info_files = file_scan_directory($directory, '/.*\.info/');
+ if (!$info_files) {
+ return FALSE;
+ }
+ foreach ($info_files as $info_file) {
+ if (drupal_substr($info_file->filename, 0, -5) == basename($directory)) {
+ // Info file Has the same name as the directory, return it.
+ return $info_file->uri;
+ }
+ }
+ // Otherwise, return the first one.
+ $info_file = array_shift($info_files);
+ return $info_file->uri;
+ }
+
+ /**
+ * Get the name of the project directory (basename).
+ *
+ * @todo: It would be nice, if projects contained an info file which could
+ * provide their canonical name.
+ *
+ * @param string $directory
+ * @return string
+ */
+ public static function getProjectName($directory) {
+ return basename($directory);
+ }
+
+ /**
+ * Return the project name from a Drupal info file.
+ *
+ * @param string $directory
+ * Directory to search for the info file.
+ * @return string
+ */
+ public static function getProjectTitle($directory) {
+ $info_file = self::findInfoFile($directory);
+ $info = drupal_parse_info_file($info_file);
+ if (!$info) {
+ throw new UpdaterException(t('Unable to parse info file.'));
+ }
+ return $info['name'];
+ }
+
+ /**
+ * Store the default parameters for the Updater.
+ *
+ * @param array $overrides
+ * An array of overrides for the default parameters.
+ * @return array
+ * An array of configuration parameters for an update or install operation.
+ */
+ protected function getInstallArgs($overrides = array()) {
+ $args = array(
+ 'make_backup' => FALSE,
+ 'install_dir' => $this->getInstallDirectory(),
+ 'backup_dir' => $this->getBackupDir(),
+ );
+ return array_merge($args, $overrides);
+ }
+
+ /**
+ * Updates a Drupal project, returns a list of next actions.
+ *
+ * @param FileTransfer $filetransfer
+ * Object which is a child of FileTransfer. Used for moving files
+ * to the server.
+ * @param array $overrides
+ * An array of settings to override defaults
+ * @see self::getInstallArgs
+ * @return array
+ * An array of links which the user may need to complete the update
+ */
+ public function update(&$filetransfer, $overrides = array()) {
+ try {
+ // Establish arguments with possible overrides.
+ $args = $this->getInstallArgs($overrides);
+
+ // Take a Backup.
+ if ($args['make_backup']) {
+ $this->makeBackup($args['install_dir'], $args['backup_dir']);
+ }
+
+ if (!$this->name) {
+ // This is bad, don't want to delete the install directory.
+ throw new UpdaterException(t('Fatal error in update, cowardly refusing to wipe out the install directory.'));
+ }
+
+ // Make sure the installation parent directory exists and is writable.
+ $this->prepareInstallDirectory($filetransfer, $args['install_dir']);
+
+ // Note: If the project is installed in sites/all, it will not be
+ // deleted. It will be installed in sites/default as that will override
+ // the sites/all reference and not break other sites which are using it.
+ if (is_dir($args['install_dir'] . '/' . $this->name)) {
+ // Remove the existing installed file.
+ $filetransfer->removeDirectory($args['install_dir'] . '/' . $this->name);
+ }
+
+ // Copy the directory in place.
+ $filetransfer->copyDirectory($this->source, $args['install_dir']);
+
+ // Make sure what we just installed is readable by the web server.
+ $this->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
+
+ // Run the updates.
+ // @TODO: decide if we want to implement this.
+ $this->postUpdate();
+
+ // For now, just return a list of links of things to do.
+ return $this->postUpdateTasks();
+ }
+ catch (FileTransferException $e) {
+ throw new UpdaterFileTransferException(t('File Transfer failed, reason: !reason', array('!reason' => strtr($e->getMessage(), $e->arguments))));
+ }
+ }
+
+ /**
+ * Installs a Drupal project, returns a list of next actions.
+ *
+ * @param FileTransfer $filetransfer
+ * Object which is a child of FileTransfer.
+ * @param array $overrides
+ * An array of settings to override defaults.
+ * @see self::getInstallArgs
+ * @return array
+ * An array of links which the user may need to complete the install.
+ */
+ public function install(&$filetransfer, $overrides = array()) {
+ try {
+ // Establish arguments with possible overrides.
+ $args = $this->getInstallArgs($overrides);
+
+ // Make sure the installation parent directory exists and is writable.
+ $this->prepareInstallDirectory($filetransfer, $args['install_dir']);
+
+ // Copy the directory in place.
+ $filetransfer->copyDirectory($this->source, $args['install_dir']);
+
+ // Make sure what we just installed is readable by the web server.
+ $this->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
+
+ // Potentially enable something?
+ // @TODO: decide if we want to implement this.
+ $this->postInstall();
+ // For now, just return a list of links of things to do.
+ return $this->postInstallTasks();
+ }
+ catch (FileTransferException $e) {
+ throw new UpdaterFileTransferException(t('File Transfer failed, reason: !reason', array('!reason' => strtr($e->getMessage(), $e->arguments))));
+ }
+ }
+
+ /**
+ * Make sure the installation parent directory exists and is writable.
+ *
+ * @param FileTransfer $filetransfer
+ * Object which is a child of FileTransfer.
+ * @param string $directory
+ * The installation directory to prepare.
+ */
+ public function prepareInstallDirectory(&$filetransfer, $directory) {
+ // Make the parent dir writable if need be and create the dir.
+ if (!is_dir($directory)) {
+ $parent_dir = dirname($directory);
+ if (!is_writable($parent_dir)) {
+ @chmod($parent_dir, 0755);
+ // It is expected that this will fail if the directory is owned by the
+ // FTP user. If the FTP user == web server, it will succeed.
+ try {
+ $filetransfer->createDirectory($directory);
+ $this->makeWorldReadable($filetransfer, $directory);
+ }
+ catch (FileTransferException $e) {
+ // Probably still not writable. Try to chmod and do it again.
+ // @todo: Make a new exception class so we can catch it differently.
+ try {
+ $old_perms = substr(sprintf('%o', fileperms($parent_dir)), -4);
+ $filetransfer->chmod($parent_dir, 0755);
+ $filetransfer->createDirectory($directory);
+ $this->makeWorldReadable($filetransfer, $directory);
+ // Put the permissions back.
+ $filetransfer->chmod($parent_dir, intval($old_perms, 8));
+ }
+ catch (FileTransferException $e) {
+ $message = t($e->getMessage(), $e->arguments);
+ $throw_message = t('Unable to create %directory due to the following: %reason', array('%directory' => $install_location, '%reason' => $message));
+ throw new UpdaterException($throw_message);
+ }
+ }
+ // Put the parent directory back.
+ @chmod($parent_dir, 0555);
+ }
+ }
+ }
+
+ /**
+ * Ensure that a given directory is world readable.
+ *
+ * @param FileTransfer $filetransfer
+ * Object which is a child of FileTransfer.
+ * @param string $path
+ * The file path to make world readable.
+ * @param bool $recursive
+ * If the chmod should be applied recursively.
+ */
+ public function makeWorldReadable(&$filetransfer, $path, $recursive = TRUE) {
+ if (!is_executable($path)) {
+ // Set it to read + execute.
+ $new_perms = substr(sprintf('%o', fileperms($path)), -4, -1) . "5";
+ $filetransfer->chmod($path, intval($new_perms, 8), $recursive);
+ }
+ }
+
+ /**
+ * Perform a backup.
+ *
+ * @todo Not implemented.
+ */
+ public function makeBackup(&$filetransfer, $from, $to) {
+ }
+
+ /**
+ * Return the full path to a directory where backups should be written.
+ */
+ public function getBackupDir() {
+ return file_directory_path('temporary');
+ }
+
+ /**
+ * Perform actions after new code is updated.
+ */
+ public function postUpdate() {
+ }
+
+ /**
+ * Perform actions after installation.
+ */
+ public function postInstall() {
+ }
+
+ /**
+ * Return an array of links to pages that should be visited post operation.
+ *
+ * @return array
+ * Links which provide actions to take after the install is finished.
+ */
+ public function postInstallTasks() {
+ return array();
+ }
+
+ /**
+ * Return an array of links to pages that should be visited post operation.
+ *
+ * @return array
+ * Links which provide actions to take after the update is finished.
+ */
+ public function postUpdateTasks() {
+ return array();
+ }
+}
+
+/**
+ * Exception class for the Updater class hierarchy.
+ *
+ * This is identical to the base Exception class, we just give it a more
+ * specific name so that call sites that want to tell the difference can
+ * specifically catch these exceptions and treat them differently.
+ */
+class UpdaterException extends Exception {
+}
+
+/**
+ * Child class of UpdaterException that indicates a FileTransfer exception.
+ *
+ * We have to catch FileTransfer exceptions and wrap those in t(), since
+ * FileTransfer is so low-level that it doesn't use any Drupal APIs and none
+ * of the strings are translated.
+ */
+class UpdaterFileTransferException extends UpdaterException {
+}