<?php
if (!class_exists('crm_hardlock')) require_once("common_cadline_libraries/crm_hardlock.class.php");
if (!class_exists('crm_users')) require_once("common_cadline_libraries/crm_users.class.php");

if (isset($_GET['npid'])) {
    $aktDate = 60; // ennyi napig ad érvényességet
    $npid = trim(stripslashes($_GET['npid']));
    $verid = (isset($_GET['verid']) && (int)$_GET['verid'] > 0 ? (int)$_GET['verid'] : 0);
    $product = $_GET["product"];

    // Kikeresi, hogy van-e regisztralva a computers tablaba ilyen npid-u gép, ha nincs, akkor beleteszi
    $compid = crm_hardlock::AddOrUpdateComputer($npid, "");

    // Megnezi, hogy van-e ilyen computer_id-val és product koddal regisztralt nonprofit kulcs
    crm_database::getInstance()->query("SELECT nonprofit.hlID AS hlID, nonprofit.nUserID AS nUserID FROM `" . CRMADATBAZIS . "`.`nonprofit`
          LEFT JOIN `" . CRMADATBAZIS . "`.hardlock ON hardlock.hlNum = nonprofit.hlID
          WHERE nonprofit.computer_id='" . $compid . "' AND nonprofit.verid='" . $verid . "' AND nonprofit.partnerID is null AND hardlock.product_id=" . $product . " LIMIT 1");
    $ered = crm_database::getInstance()->fetchAssoc();

    if (count($ered)) {
        $ered = $ered[0];
        $hlNum = $ered['hlID'];
        $nUserID = $ered['nUserID'];

        crm_database::getInstance()->query("SELECT * FROM `" . CRMADATBAZIS . "`.`users` WHERE nUserID='" . $nUserID . "' LIMIT 1");
        $user = crm_database::getInstance()->fetchAssoc();
        $user = $user[0];

        if ($user['nUserType'] == 0) {
            crm_database::getInstance()->query("UPDATE " . CRMADATBAZIS . ".users SET nUserType=2 WHERE nUserID=" . $nUserID . " LIMIT 1;");
        }

        crm_database::getInstance()->query("SELECT * FROM `" . CRMADATBAZIS . "`.`p_versions` WHERE " . ($verid > 0 ? "verPassword8and9='" . $verid . "'" : "verActual='Y'") . " LIMIT 1;");
        $ver = crm_database::getInstance()->fetchAssoc();

        $type = $product == 2 ? 76 : 8;
        $password = Codegen::GeneratePassword($hlNum, 0, $type, $ver[0]['verCode']);

        if (isset($_GET['createProgram']) && $_GET['createProgram'] == "true") { //ba joomlabol jott: a com_nonprofit getpass fv.-bol
            ;
        } else {
            print($password); //ba archlinebol jott az alxdaypassword-bol
            return;
        }


        if (trim($ered['activation_datetime']) == '') {
            $l = ($user['old_db'] == 'clusers' ? 'hu-HU' : 'en-GB');
            $lang = parse_ini_file($_SERVER['DOCUMENT_ROOT'] . '/language/' . $l . '/' . $l . '.com_nonprofit.ini');

            if (in_array($user['nUserStatus'], array(-5, -4, -3, -2, -1, 0, 1, 2, 3, 99))) {
                crm_users::SetUserStatus($nUserID, 4);
            }

            crm_database::getInstance()->update("`" . CRMADATBAZIS . "`.`nonprofit`", array('activation_datetime' => date('Y-m-d H:i:s'), 'verid' => $verid), array('id' => $ered['id']));

            crm_database::getInstance()->query("SELECT * FROM `" . CRMADATBAZIS . "`.`u_history` WHERE `nUserID`=" . $nUserID . " AND `nTopicID`=" . ($user['old_db'] == 'clusers' ? 301 : 303) . "  AND `dateEvent`='" . date('Y-m-d', time()) . "';");
            $ered = crm_database::getInstance()->fetchAssoc();

            if (!count($ered)) {
                crm_users::AddUserHistory(array(
                    'nUserID' => $nUserID,
                    'nTopicID' => ($user['old_db'] == 'clusers' ? 301 : 303), //($l=='hu-HU' ? 301 : 303),
                    'dateEvent' => date('Y-m-d', time()),
                    'strPlace' => 'web',
                    'strInfo' => sprintf($lang['COM_NONPROFIT_KEY_ACTIVATION_INFO'], $hlNum),
                    'nManagerID' => 36,
                    'insertDate' => date('Y-m-d H:i:s', time())
                ));
            }
        }

        if (empty($ered['isid']) && isset($_GET['isid'])) {
            $isid = trim($_GET['isid']);
            crm_database::getInstance()->update("`" . CRMADATBAZIS . "`.`nonprofit`", array('isid' => $isid), array('id' => $ered['id']));
            $ered['isid'] = $isid;
        }

        crm_hardlock::DeleteProgram($hlNum);

        $aktkod = Codegen::GenerateTimeCode($password, $ered['isid'], $aktDate);

        crm_database::getInstance()->query("SELECT * FROM `" . CRMADATBAZIS . "`.`h_programs` WHERE `prPass`='" . $password . "' AND `prSellDate`='" . date('Y-m-d', time()) . "';");
        $ered = crm_database::getInstance()->fetchAssoc();

        if (!count($ered)) {
            crm_hardlock::AddProgram(array(
                'prTypeID' => 8,
                'prVerID' => $ver[0]['verID'],
                'prLan' => 0,
                'prPass' => $password, // 99xxxxxxxxxxxxxx
                'prSellDate' => date('Y-m-d', time()),
                'prActDate' => date('Y-m-d', time() + $aktDate * 86400),
                'prFizetve' => date('Y-m-d', time() + $aktDate * 86400),
                'prActCode' => trim($aktkod),
                'prTime' => date('Y-m-d H:i:s', time()),
                'prLastMod' => date('Y-m-d H:i:s', time()),
                'prPrice' => 0,
                'prContact' => 5, // Non-profit
                'prHlNum' => $hlNum, // 99xxxx
                'prManID' => 36, // web
                'prStat' => 0,
                'prHiType' => 1, // 1 Rendelés | 2 Típus Váltás | 3 Frissítés | 4 Auto Friss.
                'prQuota' => 0
            ));
        }
        print($password);
    }
}
