function _do_import_users()
 {
     //test
     global $wpdb;
     // extract
     extract($_POST);
     // import f*g
     define('MGM_DOING_USERS_IMPORT', TRUE);
     // parese
     $i_response = $this->_parse_import_file();
     // log
     // mgm_log($response, __FUNCTION__);
     // stop
     // $response['status'] = 'error';
     // process data:
     if ($i_response['status'] == 'success' && in_array('user_email', $i_response['header'])) {
         // success
         // extract
         $i_users = $i_response['users'];
         $i_header = $i_response['header'];
         // object
         $mgm_packs = mgm_get_class('mgm_subscription_packs');
         $mgm_roles = mgm_get_class('mgm_roles');
         // types
         $membership_types = mgm_get_class('membership_types')->get_membership_types();
         $memtypes = mgm_get_class('membership_types');
         // check users
         if (!empty($i_users)) {
             // users found
             // init
             $row_count = count($i_users);
             $col_count = count($i_header);
             // limits
             $row_limit = 2000;
             $user_count = 0;
             // log
             // mgm_log(sprintf('Rows: [%d] Cols: [%d]',$row_count, $col_count), __FUNCTION__);
             // user fields
             $user_fields = array('first_name', 'last_name', 'user_nicename', 'user_url', 'display_name', 'nickname', 'user_firstname', 'user_lastname', 'user_description', 'user_registered');
             // flag
             $update_count = $user_count = 0;
             // new users
             $new_users = array();
             $specialchars = array(',', '\'', '"', "\n\r", "\n", '\\', '/', '$', '`', '(', ')', ' ', " ");
             // custom fields
             $cf_register_page = mgm_get_class('member_custom_fields')->get_fields_where(array('display' => array('on_register' => true, 'on_profile' => true)));
             // count
             $cf_count = count($cf_register_page);
             // exclude
             $cf_exclude_names = array('subscription_introduction', 'subscription_options', 'terms_conditions', 'privacy_policy', 'description', 'payment_gateways', 'password_conf', 'autoresponder');
             // types
             $cf_exclude_types = array('html', 'label', 'captcha');
             // loop
             foreach ($i_users as $i_user) {
                 // init
                 $update_user = $insert_user = $is_membership_update = $is_multiple_membership_update = $multiple_membership_exists = $update_role = false;
                 // pack
                 $pack = array();
                 // increment
                 $user_count++;
                 // remove N/A ?
                 // $i_user = str_ireplace('N/A', '', $i_user);
                 // init
                 // id
                 $id = isset($i_user['ID']) && is_numeric($i_user['ID']) ? $i_user['ID'] : '';
                 $email = str_replace($specialchars, '', sanitize_email($i_user['user_email']));
                 $user_login = str_replace($specialchars, '', sanitize_user($i_user['user_login']));
                 $user_password = isset($i_user['user_password']) ? $i_user['user_password'] : '';
                 // Issue #1559: Standardize membership type name to use machine name
                 $membership_type = $memtypes->get_type_code($i_user['membership_type']);
                 $pack_id = isset($i_user['pack_id']) ? sanitize_user($i_user['pack_id']) : '';
                 // log
                 // mgm_log(sprintf('step 1: user_count: [%d] id: [%s] email: [%s] user_login: [%s] user_password: [%s]', $user_count, $id, $email, $user_login, $user_password), __FUNCTION__);
                 // user id not valid
                 if (!is_numeric($id)) {
                     // check login/email
                     if (!empty($user_login) && !empty($email)) {
                         // user
                         $_user = get_user_by('login', $user_login);
                         // if update and different email
                         if (isset($_user->ID) && $_user->user_email != $email) {
                             // log
                             // mgm_log(sprintf('step 2: %s %s', $_user->user_email, $email), __FUNCTION__);
                             // continue
                             continue;
                         }
                         // fresh insert/registration:
                         if (!$_user) {
                             // password
                             $user_password = !empty($user_password) ? $user_password : wp_generate_password();
                             $user_password = str_replace($specialchars, '', $user_password);
                             // trim
                             $user_password = trim($user_password);
                             // log
                             // mgm_log('step 2.1 user_password: '******'step 3: %s, %s ', $user_password, $id), __FUNCTION__);
                             // check error
                             if (is_wp_error($id)) {
                                 // log
                                 // mgm_log(sprintf('step 4: %s', print_r($id->get_error_messages(),1)), __FUNCTION__);
                                 // unset
                                 unset($id);
                                 // continue
                                 continue;
                             }
                             // set new user
                             $new_users[$id]['email'] = $email;
                             $new_users[$id]['user_login'] = $user_login;
                             // update option
                             update_user_option($id, 'default_password_nag', true, true);
                             // flag
                             $insert_user = true;
                         } else {
                             // set id to user
                             $id = $_user->ID;
                         }
                         // unset
                         unset($_user);
                     } else {
                         // login/email not present
                         // log
                         // mgm_log(sprintf('step 5: %s, %s ', $user_login, $email), __FUNCTION__);
                         // continue
                         continue;
                         //skip the record
                     }
                 } else {
                     // update
                     $update_user = true;
                 }
                 // get User object:
                 $user = new WP_user($id);
                 // log
                 // mgm_log(sprintf('step 6: %s ', print_r($user,1)), __FUNCTION__);
                 //issue #700
                 $format = mgm_get_date_format('date_format_short');
                 // ------------------------------------------
                 // user to mgm member
                 if (isset($user->ID) && $user->ID > 0) {
                     // get mgm object:
                     $member = mgm_get_member($user->ID);
                     // update custom fields:
                     if (!empty($member)) {
                         // update pack id if not supplied and already exists
                         // This is to make pack_id optional: issue#: 807
                         if (!is_numeric($pack_id) && isset($member->pack_id) && $member->pack_id > 0) {
                             $pack_id = $member->pack_id;
                         }
                         // update misc fields:
                         if (!isset($member->rss_token) || isset($member->rss_token) && empty($member->rss_token)) {
                             $member->rss_token = mgm_create_rss_token();
                         }
                         // init
                         $user_password_enc = mgm_encrypt_password($user_password, $user->ID, $member->rss_token);
                         // mgm_log($user_password_enc, __FUNCTION__);
                         // check
                         if ($cf_count > 0) {
                             // loop custom fields
                             foreach ($cf_register_page as $field) {
                                 // key
                                 $key = $field['name'];
                                 // mgm_log($key, __FUNCTION__);
                                 // skip unwanted fields
                                 if (in_array($field['name'], $cf_exclude_names) || in_array($field['type'], $cf_exclude_types)) {
                                     // log
                                     // mgm_log(sprintf('step 7: %s ', $field['name']), __FUNCTION__);
                                     // continue
                                     continue;
                                 }
                                 // init
                                 $val = '';
                                 //issue #700
                                 // check
                                 if (isset($i_user[$key]) && !empty($i_user[$key]) && preg_match('/date/i', $key)) {
                                     // validate date
                                     if (mgm_is_valid_date($i_user[$key]) && mgm_format_inputdate_to_mysql($i_user[$key], $format)) {
                                         $val = $i_user[$key];
                                     }
                                 } elseif ($key == 'email') {
                                     // email and username custom fields
                                     $val = $email;
                                 } elseif ($key == 'username') {
                                     // username
                                     $val = $user_login;
                                 } elseif ($key == 'password') {
                                     // password
                                     if (!empty($user_password_enc)) {
                                         // set
                                         $val = $user_password_enc;
                                         // log
                                         // mgm_log(sprintf('step 7.1: %s ', $user_password_enc), __FUNCTION__);
                                     }
                                 } else {
                                     $val = isset($i_user[$key]) ? $i_user[$key] : '';
                                 }
                                 // If checkbox, then serialize the value: Issue #1070
                                 if ($field['type'] == 'checkbox' && !empty($val)) {
                                     $val = serialize(explode("|", $val));
                                 }
                                 // update fields:
                                 if (!empty($val) || !isset($member->custom_fields->{$key})) {
                                     // set
                                     $member->custom_fields->{$key} = $val;
                                 }
                                 // unset
                                 unset($field, $val);
                             }
                         }
                         // custom fields updated
                         // log
                         // mgm_log(sprintf('step 8: %s ', print_r($member,1)), __FUNCTION__);
                         // update membership: main mgm_member object
                         if (!empty($membership_type) && is_numeric($pack_id)) {
                             // pack
                             if ($pack = $mgm_packs->get_pack($pack_id)) {
                                 // valid pack
                                 $member->pack_id = $pack_id;
                             } else {
                                 // log
                                 // mgm_log(sprintf('step 9: %s ', print_r($pack,1)), __FUNCTION__);
                                 // error:
                                 continue;
                             }
                             // membership types:
                             $sel_type = '';
                             // loop
                             foreach ($membership_types as $key => $type) {
                                 // check
                                 if ($membership_type == $key || $membership_type == $type) {
                                     // match
                                     $sel_type = $key;
                                     break;
                                 }
                             }
                             // check
                             if (!empty($sel_type)) {
                                 $membership_type = $sel_type;
                             } else {
                                 // log
                                 // mgm_log(sprintf('step 10: %s ', $sel_type), __FUNCTION__);
                                 // continue
                                 continue;
                             }
                             // to distinguish between primary membership and other membership(Y/N)
                             if (!isset($i_user['other_membership']) || isset($i_user['other_membership']) && $i_user['other_membership'] != 'Y') {
                                 // set
                                 $member->membership_type = $membership_type;
                                 // update current membership:
                                 $_response = $this->_update_member_object($member, $pack, $i_user);
                                 // check
                                 if (!$_response['status']) {
                                     // log
                                     // mgm_log(sprintf('step 11: %s ', print_r($_response,1)), __FUNCTION__);
                                     // skip the row
                                     continue;
                                 }
                                 // set
                                 $member = $_response['mgm_member'];
                                 // check guest
                                 if (strtolower($member->membership_type) == 'guest') {
                                     // default
                                     $member->other_membership_types = array();
                                 } else {
                                     // flag
                                     $update_role = true;
                                 }
                             } else {
                                 // init
                                 $multiple_updated = false;
                                 // if multiple mgm_member object:
                                 if (isset($member->other_membership_types) && !empty($member->other_membership_types)) {
                                     // loop
                                     foreach ((array) $member->other_membership_types as $key => $member) {
                                         // convert
                                         $member = mgm_convert_array_to_memberobj($member, $user->ID);
                                         // type
                                         if ($member->membership_type == $membership_type) {
                                             // check
                                             $_response = $this->_update_member_object($member, $pack, $i_user);
                                             // check
                                             if (!$_response['status']) {
                                                 // log
                                                 // mgm_log(sprintf('step 12: %s ', print_r($_response,1)), __FUNCTION__);
                                                 // skip the row:
                                                 continue;
                                             }
                                             // make sure array is saved:
                                             $_response['mgm_member'] = mgm_convert_memberobj_to_array($_response['mgm_member']);
                                             // set
                                             $member->other_membership_types[$key] = $_response['mgm_member'];
                                             // flag
                                             $multiple_updated = true;
                                             // break
                                             break;
                                         }
                                     }
                                 }
                                 //else {mgm_log('skip other_memberships 3', __FUNCTION__);}
                                 // add new to mother_membership_types object:
                                 if (!$multiple_updated) {
                                     // update
                                     $_response = $this->_update_member_object(new stdClass(), $pack, $i_user);
                                     // check
                                     if (!$_response['status']) {
                                         // log
                                         // mgm_log(sprintf('step 13: %s ', print_r($_response,1)), __FUNCTION__);
                                         // skip the row:
                                         continue;
                                     }
                                     // set
                                     $_response['mgm_member'] = mgm_convert_memberobj_to_array($_response['mgm_member']);
                                     // set
                                     $member->other_membership_types[] = $_response['mgm_member'];
                                     // flag
                                     $update_role = true;
                                 }
                             }
                         }
                         // payment type:
                         if (!isset($member->payment_type) || isset($member->payment_type) && empty($member->payment_type)) {
                             $member->payment_type = 'subscription';
                         }
                         // update password:
                         if (!empty($user_password)) {
                             // issue#: 672
                             // generate iss#688
                             /*if(empty($user_password_enc)) {
                             			// set
                             			$user_password_enc = mgm_encrypt_password($user_password, $user->ID);
                             			// log
                             			// mgm_log(sprintf('step 7.2: regenarete password: %s ', $user_password_enc), __FUNCTION__);
                             		}*/
                             // set
                             $member->user_password = $user_password_enc;
                             // md5
                             // $user_password_md5 = wp_hash_password($user_password);
                             //mgm_log($wpdb->prepare("UPDATE ".$wpdb->users." SET user_pass = %s WHERE ID = %d", $user_password_md5, $user->ID), __FUNCTION__);
                             // db update
                             //$wpdb->query( $wpdb->prepare("UPDATE ".$wpdb->users." SET user_pass = %s WHERE ID = %d", $user_password_md5, $user->ID) );
                             // new user
                             if ($insert_user) {
                                 $new_users[$id]['user_password'] = $user_password;
                             }
                         }
                         // save mgm_member object:
                         $member->save();
                         // update role:
                         if ($update_role) {
                             // update role/change order
                             $mgm_roles->add_user_role($user->ID, $pack['role']);
                         }
                         // log
                         // mgm_log(sprintf('step 14: %s ', print_r($member,1)), __FUNCTION__);
                     }
                     // update other user fields:
                     $user_extra = array();
                     if (!empty($user_password)) {
                         $user_extra['user_pass'] = $user_password;
                     }
                     // loop
                     foreach ($i_user as $key => $value) {
                         // check
                         if (in_array($key, $user_fields) && !empty($value)) {
                             if ($key == 'user_registered') {
                                 if (mgm_is_valid_date($value) && ($mysql_date = mgm_format_inputdate_to_mysql($value))) {
                                     $user_extra[$key] = $mysql_date;
                                 }
                             } else {
                                 // set
                                 $user_extra[$key] = $value;
                             }
                         }
                     }
                     // update
                     if (!empty($user_extra)) {
                         // set
                         $user_extra['ID'] = $user->ID;
                         // update
                         wp_update_user($user_extra);
                     }
                     // update
                     $update_count++;
                     // check here:
                     unset($member, $user, $user_extra);
                 }
                 // check limit reached:
                 if ($user_count >= $row_limit) {
                     // check
                     if ($row_count > $row_limit) {
                         // set
                         $response['message'] = sprintf(__('( Import stopped at: %s as limit( %d ) reached. )', 'mgm'), $email, $row_limit);
                     }
                     // break;
                     break;
                 }
                 // ------------------------------------------
                 // unset
                 unset($i_user);
                 // debug
                 // if(function_exists('gc_collect_cycles')) gc_collect_cycles();
                 // wait
                 if (!($user_count % 25)) {
                     sleep(1);
                 }
             }
             // end imported users loop
             //	mgm_log('IMPORT MEMORY PEAK2.5: ' . memory_get_peak_usage(true)/(1024*1024));
             // free unwanted resources
             unset($cf_register_page, $cf_exclude_names, $user_fields, $mgm_packs, $mgm_roles, $user_count);
             // debug
             // if(function_exists('gc_collect_cycles')) gc_collect_cycles();
             // done importing, mail and notify
             if ($update_count) {
                 // unset
                 unset($update_count);
                 // set response
                 $response['status'] = true;
                 $response['users_count'] = count($new_users);
                 // send admin notification:
                 // send to admin
                 if (!empty($new_users)) {
                     $system_obj = mgm_get_class('system');
                     // message
                     $message = sprintf('(%d) %s  %s: <br/><br/>', count($new_users), __('New user registration on your blog', 'mgm'), get_option('blogname'));
                     // loop
                     foreach ($new_users as $user_id => $n_user) {
                         // set
                         $message .= sprintf('%s: %s <br/>', __('Username', 'mgm'), $n_user['user_login']);
                         $message .= sprintf('%s: %s <br/>', __('E-mail', 'mgm'), $n_user['email']);
                         $message .= "-----------------------------------<br/><br/>";
                         // unset
                         unset($n_user);
                         // send email to the user:
                         // mgm_new_user_notification($user_id, $new['user_password'],false);
                     }
                     // unset
                     unset($new_users);
                     // check
                     if (isset($response['message'])) {
                         // set
                         $message .= $response['message'];
                         $message .= "-----------------------------------<br/><br/>";
                     }
                     // admin email:
                     @mgm_mail(get_option('admin_email'), sprintf('[%s] %s', __('New User Registration', 'mgm'), get_option('blogname')), $message);
                     // unset
                     unset($message);
                     // debug
                     // if(function_exists('gc_collect_cycles')) gc_collect_cycles();
                 }
             } else {
                 // none updated
                 $response['error'] = __('No users imported', 'mgm');
             }
         } else {
             // no users
             $response['error'] = __('Empty records', 'mgm');
         }
     } else {
         // no users
         $response['error'] = __('Error in processing users', 'mgm');
     }
     // mgm_log('IMPORT MEMORY PEAK3: ' . memory_get_peak_usage(true)/(1024*1024));
     // mgm_log('$response:' . mgm_array_dump($response, true));
     // return
     return $response;
 }
/**
 * register post process
 *
 * @param int $user_id 
 * @return void or int $user_id
 */
function mgm_register($user_id)
{
    global $wpdb, $post;
    // check import in action and skip, tools->import calls mgm_register via "user_register" hook, this will help skip
    if (defined('MGM_DOING_USERS_IMPORT') && MGM_DOING_USERS_IMPORT == TRUE) {
        // return
        return $user_id;
    }
    // get mgm_system
    $system_obj = mgm_get_class('system');
    // hide
    $hide_custom_fields = $system_obj->get_setting('hide_custom_fields');
    // packs
    $packs = mgm_get_class('subscription_packs');
    // members object
    $member = mgm_get_member($user_id);
    // set status
    $member->set_field('status', MGM_STATUS_NULL);
    // get custom fields
    $cf_register_page = mgm_get_class('member_custom_fields')->get_fields_where(array('display' => array('on_register' => true)));
    // mgm_subscription
    $mgm_subscription = mgm_post_var('mgm_subscription');
    // get subs
    $subs_pack = mgm_decode_package($mgm_subscription);
    // extract
    extract($subs_pack);
    // payment_gateways if set:
    $mgm_payment_gateways = mgm_post_var('mgm_payment_gateways');
    // Eg: $_POST['mgm_payment_gateways'] = mgm_paypal
    $cf_payment_gateways = !empty($mgm_payment_gateways) ? $mgm_payment_gateways : NULL;
    // init
    $member_custom_fields = array();
    // wordpress register
    $wordpres_form = mgm_check_wordpress_login();
    // system - issue #1237
    $short_format = !empty($system_obj->setting['date_format_short']) ? $system_obj->setting['date_format_short'] : MGM_DATE_FORMAT_SHORT;
    // loop
    foreach ($cf_register_page as $field) {
        // skip custom fields by settings call
        if ($hide_custom_fields == 'Y' || $hide_custom_fields == 'W' && $wordpres_form || $hide_custom_fields == 'C' && !$wordpres_form) {
            // if($hide_custom_fields && $field['name'] != 'subscription_options') continue;
            if (!in_array($field['name'], array('subscription_options', 'payment_gateways'))) {
                continue;
            }
        }
        //skip if payment_gateways custom field
        if ($field['name'] == 'payment_gateways') {
            continue;
        }
        //
        // do not save html
        if ($field['type'] == 'html' || $field['type'] == 'label') {
            continue;
        }
        // save
        switch ($field['name']) {
            case 'username':
                // #739
                if (isset($_POST[$field['attributes']['capture_field_alias']])) {
                    $member_custom_fields[$field['name']] = @$_POST[$field['attributes']['capture_field_alias']];
                } else {
                    $member_custom_fields[$field['name']] = @$_POST['user_login'];
                }
                break;
            case 'email':
                // #739
                if (isset($_POST[$field['attributes']['capture_field_alias']])) {
                    $member_custom_fields[$field['name']] = @$_POST[$field['attributes']['capture_field_alias']];
                } else {
                    $member_custom_fields[$field['name']] = @$_POST['user_email'];
                }
                break;
            case 'password':
                // #739
                // check
                if (isset($field['attributes']['capture_field_alias']) && isset($_POST[$field['attributes']['capture_field_alias']])) {
                    if (!empty($_POST[$field['attributes']['capture_field_alias']])) {
                        $user_password = @$_POST[$field['attributes']['capture_field_alias']];
                        $member_custom_fields[$field['name']] = mgm_encrypt_password($user_password, $user_id);
                    }
                } else {
                    if (!empty($_POST['user_password'])) {
                        $user_password = $_POST['user_password'];
                        $member_custom_fields[$field['name']] = mgm_encrypt_password($user_password, $user_id);
                    }
                }
                break;
            case 'autoresponder':
                // #739
                if (isset($field['attributes']['capture_field_alias']) && isset($_POST[$field['attributes']['capture_field_alias']])) {
                    // checked issue #839
                    // if(in_array(strtolower($_POST[$field['attributes']['capture_field_alias']]), array('y','yes'))){
                    if (!empty($_POST[$field['attributes']['capture_field_alias']]) && $_POST['mgm_register_field'][$field['name']] == $field['value']) {
                        $member->subscribed = 'Y';
                        $member->autoresponder = $system_obj->active_modules['autoresponder'];
                    }
                } else {
                    // checked issue #839
                    // if(in_array(strtolower($_POST['mgm_register_field'][$field['name']]), array('y','yes'))){
                    if (!empty($_POST['mgm_register_field'][$field['name']]) && $_POST['mgm_register_field'][$field['name']] == $field['value']) {
                        // set to member, to be used on payment
                        $member->subscribed = 'Y';
                        $member->autoresponder = $system_obj->active_modules['autoresponder'];
                    }
                }
                break;
            case 'coupon':
                // #739
                // check alias
                if (isset($field['attributes']['capture_field_alias']) && isset($_POST[$field['attributes']['capture_field_alias']])) {
                    // check
                    if (!empty($_POST[$field['attributes']['capture_field_alias']])) {
                        // validate
                        if ($coupon = mgm_validate_coupon($_POST[$field['attributes']['capture_field_alias']], $cost)) {
                            // set
                            $member->coupon = $coupon;
                            // update coupon usage
                            mgm_update_coupon_usage($coupon['id'], 'register');
                        }
                    }
                } else {
                    // check primary
                    if (isset($_POST['mgm_register_field'][$field['name']]) && !empty($_POST['mgm_register_field'][$field['name']])) {
                        // validate
                        if ($coupon = mgm_validate_coupon($_POST['mgm_register_field'][$field['name']], $cost)) {
                            // set
                            $member->coupon = $coupon;
                            // update coupon usage
                            mgm_update_coupon_usage($coupon['id'], 'register');
                        }
                    }
                }
                break;
            case 'birthdate':
                // #739
                if (isset($field['attributes']['capture_field_alias']) && isset($_POST[$field['attributes']['capture_field_alias']])) {
                    //issue #1237
                    $member_custom_fields[$field['name']] = mgm_format_inputdate_to_mysql($_POST[$field['attributes']['capture_field_alias']], $short_format);
                } else {
                    //convert from short date format to mysql format - issue #1237
                    $member_custom_fields[$field['name']] = mgm_format_inputdate_to_mysql($_POST['mgm_register_field'][$field['name']], $short_format);
                }
                break;
            default:
                // #739
                if (isset($field['attributes']['capture_field_alias']) && isset($_POST[$field['attributes']['capture_field_alias']])) {
                    $member_custom_fields[$field['name']] = @$_POST[$field['attributes']['capture_field_alias']];
                } elseif ($field['type'] == 'checkbox' && is_array(@$_POST['mgm_register_field'][$field['name']])) {
                    //$member_custom_fields[$field['name']] = implode(" ", @$_POST['mgm_register_field'][$field['name']]);
                    //issue #1070
                    $val = @$_POST['mgm_register_field'][$field['name']];
                    $member_custom_fields[$field['name']] = serialize($val);
                } else {
                    $member_custom_fields[$field['name']] = @$_POST['mgm_register_field'][$field['name']];
                }
                break;
        }
    }
    // end fields save
    // user password not provided
    /*
    	if (!isset( $user_password )){
    		$user_password = (isset($_POST['pass1']) && !empty($_POST['pass1'])) ? trim($_POST['pass1']) : substr(md5(uniqid(microtime())), 0, 7);		
    	}*/
    // user password not provided
    if (!isset($user_password)) {
        // take custom password fields, iss#717, consider BP custom password field
        $password_fields = array('pass1', 'signup_password');
        // loop
        foreach ($password_fields as $password_field) {
            // check if set
            if (isset($_POST[$password_field]) && !empty($_POST[$password_field])) {
                $user_password = trim($_POST[$password_field]);
                break;
            }
        }
    }
    // auto generate if still missing
    if (!isset($user_password)) {
        $user_password = substr(md5(uniqid(microtime())), 0, 7);
    }
    //encrypt password and save in
    $member->user_password = mgm_encrypt_password($user_password, $user_id);
    // md5
    $user_password_hash = wp_hash_password($user_password);
    // db update
    $wpdb->query($wpdb->prepare("UPDATE `{$wpdb->users}` SET `user_pass` = %s WHERE ID = %d", $user_password_hash, $user_id));
    // unset label fields
    if (isset($member_custom_fields['password_conf'])) {
        unset($member_custom_fields['password_conf']);
    }
    // set custom
    $member->set_custom_fields($member_custom_fields);
    // set pack
    if ($pack_id) {
        // pack
        $pack = $packs->get_pack($pack_id);
        // set
        $member->amount = $pack['cost'];
        $member->duration = $pack['duration'];
        $member->duration_type = $pack['duration_type'];
        $member->active_num_cycles = $pack['num_cycles'];
        // set membership type
        $member->membership_type = $membership_type;
        // from mgm_subscription
        // set in member
        $member->pack_id = $pack_id;
        // from mgm_subscription
    }
    // set status
    $member->status = MGM_STATUS_NULL;
    // update option
    $member->save();
    // update user firstname/last name
    mgm_update_default_userdata($user_id);
    // admin check
    $is_admin = is_admin();
    //&& current_user_can('manage_options');
    // send
    $notify_user = true;
    // Block registration emails if Buddypress is enabled and disable_registration_email_bp value is Yes
    $block_reg_email = bool_from_yn(mgm_get_class('system')->get_setting('disable_registration_email_bp'));
    // send notification, bp active, do not send password, #739
    if (!isset($_POST['send_password']) && $is_admin || mgm_is_plugin_active('buddypress/bp-loader.php') && $block_reg_email) {
        $notify_user = false;
    }
    // send notification - issue #1468
    if ($system_obj->setting['enable_new_user_email_notifiction_after_user_active'] == 'N') {
        if ($notify_user) {
            mgm_new_user_notification($user_id, $user_password, $is_admin ? false : true);
        }
        $notify_user = false;
    }
    // hook for other plugin who wishes to use default "user_register"
    do_action('mgm_user_register', $user_id);
    // process payment only when registered from site, not when user added by admin
    if ($is_admin) {
        // unset
        unset($_POST['send_password']);
        //prevent sending user email again
        // assign default pack
        do_action('mgm_admin_user_register', $user_id, $notify_user);
        // return id
        return $user_id;
    }
    // if on wordpress page or custompage
    $post_id = get_the_ID();
    // post custom register
    if ($post_id > 0 && $post->post_type == 'post') {
        $redirect = get_permalink($post_id);
    } else {
        $redirect = mgm_get_custom_url('transactions');
    }
    // if buddypress url replace by register url : issue#: 791
    $redirect = apply_filters('mgm_bp_register_url', $redirect);
    // userdata
    $userdata = get_userdata($user_id);
    // note this fix VERY IMPORTANT, needed for PAYPAL PRO CC POST
    $redirect = add_query_arg(array('username' => urlencode($userdata->user_login)), $redirect);
    // add redirect
    if ($redirector = mgm_request_var('mgm_redirector', mgm_request_var('redirect_to', '', true), true)) {
        $redirect = add_query_arg(array('redirector' => $redirector), $redirect);
    }
    // with subscription
    if ($mgm_subscription) {
        $redirect = add_query_arg(array('subs' => $mgm_subscription, 'method' => 'payment_subscribe'), $redirect);
    }
    // bypass step2 if payment gateway is submitted: issue #: 469
    if (!is_null($cf_payment_gateways)) {
        // pack
        $packs_obj = mgm_get_class('subscription_packs');
        // validate
        $pack = $packs_obj->validate_pack($cost, $duration, $duration_type, $membership_type, $pack_id);
        // error
        if ($pack != false) {
            // get pack
            mgm_get_register_coupon_pack($member, $pack);
            // cost
            if ((double) $pack['cost'] > 0) {
                //get an object of the payment gateway:
                $mod_obj = mgm_get_module($cf_payment_gateways, 'payment');
                // tran options
                $tran_options = array('is_registration' => true, 'user_id' => $user_id, 'notify_user' => $notify_user);
                // is register & purchase
                if (isset($_POST['post_id'])) {
                    $tran_options['post_id'] = (int) $_POST['post_id'];
                }
                // is register & purchase postpack
                if (isset($_POST['postpack_post_id']) && isset($_POST['postpack_id'])) {
                    $tran_options['postpack_post_id'] = (int) $_POST['postpack_post_id'];
                    $tran_options['postpack_id'] = (int) $_POST['postpack_id'];
                }
                // create transaction
                // $tran_id = $mod_obj->_create_transaction($pack, $tran_options);
                $tran_id = mgm_add_transaction($pack, $tran_options);
                //bypass directly to process return if manual payment:
                if ($cf_payment_gateways == 'mgm_manualpay') {
                    // set
                    $_POST['custom'] = $tran_id;
                    // direct call to module return function:
                    $mod_obj->process_return();
                    // exit
                    exit;
                }
                // encode id:
                $tran_id = mgm_encode_id($tran_id);
                // redirect - if on wordpress page or custompage - issue #1648
                if ($post_id > 0 && $post->post_type == 'post') {
                    $redirect = $mod_obj->_get_endpoint('html_redirect', true);
                } else {
                    $redirect = $mod_obj->_get_endpoint('html_redirect', false);
                }
                // if buddypress url replace by register url : issue#: 791
                $redirect = add_query_arg(array('tran_id' => $tran_id), apply_filters('mgm_bp_register_url', $redirect));
            } else {
                // issue #1468
                $redirect = add_query_arg(array('notify_user' => $notify_user), $redirect);
            }
        }
    }
    // ends custom payment gateway bypassing
    // is register & purchase
    if (isset($_POST['post_id'])) {
        $redirect = add_query_arg(array('post_id' => (int) $_POST['post_id']), $redirect);
    }
    // is register & purchase postpack
    if (isset($_POST['postpack_post_id']) && isset($_POST['postpack_id'])) {
        $redirect = add_query_arg(array('postpack_id' => (int) $_POST['postpack_id'], 'postpack_post_id' => (int) $_POST['postpack_post_id']), $redirect);
    }
    // redirect filter, returing a false can stop the redirect
    $redirect = apply_filters('mgm_after_regiter_redirect', mgm_site_url($redirect));
    // redirect
    if ($redirect !== FALSE) {
        // do the redirect to payment
        mgm_redirect($redirect);
        // this goes to subscribe, mgm_functions.php/mgm_get_subscription_buttons
        // exit
        exit;
    }
    // default
    return $user_id;
}
/**
 * validate and save profile data
 *
 * @param int user id
 * @return int user id
 */
function mgm_user_profile_update($user_id)
{
    global $wpdb;
    // get user
    if ($user_id > 0) {
        $user_data = get_userdata($user_id);
    }
    // error
    if (!$user_data->ID) {
        return $user_id;
    }
    // flag to control callback re calling via hooks clash, iss#705
    define('MGM_DOING_USERS_PROFILE_UPDATE', TRUE);
    // set aside member object
    $member = mgm_get_member($user_id);
    // create empty user
    $user = new stdClass();
    // set id
    $user->ID = $user_data->ID;
    // sanitize user login
    if (isset($_POST['user_login'])) {
        $user->user_login = sanitize_user($_POST['user_login'], true);
    }
    // asnitize email and copy
    if (isset($_POST['user_email'])) {
        $user->user_email = sanitize_text_field($_POST['user_email']);
    }
    // urls
    if (isset($_POST['mgm_profile_field']['url'])) {
        if (empty($_POST['mgm_profile_field']['url']) || $_POST['mgm_profile_field']['url'] == 'http://') {
            $user->user_url = '';
        } else {
            $user->user_url = esc_url_raw($_POST['mgm_profile_field']['url']);
            $user->user_url = preg_match('/^(https?|ftps?|mailto|news|irc|gopher|nntp|feed|telnet):/is', $user->user_url) ? $user->user_url : 'http://' . $user->user_url;
        }
    }
    if (isset($_POST['mgm_profile_field']['first_name'])) {
        $user->first_name = sanitize_text_field($_POST['mgm_profile_field']['first_name']);
    }
    if (isset($_POST['mgm_profile_field']['last_name'])) {
        $user->last_name = sanitize_text_field($_POST['mgm_profile_field']['last_name']);
    }
    if (isset($_POST['mgm_profile_field']['nickname'])) {
        $user->nickname = sanitize_text_field($_POST['mgm_profile_field']['nickname']);
    }
    if (isset($_POST['mgm_profile_field']['display_name'])) {
        $user->display_name = sanitize_text_field($_POST['mgm_profile_field']['display_name']);
    }
    if (isset($_POST['mgm_profile_field']['description'])) {
        $user->description = trim($_POST['mgm_profile_field']['description']);
    }
    // init errors
    $errors = new WP_Error();
    // check user login
    if (isset($_POST['user_login']) && !validate_username($_POST['user_login'])) {
        $errors->add('user_login', __('<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.', 'mgm'));
    }
    // user login duplicate
    if (($owner_id = username_exists($user->user_login)) && $owner_id != $user->ID) {
        $errors->add('user_login', __('<strong>ERROR</strong>: This username is already registered. Please choose another one.', 'mgm'));
    }
    // nickname
    //!isset( $_POST['mgm_profile_field']['nickname'] ) || - issue #1207
    if (isset($_POST['mgm_profile_field']['nickname']) && empty($_POST['mgm_profile_field']['nickname'])) {
        $errors->add('nickname', __('<strong>ERROR</strong>: You must provide a Nick Name.', 'mgm'));
    }
    // email - issue #1207
    if (isset($_POST['user_email']) && empty($user->user_email)) {
        $errors->add('empty_email', __('<strong>ERROR</strong>: Please enter an e-mail address.', 'mgm'), array('form-field' => 'email'));
    } elseif (isset($_POST['user_email']) && !is_email($user->user_email)) {
        $errors->add('invalid_email', __('<strong>ERROR</strong>: The e-mail address isn&#8217;t correct.', 'mgm'), array('form-field' => 'email'));
    } elseif (isset($_POST['user_email']) && ($owner_id = email_exists($user->user_email)) && $owner_id != $user->ID) {
        $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.', 'mgm'), array('form-field' => 'email'));
    }
    // password:
    $pass1 = $pass2 = '';
    if (isset($_POST['user_password'])) {
        $pass1 = sanitize_text_field($_POST['user_password']);
    }
    if (isset($_POST['user_password_conf'])) {
        $pass2 = sanitize_text_field($_POST['user_password_conf']);
    }
    /* checking the password has been typed twice */
    do_action_ref_array('check_passwords', array($user->user_login, &$pass1, &$pass2));
    //issue #1207
    if (isset($_POST['user_password']) && isset($_POST['user_password_conf'])) {
        if (empty($pass1) && !empty($pass2)) {
            $errors->add('pass', __('<strong>ERROR</strong>: You entered your new password only once.', 'mgm'), array('form-field' => 'pass1'));
        } elseif (!empty($pass1) && empty($pass2)) {
            $errors->add('pass', __('<strong>ERROR</strong>: You entered your new password only once.', 'mgm'), array('form-field' => 'pass2'));
        }
    }
    /* Check for "\" in password */
    //issue #1207
    if (isset($_POST['user_password']) && isset($_POST['user_password_conf'])) {
        if (false !== strpos(stripslashes($pass1), "\\")) {
            $errors->add('pass', __('<strong>ERROR</strong>: Passwords may not contain the character "\\".', 'mgm'), array('form-field' => 'pass1'));
        }
    }
    // get default fields
    $profile_fields = mgm_get_config('default_profile_fields', array());
    // get active custom fields on profile page
    $cf_profile_page = mgm_get_class('member_custom_fields')->get_fields_where(array('display' => array('on_profile' => true)));
    //init - issue #1573
    $show_membership_fields_arr = array();
    if (isset($_REQUEST['membership']) && !empty($_REQUEST['membership'])) {
        // membership
        $membership = $_REQUEST['membership'];
        // get active custom fields on register
        $cf_profile_by_membership_types = mgm_get_class('member_custom_fields')->get_fields_where(array('attributes' => array('profile_by_membership_types' => true)));
        //mgm_pr($cf_profile_by_membership_types);
        //check
        if (!empty($cf_profile_by_membership_types)) {
            //loop
            foreach ($cf_profile_by_membership_types as $cf_profile_by_membership_type) {
                //membership_type
                $membership_types_string = $cf_profile_by_membership_type['attributes']['profile_membership_types_field_alias'];
                //check
                if (preg_match('/\\b' . $membership . '\\b/', $membership_types_string)) {
                    $show_fields_arr[] = $cf_profile_by_membership_type['name'];
                    $show_membership_fields_arr[] = $cf_profile_by_membership_type;
                    if ($cf_profile_by_membership_type['name'] == 'password') {
                        foreach ($cf_profile_by_membership_types as $cf_profile_by_membership) {
                            if ($cf_profile_by_membership['name'] == 'password_conf') {
                                $show_membership_fields_arr[] = $cf_profile_by_membership;
                            }
                        }
                    }
                }
            }
        }
    }
    //merge - issue #1573
    if (isset($show_membership_fields_arr) && is_array($show_membership_fields_arr) && !empty($show_membership_fields_arr)) {
        $cf_profile_page = array_merge($cf_profile_page, $show_membership_fields_arr);
    }
    //Profile page password filed is default .#issue 799
    $falg = 0;
    $pass_field = '';
    foreach ($cf_profile_page as $field) {
        if ($field['name'] == 'password') {
            $falg = 1;
            //issue #973
            $pass_field = $field;
        }
    }
    if ($falg == 0) {
        $cf_profile_page[] = array('name' => 'password', 'label' => 'Password', 'type' => 'password', 'system' => 1);
    }
    //issue #973 & issue #1207
    if (isset($_POST['user_password']) && isset($_POST['user_password_conf']) && !empty($pass1) && !empty($pass2) && (isset($pass_field['attributes']['password_min_length']) && $pass_field['attributes']['password_min_length'] == true) || isset($pass_field['attributes']['password_max_length']) && $pass_field['attributes']['password_max_length'] == true) {
        if (strlen($pass1) < $pass_field['attributes']['password_min_length_field_alias'] || strlen($pass2) < $pass_field['attributes']['password_min_length_field_alias']) {
            $errors->add('pass', sprintf(__('<strong>ERROR</strong>:Password is too short, minimum %d characters.', 'mgm'), $pass_field['attributes']['password_min_length_field_alias']), array('form-field' => 'pass1'));
        } elseif (strlen($pass1) > $pass_field['attributes']['password_max_length_field_alias'] || strlen($pass2) > $pass_field['attributes']['password_max_length_field_alias']) {
            $errors->add('pass', sprintf(__('<strong>ERROR</strong>:Password is too long, minimum %d characters.', 'mgm'), $pass_field['attributes']['password_max_length_field_alias']), array('form-field' => 'pass1'));
        } elseif ($pass1 != $pass2) {
            $errors->add('pass', __('<strong>ERROR</strong>: Please enter the same password in the two password fields.', 'mgm'), array('form-field' => 'pass1'));
        }
    } elseif (isset($_POST['user_password']) && isset($_POST['user_password_conf']) && $pass1 != $pass2) {
        $errors->add('pass', __('<strong>ERROR</strong>: Please enter the same password in the two password fields.', 'mgm'), array('form-field' => 'pass1'));
    }
    // confirm email - issue #1315
    if (isset($_POST['user_email_conf']) && empty($_POST['user_email_conf'])) {
        $errors->add($field['name'], __('<strong>ERROR</strong>: Please type your confirm e-mail address.', 'mgm'));
    } elseif (isset($_POST['user_email_conf']) && !is_email($_POST['user_email_conf'])) {
        $errors->add('invalid_email_conf', __('<strong>ERROR</strong>: The confirm email address isn&#8217;t correct.', 'mgm'));
    } elseif (isset($_POST['user_email_conf']) && is_email($_POST['user_email']) && $_POST['user_email_conf'] != $_POST['user_email']) {
        $errors->add($field['name'], __('<strong>ERROR</strong>: E-mail does not match. Please re-type.', 'mgm'));
    }
    //issue #1207
    $m_pass = '';
    // set
    if (!empty($pass1) || !empty($pass2)) {
        if (!empty($pass1) && !empty($pass2)) {
            $user->user_pass = wp_hash_password($pass1);
            $m_pass = $pass1;
        } elseif (!empty($pass1)) {
            $user->user_pass = wp_hash_password($pass1);
            $m_pass = $pass1;
        } else {
            $user->user_pass = wp_hash_password($pass2);
            $m_pass = $pass2;
        }
        //issue #703
        //$user->user_pass = $pass1;
        $member->user_password = mgm_encrypt_password($m_pass, $user->ID);
    }
    // loop
    foreach ($cf_profile_page as $field) {
        // skip default fields, validated already
        if (in_array($field['name'], array('username', 'email', 'password', 'password_conf', 'email_conf'))) {
            continue;
        }
        // skip html
        if ($field['type'] == 'html' || $field['type'] == 'label') {
            continue;
        }
        // check register and required
        if ((bool) $field['attributes']['required'] === true) {
            // error
            $error_codes = $errors->get_error_codes();
            // validate other
            if (!isset($_POST['mgm_profile_field'][$field['name']]) || empty($_POST['mgm_profile_field'][$field['name']])) {
                //issue #703
                $errors->add($field['name'], __('<strong>ERROR</strong>: You must provide a ', 'mgm') . mgm_stripslashes_deep($field['label']) . '.');
            }
        }
    }
    // Allow plugins to return their own errors.
    do_action_ref_array('user_profile_update_errors', array(&$errors, $update, &$user));
    // error
    if ($errors->get_error_codes()) {
        return $errors;
    }
    // init pass
    $user_password = '';
    // system - issue #1237
    $system_obj = mgm_get_class('system');
    $short_format = !empty($system_obj->setting['date_format_short']) ? $system_obj->setting['date_format_short'] : MGM_DATE_FORMAT_SHORT;
    // update custom fields values:
    if (isset($_POST['mgm_profile_field'])) {
        // loop fields
        foreach ($cf_profile_page as $field) {
            // skip html
            if ($field['type'] == 'html' || $field['type'] == 'label' || $field['name'] == 'password_conf') {
                continue;
            }
            // set
            if (isset($_POST['mgm_profile_field'][$field['name']])) {
                // value
                $value = $_POST['mgm_profile_field'][$field['name']];
                // birthdate
                if ($field['name'] == 'birthdate') {
                    //convert to mysql date format(to standardise the date format) -issue #1237
                    $value = mgm_format_inputdate_to_mysql($value, $short_format);
                } elseif ($field['name'] == 'password') {
                    // pass iss#705
                    $user_password = $value;
                    // issue#: 672
                    $value = mgm_encrypt_password($value, $user_id);
                } elseif ($field['type'] == 'checkbox' && is_array($value)) {
                    //$value = implode(" ", $value);
                    //issue #1070
                    $value = serialize($value);
                }
                // set
                $member->custom_fields->{$field}['name'] = $value;
            } elseif (isset($_POST[$field['name']])) {
                // value
                $value = $_POST[$field['name']];
                // birthdate
                if ($field['name'] == 'birthdate') {
                    // convert to mysql date format(to standardise the date format) - issue #1237
                    $value = mgm_format_inputdate_to_mysql($value, $short_format);
                } elseif ($field['name'] == 'password') {
                    // pass iss#705
                    $user_password = $value;
                    //issue#: 672
                    $value = mgm_encrypt_password($value, $user_id);
                } elseif ($field['type'] == 'checkbox' && is_array($value)) {
                    //issue #1070
                    $value = serialize($value);
                    //$value = implode(" ", $value);
                }
                // set
                $member->custom_fields->{$field}['name'] = $value;
            } elseif ($field['name'] == 'password' && !empty($pass1)) {
                // pass iss#705
                $user_password = $pass1;
                // value
                $value = mgm_encrypt_password($pass1, $user_id);
                // set
                $member->custom_fields->{$field}['name'] = $value;
            } elseif ($field['type'] == 'checkbox' && isset($member->custom_fields->{$field}['name'])) {
                // If no value selected
                $member->custom_fields->{$field}['name'] = '';
            }
        }
    }
    // update
    $member->save();
    //issue #1207
    if (!empty($m_pass)) {
        // pass iss#705
        $user_password = $m_pass;
    }
    // iss#705
    // userdata to update, leave password here
    $userdata = get_object_vars($user);
    // unset encoded password from userdata
    unset($userdata['user_pass']);
    // update password
    if (!empty($user_password)) {
        // set
        $userdata['user_pass'] = $user_password;
    }
    // save main user data & return user id
    return $user_id = wp_update_user($userdata);
}
/**
 * save custom user fields, should only execute 
 *  when user data is saved from admin user edit screen
 *
 * @param int user id
 * @return bool success
 * @uses "profile_update" hook
 */
function mgm_save_custom_fields($user_id = NULL)
{
    // get user id
    if (!$user_id) {
        $user_id = @(int) $_POST['user_id'];
    }
    // check profile update in action and skip, profile update ( mgm_user_profile_update() )
    // calls mgm_save_custom_fields via "profile_update" hook, this will help skip
    if (defined('MGM_DOING_USERS_PROFILE_UPDATE') && MGM_DOING_USERS_PROFILE_UPDATE == TRUE) {
        // return
        return $user_id;
    }
    // get member & user
    $user = get_userdata($user_id);
    //check logged in user is super admin:
    $is_admin = is_super_admin() ? true : false;
    // member
    $member = mgm_get_member($user_id);
    // default return
    $return = false;
    // submit
    if (isset($_POST['submit'])) {
        // password update
        if ($pass = $_POST['pass1']) {
            $member->user_password = mgm_encrypt_password($pass, $user_id);
        }
        // get default fields
        $profile_fields = mgm_get_config('default_profile_fields', array());
        // get active profile fileds
        // issue #954
        //$cf_on_profilepage = mgm_get_class('member_custom_fields')->get_fields_where(array('display'=>array('on_profile'=>true)));
        $cf_profile_pg = mgm_get_class('member_custom_fields');
        $cf_on_profilepage = array();
        foreach (array_unique($cf_profile_pg->sort_orders) as $id) {
            foreach ($cf_profile_pg->custom_fields as $field) {
                // issue #954: show the field only if it is enabled for profile page
                if ($field['id'] == $id && ($field['display']['on_profile'] || $is_admin)) {
                    $cf_on_profilepage[] = $field;
                }
            }
        }
        // loop fields
        foreach ($cf_on_profilepage as $field) {
            // skip html
            if (in_array($field['type'], array('html', 'label')) || $field['name'] == 'password_conf') {
                continue;
            }
            //issue#: 206
            // custom
            if (isset($_POST['mgm_profile_field'][$field['name']])) {
                // value as it was posted
                $value = $_POST['mgm_profile_field'][$field['name']];
                // convert to date for birth date
                if ($field['name'] == 'birthdate') {
                    $value = mgm_format_inputdate_to_mysql($value);
                } elseif ($field['name'] == 'password') {
                    // encode for password
                    $value = mgm_encrypt_password($value, $user_id);
                } elseif ($field['type'] == 'checkbox' && is_array($value)) {
                    // join for checkbox with multi value
                    //$value = implode(' ', $value);
                    //issue #1070
                    $value = serialize($value);
                }
            } else {
                if (isset($_POST[$profile_fields[$field['name']]['name']])) {
                    // wordpress
                    // value as it was posted
                    $value = $_POST[$profile_fields[$field['name']]['name']];
                    // convert to date for birth date
                    if ($field['name'] == 'birthdate') {
                        $value = mgm_format_inputdate_to_mysql($value);
                    } elseif ($field['name'] == 'password') {
                        // encode for password
                        $value = mgm_encrypt_password($value, $user_id);
                    } elseif ($field['type'] == 'checkbox' && is_array($value)) {
                        // join for checkbox with multi value
                        //$value = implode(' ', $value);
                        //issue #1070
                        $value = serialize($value);
                    }
                } else {
                    // default
                    // value as it was posted
                    $value = $_POST[$field['name']];
                    // convert to date for birth date
                    if ($field['name'] == 'birthdate') {
                        $value = mgm_format_inputdate_to_mysql($value);
                    } elseif ($field['name'] == 'password' && !empty($_POST['pass1'])) {
                        // encode for password
                        $value = mgm_encrypt_password($_POST['pass1'], $user_id);
                    } elseif ($field['type'] == 'checkbox' && is_array($value)) {
                        // join for checkbox with multi value
                        //$value = implode(' ', $value);
                        //issue #1070
                        $value = serialize($value);
                    }
                }
            }
            // set
            $member->custom_fields->{$field}['name'] = $value;
        }
        // update
        $member->save();
        // return as true
        $return = true;
        //important: the below function is to reinsert the user multiple roles.
        //This is required as the default profile page deletes the unselected roles from user
        mgm_reset_roles();
    }
    // mgm_array_dump($user);die;
    // return
    return $return;
}