function cw_fbauth_user_login($user)
{
    global $tables, $current_area;
    cw_load('crypt', 'user', 'mail');
    $user_data = cw_query_first("SELECT customer_id, email, password\n\t\t\t\t\t\t\t\t\tFROM {$tables['customers']} \n\t\t\t\t\t\t\t\t\tWHERE oauth_uid='" . $user['id'] . "' AND oauth_type='F'  \n\t\t\t\t\t\t\t\t\t\tAND usertype='" . $current_area . "' \n\t\t\t\t\t\t\t\t\t\tAND status='Y'");
    if (!empty($user_data)) {
        // login user
        global $email, $password, $action;
        $email = $user_data['email'];
        $password = text_decrypt($user_data['password']);
        $action = 'login';
        cw_include('include/login.php');
    } else {
        // create user
        $register = array();
        $register['email'] = $user['email'];
        if (strpos($user['email'], "proxymail.facebook.com") !== FALSE) {
            cw_header_location("index.php?target=fb_auth_get_email", TRUE);
        }
        $register['password'] = $register['password2'] = md5(uniqid('cw_', TRUE), TRUE);
        $register['usertype'] = $current_area;
        $partner_membership =& cw_session_register('partner_membership');
        $register['membership_id'] = $partner_membership;
        $customer_id = cw_user_create_profile($register);
        $identifiers =& cw_session_register('identifiers', array());
        $identifiers[$current_area] = array('customer_id' => $customer_id);
        $customer = array('oauth_uid' => $user['id']);
        cw_array2update('customers', $customer, "customer_id='{$customer_id}'");
        $address = array('main' => 1, 'firstname' => $user['first_name'], 'lastname' => $user['last_name']);
        $additional_info = array('sex' => $user['gender'] == 'male' ? 1 : 0);
        $userinfo = array('addresses' => $address, 'additional_info' => $additional_info);
        cw_user_update($userinfo, $customer_id, $customer_id);
        cw_user_send_modification_mail($customer_id, TRUE);
    }
}
         cw_add_top_message(cw_get_langvar_by_name('err_account_temporary_disabled'), 'E');
         cw_header_location($instagram_redirect_url);
     }
 } else {
     //add new customer to database
     $register = array();
     $register['email'] = $instagram_email;
     $register['usertype'] = $current_area;
     $partner_membership =& cw_session_register('partner_membership');
     $register['membership_id'] = $partner_membership;
     $customer_id = cw_user_create_profile($register);
     cw_array2update('customers', array('oauth_uid' => $instagram_user_id, 'oauth_type' => 'I'), "customer_id='{$customer_id}'");
     $user_name = explode(" ", $instagram_user_name);
     $address = array('main' => 1, 'firstname' => $user_name[0], 'lastname' => $user_name[1]);
     $userinfo = array('addresses' => $address);
     cw_user_update($userinfo, $customer_id, $customer_id);
     $user_data = cw_query_first("SELECT {$tables['customers']}.* FROM {$tables['customers']} WHERE customer_id='{$customer_id}'");
 }
 if (!empty($user_data)) {
     //perform login
     $email = $user_data['email'];
     if ($usertype == 'R') {
         $usertype = 'C';
         $product_list_template =& cw_session_register("product_list_template");
         $product_list_template = 2;
     }
     $identifiers[$current_area == 'R' ? 'C' : $current_area] = array('customer_id' => $user_data['customer_id']);
     $customer_id = $user_data['customer_id'];
     if (in_array($current_area, array("C", "R"))) {
         cw_session_register("login_redirect");
         $login_redirect = 1;
     $fill_error['basic']['email'] = true;
 }
 // validation is OK
 if (!count($fill_error)) {
     $new_profile = cw_check_user_field_build_profile($userinfo, $update_fields, $profile_fields);
     $is_new_profile = false;
     if ($mode == 'add') {
         $new_profile['usertype'] = $usertype;
         $user = cw_user_create_profile($new_profile);
         // Login registered user
         $identifiers =& cw_session_register('identifiers', array());
         $identifiers['C'] = array('customer_id' => $user);
         $is_new_profile = true;
         cw_event('on_login', array($user, 'C', 1));
     }
     cw_user_update($new_profile, $user, $customer_id);
     // Sign in confirmation letter sending have to be apparently blocked if an user is fake
     if (!isset($is_anonymous) || $is_anonymous != 1) {
         cw_user_send_modification_mail($user, $is_new_profile);
     }
     cw_event('on_profile_modify', array($user, $new_profile));
     if ($mode == 'add' && $is_anonymous) {
         $top_message_text = 'Please proceed to shipping and payment';
     } else {
         $top_message_text = $mode == 'add' ? cw_get_langvar_by_name('msg_user_has_been_added_' . (empty($usertype) ? 'C' : $usertype)) : cw_get_langvar_by_name('msg_profile_upd');
     }
     cw_add_top_message($top_message_text, 'I');
 } else {
     $filled_profile = $update_fields;
     cw_add_top_message(cw_check_user_get_error($fill_error), 'E');
 }
function cw_doc_update_user_information($doc_type, $doc_info_id, $userinfo, $is_create_user = false)
{
    global $customer_id, $config;
    $address_fields = array('company', 'title', 'firstname', 'lastname', 'address', 'address_2', 'city', 'county', 'state', 'country', 'zipcode', 'phone', 'fax', 'region');
    foreach (array('main_address', 'current_address') as $address_type) {
        $address = $userinfo[$address_type];
        cw_array2update('customers_addresses', cw_addslashes($address), "address_id='{$address['address_id']}'", $address_fields);
        if (is_array($address['custom_fields']) && !empty($address['custom_fields'])) {
            cw_profile_fields_update_type(0, $address['address_id'], 'A', $address['custom_fields']);
        }
    }
    if (!$userinfo['customer_id'] && $is_create_user) {
        $userinfo['customer_id'] = cw_user_create_profile(array('usertype' => $userinfo['usertype']));
        $userinfo['additional_info'] = $userinfo;
        cw_user_update(cw_addslashes($userinfo), $userinfo['customer_id'], $customer_id);
    }
    $userinfo_fields = array('customer_id', 'membership_id', 'company', 'email', 'tax_number', 'tax_exempt', 'ssn', 'company_type', 'usertype');
    // Real e-mail for anonymous buyer that will be placed in order
    $userinfo['email'] = cw_real_mail_address($userinfo['email']);
    # kornev, 'company_id' ?
    cw_array2update('docs_user_info', cw_addslashes($userinfo), "doc_info_id='{$doc_info_id}'", $userinfo_fields);
}
 $result = cw_func_call('cw_paypal_express_request', array('request' => $request));
 $state_err = 0;
 $address = array('firstname' => empty($result['address']['FirstName']) ? $result['FirstName'] : $result['address']['FirstName'], 'lastname' => empty($result['address']['LastName']) ? $result['LastName'] : $result['address']['LastName'], 'address' => preg_replace('![\\s\\n\\r]+!s', ' ', $result['address']['Street1']) . "\n" . preg_replace('![\\s\\n\\r]+!s', ' ', @$result['address']['Street2']), 'city' => $result['address']['CityName'], 'country' => $result['address']['Country'], 'zipcode' => $result['address']['PostalCode'], 'phone' => empty($result['address']['Phone']) ? $result['ContactPhone'] : $result['address']['Phone'], 'state' => cw_paypal_express_detect_state($result['address']['Country'], $result['address']['StateOrProvince'], $state_err));
 if ($config["General"]["use_counties"] == "Y") {
     $default_county = cw_default_county($address['state'], $address['country']);
     $address['county'] = empty($default_county) ? $result['address']['StateOrProvince'] : $default_county;
 }
 $customer_id =& cw_session_register('customer_id');
 if ($customer_id) {
     $address_id = cw_query_first_cell("select address_id from {$tables['customers_addresses']} where customer_id='{$customer_id}' and current=1");
     cw_user_update_address($customer_id, $address_id, cw_addslashes($address));
 } else {
     $profile_values = array('email' => $result['Payer'], 'current_address' => $address);
     $customer_id = cw_user_create_profile(array('usertype' => 'C'));
     $profile_values['status'] = 'Y';
     cw_user_update($profile_values, $customer_id, $customer_id);
     $identifiers =& cw_session_register("identifiers", array());
     $identifiers['C'] = array('customer_id' => $customer_id);
 }
 $paypal_express_details = $result;
 switch ($state_err) {
     case 1:
         $top_message = array("type" => "W", "content" => cw_get_langvar_by_name("lbl_paypal_wrong_country_note"));
         break;
     case 2:
         $top_message = array("type" => "W", "content" => cw_get_langvar_by_name("lbl_paypal_wrong_state_note"));
 }
 # kornev, re-calculate the cart with the address and place the order
 $cart['userinfo'] = $userinfo = cw_user_get_info($customer_id, 65535);
 $products = cw_call('cw_products_in_cart', array($cart, $userinfo));
 $cart = cw_func_call('cw_cart_calc', array('cart' => $cart, 'products' => $products, 'userinfo' => $userinfo));