<?php

require_once "conf.php";
require_once "session.inc";
$pricewatch = session_id();
if ($pricewatch == "") {
    HEADER("Location: " . link_s("index.php"));
}
if ($asin_fehler == '1') {
    $asin = $link_asin;
    session_register("asin");
    HEADER("Location: " . link_s("produkte_neu2.php"));
    exit;
}
if ($link_asin != "") {
    if ($asin = asin_search($link_asin)) {
        session_register("asin");
        HEADER("Location: " . link_s("produkte_neu2.php"));
        exit;
    } else {
        $asin_fehler = 1;
    }
}
session_unregister("asin");
$text = $head;
$text .= $body;
$text .= menu("");
$text .= main();
$text .= $foot;
echo gzipoutput($text);
// ===========================================================================
function addProduct()
{
    global $mainframe, $_POST, $database, $my;
    //STEP 3
    //store in database
    if (isset($_POST["asin"])) {
        $_POST["asin"] = stripslashes($_POST["asin"]);
        $_POST["amazon_price"] = stripslashes($_POST["amazon_price"]);
        $_POST["amazon_new_price"] = stripslashes($_POST["amazon_new_price"]);
        $_POST["amazon_used_price"] = stripslashes($_POST["amazon_used_price"]);
        $_POST["amazon_remind"] = stripslashes($_POST["amazon_remind"]);
        $_POST["amazon_3rd_new_remind"] = stripslashes($_POST["amazon_3rd_new_remind"]);
        $_POST["amazon_3rd_used_remind"] = stripslashes($_POST["amazon_3rd_used_remind"]);
        $_POST["runtime"] = trim($_POST["runtime"]);
        $_POST["category_name"] = trim($_POST["category_name"]);
        $_POST["name"] = trim($_POST["name"]);
        $_POST["amazon_remind"] = trim($_POST["amazon_remind"]);
        $_POST["amazon_3rd_new_remind"] = trim($_POST["amazon_3rd_new_remind"]);
        $_POST["amazon_3rd_used_remind"] = trim($_POST["amazon_3rd_used_remind"]);
        $chars = array(",", ".");
        $_POST["amazon_remind"] = str_replace($chars, "", $_POST["amazon_remind"]);
        $_POST["amazon_3rd_new_remind"] = str_replace($chars, "", $_POST["amazon_3rd_new_remind"]);
        $_POST["amazon_3rd_used_remind"] = str_replace($chars, "", $_POST["amazon_3rd_used_remind"]);
        //if not check partner => NULL
        //if check parter && checker everytime => 2147483647
        //else change nothing
        if ($_POST["check_amazon"]) {
            if ($_POST["amazon_remind_everytime"] || !isDigital($_POST["amazon_remind"])) {
                $_POST["amazon_remind"] = 2147483647;
            }
        } else {
            $_POST["amazon_remind"] = "NULL";
        }
        if ($_POST["check_amazon_3rd_new"]) {
            if ($_POST["amazon_3rd_new_remind_everytime"] || !isDigital($_POST["amazon_3rd_new_remind"])) {
                $_POST["amazon_3rd_new_remind"] = 2147483647;
            }
        } else {
            $_POST["amazon_3rd_new_remind"] = "NULL";
        }
        if ($_POST["check_amazon_3rd_used"]) {
            if ($_POST["amazon_3rd_used_remind_everytime"] || !isDigital($_POST["amazon_3rd_used_remind"])) {
                $_POST["amazon_3rd_used_remind"] = 2147483647;
            }
        } else {
            $_POST["amazon_3rd_used_remind"] = "NULL";
        }
        if (!isDigital($_POST["amazon_remind"])) {
            $_POST["amazon_remind"] = "NULL";
        }
        if (!isDigital($_POST["amazon_3rd_new_remind"])) {
            $_POST["amazon_3rd_new_remind"] = "NULL";
        }
        if (!isDigital($_POST["amazon_3rd_used_remind"])) {
            $_POST["amazon_3rd_used_remind"] = "NULL";
        }
        if (!isDigital($_POST["amazon_price"])) {
            $_POST["amazon_price"] = "NULL";
        }
        if (!isDigital($_POST["amazon_new_price"])) {
            $_POST["amazon_new_price"] = "NULL";
        }
        if (!isDigital($_POST["amazon_used_price"])) {
            $_POST["amazon_used_price"] = "NULL";
        }
        if (!isDigital($_POST["amazon_remind"])) {
            $_POST["amazon_remind"] = "NULL";
        }
        if (!isDigital($_POST["amazon_3rd_new_remind"])) {
            $_POST["amazon_3rd_new_remind"] = "NULL";
        }
        if (!isDigital($_POST["amazon_3rd_used_remind"])) {
            $_POST["amazon_3rd_used_remind"] = "NULL";
        }
        if (!isDigital($_POST["runtime"])) {
            $_POST["runtime"] = "NULL";
        }
        if ($_POST[amazon_guard_availabillity] == 1) {
            $_POST[amazon_guard_availabillity] = "true";
        } else {
            $_POST[amazon_guard_availabillity] = "false";
        }
        //create new category?
        if ($_POST["category"] == "NULL") {
            $query = "INSERT INTO priceguard_categories (mos_user_id, parent, name) VALUES (" . $my->id . ", ";
            $query .= $_POST["category"] . ", \"";
            $query .= $_POST["category_name"] . "\")";
            $database->setQuery($query);
            $success = $database->query();
            echo $query . "<br>";
            $X = "(SELECT id FROM priceguard_categories WHERE mos_user_id=" . $my->id . " AND name='" . $_POST["category_name"] . "' AND ";
            if ($_POST["category"] == "NULL") {
                $X .= "parent IS NULL LIMIT 1)";
            } else {
                $X .= "parent=" . $_POST["category"] . " LIMIT 1)";
            }
            $_POST["category"] = $X;
        }
        //insert our product in amazon_product
        $query = "INSERT INTO amazon_product (asin, amazon_price, 3rdparty_new_price, 3rdparty_used_price, amazon_availabillity, image_small) VALUES (\n\t\t'" . $_POST["asin"] . "',\n\t\t" . $_POST["amazon_price"] . ",\n\t\t" . $_POST["amazon_new_price"] . ",\n\t\t" . $_POST["amazon_used_price"] . ",\n\t\t'" . $_POST["amazon_availability"] . "',\n\t\t'" . $_POST["amazon_picture_url"] . "'\n\t\t);";
        $database->setQuery($query);
        $database->query();
        echo $query . "<br>";
        //insert user information into priceguard_product
        $query = "INSERT INTO priceguard_product (priceguard_categories_id, mos_user_id, amazon_product_asin, name,   guard_availabillity, deadline, remind_price_amazon, remind_price_amazon_3rdparty_new, remind_price_amazon_3rdparty_used) VALUES (\n\t\t" . $_POST["category"] . ",\n\t\t" . $my->id . ",\n\t\t'" . $_POST["asin"] . "',\n\t\t'" . $_POST["name"] . "',\n\t\t" . $_POST["amazon_guard_availabillity"] . ",\n\t\t" . $_POST["runtime"] . ",\n\t\t" . $_POST["amazon_remind"] . ",\n\t\t" . $_POST["amazon_3rd_new_remind"] . ",\n\t\t" . $_POST["amazon_3rd_used_remind"] . ");";
        $database->setQuery($query);
        $database->query();
        echo $query . "<br>";
    }
    //STEP 1
    //ask for link
    if (!isset($_POST[amazon_link])) {
        $mainframe->setPageTitle(preg_replace("/([ÂÃ])([€-¿])/e", "chr(ord('\\1')<<6&0xC0|ord('\\2')&0x3F)", "Produkt hinzufügen 1/2"));
        HTML_priceguard::addProduct1();
    }
    //STEP 2
    //ask user for product information
    if (isset($_POST[amazon_link])) {
        $mainframe->setPageTitle(preg_replace("/([ÂÃ])([€-¿])/e", "chr(ord('\\1')<<6&0xC0|ord('\\2')&0x3F)", "Produkt hinzufügen 2/2"));
        $amazon = new Services_AmazonECS4("1R0VYFH9MMZM6SNJBD02", "gschaftshuonl-21");
        $amazon->setLocale("DE");
        $options = array();
        $options['ResponseGroup'] = 'Small,Offers,Images';
        $result = $amazon->ItemLookup(asin_search($_POST[amazon_link]), $options);
        if (PEAR::isError($result)) {
            echo '<p>Fehler:<br/>';
            echo htmlspecialchars($result->message);
            echo '</p>';
        } elseif ($result["Request"]["IsValid"] != "True") {
            echo '<p>Fehler:<br/>';
            echo "Link nicht korrekt";
            echo '</p>';
        } else {
            HTML_priceguard::addProduct2($result);
        }
    }
}