You are here

function oauth_request_call_submit in OAuth 1.0 6

File

./oauth.module, line 652

Code

function oauth_request_call_submit($form, &$form_state) {
  $key = $form_state['values']['consumer_key'];
  $secret = $form_state['values']['consumer_secret'];
  $request_url = $form_state['values']['request_url'];
  global $user;

  // signature method declarations
  $plaintext_method = new OAuthSignatureMethod_PLAINTEXT();
  $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
  $rsa_method = new TestOAuthSignatureMethod_RSA_SHA1();
  if (!$_SESSION['oauth']['oauth_signature_method']) {
    if ($form_state['values']['sig_method'] == 0) {
      $user_sig_method = 'HMAC-SHA1';
      $_SESSION['oauth']['oauth_signature_method'] = 'HMAC-SHA1';
    }
    elseif ($form_state['values']['sig_method'] == 1) {
      $user_sig_method = 'PLAINTEXT';
      $_SESSION['oauth']['oauth_signature_method'] = 'PLAINTEXT';
    }
    elseif ($form_state['values']['sig_method'] == 2) {
      $user_sig_method = 'RSA-SHA1';
      $_SESSION['oauth']['oauth_signature_method'] = 'RSA-SHA1';
    }
  }
  if ($_SESSION['oauth']['oauth_signature_method'] == 'HMAC-SHA1') {
    $sig_method = $hmac_method;
  }
  else {
    if ($_SESSION['oauth']['oauth_signature_method'] == 'PLAINTEXT') {
      $sig_method = $plaintext_method;
    }
    else {
      if ($_SESSION['oauth']['oauth_signature_method'] == 'RSA-SHA1') {
        $sig_method = $rsa_method;
      }
    }
  }
  $test_token = NULL;
  $token = $form_state['values']['oauth_token'];
  $token_secret = $form_state['values']['oauth_token_secret'];
  $test_consumer = new OAuthConsumer($key, $secret, NULL);
  if ($form_state['values']['op'] == 'OAuth Authentication Call') {
    $_SESSION['oauth']['key'] = $form_state['values']['consumer_key'];
    $_SESSION['oauth']['key_secret'] = $form_state['values']['consumer_secret'];
    $_SESSION['oauth']['oauth_token'] = $token;
    $_SESSION['oauth']['oauth_token_secret'] = $token_secret;
    $test_token = new OAuthConsumer($token, $token_secret, $callback_url);
    $test_consumer = new OAuthConsumer($form_state['values']['consumer_key'], $form_state['values']['consumer_secret'], $callback_url);
    $parsed = parse_url($form_state['values']['auth_url']);
    $params = array();
    parse_str($parsed['query'], $params);
    $req_auth = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, "GET", $form_state['values']['auth_url'], $params);
    $req_auth
      ->sign_request($sig_method, $test_consumer, $test_token);
    $form_state['rebuild'] = TRUE;
    $_SESSION['oauth']['operation'] = 'access';
    $domain = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    $callback_url = 'http://' . $domain;
    Header("Location: {$req_auth}&oauth_callback={$callback_url}");
  }
  elseif ($form_state['values']['op'] == 'OAuth Access Token Call') {
    $test_consumer = new OAuthConsumer($_SESSION['oauth']['key'], $_SESSION['oauth']['key_secret'], NULL);
    $test_token = new OAuthConsumer($_SESSION['oauth']['oauth_token'], $_SESSION['oauth']['oauth_token_secret']);
    $parsed = parse_url($form_state['values']['access_url']);
    $params = array();
    parse_str($parsed['query'], $params);
    $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, "GET", $form_state['values']['access_url'], $params);
    $acc_req
      ->sign_request($sig_method, $test_consumer, $test_token);
    $result_access = drupal_http_request($acc_req, $headers = array(), $method = 'GET', $data = NULL, $retry = 3);
    parse_str($result_access->data);
    $_SESSION['oauth']['oauth_access_token'] = $oauth_token;
    $_SESSION['oauth']['oauth_access_token_secret'] = $oauth_token_secret;
    drupal_set_message('<pre>' . print_r("oauth_access_token  {$oauth_token}", TRUE) . '</pre>');
    drupal_set_message('<pre>' . print_r("oauth_access_token_secret  {$oauth_token_secret}", TRUE) . '</pre>');
    drupal_redirect_form($form, 'admin/build/services');
  }
  if ($form_state['values']['op'] == 'Request Token Call') {
    $parsed = parse_url($request_url);
    $params = array();
    parse_str($parsed['query'], $params);
    $req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "GET", $request_url, $params);
    $req_req
      ->sign_request($sig_method, $test_consumer, NULL);
    $result_request = drupal_http_request($req_req, $headers = array(), $method = 'GET', $data = NULL, $retry = 3);
    parse_str($result_request->data);
    drupal_set_message('<pre>' . print_r("oauth_token  {$oauth_token}", TRUE) . '</pre>');
    drupal_set_message('<pre>' . print_r("oauth_token_secret  {$oauth_token_secret}", TRUE) . '</pre>');
    $form_state['rebuild'] = TRUE;
    $form_state['data']['oauth_token'] = $oauth_token;
    $form_state['data']['oauth_token_secret'] = $oauth_token_secret;
    $_SESSION['oauth']['operation'] = 'auth';
  }
}