function import_products($xcart_conf)
{
    extract($xcart_conf);
    extract(cw_vers_diff_attr($conn));
    fout("<br /><br />Import of manufacturers, Products, Categories...<br />");
    $cats = cw_table_copy_from_xcart_lit_sn($conn, 'categories', "categoryid as category_id, parentid as parent_id, if(avail='Y',1,0) as status, category, description, order_by");
    cw_table_copy_from_xcart_lit($conn, 'xcart_categories', 'cw_categories_stats', "categoryid as category_id, views_stats");
    cw_table_copy_from_xcart_lit_sn($conn, 'categories_lng', "UPPER(code) as code, categoryid as category_id, category, description");
    cw_table_copy_from_xcart_lit_sn($conn, 'products_categories', "categoryid as category_id, productid as product_id,\n\t\t  if(main='Y',1,0) as main, orderby");
    $prods = cw_table_copy_from_xcart_comm_sn($conn, 'products', "productid as product_id, if(forsale='Y',1,0) as status");
    cw_table_copy_from_xcart_lit($conn, 'xcart_products', 'cw_products_stats', "productid as product_id, views_stats, sales_stats, del_stats");
    cw_table_copy_from_xcart_lit($conn, 'xcart_products', 'cw_products_warehouses_amount', "productid as product_id, avail");
    cw_table_copy_from_xcart_lit_sn($conn, 'products_lng', "UPPER(code) as code, productid as product_id, product,descr,fulldescr,keywords");
    db_query("TRUNCATE TABLE cw_products_prices");
    $sql = "select priceid as price_id, prc.productid as product_id, quantity, price, list_price,\n\t\tvariantid as variant_id, membershipid as membership_id from xcart_pricing prc\n\t\tleft join xcart_products prd on prc.productid=prd.productid order by product_id, variant_id";
    $prices = cw_query($sql, $conn);
    foreach ($prices as $arr) {
        cw_array2insert_esc('cw_products_prices', $arr);
    }
    // Memberships
    db_query("Delete from cw_memberships where membership_id!=0");
    $mbrsh = cw_query("select * from xcart_memberships  where membershipid!=0", $conn);
    $level = cw_query_first_cell("select level from cw_access_levels  where membership_id=0 and area='A'");
    db_query("Delete from cw_access_levels where membership_id!=0");
    foreach ($mbrsh as $v) {
        extract($v);
        if ($area == 'A' || $area == 'P') {
            $area = 'A';
        } else {
            $area = 'C';
        }
        $membership_id = $membershipid;
        $show_prices = 1;
        cw_array2insert_esc('cw_memberships', compact('membership_id', 'area', 'membership', 'active', 'orderby', 'flag', 'show_prices'));
        if ($area == 'A') {
            cw_array2insert_esc('cw_access_levels', compact('membership_id', 'area', 'level'));
        }
    }
    db_query("Delete from cw_memberships_lng where membership_id!=0");
    $mbrshl = cw_query("select * from xcart_memberships_lng  where membershipid!=0", $conn);
    foreach ($mbrshl as $v) {
        extract($v);
        $membership_id = $membershipid;
        $code = strtoupper($code);
        cw_array2insert_esc('cw_memberships_lng', compact('membership_id', 'code', 'membership'));
    }
    cw_table_copy_from_xcart_lit($conn, 'xcart_products', 'cw_products_memberships', "productid as product_id, 0 as memberships_id");
    cw_table_copy_from_xcart_lit($conn, 'xcart_categories', 'cw_categories_memberships', "categoryid as category_id, 0 as memberships_id");
    $catmem = cw_query("select * from xcart_category_memberships", $conn);
    $prodmem = cw_query("select * from xcart_product_memberships", $conn);
    foreach ($catmem as $v) {
        extract($v);
        $membership_id = $membershipid;
        $category_id = $categoryid;
        cw_array2insert('cw_categories_memberships', compact('category_id', 'membership_id'));
    }
    foreach ($prodmem as $v) {
        extract($v);
        $membership_id = $membershipid;
        $product_id = $productid;
        cw_array2insert('cw_products_memberships', compact('product_id', 'membership_id'));
    }
    // manufacturers
    $mans = cw_table_copy_from_xcart_comm_sn($conn, 'manufacturers', "manufacturerid as manufacturer_id,\n\t\tprovider as warehouse_customer_id, if(avail='Y',1,0) as avail");
    cw_table_copy_from_xcart_comm_sn($conn, 'manufacturers_lng', 'manufacturerid as manufacturer_id, UPPER(code) as code');
    // Set Attributes: 0 for domains; Meta Tags, manufacturer_id for products, etc.
    if ($withcurls) {
        $tmp = cw_query("SELECT * from xcart_clean_urls", $conn);
        $clean_urls = array();
        foreach ($tmp as $v) {
            extract($v);
            $clean_urls[$resource_type][$resource_id] = str_replace('.', '-', $clean_url);
        }
    } else {
        $tmp = cw_query("Select 'C' as type, categoryid as id, LCASE(category) as name from xcart_categories\n\t\t\tunion Select 'P' as type, productid as id, LCASE(product) as name from xcart_products\n\t\t\tunion Select 'M' as type, manufacturerid as id, LCASE(manufacturer) as name from xcart_manufacturers", $conn);
        $clean_urls = array();
        $curls = array();
        foreach ($tmp as $v) {
            extract($v);
            $cu = preg_replace("'[^a-z0-9]+'s", ' ', $name);
            $cu = str_replace(' ', '-', $cu);
            $i = 1;
            $curl = $cu;
            while (in_array($curl, $curls)) {
                $curl = $cu . '-' . $i++;
            }
            $clean_urls[$type][$id] = $curl;
            $curls[] = $curl;
        }
        unset($curls);
    }
    unset($tmp);
    if ($pwithmeta) {
        $prodmeta = cw_query("Select productid as id, 'P' as type, product, manufacturerid,\n\t\t  meta_description, meta_keywords, title_tag, add_date from xcart_products", $conn);
    } else {
        $prodmeta = cw_query("Select productid as id, 'P' as type, product, descr,\n\t\t\tmanufacturerid, add_date from xcart_products", $conn);
        foreach ($prodmeta as $k => $v) {
            extract($v);
            $prodmeta[$k]['title_tag'] = trim($product);
            $descr = preg_replace("'</?[a-z]+.*?>'is", " ", $descr);
            $descr = trim(preg_replace("'\\s+'", " ", $descr));
            $prodmeta[$k]['meta_description'] = $descr;
            $mk = preg_replace("'[^a-z]+'is", " ", $product);
            $prodmeta[$k]['meta_keywords'] = str_replace(' ', ', ', trim($mk));
        }
    }
    db_query("TRUNCATE TABLE cw_products_system_info");
    $admin = cw_query_first_cell("select customer_id from cw_customers where usertype='A' limit 1");
    foreach ($prodmeta as $v) {
        extract($v);
        cw_set_attr($type, 'domains', $id, 0);
        cw_set_attr($type, 'meta_title', $id, $title_tag);
        cw_set_attr($type, 'meta_keywords', $id, $meta_keywords);
        cw_set_attr($type, 'meta_description', $id, $meta_description);
        if (isset($clean_urls[$type]) && isset($clean_urls[$type][$id])) {
            cw_set_attr($type, 'clean_url', $id, $clean_urls[$type][$id]);
        }
        cw_set_attr($type, 'manufacturer_id', $id, $manufacturerid);
        $product_id = $id;
        $creation_customer_id = $modification_customer_id = $admin;
        $creation_date = $modification_date = $add_date;
        cw_array2insert('cw_products_system_info', compact('product_id', 'creation_customer_id', 'creation_date', 'modification_customer_id', 'modification_date'));
    }
    if ($cwithmeta) {
        $catmeta = cw_query("Select categoryid as id, 'C' as type, category,\n\t\t  meta_description, meta_keywords, title_tag from xcart_categories", $conn);
    } else {
        foreach ($cats as $k => $v) {
            extract($v);
            $catmeta[$k]['title_tag'] = trim($category);
            $descr = preg_replace("'</?[a-z]+.*?>'is", " ", $description);
            $descr = trim(preg_replace("'\\s+'", " ", $descr));
            $catmeta[$k]['meta_description'] = $descr;
            $mk = preg_replace("'[^a-z]+'is", " ", $category);
            $catmeta[$k]['meta_keywords'] = str_replace(' ', ', ', trim($mk));
            $catmeta[$k]['type'] = 'C';
            $catmeta[$k]['id'] = $category_id;
        }
    }
    foreach ($catmeta as $v) {
        extract($v);
        cw_set_attr($type, 'domains', $id, 0);
        cw_set_attr($type, 'meta_title', $id, $title_tag);
        cw_set_attr($type, 'meta_keywords', $id, $meta_keywords);
        cw_set_attr($type, 'meta_description', $id, $meta_description);
        if (isset($clean_urls[$type]) && isset($clean_urls[$type][$id])) {
            cw_set_attr($type, 'clean_url', $id, $clean_urls[$type][$id]);
        }
    }
    if ($cwithmeta) {
        $manmeta = cw_query("Select manufacturerid as id, 'M' as type, manufacturer,\n\t\t  meta_description, meta_keywords, title_tag from xcart_manufacturers", $conn);
    } else {
        foreach ($mans as $k => $v) {
            extract($v);
            $manmeta[$k]['title_tag'] = trim($manufacturer);
            $descr = preg_replace("'</?[a-z]+.*?>'is", " ", $descr);
            $descr = trim(preg_replace("'\\s+'", " ", $descr));
            $manmeta[$k]['meta_description'] = $descr;
            $mk = preg_replace("'[^a-z]+'is", " ", $manufacturer);
            $manmeta[$k]['meta_keywords'] = str_replace(' ', ', ', trim($mk));
            $manmeta[$k]['type'] = 'M';
            $manmeta[$k]['id'] = $manufacturer_id;
        }
    }
    if (is_array($manmeta)) {
        foreach ($manmeta as $v) {
            extract($v);
            cw_set_attr($type, 'domains', $id, 0);
            cw_set_attr($type, 'meta_title', $id, $title_tag);
            cw_set_attr($type, 'meta_keywords', $id, $meta_keywords);
            cw_set_attr($type, 'meta_description', $id, $meta_description);
            if (isset($clean_urls[$type]) && isset($clean_urls[$type][$id])) {
                cw_set_attr($type, 'clean_url', $id, $clean_urls[$type][$id]);
            }
        }
    }
    // Grab categories, products (Thumbnails & Detailed), and manufacturers related Images
    cw_imgs_copy($xcart_conf, 'xcart_images_C', 'cw_categories_images_thumb', 'categories_images_thumb');
    cw_imgs_copy($xcart_conf, 'xcart_images_T', 'cw_products_images_thumb', 'products_images_thumb', 70);
    cw_imgs_copy($xcart_conf, 'xcart_images_P', 'cw_products_images_det', 'products_images_det');
    cw_imgs_copy($xcart_conf, 'xcart_images_D', 'cw_products_detailed_images', 'products_detailed_images', 800);
    cw_imgs_copy($xcart_conf, 'xcart_images_M', 'cw_manufacturer_images', 'manufacturer_images', 150);
    // Variants -- Classes
    cw_table_copy_from_xcart_comm($conn, 'xcart_variants', 'cw_product_variants', "variantid as variant_id, productid as product_id, concat(productcode,variantid) as eancode");
    cw_table_copy_from_xcart_lit($conn, 'xcart_variant_items', 'cw_product_variant_items', "optionid as option_id, variantid as variant_id");
    // Variants -- available in stock -- adding to cw_products_warehouses_amount,
    // where we alredy have 'avail' field for variants equal 0 (for prods without variants)
    $var_prod_avail = cw_query("Select variantid as variant_id, productid as product_id, avail from xcart_variants", $conn);
    foreach ($var_prod_avail as $v) {
        cw_array2insert('cw_products_warehouses_amount', $v);
    }
    cw_table_copy_from_xcart_comm($conn, 'xcart_classes', 'cw_product_options', "classid as product_option_id, class as field,\n\t\tclasstext as name, productid as product_id, if(avail='Y',1,0) as avail, is_modifier as type");
    cw_table_copy_from_xcart_comm($conn, 'xcart_class_options', 'cw_product_options_values', "classid as product_option_id, option_name as name, if(avail='Y',1,0) as avail,\n\t\t    optionid as option_id, if(modifier_type='\$',0,1) as modifier_type");
    cw_table_copy_from_xcart_lit($conn, 'xcart_class_lng', 'cw_product_options_lng', "UPPER(code) as code, classid as product_option_id, classtext as name");
    db_query("TRUNCATE TABLE cw_product_options_values_lng");
    db_query("TRUNCATE TABLE cw_customers_warehouses");
    // Product Links ---
    cw_table_copy_from_xcart_comm($conn, 'xcart_product_links', 'cw_linked_products', "productid1 as product_id, productid2 as linked_product_id, 1 as active");
    // Featured Products
    cw_table_copy_from_xcart_lit_sn($conn, 'featured_products', "productid as product_id, 0 as category_id,\n\t\tproduct_order, if(avail='Y',1,0) as avail");
    // Product review, ratings
    cw_table_copy_from_xcart_comm($conn, 'xcart_product_reviews', 'cw_products_reviews', "productid as product_id");
    cw_table_copy_from_xcart_comm($conn, 'xcart_product_votes', 'cw_products_votes', "productid as product_id, vote_value/20 as vote_value");
    // Taxes, tax rates
    cw_table_copy_from_xcart_comm_sn($conn, 'taxes', "taxid as tax_id, if(active='Y',1,0) as active,\n\t\t  if(price_includes_tax='Y',1,0) as price_includes_tax, if(display_including_tax='Y',1,0) as display_including_tax");
    cw_table_copy_from_xcart_comm_sn($conn, 'tax_rates', "rateid as rate_id, taxid as tax_id, zoneid as zone_id,\n\t\t  0 as wherehouse_customer_id");
    db_query("DELETE from cw_languages_alt where name rlike '^tax\\_[0-9]+\$'");
    cw_import_config($conn);
    // Menu -- Make top categories featured -- and add 8 top categories (with subcategories) to Top Menu
    db_query("Update cw_categories set featured=1 where parent_id=0 and status=1");
    db_query("Update cw_categories set tm_active=1 where parent_id!=0");
    $ids = cw_query_first_cell("select group_concat(category_id) as cids from\n\t\t(select category_id from cw_categories where parent_id=0 and status=1 order by order_by limit 8) cat");
    db_query("Update cw_categories set tm_active=1 where category_id in ({$ids})");
    cw_cat_tree_building();
    db_query("TRUNCATE TABLE cw_categories_subcount");
    cw_recalc_subcat_count(0);
}
function cw_csv2table($fn, $delimiter = '')
{
    global $tables;
    $h = fopen($fn, 'r');
    $tab = '';
    $fields = '';
    $brf_err = cw_get_langvar_by_name('lbl_imp_err_br_file');
    $wrtab_err = cw_get_langvar_by_name('lbl_imp_err_tab_not_exst');
    while ($tab == '') {
        $tab = trim(fgets($h));
    }
    if (!preg_match("'^\\[[^\\s]*\\]\$'i", $tab)) {
        return $brf_err;
    }
    $tab = preg_replace("'^\\[(.*)\\]\$'i", "\$1", $tab);
    if (!isset($tables[$tab])) {
        return $wrtab_err;
    }
    while ($fields == '') {
        $fields = trim(fgets($h));
    }
    if (!preg_match("'^\\![^\\s\\!]+\\!.+'i", $fields)) {
        return $brf_err;
    }
    if ($delimiter == '') {
        $delimiter = preg_replace("'^\\![^\\s\\!]+(.)\\!.+'", "\$1", $fields);
    }
    $fields = explode($delimiter, $fields);
    foreach ($fields as $k => $v) {
        if (!preg_match("'^\\![^\\s]+\$'i", trim($v))) {
            return $brf_err;
        }
        $v = preg_replace("'^\\!(.*)\$'", "\$1", trim($v));
        $fields[$k] = $v;
    }
    while (($data = fgetcsv($h, 0, $delimiter)) !== false) {
        if (sizeof($data) != sizeof($fields)) {
            return $brf_err;
        }
        foreach ($fields as $k => $v) {
            $data2[$v] = $data[$k];
        }
        $arr[] = $data2;
    }
    db_query("TRUNCATE TABLE {$tables[$tab]}");
    foreach ($arr as $data) {
        cw_array2insert_esc($tab, $data);
    }
    return true;
}