upload.document.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. /**
  12. * Process the document and return to the document tool
  13. */
  14. /*
  15. Libraries
  16. */
  17. //many useful functions in main_api.lib.php, by default included
  18. if (!function_exists('api_get_path')) {
  19. header('location: upload.php');
  20. die;
  21. }
  22. $courseDir = $_course['path'] . "/document";
  23. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  24. $base_work_dir = $sys_course_path . $courseDir;
  25. $noPHP_SELF = true;
  26. $max_filled_space = DocumentManager::get_course_quota();
  27. //what's the current path?
  28. if (isset($_POST['curdirpath'])) {
  29. $path = $_POST['curdirpath'];
  30. } else {
  31. $path = '/';
  32. }
  33. // Check the path
  34. // If the path is not found (no document id), set the path to /
  35. if (!DocumentManager::get_document_id($_course, $path)) {
  36. $path = '/';
  37. }
  38. /**
  39. * Header
  40. */
  41. $nameTools = get_lang('UplUploadDocument');
  42. $interbreadcrumb[] = array(
  43. "url" => "./document.php?curdirpath=" . urlencode(
  44. $path
  45. ) . $req_gid,
  46. "name" => $langDocuments
  47. );
  48. Display::display_header($nameTools, "Doc");
  49. //show the title
  50. api_display_tool_title($nameTools . $add_group_to_title);
  51. /**
  52. * Process
  53. */
  54. //user has submitted a file
  55. if (isset($_FILES['user_upload'])) {
  56. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  57. if ($upload_ok) {
  58. //file got on the server without problems, now process it
  59. $new_path = handle_uploaded_document(
  60. $_course,
  61. $_FILES['user_upload'],
  62. $base_work_dir,
  63. $_POST['curdirpath'],
  64. $_user['user_id'],
  65. $to_group_id,
  66. $to_user_id,
  67. $_POST['unzip'],
  68. $_POST['if_exists']
  69. );
  70. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  71. $new_title = isset($_POST['title']) ? trim($_POST['title']) : '';
  72. if ($new_path && ($new_comment || $new_title))
  73. if (($docid = DocumentManager::get_document_id($_course, $new_path))) {
  74. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  75. $ct = '';
  76. if ($new_comment) $ct .= ", comment='$new_comment'";
  77. if ($new_title) $ct .= ", title='$new_title'";
  78. Database::query("UPDATE $table_document SET" . substr($ct, 1) ." WHERE id = '$docid'");
  79. }
  80. //check for missing images in html files
  81. $missing_files = check_for_missing_files($base_work_dir.$_POST['curdirpath'].$new_path);
  82. if ($missing_files) {
  83. //show a form to upload the missing files
  84. Display::display_normal_message(build_missing_files_form($missing_files,$_POST['curdirpath'],$_FILES['user_upload']['name']));
  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. $_user['user_id'],
  99. api_get_session_id(),
  100. $to_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($_course, $_FILES['img_file'],$base_work_dir,$missing_files_dir,$_user['user_id'],$to_group_id,$to_user_id,$max_filled_space);
  108. //open the html file and replace the paths
  109. replace_img_path_in_html_file(
  110. $_POST['img_file_path'],
  111. $paths_to_replace_in_file,
  112. $base_work_dir . $_POST['related_file']
  113. );
  114. //update parent folders
  115. item_property_update_on_folder($_course,$_POST['curdirpath'],$_user['user_id']);
  116. }
  117. }
  118. //they want to create a directory
  119. if (isset($_POST['create_dir']) && $_POST['dirname']!='') {
  120. $added_slash = ($path=='/')?'':'/';
  121. $dir_name = $path.$added_slash.api_replace_dangerous_char($_POST['dirname']);
  122. $created_dir = create_unexisting_directory($_course,$_user['user_id'],api_get_session_id(), $to_group_id,$to_user_id,$base_work_dir,$dir_name,$_POST['dirname']);
  123. if ($created_dir) {
  124. Display::display_normal_message(get_lang('DirCr'));
  125. $path = $created_dir;
  126. } else {
  127. display_error(get_lang('CannotCreateDir'));
  128. }
  129. }
  130. if (isset($_GET['createdir'])) {
  131. //create the form that asks for the directory name
  132. $new_folder_text = '<form action="'.api_get_self().'" method="POST">';
  133. $new_folder_text .= '<input type="hidden" name="curdirpath" value="'.$path.'"/>';
  134. $new_folder_text .= get_lang('NewDir') .' ';
  135. $new_folder_text .= '<input type="text" name="dirname"/>';
  136. $new_folder_text .= '<input type="submit" name="create_dir" value="'.get_lang('Ok').'"/>';
  137. $new_folder_text .= '</form>';
  138. //show the form
  139. Display::display_normal_message($new_folder_text);
  140. } else { //give them a link to create a directory
  141. ?>
  142. <p><a href="<?php echo api_get_self(); ?>?path=<?php echo $path; ?>&amp;createdir=1"><img src="../img/new_folder.gif" border="0" align="absmiddle" alt ="" />
  143. <?php echo(get_lang('CreateDir'));?>
  144. </a>
  145. </p>
  146. <?php
  147. }
  148. ?>
  149. <div id="folderselector">
  150. </div>
  151. <!-- start upload form -->
  152. <form action="<?php echo api_get_self(); ?>" method="POST" name="upload" enctype="multipart/form-data">
  153. <!-- <input type="hidden" name="MAX_FILE_SIZE" value="5400"> -->
  154. <input type="hidden" name="curdirpath" value="<?php echo $path; ?>">
  155. <table>
  156. <tr>
  157. <td valign="top">
  158. <?php echo get_lang('File'); ?>
  159. </td>
  160. <td>
  161. <input type="file" name="user_upload"/>
  162. </td>
  163. </tr>
  164. <tr>
  165. <td><?php echo get_lang('Title');?></td>
  166. <td><input type="text" size="20" name="title" style="width:300px;"></td>
  167. </tr>
  168. <tr>
  169. <td valign="top"><?php echo get_lang('Comment');?></td>
  170. <td><textarea rows="3" cols="20" name="comment" wrap="virtual" style="width:300px;"></textarea></td>
  171. </tr>
  172. <tr>
  173. <td valign="top">
  174. <?php echo get_lang('Options'); ?>
  175. </td>
  176. <td>
  177. - <input type="checkbox" name="unzip" value="1" onclick="check_unzip()"/> <?php echo(get_lang('Uncompress'));?><br/>
  178. - <?php echo (get_lang('UplWhatIfFileExists'));?><br/>
  179. &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/>
  180. &nbsp;&nbsp;&nbsp;<input type="radio" name="if_exists" value="overwrite" title="<?php echo (get_lang('UplOverwriteLong'));?>"/> <?php echo (get_lang('UplOverwrite'));?><br/>
  181. &nbsp;&nbsp;&nbsp;<input type="radio" name="if_exists" value="rename" title="<?php echo (get_lang('UplRenameLong'));?>"/> <?php echo (get_lang('UplRename'));?>
  182. </td>
  183. </tr>
  184. </table>
  185. <input type="submit" value="<?php echo(get_lang('Ok'));?>">
  186. </form>
  187. <!-- end upload form -->
  188. <!-- so they can get back to the documents -->
  189. <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>
  190. <?php
  191. Display::display_footer();