Uc.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\common\controller;
  3. use app\common\library\Auth;
  4. use think\Controller;
  5. use think\Loader;
  6. //UC通信接口基类,抽象类,必须继承使用
  7. define('API_ADDUSER', 1); //note 用户添加 API 接口开关
  8. define('API_DELETEUSER', 1); //note 用户删除 API 接口开关
  9. define('API_GETTAG', 1); //note 获取标签 API 接口开关
  10. define('API_SYNLOGIN', 1); //note 同步登录 API 接口开关
  11. define('API_SYNLOGOUT', 1); //note 同步登出 API 接口开关
  12. define('API_UPDATEPW', 1); //note 更改用户密码 开关
  13. define('API_UPDATEINFO', 1); //note 更改用户信息 开关
  14. define('API_UPDATEBADWORDS', 1); //note 更新关键字列表 开关
  15. define('API_UPDATEHOSTS', 1); //note 更新域名解析缓存 开关
  16. define('API_UPDATEAPPS', 1); //note 更新应用列表 开关
  17. define('API_UPDATECLIENT', 1); //note 更新客户端缓存 开关
  18. define('API_UPDATECREDIT', 1); //note 更新用户积分 开关
  19. define('API_GETCREDITSETTINGS', 1); //note 向 UCenter 提供积分设置 开关
  20. define('API_GETCREDIT', 1); //note 获取用户的某项积分 开关
  21. define('API_UPDATECREDITSETTINGS', 1); //note 更新应用积分设置 开关
  22. define('API_RETURN_SUCCEED', '1');
  23. define('API_RETURN_FAILED', '-1');
  24. define('API_RETURN_FORBIDDEN', '-2');
  25. abstract class Uc extends Controller
  26. {
  27. /**
  28. *
  29. * @var \app\common\library\Auth
  30. */
  31. protected $user = null;
  32. public $code; //code参数原始字符串
  33. public $action; //解析code得到的动作名
  34. public $error = NULL;
  35. public $get; //get数据
  36. public $post; //post数据
  37. protected $appdir; //uc_client所在目录
  38. /**
  39. * 初始化方法
  40. */
  41. public function _initialize()
  42. {
  43. parent::_initialize();
  44. $modulename = $this->request->module();
  45. $controllername = strtolower($this->request->controller());
  46. $actionname = strtolower($this->request->action());
  47. $path = '/' . $modulename . '/' . str_replace('.', '/', $controllername) . '/' . $actionname;
  48. $this->user = Auth::instance();
  49. // 设置当前请求的URI
  50. $this->user->setRequestUri($path);
  51. // 检测当前是否登录并进行初始化
  52. //$this->user->init();
  53. //
  54. //加载UC函数库
  55. //加载UC XML类库
  56. Loader::import('fast.ucenter.common.Functions');
  57. Loader::import('fast.ucenter.common.XML');
  58. $this->initConfig(); //初始化UC应用配置
  59. $this->initRequest(); //初始化请求
  60. }
  61. function initConfig()
  62. {
  63. if (!defined('UC_API'))
  64. {
  65. $this->error('未发现uc常量配置信息');
  66. }
  67. }
  68. function _serialize($arr, $htmlon = 0)
  69. {
  70. return xml_serialize($arr, $htmlon);
  71. }
  72. /**
  73. * 解析请求
  74. * @return boolean
  75. */
  76. public function initRequest()
  77. {
  78. $code = $this->request->get('code');
  79. parse_str(_uc_authcode($code, 'DECODE', UC_KEY), $get);
  80. if (get_magic_quotes_gpc())
  81. {
  82. $get = _uc_stripslashes($get);
  83. }
  84. if (empty($get) || !isset($get['time']) || !isset($get['action']))
  85. {
  86. $this->error = '非法请求';
  87. return false;
  88. }
  89. $timestamp = time();
  90. if ($timestamp - $get['time'] > 36001111)
  91. {
  92. $this->error = '请求有效期已过';
  93. return false;
  94. }
  95. $requestdata = file_get_contents('php://input');
  96. $this->code = $code;
  97. $this->action = strtolower(parse_name($get['action'], '1'));
  98. $this->get = $get;
  99. $this->post = @xml_unserialize($requestdata);
  100. $this->appdir = EXTEND_PATH . 'fast/ucenter/client/';
  101. // 定义允许请求的接口
  102. $allowaction = ['test', 'adduser', 'deleteuser', 'gettag', 'synregister', 'synlogin', 'synlogout', 'updateinfo', 'updatebadwords', 'updatehosts', 'updateapps', 'updateclient', 'updatecredit', 'getcreditsettings', 'updatecreditsettings'];
  103. if (!in_array($this->action, $allowaction))
  104. {
  105. $this->error = '请求不允许';
  106. return false;
  107. }
  108. }
  109. /**
  110. * 响应ucserver的通信请求,调用相应方法,输出最终结果并结束整个流程
  111. */
  112. public function response()
  113. {
  114. if ($this->_before_response())
  115. {
  116. if ($this->error !== NULL)
  117. {
  118. exit($this->error);
  119. }
  120. $response = call_user_func(array($this, $this->action));
  121. }
  122. if ($this->_after_response($response))
  123. {
  124. exit($response);
  125. }
  126. exit('-1');
  127. }
  128. protected function _before_response()
  129. {
  130. return true;
  131. }
  132. protected function _after_response($response = "")
  133. {
  134. return true;
  135. }
  136. public function test()
  137. {
  138. return API_RETURN_SUCCEED;
  139. }
  140. }