upload.document.php 9.1 KB

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