lp_upload.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 __DIR__.'/../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. Display::addFlash(
  29. Display::return_message(get_lang('UplFileTooBig'))
  30. );
  31. return false;
  32. unset($_FILES['user_file']);
  33. } elseif ($_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));
  44. $type = learnpath::get_package_type(
  45. $_FILES['user_file']['tmp_name'],
  46. $_FILES['user_file']['name']
  47. );
  48. $proximity = 'local';
  49. if (!empty($_REQUEST['content_proximity'])) {
  50. $proximity = Database::escape_string($_REQUEST['content_proximity']);
  51. }
  52. $maker = 'Scorm';
  53. if (!empty($_REQUEST['content_maker'])) {
  54. $maker = Database::escape_string($_REQUEST['content_maker']);
  55. }
  56. switch ($type) {
  57. case 'scorm':
  58. $oScorm = new scorm();
  59. $manifest = $oScorm->import_package($_FILES['user_file'], $current_dir);
  60. if (!empty($manifest)) {
  61. $oScorm->parse_manifest($manifest);
  62. $fixTemplate = api_get_configuration_value('learnpath_fix_xerte_template');
  63. $proxyPath = api_get_configuration_value('learnpath_proxy_url');
  64. if ($fixTemplate && !empty($proxyPath)) {
  65. // Check organisations:
  66. if (isset($oScorm->manifest['organizations'])) {
  67. foreach ($oScorm->manifest['organizations'] as $data) {
  68. if (strpos(strtolower($data), 'xerte') !== false) {
  69. // Check if template.xml exists:
  70. $templatePath = str_replace('imsmanifest.xml', 'template.xml', $manifest);
  71. if (file_exists($templatePath) && is_file($templatePath)) {
  72. $templateContent = file_get_contents($templatePath);
  73. $find = array(
  74. 'href="www.',
  75. 'href="https://',
  76. 'href="http://',
  77. 'url="www.',
  78. 'pdfs/download.php?'
  79. );
  80. $replace = array(
  81. 'href="http://www.',
  82. 'target = "_blank" href="'.$proxyPath.'?type=link&src=https://',
  83. 'target = "_blank" href="'.$proxyPath.'?type=link&src=http://',
  84. 'url="http://www.',
  85. 'pdfs/download.php&'
  86. );
  87. $templateContent = str_replace($find, $replace, $templateContent);
  88. file_put_contents($templatePath, $templateContent);
  89. }
  90. // Fix link generation:
  91. $linkPath = str_replace('imsmanifest.xml', 'models_html5/links.html', $manifest);
  92. if (file_exists($linkPath) && is_file($linkPath)) {
  93. $linkContent = file_get_contents($linkPath);
  94. $find = array(
  95. ':this.getAttribute("url")'
  96. );
  97. $replace = array(
  98. ':"'.$proxyPath.'?type=link&src=" + this.getAttribute("url")'
  99. );
  100. $linkContent = str_replace($find, $replace, $linkContent);
  101. file_put_contents($linkPath, $linkContent);
  102. }
  103. // Fix iframe generation
  104. $framePath = str_replace('imsmanifest.xml', 'models_html5/embedDiv.html', $manifest);
  105. if (file_exists($framePath) && is_file($framePath)) {
  106. $content = file_get_contents($framePath);
  107. $find = array(
  108. '$iFrameHolder.html(iFrameTag);'
  109. );
  110. $replace = array(
  111. '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); '
  112. );
  113. $content = str_replace($find, $replace, $content);
  114. file_put_contents($framePath, $content);
  115. }
  116. // Fix new window generation
  117. $newWindowPath = str_replace('imsmanifest.xml', 'models_html5/newWindow.html', $manifest);
  118. if (file_exists($newWindowPath) && is_file($newWindowPath)) {
  119. $content = file_get_contents($newWindowPath);
  120. $find = array(
  121. 'var src = x_currentPageXML'
  122. );
  123. $replace = array(
  124. 'var src = "'.$proxyPath.'?type=link&src=" + x_currentPageXML'
  125. );
  126. $content = str_replace($find, $replace, $content);
  127. file_put_contents($newWindowPath, $content);
  128. }
  129. }
  130. }
  131. }
  132. }
  133. $oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
  134. Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
  135. }
  136. $oScorm->set_proximity($proximity);
  137. $oScorm->set_maker($maker);
  138. $oScorm->set_jslib('scorm_api.php');
  139. break;
  140. case 'aicc':
  141. $oAICC = new aicc();
  142. $config_dir = $oAICC->import_package($_FILES['user_file']);
  143. if (!empty($config_dir)) {
  144. $oAICC->parse_config_files($config_dir);
  145. $oAICC->import_aicc(api_get_course_id());
  146. Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
  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'], 'make_lp', $_POST['slide_size']);
  157. Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
  158. break;
  159. case 'woogie':
  160. require_once 'openoffice_text.class.php';
  161. $split_steps = (empty($_POST['split_steps']) || $_POST['split_steps'] == 'per_page') ? 'per_page' : 'per_chapter';
  162. $o_doc = new OpenofficeText($split_steps);
  163. $first_item_id = $o_doc->convert_document($_FILES['user_file']);
  164. Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
  165. break;
  166. case '':
  167. default:
  168. Display::addFlash(Display::return_message(get_lang('ScormUnknownPackageFormat'), 'warning'));
  169. return false;
  170. break;
  171. }
  172. } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
  173. // end if is_uploaded_file
  174. // If file name given to get in /upload/, try importing this way.
  175. // A file upload has been detected, now deal with the file...
  176. // Directory creation.
  177. $stopping_error = false;
  178. if (!isset($_POST['file_name'])) {
  179. return false;
  180. }
  181. // Escape path with basename so it can only be directly into the archive/ directory.
  182. $s = api_get_path(SYS_ARCHIVE_PATH).basename($_POST['file_name']);
  183. // Get name of the zip file without the extension
  184. $info = pathinfo($s);
  185. $filename = $info['basename'];
  186. $extension = $info['extension'];
  187. $file_base_name = str_replace('.'.$extension, '', $filename);
  188. $new_dir = api_replace_dangerous_char(trim($file_base_name));
  189. $result = learnpath::verify_document_size($s);
  190. if ($result == true) {
  191. Display::addFlash(
  192. Display::return_message(get_lang('UplFileTooBig'))
  193. );
  194. }
  195. $type = learnpath::get_package_type($s, basename($s));
  196. switch ($type) {
  197. case 'scorm':
  198. $oScorm = new scorm();
  199. $manifest = $oScorm->import_local_package($s, $current_dir);
  200. if (!empty($manifest)) {
  201. $oScorm->parse_manifest($manifest);
  202. $oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
  203. Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
  204. }
  205. $proximity = '';
  206. if (!empty($_REQUEST['content_proximity'])) {
  207. $proximity = Database::escape_string($_REQUEST['content_proximity']);
  208. }
  209. $maker = '';
  210. if (!empty($_REQUEST['content_maker'])) {
  211. $maker = Database::escape_string($_REQUEST['content_maker']);
  212. }
  213. $oScorm->set_proximity($proximity);
  214. $oScorm->set_maker($maker);
  215. $oScorm->set_jslib('scorm_api.php');
  216. break;
  217. case 'aicc':
  218. $oAICC = new aicc();
  219. $config_dir = $oAICC->import_local_package($s, $current_dir);
  220. if (!empty($config_dir)) {
  221. $oAICC->parse_config_files($config_dir);
  222. $oAICC->import_aicc(api_get_course_id());
  223. Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
  224. }
  225. $proximity = '';
  226. if (!empty($_REQUEST['content_proximity'])) {
  227. $proximity = Database::escape_string($_REQUEST['content_proximity']);
  228. }
  229. $maker = '';
  230. if (!empty($_REQUEST['content_maker'])) {
  231. $maker = Database::escape_string($_REQUEST['content_maker']);
  232. }
  233. $oAICC->set_proximity($proximity);
  234. $oAICC->set_maker($maker);
  235. $oAICC->set_jslib('aicc_api.php');
  236. break;
  237. case '':
  238. default:
  239. Display::addFlash(
  240. Display::return_message(get_lang('ScormUnknownPackageFormat'), 'warning')
  241. );
  242. return false;
  243. break;
  244. }
  245. }