PageRenderTime 49ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/attachment.php

https://github.com/delete66/sikevux-s-tracker
PHP | 93 lines | 82 code | 6 blank | 5 comment | 13 complexity | cc8a954389e4a86de54d6923f0aca08b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. require_once("include/bittorrent.php");
  3. require_once("include/user_functions.php");
  4. require_once("include/bbcode_functions.php");
  5. dbconn();
  6. maxcoder();
  7. if(!logged_in())
  8. {
  9. header("HTTP/1.0 404 Not Found");
  10. // moddifed logginorreturn by retro//Remember to change the following line to match your server
  11. print("<html><h1>Not Found</h1><p>The requested URL /{$_SERVER['PHP_SELF']} was not found on this server.</p><hr /><address>Apache/1.1.11 (xxxxx) Server at ".$_SERVER['SERVER_NAME']." Port 80</address></body></html>\n");
  12. die();
  13. }
  14. parked();
  15. if (get_user_class() < UC_VIP) {
  16. stdmsg("Sorry", "No permissions.");
  17. stdfoot();
  18. exit;
  19. }
  20. // ######################## SET PHP ENVIRONMENT ###########################
  21. error_reporting(E_ALL & ~E_NOTICE);
  22. @ini_set('zlib.output_compression', 'Off');
  23. @set_time_limit(0);
  24. if (@ini_get('output_handler') == 'ob_gzhandler' AND @ob_get_length() !== false)
  25. { // if output_handler = ob_gzhandler, turn it off and remove the header sent by PHP
  26. @ob_end_clean();
  27. header('Content-Encoding:');
  28. }
  29. if (empty($_REQUEST['attachmentid']))
  30. {
  31. // return not found header
  32. httperr();
  33. }
  34. $id = (int)$_GET['attachmentid'];
  35. $attachment_dir = ROOT_PATH."forumattaches";
  36. $at = sql_query("SELECT * FROM attachments WHERE id=".sqlesc($id)) or sqlerr(__FILE__, __LINE__);
  37. $resat = mysql_fetch_assoc($at);
  38. $filename = $attachment_dir.'/'.$resat['filename'];
  39. if (!$resat || !is_file($filename) || !is_readable($filename))
  40. {
  41. // return not found header
  42. httperr();
  43. }
  44. if ($_GET['action'] == 'delete') {
  45. if (get_user_class() >= UC_MODERATOR) {
  46. @unlink($filename);
  47. sql_query("DELETE FROM attachments WHERE id=".sqlesc($id)) or sqlerr(__FILE__, __LINE__);
  48. sql_query("DELETE FROM attachmentdownloads WHERE fileid=".sqlesc($id)) or sqlerr(__FILE__, __LINE__);
  49. die('<font color=red>File successfull deleted...');
  50. }else {
  51. httperr();
  52. }
  53. }
  54. $file_extension = strtolower(substr(strrchr($filename,"."),1));
  55. switch( $file_extension )
  56. {
  57. case "pdf": $ctype="application/pdf"; break;
  58. case "exe": $ctype="application/octet-stream"; break;
  59. case "zip": $ctype="application/zip"; break;
  60. case "rar": $ctype="application/zip"; break;
  61. case "doc": $ctype="application/msword"; break;
  62. case "xls": $ctype="application/vnd.ms-excel"; break;
  63. case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  64. case "gif": $ctype="image/gif"; break;
  65. case "png": $ctype="image/png"; break;
  66. case "jpeg":
  67. case "jpg": $ctype="image/jpg"; break;
  68. default: $ctype="application/force-download";
  69. }
  70. sql_query("UPDATE attachments SET downloads = downloads + 1 WHERE id = ".sqlesc($id)) or sqlerr(__FILE__, __LINE__);
  71. $res = sql_query("SELECT fileid FROM attachmentdownloads WHERE fileid=".sqlesc($id)." AND userid=".sqlesc($CURUSER['id']));
  72. if(mysql_num_rows($res) == "0")
  73. sql_query("INSERT INTO attachmentdownloads (filename,fileid,username,userid,date,downloads) VALUES (".sqlesc($resat['filename']).", ".sqlesc($id).", ".sqlesc($CURUSER['username']).", ".sqlesc($CURUSER['id']).", ".sqlesc(get_date_time()).", 1)") or sqlerr(__FILE__, __LINE__);
  74. else
  75. sql_query("UPDATE attachmentdownloads SET downloads = downloads + 1 WHERE fileid=".sqlesc($id)." AND userid=".sqlesc($CURUSER['id']));
  76. header("Pragma: public"); // required
  77. header("Expires: 0");
  78. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  79. header("Cache-Control: private",false); // required for certain browsers
  80. header("Content-Type: $ctype");
  81. // change, added quotes to allow spaces in filenames, by Rajkumar Singh
  82. header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
  83. header("Content-Transfer-Encoding: binary");
  84. header("Content-Length: ".filesize($filename));
  85. readfile("$filename");
  86. exit();
  87. ?>