人人商城

dock.ctrl.php 5.7KB

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