lp_upload.php 8.3 KB

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