Fix for multi page form

  1. function oauth_request_call(){
  2.        
  3.         $form['#multistep'] = TRUE;
  4.   //drupal_set_message('<pre>'. print_r(&$form_state, TRUE) .'</pre>');
  5.     $step = isset($form_state) ? $form_state['values']['step'] : 1;
  6.     // store next strp in hidden field
  7.    
  8.     $form['step'] = array(
  9.         '#type' => 'hidden',
  10.         '#value' => $step + 1
  11.     );
  12.           
  13.     drupal_set_message('<pre>'. print_r($step, TRUE) .'</pre>');       
  14.  
  15.     // indicator of step number
  16.     $form['indicator'] = array(
  17.         '#type' => 'fieldset',
  18.         '#title' => t('Step @number', array('@number' => $step)),
  19.     );
  20.         switch($step){
  21.                 case 1:
  22.         $form['indicator']['consumer'] = array(
  23.                 '#type' => 'fieldset',
  24.                 '#title' => t('consumer details'),
  25.         );
  26.         $form['indicator']['consumer']['consumer_key'] = array(
  27.                 '#title' => t('consumer key'),
  28.                 '#description' => t('consumer key of user on test server'),
  29.                 '#type' => 'textfield',
  30.         );
  31.         $form['indicator']['consumer']['consumer_secret'] = array(
  32.                 '#title' => t('consumer secret'),
  33.                 '#description' => t('consumer secret of user on test server'),
  34.                 '#type' => 'textfield',
  35.         );
  36.         $form['indicator']['endpoints'] = array(
  37.                 '#title' => t('end points of testing server'),
  38.                 '#type' => 'fieldset',
  39.         );
  40.         $form['indicator']['endpoints']['request_url'] = array(
  41.                 '#title' => t('Request URL'),
  42.                 '#type' => 'textfield',
  43.                 '#weight' => 5,
  44.         );
  45.         break;
  46.         //$form_state['values']['step'] = 2;
  47.        
  48.        
  49.  
  50.  }
  51.         if($step == 2) {
  52.         drupal_set_message('<pre>'. print_r($result_request->data, TRUE) .'</pre>');   
  53.         $form['indicator']['endpoints'] = array(
  54.                 '#title' => t('end points of testing server'),
  55.                 '#type' => 'fieldset',
  56.         );
  57.         $form['indicator']['endpoints']['auth_url'] = array(
  58.                 '#title' => t('Authentication URL'),
  59.                 '#type' => 'textfield',
  60.                 '#weight' => 6,
  61.         );
  62.        
  63.         $form['indicator']['sig_method'] = array(
  64.                 '#title' => t('Please select signature method to use'),
  65.                 '#type' => 'radios',
  66.                 '#options' => array( 0 => t('HMAC-SHA1'), 1 => t('PLAINTEXT'), 2 => t('RSA-SHA1')),
  67.                 '#default_value' => 1,
  68.         );
  69.         $form['indicator']['token'] = array(
  70.                 '#type' => 'fieldset',
  71.                 '#title' => t('Token and Token secret'),
  72.                 '#description' => t('Paste Token and Token Secret from above which are obtained after request call'),
  73.         );
  74.         $form['indicator']['token']['oauth_token'] = array(
  75.                 '#title' => t('OAuth token'),
  76.                 '#description' => t('Please paste token from above which you obtain from request call'),
  77.                 '#type' => 'textfield',
  78.                 '#weight' => 12,
  79.         );
  80.         $form['indicator']['token']['oauth_token_secret'] = array(
  81.                 '#title' => t('OAuth token secret'),
  82.                 '#description' => t('Please paste token secret from above which you obtain from request call'),
  83.                 '#type' => 'textfield',
  84.                 '#weight' => 13,
  85.         );
  86.         $form['auth_call'] = array(
  87.                 '#value' => t('OAuth Authentication Call'),
  88.                 '#type' => 'submit',
  89.                 '#weight' => 14,
  90.         );
  91.        
  92.  }
  93.        
  94.         if($step == 3){
  95.         $form['indicator']['endpoints']['access_url'] = array(
  96.                 '#title' => t('Access URL'),
  97.                 '#type' => 'textfield',
  98.                 '#weight' => 7,
  99.         );
  100.         $form['access_call'] = array(
  101.                 '#value' => t('OAuth Access Token Call'),
  102.                 '#type' => 'submit',
  103.                 '#weight' => 15,
  104.         );
  105.        
  106.  }     
  107.  
  108.  $form['indicator']['request_call'] = array(
  109.                 '#value' => t('Request Token Call'),
  110.                 '#title' => t('Make call to server'),
  111.                 '#type' => 'submit',
  112.                 '#weight' => 10,
  113.         );     
  114.         switch($step){
  115.                 case 1:
  116.                         $form_state['values']['step'] = 2;
  117.                         break;
  118.                 case 2:
  119.                         $form['consumer_key'] = array(
  120.                                 '#type' => 'hidden',
  121.                                 '#value' => $form_values['consumer_key'],
  122.                         );
  123.                         $form['consumer_secret'] = array(
  124.                                 '#type' => 'hidden',
  125.                                 '#value' => $form_values['consumer_secret'],
  126.                         );
  127.                         $form['request_url'] = array(
  128.                                 '#type' => 'hidden',
  129.                                 '#value' => $form_values['request_url'],
  130.                         );
  131.                         break;
  132.                
  133.                 }
  134.  
  135.         return $form;
  136.        
  137. }
  138.  
  139. function oauth_request_call_submit($form, &$form_state){
  140.        
  141.         $key = $form_state['values']['consumer_key'];
  142.         $secret = $form_state['values']['consumer_secret'];
  143.         $request_url = $form_state['values']['request_url'];
  144.                
  145.         if($form_state['values']['sig_method'] == 0){
  146.           $user_sig_method = 'HMAC-SHA1';       
  147.                 }elseif($form_state['values']['sig_method'] == 1){
  148.                         $user_sig_method = 'PLAINTEXT'
  149.                   }elseif($form_state['values']['sig_method'] == 2){
  150.                         $user_sig_method = 'RSA-SHA1'
  151.            }
  152.         $test_token = NULL;
  153.        
  154.          
  155.         if ($user_sig_method) {
  156.        $sig_method = $sig_methods[$user_sig_method];
  157.        print_r($sig_method);
  158.         }
  159.        
  160.        
  161.         $plaintext_method = new OAuthSignatureMethod_PLAINTEXT();
  162.         //right now doing just for PLAINTEXT
  163.         $sig_method = $plaintext_method;
  164.        
  165.         $token = $form_state['values']['oauth_token'];
  166.     $token_secret = $form_state['values']['oauth_token_secret'];
  167.    
  168.     //drupal_set_message('<pre>'. print_r($sig_method, TRUE) .'</pre>');
  169.         $test_consumer = new OAuthConsumer($key, $secret, NULL);
  170.        
  171.    
  172.     if($form_state['values']['op'] == 'OAuth Authentication Call'){
  173.         $test_token = new OAuthConsumer($token, $token_secret);
  174.         //$callback_url = "$base_url/client.php?key=$key&secret=$secret&token=$token&token_secret=$token_secret&endpoint=" . urlencode($endpoint);
  175.         $callback_url = "$base_url/?q=admin/oauth/services";
  176.                 $auth_url = $form_state['values']['auth_url'] . "/oauth_token=$token&oauth_callback=".urlencode($callback_url)
  177.         //$result_auth = drupal_http_request($auth_url, $headers = array(), $method = 'GET', $data = NULL, $retry = 3);
  178.         //drupal_set_message('<pre>'. print_r($auth_url, TRUE) .'</pre>');
  179.         drupal_redirect_form($form, $auth_url);
  180.     }elseif($form_state['values']['op'] == 'OAuth Access Token Call'){
  181.                 //drupal_set_message('<pre>'. print_r($form_state['values']['access_url'], TRUE) .'</pre>');
  182.                 $test_token = new OAuthConsumer($form_state['values']['oauth_token'], $form_state['values']['oauth_token_secret']);
  183.                 $parsed = parse_url($form_state['values']['access_url']);
  184.                         $params = array();
  185.                         parse_str($parsed['query'], $params);
  186.  
  187.                         $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, "GET", $form_state['values']['access_url'], $params);
  188.                         $acc_req->sign_request($sig_method, $test_consumer, $test_token);       
  189.                 $result_access = drupal_http_request($acc_req, $headers = array(), $method = 'GET', $data = NULL, $retry = 3);
  190.                 drupal_set_message('<pre>'. print_r($result_access->data, TRUE) .'</pre>');
  191.                 //drupal_redirect_form($form, $acc_req);
  192.         }
  193.     if($form_state['values']['op'] == 'Request Token Call'){
  194.     $parsed = parse_url($request_url);
  195.     $params = array();
  196.     parse_str($parsed['query'], $params);
  197.     //drupal_set_message('<pre>'. print_r($test_consumer, TRUE) .'</pre>');
  198.     $req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "GET", $request_url, $params);
  199.     $req_req->sign_request($sig_method, $test_consumer, NULL);
  200.     //drupal_set_message('<pre>'. print_r($req_req, TRUE) .'</pre>');
  201.     //drupal_redirect_form($form, $req_req);
  202.     //Header("Location: $req_req");
  203.     $result_request = drupal_http_request($req_req, $headers = array(), $method = 'GET', $data = NULL, $retry = 3);
  204.     //drupal_set_message('<pre>'. print_r($form_state, TRUE) .'</pre>');
  205.     return $result_request;
  206.  }
  207.    
  208.  
  209. }