/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { try { $response = $this->client->nspiCall('/agent-api/subscription', array('identifier' => trim($form_state->getValue('acquia_identifier'))), trim($form_state->getValue('acquia_key'))); } catch (ConnectorException $e) { // Set form error to prevent switching to the next page. if ($e->isCustomized()) { // Allow to connect with expired subscription if ($e->getCustomMessage('code') == Subscription::EXPIRED) { $form_state->setValue('subscription', 'Expired subscription.'); return; } acquia_connector_report_restapi_error($e->getCustomMessage('code'), $e->getCustomMessage()); $form_state->setErrorByName(''); } else { $form_state->setErrorByName('', t('Server error, please submit again.')); } return; } $response = $response['result']; if (empty($response['body']['subscription_name'])) { $form_state->setErrorByName('acquia_identifier', t('No subscriptions were found.')); } else { $form_state->setValue('subscription', $response['body']['subscription_name']); } }
/** * Checks if NSPI server has an updated SPI data definition. * If it does, then this function updates local copy of SPI definition data. * * @return boolean * True if SPI definition data has been updated */ private function updateDefinition() { $core_version = substr(\Drupal::VERSION, 0, 1); $spi_def_end_point = '/spi_def/get/' . $core_version; $response = $this->client->getDefinition($spi_def_end_point); if (!$response) { \Drupal::logger('acquia spi')->error('Failed to obtain latest SPI data definition.'); return FALSE; } else { $response_data = $response; $expected_data_types = array('drupal_version' => 'string', 'timestamp' => 'string', 'acquia_spi_variables' => 'array'); // Make sure that $response_data contains everything expected. foreach ($expected_data_types as $key => $values) { if (!array_key_exists($key, $response_data) || gettype($response_data[$key]) != $expected_data_types[$key]) { \Drupal::logger('acquia spi')->error('Received SPI data definition does not match expected pattern while checking "@key". Received and expected data: @data', array('@key' => $key, '@data' => var_export(array_merge(array('expected_data' => $expected_data_types), array('response_data' => $response_data)), 1), TRUE)); return FALSE; } } if ($response_data['drupal_version'] != $core_version) { \Drupal::logger('acquia spi')->notice('Received SPI data definition does not match with current version of your Drupal installation. Data received for Drupal @version', array('@version' => $response_data['drupal_version'])); return FALSE; } } // NSPI response is in expected format. if ((int) $response_data['timestamp'] > (int) $this->config('acquia_connector.settings')->get('spi.def_timestamp')) { // Compare stored variable names to incoming and report on update. $old_vars = $this->config('acquia_connector.settings')->get('spi.def_vars'); $new_vars = $response_data['acquia_spi_variables']; $new_optional_vars = 0; foreach ($new_vars as $new_var_name => $new_var) { // Count if received from NSPI optional variable is not present in old local SPI definition // or if it already was in old SPI definition, but was not optional if ($new_var['optional'] && !array_key_exists($new_var_name, $old_vars) || $new_var['optional'] && isset($old_vars[$new_var_name]) && !$old_vars[$new_var_name]['optional']) { $new_optional_vars++; } } // Clean up waived vars that are not exposed by NSPI anymore. $waived_spi_def_vars = $this->config('acquia_connector.settings')->get('spi.def_waived_vars'); $changed_bool = FALSE; foreach ($waived_spi_def_vars as $key => $waived_var) { if (!in_array($waived_var, $new_vars)) { unset($waived_spi_def_vars[$key]); $changed_bool = TRUE; } } $config = \Drupal::configFactory()->getEditable('acquia_connector.settings'); if ($changed_bool) { $config->set('spi.def_waived_vars', $waived_spi_def_vars); } // Finally, save SPI definition data. if ($new_optional_vars > 0) { $config->set('spi.new_optional_data', 1); } $config->set('spi.def_timestamp', $response_data['timestamp']); $config->set('spi.def_vars', $response_data['acquia_spi_variables']); $config->save(); return TRUE; } return FALSE; }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { $storage = $form_state->getStorage(); if (!isset($storage['choose'])) { try { $response = $this->client->getSubscriptionCredentials($form_state->getValue('email'), $form_state->getValue('pass')); } catch (ConnectorException $e) { // Set form error to prevent switching to the next page. if ($e->isCustomized()) { $form_state->setErrorByName('', $e->getCustomMessage()); } else { \Drupal::logger('acquia connector')->error($e->getMessage()); $form_state->setErrorByName('', $this->t('Can\'t connect to the Acquia Subscription.')); } } if (!empty($response)) { $storage['response'] = $response; } } $form_state->setStorage($storage); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('acquia_connector.settings'); $identifier = $config->get('identifier'); $key = $config->get('key'); $subscription = $config->get('subscription_name'); if (empty($identifier) && empty($key)) { return new RedirectResponse($this->url('acquia_connector.start')); } // Check our connection to the Acquia Network and validity of the credentials. try { $this->client->getSubscription($identifier, $key); } catch (ConnectorException $e) { $error_message = acquia_connector_connection_error_message($e->getCustomMessage('code', FALSE)); $ssl_available = in_array('ssl', stream_get_transports(), TRUE) && !defined('ACQUIA_DEVELOPMENT_NOSSL') && $config->get('spi.ssl_verify'); if (empty($error_message) && $ssl_available) { $error_message = $this->t('There was an error in validating your subscription credentials. You may want to try disabling SSL peer verification by setting the variable acquia_connector.settings:spi.ssl_verify to false.'); } drupal_set_message($error_message, 'error', FALSE); } $form['connected'] = array('#markup' => $this->t('<h3>Connected to the Acquia Network</h3>')); if (!empty($subscription)) { $form['subscription'] = array('#markup' => $this->t('Subscription: @sub <a href="@url">change</a>', array('@sub' => $subscription, '@url' => $this->url('acquia_connector.setup')))); } $form['connection'] = array('#type' => 'fieldset', '#title' => $this->t('Acquia Subscription Settings'), '#collapsible' => FALSE); $form['migrate'] = array('#type' => 'details', '#title' => $this->t('Acquia Cloud Migrate'), '#description' => $this->t('Transfer a fully-functional copy of your site to Acquia Cloud. <a href="@url">Learn more</a>.', array('@url' => Url::fromUri('https://docs.acquia.com/cloud/site/import/connector')->getUri())), '#open' => !\Drupal::request()->server->has('AH_SITE_GROUP')); $form['migrate']['submit'] = array('#type' => 'submit', '#value' => $this->t('Migrate'), '#submit' => ['::submitMigrateGoForm']); $last_migration = \Drupal::state()->get('migrate.cloud', []); if (!empty($last_migration['db_file']) || !empty($last_migration['tar_file']) || !empty($last_migration['dir'])) { // Replace Upload button with Cleanup. unset($form['migrate']['#description']); $form['migrate']['#prefix'] = '<div class="messages error">' . $this->t('Temporary files were leftover from last migration attempt.') . '</div>'; $form['migrate']['submit']['#value'] = $this->t('Cleanup files'); $form['migrate']['submit']['#submit'] = ['::submitMigrateCleanupForm']; } // Help documentation is local unless the Help module is disabled. if ($this->moduleHandler->moduleExists('help')) { $help_url = \Drupal::url('help.page', array('name' => 'acquia_connector')); } else { $help_url = Url::fromUri('https://docs.acquia.com/network/install')->getUri(); } if (!empty($identifier) && !empty($key)) { $ssl_available = in_array('ssl', stream_get_transports(), TRUE) && !defined('ACQUIA_DEVELOPMENT_NOSSL'); $form['connection']['spi'] = array('#prefix' => '<div class="acquia-spi">', '#suffix' => '</div>', '#weight' => 0); $form['connection']['description']['#markup'] = $this->t('Allow collection and examination of the following items. <a href="@url">Learn more</a>.', array('@url' => $help_url)); $form['connection']['description']['#weight'] = '-1'; $form['connection']['spi']['admin_priv'] = array('#type' => 'checkbox', '#title' => $this->t('Admin privileges'), '#default_value' => $config->get('spi.admin_priv')); $form['connection']['spi']['send_node_user'] = array('#type' => 'checkbox', '#title' => $this->t('Nodes and users'), '#default_value' => $config->get('spi.send_node_user')); $form['connection']['spi']['send_watchdog'] = array('#type' => 'checkbox', '#title' => $this->t('Watchdog logs'), '#default_value' => $config->get('spi.send_watchdog')); $form['connection']['spi']['module_diff_data'] = array('#type' => 'checkbox', '#title' => t('Source code'), '#default_value' => (int) $config->get('spi.module_diff_data') && $ssl_available, '#description' => $this->t('Source code analysis requires a SSL connection and for your site to be publicly accessible. <a href="@url">Learn more</a>.', array('@url' => $help_url)), '#disabled' => !$ssl_available); $form['connection']['acquia_dynamic_banner'] = array('#type' => 'checkbox', '#title' => $this->t('Receive updates from Acquia Subscription'), '#default_value' => $config->get('spi.dynamic_banner')); $form['connection']['alter_variables'] = array('#type' => 'checkbox', '#title' => $this->t('Allow Insight to update list of approved variables.'), '#default_value' => (int) $config->get('spi.set_variables_override'), '#description' => $this->t('Insight can set variables on your site to recommended values at your approval, but only from a specific list of variables. Check this box to allow Insight to update the list of approved variables. <a href="@url">Learn more</a>.', array('@url' => $help_url))); $use_cron = $config->get('spi.use_cron'); $form['connection']['use_cron'] = array('#type' => 'checkbox', '#title' => $this->t('Send via Drupal cron'), '#default_value' => $use_cron); $form['#attached']['library'][] = 'acquia_connector/acquia_connector.form'; $key = sha1($this->privateKey->get()); $url = Url::fromRoute('acquia_connector.send', [], ['query' => ['key' => $key], 'absolute' => TRUE])->toString(); $form['connection']['use_cron_url'] = array('#type' => 'container', '#children' => $this->t('Enter the following URL in your server\'s crontab to send SPI data:<br /><em>@url</em>', array('@url' => $url)), '#states' => array('visible' => array(':input[name="use_cron"]' => array('checked' => FALSE)))); } return parent::buildForm($form, $form_state); }