upload.php 11 KB

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