portal_attachment.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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: portal_attachment.php 25246 2011-11-02 03:34:53Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. $operation = $_GET['op'] ? $_GET['op'] : '';
  12. $id = empty($_GET['id']) ? 0 : intval($_GET['id']);
  13. $aid = empty($_GET['aid']) ? '' : intval($_GET['aid']);
  14. $attach = C::t('portal_attachment')->fetch($id);
  15. if(empty($attach)) {
  16. showmessage('portal_attachment_noexist');
  17. }
  18. if($operation == 'delete') {
  19. if(!$_G['group']['allowmanagearticle'] && $_G['uid'] != $attach['uid']) {
  20. showmessage('portal_attachment_nopermission_delete');
  21. }
  22. if($aid) {
  23. C::t('portal_article_title')->update($aid, array('pic' => ''));
  24. }
  25. C::t('portal_attachment')->delete($id);
  26. pic_delete($attach['attachment'], 'portal', $attach['thumb'], $attach['remote']);
  27. showmessage('portal_image_noexist');
  28. } elseif($operation == 'getattach') {
  29. require_once libfile('function/attachment');
  30. if($attach['isimage']) {
  31. require_once libfile('function/home');
  32. $smallimg = pic_get($attach['attachment'], 'portal', $attach['thumb'], $attach['remote']);
  33. $bigimg = pic_get($attach['attachment'], 'portal', 0, $attach['remote']);
  34. $coverstr = addslashes(serialize(array('pic'=>'portal/'.$attach['attachment'], 'thumb'=>$attach['thumb'], 'remote'=>$attach['remote'])));
  35. }
  36. $attach['filetype'] = attachtype($attach['filetype']."\t".$attach['filetype']);
  37. $attach['filesize'] = sizecount($attach['filesize']);
  38. include template('portal/portal_attachment');
  39. exit;
  40. } else {
  41. $filename = $_G['setting']['attachdir'].'/portal/'.$attach['attachment'];
  42. if(!$attach['remote'] && !is_readable($filename)) {
  43. showmessage('attachment_nonexistence');
  44. }
  45. $readmod = 2;//read local file's function: 1=fread 2=readfile 3=fpassthru 4=fpassthru+multiple
  46. $range = 0;
  47. if($readmod == 4 && !empty($_SERVER['HTTP_RANGE'])) {
  48. list($range) = explode('-',(str_replace('bytes=', '', $_SERVER['HTTP_RANGE'])));
  49. }
  50. if($attach['remote'] && !$_G['setting']['ftp']['hideurl'] && $attach['isimage']) {
  51. dheader('location:'.$_G['setting']['ftp']['attachurl'].'portal/'.$attach['attachment']);
  52. }
  53. $filesize = $attach['filesize'];
  54. $attach['filename'] = '"'.(strtolower(CHARSET) == 'utf-8' && strexists($_SERVER['HTTP_USER_AGENT'], 'MSIE') ? urlencode($attach['filename']) : $attach['filename']).'"';
  55. dheader('Date: '.gmdate('D, d M Y H:i:s', $attach['dateline']).' GMT');
  56. dheader('Last-Modified: '.gmdate('D, d M Y H:i:s', $attach['dateline']).' GMT');
  57. dheader('Content-Encoding: none');
  58. dheader('Content-Disposition: attachment; filename='.$attach['filename']);
  59. dheader('Content-Type: '.$attach['filetype']);
  60. dheader('Content-Length: '.$filesize);
  61. if($readmod == 4) {
  62. dheader('Accept-Ranges: bytes');
  63. if(!empty($_SERVER['HTTP_RANGE'])) {
  64. $rangesize = ($filesize - $range) > 0 ? ($filesize - $range) : 0;
  65. dheader('Content-Length: '.$rangesize);
  66. dheader('HTTP/1.1 206 Partial Content');
  67. dheader('Content-Range: bytes='.$range.'-'.($filesize-1).'/'.($filesize));
  68. }
  69. }
  70. $attach['remote'] ? getremotefile($attach['attachment']) : getlocalfile($filename, $readmod, $range);
  71. }
  72. function getremotefile($file) {
  73. global $_G;
  74. @set_time_limit(0);
  75. if(!@readfile($_G['setting']['ftp']['attachurl'].'portal/'.$file)) {
  76. $ftp = new discuz_ftp();
  77. if(!($_G['setting']['ftp']['connid'] = $ftp->connect())) {
  78. return FALSE;
  79. }
  80. $tmpfile = @tempnam($_G['setting']['attachdir'], '');
  81. if($ftp->ftp_get($_G['setting']['ftp']['connid'], $tmpfile, $file, FTP_BINARY)) {
  82. @readfile($tmpfile);
  83. @unlink($tmpfile);
  84. } else {
  85. @unlink($tmpfile);
  86. return FALSE;
  87. }
  88. }
  89. return TRUE;
  90. }
  91. function getlocalfile($filename, $readmod = 2, $range = 0) {
  92. if($readmod == 1 || $readmod == 3 || $readmod == 4) {
  93. if($fp = @fopen($filename, 'rb')) {
  94. @fseek($fp, $range);
  95. if(function_exists('fpassthru') && ($readmod == 3 || $readmod == 4)) {
  96. @fpassthru($fp);
  97. } else {
  98. echo @fread($fp, filesize($filename));
  99. }
  100. }
  101. @fclose($fp);
  102. } else {
  103. @readfile($filename);
  104. }
  105. @flush();
  106. @ob_flush();
  107. }
  108. ?>