Passport.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Cas\Controller;
  3. use KIF\Cache\Memcached;
  4. use KIF\Core\Config;
  5. use KIF\Core\Request;
  6. use Cas\Dao\UserInfo;
  7. /**
  8. *
  9. * passport 从第三方获取userid
  10. * @author rabin
  11. *
  12. */
  13. class Passport extends Controller{
  14. private $config;
  15. public function __construct() {
  16. $this->config = Config::getInstance()->get('passport');
  17. //$this->objMemcached = new Memcached();
  18. //$this->refer = 'passportRefer';
  19. }
  20. public function doGet() {
  21. $refer = Request::g ( 'referer' );
  22. $cas_uid = Request::g ( 'cas_uid' );
  23. $tokenid = Request::g ( 'tokenid' );
  24. $objUserData = new UserInfo();
  25. $userData = $objUserData -> get($cas_uid);
  26. //$this->objMemcached->set($this->refer, $refer);
  27. $host = $this->config['url'];
  28. $param = $this->config['param'];
  29. /*
  30. if ($param) {
  31. foreach ($param as $k => $v) {
  32. if ($v == '{passport_uid}') {
  33. if (isset($userData['passport_uid']) && $userData['passport_uid']) {
  34. $param[$k] = $userData['passport_uid'];
  35. } else {
  36. $param[$k] = $v = $tokenid;
  37. }
  38. }
  39. if (!$v) {
  40. unset($param[$k]);
  41. }
  42. }
  43. }
  44. */
  45. $param['authorizedCode'] = 'fulishe';
  46. $param['tokenId'] = $tokenid;
  47. //echo $this->config['url'] . "\r\n";
  48. //print_r($param);
  49. $param['tokenId'] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $param['tokenId']);
  50. $data = $this->httpPost($host, $param);
  51. $data = json_decode($data, true);
  52. /*
  53. $data['data'] = array
  54. (
  55. 'nickName' => 'rabin',
  56. 'avatar' => 'http://echarts.baidu.com/images/logo.png',
  57. 'tokenId' => '111111',
  58. );
  59. */
  60. if (isset($data['data']) && isset($data['data']['userId']) && $data['data']['userId']) {
  61. $refer = urldecode($refer);
  62. $refer = str_replace('&amp;', '&', $refer);
  63. $param['nickname'] = $data['data']['nickName'];
  64. $param['headimgurl'] = $data['data']['avatar'];
  65. $param['uid'] = $data['data']['userId'];
  66. $refer .= '&' . http_build_query($param);
  67. header ( "Location: " . $refer);
  68. } else {
  69. echo $tokenid . "<br />";
  70. print_r($data);die;
  71. }
  72. }
  73. private function httpGet($url) {
  74. $curl = curl_init();
  75. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  76. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  77. curl_setopt($curl, CURLOPT_URL, $url);
  78. $res = curl_exec($curl);
  79. curl_close($curl);
  80. return $res;
  81. }
  82. private function httpPost($url, $param) {
  83. $curl = curl_init();
  84. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  85. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  86. curl_setopt($curl, CURLOPT_URL, $url);
  87. curl_setopt($curl, CURLOPT_POST, 1);
  88. curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
  89. $res = curl_exec($curl);
  90. curl_close($curl);
  91. return $res;
  92. }
  93. }