Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
oauth.inc
1 <?php
17 require_once SQ_ATTRIBUTES_PATH.'/serialise/serialise.inc';
18 require_once SQ_ATTRIBUTES_PATH.'/option_list/option_list.inc';
19 require_once SQ_ATTRIBUTES_PATH.'/http_request/http_request.inc';
20 
33 {
34  private $_methods = Array(
35  'GET' => 'GET',
36  'POST' => 'POST',
37  );
38  private $_signature_methods = Array(
39  'HMAC-SHA1' => 'HMAC-SHA1',
40  //'RSA-SHA1' => 'RSA-SHA1',
41  'PLAINTEXT' => 'PLAINTEXT',
42  );
43  private $_type = Array(
44  'THREE_LEGGED' => '3 legged',
45  'TWO_LEGGED' => '2 legged',
46  );
47 
48  private $_header_params = Array();
49  private $_errors = Array();
50 
57  function __construct($attribute = 0, $value = NULL)
58  {
59  parent::__construct($attribute, $value);
60 
61  }//end constructor
62 
63 
73  public function paint($prefix, $read_only = FALSE)
74  {
75  $prefix = str_replace(':', '_', $prefix);
76  $current_value = @unserialize($this->value);
77 
78  // Set default values.
79  if (!is_array($current_value)) $current_value = Array();
80 
81  if (!isset($current_value['consumer_key'])) $current_value['consumer_key'] = '';
82  if (!isset($current_value['consumer_secret'])) $current_value['consumer_secret'] = FALSE;
83  if (!isset($current_value['signature_method'])) $current_value['signature_method'] = 'HMAC-SHA1';
84  if (!isset($current_value['method'])) $current_value['method'] = 'GET';
85 
86  if (!isset($current_value['request_token_url'])) $current_value['request_token_url'] = '';
87  if (!isset($current_value['authorization_url'])) $current_value['authorization_url'] = '';
88  if (!isset($current_value['access_token_url'])) $current_value['access_token_url'] = '';
89  if (!isset($current_value['callback_url'])) $current_value['callback_url'] = '';
90 
91  if (!isset($current_value['timeout'])) $current_value['timeout'] = 10;
92  if (!isset($current_value['request_headers'])) $current_value['request_headers'] = Array();
93  if (!isset($current_value['request_body'])) $current_value['request_body'] = '';
94  ?>
95 
96  <table border="0" class="sq-backend-table">
97  <tr>
98  <th width="20%"><?php echo translate('oauth_consumer_key'); ?></th>
99  <td>
100  <?php
101  if ($read_only) {
102  echo htmlspecialchars($current_value['consumer_key']);
103  } else {
104  text_box($prefix.'_consumer_key', $current_value['consumer_key'], 80);
105  }
106  ?>
107  </td>
108  </tr>
109  <tr>
110  <th width="20%"><?php echo translate('oauth_consumer_secret'); ?></th>
111  <td>
112  <?php
113  if ($read_only) {
114  echo htmlspecialchars($current_value['consumer_secret']);
115  } else {
116  text_box($prefix.'_consumer_secret', $current_value['consumer_secret'], 80);
117  }
118  ?>
119  </td>
120  </tr>
121  <?php if (!isset($current_value['type']) || $current_value['type'] == 'THREE_LEGGED') {?>
122  <tr>
123  <th width="20%"><?php echo translate('http_request_method'); ?></th>
124  <td>
125  <?php
126  if ($read_only) {
127  echo htmlspecialchars($current_value['method']);
128  } else {
129  combo_box($prefix.'_method', $this->_methods, FALSE, $current_value['method']);
130  }
131  ?>
132  </td>
133  </tr>
134  <tr>
135  <th><?php echo translate('oauth_request_token_url'); ?></th>
136  <td>
137  <?php
138  if ($read_only) {
139  echo htmlspecialchars($current_value['request_token_url']);
140  } else {
141  text_box($prefix.'_request_token_url', $current_value['request_token_url'], 80);
142  }
143  ?>
144  </td>
145  </tr>
146  <tr>
147  <th><?php echo translate('oauth_authorization_url'); ?></th>
148  <td>
149  <?php
150  if ($read_only) {
151  echo htmlspecialchars($current_value['authorization_url']);
152  } else {
153  text_box($prefix.'_authorization_url', $current_value['authorization_url'], 80);
154  }
155  ?>
156  </td>
157  </tr>
158  <tr>
159  <th><?php echo translate('oauth_access_token_url'); ?></th>
160  <td>
161  <?php
162  if ($read_only) {
163  echo htmlspecialchars($current_value['access_token_url']);
164  } else {
165  text_box($prefix.'_access_token_url', $current_value['access_token_url'], 80);
166  }
167  ?>
168  </td>
169  </tr>
170  <tr>
171  <th><?php echo translate('oauth_callback_url'); ?></th>
172  <td>
173  <?php
174  if ($read_only) {
175  echo htmlspecialchars($current_value['callback_url']);
176  } else {
177  text_box($prefix.'_callback_url', $current_value['callback_url'], 80);
178  }
179  ?>
180  </td>
181  </tr>
182  <?php }?>
183  <tr>
184  <th><?php echo translate('oauth_signature_method'); ?></th>
185  <td>
186  <?php
187  if ($read_only) {
188  echo htmlspecialchars($this->_signature_methods[$current_value['signature_method']]);
189  } else {
190  combo_box($prefix.'_signature_method', $this->_signature_methods, FALSE, $current_value['signature_method']);
191  }
192  ?>
193  </td>
194  </tr>
195  <?php if (!isset($current_value['type']) || $current_value['type'] == 'THREE_LEGGED') {?>
196  <tr>
197  <th><?php echo translate('http_request_request_headers'); ?></th>
198  <td>
199  <?php
200  $hl = new Asset_Attribute_Option_List();
201  $hl->value = implode($hl->delimiter, $current_value['request_headers']);
202  $hl->setEditParam('width', '60');
203  $hl->paint($prefix.'_request_headers', $read_only);
204  ?>
205  </td>
206  </tr>
207  <?php
208  }
209  if (($current_value['method'] == 'POST') || ($current_value['method'] == 'PUT')) {
210  ?>
211  <tr>
212  <th><?php echo translate('http_request_request_body'); ?></th>
213  <td>
214  <?php
215  if ($read_only) {
216  echo htmlspecialchars($current_value['request_body']);
217  } else {
218  text_area($prefix.'_request_body', $current_value['request_body'], 70, 20, 0, 'style="font-family: monospace;"');
219  }
220  ?>
221  </td>
222  </tr>
223  <tr>
224  <th><?php echo translate('http_request_timeout'); ?></th>
225  <td>
226  <?php
227  if ($read_only) {
228  echo htmlspecialchars($current_value['timeout']);
229  } else {
230  text_box($prefix.'_timeout', $current_value['timeout'], 4);
231  }
232  ?>
233  </td>
234  </tr>
235  <?php
236  } else {
237  hidden_field($prefix.'_request_body', $current_value['request_body']);
238  }
239  ?>
240  </table>
241  <?php
242 
243  }//end paint()
244 
245 
254  public function process($prefix)
255  {
256  $prefix = str_replace(':', '_', $prefix);
257 
258  $value = Array();
259 
260  if ($this->getType() != 'TWO_LEGGED' && !isset($_REQUEST[$prefix.'_method'])) return FALSE;
261 
262  // Assign values
263  $value['consumer_key'] = (isset($_REQUEST[$prefix.'_consumer_key'])) ? trim($_REQUEST[$prefix.'_consumer_key']) : '';
264  $value['consumer_secret'] = (isset($_REQUEST[$prefix.'_consumer_secret'])) ? trim($_REQUEST[$prefix.'_consumer_secret']) : '';
265  $value['signature_method'] = (isset($_REQUEST[$prefix.'_signature_method'])) ? trim($_REQUEST[$prefix.'_signature_method']) : '';
266  $value['method'] = (isset($_REQUEST[$prefix.'_method'])) ? $_REQUEST[$prefix.'_method'] : '';
267 
268  $value['request_token_url'] = (isset($_REQUEST[$prefix.'_request_token_url'])) ? $_REQUEST[$prefix.'_request_token_url'] : '';
269  $value['authorization_url'] = (isset($_REQUEST[$prefix.'_authorization_url'])) ? $_REQUEST[$prefix.'_authorization_url'] : '';
270  $value['access_token_url'] = (isset($_REQUEST[$prefix.'_access_token_url'])) ? $_REQUEST[$prefix.'_access_token_url'] : '';
271  $value['callback_url'] = (isset($_REQUEST[$prefix.'_callback_url'])) ? $_REQUEST[$prefix.'_callback_url'] : '';
272 
273  $value['timeout'] = (isset($_REQUEST[$prefix.'_timeout'])) ? $_REQUEST[$prefix.'_timeout'] : 0;
274  $value['follow_redirect'] = FALSE;
275  $value['cache_options'] = 'NEVER';
276  $value['cache_post_requests'] = FALSE;
277  $value['default_cache_expiry'] = 60;
278  $value['request_body'] = (isset($_REQUEST[$prefix.'_request_body'])) ? trim($_REQUEST[$prefix.'_request_body']) : '';
279 
280  $hl = new Asset_Attribute_Option_List();
281  $hl->process($prefix.'_request_headers');
282  $value['request_headers'] = trim($hl->value) ? explode($hl->delimiter, $hl->value) : NULL;
283 
284  $value['type'] = $this->getType(); // the type is controlled by calling asset(which sets the type as default), not the user
285 
286  $value['run_test'] = FALSE;
287 
288  $this->processed = $this->setValue($value);
289 
290  }//end process()
291 
292 
302  private function _encodeParam($input, $url_encode = TRUE)
303  {
304  // if the passed value is an array, encode both keys and values
305  if (is_array($input)) {
306  $encoded_array = Array();
307  foreach ($input as $key => $val) {
308  $encoded_array[$this->_encodeParam($key)] = $this->_encodeParam($val);
309  }
310  return $encoded_array;
311  } else {
312  $input = utf8_encode($input);
313  if ($url_encode) {
314  $input = str_replace('+', ' ', str_replace('%7E', '~', rawurlencode($input)));
315  }
316 
317  return $input;
318  }
319 
320 
321  }//end _encodeParam()
322 
323 
331  private function _generateNonce()
332  {
333  $mt = microtime();
334  $rand = mt_rand();
335  return md5($mt . $rand);
336 
337  }//end _generateNonce()
338 
339 
350  private function _generateBaseString($request_method, $request_url, $params)
351  {
352  $base_string_params = Array();
353 
354  // construct parameters in name=value format
355  foreach ($params as $param_key => $param_val) {
356  $base_string_params[] = $param_key.'='.$param_val;
357  }
358 
359  // sort the parameters
360  sort($base_string_params);
361 
362  // and connect them all together with '&'
363  $base_string = implode('&', $base_string_params);
364 
365  // add request method & request url in front and it's a base string for signature
366  return $this->_encodeParam($request_method).'&'.$this->_encodeParam($request_url).'&'.$this->_encodeParam($base_string);
367 
368  }//end _generateBaseString()
369 
370 
381  private function _generateSignature($key, $base_string, $method)
382  {
383  switch ($method) {
384  case 'HMAC-SHA1' :
385  return $this->_encodeParam(base64_encode(hash_hmac("sha1", $base_string, $key, TRUE)));
386 
387  // TODO: support RSA-SHA1
388  //case 'RSA-SHA1':
389 
390  case 'PLAINTEXT':
391  default:
392  return $this->_encodeParam($base_string);
393  }
394 
395  }//end _generateSignature()
396 
397 
406  private function _generateAuthorizationHeader($params)
407  {
408  $headers = Array();
409  foreach ($params as $param_key => $param_val) {
410  $headers[] = $param_key.'="'.$param_val.'"';
411  }
412  return 'Authorization: OAuth '.implode(',', $headers);
413 
414  }//end _generateAuthorizationHeader()
415 
416 
426  private function _getQueryParameterArray($url, $url_encode = TRUE)
427  {
428  if (empty($url)) return NULL;
429 
430  $query_parameters = Array();
431 
432 
433  // if the url has the parameters part, return the parameters as an array
434  $url_param = explode('?', $url);
435  if (isset($url_param[1])) {
436  $params = explode('&', $url_param[1]);
437  foreach ($params as $param) {
438  $param_val = explode('=', $param);
439  // the url might already be url encoded - decode it to avoid double encoding later on
440  // so that it'll work whehter the url has been encoded or not
441  if($url_encode) {
442  $param_val[0] = urldecode($param_val[0]);
443  $param_val[1] = urldecode($param_val[1]);
444  }
445  $query_parameters[$this->_encodeParam($param_val[0], $url_encode)] = $this->_encodeParam($param_val[1], $url_encode);
446  }
447  }
448 
449  return $query_parameters;
450 
451  }//end _getQueryParameterArray()
452 
453 
462  private function _getTokenInfoArray($token_info_string)
463  {
464  $token_info = Array();
465 
466  // take out each item in token info - typically token and token secret, and possibly callback confirmed
467  $token_strings = explode('&', $token_info_string);
468 
469  // if there is no more than one item, then it's not proper token info
470  if (count($token_strings) <= 1) return $token_info;
471 
472  // put each info item into an key-value Array
473  foreach ($token_strings as $token_string) {
474  // each token info is in the 'name=value' format
475  $token_item = explode('=', $token_string);
476  if (count($token_item) == 2) {
477  $token_info[$token_item[0]] = $token_item[1];
478  }
479  }
480 
481  return $token_info;
482 
483  }//end _getTokenInfoArray()
484 
485 
495  public function getRequestToken($consumer_key='', $consumer_secret='')
496  {
497  $this->_config = @unserialize($this->value);
498  if (empty($this->_config)) return;
499 
500  if (empty($this->_config['request_token_url'])) return;
501 
502  // Build the result data structure. This will be returned eventually, and is also used to form the cache keys.
503  $this->_res = Array(
504  'request' => Array(
505  'method' => $this->_config['method'],
506  'headers' => count($this->_config['request_headers']) ? $this->_config['request_headers'] : Array(),
507  'body' => $this->_config['request_body'],
508  'urls' => Array($this->_config['request_token_url']),
509  'auth' => Array(
510  'type' => 'NONE',
511  ),
512  'consumer_key' => $this->_config['consumer_key'],
513  'consumer_secret' => $this->_config['consumer_secret'],
514  'callback_url' => $this->_config['callback_url'],
515  ),
516  'responses' => Array(),
517  'response' => Array(),
518  );
519 
520  // Replace keywords in each URL.
521  foreach ($this->_res['request']['urls'] as &$url) {
522  $this->_replaceKeywords($url);
523  }
524 
525  // Replace keywords in each Header.
526  for ($i = 0; $i < count($this->_res['request']['headers']); $i++) {
527  $this->_replaceKeywords($this->_res['request']['headers'][$i]);
528 
529  // Discard badly formatted headers.
530  if (preg_match('/(.+):(.+)/', $this->_res['request']['headers'][$i]) == 0) {
531  unset($this->_res['request']['headers'][$i]);
532  }
533  }
534 
535  // Replace keywords in the body.
536  $this->_replaceKeywords($this->_res['request']['body']);
537 
538  // replace keywords in oauth related values
539  $this->_replaceKeywords($this->_res['request']['consumer_key']);
540  $this->_replaceKeywords($this->_res['request']['consumer_secret']);
541  $this->_replaceKeywords($this->_res['request']['callback_url']);
542 
543  // if the header parameters haven't been constructed yet,
544  // construct the header parameters required for requesting request token
545  if (empty($this->_header_params)) {
546  $nonce = $this->_generateNonce();
547  $timestamp = time();
548 
549  $this->_header_params = Array(
550  'oauth_consumer_key' => $this->_res['request']['consumer_key'],
551  'oauth_signature_method' => $this->_config['signature_method'],
552  'oauth_timestamp' => $timestamp,
553  'oauth_nonce' => $nonce,
554  'oauth_version' => "1.0",
555  'oauth_callback' => $this->_res['request']['callback_url'],
556  );
557 
558  // gather parameters reuired for signature and encode them before signing the signature
559  $all_params = $this->_header_params = $this->_encodeParam($this->_header_params);
560  $query_params = $this->_getQueryParameterArray($this->_res['request']['urls'][0]);
561  if (!empty($query_params)) {
562  // if query parameters exist, they have to be included in the signature
563  // but shouldn't be included in the header parameters
564  $all_params = array_merge($all_params, $query_params);
565  }
566 
567  // construct a base string for the signature
568  // request url used for signature shouldn't contain request parameters
569  $request_url = explode('?', $this->_res['request']['urls'][0]);
570  $base_string = $this->_generateBaseString($this->_config['method'], $request_url[0], $all_params);
571 
572  // signature key is a combination of cosumer key and token secret
573  // but we haven't got a token secret yet, so, the token secret is an empty string
574  $token_secret = '';
575  $signature_key = $this->_encodeParam($this->_res['request']['consumer_secret']).'&'.$this->_encodeParam($token_secret);
576 
577  // generate a signature and set is as a header parameter
578  $this->_header_params['oauth_signature'] = $this->_generateSignature($signature_key, $base_string, $this->_config['signature_method']);
579  }
580 
581  // generate a header string required for oauth authorization
582  $header_string = $this->_generateAuthorizationHeader($this->_header_params);
583  $this->_res['request']['headers'][] = $header_string;
584 
585  // Go!
586  $this->_performRequests();
587 
588  // Set the first in responses to response
589  $this->_res['response'] =& $this->_res['responses'][0];
590 
591  // if the response is received, the response should contain request token & request token secret
592  // (and possibly callback_confirmed which is always true)
593  // remember the token and secret and set secret in the session to make it available later for access token request
594  // then clear the response and have the token authorized
595  if (!empty($this->_res['response']['body'])) {
596  $token_info = $this->_getTokenInfoArray($this->_res['response']['body']);
597 
598  // to move on to user authorization, token and token secret are required
599  if (empty($token_info) || !isset($token_info['oauth_token']) || !isset($token_info['oauth_token_secret'])) {
600  $this->_errors[] = translate('oauth_request_token_failed', $this->_res['request']['urls'][0], $this->_res['response']['info']['http_code'], $this->_res['response']['body']);
601  return $this->_res;
602  }
603 
604  // return the request token
605  $this->_res['request_token'] = $token_info;
606  return $this->_res;
607  }
608 
609  // shouldn't reach here...but if it does, it means we didn't get a response body, so, something must have gone wrong...
610  $this->_errors[] = translate('oauth_request_token_failed', $this->_res['request']['urls'][0], $this->_res['response']['info']['http_code'], $this->_res['response']['body']);
611 
612  return NULL;
613 
614  }//end getRequestToken()
615 
616 
626  public function authorizeRedirect($request_token = Array())
627  {
628  $this->_config = @unserialize($this->value);
629  if (empty($this->_config)) return;
630  if (empty($this->_config['authorization_url'])) return;
631  if(!isset($request_token['oauth_token'])) return;
632 
633  $url = $this->_config['authorization_url'].'?oauth_token='.$request_token['oauth_token'];
634  do_redirect($url);
635  }//end authorizeRedirect()
636 
644  public function authorize($request_token = Array())
645  {
646  $this->_config = @unserialize($this->value);
647  if (empty($this->_config)) return;
648 
649  if (empty($this->_config['authorization_url'])) return;
650 
651  // if a request token is not available, it cannot be authorized by the user
652  if (empty($request_token) || !isset($request_token['oauth_token'])) {
653  $auth_url = $this->_config['authorization_url'];
654  $this->_replaceKeywords($auth_url);
655  $this->_errors[] = translate('oauth_request_token_not_found', $auth_url);
656  return NULL;
657  }
658 
659  // Build the result data structure. This will be returned eventually, and is also used to form the cache keys.
660  $this->_res = Array(
661  'request' => Array(
662  'method' => $this->_config['method'],
663  'headers' => Array(),
664  'body' => '',
665  'urls' => Array($this->_config['authorization_url'].'?oauth_token='.$request_token['oauth_token']),
666  'auth' => Array(
667  'type' => 'NONE',
668  ),
669  ),
670  'responses' => Array(),
671  'response' => Array(),
672  );
673 
674  // Replace keywords in each URL.
675  foreach ($this->_res['request']['urls'] as &$url) {
676  $this->_replaceKeywords($url);
677  }
678 
679  // Replace keywords in each Header.
680  for ($i = 0; $i < count($this->_res['request']['headers']); $i++) {
681  $this->_replaceKeywords($this->_res['request']['headers'][$i]);
682 
683  // Discard badly formatted headers.
684  if (preg_match('/(.+):(.+)/', $this->_res['request']['headers'][$i]) == 0) {
685  unset($this->_res['request']['headers'][$i]);
686  }
687  }
688 
689  // Replace keywords in the body.
690  $this->_replaceKeywords($this->_res['request']['body']);
691 
692  // Go!
693  $this->_performRequests();
694 
695  // Set the first in responses to response
696  $this->_res['response'] =& $this->_res['responses'][0];
697 
698  // we need to rely on the http code they return in the response to check if the request went through
699  // as the presentation of authorization/log in pages are up to the service provider...
700  if ($this->_res['response']['info']['http_code'] >= 400) {
701  $this->_errors[] = translate('oauth_authorization_request_failed', $this->_res['request']['urls'][0], $this->_res['response']['info']['http_code'], $this->_res['response']['body']);
702  return $this->_res;
703  }
704 
705  // _res now should contain the authorization/log in page of the service provider
706  return $this->_res;
707 
708  }//end authorize()
709 
710 
720  public function getAccessToken($authorized_token, $verifier, $token_secret)
721  {
722  $this->_config = @unserialize($this->value);
723  if (empty($this->_config)) return;
724 
725  // we need access token request url
726  if (empty($this->_config['access_token_url'])) return;
727 
728  // we need authorized token, verifier and token secret returned from the service provider to carry on
729  if (!isset($authorized_token) || !isset($verifier) || !isset($token_secret)) {
730  $access_url = $this->_config['access_token_url'];
731  $this->_replaceKeywords($access_url);
732  $this->_errors[] = translate('oauth_access_token_missing_params', $access_url, $this->_res['response']['info']['http_code'], $this->_res['response']['body']);
733  return NULL;
734  }
735 
736  // Build the result data structure. This will be returned eventually, and is also used to form the cache keys.
737  $this->_res = Array(
738  'request' => Array(
739  'method' => $this->_config['method'],
740  'headers' => count($this->_config['request_headers']) ? $this->_config['request_headers'] : Array(),
741  'body' => '',
742  'urls' => Array($this->_config['access_token_url']),
743  'auth' => Array(
744  'type' => 'NONE',
745  ),
746  'consumer_key' => $this->_config['consumer_key'],
747  'consumer_secret' => $this->_config['consumer_secret'],
748  ),
749  'responses' => Array(),
750  'response' => Array(),
751  );
752 
753  // Replace keywords in each URL.
754  foreach ($this->_res['request']['urls'] as &$url) {
755  $this->_replaceKeywords($url);
756  }
757 
758  // Replace keywords in each Header.
759  for ($i = 0; $i < count($this->_res['request']['headers']); $i++) {
760  $this->_replaceKeywords($this->_res['request']['headers'][$i]);
761 
762  // Discard badly formatted headers.
763  if (preg_match('/(.+):(.+)/', $this->_res['request']['headers'][$i]) == 0) {
764  unset($this->_res['request']['headers'][$i]);
765  }
766  }
767 
768  // Replace keywords in the body.
769  $this->_replaceKeywords($this->_res['request']['body']);
770 
771  // replace keywords in oauth related values
772  $this->_replaceKeywords($this->_res['request']['consumer_key']);
773  $this->_replaceKeywords($this->_res['request']['consumer_secret']);
774 
775  // if the header parameters haven't been constructed yet,
776  // construct the header parameters required for requesting access token
777  if (empty($this->_header_params)) {
778  $nonce = $this->_generateNonce();
779  $timestamp = time();
780 
781  $this->_header_params = Array(
782  'oauth_consumer_key' => $this->_res['request']['consumer_key'],
783  'oauth_token' => $authorized_token,
784  'oauth_signature_method' => $this->_config['signature_method'],
785  'oauth_timestamp' => $timestamp,
786  'oauth_nonce' => $nonce,
787  'oauth_version' => "1.0",
788  'oauth_verifier' => $verifier,
789  );
790 
791  // gather parameters reuired for signature and encode them before signing the signature
792  $all_params = $this->_header_params = $this->_encodeParam($this->_header_params);
793  $query_params = $this->_getQueryParameterArray($this->_res['request']['urls'][0]);
794  if (!empty($query_params)) {
795  // if query parameters exist, they have to be included in the signature
796  // but shouldn't be included in the header parameters
797  $all_params = array_merge($all_params, $query_params);
798  }
799 
800  // construct a base string for the signature
801  // request url used for signature shouldn't contain request parameters
802  $request_url = explode('?', $this->_res['request']['urls'][0]);
803  $base_string = $this->_generateBaseString($this->_config['method'], $request_url[0], $all_params);
804 
805  // signature key is a combination of cosumer key and token secret (received when a request token was issued)
806  $signature_key = $this->_encodeParam($this->_res['request']['consumer_secret']).'&'.$token_secret;
807 
808  // generate a signature and set is as a header parameter
809  $this->_header_params['oauth_signature'] = $this->_generateSignature($signature_key, $base_string, $this->_config['signature_method']);
810  }
811 
812  // generate a header string required for oauth authorization
813  $header_string = $this->_generateAuthorizationHeader($this->_header_params);
814  $this->_res['request']['headers'][] = $header_string;
815 
816  // Go!
817  $this->_performRequests();
818 
819  // Set the first in responses to response
820  $this->_res['response'] =& $this->_res['responses'][0];
821 
822  // if the response is received, the response should contain access token & access token secret
823  // remember the token and secret in the session to make them available later to obtain user data
824  // and set the session variable to indicate we have successfully exchaned the request(authorized) token
825  // to an access token, then clear the response and request user data
826  if (!empty($this->_res['response']['body'])) {
827  $token_info = $this->_getTokenInfoArray($this->_res['response']['body']);
828 
829  if (empty($token_info) || !isset($token_info['oauth_token']) || !isset($token_info['oauth_token_secret'])) {
830  $this->_errors[] = translate('oauth_access_token_failed', $this->_res['request']['urls'][0], $this->_res['response']['info']['http_code'], $this->_res['response']['body']);
831  return $this->_res;
832  }
833 
834  // return the access token
835  $this->_res['access_token'] = $token_info;
836  return $this->_res;
837  }
838 
839  return $this->_res;
840 
841  }//end getAccessToken()
842 
843 
852  public function getUserDataAuthHeader($url='', $access_token='', $access_token_secret='', $url_encode = TRUE)
853  {
854  $this->_config = @unserialize($this->value);
855  if (empty($this->_config)) return;
856 
857  // we need feed url, access token and access secret
858  if (empty($url) || ($this->getType() == 'THREE_LEGGED' && (empty($access_token) || empty($access_token_secret)))) {
859  $this->_errors[] = translate('oauth_user_data_missing_params', $url);
860  return;
861  }
862 
863  $nonce = $this->_generateNonce();
864  $timestamp = time();
865 
866  // construct the header parameters required for requesting user data
867  $this->_header_params = Array(
868  'oauth_consumer_key' => $this->_config['consumer_key'],
869  'oauth_signature_method' => $this->_config['signature_method'],
870  'oauth_timestamp' => $timestamp,
871  'oauth_nonce' => $nonce,
872  'oauth_version' => "1.0",
873  );
874 
875  // gather parameters reuired for signature and encode them before signing the signature
876  // but don't encode the access token as it's already been encoded
877  $all_params = $this->_header_params = $this->_encodeParam($this->_header_params);
878  if ($this->getType() == 'THREE_LEGGED') $all_params['oauth_token'] = $this->_header_params['oauth_token'] = $access_token;
879  $query_params = $this->_getQueryParameterArray($url, $url_encode);
880  // include post body in the base string
881  if($this->_config['method'] == 'POST' && !empty($this->_config['request_body'])) {
882  $post_params = $this->_getQueryParameterArray('?'.$this->_config['request_body']);
883  $query_params = array_merge($query_params, $post_params);
884  }
885  if (!empty($query_params)) {
886  // if query parameters exist, they have to be included in the signature
887  // but shouldn't be included in the header parameters
888  $all_params = array_merge($all_params, $query_params);
889  }
890 
891  // construct a base string for the signature
892  // request url used for signature shouldn't contain request parameters
893  $request_url = explode('?', $url);
894  $base_string = $this->_generateBaseString($this->_config['method'], $request_url[0], $all_params);
895 
896  // signature key is a combination of cosumer key and token secret (received when an access token was issued)
897  $signature_key = $this->_encodeParam($this->_config['consumer_secret']).'&'.$access_token_secret;
898  $this->_header_params['oauth_signature'] = $this->_generateSignature($signature_key, $base_string, $this->_config['signature_method']);
899 
900  // generate a header string required to get user data
901  return $this->_generateAuthorizationHeader($this->_header_params);
902 
903  }//end getUserDataAuthHeader
904 
905 
914  public function getErrors()
915  {
916  return $this->_errors;
917 
918  }//end getErrors()
919 
920 
929  public function getType()
930  {
931  $value = @unserialize($this->value);
932  return isset($value['type']) ? $value['type'] : 'THREE_LEGGED';
933  }
934 
935 
944  public function setConfig($config_name, $config_value)
945  {
946  $value = @unserialize($this->value);
947  if ($value[$config_name] == $config_value) return;
948  switch ($config_name) {
949  // if the config to set is 'method' check the value is valid
950  case 'method':
951  if (isset($this->_methods[$config_value])) {
952  $value['method'] = $config_value;
953  }
954  break;
955  // otherwise set the config to this asset's attribute
956  default:
957  $value[$config_name] = $config_value;
958  }
959 
960  $this->setValue($value);
961 
962  }
963 
964 }
965 ?>