123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- namespace Cas\Controller\API;
- use KIF\Core\Controller;
- use KIF\Core\Request;
- use Cas\Module\User;
- use Cas\Dao\UserInfo;
- use KIF\Math\Math;
- use KIF\Cookie;
- /**
- * 关于用户和passport接口
- *
- * @author lihuanchun
- *
- */
- class SetUser extends Controller {
- private $key;
- private $params;
- private $objMUser;
- public function __construct() {
- $this->key = 'rD5carfCGbHAhF';
- $this->params = $this->getRequestParams ();
-
- /*
- if (! $this->checkSign ( $params ['sign'], $params )) {
- //exit ( 'Sign error' );
- }
- */
-
- $this->objMUser = new User ();
-
- }
-
-
- /**
- * 用户登录caozuo
- */
- public function doRepLogin(){
-
- }
-
-
-
- /**
- * 设置用户
- * 请求地址:http://cas.lishuy.com/index.php?c=API_Req&a=Page&sign=51CE47C9A3681845D5ACCF3728DABD6A
- * http://cas.lishuy.com/API_SetUser_Req/?sign=51CE47C9A3681845D5ACCF3728DABD6A
- * 参数:
- * passport_uid 对方UID
- * cas_uid 本平台UID
- * nickname 用户昵称
- * headimgurl 用户头像地址
- * sign 加密串
- *
- * 返回
- */
- public function doReq() {
- // 1.获取传递来的相关信息
- $info ['nickname'] = urlencode($this->params ['nickname']);
- $info ['headimgurl'] = urlencode($this->params ['headimgurl']);
- $info ['passport_uid'] = $this->params ['passport_uid'];
- $cas_uid = $this->params ['cas_uid'];
- // 2.绑定用户信息
- if ($this->objMUser->bindingUser ( $info, $cas_uid )) {
- exit ( 'success' );
- }
- ;
- exit ( 'fail' );
- }
-
- /**
- * 统一着陆页面
- * 页面地址:http://cas.lishuy.com/index.php?c=API_SetUser&a=Page&sign=51CE47C9A3681845D5ACCF3728DABD6A
- * http://cas.lishuy.com/API_SetUser_Page/?sign=51CE47C9A3681845D5ACCF3728DABD6A
- * 参数:
- * passport_uid 对方UID
- * cas_uid 本平台UID
- * nickname 用户昵称
- * headimgurl 用户头像地址
- * return_url 原跳转地址
- * sign 加密串
- */
- public function doPage() {
-
- // 1.获取传递来的相关信息
- $info ['nickname'] = urldecode($this->params ['nickname']);
- $info ['headimgurl'] = urldecode($this->params ['headimgurl']);
- $info ['passport_uid'] = $this->params ['uid'];
- $cas_uid = $this->params['cas_uid'];
- $back_url = urldecode($this->params['cas_return_url']);
- if(isset($this->params ['return_url'])){
- $this_return_url = urldecode($this->params ['return_url']);
- $arr = $this->convertUrlQuery($this_return_url);
- $back_url = urldecode($arr['cas_return_url']);
- $cas_uid = $arr['cas_uid'];
- }
-
- // 2.绑定用户信息
- if($info ['passport_uid']){
- $userData = $this->objMUser->getUserDataPasspord($info ['passport_uid']);
- if(!$userData){
- $this->objMUser->bindingUser ( $info, $cas_uid );
- $userData = $this->objMUser->get($cas_uid);
- }
- }else{
- $userData = $this->objMUser->get($cas_uid);
- }
- $userData ['key'] = Math::md5_16 ( $cas_uid );
- $userData = json_encode ( $userData );
- Cookie::set ( 'USER_INFO', $userData, 60 * 60 * 24 * 20 ,'.'.$_SERVER['HTTP_HOST']);
-
- header ( "Location: " . $back_url );
- exit ();
- }
-
- /**
- * 校验请求的合法性
- *
- * @param array $request_params
- * @param string $sign
- * @return Boolean
- */
- public function verifySign(array $request_params, $sign) {
- if (empty ( $sign )) {
- return false;
- }
- $new_sign = $this->createSign ( $request_params );
- if (strtolower ( $new_sign ) != strtolower ( $sign )) {
- return false;
- }
- return true;
- }
-
- /**
- * 获取请求的参数集。依赖 REQUEST_METHOD 做判断
- *
- * @return array
- */
- public function getRequestParams() {
- $params = array ();
- if ($_SERVER ['REQUEST_METHOD'] == 'POST') {
- $params = $_POST;
- if (! $params) {
- $params = $_GET;
- }
- } else if ($_SERVER ['REQUEST_METHOD'] == 'GET') {
- $params = $_GET;
- } else {
- throw new Exception ( 'NOT_SUPPORT_REQUEST_METHOD' );
- }
- return $params;
- }
-
- /**
- * 生成请求参数的sign
- *
- * @param array $params
- * @return String
- */
- public function createSign(array $params) {
- if ($params && is_array ( $params )) {
- ksort ( $params );
- $str = $this->key;
- foreach ( $params as $key => $value ) {
- if ($key != 'sign') {
- $str .= $key . $value;
- }
- }
- return strtoupper ( md5 ( $str ) );
- }
- return '';
- }
-
- public function convertUrlQuery($url)
- {
- $arr = parse_url($url);
- $query = $arr['query'];
- $queryParts = explode('&', $query);
-
- $params = array();
- foreach ($queryParts as $param)
- {
- $item = explode('=', $param);
- $params[$item[0]] = $item[1];
- }
-
- return $params;
- }
-
- /**
- * 指定的sign值是否正确
- *
- * @param String $sign
- * 给定需要验证的sign值
- * @param Array $params
- * 用来计算sign值的参数集合
- * @return Boolean
- */
- public function checkSign($sign, $params = null) {
- $sign2 = $this->createSign ( $params );
- return ($sign2 == $sign);
- }
- public function run() {
- $action = $this->action;
- $this->$action ();
- }
- public function display() {
- $this->render ();
- }
- }
|