Esempio n. 1
0
		/**
		 * Execute module upgrade
		 *
		 * @return void
		 */
		public function execute_upgrade( $old, $new ) {
			// Upgrade to new provider module system
			if ( $old < 4038 ) {

				global $wpdb;
				$settings = get_site_option( 'itsec_two_factor' );
				// If two-factor wasn't enabled or already has providers for some reason, don't worry about upgrading it
				if ( ! isset( $settings['enabled'] ) || ! $settings['enabled'] || ! empty( $settings['enabled-providers'] ) ) {
					return;
				}
				$settings = array(
					'enabled' => true,
					'enabled-providers' => array(
						'Two_Factor_Totp',
						'Two_Factor_Backup_Codes'
					)
				);
				update_site_option( 'itsec_two_factor', $settings );
				// Instantiate enabled providers so we can handle all the updating
				$helper = ITSEC_Two_Factor_Helper::get_instance();
				$helper->get_enabled_provider_instances( true );

				/**
				 * Migrate all app passes to new system
				 */
				$meta_results = $wpdb->get_results( "SELECT * FROM `{$wpdb->usermeta}` WHERE `meta_key` = 'itsec_two_factor_app_pass'" );

				foreach ( $meta_results as $user_meta ) {
					// New Style Passwords, in case any exist from other compatible plugins
					$passwords = Application_Passwords::get_user_application_passwords( $user_meta->user_id );
					if ( ! $passwords ) {
						$passwords = array();
					}

					$app_passwords = maybe_unserialize( $user_meta->meta_value );
					if ( is_array( $app_passwords ) ) {
						foreach ( $app_passwords as $name => $app_password ) {
							$passwords[]  = array(
								'name'      => $name,
								'password'  => $app_password,
								'created'   => time(),
								'last_used' => null,
								'last_ip'   => null,
							);
						}
					}
					// Store them all
					Application_Passwords::set_user_application_passwords( $user_meta->user_id, $passwords );
					delete_user_meta( $user_meta->user_id, 'itsec_two_factor_app_pass' );

				}

				/**
				 * Enable the TOTP provider for any user that is already using two-factor
				 */
				$meta_results = $wpdb->get_results( "SELECT * FROM `{$wpdb->usermeta}` WHERE `meta_key` = 'itsec_two_factor_enabled'" );
				foreach ( $meta_results as $user_meta ) {
					// Out with the old
					delete_user_meta( $user_meta->user_id, 'itsec_two_factor_enabled' );
					// Enable TOTP
					update_usermeta( $user_meta->user_id, '_two_factor_enabled_providers', array( 'Two_Factor_Totp' ) );
					// Make TOTP default
					update_usermeta( $user_meta->user_id, '_two_factor_provider', 'Two_Factor_Totp' );
				}

				// Change meta key from old 'itsec_two_factor_key' to new '_two_factor_totp_key'
				$wpdb->update( $wpdb->usermeta, array( 'meta_key' => '_two_factor_totp_key' ), array( 'meta_key' => 'itsec_two_factor_key' ) );
			}

		}
	public function __construct() {
		require_once( 'class-itsec-two-factor-helper.php' );
		require_once( 'class-itsec-two-factor-core-compat.php' );
		$this->_helper  = ITSEC_Two_Factor_Helper::get_instance();
	}