@@ -19,150 +19,150 @@ |
||
19 | 19 | const SCOPE_WRITE = 'write'; |
20 | 20 | |
21 | 21 | |
22 | - public function __construct( |
|
23 | - CredentialsInterface $credentials, |
|
24 | - ClientInterface $httpClient, |
|
25 | - TokenStorageInterface $storage, |
|
26 | - $scopes = array(), |
|
27 | - UriInterface $baseApiUri = null |
|
28 | - ) { |
|
29 | - parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri); |
|
30 | - if ($baseApiUri === null) { |
|
31 | - $url = getDolGlobalString('OAUTH_GENERIC-'.$storage->getKeyForProvider().'_URL'); |
|
32 | - //$url = 'https://aaaaa.com'; |
|
33 | - if (!empty($url)) { |
|
22 | + public function __construct( |
|
23 | + CredentialsInterface $credentials, |
|
24 | + ClientInterface $httpClient, |
|
25 | + TokenStorageInterface $storage, |
|
26 | + $scopes = array(), |
|
27 | + UriInterface $baseApiUri = null |
|
28 | + ) { |
|
29 | + parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri); |
|
30 | + if ($baseApiUri === null) { |
|
31 | + $url = getDolGlobalString('OAUTH_GENERIC-'.$storage->getKeyForProvider().'_URL'); |
|
32 | + //$url = 'https://aaaaa.com'; |
|
33 | + if (!empty($url)) { |
|
34 | 34 | $this->baseApiUri = new Uri($url); |
35 | - } |
|
36 | - } |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Return the private property $this->baseApiUri |
|
41 | - */ |
|
42 | - public function getBaseApiUri() |
|
43 | - { |
|
44 | - return $this->baseApiUri; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * {@inheritdoc} |
|
49 | - */ |
|
50 | - public function getRequestTokenEndpoint() |
|
51 | - { |
|
52 | - return new Uri($this->baseApiUri.'/oauth/request'); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * {@inheritdoc} |
|
57 | - */ |
|
58 | - public function getAuthorizationEndpoint() |
|
59 | - { |
|
60 | - return new Uri($this->baseApiUri.'/oauth/authorize'); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * {@inheritdoc} |
|
65 | - */ |
|
66 | - public function getAccessTokenEndpoint() |
|
67 | - { |
|
68 | - return new Uri($this->baseApiUri.'/oauth/authorize'); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * {@inheritdoc} |
|
73 | - */ |
|
74 | - public function getAuthorizationUri(array $additionalParameters = array()) |
|
75 | - { |
|
76 | - $parameters = array_merge( |
|
77 | - $additionalParameters, |
|
78 | - array( |
|
79 | - 'redirect_uri' => $this->credentials->getCallbackUrl(), |
|
80 | - ) |
|
81 | - ); |
|
82 | - |
|
83 | - // Build the url |
|
84 | - $url = clone $this->getAuthorizationEndpoint(); |
|
85 | - foreach ($parameters as $key => $val) { |
|
86 | - $url->addToQuery($key, $val); |
|
87 | - } |
|
88 | - |
|
89 | - return $url; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * {@inheritdoc} |
|
94 | - */ |
|
95 | - public function requestRequestToken() |
|
96 | - { |
|
97 | - $responseBody = $this->httpClient->retrieveResponse( |
|
98 | - $this->getRequestTokenEndpoint(), |
|
99 | - array( |
|
100 | - 'consumer_key' => $this->credentials->getConsumerId(), |
|
101 | - 'redirect_uri' => $this->credentials->getCallbackUrl(), |
|
102 | - ) |
|
103 | - ); |
|
104 | - |
|
105 | - $code = $this->parseRequestTokenResponse($responseBody); |
|
106 | - |
|
107 | - return $code; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * {@inheritdoc} |
|
112 | - */ |
|
113 | - protected function parseRequestTokenResponse($responseBody) |
|
114 | - { |
|
115 | - parse_str($responseBody, $data); |
|
116 | - |
|
117 | - if (null === $data || !is_array($data)) { |
|
118 | - throw new TokenResponseException('Unable to parse response.'); |
|
119 | - } elseif (!isset($data['code'])) { |
|
120 | - throw new TokenResponseException('Error in retrieving code.'); |
|
121 | - } |
|
122 | - return $data['code']; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * {@inheritdoc} |
|
127 | - */ |
|
128 | - public function requestAccessToken($code, $state = null) |
|
129 | - { |
|
130 | - $bodyParams = array( |
|
131 | - 'consumer_key' => $this->credentials->getConsumerId(), |
|
132 | - 'code' => $code, |
|
133 | - ); |
|
134 | - |
|
135 | - $responseBody = $this->httpClient->retrieveResponse( |
|
136 | - $this->getAccessTokenEndpoint(), |
|
137 | - $bodyParams, |
|
138 | - $this->getExtraOAuthHeaders() |
|
139 | - ); |
|
140 | - $token = $this->parseAccessTokenResponse($responseBody); |
|
141 | - $this->storage->storeAccessToken($this->service(), $token); |
|
142 | - |
|
143 | - return $token; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * {@inheritdoc} |
|
148 | - */ |
|
149 | - protected function parseAccessTokenResponse($responseBody) |
|
150 | - { |
|
151 | - parse_str($responseBody, $data); |
|
152 | - |
|
153 | - if ($data === null || !is_array($data)) { |
|
154 | - throw new TokenResponseException('Unable to parse response.'); |
|
155 | - } elseif (isset($data['error'])) { |
|
156 | - throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
|
157 | - } |
|
158 | - |
|
159 | - $token = new StdOAuth2Token(); |
|
160 | - #$token->setRequestToken($data['access_token']); |
|
161 | - $token->setAccessToken($data['access_token']); |
|
162 | - $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES); |
|
163 | - unset($data['access_token']); |
|
164 | - $token->setExtraParams($data); |
|
165 | - |
|
166 | - return $token; |
|
167 | - } |
|
35 | + } |
|
36 | + } |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Return the private property $this->baseApiUri |
|
41 | + */ |
|
42 | + public function getBaseApiUri() |
|
43 | + { |
|
44 | + return $this->baseApiUri; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * {@inheritdoc} |
|
49 | + */ |
|
50 | + public function getRequestTokenEndpoint() |
|
51 | + { |
|
52 | + return new Uri($this->baseApiUri.'/oauth/request'); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * {@inheritdoc} |
|
57 | + */ |
|
58 | + public function getAuthorizationEndpoint() |
|
59 | + { |
|
60 | + return new Uri($this->baseApiUri.'/oauth/authorize'); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * {@inheritdoc} |
|
65 | + */ |
|
66 | + public function getAccessTokenEndpoint() |
|
67 | + { |
|
68 | + return new Uri($this->baseApiUri.'/oauth/authorize'); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * {@inheritdoc} |
|
73 | + */ |
|
74 | + public function getAuthorizationUri(array $additionalParameters = array()) |
|
75 | + { |
|
76 | + $parameters = array_merge( |
|
77 | + $additionalParameters, |
|
78 | + array( |
|
79 | + 'redirect_uri' => $this->credentials->getCallbackUrl(), |
|
80 | + ) |
|
81 | + ); |
|
82 | + |
|
83 | + // Build the url |
|
84 | + $url = clone $this->getAuthorizationEndpoint(); |
|
85 | + foreach ($parameters as $key => $val) { |
|
86 | + $url->addToQuery($key, $val); |
|
87 | + } |
|
88 | + |
|
89 | + return $url; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * {@inheritdoc} |
|
94 | + */ |
|
95 | + public function requestRequestToken() |
|
96 | + { |
|
97 | + $responseBody = $this->httpClient->retrieveResponse( |
|
98 | + $this->getRequestTokenEndpoint(), |
|
99 | + array( |
|
100 | + 'consumer_key' => $this->credentials->getConsumerId(), |
|
101 | + 'redirect_uri' => $this->credentials->getCallbackUrl(), |
|
102 | + ) |
|
103 | + ); |
|
104 | + |
|
105 | + $code = $this->parseRequestTokenResponse($responseBody); |
|
106 | + |
|
107 | + return $code; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * {@inheritdoc} |
|
112 | + */ |
|
113 | + protected function parseRequestTokenResponse($responseBody) |
|
114 | + { |
|
115 | + parse_str($responseBody, $data); |
|
116 | + |
|
117 | + if (null === $data || !is_array($data)) { |
|
118 | + throw new TokenResponseException('Unable to parse response.'); |
|
119 | + } elseif (!isset($data['code'])) { |
|
120 | + throw new TokenResponseException('Error in retrieving code.'); |
|
121 | + } |
|
122 | + return $data['code']; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * {@inheritdoc} |
|
127 | + */ |
|
128 | + public function requestAccessToken($code, $state = null) |
|
129 | + { |
|
130 | + $bodyParams = array( |
|
131 | + 'consumer_key' => $this->credentials->getConsumerId(), |
|
132 | + 'code' => $code, |
|
133 | + ); |
|
134 | + |
|
135 | + $responseBody = $this->httpClient->retrieveResponse( |
|
136 | + $this->getAccessTokenEndpoint(), |
|
137 | + $bodyParams, |
|
138 | + $this->getExtraOAuthHeaders() |
|
139 | + ); |
|
140 | + $token = $this->parseAccessTokenResponse($responseBody); |
|
141 | + $this->storage->storeAccessToken($this->service(), $token); |
|
142 | + |
|
143 | + return $token; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * {@inheritdoc} |
|
148 | + */ |
|
149 | + protected function parseAccessTokenResponse($responseBody) |
|
150 | + { |
|
151 | + parse_str($responseBody, $data); |
|
152 | + |
|
153 | + if ($data === null || !is_array($data)) { |
|
154 | + throw new TokenResponseException('Unable to parse response.'); |
|
155 | + } elseif (isset($data['error'])) { |
|
156 | + throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
|
157 | + } |
|
158 | + |
|
159 | + $token = new StdOAuth2Token(); |
|
160 | + #$token->setRequestToken($data['access_token']); |
|
161 | + $token->setAccessToken($data['access_token']); |
|
162 | + $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES); |
|
163 | + unset($data['access_token']); |
|
164 | + $token->setExtraParams($data); |
|
165 | + |
|
166 | + return $token; |
|
167 | + } |
|
168 | 168 | } |
@@ -10,142 +10,142 @@ |
||
10 | 10 | */ |
11 | 11 | class CurlClient extends AbstractClient |
12 | 12 | { |
13 | - /** |
|
14 | - * If true, explicitly sets cURL to use SSL version 3. Use this if cURL |
|
15 | - * compiles with GnuTLS SSL. |
|
16 | - * |
|
17 | - * @var bool |
|
18 | - */ |
|
19 | - private $forceSSL3 = false; |
|
20 | - |
|
21 | - /** |
|
22 | - * Additional parameters (as `key => value` pairs) to be passed to `curl_setopt` |
|
23 | - * |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - private $parameters = array(); |
|
27 | - |
|
28 | - /** |
|
29 | - * Additional `curl_setopt` parameters |
|
30 | - * |
|
31 | - * @param array $parameters |
|
32 | - */ |
|
33 | - public function setCurlParameters(array $parameters) |
|
34 | - { |
|
35 | - $this->parameters = $parameters; |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * @param bool $force |
|
40 | - * |
|
41 | - * @return CurlClient |
|
42 | - */ |
|
43 | - public function setForceSSL3($force) |
|
44 | - { |
|
45 | - $this->forceSSL3 = $force; |
|
46 | - |
|
47 | - return $this; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * Any implementing HTTP providers should send a request to the provided endpoint with the parameters. |
|
52 | - * They should return, in string form, the response body and throw an exception on error. |
|
53 | - * |
|
54 | - * @param UriInterface $endpoint |
|
55 | - * @param mixed $requestBody |
|
56 | - * @param array $extraHeaders |
|
57 | - * @param string $method |
|
58 | - * |
|
59 | - * @return string |
|
60 | - * |
|
61 | - * @throws TokenResponseException |
|
62 | - * @throws \InvalidArgumentException |
|
63 | - */ |
|
64 | - public function retrieveResponse( |
|
65 | - UriInterface $endpoint, |
|
66 | - $requestBody, |
|
67 | - array $extraHeaders = array(), |
|
68 | - $method = 'POST' |
|
69 | - ) { |
|
70 | - // Normalize method name |
|
71 | - $method = strtoupper($method); |
|
72 | - |
|
73 | - $this->normalizeHeaders($extraHeaders); |
|
74 | - |
|
75 | - if ($method === 'GET' && !empty($requestBody)) { |
|
76 | - throw new \InvalidArgumentException('No body expected for "GET" request.'); |
|
77 | - } |
|
78 | - |
|
79 | - if (!isset($extraHeaders['Content-Type']) && $method === 'POST' && is_array($requestBody)) { |
|
80 | - $extraHeaders['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded'; |
|
81 | - } |
|
82 | - |
|
83 | - $extraHeaders['Host'] = 'Host: '.$endpoint->getHost(); |
|
84 | - $extraHeaders['Connection'] = 'Connection: close'; |
|
85 | - |
|
86 | - $ch = curl_init(); |
|
87 | - |
|
88 | - curl_setopt($ch, CURLOPT_URL, $endpoint->getAbsoluteUri()); |
|
89 | - |
|
90 | - if ($method === 'POST' || $method === 'PUT') { |
|
91 | - if ($requestBody && is_array($requestBody)) { |
|
92 | - $requestBody = http_build_query($requestBody, '', '&'); |
|
93 | - } |
|
94 | - |
|
95 | - if ($method === 'PUT') { |
|
96 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
97 | - } else { |
|
98 | - curl_setopt($ch, CURLOPT_POST, true); |
|
99 | - } |
|
100 | - |
|
101 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); |
|
102 | - } else { |
|
103 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
|
104 | - } |
|
105 | - |
|
106 | - if ($this->maxRedirects > 0) { |
|
107 | - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
108 | - curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirects); |
|
109 | - } |
|
110 | - |
|
111 | - curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
|
112 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
113 | - curl_setopt($ch, CURLOPT_HEADER, false); |
|
114 | - curl_setopt($ch, CURLOPT_HTTPHEADER, $extraHeaders); |
|
115 | - curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); |
|
116 | - |
|
117 | - foreach ($this->parameters as $key => $value) { |
|
118 | - curl_setopt($ch, $key, $value); |
|
119 | - } |
|
120 | - |
|
121 | - if ($this->forceSSL3) { |
|
122 | - curl_setopt($ch, CURLOPT_SSLVERSION, 3); |
|
123 | - } |
|
124 | - |
|
125 | - // @CHANGE DOL_LDR Add log |
|
126 | - if (function_exists('getDolGlobalString') && getDolGlobalString('OAUTH_DEBUG')) { |
|
127 | - file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", $endpoint->getAbsoluteUri()."\n", FILE_APPEND); |
|
128 | - file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", $requestBody."\n", FILE_APPEND); |
|
129 | - file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", $method."\n", FILE_APPEND); |
|
130 | - file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", var_export($extraHeaders, true)."\n", FILE_APPEND); |
|
131 | - @chmod(DOL_DATA_ROOT . "/dolibarr_oauth.log", octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK)); |
|
132 | - } |
|
133 | - |
|
134 | - $response = curl_exec($ch); |
|
135 | - $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
136 | - |
|
137 | - if (false === $response) { |
|
138 | - $errNo = curl_errno($ch); |
|
139 | - $errStr = curl_error($ch); |
|
140 | - curl_close($ch); |
|
141 | - if (empty($errStr)) { |
|
142 | - throw new TokenResponseException('Failed to request resource.', $responseCode); |
|
143 | - } |
|
144 | - throw new TokenResponseException('cURL Error # '.$errNo.': '.$errStr, $responseCode); |
|
145 | - } |
|
146 | - |
|
147 | - curl_close($ch); |
|
148 | - |
|
149 | - return $response; |
|
150 | - } |
|
13 | + /** |
|
14 | + * If true, explicitly sets cURL to use SSL version 3. Use this if cURL |
|
15 | + * compiles with GnuTLS SSL. |
|
16 | + * |
|
17 | + * @var bool |
|
18 | + */ |
|
19 | + private $forceSSL3 = false; |
|
20 | + |
|
21 | + /** |
|
22 | + * Additional parameters (as `key => value` pairs) to be passed to `curl_setopt` |
|
23 | + * |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + private $parameters = array(); |
|
27 | + |
|
28 | + /** |
|
29 | + * Additional `curl_setopt` parameters |
|
30 | + * |
|
31 | + * @param array $parameters |
|
32 | + */ |
|
33 | + public function setCurlParameters(array $parameters) |
|
34 | + { |
|
35 | + $this->parameters = $parameters; |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * @param bool $force |
|
40 | + * |
|
41 | + * @return CurlClient |
|
42 | + */ |
|
43 | + public function setForceSSL3($force) |
|
44 | + { |
|
45 | + $this->forceSSL3 = $force; |
|
46 | + |
|
47 | + return $this; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * Any implementing HTTP providers should send a request to the provided endpoint with the parameters. |
|
52 | + * They should return, in string form, the response body and throw an exception on error. |
|
53 | + * |
|
54 | + * @param UriInterface $endpoint |
|
55 | + * @param mixed $requestBody |
|
56 | + * @param array $extraHeaders |
|
57 | + * @param string $method |
|
58 | + * |
|
59 | + * @return string |
|
60 | + * |
|
61 | + * @throws TokenResponseException |
|
62 | + * @throws \InvalidArgumentException |
|
63 | + */ |
|
64 | + public function retrieveResponse( |
|
65 | + UriInterface $endpoint, |
|
66 | + $requestBody, |
|
67 | + array $extraHeaders = array(), |
|
68 | + $method = 'POST' |
|
69 | + ) { |
|
70 | + // Normalize method name |
|
71 | + $method = strtoupper($method); |
|
72 | + |
|
73 | + $this->normalizeHeaders($extraHeaders); |
|
74 | + |
|
75 | + if ($method === 'GET' && !empty($requestBody)) { |
|
76 | + throw new \InvalidArgumentException('No body expected for "GET" request.'); |
|
77 | + } |
|
78 | + |
|
79 | + if (!isset($extraHeaders['Content-Type']) && $method === 'POST' && is_array($requestBody)) { |
|
80 | + $extraHeaders['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded'; |
|
81 | + } |
|
82 | + |
|
83 | + $extraHeaders['Host'] = 'Host: '.$endpoint->getHost(); |
|
84 | + $extraHeaders['Connection'] = 'Connection: close'; |
|
85 | + |
|
86 | + $ch = curl_init(); |
|
87 | + |
|
88 | + curl_setopt($ch, CURLOPT_URL, $endpoint->getAbsoluteUri()); |
|
89 | + |
|
90 | + if ($method === 'POST' || $method === 'PUT') { |
|
91 | + if ($requestBody && is_array($requestBody)) { |
|
92 | + $requestBody = http_build_query($requestBody, '', '&'); |
|
93 | + } |
|
94 | + |
|
95 | + if ($method === 'PUT') { |
|
96 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
97 | + } else { |
|
98 | + curl_setopt($ch, CURLOPT_POST, true); |
|
99 | + } |
|
100 | + |
|
101 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); |
|
102 | + } else { |
|
103 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
|
104 | + } |
|
105 | + |
|
106 | + if ($this->maxRedirects > 0) { |
|
107 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
108 | + curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirects); |
|
109 | + } |
|
110 | + |
|
111 | + curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
|
112 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
113 | + curl_setopt($ch, CURLOPT_HEADER, false); |
|
114 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $extraHeaders); |
|
115 | + curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); |
|
116 | + |
|
117 | + foreach ($this->parameters as $key => $value) { |
|
118 | + curl_setopt($ch, $key, $value); |
|
119 | + } |
|
120 | + |
|
121 | + if ($this->forceSSL3) { |
|
122 | + curl_setopt($ch, CURLOPT_SSLVERSION, 3); |
|
123 | + } |
|
124 | + |
|
125 | + // @CHANGE DOL_LDR Add log |
|
126 | + if (function_exists('getDolGlobalString') && getDolGlobalString('OAUTH_DEBUG')) { |
|
127 | + file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", $endpoint->getAbsoluteUri()."\n", FILE_APPEND); |
|
128 | + file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", $requestBody."\n", FILE_APPEND); |
|
129 | + file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", $method."\n", FILE_APPEND); |
|
130 | + file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", var_export($extraHeaders, true)."\n", FILE_APPEND); |
|
131 | + @chmod(DOL_DATA_ROOT . "/dolibarr_oauth.log", octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK)); |
|
132 | + } |
|
133 | + |
|
134 | + $response = curl_exec($ch); |
|
135 | + $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
136 | + |
|
137 | + if (false === $response) { |
|
138 | + $errNo = curl_errno($ch); |
|
139 | + $errStr = curl_error($ch); |
|
140 | + curl_close($ch); |
|
141 | + if (empty($errStr)) { |
|
142 | + throw new TokenResponseException('Failed to request resource.', $responseCode); |
|
143 | + } |
|
144 | + throw new TokenResponseException('cURL Error # '.$errNo.': '.$errStr, $responseCode); |
|
145 | + } |
|
146 | + |
|
147 | + curl_close($ch); |
|
148 | + |
|
149 | + return $response; |
|
150 | + } |
|
151 | 151 | } |
@@ -10,96 +10,96 @@ |
||
10 | 10 | */ |
11 | 11 | class StreamClient extends AbstractClient |
12 | 12 | { |
13 | - /** |
|
14 | - * Any implementing HTTP providers should send a request to the provided endpoint with the parameters. |
|
15 | - * They should return, in string form, the response body and throw an exception on error. |
|
16 | - * |
|
17 | - * @param UriInterface $endpoint |
|
18 | - * @param mixed $requestBody |
|
19 | - * @param array $extraHeaders |
|
20 | - * @param string $method |
|
21 | - * |
|
22 | - * @return string |
|
23 | - * |
|
24 | - * @throws TokenResponseException |
|
25 | - * @throws \InvalidArgumentException |
|
26 | - */ |
|
27 | - public function retrieveResponse( |
|
28 | - UriInterface $endpoint, |
|
29 | - $requestBody, |
|
30 | - array $extraHeaders = array(), |
|
31 | - $method = 'POST' |
|
32 | - ) { |
|
33 | - // Normalize method name |
|
34 | - $method = strtoupper($method); |
|
13 | + /** |
|
14 | + * Any implementing HTTP providers should send a request to the provided endpoint with the parameters. |
|
15 | + * They should return, in string form, the response body and throw an exception on error. |
|
16 | + * |
|
17 | + * @param UriInterface $endpoint |
|
18 | + * @param mixed $requestBody |
|
19 | + * @param array $extraHeaders |
|
20 | + * @param string $method |
|
21 | + * |
|
22 | + * @return string |
|
23 | + * |
|
24 | + * @throws TokenResponseException |
|
25 | + * @throws \InvalidArgumentException |
|
26 | + */ |
|
27 | + public function retrieveResponse( |
|
28 | + UriInterface $endpoint, |
|
29 | + $requestBody, |
|
30 | + array $extraHeaders = array(), |
|
31 | + $method = 'POST' |
|
32 | + ) { |
|
33 | + // Normalize method name |
|
34 | + $method = strtoupper($method); |
|
35 | 35 | |
36 | - $this->normalizeHeaders($extraHeaders); |
|
36 | + $this->normalizeHeaders($extraHeaders); |
|
37 | 37 | |
38 | - if ($method === 'GET' && !empty($requestBody)) { |
|
39 | - throw new \InvalidArgumentException('No body expected for "GET" request.'); |
|
40 | - } |
|
38 | + if ($method === 'GET' && !empty($requestBody)) { |
|
39 | + throw new \InvalidArgumentException('No body expected for "GET" request.'); |
|
40 | + } |
|
41 | 41 | |
42 | - if (!isset($extraHeaders['Content-Type']) && $method === 'POST' && is_array($requestBody)) { |
|
43 | - $extraHeaders['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded'; |
|
44 | - } |
|
42 | + if (!isset($extraHeaders['Content-Type']) && $method === 'POST' && is_array($requestBody)) { |
|
43 | + $extraHeaders['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded'; |
|
44 | + } |
|
45 | 45 | |
46 | - $host = 'Host: '.$endpoint->getHost(); |
|
47 | - // Append port to Host if it has been specified |
|
48 | - if ($endpoint->hasExplicitPortSpecified()) { |
|
49 | - $host .= ':'.$endpoint->getPort(); |
|
50 | - } |
|
46 | + $host = 'Host: '.$endpoint->getHost(); |
|
47 | + // Append port to Host if it has been specified |
|
48 | + if ($endpoint->hasExplicitPortSpecified()) { |
|
49 | + $host .= ':'.$endpoint->getPort(); |
|
50 | + } |
|
51 | 51 | |
52 | - $extraHeaders['Host'] = $host; |
|
53 | - $extraHeaders['Connection'] = 'Connection: close'; |
|
52 | + $extraHeaders['Host'] = $host; |
|
53 | + $extraHeaders['Connection'] = 'Connection: close'; |
|
54 | 54 | |
55 | - if (is_array($requestBody)) { |
|
56 | - $requestBody = http_build_query($requestBody, '', '&'); |
|
57 | - } |
|
58 | - $extraHeaders['Content-length'] = 'Content-length: '.strlen($requestBody); |
|
55 | + if (is_array($requestBody)) { |
|
56 | + $requestBody = http_build_query($requestBody, '', '&'); |
|
57 | + } |
|
58 | + $extraHeaders['Content-length'] = 'Content-length: '.strlen($requestBody); |
|
59 | 59 | |
60 | - //var_dump($requestBody); var_dump($extraHeaders);var_dump($method);exit; |
|
61 | - $context = $this->generateStreamContext($requestBody, $extraHeaders, $method); |
|
60 | + //var_dump($requestBody); var_dump($extraHeaders);var_dump($method);exit; |
|
61 | + $context = $this->generateStreamContext($requestBody, $extraHeaders, $method); |
|
62 | 62 | |
63 | 63 | // @CHANGE DOL_LDR Add log |
64 | - if (function_exists('getDolGlobalString') && getDolGlobalString('OAUTH_DEBUG')) { |
|
65 | - file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", $endpoint->getAbsoluteUri()."\n", FILE_APPEND); |
|
66 | - file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", $requestBody."\n", FILE_APPEND); |
|
67 | - file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", $method."\n", FILE_APPEND); |
|
68 | - file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", var_export($extraHeaders, true)."\n", FILE_APPEND); |
|
69 | - @chmod(DOL_DATA_ROOT . "/dolibarr_oauth.log", octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK)); |
|
70 | - } |
|
64 | + if (function_exists('getDolGlobalString') && getDolGlobalString('OAUTH_DEBUG')) { |
|
65 | + file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", $endpoint->getAbsoluteUri()."\n", FILE_APPEND); |
|
66 | + file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", $requestBody."\n", FILE_APPEND); |
|
67 | + file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", $method."\n", FILE_APPEND); |
|
68 | + file_put_contents(DOL_DATA_ROOT . "/dolibarr_oauth.log", var_export($extraHeaders, true)."\n", FILE_APPEND); |
|
69 | + @chmod(DOL_DATA_ROOT . "/dolibarr_oauth.log", octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK)); |
|
70 | + } |
|
71 | 71 | |
72 | - $level = error_reporting(0); |
|
73 | - $response = file_get_contents($endpoint->getAbsoluteUri(), false, $context); |
|
74 | - error_reporting($level); |
|
75 | - if (false === $response) { |
|
76 | - $lastError = error_get_last(); |
|
77 | - if (is_null($lastError)) { |
|
78 | - throw new TokenResponseException( |
|
79 | - 'Failed to request resource. HTTP Code: ' . |
|
80 | - ((isset($http_response_header[0]))?$http_response_header[0]:'No response') |
|
81 | - ); |
|
82 | - } |
|
83 | - throw new TokenResponseException($lastError['message']); |
|
84 | - } |
|
72 | + $level = error_reporting(0); |
|
73 | + $response = file_get_contents($endpoint->getAbsoluteUri(), false, $context); |
|
74 | + error_reporting($level); |
|
75 | + if (false === $response) { |
|
76 | + $lastError = error_get_last(); |
|
77 | + if (is_null($lastError)) { |
|
78 | + throw new TokenResponseException( |
|
79 | + 'Failed to request resource. HTTP Code: ' . |
|
80 | + ((isset($http_response_header[0]))?$http_response_header[0]:'No response') |
|
81 | + ); |
|
82 | + } |
|
83 | + throw new TokenResponseException($lastError['message']); |
|
84 | + } |
|
85 | 85 | |
86 | - return $response; |
|
87 | - } |
|
86 | + return $response; |
|
87 | + } |
|
88 | 88 | |
89 | - private function generateStreamContext($body, $headers, $method) |
|
90 | - { |
|
91 | - return stream_context_create( |
|
92 | - array( |
|
93 | - 'http' => array( |
|
94 | - 'method' => $method, |
|
95 | - 'header' => implode("\r\n", array_values($headers)), |
|
96 | - 'content' => $body, |
|
97 | - 'protocol_version' => '1.1', |
|
98 | - 'user_agent' => $this->userAgent, |
|
99 | - 'max_redirects' => $this->maxRedirects, |
|
100 | - 'timeout' => $this->timeout |
|
101 | - ), |
|
102 | - ) |
|
103 | - ); |
|
104 | - } |
|
89 | + private function generateStreamContext($body, $headers, $method) |
|
90 | + { |
|
91 | + return stream_context_create( |
|
92 | + array( |
|
93 | + 'http' => array( |
|
94 | + 'method' => $method, |
|
95 | + 'header' => implode("\r\n", array_values($headers)), |
|
96 | + 'content' => $body, |
|
97 | + 'protocol_version' => '1.1', |
|
98 | + 'user_agent' => $this->userAgent, |
|
99 | + 'max_redirects' => $this->maxRedirects, |
|
100 | + 'timeout' => $this->timeout |
|
101 | + ), |
|
102 | + ) |
|
103 | + ); |
|
104 | + } |
|
105 | 105 | } |
@@ -106,10 +106,10 @@ |
||
106 | 106 | } |
107 | 107 | |
108 | 108 | /** |
109 | - * Get the last fetch date. |
|
110 | - * |
|
111 | - * @return int Timestamp of the last successful fetch. |
|
112 | - */ |
|
109 | + * Get the last fetch date. |
|
110 | + * |
|
111 | + * @return int Timestamp of the last successful fetch. |
|
112 | + */ |
|
113 | 113 | public function getLastFetchDate() |
114 | 114 | { |
115 | 115 | return $this->lastFetchDate; |