You are here

function _acquia_migrate_transmit in Acquia Connector 6.2

Same name and namespace in other branches
  1. 7.2 acquia_agent/acquia_agent.migrate.inc \_acquia_migrate_transmit()

Perform POST of archive chunk to Acquia hosting environment URL.

1 call to _acquia_migrate_transmit()
_acquia_migrate_transmit_chunk in acquia_agent/acquia_agent.migrate.inc

File

acquia_agent/acquia_agent.migrate.inc, line 440
Upload to Acquia Cloud.

Code

function _acquia_migrate_transmit(&$migration, $content) {
  $params = $migration['request_params'];
  $params['nonce'] = $migration['env']['nonce'];
  $params['t'] = time();
  $params[$migration['env']['stage']] = acquia_migrate_get_token($params['t'], $params['r'], $migration['env']['secret']);
  $data = '';
  $boundary = _acquia_migrate_multipart_boundary();
  $data = _acquia_migrate_multipart_encode_params($boundary, $params, $migration['file_name'], $content);
  $headers = array(
    'Content-Type' => "multipart/form-data, boundary={$boundary}",
    'User-Agent' => 'Acquia Migrate Client/1.x (Drupal ' . VERSION . ';)',
  );
  $url = $migration['env']['url'];
  $return = drupal_http_request($url, $headers, 'POST', $data);
  if ($return->code == 200) {
    $output = json_decode($return->data, TRUE);
    if (!is_array($output)) {
      $migration['error'] = t('Error occurred, please try again or consult the logs.');
      $migration['error_data'] = $return->data;
      return FALSE;
    }
    elseif (!empty($output['err'])) {
      $migration['error'] = $output['err'];
      $migration['error_data'] = $return->data;
      return FALSE;
    }
    else {

      // Validate signature.
      $response_signature = $output['sig'];
      unset($output['sig']);
      $sig = '';
      foreach ($output as $value) {
        $sig .= $value;
      }
      $signature = hash_hmac('sha256', $sig, $migration['env']['secret']);

      // Check if response is correct, if not stop migration.
      if ($signature != $response_signature) {
        $migration['error'] = t('Signature from server is wrong');
        $migration['error_data'] = $return->data;
        return FALSE;
      }
    }
  }
  elseif ($return->code == 302) {

    // Final chunk, signature and any error is in Location URL.
    $redirect_url = $return->redirect_url;
    $parsed = parse_url($redirect_url);
    parse_str($parsed['query'], $query);
    if (!empty($query['err'])) {
      $migration['error'] = $query['err'];
      $migration['error_data'] = $return->data;
      return FALSE;
    }
    else {
      $query_sig = $query['sig'];
      unset($query['sig']);
      $sig = '';
      foreach ($query as $k => $v) {
        $sig .= $v;
      }
      $signature = hash_hmac('sha256', $sig, $migration['env']['secret']);
      if ($signature == $query_sig) {
        $query['sig'] = $query_sig;
        $migration['redirect'] = array(
          'url' => $redirect_url,
          'data' => $query,
        );
      }
      else {
        $migration['error'] = t('Signature from server is wrong');
        $migration['error_data'] = $return->data;
        return FALSE;
      }
    }
  }
  else {
    $migration['error'] = t('Transfer error');
    $migration['error_data'] = $return->data;
    return FALSE;
  }
}