lp_upload.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script managing the learnpath upload. To best treat the uploaded file, make sure we can identify it.
  5. * @package chamilo.learnpath
  6. * @author Yannick Warnier <ywarnier@beeznest.org>
  7. */
  8. /**
  9. * Code
  10. */
  11. // Flag to allow for anonymous user - needs to be set before global.inc.php.
  12. $use_anonymous = true;
  13. require_once '../inc/global.inc.php';
  14. $course_dir = api_get_course_path().'/scorm';
  15. $course_sys_dir = api_get_path(SYS_COURSE_PATH).$course_dir;
  16. if (empty($_POST['current_dir'])) {
  17. $current_dir = '';
  18. } else {
  19. $current_dir = api_replace_dangerous_char(trim($_POST['current_dir']), 'strict');
  20. }
  21. $uncompress = 1;
  22. //error_log('New LP - lp_upload.php', 0);
  23. /*
  24. * Check the request method in place of a variable from POST
  25. * because if the file size exceed the maximum file upload
  26. * size set in php.ini, all variables from POST are cleared !
  27. */
  28. $user_file = Request::is_post() ? Request::file('user_file') : array();
  29. $user_file = $user_file ? $user_file : array();
  30. $is_error = isset($user_file['error']) ? $user_file['error'] : false;
  31. if( Request::is_post() && $is_error){
  32. return api_failure::set_failure('upload_file_too_big');
  33. unset($_FILEs['user_file']);
  34. } else if ($_SERVER['REQUEST_METHOD'] == 'POST' && count($_FILES) > 0 && !empty($_FILES['user_file']['name'])) {
  35. // A file upload has been detected, now deal with the file...
  36. // Directory creation.
  37. $stopping_error = false;
  38. $s = $_FILES['user_file']['name'];
  39. // Get name of the zip file without the extension.
  40. $info = pathinfo($s);
  41. $filename = $info['basename'];
  42. $extension = $info['extension'];
  43. $file_base_name = str_replace('.'.$extension, '', $filename);
  44. $new_dir = api_replace_dangerous_char(trim($file_base_name), 'strict');
  45. require_once 'learnpath.class.php';
  46. $type = learnpath::get_package_type($_FILES['user_file']['tmp_name'], $_FILES['user_file']['name']);
  47. $proximity = 'local';
  48. if (!empty($_REQUEST['content_proximity'])) {
  49. $proximity = Database::escape_string($_REQUEST['content_proximity']);
  50. }
  51. $maker = 'Scorm';
  52. if (!empty($_REQUEST['content_maker'])) {
  53. $maker = Database::escape_string($_REQUEST['content_maker']);
  54. }
  55. switch ($type) {
  56. case 'scorm':
  57. require_once 'scorm.class.php';
  58. $oScorm = new scorm();
  59. $manifest = $oScorm->import_package($_FILES['user_file'], $current_dir);
  60. if (!$manifest) { //if api_set_failure
  61. return api_failure::set_failure(api_failure::get_last_failure());
  62. }
  63. if (!empty($manifest)) {
  64. $oScorm->parse_manifest($manifest);
  65. $oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
  66. } else {
  67. // Show error message stored in $oScrom->error_msg.
  68. }
  69. $oScorm->set_proximity($proximity);
  70. $oScorm->set_maker($maker);
  71. $oScorm->set_jslib('scorm_api.php');
  72. break;
  73. case 'aicc':
  74. require_once 'aicc.class.php';
  75. $oAICC = new aicc();
  76. $config_dir = $oAICC->import_package($_FILES['user_file']);
  77. if (!empty($config_dir)) {
  78. $oAICC->parse_config_files($config_dir);
  79. $oAICC->import_aicc(api_get_course_id());
  80. }
  81. $oAICC->set_proximity($proximity);
  82. $oAICC->set_maker($maker);
  83. $oAICC->set_jslib('aicc_api.php');
  84. break;
  85. case 'oogie':
  86. require_once 'openoffice_presentation.class.php';
  87. $take_slide_name = empty($_POST['take_slide_name']) ? false : true;
  88. $o_ppt = new OpenofficePresentation($take_slide_name);
  89. $first_item_id = $o_ppt->convert_document($_FILES['user_file']);
  90. break;
  91. case 'woogie':
  92. require_once 'openoffice_text.class.php';
  93. $split_steps = (empty($_POST['split_steps']) || $_POST['split_steps'] == 'per_page') ? 'per_page' : 'per_chapter';
  94. $o_doc = new OpenofficeText($split_steps);
  95. $first_item_id = $o_doc->convert_document($_FILES['user_file']);
  96. break;
  97. case '':
  98. default:
  99. return api_failure::set_failure('not_a_learning_path');
  100. }
  101. } elseif($_SERVER['REQUEST_METHOD'] == 'POST') {
  102. // end if is_uploaded_file
  103. // If file name given to get in claroline/upload/, try importing this way.
  104. // A file upload has been detected, now deal with the file...
  105. // Directory creation.
  106. $stopping_error = false;
  107. // Escape path with basename so it can only be directly into the claroline/upload directory.
  108. $s = api_get_path(SYS_ARCHIVE_PATH).basename($_POST['file_name']);
  109. // Get name of the zip file without the extension
  110. $info = pathinfo($s);
  111. $filename = $info['basename'];
  112. $extension = $info['extension'];
  113. $file_base_name = str_replace('.'.$extension, '', $filename);
  114. $new_dir = api_replace_dangerous_char(trim($file_base_name), 'strict');
  115. require_once 'learnpath.class.php';
  116. $result = learnpath::verify_document_size($s);
  117. if ($result == true) {
  118. return api_failure::set_failure('upload_file_too_big');
  119. }
  120. $type = learnpath::get_package_type($s, basename($s));
  121. switch ($type) {
  122. case 'scorm':
  123. require_once 'scorm.class.php';
  124. $oScorm = new scorm();
  125. $manifest = $oScorm->import_local_package($s, $current_dir);
  126. if ($manifest === false ) { //if ap i_set_failure
  127. return api_failure::set_failure(api_failure::get_last_failure());
  128. }
  129. if (!empty($manifest)) {
  130. $oScorm->parse_manifest($manifest);
  131. $oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
  132. }
  133. $proximity = '';
  134. if (!empty($_REQUEST['content_proximity'])) { $proximity = Database::escape_string($_REQUEST['content_proximity']); }
  135. $maker = '';
  136. if (!empty($_REQUEST['content_maker'])) {$maker = Database::escape_string($_REQUEST['content_maker']); }
  137. $oScorm->set_proximity($proximity);
  138. $oScorm->set_maker($maker);
  139. $oScorm->set_jslib('scorm_api.php');
  140. break;
  141. case 'aicc':
  142. require_once 'aicc.class.php';
  143. $oAICC = new aicc();
  144. $config_dir = $oAICC->import_local_package($s, $current_dir);
  145. if (!empty($config_dir)) {
  146. $oAICC->parse_config_files($config_dir);
  147. $oAICC->import_aicc(api_get_course_id());
  148. }
  149. $proximity = '';
  150. if (!empty($_REQUEST['content_proximity'])) { $proximity = Database::escape_string($_REQUEST['content_proximity']); }
  151. $maker = '';
  152. if (!empty($_REQUEST['content_maker'])) { $maker = Database::escape_string($_REQUEST['content_maker']); }
  153. $oAICC->set_proximity($proximity);
  154. $oAICC->set_maker($maker);
  155. $oAICC->set_jslib('aicc_api.php');
  156. break;
  157. case '':
  158. default:
  159. return api_failure::set_failure('not_a_learning_path');
  160. }
  161. }