<?php
error_reporting(E_ERROR | E_PARSE);
if (!class_exists('Authenticate')) require_once("common_cadline_libraries/crm_test.class.php");

$session    = $_GET["session"];
$authCode   = $_GET["authCode"];

$auth = new Authenticate();
$auth->setAuthCode($authCode);
$auth->setAuthSession($session);
$auth->auth();

$action     = $_GET['action'];
$code       = $_GET['code'];
$date       = $_GET['date'];
$password   = $_GET['password'];
$strIsids   = $_GET['isids'];
$active     = $_GET['active'];
$all        = $_GET['all'];
$floatings  = $_GET['floatings'];
$disabled   = $_GET['disabled'] == 'true';
$clr_info   = $_GET['clr_info'] == 'true';
$api        = new crm_test_API($code, $password);

if ($api->checkCode()) {
    switch ($action) {
        case 'clear_program':
            $result = $api->clearProgram($clr_info);
            if ($result == false) print crm_hardlock::GetActivationError(crm_hardlock::CLEAR_FAILED);
            else print "ok";
            break;

        case 'update_fizetve':
            $api->setActDate($date);
            print $api->updateFizetve();
            break;

        case 'is_softwarekey':
            print Codegen::IsSoftwareKey($password) ? "true" : "false";
            break;

        case 'p_serial_isid_active_all':
            if ($api->isFloating()) {
                $floatingsArray = array();

                if (isset($floatings) && $floatings != '')
                    $floatingsArray = explode(";", $floatings);

                $message = "";

                $floatingActive = $api->countFloating($floatingsArray, $message);

                if (!$floatingActive) {
                    print $message;
                    die();
                }
            }

            if ($active != '') {
                $db_active = $api->countRegistration('active');
                if ($active != $db_active) {
                    print "error: nem egyeznek meg az active regisztraciok";
                    die();
                }
            }

            if ($all != '') {
                $db_all = $api->countRegistration('all');

                if ($all != $db_all) {
                    print "error: a regisztraciok nem egyeznek meg";
                    die();
                }
            }

            print "ok";
            break;

        case 'set_disabled':
            print $api->setDisabled($disabled) ? "true" : "false";
            break;

        case 'p_serial_isid_check_timecodes':
            if ($strIsids == '') {
                if ($api->countActivationCode() > 0) {
                    print 'error: van regisztracio';
                    die();
                }
            } else {
                $isids = explode(";", $strIsids);
                for ($i = 0; $i < count($isids); $i++) {
                    $api->setIsid($isids[$i]);

                    if (!$api->checkActivationCode()) {
                        print 'error: nincs aktivalokod';
                        die();
                    }
                }
            }
            print 'ok';
            break;

        case 'getNonProfitNpidIsid':
            $sql = "SELECT * FROM cl_hlusers.nonprofit ORDER BY RAND() LIMIT 1";
            Database::getInstance()->query($sql);
            $nonprofitKey = Database::getInstance()->fetchNext();

            $isid   = $nonprofitKey['isid'];
            $compID = $nonprofitKey['computer_id'];

            $sql = "SELECT * FROM cl_hlusers.computers WHERE id = ?";
            Database::getInstance()->query($sql, array('i', $compID));
            $computer = Database::getInstance()->fetchNext();
            $npID = $computer['computer_id'];

            print $isid . ';' . $npID;
            break;

        default:
            // code...
            break;
    }
} else print "Wrong code";
