upload.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. /**
  35. * Code
  36. */
  37. // Name of the language file that needs to be included
  38. $language_file = array('document','gradebook');
  39. // Including the global initialization file
  40. //require_once '../inc/global.inc.php';
  41. // Including additional libraries
  42. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  43. require_once 'document.inc.php';
  44. // Adding extra javascript to the form
  45. $htmlHeadXtra[] = api_get_jquery_libraries_js(array('jquery-ui', 'jquery-upload'));
  46. $htmlHeadXtra[] = '<script>
  47. function check_unzip() {
  48. if (document.upload.unzip.checked){
  49. document.upload.if_exists[0].disabled=true;
  50. document.upload.if_exists[1].checked=true;
  51. document.upload.if_exists[2].disabled=true;
  52. } else {
  53. document.upload.if_exists[0].checked=true;
  54. document.upload.if_exists[0].disabled=false;
  55. document.upload.if_exists[2].disabled=false;
  56. }
  57. }
  58. function setFocus(){
  59. $("#title_file").focus();
  60. }
  61. </script>';
  62. $htmlHeadXtra[] = "
  63. <script>
  64. $(function () {
  65. setFocus();
  66. $('#file_upload').fileUploadUI({
  67. uploadTable: $('.files'),
  68. downloadTable: $('.files'),
  69. buildUploadRow: function (files, index) {
  70. $('.files').show();
  71. return $('<tr><td>' + files[index].name + '<\/td>' +
  72. '<td class=\"file_upload_progress\"><div><\/div><\/td>' +
  73. '<td class=\"file_upload_cancel\">' +
  74. '<button class=\"ui-state-default ui-corner-all\" title=\"".get_lang('Cancel')."\">' + '<span class=\"ui-icon ui-icon-cancel\">".get_lang('Cancel')."<\/span>' +'<\/button>'+
  75. '<\/td><\/tr>');
  76. },
  77. buildDownloadRow: function (file) {
  78. return $('<tr><td>' + file.name + '<\/td> <td> ' + file.size + ' <\/td> <td>&nbsp;' + file.result + ' <\/td> <\/tr>');
  79. }
  80. });
  81. $('#tabs').tabs();
  82. });
  83. </script>";
  84. // Variables
  85. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  86. $_course = api_get_course_info();
  87. $courseDir = $_course['path'].'/document';
  88. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  89. $base_work_dir = $sys_course_path.$courseDir;
  90. $selectcat = isset($_GET['selectcat']) ? Security::remove_XSS($_GET['selectcat']) : null;
  91. $document_data = DocumentManager::get_document_data_by_id($_REQUEST['id'], api_get_course_id(), true);
  92. if (empty($document_data)) {
  93. $document_id = $parent_id = 0;
  94. $path = '/';
  95. } else {
  96. $document_id = $document_data['id'];
  97. $path = $document_data['path'];
  98. $parent_id = DocumentManager::get_document_id(api_get_course_info(), dirname($path));
  99. }
  100. $group_properties = array();
  101. // This needs cleaning!
  102. if (api_get_group_id()) {
  103. // If the group id is set, check if the user has the right to be here
  104. // Get group info
  105. $group_properties = GroupManager::get_group_properties(api_get_group_id());
  106. if ($is_allowed_to_edit || GroupManager::is_user_in_group($_user['user_id'], api_get_group_id())) { // Only courseadmin or group members allowed
  107. $to_group_id = api_get_group_id();
  108. $req_gid = '&amp;gidReq='.api_get_group_id();
  109. $interbreadcrumb[] = array('url' => '../group/group_space.php?gidReq='.api_get_group_id(), 'name' => get_lang('GroupSpace'));
  110. } else {
  111. api_not_allowed(true);
  112. }
  113. } elseif ($is_allowed_to_edit || is_my_shared_folder(api_get_user_id(), $path, api_get_session_id())) {
  114. // Admin for "regular" upload, no group documents. And check if is my shared folder
  115. $to_group_id = 0;
  116. $req_gid = '';
  117. } else { // No course admin and no group member...
  118. api_not_allowed(true);
  119. }
  120. // Group docs can only be uploaded in the group directory
  121. if ($to_group_id != 0 && $path == '/') {
  122. $path = $group_properties['directory'];
  123. }
  124. // I'm in the certification module?
  125. $is_certificate_mode = false;
  126. $is_certificate_array = explode('/', $path);
  127. array_shift($is_certificate_array);
  128. if ($is_certificate_array[0] == 'certificates') {
  129. $is_certificate_mode = true;
  130. }
  131. // Title of the tool
  132. $add_group_to_title = null;
  133. if ($to_group_id != 0) { // Add group name after for group documents
  134. $add_group_to_title = ' ('.$group_properties['name'].')';
  135. }
  136. if (isset($_REQUEST['certificate'])) {
  137. $nameTools = get_lang('UploadCertificate').$add_group_to_title;
  138. } else {
  139. $nameTools = get_lang('UplUploadDocument').$add_group_to_title;
  140. }
  141. // Breadcrumbs
  142. if ($is_certificate_mode) {
  143. $interbreadcrumb[] = array('url' => '../gradebook/'.$_SESSION['gradebook_dest'], 'name' => get_lang('Gradebook'));
  144. } else {
  145. $interbreadcrumb[] = array('url' => './document.php?id='.$document_id.$req_gid, 'name'=> get_lang('Documents'));
  146. }
  147. // Interbreadcrumb for the current directory root path
  148. if (empty($document_data['parents'])) {
  149. $interbreadcrumb[] = array('url' => '#', 'name' => $document_data['title']);
  150. } else {
  151. foreach($document_data['parents'] as $document_sub_data) {
  152. $interbreadcrumb[] = array('url' => $document_sub_data['document_url'], 'name' => $document_sub_data['title']);
  153. }
  154. }
  155. $this_section = SECTION_COURSES;
  156. $interbreadcrumb[] = array('url' => '#', 'name' => $nameTools);
  157. // Display the header
  158. Display::display_header($nameTools, 'Doc');
  159. /* Here we do all the work */
  160. // User has submitted a file
  161. if (!empty($_FILES)) {
  162. DocumentManager::upload_document($_FILES, $_POST['curdirpath'], $_POST['title'], $_POST['comment'], $_POST['unzip'], $_POST['if_exists'], $_POST['index_document'], true);
  163. }
  164. // Actions
  165. echo '<div class="actions">';
  166. // Link back to the documents overview
  167. if ($is_certificate_mode) {
  168. //echo '<a href="document.php?id='.$document_id.'&selectcat=' . $selectcat.'">'.Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('CertificateOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  169. } else {
  170. //echo '<a href="document.php?id='.$document_id.'">'.Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('DocumentsOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  171. }
  172. // Link to create a folder
  173. /*
  174. if (!isset($_GET['createdir']) && !is_my_shared_folder($_user['user_id'], $path, api_get_session_id()) && !$is_certificate_mode) {
  175. echo '<a href="'.api_get_self().'?path='.$path.'&amp;createdir=1">'.Display::return_icon('new_folder.png', get_lang('CreateDir'),'',ICON_SIZE_MEDIUM).'</a>';
  176. }*/
  177. echo '</div>';
  178. // Form to select directory
  179. $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit);
  180. if (!$is_certificate_mode) {
  181. echo build_directory_selector($folders, $document_id, (isset($group_properties['directory']) ? $group_properties['directory'] : array()));
  182. }
  183. $action = api_get_self().'?'.api_get_cidreq().'&id='.$document_id;
  184. $form = new FormValidator('upload', 'POST', $action.'#tabs-2', '', 'enctype="multipart/form-data"');
  185. $form->addElement('hidden', 'id', $document_id);
  186. $form->addElement('hidden', 'curdirpath', $path);
  187. $course_quota = Text::format_file_size(DocumentManager::get_course_quota() - DocumentManager::documents_total_space());
  188. $label = get_lang('MaxFileSize').': '.ini_get('upload_max_filesize').'<br/>'.get_lang('DocumentQuota').': '.$course_quota;
  189. $form->addElement('file', 'file', array(get_lang('File'), $label), 'style="width: 250px" id="user_upload"');
  190. $form->addElement('text', 'title', get_lang('Title'), array('size' => '20', 'style' => 'width:300px', 'id' => 'title_file'));
  191. $form->addElement('textarea', 'comment', get_lang('Comment'), 'wrap="virtual" style="width:300px;"');
  192. // Advanced parameters
  193. $form->addElement('label', null, Display::url(get_lang('AdvancedParameters'), '#', array('id' => 'upload_settings', 'class' => 'advanced_options')));
  194. $form->addElement('html', '<div id="upload_settings_options" style="display:none">');
  195. // Check box options
  196. $form->addElement('checkbox', 'unzip', get_lang('Options'), get_lang('Uncompress'), 'onclick="javascript: check_unzip();" value="1"');
  197. if (api_get_setting('search_enabled') == 'true') {
  198. //TODO: include language file
  199. $supported_formats = get_lang('SupportedFormatsForIndex').': HTML, PDF, TXT, PDF, Postscript, MS Word, RTF, MS Power Point';
  200. $form->addElement('checkbox', 'index_document', '', get_lang('SearchFeatureDoIndexDocument').'<div style="font-size: 80%" >'.$supported_formats.'</div>');
  201. $form->addElement('html', '<br /><div class="sub-form">');
  202. $form->addElement('html', '<div class="label">'.get_lang('SearchFeatureDocumentLanguage').'</div>');
  203. $form->addElement('html', '<div>'.api_get_languages_combo(null,false).'</div>');
  204. $form->addElement('html', '</div><div class="sub-form">');
  205. $specific_fields = get_specific_field_list();
  206. foreach ($specific_fields as $specific_field) {
  207. $form->addElement('text', $specific_field['code'], $specific_field['name'].' : ');
  208. }
  209. $form->addElement('html', '</div>');
  210. }
  211. $form->addElement('radio', 'if_exists', get_lang('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'nothing');
  212. $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'overwrite');
  213. $form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'rename');
  214. // Close the java script and avoid the footer up
  215. $form -> addElement('html', '</div>');
  216. // Button upload document
  217. $form->addElement('style_submit_button', 'submitDocument', get_lang('SendDocument'), 'class="upload"');
  218. $form->add_real_progress_bar('DocumentUpload', 'file');
  219. $defaults = array('index_document' => 'checked="checked"');
  220. $form->setDefaults($defaults);
  221. $simple_form = $form->return_form();
  222. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?a=upload_file';
  223. $multiple_form = get_lang('ClickToSelectOrDragAndDropMultipleFilesOnTheUploadField').'<br />';
  224. //Adding icon replace the <div>'.get_lang('UploadFiles').'</div> with this:
  225. //<div style="width:50%;margin:0 auto;"> '.Display::div(Display::return_icon('folder_document.png', '', array(), 64), array('style'=>'float:left')).' '.get_lang('UploadFiles').'</div>
  226. $multiple_form .= '
  227. <center>
  228. <form id="file_upload" action="'.$url.'" method="POST" enctype="multipart/form-data">
  229. <input type="hidden" name="curdirpath" value="'.$path.'" />
  230. <input type="file" name="file" multiple>
  231. <button type="submit">Upload</button>
  232. '.get_lang('UploadFiles').'
  233. </form>
  234. </center>
  235. <table style="display:none; width:50%" class="files data_table">
  236. <tr>
  237. <th>'.get_lang('FileName').'</th>
  238. <th>'.get_lang('Size').'</th>
  239. <th>'.get_lang('Status').'</th>
  240. </tr>
  241. </table>';
  242. $nav_info = api_get_navigator();
  243. if ($nav_info ['name'] == 'Internet Explorer') {
  244. echo $simple_form;
  245. } else {
  246. $headers = array(get_lang('Send') , get_lang('Send').' ('.get_lang('Simple').')');
  247. echo Display::tabs($headers, array($multiple_form, $simple_form),'tabs');
  248. }
  249. Display::display_footer();