record_document.php 3.4 KB

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