upload.php 12 KB

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