cloudapi.class.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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-old.w7.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="https://dev.w7.cc/numkey" 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 = md5('module' . complex_authkey()) . '.cer';
  51. $cer_filepath = $this->cer_filepath($cer_filename);
  52. if (is_file($cer_filepath)) {
  53. $expire_time = filemtime($cer_filepath) + self::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://api-upgrade.w7.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获取授权失败,模块中未发现数字证书(' . md5('module' . complex_authkey()) . '.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_filepath = IA_ROOT . '/framework/builtin/core/' . md5(complex_authkey()) . '.cer';
  83. load()->func('file');
  84. $we7_team_dir = dirname($cer_filepath);
  85. if (!is_dir($we7_team_dir)) {
  86. mkdirs($we7_team_dir);
  87. }
  88. if (is_file($cer_filepath)) {
  89. $expire_time = filemtime($cer_filepath) + self::ACCESS_TOKEN_EXPIRE_IN - 200;
  90. if (TIMESTAMP > $expire_time) {
  91. unlink($cer_filepath);
  92. }
  93. }
  94. if (!is_file($cer_filepath)) {
  95. $pars = _cloud_build_params();
  96. $pars['method'] = 'api.oauth';
  97. $pars['module'] = $this->module;
  98. $data = cloud_request('http://api-upgrade.w7.cc/gateway.php', $pars);
  99. if (is_error($data)) {
  100. return $data;
  101. }
  102. $data = json_decode($data['content'], true);
  103. if (is_error($data)) {
  104. return $data;
  105. }
  106. }
  107. if (is_file($cer_filepath)) {
  108. $cer = file_get_contents($cer_filepath);
  109. if (is_error($cer)) {
  110. return error(1, '访问云API获取授权失败,模块中未发现数字证书(' . md5('module' . complex_authkey()) . '.cer).');
  111. }
  112. return $cer;
  113. } else {
  114. return $this->default_token;
  115. }
  116. }
  117. private function deleteModuleCer() {
  118. $cer_filename = md5('module' . complex_authkey()) . '.cer';
  119. $cer_filepath = $this->cer_filepath($cer_filename);
  120. if (is_file($cer_filepath)) {
  121. unlink($cer_filepath);
  122. }
  123. }
  124. private function getAccessToken() {
  125. global $_W;
  126. if ($this->sys_call) {
  127. $token = $this->systemCerContent();
  128. } else {
  129. if ($this->development) {
  130. $token = $this->developerCerContent();
  131. } else {
  132. $token = $this->moduleCerContent();
  133. }
  134. }
  135. if (empty($token)) {
  136. return error(1, '错误的数字证书内容.');
  137. }
  138. if (is_error($token)) {
  139. return $token;
  140. }
  141. $access_token = array(
  142. 'token' => $token,
  143. 'module' => $this->module,
  144. );
  145. return base64_encode(json_encode($access_token));
  146. }
  147. public function url($api, $method, $params = array(), $dataType = 'json') {
  148. $access_token = $this->getAccessToken();
  149. if (is_error($access_token)) {
  150. return $access_token;
  151. }
  152. if (empty($params) || !is_array($params)) {
  153. $params = array();
  154. }
  155. $url = sprintf($this->url, $api, $method, $access_token);
  156. if (!empty($dataType)) {
  157. $url .= "&dataType={$dataType}";
  158. }
  159. if (!empty($params)) {
  160. $querystring = base64_encode(json_encode($params));
  161. $url .= "&api_qs={$querystring}";
  162. }
  163. if (strlen($url) > 2800) {
  164. return error(1, 'url query string too long');
  165. }
  166. return $url;
  167. }
  168. private function actionResult($result, $dataType = 'json') {
  169. if ('html' == $dataType) {
  170. return $result;
  171. }
  172. if ('json' == $dataType) {
  173. $result = strval($result);
  174. $json_result = json_decode($result, true);
  175. if (is_null($json_result)) {
  176. $json_result = error(1, '返回结果不是有效的JSON');
  177. }
  178. if (is_error($json_result)) {
  179. if (10000 == $json_result['errno']) {
  180. $this->deleteCer();
  181. $this->deleteModuleCer();
  182. }
  183. return $json_result;
  184. }
  185. return $json_result;
  186. }
  187. return $result;
  188. }
  189. public function get($api, $method, $url_params = array(), $dataType = 'json', $with_cookie = true) {
  190. $url = $this->url($api, $method, $url_params, $dataType);
  191. if (is_error($url)) {
  192. return $url;
  193. }
  194. $response = ihttp_get($url);
  195. if (is_error($response)) {
  196. return $response;
  197. }
  198. if ($with_cookie) {
  199. $ihttp_options = array();
  200. if ($response['headers'] && $response['headers']['Set-Cookie']) {
  201. $cookiejar = $response['headers']['Set-Cookie'];
  202. }
  203. if (!empty($cookiejar)) {
  204. if (is_array($cookiejar)) {
  205. $ihttp_options['CURLOPT_COOKIE'] = implode('; ', $cookiejar);
  206. } else {
  207. $ihttp_options['CURLOPT_COOKIE'] = $cookiejar;
  208. }
  209. }
  210. $response = ihttp_request($url, array(), $ihttp_options);
  211. if (is_error($response)) {
  212. return $response;
  213. }
  214. }
  215. $result = $this->actionResult($response['content'], $dataType);
  216. return $result;
  217. }
  218. public function post($api, $method, $post_params = array(), $dataType = 'json', $with_cookie = true) {
  219. $url = $this->url($api, $method, array(), $dataType);
  220. if (is_error($url)) {
  221. return $url;
  222. }
  223. $ihttp_options = array();
  224. if ($with_cookie) {
  225. $response = ihttp_get($url);
  226. if (is_error($response)) {
  227. return $response;
  228. }
  229. $ihttp_options = array();
  230. if ($response['headers'] && $response['headers']['Set-Cookie']) {
  231. $cookiejar = $response['headers']['Set-Cookie'];
  232. }
  233. if (!empty($cookiejar)) {
  234. if (is_array($cookiejar)) {
  235. $ihttp_options['CURLOPT_COOKIE'] = implode('; ', $cookiejar);
  236. } else {
  237. $ihttp_options['CURLOPT_COOKIE'] = $cookiejar;
  238. }
  239. }
  240. }
  241. $response = ihttp_request($url, $post_params, $ihttp_options);
  242. if (is_error($response)) {
  243. return $response;
  244. }
  245. if ('binary' == $dataType) {
  246. return $response;
  247. }
  248. return $this->actionResult($response['content'], $dataType);
  249. }
  250. public function deleteCer() {
  251. if ($this->sys_call) {
  252. $files = file_tree(IA_ROOT . '/framework/builtin/core');
  253. foreach ($files as $key => $value) {
  254. if (strpos($value, '.cer')) {
  255. unlink($value);
  256. }
  257. }
  258. }
  259. }
  260. }