2019-07-10 15:47:44 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce.com Product Installation.
|
|
|
|
*
|
2020-08-05 20:49:10 +00:00
|
|
|
* @package WooCommerce\WCCom
|
2019-07-15 16:13:16 +00:00
|
|
|
* @since 3.7.0
|
2019-07-10 15:47:44 +00:00
|
|
|
*/
|
|
|
|
|
2019-07-15 15:58:41 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
2019-07-10 15:47:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_WCCOM_Site Class
|
|
|
|
*
|
|
|
|
* Main class for WooCommerce.com connected site.
|
|
|
|
*/
|
|
|
|
class WC_WCCOM_Site {
|
2019-07-15 16:13:16 +00:00
|
|
|
|
2019-11-25 16:32:10 +00:00
|
|
|
const AUTH_ERROR_FILTER_NAME = 'wccom_auth_error';
|
|
|
|
|
2019-07-10 15:47:44 +00:00
|
|
|
/**
|
|
|
|
* Load the WCCOM site class.
|
2019-07-15 08:42:36 +00:00
|
|
|
*
|
|
|
|
* @since 3.7.0
|
2019-07-10 15:47:44 +00:00
|
|
|
*/
|
|
|
|
public static function load() {
|
|
|
|
self::includes();
|
|
|
|
|
|
|
|
add_action( 'woocommerce_wccom_install_products', array( 'WC_WCCOM_Site_Installer', 'install' ) );
|
2019-07-15 14:59:48 +00:00
|
|
|
add_filter( 'determine_current_user', array( __CLASS__, 'authenticate_wccom' ), 14 );
|
2019-07-11 11:19:08 +00:00
|
|
|
add_action( 'woocommerce_rest_api_get_rest_namespaces', array( __CLASS__, 'register_rest_namespace' ) );
|
2019-07-10 15:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include support files.
|
2019-07-15 08:42:36 +00:00
|
|
|
*
|
|
|
|
* @since 3.7.0
|
2019-07-10 15:47:44 +00:00
|
|
|
*/
|
|
|
|
protected static function includes() {
|
2019-07-11 12:58:16 +00:00
|
|
|
require_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper.php';
|
2019-07-15 16:07:34 +00:00
|
|
|
require_once WC_ABSPATH . 'includes/wccom-site/class-wc-wccom-site-installer.php';
|
2019-09-26 02:11:15 +00:00
|
|
|
require_once WC_ABSPATH . 'includes/wccom-site/class-wc-wccom-site-installer-requirements-check.php';
|
2019-07-10 15:47:44 +00:00
|
|
|
}
|
2019-07-11 11:19:08 +00:00
|
|
|
|
2019-07-15 14:59:48 +00:00
|
|
|
/**
|
|
|
|
* Authenticate WooCommerce.com request.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
* @param int|false $user_id User ID.
|
|
|
|
* @return int|false
|
|
|
|
*/
|
|
|
|
public static function authenticate_wccom( $user_id ) {
|
|
|
|
if ( ! empty( $user_id ) || ! self::is_request_to_wccom_site_rest_api() ) {
|
|
|
|
return $user_id;
|
|
|
|
}
|
|
|
|
|
2020-01-30 07:54:40 +00:00
|
|
|
$auth_header = trim( self::get_authorization_header() );
|
2019-07-15 14:59:48 +00:00
|
|
|
|
2020-01-30 07:54:40 +00:00
|
|
|
if ( stripos( $auth_header, 'Bearer ' ) === 0 ) {
|
|
|
|
$access_token = trim( substr( $auth_header, 7 ) );
|
|
|
|
} elseif ( ! empty( $_GET['token'] ) && is_string( $_GET['token'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
|
|
|
$access_token = trim( $_GET['token'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
|
|
|
} else {
|
2019-11-25 16:32:10 +00:00
|
|
|
add_filter(
|
|
|
|
self::AUTH_ERROR_FILTER_NAME,
|
|
|
|
function() {
|
|
|
|
return new WP_Error(
|
2020-01-30 07:54:40 +00:00
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::NO_ACCESS_TOKEN_CODE,
|
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::NO_ACCESS_TOKEN_MESSAGE,
|
|
|
|
array( 'status' => WC_REST_WCCOM_Site_Installer_Errors::NO_ACCESS_TOKEN_HTTP_CODE )
|
2019-11-25 16:32:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2019-07-15 14:59:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-30 07:54:40 +00:00
|
|
|
if ( ! empty( $_SERVER['HTTP_X_WOO_SIGNATURE'] ) ) {
|
|
|
|
$signature = trim( $_SERVER['HTTP_X_WOO_SIGNATURE'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
|
|
|
} elseif ( ! empty( $_GET['signature'] ) && is_string( $_GET['signature'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
|
|
|
$signature = trim( $_GET['signature'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
|
|
|
} else {
|
2019-11-25 16:32:10 +00:00
|
|
|
add_filter(
|
|
|
|
self::AUTH_ERROR_FILTER_NAME,
|
|
|
|
function() {
|
|
|
|
return new WP_Error(
|
2020-01-30 07:54:40 +00:00
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::NO_SIGNATURE_CODE,
|
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::NO_SIGNATURE_MESSAGE,
|
|
|
|
array( 'status' => WC_REST_WCCOM_Site_Installer_Errors::NO_SIGNATURE_HTTP_CODE )
|
2019-11-25 16:32:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2019-07-15 14:59:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-options.php';
|
2020-01-30 07:54:40 +00:00
|
|
|
$site_auth = WC_Helper_Options::get( 'auth' );
|
2019-11-25 16:32:10 +00:00
|
|
|
|
|
|
|
if ( empty( $site_auth['access_token'] ) ) {
|
|
|
|
add_filter(
|
|
|
|
self::AUTH_ERROR_FILTER_NAME,
|
|
|
|
function() {
|
|
|
|
return new WP_Error(
|
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::SITE_NOT_CONNECTED_CODE,
|
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::SITE_NOT_CONNECTED_MESSAGE,
|
|
|
|
array( 'status' => WC_REST_WCCOM_Site_Installer_Errors::SITE_NOT_CONNECTED_HTTP_CODE )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! hash_equals( $access_token, $site_auth['access_token'] ) ) {
|
|
|
|
add_filter(
|
|
|
|
self::AUTH_ERROR_FILTER_NAME,
|
|
|
|
function() {
|
|
|
|
return new WP_Error(
|
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::INVALID_TOKEN_CODE,
|
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::INVALID_TOKEN_MESSAGE,
|
|
|
|
array( 'status' => WC_REST_WCCOM_Site_Installer_Errors::INVALID_TOKEN_HTTP_CODE )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2019-07-15 14:59:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-30 07:54:40 +00:00
|
|
|
$body = WP_REST_Server::get_raw_data();
|
2019-07-15 14:59:48 +00:00
|
|
|
|
|
|
|
if ( ! self::verify_wccom_request( $body, $signature, $site_auth['access_token_secret'] ) ) {
|
2019-11-25 16:32:10 +00:00
|
|
|
add_filter(
|
|
|
|
self::AUTH_ERROR_FILTER_NAME,
|
|
|
|
function() {
|
|
|
|
return new WP_Error(
|
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::REQUEST_VERIFICATION_FAILED_CODE,
|
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::REQUEST_VERIFICATION_FAILED_MESSAGE,
|
|
|
|
array( 'status' => WC_REST_WCCOM_Site_Installer_Errors::REQUEST_VERIFICATION_FAILED_HTTP_CODE )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2019-07-15 14:59:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = get_user_by( 'id', $site_auth['user_id'] );
|
|
|
|
if ( ! $user ) {
|
2019-11-25 16:32:10 +00:00
|
|
|
add_filter(
|
|
|
|
self::AUTH_ERROR_FILTER_NAME,
|
|
|
|
function() {
|
|
|
|
return new WP_Error(
|
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::USER_NOT_FOUND_CODE,
|
|
|
|
WC_REST_WCCOM_Site_Installer_Errors::USER_NOT_FOUND_MESSAGE,
|
|
|
|
array( 'status' => WC_REST_WCCOM_Site_Installer_Errors::USER_NOT_FOUND_HTTP_CODE )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2019-07-15 14:59:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2019-07-15 15:47:07 +00:00
|
|
|
/**
|
|
|
|
* Get the authorization header.
|
|
|
|
*
|
|
|
|
* On certain systems and configurations, the Authorization header will be
|
|
|
|
* stripped out by the server or PHP. Typically this is then used to
|
|
|
|
* generate `PHP_AUTH_USER`/`PHP_AUTH_PASS` but not passed on. We use
|
|
|
|
* `getallheaders` here to try and grab it out instead.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
* @return string Authorization header if set.
|
|
|
|
*/
|
|
|
|
protected static function get_authorization_header() {
|
|
|
|
if ( ! empty( $_SERVER['HTTP_AUTHORIZATION'] ) ) {
|
2019-07-15 16:13:16 +00:00
|
|
|
return wp_unslash( $_SERVER['HTTP_AUTHORIZATION'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
2019-07-15 15:47:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( function_exists( 'getallheaders' ) ) {
|
|
|
|
$headers = getallheaders();
|
|
|
|
// Check for the authoization header case-insensitively.
|
|
|
|
foreach ( $headers as $key => $value ) {
|
|
|
|
if ( 'authorization' === strtolower( $key ) ) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-07-15 14:59:48 +00:00
|
|
|
/**
|
|
|
|
* Check if this is a request to WCCOM Site REST API.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected static function is_request_to_wccom_site_rest_api() {
|
|
|
|
|
2019-12-03 17:04:15 +00:00
|
|
|
if ( isset( $_REQUEST['rest_route'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
2020-01-30 09:56:52 +00:00
|
|
|
$route = wp_unslash( $_REQUEST['rest_route'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Recommended
|
|
|
|
$rest_prefix = '';
|
2019-12-02 19:56:42 +00:00
|
|
|
} else {
|
2019-12-03 17:04:15 +00:00
|
|
|
$route = wp_unslash( add_query_arg( array() ) );
|
2019-12-02 19:56:42 +00:00
|
|
|
$rest_prefix = trailingslashit( rest_get_url_prefix() );
|
|
|
|
}
|
2019-07-15 14:59:48 +00:00
|
|
|
|
2019-12-02 19:56:42 +00:00
|
|
|
return false !== strpos( $route, $rest_prefix . 'wccom-site/' );
|
2019-07-15 14:59:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify WooCommerce.com request from a given body and signature request.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
* @param string $body Request body.
|
2019-07-15 16:13:16 +00:00
|
|
|
* @param string $signature Request signature found in X-Woo-Signature header.
|
2019-07-15 14:59:48 +00:00
|
|
|
* @param string $access_token_secret Access token secret for this site.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected static function verify_wccom_request( $body, $signature, $access_token_secret ) {
|
|
|
|
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
|
|
|
$data = array(
|
|
|
|
'host' => $_SERVER['HTTP_HOST'],
|
2020-02-03 12:47:04 +00:00
|
|
|
'request_uri' => urldecode( remove_query_arg( array( 'token', 'signature' ), $_SERVER['REQUEST_URI'] ) ),
|
2019-07-15 14:59:48 +00:00
|
|
|
'method' => strtoupper( $_SERVER['REQUEST_METHOD'] ),
|
|
|
|
);
|
|
|
|
// phpcs:enable
|
|
|
|
|
|
|
|
if ( ! empty( $body ) ) {
|
|
|
|
$data['body'] = $body;
|
|
|
|
}
|
|
|
|
|
|
|
|
$expected_signature = hash_hmac( 'sha256', wp_json_encode( $data ), $access_token_secret );
|
|
|
|
|
|
|
|
return hash_equals( $expected_signature, $signature );
|
|
|
|
}
|
|
|
|
|
2019-07-11 11:19:08 +00:00
|
|
|
/**
|
|
|
|
* Register wccom-site REST namespace.
|
|
|
|
*
|
2019-07-15 08:42:36 +00:00
|
|
|
* @since 3.7.0
|
2019-07-11 11:19:08 +00:00
|
|
|
* @param array $namespaces List of registered namespaces.
|
|
|
|
* @return array Registered namespaces.
|
|
|
|
*/
|
|
|
|
public static function register_rest_namespace( $namespaces ) {
|
2019-11-25 09:47:18 +00:00
|
|
|
require_once WC_ABSPATH . 'includes/wccom-site/rest-api/class-wc-rest-wccom-site-installer-errors.php';
|
2019-07-15 16:19:03 +00:00
|
|
|
require_once WC_ABSPATH . 'includes/wccom-site/rest-api/endpoints/class-wc-rest-wccom-site-installer-controller.php';
|
2019-07-11 11:19:08 +00:00
|
|
|
|
|
|
|
$namespaces['wccom-site/v1'] = array(
|
2019-07-15 16:19:03 +00:00
|
|
|
'installer' => 'WC_REST_WCCOM_Site_Installer_Controller',
|
2019-07-11 11:19:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $namespaces;
|
|
|
|
}
|
2019-07-10 15:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WC_WCCOM_Site::load();
|