|
1
|
|
|
<?php |
|
2
|
|
|
ar_pinp::allow('ar_connect_oauth'); |
|
3
|
|
|
ar_pinp::allow('ar_connect_oauthClient'); |
|
4
|
|
|
|
|
5
|
|
|
class ar_connect_oauth extends arBase { |
|
6
|
|
|
|
|
7
|
|
|
public static function getSBS( $method, $uri, $request_parameters = array() ) { |
|
8
|
|
|
if ( function_exists('oauth_get_sbs') ) { |
|
9
|
|
|
return oauth_get_sbs( $method, $uri, $request_parameters ); |
|
10
|
|
|
} else { |
|
11
|
|
|
return ar_error::raiseError( 'OAuth PECL extension not installed.', ar_exceptions::CONFIGURATION_ERROR ); |
|
12
|
|
|
} |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public static function client( $consumer_key, $consumer_secret, $signature_method = OAUTH_SIG_METHOD_HMACSHA1, $auth_type = 0 ) { |
|
16
|
|
|
return new ar_connect_oauthClient( $consumer_key, $consumer_secret, $signature_method, $auth_type ); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
class ar_connect_oauthClient extends arWrapper implements ar_httpClient { |
|
22
|
|
|
|
|
23
|
|
|
public function __construct( $consumer_key, $consumer_secret, $signature_method= OAUTH_SIG_METHOD_HMACSHA1, $auth_type = 0 ) { |
|
24
|
|
|
if ( !class_exists('OAuth') ) { |
|
25
|
|
|
return ar_error::raiseError( 'OAuth PECL extension not installed', ar_exceptions::CONFIGURATION_ERROR ); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
$oauth = new OAuth( $consumer_key, $consumer_secret, $signature_method, $auth_type ); |
|
28
|
|
|
$oauth->setRequestEngine(OAUTH_REQENGINE_STREAMS); |
|
29
|
|
|
parent::__construct( $oauth ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function send( $type, $url, $request = null, $options = array() ) { |
|
33
|
|
|
if ( is_string($options['header']) ) { |
|
34
|
|
|
$headers = preg_split( '\r\n', $options['header'] ); |
|
35
|
|
|
} else { |
|
36
|
|
|
$headers = (array) $options['header']; |
|
37
|
|
|
} |
|
38
|
|
|
switch( $type ) { |
|
39
|
|
|
case 'GET' : $type = OAUTH_HTTP_METHOD_GET; |
|
40
|
|
|
break; |
|
41
|
|
|
case 'POST' : $type = OAUTH_HTTP_METHOD_POST; |
|
42
|
|
|
break; |
|
43
|
|
|
case 'PUT' : $type = OAUTH_HTTP_METHOD_PUT; |
|
44
|
|
|
break; |
|
45
|
|
|
case 'DELETE' : $type = OAUTH_HTTP_METHOD_DELETE; |
|
46
|
|
|
break; |
|
47
|
|
|
} |
|
48
|
|
|
try { |
|
49
|
|
|
if ( $this->wrapped->fetch( (string) $url, $request, $type, $headers ) ) { |
|
50
|
|
|
return $this->wrapped->getLastResponse(); |
|
51
|
|
|
} else { |
|
52
|
|
|
return ar_error::raiseError( 'OAuth fetch failed', ar_exceptions::UNKNOWN_ERROR ); |
|
53
|
|
|
} |
|
54
|
|
|
} catch( Exception $e ) { |
|
55
|
|
|
return ar_error::raiseError( $e->getMessage(), $e->getCode() ); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function get( $url, $request = null, $options = array() ) { |
|
60
|
|
|
return $this->send( 'GET', (string) $url, $request, $options ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function post( $url, $request = null, $options = array() ) { |
|
64
|
|
|
return $this->send( 'POST', (string) $url, $request, $options ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function put( $url, $request = null, $options = array() ) { |
|
68
|
|
|
return $this->send( 'PUT', (string) $url, $request, $options ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function delete( $url, $request = null, $options = array() ) { |
|
72
|
|
|
return $this->send( 'DELETE', (string) $url, $request, $options ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function headers( $headers ) { |
|
76
|
|
|
if (is_array($headers)) { |
|
77
|
|
|
$headers = join("\r\n", $headers); |
|
78
|
|
|
} |
|
79
|
|
|
if ($this->options['header'] && substr( $this->options['header'], -2 )!=="\r\n") { |
|
|
|
|
|
|
80
|
|
|
$this->options['header'] .= "\r\n"; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
$this->options['header'] .= $headers; |
|
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|