PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/src/applications/bbs/controller/AttachController.php

https://gitlab.com/wuhang2003/phpwind
PHP | 403 lines | 344 code | 35 blank | 24 comment | 86 complexity | 9248f31ca93c2c8f59fbe5b848c52a9a MD5 | raw file
  1. <?php
  2. /**
  3. * 附件操作
  4. *
  5. * @author Jianmin Chen <sky_hold@163.com>
  6. * @license http://www.phpwind.com
  7. * @version $Id: AttachController.php 28798 2013-05-24 06:20:13Z jieyin $
  8. * @package forum
  9. */
  10. class AttachController extends PwBaseController {
  11. public function run() {
  12. }
  13. public function downloadAction() {
  14. $aid = (int)$this->getInput('aid', 'get');
  15. $submit = (int)$this->getInput('submit', 'post');
  16. $attach = Wekit::load('attach.PwThreadAttach')->getAttach($aid);
  17. if (!$attach) {
  18. $this->showError('BBS:thread.buy.attach.error');
  19. }
  20. Wind::import('SRV:forum.bo.PwForumBo');
  21. $forum = new PwForumBo($attach['fid']);
  22. if (!$forum->isForum()) {
  23. $this->showError('data.error');
  24. }
  25. if ($attach['cost'] && !$this->loginUser->isExists()) {
  26. $this->showError('download.fail.login.not','bbs/attach/download');
  27. }
  28. if (!$forum->allowDownload($this->loginUser)) {
  29. if (!$this->loginUser->isExists()) {
  30. $this->showError('download.fail.login.not','bbs/attach/download');
  31. }
  32. $this->showError(array('BBS:forum.permissions.download.allow', array('{grouptitle}' => $this->loginUser->getGroupInfo('name'))));
  33. }
  34. if (!$forum->foruminfo['allow_download'] && !$this->loginUser->getPermission('allow_download')) {
  35. if (!$this->loginUser->isExists()) {
  36. $this->showError('download.fail.login.not','bbs/attach/download');
  37. }
  38. $this->showError(array('permission.download.allow', array('{grouptitle}' => $this->loginUser->getGroupInfo('name'))));
  39. }
  40. Wind::import('SRV:credit.bo.PwCreditBo');
  41. $creditBo = PwCreditBo::getInstance();
  42. // 购买积分检查
  43. if (($attach = $this->_checkAttachCost($attach)) instanceof PwError) {
  44. $this->showError($attach->getError());
  45. }
  46. // 下载积分检查
  47. if (($reduceDownload = $this->_checkAttachDownload('download_att', $attach, $forum)) instanceof PwError) {
  48. $this->showError($reduceDownload->getError());
  49. }
  50. //下载积分提示
  51. $lang = Wind::getComponent('i18n');
  52. if (1 == $this->loginUser->getPermission('allow_download') && $reduceDownload && $attach['cost']) {
  53. $dataShow = $lang->getMessage('BBS:thread.attachbuy.message.all', array('{buyCount}' =>-$attach['cost'].$creditBo->cType[$attach['ctype']], '{downCount}' => rtrim($reduceDownload, ',')));
  54. } elseif (1 == $this->loginUser->getPermission('allow_download') && $reduceDownload && !$attach['cost']) {
  55. $dataShow = $lang->getMessage('BBS:thread.attachbuy.message.download', array('{downCount}' => rtrim($reduceDownload, ',')));
  56. } elseif ($attach['cost']) {
  57. $dataShow = $lang->getMessage('BBS:thread.attachbuy.message.buy', array('{count}' => $this->loginUser->getCredit($attach['ctype']).$creditBo->cType[$attach['ctype']], '{buyCount}' => -$attach['cost'].$creditBo->cType[$attach['ctype']]));
  58. } else {
  59. $dataShow = $lang->getMessage('BBS:thread.attachbuy.message.success');
  60. }
  61. !$submit && $this->showMessage($dataShow);
  62. //购买积分操作
  63. $this->_operateBuyCredit($attach);
  64. // 下载积分
  65. if ($reduceDownload) {
  66. Wind::import('SRV:attach.dm.PwThreadAttachBuyDm');
  67. $dm = new PwThreadAttachBuyDm();
  68. $dm->setAid($aid)
  69. ->setCreatedUserid($this->loginUser->uid)
  70. ->setCreatedTime(Pw::getTime())
  71. ->setCtype($attach['ctype'])
  72. ->setCost($attach['cost']);
  73. Wekit::load('attach.PwThreadAttachDownload')->add($dm);
  74. $this->_operateCredit('download_att', $forum);
  75. }
  76. //更新附件点击数
  77. Wind::import('SRV:attach.dm.PwThreadAttachDm');
  78. $dm = new PwThreadAttachDm($aid);
  79. $dm->addHits(1);
  80. Wekit::load('attach.PwThreadAttach')->updateAttach($dm);
  81. $filename = basename($attach['path']);
  82. $fileext = substr(strrchr($attach['path'], '.'), 1);
  83. $filesize = 0;
  84. if (strpos(Wind::getApp()->getRequest()->getServer('HTTP_USER_AGENT'), 'MSIE') !== false && $fileext == 'torrent') {
  85. $attachment = 'inline';
  86. } else {
  87. $attachment = 'attachment';
  88. }
  89. $attach['name'] = trim(str_replace('&nbsp;', ' ', $attach['name']));
  90. if (strtoupper(Wekit::V('charset')) == 'UTF-8') {
  91. $attach['name'] = Pw::convert($attach['name'], "gbk", 'utf-8');
  92. }
  93. $filesize = 0;
  94. $fgeturl = Wind::getComponent('storage')->getDownloadUrl($attach['path']);
  95. if (strpos($fgeturl, 'http') !== 0) {
  96. if (!is_readable($fgeturl)) {
  97. $this->showError('BBS:thread.buy.attach.error');
  98. }
  99. $filesize = filesize($fgeturl);
  100. }
  101. $timestamp = Pw::getTime();
  102. $ctype = '';
  103. switch ($fileext) {
  104. case "pdf":
  105. $ctype = "application/pdf";
  106. break;
  107. case "rar":
  108. case "zip":
  109. $ctype = "application/zip";
  110. break;
  111. case "doc":
  112. $ctype = "application/msword";
  113. break;
  114. case "xls":
  115. $ctype = "application/vnd.ms-excel";
  116. break;
  117. case "ppt":
  118. $ctype = "application/vnd.ms-powerpoint";
  119. break;
  120. case "gif":
  121. $ctype = "image/gif";
  122. break;
  123. case "png":
  124. $ctype = "image/png";
  125. break;
  126. case "jpeg":
  127. case "jpg":
  128. $ctype = "image/jpeg";
  129. break;
  130. case "wav":
  131. $ctype = "audio/x-wav";
  132. break;
  133. case "mpeg":
  134. case "mpg":
  135. case "mpe":
  136. $ctype = "video/x-mpeg";
  137. break;
  138. case "mov":
  139. $ctype = "video/quicktime";
  140. break;
  141. case "avi":
  142. $ctype = "video/x-msvideo";
  143. break;
  144. case "txt":
  145. $ctype = "text/plain";
  146. break;
  147. default:
  148. $ctype = "application/octet-stream";
  149. }
  150. ob_end_clean();
  151. header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $timestamp + 86400) . ' GMT');
  152. header('Expires: ' . gmdate('D, d M Y H:i:s', $timestamp + 86400) . ' GMT');
  153. header('Cache-control: max-age=86400');
  154. header('Content-Encoding: none');
  155. header("Content-Disposition: $attachment; filename=\"{$attach['name']}\"");
  156. header("Content-type: $ctype");
  157. header("Content-Transfer-Encoding: binary");
  158. $filesize && header("Content-Length: $filesize");
  159. $i = 1;
  160. while (!@readfile($fgeturl)) {
  161. if (++$i > 3) break;
  162. }
  163. exit();
  164. }
  165. public function deleteAction() {
  166. $aid = $this->getInput('aid', 'post');
  167. if (!$aid) {
  168. $this->showError('operate.fail');
  169. }
  170. if (!$attach = Wekit::load('attach.PwThreadAttach')->getAttach($aid)) {
  171. $this->showError('data.error');
  172. }
  173. Wind::import('SRV:forum.bo.PwForumBo');
  174. $forum = new PwForumBo($attach['fid']);
  175. if (!$forum->isForum()) {
  176. $this->showError('data.error');
  177. }
  178. if ($this->loginUser->uid != $attach['created_userid']) {
  179. if (!$this->loginUser->getPermission('operate_thread.deleteatt', $forum->isBM($this->loginUser->username))) {
  180. $this->showError('permission.attach.delete.deny');
  181. }
  182. if (!$this->loginUser->comparePermission($attach['created_userid'])) {
  183. $this->showError(array('permission.level.deleteatt', array('{grouptitle}' => $this->loginUser->getGroupInfo('name'))));
  184. }
  185. }
  186. Wekit::load('attach.PwThreadAttach')->deleteAttach($aid);
  187. Pw::deleteAttach($attach['path'], $attach['ifthumb']);
  188. if ($this->loginUser->uid != $attach['created_userid']) {
  189. Wekit::load('log.srv.PwLogService')->addDeleteAtachLog($this->loginUser, $attach);
  190. }
  191. if ($attach['tid']) {
  192. if (!$attach['pid']) {
  193. $thread = Wekit::load('forum.PwThread')->getThread($attach['tid'], PwThread::FETCH_ALL);
  194. Wind::import('SRV:forum.dm.PwTopicDm');
  195. $dm = new PwTopicDm($attach['tid']);
  196. if (!Wekit::load('attach.PwThreadAttach')->countType($attach['tid'], 0, $attach['type'])) {
  197. $dm->setHasAttach($attach['type'], false);
  198. }
  199. if (!Pw::getstatus($thread['tpcstatus'], PwThread::STATUS_OPERATORLOG) && $this->loginUser->uid != $attach['created_userid']) {
  200. $dm->setOperatorLog(true);
  201. }
  202. } else {
  203. $thread = Wekit::load('forum.PwThread')->getPost($attach['pid']);
  204. Wind::import('SRV:forum.dm.PwReplyDm');
  205. $dm = new PwReplyDm($attach['pid']);
  206. }
  207. if ($thread['aids'] > 0) {
  208. $thread['aids']--;
  209. }
  210. $dm->setAids($thread['aids']);
  211. if (($content = str_replace('[attachment=' . $aid . ']', '', $thread['content'])) != $thread['content']) {
  212. $dm->setContent($content);
  213. }
  214. if (!$attach['pid']) {
  215. Wekit::load('forum.PwThread')->updateThread($dm);
  216. } else {
  217. Wekit::load('forum.PwThread')->updatePost($dm);
  218. }
  219. }
  220. $this->showMessage('success');
  221. }
  222. public function recordAction() {
  223. list($aid, $page) = $this->getInput(array('aid', 'page'));
  224. $perpage = 10;
  225. $page < 1 && $page = 1;
  226. list($offset, $limit) = Pw::page2limit($page, $perpage);
  227. $count = Wekit::load('attach.PwThreadAttachBuy')->countByAid($aid);
  228. if (!$count) {
  229. $this->showError('BBS:thread.buy.error.norecord');
  230. }
  231. Wind::import('SRV:credit.bo.PwCreditBo');
  232. $record = Wekit::load('attach.PwThreadAttachBuy')->getByAid($aid, $limit, $offset);
  233. !$record && $this->showError('BBS:thread.buy.error.norecord');
  234. $users = Wekit::load('user.PwUser')->fetchUserByUid(array_keys($record));
  235. $data = array();
  236. $cType = PwCreditBo::getInstance()->cType;
  237. foreach ($record as $key => $value) {
  238. $data[] = array(
  239. 'uid' => $value['created_userid'],
  240. 'username' => $users[$value['created_userid']]['username'],
  241. 'cost' => $value['cost'],
  242. 'ctype' => $cType[$value['ctype']],
  243. 'created_time' => Pw::time2str($value['created_time'])
  244. );
  245. }
  246. $totalpage = ceil($count / $perpage);
  247. $nextpage = $page+1;
  248. $nextpage = $nextpage > $totalpage ? $totalpage : $nextpage;
  249. $this->setOutput(array('data' => $data, 'totalpage' => $totalpage, 'page' => $nextpage), 'data');
  250. $this->showMessage('success');
  251. }
  252. private function _getDownloadCredit($operation, PwUserBo $user, PwCreditBo $creditBo, $creditset = array()) {
  253. $strategy = $creditBo->getStrategy($operation);
  254. if ($this->_checkCreditSetEmpty($strategy['credit']) && $this->_checkCreditSetEmpty($creditset['credit'])) {
  255. return false;
  256. }
  257. //如果外部有积分设置传入则使用外部的积分设置策略
  258. if (!empty($creditset['limit']) || ($creditset['credit'] && false === $this->_checkCreditSetEmpty($creditset['credit']))) {
  259. $strategy['limit'] = $creditset['limit'];
  260. $strategy['credit'] = $creditset['credit'];
  261. }
  262. if ($strategy['limit']) {
  263. $count = $creditBo->getOperateCount($user->uid, $operation);
  264. if ($count >= $strategy['limit']) return false;
  265. }
  266. return $strategy['credit'];
  267. }
  268. private function _checkCreditSetEmpty($credit) {
  269. foreach ($credit as $key => $value) {
  270. if ($value) return false;
  271. }
  272. return true;
  273. }
  274. /**
  275. * 检查购买积分
  276. */
  277. protected function _checkAttachCost($attach) {
  278. if (!$attach['cost']) return $attach;
  279. $user = Wekit::getLoginUser();
  280. if ($attach['created_userid'] == $user->uid) {
  281. $attach['cost'] = 0;
  282. return $attach;
  283. }
  284. $attachbuy = Wekit::load('attach.PwThreadAttachBuy');
  285. if (!$attachbuy->getByAidAndUid($attach['aid'], $user->uid)) {
  286. $myCredit = $user->getCredit($attach['ctype']);
  287. if ($attach['cost'] > $myCredit) {
  288. Wind::import('SRV:credit.bo.PwCreditBo');
  289. $creditBo = PwCreditBo::getInstance();
  290. $creditType = $creditBo->cType[$attach['ctype']];
  291. return new PwError('BBS:thread.buy.error.credit.notenough', array('{myCredit}' => $myCredit.$creditType, '{count}' => -$attach['cost'].$creditType));
  292. }
  293. } else {
  294. $attach['cost'] = 0;
  295. }
  296. return $attach;
  297. }
  298. /**
  299. * 下载购买积分
  300. */
  301. protected function _checkAttachDownload($operate, $attach, PwForumBo $forum) {
  302. $user = Wekit::getLoginUser();
  303. if (1 != $user->getPermission('allow_download')) {
  304. return false;
  305. }
  306. Wind::import('SRV:credit.bo.PwCreditBo');
  307. $creditBo = PwCreditBo::getInstance();
  308. $forumCredit = $forum->getCreditSet($operate);
  309. $downloadCredit = $this->_getDownloadCredit($operate, $user, $creditBo, $forumCredit);
  310. if (!$downloadCredit) return false;
  311. if (!$user->isExists()) {
  312. return new PwError('download.fail.login.not');
  313. }
  314. $attachdownload = Wekit::load('attach.PwThreadAttachDownload');
  315. $ifDown = $attachdownload->getByAidAndUid($attach['aid'], $user->uid);
  316. if ($ifDown) return false;
  317. $reduceDownload = '';
  318. foreach ($downloadCredit as $k => $v) {
  319. $tv = $v;
  320. ($attach['ctype'] == $k) && $tv = $v - $attach['cost'];
  321. $vt = $tv > 0 ? '+'.$tv : $tv;
  322. if (-$tv > $user->getCredit($k)) {
  323. return new PwError('BBS:thread.download.error.credit.notenough', array('{myCredit}' => $this->loginUser->getCredit($k).$creditBo->cType[$k], '{count}' => $vt.$creditBo->cType[$k]));
  324. }
  325. $v && $reduceDownload .= ($v > 0 ? '+'.abs($v) : $v).$creditBo->cType[$k].',';
  326. }
  327. return $reduceDownload;
  328. }
  329. /**
  330. * 更新积分
  331. */
  332. protected function _operateCredit($operate, PwForumBo $forum) {
  333. Wind::import('SRV:credit.bo.PwCreditBo');
  334. $credit = PwCreditBo::getInstance();
  335. $user = Wekit::getLoginUser();
  336. $credit->operate($operate, $user, true, array('forumname' => $forum->foruminfo['name']),$forum->getCreditSet($operate));
  337. $credit->execute();
  338. }
  339. protected function _operateBuyCredit($attach) {
  340. $user = Wekit::getLoginUser();
  341. if (!$attach['cost'] || $attach['created_userid'] == $user->uid) {
  342. return false;
  343. }
  344. Wind::import('SRV:credit.bo.PwCreditBo');
  345. $creditBo = PwCreditBo::getInstance();
  346. Wind::import('SRV:attach.dm.PwThreadAttachBuyDm');
  347. $dm = new PwThreadAttachBuyDm();
  348. $dm->setAid($attach['aid'])
  349. ->setCreatedUserid($user->uid)
  350. ->setCreatedTime(Pw::getTime())
  351. ->setCtype($attach['ctype'])
  352. ->setCost($attach['cost']);
  353. Wekit::load('attach.PwThreadAttachBuy')->add($dm);
  354. $creditBo->addLog('attach_buy', array($attach['ctype'] => -$attach['cost']), $user, array(
  355. 'name' => $attach['name']
  356. ));
  357. $creditBo->set($user->uid, $attach['ctype'], -$attach['cost'], true);
  358. $user = new PwUserBo($attach['created_userid']);
  359. if (($max = $user->getPermission('sell_credit_range.maxincome')) && Wekit::load('attach.PwThreadAttachBuy')->sumCost($attach['aid']) > $max) {
  360. } else {
  361. $creditBo->addLog('attach_sell', array($attach['ctype'] => $attach['cost']), $user, array(
  362. 'name' => $attach['name']
  363. ));
  364. $creditBo->set($user->uid, $attach['ctype'], $attach['cost'], true);
  365. }
  366. $creditBo->execute();
  367. }
  368. }