cloudapi.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. return MODULE_ROOT.'/'.$file;
  45. }
  46. private function moduleCerContent(){
  47. $cer_filename = 'module.cer';
  48. $cer_filepath = $this->cer_filepath($cer_filename);
  49. if (is_file($cer_filepath)) {
  50. $expire_time = filemtime($cer_filepath) + CloudApi::ACCESS_TOKEN_EXPIRE_IN - 200;
  51. if (TIMESTAMP > $expire_time) {
  52. unlink($cer_filepath);
  53. }
  54. }
  55. if (!is_file($cer_filepath)) {
  56. $pars = _cloud_build_params();
  57. $pars['method'] = 'api.oauth';
  58. $pars['module'] = $this->module;
  59. $data = cloud_request('http://v2.addons.we7.cc/gateway.php', $pars);
  60. if (is_error($data)) {
  61. return $data;
  62. }
  63. $data = json_decode($data['content'], true);
  64. if (is_error($data)) {
  65. return $data;
  66. }
  67. }
  68. $cer = $this->getCerContent($cer_filename);
  69. if (is_error($cer)) {
  70. return error(1, '访问云API获取授权失败,模块中未发现数字证书(module.cer).');
  71. }
  72. return $cer;
  73. }
  74. private function systemCerContent(){
  75. global $_W;
  76. if (empty($_W['setting']['site'])) {
  77. return $this->default_token;
  78. }
  79. $cer_filename = 'module.cer';
  80. $cer_filepath = IA_ROOT.'/framework/builtin/core/module.cer';
  81. load()->func('file');
  82. $we7_team_dir = dirname($cer_filepath);
  83. if (!is_dir($we7_team_dir)) {
  84. mkdirs($we7_team_dir);
  85. }
  86. if (is_file($cer_filepath)) {
  87. $expire_time = filemtime($cer_filepath) + CloudApi::ACCESS_TOKEN_EXPIRE_IN - 200;
  88. if (TIMESTAMP > $expire_time) {
  89. unlink($cer_filepath);
  90. }
  91. }
  92. if (!is_file($cer_filepath)) {
  93. $pars = _cloud_build_params();
  94. $pars['method'] = 'api.oauth';
  95. $pars['module'] = $this->module;
  96. $data = cloud_request('http://v2.addons.we7.cc/gateway.php', $pars);
  97. if (is_error($data)) {
  98. return $data;
  99. }
  100. $data = json_decode($data['content'], true);
  101. if (is_error($data)) {
  102. return $data;
  103. }
  104. }
  105. if (is_file($cer_filepath)) {
  106. $cer = file_get_contents($cer_filepath);
  107. if (is_error($cer)) {
  108. return error(1, '访问云API获取授权失败,模块中未发现数字证书(module.cer).');
  109. }
  110. return $cer;
  111. } else {
  112. return $this->default_token;
  113. }
  114. }
  115. private function deleteModuleCer() {
  116. $cer_filename = 'module.cer';
  117. $cer_filepath = $this->cer_filepath($cer_filename);
  118. if (is_file($cer_filepath)) {
  119. unlink($cer_filepath);
  120. }
  121. }
  122. private function getAccessToken(){
  123. global $_W;
  124. if ($this->sys_call) {
  125. $token = $this->systemCerContent();
  126. } else {
  127. if ($this->development) {
  128. $token = $this->developerCerContent();
  129. } else {
  130. $token = $this->moduleCerContent();
  131. }
  132. }
  133. if (empty($token)) {
  134. return error(1, '错误的数字证书内容.');
  135. }
  136. if (is_error($token)) {
  137. return $token;
  138. }
  139. $access_token = array(
  140. 'token' => $token,
  141. 'module' => $this->module,
  142. );
  143. return base64_encode(json_encode($access_token));
  144. }
  145. public function url($api, $method, $params = array(), $dataType = 'json') {
  146. $access_token = $this->getAccessToken();
  147. if (is_error($access_token)) {
  148. return $access_token;
  149. }
  150. if (empty($params) || !is_array($params)) {
  151. $params = array();
  152. }
  153. $url = sprintf($this->url, $api, $method, $access_token);
  154. if (!empty($dataType)) {
  155. $url .= "&dataType={$dataType}";
  156. }
  157. if (!empty($params)) {
  158. $querystring = base64_encode(json_encode($params));
  159. $url .= "&api_qs={$querystring}";
  160. }
  161. if (strlen($url) > 2800) {
  162. return error(1, 'url query string too long');
  163. }
  164. return $url;
  165. }
  166. private function actionResult($result, $dataType = 'json') {
  167. if ($dataType == 'html') {
  168. return $result;
  169. }
  170. if ($dataType == 'json') {
  171. $result = strval($result);
  172. $json_result = json_decode($result, true);
  173. if (is_null($json_result)) {
  174. $json_result = error(1, '返回结果不是有效的JSON');
  175. }
  176. if (is_error($json_result)) {
  177. if ($json_result['errno'] == 10000) {
  178. $this->deleteModuleCer();
  179. };
  180. if($json_result['errno'] == 1) {
  181. $this->deleteCer();
  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. $this->deleteCer();
  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. $this->deleteCer();
  214. return $response;
  215. }
  216. }
  217. $result = $this->actionResult($response['content'], $dataType);
  218. return $result;
  219. }
  220. public function post($api, $method, $post_params = array(), $dataType = 'json', $with_cookie = true) {
  221. $url = $this->url($api, $method, array(), $dataType);
  222. if (is_error($url)) {
  223. return $url;
  224. }
  225. $ihttp_options = array();
  226. if($with_cookie) {
  227. $response = ihttp_get($url);
  228. if (is_error($response)) {
  229. $this->deleteCer();
  230. return $response;
  231. }
  232. $ihttp_options = array();
  233. if ($response['headers'] && $response['headers']['Set-Cookie']) {
  234. $cookiejar = $response['headers']['Set-Cookie'];
  235. }
  236. if (!empty($cookiejar)) {
  237. if (is_array($cookiejar)) {
  238. $ihttp_options['CURLOPT_COOKIE'] = implode('; ', $cookiejar);
  239. } else {
  240. $ihttp_options['CURLOPT_COOKIE'] = $cookiejar;
  241. }
  242. }
  243. }
  244. $response = ihttp_request($url, $post_params, $ihttp_options);
  245. if (is_error($response)) {
  246. $this->deleteCer();
  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. }