upload.document.php 7.5 KB

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