attachment.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * MyBB 1.8
  4. * Copyright 2014 MyBB Group, All Rights Reserved
  5. *
  6. * Website: http://www.mybb.com
  7. * License: http://www.mybb.com/about/license
  8. *
  9. */
  10. define("IN_MYBB", 1);
  11. define('THIS_SCRIPT', 'attachment.php');
  12. require_once "./global.php";
  13. if($mybb->settings['enableattachments'] != 1)
  14. {
  15. error($lang->attachments_disabled);
  16. }
  17. // Find the AID we're looking for
  18. if(isset($mybb->input['thumbnail']))
  19. {
  20. $aid = $mybb->get_input('thumbnail', MyBB::INPUT_INT);
  21. }
  22. else
  23. {
  24. $aid = $mybb->get_input('aid', MyBB::INPUT_INT);
  25. }
  26. $pid = $mybb->get_input('pid', MyBB::INPUT_INT);
  27. // Select attachment data from database
  28. if($aid)
  29. {
  30. $query = $db->simple_select("attachments", "*", "aid='{$aid}'");
  31. }
  32. else
  33. {
  34. $query = $db->simple_select("attachments", "*", "pid='{$pid}'");
  35. }
  36. $attachment = $db->fetch_array($query);
  37. $plugins->run_hooks("attachment_start");
  38. if(!$attachment)
  39. {
  40. error($lang->error_invalidattachment);
  41. }
  42. if($attachment['thumbnail'] == '' && isset($mybb->input['thumbnail']))
  43. {
  44. error($lang->error_invalidattachment);
  45. }
  46. $attachtypes = (array)$cache->read('attachtypes');
  47. $ext = get_extension($attachment['filename']);
  48. if(empty($attachtypes[$ext]))
  49. {
  50. error($lang->error_invalidattachment);
  51. }
  52. $attachtype = $attachtypes[$ext];
  53. $pid = $attachment['pid'];
  54. // Don't check the permissions on preview
  55. if($pid || $attachment['uid'] != $mybb->user['uid'])
  56. {
  57. $post = get_post($pid);
  58. $thread = get_thread($post['tid']);
  59. if(!$thread && !isset($mybb->input['thumbnail']))
  60. {
  61. error($lang->error_invalidthread);
  62. }
  63. $fid = $thread['fid'];
  64. // Get forum info
  65. $forum = get_forum($fid);
  66. // Permissions
  67. $forumpermissions = forum_permissions($fid);
  68. if($forumpermissions['canview'] == 0 || $forumpermissions['canviewthreads'] == 0 || (isset($forumpermissions['canonlyviewownthreads']) && $forumpermissions['canonlyviewownthreads'] != 0 && $thread['uid'] != $mybb->user['uid']) || ($forumpermissions['candlattachments'] == 0 && !$mybb->input['thumbnail']))
  69. {
  70. error_no_permission();
  71. }
  72. // Error if attachment is invalid or not visible
  73. if(!$attachment['attachname'] || (!is_moderator($fid, "canviewunapprove") && ($attachment['visible'] != 1 || $thread['visible'] != 1 || $post['visible'] != 1)))
  74. {
  75. error($lang->error_invalidattachment);
  76. }
  77. if($attachtype['forums'] != -1 && strpos(','.$attachtype['forums'].',', ','.$fid.',') === false)
  78. {
  79. error_no_permission();
  80. }
  81. }
  82. if(!isset($mybb->input['thumbnail'])) // Only increment the download count if this is not a thumbnail
  83. {
  84. if(!is_member($attachtype['groups']))
  85. {
  86. error_no_permission();
  87. }
  88. $attachupdate = array(
  89. "downloads" => $attachment['downloads']+1,
  90. );
  91. $db->update_query("attachments", $attachupdate, "aid='{$attachment['aid']}'");
  92. }
  93. // basename isn't UTF-8 safe. This is a workaround.
  94. $attachment['filename'] = ltrim(basename(' '.$attachment['filename']));
  95. $plugins->run_hooks("attachment_end");
  96. if(isset($mybb->input['thumbnail']))
  97. {
  98. if(!file_exists($mybb->settings['uploadspath']."/".$attachment['thumbnail']))
  99. {
  100. error($lang->error_invalidattachment);
  101. }
  102. $ext = get_extension($attachment['thumbnail']);
  103. switch($ext)
  104. {
  105. case "gif":
  106. $type = "image/gif";
  107. break;
  108. case "bmp":
  109. $type = "image/bmp";
  110. break;
  111. case "png":
  112. $type = "image/png";
  113. break;
  114. case "jpg":
  115. case "jpeg":
  116. case "jpe":
  117. $type = "image/jpeg";
  118. break;
  119. default:
  120. $type = "image/unknown";
  121. break;
  122. }
  123. header("Content-disposition: filename=\"{$attachment['filename']}\"");
  124. header("Content-type: ".$type);
  125. $thumb = $mybb->settings['uploadspath']."/".$attachment['thumbnail'];
  126. header("Content-length: ".@filesize($thumb));
  127. $handle = fopen($thumb, 'rb');
  128. while(!feof($handle))
  129. {
  130. echo fread($handle, 8192);
  131. }
  132. fclose($handle);
  133. }
  134. else
  135. {
  136. if(!file_exists($mybb->settings['uploadspath']."/".$attachment['attachname']))
  137. {
  138. error($lang->error_invalidattachment);
  139. }
  140. $ext = get_extension($attachment['filename']);
  141. switch($attachment['filetype'])
  142. {
  143. case "application/pdf":
  144. case "image/bmp":
  145. case "image/gif":
  146. case "image/jpeg":
  147. case "image/pjpeg":
  148. case "image/png":
  149. case "text/plain":
  150. header("Content-type: {$attachment['filetype']}");
  151. $disposition = "inline";
  152. break;
  153. default:
  154. $filetype = $attachment['filetype'];
  155. if(!$filetype)
  156. {
  157. $filetype = 'application/force-download';
  158. }
  159. header("Content-type: {$filetype}");
  160. $disposition = "attachment";
  161. }
  162. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "msie") !== false)
  163. {
  164. header("Content-disposition: attachment; filename=\"{$attachment['filename']}\"");
  165. }
  166. else
  167. {
  168. header("Content-disposition: {$disposition}; filename=\"{$attachment['filename']}\"");
  169. }
  170. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "msie 6.0") !== false)
  171. {
  172. header("Expires: -1");
  173. }
  174. header("Content-length: {$attachment['filesize']}");
  175. header("Content-range: bytes=0-".($attachment['filesize']-1)."/".$attachment['filesize']);
  176. $handle = fopen($mybb->settings['uploadspath']."/".$attachment['attachname'], 'rb');
  177. while(!feof($handle))
  178. {
  179. echo fread($handle, 8192);
  180. }
  181. fclose($handle);
  182. }