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