dropbox_download.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php //$id: $
  2. /* For licensing terms, see /license.txt */
  3. /*
  4. ==============================================================================
  5. INIT SECTION
  6. ==============================================================================
  7. */
  8. // we cannot use dropbox_init.inc.php because this one already outputs data.
  9. //name of langfile
  10. // name of the language file that needs to be included
  11. $language_file = "dropbox";
  12. // including the basic Dokeos initialisation file
  13. require_once '../inc/global.inc.php';
  14. // the dropbox configuration parameters
  15. require_once 'dropbox_config.inc.php';
  16. // the dropbox file that contains additional functions
  17. require_once 'dropbox_functions.inc.php';
  18. // the dropbox class
  19. require_once 'dropbox_class.inc.php';
  20. //
  21. require_once api_get_path(LIBRARY_PATH).'/document.lib.php';
  22. /*
  23. ==============================================================================
  24. DOWNLOAD A FOLDER
  25. ==============================================================================
  26. */
  27. if ( isset($_GET['cat_id']) AND is_numeric($_GET['cat_id']) AND $_GET['action']=='downloadcategory' AND isset($_GET['sent_received']) )
  28. {
  29. // step 1: constructingd' the sql statement. Due to the nature off the classes of the dropbox the categories for sent files are stored in the table
  30. // dropbox_file while the categories for the received files are stored in dropbox_post. It would have been more elegant if these could be stored
  31. // in dropbox_person (which stores the link file-person)
  32. // Therefore we have to create to separate sql statements to find which files are in the categorie (depending if we zip-download a sent category or a
  33. // received category)
  34. if ($_GET['sent_received']=='sent')
  35. {
  36. // here we also incorporate the person table to make sure that deleted sent documents are not included.
  37. $sql="SELECT DISTINCT file.id, file.filename, file.title FROM ".$dropbox_cnf["tbl_file"]." file, ".$dropbox_cnf["tbl_person"]." person
  38. WHERE file.uploader_id='".Database::escape_string($_user['user_id'])."'
  39. AND file.cat_id='".Database::escape_string($_GET['cat_id'])."'
  40. AND person.user_id='".Database::escape_string($_user['user_id'])."'
  41. AND person.file_id=file.id
  42. " ;
  43. }
  44. if ($_GET['sent_received']=='received')
  45. {
  46. $sql="SELECT DISTINCT file.id, file.filename, file.title FROM ".$dropbox_cnf["tbl_file"]." file, ".$dropbox_cnf["tbl_person"]." person, ".$dropbox_cnf["tbl_post"]." post
  47. WHERE post.cat_id='".Database::escape_string($_GET['cat_id'])."'
  48. AND person.user_id='".Database::escape_string($_user['user_id'])."'
  49. AND person.file_id=file.id
  50. AND post.file_id=file.id
  51. " ;
  52. }
  53. $result=Database::query($sql);
  54. while ($row=Database::fetch_array($result))
  55. {
  56. $files_to_download[]=$row['id'];
  57. }
  58. if (!is_array($files_to_download) OR empty($files_to_download))
  59. {
  60. header ("location: index.php?view=".Security::remove_XSS($_GET['sent_received'])."&error=ErrorNoFilesInFolder");
  61. exit;
  62. }
  63. zip_download($files_to_download);
  64. exit;
  65. }
  66. /*
  67. ==============================================================================
  68. DOWNLOAD A FILE
  69. ==============================================================================
  70. */
  71. /*
  72. ------------------------------------------------------------------------------
  73. AUTHORIZATION
  74. ------------------------------------------------------------------------------
  75. */
  76. // Check if the id makes sense
  77. if ( ! isset( $_GET['id']) || ! is_numeric( $_GET['id']))
  78. {
  79. Display::display_header($nameTools,"Dropbox");
  80. Display :: display_error_message(get_lang('Error'));
  81. Display::display_footer();
  82. exit;
  83. }
  84. // Check if the user is allowed to download the file
  85. $allowed_to_download=false;
  86. // Check if the user has sent or received the file.
  87. $sql="SELECT * FROM ".$dropbox_cnf["tbl_person"]." WHERE file_id='".Database::escape_string($_GET['id'])."' AND user_id='".Database::escape_string($_user['user_id'])."'";
  88. $result=Database::query($sql);
  89. if (Database::num_rows($result)>0)
  90. {
  91. $allowed_to_download=true;
  92. }
  93. /*
  94. ------------------------------------------------------------------------------
  95. ERROR IF NOT ALLOWED TO DOWNLOAD
  96. ------------------------------------------------------------------------------
  97. */
  98. if (!$allowed_to_download)
  99. {
  100. Display::display_header($nameTools,"Dropbox");
  101. Display :: display_error_message(get_lang('YouAreNotAllowedToDownloadThisFile'));
  102. Display::display_footer();
  103. exit;
  104. }
  105. /*
  106. ------------------------------------------------------------------------------
  107. DOWNLOAD THE FILE
  108. ------------------------------------------------------------------------------
  109. */
  110. // the user is allowed to download the file
  111. else
  112. {
  113. $_SESSION['_seen'][$_course['id']][TOOL_DROPBOX][]=intval($_GET['id']);
  114. $work = new Dropbox_work($_GET['id']);
  115. $path = dropbox_cnf("sysPath") . "/" . $work -> filename; //path to file as stored on server
  116. $file = $work->title;
  117. require_once(api_get_path(LIBRARY_PATH) . '/document.lib.php');
  118. $mimetype = DocumentManager::file_get_mime_type(TRUE);
  119. $fileparts = explode( '.', $file);
  120. $filepartscount = count( $fileparts);
  121. if ( ( $filepartscount > 1) && isset( $mimetype[$fileparts [$filepartscount - 1]]) && $_GET['action']<>'download')
  122. {
  123. // give hint to browser about filetype
  124. header( "Content-type: " . $mimetype[$fileparts [$filepartscount - 1]] . "\n");
  125. }
  126. else
  127. {
  128. //no information about filetype: force a download dialog window in browser
  129. header( "Content-type: application/octet-stream\n");
  130. }
  131. if (!in_array(strtolower($fileparts [$filepartscount - 1]), array('doc','xls','ppt','pps','sxw','sxc','sxi')))
  132. {
  133. header('Content-Disposition: inline; filename='.$file); // bugs with open office
  134. }
  135. else
  136. {
  137. header('Content-Disposition: attachment; filename='.$file);
  138. }
  139. /**
  140. * Note that if you use these two headers from a previous example:
  141. * header('Cache-Control: no-cache, must-revalidate');
  142. * header('Pragma: no-cache');
  143. * before sending a file to the browser, the "Open" option on Internet Explorer's file download dialog will not work properly. If the user clicks "Open" instead of "Save," the target application will open an empty file, because the downloaded file was not cached. The user will have to save the file to their hard drive in order to use it.
  144. * Make sure to leave these headers out if you'd like your visitors to be able to use IE's "Open" option.
  145. */
  146. header( "Pragma: \n");
  147. header( "Cache-Control: \n");
  148. header( "Cache-Control: public\n"); // IE cannot download from sessions without a cache
  149. /*if ( isset( $_SERVER["HTTPS"]))
  150. {
  151. /**
  152. * We need to set the following headers to make downloads work using IE in HTTPS mode.
  153. *
  154. //header( "Pragma: ");
  155. //header( "Cache-Control: ");
  156. header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT\n");
  157. header( "Last-Modified: " . gmdate( "D, d M Y H:i:s") . " GMT\n");
  158. header( "Cache-Control: no-store, no-cache, must-revalidate\n"); // HTTP/1.1
  159. header( "Cache-Control: post-check=0, pre-check=0\n", false);
  160. }*/
  161. header( "Content-Description: " . trim( htmlentities( $file)) . "\n");
  162. header( "Content-Transfer-Encoding: binary\n");
  163. header( "Content-Length: " . filesize( $path)."\n" );
  164. $fp = fopen( $path, "rb");
  165. fpassthru( $fp);
  166. exit();
  167. }
  168. /**
  169. ==============================================================================
  170. * Dropbox module for Claroline
  171. * handles downloads of files. Direct downloading is prevented because of an .htaccess file in the
  172. * dropbox directory. So everything goes through this script.
  173. *
  174. * 1. Initialising vars
  175. * 2. Authorisation
  176. * 3. Sanity check of get data & file
  177. * 4. Send headers
  178. * 5. Send file
  179. *
  180. *
  181. * NOTE :
  182. * When testing this with PHP4.0.4 on WinXP and Apache2 there were problems with downloading in IE6
  183. * After searching the only explanation I could find is a problem with the headers:
  184. *
  185. * HEADERS SENT WITH PHP4.3:
  186. * HTTP/1.1�200�OK(CR)
  187. * (LF)
  188. * Date:�Fri,�12�Sep�2003�19:07:33�GMT(CR)
  189. * (LF)
  190. * Server:�Apache/2.0.47�(Win32)�PHP/4.3.3(CR)
  191. * (LF)
  192. * X-Powered-By:�PHP/4.3.3(CR)
  193. * (LF)
  194. * Set-Cookie:�PHPSESSID=06880edcc8363be3f60929576fc1bc6e;�path=/(CR)
  195. * (LF)
  196. * Expires:�Thu,�19�Nov�1981�08:52:00�GMT(CR)
  197. * (LF)
  198. * Cache-Control:�public(CR)
  199. * (LF)
  200. * Pragma:�(CR)
  201. * (LF)
  202. * Content-Transfer-Encoding:�binary(CR)
  203. * (LF)
  204. * Content-Disposition:�attachment;�filename=SV-262E4.png(CR)
  205. * (LF)
  206. * Content-Length:�92178(CR)
  207. * (LF)
  208. * Connection:�close(CR)
  209. * (LF)
  210. * Content-Type:�application/octet-stream(CR)
  211. * (LF)
  212. * (CR)
  213. * (LF)
  214. *
  215. * HEADERS SENT WITH PHP4.0.4:
  216. * HTTP/1.1�200�OK(CR)
  217. * (LF)
  218. * Date:�Fri,�12�Sep�2003�18:28:21�GMT(CR)
  219. * (LF)
  220. * Server:�Apache/2.0.47�(Win32)(CR)
  221. * (LF)
  222. * X-Powered-By:�PHP/4.0.4(CR)
  223. * (LF)
  224. * Expires:�Thu,�19�Nov�1981�08:52:00�GMT(CR)
  225. * (LF)
  226. * Cache-Control:�no-store,�no-cache,�must-revalidate,�post-check=0,�pre-check=0,�,�public(CR)
  227. * (LF)
  228. * Pragma:�no-cache,�(CR)
  229. * (LF)
  230. * Content-Disposition:�attachment;�filename=SV-262E4.png(CR)
  231. * (LF)
  232. * Content-Transfer-Encoding:�binary(CR)
  233. * (LF)
  234. * Set-Cookie:�PHPSESSID=0a5b1c1b9d5e3b474fef359ee55e82d0;�path=/(CR)
  235. * (LF)
  236. * Content-Length:�92178(CR)
  237. * (LF)
  238. * Connection:�close(CR)
  239. * (LF)
  240. * Content-Type:�application/octet-stream(CR)
  241. * (LF)
  242. * (CR)
  243. * (LF)
  244. *
  245. * As you can see the there is a difference in the Cache-Control directive. I suspect that this
  246. * explains the problem. Also take a look at http://bugs.php.net/bug.php?id=16458.
  247. *
  248. * @version 1.21
  249. * @copyright 2004-2005
  250. * @author Jan Bols <jan@ivpv.UGent.be>, main programmer
  251. * @author René Haentjens <rene.haentjens@UGent.be>, several contributions (see RH)
  252. * @author Roan Embrechts, virtual course support
  253. *
  254. * @package dokeos.dropbox
  255. ==============================================================================
  256. */
  257. /*
  258. ==============================================================================
  259. INITIALISING VARIABLES
  260. ==============================================================================
  261. */
  262. require_once( "dropbox_init.inc.php"); //only call init1 because init2 outputs data
  263. require_once( "dropbox_class.inc.php");
  264. /*
  265. ==============================================================================
  266. AUTHORISATION SECTION
  267. ==============================================================================
  268. */
  269. if ( !isset( $_user['user_id']) || !$is_course_member )
  270. {
  271. exit( );
  272. }
  273. if ($_GET['mailing']) // RH: Mailing detail window call
  274. getUserOwningThisMailing($_GET['mailing'], $_user['user_id'], '500'); // RH or die
  275. /*
  276. ==============================================================================
  277. SANITY CHECKS OF GET DATA & FILE
  278. ==============================================================================
  279. */
  280. if ( ! isset( $_GET['id']) || ! is_numeric( $_GET['id'])) die(dropbox_lang("generalError")." (code 501)");
  281. $work = new Dropbox_work($_GET['id']);
  282. $path = dropbox_cnf("sysPath") . "/" . $work -> filename; //path to file as stored on server
  283. $file = $work->title;
  284. // check that this file exists and that it doesn't include any special characters
  285. //if ( !is_file( $path) || ! eregi( '^[A-Z0-9_\-][A-Z0-9._\-]*$', $file))
  286. if ( !is_file( $path))
  287. {
  288. die(dropbox_lang("generalError")." (code 504)");
  289. }
  290. /*
  291. ==============================================================================
  292. SEND HEADERS
  293. ==============================================================================
  294. */
  295. require_once(api_get_path(LIBRARY_PATH) . '/document.lib.php');
  296. $mimetype = DocumentManager::file_get_mime_type(TRUE);
  297. $fileparts = explode( '.', $file);
  298. $filepartscount = count( $fileparts);
  299. if ( ( $filepartscount > 1) && isset( $mimetype[$fileparts [$filepartscount - 1]]))
  300. {
  301. // give hint to browser about filetype
  302. header( "Content-type: " . $mimetype[$fileparts [$filepartscount - 1]] . "\n");
  303. }
  304. else
  305. {
  306. //no information about filetype: force a download dialog window in browser
  307. header( "Content-type: application/octet-stream\n");
  308. }
  309. if (!in_array(strtolower($fileparts [$filepartscount - 1]), array('doc','xls','ppt','pps','sxw','sxc','sxi')))
  310. {
  311. header('Content-Disposition: inline; filename='.$file); // bugs with open office
  312. }
  313. else
  314. {
  315. header('Content-Disposition: attachment; filename='.$file);
  316. }
  317. /**
  318. * Note that if you use these two headers from a previous example:
  319. * header('Cache-Control: no-cache, must-revalidate');
  320. * header('Pragma: no-cache');
  321. * before sending a file to the browser, the "Open" option on Internet Explorer's file download dialog will not work properly. If the user clicks "Open" instead of "Save," the target application will open an empty file, because the downloaded file was not cached. The user will have to save the file to their hard drive in order to use it.
  322. * Make sure to leave these headers out if you'd like your visitors to be able to use IE's "Open" option.
  323. */
  324. header( "Pragma: \n");
  325. header( "Cache-Control: \n");
  326. header( "Cache-Control: public\n"); // IE cannot download from sessions without a cache
  327. /*if ( isset( $_SERVER["HTTPS"]))
  328. {
  329. /**
  330. * We need to set the following headers to make downloads work using IE in HTTPS mode.
  331. *
  332. //header( "Pragma: ");
  333. //header( "Cache-Control: ");
  334. header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT\n");
  335. header( "Last-Modified: " . gmdate( "D, d M Y H:i:s") . " GMT\n");
  336. header( "Cache-Control: no-store, no-cache, must-revalidate\n"); // HTTP/1.1
  337. header( "Cache-Control: post-check=0, pre-check=0\n", false);
  338. }*/
  339. header( "Content-Description: " . trim( htmlentities( $file)) . "\n");
  340. header( "Content-Transfer-Encoding: binary\n");
  341. header( "Content-Length: " . filesize( $path)."\n" );
  342. /*
  343. ==============================================================================
  344. SEND FILE
  345. ==============================================================================
  346. */
  347. $fp = fopen( $path, "rb");
  348. fpassthru( $fp);
  349. exit( );
  350. /**
  351. * Found a workaround to another headache that just cropped up tonight. Apparently Opera 6.1 on Linux (unsure of other versions/platforms) has problems downloading files using the above methods if you have enabled compression via zlib.output_compression in php.ini.
  352. * It seems that Opera sees that the actual transfer size is less than the size in the "Content-length" header for the download and decides that the transfer was incomplete or corrupted. It then either continuously retries the download or else leaves you with a corrupted file.
  353. * Solution: Make sure your download script/section is off in its own directory. and add the following to your .htaccess file for that directory:
  354. * php_flag zlib.output_compression off
  355. */
  356. ?>