attachment.mod.php 8.7 KB

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