remote.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. require_once '../inc/global.inc.php';
  18. //require_once '../inc/global.inc.php';
  19. api_block_anonymous_users();
  20. /*==== Variables initialisation ====*/
  21. $action = $_REQUEST['action']; //safe as only used in if()'s
  22. $seek = array('/', '%2F', '..');
  23. $destroy = array('', '', '');
  24. $cidReq = str_replace($seek, $destroy, $_REQUEST["cidReq"]);
  25. $cidReq = Security::remove_XSS($cidReq);
  26. $user_id = api_get_user_id();
  27. $coursePath = api_get_path(SYS_COURSE_PATH).$cidReq.'/document';
  28. $_course = CourseManager::get_course_information($cidReq);
  29. if ($_course == null) {
  30. die ("problem when fetching course information");
  31. }
  32. // stupid variable initialisation for old version of DocumentManager functions.
  33. $_course['path'] = $_course['directory'];
  34. $is_manager = (CourseManager::get_user_in_course_status($user_id, $cidReq) == COURSEMANAGER);
  35. if ($debug > 0) {
  36. error_log($coursePath, 0);
  37. }
  38. // FIXME: check security around $_REQUEST["cwd"]
  39. $cwd = $_REQUEST['cwd'];
  40. // treat /..
  41. $nParent = 0; // the number of /.. into the url
  42. while (substr($cwd, -3, 3) == '/..') {
  43. // go to parent directory
  44. $cwd= substr($cwd, 0, -3);
  45. if (strlen($cwd) == 0) { $cwd='/'; }
  46. $nParent++;
  47. }
  48. for (; $nParent > 0; $nParent--) {
  49. $cwd = (strrpos($cwd, '/') > -1 ? substr($cwd, 0, strrpos($cwd, '/')) : $cwd);
  50. }
  51. if (strlen($cwd) == 0) {
  52. $cwd = '/';
  53. }
  54. if (Security::check_abs_path($cwd, api_get_path(SYS_PATH))) {
  55. die();
  56. }
  57. if ($action == 'list') {
  58. /*==== List files ====*/
  59. if ($debug>0) { error_log("sending file list",0); }
  60. // get files list
  61. $files = DocumentManager::get_all_document_data($_course, $cwd, 0, null, false);
  62. // adding download link to files
  63. foreach ($files as $k => $f) {
  64. if ($f['filetype'] == 'file') {
  65. //$files[$k]['download'] = api_get_path(WEB_CODE_PATH)."/document/document.php?cidReq=$cidReq&action=download&id=".urlencode($f['path']);
  66. $files[$k]['download'] = api_get_path(WEB_COURSE_PATH).$cidReq."/document".$f['path'];
  67. }
  68. print json_encode($files);
  69. exit;
  70. }
  71. }