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