<?php

/**
 * 联想 配置信息
 */
class config_for_Lenovo
{

    /**
     * @var string cp在1sdk后台注册的应用ID
     */
    public $appid = '1803190977289.app.ln';

    /**
     * @var string 与1sdk约定的秘钥
     */
    public $appkey = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKKRO2BdupDym4eOz6urAy5l3/sE
xEvfqSX7lB/S/YHMdrN5qYhGANG46r9ucahqpEaAaqHCYR5M+U5c1h1BgL70kyuWfYhS31GfqWI5
c8TZx+8z/LwnrzuC41LGUanDrgR/F4VJfNKWAXQCjieRZef8Y0OCskxYGdIDzvMfnwixAgMBAAEC
gYBC4+tAjZB0j767GmIMysL2VmjAyodSiOb6f/fE2HYC8HRvCMJfFh7ELIg62GDf4XFSqQg3Itn/
M8DpI33eb9NfFu2QGEX0zlBCzSaCeN6toZkhx8Noi0UXZKt31qlATDThqiF7DEoc0Dtqm4H6Nsr9
ltQoJYT8068G96sIRa1XgQJBANgsYB72WDaYEL35IbE8fO94leAPNVwS+oQkdbKx/DDT0I4UfUFX
LmuR4BWEGoJhNbjc6YezEyM944imr0+Lb6kCQQDAhJNTBA3pnHmU/PGSUzSyq9Im3FcreoIHBAJK
KaWCZgYAqkaJ7iayflxS12CwQy2y+uvvhKQTxDEZc2peT5XJAkEAkBnsrVcb8LCnoGu9eO9QfTXN
+sFImasREhtX1hntegMIqF2iLXdXO1/Pl6grH7R7YlLo3YuS+1AbKBGtFYJmmQJBAKZe6+JGEq/c
JcX3gdxuQ5BUpFwo9rwANvZRJJlqvDmgaXeRrzvrr015ndeJgWlR5OBTsClhe1oJplTURQwTwuEC
QBbmKwZzvmdtyy6iZVelzFRCYlMiq2VdI1uKLgN3k0LZKQSKZugrR7/wR7eRN6CshBT40z6l11is
umUsSFPuJG0=';

    /**
     * @var string 联想后台验证登录状态并换取uid的接口地址
     */
    public $verifySession_url = 'http://passport.lenovo.com/interserver/authen/1.2/getaccountid';

    /**
     * @var string 联想后台验证登录状态时的请求方式(get)
     */
    public $verifySession_method = 'GET';

    /**
     * @var array 联想商品id到游戏内商品id的映射
     */
    private $itemMap = array(
        '198342' => '801801',
        '198343' => '801802',
        '198344' => '801803',
        '198345' => '801804',
        '198346' => '801805',
        '198347' => '801806'
    );

    function ProductId($lenovoShopItemID)
    {
        return $this->itemMap[$lenovoShopItemID];
    }

    /**
     * configs for 联想
     * @return config_for_Lenovo Description
     */
    static function Inst()
    {
        return new config_for_Lenovo();
    }

    /**
     * 联想 依据(登录校验接口)错误码查询详细信息
     * @param type $errcode
     * @return string
     */
    public function ErrMsg($errcode)
    {
// <editor-fold defaultstate="collapsed" desc=" 错误码对应的信息 ">
        $errmsg = array(
            "USS-0100" => "无效的用户名，需要检查用户名格式是否正确。",
            "USS-0101" => "口令错误。",
            "USS-0103" => "无此用户，请检查用户名是否正确",
            "USS-0104" => "用户名已存在，不允许重复注册",
            "USS-0105" => "帐号必须激活后才能登录",
            "USS-0108" => "该用户已激活，请勿重复操作。",
            "USS-0110" => "无效的 IMEI，SN 或 MAC",
            "USS-0111" => "帐号已被 disable",
            "USS-0113" => "口令类型错误",
            "USS-0121" => "无效的 realm",
            "USS-0122" => "此服务不支持该 realm",
            "USS-0126" => "Ticket 值解析失败。",
            "USS-0135" => "无效的请求数据",
            "USS-0151" => "账号已锁定",
            "USS-0160" => "需要使用验证码（在申请帐号时，如果服务端检测到异常行为，会返回该错误给客户端，客户端需要使用图形验证码）",
            "USS-0170" => "密码格式错误。(密码的限制规则是: 4～20 位字符，包括英文大小写字母、英文数字、减号和下划线)",
            "USS-0181" => "校验码错误或失效，针对手机账号对账号校验时可能产生",
            "USS-0190" => "(针对短信注册，服务端不支持该运营商的号码。) 尚未开通，请尝试其他方式注册。",
            "USS-0540" => "无效的 lpsust",
            "USS-0542" => "未提供 lpsust 信息",
            "USS-0202" => "用户登录已失效",
            "USS-0x0000" => "后台快捷登录使用次数达到 10 次",
            "USS-0x0001" => "后台快捷登录失败",
            "USS-0x0002" => "后台快捷登录检测到未安装联想通行证");
// </editor-fold>
        if (array_key_exists($errcode, $errmsg)) {
            return $errmsg[$errcode];
        }
        return "未知错误!";
    }

    /**
     * @var type 私钥
     */
    private $priKey = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKKRO2BdupDym4eOz6urAy5l3/sExEvfqSX7lB/S/YHMdrN5qYhGANG46r9ucahqpEaAaqHCYR5M+U5c1h1BgL70kyuWfYhS31GfqWI5c8TZx+8z/LwnrzuC41LGUanDrgR/F4VJfNKWAXQCjieRZef8Y0OCskxYGdIDzvMfnwixAgMBAAECgYBC4+tAjZB0j767GmIMysL2VmjAyodSiOb6f/fE2HYC8HRvCMJfFh7ELIg62GDf4XFSqQg3Itn/M8DpI33eb9NfFu2QGEX0zlBCzSaCeN6toZkhx8Noi0UXZKt31qlATDThqiF7DEoc0Dtqm4H6Nsr9
ltQoJYT8068G96sIRa1XgQJBANgsYB72WDaYEL35IbE8fO94leAPNVwS+oQkdbKx/DDT0I4UfUFXLmuR4BWEGoJhNbjc6YezEyM944imr0+Lb6kCQQDAhJNTBA3pnHmU/PGSUzSyq9Im3FcreoIHBAJKKaWCZgYAqkaJ7iayflxS12CwQy2y+uvvhKQTxDEZc2peT5XJAkEAkBnsrVcb8LCnoGu9eO9QfTXN+sFImasREhtX1hntegMIqF2iLXdXO1/Pl6grH7R7YlLo3YuS+1AbKBGtFYJmmQJBAKZe6+JGEq/cJcX3gdxuQ5BUpFwo9rwANvZRJJlqvDmgaXeRrzvrr015ndeJgWlR5OBTsClhe1oJplTURQwTwuECQBbmKwZzvmdtyy6iZVelzFRCYlMiq2VdI1uKLgN3k0LZKQSKZugrR7/wR7eRN6CshBT40z6l11isumUsSFPuJG0=';

    /**
     * 联想 - RSA签名
     * @param $data   待签名数据
     * return 签名结果
     */
    function sign($data)
    {
        $priKey = $this->priKey;
        if (strpos($priKey, "BEGIN RSA PRIVATE KEY") === false) {
            $priKey = wordwrap($priKey, 64, "\n", true);
            $priKey = "-----BEGIN PRIVATE KEY-----\n" . $priKey . "\n-----END PRIVATE KEY-----";
        }
        $res = openssl_get_privatekey($priKey);
        openssl_sign($data, $sign, $res);
        openssl_free_key($res);
        $sign = base64_encode($sign);
        return $sign;
    }

    /**
     * 联想 - RSA验签
     * @param $data   待签名数据
     * @param $priKey 密钥
     * @param $sign   要校对的的签名结果
     * return 验证结果
     */
    function verify($data, $sign)
    {
        $selfSign = $this->sign($data);
        if ($selfSign && $selfSign == $sign) {
            return true;
        } else {
            return false;
        }
    }

}
