<?php
/**
 * JYmusic音乐管理系统
 *
 * @version     2.0
 * @author      战神~~巴蒂 <jyuucn@163.com>
 * @license     http://jyuu.cn/license
 * @copyright   2014 - 2017 JYmusic
 */

namespace Addons\Comment\controller;

use app\common\api\user as UserApi;
use app\common\api\User;
use app\common\controller\BaseController;

class SsoController extends BaseController
{
    /* 畅言单点登录用户信息 */
    public function cyuser()
    {
        $uid = is_login();
        if (!$uid) {
            return jsonp(["is_login"=>0]);
        }
        $user = get_user_info($uid);
        $ret = [
            "is_login"		=>1, //已登录，返回登录的用户信息
            "user"			=> [
                "user_id"		=> $uid,
                "nickname"		=> $user['nickname'],
                "img_url"       => $user['avatar'],
                "profile_url"   => $user['url'],
                "sign"			=> '*******'
            ]
        ];
        return jsonp($ret);
    }
    
    
	/*有言单点登录*/
	public function uysso(){
		if (!$uid = is_login()) {
            setcookie('syncuyan', 'logout', time() + 3600, '/', 'jyuu.cn');
        } else {
            $user       = get_user_info($uid);
			$conf		= get_addon_config('SocialComment');
			$key		= $conf['comment_key_youyan'];
			$encode_data = array(
				'uid'		=> $uid,
				'uname'		=> $user['nickname'],
				'email'		=> "",
				'uface'		=> $user['avatar'],
				'ulink'		=> $user['url'],
				'expire'	=> 3600
			);
			setcookie('syncuyan', $this->des_encrypt(json_encode($encode_data), $key), time() + 3600, '/', $_SERVER['HTTP_HOST']); 
			echo "已经登录！"; die;
		}
	}
 
	
	/*畅言登录*/
	public function cylogin ()
    {
		$cy_uid	= (int)I('get.user_id');	
		if($cy_uid){//判断畅言是否绑定了该用户		
			if(!$uid = is_login()){
				$ret=array(
					'user_id'=>'1',
					'reload_page'=>0
				);
			}else{
				$ret=array(
					'user_id'=>$uid,
					'reload_page'=>0
				);
			}

		}else{ 
			if ($cy_uid  && D('Member')->login($uid)){
				$ret= array(
					'user_id'=>$uid,
					'reload_page'=>0
				);
			}else{
				$ret=array(
					'user_id'=>'1',
					'reload_page'=>0
				);
			}
			
		}
		echo $_GET['callback'].'('.json_encode($ret).')';
	}
	
	public function cylogout (){
	    $api = new User();
        $api->logout();
		return jsonp([
            'code'=>1,
            'reload_page'=>0
        ]);
	}
 
	// 有言加密，注意，加密前需要把数组转换为json格式的字符串
	protected  function des_encrypt($string, $key) {
		$size = mcrypt_get_block_size('des', 'ecb');
		$string = mb_convert_encoding($string, 'GBK', 'UTF-8');
		$pad = $size - (strlen($string) % $size);
		$string = $string . str_repeat(chr($pad), $pad);
		$td = mcrypt_module_open('des', '', 'ecb', '');
		$iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
		@mcrypt_generic_init($td, $key, $iv);
		$data = mcrypt_generic($td, $string);
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		$data = base64_encode($data);
		return $data;
	}
	// 解密，解密后返回的是json格式的字符串
	protected  function des_decrypt($string, $key) {
		$string = base64_decode($string);
		$td = mcrypt_module_open('des', '', 'ecb', '');
		$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
		$ks = mcrypt_enc_get_key_size($td);
		@mcrypt_generic_init($td, $key, $iv);
		$decrypted = mdecrypt_generic($td, $string);
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		$pad = ord($decrypted{strlen($decrypted) - 1});
		if($pad > strlen($decrypted)) {
			return false;
		}
		if(strspn($decrypted, chr($pad), strlen($decrypted) - $pad) != $pad) {
			return false;
		}
		$result = substr($decrypted, 0, -1 * $pad);
		$result = mb_convert_encoding($result, 'UTF-8', 'GBK');
		return $result;
	}
}
