人人商城

attachment.mod.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. function attachment_set_attach_url() {
  8. global $_W;
  9. $_W['setting']['remote_complete_info'] = $_W['setting']['remote'];
  10. if (!empty($_W['setting']['remote'][$_W['uniacid']]['type'])) {
  11. $_W['setting']['remote'] = $_W['setting']['remote'][$_W['uniacid']];
  12. }
  13. $attach_url = $_W['attachurl_local'] = $_W['siteroot'] . $_W['config']['upload']['attachdir'] . '/';
  14. if (!empty($_W['setting']['remote']['type'])) {
  15. if ($_W['setting']['remote']['type'] == ATTACH_FTP) {
  16. $attach_url = $_W['attachurl_remote'] = $_W['setting']['remote']['ftp']['url'] . '/';
  17. } elseif ($_W['setting']['remote']['type'] == ATTACH_OSS) {
  18. $attach_url = $_W['attachurl_remote'] = $_W['setting']['remote']['alioss']['url'] . '/';
  19. } elseif ($_W['setting']['remote']['type'] == ATTACH_QINIU) {
  20. $attach_url = $_W['attachurl_remote'] = $_W['setting']['remote']['qiniu']['url'] . '/';
  21. } elseif ($_W['setting']['remote']['type'] == ATTACH_COS) {
  22. $attach_url = $_W['attachurl_remote'] = $_W['setting']['remote']['cos']['url'] . '/';
  23. }
  24. }
  25. return $attach_url;
  26. }
  27. function attachment_alioss_datacenters() {
  28. $bucket_datacenter = array(
  29. 'oss-cn-hangzhou' => '杭州数据中心',
  30. 'oss-cn-qingdao' => '青岛数据中心',
  31. 'oss-cn-beijing' => '北京数据中心',
  32. 'oss-cn-hongkong' => '香港数据中心',
  33. 'oss-cn-shenzhen' => '深圳数据中心',
  34. 'oss-cn-shanghai' => '上海数据中心',
  35. 'oss-us-west-1' => '美国硅谷数据中心',
  36. );
  37. return $bucket_datacenter;
  38. }
  39. function attachment_newalioss_auth($key, $secret, $bucket, $internal = false){
  40. load()->library('oss');
  41. $buckets = attachment_alioss_buctkets($key, $secret);
  42. $host = $internal ? '-internal.aliyuncs.com' : '.aliyuncs.com';
  43. $url = 'http://'.$buckets[$bucket]['location'] . $host;
  44. $filename = 'MicroEngine.ico';
  45. try {
  46. $ossClient = new \OSS\OssClient($key, $secret, $url);
  47. $ossClient->uploadFile($bucket, $filename, ATTACHMENT_ROOT.'images/global/'.$filename);
  48. } catch (\OSS\Core\OssException $e) {
  49. return error(1, $e->getMessage());
  50. }
  51. return 1;
  52. }
  53. function attachment_alioss_buctkets($key, $secret) {
  54. load()->library('oss');
  55. $url = 'http://oss-cn-beijing.aliyuncs.com';
  56. try {
  57. $ossClient = new \OSS\OssClient($key, $secret, $url);
  58. } catch(\OSS\Core\OssException $e) {
  59. return error(1, $e->getMessage());
  60. }
  61. try{
  62. $bucketlistinfo = $ossClient->listBuckets();
  63. } catch(OSS\OSS_Exception $e) {
  64. return error(1, $e->getMessage());
  65. }
  66. $bucketlistinfo = $bucketlistinfo->getBucketList();
  67. $bucketlist = array();
  68. foreach ($bucketlistinfo as &$bucket) {
  69. $bucketlist[$bucket->getName()] = array('name' => $bucket->getName(), 'location' => $bucket->getLocation());
  70. }
  71. return $bucketlist;
  72. }
  73. function attachment_qiniu_auth($key, $secret,$bucket) {
  74. load()->library('qiniu');
  75. $auth = new Qiniu\Auth($key, $secret);
  76. $token = $auth->uploadToken($bucket);
  77. $config = new Qiniu\Config();
  78. $uploadmgr = new Qiniu\Storage\UploadManager($config);
  79. list($ret, $err) = $uploadmgr->putFile($token, 'MicroEngine.ico', ATTACHMENT_ROOT.'images/global/MicroEngine.ico');
  80. if ($err !== null) {
  81. $err = (array)$err;
  82. $err = (array)array_pop($err);
  83. $err = json_decode($err['body'], true);
  84. return error(-1, $err);
  85. } else {
  86. return true;
  87. }
  88. }
  89. function attachment_cos_auth($bucket,$appid, $key, $secret, $bucket_local = '') {
  90. if (!is_numeric($appid)) {
  91. return error(-1, '传入appid值不合法, 请重新输入');
  92. }
  93. if (!preg_match('/^[a-zA-Z0-9]{36}$/', $key)) {
  94. return error(-1, '传入secretid值不合法,请重新传入');
  95. }
  96. if (!preg_match('/^[a-zA-Z0-9]{32}$/', $secret)) {
  97. return error(-1, '传入secretkey值不合法,请重新传入');
  98. }
  99. if (!empty($bucket_local)) {
  100. $con = $original = file_get_contents(IA_ROOT.'/framework/library/cosv4.2/qcloudcos/conf.php');
  101. if (empty($con)) {
  102. $conf_content = base64_decode("PD9waHANCg0KbmFtZXNwYWNlIHFjbG91ZGNvczsNCg0KY2xhc3MgQ29uZiB7DQogICAgLy8gQ29zIHBocCBzZGsgdmVyc2lvbiBudW1iZXIuDQogICAgY29uc3QgVkVSU0lPTiA9ICd2NC4yLjInOw0KICAgIGNvbnN0IEFQSV9DT1NBUElfRU5EX1BPSU5UID0gJ2h0dHA6Ly9yZWdpb24uZmlsZS5teXFjbG91ZC5jb20vZmlsZXMvdjIvJzsNCg0KICAgIC8vIFBsZWFzZSByZWZlciB0byBodHRwOi8vY29uc29sZS5xY2xvdWQuY29tL2NvcyB0byBmZXRjaCB5b3VyIGFwcF9pZCwgc2VjcmV0X2lkIGFuZCBzZWNyZXRfa2V5Lg0KICAgIGNvbnN0IEFQUF9JRCA9ICcnOw0KICAgIGNvbnN0IFNFQ1JFVF9JRCA9ICcnOw0KICAgIGNvbnN0IFNFQ1JFVF9LRVkgPSAnJzsNCg0KICAgIC8qKg0KICAgICAqIEdldCB0aGUgVXNlci1BZ2VudCBzdHJpbmcgdG8gc2VuZCB0byBDT1Mgc2VydmVyLg0KICAgICAqLw0KICAgIHB1YmxpYyBzdGF0aWMgZnVuY3Rpb24gZ2V0VXNlckFnZW50KCkgew0KICAgICAgICByZXR1cm4gJ2Nvcy1waHAtc2RrLScgLiBzZWxmOjpWRVJTSU9OOw0KICAgIH0NCn0NCg==");
  103. file_put_contents(IA_ROOT.'/framework/library/cosv4.2/qcloudcos/conf.php', $conf_content);
  104. $con = $original = $conf_content;
  105. }
  106. $con = preg_replace('/const[\s]APP_ID[\s]=[\s]\'.*\';/', 'const APP_ID = \''.$appid.'\';', $con);
  107. $con = preg_replace('/const[\s]SECRET_ID[\s]=[\s]\'.*\';/', 'const SECRET_ID = \''.$key.'\';', $con);
  108. $con = preg_replace('/const[\s]SECRET_KEY[\s]=[\s]\'.*\';/', 'const SECRET_KEY = \''.$secret.'\';', $con);
  109. file_put_contents(IA_ROOT.'/framework/library/cosv4.2/qcloudcos/conf.php', $con);
  110. load()->library('cos');
  111. qcloudcos\Cosapi :: setRegion($bucket_local);
  112. qcloudcos\Cosapi :: setTimeout(180);
  113. $uploadRet = qcloudcos\Cosapi::upload($bucket, ATTACHMENT_ROOT . 'images/global/MicroEngine.ico', '/MicroEngine.ico','',3 * 1024 * 1024, 0);
  114. } else {
  115. load()->library('cosv3');
  116. $con = $original = @file_get_contents(IA_ROOT.'/framework/library/cos/Qcloud_cos/Conf.php');
  117. if (empty($con)) {
  118. $conf_content = base64_decode("PD9waHANCm5hbWVzcGFjZSBRY2xvdWRfY29zOw0KDQpjbGFzcyBDb25mDQp7DQogICAgY29uc3QgUEtHX1ZFUlNJT04gPSAndjMuMyc7DQoNCiAgICBjb25zdCBBUElfSU1BR0VfRU5EX1BPSU5UID0gJ2h0dHA6Ly93ZWIuaW1hZ2UubXlxY2xvdWQuY29tL3Bob3Rvcy92MS8nOw0KICAgIGNvbnN0IEFQSV9WSURFT19FTkRfUE9JTlQgPSAnaHR0cDovL3dlYi52aWRlby5teXFjbG91ZC5jb20vdmlkZW9zL3YxLyc7DQogICAgY29uc3QgQVBJX0NPU0FQSV9FTkRfUE9JTlQgPSAnaHR0cDovL3dlYi5maWxlLm15cWNsb3VkLmNvbS9maWxlcy92MS8nOw0KICAgIC8v6K+35YiwaHR0cDovL2NvbnNvbGUucWNsb3VkLmNvbS9jb3Pljrvojrflj5bkvaDnmoRhcHBpZOOAgXNpZOOAgXNrZXkNCiAgICBjb25zdCBBUFBJRCA9ICcnOw0KICAgIGNvbnN0IFNFQ1JFVF9JRCA9ICcnOw0KICAgIGNvbnN0IFNFQ1JFVF9LRVkgPSAnJzsNCg0KDQogICAgcHVibGljIHN0YXRpYyBmdW5jdGlvbiBnZXRVQSgpIHsNCiAgICAgICAgcmV0dXJuICdjb3MtcGhwLXNkay0nLnNlbGY6OlBLR19WRVJTSU9OOw0KICAgIH0NCn0NCg0KLy9lbmQgb2Ygc2NyaXB0DQo=");
  119. file_put_contents(IA_ROOT.'/framework/library/cos/Qcloud_cos/Conf.php', $conf_content);
  120. $con = $original = $conf_content;
  121. }
  122. $con = preg_replace('/const[\s]APPID[\s]=[\s]\'.*\';/', 'const APPID = \''.$appid.'\';', $con);
  123. $con = preg_replace('/const[\s]SECRET_ID[\s]=[\s]\'.*\';/', 'const SECRET_ID = \''.$key.'\';', $con);
  124. $con = preg_replace('/const[\s]SECRET_KEY[\s]=[\s]\'.*\';/', 'const SECRET_KEY = \''.$secret.'\';', $con);
  125. file_put_contents(IA_ROOT.'/framework/library/cos/Qcloud_cos/Conf.php', $con);
  126. $uploadRet = Qcloud_cos\Cosapi::upload($bucket, ATTACHMENT_ROOT.'images/global/MicroEngine.ico', '/MicroEngine.ico','',3 * 1024 * 1024, 0);
  127. }
  128. if ($uploadRet['code'] != 0) {
  129. switch ($uploadRet['code']) {
  130. case -62:
  131. $message = '输入的appid有误';
  132. break;
  133. case -79:
  134. $message = '输入的SecretID有误';
  135. break;
  136. case -97:
  137. $message = '输入的SecretKEY有误';
  138. break;
  139. case -166:
  140. $message = '输入的bucket有误';
  141. break;
  142. case -133:
  143. $message = '请确认你的bucket是否存在';
  144. break;
  145. default:
  146. $message = $uploadRet['message'];
  147. }
  148. if (empty($bucket_local)) {
  149. file_put_contents(IA_ROOT.'/framework/library/cos/Qcloud_cos/Conf.php', $original);
  150. } else {
  151. file_put_contents(IA_ROOT.'/framework/library/cosv4.2/qcloudcos/Conf.php', $original);
  152. }
  153. return error(-1, $message);
  154. }
  155. return true;
  156. }
  157. function attachment_reset_uniacid($uniacid) {
  158. global $_W;
  159. if ($_W['role'] == ACCOUNT_MANAGE_NAME_FOUNDER) {
  160. if (empty($uniacid)) {
  161. $_W['uniacid'] = 0;
  162. }
  163. } else {
  164. $account = table('account');
  165. $accounts = $account->userOwnedAccount($_W['uid']);
  166. if (is_array($accounts) && isset($accounts[$uniacid])) {
  167. $_W['uniacid'] = $uniacid;
  168. }
  169. }
  170. return true;
  171. }