upload.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php // $Id: upload.php 10204 2006-11-26 20:46:53Z pcool $
  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 that needs to be included
  58. $language_file = 'document';
  59. // global settings initialisation
  60. // also provides access to main api (inc/lib/main_api.lib.php)
  61. include("../inc/global.inc.php");
  62. include('document.inc.php');
  63. $htmlHeadXtra[] =
  64. "<script type=\"text/javascript\">
  65. <!-- //
  66. function check_unzip() {
  67. if(document.upload.unzip.checked==true){
  68. document.upload.if_exists[0].disabled=true;
  69. document.upload.if_exists[1].checked=true;
  70. document.upload.if_exists[2].disabled=true;
  71. }
  72. else {
  73. document.upload.if_exists[0].checked=true;
  74. document.upload.if_exists[0].disabled=false;
  75. document.upload.if_exists[2].disabled=false;
  76. }
  77. }
  78. // -->
  79. </script>";
  80. /*
  81. -----------------------------------------------------------
  82. Variables
  83. - some need defining before inclusion of libraries
  84. -----------------------------------------------------------
  85. */
  86. $is_allowed_to_edit = api_is_allowed_to_edit();
  87. $courseDir = $_course['path']."/document";
  88. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  89. $base_work_dir = $sys_course_path.$courseDir;
  90. $noPHP_SELF=true;
  91. //this needs cleaning!
  92. if(isset($_SESSION['_gid']) && $_SESSION['_gid']!='') //if the group id is set, check if the user has the right to be here
  93. {
  94. //needed for group related stuff
  95. include_once(api_get_path(LIBRARY_PATH) . 'groupmanager.lib.php');
  96. //get group info
  97. $group_properties = GroupManager::get_group_properties($_SESSION['_gid']);
  98. $noPHP_SELF=true;
  99. if($is_allowed_to_edit || GroupManager::is_user_in_group($_user['user_id'],$_SESSION['_gid'])) //only courseadmin or group members allowed
  100. {
  101. $to_group_id = $_SESSION['_gid'];
  102. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  103. $interbreadcrumb[]= array ("url"=>"../group/group_space.php?gidReq=".$_SESSION['_gid'], "name"=> get_lang('GroupSpace'));
  104. }
  105. else
  106. {
  107. api_not_allowed();
  108. }
  109. }
  110. elseif($is_allowed_to_edit) //admin for "regular" upload, no group documents
  111. {
  112. $to_group_id = 0;
  113. $req_gid = '';
  114. }
  115. else //no course admin and no group member...
  116. {
  117. api_not_allowed();
  118. }
  119. //what's the current path?
  120. if(isset($_GET['path']) && $_GET['path']!='')
  121. {
  122. $path = $_GET['path'];
  123. }
  124. elseif (isset($_POST['curdirpath']))
  125. {
  126. $path = $_POST['curdirpath'];
  127. }
  128. else
  129. {
  130. $path = '/';
  131. }
  132. /*
  133. -----------------------------------------------------------
  134. Libraries
  135. -----------------------------------------------------------
  136. */
  137. //many useful functions in main_api.lib.php, by default included
  138. include_once(api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php');
  139. include_once(api_get_path(LIBRARY_PATH) . 'events.lib.inc.php');
  140. include_once(api_get_path(LIBRARY_PATH) . 'document.lib.php');
  141. //check the path
  142. //if the path is not found (no document id), set the path to /
  143. if(!DocumentManager::get_document_id($_course,$path))
  144. {
  145. $path = '/';
  146. }
  147. //group docs can only be uploaded in the group directory
  148. if($to_group_id!=0 && $path=='/')
  149. {
  150. $path = $group_properties['directory'];
  151. }
  152. //if we want to unzip a file, we need the library
  153. if (isset($_POST['unzip']) && $_POST['unzip'] == 1)
  154. {
  155. include(api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
  156. }
  157. /*
  158. -----------------------------------------------------------
  159. Variables
  160. -----------------------------------------------------------
  161. */
  162. $max_filled_space = DocumentManager::get_course_quota();
  163. /*
  164. -----------------------------------------------------------
  165. Header
  166. -----------------------------------------------------------
  167. */
  168. $nameTools = get_lang('UplUploadDocument');
  169. $interbreadcrumb[]=array("url"=>"./document.php?curdirpath=".urlencode($path).$req_gid, "name"=> $langDocuments);
  170. Display::display_header($nameTools,"Doc");
  171. if($to_group_id !=0) //add group name after for group documents
  172. {
  173. $add_group_to_title = ' ('.$group_properties['name'].')';
  174. }
  175. //show the title
  176. api_display_tool_title($nameTools.$add_group_to_title);
  177. /*
  178. -----------------------------------------------------------
  179. Here we do all the work
  180. -----------------------------------------------------------
  181. */
  182. //user has submitted a file
  183. if(isset($_FILES['user_upload']))
  184. {
  185. //echo("<pre>");
  186. //print_r($_FILES['user_upload']);
  187. //echo("</pre>");
  188. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  189. if($upload_ok)
  190. {
  191. //file got on the server without problems, now process it
  192. $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']);
  193. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  194. $new_title = isset($_POST['title']) ? trim($_POST['title']) : '';
  195. if ($new_path && ($new_comment || $new_title))
  196. if (($docid = DocumentManager::get_document_id($_course, $new_path)))
  197. {
  198. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  199. $ct = '';
  200. if ($new_comment) $ct .= ", comment='$new_comment'";
  201. if ($new_title) $ct .= ", title='$new_title'";
  202. api_sql_query("UPDATE $table_document SET" . substr($ct, 1) .
  203. " WHERE id = '$docid'", __FILE__, __LINE__);
  204. }
  205. //check for missing images in html files
  206. $missing_files = check_for_missing_files($base_work_dir.$_POST['curdirpath'].$new_path);
  207. if($missing_files)
  208. {
  209. //show a form to upload the missing files
  210. Display::display_normal_message(build_missing_files_form($missing_files,$_POST['curdirpath'],$_FILES['user_upload']['name']));
  211. }
  212. }
  213. }
  214. //missing images are submitted
  215. if(isset($_POST['submit_image']))
  216. {
  217. $number_of_uploaded_images = count($_FILES['img_file']['name']);
  218. //if images are uploaded
  219. if ($number_of_uploaded_images > 0)
  220. {
  221. //we could also create a function for this, I'm not sure...
  222. //create a directory for the missing files
  223. $img_directory = str_replace('.','_',$_POST['related_file']."_files");
  224. $missing_files_dir = create_unexisting_directory($_course,$_user['user_id'],$to_group_id,$to_user_id,$base_work_dir,$img_directory);
  225. //put the uploaded files in the new directory and get the paths
  226. $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);
  227. //open the html file and replace the paths
  228. replace_img_path_in_html_file($_POST['img_file_path'],$paths_to_replace_in_file,$base_work_dir.$_POST['related_file']);
  229. //update parent folders
  230. item_property_update_on_folder($_course,$_POST['curdirpath'],$_user['user_id']);
  231. }
  232. }
  233. //they want to create a directory
  234. if(isset($_POST['create_dir']) && $_POST['dirname']!='')
  235. {
  236. $added_slash = ($path=='/')?'':'/';
  237. $dir_name = $path.$added_slash.replace_dangerous_char($_POST['dirname']);
  238. $created_dir = create_unexisting_directory($_course,$_user['user_id'],$to_group_id,$to_user_id,$base_work_dir,$dir_name,$_POST['dirname']);
  239. if($created_dir)
  240. {
  241. //Display::display_normal_message("<strong>".$created_dir."</strong> was created!");
  242. Display::display_normal_message(get_lang('DirCr'));
  243. $path = $created_dir;
  244. }
  245. else
  246. {
  247. display_error(get_lang('CannotCreateDir'));
  248. }
  249. }
  250. //tracking not needed here?
  251. //event_access_tool(TOOL_DOCUMENT);
  252. /*============================================================================*/
  253. ?>
  254. <?php
  255. //=======================================//
  256. //they want to create a new directory//
  257. //=======================================//
  258. if(isset($_GET['createdir']))
  259. {
  260. //create the form that asks for the directory name
  261. $new_folder_text = '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
  262. $new_folder_text .= '<input type="hidden" name="curdirpath" value="'.$path.'"/>';
  263. $new_folder_text .= get_lang('NewDir') .' ';
  264. $new_folder_text .= '<input type="text" name="dirname"/>';
  265. $new_folder_text .= '<input type="submit" name="create_dir" value="'.get_lang('Ok').'"/>';
  266. $new_folder_text .= '</form>';
  267. //show the form
  268. Display::display_normal_message($new_folder_text);
  269. }
  270. else { //give them a link to create a directory
  271. ?>
  272. <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?path=<?php echo $path; ?>&amp;createdir=1"><img src="../img/folder_new.gif" border="0" align="absmiddle" alt ="" /> <?php echo(get_lang('CreateDir'));?></a></p>
  273. <?php
  274. }
  275. ?>
  276. <div id="folderselector">
  277. <?php
  278. //form to select directory
  279. $folders = DocumentManager::get_all_document_folders($_course,$to_group_id,$is_allowed_to_edit);
  280. echo(build_directory_selector($folders,$path,$group_properties['directory']));
  281. ?>
  282. </div>
  283. <!-- start upload form -->
  284. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" name="upload" enctype="multipart/form-data">
  285. <!-- <input type="hidden" name="MAX_FILE_SIZE" value="5400"> -->
  286. <input type="hidden" name="curdirpath" value="<?php echo $path; ?>">
  287. <table>
  288. <tr>
  289. <td valign="top">
  290. <?php echo get_lang('File'); ?>
  291. </td>
  292. <td>
  293. <input type="file" name="user_upload"/>
  294. </td>
  295. </tr>
  296. <?php
  297. if(get_setting('use_document_title')=='true')
  298. {
  299. ?>
  300. <tr>
  301. <td><?php echo get_lang('Title');?></td>
  302. <td><input type="text" size="20" name="title" style="width:300px;"></td>
  303. </tr>
  304. <?php
  305. }
  306. ?>
  307. <tr>
  308. <td valign="top"><?php echo get_lang('Comment');?></td>
  309. <td><textarea rows="3" cols="20" name="comment" wrap="virtual" style="width:300px;"></textarea></td>
  310. </tr>
  311. <tr>
  312. <td valign="top">
  313. <?php echo get_lang('Options'); ?>
  314. </td>
  315. <td>
  316. - <input type="checkbox" name="unzip" value="1" onclick="check_unzip()"/> <?php echo(get_lang('Uncompress'));?><br/>
  317. - <?php echo (get_lang('UplWhatIfFileExists'));?><br/>
  318. &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/>
  319. &nbsp;&nbsp;&nbsp;<input type="radio" name="if_exists" value="overwrite" title="<?php echo (get_lang('UplOverwriteLong'));?>"/> <?php echo (get_lang('UplOverwrite'));?><br/>
  320. &nbsp;&nbsp;&nbsp;<input type="radio" name="if_exists" value="rename" title="<?php echo (get_lang('UplRenameLong'));?>"/> <?php echo (get_lang('UplRename'));?>
  321. </td>
  322. </tr>
  323. </table>
  324. <input type="submit" value="<?php echo(get_lang('Ok'));?>">
  325. </form>
  326. <!-- end upload form -->
  327. <!-- so they can get back to the documents -->
  328. <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>
  329. <?php
  330. /*
  331. ==============================================================================
  332. FOOTER
  333. ==============================================================================
  334. */
  335. Display::display_footer();
  336. ?>