dock.ctrl.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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()->model('setting');
  9. $dos = array(
  10. 'auth',
  11. 'build',
  12. 'init',
  13. 'schema',
  14. 'download',
  15. 'module.query',
  16. 'module.bought',
  17. 'module.info',
  18. 'module.build',
  19. 'module.setting.cloud',
  20. 'theme.query',
  21. 'theme.info',
  22. 'theme.build',
  23. 'application.build',
  24. 'sms.send',
  25. 'sms.info',
  26. 'api.oauth',
  27. );
  28. $do = in_array($do, $dos) ? $do : '';
  29. if($do != 'auth') {
  30. if(is_error(cloud_prepare())) {
  31. exit('cloud service is unavailable.');
  32. }
  33. }
  34. $post = file_get_contents('php://input');
  35. if($do == 'auth') {
  36. $auth = @json_decode(base64_decode($post), true);
  37. if (empty($auth)) {
  38. exit('推送的站点数据有误');
  39. }
  40. setting_save($auth, 'site');
  41. exit('success');
  42. }
  43. if($do == 'build') {
  44. $dat = __secure_decode($post);
  45. if(!empty($dat)) {
  46. $secret = random(32);
  47. $ret = array();
  48. $ret['data'] = $dat;
  49. $ret['secret'] = $secret;
  50. file_put_contents(IA_ROOT . '/data/application.build', iserializer($ret));
  51. exit($secret);
  52. }
  53. }
  54. if($do == 'schema') {
  55. $dat = __secure_decode($post);
  56. if(!empty($dat)) {
  57. $secret = random(32);
  58. $ret = array();
  59. $ret['data'] = $dat;
  60. $ret['secret'] = $secret;
  61. file_put_contents(IA_ROOT . '/data/application.schema', iserializer($ret));
  62. exit($secret);
  63. }
  64. }
  65. if($do == 'download') {
  66. $data = base64_decode($post);
  67. if (base64_encode($data) !== $post) {
  68. $data = $post;
  69. }
  70. $ret = iunserializer($data);
  71. $gz = function_exists('gzcompress') && function_exists('gzuncompress');
  72. $file = base64_decode($ret['file']);
  73. if($gz) {
  74. $file = gzuncompress($file);
  75. }
  76. $_W['setting']['site']['token'] = authcode(cache_load(cache_system_key('cloud_transtoken')), 'DECODE');
  77. $string = (md5($file) . $ret['path'] . $_W['setting']['site']['token']);
  78. if(!empty($_W['setting']['site']['token']) && md5($string) === $ret['sign']) {
  79. if (strpos($ret['path'], '/web/') === 0 || strpos($ret['path'], '/framework/') === 0) {
  80. $patch_path = sprintf('%s/data/patch/upgrade/%s', IA_ROOT, date('Ymd'));
  81. } else {
  82. $patch_path = IA_ROOT;
  83. }
  84. $path = $patch_path . $ret['path'];
  85. load()->func('file');
  86. @mkdirs(dirname($path));
  87. file_put_contents($path, $file);
  88. $sign = md5(md5_file($path) . $ret['path'] . $_W['setting']['site']['token']);
  89. if($ret['sign'] === $sign) {
  90. exit('success');
  91. }
  92. }
  93. exit('failed');
  94. }
  95. if(in_array($do, array('module.query', 'module.bought', 'module.info', 'module.build', 'theme.query', 'theme.info', 'theme.build', 'application.build'))) {
  96. $dat = __secure_decode($post);
  97. if(!empty($dat)) {
  98. $secret = random(32);
  99. $ret = array();
  100. $ret['data'] = $dat;
  101. $ret['secret'] = $secret;
  102. file_put_contents(IA_ROOT . '/data/' . $do, iserializer($ret));
  103. exit($secret);
  104. }
  105. }
  106. if ($do == 'module.setting.cloud') {
  107. $data = __secure_decode($post);
  108. $data = iunserializer($data);
  109. $setting = $data['setting'];
  110. $uniacid = $data['acid'];
  111. $_W['uniacid'] = $data['acid'];
  112. $module = WeUtility::createModule($data['module']);
  113. $module->saveSettings($setting);
  114. cache_delete(cache_system_key('module_info', array('module_name' => $data['module'])));
  115. cache_delete(cache_system_key('module_setting', array('module_name' => $data['module'], 'uniacid' => $_W['uniacid'])), $setting);
  116. echo 'success';
  117. exit;
  118. }
  119. if ($do == 'sms.send') {
  120. $dat = __secure_decode($post);
  121. $dat = iunserializer($dat);
  122. }
  123. if ($do == 'sms.info') {
  124. $dat = __secure_decode($post);
  125. $dat = iunserializer($dat);
  126. if(!empty($dat) && is_array($dat)) {
  127. setting_save($dat, "sms.info");
  128. cache_clean();
  129. die('success');
  130. }
  131. die('fail');
  132. }
  133. if ($do == 'api.oauth') {
  134. $dat = __secure_decode($post);
  135. $dat = iunserializer($dat);
  136. if(!empty($dat) && is_array($dat)) {
  137. if ($dat['module'] == 'core') {
  138. $result = file_put_contents(IA_ROOT.'/framework/builtin/core/module.cer', $dat['access_token']);
  139. } else {
  140. $result = file_put_contents(IA_ROOT."/addons/{$dat['module']}/module.cer", $dat['access_token']);
  141. }
  142. if ($result !== false) {
  143. die('success');
  144. }
  145. die('获取到的访问云API的数字证书写入失败.');
  146. }
  147. die('获取云API授权失败: api oauth.');
  148. }
  149. function __secure_decode($post) {
  150. global $_W;
  151. $data = base64_decode($post);
  152. if (base64_encode($data) !== $post) {
  153. $data = $post;
  154. }
  155. $ret = iunserializer($data);
  156. $string = ($ret['data'] . $_W['setting']['site']['token']);
  157. if(md5($string) === $ret['sign']) {
  158. return $ret['data'];
  159. }
  160. return false;
  161. }