<?php

class crmHelper
{
    public static $ch;

    public function __construct()
    {
    }

    public static function getInstance()
    {
        if (!empty(self::$ch)) return self::$ch;
        self::$ch = new HardlockHelper();
        return self::$ch;
    }

    public static function AddOrUpdateComputer($npID)
    {
        MySqlHelper::getInstance()->query("SELECT * FROM `cl_hlusers`.`computers` WHERE `computer_id`='" . $npID . "' LIMIT 1;");
        $res = MySqlHelper::getInstance()->fetchAssoc();

        if (empty($res)) {
            $comp = array(
                'computer_id' => $npID,
                'last_mod' => date('Y-m-d', time())
            );
            MySqlHelper::getInstance()->insert("`cl_hlusers`.`computers`", $comp);
        } else {
            MySqlHelper::getInstance()->update("`cl_hlusers`.`computers`", array('last_mod' => date('Y-m-d', time())), array('computer_id' => $npID));
        }

        MySqlHelper::getInstance()->query("SELECT * FROM `cl_hlusers`.`computers` WHERE `computer_id`='" . $npID . "' LIMIT 1;");
        $res = MySqlHelper::getInstance()->fetchAssoc();

        return $res[0]['id'];
    }

    public static function GetAllPrograms($nUserID, $product_id, $verid)
    {
        MySqlHelper::getInstance()->query("SELECT h_programs.prPass AS prHlNum, h_programs.prActCode AS prActCode, h_programs.prTypeID AS prTypeID, h_programs.prVerID AS prVerID, h_programs.prLan AS prLan
            FROM `cl_hlusers`.`hardlock`
            INNER JOIN `cl_hlusers`.`h_programs` ON h_programs.prHlNum = hardlock.hlNum
            WHERE (hardlock.hlUser='" . $nUserID . "' OR hardlock.borrow='" . $nUserID . "') AND hardlock.product_id='" . $product_id . "' AND hardlock.hlStat!='0' AND h_programs.prVerID='" . $verid . "'");
        $res2 = MySqlHelper::getInstance()->fetchAssoc();

        return $res2;
    }

    public static function GetVerIDFromPassword($pwd)
    {
        $ver = substr($pwd, 7, 2);
        MySqlHelper::getInstance()->query("SELECT * FROM `cl_hlusers`.`p_versions` WHERE `verPassword8and9`='" . $ver . "' LIMIT 1;");
        $res = MySqlHelper::getInstance()->fetchAssoc();

        return $res[0]['verID'];
    }

    public static function encode($privateKey, $publicKey, $data)
    {
        $key = self::CalcKey($privateKey, $publicKey);
        $strLen = strlen($data);
        $keyLen = strlen($key);
        for ($i = 0; $i < $strLen; $i++) {
            $ordStr = ord(substr($data, $i, 1));
            if ($j == $keyLen) {
                $j = 0;
            }
            $ordKey = ord(substr($key, $j, 1));
            $j++;
            $hash .= strrev(base_convert(dechex($ordStr + $ordKey), 16, 36));
        }
        return $hash;
    }

    public static function CalcKey($privateKey, $publicKey)
    {
        $privateKeyLength = strlen($privateKey);
        $publicKeyLength = strlen($publicKey);

        if ($privateKeyLength > $publicKeyLength) {
            $result = $privateKey;
            for ($i = 1; $i < $publicKeyLength; $i += 2) {
                $result[$i] = $publicKey[$i];
            }
        } else {
            $result = $publicKey;
            for ($i = 0; $i < $privateKeyLength; $i += 2) {
                $result[$i] = $privateKey[$i];
            }
        }
        return sha1($result);
    }

    public static function decode($privateKey, $publicKey, $data)
    {
        $key = self::CalcKey($privateKey, $publicKey);
        $strLen = strlen($data);
        $keyLen = strlen($key);
        for ($i = 0; $i < $strLen; $i += 2) {
            $ordStr = hexdec(base_convert(strrev(substr($data, $i, 2)), 36, 16));
            if ($j == $keyLen) {
                $j = 0;
            }
            $ordKey = ord(substr($key, $j, 1));
            $j++;
            $hash .= chr($ordStr - $ordKey);
        }
        return $hash;
    }

    public static function decode2($string, $key)
    {
        $key = sha1($key);
        $strLen = strlen($string);
        $keyLen = strlen($key);
        for ($i = 0; $i < $strLen; $i += 2) {
            $ordStr = hexdec(base_convert(strrev(substr($string, $i, 2)), 36, 16));
            if ($j == $keyLen) {
                $j = 0;
            }
            $ordKey = ord(substr($key, $j, 1));
            $j++;
            $hash .= chr($ordStr - $ordKey);
        }
        return $hash;
    }

    public static function GetVerName($verid)
    {
        MySqlHelper::getInstance()->query("SELECT * FROM `cl_hlusers`.`p_versions` WHERE `verID`='" . $verid . "' LIMIT 1;");
        $res = MySqlHelper::getInstance()->fetchAssoc();

        return $res[0]['verName'];
    }

    public static function GetTypeName($typeid)
    {
        MySqlHelper::getInstance()->query("SELECT * FROM `cl_hlusers`.`p_types` WHERE `typeCode`='" . $typeid . "' LIMIT 1;");
        $res = MySqlHelper::getInstance()->fetchAssoc();

        return $res[0]['typeName'];
    }

    public static function GetActCode($serial)
    {
        MySqlHelper::getInstance()->query("SELECT * FROM `cl_hlusers`.`h_programs` WHERE `prPass`='" . $serial . "' LIMIT 1;");
        $res = MySqlHelper::getInstance()->fetchAssoc();

        return $res[0]['prActCode'];
    }

    public static function GetWebformPrivateKey()
    {
        return "3BjGQ6fzMtHupM5V8U3l_u";
    }

    public static function SetWebformResult($session, $result, $encodedResult)
    {
        MySqlHelper::getInstance()->query("SELECT * FROM `cl_hlusers`.`webform_results` WHERE `session_id`='" . $session . "' LIMIT 1;");
        $res = MySqlHelper::getInstance()->fetchAssoc();

        if (empty($res)) {
            $sess = array(
                'session_id' => $session,
                'result' => $result . '=' . $encodedResult,
                'insertDate' => date("Y-m-d")
            );
            MySqlHelper::getInstance()->insert("`cl_hlusers`.`webform_results`", $sess);
        } else {
            MySqlHelper::getInstance()->update("`cl_hlusers`.`webform_results`", array('result' => $result . '=' . $encodedResult, 'insertDate' => date("Y-m-d")), array('session_id' => $session));
        }
    }

    public static function SetWebformResource($session, $hint, $buttontext)
    {
        MySqlHelper::getInstance()->query("SELECT * FROM `cl_hlusers`.`webform_results` WHERE `session_id`='" . $session . "' LIMIT 1;");
        $res = MySqlHelper::getInstance()->fetchAssoc();

        if (empty($res)) {
            $sess = array(
                'session_id' => $session,
                'insertDate' => date("Y-m-d"),
                'message' => $hint,
                'button' => $buttontext
            );
            MySqlHelper::getInstance()->insert("`cl_hlusers`.`webform_results`", $sess);
        } else {
            MySqlHelper::getInstance()->update("`cl_hlusers`.`webform_results`", array('insertDate' => date("Y-m-d"), 'message' => $hint, 'button' => $buttontext), array('session_id' => $session));
        }
    }

    public static function get_valueFromStringUrl($url, $parameter_name)
    {
        $parts = parse_url($url);
        if (isset($parts['query'])) {
            parse_str($parts['query'], $query);
            if (isset($query[$parameter_name]))
                return $query[$parameter_name];
            else
                return null;
        } else {
            return null;
        }
    }
}
