lp_upload.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Component\CourseCopy\CourseArchiver;
  4. use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
  5. /**
  6. * Script managing the learnpath upload. To best treat the uploaded file, make sure we can identify it.
  7. *
  8. * @package chamilo.learnpath
  9. *
  10. * @author Yannick Warnier <ywarnier@beeznest.org>
  11. */
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. api_protect_course_script();
  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']));
  20. }
  21. $uncompress = 1;
  22. /*
  23. * Check the request method in place of a variable from POST
  24. * because if the file size exceed the maximum file upload
  25. * size set in php.ini, all variables from POST are cleared !
  26. */
  27. $user_file = isset($_GET['user_file']) ? $_GET['user_file'] : [];
  28. $user_file = $user_file ? $user_file : [];
  29. $is_error = isset($user_file['error']) ? $user_file['error'] : false;
  30. if (isset($_POST) && $is_error) {
  31. Display::addFlash(
  32. Display::return_message(get_lang('UplFileTooBig'))
  33. );
  34. return false;
  35. unset($_FILES['user_file']);
  36. } elseif ($_SERVER['REQUEST_METHOD'] == 'POST' && count($_FILES) > 0 && !empty($_FILES['user_file']['name'])) {
  37. // A file upload has been detected, now deal with the file...
  38. // Directory creation.
  39. $stopping_error = false;
  40. $s = $_FILES['user_file']['name'];
  41. // Get name of the zip file without the extension.
  42. $info = pathinfo($s);
  43. $filename = $info['basename'];
  44. $extension = $info['extension'];
  45. $file_base_name = str_replace('.'.$extension, '', $filename);
  46. $new_dir = api_replace_dangerous_char(trim($file_base_name));
  47. $type = learnpath::get_package_type(
  48. $_FILES['user_file']['tmp_name'],
  49. $_FILES['user_file']['name']
  50. );
  51. $proximity = 'local';
  52. if (!empty($_REQUEST['content_proximity'])) {
  53. $proximity = Database::escape_string($_REQUEST['content_proximity']);
  54. }
  55. $maker = 'Scorm';
  56. if (!empty($_REQUEST['content_maker'])) {
  57. $maker = Database::escape_string($_REQUEST['content_maker']);
  58. }
  59. switch ($type) {
  60. case 'chamilo':
  61. $filename = CourseArchiver::importUploadedFile(
  62. $_FILES['user_file']['tmp_name']
  63. );
  64. if ($filename) {
  65. $course = CourseArchiver::readCourse($filename, false);
  66. $courseRestorer = new CourseRestorer($course);
  67. // FILE_SKIP, FILE_RENAME or FILE_OVERWRITE
  68. $courseRestorer->set_file_option(FILE_OVERWRITE);
  69. $courseRestorer->restore();
  70. Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
  71. }
  72. break;
  73. case 'scorm':
  74. $oScorm = new scorm();
  75. $manifest = $oScorm->import_package($_FILES['user_file'], $current_dir);
  76. if (!empty($manifest)) {
  77. $oScorm->parse_manifest($manifest);
  78. $oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
  79. Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
  80. }
  81. $oScorm->set_proximity($proximity);
  82. $oScorm->set_maker($maker);
  83. $oScorm->set_jslib('scorm_api.php');
  84. break;
  85. case 'aicc':
  86. $oAICC = new aicc();
  87. $config_dir = $oAICC->import_package($_FILES['user_file']);
  88. if (!empty($config_dir)) {
  89. $oAICC->parse_config_files($config_dir);
  90. $oAICC->import_aicc(api_get_course_id());
  91. Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
  92. }
  93. $oAICC->set_proximity($proximity);
  94. $oAICC->set_maker($maker);
  95. $oAICC->set_jslib('aicc_api.php');
  96. break;
  97. case 'oogie':
  98. require_once 'openoffice_presentation.class.php';
  99. $take_slide_name = empty($_POST['take_slide_name']) ? false : true;
  100. $o_ppt = new OpenofficePresentation($take_slide_name);
  101. $first_item_id = $o_ppt->convert_document($_FILES['user_file'], 'make_lp', $_POST['slide_size']);
  102. Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
  103. break;
  104. case 'woogie':
  105. require_once 'openoffice_text.class.php';
  106. $split_steps = (empty($_POST['split_steps']) || $_POST['split_steps'] == 'per_page') ? 'per_page' : 'per_chapter';
  107. $o_doc = new OpenofficeText($split_steps);
  108. $first_item_id = $o_doc->convert_document($_FILES['user_file']);
  109. Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
  110. break;
  111. case '':
  112. default:
  113. Display::addFlash(Display::return_message(get_lang('ScormUnknownPackageFormat'), 'warning'));
  114. return false;
  115. break;
  116. }
  117. } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
  118. // end if is_uploaded_file
  119. // If file name given to get in /upload/, try importing this way.
  120. // A file upload has been detected, now deal with the file...
  121. // Directory creation.
  122. $stopping_error = false;
  123. if (!isset($_POST['file_name'])) {
  124. return false;
  125. }
  126. // Escape path with basename so it can only be directly into the archive/ directory.
  127. $s = api_get_path(SYS_ARCHIVE_PATH).basename($_POST['file_name']);
  128. // Get name of the zip file without the extension
  129. $info = pathinfo($s);
  130. $filename = $info['basename'];
  131. $extension = $info['extension'];
  132. $file_base_name = str_replace('.'.$extension, '', $filename);
  133. $new_dir = api_replace_dangerous_char(trim($file_base_name));
  134. $result = learnpath::verify_document_size($s);
  135. if ($result == true) {
  136. Display::addFlash(
  137. Display::return_message(get_lang('UplFileTooBig'))
  138. );
  139. }
  140. $type = learnpath::get_package_type($s, basename($s));
  141. switch ($type) {
  142. case 'scorm':
  143. $oScorm = new scorm();
  144. $manifest = $oScorm->import_local_package($s, $current_dir);
  145. if (!empty($manifest)) {
  146. $oScorm->parse_manifest($manifest);
  147. $oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
  148. Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
  149. }
  150. $proximity = '';
  151. if (!empty($_REQUEST['content_proximity'])) {
  152. $proximity = Database::escape_string($_REQUEST['content_proximity']);
  153. }
  154. $maker = '';
  155. if (!empty($_REQUEST['content_maker'])) {
  156. $maker = Database::escape_string($_REQUEST['content_maker']);
  157. }
  158. $oScorm->set_proximity($proximity);
  159. $oScorm->set_maker($maker);
  160. $oScorm->set_jslib('scorm_api.php');
  161. break;
  162. case 'aicc':
  163. $oAICC = new aicc();
  164. $config_dir = $oAICC->import_local_package($s, $current_dir);
  165. if (!empty($config_dir)) {
  166. $oAICC->parse_config_files($config_dir);
  167. $oAICC->import_aicc(api_get_course_id());
  168. Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
  169. }
  170. $proximity = '';
  171. if (!empty($_REQUEST['content_proximity'])) {
  172. $proximity = Database::escape_string($_REQUEST['content_proximity']);
  173. }
  174. $maker = '';
  175. if (!empty($_REQUEST['content_maker'])) {
  176. $maker = Database::escape_string($_REQUEST['content_maker']);
  177. }
  178. $oAICC->set_proximity($proximity);
  179. $oAICC->set_maker($maker);
  180. $oAICC->set_jslib('aicc_api.php');
  181. break;
  182. case '':
  183. default:
  184. Display::addFlash(
  185. Display::return_message(get_lang('ScormUnknownPackageFormat'), 'warning')
  186. );
  187. return false;
  188. break;
  189. }
  190. }