lp_upload.php 12 KB

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