upload.document.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Process part of the document sub-process for upload. This script MUST BE included by upload/index.php
  5. * as it prepares most of the variables needed here.
  6. *
  7. * @todo check if this file is deprecated ... jmontoya
  8. *
  9. * @package chamilo.upload
  10. *
  11. * @author Yannick Warnier <ywarnier@beeznest.org>
  12. */
  13. $_course = api_get_course_info();
  14. $courseDir = $_course['path']."/document";
  15. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  16. $base_work_dir = $sys_course_path.$courseDir;
  17. $max_filled_space = DocumentManager::get_course_quota();
  18. //what's the current path?
  19. if (isset($_POST['curdirpath'])) {
  20. $path = Security::remove_XSS($_POST['curdirpath']);
  21. } else {
  22. $path = '/';
  23. }
  24. // Check the path
  25. // If the path is not found (no document id), set the path to /
  26. if (!DocumentManager::get_document_id($_course, $path)) {
  27. $path = '/';
  28. }
  29. /**
  30. * Header.
  31. */
  32. $nameTools = get_lang('Upload documents');
  33. $interbreadcrumb[] = [
  34. "url" => api_get_path(WEB_CODE_PATH)."document/document.php?curdirpath=".urlencode($path).'&'.api_get_cidreq(),
  35. "name" => get_lang('Documents'),
  36. ];
  37. Display::display_header($nameTools, "Doc");
  38. //show the title
  39. api_display_tool_title($nameTools.$add_group_to_title);
  40. /**
  41. * Process.
  42. */
  43. //user has submitted a file
  44. if (isset($_FILES['user_upload'])) {
  45. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  46. if ($upload_ok) {
  47. //file got on the server without problems, now process it
  48. $new_path = handle_uploaded_document(
  49. $_course,
  50. $_FILES['user_upload'],
  51. $base_work_dir,
  52. $_POST['curdirpath'],
  53. api_get_user_id(),
  54. api_get_group_id(),
  55. $to_user_id,
  56. $_POST['unzip'],
  57. $_POST['if_exists']
  58. );
  59. $new_comment = isset($_POST['comment']) ? Database::escape_string(trim($_POST['comment'])) : '';
  60. $new_title = isset($_POST['title']) ? Database::escape_string(trim($_POST['title'])) : '';
  61. if ($new_path && ($new_comment || $new_title)) {
  62. if (($docid = DocumentManager::get_document_id($_course, $new_path))) {
  63. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  64. $ct = '';
  65. if ($new_comment) {
  66. $ct .= ", comment='$new_comment'";
  67. }
  68. if ($new_title) {
  69. $ct .= ", title='$new_title'";
  70. }
  71. Database::query("UPDATE $table_document SET".substr($ct, 1)." WHERE id = '$docid'");
  72. }
  73. }
  74. //check for missing images in html files
  75. $missing_files = check_for_missing_files($base_work_dir.$_POST['curdirpath'].$new_path);
  76. if ($missing_files) {
  77. //show a form to upload the missing files
  78. echo Display::return_message(
  79. build_missing_files_form(
  80. $missing_files,
  81. $_POST['curdirpath'],
  82. $_FILES['user_upload']['name']
  83. )
  84. );
  85. }
  86. }
  87. }
  88. //missing images are submitted
  89. if (isset($_POST['submit_image'])) {
  90. $number_of_uploaded_images = count($_FILES['img_file']['name']);
  91. //if images are uploaded
  92. if ($number_of_uploaded_images > 0) {
  93. //we could also create a function for this, I'm not sure...
  94. //create a directory for the missing files
  95. $img_directory = str_replace('.', '_', $_POST['related_file']."_files");
  96. $folderData = create_unexisting_directory(
  97. $_course,
  98. api_get_user_id(),
  99. api_get_session_id(),
  100. api_get_group_id(),
  101. $to_user_id,
  102. $base_work_dir,
  103. $img_directory
  104. );
  105. $missing_files_dir = $folderData['path'];
  106. //put the uploaded files in the new directory and get the paths
  107. $paths_to_replace_in_file = move_uploaded_file_collection_into_directory(
  108. $_course,
  109. $_FILES['img_file'],
  110. $base_work_dir,
  111. $missing_files_dir,
  112. $_user['user_id'],
  113. $to_group_id,
  114. $to_user_id,
  115. $max_filled_space
  116. );
  117. //open the html file and replace the paths
  118. replace_img_path_in_html_file(
  119. $_POST['img_file_path'],
  120. $paths_to_replace_in_file,
  121. $base_work_dir.$_POST['related_file']
  122. );
  123. //update parent folders
  124. item_property_update_on_folder($_course, $_POST['curdirpath'], $_user['user_id']);
  125. }
  126. }
  127. //they want to create a directory
  128. if (isset($_POST['create_dir']) && $_POST['dirname'] != '') {
  129. $added_slash = $path == '/' ? '' : '/';
  130. $dir_name = $path.$added_slash.api_replace_dangerous_char($_POST['dirname']);
  131. $created_dir = create_unexisting_directory(
  132. $_course,
  133. api_get_user_id(),
  134. api_get_session_id(),
  135. api_get_group_id(),
  136. $to_user_id,
  137. $base_work_dir,
  138. $dir_name,
  139. $_POST['dirname']
  140. );
  141. if ($created_dir) {
  142. echo Display::return_message(get_lang('Folder created'));
  143. $path = $created_dir;
  144. } else {
  145. echo Display::return_message(get_lang('Unable to create the folder.'));
  146. }
  147. }
  148. if (isset($_GET['createdir'])) {
  149. //create the form that asks for the directory name
  150. $new_folder_text = '<form action="'.api_get_self().'" method="POST">';
  151. $new_folder_text .= '<input type="hidden" name="curdirpath" value="'.$path.'"/>';
  152. $new_folder_text .= get_lang('Name of the new folder').' ';
  153. $new_folder_text .= '<input type="text" name="dirname"/>';
  154. $new_folder_text .= '<input type="submit" name="create_dir" value="'.get_lang('Validate').'"/>';
  155. $new_folder_text .= '</form>';
  156. //show the form
  157. echo Display::return_message($new_folder_text, 'normal');
  158. } else {
  159. //give them a link to create a directory?>
  160. <p>
  161. <a href="<?php echo api_get_self(); ?>?path=<?php echo $path; ?>&amp;createdir=1">
  162. <?php echo Display::return_icon('new_folder.gif'); ?>
  163. <?php echo get_lang('Create folder'); ?>
  164. </a>
  165. </p>
  166. <?php
  167. }
  168. ?>
  169. <div id="folderselector">
  170. </div>
  171. <!-- start upload form -->
  172. <form action="<?php echo api_get_self(); ?>" method="POST" name="upload" enctype="multipart/form-data">
  173. <!-- <input type="hidden" name="MAX_FILE_SIZE" value="5400"> -->
  174. <input type="hidden" name="curdirpath" value="<?php echo $path; ?>">
  175. <table>
  176. <tr>
  177. <td valign="top">
  178. <?php echo get_lang('File'); ?>
  179. </td>
  180. <td>
  181. <input type="file" name="user_upload"/>
  182. </td>
  183. </tr>
  184. <tr>
  185. <td><?php echo get_lang('Title'); ?></td>
  186. <td><input type="text" size="20" name="title" style="width:300px;"></td>
  187. </tr>
  188. <tr>
  189. <td valign="top"><?php echo get_lang('Comment'); ?></td>
  190. <td><textarea rows="3" cols="20" name="comment" wrap="virtual" style="width:300px;"></textarea></td>
  191. </tr>
  192. <tr>
  193. <td valign="top">
  194. <?php echo get_lang('Options'); ?>
  195. </td>
  196. <td>
  197. - <input type="checkbox" name="unzip" value="1" onclick="check_unzip()"/> <?php echo get_lang('Uncompress zip'); ?><br/>
  198. - <?php echo get_lang('If file exists:'); ?><br/>
  199. &nbsp;&nbsp;&nbsp;<input type="radio" name="if_exists" value="nothing" title="<?php echo get_lang('Don\'t upload if file exists'); ?>" checked="checked"/> <?php echo get_lang('Do nothing'); ?><br/>
  200. &nbsp;&nbsp;&nbsp;<input type="radio" name="if_exists" value="overwrite" title="<?php echo get_lang('Overwrite the existing file'); ?>"/> <?php echo get_lang('Overwrite'); ?><br/>
  201. &nbsp;&nbsp;&nbsp;<input type="radio" name="if_exists" value="rename" title="<?php echo get_lang('Rename the uploaded file if it exists'); ?>"/> <?php echo get_lang('Rename'); ?>
  202. </td>
  203. </tr>
  204. </table>
  205. <input type="submit" value="<?php echo get_lang('Validate'); ?>">
  206. </form>
  207. <!-- end upload form -->
  208. <!-- so they can get back to the documents -->
  209. <p><?php echo get_lang('Back'); ?> <?php echo get_lang('To'); ?> <a href="document.php?curdirpath=<?php echo $path; ?>"><?php echo get_lang('Documents overview'); ?></a></p>
  210. <?php
  211. Display::display_footer();