*/ class Wp_Database_Tools_License { /** * The status returned by the EDD api * * @since 1.0.0 * @access protected * @var array $string Content the status license valid|invalid. */ protected $status; /** * The characteristics returned by an endpoint generated in the * main installation that returns the values ​​of an ACF * * @since 1.0.0 * @access protected * @var array $features Contains plugin features. */ protected $features; /** * Collect the plugin prices returned by the EDD API * * @since 1.0.0 * @access protected * @var array $price Content plugin prices. */ protected $price; /** * The boolean if the license is active. * * @since 1.0.0 * @access protected * @var boolean $is_active license active. */ protected $is_active; /** * The unique identifier of this plugin. * * @since 1.0.0 * @access protected * @var string $plugin_name The string used to uniquely identify this plugin. */ protected $plugin_name; /** * The license key that is stored encrypted in the database * * @since 1.0.0 * @access protected * @var string $license_key The string of key license. */ protected $license_key; /** * The url license plugin. * * @since 1.0.0 * @access protected * @var string $license_url The string of key license. */ protected $license_url; public function __construct( $plugin_name ) { $this->plugin_name = $plugin_name; $this->license_url = home_url(); $this->init_license(); } private function init_license() { $this->check_status(); $this->check_price(); $this->check_features(); } /** * Check current license status. * * @since 1.0.0 */ private function check_status() { $this->status = get_option( WPDBT_PREFIX . 'edd_license_status' ); if ( $this->status !== false ) { $license = get_option( WPDBT_PREFIX . 'edd_license_key' ); if ( $license !== false && $license !== '' ) { $this->license_key = $this->decrypt( $license ); } } $this->is_active = ( $this->status == 'valid' ); } /** * Getting prices using the EDD API. * * @since 1.0.0 */ private function check_price() { $TRANSIENT_PRICES_PRO_KEY = WPDBT_PREFIX . 'prices_pro'; if ( get_transient( $TRANSIENT_PRICES_PRO_KEY ) ) { $this->price = get_transient( $TRANSIENT_PRICES_PRO_KEY ); return; } $response = wp_remote_get( WPDBT_EDD_STORE_URL . '/edd-api/v2/products/?product=140' ); if ( is_wp_error( $response ) ) { return; } if ( ! $response['body'] ) { return; } $response = json_decode( $response['body'] ); if ( ! $response ) { return; } if ( ! $response->products[0]->pricing ) { return; } set_transient( $TRANSIENT_PRICES_PRO_KEY, $response->products[0]->pricing, 86400 ); $this->price = $response->products[0]->pricing; } /** * Getting features from a custom enpoint using the WordPress rest API. * * @since 1.0.0 */ private function check_features() { $TRANSIENT_FEATURES_PRO_KEY = WPDBT_PREFIX . 'features_pro'; if ( get_transient( $TRANSIENT_FEATURES_PRO_KEY ) ) { $this->features = get_transient( $TRANSIENT_FEATURES_PRO_KEY ); return; } $response = wp_remote_get( WPDBT_EDD_STORE_URL . '/wp-json/downloads/v1/acf/140' ); if ( is_wp_error( $response ) ) { return; } $response = json_decode( $response['body'] ); if ( ! $response->success ) { return; } set_transient( $TRANSIENT_FEATURES_PRO_KEY, $response->data, 86400 ); $this->features = $response->data; } private function encrypt( $data ) { $key = WPDBT_ENCRYPTION_KEY; $plaintext = $data; $ivlen = openssl_cipher_iv_length( $cipher = WPDBT_ENCRYPTION_METHOD ); $iv = openssl_random_pseudo_bytes( $ivlen ); $ciphertext_raw = openssl_encrypt( $plaintext, $cipher, $key, $options = OPENSSL_RAW_DATA, $iv ); $hmac = hash_hmac( 'sha256', $ciphertext_raw, $key, $as_binary = true ); $ciphertext = base64_encode( $iv . $hmac . $ciphertext_raw ); return $ciphertext; } private function decrypt( $data ) { $key = WPDBT_ENCRYPTION_KEY; $c = base64_decode( $data ); $ivlen = openssl_cipher_iv_length( $cipher = WPDBT_ENCRYPTION_METHOD ); $iv = substr( $c, 0, $ivlen ); $hmac = substr( $c, $ivlen, $sha2len = 32 ); $ciphertext_raw = substr( $c, $ivlen + $sha2len ); $original_plaintext = openssl_decrypt( $ciphertext_raw, $cipher, $key, $options = OPENSSL_RAW_DATA, $iv ); $calcmac = hash_hmac( 'sha256', $ciphertext_raw, $key, $as_binary = true ); if ( hash_equals( $hmac, $calcmac ) ) { return $original_plaintext; } } public function form_action_license() { $retrieved_nonce = $_REQUEST['edd_sample_nonce']; if ( ! wp_verify_nonce( $retrieved_nonce, 'active_plugin' ) ) { if ( ! check_admin_referer( 'edd_sample_nonce', 'edd_sample_nonce' ) ) { return; } $license = sanitize_text_field( $_POST['license'] ); // data to send in our API request $api_params = array( 'edd_action' => 'activate_license', 'license' => $license, 'item_name' => urlencode( WPDBT_EDD_ITEM_NAME ), // the name of our product in EDD 'url' => home_url(), ); $response = wp_remote_post( WPDBT_EDD_STORE_URL, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params, ) ); if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { $message = ( is_wp_error( $response ) && ! empty( $response->get_error_message() ) ) ? $response->get_error_message() : __( 'An error occurred, please try again.' ); } else { $license_data = json_decode( wp_remote_retrieve_body( $response ) ); if ( false === $license_data->success ) { switch ( $license_data->error ) { case 'expired': $message = sprintf( __( 'Your license key expired on %s.', WPDBT_SLUG ), date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) ) ); break; case 'revoked': $message = __( 'Your license key has been disabled', WPDBT_SLUG ); break; case 'missing': $message = __( 'Invalid license', WPDBT_SLUG ); break; case 'invalid': case 'site_inactive': $message = __( 'Your license is not active for this URL', WPDBT_SLUG ); break; case 'item_name_mismatch': $message = sprintf( __( 'This appears to be an invalid license key for %s.' ), WPDBT_SLUG ); break; case 'no_activations_left': $message = __( 'Your license key has reached its activation limit', WPDBT_SLUG ); break; default: $message = __( 'An error occurred, please try again', WPDBT_SLUG ); break; } } if ( ! empty( $message ) ) { $base_url = admin_url( 'admin.php?page=license' ); $redirect = add_query_arg( array( WPDBT_PREFIX . 'sl_activation' => 'false', 'message' => urlencode( $message ), ), $base_url ); wp_redirect( $redirect ); exit(); } // SAVE OPTION $base_url = admin_url( 'admin.php?page=license' ); update_option( WPDBT_PREFIX . 'edd_license_status', $license_data->license ); update_option( WPDBT_PREFIX . 'edd_license_key', $this->encrypt( $license ) ); $redirect = add_query_arg( array( WPDBT_PREFIX . 'sl_activation' => 'true', 'message' => urlencode( 'Licencia activada' ), ), $base_url ); wp_redirect( $redirect ); exit(); } } else { wp_die( __( 'Invalid nonce specified', $this->plugin_name ), __( 'Error', $this->plugin_name ), array( 'response' => 403, 'back_link' => 'admin.php?page=' . $this->plugin_name, ) ); } } /** * The array of current status license. * * @since 1.0.0 * @return array The array of current status license. */ public function get_status() { return $this->status; } /** * The array of features pro license. * * @since 1.0.0 * @return array The array of features pro license. */ public function get_features() { return $this->features; } /** * The string of of current price pro license. * * @since 1.0.0 * @return string The string of current price pro license. */ public function get_price() { return $this->price; } /** * The boolean of current current license. * * @since 1.0.0 * @return boolean The boolean current license status. */ public function get_is_active() { return $this->is_active; } /** * The string of of current price pro license. * * @since 1.0.0 * @return string The string of current price pro license. */ public function get_license_key() { return $this->license_key; } /** * Return the license key encrypt. * * @since 1.0.0 * @return string The license key encrypt. */ public function get_license_key_encrypt() { return get_option( WPDBT_PREFIX . 'edd_license_key' ); } /** * The string of of current price pro license. * * @since 1.0.0 * @return string The string of current price pro license. */ public function get_license_url() { return $this->license_url; } /** * Set the license key value. * * @param string $value license key value. * @since 1.0.0 */ public function set_license_key( $value ) { update_option( WPDBT_PREFIX . 'edd_license_status', $value ); $this->license_key = $value; } }