upload.document.php 7.7 KB

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