upload.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Main script for the documents tool.
  5. *
  6. * This script allows the user to manage files and directories on a remote http server.
  7. *
  8. * The user can : - navigate through files and directories.
  9. * - upload a file
  10. * - delete, copy a file or a directory
  11. * - edit properties & content (name, comments, html content)
  12. *
  13. * The script is organised in four sections.
  14. *
  15. * 1) Execute the command called by the user
  16. * Note: somme commands of this section are organised in two steps.
  17. * The script always begins with the second step,
  18. * so it allows to return more easily to the first step.
  19. *
  20. * Note (March 2004) some editing functions (renaming, commenting)
  21. * are moved to a separate page, edit_document.php. This is also
  22. * where xml and other stuff should be added.
  23. *
  24. * 2) Define the directory to display
  25. *
  26. * 3) Read files and directories from the directory defined in part 2
  27. * 4) Display all of that on an HTML page
  28. *
  29. * @todo eliminate code duplication between
  30. * document/document.php, scormdocument.php
  31. *
  32. * @package chamilo.document
  33. */
  34. // Including the global initialization file
  35. require_once __DIR__.'/../inc/global.inc.php';
  36. // Including additional libraries
  37. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  38. // Adding extra javascript to the form
  39. $htmlHeadXtra[] = api_get_jquery_libraries_js(['jquery-ui', 'jquery-upload']);
  40. // Variables
  41. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  42. $_course = api_get_course_info();
  43. $groupId = api_get_group_id();
  44. $courseDir = $_course['path'].'/document';
  45. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  46. $base_work_dir = $sys_course_path.$courseDir;
  47. $sessionId = api_get_session_id();
  48. $selectcat = isset($_GET['selectcat']) ? Security::remove_XSS($_GET['selectcat']) : null;
  49. $document_data = [];
  50. if (isset($_REQUEST['id'])) {
  51. $document_data = DocumentManager::get_document_data_by_id(
  52. $_REQUEST['id'],
  53. api_get_course_id(),
  54. true,
  55. $sessionId
  56. );
  57. if ($sessionId != 0 && !$document_data) {
  58. $document_data = DocumentManager::get_document_data_by_id(
  59. $_REQUEST['id'],
  60. api_get_course_id(),
  61. true,
  62. 0
  63. );
  64. }
  65. }
  66. if (empty($document_data)) {
  67. $document_id = $parent_id = 0;
  68. $path = '/';
  69. } else {
  70. $document_id = $document_data['id'];
  71. $path = $document_data['path'];
  72. $parent_id = DocumentManager::get_document_id(
  73. api_get_course_info(),
  74. dirname($path)
  75. );
  76. }
  77. $group_properties = [];
  78. $htmlHeadXtra[] = '<script>
  79. function check_unzip() {
  80. if (document.upload.unzip.checked){
  81. //document.upload.if_exists[0].disabled=true;
  82. document.upload.if_exists[1].checked=true;
  83. //document.upload.if_exists[2].disabled=true;
  84. } else {
  85. document.upload.if_exists[2].checked=true;
  86. //document.upload.if_exists[0].disabled=false;
  87. //document.upload.if_exists[2].disabled=false;
  88. }
  89. }
  90. function setFocus() {
  91. $("#title_file").focus();
  92. }
  93. </script>';
  94. $groupIid = 0;
  95. // This needs cleaning!
  96. if (!empty($groupId)) {
  97. // If the group id is set, check if the user has the right to be here
  98. // Get group info
  99. $group_properties = GroupManager::get_group_properties($groupId);
  100. $groupIid = $group_properties['iid'];
  101. // Only courseadmin or group members allowed
  102. if ($is_allowed_to_edit || GroupManager::is_user_in_group(api_get_user_id(), $group_properties)) {
  103. $interbreadcrumb[] = [
  104. 'url' => api_get_path(WEB_CODE_PATH).'group/group_space.php?'.api_get_cidreq(),
  105. 'name' => get_lang('GroupSpace'),
  106. ];
  107. } else {
  108. api_not_allowed(true);
  109. }
  110. GroupManager::allowUploadEditDocument(api_get_user_id(), api_get_course_int_id(), $group_properties, true);
  111. } elseif ($is_allowed_to_edit ||
  112. DocumentManager::is_my_shared_folder(api_get_user_id(), $path, api_get_session_id())) {
  113. } else {
  114. // No course admin and no group member...
  115. api_not_allowed(true);
  116. }
  117. // Group docs can only be uploaded in the group directory
  118. if ($groupId != 0 && $path == '/') {
  119. $path = $group_properties['directory'];
  120. }
  121. // I'm in the certification module?
  122. $is_certificate_mode = false;
  123. $is_certificate_array = explode('/', $path);
  124. array_shift($is_certificate_array);
  125. if ($is_certificate_array[0] == 'certificates') {
  126. $is_certificate_mode = true;
  127. }
  128. // Title of the tool
  129. $add_group_to_title = null;
  130. if ($groupId != 0) {
  131. // Add group name after for group documents
  132. $add_group_to_title = ' ('.$group_properties['name'].')';
  133. }
  134. if (isset($_REQUEST['certificate'])) {
  135. $nameTools = get_lang('UploadCertificate').$add_group_to_title;
  136. $is_certificate_mode = true;
  137. } else {
  138. $nameTools = get_lang('UplUploadDocument').$add_group_to_title;
  139. }
  140. $certificateLink = '';
  141. if ($is_certificate_mode) {
  142. $certificateLink = '&certificate=true';
  143. }
  144. // Breadcrumbs
  145. if ($is_certificate_mode) {
  146. $interbreadcrumb[] = [
  147. 'url' => '../gradebook/index.php?'.api_get_cidreq().$certificateLink,
  148. 'name' => get_lang('Gradebook'),
  149. ];
  150. } else {
  151. $interbreadcrumb[] = [
  152. 'url' => './document.php?id='.$document_id.'&'.api_get_cidreq().$certificateLink,
  153. 'name' => get_lang('Documents'),
  154. ];
  155. }
  156. // Interbreadcrumb for the current directory root path
  157. if ($document_data) {
  158. if (empty($document_data['parents'])) {
  159. $interbreadcrumb[] = ['url' => '#', 'name' => $document_data['title']];
  160. } else {
  161. foreach ($document_data['parents'] as $document_sub_data) {
  162. $interbreadcrumb[] = [
  163. 'url' => $document_sub_data['document_url'].$certificateLink,
  164. 'name' => $document_sub_data['title'],
  165. ];
  166. }
  167. }
  168. }
  169. $this_section = SECTION_COURSES;
  170. /* Here we do all the work */
  171. $unzip = isset($_POST['unzip']) ? $_POST['unzip'] : null;
  172. $index = isset($_POST['index_document']) ? $_POST['index_document'] : null;
  173. // User has submitted a file
  174. if (!empty($_FILES)) {
  175. DocumentManager::upload_document(
  176. $_FILES,
  177. $_POST['curdirpath'],
  178. $_POST['title'],
  179. $_POST['comment'],
  180. $unzip,
  181. $_POST['if_exists'],
  182. $index,
  183. true
  184. );
  185. $redirectUrl = api_get_self().'?'.api_get_cidreq().$certificateLink;
  186. if ($document_data) {
  187. $redirectUrl .= '&'.http_build_query(
  188. [
  189. 'id' => $document_data['iid'],
  190. ]
  191. );
  192. }
  193. header("Location: $redirectUrl");
  194. exit;
  195. }
  196. // Display the header
  197. Display::display_header($nameTools, 'Doc');
  198. // Actions
  199. // Link back to the documents overview
  200. if ($is_certificate_mode) {
  201. $actions = '<a href="document.php?id='.$document_id.'&selectcat='.$selectcat.'&'.api_get_cidreq().'">'.
  202. Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('CertificateOverview'), '', ICON_SIZE_MEDIUM).
  203. '</a>';
  204. } else {
  205. $actions = '<a href="document.php?id='.$document_id.'&'.api_get_cidreq().'">'.
  206. Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('DocumentsOverview'), '', ICON_SIZE_MEDIUM).
  207. '</a>';
  208. }
  209. // Link to create a folder
  210. echo $toolbar = Display::toolbarAction('toolbar-upload', [$actions]);
  211. // Form to select directory
  212. $folders = DocumentManager::get_all_document_folders(
  213. $_course,
  214. $groupIid,
  215. $is_allowed_to_edit
  216. );
  217. if (!$is_certificate_mode) {
  218. echo DocumentManager::build_directory_selector(
  219. $folders,
  220. $document_id,
  221. (isset($group_properties['directory']) ? $group_properties['directory'] : [])
  222. );
  223. }
  224. $action = api_get_self().'?'.api_get_cidreq().'&id='.$document_id.$certificateLink;
  225. $form = new FormValidator(
  226. 'upload',
  227. 'POST',
  228. $action.'#tabs-2',
  229. '',
  230. ['enctype' => 'multipart/form-data']
  231. );
  232. $form->addElement('hidden', 'id', $document_id);
  233. $form->addElement('hidden', 'curdirpath', $path);
  234. $courseQuota = format_file_size(DocumentManager::get_course_quota() - DocumentManager::documents_total_space());
  235. $label =
  236. get_lang('MaxFileSize').': '.ini_get('upload_max_filesize').'<br/>'.
  237. get_lang('DocumentQuota').': '.$courseQuota;
  238. $form->addElement('file', 'file', [get_lang('File'), $label], 'style="width: 250px" id="user_upload"');
  239. $form->addElement('text', 'title', get_lang('Title'), ['id' => 'title_file']);
  240. $form->addElement('textarea', 'comment', get_lang('Comment'));
  241. // Advanced parameters
  242. $form->addButtonAdvancedSettings('advanced_params');
  243. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  244. // Check box options
  245. $form->addElement(
  246. 'checkbox',
  247. 'unzip',
  248. get_lang('Options'),
  249. get_lang('Uncompress'),
  250. 'onclick="javascript: check_unzip();" value="1"'
  251. );
  252. if (api_get_setting('search_enabled') === 'true') {
  253. //TODO: include language file
  254. $supportedFormats = get_lang('SupportedFormatsForIndex').': HTML, PDF, TXT, PDF, Postscript, MS Word, RTF, MS Power Point';
  255. $form->addElement(
  256. 'checkbox',
  257. 'index_document',
  258. '',
  259. get_lang('SearchFeatureDoIndexDocument').'<div style="font-size: 80%" >'.$supportedFormats.'</div>'
  260. );
  261. $form->addElement('html', '<br /><div class="sub-form">');
  262. $form->addElement('html', '<div class="label">'.get_lang('SearchFeatureDocumentLanguage').'</div>');
  263. $form->addLabel(get_lang('Language'), api_get_languages_combo());
  264. $form->addElement('html', '</div><div class="sub-form">');
  265. $specific_fields = get_specific_field_list();
  266. foreach ($specific_fields as $specific_field) {
  267. $form->addElement('text', $specific_field['code'], $specific_field['name']);
  268. }
  269. $form->addElement('html', '</div>');
  270. }
  271. $form->addElement('radio', 'if_exists', get_lang('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'nothing');
  272. $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'overwrite');
  273. $form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'rename');
  274. // Close the java script and avoid the footer up
  275. $form->addElement('html', '</div>');
  276. // Button upload document
  277. $form->addButtonSend(get_lang('SendDocument'), 'submitDocument');
  278. $form->addProgress('DocumentUpload', 'file');
  279. $fileExistsOption = api_get_setting('document_if_file_exists_option');
  280. $defaultFileExistsOption = 'rename';
  281. if (!empty($fileExistsOption)) {
  282. $defaultFileExistsOption = $fileExistsOption;
  283. }
  284. $defaults = [
  285. 'index_document' => 'checked="checked"',
  286. 'if_exists' => $defaultFileExistsOption,
  287. ];
  288. $form->setDefaults($defaults);
  289. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath='.$path;
  290. $multipleForm = new FormValidator(
  291. 'drag_drop',
  292. 'post',
  293. '#',
  294. ['enctype' => 'multipart/form-data']
  295. );
  296. $multipleForm->addMultipleUpload($url);
  297. $headers = [
  298. get_lang('Upload'),
  299. get_lang('Upload').' ('.get_lang('Simple').')',
  300. ];
  301. echo Display::tabs($headers, [$multipleForm->returnForm(), $form->returnForm()], 'tabs');
  302. Display::display_footer();