upload.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php // $Id: upload.php 9246 2006-09-25 13:24:53Z bmol $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2005 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * Main script for the documents tool
  22. *
  23. * This script allows the user to manage files and directories on a remote http server.
  24. *
  25. * The user can : - navigate through files and directories.
  26. * - upload a file
  27. * - delete, copy a file or a directory
  28. * - edit properties & content (name, comments, html content)
  29. *
  30. * The script is organised in four sections.
  31. *
  32. * 1) Execute the command called by the user
  33. * Note: somme commands of this section are organised in two steps.
  34. * The script always begins with the second step,
  35. * so it allows to return more easily to the first step.
  36. *
  37. * Note (March 2004) some editing functions (renaming, commenting)
  38. * are moved to a separate page, edit_document.php. This is also
  39. * where xml and other stuff should be added.
  40. *
  41. * 2) Define the directory to display
  42. *
  43. * 3) Read files and directories from the directory defined in part 2
  44. * 4) Display all of that on an HTML page
  45. *
  46. * @todo eliminate code duplication between
  47. * document/document.php, scormdocument.php
  48. *
  49. * @package dokeos.document
  50. ==============================================================================
  51. */
  52. /*
  53. ==============================================================================
  54. INIT SECTION
  55. ==============================================================================
  56. */
  57. // name of the language file which needs to be included
  58. // 'inc.php' is automatically appended to the file name
  59. $langFile = "document";
  60. // global settings initialisation
  61. // also provides access to main api (inc/lib/main_api.lib.php)
  62. include("../inc/global.inc.php");
  63. include('document.inc.php');
  64. $htmlHeadXtra[] =
  65. "<script type=\"text/javascript\">
  66. <!-- //
  67. function check_unzip() {
  68. if(document.upload.unzip.checked==true){
  69. document.upload.if_exists[0].disabled=true;
  70. document.upload.if_exists[1].checked=true;
  71. document.upload.if_exists[2].disabled=true;
  72. }
  73. else {
  74. document.upload.if_exists[0].checked=true;
  75. document.upload.if_exists[0].disabled=false;
  76. document.upload.if_exists[2].disabled=false;
  77. }
  78. }
  79. // -->
  80. </script>";
  81. /*
  82. -----------------------------------------------------------
  83. Variables
  84. - some need defining before inclusion of libraries
  85. -----------------------------------------------------------
  86. */
  87. $is_allowed_to_edit = api_is_allowed_to_edit();
  88. $courseDir = $_course['path']."/document";
  89. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  90. $base_work_dir = $sys_course_path.$courseDir;
  91. $noPHP_SELF=true;
  92. //this needs cleaning!
  93. if(isset($_SESSION['_gid']) && $_SESSION['_gid']!='') //if the group id is set, check if the user has the right to be here
  94. {
  95. //needed for group related stuff
  96. include_once(api_get_path(LIBRARY_PATH) . 'groupmanager.lib.php');
  97. //get group info
  98. $group_properties = GroupManager::get_group_properties($_SESSION['_gid']);
  99. $noPHP_SELF=true;
  100. if($is_allowed_to_edit || GroupManager::is_user_in_group($_uid,$_SESSION['_gid'])) //only courseadmin or group members allowed
  101. {
  102. $to_group_id = $_SESSION['_gid'];
  103. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  104. $interbreadcrumb[]= array ("url"=>"../group/group_space.php?gidReq=".$_SESSION['_gid'], "name"=> get_lang('GroupSpace'));
  105. }
  106. else
  107. {
  108. api_not_allowed();
  109. }
  110. }
  111. elseif($is_allowed_to_edit) //admin for "regular" upload, no group documents
  112. {
  113. $to_group_id = 0;
  114. $req_gid = '';
  115. }
  116. else //no course admin and no group member...
  117. {
  118. api_not_allowed();
  119. }
  120. //what's the current path?
  121. if(isset($_GET['path']) && $_GET['path']!='')
  122. {
  123. $path = $_GET['path'];
  124. }
  125. elseif (isset($_POST['curdirpath']))
  126. {
  127. $path = $_POST['curdirpath'];
  128. }
  129. else
  130. {
  131. $path = '/';
  132. }
  133. /*
  134. -----------------------------------------------------------
  135. Libraries
  136. -----------------------------------------------------------
  137. */
  138. //many useful functions in main_api.lib.php, by default included
  139. include_once(api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php');
  140. include_once(api_get_path(LIBRARY_PATH) . 'events.lib.inc.php');
  141. include_once(api_get_path(LIBRARY_PATH) . 'document.lib.php');
  142. //check the path
  143. //if the path is not found (no document id), set the path to /
  144. if(!DocumentManager::get_document_id($_course,$path))
  145. {
  146. $path = '/';
  147. }
  148. //group docs can only be uploaded in the group directory
  149. if($to_group_id!=0 && $path=='/')
  150. {
  151. $path = $group_properties['directory'];
  152. }
  153. //if we want to unzip a file, we need the library
  154. if (isset($_POST['unzip']) && $_POST['unzip'] == 1)
  155. {
  156. include(api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
  157. }
  158. /*
  159. -----------------------------------------------------------
  160. Variables
  161. -----------------------------------------------------------
  162. */
  163. $max_filled_space = DocumentManager::get_course_quota();
  164. /*
  165. -----------------------------------------------------------
  166. Header
  167. -----------------------------------------------------------
  168. */
  169. $nameTools = get_lang('UplUploadDocument');
  170. $interbreadcrumb[]=array("url"=>"./document.php?curdirpath=".urlencode($path).$req_gid, "name"=> $langDocuments);
  171. Display::display_header($nameTools,"Doc");
  172. if($to_group_id !=0) //add group name after for group documents
  173. {
  174. $add_group_to_title = ' ('.$group_properties['name'].')';
  175. }
  176. //show the title
  177. api_display_tool_title($nameTools.$add_group_to_title);
  178. /*
  179. -----------------------------------------------------------
  180. Here we do all the work
  181. -----------------------------------------------------------
  182. */
  183. //user has submitted a file
  184. if(isset($_FILES['user_upload']))
  185. {
  186. //echo("<pre>");
  187. //print_r($_FILES['user_upload']);
  188. //echo("</pre>");
  189. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  190. if($upload_ok)
  191. {
  192. //file got on the server without problems, now process it
  193. $new_path = handle_uploaded_document($_course, $_FILES['user_upload'],$base_work_dir,$_POST['curdirpath'],$_uid,$to_group_id,$to_user_id,$max_filled_space,$_POST['unzip'],$_POST['if_exists']);
  194. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  195. $new_title = isset($_POST['title']) ? trim($_POST['title']) : '';
  196. if ($new_path && ($new_comment || $new_title))
  197. if (($docid = DocumentManager::get_document_id($_course, $new_path)))
  198. {
  199. $table_document = Database::get_course_table(DOCUMENT_TABLE);
  200. $ct = '';
  201. if ($new_comment) $ct .= ", comment='$new_comment'";
  202. if ($new_title) $ct .= ", title='$new_title'";
  203. api_sql_query("UPDATE $table_document SET" . substr($ct, 1) .
  204. " WHERE id = '$docid'", __FILE__, __LINE__);
  205. }
  206. //check for missing images in html files
  207. $missing_files = check_for_missing_files($base_work_dir.$_POST['curdirpath'].$new_path);
  208. if($missing_files)
  209. {
  210. //show a form to upload the missing files
  211. Display::display_normal_message(build_missing_files_form($missing_files,$_POST['curdirpath'],$_FILES['user_upload']['name']));
  212. }
  213. }
  214. }
  215. //missing images are submitted
  216. if(isset($_POST['submit_image']))
  217. {
  218. $number_of_uploaded_images = count($_FILES['img_file']['name']);
  219. //if images are uploaded
  220. if ($number_of_uploaded_images > 0)
  221. {
  222. //we could also create a function for this, I'm not sure...
  223. //create a directory for the missing files
  224. $img_directory = str_replace('.','_',$_POST['related_file']."_files");
  225. $missing_files_dir = create_unexisting_directory($_course,$_uid,$to_group_id,$to_user_id,$base_work_dir,$img_directory);
  226. //put the uploaded files in the new directory and get the paths
  227. $paths_to_replace_in_file = move_uploaded_file_collection_into_directory($_course, $_FILES['img_file'],$base_work_dir,$missing_files_dir,$_uid,$to_group_id,$to_user_id,$max_filled_space);
  228. //open the html file and replace the paths
  229. replace_img_path_in_html_file($_POST['img_file_path'],$paths_to_replace_in_file,$base_work_dir.$_POST['related_file']);
  230. //update parent folders
  231. item_property_update_on_folder($_course,$_POST['curdirpath'],$_uid);
  232. }
  233. }
  234. //they want to create a directory
  235. if(isset($_POST['create_dir']) && $_POST['dirname']!='')
  236. {
  237. $added_slash = ($path=='/')?'':'/';
  238. $dir_name = $path.$added_slash.replace_dangerous_char($_POST['dirname']);
  239. $created_dir = create_unexisting_directory($_course,$_uid,$to_group_id,$to_user_id,$base_work_dir,$dir_name,$_POST['dirname']);
  240. if($created_dir)
  241. {
  242. //Display::display_normal_message("<strong>".$created_dir."</strong> was created!");
  243. Display::display_normal_message(get_lang('DirCr'));
  244. $path = $created_dir;
  245. }
  246. else
  247. {
  248. display_error(get_lang('CannotCreateDir'));
  249. }
  250. }
  251. //tracking not needed here?
  252. //event_access_tool(TOOL_DOCUMENT);
  253. /*============================================================================*/
  254. ?>
  255. <?php
  256. //=======================================//
  257. //they want to create a new directory//
  258. //=======================================//
  259. if(isset($_GET['createdir']))
  260. {
  261. //create the form that asks for the directory name
  262. $new_folder_text = '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
  263. $new_folder_text .= '<input type="hidden" name="curdirpath" value="'.$path.'"/>';
  264. $new_folder_text .= get_lang('NewDir') .' ';
  265. $new_folder_text .= '<input type="text" name="dirname"/>';
  266. $new_folder_text .= '<input type="submit" name="create_dir" value="'.get_lang('Ok').'"/>';
  267. $new_folder_text .= '</form>';
  268. //show the form
  269. Display::display_normal_message($new_folder_text);
  270. }
  271. else { //give them a link to create a directory
  272. ?>
  273. <p><a href="<?php echo $_SERVER['PHP_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>
  274. <?php
  275. }
  276. ?>
  277. <div id="folderselector">
  278. <?php
  279. //form to select directory
  280. $folders = DocumentManager::get_all_document_folders($_course,$to_group_id,$is_allowed_to_edit);
  281. echo(build_directory_selector($folders,$path,$group_properties['directory']));
  282. ?>
  283. </div>
  284. <!-- start upload form -->
  285. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" name="upload" enctype="multipart/form-data">
  286. <!-- <input type="hidden" name="MAX_FILE_SIZE" value="5400"> -->
  287. <input type="hidden" name="curdirpath" value="<?php echo $path; ?>">
  288. <table>
  289. <tr>
  290. <td valign="top">
  291. <?php echo get_lang('File'); ?>
  292. </td>
  293. <td>
  294. <input type="file" name="user_upload"/>
  295. </td>
  296. </tr>
  297. <?php
  298. if(get_setting('use_document_title')=='true')
  299. {
  300. ?>
  301. <tr>
  302. <td><?php echo get_lang('Title');?></td>
  303. <td><input type="text" size="20" name="title" style="width:300px;"></td>
  304. </tr>
  305. <?php
  306. }
  307. ?>
  308. <tr>
  309. <td valign="top"><?php echo get_lang('Comment');?></td>
  310. <td><textarea rows="3" cols="20" name="comment" wrap="virtual" style="width:300px;"></textarea></td>
  311. </tr>
  312. <tr>
  313. <td valign="top">
  314. <?php echo get_lang('Options'); ?>
  315. </td>
  316. <td>
  317. - <input type="checkbox" name="unzip" value="1" onclick="check_unzip()"/> <?php echo(get_lang('Uncompress'));?><br/>
  318. - <?php echo (get_lang('UplWhatIfFileExists'));?><br/>
  319. &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/>
  320. &nbsp;&nbsp;&nbsp;<input type="radio" name="if_exists" value="overwrite" title="<?php echo (get_lang('UplOverwriteLong'));?>"/> <?php echo (get_lang('UplOverwrite'));?><br/>
  321. &nbsp;&nbsp;&nbsp;<input type="radio" name="if_exists" value="rename" title="<?php echo (get_lang('UplRenameLong'));?>"/> <?php echo (get_lang('UplRename'));?>
  322. </td>
  323. </tr>
  324. </table>
  325. <input type="submit" value="<?php echo(get_lang('Ok'));?>">
  326. </form>
  327. <!-- end upload form -->
  328. <!-- so they can get back to the documents -->
  329. <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>
  330. <?php
  331. /*
  332. ==============================================================================
  333. FOOTER
  334. ==============================================================================
  335. */
  336. Display::display_footer();
  337. ?>