accountSetupConfig.php
1 month ago
adminController.php
9 months ago
appConfig.php
6 months ago
powerBIConfig.php
6 months ago
powerBIsettingsConfig.php
9 months ago
accountSetupConfig.php
338 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles Account Setup Configuration. |
| 4 | * |
| 5 | * @package embed-power-bi-reports\Controller |
| 6 | */ |
| 7 | |
| 8 | namespace MoEmbedPowerBI\Controller; |
| 9 | |
| 10 | use MoEmbedPowerBI\API\CustomerEPBR; |
| 11 | use MoEmbedPowerBI\Wrappers\wpWrapper; |
| 12 | use MoEmbedPowerBI\Wrappers\secureInput; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Class to handle account setup Configurations. |
| 20 | */ |
| 21 | class accountSetupConfig { |
| 22 | |
| 23 | /** |
| 24 | * Holds the Account Setup Config class instance. |
| 25 | * |
| 26 | * @var Account_Setup_Config |
| 27 | */ |
| 28 | private static $instance; |
| 29 | |
| 30 | /** |
| 31 | * Object instance(Account Setup Config) getter method. |
| 32 | * |
| 33 | * @return Account_Setup_Config |
| 34 | */ |
| 35 | public static function get_controller() { |
| 36 | if ( ! isset( self::$instance ) ) { |
| 37 | $class = __CLASS__; |
| 38 | self::$instance = new $class(); |
| 39 | } |
| 40 | return self::$instance; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Function to execute form actions based on the option value recieved in post request. |
| 45 | * |
| 46 | * @param array $option This contains option value for admin controller to carry out respective actions. |
| 47 | * @return void |
| 48 | */ |
| 49 | public function mo_epbr_save_settings( $option ) { |
| 50 | |
| 51 | if ( ! isset( $option ) ) { |
| 52 | return; |
| 53 | } |
| 54 | switch ( $option ) { |
| 55 | |
| 56 | case 'mo_api_account_registration_setup_option': |
| 57 | $this->mo_epbr_account_registration_setup(); |
| 58 | break; |
| 59 | |
| 60 | case 'mo_api_remove_account_option': |
| 61 | $this->mo_epbr_remove_account(); |
| 62 | break; |
| 63 | |
| 64 | case 'mo_api_account_login_setup_option': |
| 65 | $this->mo_epbr_account_login_setup(); |
| 66 | break; |
| 67 | |
| 68 | case 'mo_api_is_login': |
| 69 | $this->mo_epbr_is_login(); |
| 70 | break; |
| 71 | |
| 72 | case 'mo_api_is_regis': |
| 73 | $this->mo_epbr_is_regis(); |
| 74 | break; |
| 75 | |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Function to execute the account registration setup. |
| 81 | * |
| 82 | * @return void |
| 83 | */ |
| 84 | private function mo_epbr_account_registration_setup() { |
| 85 | |
| 86 | if ( ! $this->mo_epbr_is_extension_installed( 'curl' ) ) { |
| 87 | wpWrapper::mo_epbr__show_success_notice( 'ERROR: PHP cURL extension is not installed or disabled. Login failed.' ); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | // Get secure user data using centralized function |
| 92 | $user_data = secureInput::mo_epbr_get_secure_data( |
| 93 | 'mo_api_account_registration_setup_option', |
| 94 | array( |
| 95 | 'account_email' => 'email', |
| 96 | 'account_pwd' => 'password', |
| 97 | 'confirm_account_pwd' => 'password', |
| 98 | ), |
| 99 | '_wpnonce' |
| 100 | ); |
| 101 | |
| 102 | // Check if we got valid data (includes nonce and capability verification) |
| 103 | if ( empty( $user_data ) ) { |
| 104 | wpWrapper::mo_epbr__show_error_notice( 'Security validation failed.' ); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | // Check required fields |
| 109 | if ( empty( $user_data['account_email'] ) || empty( $user_data['account_pwd'] ) || empty( $user_data['confirm_account_pwd'] ) ) { |
| 110 | wpWrapper::mo_epbr__show_error_notice( 'All the fields are required. Please enter valid entries.' ); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | // Validate password patterns |
| 115 | if ( wpWrapper::mo_epbr_check_password_pattern( $user_data['account_pwd'] ) || |
| 116 | wpWrapper::mo_epbr_check_password_pattern( $user_data['confirm_account_pwd'] ) ) { |
| 117 | wpWrapper::mo_epbr__show_error_notice( 'Minimum 6 characters should be present. Maximum 15 characters should be present. Only following symbols (!@#.$%^&*-_) should be present.' ); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | // Extract sanitized data |
| 122 | $email = $user_data['account_email']; |
| 123 | $password = $user_data['account_pwd']; |
| 124 | $confirm_password = $user_data['confirm_account_pwd']; |
| 125 | wpWrapper::mo_epbr_set_option( 'mo_epbr_admin_email', $email ); |
| 126 | |
| 127 | if ( strcmp( $password, $confirm_password ) === 0 ) { |
| 128 | wpWrapper::mo_epbr_set_option( 'mo_epbr_admin_password', $password ); |
| 129 | $customer = new CustomerEPBR(); |
| 130 | $customer_exist = json_decode( $customer->mo_epbr_check_customer(), true ); |
| 131 | |
| 132 | if ( ! is_null( $customer_exist ) ) { |
| 133 | if ( strcasecmp( $customer_exist['status'], 'CUSTOMER_NOT_FOUND' ) === 0 ) { |
| 134 | $response = $this->create_mo_customer(); |
| 135 | if ( is_array( $response ) && array_key_exists( 'status', $response ) && 'success' === $response['status'] ) { |
| 136 | wpWrapper::mo_epbr__show_success_notice( 'Successfully Registered' ); |
| 137 | } else { |
| 138 | wpWrapper::mo_epbr__show_error_notice( 'Something went wrong. Please try again after some time.' ); |
| 139 | } |
| 140 | } else { |
| 141 | $response = $this->get_mo_current_customer(); |
| 142 | if ( is_array( $response ) && array_key_exists( 'status', $response ) && 'success' === $response['status'] ) { |
| 143 | wpWrapper::mo_epbr__show_success_notice( 'Successfully Logged In' ); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } else { |
| 148 | wpWrapper::mo_epbr__show_error_notice( "Password Doesn't match" ); |
| 149 | } |
| 150 | } |
| 151 | /** |
| 152 | * Method to perform account login under account setup tab. |
| 153 | * |
| 154 | * @return void |
| 155 | */ |
| 156 | private function mo_epbr_account_login_setup() { |
| 157 | |
| 158 | if ( ! $this->mo_epbr_is_extension_installed( 'curl' ) ) { |
| 159 | wpWrapper::mo_epbr__show_error_notice( 'ERROR: PHP cURL extension is not installed or disabled. Login failed.' ); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | // Get secure user data using centralized function |
| 164 | $user_data = secureInput::mo_epbr_get_secure_data( |
| 165 | 'mo_api_account_login_setup_option', |
| 166 | array( |
| 167 | 'account_email' => 'email', |
| 168 | 'account_pwd' => 'password', |
| 169 | ), |
| 170 | '_wpnonce' |
| 171 | ); |
| 172 | |
| 173 | // Check if we got valid data (includes nonce and capability verification) |
| 174 | if ( empty( $user_data ) ) { |
| 175 | wpWrapper::mo_epbr__show_error_notice( 'Security validation failed.' ); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | // Check required fields |
| 180 | if ( empty( $user_data['account_email'] ) || empty( $user_data['account_pwd'] ) ) { |
| 181 | wpWrapper::mo_epbr__show_error_notice( 'All the fields are required. Please enter valid entries.' ); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | // Validate password pattern |
| 186 | if ( wpWrapper::mo_epbr_check_password_pattern( $user_data['account_pwd'] ) ) { |
| 187 | wpWrapper::mo_epbr__show_error_notice( 'Minimum 6 characters should be present. Maximum 15 characters should be present. Only following symbols (!@#.$%^&*-_) should be present.' ); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | // Extract sanitized data |
| 192 | $email = $user_data['account_email']; |
| 193 | $password = $user_data['account_pwd']; |
| 194 | wpWrapper::mo_epbr_set_option( 'mo_epbr_admin_email', $email ); |
| 195 | wpWrapper::mo_epbr_set_option( 'mo_epbr_admin_password', $password ); |
| 196 | |
| 197 | $customer = new CustomerEPBR(); |
| 198 | $content = $customer->mo_epbr_get_customer_key(); |
| 199 | |
| 200 | if ( ! $content ) { |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | $customer_key = json_decode( $content, true ); |
| 205 | |
| 206 | if ( json_last_error() === JSON_ERROR_NONE ) { |
| 207 | wpWrapper::mo_epbr_set_option( 'mo_epbr_admin_customer_key', $customer_key['id'] ); |
| 208 | wpWrapper::mo_epbr_set_option( 'mo_epbr_admin_api_key', $customer_key['apiKey'] ); |
| 209 | wpWrapper::mo_epbr_set_option( 'mo_epbr_customer_token', $customer_key['token'] ); |
| 210 | if ( ! empty( $customer_key['phone'] ) ) { |
| 211 | wpWrapper::mo_epbr_set_option( 'mo_epbr_admin_phone', $customer_key['phone'] ); |
| 212 | } |
| 213 | |
| 214 | wpWrapper::mo_epbr__show_success_notice( 'Successfully Logged In' ); |
| 215 | |
| 216 | } else { |
| 217 | wpWrapper::mo_epbr__show_error_notice( 'Invalid username or password. Please try again.' ); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Function to remove account. |
| 223 | * |
| 224 | * @return void |
| 225 | */ |
| 226 | private function mo_epbr_remove_account() { |
| 227 | if ( ! current_user_can( 'manage_options' ) ) { |
| 228 | wp_die( __( 'You do not have permission to perform this action.' ) ); |
| 229 | } |
| 230 | check_admin_referer( 'mo_api_remove_account_option' ); |
| 231 | wpWrapper::mo_epbr_deactivate(); |
| 232 | delete_option( 'mo_epbr_registration_status' ); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Function to check whether the extension is installed or nor. |
| 237 | * |
| 238 | * @param string $extension_name Holds the name of extension. |
| 239 | * @return boolean |
| 240 | */ |
| 241 | private function mo_epbr_is_extension_installed( $extension_name ) { |
| 242 | if ( in_array( $extension_name, get_loaded_extensions(), true ) ) { |
| 243 | return 1; |
| 244 | } else { |
| 245 | return 0; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Function to create new customer. |
| 251 | * |
| 252 | * @return array |
| 253 | */ |
| 254 | public function create_mo_customer() { |
| 255 | $customer = new CustomerEPBR(); |
| 256 | $customer_key = json_decode( $customer->mo_epbr_create_customer(), true ); |
| 257 | if ( ! is_null( $customer_key ) ) { |
| 258 | $response = array(); |
| 259 | if ( strcasecmp( $customer_key['status'], 'CUSTOMER_USERNAME_ALREADY_EXISTS' ) === 0 ) { |
| 260 | $api_response = $this->get_mo_current_customer(); |
| 261 | if ( $api_response ) { |
| 262 | $response['status'] = 'success'; |
| 263 | } else { |
| 264 | $response['status'] = 'error'; |
| 265 | } |
| 266 | } elseif ( strcasecmp( $customer_key['status'], 'SUCCESS' ) === 0 ) { |
| 267 | wpWrapper::mo_epbr_set_option( 'mo_epbr_admin_customer_key', $customer_key['id'] ); |
| 268 | wpWrapper::mo_epbr_set_option( 'mo_epbr_admin_api_key', $customer_key['apiKey'] ); |
| 269 | wpWrapper::mo_epbr_set_option( 'mo_epbr_customer_token', $customer_key['token'] ); |
| 270 | delete_option( 'mo_epbr_verify_customer' ); |
| 271 | $response['status'] = 'success'; |
| 272 | return $response; |
| 273 | } |
| 274 | return $response; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Function to get the current miniorange user. |
| 280 | * |
| 281 | * @return array |
| 282 | */ |
| 283 | public function get_mo_current_customer() { |
| 284 | $customer = new CustomerEPBR(); |
| 285 | $content = $customer->mo_epbr_get_customer_key(); |
| 286 | |
| 287 | if ( ! is_null( $content ) ) { |
| 288 | $customer_key = json_decode( $content, true ); |
| 289 | $response = array(); |
| 290 | if ( json_last_error() === JSON_ERROR_NONE ) { |
| 291 | wpWrapper::mo_epbr_set_option( 'mo_epbr_admin_customer_key', $customer_key['id'] ); |
| 292 | wpWrapper::mo_epbr_set_option( 'mo_epbr_admin_api_key', $customer_key['apiKey'] ); |
| 293 | wpWrapper::mo_epbr_set_option( 'mo_epbr_customer_token', $customer_key['token'] ); |
| 294 | delete_option( 'mo_epbr_verify_customer' ); |
| 295 | $response['status'] = 'success'; |
| 296 | return $response; |
| 297 | } else { |
| 298 | wpWrapper::mo_epbr__show_error_notice( 'You already have an account with miniOrange. Please enter a valid password.' ); |
| 299 | $response['status'] = 'error'; |
| 300 | return $response; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Fucntion to check whether the customer is successfully logged in. |
| 307 | * |
| 308 | * @return void |
| 309 | */ |
| 310 | private function mo_epbr_is_login() { |
| 311 | // Get secure login data with nonce verification |
| 312 | $login_data = secureInput::mo_epbr_get_secure_data( 'mo_api_is_login', array() ); |
| 313 | |
| 314 | // If nonce verification fails, deny access |
| 315 | if ( empty( $login_data ) || ! isset( $login_data['_verified'] ) ) { |
| 316 | wp_die( 'Security check failed' ); |
| 317 | } |
| 318 | |
| 319 | wpWrapper::mo_epbr_set_option( 'mo_epbr_registration_status', 'Login User' ); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Function to check whether the customer was successfully registered or not. |
| 324 | * |
| 325 | * @return void |
| 326 | */ |
| 327 | private function mo_epbr_is_regis() { |
| 328 | // Get secure registration data |
| 329 | $regis_data = secureInput::mo_epbr_get_secure_data( 'mo_api_is_regis', array() ); |
| 330 | |
| 331 | // If nonce verification fails, deny access |
| 332 | if ( empty( $regis_data ) || ! isset( $regis_data['_verified'] ) ) { |
| 333 | wp_die( 'Security check failed' ); |
| 334 | } |
| 335 | wpWrapper::mo_epbr_set_option( 'mo_epbr_registration_status', '' ); |
| 336 | } |
| 337 | } |
| 338 |