SetUser.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace Cas\Controller\API;
  3. use KIF\Core\Controller;
  4. use KIF\Core\Request;
  5. use Cas\Module\User;
  6. use Cas\Dao\UserInfo;
  7. use KIF\Math\Math;
  8. use KIF\Cookie;
  9. /**
  10. * 关于用户和passport接口
  11. *
  12. * @author lihuanchun
  13. *
  14. */
  15. class SetUser extends Controller {
  16. private $key;
  17. private $params;
  18. private $objMUser;
  19. public function __construct() {
  20. $this->key = 'rD5carfCGbHAhF';
  21. $this->params = $this->getRequestParams ();
  22. /*
  23. if (! $this->checkSign ( $params ['sign'], $params )) {
  24. //exit ( 'Sign error' );
  25. }
  26. */
  27. $this->objMUser = new User ();
  28. }
  29. /**
  30. * 用户登录caozuo
  31. */
  32. public function doRepLogin(){
  33. }
  34. /**
  35. * 设置用户
  36. * 请求地址:http://cas.lishuy.com/index.php?c=API_Req&a=Page&sign=51CE47C9A3681845D5ACCF3728DABD6A
  37. * http://cas.lishuy.com/API_SetUser_Req/?sign=51CE47C9A3681845D5ACCF3728DABD6A
  38. * 参数:
  39. * passport_uid 对方UID
  40. * cas_uid 本平台UID
  41. * nickname 用户昵称
  42. * headimgurl 用户头像地址
  43. * sign 加密串
  44. *
  45. * 返回
  46. */
  47. public function doReq() {
  48. // 1.获取传递来的相关信息
  49. $info ['nickname'] = urlencode($this->params ['nickname']);
  50. $info ['headimgurl'] = urlencode($this->params ['headimgurl']);
  51. $info ['passport_uid'] = $this->params ['passport_uid'];
  52. $cas_uid = $this->params ['cas_uid'];
  53. // 2.绑定用户信息
  54. if ($this->objMUser->bindingUser ( $info, $cas_uid )) {
  55. exit ( 'success' );
  56. }
  57. ;
  58. exit ( 'fail' );
  59. }
  60. /**
  61. * 统一着陆页面
  62. * 页面地址:http://cas.lishuy.com/index.php?c=API_SetUser&a=Page&sign=51CE47C9A3681845D5ACCF3728DABD6A
  63. * http://cas.lishuy.com/API_SetUser_Page/?sign=51CE47C9A3681845D5ACCF3728DABD6A
  64. * 参数:
  65. * passport_uid 对方UID
  66. * cas_uid 本平台UID
  67. * nickname 用户昵称
  68. * headimgurl 用户头像地址
  69. * return_url 原跳转地址
  70. * sign 加密串
  71. */
  72. public function doPage() {
  73. // 1.获取传递来的相关信息
  74. $info ['nickname'] = urldecode($this->params ['nickname']);
  75. $info ['headimgurl'] = urldecode($this->params ['headimgurl']);
  76. $info ['passport_uid'] = $this->params ['uid'];
  77. $cas_uid = $this->params['cas_uid'];
  78. $back_url = urldecode($this->params['cas_return_url']);
  79. if(isset($this->params ['return_url'])){
  80. $this_return_url = urldecode($this->params ['return_url']);
  81. $arr = $this->convertUrlQuery($this_return_url);
  82. $back_url = urldecode($arr['cas_return_url']);
  83. $cas_uid = $arr['cas_uid'];
  84. }
  85. // 2.绑定用户信息
  86. if($info ['passport_uid']){
  87. $userData = $this->objMUser->getUserDataPasspord($info ['passport_uid']);
  88. if(!$userData){
  89. $this->objMUser->bindingUser ( $info, $cas_uid );
  90. $userData = $this->objMUser->get($cas_uid);
  91. }
  92. }else{
  93. $userData = $this->objMUser->get($cas_uid);
  94. }
  95. $userData ['key'] = Math::md5_16 ( $cas_uid );
  96. $userData = json_encode ( $userData );
  97. Cookie::set ( 'USER_INFO', $userData, 60 * 60 * 24 * 20 ,'.'.$_SERVER['HTTP_HOST']);
  98. header ( "Location: " . $back_url );
  99. exit ();
  100. }
  101. /**
  102. * 校验请求的合法性
  103. *
  104. * @param array $request_params
  105. * @param string $sign
  106. * @return Boolean
  107. */
  108. public function verifySign(array $request_params, $sign) {
  109. if (empty ( $sign )) {
  110. return false;
  111. }
  112. $new_sign = $this->createSign ( $request_params );
  113. if (strtolower ( $new_sign ) != strtolower ( $sign )) {
  114. return false;
  115. }
  116. return true;
  117. }
  118. /**
  119. * 获取请求的参数集。依赖 REQUEST_METHOD 做判断
  120. *
  121. * @return array
  122. */
  123. public function getRequestParams() {
  124. $params = array ();
  125. if ($_SERVER ['REQUEST_METHOD'] == 'POST') {
  126. $params = $_POST;
  127. if (! $params) {
  128. $params = $_GET;
  129. }
  130. } else if ($_SERVER ['REQUEST_METHOD'] == 'GET') {
  131. $params = $_GET;
  132. } else {
  133. throw new Exception ( 'NOT_SUPPORT_REQUEST_METHOD' );
  134. }
  135. return $params;
  136. }
  137. /**
  138. * 生成请求参数的sign
  139. *
  140. * @param array $params
  141. * @return String
  142. */
  143. public function createSign(array $params) {
  144. if ($params && is_array ( $params )) {
  145. ksort ( $params );
  146. $str = $this->key;
  147. foreach ( $params as $key => $value ) {
  148. if ($key != 'sign') {
  149. $str .= $key . $value;
  150. }
  151. }
  152. return strtoupper ( md5 ( $str ) );
  153. }
  154. return '';
  155. }
  156. public function convertUrlQuery($url)
  157. {
  158. $arr = parse_url($url);
  159. $query = $arr['query'];
  160. $queryParts = explode('&', $query);
  161. $params = array();
  162. foreach ($queryParts as $param)
  163. {
  164. $item = explode('=', $param);
  165. $params[$item[0]] = $item[1];
  166. }
  167. return $params;
  168. }
  169. /**
  170. * 指定的sign值是否正确
  171. *
  172. * @param String $sign
  173. * 给定需要验证的sign值
  174. * @param Array $params
  175. * 用来计算sign值的参数集合
  176. * @return Boolean
  177. */
  178. public function checkSign($sign, $params = null) {
  179. $sign2 = $this->createSign ( $params );
  180. return ($sign2 == $sign);
  181. }
  182. public function run() {
  183. $action = $this->action;
  184. $this->$action ();
  185. }
  186. public function display() {
  187. $this->render ();
  188. }
  189. }