record_document.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. $language_file = array('document');
  4. require_once '../../../inc/global.inc.php';
  5. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  6. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  7. // Add security from Chamilo
  8. api_protect_course_script();
  9. api_block_anonymous_users();
  10. $_course = api_get_course_info();
  11. # Save the audio to a URL-accessible directory for playback.
  12. parse_str($_SERVER['QUERY_STRING'], $params);
  13. if (isset($params['waminame']) && isset($params['wamidir']) && isset($params['wamiuserid'])) {
  14. $waminame = $params['waminame'];
  15. $wamidir = $params['wamidir'];
  16. $wamiuserid = $params['wamiuserid'];
  17. } else {
  18. api_not_allowed();
  19. die();
  20. }
  21. if ($wamiuserid != api_get_user_id() || api_get_user_id() == 0 || $wamiuserid == 0) {
  22. api_not_allowed();
  23. die();
  24. }
  25. // Clean
  26. $waminame = Security::remove_XSS($waminame);
  27. $waminame = Database::escape_string($waminame);
  28. $waminame = replace_dangerous_char($waminame, 'strict');
  29. $waminame = disable_dangerous_file($waminame);
  30. $wamidir = Security::remove_XSS($wamidir);
  31. $content = file_get_contents('php://input');
  32. if (empty($content)) {
  33. exit;
  34. }
  35. $ext = explode('.', $waminame);
  36. $ext = strtolower($ext[sizeof($ext) - 1]);
  37. if ($ext != 'wav') {
  38. die();
  39. }
  40. // Do not use here check Fileinfo method because return: text/plain
  41. $dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  42. $saveDir = $dirBaseDocuments . $wamidir;
  43. if (!is_dir($saveDir)) {
  44. DocumentManager::createDefaultAudioFolder($_course);
  45. }
  46. //avoid duplicates
  47. $waminame_to_save = $waminame;
  48. $title_to_save = str_replace('_', ' ', $waminame);
  49. $waminame_noex = basename($waminame, ".wav");
  50. if (file_exists($saveDir.'/'.$waminame_noex.'.'.$ext)) {
  51. $i = 1;
  52. while (file_exists($saveDir.'/'.$waminame_noex.'_'.$i.'.'.$ext)) {
  53. $i++;
  54. }
  55. $waminame_to_save = $waminame_noex.'_'.$i.'.'.$ext;
  56. $title_to_save = $waminame_noex.'_'.$i.'.'.$ext;
  57. $title_to_save = str_replace('_', ' ', $title_to_save);
  58. }
  59. $documentPath = $saveDir.'/'.$waminame_to_save;
  60. // Add to disk
  61. $fh = fopen($documentPath, 'w') or die("can't open file");
  62. fwrite($fh, $content);
  63. fclose($fh);
  64. $fileInfo = pathinfo($documentPath);
  65. $courseInfo = api_get_course_info();
  66. $file = array(
  67. 'file' => array(
  68. 'name' => $fileInfo['basename'],
  69. 'tmp_name' => $documentPath,
  70. 'size' => filesize($documentPath),
  71. 'from_file' => true
  72. )
  73. );
  74. $output = true;
  75. ob_start();
  76. $documentData = DocumentManager::upload_document(
  77. $file,
  78. $wamidir,
  79. $fileInfo['basename'],
  80. 'wav',
  81. 0,
  82. 'overwrite',
  83. false,
  84. $output
  85. );
  86. $contents = ob_get_contents();
  87. if (!empty($documentData)) {
  88. $newDocId = $documentData['id'];
  89. $documentData['comment'] = 'mp3';
  90. $newMp3DocumentId = DocumentManager::addAndConvertWavToMp3(
  91. $documentData,
  92. $courseInfo,
  93. api_get_session_id(),
  94. api_get_user_id(),
  95. 'overwrite',
  96. true
  97. );
  98. if ($newMp3DocumentId) {
  99. $newDocId = $newMp3DocumentId;
  100. }
  101. if (isset($_REQUEST['lp_item_id']) && !empty($_REQUEST['lp_item_id'])) {
  102. $lpItemId = $_REQUEST['lp_item_id'];
  103. /** @var learnpath $lp */
  104. $lp = isset($_SESSION['oLP']) ? $_SESSION['oLP'] : null;
  105. if (!empty($lp)) {
  106. $lp->set_modified_on();
  107. $lpItem = new learnpathItem($lpItemId);
  108. $lpItem->add_audio_from_documents($newDocId);
  109. Display::addFlash(
  110. Display::return_message(get_lang('Updated'), 'info')
  111. );
  112. }
  113. }
  114. } else {
  115. Display::addFlash($contents);
  116. }