Storage.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: Storage.php 29263 2012-03-31 05:45:08Z yexinhao $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class Cloud_Service_Storage {
  12. protected static $debug = 0;
  13. protected static $_appStatus;
  14. protected static $_siteId;
  15. protected static $_encKey;
  16. protected static $_util;
  17. protected static $_instance;
  18. public static function getInstance() {
  19. global $_G;
  20. if (!(self::$_instance instanceof self)) {
  21. self::$_instance = new self();
  22. $cloudAppService = Cloud::loadClass('Service_App');
  23. self::$_appStatus = $cloudAppService->getCloudAppStatus('storage');
  24. self::$_siteId = $_G['setting']['my_siteid'];
  25. self::$_encKey = $_G['setting']['xf_storage_enc_key'];
  26. self::$_util = Cloud::loadClass('Service_Util');
  27. }
  28. return self::$_instance;
  29. }
  30. public function ftnFormhash($specialadd = '') {
  31. global $_G;
  32. return substr(md5(substr($_G['timestamp'], 0, -4) . $_G['username'] . $_G['uid'] . $_G['authkey'] . self::$_encKey . $specialadd), 8, 8);
  33. }
  34. public function makeFtnSig($formhash) {
  35. global $_G;
  36. $openId = $this->getOpenId($_G['uid']);
  37. $signGetx = array(
  38. 's_id' => self::$_siteId,
  39. 's_site_uid' => $_G['uid'],
  40. 'ts' => $_G['timestamp'],
  41. 'discuz_form_hash' => $formhash,
  42. 'site_url' => $_G['siteurl'],
  43. 'discuz_openid' => $openId,
  44. );
  45. ksort($signGetx);
  46. return self::$_util->hashHmac('sha1', self::$_util->httpBuildQuery($signGetx, '', '&'), self::$_encKey);
  47. }
  48. public function makeIframeUrl($formhash) {
  49. global $_G;
  50. $openId = $this->getOpenId($_G['uid']);
  51. $ftnGetx = array(
  52. 's_id' => self::$_siteId,
  53. 's_site_uid' => $_G['uid'],
  54. 'ts' => $_G['timestamp'],
  55. 'discuz_form_hash' => $formhash,
  56. 'site_url' => $_G['siteurl'],
  57. 'discuz_openid' => $openId,
  58. );
  59. $url = "http://cp.discuz.qq.com/storage/FTN?" . self::$_util->httpBuildQuery($ftnGetx, '', '&');
  60. $url = $url.'&sign=' . $this->makeFtnSig($formhash);
  61. return $url;
  62. }
  63. public function makeQQdlUrl($sha, $filename) {
  64. global $_G;
  65. $filename = trim($filename);
  66. $filename = urlencode(diconv($filename,CHARSET,'UTF-8'));
  67. $url = $_G['siteurl'] . $filename . '?&&txf_fid=' . $sha . '&siteid=' . self::$_siteId;
  68. return 'qqdl://'.base64_encode($url);
  69. }
  70. public function makeDownloadurl($sha1, $filesize, $filename) {
  71. global $_G;
  72. $filename = trim($filename,' "'); // Discuz! 默认的filename两侧会加上 双引号
  73. $filename = diconv($filename,CHARSET,'UTF-8');
  74. $filename = $this->str2hex($filename);
  75. $filename = strtolower($filename[1]);
  76. $post = 'http://dz.xf.qq.com/ftn.php?v=1&&';
  77. $k = self::$_util->hashHmac('sha1', sprintf('%s|%s|%s', $sha1, $_G['timestamp'], self::$_siteId), self::$_encKey);
  78. $parm = array(
  79. 'site_id' => self::$_siteId,
  80. 't' => $_G['timestamp'],
  81. 'sha1' => $sha1,
  82. 'filesize' => $filesize,
  83. 'filename' => $filename,
  84. 'k' => $k,
  85. 'ip' => $_G['clientip']
  86. );
  87. return $post . self::$_util->httpBuildQuery($parm, '', '&&');
  88. }
  89. private function _joinParm($parm = array(),$joiner = '&'){
  90. $andflag = '';
  91. $return = '';
  92. foreach($parm as $key => $value){
  93. $value = urlencode($value);
  94. $return .= $andflag.$key.'='.$value;
  95. $andflag = $joiner;
  96. }
  97. return $return;
  98. }
  99. public function str2hex($str){
  100. $length = strlen($str)*2;
  101. return unpack('H' . $length, $str);
  102. }
  103. public function getOpenId($uid) {
  104. $member = C::t('common_member')->fetch($uid);
  105. if ($member['conisbind']) {
  106. $connectInfo = C::t('#qqconnect#common_member_connect')->fetch($uid);
  107. $openId = $connectInfo['conopenid'];
  108. } else {
  109. $openId = '';
  110. }
  111. return $openId;
  112. }
  113. public function checkAttachment($attach, $redirect = true) {
  114. if (strpos($attach['attachment'], 'storage:') !== false) {
  115. C::t('forum_attachment')->update_download($attach['aid']);
  116. $sha1 = substr($attach['attachment'], -40);
  117. $downloadUrl = $this->makeDownloadurl($sha1, $attach['filesize'], $attach['filename']);
  118. if ($redirect) {
  119. dheader('location:' . $downloadUrl);
  120. exit;
  121. } else {
  122. return $downloadUrl;
  123. }
  124. }
  125. }
  126. public function getAttachmentByPids($pids) {
  127. global $_G;
  128. if (!is_array($pids)) {
  129. $pids = explode(',', $pids);
  130. }
  131. include_once libfile('function/post');
  132. $attachment = array();
  133. foreach ($pids as $pid) {
  134. $data = getattach($pid);
  135. $attachment[] = $data['used'];
  136. }
  137. return $attachment;
  138. }
  139. }