diff --git a/main/document/upload.php b/main/document/upload.php index b37144f4f7..4269d90cbf 100755 --- a/main/document/upload.php +++ b/main/document/upload.php @@ -204,7 +204,6 @@ $(function () { "; - // This needs cleaning! if (!empty($groupId)) { // If the group id is set, check if the user has the right to be here diff --git a/main/dropbox/dropbox_functions.inc.php b/main/dropbox/dropbox_functions.inc.php index 8fd01619cf..4944bd0404 100755 --- a/main/dropbox/dropbox_functions.inc.php +++ b/main/dropbox/dropbox_functions.inc.php @@ -222,9 +222,9 @@ function store_move($id, $target, $part) $dropbox_cnf = getDropboxConf(); $course_id = api_get_course_int_id(); - if ((isset($id) AND $id != '') AND - (isset($target) AND $target != '') AND - (isset($part) AND $part != '') + if ((isset($id) && $id != '') && + (isset($target) && $target != '') && + (isset($part) && $part != '') ) { if ($part == 'received') { @@ -469,7 +469,7 @@ function display_addcategory_form($category_name = '', $id = '', $action) $course_id = api_get_course_int_id(); $title = get_lang('AddNewCategory'); - if (isset($id) AND $id != '') { + if (isset($id) && $id != '') { // retrieve the category we are editing $sql = "SELECT * FROM ".$dropbox_cnf['tbl_category']." WHERE c_id = $course_id AND cat_id = ".intval($id).""; @@ -693,7 +693,53 @@ function display_add_form($dropbox_unid, $viewReceivedCategory, $viewSentCategor ) ); $form->addButtonUpload(get_lang('Upload'), 'submitWork'); - $form->display(); + + + $headers = array( + get_lang('Upload'), + get_lang('Upload').' ('.get_lang('Simple').')', + ); + + $multipleForm = new FormValidator( + 'sent_multiple', + 'post', + '#', + null, + array('enctype' => 'multipart/form-data', 'id' => 'fileupload') + ); + + $multipleForm->addSelect( + 'recipients', + get_lang('SendTo'), + $options, + array( + 'multiple' => 'multiple', + 'size' => '10', + 'id' => 'recipient_form' + ) + ); + + $multipleForm->addHtml(' + + '); + + echo Display::tabs($headers, array($multipleForm->returnForm(), $form->returnForm()), 'tabs'); } /** @@ -863,14 +909,20 @@ function dropbox_cnf($variable) } /** + * @param array $file + * * @return array|null|string */ -function store_add_dropbox() +function store_add_dropbox($file = []) { $_course = api_get_course_info(); $_user = api_get_user_info(); $dropbox_cnf = getDropboxConf(); + if (empty($file)) { + $file = isset($_FILES['file']) ? $_FILES['file'] : null; + } + // Validating the form data // there are no recipients selected @@ -886,28 +938,34 @@ function store_add_dropbox() } elseif ($rec == 'upload') { $thisIsJustUpload = true; } elseif (strpos($rec, 'user_') === 0 && !isCourseMember(substr($rec, strlen('user_')))) { - return get_lang('InvalideUserDetected'); + Display::addFlash(Display::return_message(get_lang('InvalideUserDetected'), 'warning')); + return false; } elseif (strpos($rec, 'group_') !== 0 && strpos($rec, 'user_') !== 0) { - return get_lang('InvalideGroupDetected'); + Display::addFlash(Display::return_message(get_lang('InvalideGroupDetected'), 'warning')); + return false; } } } // we are doing a mailing but an additional recipient is selected if ($thisIsAMailing && (count($_POST['recipients']) != 1)) { - return get_lang('MailingSelectNoOther'); + Display::addFlash(Display::return_message(get_lang('MailingSelectNoOther'), 'warning')); + + return false; } // we are doing a just upload but an additional recipient is selected. // note: why can't this be valid? It is like sending a document to // yourself AND to a different person (I do this quite often with my e-mails) if ($thisIsJustUpload && (count($_POST['recipients']) != 1)) { - return get_lang('MailingJustUploadSelectNoOther'); + + Display::addFlash(Display::return_message(get_lang('MailingJustUploadSelectNoOther'), 'warning')); + return false; } - if (empty($_FILES['file']['name'])) { - $error = true; - return get_lang('NoFileSpecified'); + if (empty($file['name'])) { + Display::addFlash(Display::return_message(get_lang('NoFileSpecified'), 'warning')); + return false; } // are we overwriting a previous file or sending a new one @@ -919,23 +977,26 @@ function store_add_dropbox() // doing the upload - $dropbox_filename = $_FILES['file']['name']; - $dropbox_filesize = $_FILES['file']['size']; - $dropbox_filetype = $_FILES['file']['type']; - $dropbox_filetmpname = $_FILES['file']['tmp_name']; + $dropbox_filename = $file['name']; + $dropbox_filesize = $file['size']; + $dropbox_filetype = $file['type']; + $dropbox_filetmpname = $file['tmp_name']; // check if the filesize does not exceed the allowed size. if ($dropbox_filesize <= 0 || $dropbox_filesize > $dropbox_cnf['maxFilesize']) { - return get_lang('DropboxFileTooBig'); - // TODO: The "too big" message does not fit in the case of uploading zero-sized file. + Display::addFlash(Display::return_message(get_lang('DropboxFileTooBig'), 'warning')); + + return false; } // check if the file is actually uploaded if (!is_uploaded_file($dropbox_filetmpname)) { // check user fraud : no clean error msg. - return get_lang('TheFileIsNotUploaded'); + Display::addFlash(Display::return_message(get_lang('TheFileIsNotUploaded'), 'warning')); + + return false; } - $upload_ok = process_uploaded_file($_FILES['file'], true); + $upload_ok = process_uploaded_file($file, true); if (!$upload_ok) { return null; @@ -950,7 +1011,8 @@ function store_add_dropbox() //filter extension if (!filter_extension($dropbox_filename)) { - return get_lang('UplUnableToSaveFileFilteredExtension'); + Display::addFlash(Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'warning')); + return false; } // set title @@ -973,10 +1035,13 @@ function store_add_dropbox() foreach ($dropbox_person->sentWork as $w) { if ($w->title == $dropbox_filename) { if (($w->recipients[0]['id'] > dropbox_cnf('mailingIdBase')) xor $thisIsAMailing) { - return get_lang('MailingNonMailingError'); + Display::addFlash(Display::return_message(get_lang('MailingNonMailingError'), 'warning')); + return false; } if (($w->recipients[0]['id'] == $_user['user_id']) xor $thisIsJustUpload) { - return get_lang('MailingJustUploadSelectNoOther'); + + Display::addFlash(Display::return_message(get_lang('MailingJustUploadSelectNoOther'), 'warning')); + return false; } $dropbox_filename = $w->filename; $found = true; // note: do we still need this? @@ -1046,7 +1111,7 @@ function store_add_dropbox() } } - new Dropbox_SentWork( + $result = new Dropbox_SentWork( $_user['user_id'], $dropbox_title, isset($_POST['description']) ? $_POST['description'] : '', @@ -1057,11 +1122,13 @@ function store_add_dropbox() ); Security::clear_token(); - return get_lang('FileUploadSucces'); + Display::addFlash(Display::return_message(get_lang('FileUploadSucces'))); + + return $result; } /** -* this function transforms the array containing all the feedback into something visually attractive. +* Transforms the array containing all the feedback into something visually attractive. * * @param an array containing all the feedback about the given message. * diff --git a/main/dropbox/dropbox_init.inc.php b/main/dropbox/dropbox_init.inc.php index c3bd9fefb6..820bb2850b 100755 --- a/main/dropbox/dropbox_init.inc.php +++ b/main/dropbox/dropbox_init.inc.php @@ -113,15 +113,10 @@ Version 1.4 (Yannick Warnier) * @package chamilo.dropbox */ -/** - * Code - */ -/* INIT SECTION */ - use ChamiloSession as Session; // including the basic Chamilo initialisation file -require_once '../inc/global.inc.php'; +require_once __DIR__.'/../inc/global.inc.php'; $is_allowed_in_course = api_is_allowed_in_course(); $is_courseTutor = api_is_course_tutor(); $is_courseAdmin = api_is_course_admin(); @@ -135,7 +130,6 @@ Session::write('dropbox_conf', $dropbox_cnf); // the dropbox file that contains additional functions require_once 'dropbox_functions.inc.php'; - // protecting the script api_protect_course_script(); @@ -182,39 +176,39 @@ if ($action == 'add') { } /* Create javascript and htmlHeaders */ -$javascript = ""; + } else { header('location: index.php?view='.$view.'&error=Error'); exit; diff --git a/main/dropbox/index.php b/main/dropbox/index.php index 5ba1c45d7a..57a45631a3 100755 --- a/main/dropbox/index.php +++ b/main/dropbox/index.php @@ -63,10 +63,7 @@ if ($action == 'add') { if (isset($_POST['submitWork'])) { $check = Security::check_token(); if ($check) { - $message = store_add_dropbox(); - if (!empty($message)) { - Display :: display_confirmation_message($message); - } + store_add_dropbox(); } } diff --git a/main/inc/ajax/document.ajax.php b/main/inc/ajax/document.ajax.php index 6968643af4..524a1944d2 100755 --- a/main/inc/ajax/document.ajax.php +++ b/main/inc/ajax/document.ajax.php @@ -62,27 +62,24 @@ switch ($action) { ); $json = array(); - $json['name'] = Display::url( - api_htmlentities($result['title']), - api_htmlentities($result['url']), - array('target'=>'_blank') - ); - - $json['url'] = $result['url']; + if (!empty($result) && is_array($result)) { + $json['name'] = Display::url( + api_htmlentities($result['title']), + api_htmlentities($result['url']), + array('target'=>'_blank') + ); - $json['size'] = format_file_size($file['size']); - $json['type'] = api_htmlentities($file['type']); + $json['url'] = $result['url']; + $json['size'] = format_file_size($file['size']); + $json['type'] = api_htmlentities($file['type']); - if (!empty($result) && is_array($result)) { $json['result'] = Display::return_icon( 'accept.png', get_lang('Uploaded') ); } else { - $json['result'] = Display::return_icon( - 'exclamation.png', - get_lang('Error') - ); + $json['url'] = ''; + $json['error'] = get_lang('Error'); } $resultList[] = $json; } diff --git a/main/inc/ajax/dropbox.ajax.php b/main/inc/ajax/dropbox.ajax.php new file mode 100644 index 0000000000..9a8de3e00c --- /dev/null +++ b/main/inc/ajax/dropbox.ajax.php @@ -0,0 +1,74 @@ + get_lang('YouMustSelectAtLeastOneDestinee')]; + echo json_encode(['files' => $resultList]); + exit; + } + + if (!empty($_FILES)) { + $files = $_FILES['files']; + $fileList = []; + foreach ($files as $name => $array) { + $counter = 0; + foreach ($array as $data) { + $fileList[$counter][$name] = $data; + $counter++; + } + } + + $resultList = []; + foreach ($fileList as $file) { + $globalFile = []; + $globalFile['files'] = $file; + /** @var Dropbox_SentWork $result */ + $result = store_add_dropbox($file); + + $json = array(); + + if (!empty($result)) { + + $json['name'] = Display::url( + api_htmlentities($result->title), + api_htmlentities(api_get_path(WEB_CODE_PATH).'dropbox/index.php?'.api_get_cidreq()), + array('target' => '_blank') + ); + + $json['url'] = api_get_path(WEB_CODE_PATH).'dropbox/index.php?'.api_get_cidreq(); + $json['size'] = format_file_size($result->filesize); + $json['type'] = api_htmlentities($file['type']); + $json['result'] = Display::return_icon( + 'accept.png', + get_lang('Uploaded') + ); + } else { + $json['result'] = Display::return_icon( + 'exclamation.png', + get_lang('Error') + ); + } + $resultList[] = $json; + } + + echo json_encode(['files' => $resultList]); + } + exit; + break; +} +exit; diff --git a/main/inc/ajax/work.ajax.php b/main/inc/ajax/work.ajax.php index 3a15685ad0..db4738cb60 100755 --- a/main/inc/ajax/work.ajax.php +++ b/main/inc/ajax/work.ajax.php @@ -44,27 +44,23 @@ switch ($action) { $result = processWorkForm($workInfo, $values, $courseInfo, $sessionId, 0, $userId, $file); $json = array(); - $json['name'] = Display::url( - api_htmlentities($result['title']), - api_htmlentities($result['view_url']), - array('target' => '_blank') - ); - - $json['url'] = $result['view_url']; - //$json['size'] = format_file_size($result['filesize']); - $json['size'] = ''; - $json['type'] = api_htmlentities($result['filetype']); - if (!empty($result) && is_array($result) && empty($result['error'])) { + $json['name'] = Display::url( + api_htmlentities($result['title']), + api_htmlentities($result['view_url']), + array('target' => '_blank') + ); + + $json['url'] = $result['view_url']; + $json['size'] = ''; + $json['type'] = api_htmlentities($result['filetype']); $json['result'] = Display::return_icon( 'accept.png', get_lang('Uploaded') ); } else { - $json['result'] = Display::return_icon( - 'exclamation.png', - get_lang('Error') - ); + $json['url'] = ''; + $json['error'] = get_lang('Error'); } $resultList[] = $json; } diff --git a/main/work/upload.php b/main/work/upload.php index e94ddc08a7..73a3b37621 100755 --- a/main/work/upload.php +++ b/main/work/upload.php @@ -120,8 +120,7 @@ $url = api_get_path(WEB_AJAX_PATH).'work.ajax.php?'.api_get_cidreq().'&a=upload_ $htmlHeadXtra[] = api_get_jquery_libraries_js(array('jquery-ui', 'jquery-upload')); $htmlHeadXtra[] = to_javascript_work(); -$htmlHeadXtra[] = " -