function cw_checkout_login_prepare()
{
    global $smarty, $user_account, $customer_id, $user_address;
    $customer_id =& cw_session_register('customer_id', 0);
    $fields_area = cw_profile_fields_get_area($customer_id, $salesman_membership, 1);
    list($profile_sections, $profile_fields, $additional_fields) = cw_profile_fields_get_sections('U', true, $fields_area);
    # kornev, the login information is not required here.
    if ($customer_id) {
        unset($profile_sections['web']);
    }
    $smarty->assign('profile_fields', $profile_fields);
    $smarty->assign('profile_sections', $profile_sections);
    cw_include('include/check_userdata.php');
    cw_include('include/check_usercart.php');
    $userinfo = cw_call('cw_checkout_userinfo', array($user_account));
    $smarty->assign('userinfo', $userinfo);
    $smarty->assign('user_account', $user_account);
}
<?php

cw_load('profile_fields', 'map', 'check_user_field');
$fill_error =& cw_session_register('fill_error');
$prefilled_contact_list =& cw_session_register('prefilled_contact_list');
$area = cw_profile_fields_get_area($user);
list($profile_sections, $profile_fields) = cw_profile_fields_get_sections('U', true, $area);
$smarty->assign('profile_fields', $profile_fields);
if ($action == 'update_contact_list') {
    $fill_error = array();
    cw_array_map('trim', $contact_list);
    foreach ($profile_fields['contact_list'] as $k => $v) {
        if ($v['type'] == 'D' && empty($contact_list[$k]) && $v['is_avail'] && $v['is_required']) {
            $fill_error[$k] = true;
        } elseif ($v['type'] != 'D' && empty($contact_list['custom_fields'][$k]) && $v['is_avail'] && $v['is_required']) {
            $fill_error[$k] = true;
        }
    }
    $prefilled_contact_list = array();
    if (count($fill_error)) {
        $top_message = array('type' => 'E', 'content' => cw_check_user_get_error($fill_error));
        $prefilled_contact_list = $contact_list;
        cw_header_location("index.php?target={$target}&mode={$mode}&user={$user}&contact_list_id={$contact_list_id}");
    } else {
        cw_user_update_contact_list($user, $contact_list_id, $contact_list);
        cw_header_location("index.php?target={$target}&mode={$mode}&user={$user}");
    }
}
if ($action == 'delete' && is_array($del)) {
    foreach ($del as $contact_list_id => $val) {
        cw_user_delete_contact_list($user, $contact_list_id);
function cw_user_send_modification_mail($customer_id, $is_new)
{
    global $config, $smarty;
    $userinfo = cw_user_get_info($customer_id, 65535);
    $fields_area = cw_profile_fields_get_area($customer_id);
    list($profile_sections, $profile_fields, $additional_fields) = cw_profile_fields_get_sections('U', true, $fields_area);
    $smarty->assign('userinfo', $userinfo);
    $smarty->assign('profile_sections', $profile_sections);
    $smarty->assign('profile_fields', $profile_fields);
    $smarty->assign('additional_fields', $additional_fields);
    if ($is_new) {
        if ($config['Email']['eml_signin_notif'] == 'Y') {
            cw_call('cw_send_mail', array($config['Company']['users_department'], $userinfo['email'], 'mail/users/signin_subj.tpl', 'mail/users/signin.tpl'));
        }
        if ($config['Email']['eml_signin_notif_admin'] == 'Y') {
            cw_call('cw_send_mail', array($userinfo['email'], $config['Company']['users_department'], 'mail/users/signin_subj.tpl', 'mail/users/signin_admin.tpl', $config['default_admin_language']));
        }
    } else {
        if ($config['Email']['eml_profile_modified_customer'] == 'Y') {
            cw_call('cw_send_mail', array($config['Company']['users_department'], $userinfo['email'], 'mail/users/modified_subj.tpl', 'mail/users/modified.tpl'));
        }
        if ($config['Email']['eml_profile_modified_admin'] == 'Y') {
            cw_call('cw_send_mail', array($userinfo['email'], $config['Company']['users_department'], 'mail/users/modified_admin_subj.tpl', 'mail/users/modified_admin.tpl'));
        }
    }
}
    }
    $anonymous_email = $pref . '-' . ++$next . '-mailto-' . $anonymous_email . '@' . $host;
    $update_fields['basic']['email'] = $anonymous_email;
    $update_fields['basic']['password2'] = $update_fields['basic']['password'] = cw_user_generate_password();
    $smarty->assign('is_anonymous', $is_anonymous);
    $smarty->assign('real_email', $real_email);
}
$display_antibot = false;
$search =& cw_session_register('search', array());
$smarty->assign('purchased_products', isset($search['purchased_products']) ? $search['purchased_products'] : array());
/*
 * require $app_main_dir.'/include/users/register.php'; {
 */
$fill_error =& cw_session_register('fill_error', array());
$filled_profile =& cw_session_register('filled_profile', array());
$fields_area = cw_profile_fields_get_area($user, $salesman_membership, $self_modification, AREA_TYPE == 'A' ? $usertype : null);
list($profile_sections, $profile_fields, $additional_fields) = cw_profile_fields_get_sections('U', true, $fields_area);
$userphoto = array();
if ($user) {
    $userinfo = cw_call('cw_user_get_info', array($user, 65535));
    $userphoto = cw_call('cw_user_get_avatar', array($user));
}
if ($action == 'update') {
    $fill_error = array();
    $update_fields['address'] = cw_user_address_array($update_fields['address']);
    // make sure we processes array of addresses
    // password is not required for existing user
    if ($mode != 'add') {
        $profile_fields['basic']['password']['is_required'] = 0;
    }
    // handle flags "as_new" and "is_same_address"
$aom_orders[$doc_id] = cw_aom_normalize_after_update($aom_orders[$doc_id], $doc_data);
if (!isset($aom_orders[$doc_id]['info']['use_shipping_cost_alt'])) {
    if ($aom_orders[$doc_id]['info']['shipping_cost'] == $aom_orders[$doc_id]['info']['shipping_cost_alt']) {
        $aom_orders[$doc_id]['info']['use_shipping_cost_alt'] = "N";
    } else {
        $aom_orders[$doc_id]['info']['use_shipping_cost_alt'] = "Y";
    }
}
$smarty->assign('cart_giftcerts', $aom_orders[$doc_id]['giftcerts']);
# user information
cw_load('map', 'profile_fields');
$smarty->assign('countries', cw_map_get_countries());
if (!$aom_orders[$doc_id]['userinfo']['usertype']) {
    $aom_orders[$doc_id]['userinfo']['usertype'] = cw_doc_get_defaulttype($aom_orders[$doc_id]['type']);
}
$fields_area = cw_profile_fields_get_area($aom_orders[$doc_id]['userinfo']['customer_id'], $aom_orders[$doc_id]['userinfo']['membership_id'], 0, $aom_orders[$doc_id]['userinfo']['usertype'], true);
list($profile_sections, $profile_fields, $additional_fields) = cw_profile_fields_get_sections('U', true, $fields_area);
$smarty->assign('profile_sections', $profile_sections);
$smarty->assign('profile_fields', $profile_fields);
$smarty->assign('additional_fields', $additional_fields);
$smarty->assign('cart_customer', $aom_orders[$doc_id]['userinfo']);
$smarty->assign('customer', $doc_data['userinfo']);
$mem_usertype = $aom_orders[$doc_id]['userinfo']['usertype'];
$smarty->assign('memberships', cw_user_get_memberships($mem_usertype));
if ($real_taxes == "Y") {
    global $current_area, $customer_id, $user_account;
    $_saved_data = compact("current_area", "customer_id", "user_account");
    $current_area = $current_area == 'G' ? 'G' : 'C';
    $customer_id = $aom_orders[$doc_id]['userinfo']['customer_id'];
    $user_account = $aom_orders[$doc_id]['userinfo'];
}
# kornev, check the requirements before the checkout
if ($mode == 'checkout') {
    if (cw_is_cart_empty($cart)) {
        cw_header_location('index.php?target=' . $target);
    }
    cw_session_unregister('secure_oid');
    if ($cart['info']['display_subtotal'] < $config['General']['minimal_order_amount'] && $config['General']['minimal_order_amount'] > 0) {
        cw_header_location('index.php?target=error_message&error=min_order');
    }
    if ($config['General']['maximum_order_amount'] > 0 && $cart['info']['display_subtotal'] > $config['General']['maximum_order_amount']) {
        cw_header_location("index.php?target=error_message&max_order");
    }
    if ($config['General']['maximum_order_items'] > 0 && cw_cart_count_items($cart) > $config['General']['maximum_order_items']) {
        cw_header_location("index.php?target=error_message&error=max_items");
    }
    $fields_area = cw_profile_fields_get_area($customer_id, $salesman_membership, 1);
    list($profile_sections, $profile_fields, $additional_fields) = cw_profile_fields_get_sections('U', true, $fields_area);
    # kornev, the web information is not required here.
    if ($customer_id) {
        unset($profile_sections['web']);
    }
    $smarty->assign('userinfo', $userinfo);
    $smarty->assign('profile_fields', $profile_fields);
    $smarty->assign('profile_sections', $profile_sections);
    cw_addons_add_css('customer/checkout/opc.css');
    cw_func_call('cw_checkout_prepare');
}
$giftcerts = !empty($cart['giftcerts']) ? $cart['giftcerts'] : array();
$wcart = cw_func_call('cw_cart_get_warehouses_cart', array('cart' => $cart, 'products' => $products, 'userinfo' => $userinfo));
$smarty->assign('warehouses_cart', $wcart);
if (!cw_is_cart_empty($cart)) {
function cw_doc_get($doc_id, $info_type = 0)
{
    global $tables;
    global $config, $addons;
    global $app_main_dir;
    global $smarty;
    cw_load('warehouse', 'profile_fields');
    $doc = cw_query_first("select * from {$tables['docs']} where doc_id='{$doc_id}'");
    $doc['info'] = cw_query_first("select * from {$tables['docs_info']} where doc_info_id='{$doc['doc_info_id']}'");
    $doc['info']['carrier'] = cw_call('cw_shipping_get_carrier', array($doc['info']['shipping_id']));
    $doc['info']['applied_taxes'] = unserialize($doc['info']['applied_taxes']);
    $doc['info']['extra'] = unserialize($doc['info']['extra']);
    $doc['info']['extras'] = cw_call('cw_doc_get_extras_data', array($doc_id));
    $doc['userinfo'] = cw_query_first("select * from {$tables['docs_user_info']} where doc_info_id='{$doc['doc_info_id']}'");
    $doc['userinfo']['main_address'] = cw_user_get_address(null, $doc['userinfo']['main_address_id']);
    $doc['userinfo']['current_address'] = cw_user_get_address(null, $doc['userinfo']['current_address_id']);
    $fields_area = cw_profile_fields_get_area($doc['userinfo']['customer_id'], $doc['userinfo']['membership_id']);
    list($profile_sections, $profile_fields, $additional_fields) = cw_profile_fields_get_sections('U', true, $fields_area);
    $doc['userinfo']['profile_sections'] = $profile_sections;
    $doc['userinfo']['profile_fields'] = $profile_fields;
    $doc['related_docs'] = cw_doc_get_related($doc_id);
    $doc['settings'] = cw_doc_get_settings($doc['doc_info_id']);
    if ($addons['egoods']) {
        $join .= " left join {$tables['download_keys']} ON {$tables['docs_items']}.item_id={$tables['download_keys']}.item_id AND {$tables['download_keys']}.product_id={$tables['docs_items']}.product_id";
        $fields .= ", {$tables['download_keys']}.download_key, {$tables['download_keys']}.expires";
    }
    $join .= " left join {$tables['products_system_info']} on {$tables['products_system_info']}.product_id={$tables['docs_items']}.product_id";
    $fields .= ", {$tables['products_system_info']}.supplier_customer_id";
    $products = cw_query("select {$tables['products']}.*, {$tables['products']}.productcode as sku, {$tables['docs_items']}.*, IF({$tables['products']}.product_id IS NULL, 'Y', '') as is_deleted, IF({$tables['docs_items']}.product = '', {$tables['products']}.product, {$tables['docs_items']}.product) as product {$fields} FROM {$tables['docs_items']} LEFT JOIN {$tables['products']} ON {$tables['docs_items']}.product_id = {$tables['products']}.product_id {$join} WHERE {$tables['docs_items']}.doc_id='{$doc_id}'");
    $products = cw_doc_translate_products($products, $doc['info']['language']);
    $is_returns = false;
    cw_load('warehouse');
    $gift_doc_ids = cw_doc_get_related_docs($doc_id);
    $gift_doc_ids = array_unique(array_merge($gift_doc_ids, array($doc_id)));
    $giftcerts = cw_query("SELECT * {$gc_add_date} FROM {$tables['giftcerts']} WHERE doc_id in ('" . implode(',', $gift_doc_ids) . "')");
    if (!empty($giftcerts) && $config['General']['use_counties'] == "Y") {
        foreach ($giftcerts as $k => $v) {
            if (!empty($v['recipient_county'])) {
                $giftcerts[$k]['recipient_countyname'] = cw_get_county($v['recipient_county']);
            }
        }
    }
    $doc['giftcerts'] = $giftcerts;
    if ($doc['info']['giftcert_ids']) {
        $doc['info']['applied_giftcerts'] = explode('*', $doc['info']['giftcert_ids']);
        if ($doc['info']['applied_giftcerts']) {
            $tmp = array();
            foreach ($doc['info']['applied_giftcerts'] as $k => $v) {
                if (empty($v)) {
                    continue;
                }
                list($arr['giftcert_id'], $arr['giftcert_cost']) = explode(':', $v);
                $tmp[] = $arr;
            }
            $doc['info']['applied_giftcerts'] = $tmp;
        }
    }
    $doc['is_returns'] = $is_returns;
    if (cw_query_first_cell("select count(*) from {$tables['docs_items']}, {$tables['download_keys']} WHERE {$tables['docs_items']}.doc_id = '{$doc_id}' and {$tables['download_keys']}.item_id = {$tables['docs_items']}.item_id ")) {
        $doc['is_egood'] = 'Y';
    } elseif (cw_query_first_cell("select count(*) from {$tables['docs_items']}, {$tables['products']} WHERE {$tables['docs_items']}.doc_id = '{$doc_id}' and {$tables['docs_items']}.product_id={$tables['products']}.product_id AND {$tables['products']}.distribution != ''")) {
        $doc['is_egood'] = 'E';
    }
    if (preg_match("/(free_ship|percent|absolute)(?:``)(.+)/S", $doc['coupon'], $found)) {
        $doc['coupon'] = $found[2];
        $doc['coupon_type'] = $found[1];
    }
    $order['info']['extra']['tax_info']['product_tax_name'] = '';
    $_products_taxes = array();
    if ($products) {
        foreach ($products as $k => $v) {
            if ($addons['sn']) {
                $v['serial_numbers'] = cw_query("select * from {$tables['docs_items_serials']} where item_id='{$v['item_id']}'");
            }
            $v['product_options_txt'] = $v['product_options'];
            if ($v['extra_data']) {
                $v['extra_data'] = unserialize($v['extra_data']);
                if (is_array(@$v['extra_data']['display'])) {
                    foreach ($v['extra_data']['display'] as $i => $j) {
                        $v["display_" . $i] = $j;
                    }
                }
                if (is_array($v['extra_data']['taxes'])) {
                    foreach ($v['extra_data']['taxes'] as $i => $j) {
                        if ($j['tax_value'] > 0) {
                            $_products_taxes[$i] = $j['tax_display_name'];
                        }
                    }
                }
            }
            $v['original_price'] = $v['ordered_price'] = $v['price'];
            $v['price_deducted_tax'] = "Y";
            if ($v['is_deleted'] != 'Y') {
                $v['original_price'] = cw_query_first_cell("SELECT {$tables['products_prices']}.price FROM {$tables['products_prices']} WHERE {$tables['products_prices']}.product_id = '{$v['product_id']}' AND {$tables['products_prices']}.membership_id IN (0, '{$userinfo['membership_id']}') AND {$tables['products_prices']}.quantity <= '{$v['amount']}' AND {$tables['products_prices']}.variant_id = 0");
                # kornev, TOFIX
                if ($addons['product_options'] && $v['extra_data']['product_options']) {
                    list($variant, $product_options) = cw_get_product_options_data($v['product_id'], $v['extra_data']['product_options'], $userinfo['membership_id']);
                    if ($product_options === false) {
                        unset($product_options);
                    } else {
                        if (empty($variant['price'])) {
                            $variant['price'] = $v['original_price'];
                        }
                        $v['original_price'] = $variant['price'];
                        unset($variant['price']);
                        if ($product_options) {
                            foreach ($product_options as $o) {
                                if ($o['modifier_type'] == '%') {
                                    $v['original_price'] += $v['original_price'] * $o['price_modifier'] / 100;
                                } else {
                                    $v['original_price'] += $o['price_modifier'];
                                }
                            }
                        }
                        $v['product_options'] = $product_options;
                        # Check current and saved product options set
                        if (!empty($v['product_options_txt'])) {
                            $flag_txt = true;
                            # Check saved product options
                            $count = 0;
                            foreach ($v['product_options'] as $opt) {
                                if (preg_match("/" . preg_quote($opt['class'], "/") . ": " . preg_quote($opt['option_name'], "/") . "/Sm", $v['product_options_txt'])) {
                                    $count++;
                                }
                            }
                            if ($count != count($v['product_options'])) {
                                $flag_txt = false;
                            }
                            # Check current product options set
                            if ($flag_txt) {
                                $count = 0;
                                $tmp = explode("\n", $v['product_options_txt']);
                                foreach ($tmp as $txt_row) {
                                    if (!preg_match("/^([^:]+): (.*)\$/S", trim($txt_row), $match)) {
                                        continue;
                                    }
                                    foreach ($v['product_options'] as $opt) {
                                        if ($match[1] == $opt['option_name'] && $match[2] == trim($opt['name'])) {
                                            $count++;
                                            break;
                                        }
                                    }
                                }
                                if ($count != count($tmp)) {
                                    $flag_txt = false;
                                }
                            }
                            # Force display saved product options set
                            # if saved and current product options sets wasn't equal
                            if (!$flag_txt) {
                                $v['force_product_options_txt'] = true;
                            }
                        }
                        if (!empty($variant)) {
                            $v = cw_array_merge($v, $variant);
                        }
                    }
                }
            }
            $products[$k] = $v;
        }
    }
    $doc['products'] = $products;
    if (count($_products_taxes) == 1) {
        $order['info']['extra']['tax_info']['product_tax_name'] = array_pop($_products_taxes);
    }
    if ($order['coupon_type'] == "free_ship") {
        $order['shipping_cost'] = $order['coupon_discount'];
        $order['discounted_subtotal'] += $order['coupon_discount'];
    }
    $order['discounted_subtotal'] = price_format($order['discounted_subtotal']);
    # kornev, 512 is free for now (something was removed)
    if ($info_type & 1024 && $addons['pos']) {
        cw_load('pos');
        $doc['pos'] = cw_pos_get_doc_info($doc['doc_info_id']);
    }
    if ($info_type & 2048) {
        $doc['warehouse'] = cw_warehouse_get_like_user($doc['info']['warehouse_customer_id'], $doc['info']['warehouse_customer_id']);
    }
    if ($info_type & 4096) {
        $doc['quotes'] = cw_doc_get_quotes($doc_id);
    }
    if ($info_type & 8192) {
        $doc['info']['details'] = text_decrypt($doc['info']['details']);
    }
    if ($info_type & 1) {
        $doc['attributes'] = cw_func_call('cw_attributes_get', array('item_id' => $doc_id, 'item_type' => 'O'));
    }
    return $doc;
}
<?php

cw_load('profile_fields', 'check_user_field');
cw_load('ajax');
global $self_modification, $current_area, $usertype, $address_type;
$avail_area = cw_profile_fields_get_area($user, 0, $self_modification, AREA_TYPE == 'A' ? empty($usertype) ? 'C' : $usertype : 'C');
list($profile_sections, $profile_fields) = cw_profile_fields_get_sections('U', true, $avail_area);
$smarty->assign('profile_fields', $profile_fields);
$smarty->assign('is_checkout', $is_checkout);
$user_address =& cw_session_register('user_address', array());
// Declare session var for addresses
// In customer area $user and $customer_id must be equal
// The same also if it is not possible to define user by address_id.
if ($current_area != 'A' || empty($user) && empty($address_id)) {
    $user = $customer_id;
}
// Request main/current address from session
// ?action=load&address_type=main
if (empty($address_id) && ($address_type == 'main' || $address_type == 'current')) {
    $address = cw_user_get_address_by_type($address_type);
    $address_id = intval($address['address_id']);
}
// Request main/current address from address book
// ?action=load&address_id=main[&user=uid]
if (empty($address_type) && ($address_id == 'main' || $address_id == 'current')) {
    //    $address_type = $address_id;
    $address = cw_user_get_address($user, $address_id);
    $address_id = intval($address['address_id']);
}
// Request address by ID from address book without user
// Define user by address
function cw_profile_fields_get_additional($customer_id)
{
    global $tables, $current_language;
    $area = cw_profile_fields_get_area($customer_id);
    return cw_profile_fields_get_sections('U', true, $area);
}