PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.4
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.4
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / modules / content-generation / class-cartflows-ai-auth.php

class-cartflows-ai-auth.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.4, at modules/content-generation/class-cartflows-ai-auth.php

170 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * AI Authentication.
4 *
5 * @package cartflows
6 */
7
8 // Exit if accessed directly.
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12 /**
13 * AI Authentication.
14 *
15 * @since x.x.x
16 */
17 class Cartflows_Ai_Auth {
18
19 /**
20 * Member Variable
21 *
22 * @var object instance
23 */
24 private static $instance;
25
26 /**
27 * Encryption key.
28 *
29 * @since x.x.x
30 * @var string
31 */
32 public $key;
33
34 /**
35 * Initiator
36 */
37 public static function get_instance() {
38
39 if ( ! isset( self::$instance ) ) {
40 self::$instance = new self();
41 }
42
43 return self::$instance;
44 }
45
46 /**
47 * Constructor
48 */
49 public function __construct() {}
50
51 /**
52 * Get Auth URL.
53 *
54 * @since x.x.x
55 * @return string|WP_Error
56 */
57 public function get_auth_url() {
58 // Generate a random key of 16 characters.
59 $this->key = wp_generate_password( 16, false );
60
61 // Prepare the token data.
62 $token_data = array(
63 'redirect-back' => admin_url( 'admin.php?page=cartflows' ),
64 'key' => $this->key,
65 'site-url' => site_url(),
66 'nonce' => wp_create_nonce( 'cartflows_ai_auth_nonce' ),
67 );
68
69 $encoded_token_data = wp_json_encode( $token_data );
70
71 if ( empty( $encoded_token_data ) ) {
72 return new WP_Error( 'failed_to_encode_token_data', __( 'Failed to encode the token data.', 'cartflows' ) );
73 }
74
75 return CARTFLOWS_SERVER_URL . 'auth/?token=' . base64_encode( $encoded_token_data );
76 }
77
78 /**
79 * Get Auth status.
80 *
81 * @since x.x.x
82 * @return bool
83 */
84 public function get_auth_status() {
85 $auth_status = get_option( 'cartflows_auth', false );
86 return ! empty( $auth_status );
87 }
88
89 /**
90 * Save Auth.
91 *
92 * @since x.x.x
93 * @param string $data Data to save.
94 * @param string $key Key to use for encryption.
95 * @param string $method Encryption method. Default is AES-256-CBC.
96 * @return bool|WP_Error
97 */
98 public function save_auth( $data, $key, $method = 'AES-256-CBC' ) {
99
100 // Decode the data and split IV and encrypted data.
101 $decoded_data = base64_decode( $data ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
102
103 // if the data is not base64 encoded then return false.
104 if ( empty( $decoded_data ) ) {
105 return new WP_Error( 'failed_to_decode', __( 'Failed to decode the access key.', 'cartflows' ) );
106 }
107
108 // split the key and encrypted data.
109 list( $key, $encrypted ) = explode( '::', $decoded_data, 2 );
110
111 // Decrypt the data using the key.
112 $decrypted = openssl_decrypt( $encrypted, $method, $key, 0, $key );
113
114 // if the decryption returns false then send error.
115 if ( empty( $decrypted ) ) {
116 return new WP_Error( 'failed_to_decrypt', __( 'Failed to decrypt the access key.', 'cartflows' ) );
117 }
118
119 // json decode the decrypted data.
120 $decrypted_data_array = json_decode( $decrypted, true );
121
122 if ( ! is_array( $decrypted_data_array ) || empty( $decrypted_data_array ) ) {
123 return new WP_Error( 'failed_to_json_decode', __( 'Failed to json decode the decrypted data.', 'cartflows' ) );
124 }
125
126 // verify the nonce that comes in $encrypted_email_array.
127 if ( ! empty( $decrypted_data_array['nonce'] ) && ! wp_verify_nonce( $decrypted_data_array['nonce'], 'cartflows_ai_auth_nonce' ) ) {
128 return new WP_Error( 'nonce_verification_failed', __( 'Nonce verification failed.', 'cartflows' ) );
129 }
130
131 // check if the user email is present in the decrypted data.
132 if ( empty( $decrypted_data_array['user_email'] ) ) {
133 return new WP_Error( 'no_user_email', __( 'No user email found in the decrypted data.', 'cartflows' ) );
134 }
135
136 // Extract is_subscribed value if present.
137 $is_subscribed = false;
138 if ( isset( $decrypted_data_array['is_subscribed'] ) ) {
139 // Convert string 'true'/'false' to boolean if needed.
140 if ( is_string( $decrypted_data_array['is_subscribed'] ) ) {
141 $is_subscribed = 'true' === $decrypted_data_array['is_subscribed'];
142 } else {
143 $is_subscribed = (bool) $decrypted_data_array['is_subscribed'];
144 }
145
146 // Update the analytics option based on the preference.
147 // Set 'yes' if opted in, empty string if not.
148 $enable_contribution = $is_subscribed ? 'yes' : '';
149 update_option( 'cartflows_usage_optin', $enable_contribution );
150
151 // Remove is_subscribed from the decrypted data.
152 unset( $decrypted_data_array['is_subscribed'] );
153 }
154
155 // remove the nonce from the decrypted data before saving it to the options.
156 unset( $decrypted_data_array['nonce'] );
157
158 // save the user email to the options.
159 update_option( 'cartflows_auth', $decrypted_data_array );
160
161 return true;
162 }
163
164 }
165
166 /**
167 * Kicking this off by calling 'get_instance()' method
168 */
169 Cartflows_Ai_Auth::get_instance();
170