<?php

include 'simple_html_dom.php';
function strip_tags_content($text, $tags = '', $invert = FALSE)
{
    preg_match_all('/<(.+?)[\\s]*\\/?[\\s]*>/si', trim($tags), $tags);
    $tags = array_unique($tags[1]);
    if (is_array($tags) and count($tags) > 0) {
        if ($invert == FALSE) {
            return preg_replace('@<(?!(?:' . implode('|', $tags) . ')\\b)(\\w+)\\b.*?>.*?</\\1>@si', '', $text);
        } else {
            return preg_replace('@<(' . implode('|', $tags) . ')\\b.*?>.*?</\\1>@si', '', $text);
        }
    } elseif ($invert == FALSE) {
        return preg_replace('@<(\\w+)\\b.*?>.*?</\\1>@si', '', $text);
    }
    return $text;
}
function honest_product()
{
    $cod_produs = $_REQUEST['cod_produs'];
    $url = 'https://honest.ro/web/ap.exe/v2auth.ajax_integrator';
    $login = ['ajax_type' => 'LOGIN', 'val1' => $_REQUEST['username'], 'val2' => $_REQUEST['pass'], 'val3' => '', 'val4' => '', 'val5' => 'Y'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $login);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "honest_cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "honest_cookies.txt");
    $response = curl_exec($ch);
    curl_close($ch);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://honest.ro/articol/" . $cod_produs);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "honest_cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "honest_cookies.txt");
    $reponsez = curl_exec($ch);
    curl_close($ch);
    $page = $reponsez;
    $html = str_get_html($page);
    $stock = $price = array();
    if ($html->find('.cd_stock') && $html->find('.c_price')) {
        $content = $html->find('.cd_stock', 0)->next_sibling();
        $rows = $content->find('table tr');
        foreach ($rows as $row) {
            $item = explode(':', strip_tags($row->innertext));
            $stock[$item[0]] = (int) $item[1];
        }
        $content = $html->find('.cd_price', 0)->next_sibling();
        $rows = $content->find('table tr');
        foreach ($rows as $k => $row) {
            $item = $row->find('td');
            $label = preg_replace("/[^A-Za-z]+/", "", $item[0]->innertext);
            $label = str_replace('iclassfafaleafhgtwptooltipariahiddentruetitleLaacestpreseadaugvaloareTaxVerdestylecolori', '', $label);
            $price[$label] = strip_tags_content($item[1]->innertext);
            $price[$label] = str_replace('.', '', $price[$label]);
            $price[$label] = str_replace(',', '.', $price[$label]);
        }
        $return = array('stock' => $stock, 'price' => $price);
    } else {
        $return = false;
    }
    $data = json_encode($return);
    return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && $_SERVER["CONTENT_TYPE"] == "application/json") {
    $data = trim(file_get_contents("php://input"));
    $json = json_decode($data, true);
    if (is_array($json)) {
        $_REQUEST = $json + $_REQUEST;
    }
    echo honest_product();
} else {
    http_response_code(405);
}