PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/model/cloud.mod.php

https://gitlab.com/llqbaimao/o2o
PHP | 404 lines | 375 code | 20 blank | 9 comment | 89 complexity | 01b167be954023377a7f25d4927a89b4 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  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()->func('communication');
  8. function cloud_client_define() {
  9. return array(
  10. '/framework/function/communication.func.php',
  11. '/framework/model/cloud.mod.php',
  12. '/web/source/cloud/upgrade.ctrl.php',
  13. '/web/source/cloud/process.ctrl.php',
  14. '/web/source/cloud/dock.ctrl.php',
  15. '/web/themes/default/cloud/upgrade.html',
  16. '/web/themes/default/cloud/process.html'
  17. );
  18. }
  19. function cloud_prepare() {
  20. global $_W;
  21. setting_load('site');
  22. if(empty($_W['setting']['site']['key']) || empty($_W['setting']['site']['token'])) {
  23. return error('-1', "您的程序需要在微擎云服务平台注册你的站点资料, 来接入云平台服务后才能使用相应功能.");
  24. }
  25. return true;
  26. }
  27. function cloud_m_prepare($name) {
  28. $pars['method'] = 'module.check';
  29. $pars['module'] = $name;
  30. $dat = ihttp_post('http://v2.addons.we7.cc/gateway.php', $pars);
  31. if (is_error($dat)) {
  32. return $dat;
  33. }
  34. /*
  35. if ($dat['content'] == 'install-module-protect') {
  36. return error('-1', '此模块已设置版权保护,您只能通过云平台来安装。');
  37. }
  38. */
  39. return true;
  40. }
  41. function _cloud_build_params() {
  42. global $_W;
  43. $pars = array();
  44. $pars['host'] = $_SERVER['HTTP_HOST'];
  45. $pars['family'] = IMS_FAMILY;
  46. $pars['version'] = IMS_VERSION;
  47. $pars['release'] = IMS_RELEASE_DATE;
  48. $pars['key'] = $_W['setting']['site']['key'];
  49. $pars['password'] = md5($_W['setting']['site']['key'] . $_W['setting']['site']['token']);
  50. $clients = cloud_client_define();
  51. $string = '';
  52. foreach($clients as $cli) {
  53. $string .= md5_file(IA_ROOT . $cli);
  54. }
  55. $pars['client'] = md5($string);
  56. return $pars;
  57. }
  58. function cloud_m_build($modulename) {
  59. $sql = 'SELECT * FROM ' . tablename('modules') . ' WHERE `name`=:name';
  60. $module = pdo_fetch($sql, array(':name' => $modulename));
  61. $pars = _cloud_build_params();
  62. $pars['method'] = 'module.build';
  63. $pars['module'] = $modulename;
  64. if(!empty($module)) {
  65. $pars['module_version'] = $module['version'];
  66. }
  67. $dat = ihttp_post('http://v2.addons.we7.cc/gateway.php', $pars);
  68. $file = IA_ROOT . '/data/module.build';
  69. $ret = _cloud_shipping_parse($dat, $file);
  70. if(!is_error($ret)) {
  71. $dir = IA_ROOT . '/addons/' . $modulename;
  72. $files = array();
  73. if(!empty($ret['files'])) {
  74. foreach($ret['files'] as $file) {
  75. $entry = $dir . $file['path'];
  76. if(!is_file($entry) || md5_file($entry) != $file['checksum']) {
  77. $files[] = '/'. $modulename . $file['path'];
  78. }
  79. }
  80. }
  81. $ret['files'] = $files;
  82. $schemas = array();
  83. if(!empty($ret['schemas'])) {
  84. load()->func('db');
  85. foreach($ret['schemas'] as $remote) {
  86. $name = substr($remote['tablename'], 4);
  87. $local = db_table_schema(pdo(), $name);
  88. unset($remote['increment']);
  89. unset($local['increment']);
  90. if(empty($local)) {
  91. $schemas[] = $remote;
  92. } else {
  93. $diffs = db_table_fix_sql($local, $remote);
  94. if(!empty($diffs)) {
  95. $schemas[] = $remote;
  96. }
  97. }
  98. }
  99. }
  100. $ret['upgrade'] = true;
  101. $ret['type'] = 'module';
  102. $ret['schemas'] = $schemas;
  103. if(empty($module)) {
  104. $ret['install'] = 1;
  105. }
  106. }
  107. return $ret;
  108. }
  109. function cloud_m_query() {
  110. $pars = _cloud_build_params();
  111. $pars['method'] = 'module.query';
  112. $dat = ihttp_post('http://v2.addons.we7.cc/gateway.php', $pars);
  113. $file = IA_ROOT . '/data/module.query';
  114. $ret = _cloud_shipping_parse($dat, $file);
  115. return $ret;
  116. }
  117. function cloud_m_info($name) {
  118. $pars = _cloud_build_params();
  119. $pars['method'] = 'module.info';
  120. $pars['module'] = $name;
  121. $dat = ihttp_post('http://v2.addons.we7.cc/gateway.php', $pars);
  122. $file = IA_ROOT . '/data/module.info';
  123. $ret = _cloud_shipping_parse($dat, $file);
  124. return $ret;
  125. }
  126. function cloud_m_upgradeinfo($name) {
  127. $module = pdo_fetch("SELECT name, version FROM ".tablename('modules')." WHERE name = '{$name}'");
  128. $pars = _cloud_build_params();
  129. $pars['method'] = 'module.info';
  130. $pars['module'] = $name;
  131. $pars['curversion'] = $module['version'];
  132. $pars['isupgrade'] = 1;
  133. $dat = ihttp_post('http://v2.addons.we7.cc/gateway.php', $pars);
  134. $file = IA_ROOT . '/data/module.info';
  135. $ret = _cloud_shipping_parse($dat, $file);
  136. return $ret;
  137. }
  138. function cloud_t_prepare($name) {
  139. $pars['method'] = 'theme.check';
  140. $pars['theme'] = $name;
  141. $dat = ihttp_post('http://v2.addons.we7.cc/gateway.php', $pars);
  142. if (is_error($dat)) {
  143. return $dat;
  144. }
  145. if ($dat['content'] == 'install-theme-protect') {
  146. return error('-1', '此模板已设置版权保护,您只能通过云平台来安装。');
  147. }
  148. return true;
  149. }
  150. function cloud_t_query() {
  151. $pars = _cloud_build_params();
  152. $pars['method'] = 'theme.query';
  153. $dat = ihttp_post('http://v2.addons.we7.cc/gateway.php', $pars);
  154. $file = IA_ROOT . '/data/theme.query';
  155. $ret = _cloud_shipping_parse($dat, $file);
  156. return $ret;
  157. }
  158. function cloud_t_info($name) {
  159. $pars = _cloud_build_params();
  160. $pars['method'] = 'theme.info';
  161. $pars['theme'] = $name;
  162. $dat = ihttp_post('http://v2.addons.we7.cc/gateway.php', $pars);
  163. $file = IA_ROOT . '/data/theme.info';
  164. $ret = _cloud_shipping_parse($dat, $file);
  165. return $ret;
  166. }
  167. function cloud_t_build($name) {
  168. $sql = 'SELECT * FROM ' . tablename('site_templates') . ' WHERE `name`=:name';
  169. $theme = pdo_fetch($sql, array(':name' => $name));
  170. $pars = _cloud_build_params();
  171. $pars['method'] = 'theme.build';
  172. $pars['theme'] = $name;
  173. if(!empty($theme)) {
  174. $pars['themeversion'] = $theme['version'];
  175. }
  176. $dat = ihttp_post('http://v2.addons.we7.cc/gateway.php', $pars);
  177. $file = IA_ROOT . '/data/theme.build';
  178. $ret = _cloud_shipping_parse($dat, $file);
  179. if(!is_error($ret)) {
  180. $dir = IA_ROOT . '/app/themes/' . $name;
  181. $files = array();
  182. if(!empty($ret['files'])) {
  183. foreach($ret['files'] as $file) {
  184. $entry = $dir . $file['path'];
  185. if(!is_file($entry) || md5_file($entry) != $file['checksum']) {
  186. $files[] = '/'. $name . $file['path'];
  187. }
  188. }
  189. }
  190. $ret['files'] = $files;
  191. $ret['upgrade'] = true;
  192. $ret['type'] = 'theme';
  193. if(empty($theme)) {
  194. $ret['install'] = 1;
  195. }
  196. }
  197. return $ret;
  198. }
  199. function cloud_sms_send($mobile, $content) {
  200. global $_W;
  201. $row = pdo_fetch("SELECT `notify` FROM ".tablename('uni_settings') . " WHERE uniacid = :uniacid", array(':uniacid' => $_W['uniacid']));
  202. $row['notify'] = @iunserializer($row['notify']);
  203. if(!empty($row['notify']) && !empty($row['notify']['sms'])) {
  204. $config = $row['notify']['sms'];
  205. $balance = intval($config['balance']);
  206. if($balance <= 0) {
  207. return error(-1, '发送短信失败, 请联系系统管理人员. 错误详情: 短信余额不足');
  208. }
  209. $sign = $config['signature'];
  210. if(empty($sign) && IMS_FAMILY == 'x') {
  211. $sign = $_W['setting']['copyright']['sitename'];
  212. }
  213. if(empty($sign)) {
  214. $sign = '微擎';
  215. }
  216. $pars = _cloud_build_params();
  217. $pars['method'] = 'sms.send';
  218. $pars['mobile'] = $mobile;
  219. $pars['content'] = $content . "【{$sign}】";
  220. $dat = ihttp_post('http://v2.addons.we7.cc/gateway.php', $pars);
  221. $file = IA_ROOT . '/data/sms.send';
  222. $ret = _cloud_shipping_parse($dat, $file);
  223. if (is_error($ret)) {
  224. return error($ret['errno'], $ret['message']);
  225. }
  226. if ($ret == 'success') {
  227. return true;
  228. } else {
  229. return error(-1, $ret);
  230. }
  231. }
  232. return error(-1, '发送短信失败, 请联系系统管理人员. 错误详情: 没有设置短信配额或参数');
  233. }
  234. function cloud_build() {
  235. $pars = _cloud_build_params();
  236. $pars['method'] = 'application.build';
  237. $dat = ihttp_post('http://v2.addons.we7.cc/gateway.php', $pars);
  238. $file = IA_ROOT . '/data/application.build';
  239. $ret = _cloud_shipping_parse($dat, $file);
  240. if(!is_error($ret)) {
  241. if($ret['state'] == 'warning') {
  242. $ret['files'] = cloud_client_define();
  243. unset($ret['schemas']);
  244. unset($ret['scripts']);
  245. } else {
  246. $files = array();
  247. if(!empty($ret['files'])) {
  248. foreach($ret['files'] as $file) {
  249. $entry = IA_ROOT . $file['path'];
  250. if(!is_file($entry) || md5_file($entry) != $file['checksum']) {
  251. $files[] = $file['path'];
  252. }
  253. }
  254. }
  255. $ret['files'] = $files;
  256. $schemas = array();
  257. if(!empty($ret['schemas'])) {
  258. load()->func('db');
  259. foreach($ret['schemas'] as $remote) {
  260. $name = substr($remote['tablename'], 4);
  261. $local = db_table_schema(pdo(), $name);
  262. unset($remote['increment']);
  263. unset($local['increment']);
  264. if(empty($local)) {
  265. $schemas[] = $remote;
  266. } else {
  267. $sqls = db_table_fix_sql($local, $remote);
  268. if(!empty($sqls)) {
  269. $schemas[] = $remote;
  270. }
  271. }
  272. }
  273. }
  274. $ret['schemas'] = $schemas;
  275. }
  276. if($ret['family'] == 'x' && IMS_FAMILY == 'v') {
  277. load()->model('setting');
  278. setting_upgrade_version('x', IMS_VERSION, IMS_RELEASE_DATE);
  279. message('您已经购买了商业授权版本, 系统将转换为商业版, 并重新运行自动更新程序.', 'refresh');
  280. }
  281. $ret['upgrade'] = false;
  282. if(!empty($ret['files']) || !empty($ret['schemas']) || !empty($ret['scripts'])) {
  283. $ret['upgrade'] = true;
  284. }
  285. $upgrade = array();
  286. $upgrade['upgrade'] = $ret['upgrade'];
  287. $upgrade['lastupdate'] = TIMESTAMP;
  288. cache_write('upgrade', $upgrade);
  289. }
  290. return $ret;
  291. }
  292. function cloud_schema() {
  293. $pars = _cloud_build_params();
  294. $pars['method'] = 'application.schema';
  295. $dat = ihttp_post('http://v2.addons.we7.cc/gateway.php', $pars);
  296. $file = IA_ROOT . '/data/application.schema';
  297. $ret = _cloud_shipping_parse($dat, $file);
  298. if(!is_error($ret)) {
  299. $schemas = array();
  300. if(!empty($ret['schemas'])) {
  301. load()->func('db');
  302. foreach($ret['schemas'] as $remote) {
  303. $name = substr($remote['tablename'], 4);
  304. $local = db_table_schema(pdo(), $name);
  305. unset($remote['increment']);
  306. unset($local['increment']);
  307. if(empty($local)) {
  308. $schemas[] = $remote;
  309. } else {
  310. $diffs = db_schema_compare($local, $remote);
  311. if(!empty($diffs)) {
  312. $schemas[] = $remote;
  313. }
  314. }
  315. }
  316. }
  317. $ret['schemas'] = $schemas;
  318. }
  319. return $ret;
  320. }
  321. function cloud_download($path, $type = '') {
  322. $pars = _cloud_build_params();
  323. $pars['method'] = 'application.shipping';
  324. $pars['path'] = $path;
  325. $pars['type'] = $type;
  326. $pars['gz'] = function_exists('gzcompress') && function_exists('gzuncompress') ? 'true' : 'false';
  327. $headers = array('content-type' => 'application/x-www-form-urlencoded');
  328. $dat = ihttp_request('http://v2.addons.we7.cc/gateway.php', $pars, $headers, 300);
  329. if(is_error($dat)) {
  330. return error(-1, '网络存在错误, 请稍后重试。' . $dat['message']);
  331. }
  332. if($dat['content'] == 'success') {
  333. return true;
  334. }
  335. $ret = @json_decode($dat['content'], true);
  336. if(is_error($ret)) {
  337. return $ret;
  338. } else {
  339. return error(-1, '不能下载文件, 请稍后重试。');
  340. }
  341. }
  342. function _cloud_shipping_parse($dat, $file) {
  343. if(is_error($dat)){
  344. return error(-1, '网络传输错误, 请检查您的cURL是否可用, 或者服务器网络是否正常. ' . $dat['message']);
  345. }
  346. $tmp = unserialize($dat['content']);
  347. if (is_array($tmp) && is_error($tmp)) {
  348. if($tmp['errno'] == '-2') {
  349. $data = file_get_contents(IA_ROOT . '/framework/version.inc.php');
  350. file_put_contents(IA_ROOT . '/framework/version.inc.php', str_replace("'x'", "'v'", $data));
  351. }
  352. return $tmp;
  353. }
  354. if ($dat['content'] == 'patching') {
  355. return error(-1, '补丁程序正在更新中,请稍后再试!');
  356. }
  357. if(strlen($dat['content']) != 32) {
  358. return error(-1, '云服务平台向您的服务器传输数据过程中出现错误, 这个错误可能是由于您的通信密钥和云服务不一致, 请尝试诊断云服务参数(重置站点ID和通信密钥). 传输原始数据:' . $dat['meta']);
  359. }
  360. $data = @file_get_contents($file);
  361. if(empty($data)) {
  362. return error(-1, '没有接收到服务器的传输的数据.');
  363. }
  364. @unlink($file);
  365. $ret = @iunserializer($data);
  366. if(empty($data) || empty($ret) || $dat['content'] != $ret['secret']) {
  367. return error(-1, '云服务平台向您的服务器传输的数据校验失败, 可能是因为您的网络不稳定, 或网络不安全, 请稍后重试.');
  368. }
  369. $ret = iunserializer($ret['data']);
  370. if (is_array($ret) && is_error($ret)) {
  371. if($ret['errno'] == '-2') {
  372. $data = file_get_contents(IA_ROOT . '/framework/version.inc.php');
  373. file_put_contents(IA_ROOT . '/framework/version.inc.php', str_replace("'x'", "'v'", $data));
  374. }
  375. }
  376. if(!is_error($ret) && is_array($ret) && !empty($ret)) {
  377. if($ret['state'] == 'fatal') {
  378. return error($ret['errorno'], '发生错误: ' . $ret['message']);
  379. }
  380. return $ret;
  381. } else {
  382. return error(-1, "发生错误: {$ret['message']}");
  383. }
  384. }