upload.document.php 8.6 KB

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