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
appConfig.php
149 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Holds the Admin Controller Config class instance. |
| 4 | * |
| 5 | * @package Admin_Controller |
| 6 | */ |
| 7 | |
| 8 | namespace MoEmbedPowerBI\Controller; |
| 9 | |
| 10 | use MoEmbedPowerBI\Wrappers\pluginConstants; |
| 11 | use MoEmbedPowerBI\Wrappers\wpWrapper; |
| 12 | use MoEmbedPowerBI\Wrappers\secureInput; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Class to handle app configurations tab functionalities. |
| 20 | */ |
| 21 | class appConfig { |
| 22 | |
| 23 | /** |
| 24 | * Holds the App Config class instance. |
| 25 | * |
| 26 | * @var App_Config |
| 27 | */ |
| 28 | private static $instance; |
| 29 | |
| 30 | /** |
| 31 | * Object instance(App Config Controller) getter method. |
| 32 | * |
| 33 | * @return App_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 save the configurations of app config tab and perform other actions. |
| 45 | * |
| 46 | * @param string $option Stores the option value from the form submitted. |
| 47 | * @return void |
| 48 | */ |
| 49 | public function mo_epbr_save_settings( $option ) { |
| 50 | switch ( $option ) { |
| 51 | case 'mo_epbr_client_config_option': |
| 52 | $this->mo_epbr_save_client_config(); |
| 53 | break; |
| 54 | |
| 55 | case 'mo_epbr_add_sso_button_wp_login': |
| 56 | $this->mo_epbr_add_sso_button(); |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Function to check whether the array is empty or null |
| 63 | * |
| 64 | * @param pointer $input Input pointer to the array. |
| 65 | * @param array $arr Array containing input values. |
| 66 | * @return pointer |
| 67 | */ |
| 68 | private function mo_epbr_check_for_empty_or_null( &$input, $arr ) { |
| 69 | // Get secure form data |
| 70 | $form_fields = array(); |
| 71 | foreach ( $arr as $key ) { |
| 72 | $form_fields[ $key ] = 'text'; |
| 73 | } |
| 74 | |
| 75 | $form_data = secureInput::mo_epbr_get_secure_data( |
| 76 | 'mo_epbr_client_config_option', |
| 77 | $form_fields, |
| 78 | '_wpnonce' |
| 79 | ); |
| 80 | |
| 81 | if ( empty( $form_data ) ) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | foreach ( $arr as $key ) { |
| 86 | if ( ! isset( $form_data[ $key ] ) || empty( $form_data[ $key ] ) ) { |
| 87 | return false; |
| 88 | } |
| 89 | $input[ $key ] = $form_data[ $key ]; |
| 90 | } |
| 91 | return $input; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Function to save the client configuration. |
| 96 | * |
| 97 | * @return void |
| 98 | */ |
| 99 | private function mo_epbr_save_client_config() { |
| 100 | // Nonce verification is handled by mo_epbr_check_for_empty_or_null() |
| 101 | $input_arr = array( 'client_id', 'client_secret', 'redirect_uri', 'tenant_id' ); |
| 102 | $sanitized_arr = array(); |
| 103 | if ( ! $this->mo_epbr_check_for_empty_or_null( $sanitized_arr, $input_arr ) ) { |
| 104 | wpWrapper::mo_epbr__show_error_notice( esc_html__( 'Input is empty or present in the incorrect format.', 'embed-power-bi-reports' ) ); |
| 105 | return; |
| 106 | } |
| 107 | // Get UPN ID from secure form data |
| 108 | $upn_data = secureInput::mo_epbr_get_secure_data( |
| 109 | 'mo_epbr_client_config_option', |
| 110 | array( 'upn_id' => 'text' ), |
| 111 | '_wpnonce' |
| 112 | ); |
| 113 | $sanitized_arr['upn_id'] = $upn_data['upn_id'] ?? ''; |
| 114 | $sanitized_arr['client_secret'] = wpWrapper::mo_epbr_encrypt_data( $sanitized_arr['client_secret'], hash( 'sha256', $sanitized_arr['client_id'] ) ); |
| 115 | wpWrapper::mo_epbr_set_option( pluginConstants::APPLICATION_CONFIG_OPTION, $sanitized_arr ); |
| 116 | wpWrapper::mo_epbr__show_success_notice( esc_html__( 'Settings Saved Successfully.', 'embed-power-bi-reports' ) ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Function to add the SSO login button via azure ad on WordPress login page. |
| 121 | * |
| 122 | * @return void |
| 123 | */ |
| 124 | private function mo_epbr_add_sso_button() { |
| 125 | $app_config = wpWrapper::mo_epbr_get_option( pluginConstants::APPLICATION_CONFIG_OPTION ); |
| 126 | $app_id = ''; |
| 127 | if ( ! empty( $app_config ) && isset( $app_config['client_id'] ) ) { |
| 128 | $app_id = $app_config['client_id']; |
| 129 | } |
| 130 | if ( $app_id ) { |
| 131 | $sso_data = secureInput::mo_epbr_get_secure_data( |
| 132 | 'mo_epbr_add_sso_button_wp_login', |
| 133 | array( |
| 134 | 'option' => 'text', |
| 135 | 'mo_epbr_add_sso_button_wp' => 'checkbox', |
| 136 | ), |
| 137 | '_wpnonce' |
| 138 | ); |
| 139 | |
| 140 | if ( ! empty( $sso_data ) && 'mo_epbr_add_sso_button_wp_login' === $sso_data['option'] ) { |
| 141 | wpWrapper::mo_epbr_set_option( 'mo_epbr_add_sso_button_wp', $sso_data['mo_epbr_add_sso_button_wp'] ?? false ); |
| 142 | wpWrapper::mo_epbr__show_success_notice( esc_html__( 'Settings Updated Successfully.', 'embed-power-bi-reports' ) ); |
| 143 | } |
| 144 | } else { |
| 145 | wpWrapper::mo_epbr__show_error_notice( 'Kindly configure the application to use the login functionality.' ); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 |