upload.document.php 9.1 KB

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