upload.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.document
  5. */
  6. use Chamilo\CoreBundle\Framework\Container;
  7. require_once __DIR__.'/../inc/global.inc.php';
  8. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  9. api_protect_course_script(true);
  10. // Adding extra javascript to the form
  11. $htmlHeadXtra[] = api_get_jquery_libraries_js(['jquery-ui', 'jquery-upload']);
  12. // Variables
  13. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  14. $_course = api_get_course_info();
  15. $groupId = api_get_group_id();
  16. $courseDir = $_course['path'].'/document';
  17. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  18. $base_work_dir = $sys_course_path.$courseDir;
  19. $sessionId = api_get_session_id();
  20. $selectcat = isset($_GET['selectcat']) ? (int) $_GET['selectcat'] : null;
  21. $document_data = [];
  22. if (isset($_REQUEST['id'])) {
  23. $document_data = DocumentManager::get_document_data_by_id(
  24. $_REQUEST['id'],
  25. api_get_course_id(),
  26. true,
  27. $sessionId
  28. );
  29. if ($sessionId != 0 && !$document_data) {
  30. $document_data = DocumentManager::get_document_data_by_id(
  31. $_REQUEST['id'],
  32. api_get_course_id(),
  33. true,
  34. 0
  35. );
  36. }
  37. }
  38. if (empty($document_data)) {
  39. $document_id = $parent_id = 0;
  40. $path = '/';
  41. } else {
  42. $document_id = $document_data['id'];
  43. $path = $document_data['path'];
  44. $parent_id = DocumentManager::get_document_id(
  45. api_get_course_info(),
  46. dirname($path)
  47. );
  48. }
  49. $group_properties = [];
  50. $htmlHeadXtra[] = '<script>
  51. function check_unzip() {
  52. if (document.upload.unzip.checked) {
  53. document.upload.if_exists[1].checked=true;
  54. } else {
  55. document.upload.if_exists[2].checked=true;
  56. }
  57. }
  58. function setFocus() {
  59. $("#title_file").focus();
  60. }
  61. </script>';
  62. $groupIid = 0;
  63. // This needs cleaning!
  64. if (!empty($groupId)) {
  65. // If the group id is set, check if the user has the right to be here
  66. // Get group info
  67. $group_properties = GroupManager::get_group_properties($groupId);
  68. $groupIid = $group_properties['iid'];
  69. // Only courseadmin or group members allowed
  70. if ($is_allowed_to_edit || GroupManager::is_user_in_group(api_get_user_id(), $group_properties)) {
  71. $interbreadcrumb[] = [
  72. 'url' => api_get_path(WEB_CODE_PATH).'group/group_space.php?'.api_get_cidreq(),
  73. 'name' => get_lang('Group area'),
  74. ];
  75. } else {
  76. api_not_allowed(true);
  77. }
  78. GroupManager::allowUploadEditDocument(api_get_user_id(), api_get_course_int_id(), $group_properties, null, true);
  79. } elseif ($is_allowed_to_edit ||
  80. DocumentManager::is_my_shared_folder(api_get_user_id(), $path, api_get_session_id())) {
  81. } else {
  82. // No course admin and no group member...
  83. api_not_allowed(true);
  84. }
  85. // Group docs can only be uploaded in the group directory
  86. if ($groupId != 0 && $path == '/') {
  87. $path = $group_properties['directory'];
  88. }
  89. // I'm in the certification module?
  90. $is_certificate_mode = false;
  91. $is_certificate_array = explode('/', $path);
  92. array_shift($is_certificate_array);
  93. if ($is_certificate_array[0] == 'certificates') {
  94. $is_certificate_mode = true;
  95. }
  96. // Title of the tool
  97. $add_group_to_title = null;
  98. if ($groupId != 0) {
  99. // Add group name after for group documents
  100. $add_group_to_title = ' ('.$group_properties['name'].')';
  101. }
  102. if (isset($_REQUEST['certificate'])) {
  103. $nameTools = get_lang('Upload certificate').$add_group_to_title;
  104. $is_certificate_mode = true;
  105. } else {
  106. $nameTools = get_lang('Upload documents').$add_group_to_title;
  107. }
  108. $certificateLink = '';
  109. if ($is_certificate_mode) {
  110. $certificateLink = '&certificate=true';
  111. }
  112. // Breadcrumbs
  113. if ($is_certificate_mode) {
  114. $interbreadcrumb[] = [
  115. 'url' => '../gradebook/index.php?'.api_get_cidreq().$certificateLink,
  116. 'name' => get_lang('Assessments'),
  117. ];
  118. } else {
  119. $interbreadcrumb[] = [
  120. 'url' => './document.php?id='.$parent_id.'&'.api_get_cidreq().$certificateLink,
  121. 'name' => get_lang('Documents'),
  122. ];
  123. }
  124. // Interbreadcrumb for the current directory root path
  125. if ($document_data) {
  126. if (empty($document_data['parents'])) {
  127. $interbreadcrumb[] = ['url' => '#', 'name' => $document_data['title']];
  128. } else {
  129. foreach ($document_data['parents'] as $document_sub_data) {
  130. $interbreadcrumb[] = [
  131. 'url' => $document_sub_data['document_url'].$certificateLink,
  132. 'name' => $document_sub_data['title'],
  133. ];
  134. }
  135. }
  136. }
  137. $this_section = SECTION_COURSES;
  138. /* Here we do all the work */
  139. $unzip = isset($_POST['unzip']) ? $_POST['unzip'] : null;
  140. $index = isset($_POST['index_document']) ? $_POST['index_document'] : null;
  141. // User has submitted a file
  142. if (!empty($_FILES)) {
  143. $document = DocumentManager::upload_document(
  144. $_FILES,
  145. $_POST['curdirpath'],
  146. $_POST['title'],
  147. $_POST['comment'],
  148. $unzip,
  149. $_POST['if_exists'],
  150. $index,
  151. true,
  152. 'file',
  153. true,
  154. $_REQUEST['id'] ?? 0
  155. );
  156. $redirectUrl = api_get_self().'?'.api_get_cidreq().$certificateLink;
  157. if ($document) {
  158. $redirectUrl .= '&'.http_build_query(
  159. [
  160. 'id' => $parent_id,
  161. ]
  162. );
  163. }
  164. header("Location: $redirectUrl");
  165. exit;
  166. }
  167. // Display the header
  168. Display::display_header($nameTools, 'Doc');
  169. // Actions
  170. // Link back to the documents overview
  171. if ($is_certificate_mode) {
  172. $actions = '<a href="document.php?id='.$document_id.'&selectcat='.$selectcat.'&'.api_get_cidreq().'">'.
  173. Display::return_icon('back.png', get_lang('Back to').' '.get_lang('Certificate overview'), '', ICON_SIZE_MEDIUM).
  174. '</a>';
  175. } else {
  176. $actions = '<a href="document.php?id='.$document_id.'&'.api_get_cidreq().'">'.
  177. Display::return_icon('back.png', get_lang('Back to').' '.get_lang('Documents overview'), '', ICON_SIZE_MEDIUM).
  178. '</a>';
  179. }
  180. // Link to create a folder
  181. echo $toolbar = Display::toolbarAction('toolbar-upload', [$actions]);
  182. // Form to select directory
  183. $folders = DocumentManager::get_all_document_folders(
  184. $_course,
  185. $groupIid,
  186. $is_allowed_to_edit
  187. );
  188. if (!$is_certificate_mode) {
  189. echo DocumentManager::build_directory_selector(
  190. $folders,
  191. $document_id,
  192. (isset($group_properties['directory']) ? $group_properties['directory'] : [])
  193. );
  194. }
  195. $action = api_get_self().'?'.api_get_cidreq().'&id='.$document_id.$certificateLink;
  196. $form = new FormValidator(
  197. 'upload',
  198. 'POST',
  199. $action.'#tabs-2',
  200. '',
  201. ['enctype' => 'multipart/form-data']
  202. );
  203. $form->addElement('hidden', 'id', $document_id);
  204. $form->addElement('hidden', 'curdirpath', $path);
  205. $repo = Container::getDocumentRepository();
  206. $total = $repo->getTotalSpace(api_get_course_int_id());
  207. $courseQuota = format_file_size(DocumentManager::get_course_quota() - $total);
  208. $label =
  209. get_lang('Maximum file size').': '.ini_get('upload_max_filesize').'<br/>'.
  210. get_lang('Space Available').': '.$courseQuota;
  211. $form->addElement('file', 'file', [get_lang('File'), $label], 'style="width: 250px" id="user_upload"');
  212. $form->addElement('text', 'title', get_lang('Title'), ['id' => 'title_file']);
  213. $form->addElement('textarea', 'comment', get_lang('Comment'));
  214. // Advanced parameters
  215. $form->addButtonAdvancedSettings('advanced_params');
  216. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  217. // Check box options
  218. $form->addElement(
  219. 'checkbox',
  220. 'unzip',
  221. get_lang('Options'),
  222. get_lang('Uncompress zip'),
  223. 'onclick="javascript: check_unzip();" value="1"'
  224. );
  225. if (api_get_setting('search_enabled') === 'true') {
  226. //TODO: include language file
  227. $supportedFormats = get_lang('Supported formats for index').': HTML, PDF, TXT, PDF, Postscript, MS Word, RTF, MS Power Point';
  228. $form->addElement(
  229. 'checkbox',
  230. 'index_document',
  231. '',
  232. get_lang('Index document text?').'<div style="font-size: 80%" >'.$supportedFormats.'</div>'
  233. );
  234. $form->addElement('html', '<br /><div class="sub-form">');
  235. $form->addElement('html', '<div class="label">'.get_lang('Document language for indexation').'</div>');
  236. $form->addLabel(get_lang('Language'), api_get_languages_combo());
  237. $form->addElement('html', '</div><div class="sub-form">');
  238. $specific_fields = get_specific_field_list();
  239. foreach ($specific_fields as $specific_field) {
  240. $form->addElement('text', $specific_field['code'], $specific_field['name']);
  241. }
  242. $form->addElement('html', '</div>');
  243. }
  244. $form->addElement('radio', 'if_exists', get_lang('If file exists:'), get_lang('Do nothing'), 'nothing');
  245. $form->addElement('radio', 'if_exists', '', get_lang('Overwrite the existing file'), 'overwrite');
  246. $form->addElement('radio', 'if_exists', '', get_lang('Rename the uploaded file if it exists'), 'rename');
  247. // Close the java script and avoid the footer up
  248. $form->addElement('html', '</div>');
  249. // Button upload document
  250. $form->addButtonSend(get_lang('Upload file'), 'submitDocument');
  251. $form->addProgress('DocumentUpload', 'file');
  252. $fileExistsOption = api_get_setting('document_if_file_exists_option');
  253. $defaultFileExistsOption = 'rename';
  254. if (!empty($fileExistsOption)) {
  255. $defaultFileExistsOption = $fileExistsOption;
  256. }
  257. $defaults = [
  258. 'index_document' => 'checked="checked"',
  259. 'if_exists' => $defaultFileExistsOption,
  260. ];
  261. $form->setDefaults($defaults);
  262. $url = api_get_path(WEB_AJAX_PATH).
  263. 'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath='.$path.'&directory_parent_id='.(int) ($_REQUEST['id'] ?? 0);
  264. $multipleForm = new FormValidator(
  265. 'drag_drop',
  266. 'post',
  267. '#',
  268. ['enctype' => 'multipart/form-data']
  269. );
  270. $multipleForm->addMultipleUpload($url);
  271. $headers = [
  272. get_lang('Upload'),
  273. get_lang('Upload').' ('.get_lang('Simple').')',
  274. ];
  275. echo Display::tabs($headers, [$multipleForm->returnForm(), $form->returnForm()], 'tabs');
  276. Display::display_footer();