<?php
/**
 * @Cscms 3.5 open source management system
 * @copyright 2009-2013 chshcms.com. All rights reserved.
 * @Author:Cheng Jie
 * @Dtime:2013-04-27
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

 class CsdjoAuth extends CI_Model
 {
 function qq_login($state, $appid, $scope, $callback)
	{
		$login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" 
			. $appid . "&redirect_uri=" . rawurlencode($callback)
			. "&state=" . $state
			. "&scope=".$scope;
		header("Location:$login_url");
	}

	function qq_callback($inputs, $appid, $appkey, $callback)
	{
		if($inputs['state'] == $inputs['state']) //csrf
		{
			$token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
				. "client_id=" . $appid. "&redirect_uri=" . rawurlencode($callback)
				. "&client_secret=" . $appkey. "&code=" . $inputs["code"];
	
			$response = $this->HtmlAll($token_url);
			if (strpos($response, "callback") !== false)
			{
				$lpos = strpos($response, "(");
				$rpos = strrpos($response, ")");
				$response  = substr($response, $lpos + 1, $rpos - $lpos -1);
				$msg = json_decode($response);
				if (isset($msg->error))
				{
					exit;
				}
			}
	
			$params = array();
			parse_str($response, $params);
	
			return $params["access_token"];
		}

	}
	
	function get_openid($inputs,$access_token)
	{
		$graph_url = "https://graph.qq.com/oauth2.0/me?access_token=" . $access_token;
	
		$str  = $this->HtmlAll($graph_url);
		if (strpos($str, "callback") !== false)
		{
			$lpos = strpos($str, "(");
			$rpos = strrpos($str, ")");
			$str  = substr($str, $lpos + 1, $rpos - $lpos -1);
		}
	
		$user = json_decode($str);
		if (isset($user->error))
		{

			exit;
		}
	

		return $user->openid;

	}
	//获取用户信息


	function get_user_info($access_token, $appid, $open_id)
	{
		$get_user_info = "https://graph.qq.com/user/get_user_info?"
			. "access_token=" . $access_token
			. "&oauth_consumer_key=" . $appid
			. "&openid=" . $open_id
			. "&format=json";
	
		$info =  $this->HtmlAll($get_user_info);
		$arr = json_decode($info, true);
	
		return $arr;
	}
	function HtmlAll($url,$codes='gbk'){
          if(empty($url)) return '';
	  if(function_exists('curl_init')){
		$curl = curl_init(); //初始化curl
		curl_setopt($curl, CURLOPT_URL, $url); //设置访问的网站地址
		curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //模拟用户使用的浏览器
		curl_setopt($curl, CURLOPT_AUTOREFERER, 1);    //自动设置来路信息
		curl_setopt($curl, CURLOPT_TIMEOUT, 100);      //设置超时限制防止死循环
		curl_setopt($curl, CURLOPT_HEADER, 0);         //显示返回的header区域内容
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //获取的信息以文件流的形式返回
		$ConTent = curl_exec($curl);
		curl_close($curl);
	  }else{
		$ConTent = @file_get_contents($url); //不支持curl函数直接使用get模式抓取
	  }
          if(strtolower($codes)!='gbk'){
               if(function_exists("mb_convert_encoding")){
                    $ConTent=mb_convert_encoding($ConTent,'gbk',$codes);
               }else{
                    $ConTent=@iconv($codes,'gbk',$ConTent);
               }
          }
          $ConTent=str_replace('.js','js',$ConTent);
          $ConTent=str_replace('ajaxjs','ajax.js',$ConTent);
          $ConTent=str_replace('</textarea>','&lt;/textarea&gt;',$ConTent);
          return $ConTent;
    }
	
	}