Completed
Branch develop (2dd6cc)
by
unknown
26:03
created
htdocs/includes/OAuth/Common/Http/Client/CurlClient.php 1 patch
Indentation   +138 added lines, -138 removed lines patch added patch discarded remove patch
@@ -10,142 +10,142 @@
 block discarded – undo
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
-        $extraHeaders = $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
+		$extraHeaders = $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
 }
Please login to merge, or discard this patch.
htdocs/includes/OAuth/Common/Http/Client/StreamClient.php 1 patch
Indentation   +80 added lines, -80 removed lines patch added patch discarded remove patch
@@ -10,96 +10,96 @@
 block discarded – undo
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
-        $extraHeaders = $this->normalizeHeaders($extraHeaders);
36
+		$extraHeaders = $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
 }
Please login to merge, or discard this patch.
htdocs/includes/OAuth/Common/Http/Client/AbstractClient.php 1 patch
Indentation   +53 added lines, -53 removed lines patch added patch discarded remove patch
@@ -7,66 +7,66 @@
 block discarded – undo
7 7
  */
8 8
 abstract class AbstractClient implements ClientInterface
9 9
 {
10
-    /**
11
-     * @var string The user agent string passed to services
12
-     */
13
-    protected $userAgent;
10
+	/**
11
+	 * @var string The user agent string passed to services
12
+	 */
13
+	protected $userAgent;
14 14
 
15
-    /**
16
-     * @var int The maximum number of redirects
17
-     */
18
-    protected $maxRedirects = 5;
15
+	/**
16
+	 * @var int The maximum number of redirects
17
+	 */
18
+	protected $maxRedirects = 5;
19 19
 
20
-    /**
21
-     * @var int The maximum timeout
22
-     */
23
-    protected $timeout = 15;
20
+	/**
21
+	 * @var int The maximum timeout
22
+	 */
23
+	protected $timeout = 15;
24 24
 
25
-    /**
26
-     * Creates instance
27
-     *
28
-     * @param string $userAgent The UA string the client will use
29
-     */
30
-    public function __construct($userAgent = 'PHPoAuthLib')
31
-    {
32
-        $this->userAgent = $userAgent;
33
-    }
25
+	/**
26
+	 * Creates instance
27
+	 *
28
+	 * @param string $userAgent The UA string the client will use
29
+	 */
30
+	public function __construct($userAgent = 'PHPoAuthLib')
31
+	{
32
+		$this->userAgent = $userAgent;
33
+	}
34 34
 
35
-    /**
36
-     * @param int $redirects Maximum redirects for client
37
-     *
38
-     * @return ClientInterface
39
-     */
40
-    public function setMaxRedirects($redirects)
41
-    {
42
-        $this->maxRedirects = $redirects;
35
+	/**
36
+	 * @param int $redirects Maximum redirects for client
37
+	 *
38
+	 * @return ClientInterface
39
+	 */
40
+	public function setMaxRedirects($redirects)
41
+	{
42
+		$this->maxRedirects = $redirects;
43 43
 
44
-        return $this;
45
-    }
44
+		return $this;
45
+	}
46 46
 
47
-    /**
48
-     * @param int $timeout Request timeout time for client in seconds
49
-     *
50
-     * @return ClientInterface
51
-     */
52
-    public function setTimeout($timeout)
53
-    {
54
-        $this->timeout = $timeout;
47
+	/**
48
+	 * @param int $timeout Request timeout time for client in seconds
49
+	 *
50
+	 * @return ClientInterface
51
+	 */
52
+	public function setTimeout($timeout)
53
+	{
54
+		$this->timeout = $timeout;
55 55
 
56
-        return $this;
57
-    }
56
+		return $this;
57
+	}
58 58
 
59
-    /**
60
-     * @param array $headers
61
-     */
62
-    public function normalizeHeaders($headers)
63
-    {
64
-        $normalizeHeaders = [];
65
-        foreach ($headers as $key => $val) {
66
-            $val = ucfirst(strtolower($key)) . ': ' . $val;
67
-            $normalizeHeaders[$key] = $val;
68
-        }
59
+	/**
60
+	 * @param array $headers
61
+	 */
62
+	public function normalizeHeaders($headers)
63
+	{
64
+		$normalizeHeaders = [];
65
+		foreach ($headers as $key => $val) {
66
+			$val = ucfirst(strtolower($key)) . ': ' . $val;
67
+			$normalizeHeaders[$key] = $val;
68
+		}
69 69
 
70
-        return $normalizeHeaders;
71
-    }
70
+		return $normalizeHeaders;
71
+	}
72 72
 }
Please login to merge, or discard this patch.