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';
34 private $_methods = Array(
38 private $_signature_methods = Array(
39 'HMAC-SHA1' =>
'HMAC-SHA1',
41 'PLAINTEXT' =>
'PLAINTEXT',
43 private $_type = Array(
44 'THREE_LEGGED' =>
'3 legged',
45 'TWO_LEGGED' =>
'2 legged',
48 private $_header_params = Array();
49 private $_errors = Array();
59 parent::__construct($attribute, $value);
73 public function paint($prefix, $read_only = FALSE)
75 $prefix = str_replace(
':',
'_', $prefix);
76 $current_value = @unserialize($this->value);
79 if (!is_array($current_value)) $current_value = Array();
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';
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'] =
'';
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'] =
'';
96 <table border=
"0" class=
"sq-backend-table">
98 <th width=
"20%"><?php echo translate(
'oauth_consumer_key'); ?></th>
102 echo htmlspecialchars($current_value[
'consumer_key']);
104 text_box($prefix.
'_consumer_key', $current_value[
'consumer_key'], 80);
110 <th width=
"20%"><?php echo translate(
'oauth_consumer_secret'); ?></th>
114 echo htmlspecialchars($current_value[
'consumer_secret']);
116 text_box($prefix.
'_consumer_secret', $current_value[
'consumer_secret'], 80);
121 <?php
if (!isset($current_value[
'type']) || $current_value[
'type'] ==
'THREE_LEGGED') {?>
123 <th width=
"20%"><?php echo translate(
'http_request_method'); ?></th>
127 echo htmlspecialchars($current_value[
'method']);
129 combo_box($prefix.
'_method', $this->_methods, FALSE, $current_value[
'method']);
135 <th><?php echo translate(
'oauth_request_token_url'); ?></th>
139 echo htmlspecialchars($current_value[
'request_token_url']);
141 text_box($prefix.
'_request_token_url', $current_value[
'request_token_url'], 80);
147 <th><?php echo translate(
'oauth_authorization_url'); ?></th>
151 echo htmlspecialchars($current_value[
'authorization_url']);
153 text_box($prefix.
'_authorization_url', $current_value[
'authorization_url'], 80);
159 <th><?php echo translate(
'oauth_access_token_url'); ?></th>
163 echo htmlspecialchars($current_value[
'access_token_url']);
165 text_box($prefix.
'_access_token_url', $current_value[
'access_token_url'], 80);
171 <th><?php echo translate(
'oauth_callback_url'); ?></th>
175 echo htmlspecialchars($current_value[
'callback_url']);
177 text_box($prefix.
'_callback_url', $current_value[
'callback_url'], 80);
184 <th><?php echo translate(
'oauth_signature_method'); ?></th>
188 echo htmlspecialchars($this->_signature_methods[$current_value[
'signature_method']]);
190 combo_box($prefix.
'_signature_method', $this->_signature_methods, FALSE, $current_value[
'signature_method']);
195 <?php
if (!isset($current_value[
'type']) || $current_value[
'type'] ==
'THREE_LEGGED') {?>
197 <th><?php echo translate(
'http_request_request_headers'); ?></th>
201 $hl->value = implode($hl->delimiter, $current_value[
'request_headers']);
202 $hl->setEditParam(
'width',
'60');
203 $hl->paint($prefix.
'_request_headers', $read_only);
209 if (($current_value[
'method'] ==
'POST') || ($current_value[
'method'] ==
'PUT')) {
212 <th><?php echo translate(
'http_request_request_body'); ?></th>
216 echo htmlspecialchars($current_value[
'request_body']);
218 text_area($prefix.
'_request_body', $current_value[
'request_body'], 70, 20, 0,
'style="font-family: monospace;"');
224 <th><?php echo translate(
'http_request_timeout'); ?></th>
228 echo htmlspecialchars($current_value[
'timeout']);
230 text_box($prefix.
'_timeout', $current_value[
'timeout'], 4);
237 hidden_field($prefix.
'_request_body', $current_value[
'request_body']);
256 $prefix = str_replace(
':',
'_', $prefix);
260 if ($this->
getType() !=
'TWO_LEGGED' && !isset($_REQUEST[$prefix.
'_method']))
return FALSE;
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'] :
'';
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'] :
'';
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']) :
'';
281 $hl->process($prefix.
'_request_headers');
282 $value[
'request_headers'] = trim($hl->value) ? explode($hl->delimiter, $hl->value) : NULL;
284 $value[
'type'] = $this->
getType();
286 $value[
'run_test'] = FALSE;
288 $this->processed = $this->
setValue($value);
302 private function _encodeParam($input, $url_encode = TRUE)
305 if (is_array($input)) {
306 $encoded_array = Array();
307 foreach ($input as $key => $val) {
308 $encoded_array[$this->_encodeParam($key)] = $this->_encodeParam($val);
310 return $encoded_array;
312 $input = utf8_encode($input);
314 $input = str_replace(
'+',
' ', str_replace(
'%7E',
'~', rawurlencode($input)));
331 private function _generateNonce()
335 return md5($mt . $rand);
350 private function _generateBaseString($request_method, $request_url, $params)
352 $base_string_params = Array();
355 foreach ($params as $param_key => $param_val) {
356 $base_string_params[] = $param_key.
'='.$param_val;
360 sort($base_string_params);
363 $base_string = implode(
'&', $base_string_params);
366 return $this->_encodeParam($request_method).
'&'.$this->_encodeParam($request_url).
'&'.$this->_encodeParam($base_string);
381 private function _generateSignature($key, $base_string, $method)
385 return $this->_encodeParam(base64_encode(hash_hmac(
"sha1", $base_string, $key, TRUE)));
392 return $this->_encodeParam($base_string);
406 private function _generateAuthorizationHeader($params)
409 foreach ($params as $param_key => $param_val) {
410 $headers[] = $param_key.
'="'.$param_val.
'"';
412 return 'Authorization: OAuth '.implode(
',', $headers);
426 private function _getQueryParameterArray($url, $url_encode = TRUE)
428 if (empty($url))
return NULL;
430 $query_parameters = 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);
442 $param_val[0] = urldecode($param_val[0]);
443 $param_val[1] = urldecode($param_val[1]);
445 $query_parameters[$this->_encodeParam($param_val[0], $url_encode)] = $this->_encodeParam($param_val[1], $url_encode);
449 return $query_parameters;
462 private function _getTokenInfoArray($token_info_string)
464 $token_info = Array();
467 $token_strings = explode(
'&', $token_info_string);
470 if (count($token_strings) <= 1)
return $token_info;
473 foreach ($token_strings as $token_string) {
475 $token_item = explode(
'=', $token_string);
476 if (count($token_item) == 2) {
477 $token_info[$token_item[0]] = $token_item[1];
497 $this->_config = @unserialize($this->value);
498 if (empty($this->_config))
return;
500 if (empty($this->_config[
'request_token_url']))
return;
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']),
512 'consumer_key' => $this->_config[
'consumer_key'],
513 'consumer_secret' => $this->_config[
'consumer_secret'],
514 'callback_url' => $this->_config[
'callback_url'],
516 'responses' => Array(),
517 'response' => Array(),
521 foreach ($this->_res[
'request'][
'urls'] as &$url) {
526 for ($i = 0; $i < count($this->_res[
'request'][
'headers']); $i++) {
530 if (preg_match(
'/(.+):(.+)/', $this->_res[
'request'][
'headers'][$i]) == 0) {
531 unset($this->_res[
'request'][
'headers'][$i]);
545 if (empty($this->_header_params)) {
546 $nonce = $this->_generateNonce();
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'],
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)) {
564 $all_params = array_merge($all_params, $query_params);
569 $request_url = explode(
'?', $this->_res[
'request'][
'urls'][0]);
570 $base_string = $this->_generateBaseString($this->_config[
'method'], $request_url[0], $all_params);
575 $signature_key = $this->_encodeParam($this->_res[
'request'][
'consumer_secret']).
'&'.$this->_encodeParam($token_secret);
578 $this->_header_params[
'oauth_signature'] = $this->_generateSignature($signature_key, $base_string, $this->_config[
'signature_method']);
582 $header_string = $this->_generateAuthorizationHeader($this->_header_params);
583 $this->_res[
'request'][
'headers'][] = $header_string;
589 $this->_res[
'response'] =& $this->_res[
'responses'][0];
595 if (!empty($this->_res[
'response'][
'body'])) {
596 $token_info = $this->_getTokenInfoArray($this->_res[
'response'][
'body']);
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']);
605 $this->_res[
'request_token'] = $token_info;
610 $this->_errors[] = translate(
'oauth_request_token_failed', $this->_res[
'request'][
'urls'][0], $this->_res[
'response'][
'info'][
'http_code'], $this->_res[
'response'][
'body']);
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;
633 $url = $this->_config[
'authorization_url'].
'?oauth_token='.$request_token[
'oauth_token'];
646 $this->_config = @unserialize($this->value);
647 if (empty($this->_config))
return;
649 if (empty($this->_config[
'authorization_url']))
return;
652 if (empty($request_token) || !isset($request_token[
'oauth_token'])) {
653 $auth_url = $this->_config[
'authorization_url'];
655 $this->_errors[] = translate(
'oauth_request_token_not_found', $auth_url);
662 'method' => $this->_config[
'method'],
663 'headers' => Array(),
665 'urls' => Array($this->_config[
'authorization_url'].
'?oauth_token='.$request_token[
'oauth_token']),
670 'responses' => Array(),
671 'response' => Array(),
675 foreach ($this->_res[
'request'][
'urls'] as &$url) {
680 for ($i = 0; $i < count($this->_res[
'request'][
'headers']); $i++) {
684 if (preg_match(
'/(.+):(.+)/', $this->_res[
'request'][
'headers'][$i]) == 0) {
685 unset($this->_res[
'request'][
'headers'][$i]);
696 $this->_res[
'response'] =& $this->_res[
'responses'][0];
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']);
722 $this->_config = @unserialize($this->value);
723 if (empty($this->_config))
return;
726 if (empty($this->_config[
'access_token_url']))
return;
729 if (!isset($authorized_token) || !isset($verifier) || !isset($token_secret)) {
730 $access_url = $this->_config[
'access_token_url'];
732 $this->_errors[] = translate(
'oauth_access_token_missing_params', $access_url, $this->_res[
'response'][
'info'][
'http_code'], $this->_res[
'response'][
'body']);
739 'method' => $this->_config[
'method'],
740 'headers' => count($this->_config[
'request_headers']) ? $this->_config[
'request_headers'] : Array(),
742 'urls' => Array($this->_config[
'access_token_url']),
746 'consumer_key' => $this->_config[
'consumer_key'],
747 'consumer_secret' => $this->_config[
'consumer_secret'],
749 'responses' => Array(),
750 'response' => Array(),
754 foreach ($this->_res[
'request'][
'urls'] as &$url) {
759 for ($i = 0; $i < count($this->_res[
'request'][
'headers']); $i++) {
763 if (preg_match(
'/(.+):(.+)/', $this->_res[
'request'][
'headers'][$i]) == 0) {
764 unset($this->_res[
'request'][
'headers'][$i]);
777 if (empty($this->_header_params)) {
778 $nonce = $this->_generateNonce();
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,
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)) {
797 $all_params = array_merge($all_params, $query_params);
802 $request_url = explode(
'?', $this->_res[
'request'][
'urls'][0]);
803 $base_string = $this->_generateBaseString($this->_config[
'method'], $request_url[0], $all_params);
806 $signature_key = $this->_encodeParam($this->_res[
'request'][
'consumer_secret']).
'&'.$token_secret;
809 $this->_header_params[
'oauth_signature'] = $this->_generateSignature($signature_key, $base_string, $this->_config[
'signature_method']);
813 $header_string = $this->_generateAuthorizationHeader($this->_header_params);
814 $this->_res[
'request'][
'headers'][] = $header_string;
820 $this->_res[
'response'] =& $this->_res[
'responses'][0];
826 if (!empty($this->_res[
'response'][
'body'])) {
827 $token_info = $this->_getTokenInfoArray($this->_res[
'response'][
'body']);
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']);
835 $this->_res[
'access_token'] = $token_info;
854 $this->_config = @unserialize($this->value);
855 if (empty($this->_config))
return;
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);
863 $nonce = $this->_generateNonce();
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",
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);
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);
885 if (!empty($query_params)) {
888 $all_params = array_merge($all_params, $query_params);
893 $request_url = explode(
'?', $url);
894 $base_string = $this->_generateBaseString($this->_config[
'method'], $request_url[0], $all_params);
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']);
901 return $this->_generateAuthorizationHeader($this->_header_params);
916 return $this->_errors;
931 $value = @unserialize($this->value);
932 return isset($value[
'type']) ? $value[
'type'] :
'THREE_LEGGED';
946 $value = @unserialize($this->value);
947 if ($value[$config_name] == $config_value)
return;
948 switch ($config_name) {
951 if (isset($this->_methods[$config_value])) {
952 $value[
'method'] = $config_value;
957 $value[$config_name] = $config_value;