remote.php 2.5 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. *
  6. * @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...)
  7. *
  8. * @package chamilo.document
  9. */
  10. /**
  11. * Script that allows remote download of a file.
  12. *
  13. * @param string Action parameter (action=...)
  14. * @param string Course code (cidReq=...)
  15. * @param string Current working directory (cwd=...)
  16. *
  17. * @return string JSON output
  18. */
  19. /* FIX for IE cache when using https */
  20. session_cache_limiter('none');
  21. require_once __DIR__.'/../inc/global.inc.php';
  22. api_protect_course_script(true);
  23. api_block_anonymous_users();
  24. /*==== Variables initialisation ====*/
  25. $action = $_REQUEST['action']; //safe as only used in if()'s
  26. $seek = ['/', '%2F', '..'];
  27. $destroy = ['', '', ''];
  28. $cidReq = str_replace($seek, $destroy, $_REQUEST["cidReq"]);
  29. $cidReq = Security::remove_XSS($cidReq);
  30. $user_id = api_get_user_id();
  31. $coursePath = api_get_path(SYS_COURSE_PATH).$cidReq.'/document';
  32. $_course = api_get_course_info($cidReq);
  33. if (empty($_course)) {
  34. die("problem when fetching course information");
  35. }
  36. // stupid variable initialisation for old version of DocumentManager functions.
  37. $_course['path'] = $_course['directory'];
  38. $is_manager = (CourseManager::getUserInCourseStatus($user_id, $_course['real_id']) == COURSEMANAGER);
  39. if ($debug > 0) {
  40. error_log($coursePath, 0);
  41. }
  42. // FIXME: check security around $_REQUEST["cwd"]
  43. $cwd = $_REQUEST['cwd'];
  44. // treat /..
  45. $nParent = 0; // the number of /.. into the url
  46. while (substr($cwd, -3, 3) == '/..') {
  47. // go to parent directory
  48. $cwd = substr($cwd, 0, -3);
  49. if (strlen($cwd) == 0) {
  50. $cwd = '/';
  51. }
  52. $nParent++;
  53. }
  54. for (; $nParent > 0; $nParent--) {
  55. $cwd = (strrpos($cwd, '/') > -1 ? substr($cwd, 0, strrpos($cwd, '/')) : $cwd);
  56. }
  57. if (strlen($cwd) == 0) {
  58. $cwd = '/';
  59. }
  60. if (Security::check_abs_path($cwd, api_get_path(SYS_PATH))) {
  61. die();
  62. }
  63. if ($action == 'list') {
  64. /*==== List files ====*/
  65. if ($debug > 0) {
  66. error_log("sending file list", 0);
  67. }
  68. // get files list
  69. $files = DocumentManager::getAllDocumentData($_course, $cwd, 0, null, false);
  70. // adding download link to files
  71. foreach ($files as $k => $f) {
  72. if ($f['filetype'] == 'file') {
  73. $files[$k]['download'] = api_get_path(WEB_COURSE_PATH).$cidReq."/document".$f['path'];
  74. }
  75. echo json_encode($files);
  76. exit;
  77. }
  78. }