upload.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 '../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(array('jquery-ui', 'jquery-upload'));
  40. $htmlHeadXtra[] = '<script>
  41. function check_unzip() {
  42. if (document.upload.unzip.checked){
  43. document.upload.if_exists[0].disabled=true;
  44. document.upload.if_exists[1].checked=true;
  45. document.upload.if_exists[2].disabled=true;
  46. } else {
  47. document.upload.if_exists[0].checked=true;
  48. document.upload.if_exists[0].disabled=false;
  49. document.upload.if_exists[2].disabled=false;
  50. }
  51. }
  52. function advanced_parameters() {
  53. if (document.getElementById(\'options\').style.display == \'none\') {
  54. document.getElementById(\'options\').style.display = \'block\';
  55. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_hide.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  56. } else {
  57. document.getElementById(\'options\').style.display = \'none\';
  58. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  59. }
  60. }
  61. function setFocus(){
  62. $("#title_file").focus();
  63. }
  64. </script>';
  65. $htmlHeadXtra[] = "
  66. <script>
  67. $(function () {
  68. setFocus();
  69. $('#file_upload').fileUploadUI({
  70. uploadTable: $('.files'),
  71. downloadTable: $('.files'),
  72. buildUploadRow: function (files, index) {
  73. $('.files').show();
  74. return $('<tr><td>' + files[index].name + '<\/td>' +
  75. '<td class=\"file_upload_progress\"><div><\/div><\/td>' +
  76. '<td class=\"file_upload_cancel\">' +
  77. '<button class=\"ui-state-default ui-corner-all\" title=\"".get_lang('Cancel')."\">' + '<span class=\"ui-icon ui-icon-cancel\">".get_lang('Cancel')."<\/span>' +'<\/button>'+
  78. '<\/td><\/tr>');
  79. },
  80. buildDownloadRow: function (file) {
  81. return $('<tr><td>' + file.name + '<\/td> <td> ' + file.size + ' <\/td> <td>&nbsp;' + file.result + ' <\/td> <\/tr>');
  82. }
  83. });
  84. $('#tabs').tabs();
  85. });
  86. </script>";
  87. // Variables
  88. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  89. $_course = api_get_course_info();
  90. $groupId = api_get_group_id();
  91. $courseDir = $_course['path'].'/document';
  92. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  93. $base_work_dir = $sys_course_path.$courseDir;
  94. $sessionId = api_get_session_id();
  95. $selectcat = isset($_GET['selectcat']) ? Security::remove_XSS($_GET['selectcat']) : null;
  96. $document_data = DocumentManager::get_document_data_by_id(
  97. $_REQUEST['id'],
  98. api_get_course_id(),
  99. true,
  100. $sessionId
  101. );
  102. if ($sessionId != 0 && !$document_data) {
  103. $document_data = DocumentManager::get_document_data_by_id(
  104. $_REQUEST['id'],
  105. api_get_course_id(),
  106. true,
  107. 0
  108. );
  109. }
  110. if (empty($document_data)) {
  111. $document_id = $parent_id = 0;
  112. $path = '/';
  113. } else {
  114. $document_id = $document_data['id'];
  115. $path = $document_data['path'];
  116. $parent_id = DocumentManager::get_document_id(
  117. api_get_course_info(),
  118. dirname($path)
  119. );
  120. }
  121. $group_properties = array();
  122. // This needs cleaning!
  123. if (!empty($groupId)) {
  124. // If the group id is set, check if the user has the right to be here
  125. // Get group info
  126. $group_properties = GroupManager::get_group_properties($groupId);
  127. // Only courseadmin or group members allowed
  128. if ($is_allowed_to_edit || GroupManager::is_user_in_group(api_get_user_id(), $groupId)) {
  129. $interbreadcrumb[] = array('url' => '../group/group_space.php?'.api_get_cidreq(), 'name' => get_lang('GroupSpace'));
  130. } else {
  131. api_not_allowed(true);
  132. }
  133. } elseif ($is_allowed_to_edit ||
  134. DocumentManager::is_my_shared_folder(api_get_user_id(), $path, api_get_session_id())) {
  135. } else {
  136. // No course admin and no group member...
  137. api_not_allowed(true);
  138. }
  139. // Group docs can only be uploaded in the group directory
  140. if ($groupId != 0 && $path == '/') {
  141. $path = $group_properties['directory'];
  142. }
  143. // I'm in the certification module?
  144. $is_certificate_mode = false;
  145. $is_certificate_array = explode('/', $path);
  146. array_shift($is_certificate_array);
  147. if ($is_certificate_array[0] == 'certificates') {
  148. $is_certificate_mode = true;
  149. }
  150. // Title of the tool
  151. $add_group_to_title = null;
  152. if ($groupId != 0) {
  153. // Add group name after for group documents
  154. $add_group_to_title = ' ('.$group_properties['name'].')';
  155. }
  156. if (isset($_REQUEST['certificate'])) {
  157. $nameTools = get_lang('UploadCertificate').$add_group_to_title;
  158. } else {
  159. $nameTools = get_lang('UplUploadDocument').$add_group_to_title;
  160. }
  161. // Breadcrumbs
  162. if ($is_certificate_mode) {
  163. $interbreadcrumb[] = array('url' => '../gradebook/'.$_SESSION['gradebook_dest'], 'name' => get_lang('Gradebook'));
  164. } else {
  165. $interbreadcrumb[] = array('url' => './document.php?id='.$document_id.'&'.api_get_cidreq(), 'name'=> get_lang('Documents'));
  166. }
  167. // Interbreadcrumb for the current directory root path
  168. if (empty($document_data['parents'])) {
  169. $interbreadcrumb[] = array('url' => '#', 'name' => $document_data['title']);
  170. } else {
  171. foreach ($document_data['parents'] as $document_sub_data) {
  172. $interbreadcrumb[] = array('url' => $document_sub_data['document_url'], 'name' => $document_sub_data['title']);
  173. }
  174. }
  175. $this_section = SECTION_COURSES;
  176. // Display the header
  177. Display::display_header($nameTools, 'Doc');
  178. /* Here we do all the work */
  179. $unzip = isset($_POST['unzip']) ? $_POST['unzip'] : null;
  180. $index = isset($_POST['index_document']) ? $_POST['index_document'] : null;
  181. // User has submitted a file
  182. if (!empty($_FILES)) {
  183. DocumentManager::upload_document(
  184. $_FILES,
  185. $_POST['curdirpath'],
  186. $_POST['title'],
  187. $_POST['comment'],
  188. $unzip,
  189. $_POST['if_exists'],
  190. $index,
  191. true
  192. );
  193. }
  194. // Actions
  195. echo '<div class="actions">';
  196. // Link back to the documents overview
  197. if ($is_certificate_mode) {
  198. echo '<a href="document.php?id='.$document_id.'&selectcat=' . $selectcat.'&'.api_get_cidreq().'">'.
  199. Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('CertificateOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  200. } else {
  201. echo '<a href="document.php?id='.$document_id.'&'.api_get_cidreq().'">'.
  202. Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('DocumentsOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  203. }
  204. // Link to create a folder
  205. echo '</div>';
  206. // Form to select directory
  207. $folders = DocumentManager::get_all_document_folders($_course, $groupId, $is_allowed_to_edit);
  208. if (!$is_certificate_mode) {
  209. echo DocumentManager::build_directory_selector(
  210. $folders,
  211. $document_id,
  212. (isset($group_properties['directory']) ? $group_properties['directory'] : array())
  213. );
  214. }
  215. $action = api_get_self().'?'.api_get_cidreq().'&id='.$document_id;
  216. $form = new FormValidator('upload', 'POST', $action.'#tabs-2', '', 'enctype="multipart/form-data"');
  217. $form->addElement('hidden', 'id', $document_id);
  218. $form->addElement('hidden', 'curdirpath', $path);
  219. $course_quota = format_file_size(DocumentManager::get_course_quota() - DocumentManager::documents_total_space());
  220. $label = get_lang('MaxFileSize').': '.ini_get('upload_max_filesize').'<br/>'.get_lang('DocumentQuota').': '.$course_quota;
  221. $form->addElement('file', 'file', array(get_lang('File'), $label), 'style="width: 250px" id="user_upload"');
  222. $form->addElement('text', 'title', get_lang('Title'), array('size' => '20', 'style' => 'width:300px', 'id' => 'title_file'));
  223. $form->addElement('textarea', 'comment', get_lang('Comment'), 'wrap="virtual" style="width:300px;"');
  224. $advanced = '<a href="javascript://" onclick=" return advanced_parameters()"><span id="img_plus_and_minus"><div style="vertical-align:top;" ><img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'</div></span></a>';
  225. // Advanced parameters
  226. $form->addElement('advanced_settings', $advanced);
  227. $form->addElement('html', '<div id="options" style="display:none">');
  228. // Check box options
  229. $form->addElement('checkbox', 'unzip', get_lang('Options'), get_lang('Uncompress'), 'onclick="javascript: check_unzip();" value="1"');
  230. if (api_get_setting('search_enabled') == 'true') {
  231. //TODO: include language file
  232. $supported_formats = get_lang('SupportedFormatsForIndex').': HTML, PDF, TXT, PDF, Postscript, MS Word, RTF, MS Power Point';
  233. $form->addElement('checkbox', 'index_document', '', get_lang('SearchFeatureDoIndexDocument').'<div style="font-size: 80%" >'.$supported_formats.'</div>');
  234. $form->addElement('html', '<br /><div class="sub-form">');
  235. $form->addElement('html', '<div class="label">'.get_lang('SearchFeatureDocumentLanguage').'</div>');
  236. $form->addElement('html', '<div>'.api_get_languages_combo(null,false).'</div>');
  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('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'nothing');
  245. $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'overwrite');
  246. $form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), '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('SendDocument'), 'submitDocument');
  251. $form->add_real_progress_bar('DocumentUpload', 'file');
  252. $fileExistsOption = api_get_configuration_value('document_if_file_exists_option');
  253. $defaultFileExistsOption = 'rename';
  254. if (!empty($fileExistsOption)) {
  255. $defaultFileExistsOption = $fileExistsOption;
  256. }
  257. $defaults = array(
  258. 'index_document' => 'checked="checked"',
  259. 'if_exists' => $defaultFileExistsOption
  260. );
  261. $form->setDefaults($defaults);
  262. $simple_form = $form->return_form();
  263. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file';
  264. $multiple_form = get_lang('ClickToSelectOrDragAndDropMultipleFilesOnTheUploadField').'<br />';
  265. $multiple_form .= '
  266. <div class="form-ajax">
  267. <form id="file_upload" action="'.$url.'" method="POST" enctype="multipart/form-data">
  268. <input type="hidden" name="curdirpath" value="'.$path.'" />
  269. <input type="file" name="file" multiple>
  270. <button type="submit">Upload</button>
  271. <div class="button-load">
  272. '.get_lang('UploadFiles').'
  273. </div>
  274. </form>
  275. </div>
  276. <table style="display:none; width:50%" class="files data_table">
  277. <tr>
  278. <th>'.get_lang('FileName').'</th>
  279. <th>'.get_lang('Size').'</th>
  280. <th>'.get_lang('Status').'</th>
  281. </tr>
  282. </table>';
  283. $nav_info = api_get_navigator();
  284. if ($nav_info ['name'] == 'Internet Explorer') {
  285. echo $simple_form;
  286. } else {
  287. $headers = array(get_lang('Send') , get_lang('Send').' ('.get_lang('Simple').')');
  288. echo Display::tabs($headers, array($multiple_form, $simple_form),'tabs');
  289. }
  290. Display::display_footer();