upload.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php // $Id: upload.php 12269 2007-05-03 14:17:37Z elixir_julian $
  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(true);
  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(true);
  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_confirmation_message(get_lang('DirCr'),false);
  242. $path = $created_dir;
  243. }
  244. else
  245. {
  246. display_error(get_lang('CannotCreateDir'));
  247. }
  248. }
  249. //tracking not needed here?
  250. //event_access_tool(TOOL_DOCUMENT);
  251. /*============================================================================*/
  252. ?>
  253. <?php
  254. //=======================================//
  255. //they want to create a new directory//
  256. //=======================================//
  257. if(isset($_GET['createdir']))
  258. {
  259. //create the form that asks for the directory name
  260. $new_folder_text = '<form action="'.api_get_self().'" method="POST">';
  261. $new_folder_text .= '<input type="hidden" name="curdirpath" value="'.$path.'"/>';
  262. $new_folder_text .= get_lang('NewDir') .' ';
  263. $new_folder_text .= '<input type="text" name="dirname"/>';
  264. $new_folder_text .= '<input type="submit" name="create_dir" value="'.get_lang('Ok').'"/>';
  265. $new_folder_text .= '</form>';
  266. //show the form
  267. Display::display_normal_message($new_folder_text, false);
  268. }
  269. else { //give them a link to create a directory
  270. ?>
  271. <p><a href="<?php echo api_get_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>
  272. <?php
  273. }
  274. ?>
  275. <div id="folderselector">
  276. <?php
  277. //form to select directory
  278. $folders = DocumentManager::get_all_document_folders($_course,$to_group_id,$is_allowed_to_edit);
  279. echo(build_directory_selector($folders,$path,$group_properties['directory']));
  280. ?>
  281. </div>
  282. <!-- start upload form -->
  283. <?php
  284. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  285. $form = new FormValidator('upload','POST',$_SERVER['PHP_SELF'],'','enctype="multipart/form-data"');
  286. $form->addElement('hidden','curdirpath',$path);
  287. $form->addElement('file','user_upload',get_lang('File'),'');
  288. if(get_setting('use_document_title')=='true')
  289. {
  290. $form->addElement('text','title',get_lang('Title'),'size="20" style="width:300px;"');
  291. $form->addElement('textarea','comment',get_lang('Comment'),'wrap="virtual" style="width:300px;"');
  292. }
  293. $form->addElement('checkbox','unzip',get_lang('Options'),get_lang('Uncompress'),'onclick="check_unzip()" value="1"');
  294. $form->addElement('radio', 'if_exists', get_lang('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'value="nothing"');
  295. $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'value="overwrite"');
  296. $form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'value="rename"');
  297. $form->addElement('submit', 'submitDocument', get_lang('Ok'));
  298. $form->add_real_progress_bar('DocumentUpload','user_upload');
  299. $form->display();
  300. ?>
  301. <!-- end upload form -->
  302. <!-- so they can get back to the documents -->
  303. <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>
  304. <?php
  305. /*
  306. ==============================================================================
  307. FOOTER
  308. ==============================================================================
  309. */
  310. Display::display_footer();
  311. ?>