PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/api/manyou/Service/Disk.php

https://github.com/kuaileshike/upload
PHP | 244 lines | 207 code | 31 blank | 6 comment | 40 complexity | e6cd2e3fecac6c60e6cb893c150b89be MD5 | raw file
  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: Disk.php 30716 2012-06-14 03:02:50Z songlixin $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class Cloud_Service_Disk {
  12. protected $_util;
  13. protected $_client;
  14. protected $_baseUrl;
  15. protected static $_instance;
  16. public static function getInstance() {
  17. global $_G;
  18. if (!(self::$_instance instanceof self)) {
  19. self::$_instance = new self();
  20. }
  21. return self::$_instance;
  22. }
  23. public function __construct() {
  24. global $_G;
  25. $this->_util = Cloud::loadClass('Service_Util');
  26. $this->_client = Cloud::loadClass('Service_Client_Disk');
  27. $this->_baseUrl = $_G['siteurl'] . 'apptest.php?';
  28. }
  29. public function saveTask($attach, $openId) {
  30. global $_G;
  31. if (!$_G['uid']) {
  32. throw new Cloud_Exception('noLogin', '30001');
  33. }
  34. if (!$openId) {
  35. throw new Cloud_Exception('noopenId', '30002');
  36. }
  37. $verifyCode = md5($openId . $attach['aid'] . $_G['timestamp'] . $_G['uid']);
  38. $taskData = array(
  39. 'aid' => $attach['aid'],
  40. 'uid' => $_G['uid'],
  41. 'openId' => $openId,
  42. 'filename' => $attach['filename'],
  43. 'verifycode' => $verifyCode,
  44. 'status' => 0,
  45. 'dateline' => $_G['timestamp'],
  46. );
  47. $taskId = C::t('#qqconnect#connect_disktask')->insert($taskData, true);
  48. $downParams = array(
  49. 'taskid' => $taskId,
  50. 'verifycode' => $verifyCode,
  51. );
  52. $downloadUrl = $this->_baseUrl . $this->_util->generateSiteSignUrl($downParams);
  53. $filePath = self::_getFullPath($attach['attachment']);
  54. if ($attach['filesize'] <= 10000000 && file_exists($filePath)) {
  55. $md5 = md5_file($filePath);
  56. $hash = hash_file('sha1', $filePath);
  57. }
  58. $cloudData = array(
  59. 'sId' => $_G['setting']['my_siteid'],
  60. 'openId' => $openId,
  61. 'batchTasks' => array(
  62. array(
  63. 'aId' => $attach['aid'],
  64. 'fileName' => $attach['filename'],
  65. 'downloadUrl' => $downloadUrl,
  66. 'size' => filesize($filePath),
  67. 'md5' => $md5,
  68. 'hash' => $hash,
  69. ),
  70. ),
  71. 'clientIp' => $_G['clientip'],
  72. );
  73. return $this->_client->sendTask($cloudData);
  74. }
  75. public function getAttachment() {
  76. global $_G;
  77. try {
  78. $taskData = $this->checkTask();
  79. } catch (Exception $e) {
  80. throw new Cloud_Exception($e);
  81. }
  82. $task = $taskData['task'];
  83. $attach = $taskData['attach'];
  84. $taskId = $task['taskid'];
  85. C::t('#qqconnect#connect_disktask')->update($taskId, array(
  86. 'status' => 1,
  87. 'downloadtime' => $_G['timestamp'],
  88. ));
  89. $db = DB::object();
  90. $db->close();
  91. ob_end_clean();
  92. $attach['filename'] = '"'.(strtolower(CHARSET) == 'utf-8' && strexists($_SERVER['HTTP_USER_AGENT'], 'MSIE') ? urlencode($attach['filename']) : $attach['filename']).'"';
  93. dheader('Content-Type: application/octet-stream');
  94. dheader('Content-Length: ' . $attach['filesize']);
  95. dheader('Content-Disposition: attachment; filename='.$attach['filename']);
  96. self::_checkXSendFile($attach['attachment']);
  97. if ($attach['remote']) {
  98. self::_getRemoteFile($attach['attachment']);
  99. } else {
  100. self::_getLocalFile($attach['attachment']);
  101. }
  102. exit;
  103. }
  104. public function checkTask() {
  105. global $_G;
  106. $sId = $_G['setting']['my_siteid'];
  107. $ts = $_GET['ts'];
  108. $taskId = $_GET['taskid'];
  109. $verifyCode = $_GET['verifycode'];
  110. if ($sId != $_GET['s_id']) {
  111. throw new Cloud_Exception('sIdError', '30004');
  112. }
  113. if ($_G['timestamp'] - $ts > 86400) {
  114. throw new Cloud_Exception('downloadTimeOut', '30005');
  115. }
  116. $params = array(
  117. 'taskid' => $taskId,
  118. 'verifycode' => $verifyCode,
  119. 's_id' => $_GET['s_id'],
  120. 's_site_uid' => $_GET['s_site_uid'],
  121. );
  122. $sig = $_GET['sig'];
  123. $sKey = $_G['setting']['my_sitekey'];
  124. ksort($params);
  125. $str = $this->_util->httpBuildQuery($params, '', '&');
  126. if ($sig != md5(sprintf('%s|%s|%s', $str, $sKey, $ts))) {
  127. throw new Cloud_Exception('sigError', '30003');
  128. }
  129. $task = C::t('#qqconnect#connect_disktask')->fetch($taskId);
  130. if (!$task) {
  131. throw new Cloud_Exception('noTask', '30006');
  132. }
  133. if ($verifyCode != $task['verifycode']) {
  134. throw new Cloud_Exception('verifyError', '30009');
  135. }
  136. $attach = C::t('forum_attachment_n')->fetch('aid:' . $task['aid'], $task['aid']);
  137. if (!$attach) {
  138. throw new Cloud_Exception('noAttachment', '30007');
  139. }
  140. $return = array(
  141. 'task' => $task,
  142. 'attach' => $attach,
  143. );
  144. return $return;
  145. }
  146. public function clearDirtyData() {
  147. return C::t('#qqconnect#connect_disktask')->delete_by_status(1);
  148. }
  149. private static function _getFullPath($attachment) {
  150. global $_G;
  151. return $_G['setting']['attachdir'] . 'forum/' . $attachment;
  152. }
  153. private static function _getLocalFile($file) {
  154. $filename = self::_getFullPath($file);
  155. $readmod = getglobal('config/download/readmod');
  156. $readmod = $readmod > 0 && $readmod < 5 ? $readmod : 2;
  157. if($readmod == 1 || $readmod == 3 || $readmod == 4) {
  158. if($fp = @fopen($filename, 'rb')) {
  159. if(function_exists('fpassthru') && ($readmod == 3 || $readmod == 4)) {
  160. @fpassthru($fp);
  161. } else {
  162. echo @fread($fp, filesize($filename));
  163. }
  164. }
  165. @fclose($fp);
  166. } else {
  167. @readfile($filename);
  168. }
  169. @flush(); @ob_flush();
  170. }
  171. private static function _getRemoteFile($file) {
  172. global $_G;
  173. @set_time_limit(0);
  174. if(!@readfile($_G['setting']['ftp']['attachurl'] . 'forum/' . $file)) {
  175. $ftp = ftpcmd('object');
  176. $tmpfile = @tempnam($_G['setting']['attachdir'], '');
  177. if($ftp->ftp_get($tmpfile, 'forum/'.$file, FTP_BINARY)) {
  178. @readfile($tmpfile);
  179. @unlink($tmpfile);
  180. } else {
  181. @unlink($tmpfile);
  182. return FALSE;
  183. }
  184. }
  185. return TRUE;
  186. }
  187. private static function _checkXSendFile($file) {
  188. $filename = self::_getFullPath($file);
  189. $xsendfile = getglobal('config/download/xsendfile');
  190. if(!empty($xsendfile)) {
  191. $type = intval($xsendfile['type']);
  192. $cmd = '';
  193. switch ($type) {
  194. case 1:
  195. $cmd = 'X-Accel-Redirect';
  196. $url = $xsendfile['dir'] . $file;
  197. break;
  198. case 2:
  199. $cmd = $_SERVER['SERVER_SOFTWARE'] <'lighttpd/1.5' ? 'X-LIGHTTPD-send-file' : 'X-Sendfile';
  200. $url = $filename;
  201. break;
  202. case 3:
  203. $cmd = 'X-Sendfile';
  204. $url = $filename;
  205. break;
  206. }
  207. if($cmd) {
  208. dheader("$cmd: $url");
  209. exit();
  210. }
  211. }
  212. }
  213. }