예제 #1
0
function cw_dpi_check_viewers($_addon = null)
{
    global $smarty, $tables, $config;
    if (empty($_addon)) {
        return false;
    }
    $_addon = (string) $_addon;
    $result = true;
    global $addon_skin_dir;
    $addon_skin_dir = end($smarty->template_dir) . DIRECTORY_SEPARATOR . 'addons' . DIRECTORY_SEPARATOR . $_addon . DIRECTORY_SEPARATOR . 'viewers' . DIRECTORY_SEPARATOR;
    $addon_skin_dir = str_replace(array('//', '\\\\'), array('/', '\\'), $addon_skin_dir);
    $available_viewers = glob($addon_skin_dir . '*', GLOB_ONLYDIR | GLOB_NOSORT);
    do {
        if (empty($available_viewers) || !is_array($available_viewers)) {
            $result = false;
            break;
        }
        $_available_viewers = str_replace($addon_skin_dir, '', $available_viewers);
        $_available_viewers = array_flip($_available_viewers);
        foreach ($_available_viewers as $key => $value) {
            $_available_viewers[$key] = $available_viewers[$value];
        }
        $available_viewers = $_available_viewers;
        unset($_available_viewers);
        $available_viewers_str = array_map('cw_dpi_process_items', array_keys($available_viewers));
        $available_viewers_str = implode("\n", cw_addslashes($available_viewers_str));
        $_current_viewer = $config[$_addon]['dpi_images_viewer'];
        if (!isset($available_viewers[$_current_viewer]) || empty($available_viewers[$_current_viewer])) {
            $_current_viewer = array_shift(array_keys($available_viewers));
            db_query("UPDATE {$tables['config']} SET variants = '' WHERE name = 'dpi_theme'");
            $config[$_addon]['dpi_theme'] = null;
        }
        db_query("UPDATE {$tables['config']} SET value = '" . addslashes($_current_viewer) . "', variants = '{$available_viewers_str}' WHERE name = 'dpi_images_viewer'");
        $config[$_addon]['dpi_images_viewer'] = $_current_viewer;
        if (!isset($available_viewers[$_current_viewer]) || empty($available_viewers[$_current_viewer])) {
            $result = false;
            break;
        }
        $viewer_dir = $available_viewers[$_current_viewer] . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR;
        $available_themes = glob($viewer_dir . '*', GLOB_ONLYDIR | GLOB_NOSORT);
        $available_themes_str = null;
        $_current_theme = null;
        if (!empty($available_themes) && is_array($available_themes)) {
            $available_themes = str_replace($viewer_dir, '', $available_themes);
            $available_themes_str = array_map('cw_dpi_process_items', $available_themes);
            $available_themes_str = implode("\n", cw_addslashes($available_themes_str));
            $_current_theme = $config[$_addon]['dpi_theme'];
            if (empty($_current_theme) || !in_array($_current_theme, $available_themes)) {
                $_current_theme = array_shift($available_themes);
            }
        }
        db_query("UPDATE {$tables['config']} SET value = '" . addslashes($_current_theme) . "', variants = '{$available_themes_str}' WHERE name = 'dpi_theme'");
        $config[$_addon]['dpi_theme'] = $_current_theme;
    } while (0);
    return $result;
}
function cw_add_class_data($data, $product_id)
{
    global $tables;
    # Update class data
    $comp = $data['class'];
    $comp['product_id'] = $product_id;
    cw_unset($comp, "product_option_id");
    $comp = cw_addslashes($comp);
    $product_option_id = cw_query_first_cell("SELECT product_option_id FROM {$tables['product_options']} WHERE class = '{$comp['class']}' AND product_id = '{$comp['product_id']}'");
    $is_new = empty($product_option_id);
    if (!empty($product_option_id)) {
        cw_array2update("product_options", $comp, "product_option_id = '{$product_option_id}'");
    } else {
        $product_option_id = cw_array2insert("product_options", $comp);
    }
    # Update class multilanguage data
    db_query("DELETE FROM {$tables['product_options_lng']} WHERE product_option_id = '{$product_option_id}'");
    foreach ($data['product_options_lng'] as $v) {
        $v['product_option_id'] = $product_option_id;
        $v = cw_addslashes($v);
        cw_array2insert("product_options_lng", $v, true);
    }
    # Update class options
    $ids = array();
    foreach ($data['product_options_values'] as $k => $opt) {
        $opt['product_option_id'] = $product_option_id;
        $old_option_id = $opt['option_id'];
        cw_unset($opt, "option_id");
        $opt = cw_addslashes($opt);
        $option_id = cw_query_first_cell("SELECT option_id FROM {$tables['product_options_values']} WHERE product_option_id = '{$product_option_id}' AND name = '{$opt['name']}'");
        if (empty($option_id)) {
            $option_id = cw_array2insert("product_options_values", $opt);
        } else {
            cw_array2update("product_options_values", $opt, "option_id = '{$option_id}'");
        }
        $ids[$old_option_id] = $option_id;
    }
    # Update class option multilanguage data
    db_query("DELETE FROM {$tables['product_options_values_lng']} WHERE option_id = '{$option_id}'");
    foreach ($data['product_options_values_lng'] as $v) {
        if (!isset($ids[$v['option_id']])) {
            continue;
        }
        $v['option_id'] = $ids[$v['option_id']];
        $v = cw_addslashes($v);
        cw_array2insert("product_options_values_lng", $v, true);
    }
    # Detect and delete old product option class options
    $ids = cw_query_column("SELECT option_id FROM {$tables['product_options_values']} WHERE product_option_id = '{$product_option_id}' AND option_id NOT IN ('" . implode("','", $ids) . "')");
    if (!empty($ids)) {
        db_query("DELETE FROM {$tables['product_options_values']} WHERE product_option_id = '{$product_option_id}' AND option_id IN ('" . implode("','", $ids) . "')");
        db_query("DELETE FROM {$tables['product_options_values_lng']} WHERE option_id IN ('" . implode("','", $ids) . "')");
        db_query("DELETE FROM {$tables['products_options_ex']} WHERE option_id IN ('" . implode("','", $ids) . "')");
    }
}
function cw_send_simple_mail($from, $to, $subject, $body, $extra_headers = array(), $files = array())
{
    global $current_language;
    if (empty($to)) {
        return;
    }
    $to = cw_real_mail_address($to);
    $language = $language ? $language : $current_language;
    $_files = implode(",", $files);
    cw_array2insert('mail_spool', cw_addslashes(array('mail_from' => $from, 'mail_to' => $to, 'subject' => $subject, 'body' => $body, 'crypted' => false, 'files' => $_files)));
    return;
}
function dod_modify_bonuses($generator_id)
{
    global $tables, $bonus_names;
    global $dod_bonuses, $dod_bonus;
    if (empty($generator_id)) {
        $GLOBALS['_dod_bonuses'] =& $dod_bonuses;
        cw_session_register('_dod_bonuses');
        $GLOBALS['_dod_bonus'] =& $dod_bonus;
        cw_session_register('_dod_bonus');
        return array(true, null);
        //return array(false, 'generator Id was not provided');
    }
    db_query("DELETE FROM {$tables['dod_bonuses']} WHERE generator_id = '{$generator_id}'");
    db_query("DELETE FROM {$tables['dod_bonus_details']} WHERE generator_id = '{$generator_id}'");
    $available_fields = array('bonus_id' => 'int', 'generator_id' => 'int', 'type' => 'string', 'apply' => 'int', 'coupon' => 'string', 'discount' => 'float', 'disctype' => 'int');
    $excl_from_base_list = array('bonus_id');
    foreach ($excl_from_base_list as $field) {
        if (isset($available_fields[$field])) {
            unset($available_fields[$field]);
        }
    }
    $optional_fields = array('discount', 'disctype');
    $date_fields = array();
    $skip_striptags_fields = array();
    if (empty($dod_bonuses[DOD_DISCOUNT])) {
        $dod_bonuses[DOD_DISCOUNT] = 1;
        $unused_dod_discount_bonus = 1;
    } else {
        $unused_dod_discount_bonus = 0;
    }
    if (empty($dod_bonuses) || !is_array($dod_bonuses)) {
        return array(true, null);
    }
    $available_btypes = array(DOD_DISCOUNT, DOD_FREE_PRODS, DOD_FREE_SHIP, DOD_COUPON);
    $bonuses = array();
    foreach ($dod_bonuses as $bonus_type => $trash) {
        if (!isset($dod_bonus[$bonus_type]) || empty($dod_bonus[$bonus_type]) || !in_array($bonus_type, $available_btypes)) {
            unset($dod_bonuses[$bonus_type]);
        } else {
            $bonuses[$bonus_type] = $dod_bonus[$bonus_type];
        }
    }
    unset($dod_bonus);
    if (empty($dod_bonuses) || empty($bonuses)) {
        return array(true, null);
    }
    $GLOBALS['_dod_bonuses'] =& $dod_bonuses;
    cw_session_register('_dod_bonuses');
    $errors = array();
    $tmp_optional_fields = $optional_fields;
    foreach ($bonuses as $bonus_type => $input_data) {
        $optional_fields = $tmp_optional_fields;
        $additional_lang_data = array();
        $pids = $cids = array();
        $input_data['generator_id'] = $generator_id;
        $input_data['type'] = $bonus_type;
        if ($bonus_type != DOD_COUPON) {
            $input_data['coupon'] = 1;
            if ($input_data['apply'] == DOD_APPLY_PRODS || $bonus_type == DOD_FREE_PRODS || $bonus_type == DOD_DISCOUNT) {
                if (!isset($input_data['products']) && !isset($input_data['cats']) && !isset($input_data['mans']) && !isset($input_data['attr']) || empty($input_data['products']) && empty($input_data['cats']) && empty($input_data['mans']) && empty($input_data['attr'])) {
                    $additional_lang_data = array('bonus' => cw_get_langvar_by_name($bonus_names[$bonus_type]));
                    $errors[] = cw_get_langvar_by_name('msg_dod_bonus_incorrect', $additional_lang_data);
                    continue;
                }
                if (isset($input_data['products']) && !empty($input_data['products'])) {
                    $products_data = array();
                    foreach ($input_data['products'] as $product_data) {
                        $product_data['id'] = trim($product_data['id']);
                        $products_data[$product_data['id']] = $product_data['quantity'];
                    }
                    $pids = array_keys($products_data);
                    $pids = cw_query_column("SELECT product_id as id FROM {$tables['products']} WHERE product_id IN ('" . implode("','", $pids) . "')");
                }
                if (isset($input_data['cats']) && !empty($input_data['cats'])) {
                    $cats_data = array();
                    foreach ($input_data['cats'] as $cat_data) {
                        $cat_data['id'] = trim($cat_data['id']);
                        $cats_data[$cat_data['id']] = $cat_data['quantity'];
                    }
                    $cids = array_keys($cats_data);
                    $cids = cw_query_column("SELECT category_id as id FROM {$tables['categories']} WHERE category_id IN ('" . implode("','", $cids) . "')");
                }
                if (isset($input_data['mans']) && !empty($input_data['mans'])) {
                    $mids = array_values($input_data['mans']);
                }
                if (isset($input_data['attr'])) {
                    // Prepare attributes data
                    if (isset($input_data['attr']) && !empty($input_data['attr'])) {
                        $attr_data = array();
                        foreach ($input_data['attr'] as $a_data) {
                            $attr_data[trim($a_data['attribute_id'])] = array('quantity' => $a_data['quantity'], 'value' => current($a_data['value']), 'operation' => $a_data['operation']);
                        }
                        $attrids = array_keys($attr_data);
                        $attrids = cw_query_column("SELECT attribute_id as id FROM {$tables['attributes']} WHERE attribute_id IN ('" . implode("','", $attrids) . "')");
                    }
                }
                if (empty($pids) && empty($cids) && empty($mids) && empty($attrids)) {
                    $additional_lang_data = array('bonus' => cw_get_langvar_by_name($bonus_names[$bonus_type]));
                    $errors[] = cw_get_langvar_by_name('msg_dod_bonus_incorrect', $additional_lang_data);
                    continue;
                }
            }
        }
        if ($bonus_type != DOD_DISCOUNT && $bonus_type != DOD_FREE_SHIP) {
            $input_data['discount'] = $input_data['disctype'] = null;
        } elseif ($bonus_type == DOD_FREE_SHIP) {
            $input_data['disctype'] = null;
        } else {
            $optional_fields = array();
        }
        if (in_array($bonus_type, array(DOD_FREE_PRODS, DOD_COUPON))) {
            $optional_fields[] = 'apply';
        }
        $data = array();
        foreach ($date_fields as $field) {
            if (isset($input_data[$field]) && !empty($input_data[$field])) {
                $input_data[$field] = cw_core_strtotime($input_data[$field]);
            }
        }
        $error = null;
        foreach ($available_fields as $field => $field_type) {
            if (isset($input_data[$field])) {
                $result = settype($input_data[$field], $field_type);
                if ($result === false) {
                    $error = 'msg_dod_incorrect_field_type';
                    $additional_lang_data = array('field_name' => $field);
                    break;
                }
                if (empty($input_data[$field]) && !($bonus_type == DOD_DISCOUNT && $unused_dod_discount_bonus)) {
                    if (in_array($field, $optional_fields)) {
                        $data[$field] = null;
                    } else {
                        $additional_lang_data = array('bonus' => cw_get_langvar_by_name($bonus_names[$bonus_type]));
                        $error = 'msg_dod_bonus_incorrect';
                        break;
                    }
                } else {
                    if ($field_type == 'string' && !in_array($field, $skip_striptags_fields)) {
                        $input_data[$field] = cw_strip_tags($input_data[$field]);
                    }
                    $data[$field] =& $input_data[$field];
                }
            } else {
                if ($field_type == 'bool') {
                    $data[$field] = 0;
                } else {
                    if (in_array($field, $optional_fields)) {
                        $data[$field] = null;
                    } else {
                        $additional_lang_data = array('bonus' => cw_get_langvar_by_name($bonus_names[$bonus_type]));
                        $error = 'msg_dod_bonus_incorrect';
                        break;
                    }
                }
            }
        }
        if (!empty($error)) {
            $errors[] = cw_get_langvar_by_name($error, $additional_lang_data);
            continue;
        }
        if (empty($data)) {
            continue;
        }
        if ($data['type'] == DOD_DISCOUNT) {
            $data['unused'] = $unused_dod_discount_bonus;
        }
        $bonus_id = cw_array2insert($tables['dod_bonuses'], cw_addslashes($data));
        if ($bonus_type == DOD_FREE_SHIP) {
            foreach ($input_data['methods'] as $trash => $shipping_id) {
                $data = array('generator_id' => $generator_id, 'bonus_id' => $bonus_id, 'object_id' => $shipping_id, 'object_type' => DOD_OBJ_TYPE_SHIPPING);
                cw_array2insert($tables['dod_bonus_details'], cw_addslashes($data));
            }
        }
        if ($bonus_type != DOD_COUPON) {
            if ($input_data['apply'] == DOD_APPLY_PRODS || $bonus_type == DOD_FREE_PRODS || $bonus_type == DOD_DISCOUNT) {
                if (!empty($pids)) {
                    $data = array();
                    $data['generator_id'] = $generator_id;
                    $data['bonus_id'] = $bonus_id;
                    foreach ($pids as $pid) {
                        $data['object_id'] = $pid;
                        $data['object_type'] = DOD_OBJ_TYPE_PRODS;
                        $data['quantity'] = $products_data[$pid];
                        if (empty($data['quantity'])) {
                            $data['quantity'] = 1;
                        }
                        cw_array2insert($tables['dod_bonus_details'], cw_addslashes($data));
                    }
                }
                if (!empty($cids)) {
                    $data = array();
                    $data['generator_id'] = $generator_id;
                    $data['bonus_id'] = $bonus_id;
                    foreach ($cids as $cid) {
                        $data['object_id'] = $cid;
                        $data['object_type'] = DOD_OBJ_TYPE_CATS;
                        $data['quantity'] = $cats_data[$cid];
                        if (empty($data['quantity'])) {
                            $data['quantity'] = 1;
                        }
                        cw_array2insert($tables['dod_bonus_details'], cw_addslashes($data));
                    }
                }
                if (!empty($mids)) {
                    $data = array();
                    $data['generator_id'] = $generator_id;
                    $data['bonus_id'] = $bonus_id;
                    $data['quantity'] = 1;
                    foreach ($mids as $mid) {
                        $data['object_id'] = $mid;
                        $data['object_type'] = DOD_OBJ_TYPE_MANS;
                        cw_array2insert($tables['dod_bonus_details'], cw_addslashes($data));
                    }
                }
                // Save attributes to condition details
                if (!empty($attrids)) {
                    $data = array();
                    $data['generator_id'] = $generator_id;
                    $data['bonus_id'] = $bonus_id;
                    $data['quantity'] = 1;
                    foreach ($attrids as $aid) {
                        $data['object_id'] = $aid;
                        $data['object_type'] = DOD_OBJ_TYPE_ATTR;
                        $data['param1'] = $attr_data[$aid]['value'];
                        $data['param2'] = $attr_data[$aid]['operation'];
                        cw_array2insert($tables['dod_bonus_details'], cw_addslashes($data));
                    }
                }
            }
        }
        unset($bonuses[$bonus_type]);
    }
    if (!empty($bonuses)) {
        $GLOBALS['_dod_bonus'] =& $bonuses;
        cw_session_register('_dod_bonus');
    }
    if (!empty($errors)) {
        $error = implode("<br />\n", $errors);
        return array(false, $error);
    }
    return array(true, null);
}
function cw_paypal_express_detect_state($country, $state, &$err)
{
    global $tables;
    if (empty($state)) {
        return '';
    }
    $state = cw_addslashes($state);
    $country = cw_addslashes($country);
    $state_exists = cw_query_first_cell("SELECT COUNT(*) FROM {$tables['map_states']} WHERE country_code = '{$country}' AND code = '{$state}'") > 0;
    if ($state_exists) {
        return $state;
    }
    $country_data = cw_query_first("SELECT code, display_states FROM {$tables['map_countries']} WHERE code = '{$country}' AND active = 'Y'");
    if (empty($country_data)) {
        $err = 1;
        return '';
    }
    if ($country_data['display_states'] != 'Y') {
        return $state;
    }
    $has_states = cw_query_first_cell("SELECT COUNT(*) FROM {$tables['map_states']} WHERE country_code = '{$country}'") > 0;
    if (!$has_states) {
        return $state;
    }
    $state_code = cw_query_first_cell("SELECT code FROM {$tables['map_states']} WHERE state = '{$state}' AND country_code = '{$country}'");
    if (!empty($state_code)) {
        return $state_code;
    }
    $err = 2;
    return cw_query_first_cell("SELECT code FROM {$tables['mapp_states']} WHERE country_code = '{$country}' LIMIT 1");
}
function cw_group_edit_copy($ge_id, $tbl, $field, $value, $fields = array(), $add_cond = '')
{
    global $tables;
    if (!$ge_id) {
        return;
    }
    if (!$fields) {
        $fields = cw_query_column("show columns from " . $tables[$tbl] . " where Extra != 'auto_increment'", 'Field');
    }
    if ($add_cond) {
        $add_cond = ' and ' . $add_cond;
    }
    $current_record = cw_addslashes(cw_query_first("select * from " . $tables[$tbl] . " where {$field}='{$value}'" . $add_cond));
    unset($current_record[$field]);
    while ($id = cw_group_edit_each($ge_id, 1, $value)) {
        $count = cw_query_first_cell("select count(*) from " . $tables[$tbl] . " where {$field} = '{$id}'" . $add_cond);
        if (!$count) {
            $current_record[$field] = $id;
            $fields[] = $field;
            cw_array2insert($tbl, $current_record, 1, $fields);
        } else {
            cw_array2update($tbl, $current_record, "{$field} = '{$id}'" . $add_cond, $fields);
        }
    }
}
function cw_product_build_flat($params, $return)
{
    extract($params);
    global $tables, $addons;
    $where = "";
    if ($product_id) {
        if (!is_array($product_id)) {
            $product_id = array($product_id);
        }
        $where = "product_id in ('" . implode("', '", $product_id) . "')";
        db_query("delete from {$tables['products_flat']} where {$where}");
    } else {
        db_query("delete from {$tables['products_flat']}");
    }
    $fields = $from_tbls = $query_joins = $where = $groupbys = $having = $orderbys = array();
    $from_tbls[] = 'products';
    $fields[] = "{$tables['products']}.product_id";
    $where[] = $tables['products'] . '.' . $where;
    # kornev, get the query from the addons
    if ($return) {
        foreach ($return as $saname => $sadata) {
            if (isset(${$saname}) && is_array(${$saname}) && empty(${$saname})) {
                ${$saname} = $sadata;
            }
        }
    }
    # kornev, there are nothing to do if the fields are empty (by default)
    if (count($fields) == 1) {
        return;
    }
    $groupbys[] = "{$tables['products']}.product_id";
    # kornev, generate it;
    $search_query = cw_db_generate_query($fields, $from_tbls, $query_joins, $where, $groupbys, $having, $orderbys);
    if ($tick > 0) {
        cw_display_service_header('lbl_rebuild_products_flat');
    }
    $sd = db_query($search_query);
    $updated = 0;
    if ($sd) {
        while ($row = db_fetch_array($sd)) {
            cw_array2insert('products_flat', cw_addslashes($row), true);
            $updated++;
            if ($tick > 0 && $updated % $tick == 0) {
                cw_flush('.');
            }
            if ($tick > 0 && $updated / $tick % 100 == 0) {
                cw_flush('<br/>');
            }
        }
    }
    db_free_result($sd);
    return $updated;
}
function cw_clone_survey($survey_id)
{
    global $tables;
    if (!is_array($survey_id)) {
        $survey_id = array($survey_id);
    }
    $i = 0;
    foreach ($survey_id as $id) {
        $data = cw_query_first("SELECT * FROM {$tables['surveys']} WHERE survey_id = '{$id}'");
        if (empty($data)) {
            continue;
        }
        # Clone survey details
        unset($data['survey_id']);
        $data['survey_type'] = 'D';
        cw_addslashes($data);
        $newid = cw_array2insert("surveys", $data);
        if (!$newid) {
            continue;
        }
        # Clone survey multilanguage variables
        $vars = array("name", "header", "footer", "complete");
        foreach ($vars as $v) {
            $lang = cw_query_hash("SELECT code, value FROM {$tables['languages_alt']} WHERE name = 'survey_" . $v . "_" . $id . "'", "code", false, true);
            if (empty($lang)) {
                continue;
            }
            foreach ($lang as $c => $l) {
                if ($v == 'name') {
                    $l .= " (CLONE)";
                }
                cw_languages_alt_insert("survey_" . $v . "_" . $newid, addslashes($l), $c);
            }
        }
        # Clone survey questions
        $questions = cw_query_hash("SELECT * FROM {$tables['survey_questions']} WHERE survey_id = '{$id}'", "question_id", false);
        if (!empty($questions)) {
            foreach ($questions as $qid => $q) {
                $q = cw_addslashes($q);
                $q['survey_id'] = $newid;
                $newqid = cw_array2insert("survey_questions", $q);
                if (empty($newqid)) {
                    continue;
                }
                # Clone question names
                $lang = cw_query_hash("SELECT code, value FROM {$tables['languages_alt']} WHERE name = 'question_name_" . $qid . "'", "code", false, true);
                if (!empty($lang)) {
                    foreach ($lang as $c => $l) {
                        cw_languages_alt_insert("question_name_" . $newqid, addslashes($l), $c);
                    }
                }
                if ($q['answers_type'] == 'N') {
                    continue;
                }
                # Clone question answers
                $answers = cw_query_hash("SELECT * FROM {$tables['survey_answers']} WHERE question_id = '{$qid}'", "answer_id", false);
                if (empty($answers)) {
                    continue;
                }
                foreach ($answers as $aid => $a) {
                    $a = cw_addslashes($a);
                    $a['question_id'] = $newqid;
                    $newaid = cw_array2insert("survey_answers", $a);
                    if (empty($newaid)) {
                        continue;
                    }
                    # Clone answer names
                    $lang = cw_query_hash("SELECT code, value FROM {$tables['languages_alt']} WHERE name = 'answer_name_" . $aid . "'", "code", false, true);
                    if (!empty($lang)) {
                        foreach ($lang as $c => $l) {
                            cw_languages_alt_insert("answer_name_" . $newaid, addslashes($l), $c);
                        }
                    }
                }
            }
        }
        # Clone events
        $events = cw_query("SELECT * FROM {$tables['survey_events']} WHERE survey_id = '{$id}'");
        if (!empty($events)) {
            foreach ($events as $e) {
                $e['survey_id'] = $newid;
                cw_array2insert("survey_events", $e);
            }
        }
        $i++;
    }
    return $i;
}
function cw_doc_update_item($doc_id, &$product)
{
    global $tables, $addons;
    $product['extra_data'] = cw_call('cw_doc_prepare_doc_item_extra_data', array($product));
    $product['product_options'] = '';
    # kornev, TOFIX
    if ($addons['product_options']) {
        $product['product_options'] = cw_serialize_options($options);
    }
    $product['extra_data'] = serialize($product['extra_data']);
    $query_data = cw_addslashes($product);
    $query_data['doc_id'] = $doc_id;
    $query_data['history_cost'] = cw_call('cw_doc_prepare_doc_item_history_cost', array($product));
    $item = $product['item_id'] = cw_array2insert('docs_items', $query_data, true, array('item_id', 'doc_id', 'product_id', 'product_options', 'amount', 'price', 'history_cost', 'net_price', 'extra_data', 'productcode', 'warehouse_customer_id', 'product', 'discount_formula', 'variant_id', 'is_auto_calc', 'end_price'));
    return $item;
}
function ps_modify_conditions($offer_id)
{
    global $tables, $cond_names;
    global $ps_conditions, $ps_conds;
    if (empty($offer_id)) {
        $GLOBALS['_ps_conditions'] =& $ps_conditions;
        cw_session_register('_ps_conditions');
        $GLOBALS['_ps_conds'] =& $ps_conds;
        cw_session_register('_ps_conds');
        return array(true, null);
        //return array(false, 'Offer Id was not provided');
    }
    db_query("DELETE FROM {$tables['ps_conditions']} WHERE offer_id = '{$offer_id}'");
    db_query("DELETE FROM {$tables['ps_cond_details']} WHERE offer_id = '{$offer_id}'");
    $available_fields = array('cond_id' => 'int', 'offer_id' => 'int', 'type' => 'string', 'coupon' => 'string');
    $excl_from_base_list = array('cond_id');
    foreach ($excl_from_base_list as $field) {
        if (isset($available_fields[$field])) {
            unset($available_fields[$field]);
        }
    }
    $optional_fields = array('coupon');
    $date_fields = array();
    $skip_striptags_fields = array();
    if (empty($ps_conditions) || !is_array($ps_conditions)) {
        return array(true, null);
    }
    $available_ctypes = array(PS_TOTAL, PS_SHIP_ADDRESS, PS_SPEC_PRODUCTS, PS_WEIGHT, PS_MEMBERSHIP, PS_USE_COUPON);
    $conditions = array();
    foreach ($ps_conditions as $cond_type => $trash) {
        if (!isset($ps_conds[$cond_type]) || empty($ps_conds[$cond_type]) || !in_array($cond_type, $available_ctypes)) {
            unset($ps_conditions[$cond_type]);
        } else {
            $conditions[$cond_type] = $ps_conds[$cond_type];
        }
    }
    unset($ps_conds);
    if (empty($ps_conditions) || empty($conditions)) {
        return array(true, null);
    }
    $GLOBALS['_ps_conditions'] =& $ps_conditions;
    cw_session_register('_ps_conditions');
    $errors = array();
    $tmp_optional_fields = $optional_fields;
    foreach ($conditions as $cond_type => $input_data) {
        $optional_fields = $tmp_optional_fields;
        $additional_lang_data = array();
        $pids = $cids = $mids = array();
        $input_data['offer_id'] = $offer_id;
        $input_data['type'] = $cond_type;
        if ($cond_type == PS_SPEC_PRODUCTS) {
            if (!isset($input_data['products']) && !isset($input_data['cats']) && !isset($input_data['mans']) && !isset($input_data['attr']) || empty($input_data['products']) && empty($input_data['cats']) && empty($input_data['mans']) && empty($input_data['attr'])) {
                $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                $errors[] = cw_get_langvar_by_name('msg_ps_cond_incorrect', $additional_lang_data);
                continue;
            }
            // Prepare products data
            if (isset($input_data['products']) && !empty($input_data['products'])) {
                $products_data = array();
                foreach ($input_data['products'] as $product_data) {
                    $product_data['id'] = trim($product_data['id']);
                    $products_data[$product_data['id']] = $product_data['quantity'];
                }
                $pids = array_keys($products_data);
                $pids = cw_query_column("SELECT product_id as id FROM {$tables['products']} WHERE product_id IN ('" . implode("','", $pids) . "')");
            }
            // Prepare categories data
            if (isset($input_data['cats']) && !empty($input_data['cats'])) {
                $cats_data = array();
                foreach ($input_data['cats'] as $cat_data) {
                    $cat_data['id'] = trim($cat_data['id']);
                    $cats_data[$cat_data['id']] = $cat_data['quantity'];
                }
                $cids = array_keys($cats_data);
                $cids = cw_query_column("SELECT category_id as id FROM {$tables['categories']} WHERE category_id IN ('" . implode("','", $cids) . "')");
            }
            // Prepare attributes data
            if (isset($input_data['attr']) && !empty($input_data['attr'])) {
                $attr_data = array();
                foreach ($input_data['attr'] as $a_data) {
                    $attr_data[trim($a_data['attribute_id'])] = array('quantity' => $a_data['quantity'], 'value' => current($a_data['value']), 'operation' => $a_data['operation']);
                }
                $attrids = array_keys($attr_data);
                $attrids = cw_query_column("SELECT attribute_id as id FROM {$tables['attributes']} WHERE attribute_id IN ('" . implode("','", $attrids) . "')");
            }
            // Prepare manufacturers data
            if (isset($input_data['mans']) && !empty($input_data['mans'])) {
                $mans_data = array();
                foreach ($input_data['mans'] as $man_data) {
                    $man_data['id'] = trim($man_data['id']);
                    $mans_data[$man_data['id']] = $man_data['quantity'];
                }
                $mids = array_keys($mans_data);
                $mids = cw_query_column("SELECT manufacturer_id as id FROM {$tables['manufacturers']} WHERE manufacturer_id IN ('" . implode("','", $mids) . "')");
            }
            if (empty($pids) && empty($cids) && empty($mids) && empty($attrids)) {
                $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                $errors[] = cw_get_langvar_by_name('msg_ps_cond_incorrect', $additional_lang_data);
                continue;
            }
        } elseif ($cond_type == PS_SHIP_ADDRESS) {
            if (!isset($input_data['zones']) || empty($input_data['zones'])) {
                $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                $errors[] = cw_get_langvar_by_name('msg_ps_cond_incorrect', $additional_lang_data);
            }
            $zone_ids = array_map(create_function('$id', 'return trim($id);'), $input_data['zones']);
            $zone_ids = cw_query_column("SELECT zone_id as id FROM {$tables['zones']} WHERE zone_id IN ('" . implode("','", $zone_ids) . "')");
            if (empty($zone_ids) || !is_array($zone_ids)) {
                $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                $errors[] = cw_get_langvar_by_name('msg_ps_cond_incorrect', $additional_lang_data);
            }
        } elseif ($cond_type == PS_TOTAL || $cond_type == PS_WEIGHT) {
            if (!isset($input_data['from']) || intval($input_data['from']) < 0 || !isset($input_data['till']) || intval($input_data['till']) < 0 || intval($input_data['till']) < intval($input_data['from']) && intval($input_data['till']) != 0 && intval($input_data['from']) != 0) {
                $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                $errors[] = cw_get_langvar_by_name('msg_ps_cond_incorrect', $additional_lang_data);
            }
        }
        $data = array();
        foreach ($date_fields as $field) {
            if (isset($input_data[$field]) && !empty($input_data[$field])) {
                $input_data[$field] = cw_core_strtotime($input_data[$field]);
            }
        }
        $error = null;
        foreach ($available_fields as $field => $field_type) {
            if (isset($input_data[$field])) {
                $result = settype($input_data[$field], $field_type);
                if ($result === false) {
                    $error = 'msg_ps_incorrect_field_type';
                    $additional_lang_data = array('field_name' => $field);
                    break;
                }
                if (empty($input_data[$field])) {
                    if (in_array($field, $optional_fields)) {
                        $data[$field] = null;
                    } else {
                        $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                        $error = 'msg_ps_cond_incorrect';
                        break;
                    }
                } else {
                    if ($field_type == 'string' && !in_array($field, $skip_striptags_fields)) {
                        $input_data[$field] = cw_strip_tags($input_data[$field]);
                    }
                    $data[$field] =& $input_data[$field];
                }
            } else {
                if ($field_type == 'bool') {
                    $data[$field] = 0;
                } else {
                    if (in_array($field, $optional_fields)) {
                        $data[$field] = null;
                    } else {
                        $additional_lang_data = array('cond' => cw_get_langvar_by_name($cond_names[$cond_type]));
                        $error = 'msg_ps_cond_incorrect';
                        break;
                    }
                }
            }
        }
        if (!empty($error)) {
            $errors[] = cw_get_langvar_by_name($error, $additional_lang_data);
            continue;
        }
        if (empty($data)) {
            continue;
        }
        if ($cond_type == PS_WEIGHT || $cond_type == PS_TOTAL) {
            $data['total'] = intval($input_data['from']);
            $cond_id = cw_array2insert($tables['ps_conditions'], cw_addslashes($data));
            $data['total'] = intval($input_data['till']);
            $cond_id2 = cw_array2insert($tables['ps_conditions'], cw_addslashes($data));
        } else {
            $cond_id = cw_array2insert($tables['ps_conditions'], cw_addslashes($data));
        }
        if ($cond_type == PS_SPEC_PRODUCTS) {
            // Save products to condition details
            if (!empty($pids)) {
                $data = array();
                $data['offer_id'] = $offer_id;
                $data['cond_id'] = $cond_id;
                foreach ($pids as $pid) {
                    $data['object_id'] = $pid;
                    $data['object_type'] = PS_OBJ_TYPE_PRODS;
                    $data['quantity'] = $products_data[$pid];
                    if (empty($data['quantity'])) {
                        $data['quantity'] = 1;
                    }
                    cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
                }
            }
            // Save categories to condition details
            if (!empty($cids)) {
                $data = array();
                $data['offer_id'] = $offer_id;
                $data['cond_id'] = $cond_id;
                foreach ($cids as $cid) {
                    $data['object_id'] = $cid;
                    $data['object_type'] = PS_OBJ_TYPE_CATS;
                    $data['quantity'] = $cats_data[$cid];
                    if (empty($data['quantity'])) {
                        $data['quantity'] = 1;
                    }
                    cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
                }
            }
            // Save attributes to condition details
            if (!empty($attrids)) {
                $data = array();
                $data['offer_id'] = $offer_id;
                $data['cond_id'] = $cond_id;
                foreach ($attrids as $aid) {
                    $data['object_id'] = $aid;
                    $data['object_type'] = PS_OBJ_TYPE_ATTR;
                    $data['quantity'] = $attr_data[$aid]['quantity'];
                    $data['param1'] = $attr_data[$aid]['value'];
                    $data['param2'] = $attr_data[$aid]['operation'];
                    if (empty($data['quantity'])) {
                        $data['quantity'] = 1;
                    }
                    cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
                }
            }
            // Save manufacturers to condition details
            if (!empty($mids)) {
                $data = array();
                $data['offer_id'] = $offer_id;
                $data['cond_id'] = $cond_id;
                foreach ($mids as $mid) {
                    $data['object_id'] = $mid;
                    $data['object_type'] = PS_OBJ_TYPE_MANS;
                    $data['quantity'] = $mans_data[$mid];
                    if (empty($data['quantity'])) {
                        $data['quantity'] = 1;
                    }
                    cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
                }
            }
        } elseif ($cond_type == PS_SHIP_ADDRESS) {
            if (!empty($zone_ids)) {
                $data = array();
                $data['offer_id'] = $offer_id;
                $data['cond_id'] = $cond_id;
                foreach ($zone_ids as $zone_id) {
                    $data['object_id'] = $zone_id;
                    $data['object_type'] = PS_OBJ_TYPE_ZONES;
                    $data['quantity'] = null;
                    cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
                }
            }
        } elseif ($cond_type == PS_TOTAL || $cond_type == PS_WEIGHT) {
            $data = array();
            $data['offer_id'] = $offer_id;
            $data['cond_id'] = $cond_id;
            $data['object_id'] = 0;
            $data['object_type'] = PS_OBJ_TYPE_FROM;
            $data['quantity'] = 0;
            cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
            if (isset($cond_id2)) {
                $data['cond_id'] = $cond_id2;
                $data['object_type'] = PS_OBJ_TYPE_TILL;
                cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
            }
        } elseif ($cond_type == PS_MEMBERSHIP) {
            $data = array();
            $data['offer_id'] = $offer_id;
            $data['cond_id'] = $cond_id;
            $data['object_id'] = $input_data['membership'];
            $data['object_type'] = PS_OBJ_TYPE_MEMBERSHIP;
            $data['quantity'] = 0;
            cw_array2insert($tables['ps_cond_details'], cw_addslashes($data));
        }
        unset($conditions[$cond_type]);
    }
    if (!empty($conditions)) {
        $GLOBALS['_ps_conds'] =& $conditions;
        cw_session_register('_ps_conds');
    }
    if (!empty($errors)) {
        $error = implode("<br />\n", $errors);
        return array(false, $error);
    }
    return array(true, null);
}
예제 #11
0
function tabs_update($tab_type = 'product', $product_id = 0)
{
    global $tables, $top_message, $available_tab_fields, $optional_tab_fields, $skip_striptags_tab_fields;
    global $_pt_addon_tables;
    $product_id = (int) $product_id;
    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
        tabs_redirect($product_id);
    }
    $product_id_condition = null;
    if ($tab_type == 'product') {
        if (empty($product_id)) {
            tabs_redirect();
        }
        $product_id_condition = 'product_id = \'' . $product_id . '\' AND ';
    }
    global $tab_ids, $pt_tabs;
    if (!isset($tab_ids)) {
        $tab_ids = isset($_POST['tab_ids']) ? $_POST['tab_ids'] : array();
    }
    if (!isset($pt_tabs)) {
        $pt_tabs = isset($_POST['tabs']) ? $_POST['tabs'] : array();
    }
    $table = $_pt_addon_tables[$tab_type];
    if (empty($pt_tabs) || !is_array($pt_tabs) || empty($tab_ids)) {
        tabs_redirect($product_id);
    }
    $tab_ids = array_unique(array_map('tabs_process_ids', array_keys($tab_ids)));
    $tab_ids_query = implode('\', \'', $tab_ids);
    $tab_ids = cw_query_column('
        SELECT `tab_id`
        FROM ' . $tables[$table] . '
        WHERE ' . $product_id_condition . 'tab_id IN (\'' . $tab_ids_query . '\')
    ');
    if (empty($tab_ids)) {
        tabs_redirect($product_id);
    }
    if (isset($available_tab_fields['tab_id'])) {
        unset($available_tab_fields['tab_id']);
    }
    $error = null;
    foreach ($tab_ids as $tab_id) {
        $data = array();
        $additional_lang_data = array();
        if (!isset($pt_tabs[$tab_id])) {
            continue;
        }
        foreach ($available_tab_fields as $field => $field_type) {
            if (isset($pt_tabs[$tab_id][$field])) {
                $result = settype($pt_tabs[$tab_id][$field], $field_type);
                if ($result === false) {
                    $error = 'msg_pt_incorrect_field_type';
                    $additional_lang_data = array('field_name' => $field . ' tab ID: ' . $tab_id);
                    break 2;
                }
                if (empty($pt_tabs[$tab_id][$field])) {
                    if (in_array($field, $optional_tab_fields)) {
                        $data[$field] = null;
                    }
                } else {
                    if ($field_type == 'string' && !in_array($field, $skip_striptags_tab_fields)) {
                        $pt_tabs[$tab_id][$field] = cw_strip_tags($pt_tabs[$tab_id][$field]);
                    }
                    $data[$field] =& $pt_tabs[$tab_id][$field];
                }
            } else {
                if ($field_type == 'bool') {
                    $data[$field] = 0;
                }
            }
        }
        if (!empty($data)) {
            cw_array2update($table, cw_addslashes($data), $product_id_condition . 'tab_id = \'' . $tab_id . '\'');
        }
    }
    $top_message = array('content' => cw_get_langvar_by_name('msg_pt_updated_succes'), 'type' => 'I');
    if (!empty($error)) {
        $top_message = array('content' => cw_get_langvar_by_name($error, $additional_lang_data), 'type' => 'E');
    }
    tabs_redirect($product_id);
}
                     cw_array2update('product_variants', array('def' => ''), "product_id = '{$pid}'");
                     cw_array2update('product_variants', array('def' => 'Y'), "product_id = '{$pid}' and variant_id='{$vid}'");
                 }
             }
         }
     }
 }
 if (!empty($def_variant)) {
     cw_array2update('product_variants', array('def' => ''), "product_id = '{$product_id}'");
     cw_array2update('product_variants', array("def" => 'Y'), "product_id = '{$product_id}' and variant_id='{$def_variant}'");
 }
 if (is_array($vids) && cw_image_check_posted($file_upload_data['products_images_var'])) {
     $vids = array_keys($vids);
     $vid = array_shift($vids);
     $image_id = cw_image_save($file_upload_data['products_images_var'], array('id' => $vid));
     $res = cw_addslashes(cw_query_first("select * from {$tables['products_images_var']} where image_id = '{$image_id}' limit 1"));
     unset($res['image_id']);
     if ($res) {
         foreach ($vids as $vid) {
             $res['id'] = $vid;
             cw_image_delete($vid, 'products_images_var');
             cw_array2insert('products_images_var', $res);
         }
     }
     if ($ge_id && $fields['variants']) {
         while ($pid = cw_ge_each($ge_id, 1, $product_id)) {
             $res['id'] = cw_variants_get_same($v, $pid);
             if (empty($res['id'])) {
                 continue;
             }
             cw_image_delete($res['id'], 'products_images_var');
function cw_image_save(&$image_posted, $added_data = array(), $_image_id = NULL)
{
    global $tables, $available_images, $skip_image, $config;
    $type = $image_posted['type'];
    $image_data = cw_image_prepare($image_posted);
    if ($available_images[$type]['max_width']) {
        cw_image_resize($image_data, $available_images[$type]['max_width']);
    } elseif ($config['Appearance']['size_user_avatar'] && $type == 'customers_images') {
        cw_image_resize($image_data, $config['Appearance']['size_user_avatar']);
    }
    if (!empty($added_data)) {
        $image_data = cw_array_merge($image_data, $added_data);
    }
    if (!$image_data || !$image_data['id']) {
        return false;
    }
    if ($skip_image[$type] == 'Y') {
        if (!empty($image_posted['is_copied'])) {
            @unlink($image_posted['file_path']);
        }
        unset($image_posted);
        return false;
    }
    $image_data['date'] = cw_core_get_time();
    $image_data = cw_addslashes($image_data);
    unset($image_posted);
    $_table = $tables[$type];
    if ($available_images[$type]['type'] == 'U') {
        cw_image_delete($image_data['id'], $type);
    }
    return cw_array2insert($type, $image_data);
}
ini_set('session.bug_compat_42', 1);
ini_set('session.bug_compat_warn', 0);
$__quotes_qpc = get_magic_quotes_gpc();
if (!defined('APP_EXT_ENV')) {
    global $reject;
    $reject = array_keys(get_defined_vars());
    if (isset($_COOKIE['is_robot']) && $_COOKIE['is_robot']) {
        define('IS_ROBOT', 1);
    }
    # kornev, strip, check and make global
    foreach (array('_GET', '_POST', '_COOKIE', '_SERVER') as $__avar) {
        if (!$__quotes_qpc) {
            $GLOBALS[$__avar] = cw_addslashes($GLOBALS[$__avar]);
        } elseif (defined('CW_MAGIC_QUOTES_SYBASE')) {
            $GLOBALS[$__avar] = cw_stripslashes_sybase($GLOBALS[$__avar]);
            $GLOBALS[$__avar] = cw_addslashes($GLOBALS[$__avar]);
        } else {
            $GLOBALS[$__avar] = cw_addslashes_keys($GLOBALS[$__avar]);
        }
        foreach ($GLOBALS[$__avar] as $__var => $__res) {
            if (cw_allowed_var($__var)) {
                global ${$__var};
                // Only admin scripts and "html_*" vars are trusted until addons init.
                // Add your html_* vars into $cw_trusted_variables list in addon init to allow HTML tags
                if (APP_AREA != 'admin' && strpos($__var, 'html_') !== 0) {
                    $__res = cw_strip_tags($__res);
                }
                $GLOBALS[$__avar][$__var] = ${$__var} = $request_prepared[$__var] = $__res;
            } else {
                cw_unset($GLOBALS[$__avar], $__var);
            }
function cw_product_options_clone($product_id)
{
    global $tables, $addons, $config;
    $new_product_id = cw_get_return();
    cw_core_copy_tables('product_options_js', 'product_id', $product_id, $new_product_id);
    $hash = array();
    $classes = cw_query("SELECT * FROM {$tables['product_options']} WHERE product_id = '{$product_id}'");
    if (!empty($classes)) {
        foreach ($classes as $v) {
            $options = cw_query("SELECT * FROM {$tables['product_options_values']} WHERE product_option_id = '{$v['product_option_id']}'");
            $old_classid = $v['product_option_id'];
            unset($v['product_option_id']);
            $v['product_id'] = $new_product_id;
            $v = cw_addslashes($v);
            $classid = cw_array2insert('product_options', $v);
            if ($options) {
                foreach ($options as $o) {
                    $old_optionid = $o['option_id'];
                    unset($o['option_id']);
                    $o['product_option_id'] = $classid;
                    $o = cw_addslashes($o);
                    $optionid = cw_array2insert('product_options_values', $o);
                    $hash[$old_optionid] = $optionid;
                    cw_core_copy_tables('product_options_values_lng', 'option_id', $old_optionid, $optionid);
                }
            }
            cw_core_copy_tables('product_options_lng', 'product_option_id', $old_classid, $classid);
        }
    }
    // Clone product option exceptions
    if (!empty($hash)) {
        $hash_ex = array();
        $exceptions = cw_query("SELECT * FROM {$tables['products_options_ex']} WHERE option_id IN ('" . implode("','", array_keys($hash)) . "')");
        if (!empty($exceptions)) {
            foreach ($exceptions as $v) {
                if (empty($hash[$v['option_id']])) {
                    continue;
                }
                $v['option_id'] = $hash[$v['option_id']];
                if (empty($hash_ex[$v['exception_id']])) {
                    $hash_ex[$v['exception_id']] = cw_query_first_cell("SELECT MAX(exception_id) FROM {$tables['product_options_ex']}") + 1;
                }
                $v['exception_id'] = $hash_ex[$v['exception_id']];
                cw_array2insert('products_options_ex', $v);
            }
        }
        unset($hash_ex);
    }
    // Clone product option variants
    $variants = db_query("SELECT * FROM {$tables['product_variants']} WHERE product_id = '{$product_id}' ORDER BY variant_id");
    if ($variants) {
        while ($v = db_fetch_array($variants)) {
            $old_variantid = $v['variant_id'];
            $v['product_id'] = $new_product_id;
            unset($v['variant_id']);
            $v['productcode'] = cw_product_generate_sku();
            if ($addons['barcode'] && $config['barcode']['gen_product_code']) {
                $v['eancode'] = cw_product_generate_sku($config['barcode']['gen_product_code'], 'eancode');
            } else {
                $v['eancode'] = cw_product_generate_sku(0, 'eancode');
            }
            //cw_ean_clear($v['eancode']);
            $v = cw_addslashes($v);
            $variantid = cw_array2insert('product_variants', $v);
            // Add Variant items
            $items = cw_query("SELECT option_id FROM {$tables['product_variant_items']} WHERE variant_id = '{$old_variantid}'");
            if (!empty($items)) {
                foreach ($items as $i) {
                    if (isset($hash[$i['option_id']])) {
                        db_query("INSERT INTO {$tables['product_variant_items']} (variant_id, option_id) VALUES ('{$variantid}', '" . $hash[$i['option_id']] . "')");
                    }
                }
            }
            // warehouse
            if ($addons['warehouse']) {
                $items = cw_query("SELECT * FROM {$tables['products_warehouses_amount']} WHERE variant_id = '{$old_variantid}' AND product_id = '{$product_id}'");
                if (!empty($items)) {
                    foreach ($items as $i) {
                        db_query("INSERT INTO {$tables['products_warehouses_amount']} (product_id, warehouse_customer_id, avail, avail_ordered, avail_sold, avail_reserved, variant_id) VALUES ('{$new_product_id}', '" . $i['warehouse_customer_id'] . "', '" . $i['avail'] . "', '" . $i['avail_ordered'] . "', '" . $i['avail_sold'] . "', '" . $i['avail_reserved'] . "', '{$variantid}')");
                    }
                }
            }
            // Add Variant prices
            $prices = cw_query("SELECT * FROM {$tables['products_prices']} WHERE variant_id = '{$old_variantid}' AND product_id = '{$product_id}'");
            if ($prices) {
                foreach ($prices as $p) {
                    unset($p['price_id']);
                    $p['variant_id'] = $variantid;
                    $p['product_id'] = $new_product_id;
                    cw_array2insert('products_prices', $p);
                }
            }
            // Add Variant thumbnails & variant images
            cw_core_copy_tables('products_images_var', 'id', $old_variantid, $variantid);
        }
        db_free_result($variants);
    }
    return $new_product_id;
}
      </GetExpressCheckoutDetailsRequest>
    </GetExpressCheckoutDetailsReq>
  </soap:Body>
</soap:Envelope>
EOT;
    $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"));