From df04b02014143a06f82ca84cc326a7e61296b776 Mon Sep 17 00:00:00 2001 From: Stefan Warnat Date: Sat, 25 Jul 2015 18:08:48 +0200 Subject: [PATCH 1/3] add HTTP Basic Auth support --- .../class-wc-api-client-http-request.php | 4 ++++ lib/woocommerce-api/class-wc-api-client.php | 22 ++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/woocommerce-api/class-wc-api-client-http-request.php b/lib/woocommerce-api/class-wc-api-client-http-request.php index b9fa7a4..c5bac0c 100644 --- a/lib/woocommerce-api/class-wc-api-client-http-request.php +++ b/lib/woocommerce-api/class-wc-api-client-http-request.php @@ -65,6 +65,10 @@ public function __construct( $args ) { $this->ch = curl_init(); + if($args['options']['httpauth_user'] !== null) { + curl_setopt($this->ch, CURLOPT_USERPWD, $args['options']['httpauth_user'] . ":" . $args['options']['httpauth_pass']); + } + // default cURL opts curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, $ssl_verify ); curl_setopt( $this->ch, CURLOPT_SSL_VERIFYHOST, $ssl_verify ); diff --git a/lib/woocommerce-api/class-wc-api-client.php b/lib/woocommerce-api/class-wc-api-client.php index e27052a..882bd8e 100644 --- a/lib/woocommerce-api/class-wc-api-client.php +++ b/lib/woocommerce-api/class-wc-api-client.php @@ -37,6 +37,12 @@ class WC_API_Client { /** @var bool true to perform SSL peer verification */ public $ssl_verify = true; + /** @var null|string if set, used for HTTP BASIC AUTH */ + public $httpauth_user = null; + + /** @var null|string if set, used for HTTP BASIC AUTH */ + public $httpauth_pass = null; + /** Resources */ /** @var WC_API_Client_Resource_Coupons instance */ @@ -191,6 +197,8 @@ public function parse_options( $options ) { 'validate_url', 'timeout', 'ssl_verify', + 'httpauth_user', + 'httpauth_pass', ); foreach ( (array) $options as $opt_key => $opt_value ) { @@ -218,7 +226,17 @@ public function parse_options( $options ) { */ public function validate_api_url() { - $index = @file_get_contents( $this->api_url ); + if($this->httpauth_user !== null) { + $context = stream_context_create(array( + 'http' => array( + 'header' => "Authorization: Basic " . base64_encode($this->httpauth_user.':'.$this->httpauth_pass) + ) + )); + + $index = @file_get_contents( $this->api_url, false, $context); + } else { + $index = @file_get_contents( $this->api_url); + } // check for HTTP 404 response (file_get_contents() returns false when encountering 404) // this usually means: @@ -278,6 +296,8 @@ public function make_api_call( $method, $path, $request_data ) { 'ssl_verify' => $this->ssl_verify, 'json_decode' => $this->return_as_array ? 'array' : 'object', 'debug' => $this->debug, + 'httpauth_user' => $this->httpauth_user, + 'httpauth_pass' => $this->httpauth_pass, ) ); From 0333025410788fd66031f07656f16e14ad818cd3 Mon Sep 17 00:00:00 2001 From: Stefan Warnat Date: Sat, 25 Jul 2015 18:15:27 +0200 Subject: [PATCH 2/3] change tab format --- lib/woocommerce-api/class-wc-api-client.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/woocommerce-api/class-wc-api-client.php b/lib/woocommerce-api/class-wc-api-client.php index 882bd8e..b9f4cca 100644 --- a/lib/woocommerce-api/class-wc-api-client.php +++ b/lib/woocommerce-api/class-wc-api-client.php @@ -197,8 +197,8 @@ public function parse_options( $options ) { 'validate_url', 'timeout', 'ssl_verify', - 'httpauth_user', - 'httpauth_pass', + 'httpauth_user', + 'httpauth_pass', ); foreach ( (array) $options as $opt_key => $opt_value ) { @@ -226,12 +226,12 @@ public function parse_options( $options ) { */ public function validate_api_url() { - if($this->httpauth_user !== null) { - $context = stream_context_create(array( - 'http' => array( - 'header' => "Authorization: Basic " . base64_encode($this->httpauth_user.':'.$this->httpauth_pass) - ) - )); + if($this->httpauth_user !== null) { + $context = stream_context_create(array( + 'http' => array( + 'header' => "Authorization: Basic " . base64_encode($this->httpauth_user.':'.$this->httpauth_pass) + ) + )); $index = @file_get_contents( $this->api_url, false, $context); } else { @@ -296,8 +296,8 @@ public function make_api_call( $method, $path, $request_data ) { 'ssl_verify' => $this->ssl_verify, 'json_decode' => $this->return_as_array ? 'array' : 'object', 'debug' => $this->debug, - 'httpauth_user' => $this->httpauth_user, - 'httpauth_pass' => $this->httpauth_pass, + 'httpauth_user' => $this->httpauth_user, + 'httpauth_pass' => $this->httpauth_pass, ) ); From 90beafb5360837fea76aec342bc9e1518b642e77 Mon Sep 17 00:00:00 2001 From: Stefan Warnat Date: Sat, 25 Jul 2015 18:17:52 +0200 Subject: [PATCH 3/3] change variable name --- .../class-wc-api-client-http-request.php | 6 +++--- lib/woocommerce-api/class-wc-api-client.php | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/woocommerce-api/class-wc-api-client-http-request.php b/lib/woocommerce-api/class-wc-api-client-http-request.php index c5bac0c..41dc3c8 100644 --- a/lib/woocommerce-api/class-wc-api-client-http-request.php +++ b/lib/woocommerce-api/class-wc-api-client-http-request.php @@ -65,9 +65,9 @@ public function __construct( $args ) { $this->ch = curl_init(); - if($args['options']['httpauth_user'] !== null) { - curl_setopt($this->ch, CURLOPT_USERPWD, $args['options']['httpauth_user'] . ":" . $args['options']['httpauth_pass']); - } + if($args['options']['httpauth_username'] !== null) { + curl_setopt($this->ch, CURLOPT_USERPWD, $args['options']['httpauth_username'] . ":" . $args['options']['httpauth_password']); + } // default cURL opts curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, $ssl_verify ); diff --git a/lib/woocommerce-api/class-wc-api-client.php b/lib/woocommerce-api/class-wc-api-client.php index b9f4cca..5c913e1 100644 --- a/lib/woocommerce-api/class-wc-api-client.php +++ b/lib/woocommerce-api/class-wc-api-client.php @@ -38,10 +38,10 @@ class WC_API_Client { public $ssl_verify = true; /** @var null|string if set, used for HTTP BASIC AUTH */ - public $httpauth_user = null; + public $httpauth_username = null; /** @var null|string if set, used for HTTP BASIC AUTH */ - public $httpauth_pass = null; + public $httpauth_password = null; /** Resources */ @@ -197,8 +197,8 @@ public function parse_options( $options ) { 'validate_url', 'timeout', 'ssl_verify', - 'httpauth_user', - 'httpauth_pass', + 'httpauth_username', + 'httpauth_password', ); foreach ( (array) $options as $opt_key => $opt_value ) { @@ -226,10 +226,10 @@ public function parse_options( $options ) { */ public function validate_api_url() { - if($this->httpauth_user !== null) { + if($this->httpauth_username !== null) { $context = stream_context_create(array( 'http' => array( - 'header' => "Authorization: Basic " . base64_encode($this->httpauth_user.':'.$this->httpauth_pass) + 'header' => "Authorization: Basic " . base64_encode($this->httpauth_username.':'.$this->httpauth_password) ) )); @@ -296,8 +296,8 @@ public function make_api_call( $method, $path, $request_data ) { 'ssl_verify' => $this->ssl_verify, 'json_decode' => $this->return_as_array ? 'array' : 'object', 'debug' => $this->debug, - 'httpauth_user' => $this->httpauth_user, - 'httpauth_pass' => $this->httpauth_pass, + 'httpauth_username' => $this->httpauth_username, + 'httpauth_password' => $this->httpauth_password, ) );