upload.document.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php //$id: $
  2. /**
  3. * Process part of the document sub-process for upload. This script MUST BE included by upload/index.php
  4. * as it prepares most of the variables needed here.
  5. * @package dokeos.upload
  6. * @author Yannick Warnier <ywarnier@beeznest.org>
  7. */
  8. /**
  9. * Process the document and return to the document tool
  10. */
  11. /*
  12. -----------------------------------------------------------
  13. Libraries
  14. -----------------------------------------------------------
  15. */
  16. //many useful functions in main_api.lib.php, by default included
  17. if(!function_exists('api_get_path')){header('location: upload.php');die;}
  18. require_once(api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php');
  19. require_once(api_get_path(LIBRARY_PATH) . 'document.lib.php');
  20. require_once('../document/document.inc.php');
  21. //REMOVE
  22. // If we want to unzip a file, we need the library
  23. if (isset($_POST['unzip']) && $_POST['unzip'] == 1)
  24. {
  25. require_once(api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
  26. }
  27. /*
  28. -----------------------------------------------------------
  29. Variables
  30. - some need defining before inclusion of libraries
  31. -----------------------------------------------------------
  32. */
  33. $courseDir = $_course['path']."/document";
  34. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  35. $base_work_dir = $sys_course_path.$courseDir;
  36. $noPHP_SELF=true;
  37. $max_filled_space = DocumentManager::get_course_quota();
  38. //what's the current path?
  39. if(isset($_POST['curdirpath'])) {
  40. $path = $_POST['curdirpath'];
  41. }else{
  42. $path = '/';
  43. }
  44. // Check the path
  45. // If the path is not found (no document id), set the path to /
  46. if(!DocumentManager::get_document_id($_course,$path)) { $path = '/'; }
  47. /**
  48. * Header
  49. */
  50. $nameTools = get_lang('UplUploadDocument');
  51. $interbreadcrumb[]=array("url"=>"./document.php?curdirpath=".urlencode($path).$req_gid, "name"=> $langDocuments);
  52. Display::display_header($nameTools,"Doc");
  53. //show the title
  54. api_display_tool_title($nameTools.$add_group_to_title);
  55. /**
  56. * Process
  57. */
  58. //user has submitted a file
  59. if(isset($_FILES['user_upload']))
  60. {
  61. //echo("<pre>");
  62. //print_r($_FILES['user_upload']);
  63. //echo("</pre>");
  64. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  65. if($upload_ok)
  66. {
  67. //file got on the server without problems, now process it
  68. $new_path = handle_uploaded_document($_course, $_FILES['user_upload'],$base_work_dir,$_POST['curdirpath'],$_user['user_id'],$to_group_id,$to_user_id,$max_filled_space,$_POST['unzip'],$_POST['if_exists']);
  69. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  70. $new_title = isset($_POST['title']) ? trim($_POST['title']) : '';
  71. if ($new_path && ($new_comment || $new_title))
  72. if (($docid = DocumentManager::get_document_id($_course, $new_path)))
  73. {
  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) .
  79. " WHERE id = '$docid'");
  80. }
  81. //check for missing images in html files
  82. $missing_files = check_for_missing_files($base_work_dir.$_POST['curdirpath'].$new_path);
  83. if($missing_files)
  84. {
  85. //show a form to upload the missing files
  86. Display::display_normal_message(build_missing_files_form($missing_files,$_POST['curdirpath'],$_FILES['user_upload']['name']));
  87. }
  88. }
  89. }
  90. //missing images are submitted
  91. if(isset($_POST['submit_image']))
  92. {
  93. $number_of_uploaded_images = count($_FILES['img_file']['name']);
  94. //if images are uploaded
  95. if ($number_of_uploaded_images > 0)
  96. {
  97. //we could also create a function for this, I'm not sure...
  98. //create a directory for the missing files
  99. $img_directory = str_replace('.','_',$_POST['related_file']."_files");
  100. $missing_files_dir = create_unexisting_directory($_course,$_user['user_id'],$to_group_id,$to_user_id,$base_work_dir,$img_directory);
  101. //put the uploaded files in the new directory and get the paths
  102. $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);
  103. //open the html file and replace the paths
  104. replace_img_path_in_html_file($_POST['img_file_path'],$paths_to_replace_in_file,$base_work_dir.$_POST['related_file']);
  105. //update parent folders
  106. item_property_update_on_folder($_course,$_POST['curdirpath'],$_user['user_id']);
  107. }
  108. }
  109. //they want to create a directory
  110. if(isset($_POST['create_dir']) && $_POST['dirname']!='')
  111. {
  112. $added_slash = ($path=='/')?'':'/';
  113. $dir_name = $path.$added_slash.replace_dangerous_char($_POST['dirname']);
  114. $created_dir = create_unexisting_directory($_course,$_user['user_id'],$to_group_id,$to_user_id,$base_work_dir,$dir_name,$_POST['dirname']);
  115. if($created_dir)
  116. {
  117. //Display::display_normal_message("<strong>".$created_dir."</strong> was created!");
  118. Display::display_normal_message(get_lang('DirCr'));
  119. $path = $created_dir;
  120. }
  121. else
  122. {
  123. display_error(get_lang('CannotCreateDir'));
  124. }
  125. }
  126. //tracking not needed here?
  127. //event_access_tool(TOOL_DOCUMENT);
  128. /*============================================================================*/
  129. ?>
  130. <?php
  131. //=======================================//
  132. //they want to create a new directory//
  133. //=======================================//
  134. if(isset($_GET['createdir']))
  135. {
  136. //create the form that asks for the directory name
  137. $new_folder_text = '<form action="'.api_get_self().'" method="POST">';
  138. $new_folder_text .= '<input type="hidden" name="curdirpath" value="'.$path.'"/>';
  139. $new_folder_text .= get_lang('NewDir') .' ';
  140. $new_folder_text .= '<input type="text" name="dirname"/>';
  141. $new_folder_text .= '<input type="submit" name="create_dir" value="'.get_lang('Ok').'"/>';
  142. $new_folder_text .= '</form>';
  143. //show the form
  144. Display::display_normal_message($new_folder_text);
  145. }
  146. else { //give them a link to create a directory
  147. ?>
  148. <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 ="" /> <?php echo(get_lang('CreateDir'));?></a></p>
  149. <?php
  150. }
  151. ?>
  152. <div id="folderselector">
  153. <?php
  154. //form to select directory
  155. //$folders = DocumentManager::get_all_document_folders($_course,$to_group_id,$is_allowed_to_edit);
  156. //echo(build_directory_selector($folders,$path,$group_properties['directory']));
  157. ?>
  158. </div>
  159. <!-- start upload form -->
  160. <form action="<?php echo api_get_self(); ?>" method="POST" name="upload" enctype="multipart/form-data">
  161. <!-- <input type="hidden" name="MAX_FILE_SIZE" value="5400"> -->
  162. <input type="hidden" name="curdirpath" value="<?php echo $path; ?>">
  163. <table>
  164. <tr>
  165. <td valign="top">
  166. <?php echo get_lang('File'); ?>
  167. </td>
  168. <td>
  169. <input type="file" name="user_upload"/>
  170. </td>
  171. </tr>
  172. <?php
  173. if(api_get_setting('use_document_title')=='true')
  174. {
  175. ?>
  176. <tr>
  177. <td><?php echo get_lang('Title');?></td>
  178. <td><input type="text" size="20" name="title" style="width:300px;"></td>
  179. </tr>
  180. <?php
  181. }
  182. ?>
  183. <tr>
  184. <td valign="top"><?php echo get_lang('Comment');?></td>
  185. <td><textarea rows="3" cols="20" name="comment" wrap="virtual" style="width:300px;"></textarea></td>
  186. </tr>
  187. <tr>
  188. <td valign="top">
  189. <?php echo get_lang('Options'); ?>
  190. </td>
  191. <td>
  192. - <input type="checkbox" name="unzip" value="1" onclick="check_unzip()"/> <?php echo(get_lang('Uncompress'));?><br/>
  193. - <?php echo (get_lang('UplWhatIfFileExists'));?><br/>
  194. &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/>
  195. &nbsp;&nbsp;&nbsp;<input type="radio" name="if_exists" value="overwrite" title="<?php echo (get_lang('UplOverwriteLong'));?>"/> <?php echo (get_lang('UplOverwrite'));?><br/>
  196. &nbsp;&nbsp;&nbsp;<input type="radio" name="if_exists" value="rename" title="<?php echo (get_lang('UplRenameLong'));?>"/> <?php echo (get_lang('UplRename'));?>
  197. </td>
  198. </tr>
  199. </table>
  200. <input type="submit" value="<?php echo(get_lang('Oki'));?>">
  201. </form>
  202. <!-- end upload form -->
  203. <!-- so they can get back to the documents -->
  204. <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>
  205. <?php
  206. /*
  207. ==============================================================================
  208. FOOTER
  209. ==============================================================================
  210. */
  211. Display::display_footer();
  212. ?>