/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $local_cache = NULL; if ($form_state->getValue('project_url')) { $local_cache = update_manager_file_get($form_state->getValue('project_url')); if (!$local_cache) { drupal_set_message($this->t('Unable to retrieve Drupal project from %url.', array('%url' => $form_state->getValue('project_url'))), 'error'); return; } } elseif ($_FILES['files']['name']['project_upload']) { $validators = array('file_validate_extensions' => array(archiver_get_extensions())); if (!($finfo = file_save_upload('project_upload', $validators, NULL, 0, FILE_EXISTS_REPLACE))) { // Failed to upload the file. file_save_upload() calls // drupal_set_message() on failure. return; } $local_cache = $finfo->getFileUri(); } $directory = _update_manager_extract_directory(); try { $archive = update_manager_archive_extract($local_cache, $directory); } catch (\Exception $e) { drupal_set_message($e->getMessage(), 'error'); return; } $files = $archive->listContents(); if (!$files) { drupal_set_message($this->t('Provided archive contains no files.'), 'error'); return; } // Unfortunately, we can only use the directory name to determine the // project name. Some archivers list the first file as the directory (i.e., // MODULE/) and others list an actual file (i.e., MODULE/README.TXT). $project = strtok($files[0], '/\\'); $archive_errors = $this->moduleHandler->invokeAll('verify_update_archive', array($project, $local_cache, $directory)); if (!empty($archive_errors)) { drupal_set_message(array_shift($archive_errors), 'error'); // @todo: Fix me in D8: We need a way to set multiple errors on the same // form element and have all of them appear! if (!empty($archive_errors)) { foreach ($archive_errors as $error) { drupal_set_message($error, 'error'); } } return; } // Make sure the Updater registry is loaded. drupal_get_updaters(); $project_location = $directory . '/' . $project; try { $updater = Updater::factory($project_location, $this->root); } catch (\Exception $e) { drupal_set_message($e->getMessage(), 'error'); return; } try { $project_title = Updater::getProjectTitle($project_location); } catch (\Exception $e) { drupal_set_message($e->getMessage(), 'error'); return; } if (!$project_title) { drupal_set_message($this->t('Unable to determine %project name.', array('%project' => $project)), 'error'); } if ($updater->isInstalled()) { drupal_set_message($this->t('%project is already installed.', array('%project' => $project_title)), 'error'); return; } $project_real_location = drupal_realpath($project_location); $arguments = array('project' => $project, 'updater_name' => get_class($updater), 'local_url' => $project_real_location); // This process is inherently difficult to test therefore use a state flag. $test_authorize = FALSE; if (drupal_valid_test_ua()) { $test_authorize = \Drupal::state()->get('test_uploaders_via_prompt', FALSE); } // If the owner of the directory we extracted is the same as the owner of // our configuration directory (e.g. sites/default) where we're trying to // install the code, there's no need to prompt for FTP/SSH credentials. // Instead, we instantiate a Drupal\Core\FileTransfer\Local and invoke // update_authorize_run_install() directly. if (fileowner($project_real_location) == fileowner($this->sitePath) && !$test_authorize) { $this->moduleHandler->loadInclude('update', 'inc', 'update.authorize'); $filetransfer = new Local($this->root); $response = call_user_func_array('update_authorize_run_install', array_merge(array($filetransfer), $arguments)); if ($response instanceof Response) { $form_state->setResponse($response); } } else { // The page title must be passed here to ensure it is initially used when // authorize.php loads for the first time with the FTP/SSH credentials // form. system_authorized_init('update_authorize_run_install', __DIR__ . '/../../update.authorize.inc', $arguments, $this->t('Update manager')); $form_state->setRedirectUrl(system_authorized_get_url()); } }
public function doInstallUpdate($update_row) { // Pull Projects $projects = update_get_projects(); // Pull Available Releases $packages = db_select('cache_update', 'cu')->fields('cu')->condition('cid', 'available_releases::%', 'LIKE')->execute()->fetchAllAssoc('cid'); // Find & Process Package foreach ($packages as $package) { // Unserialize Store $package->data = unserialize($package->data); $project = isset($projects[$package->data['short_name']]) ? $projects[$package->data['short_name']] : null; $package_update = reset($package->data['releases']); $package_uid = $project['project_type'] . ':' . $project['name'] . ':' . $package_update['version']; // Match Package if ($update_row->update_id == $package_uid) { // Type Switch switch ($update_row->type) { case 'core': $this->caller->out(' - Error: Core Updates Not Supported'); break; case 'module': case 'theme': // Download Package if (!($local_cache = update_manager_file_get($package_update['download_link']))) { $this->caller->out(' - Error: Failed to download ' . $project['name'] . ' from ' . $package_update['download_link']); return false; } // Extract it. $extract_directory = _update_manager_extract_directory(); try { update_manager_archive_extract($local_cache, $extract_directory); } catch (Exception $e) { $this->caller->out(' - Error: ' . $e->getMessage()); return false; } // Verify it. $archive_errors = update_manager_archive_verify($project['name'], $local_cache, $extract_directory); if (!empty($archive_errors)) { foreach ($archive_errors as $key => $error) { $this->caller->out(' - Error: ' . $error); } return false; } // Load Updater $project_folder = $extract_directory . '/' . $project['name']; try { $updater = Updater::factory($project_folder); } catch (Exception $e) { $this->caller->out(' - Error: ' . $e->getMessage()); return false; } $context = array('results' => array()); // Run Updater update_authorize_batch_copy_project($project['name'], get_class($updater), drupal_realpath($project_folder), new FileTransferLocal(DRUPAL_ROOT), $context); // Verify if (empty($context['finished'])) { $message = isset($context['results']['log'][$project['name']]) ? reset($context['results']['log'][$project['name']])['message'] : 'Unknown Installer Error'; $this->caller->out(' - Error: ' . $message); return false; } else { $this->caller->out(' - ' . ucfirst($update_row->type) . ' installed successfully'); } break; } } } // Return return true; }
/** * @param string $url * * @throws Oxygen_Exception * * @return array Context result generated by Drupal. * * @see update_manager_install_form_submit() */ public function downloadExtensionFromUrl($url) { module_load_include('inc', 'update', 'update.manager'); $localCache = update_manager_file_get($url); if (!$localCache) { throw new Oxygen_Exception(Oxygen_Exception::PROJECT_MANAGER_UNABLE_TO_RETRIEVE_DRUPAL_PROJECT); } $directory = _update_manager_extract_directory(); try { /** @var ArchiverInterface $archive */ $archive = update_manager_archive_extract($localCache, $directory); } catch (Exception $e) { throw new Oxygen_Exception(Oxygen_Exception::PROJECT_MANAGER_EXTRACT_FAILED, null, $e); } $files = $archive->listContents(); if (!$files) { throw new Oxygen_Exception(Oxygen_Exception::PROJECT_MANAGER_ARCHIVE_CONTAINS_NO_FILES); } // Unfortunately, we can only use the directory name to determine the project // name. Some archivers list the first file as the directory (i.e., MODULE/) // and others list an actual file (i.e., MODULE/README.TXT). $project = strtok($files[0], '/\\'); $archiveErrors = update_manager_archive_verify($project, $localCache, $directory); if (!empty($archiveErrors)) { throw new Oxygen_Exception(Oxygen_Exception::PROJECT_MANAGER_ARCHIVE_VERIFY_ERROR, array('errors' => $archiveErrors)); } // Make sure the Updater registry is loaded. drupal_get_updaters(); $projectLocation = $directory . '/' . $project; try { /** @var DrupalUpdaterInterface $updater */ $updater = Updater::factory($projectLocation); } catch (Exception $e) { throw new Oxygen_Exception(Oxygen_Exception::PROJECT_MANAGER_CAN_NOT_FIND_APPROPRIATE_UPDATER, null, $e); } try { $projectTitle = Updater::getProjectTitle($projectLocation); } catch (Exception $e) { throw new Oxygen_Exception(Oxygen_Exception::PROJECT_MANAGER_UNABLE_TO_PARSE_PROJECT_INFO, null, $e); } if (!$projectTitle) { throw new Oxygen_Exception(Oxygen_Exception::PROJECT_MANAGER_UNABLE_TO_DETERMINE_PROJECT_NAME); } if ($updater->isInstalled()) { throw new Oxygen_Exception(Oxygen_Exception::PROJECT_MANAGER_PROJECT_ALREADY_INSTALLED); } $projectRealLocation = drupal_realpath($projectLocation); // If the owner of the directory we extracted is the same as the // owner of our configuration directory (e.g. sites/default) where we're // trying to install the code, there's no need to prompt for FTP/SSH // credentials. Instead, we instantiate a FileTransferLocal and invoke // update_authorize_run_install() directly. if (fileowner($projectRealLocation) !== fileowner(DRUPAL_ROOT . '/' . conf_path())) { throw new Oxygen_Exception(Oxygen_Exception::PROJECT_MANAGER_FILE_SYSTEM_NOT_WRITABLE, array('projectOwner' => fileowner($projectRealLocation), 'siteOwner' => fileowner(conf_path()))); } module_load_include('inc', 'update', 'update.authorize'); // @TODO: Implement other file transfer types. $fileTransfer = new FileTransferLocal(DRUPAL_ROOT); $context = array(); update_authorize_batch_copy_project($project, get_class($updater), $projectRealLocation, $fileTransfer, $context); // Error example: // [ // 'results' => [ // 'log' => [ // 'views' => [ // [ // 'message' => 'Error installing / updating', // 'success' => false, // ], // [ // 'message' => 'File Transfer failed, reason: /var/www/s1/sites/all/modules is outside of the /var/www/s2', // 'success' => false, // ], // '#abort' => true, // ], // ], // 'tasks' => [], // ], // ]; // Success example: // [ // 'results' => [ // 'log' => [ // 'views' => [ // [ // 'message' => 'Installed <em class="placeholder">views</em> successfully', // 'success' => true, // ], // ], // ], // 'tasks' => [ // '<a href="./admin/modules/install">Install another module</a>', // '<a href="./admin/modules">Enable newly added modules</a>', // '<a href="./admin">Administration pages</a>', // ], // ], // 'finished' => 1, // ]; return $context; }
public function doFileTransferUpdate($update_row) { $project = $update_row->extension_id; // Actually try to download the file. if (!($local_cache = update_manager_file_get($update_row->downloadurl))) { $this->caller->out(' - Error: Failed to download ' . $project . ' from ' . $update_row->downloadurl); return false; } // Extract it. $extract_directory = _update_manager_extract_directory(); try { update_manager_archive_extract($local_cache, $extract_directory); } catch (Exception $e) { $this->caller->out(' - Error: ' . $e->getMessage()); return false; } $archive_errors = update_manager_archive_verify($project, $local_cache, $extract_directory); if (!empty($archive_errors)) { // We just need to make sure our array keys don't collide, so use the // numeric keys from the $archive_errors array. foreach ($archive_errors as $key => $error) { $this->caller->out(' - Error: ' . $key . ": " . $error); } return false; } // Store maintenance_mode setting so we can restore it when done. $maintenance_mode = variable_get('maintenance_mode', FALSE); if ($maintenance_mode == FALSE) { variable_set('maintenance_mode', TRUE); } // Make sure the Updater registry is loaded. drupal_get_updaters(); $updates = array(); $directory = _update_manager_extract_directory(); $project_location = $directory . '/' . $project; $updater = Updater::factory($project_location); $project_real_location = drupal_realpath($project_location); $updater_name = get_class($updater); $local_url = $project_real_location; // If the owner of the last directory we extracted is the same as the // owner of our configuration directory (e.g. sites/default) where we're // trying to install the code, there's no need to prompt for FTP/SSH // credentials. Instead, we instantiate a FileTransferLocal and invoke // update_authorize_run_update() directly. // // THIS PLUGIN WILL ONLY WORK IF IT IS THE SAME USER / LOCAL. if (fileowner($project_real_location) == fileowner(conf_path())) { module_load_include('inc', 'update', 'update.authorize'); $filetransfer = new FileTransferLocal(DRUPAL_ROOT); // Modified version of update_authorize_run_update() without the batch process. unset($filetransfer->connection); $updater = new $updater_name($local_url); try { if ($updater->isInstalled()) { // This is an update. $tasks = $updater->update($filetransfer); } } catch (UpdaterException $e) { $this->caller->out(' - Error: ' . $e->getMessage()); return false; } $this->caller->out(' - ' . ucfirst($project) . ' installed successfully'); $offline = variable_get('maintenance_mode', FALSE); // Now that the update completed, we need to clear the cache of available // update data and recompute our status, so prevent show bogus results. _update_authorize_clear_update_status(); // Take the site out of maintenance mode if it was previously that way. if ($offline && $maintenance_mode == FALSE) { variable_set('maintenance_mode', FALSE); $this->caller->out(' - Your site has been taken out of maintenance mode.'); } // File Update Completed, return True to process Database Update. return true; } else { $this->caller->out(" - Error: User doesn't have access to file transfers. FTP credentials are required."); return false; } }