人人商城

cloudapi.class.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->model('cloud');
  8. load()->func('communication');
  9. class CloudApi {
  10. private $url = 'http://api.we7.cc/index.php?c=%s&a=%s&access_token=%s&';
  11. private $development = false;
  12. private $module = null;
  13. private $sys_call = false;
  14. private $default_token = '91ec1f9324753048c0096d036a694f86';
  15. const ACCESS_TOKEN_EXPIRE_IN = 7200;
  16. public function __construct($development = false) {
  17. if (!defined('MODULE_ROOT')) {
  18. $this->sys_call = true;
  19. $this->module = 'core';
  20. } else {
  21. $this->sys_call = false;
  22. $this->module = pathinfo(MODULE_ROOT, PATHINFO_BASENAME);
  23. }
  24. $this->development = !is_error($this->developerCerContent());
  25. }
  26. private function getCerContent($file) {
  27. $cer_filepath = $this->cer_filepath($file);
  28. if (is_file($cer_filepath)) {
  29. $cer = file_get_contents($cer_filepath);
  30. if (!empty($cer)) {
  31. return $cer;
  32. }
  33. }
  34. return error(1, '获取访问云API的授权数字证书失败.');
  35. }
  36. private function developerCerContent(){
  37. $cer = $this->getCerContent('developer.cer');
  38. if (is_error($cer)) {
  39. return error(1, '访问云API获取授权失败,模块中没有开发者数字证书,请到 <a href="http://s.we7.cc/index.php?c=develop&a=auth" target="_blank">开发者中心</a> 下载数字证书!');
  40. }
  41. return $cer;
  42. }
  43. private function cer_filepath($file) {
  44. if (defined('MODULE_ROOT')) {
  45. return MODULE_ROOT.'/'.$file;
  46. }
  47. return $file;
  48. }
  49. private function moduleCerContent(){
  50. $cer_filename = 'module.cer';
  51. $cer_filepath = $this->cer_filepath($cer_filename);
  52. if (is_file($cer_filepath)) {
  53. $expire_time = filemtime($cer_filepath) + CloudApi::ACCESS_TOKEN_EXPIRE_IN - 200;
  54. if (TIMESTAMP > $expire_time) {
  55. unlink($cer_filepath);
  56. }
  57. }
  58. if (!is_file($cer_filepath)) {
  59. $pars = _cloud_build_params();
  60. $pars['method'] = 'api.oauth';
  61. $pars['module'] = $this->module;
  62. $data = cloud_request('http://v2.addons.we7.cc/gateway.php', $pars);
  63. if (is_error($data)) {
  64. return $data;
  65. }
  66. $data = json_decode($data['content'], true);
  67. if (is_error($data)) {
  68. return $data;
  69. }
  70. }
  71. $cer = $this->getCerContent($cer_filename);
  72. if (is_error($cer)) {
  73. return error(1, '访问云API获取授权失败,模块中未发现数字证书(module.cer).');
  74. }
  75. return $cer;
  76. }
  77. private function systemCerContent(){
  78. global $_W;
  79. if (empty($_W['setting']['site'])) {
  80. return $this->default_token;
  81. }
  82. $cer_filename = 'module.cer';
  83. $cer_filepath = IA_ROOT.'/framework/builtin/core/module.cer';
  84. load()->func('file');
  85. $we7_team_dir = dirname($cer_filepath);
  86. if (!is_dir($we7_team_dir)) {
  87. mkdirs($we7_team_dir);
  88. }
  89. if (is_file($cer_filepath)) {
  90. $expire_time = filemtime($cer_filepath) + CloudApi::ACCESS_TOKEN_EXPIRE_IN - 200;
  91. if (TIMESTAMP > $expire_time) {
  92. unlink($cer_filepath);
  93. }
  94. }
  95. if (!is_file($cer_filepath)) {
  96. $pars = _cloud_build_params();
  97. $pars['method'] = 'api.oauth';
  98. $pars['module'] = $this->module;
  99. $data = cloud_request('http://v2.addons.we7.cc/gateway.php', $pars);
  100. if (is_error($data)) {
  101. return $data;
  102. }
  103. $data = json_decode($data['content'], true);
  104. if (is_error($data)) {
  105. return $data;
  106. }
  107. }
  108. if (is_file($cer_filepath)) {
  109. $cer = file_get_contents($cer_filepath);
  110. if (is_error($cer)) {
  111. return error(1, '访问云API获取授权失败,模块中未发现数字证书(module.cer).');
  112. }
  113. return $cer;
  114. } else {
  115. return $this->default_token;
  116. }
  117. }
  118. private function deleteModuleCer() {
  119. $cer_filename = 'module.cer';
  120. $cer_filepath = $this->cer_filepath($cer_filename);
  121. if (is_file($cer_filepath)) {
  122. unlink($cer_filepath);
  123. }
  124. }
  125. private function getAccessToken(){
  126. global $_W;
  127. if ($this->sys_call) {
  128. $token = $this->systemCerContent();
  129. } else {
  130. if ($this->development) {
  131. $token = $this->developerCerContent();
  132. } else {
  133. $token = $this->moduleCerContent();
  134. }
  135. }
  136. if (empty($token)) {
  137. return error(1, '错误的数字证书内容.');
  138. }
  139. if (is_error($token)) {
  140. return $token;
  141. }
  142. $access_token = array(
  143. 'token' => $token,
  144. 'module' => $this->module,
  145. );
  146. return base64_encode(json_encode($access_token));
  147. }
  148. public function url($api, $method, $params = array(), $dataType = 'json') {
  149. $access_token = $this->getAccessToken();
  150. if (is_error($access_token)) {
  151. return $access_token;
  152. }
  153. if (empty($params) || !is_array($params)) {
  154. $params = array();
  155. }
  156. $url = sprintf($this->url, $api, $method, $access_token);
  157. if (!empty($dataType)) {
  158. $url .= "&dataType={$dataType}";
  159. }
  160. if (!empty($params)) {
  161. $querystring = base64_encode(json_encode($params));
  162. $url .= "&api_qs={$querystring}";
  163. }
  164. if (strlen($url) > 2800) {
  165. return error(1, 'url query string too long');
  166. }
  167. return $url;
  168. }
  169. private function actionResult($result, $dataType = 'json') {
  170. if ($dataType == 'html') {
  171. return $result;
  172. }
  173. if ($dataType == 'json') {
  174. $result = strval($result);
  175. $json_result = json_decode($result, true);
  176. if (is_null($json_result)) {
  177. $json_result = error(1, '返回结果不是有效的JSON');
  178. }
  179. if (is_error($json_result)) {
  180. if ($json_result['errno'] == 10000) {
  181. $this->deleteCer();
  182. $this->deleteModuleCer();
  183. };
  184. return $json_result;
  185. }
  186. return $json_result;
  187. }
  188. return $result;
  189. }
  190. public function get($api, $method, $url_params = array(), $dataType = 'json', $with_cookie = true) {
  191. $url = $this->url($api, $method, $url_params, $dataType);
  192. if (is_error($url)) {
  193. return $url;
  194. }
  195. $response = ihttp_get($url);
  196. if (is_error($response)) {
  197. return $response;
  198. }
  199. if($with_cookie) {
  200. $ihttp_options = array();
  201. if ($response['headers'] && $response['headers']['Set-Cookie']) {
  202. $cookiejar = $response['headers']['Set-Cookie'];
  203. }
  204. if (!empty($cookiejar)) {
  205. if (is_array($cookiejar)) {
  206. $ihttp_options['CURLOPT_COOKIE'] = implode('; ', $cookiejar);
  207. } else {
  208. $ihttp_options['CURLOPT_COOKIE'] = $cookiejar;
  209. }
  210. }
  211. $response = ihttp_request($url, array(), $ihttp_options);
  212. if (is_error($response)) {
  213. return $response;
  214. }
  215. }
  216. $result = $this->actionResult($response['content'], $dataType);
  217. return $result;
  218. }
  219. public function post($api, $method, $post_params = array(), $dataType = 'json', $with_cookie = true) {
  220. $url = $this->url($api, $method, array(), $dataType);
  221. if (is_error($url)) {
  222. return $url;
  223. }
  224. $ihttp_options = array();
  225. if($with_cookie) {
  226. $response = ihttp_get($url);
  227. if (is_error($response)) {
  228. return $response;
  229. }
  230. $ihttp_options = array();
  231. if ($response['headers'] && $response['headers']['Set-Cookie']) {
  232. $cookiejar = $response['headers']['Set-Cookie'];
  233. }
  234. if (!empty($cookiejar)) {
  235. if (is_array($cookiejar)) {
  236. $ihttp_options['CURLOPT_COOKIE'] = implode('; ', $cookiejar);
  237. } else {
  238. $ihttp_options['CURLOPT_COOKIE'] = $cookiejar;
  239. }
  240. }
  241. }
  242. $response = ihttp_request($url, $post_params, $ihttp_options);
  243. if (is_error($response)) {
  244. return $response;
  245. }
  246. if ($dataType == 'binary') {
  247. return $response;
  248. }
  249. return $this->actionResult($response['content'], $dataType);
  250. }
  251. private function deleteCer() {
  252. if($this->sys_call) {
  253. $cer_filepath = IA_ROOT.'/framework/builtin/core/module.cer';
  254. if (is_file($cer_filepath)) {
  255. unlink($cer_filepath);
  256. }
  257. }
  258. }
  259. }