lp_upload.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. $fixTemplate = api_get_configuration_value('learnpath_fix_xerte_template');
  62. $proxyPath = api_get_configuration_value('learnpath_proxy_url');
  63. if ($fixTemplate && !empty($proxyPath)) {
  64. // Check organisations:
  65. if (isset($oScorm->manifest['organizations'])) {
  66. foreach ($oScorm->manifest['organizations'] as $data) {
  67. if (strpos(strtolower($data), 'xerte') !== false) {
  68. // Check if template.xml exists:
  69. $templatePath = str_replace('imsmanifest.xml', 'template.xml', $manifest);
  70. if (file_exists($templatePath) && is_file($templatePath)) {
  71. $templateContent = file_get_contents($templatePath);
  72. $find = array(
  73. 'href="www.',
  74. 'href="https://',
  75. 'href="http://',
  76. 'url="www.',
  77. 'pdfs/download.php?'
  78. );
  79. $replace = array(
  80. 'href="http://www.',
  81. 'target = "_blank" href="'.$proxyPath.'?type=link&src=https://',
  82. 'target = "_blank" href="'.$proxyPath.'?type=link&src=http://',
  83. 'url="http://www.',
  84. 'pdfs/download.php&'
  85. );
  86. $templateContent = str_replace($find, $replace, $templateContent);
  87. file_put_contents($templatePath, $templateContent);
  88. }
  89. // Fix link generation:
  90. $linkPath = str_replace('imsmanifest.xml', 'models_html5/links.html', $manifest);
  91. if (file_exists($linkPath) && is_file($linkPath)) {
  92. $linkContent = file_get_contents($linkPath);
  93. $find = array(
  94. ':this.getAttribute("url")'
  95. );
  96. $replace = array(
  97. ':"'.$proxyPath.'?type=link&src=" + this.getAttribute("url")'
  98. );
  99. $linkContent = str_replace($find, $replace, $linkContent);
  100. file_put_contents($linkPath, $linkContent);
  101. }
  102. // Fix iframe generation
  103. $framePath = str_replace('imsmanifest.xml', 'models_html5/embedDiv.html', $manifest);
  104. if (file_exists($framePath) && is_file($framePath)) {
  105. $content = file_get_contents($framePath);
  106. $find = array(
  107. '$iFrameHolder.html(iFrameTag);'
  108. );
  109. $replace = array(
  110. 'iFrameTag = \'<a target ="_blank" href="'.$proxyPath.'?type=link&src=\'+ pageSrc + \'">Open website. <img src="'.api_get_path(WEB_CODE_PATH).'img/link-external.png"></a>\'; $iFrameHolder.html(iFrameTag); '
  111. );
  112. $content = str_replace($find, $replace, $content);
  113. file_put_contents($framePath, $content);
  114. }
  115. // Fix new window generation
  116. $newWindowPath = str_replace('imsmanifest.xml', 'models_html5/newWindow.html', $manifest);
  117. if (file_exists($newWindowPath) && is_file($newWindowPath)) {
  118. $content = file_get_contents($newWindowPath);
  119. $find = array(
  120. 'var src = x_currentPageXML'
  121. );
  122. $replace = array(
  123. 'var src = "'.$proxyPath.'?type=link&src=" + x_currentPageXML'
  124. );
  125. $content = str_replace($find, $replace, $content);
  126. file_put_contents($newWindowPath, $content);
  127. }
  128. }
  129. }
  130. }
  131. }
  132. $oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
  133. } else {
  134. // Show error message stored in $oScrom->error_msg.
  135. }
  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_package($_FILES['user_file']);
  144. if (!empty($config_dir)) {
  145. $oAICC->parse_config_files($config_dir);
  146. $oAICC->import_aicc(api_get_course_id());
  147. }
  148. $oAICC->set_proximity($proximity);
  149. $oAICC->set_maker($maker);
  150. $oAICC->set_jslib('aicc_api.php');
  151. break;
  152. case 'oogie':
  153. require_once 'openoffice_presentation.class.php';
  154. $take_slide_name = empty($_POST['take_slide_name']) ? false : true;
  155. $o_ppt = new OpenofficePresentation($take_slide_name);
  156. $first_item_id = $o_ppt->convert_document($_FILES['user_file']);
  157. break;
  158. case 'woogie':
  159. require_once 'openoffice_text.class.php';
  160. $split_steps = (empty($_POST['split_steps']) || $_POST['split_steps'] == 'per_page') ? 'per_page' : 'per_chapter';
  161. $o_doc = new OpenofficeText($split_steps);
  162. $first_item_id = $o_doc->convert_document($_FILES['user_file']);
  163. break;
  164. case '':
  165. default:
  166. return api_failure::set_failure('not_a_learning_path');
  167. }
  168. } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
  169. // end if is_uploaded_file
  170. // If file name given to get in /upload/, try importing this way.
  171. // A file upload has been detected, now deal with the file...
  172. // Directory creation.
  173. $stopping_error = false;
  174. if (!isset($_POST['file_name'])) {
  175. return false;
  176. }
  177. // Escape path with basename so it can only be directly into the archive/ directory.
  178. $s = api_get_path(SYS_ARCHIVE_PATH).basename($_POST['file_name']);
  179. // Get name of the zip file without the extension
  180. $info = pathinfo($s);
  181. $filename = $info['basename'];
  182. $extension = $info['extension'];
  183. $file_base_name = str_replace('.'.$extension, '', $filename);
  184. $new_dir = api_replace_dangerous_char(trim($file_base_name));
  185. $result = learnpath::verify_document_size($s);
  186. if ($result == true) {
  187. return api_failure::set_failure('upload_file_too_big');
  188. }
  189. $type = learnpath::get_package_type($s, basename($s));
  190. switch ($type) {
  191. case 'scorm':
  192. require_once 'scorm.class.php';
  193. $oScorm = new scorm();
  194. $manifest = $oScorm->import_local_package($s, $current_dir);
  195. if ($manifest === false) { //if ap i_set_failure
  196. return api_failure::set_failure(api_failure::get_last_failure());
  197. }
  198. if (!empty($manifest)) {
  199. $oScorm->parse_manifest($manifest);
  200. $oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
  201. }
  202. $proximity = '';
  203. if (!empty($_REQUEST['content_proximity'])) {
  204. $proximity = Database::escape_string($_REQUEST['content_proximity']);
  205. }
  206. $maker = '';
  207. if (!empty($_REQUEST['content_maker'])) {
  208. $maker = Database::escape_string($_REQUEST['content_maker']);
  209. }
  210. $oScorm->set_proximity($proximity);
  211. $oScorm->set_maker($maker);
  212. $oScorm->set_jslib('scorm_api.php');
  213. break;
  214. case 'aicc':
  215. require_once 'aicc.class.php';
  216. $oAICC = new aicc();
  217. $config_dir = $oAICC->import_local_package($s, $current_dir);
  218. if (!empty($config_dir)) {
  219. $oAICC->parse_config_files($config_dir);
  220. $oAICC->import_aicc(api_get_course_id());
  221. }
  222. $proximity = '';
  223. if (!empty($_REQUEST['content_proximity'])) {
  224. $proximity = Database::escape_string($_REQUEST['content_proximity']);
  225. }
  226. $maker = '';
  227. if (!empty($_REQUEST['content_maker'])) {
  228. $maker = Database::escape_string($_REQUEST['content_maker']);
  229. }
  230. $oAICC->set_proximity($proximity);
  231. $oAICC->set_maker($maker);
  232. $oAICC->set_jslib('aicc_api.php');
  233. break;
  234. case '':
  235. default:
  236. return api_failure::set_failure('not_a_learning_path');
  237. }
  238. }