upload.php 8.5 KB

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