lp_upload.php 7.0 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. /*
  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($_POST) ? isset($_FILES['user_file']) : array();
  28. $user_file = $user_file ? $user_file : array();
  29. $is_error = isset($user_file['error']) ? $user_file['error'] : false;
  30. if (isset($_POST) && $is_error) {
  31. return api_failure::set_failure('upload_file_too_big');
  32. unset($_FILES['user_file']);
  33. } else if ($_SERVER['REQUEST_METHOD'] == 'POST' && count($_FILES) > 0 && !empty($_FILES['user_file']['name'])) {
  34. // A file upload has been detected, now deal with the file...
  35. // Directory creation.
  36. $stopping_error = false;
  37. $s = $_FILES['user_file']['name'];
  38. // Get name of the zip file without the extension.
  39. $info = pathinfo($s);
  40. $filename = $info['basename'];
  41. $extension = $info['extension'];
  42. $file_base_name = str_replace('.'.$extension, '', $filename);
  43. $new_dir = api_replace_dangerous_char(trim($file_base_name), 'strict');
  44. require_once 'learnpath.class.php';
  45. $type = learnpath::get_package_type($_FILES['user_file']['tmp_name'], $_FILES['user_file']['name']);
  46. $proximity = 'local';
  47. if (!empty($_REQUEST['content_proximity'])) {
  48. $proximity = Database::escape_string($_REQUEST['content_proximity']);
  49. }
  50. $maker = 'Scorm';
  51. if (!empty($_REQUEST['content_maker'])) {
  52. $maker = Database::escape_string($_REQUEST['content_maker']);
  53. }
  54. switch ($type) {
  55. case 'scorm':
  56. require_once 'scorm.class.php';
  57. $oScorm = new scorm();
  58. $manifest = $oScorm->import_package($_FILES['user_file'], $current_dir);
  59. if (!$manifest) { //if api_set_failure
  60. return api_failure::set_failure(api_failure::get_last_failure());
  61. }
  62. if (!empty($manifest)) {
  63. $oScorm->parse_manifest($manifest);
  64. $oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
  65. } else {
  66. // Show error message stored in $oScrom->error_msg.
  67. }
  68. $oScorm->set_proximity($proximity);
  69. $oScorm->set_maker($maker);
  70. $oScorm->set_jslib('scorm_api.php');
  71. break;
  72. case 'aicc':
  73. require_once 'aicc.class.php';
  74. $oAICC = new aicc();
  75. $config_dir = $oAICC->import_package($_FILES['user_file']);
  76. if (!empty($config_dir)) {
  77. $oAICC->parse_config_files($config_dir);
  78. $oAICC->import_aicc(api_get_course_id());
  79. }
  80. $oAICC->set_proximity($proximity);
  81. $oAICC->set_maker($maker);
  82. $oAICC->set_jslib('aicc_api.php');
  83. break;
  84. case 'oogie':
  85. require_once 'openoffice_presentation.class.php';
  86. $take_slide_name = empty($_POST['take_slide_name']) ? false : true;
  87. $o_ppt = new OpenofficePresentation($take_slide_name);
  88. $first_item_id = $o_ppt->convert_document($_FILES['user_file']);
  89. break;
  90. case 'woogie':
  91. require_once 'openoffice_text.class.php';
  92. $split_steps = (empty($_POST['split_steps']) || $_POST['split_steps'] == 'per_page') ? 'per_page' : 'per_chapter';
  93. $o_doc = new OpenofficeText($split_steps);
  94. $first_item_id = $o_doc->convert_document($_FILES['user_file']);
  95. break;
  96. case '':
  97. default:
  98. return api_failure::set_failure('not_a_learning_path');
  99. }
  100. } elseif($_SERVER['REQUEST_METHOD'] == 'POST') {
  101. // end if is_uploaded_file
  102. // If file name given to get in claroline/upload/, try importing this way.
  103. // A file upload has been detected, now deal with the file...
  104. // Directory creation.
  105. $stopping_error = false;
  106. // Escape path with basename so it can only be directly into the claroline/upload directory.
  107. $s = api_get_path(SYS_ARCHIVE_PATH).basename($_POST['file_name']);
  108. // Get name of the zip file without the extension
  109. $info = pathinfo($s);
  110. $filename = $info['basename'];
  111. $extension = $info['extension'];
  112. $file_base_name = str_replace('.'.$extension, '', $filename);
  113. $new_dir = api_replace_dangerous_char(trim($file_base_name), 'strict');
  114. require_once 'learnpath.class.php';
  115. $result = learnpath::verify_document_size($s);
  116. if ($result == true) {
  117. return api_failure::set_failure('upload_file_too_big');
  118. }
  119. $type = learnpath::get_package_type($s, basename($s));
  120. switch ($type) {
  121. case 'scorm':
  122. require_once 'scorm.class.php';
  123. $oScorm = new scorm();
  124. $manifest = $oScorm->import_local_package($s, $current_dir);
  125. if ($manifest === false ) { //if ap i_set_failure
  126. return api_failure::set_failure(api_failure::get_last_failure());
  127. }
  128. if (!empty($manifest)) {
  129. $oScorm->parse_manifest($manifest);
  130. $oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
  131. }
  132. $proximity = '';
  133. if (!empty($_REQUEST['content_proximity'])) { $proximity = Database::escape_string($_REQUEST['content_proximity']); }
  134. $maker = '';
  135. if (!empty($_REQUEST['content_maker'])) {$maker = Database::escape_string($_REQUEST['content_maker']); }
  136. $oScorm->set_proximity($proximity);
  137. $oScorm->set_maker($maker);
  138. $oScorm->set_jslib('scorm_api.php');
  139. break;
  140. case 'aicc':
  141. require_once 'aicc.class.php';
  142. $oAICC = new aicc();
  143. $config_dir = $oAICC->import_local_package($s, $current_dir);
  144. if (!empty($config_dir)) {
  145. $oAICC->parse_config_files($config_dir);
  146. $oAICC->import_aicc(api_get_course_id());
  147. }
  148. $proximity = '';
  149. if (!empty($_REQUEST['content_proximity'])) { $proximity = Database::escape_string($_REQUEST['content_proximity']); }
  150. $maker = '';
  151. if (!empty($_REQUEST['content_maker'])) { $maker = Database::escape_string($_REQUEST['content_maker']); }
  152. $oAICC->set_proximity($proximity);
  153. $oAICC->set_maker($maker);
  154. $oAICC->set_jslib('aicc_api.php');
  155. break;
  156. case '':
  157. default:
  158. return api_failure::set_failure('not_a_learning_path');
  159. }
  160. }