remote.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /* See license terms in /license.txt */
  3. /**
  4. * Script that allows download of a specific file from external applications
  5. * @author Arnaud Ligot <arnaud@cblue.be>, Based on work done for old videoconference application (I have about 30 minutes to write this peace of code so if somebody has more time, feel free to rewrite it...)
  6. * @package chamilo.document
  7. */
  8. /**
  9. * Script that allows remote download of a file
  10. * @param string Action parameter (action=...)
  11. * @param string Course code (cidReq=...)
  12. * @param string Current working directory (cwd=...)
  13. * @return string JSON output
  14. */
  15. /* FIX for IE cache when using https */
  16. session_cache_limiter('none');
  17. /*==== DEBUG ====*/
  18. $debug=0;
  19. if ($debug>0) {
  20. // dump the request
  21. $v = array_keys(get_defined_vars());
  22. error_log(var_export($v, true),3, '/tmp/log');
  23. foreach (array_keys(get_defined_vars()) as $k) {
  24. if ($k == 'GLOBALS') {
  25. continue;
  26. }
  27. error_log($k, 3, '/tmp/log');
  28. error_log(var_export($$k, true), 3, '/tmp/log');
  29. }
  30. }
  31. /*==== INCLUDE ====*/
  32. require_once '../inc/global.inc.php';
  33. api_block_anonymous_users();
  34. /*==== Variables initialisation ====*/
  35. $action = $_REQUEST['action']; //safe as only used in if()'s
  36. $seek = array('/','%2F','..');
  37. $destroy = array('','','');
  38. $cidReq = str_replace($seek,$destroy,$_REQUEST["cidReq"]);
  39. $cidReq = Security::remove_XSS($cidReq);
  40. $user_id = api_get_user_id();
  41. $coursePath = api_get_path(SYS_COURSE_PATH).$cidReq.'/document';
  42. $_course = CourseManager::get_course_information($cidReq);
  43. if ($_course == null) die ("problem when fetching course information");
  44. // stupid variable initialisation for old version of DocumentManager functions.
  45. $_course['path'] = $_course['directory'];
  46. $_course['dbName'] = $_course['db_name'];
  47. $is_manager = (CourseManager::get_user_in_course_status($user_id, $cidReq) == COURSEMANAGER);
  48. if ($debug>0) { error_log($coursePath, 0); }
  49. // FIXME: check security around $_REQUEST["cwd"]
  50. $cwd = $_REQUEST['cwd'];
  51. // treat /..
  52. $nParent = 0; // the number of /.. into the url
  53. while (substr($cwd, -3, 3) == '/..') {
  54. // go to parent directory
  55. $cwd= substr($cwd, 0, -3);
  56. if (strlen($cwd) == 0) { $cwd='/'; }
  57. $nParent++;
  58. }
  59. for (;$nParent >0; $nParent--) {
  60. $cwd = (strrpos($cwd,'/')>-1 ? substr($cwd, 0, strrpos($cwd,'/')) : $cwd);
  61. }
  62. if (strlen($cwd) == 0) { $cwd='/'; }
  63. if (Security::check_abs_path($cwd,api_get_path(SYS_PATH))) {
  64. die();
  65. }
  66. if ($action == 'list') {
  67. /*==== List files ====*/
  68. if ($debug>0) { error_log("sending file list",0); }
  69. // get files list
  70. $files = DocumentManager::get_all_document_data($_course, $cwd, 0, NULL, false);
  71. // adding download link to files
  72. foreach ($files as $k=>$f) {
  73. if ($f['filetype'] == 'file') {
  74. //$files[$k]['download'] = api_get_path(WEB_CODE_PATH)."/document/document.php?cidReq=$cidReq&action=download&id=".urlencode($f['path']);
  75. $files[$k]['download'] = api_get_path(WEB_COURSE_PATH).$cidReq."/document".$f['path'];
  76. }
  77. print json_encode($files);
  78. exit;
  79. }
  80. }