PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/download.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 363 lines | 209 code | 47 blank | 107 comment | 48 complexity | 22707cb704d70e1016ceabb85e67cb9b MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. *
  12. * @Extra credits for this file
  13. * (c) 2002 Meik Sievertsen (Acyd Burn)
  14. *
  15. */
  16. define('IN_DOWNLOAD', true);
  17. define('IN_ICYPHOENIX', true);
  18. if (!defined('IP_ROOT_PATH')) define('IP_ROOT_PATH', './');
  19. if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
  20. include(IP_ROOT_PATH . 'common.' . PHP_EXT);
  21. //
  22. // Delete the / * to uncomment the block, and edit the values (read the comments) to
  23. // enable additional security to your board (preventing third site linkage)
  24. //
  25. /*
  26. define('ALLOWED_DENIED', 0);
  27. define('DENIED_ALLOWED', 1);
  28. //
  29. // From this line on you are able to edit the stuff
  30. //
  31. // Possible Values:
  32. // ALLOWED_DENIED <- First allow the listed sites, and then deny all others
  33. // DENIED_ALLOWED <- First deny the listed sites, and then allow all others
  34. $allow_deny_order = ALLOWED_DENIED;
  35. //
  36. // Allowed Syntax:
  37. // Full Domain Name -> www.opentools.de
  38. // Partial Domain Names -> opentools.de
  39. //
  40. $sites = array(
  41. $config['server_name'], // This is your domain
  42. 'opentools.de',
  43. 'phpbb.com',
  44. 'phpbbhacks.com',
  45. 'phpbb.de'
  46. );
  47. // This is the message displayed, if someone links to this site...
  48. $lang['Denied_Message'] = 'You are not authorized to view, download or link to this Site.';
  49. // End of editable area
  50. //
  51. // Parse the order and evaluate the array
  52. //
  53. $site = explode('?', $_SERVER['HTTP_REFERER']);
  54. $url = trim($site[0]);
  55. //$url = $HTTP_HOST;
  56. if ($url != '')
  57. {
  58. $allowed = ($allow_deny_order == ALLOWED_DENIED) ? false : true;
  59. for ($i = 0; $i < sizeof($sites); $i++)
  60. {
  61. if (strstr($url, $sites[$i]))
  62. {
  63. $allowed = ($allow_deny_order == ALLOWED_DENIED) ? true : false;
  64. break;
  65. }
  66. }
  67. }
  68. else
  69. {
  70. $allowed = true;
  71. }
  72. if ($allowed == false)
  73. {
  74. message_die(GENERAL_MESSAGE, $lang['Denied_Message']);
  75. }
  76. // Delete the following line, to uncomment this block
  77. */
  78. $download_id = request_var('id', 0);
  79. $thumbnail = request_var('thumb', 0);
  80. // Send file to browser
  81. function send_file_to_browser($attachment, $upload_dir)
  82. {
  83. global $HTTP_USER_AGENT, $db, $config, $lang;
  84. $filename = ($upload_dir == '') ? $attachment['physical_filename'] : $upload_dir . '/' . $attachment['physical_filename'];
  85. $gotit = false;
  86. if (!intval($config['allow_ftp_upload']))
  87. {
  88. if (@!file_exists(@amod_realpath($filename)))
  89. {
  90. message_die(GENERAL_ERROR, $lang['Error_no_attachment'] . "<br /><br /><b>404 File Not Found:</b> The File <i>" . $filename . "</i> does not exist.");
  91. }
  92. else
  93. {
  94. $gotit = true;
  95. }
  96. }
  97. // Correct the mime type - we force application/octetstream for all files, except images
  98. // Please do not change this, it is a security precaution
  99. if (!strstr($attachment['mimetype'], 'image'))
  100. {
  101. $attachment['mimetype'] = 'application/octet-stream';
  102. }
  103. // Now the tricky part... let's dance
  104. // @ob_end_clean();
  105. // @ini_set('zlib.output_compression', 'Off');
  106. header('Pragma: public');
  107. // header('Content-Transfer-Encoding: none');
  108. $real_filename = html_entity_decode(basename($attachment['real_filename']));
  109. // Send out the Headers
  110. header('Content-Type: ' . $attachment['mimetype'] . '; name="' . $real_filename . '"');
  111. header('Content-Disposition: inline; filename="' . $real_filename . '"');
  112. unset($real_filename);
  113. //
  114. // Now send the File Contents to the Browser
  115. //
  116. if ($gotit)
  117. {
  118. $size = @filesize($filename);
  119. if ($size)
  120. {
  121. header('Content-length: ' . $size);
  122. }
  123. readfile($filename);
  124. }
  125. elseif (!$gotit && intval($config['allow_ftp_upload']))
  126. {
  127. $conn_id = attach_init_ftp();
  128. $ini_val = (@phpversion() >= '4.0.0') ? 'ini_get' : 'get_cfg_var';
  129. $tmp_path = (!@$ini_val('safe_mode')) ? '/tmp' : $upload_dir;
  130. $tmp_filename = @tempnam($tmp_path, 't0000');
  131. @unlink($tmp_filename);
  132. $mode = FTP_BINARY;
  133. if ((preg_match("/text/i", $attachment['mimetype'])) || (preg_match("/html/i", $attachment['mimetype'])))
  134. {
  135. $mode = FTP_ASCII;
  136. }
  137. $result = @ftp_get($conn_id, $tmp_filename, $filename, $mode);
  138. if (!$result)
  139. {
  140. message_die(GENERAL_ERROR, $lang['Error_no_attachment'] . "<br /><br /><b>404 File Not Found:</b> The File <i>" . $filename . "</i> does not exist.");
  141. }
  142. @ftp_quit($conn_id);
  143. $size = @filesize($tmp_filename);
  144. if ($size)
  145. {
  146. header('Content-length: ' . $size);
  147. }
  148. readfile($tmp_filename);
  149. @unlink($tmp_filename);
  150. }
  151. else
  152. {
  153. message_die(GENERAL_ERROR, $lang['Error_no_attachment'] . "<br /><br /><b>404 File Not Found:</b> The File <i>" . $filename . "</i> does not exist.");
  154. }
  155. exit;
  156. }
  157. //
  158. // End Functions
  159. //
  160. // Start session management
  161. $user->session_begin();
  162. $auth->acl($user->data);
  163. $user->setup();
  164. // End session management
  165. if (!$download_id)
  166. {
  167. message_die(GENERAL_ERROR, $lang['No_attachment_selected']);
  168. }
  169. if ($config['disable_attachments_mod'] && ($user->data['user_level'] != ADMIN))
  170. {
  171. message_die(GENERAL_MESSAGE, $lang['Attachment_feature_disabled']);
  172. }
  173. $sql = 'SELECT *
  174. FROM ' . ATTACHMENTS_DESC_TABLE . '
  175. WHERE attach_id = ' . (int) $download_id;
  176. $result = $db->sql_query($sql);
  177. if (!($attachment = $db->sql_fetchrow($result)))
  178. {
  179. message_die(GENERAL_MESSAGE, $lang['Error_no_attachment']);
  180. }
  181. $attachment['physical_filename'] = get_physical_filename($attachment['physical_filename'], false);
  182. $db->sql_freeresult($result);
  183. // get forum_id for attachment authorization or private message authorization
  184. $authorized = false;
  185. $sql = 'SELECT *
  186. FROM ' . ATTACHMENTS_TABLE . '
  187. WHERE attach_id = ' . (int) $attachment['attach_id'];
  188. $result = $db->sql_query($sql);
  189. $auth_pages = $db->sql_fetchrowset($result);
  190. $num_auth_pages = $db->sql_numrows($result);
  191. for ($i = 0; $i < $num_auth_pages && $authorized == false; $i++)
  192. {
  193. $auth_pages[$i]['post_id'] = intval($auth_pages[$i]['post_id']);
  194. if ($auth_pages[$i]['post_id'] != 0)
  195. {
  196. $sql = 'SELECT forum_id
  197. FROM ' . POSTS_TABLE . '
  198. WHERE post_id = ' . (int) $auth_pages[$i]['post_id'];
  199. $result = $db->sql_query($sql);
  200. $row = $db->sql_fetchrow($result);
  201. $forum_id = $row['forum_id'];
  202. $is_auth = array();
  203. $is_auth = auth(AUTH_ALL, $forum_id, $user->data);
  204. if ($is_auth['auth_download'])
  205. {
  206. $authorized = true;
  207. }
  208. }
  209. else
  210. {
  211. if ((intval($config['allow_pm_attach'])) && (($user->data['user_id'] == $auth_pages[$i]['user_id_2']) || ($user->data['user_id'] == $auth_pages[$i]['user_id_1'])) || ($user->data['user_level'] == ADMIN))
  212. {
  213. $authorized = true;
  214. }
  215. }
  216. }
  217. if (!$authorized)
  218. {
  219. message_die(GENERAL_MESSAGE, $lang['Sorry_auth_view_attach']);
  220. }
  221. // Get Information on currently allowed Extensions
  222. $sql = "SELECT e.extension, g.download_mode
  223. FROM " . EXTENSION_GROUPS_TABLE . " g, " . EXTENSIONS_TABLE . " e
  224. WHERE (g.allow_group = 1) AND (g.group_id = e.group_id)";
  225. $result = $db->sql_query($sql);
  226. $rows = $db->sql_fetchrowset($result);
  227. $num_rows = $db->sql_numrows($result);
  228. for ($i = 0; $i < $num_rows; $i++)
  229. {
  230. $extension = strtolower(trim($rows[$i]['extension']));
  231. $allowed_extensions[] = $extension;
  232. $download_mode[$extension] = $rows[$i]['download_mode'];
  233. }
  234. // disallowed ?
  235. if (!in_array($attachment['extension'], $allowed_extensions) && ($user->data['user_level'] != ADMIN))
  236. {
  237. message_die(GENERAL_MESSAGE, sprintf($lang['Extension_disabled_after_posting'], $attachment['extension']));
  238. }
  239. $download_mode = intval($download_mode[$attachment['extension']]);
  240. if ($thumbnail)
  241. {
  242. $thumb_exists = check_thumbnail($attachment, $upload_dir);
  243. if (!empty($thumb_exists))
  244. {
  245. $attachment['physical_filename'] = get_physical_filename($attachment['physical_filename'], true);
  246. }
  247. else
  248. {
  249. $thumbnail = 0;
  250. }
  251. }
  252. // Update download count
  253. if (!$thumbnail)
  254. {
  255. update_attachments_stats($attachment['attach_id']);
  256. }
  257. // Determine the 'presenting'-method
  258. if ($download_mode == PHYSICAL_LINK)
  259. {
  260. $server_url = create_server_url();
  261. if ($script_name[strlen($script_name)] != '/')
  262. {
  263. $script_name .= '/';
  264. }
  265. if (intval($config['allow_ftp_upload']))
  266. {
  267. if (trim($config['download_path']) == '')
  268. {
  269. message_die(GENERAL_ERROR, 'Physical Download not possible with the current Attachment Setting');
  270. }
  271. $url = trim($config['download_path']) . '/' . $attachment['physical_filename'];
  272. $redirect_path = $url;
  273. }
  274. else
  275. {
  276. $url = $upload_dir . '/' . $attachment['physical_filename'];
  277. //$url = preg_replace('/^\/?(.*?\/)?$/', '\1', trim($url));
  278. $redirect_path = $server_url . $url;
  279. }
  280. // Redirect via an HTML form for PITA webservers
  281. if (@preg_match('/Microsoft|WebSTAR|Xitami/', getenv('SERVER_SOFTWARE')))
  282. {
  283. header('Refresh: 0; URL=' . $redirect_path);
  284. echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta http-equiv="refresh" content="0; url=' . $redirect_path . '"><title>Redirect</title></head><body><div align="center">If your browser does not support meta redirection please click <a href="' . $redirect_path . '">HERE</a> to be redirected</div></body></html>';
  285. exit;
  286. }
  287. // Behave as per HTTP/1.1 spec for others
  288. header('Location: ' . $redirect_path);
  289. exit;
  290. }
  291. else
  292. {
  293. if (intval($config['allow_ftp_upload']))
  294. {
  295. // We do not need a download path, we are not downloading physically
  296. send_file_to_browser($attachment, '');
  297. exit;
  298. }
  299. else
  300. {
  301. send_file_to_browser($attachment, $upload_dir);
  302. exit;
  303. }
  304. }
  305. ?>