Restful.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: Restful.php 33750 2013-08-09 09:56:01Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. Cloud::loadFile('Service_Client_RestfulException');
  12. abstract class Cloud_Service_Client_Restful {
  13. protected $_cloudApiIp = '';
  14. protected $_sId = 0;
  15. protected $_sKey = '';
  16. protected $_url = 'http://api.discuz.qq.com/site.php';
  17. protected $_format = 'PHP';
  18. protected $_ts = 0;
  19. protected $_debug = false;
  20. protected $_batchParams = array();
  21. public $errorCode = 0;
  22. public $errorMessage = '';
  23. public $my_status = false;
  24. public $cloud_status = false;
  25. public $siteName = '';
  26. public $uniqueId = '';
  27. public $siteUrl = '';
  28. public $charset = '';
  29. public $timeZone = 0;
  30. public $UCenterUrl = '';
  31. public $language = '';
  32. public $productType = '';
  33. public $productVersion = '';
  34. public $productRelease = '';
  35. public $apiVersion = '';
  36. public $siteUid = 0;
  37. protected static $_instance;
  38. public static function getInstance($debug = false) {
  39. if (!(self::$_instance instanceof self)) {
  40. self::$_instance = new self($debug);
  41. }
  42. return self::$_instance;
  43. }
  44. public function __construct($debug = false) {
  45. $this->_debug = $debug;
  46. $this->_initSiteEnv();
  47. }
  48. protected function _initSiteEnv() {
  49. global $_G;
  50. require_once DISCUZ_ROOT.'./source/discuz_version.php';
  51. $this->my_status = !empty($_G['setting']['my_app_status']) ? $_G['setting']['my_app_status'] : '';
  52. $this->cloud_status = !empty($_G['setting']['cloud_status']) ? $_G['setting']['cloud_status'] : '';
  53. $this->_sId = !empty($_G['setting']['my_siteid']) ? $_G['setting']['my_siteid'] : '';
  54. $this->_sKey = !empty($_G['setting']['my_sitekey']) ? $_G['setting']['my_sitekey'] : '';
  55. $this->_ts = TIMESTAMP;
  56. $this->siteName = !empty($_G['setting']['bbname']) ? $_G['setting']['bbname'] : '';
  57. $this->uniqueId = $_G['setting']['siteuniqueid'];
  58. $this->siteUrl = $_G['siteurl'];
  59. $this->charset = CHARSET;
  60. $this->timeZone = !empty($_G['setting']['timeoffset']) ? $_G['setting']['timeoffset'] : '';
  61. $this->UCenterUrl = !empty($_G['setting']['ucenterurl']) ? $_G['setting']['ucenterurl'] : '';
  62. $this->language = $_G['config']['output']['language'] ? $_G['config']['output']['language'] : 'zh_CN';
  63. $this->productType = 'DISCUZX';
  64. $this->productVersion = defined('DISCUZ_VERSION') ? DISCUZ_VERSION : '';
  65. $this->productRelease = defined('DISCUZ_RELEASE') ? DISCUZ_RELEASE : '';
  66. $utilService = Cloud::loadClass('Service_Util');
  67. $this->apiVersion = $utilService->getApiVersion();
  68. $this->siteUid = $_G['uid'];
  69. if ($_G['setting']['cloud_api_ip']) {
  70. $this->setCloudApiIp($_G['setting']['cloud_api_ip']);
  71. }
  72. }
  73. protected function _callMethod($method, $args, $isBatch = false, $return = false) {
  74. $this->errorCode = 0;
  75. $this->errorMessage = '';
  76. $url = $this->_url;
  77. $avgDomain = explode('.', $method);
  78. switch ($avgDomain[0]) {
  79. case 'site':
  80. $url = 'http://api.discuz.qq.com/site_cloud.php';
  81. break;
  82. case 'qqgroup':
  83. $url = 'http://api.discuz.qq.com/site_qqgroup.php';
  84. break;
  85. case 'connect':
  86. $url = 'http://api.discuz.qq.com/site_connect.php';
  87. break;
  88. case 'security':
  89. $url = 'http://api.discuz.qq.com/site_security.php';
  90. break;
  91. default:
  92. $url = $this->_url;
  93. }
  94. $params = array();
  95. $params['sId'] = $this->_sId;
  96. $params['method'] = $method;
  97. $params['format'] = strtoupper($this->_format);
  98. $params['sig'] = $this->_generateSig($params, $method, $args);
  99. $params['ts'] = $this->_ts;
  100. $postData = $this->_createPostData($params, $args);
  101. if ($isBatch) {
  102. $this->_batchParams[] = $postData;
  103. return true;
  104. } else {
  105. $utilService = Cloud::loadClass('Service_Util');
  106. $postString = $utilService->httpBuildQuery($postData, '', '&');
  107. $result = $this->_postRequest($url, $postString);
  108. if ($this->_debug) {
  109. $this->_message('receive data ' . dhtmlspecialchars($result) . "\n\n");
  110. }
  111. if(!$return) {
  112. return $this->_parseResponse($result, false, $return);
  113. } else {
  114. $response = @dunserialize($result);
  115. if(!is_array($response)) {
  116. return $result;
  117. } else {
  118. return $response;
  119. }
  120. }
  121. }
  122. }
  123. protected function _parseResponse($response, $isBatch = false) {
  124. if (!$response) {
  125. $this->_unknowErrorMessage();
  126. }
  127. $response = @dunserialize($response);
  128. if (!is_array($response)) {
  129. $this->_unknowErrorMessage();
  130. }
  131. if ($response['errCode']) {
  132. $this->errorCode = $response['errCode'];
  133. $this->errorMessage = $response['errMessage'];
  134. throw new Cloud_Service_Client_RestfulException($response['errMessage'], $response['errCode']);
  135. }
  136. if (!isset($response['result']) && !isset($response['batchResult'])) {
  137. $this->_unknowErrorMessage();
  138. }
  139. if ($isBatch) {
  140. return $response['batchResult'];
  141. } else {
  142. return $response['result'];
  143. }
  144. }
  145. public function runBatchMethod() {
  146. if (!$this->_batchParams) {
  147. return false;
  148. }
  149. $postData = array('batchParams' => $this->_batchParams);
  150. $utilService = Cloud::loadClass('Service_Util');
  151. $postString = $utilService->httpBuildQuery($postData, '', '&');
  152. $result = $this->_postRequest($this->_url, $postString);
  153. if ($this->_debug) {
  154. $this->_message('receive data ' . dhtmlspecialchars($result) . "\n\n");
  155. }
  156. return $this->_parseResponse($result, true);
  157. }
  158. protected function _unknowErrorMessage() {
  159. $this->errorCode = 1;
  160. $this->errorMessage = 'An unknown error occurred. May be DNS Error. ';
  161. throw new Cloud_Service_Client_RestfulException($this->errorMessage, $this->errorCode);
  162. }
  163. protected function _generateSig($params, $method, $args) {
  164. $postData = $this->_createPostData($params, $args);
  165. $utilService = Cloud::loadClass('Service_Util');
  166. $postString = $utilService->httpBuildQuery($postData, '', '&');
  167. if ($this->_debug) {
  168. $this->_message('sig string: ' . $postString . '|' . $this->_sKey . '|' . $this->_ts . "\n\n");
  169. }
  170. return md5(sprintf('%s|%s|%s', $postString, $this->_sKey, $this->_ts));
  171. }
  172. protected function _createPostData($params, $args) {
  173. ksort($params);
  174. ksort($args);
  175. $params['args'] = $args;
  176. return $params;
  177. }
  178. protected function _postRequest($url, $data, $ip = '') {
  179. if ($this->_debug) {
  180. $this->_message('post params: ' . $data. "\n\n");
  181. }
  182. $ip = $this->_cloudApiIp;
  183. $result = $this->_fsockopen($url, 0, $data, '', false, $ip, 5);
  184. return $result;
  185. }
  186. function _fsockopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = false, $ip = '', $timeout = 15, $block = true) {
  187. return dfsockopen($url, $limit, $post, $cookie, $bysocket, $ip, $timeout, $block);
  188. }
  189. protected function _message($msg) {
  190. echo $msg;
  191. }
  192. public function setCloudApiIp($ip) {
  193. $this->_cloudApiIp = $ip;
  194. return true;
  195. }
  196. protected function getUserOpenId($uid) {
  197. $openId = '';
  198. try {
  199. $connectInfo = C::t('#qqconnect#common_member_connect')->fetch($uid);
  200. if($connectInfo) {
  201. $openId = $connectInfo['conopenid'];
  202. }
  203. } catch (Exception $e) {}
  204. return $openId;
  205. }
  206. protected function getUserDeviceToken($uids) {
  207. $uids = (array)$uids;
  208. $deviceToken = array();
  209. try {
  210. $query = DB::query('SELECT * FROM '.DB::table('common_devicetoken').' WHERE uid IN('.dimplode($uids).')', array(), true);
  211. while($value = DB::fetch($query)) {
  212. $deviceToken[$value['uid']][] = $value['token'];
  213. }
  214. } catch (Exception $e) {}
  215. return $deviceToken;
  216. }
  217. }