dock.ctrl.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. 'callback',
  12. 'build',
  13. 'init',
  14. 'schema',
  15. 'download',
  16. 'module.query',
  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 == 'callback') {
  30. $secret = $_GPC['token'];
  31. if(!empty($secret)) {
  32. $site = json_decode(base64_decode($secret),true);
  33. setting_save($site, 'site');
  34. exit("1");
  35. }
  36. }
  37. if($do != 'auth') {
  38. if(is_error(cloud_prepare())) {
  39. exit('cloud service is unavailable.');
  40. }
  41. }
  42. $post = file_get_contents('php://input');
  43. if($do == 'auth') {
  44. $secret = random(32);
  45. $auth = @json_decode(base64_decode($post), true);
  46. if(empty($auth)) {
  47. exit;
  48. }
  49. $auth['secret'] = $secret;
  50. cache_write('cloud:auth:transfer', $auth);
  51. exit($secret);
  52. }
  53. if($do == 'build') {
  54. $dat = __secure_decode($post);
  55. if(!empty($dat)) {
  56. $secret = random(32);
  57. $ret = array();
  58. $ret['data'] = $dat;
  59. $ret['secret'] = $secret;
  60. file_put_contents(IA_ROOT . '/data/application.build', iserializer($ret));
  61. exit($secret);
  62. }
  63. }
  64. if($do == 'schema') {
  65. $dat = __secure_decode($post);
  66. if(!empty($dat)) {
  67. $secret = random(32);
  68. $ret = array();
  69. $ret['data'] = $dat;
  70. $ret['secret'] = $secret;
  71. file_put_contents(IA_ROOT . '/data/application.schema', iserializer($ret));
  72. exit($secret);
  73. }
  74. }
  75. if($do == 'download') {
  76. $data = base64_decode($post);
  77. if (base64_encode($data) !== $post) {
  78. $data = $post;
  79. }
  80. $ret = iunserializer($data);
  81. $gz = function_exists('gzcompress') && function_exists('gzuncompress');
  82. $file = base64_decode($ret['file']);
  83. if($gz) {
  84. $file = gzuncompress($file);
  85. }
  86. //$_W['setting']['site']['token'] = authcode(cache_load('cloud:transtoken'), 'DECODE');
  87. $string = (md5($file) . $ret['path'] . $_W['setting']['site']['token']);
  88. if(!empty($_W['setting']['site']['token']) && md5($string) === $ret['sign']) {
  89. $path = IA_ROOT . $ret['path'];
  90. load()->func('file');
  91. @mkdirs(dirname($path));
  92. file_put_contents($path, $file);
  93. $sign = md5(md5_file($path) . $ret['path'] . $_W['setting']['site']['token']);
  94. if($ret['sign'] === $sign) {
  95. exit('success');
  96. }
  97. }
  98. exit("failed$post ".$_W['setting']['site']['token']);
  99. }
  100. if(in_array($do, array('module.query', 'module.info', 'module.build', 'theme.query', 'theme.info', 'theme.build', 'application.build'))) {
  101. $dat = __secure_decode($post);
  102. if(!empty($dat)) {
  103. $secret = random(32);
  104. $ret = array();
  105. $ret['data'] = $dat;
  106. $ret['secret'] = $secret;
  107. file_put_contents(IA_ROOT . '/data/' . $do, iserializer($ret));
  108. exit($secret);
  109. }
  110. }
  111. if ($do == 'module.setting.cloud') {
  112. $data = __secure_decode($post);
  113. $data = unserialize($data);
  114. $setting = $data['setting'];
  115. $uniacid = $data['acid'];
  116. foreach ($data['struct'] as $name => $type) {
  117. if ($type == 'image') {
  118. $url = $setting[$name];
  119. if (empty($url)) {
  120. $setting[$name] = '';
  121. } else {
  122. $attach = cloud_resource_to_local($uniacid, 'image', $url);
  123. if (!is_error($attach)) {
  124. $setting[$name] = $attach['attachment'];
  125. } else {
  126. echo "单图上传(字段: {$name})中图片本地化失败. ";
  127. exit;
  128. }
  129. }
  130. } elseif ($type == 'richtext'){
  131. $content = $setting[$name];
  132. if (empty($content)) {
  133. $setting[$name] = '';
  134. continue;
  135. }
  136. preg_match_all('/src=&quot;(\S*)&quot;/', $content, $matches);
  137. if ($matches[1]) {
  138. $new_urls = array();
  139. foreach ($matches[1] as $url) {
  140. $attach = cloud_resource_to_local($uniacid, 'image', $url);
  141. if (!is_error($attach)) {
  142. $new_urls[] = $attach['url'];;
  143. } else {
  144. echo "富文本(字段 {$name})中图片本地化失败";
  145. exit;
  146. }
  147. };
  148. $setting[$name] = str_replace($matches[1], $new_urls, $setting[$name]);
  149. } else {
  150. $setting[$name] = $content;
  151. }
  152. } elseif ($type == 'images'){
  153. if (empty($setting[$name])) {
  154. $setting[$name] = array();
  155. continue;
  156. }
  157. foreach ($setting[$name] as $idx => $url) {
  158. if (empty($url)) {
  159. $setting[$name][$idx] = '';
  160. continue;
  161. } else {
  162. $attach = cloud_resource_to_local($uniacid, 'image', $url);
  163. if (!is_error($attach)) {
  164. $setting[$name][$idx] = $attach['attachment'];
  165. } else {
  166. echo "多图上传(字段 {$name})中图片本地化失败";
  167. exit;
  168. }
  169. }
  170. }
  171. }
  172. }
  173. $_W['uniacid'] = $data['acid'];
  174. $module = WeUtility::createModule($data['module']);
  175. $module->saveSettings($setting);
  176. cache_write("modulesetting:{$data['acid']}:{$data['module']}", $setting);
  177. echo 'success';
  178. exit;
  179. }
  180. if ($do == 'sms.send') {
  181. $dat = __secure_decode($post);
  182. $dat = iunserializer($dat);
  183. }
  184. if ($do == 'sms.info') {
  185. $dat = __secure_decode($post);
  186. $dat = iunserializer($dat);
  187. if(!empty($dat) && is_array($dat)) {
  188. setting_save($dat, "sms.info");
  189. cache_clean();
  190. die('success');
  191. }
  192. die('fail');
  193. }
  194. if ($do == 'api.oauth') {
  195. $dat = __secure_decode($post);
  196. $dat = iunserializer($dat);
  197. if(!empty($dat) && is_array($dat)) {
  198. if ($dat['module'] == 'core') {
  199. $result = file_put_contents(IA_ROOT.'/framework/builtin/core/module.cer', $dat['access_token']);
  200. } else {
  201. $result = file_put_contents(IA_ROOT."/addons/{$dat['module']}/module.cer", $dat['access_token']);
  202. }
  203. if ($result !== false) {
  204. die('success');
  205. }
  206. die('获取到的访问云API的数字证书写入失败.');
  207. }
  208. die('获取云API授权失败: api oauth.');
  209. }
  210. function __secure_decode($post) {
  211. global $_W;
  212. $data = base64_decode($post);
  213. if (base64_encode($data) !== $post) {
  214. $data = $post;
  215. }
  216. $ret = iunserializer($data);
  217. $string = ($ret['data'] . $_W['setting']['site']['token']);
  218. if(md5($string) === $ret['sign']) {
  219. return $ret['data'];
  220. }
  221. return false;
  222. }
  223. ?>