PageRenderTime 78ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/source/module/forum/forum_attachment.php

https://github.com/kuaileshike/upload
PHP | 329 lines | 277 code | 46 blank | 6 comment | 163 complexity | 203b6bf777121de0fa9d625d6d7e1f9a 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: forum_attachment.php 30933 2012-07-02 08:39:51Z zhengqingpeng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. define('NOROBOT', TRUE);
  12. @list($_GET['aid'], $_GET['k'], $_GET['t'], $_GET['uid'], $_GET['tableid']) = daddslashes(explode('|', base64_decode($_GET['aid'])));
  13. $aid = intval($_GET['aid']);
  14. if(!empty($_GET['findpost']) && ($attach = C::t('forum_attachment')->fetch($aid))) {
  15. dheader('location: forum.php?mod=redirect&goto=findpost&pid='.$attach['pid'].'&ptid='.$attach['tid']);
  16. }
  17. if($_GET['uid'] != $_G['uid'] && $_GET['uid']) {
  18. $_GET['uid'] = intval($_GET['uid']);
  19. $member = getuserbyuid($_GET['uid']);
  20. loadcache('usergroup_'.$member['groupid']);
  21. $_G['group'] = $_G['cache']['usergroup_'.$member['groupid']];
  22. }
  23. $requestmode = !empty($_GET['request']) && empty($_GET['uid']);
  24. $tableid = 'aid:'.$aid;
  25. if($_G['setting']['attachexpire']) {
  26. $k = $_GET['k'];
  27. $t = $_GET['t'];
  28. $authk = !$requestmode ? substr(md5($aid.md5($_G['config']['security']['authkey']).$t.$_GET['uid']), 0, 8) : md5($aid.md5($_G['config']['security']['authkey']).$t);
  29. if(empty($k) || empty($t) || $k != $authk || TIMESTAMP - $t > $_G['setting']['attachexpire'] * 3600) {
  30. $aid = intval($aid);
  31. if($attach = C::t('forum_attachment_n')->fetch($tableid, $aid)) {
  32. if($attach['isimage']) {
  33. dheader('location: '.$_G['siteurl'].'static/image/common/none.gif');
  34. } else {
  35. if(!$requestmode) {
  36. showmessage('attachment_expired', '', array('aid' => aidencode($aid, 0, $attach['tid']), 'pid' => $attach['pid'], 'tid' => $attach['tid']));
  37. } else {
  38. exit;
  39. }
  40. }
  41. } else {
  42. if(!$requestmode) {
  43. showmessage('attachment_nonexistence');
  44. } else {
  45. exit;
  46. }
  47. }
  48. }
  49. }
  50. $readmod = getglobal('config/download/readmod');
  51. $readmod = $readmod > 0 && $readmod < 5 ? $readmod : 2;
  52. $refererhost = parse_url($_SERVER['HTTP_REFERER']);
  53. $serverhost = $_SERVER['HTTP_HOST'];
  54. if(($pos = strpos($serverhost, ':')) !== FALSE) {
  55. $serverhost = substr($serverhost, 0, $pos);
  56. }
  57. if(!$requestmode && $_G['setting']['attachrefcheck'] && $_SERVER['HTTP_REFERER'] && !($refererhost['host'] == $serverhost)) {
  58. showmessage('attachment_referer_invalid', NULL);
  59. }
  60. periodscheck('attachbanperiods');
  61. loadcache('threadtableids');
  62. $threadtableids = !empty($_G['cache']['threadtableids']) ? $_G['cache']['threadtableids'] : array();
  63. if(!in_array(0, $threadtableids)) {
  64. $threadtableids = array_merge(array(0), $threadtableids);
  65. }
  66. $archiveid = in_array($_GET['archiveid'], $threadtableids) ? intval($_GET['archiveid']) : 0;
  67. $attachexists = FALSE;
  68. if(!empty($aid) && is_numeric($aid)) {
  69. $attach = C::t('forum_attachment_n')->fetch($tableid, $aid);
  70. $thread = C::t('forum_thread')->fetch_by_tid_displayorder($attach['tid'], 0, '>=', null, $archiveid);
  71. if($_G['uid'] && $attach['uid'] != $_G['uid']) {
  72. if($attach) {
  73. $attachpost = C::t('forum_post')->fetch($thread['posttableid'], $attach['pid'], false);
  74. $attach['invisible'] = $attachpost['invisible'];
  75. unset($attachpost);
  76. }
  77. if($attach && $attach['invisible'] == 0) {
  78. $thread && $attachexists = TRUE;
  79. }
  80. } else {
  81. $attachexists = TRUE;
  82. }
  83. }
  84. if(!$attachexists) {
  85. if(!$requestmode) {
  86. showmessage('attachment_nonexistence');
  87. } else {
  88. exit;
  89. }
  90. }
  91. if(!$requestmode) {
  92. $forum = C::t('forum_forumfield')->fetch_info_for_attach($thread['fid'], $_G['uid']);
  93. $_GET['fid'] = $forum['fid'];
  94. if($attach['isimage']) {
  95. $allowgetattach = !empty($forum['allowgetimage']) || (($_G['group']['allowgetimage'] || $_G['uid'] == $attach['uid']) && !$forum['getattachperm']) || forumperm($forum['getattachperm']);
  96. } else {
  97. $allowgetattach = !empty($forum['allowgetattach']) || (($_G['group']['allowgetattach'] || $_G['uid'] == $attach['uid']) && !$forum['getattachperm']) || forumperm($forum['getattachperm']);
  98. }
  99. if($allowgetattach && ($attach['readperm'] && $attach['readperm'] > $_G['group']['readaccess']) && $_G['adminid'] <= 0 && !($_G['uid'] && $_G['uid'] == $attach['uid'])) {
  100. showmessage('attachment_forum_nopermission', NULL, array(), array('login' => 1));
  101. }
  102. $ismoderator = in_array($_G['adminid'], array(1, 2)) ? 1 : ($_G['adminid'] == 3 ? C::t('forum_moderator')->fetch_uid_by_tid($attach['tid'], $_G['uid'], $archiveid) : 0);
  103. $ispaid = FALSE;
  104. $exemptvalue = $ismoderator ? 128 : 16;
  105. if(!$thread['special'] && $thread['price'] > 0 && (!$_G['uid'] || ($_G['uid'] != $attach['uid'] && !($_G['group']['exempt'] & $exemptvalue)))) {
  106. if(!$_G['uid'] || $_G['uid'] && !($ispaid = C::t('common_credit_log')->count_by_uid_operation_relatedid($_G['uid'], 'BTC', $attach['tid']))) {
  107. showmessage('attachment_payto', 'forum.php?mod=viewthread&tid='.$attach['tid']);
  108. }
  109. }
  110. $exemptvalue = $ismoderator ? 64 : 8;
  111. if($attach['price'] && (!$_G['uid'] || ($_G['uid'] != $attach['uid'] && !($_G['group']['exempt'] & $exemptvalue)))) {
  112. $payrequired = $_G['uid'] ? !C::t('common_credit_log')->count_by_uid_operation_relatedid($_G['uid'], 'BAC', $attach['aid']) : 1;
  113. $payrequired && showmessage('attachement_payto_attach', 'forum.php?mod=misc&action=attachpay&aid='.$attach['aid'].'&tid='.$attach['tid']);
  114. }
  115. }
  116. $isimage = $attach['isimage'];
  117. $_G['setting']['ftp']['hideurl'] = $_G['setting']['ftp']['hideurl'] || ($isimage && !empty($_GET['noupdate']) && $_G['setting']['attachimgpost'] && strtolower(substr($_G['setting']['ftp']['attachurl'], 0, 3)) == 'ftp');
  118. if(empty($_GET['nothumb']) && $attach['isimage'] && $attach['thumb']) {
  119. $db = DB::object();
  120. $db->close();
  121. !$_G['config']['output']['gzip'] && ob_end_clean();
  122. dheader('Content-Disposition: inline; filename='.getimgthumbname($attach['filename']));
  123. dheader('Content-Type: image/pjpeg');
  124. if($attach['remote']) {
  125. $_G['setting']['ftp']['hideurl'] ? getremotefile(getimgthumbname($attach['attachment'])) : dheader('location:'.$_G['setting']['ftp']['attachurl'].'forum/'.getimgthumbname($attach['attachment']));
  126. } else {
  127. getlocalfile($_G['setting']['attachdir'].'/forum/'.getimgthumbname($attach['attachment']));
  128. }
  129. exit();
  130. }
  131. $filename = $_G['setting']['attachdir'].'/forum/'.$attach['attachment'];
  132. if(!$attach['remote'] && !is_readable($filename)) {
  133. $storageService = Cloud::loadClass('Service_Storage');
  134. $storageService->checkAttachment($attach);
  135. if(!$requestmode) {
  136. showmessage('attachment_nonexistence');
  137. } else {
  138. exit;
  139. }
  140. }
  141. if(!$requestmode) {
  142. if(!$ispaid && !$forum['allowgetattach']) {
  143. if(!$forum['getattachperm'] && !$allowgetattach) {
  144. showmessage('getattachperm_none_nopermission', NULL, array(), array('login' => 1));
  145. } elseif(($forum['getattachperm'] && !forumperm($forum['getattachperm'])) || ($forum['viewperm'] && !forumperm($forum['viewperm']))) {
  146. showmessagenoperm('getattachperm', $forum['fid']);
  147. }
  148. }
  149. $exemptvalue = $ismoderator ? 32 : 4;
  150. if(!$isimage && !($_G['group']['exempt'] & $exemptvalue)) {
  151. $creditlog = updatecreditbyaction('getattach', $_G['uid'], array(), '', 1, 0, $thread['fid']);
  152. if($creditlog['updatecredit']) {
  153. if($_G['uid']) {
  154. $k = $_GET['ck'];
  155. $t = $_GET['t'];
  156. if(empty($k) || empty($t) || $k != substr(md5($aid.$t.md5($_G['config']['security']['authkey'])), 0, 8) || TIMESTAMP - $t > 3600) {
  157. dheader('location: forum.php?mod=misc&action=attachcredit&aid='.$attach['aid'].'&formhash='.FORMHASH);
  158. exit();
  159. }
  160. } else {
  161. showmessage('attachment_forum_nopermission', NULL, array(), array('login' => 1));
  162. }
  163. }
  164. }
  165. }
  166. $range = 0;
  167. if($readmod == 4 && !empty($_SERVER['HTTP_RANGE'])) {
  168. list($range) = explode('-',(str_replace('bytes=', '', $_SERVER['HTTP_RANGE'])));
  169. }
  170. if(!$requestmode && !$range && empty($_GET['noupdate'])) {
  171. if($_G['setting']['delayviewcount']) {
  172. $_G['forum_logfile'] = './data/cache/forum_attachviews_'.intval(getglobal('config/server/id')).'.log';
  173. if(substr(TIMESTAMP, -1) == '0') {
  174. attachment_updateviews($_G['forum_logfile']);
  175. }
  176. if(@$fp = fopen(DISCUZ_ROOT.$_G['forum_logfile'], 'a')) {
  177. fwrite($fp, "$aid\n");
  178. fclose($fp);
  179. } elseif($_G['adminid'] == 1) {
  180. showmessage('view_log_invalid', '', array('logfile' => $_G['forum_logfile']));
  181. }
  182. } else {
  183. C::t('forum_attachment')->update_download($aid);
  184. }
  185. }
  186. $db = DB::object();
  187. $db->close();
  188. !$_G['config']['output']['gzip'] && ob_end_clean();
  189. if($attach['remote'] && !$_G['setting']['ftp']['hideurl'] && $isimage) {
  190. dheader('location:'.$_G['setting']['ftp']['attachurl'].'forum/'.$attach['attachment']);
  191. }
  192. $filesize = !$attach['remote'] ? filesize($filename) : $attach['filesize'];
  193. $attach['filename'] = '"'.(strtolower(CHARSET) == 'utf-8' && strexists($_SERVER['HTTP_USER_AGENT'], 'MSIE') ? urlencode($attach['filename']) : $attach['filename']).'"';
  194. dheader('Date: '.gmdate('D, d M Y H:i:s', $attach['dateline']).' GMT');
  195. dheader('Last-Modified: '.gmdate('D, d M Y H:i:s', $attach['dateline']).' GMT');
  196. dheader('Content-Encoding: none');
  197. if($isimage && !empty($_GET['noupdate']) || !empty($_GET['request'])) {
  198. dheader('Content-Disposition: inline; filename='.$attach['filename']);
  199. } else {
  200. dheader('Content-Disposition: attachment; filename='.$attach['filename']);
  201. }
  202. if($isimage) {
  203. dheader('Content-Type: image');
  204. } else {
  205. dheader('Content-Type: application/octet-stream');
  206. }
  207. dheader('Content-Length: '.$filesize);
  208. $xsendfile = getglobal('config/download/xsendfile');
  209. if(!empty($xsendfile)) {
  210. $type = intval($xsendfile['type']);
  211. $cmd = '';
  212. switch ($type) {
  213. case 1: $cmd = 'X-Accel-Redirect'; $url = $xsendfile['dir'].$attach['attachment']; break;
  214. case 2: $cmd = $_SERVER['SERVER_SOFTWARE'] <'lighttpd/1.5' ? 'X-LIGHTTPD-send-file' : 'X-Sendfile'; $url = $filename; break;
  215. case 3: $cmd = 'X-Sendfile'; $url = $filename; break;
  216. }
  217. if($cmd) {
  218. dheader("$cmd: $url");
  219. exit();
  220. }
  221. }
  222. if($readmod == 4) {
  223. dheader('Accept-Ranges: bytes');
  224. if(!empty($_SERVER['HTTP_RANGE'])) {
  225. $rangesize = ($filesize - $range) > 0 ? ($filesize - $range) : 0;
  226. dheader('Content-Length: '.$rangesize);
  227. dheader('HTTP/1.1 206 Partial Content');
  228. dheader('Content-Range: bytes='.$range.'-'.($filesize-1).'/'.($filesize));
  229. }
  230. }
  231. $attach['remote'] ? getremotefile($attach['attachment']) : getlocalfile($filename, $readmod, $range);
  232. function getremotefile($file) {
  233. global $_G;
  234. @set_time_limit(0);
  235. if(!@readfile($_G['setting']['ftp']['attachurl'].'forum/'.$file)) {
  236. $ftp = ftpcmd('object');
  237. $tmpfile = @tempnam($_G['setting']['attachdir'], '');
  238. if($ftp->ftp_get($tmpfile, 'forum/'.$file, FTP_BINARY)) {
  239. @readfile($tmpfile);
  240. @unlink($tmpfile);
  241. } else {
  242. @unlink($tmpfile);
  243. return FALSE;
  244. }
  245. }
  246. return TRUE;
  247. }
  248. function getlocalfile($filename, $readmod = 2, $range = 0) {
  249. if($readmod == 1 || $readmod == 3 || $readmod == 4) {
  250. if($fp = @fopen($filename, 'rb')) {
  251. @fseek($fp, $range);
  252. if(function_exists('fpassthru') && ($readmod == 3 || $readmod == 4)) {
  253. @fpassthru($fp);
  254. } else {
  255. echo @fread($fp, filesize($filename));
  256. }
  257. }
  258. @fclose($fp);
  259. } else {
  260. @readfile($filename);
  261. }
  262. @flush(); @ob_flush();
  263. }
  264. function attachment_updateviews($logfile) {
  265. $viewlog = $viewarray = array();
  266. $newlog = DISCUZ_ROOT.$logfile.random(6);
  267. if(@rename(DISCUZ_ROOT.$logfile, $newlog)) {
  268. $viewlog = file($newlog);
  269. unlink($newlog);
  270. if(is_array($viewlog) && !empty($viewlog)) {
  271. $viewlog = array_count_values($viewlog);
  272. foreach($viewlog as $id => $views) {
  273. if($id > 0) {
  274. $viewarray[$views][] = intval($id);
  275. }
  276. }
  277. foreach($viewarray as $views => $ids) {
  278. C::t('forum_attachment')->update_download($ids, $views);
  279. }
  280. }
  281. }
  282. }
  283. ?>