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