upload.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once '../../inc/global.inc.php';
  4. require_once '../../work/work.lib.php';
  5. ini_set('soap.wsdl_cache_enabled', 0);
  6. ini_set('default_socket_timeout', '1000');
  7. api_set_more_memory_and_time_limits();
  8. $courseId = api_get_course_int_id();
  9. $courseInfo = api_get_course_info();
  10. $compilatio = new Compilatio();
  11. /* if we have to upload severals documents*/
  12. if (isset($_REQUEST['type']) && $_REQUEST['type'] === 'multi') {
  13. $docs = explode('a', $_REQUEST['doc']);
  14. for ($k = 0; $k < count($docs) - 1; $k++) {
  15. $documentId = 0;
  16. if (!isset($docs[$k])) {
  17. $documentId = (int) $docs[$k];
  18. }
  19. /**
  20. * File problem in the url field that no longer have the file extension,
  21. * Compilatio's server refuse the files
  22. * we renames in the FS and the database with the file extension that is found in the title field.
  23. */
  24. compilatioUpdateWorkDocument($documentId, $courseId);
  25. $compilatioId = $compilatio->getCompilatioId($documentId, $courseId);
  26. if (empty($compilatioId)) {
  27. $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  28. $query = "SELECT * FROM $workTable WHERE id= $documentId AND c_id= $courseId";
  29. $sqlResult = Database::query($query);
  30. $doc = Database::fetch_object($sqlResult);
  31. if ($doc) {
  32. /*We load the document in compilatio through the webservice */
  33. $currentCourseRepositoryWeb = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/';
  34. $WrkUrl = $currentCourseRepositoryWeb.$doc->url;
  35. $LocalWrkUrl = $courseInfo['course_sys_path'].$doc->url;
  36. $mime = DocumentManager::file_get_mime_type($doc->title);
  37. if ($compilatio->getTransportMode() === 'wget') {
  38. /*Compilatio's server recover tjre file throught wget like this:
  39. username:password@http://somedomain.com/reg/remotefilename.tar.gz */
  40. if (strlen($compilatio->getWgetUri()) > 2) {
  41. $filename = preg_replace(
  42. '/$',
  43. '',
  44. $compilatio->getWgetUri()
  45. ).'/'.$courseInfo['path'].'/'.$doc->url;
  46. } else {
  47. $filename = $WrkUrl;
  48. }
  49. if (strlen($compilatio->getWgetLogin()) > 2) {
  50. $filename = $compilatio->getWgetLogin().':'.$compilatio->getWgetPassword().'@'.$filename;
  51. }
  52. $mime = 'text/plain';
  53. $compilatioId = $compilatio->sendDoc($doc->title, '', $filename, $mime, 'get_url');
  54. } else {
  55. /* we use strictly the SOAP for the data trasmission */
  56. $pieces = explode('/', $doc->url);
  57. $nbPieces = count($pieces);
  58. $filename = $pieces[$nbPieces - 1];
  59. $compilatioId = $compilatio->sendDoc(
  60. $doc->title,
  61. '',
  62. $filename,
  63. $mime,
  64. file_get_contents($LocalWrkUrl)
  65. );
  66. }
  67. /*we associate in the database the document chamilo to the document compilatio*/
  68. /*we verify that the docmuent's id is an hash_md5*/
  69. if (Compilatio::isMd5($compilatioId)) {
  70. $compilatio->saveDocument($courseId, $doc->id, $compilatioId);
  71. $soapRes = $compilatio->startAnalyse($compilatioId);
  72. }
  73. }
  74. }
  75. }
  76. } else {
  77. $documentId = isset($_GET['doc']) ? $_GET['doc'] : 0;
  78. sendDocument($documentId, $courseInfo);
  79. }
  80. function sendDocument($documentId, $courseInfo)
  81. {
  82. $courseId = $courseInfo['real_id'];
  83. compilatioUpdateWorkDocument($documentId, $courseId);
  84. $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  85. $query = "SELECT * FROM $workTable
  86. WHERE id = $documentId AND c_id= $courseId";
  87. $sqlResult = Database::query($query);
  88. $doc = Database::fetch_object($sqlResult);
  89. $currentCourseRepositoryWeb = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/';
  90. $documentUrl = $currentCourseRepositoryWeb.$doc->url;
  91. $filePath = $courseInfo['course_sys_path'].$doc->url;
  92. $mime = DocumentManager::file_get_mime_type($doc->title);
  93. $compilatio = new Compilatio();
  94. if ($compilatio->getTransportMode() === 'wget') {
  95. if (strlen($compilatio->getWgetUri()) > 2) {
  96. $filename = preg_replace('/$', '', $compilatio->getWgetUri()).'/'.$courseInfo['path'].'/'.$doc->title;
  97. } else {
  98. $filename = $documentUrl;
  99. }
  100. if (strlen($compilatio->getWgetLogin()) > 2) {
  101. $filename = $compilatio->getWgetLogin().':'.$compilatio->getWgetPassword().'@'.$filename;
  102. }
  103. $compilatioId = $compilatio->sendDoc($doc->title, '', $filename, 'text/plain', 'get_url');
  104. } else {
  105. $pieces = explode('/', $doc->url);
  106. $nbPieces = count($pieces);
  107. $filename = $pieces[$nbPieces - 1];
  108. $compilatioId = $compilatio->sendDoc($doc->title, '', $filename, $mime, file_get_contents($filePath));
  109. }
  110. if (Compilatio::isMd5($compilatioId)) {
  111. $compilatio->saveDocument($courseId, $doc->id, $compilatioId);
  112. $compilatio->startAnalyse($compilatioId);
  113. echo Display::return_message(get_lang('Uploaded'));
  114. } else {
  115. echo Display::return_message(get_lang('Error'), 'error');
  116. }
  117. }
  118. /**
  119. * function for show and recovery the extension from a file.
  120. *
  121. * @param $docId
  122. * @param $courseId
  123. *
  124. * @return string
  125. */
  126. function workDocExtension($docId, $courseId)
  127. {
  128. $dbTitle = getWorkTitle($docId, $courseId);
  129. $res = getFileExtension($dbTitle);
  130. return $res;
  131. }
  132. function getFileExtension($filename)
  133. {
  134. $res = '';
  135. preg_match("/.*\.([^.]+)/", $filename, $dbTitle);
  136. if (count($dbTitle) > 1) {
  137. $res = $dbTitle[1];
  138. }
  139. return $res;
  140. }
  141. function getWorkTitle($docId, $courseId)
  142. {
  143. $docId = (int) $docId;
  144. $courseId = (int) $courseId;
  145. $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  146. $sql = "SELECT title FROM $workTable
  147. WHERE c_id= $courseId AND id = $docId";
  148. $res = Database::query($sql);
  149. if (Database::num_rows($res) > 0) {
  150. $data = Database::fetch_array($res);
  151. $res = $data['title'];
  152. }
  153. return $res;
  154. }
  155. function getFilename($txt)
  156. {
  157. $res = $txt;
  158. preg_match('|.*/([^/]+)|', $txt, $urlList);
  159. if (count($urlList) > 0) {
  160. $res = $urlList[1];
  161. }
  162. return $res;
  163. }
  164. function getWorkFolder($txt)
  165. {
  166. $res = '';
  167. preg_match('|(.*/)[^/]+|', $txt, $urlList);
  168. if (count($urlList) > 0) {
  169. $res = $urlList[1];
  170. }
  171. return $res;
  172. }
  173. function getShortFilename($txt)
  174. {
  175. $res = $txt;
  176. if (strlen($txt) > 10) {
  177. $res = substr($txt, 0, 10);
  178. }
  179. return $res;
  180. }
  181. function compilatioUpdateWorkDocument($docId, $courseId)
  182. {
  183. $_course = api_get_course_info();
  184. $docId = (int) $docId;
  185. $courseId = (int) $courseId;
  186. $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  187. $extensionFile = workDocExtension($docId, $courseId);
  188. $urlFile = get_work_path($docId);
  189. $filename = getFilename($urlFile);
  190. $work_folder = getWorkFolder($urlFile);
  191. $urlFile_ext = getFileExtension($urlFile);
  192. $coursePath = $_course['course_sys_path'];
  193. $workTitle = getWorkTitle($docId, $courseId);
  194. if ($extensionFile != '' && $urlFile_ext == '') {
  195. /* Rename the files in the FS whit the extension*/
  196. $shortFilename = $filename;
  197. $cleanWorkTitle = api_replace_dangerous_char($workTitle);
  198. $newestFilename = $shortFilename.'_'.$cleanWorkTitle;
  199. rename($coursePath.$urlFile, $coursePath.$work_folder.$newestFilename);
  200. /*rename the db's input with the extension*/
  201. $sql = "UPDATE $workTable SET url='".$work_folder.$newestFilename."'
  202. WHERE c_id=$courseId AND id=$docId";
  203. Database::query($sql);
  204. }
  205. }