PageRenderTime 69ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/root/blog/download.php

https://github.com/EXreaction/User-Blog-Mod
PHP | 387 lines | 267 code | 63 blank | 57 comment | 71 complexity | e1d2ddbec826d39911f1202f366ae9f1 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package phpBB3 User Blog
  5. * @version $Id: download.php 485 2008-08-15 23:33:57Z exreaction@gmail.com $
  6. * @copyright (c) 2008 EXreaction
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. if (!defined('IN_PHPBB'))
  11. {
  12. exit;
  13. }
  14. include($phpbb_root_path . 'blog/includes/functions_attachments.' . $phpEx);
  15. $blog_attachment = new blog_attachment();
  16. $user->add_lang('viewtopic');
  17. $download_id = request_var('id', 0);
  18. $mode = request_var('mode', '');
  19. if (!$download_id)
  20. {
  21. trigger_error('NO_ATTACHMENT_SELECTED');
  22. }
  23. if (!$config['allow_attachments'] && !$config['allow_pm_attach'])
  24. {
  25. trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
  26. }
  27. $sql = 'SELECT *
  28. FROM ' . BLOGS_ATTACHMENT_TABLE . "
  29. WHERE attach_id = $download_id";
  30. $result = $db->sql_query_limit($sql, 1);
  31. $attachment = $db->sql_fetchrow($result);
  32. $db->sql_freeresult($result);
  33. if (!$attachment)
  34. {
  35. trigger_error('ERROR_NO_ATTACHMENT');
  36. }
  37. $row = array();
  38. if ($attachment['is_orphan'])
  39. {
  40. if ($attachment['poster_id'] != $user->data['user_id'])
  41. {
  42. trigger_error('ERROR_NO_ATTACHMENT');
  43. }
  44. }
  45. if (!$auth->acl_get('u_download'))
  46. {
  47. trigger_error('SORRY_AUTH_VIEW_ATTACH');
  48. }
  49. blog_plugins::plugin_do('download_start');
  50. // Obtain all extensions...
  51. $extensions = $blog_attachment->obtain_blog_attach_extensions(true);
  52. // disallowed?
  53. if (!isset($extensions['_allowed_'][$attachment['extension']]))
  54. {
  55. trigger_error(sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension']));
  56. }
  57. if (!download_allowed())
  58. {
  59. trigger_error($user->lang['LINKAGE_FORBIDDEN']);
  60. }
  61. $download_mode = (int) $extensions[$attachment['extension']]['download_mode'];
  62. $attachment['physical_filename'] = basename($attachment['physical_filename']);
  63. $display_cat = $extensions[$attachment['extension']]['display_cat'];
  64. if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
  65. {
  66. $display_cat = ATTACHMENT_CATEGORY_NONE;
  67. }
  68. if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
  69. {
  70. $display_cat = ATTACHMENT_CATEGORY_NONE;
  71. }
  72. if ($mode == 'thumbnail')
  73. {
  74. $attachment['physical_filename'] = 'thumb_' . $attachment['physical_filename'];
  75. }
  76. else if (($display_cat == ATTACHMENT_CATEGORY_NONE || $display_cat == ATTACHMENT_CATEGORY_IMAGE) && !$attachment['is_orphan'])
  77. {
  78. // Update download count
  79. $sql = 'UPDATE ' . BLOGS_ATTACHMENT_TABLE . '
  80. SET download_count = download_count + 1
  81. WHERE attach_id = ' . $attachment['attach_id'];
  82. $db->sql_query($sql);
  83. }
  84. blog_plugins::plugin_do('download_before_send');
  85. if ($display_cat == ATTACHMENT_CATEGORY_IMAGE && $mode === 'view' && (strpos($attachment['mimetype'], 'image') === 0) && strpos(strtolower($user->browser), 'msie') !== false)
  86. {
  87. wrap_img_in_html(append_sid('./blog.' . $phpEx, 'page=download&amp;id=' . $attachment['attach_id']), $attachment['real_filename']);
  88. }
  89. else
  90. {
  91. // Determine the 'presenting'-method
  92. if ($download_mode == PHYSICAL_LINK)
  93. {
  94. // This presenting method should no longer be used
  95. if (!@is_dir($phpbb_root_path . $config['upload_path']))
  96. {
  97. trigger_error($user->lang['PHYSICAL_DOWNLOAD_NOT_POSSIBLE']);
  98. }
  99. redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename']);
  100. exit;
  101. }
  102. else
  103. {
  104. send_file_to_browser($attachment, $config['upload_path'] . '/blog_mod', $display_cat);
  105. exit;
  106. }
  107. }
  108. blog_plugins::plugin_do('download_end');
  109. /**
  110. * Wraps an url into a simple html page. Used to display attachments in IE.
  111. * this is a workaround for now; might be moved to template system later
  112. * direct any complaints to 1 Microsoft Way, Redmond
  113. */
  114. function wrap_img_in_html($src, $title)
  115. {
  116. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Strict.dtd">';
  117. echo '<html>';
  118. echo '<head>';
  119. echo '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
  120. echo '<title>' . $title . '</title>';
  121. echo '</head>';
  122. echo '<body>';
  123. echo '<div>';
  124. echo '<img src="' . $src . '" alt="' . $title . '" />';
  125. echo '</div>';
  126. echo '</body>';
  127. echo '</html>';
  128. }
  129. /**
  130. * Send file to browser
  131. */
  132. function send_file_to_browser($attachment, $upload_dir, $category)
  133. {
  134. global $user, $db, $config, $phpbb_root_path;
  135. $filename = $phpbb_root_path . $upload_dir . '/' . $attachment['physical_filename'];
  136. if (!@file_exists($filename))
  137. {
  138. trigger_error($user->lang['ERROR_NO_ATTACHMENT'] . '<br /><br />' . sprintf($user->lang['FILE_NOT_FOUND_404'], $filename));
  139. }
  140. // Correct the mime type - we force application/octetstream for all files, except images
  141. // Please do not change this, it is a security precaution
  142. if ($category != ATTACHMENT_CATEGORY_IMAGE || strpos($attachment['mimetype'], 'image') !== 0)
  143. {
  144. $attachment['mimetype'] = (strpos(strtolower($user->browser), 'msie') !== false || strpos(strtolower($user->browser), 'opera') !== false) ? 'application/octetstream' : 'application/octet-stream';
  145. }
  146. if (@ob_get_length())
  147. {
  148. @ob_end_clean();
  149. }
  150. // Now send the File Contents to the Browser
  151. $size = @filesize($filename);
  152. // To correctly display further errors we need to make sure we are using the correct headers for both (unsetting content-length may not work)
  153. // Check if headers already sent or not able to get the file contents.
  154. if (headers_sent() || !@file_exists($filename) || !@is_readable($filename))
  155. {
  156. // PHP track_errors setting On?
  157. if (!empty($php_errormsg))
  158. {
  159. trigger_error($user->lang['UNABLE_TO_DELIVER_FILE'] . '<br />' . sprintf($user->lang['TRACKED_PHP_ERROR'], $php_errormsg));
  160. }
  161. trigger_error('UNABLE_TO_DELIVER_FILE');
  162. }
  163. // Now the tricky part... let's dance
  164. header('Pragma: public');
  165. /**
  166. * Commented out X-Sendfile support. To not expose the physical filename within the header if xsendfile is absent we need to look into methods of checking it's status.
  167. *
  168. * Try X-Sendfile since it is much more server friendly - only works if the path is *not* outside of the root path...
  169. * lighttpd has core support for it. An apache2 module is available at http://celebnamer.celebworld.ws/stuff/mod_xsendfile/
  170. *
  171. * Not really ideal, but should work fine...
  172. * <code>
  173. * if (strpos($upload_dir, '/') !== 0 && strpos($upload_dir, '../') === false)
  174. * {
  175. * header('X-Sendfile: ' . $filename);
  176. * }
  177. * </code>
  178. */
  179. // Send out the Headers. Do not set Content-Disposition to inline please, it is a security measure for users using the Internet Explorer.
  180. header('Content-Type: ' . $attachment['mimetype']);
  181. if (empty($user->browser) || (strpos(strtolower($user->browser), 'msie') !== false))
  182. {
  183. header('Content-Disposition: attachment; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
  184. if (empty($user->browser) || (strpos(strtolower($user->browser), 'msie 6.0') !== false))
  185. {
  186. header('expires: -1');
  187. }
  188. }
  189. else
  190. {
  191. header('Content-Disposition: ' . ((strpos($attachment['mimetype'], 'image') === 0) ? 'inline' : 'attachment') . '; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
  192. }
  193. if ($size)
  194. {
  195. header("Content-Length: $size");
  196. }
  197. // Try to deliver in chunks
  198. @set_time_limit(0);
  199. $fp = @fopen($filename, 'rb');
  200. if ($fp !== false)
  201. {
  202. while (!feof($fp))
  203. {
  204. echo fread($fp, 8192);
  205. }
  206. fclose($fp);
  207. }
  208. else
  209. {
  210. @readfile($filename);
  211. }
  212. flush();
  213. exit;
  214. }
  215. /**
  216. * Get a browser friendly UTF-8 encoded filename
  217. */
  218. function header_filename($file)
  219. {
  220. $user_agent = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : '';
  221. // There be dragons here.
  222. // Not many follows the RFC...
  223. if (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Safari') !== false || strpos($user_agent, 'Konqueror') !== false)
  224. {
  225. return "filename=" . rawurlencode($file);
  226. }
  227. // follow the RFC for extended filename for the rest
  228. return "filename*=UTF-8''" . rawurlencode($file);
  229. }
  230. /**
  231. * Check if downloading item is allowed
  232. */
  233. function download_allowed()
  234. {
  235. global $config, $user, $db;
  236. if (!$config['secure_downloads'])
  237. {
  238. return true;
  239. }
  240. $url = (!empty($_SERVER['HTTP_REFERER'])) ? trim($_SERVER['HTTP_REFERER']) : trim(getenv('HTTP_REFERER'));
  241. if (!$url)
  242. {
  243. return ($config['secure_allow_empty_referer']) ? true : false;
  244. }
  245. // Split URL into domain and script part
  246. $url = @parse_url($url);
  247. if ($url === false)
  248. {
  249. return ($config['secure_allow_empty_referer']) ? true : false;
  250. }
  251. $hostname = $url['host'];
  252. unset($url);
  253. $allowed = ($config['secure_allow_deny']) ? false : true;
  254. $iplist = array();
  255. if (($ip_ary = @gethostbynamel($hostname)) !== false)
  256. {
  257. foreach ($ip_ary as $ip)
  258. {
  259. if ($ip)
  260. {
  261. $iplist[] = $ip;
  262. }
  263. }
  264. }
  265. // Check for own server...
  266. $server_name = (!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : getenv('SERVER_NAME');
  267. // Forcing server vars is the only way to specify/override the protocol
  268. if ($config['force_server_vars'] || !$server_name)
  269. {
  270. $server_name = $config['server_name'];
  271. }
  272. if (preg_match('#^.*?' . preg_quote($server_name, '#') . '.*?$#i', $hostname))
  273. {
  274. $allowed = true;
  275. }
  276. // Get IP's and Hostnames
  277. if (!$allowed)
  278. {
  279. $sql = 'SELECT site_ip, site_hostname, ip_exclude
  280. FROM ' . SITELIST_TABLE;
  281. $result = $db->sql_query($sql);
  282. while ($row = $db->sql_fetchrow($result))
  283. {
  284. $site_ip = trim($row['site_ip']);
  285. $site_hostname = trim($row['site_hostname']);
  286. if ($site_ip)
  287. {
  288. foreach ($iplist as $ip)
  289. {
  290. if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_ip, '#')) . '$#i', $ip))
  291. {
  292. if ($row['ip_exclude'])
  293. {
  294. $allowed = ($config['secure_allow_deny']) ? false : true;
  295. break 2;
  296. }
  297. else
  298. {
  299. $allowed = ($config['secure_allow_deny']) ? true : false;
  300. }
  301. }
  302. }
  303. }
  304. if ($site_hostname)
  305. {
  306. if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_hostname, '#')) . '$#i', $hostname))
  307. {
  308. if ($row['ip_exclude'])
  309. {
  310. $allowed = ($config['secure_allow_deny']) ? false : true;
  311. break;
  312. }
  313. else
  314. {
  315. $allowed = ($config['secure_allow_deny']) ? true : false;
  316. }
  317. }
  318. }
  319. }
  320. $db->sql_freeresult($result);
  321. }
  322. return $allowed;
  323. }
  324. ?>